Use netmsg_service_sync() to protect vlan_input_p
[dragonfly.git] / sys / net / if_ethersubr.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
b327296f 35 * $DragonFly: src/sys/net/if_ethersubr.c,v 1.91 2008/09/23 11:50:11 sephe Exp $
984263bc
MD
36 */
37
38#include "opt_atalk.h"
39#include "opt_inet.h"
40#include "opt_inet6.h"
41#include "opt_ipx.h"
9b42cabe 42#include "opt_mpls.h"
984263bc 43#include "opt_netgraph.h"
0d16ba1d 44#include "opt_carp.h"
984263bc
MD
45
46#include <sys/param.h>
47#include <sys/systm.h>
68b67450 48#include <sys/globaldata.h>
984263bc 49#include <sys/kernel.h>
f3e0b5f0 50#include <sys/ktr.h>
984263bc
MD
51#include <sys/malloc.h>
52#include <sys/mbuf.h>
68b67450 53#include <sys/msgport.h>
984263bc
MD
54#include <sys/socket.h>
55#include <sys/sockio.h>
56#include <sys/sysctl.h>
68b67450
SZ
57#include <sys/thread.h>
58#include <sys/thread2.h>
984263bc
MD
59
60#include <net/if.h>
61#include <net/netisr.h>
62#include <net/route.h>
63#include <net/if_llc.h>
64#include <net/if_dl.h>
65#include <net/if_types.h>
4d723e5a 66#include <net/ifq_var.h>
984263bc
MD
67#include <net/bpf.h>
68#include <net/ethernet.h>
e6b5847c 69#include <net/vlan/if_vlan_ether.h>
29bc1092 70#include <net/netmsg2.h>
984263bc
MD
71
72#if defined(INET) || defined(INET6)
73#include <netinet/in.h>
74#include <netinet/in_var.h>
75#include <netinet/if_ether.h>
1f2de5d4
MD
76#include <net/ipfw/ip_fw.h>
77#include <net/dummynet/ip_dummynet.h>
984263bc
MD
78#endif
79#ifdef INET6
80#include <netinet6/nd6.h>
81#endif
82
0d16ba1d
MD
83#ifdef CARP
84#include <netinet/ip_carp.h>
85#endif
86
984263bc 87#ifdef IPX
d2438d69
MD
88#include <netproto/ipx/ipx.h>
89#include <netproto/ipx/ipx_if.h>
32211831 90int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
f23061d4
JH
91int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
92 short *tp, int *hlen);
984263bc
MD
93#endif
94
95#ifdef NS
96#include <netns/ns.h>
97#include <netns/ns_if.h>
98ushort ns_nettype;
99int ether_outputdebug = 0;
100int ether_inputdebug = 0;
101#endif
102
103#ifdef NETATALK
d2438d69
MD
104#include <netproto/atalk/at.h>
105#include <netproto/atalk/at_var.h>
106#include <netproto/atalk/at_extern.h>
984263bc 107
f23061d4
JH
108#define llc_snap_org_code llc_un.type_snap.org_code
109#define llc_snap_ether_type llc_un.type_snap.ether_type
984263bc
MD
110
111extern u_char at_org_code[3];
112extern u_char aarp_org_code[3];
113#endif /* NETATALK */
114
9b42cabe
NA
115#ifdef MPLS
116#include <netproto/mpls/mpls.h>
117#endif
118
984263bc 119/* netgraph node hooks for ng_ether(4) */
601fa0f9 120void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
984263bc 121void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
36f48e5e 122 struct mbuf *m, const struct ether_header *eh);
984263bc
MD
123int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
124void (*ng_ether_attach_p)(struct ifnet *ifp);
125void (*ng_ether_detach_p)(struct ifnet *ifp);
126
50098e2e 127void (*vlan_input_p)(struct mbuf *);
3013ac0e 128
5fe66e68
JH
129static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
130 struct rtentry *);
9b77ea6e
SZ
131static void ether_restore_header(struct mbuf **, const struct ether_header *,
132 const struct ether_header *);
984263bc 133
f23061d4 134/*
a8d45119 135 * if_bridge support
f23061d4 136 */
db37145f 137struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
eb366364 138int (*bridge_output_p)(struct ifnet *, struct mbuf *);
db37145f
SS
139void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
140
5fe66e68
JH
141static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
142 struct sockaddr *);
143
144const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
c401f0fd
JS
145 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
146};
147
5fe66e68 148#define gotoerr(e) do { error = (e); goto bad; } while (0)
f23061d4 149#define IFP2AC(ifp) ((struct arpcom *)(ifp))
984263bc 150
5fe66e68 151static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
7c5bb821 152 struct ip_fw **rule,
90ca9293 153 const struct ether_header *eh);
5fe66e68 154
984263bc 155static int ether_ipfw;
9b77ea6e
SZ
156static u_int ether_restore_hdr;
157static u_int ether_prepend_hdr;
158
5fe66e68
JH
159SYSCTL_DECL(_net_link);
160SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
161SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
162 &ether_ipfw, 0, "Pass ether pkts through firewall");
9b77ea6e
SZ
163SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
164 &ether_restore_hdr, 0, "# of ether header restoration");
165SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
166 &ether_prepend_hdr, 0,
167 "# of ether header restoration which prepends mbuf");
984263bc 168
f3e0b5f0
SZ
169#define ETHER_KTR_STR "ifp=%p"
170#define ETHER_KTR_ARG_SIZE (sizeof(void *))
171#ifndef KTR_ETHERNET
172#define KTR_ETHERNET KTR_ALL
173#endif
174KTR_INFO_MASTER(ether);
175KTR_INFO(KTR_ETHERNET, ether, chain_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
176KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
177KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
178KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
179#define logether(name, arg) KTR_LOG(ether_ ## name, arg)
180
984263bc
MD
181/*
182 * Ethernet output routine.
183 * Encapsulate a packet of type family for the local net.
184 * Use trailer local net encapsulation if enough data in first
185 * packet leaves a multiple of 512 bytes of data in remainder.
186 * Assumes that ifp is actually pointer to arpcom structure.
187 */
3013ac0e
JS
188static int
189ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
f23061d4 190 struct rtentry *rt)
984263bc 191{
f23061d4
JH
192 struct ether_header *eh, *deh;
193 u_char *edst;
984263bc 194 int loop_copy = 0;
f23061d4 195 int hlen = ETHER_HDR_LEN; /* link layer header length */
984263bc 196 struct arpcom *ac = IFP2AC(ifp);
f23061d4 197 int error;
984263bc 198
57dff79c
SZ
199 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
200
3a593c54
MD
201 if (ifp->if_flags & IFF_MONITOR)
202 gotoerr(ENETDOWN);
f23061d4
JH
203 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
204 gotoerr(ENETDOWN);
205
206 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
207 if (m == NULL)
5fe66e68 208 return (ENOBUFS);
f23061d4
JH
209 eh = mtod(m, struct ether_header *);
210 edst = eh->ether_dhost;
211
5fe66e68
JH
212 /*
213 * Fill in the destination ethernet address and frame type.
214 */
984263bc
MD
215 switch (dst->sa_family) {
216#ifdef INET
217 case AF_INET:
f23061d4 218 if (!arpresolve(ifp, rt, m, dst, edst))
984263bc 219 return (0); /* if not yet resolved */
cb8d752c
NA
220#ifdef MPLS
221 if (m->m_flags & M_MPLSLABELED)
222 eh->ether_type = htons(ETHERTYPE_MPLS);
223 else
224#endif
225 eh->ether_type = htons(ETHERTYPE_IP);
984263bc
MD
226 break;
227#endif
228#ifdef INET6
229 case AF_INET6:
f23061d4 230 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
5fe66e68 231 return (0); /* Something bad happenned. */
f23061d4 232 eh->ether_type = htons(ETHERTYPE_IPV6);
984263bc
MD
233 break;
234#endif
235#ifdef IPX
236 case AF_IPX:
f23061d4
JH
237 if (ef_outputp != NULL) {
238 error = ef_outputp(ifp, &m, dst, &eh->ether_type,
239 &hlen);
240 if (error)
241 goto bad;
242 } else {
243 eh->ether_type = htons(ETHERTYPE_IPX);
244 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
245 edst, ETHER_ADDR_LEN);
246 }
984263bc
MD
247 break;
248#endif
249#ifdef NETATALK
f23061d4
JH
250 case AF_APPLETALK: {
251 struct at_ifaddr *aa;
252
253 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
254 error = 0; /* XXX */
255 goto bad;
256 }
257 /*
258 * In the phase 2 case, need to prepend an mbuf for
259 * the llc header. Since we must preserve the value
260 * of m, which is passed to us by value, we m_copy()
261 * the first mbuf, and use it for our llc header.
262 */
263 if (aa->aa_flags & AFA_PHASE2) {
264 struct llc llc;
265
266 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
5fe66e68
JH
267 eh = mtod(m, struct ether_header *);
268 edst = eh->ether_dhost;
f23061d4
JH
269 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
270 llc.llc_control = LLC_UI;
271 bcopy(at_org_code, llc.llc_snap_org_code,
272 sizeof at_org_code);
273 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
5fe66e68
JH
274 bcopy(&llc,
275 mtod(m, caddr_t) + sizeof(struct ether_header),
276 sizeof(struct llc));
f23061d4
JH
277 eh->ether_type = htons(m->m_pkthdr.len);
278 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
279 } else {
280 eh->ether_type = htons(ETHERTYPE_AT);
281 }
282 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
283 return (0);
284 break;
984263bc 285 }
5fe66e68 286#endif
984263bc
MD
287#ifdef NS
288 case AF_NS:
f23061d4 289 switch(ns_nettype) {
984263bc 290 default:
f23061d4
JH
291 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
292 eh->ether_type = 0x8137;
984263bc 293 break;
f23061d4
JH
294 case 0x0: /* Novell 802.3 */
295 eh->ether_type = htons(m->m_pkthdr.len);
984263bc 296 break;
f23061d4
JH
297 case 0xe0e0: /* Novell 802.2 and Token-Ring */
298 M_PREPEND(m, 3, MB_DONTWAIT);
5fe66e68
JH
299 eh = mtod(m, struct ether_header *);
300 edst = eh->ether_dhost;
f23061d4 301 eh->ether_type = htons(m->m_pkthdr.len);
5fe66e68 302 cp = mtod(m, u_char *) + sizeof(struct ether_header);
984263bc
MD
303 *cp++ = 0xE0;
304 *cp++ = 0xE0;
305 *cp++ = 0x03;
306 break;
307 }
f23061d4
JH
308 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
309 ETHER_ADDR_LEN);
984263bc
MD
310 /*
311 * XXX if ns_thishost is the same as the node's ethernet
312 * address then just the default code will catch this anyhow.
313 * So I'm not sure if this next clause should be here at all?
314 * [JRE]
315 */
f23061d4 316 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
984263bc 317 m->m_pkthdr.rcvif = ifp;
8bde602d 318 netisr_dispatch(NETISR_NS, m);
984263bc
MD
319 return (error);
320 }
f23061d4 321 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
984263bc 322 m->m_flags |= M_BCAST;
984263bc 323 break;
5fe66e68 324#endif
984263bc 325 case pseudo_AF_HDRCMPLT:
984263bc
MD
326 case AF_UNSPEC:
327 loop_copy = -1; /* if this is for us, don't do it */
f23061d4
JH
328 deh = (struct ether_header *)dst->sa_data;
329 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
330 eh->ether_type = deh->ether_type;
984263bc
MD
331 break;
332
333 default:
8f0777ca 334 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
f23061d4 335 gotoerr(EAFNOSUPPORT);
984263bc
MD
336 }
337
f23061d4
JH
338 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
339 memcpy(eh->ether_shost,
340 ((struct ether_header *)dst->sa_data)->ether_shost,
341 ETHER_ADDR_LEN);
984263bc 342 else
f23061d4 343 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
984263bc 344
db37145f
SS
345 /*
346 * Bridges require special output handling.
347 */
348 if (ifp->if_bridge) {
8f0777ca
SZ
349 KASSERT(bridge_output_p != NULL,
350 ("%s: if_bridge not loaded!", __func__));
ad8c8b44 351 return bridge_output_p(ifp, m);
db37145f
SS
352 }
353
984263bc
MD
354 /*
355 * If a simplex interface, and the packet is being sent to our
356 * Ethernet address or a broadcast address, loopback a copy.
357 * XXX To make a simplex device behave exactly like a duplex
358 * device, we should copy in the case of sending to our own
359 * ethernet address (thus letting the original actually appear
360 * on the wire). However, we don't do that here for security
361 * reasons and compatibility with the original behavior.
362 */
363 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
364 int csum_flags = 0;
365
366 if (m->m_pkthdr.csum_flags & CSUM_IP)
f23061d4 367 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
984263bc 368 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
f23061d4 369 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
984263bc
MD
370 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
371 struct mbuf *n;
372
f23061d4 373 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
984263bc
MD
374 n->m_pkthdr.csum_flags |= csum_flags;
375 if (csum_flags & CSUM_DATA_VALID)
376 n->m_pkthdr.csum_data = 0xffff;
f23061d4 377 if_simloop(ifp, n, dst->sa_family, hlen);
984263bc
MD
378 } else
379 ifp->if_iqdrops++;
f23061d4
JH
380 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
381 ETHER_ADDR_LEN) == 0) {
984263bc
MD
382 m->m_pkthdr.csum_flags |= csum_flags;
383 if (csum_flags & CSUM_DATA_VALID)
384 m->m_pkthdr.csum_data = 0xffff;
f23061d4 385 if_simloop(ifp, m, dst->sa_family, hlen);
984263bc
MD
386 return (0); /* XXX */
387 }
388 }
389
0d16ba1d
MD
390#ifdef CARP
391 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL)))
392 goto bad;
393#endif
394
395
984263bc
MD
396 /* Handle ng_ether(4) processing, if any */
397 if (ng_ether_output_p != NULL) {
0c3c561c
JH
398 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
399 goto bad;
984263bc
MD
400 if (m == NULL)
401 return (0);
402 }
403
404 /* Continue with link-layer output */
405 return ether_output_frame(ifp, m);
0c3c561c
JH
406
407bad:
408 m_freem(m);
409 return (error);
984263bc
MD
410}
411
412/*
413 * Ethernet link layer output routine to send a raw frame to the device.
414 *
415 * This assumes that the 14 byte Ethernet header is present and contiguous
a8d45119 416 * in the first mbuf.
984263bc
MD
417 */
418int
f23061d4 419ether_output_frame(struct ifnet *ifp, struct mbuf *m)
984263bc 420{
f23061d4 421 struct ip_fw *rule = NULL;
984263bc 422 int error = 0;
4d723e5a 423 struct altq_pktattr pktattr;
984263bc 424
57dff79c
SZ
425 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
426
eb241549
SZ
427 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
428 struct m_tag *mtag;
429
430 /* Extract info from dummynet tag */
431 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
432 KKASSERT(mtag != NULL);
84a3e25a 433 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
eb241549 434 KKASSERT(rule != NULL);
4c7020ad
SZ
435
436 m_tag_delete(m, mtag);
eb241549 437 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
5fe66e68 438 }
984263bc 439
4d723e5a
JS
440 if (ifq_is_enabled(&ifp->if_snd))
441 altq_etherclassify(&ifp->if_snd, m, &pktattr);
4986965b 442 crit_enter();
984263bc
MD
443 if (IPFW_LOADED && ether_ipfw != 0) {
444 struct ether_header save_eh, *eh;
445
446 eh = mtod(m, struct ether_header *);
447 save_eh = *eh;
448 m_adj(m, ETHER_HDR_LEN);
90ca9293 449 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
4986965b 450 crit_exit();
5fe66e68 451 if (m != NULL) {
984263bc 452 m_freem(m);
f23061d4 453 return ENOBUFS; /* pkt dropped */
984263bc
MD
454 } else
455 return 0; /* consumed e.g. in a pipe */
456 }
9b77ea6e 457
984263bc 458 /* packet was ok, restore the ethernet header */
9b77ea6e
SZ
459 ether_restore_header(&m, eh, &save_eh);
460 if (m == NULL) {
461 crit_exit();
462 return ENOBUFS;
984263bc
MD
463 }
464 }
78195a76 465 crit_exit();
984263bc
MD
466
467 /*
468 * Queue message on interface, update output statistics if
469 * successful, and start output if interface not yet active.
470 */
9db4b353 471 error = ifq_dispatch(ifp, m, &pktattr);
984263bc
MD
472 return (error);
473}
474
475/*
476 * ipfw processing for ethernet packets (in and out).
5fe66e68 477 * The second parameter is NULL from ether_demux(), and ifp from
a8d45119 478 * ether_output_frame().
984263bc 479 */
5fe66e68 480static boolean_t
90ca9293
SZ
481ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
482 const struct ether_header *eh)
984263bc 483{
29b27cb7 484 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
984263bc 485 struct ip_fw_args args;
e5ecc832 486 struct m_tag *mtag;
29b27cb7 487 struct mbuf *m;
f23061d4 488 int i;
984263bc
MD
489
490 if (*rule != NULL && fw_one_pass)
5fe66e68 491 return TRUE; /* dummynet packet, already partially processed */
984263bc
MD
492
493 /*
90ca9293 494 * I need some amount of data to be contiguous.
984263bc 495 */
f23061d4 496 i = min((*m0)->m_pkthdr.len, max_protohdr);
90ca9293 497 if ((*m0)->m_len < i) {
984263bc
MD
498 *m0 = m_pullup(*m0, i);
499 if (*m0 == NULL)
5fe66e68 500 return FALSE;
984263bc
MD
501 }
502
5de23090
SZ
503 /*
504 * Clean up tags
505 */
e5ecc832
JS
506 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
507 m_tag_delete(*m0, mtag);
5de23090
SZ
508 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
509 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
510 KKASSERT(mtag != NULL);
511 m_tag_delete(*m0, mtag);
512 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
513 }
514
515 args.m = *m0; /* the packet we are looking at */
516 args.oif = dst; /* destination, if any */
984263bc 517 args.rule = *rule; /* matching rule to restart */
984263bc
MD
518 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
519 i = ip_fw_chk_ptr(&args);
520 *m0 = args.m;
521 *rule = args.rule;
522
29b27cb7 523 if (*m0 == NULL)
5fe66e68 524 return FALSE;
984263bc 525
29b27cb7
SZ
526 switch (i) {
527 case IP_FW_PASS:
5fe66e68 528 return TRUE;
984263bc 529
29b27cb7
SZ
530 case IP_FW_DIVERT:
531 case IP_FW_TEE:
532 case IP_FW_DENY:
984263bc 533 /*
29b27cb7
SZ
534 * XXX at some point add support for divert/forward actions.
535 * If none of the above matches, we have to drop the pkt.
984263bc 536 */
29b27cb7 537 return FALSE;
984263bc 538
29b27cb7
SZ
539 case IP_FW_DUMMYNET:
540 /*
541 * Pass the pkt to dummynet, which consumes it.
542 */
90ca9293
SZ
543 m = *m0; /* pass the original to dummynet */
544 *m0 = NULL; /* and nothing back to the caller */
9b77ea6e
SZ
545
546 ether_restore_header(&m, eh, &save_eh);
547 if (m == NULL)
548 return FALSE;
549
29b27cb7
SZ
550 ip_fw_dn_io_ptr(m, args.cookie,
551 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
e4d4f9c3 552 ip_dn_queue(m);
5fe66e68 553 return FALSE;
29b27cb7
SZ
554
555 default:
556 panic("unknown ipfw return value: %d\n", i);
984263bc 557 }
984263bc
MD
558}
559
4853cd0f 560static void
68b67450
SZ
561ether_input(struct ifnet *ifp, struct mbuf *m)
562{
50098e2e 563 ether_input_chain(ifp, m, NULL);
984263bc
MD
564}
565
566/*
567 * Perform common duties while attaching to interface list
568 */
569void
78195a76 570ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
c0f6c904 571{
78195a76
MD
572 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
573 serializer);
c0f6c904
JS
574}
575
576void
78195a76
MD
577ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
578 lwkt_serialize_t serializer)
984263bc 579{
82ed7fc2 580 struct sockaddr_dl *sdl;
984263bc
MD
581
582 ifp->if_type = IFT_ETHER;
c401f0fd 583 ifp->if_addrlen = ETHER_ADDR_LEN;
5fe66e68 584 ifp->if_hdrlen = ETHER_HDR_LEN;
78195a76 585 if_attach(ifp, serializer);
984263bc 586 ifp->if_mtu = ETHERMTU;
984263bc 587 if (ifp->if_baudrate == 0)
f23061d4 588 ifp->if_baudrate = 10000000;
5fe66e68 589 ifp->if_output = ether_output;
0b076e92 590 ifp->if_input = ether_input;
5fe66e68
JH
591 ifp->if_resolvemulti = ether_resolvemulti;
592 ifp->if_broadcastaddr = etherbroadcastaddr;
f2682cb9 593 sdl = IF_LLSOCKADDR(ifp);
984263bc
MD
594 sdl->sdl_type = IFT_ETHER;
595 sdl->sdl_alen = ifp->if_addrlen;
0a8b5977 596 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
c568d5be
JS
597 /*
598 * XXX Keep the current drivers happy.
599 * XXX Remove once all drivers have been cleaned up
600 */
601 if (lla != IFP2AC(ifp)->ac_enaddr)
602 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
c0f6c904 603 bpfattach(ifp, dlt, hdrlen);
984263bc
MD
604 if (ng_ether_attach_p != NULL)
605 (*ng_ether_attach_p)(ifp);
267caeeb
JS
606
607 if_printf(ifp, "MAC address: %6D\n", lla, ":");
984263bc
MD
608}
609
610/*
611 * Perform common duties while detaching an Ethernet interface
612 */
613void
0a8b5977 614ether_ifdetach(struct ifnet *ifp)
984263bc 615{
45b8be9e 616 if_down(ifp);
45b8be9e 617
984263bc
MD
618 if (ng_ether_detach_p != NULL)
619 (*ng_ether_detach_p)(ifp);
0a8b5977 620 bpfdetach(ifp);
984263bc 621 if_detach(ifp);
984263bc
MD
622}
623
984263bc 624int
f23061d4 625ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
984263bc
MD
626{
627 struct ifaddr *ifa = (struct ifaddr *) data;
628 struct ifreq *ifr = (struct ifreq *) data;
629 int error = 0;
630
cbf2eda6
SZ
631#define IF_INIT(ifp) \
632do { \
633 if (((ifp)->if_flags & IFF_UP) == 0) { \
634 (ifp)->if_flags |= IFF_UP; \
635 (ifp)->if_init((ifp)->if_softc); \
636 } \
637} while (0)
638
78195a76
MD
639 ASSERT_SERIALIZED(ifp->if_serializer);
640
984263bc
MD
641 switch (command) {
642 case SIOCSIFADDR:
984263bc
MD
643 switch (ifa->ifa_addr->sa_family) {
644#ifdef INET
645 case AF_INET:
cbf2eda6 646 IF_INIT(ifp); /* before arpwhohas */
984263bc
MD
647 arp_ifinit(ifp, ifa);
648 break;
649#endif
650#ifdef IPX
651 /*
652 * XXX - This code is probably wrong
653 */
654 case AF_IPX:
655 {
f23061d4 656 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
984263bc
MD
657 struct arpcom *ac = IFP2AC(ifp);
658
659 if (ipx_nullhost(*ina))
f23061d4
JH
660 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
661 else
662 bcopy(ina->x_host.c_host, ac->ac_enaddr,
663 sizeof ac->ac_enaddr);
984263bc 664
cbf2eda6 665 IF_INIT(ifp); /* Set new address. */
984263bc
MD
666 break;
667 }
668#endif
669#ifdef NS
670 /*
671 * XXX - This code is probably wrong
672 */
673 case AF_NS:
674 {
82ed7fc2 675 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
984263bc
MD
676 struct arpcom *ac = IFP2AC(ifp);
677
678 if (ns_nullhost(*ina))
f23061d4
JH
679 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
680 else
681 bcopy(ina->x_host.c_host, ac->ac_enaddr,
682 sizeof ac->ac_enaddr);
984263bc
MD
683
684 /*
685 * Set new address
686 */
cbf2eda6 687 IF_INIT(ifp);
984263bc
MD
688 break;
689 }
690#endif
691 default:
cbf2eda6 692 IF_INIT(ifp);
984263bc
MD
693 break;
694 }
695 break;
696
697 case SIOCGIFADDR:
f23061d4
JH
698 bcopy(IFP2AC(ifp)->ac_enaddr,
699 ((struct sockaddr *)ifr->ifr_data)->sa_data,
700 ETHER_ADDR_LEN);
984263bc
MD
701 break;
702
703 case SIOCSIFMTU:
704 /*
705 * Set the interface MTU.
706 */
707 if (ifr->ifr_mtu > ETHERMTU) {
708 error = EINVAL;
709 } else {
710 ifp->if_mtu = ifr->ifr_mtu;
711 }
712 break;
c2d9fd91
JS
713 default:
714 error = EINVAL;
715 break;
984263bc
MD
716 }
717 return (error);
cbf2eda6
SZ
718
719#undef IF_INIT
984263bc
MD
720}
721
722int
f23061d4
JH
723ether_resolvemulti(
724 struct ifnet *ifp,
725 struct sockaddr **llsa,
726 struct sockaddr *sa)
984263bc
MD
727{
728 struct sockaddr_dl *sdl;
729 struct sockaddr_in *sin;
730#ifdef INET6
731 struct sockaddr_in6 *sin6;
732#endif
733 u_char *e_addr;
734
735 switch(sa->sa_family) {
736 case AF_LINK:
737 /*
738 * No mapping needed. Just check that it's a valid MC address.
739 */
740 sdl = (struct sockaddr_dl *)sa;
741 e_addr = LLADDR(sdl);
742 if ((e_addr[0] & 1) != 1)
743 return EADDRNOTAVAIL;
744 *llsa = 0;
745 return 0;
746
747#ifdef INET
748 case AF_INET:
749 sin = (struct sockaddr_in *)sa;
750 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
751 return EADDRNOTAVAIL;
752 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
5fe66e68 753 M_WAITOK | M_ZERO);
984263bc
MD
754 sdl->sdl_len = sizeof *sdl;
755 sdl->sdl_family = AF_LINK;
756 sdl->sdl_index = ifp->if_index;
757 sdl->sdl_type = IFT_ETHER;
758 sdl->sdl_alen = ETHER_ADDR_LEN;
759 e_addr = LLADDR(sdl);
760 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
761 *llsa = (struct sockaddr *)sdl;
762 return 0;
763#endif
764#ifdef INET6
765 case AF_INET6:
766 sin6 = (struct sockaddr_in6 *)sa;
767 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
768 /*
769 * An IP6 address of 0 means listen to all
770 * of the Ethernet multicast address used for IP6.
771 * (This is used for multicast routers.)
772 */
773 ifp->if_flags |= IFF_ALLMULTI;
774 *llsa = 0;
775 return 0;
776 }
777 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
778 return EADDRNOTAVAIL;
779 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
5fe66e68 780 M_WAITOK | M_ZERO);
984263bc
MD
781 sdl->sdl_len = sizeof *sdl;
782 sdl->sdl_family = AF_LINK;
783 sdl->sdl_index = ifp->if_index;
784 sdl->sdl_type = IFT_ETHER;
785 sdl->sdl_alen = ETHER_ADDR_LEN;
786 e_addr = LLADDR(sdl);
787 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
788 *llsa = (struct sockaddr *)sdl;
789 return 0;
790#endif
791
792 default:
793 /*
794 * Well, the text isn't quite right, but it's the name
795 * that counts...
796 */
797 return EAFNOSUPPORT;
798 }
799}
d6018c31
JS
800
801#if 0
802/*
803 * This is for reference. We have a table-driven version
804 * of the little-endian crc32 generator, which is faster
805 * than the double-loop.
806 */
807uint32_t
808ether_crc32_le(const uint8_t *buf, size_t len)
809{
810 uint32_t c, crc, carry;
811 size_t i, j;
812
813 crc = 0xffffffffU; /* initial value */
814
815 for (i = 0; i < len; i++) {
816 c = buf[i];
817 for (j = 0; j < 8; j++) {
818 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
819 crc >>= 1;
820 c >>= 1;
821 if (carry)
822 crc = (crc ^ ETHER_CRC_POLY_LE);
823 }
824 }
825
f23061d4 826 return (crc);
d6018c31
JS
827}
828#else
829uint32_t
830ether_crc32_le(const uint8_t *buf, size_t len)
831{
832 static const uint32_t crctab[] = {
833 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
834 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
835 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
836 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
837 };
838 uint32_t crc;
839 size_t i;
840
841 crc = 0xffffffffU; /* initial value */
842
843 for (i = 0; i < len; i++) {
844 crc ^= buf[i];
845 crc = (crc >> 4) ^ crctab[crc & 0xf];
846 crc = (crc >> 4) ^ crctab[crc & 0xf];
847 }
848
f23061d4 849 return (crc);
d6018c31
JS
850}
851#endif
852
853uint32_t
854ether_crc32_be(const uint8_t *buf, size_t len)
855{
856 uint32_t c, crc, carry;
857 size_t i, j;
858
859 crc = 0xffffffffU; /* initial value */
860
861 for (i = 0; i < len; i++) {
862 c = buf[i];
863 for (j = 0; j < 8; j++) {
864 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
865 crc <<= 1;
866 c >>= 1;
867 if (carry)
868 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
869 }
870 }
871
f23061d4 872 return (crc);
d6018c31 873}
4d723e5a 874
4d723e5a
JS
875/*
876 * find the size of ethernet header, and call classifier
877 */
878void
879altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
880 struct altq_pktattr *pktattr)
881{
882 struct ether_header *eh;
883 uint16_t ether_type;
884 int hlen, af, hdrsize;
885 caddr_t hdr;
886
887 hlen = sizeof(struct ether_header);
888 eh = mtod(m, struct ether_header *);
889
890 ether_type = ntohs(eh->ether_type);
891 if (ether_type < ETHERMTU) {
892 /* ick! LLC/SNAP */
893 struct llc *llc = (struct llc *)(eh + 1);
894 hlen += 8;
895
896 if (m->m_len < hlen ||
897 llc->llc_dsap != LLC_SNAP_LSAP ||
898 llc->llc_ssap != LLC_SNAP_LSAP ||
899 llc->llc_control != LLC_UI)
900 goto bad; /* not snap! */
901
902 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
903 }
904
905 if (ether_type == ETHERTYPE_IP) {
906 af = AF_INET;
907 hdrsize = 20; /* sizeof(struct ip) */
908#ifdef INET6
909 } else if (ether_type == ETHERTYPE_IPV6) {
910 af = AF_INET6;
911 hdrsize = 40; /* sizeof(struct ip6_hdr) */
912#endif
913 } else
914 goto bad;
915
916 while (m->m_len <= hlen) {
917 hlen -= m->m_len;
918 m = m->m_next;
919 }
920 hdr = m->m_data + hlen;
921 if (m->m_len < hlen + hdrsize) {
922 /*
923 * ip header is not in a single mbuf. this should not
924 * happen in the current code.
925 * (todo: use m_pulldown in the future)
926 */
927 goto bad;
928 }
929 m->m_data += hlen;
930 m->m_len -= hlen;
931 ifq_classify(ifq, m, af, pktattr);
932 m->m_data -= hlen;
933 m->m_len += hlen;
934
935 return;
936
937bad:
938 pktattr->pattr_class = NULL;
939 pktattr->pattr_hdr = NULL;
940 pktattr->pattr_af = AF_UNSPEC;
941}
9b77ea6e
SZ
942
943static void
944ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
945 const struct ether_header *save_eh)
946{
947 struct mbuf *m = *m0;
948
949 ether_restore_hdr++;
950
951 /*
952 * Prepend the header, optimize for the common case of
953 * eh pointing into the mbuf.
954 */
955 if ((const void *)(eh + 1) == (void *)m->m_data) {
956 m->m_data -= ETHER_HDR_LEN;
957 m->m_len += ETHER_HDR_LEN;
958 m->m_pkthdr.len += ETHER_HDR_LEN;
959 } else {
960 ether_prepend_hdr++;
961
962 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
963 if (m != NULL) {
3d26a382 964 bcopy(save_eh, mtod(m, struct ether_header *),
9b77ea6e
SZ
965 ETHER_HDR_LEN);
966 }
967 }
968 *m0 = m;
969}
68b67450 970
68b67450
SZ
971static void
972ether_input_ipifunc(void *arg)
973{
974 struct mbuf *m, *next;
975 lwkt_port_t port;
976
977 m = arg;
978 do {
979 next = m->m_nextpkt;
980 m->m_nextpkt = NULL;
981
982 port = m->m_pkthdr.header;
983 m->m_pkthdr.header = NULL;
984
985 lwkt_sendmsg(port,
986 &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
987
988 m = next;
989 } while (m != NULL);
990}
991
992void
993ether_input_dispatch(struct mbuf_chain *chain)
994{
995#ifdef SMP
996 int i;
997
f3e0b5f0 998 logether(disp_beg, NULL);
68b67450
SZ
999 for (i = 0; i < ncpus; ++i) {
1000 if (chain[i].mc_head != NULL) {
1001 lwkt_send_ipiq(globaldata_find(i),
1002 ether_input_ipifunc, chain[i].mc_head);
1003 }
1004 }
1005#else
f3e0b5f0 1006 logether(disp_beg, NULL);
b358f907
SZ
1007 if (chain->mc_head != NULL)
1008 ether_input_ipifunc(chain->mc_head);
68b67450 1009#endif
f3e0b5f0 1010 logether(disp_end, NULL);
68b67450
SZ
1011}
1012
57ccf5a2
SZ
1013void
1014ether_input_chain_init(struct mbuf_chain *chain)
1015{
1016#ifdef SMP
1017 int i;
1018
1019 for (i = 0; i < ncpus; ++i)
1020 chain[i].mc_head = chain[i].mc_tail = NULL;
1021#else
1022 chain->mc_head = chain->mc_tail = NULL;
1023#endif
1024}
1025
5f32d321
SZ
1026/*
1027 * Upper layer processing for a received Ethernet packet.
1028 */
297c8124 1029void
29bc1092
SZ
1030ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
1031{
1032 struct ether_header *eh;
1033 int isr, redispatch;
1034 u_short ether_type;
1035 struct ip_fw *rule = NULL;
29bc1092
SZ
1036#ifdef NETATALK
1037 struct llc *l;
1038#endif
1039
1040 M_ASSERTPKTHDR(m);
1041 KASSERT(m->m_len >= ETHER_HDR_LEN,
1042 ("ether header is no contiguous!\n"));
1043
1044 eh = mtod(m, struct ether_header *);
1045
eb241549
SZ
1046 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
1047 struct m_tag *mtag;
1048
1049 /* Extract info from dummynet tag */
1050 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1051 KKASSERT(mtag != NULL);
29bc1092 1052 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
eb241549
SZ
1053 KKASSERT(rule != NULL);
1054
29bc1092 1055 m_tag_delete(m, mtag);
eb241549
SZ
1056 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1057
1058 /* packet is passing the second time */
29bc1092 1059 goto post_stats;
eb241549 1060 }
29bc1092
SZ
1061
1062#ifdef CARP
1063 /*
1064 * XXX: Okay, we need to call carp_forus() and - if it is for
1065 * us jump over code that does the normal check
1066 * "ac_enaddr == ether_dhost". The check sequence is a bit
1067 * different from OpenBSD, so we jump over as few code as
1068 * possible, to catch _all_ sanity checks. This needs
1069 * evaluation, to see if the carp ether_dhost values break any
1070 * of these checks!
1071 */
1072 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
1073 goto post_stats;
1074#endif
1075
1076 /*
1077 * Discard packet if upper layers shouldn't see it because
1078 * it was unicast to a different Ethernet address. If the
1079 * driver is working properly, then this situation can only
1080 * happen when the interface is in promiscuous mode.
1081 */
1082 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1083 (eh->ether_dhost[0] & 1) == 0 &&
1084 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1085 m_freem(m);
1086 return;
1087 }
1088
1089post_stats:
1090 if (IPFW_LOADED && ether_ipfw != 0) {
1091 struct ether_header save_eh = *eh;
1092
1093 /* XXX old crufty stuff, needs to be removed */
1094 m_adj(m, sizeof(struct ether_header));
1095
1096 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1097 m_freem(m);
1098 return;
1099 }
1100
1101 ether_restore_header(&m, eh, &save_eh);
1102 if (m == NULL)
1103 return;
1104 eh = mtod(m, struct ether_header *);
1105 }
1106
1107 ether_type = ntohs(eh->ether_type);
1108 KKASSERT(ether_type != ETHERTYPE_VLAN);
1109
1110 if (m->m_flags & M_VLANTAG) {
b327296f
SZ
1111 void (*vlan_input_func)(struct mbuf *);
1112
1113 vlan_input_func = vlan_input_p;
1114 if (vlan_input_func != NULL) {
1115 vlan_input_func(m);
29bc1092
SZ
1116 } else {
1117 m->m_pkthdr.rcvif->if_noproto++;
1118 m_freem(m);
1119 }
29bc1092
SZ
1120 return;
1121 }
1122
1123 m_adj(m, sizeof(struct ether_header));
1124 redispatch = 0;
1125
1126 switch (ether_type) {
1127#ifdef INET
1128 case ETHERTYPE_IP:
297c8124 1129 if (ipflow_fastforward(m))
29bc1092 1130 return;
29bc1092
SZ
1131 isr = NETISR_IP;
1132 break;
1133
1134 case ETHERTYPE_ARP:
1135 if (ifp->if_flags & IFF_NOARP) {
1136 /* Discard packet if ARP is disabled on interface */
1137 m_freem(m);
1138 return;
1139 }
1140 isr = NETISR_ARP;
1141 break;
1142#endif
1143
1144#ifdef INET6
1145 case ETHERTYPE_IPV6:
1146 isr = NETISR_IPV6;
1147 break;
1148#endif
1149
1150#ifdef IPX
1151 case ETHERTYPE_IPX:
1152 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
1153 return;
1154 isr = NETISR_IPX;
1155 break;
1156#endif
1157
1158#ifdef NS
1159 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1160 isr = NETISR_NS;
1161 break;
1162
1163#endif
1164
1165#ifdef NETATALK
1166 case ETHERTYPE_AT:
1167 isr = NETISR_ATALK1;
1168 break;
1169 case ETHERTYPE_AARP:
1170 isr = NETISR_AARP;
1171 break;
1172#endif
1173
a020e9d5
SZ
1174#ifdef MPLS
1175 case ETHERTYPE_MPLS:
1176 case ETHERTYPE_MPLS_MCAST:
50098e2e 1177 /* Should have been set by ether_input_chain(). */
cb8d752c 1178 KKASSERT(m->m_flags & M_MPLSLABELED);
a020e9d5
SZ
1179 isr = NETISR_MPLS;
1180 break;
1181#endif
1182
29bc1092
SZ
1183 default:
1184 /*
1185 * The accurate msgport is not determined before
1186 * we reach here, so redo the dispatching
1187 */
1188 redispatch = 1;
1189#ifdef IPX
1190 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
1191 return;
1192#endif
1193#ifdef NS
1194 checksum = mtod(m, ushort *);
1195 /* Novell 802.3 */
1196 if ((ether_type <= ETHERMTU) &&
1197 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
1198 if (*checksum == 0xE0E0) {
1199 m->m_pkthdr.len -= 3;
1200 m->m_len -= 3;
1201 m->m_data += 3;
1202 }
1203 isr = NETISR_NS;
1204 break;
1205 }
1206#endif
1207#ifdef NETATALK
1208 if (ether_type > ETHERMTU)
1209 goto dropanyway;
1210 l = mtod(m, struct llc *);
1211 if (l->llc_dsap == LLC_SNAP_LSAP &&
1212 l->llc_ssap == LLC_SNAP_LSAP &&
1213 l->llc_control == LLC_UI) {
1214 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
1215 sizeof at_org_code) == 0 &&
1216 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
1217 m_adj(m, sizeof(struct llc));
1218 isr = NETISR_ATALK2;
1219 break;
1220 }
1221 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
1222 sizeof aarp_org_code) == 0 &&
1223 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
1224 m_adj(m, sizeof(struct llc));
1225 isr = NETISR_AARP;
1226 break;
1227 }
1228 }
1229dropanyway:
1230#endif
1231 if (ng_ether_input_orphan_p != NULL)
1232 ng_ether_input_orphan_p(ifp, m, eh);
1233 else
1234 m_freem(m);
1235 return;
1236 }
1237
1238 if (!redispatch)
1239 netisr_run(isr, m);
1240 else
1241 netisr_dispatch(isr, m);
1242}
1243
5f32d321
SZ
1244/*
1245 * First we perform any link layer operations, then continue to the
1246 * upper layers with ether_demux_oncpu().
1247 */
72b87a91 1248void
29bc1092
SZ
1249ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1250{
160af078
SZ
1251 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1252 /*
1253 * Receiving interface's flags are changed, when this
1254 * packet is waiting for processing; discard it.
1255 */
1256 m_freem(m);
1257 return;
1258 }
1259
29bc1092
SZ
1260 /*
1261 * Tap the packet off here for a bridge. bridge_input()
1262 * will return NULL if it has consumed the packet, otherwise
1263 * it gets processed as normal. Note that bridge_input()
1264 * will always return the original packet if we need to
1265 * process it locally.
1266 */
1267 if (ifp->if_bridge) {
1268 KASSERT(bridge_input_p != NULL,
1269 ("%s: if_bridge not loaded!", __func__));
1270
1271 if(m->m_flags & M_PROTO1) {
1272 m->m_flags &= ~M_PROTO1;
1273 } else {
1274 /* clear M_PROMISC, in case the packets comes from a vlan */
1275 /* m->m_flags &= ~M_PROMISC; */
1276 m = bridge_input_p(ifp, m);
1277 if (m == NULL)
1278 return;
1279
1280 KASSERT(ifp == m->m_pkthdr.rcvif,
1281 ("bridge_input_p changed rcvif\n"));
1282 }
1283 }
1284
1285 /* Handle ng_ether(4) processing, if any */
1286 if (ng_ether_input_p != NULL) {
1287 ng_ether_input_p(ifp, &m);
1288 if (m == NULL)
1289 return;
1290 }
1291
1292 /* Continue with upper layer processing */
1293 ether_demux_oncpu(ifp, m);
1294}
1295
1296static void
1297ether_input_handler(struct netmsg *nmsg)
1298{
1299 struct netmsg_packet *nmp = (struct netmsg_packet *)nmsg;
1300 struct ifnet *ifp;
1301 struct mbuf *m;
1302
1303 m = nmp->nm_packet;
1304 M_ASSERTPKTHDR(m);
1305 ifp = m->m_pkthdr.rcvif;
1306
1307 ether_input_oncpu(ifp, m);
1308}
1309
1310static __inline void
1311ether_init_netpacket(int num, struct mbuf *m)
1312{
1313 struct netmsg_packet *pmsg;
1314
1315 pmsg = &m->m_hdr.mh_netmsg;
1316 netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport, 0,
1317 ether_input_handler);
1318 pmsg->nm_packet = m;
1319 pmsg->nm_netmsg.nm_lmsg.u.ms_result = num;
1320}
1321
1322static __inline struct lwkt_port *
e420e7d5 1323ether_mport(int num, struct mbuf **m)
29bc1092 1324{
29bc1092
SZ
1325 if (num == NETISR_MAX) {
1326 /*
1327 * All packets whose target msgports can't be
1328 * determined here are dispatched to netisr0,
1329 * where further dispatching may happen.
1330 */
1331 return cpu_portfn(0);
1332 }
e420e7d5 1333 return netisr_find_port(num, m);
29bc1092
SZ
1334}
1335
62f35c44
SZ
1336/*
1337 * Process a received Ethernet packet.
1338 *
1339 * The ethernet header is assumed to be in the mbuf so the caller
1340 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1341 * bytes in the first mbuf.
1342 *
1343 * We first try to find the target msgport for this ether frame, if
1344 * there is no target msgport for it, this ether frame is discarded,
1345 * else we do following processing according to whether 'chain' is
1346 * NULL or not:
1347 * - If 'chain' is NULL, this ether frame is sent to the target msgport
50098e2e 1348 * immediately. This situation happens when ether_input_chain is
62f35c44
SZ
1349 * accessed through ifnet.if_input.
1350 * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1351 * bucket indexed by the target msgport's cpuid and the target msgport
50098e2e 1352 * is saved in mbuf's m_pkthdr.m_head. Caller of ether_input_chain
62f35c44
SZ
1353 * must initialize 'chain' by calling ether_input_chain_init().
1354 * ether_input_dispatch must be called later to send ether frames
1355 * queued on 'chain' to their target msgport.
1356 */
29bc1092 1357void
50098e2e 1358ether_input_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
29bc1092
SZ
1359{
1360 struct ether_header *eh, *save_eh, save_eh0;
1361 struct lwkt_port *port;
1362 uint16_t ether_type;
1363 int isr;
1364
1365 ASSERT_SERIALIZED(ifp->if_serializer);
1366 M_ASSERTPKTHDR(m);
1367
1368 /* Discard packet if interface is not up */
1369 if (!(ifp->if_flags & IFF_UP)) {
1370 m_freem(m);
1371 return;
1372 }
1373
1374 if (m->m_len < sizeof(struct ether_header)) {
1375 /* XXX error in the caller. */
1376 m_freem(m);
1377 return;
1378 }
1379 eh = mtod(m, struct ether_header *);
1380
1381 m->m_pkthdr.rcvif = ifp;
1382
f3e0b5f0
SZ
1383 logether(chain_beg, ifp);
1384
29bc1092
SZ
1385 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1386 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1387 ifp->if_addrlen) == 0)
1388 m->m_flags |= M_BCAST;
1389 else
1390 m->m_flags |= M_MCAST;
1391 ifp->if_imcasts++;
1392 }
1393
1394 ETHER_BPF_MTAP(ifp, m);
1395
1396 ifp->if_ibytes += m->m_pkthdr.len;
1397
1398 if (ifp->if_flags & IFF_MONITOR) {
1399 /*
1400 * Interface marked for monitoring; discard packet.
1401 */
b5a65047 1402 m_freem(m);
f3e0b5f0
SZ
1403
1404 logether(chain_end, ifp);
b5a65047 1405 return;
29bc1092
SZ
1406 }
1407
1408 if (ntohs(eh->ether_type) == ETHERTYPE_VLAN &&
1409 (m->m_flags & M_VLANTAG) == 0) {
1410 /*
1411 * Extract vlan tag if hardware does not do it for us
1412 */
1413 vlan_ether_decap(&m);
1414 if (m == NULL)
1415 return;
1416 eh = mtod(m, struct ether_header *);
1417 }
1418 ether_type = ntohs(eh->ether_type);
1419
1420 if ((m->m_flags & M_VLANTAG) && ether_type == ETHERTYPE_VLAN) {
1421 /*
1422 * To prevent possible dangerous recursion,
1423 * we don't do vlan-in-vlan
1424 */
1425 ifp->if_noproto++;
1426 m_freem(m);
1427 return;
1428 }
1429 KKASSERT(ether_type != ETHERTYPE_VLAN);
1430
1431 /*
1432 * Map ether type to netisr id.
1433 */
1434 switch (ether_type) {
1435#ifdef INET
1436 case ETHERTYPE_IP:
1437 isr = NETISR_IP;
1438 break;
1439
1440 case ETHERTYPE_ARP:
1441 isr = NETISR_ARP;
1442 break;
1443#endif
1444
1445#ifdef INET6
1446 case ETHERTYPE_IPV6:
1447 isr = NETISR_IPV6;
1448 break;
1449#endif
1450
1451#ifdef IPX
1452 case ETHERTYPE_IPX:
1453 isr = NETISR_IPX;
1454 break;
1455#endif
1456
1457#ifdef NS
1458 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1459 isr = NETISR_NS;
1460 break;
1461#endif
1462
1463#ifdef NETATALK
1464 case ETHERTYPE_AT:
1465 isr = NETISR_ATALK1;
1466 break;
1467 case ETHERTYPE_AARP:
1468 isr = NETISR_AARP;
1469 break;
1470#endif
1471
a020e9d5
SZ
1472#ifdef MPLS
1473 case ETHERTYPE_MPLS:
1474 case ETHERTYPE_MPLS_MCAST:
cb8d752c 1475 m->m_flags |= M_MPLSLABELED;
a020e9d5
SZ
1476 isr = NETISR_MPLS;
1477 break;
1478#endif
1479
29bc1092
SZ
1480 default:
1481 /*
1482 * NETISR_MAX is an invalid value; it is chosen to let
1483 * ether_mport() know that we are not able to decide
1484 * this packet's msgport here.
1485 */
1486 isr = NETISR_MAX;
1487 break;
1488 }
1489
1490 /*
1491 * If the packet is in contiguous memory, following
1492 * m_adj() could ensure that the hidden ether header
1493 * will not be destroyed, else we will have to save
1494 * the ether header for the later restoration.
1495 */
1496 if (m->m_pkthdr.len != m->m_len) {
1497 save_eh0 = *eh;
1498 save_eh = &save_eh0;
1499 } else {
1500 save_eh = NULL;
1501 }
1502
1503 /*
1504 * Temporarily remove ether header; ether_mport()
1505 * expects a packet without ether header.
1506 */
1507 m_adj(m, sizeof(struct ether_header));
1508
1509 /*
1510 * Find the packet's target msgport.
1511 */
1512 port = ether_mport(isr, &m);
1513 if (port == NULL) {
1514 KKASSERT(m == NULL);
1515 return;
1516 }
1517
1518 /*
1519 * Restore ether header.
1520 */
1521 if (save_eh != NULL) {
1522 ether_restore_header(&m, eh, save_eh);
1523 if (m == NULL)
1524 return;
1525 } else {
1526 m->m_data -= ETHER_HDR_LEN;
1527 m->m_len += ETHER_HDR_LEN;
1528 m->m_pkthdr.len += ETHER_HDR_LEN;
1529 }
1530
1531 /*
1532 * Initialize mbuf's netmsg packet _after_ possible
1533 * ether header restoration, else the initialized
1534 * netmsg packet may be lost during ether header
1535 * restoration.
1536 */
1537 ether_init_netpacket(isr, m);
1538
29bc1092
SZ
1539 if (chain != NULL) {
1540 struct mbuf_chain *c;
1541 int cpuid;
1542
1543 m->m_pkthdr.header = port; /* XXX */
1544 cpuid = port->mpu_td->td_gd->gd_cpuid;
1545
1546 c = &chain[cpuid];
1547 if (c->mc_head == NULL) {
1548 c->mc_head = c->mc_tail = m;
1549 } else {
1550 c->mc_tail->m_nextpkt = m;
1551 c->mc_tail = m;
1552 }
1553 m->m_nextpkt = NULL;
a75a1559 1554 } else {
29bc1092 1555 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
a75a1559 1556 }
f3e0b5f0 1557 logether(chain_end, ifp);
29bc1092 1558}