Handle MPLS on ETHER_INPUT2 code path
[dragonfly.git] / sys / net / if_ethersubr.c
blob6070eefd8e07fece29d6f1e7e34bbde14392b1da
1 /*
2 * Copyright (c) 1982, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
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 $
35 * $DragonFly: src/sys/net/if_ethersubr.c,v 1.76 2008/07/08 13:50:52 sephe Exp $
38 #include "opt_atalk.h"
39 #include "opt_inet.h"
40 #include "opt_inet6.h"
41 #include "opt_ipx.h"
42 #include "opt_mpls.h"
43 #include "opt_netgraph.h"
44 #include "opt_carp.h"
45 #include "opt_ethernet.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/globaldata.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/msgport.h>
54 #include <sys/socket.h>
55 #include <sys/sockio.h>
56 #include <sys/sysctl.h>
57 #include <sys/thread.h>
58 #include <sys/thread2.h>
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>
66 #include <net/ifq_var.h>
67 #include <net/bpf.h>
68 #include <net/ethernet.h>
69 #include <net/vlan/if_vlan_ether.h>
70 #include <net/netmsg2.h>
72 #if defined(INET) || defined(INET6)
73 #include <netinet/in.h>
74 #include <netinet/in_var.h>
75 #include <netinet/if_ether.h>
76 #include <net/ipfw/ip_fw.h>
77 #include <net/dummynet/ip_dummynet.h>
78 #endif
79 #ifdef INET6
80 #include <netinet6/nd6.h>
81 #endif
83 #ifdef CARP
84 #include <netinet/ip_carp.h>
85 #endif
87 #ifdef IPX
88 #include <netproto/ipx/ipx.h>
89 #include <netproto/ipx/ipx_if.h>
90 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
91 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
92 short *tp, int *hlen);
93 #endif
95 #ifdef NS
96 #include <netns/ns.h>
97 #include <netns/ns_if.h>
98 ushort ns_nettype;
99 int ether_outputdebug = 0;
100 int ether_inputdebug = 0;
101 #endif
103 #ifdef NETATALK
104 #include <netproto/atalk/at.h>
105 #include <netproto/atalk/at_var.h>
106 #include <netproto/atalk/at_extern.h>
108 #define llc_snap_org_code llc_un.type_snap.org_code
109 #define llc_snap_ether_type llc_un.type_snap.ether_type
111 extern u_char at_org_code[3];
112 extern u_char aarp_org_code[3];
113 #endif /* NETATALK */
115 #ifdef MPLS
116 #include <netproto/mpls/mpls.h>
117 #endif
119 /* netgraph node hooks for ng_ether(4) */
120 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
121 void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
122 struct mbuf *m, const struct ether_header *eh);
123 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
124 void (*ng_ether_attach_p)(struct ifnet *ifp);
125 void (*ng_ether_detach_p)(struct ifnet *ifp);
127 int (*vlan_input_p)(struct mbuf *, struct mbuf_chain *);
128 void (*vlan_input2_p)(struct mbuf *);
130 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
131 struct rtentry *);
132 static void ether_restore_header(struct mbuf **, const struct ether_header *,
133 const struct ether_header *);
134 static void ether_demux_chain(struct ifnet *, struct mbuf *,
135 struct mbuf_chain *);
138 * if_bridge support
140 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
141 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
142 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
144 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
145 struct sockaddr *);
147 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
148 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
151 #define gotoerr(e) do { error = (e); goto bad; } while (0)
152 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
154 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
155 struct ip_fw **rule,
156 const struct ether_header *eh);
158 static int ether_ipfw;
159 static u_int ether_restore_hdr;
160 static u_int ether_prepend_hdr;
162 SYSCTL_DECL(_net_link);
163 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
164 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
165 &ether_ipfw, 0, "Pass ether pkts through firewall");
166 SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
167 &ether_restore_hdr, 0, "# of ether header restoration");
168 SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
169 &ether_prepend_hdr, 0,
170 "# of ether header restoration which prepends mbuf");
173 * Ethernet output routine.
174 * Encapsulate a packet of type family for the local net.
175 * Use trailer local net encapsulation if enough data in first
176 * packet leaves a multiple of 512 bytes of data in remainder.
177 * Assumes that ifp is actually pointer to arpcom structure.
179 static int
180 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
181 struct rtentry *rt)
183 struct ether_header *eh, *deh;
184 u_char *edst;
185 int loop_copy = 0;
186 int hlen = ETHER_HDR_LEN; /* link layer header length */
187 struct arpcom *ac = IFP2AC(ifp);
188 int error;
190 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
192 if (ifp->if_flags & IFF_MONITOR)
193 gotoerr(ENETDOWN);
194 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
195 gotoerr(ENETDOWN);
197 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
198 if (m == NULL)
199 return (ENOBUFS);
200 eh = mtod(m, struct ether_header *);
201 edst = eh->ether_dhost;
204 * Fill in the destination ethernet address and frame type.
206 switch (dst->sa_family) {
207 #ifdef INET
208 case AF_INET:
209 if (!arpresolve(ifp, rt, m, dst, edst))
210 return (0); /* if not yet resolved */
211 eh->ether_type = htons(ETHERTYPE_IP);
212 break;
213 #endif
214 #ifdef INET6
215 case AF_INET6:
216 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
217 return (0); /* Something bad happenned. */
218 eh->ether_type = htons(ETHERTYPE_IPV6);
219 break;
220 #endif
221 #ifdef IPX
222 case AF_IPX:
223 if (ef_outputp != NULL) {
224 error = ef_outputp(ifp, &m, dst, &eh->ether_type,
225 &hlen);
226 if (error)
227 goto bad;
228 } else {
229 eh->ether_type = htons(ETHERTYPE_IPX);
230 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
231 edst, ETHER_ADDR_LEN);
233 break;
234 #endif
235 #ifdef NETATALK
236 case AF_APPLETALK: {
237 struct at_ifaddr *aa;
239 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
240 error = 0; /* XXX */
241 goto bad;
244 * In the phase 2 case, need to prepend an mbuf for
245 * the llc header. Since we must preserve the value
246 * of m, which is passed to us by value, we m_copy()
247 * the first mbuf, and use it for our llc header.
249 if (aa->aa_flags & AFA_PHASE2) {
250 struct llc llc;
252 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
253 eh = mtod(m, struct ether_header *);
254 edst = eh->ether_dhost;
255 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
256 llc.llc_control = LLC_UI;
257 bcopy(at_org_code, llc.llc_snap_org_code,
258 sizeof at_org_code);
259 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
260 bcopy(&llc,
261 mtod(m, caddr_t) + sizeof(struct ether_header),
262 sizeof(struct llc));
263 eh->ether_type = htons(m->m_pkthdr.len);
264 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
265 } else {
266 eh->ether_type = htons(ETHERTYPE_AT);
268 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
269 return (0);
270 break;
272 #endif
273 #ifdef NS
274 case AF_NS:
275 switch(ns_nettype) {
276 default:
277 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
278 eh->ether_type = 0x8137;
279 break;
280 case 0x0: /* Novell 802.3 */
281 eh->ether_type = htons(m->m_pkthdr.len);
282 break;
283 case 0xe0e0: /* Novell 802.2 and Token-Ring */
284 M_PREPEND(m, 3, MB_DONTWAIT);
285 eh = mtod(m, struct ether_header *);
286 edst = eh->ether_dhost;
287 eh->ether_type = htons(m->m_pkthdr.len);
288 cp = mtod(m, u_char *) + sizeof(struct ether_header);
289 *cp++ = 0xE0;
290 *cp++ = 0xE0;
291 *cp++ = 0x03;
292 break;
294 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
295 ETHER_ADDR_LEN);
297 * XXX if ns_thishost is the same as the node's ethernet
298 * address then just the default code will catch this anyhow.
299 * So I'm not sure if this next clause should be here at all?
300 * [JRE]
302 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
303 m->m_pkthdr.rcvif = ifp;
304 netisr_dispatch(NETISR_NS, m);
305 return (error);
307 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
308 m->m_flags |= M_BCAST;
309 break;
310 #endif
311 #ifdef MPLS
312 case AF_MPLS:
314 struct sockaddr *sa_gw;
316 if (rt)
317 sa_gw = (struct sockaddr *)rt->rt_gateway;
318 else {
319 /* We realy need a gateway. */
320 m_freem(m);
321 return (0);
324 switch (sa_gw->sa_family) {
325 case AF_INET:
326 if (!arpresolve(ifp, rt, m, sa_gw, edst))
327 return (0);
328 break;
329 default:
330 kprintf("ether_output: address family not supported to forward mpls packets: %d.\n", sa_gw->sa_family);
331 m_freem(m);
332 return (0);
334 eh->ether_type = htons(ETHERTYPE_MPLS); /* XXX how about multicast? */
335 break;
337 #endif
338 case pseudo_AF_HDRCMPLT:
339 case AF_UNSPEC:
340 loop_copy = -1; /* if this is for us, don't do it */
341 deh = (struct ether_header *)dst->sa_data;
342 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
343 eh->ether_type = deh->ether_type;
344 break;
346 default:
347 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
348 gotoerr(EAFNOSUPPORT);
351 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
352 memcpy(eh->ether_shost,
353 ((struct ether_header *)dst->sa_data)->ether_shost,
354 ETHER_ADDR_LEN);
355 else
356 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
359 * Bridges require special output handling.
361 if (ifp->if_bridge) {
362 KASSERT(bridge_output_p != NULL,
363 ("%s: if_bridge not loaded!", __func__));
364 return bridge_output_p(ifp, m);
368 * If a simplex interface, and the packet is being sent to our
369 * Ethernet address or a broadcast address, loopback a copy.
370 * XXX To make a simplex device behave exactly like a duplex
371 * device, we should copy in the case of sending to our own
372 * ethernet address (thus letting the original actually appear
373 * on the wire). However, we don't do that here for security
374 * reasons and compatibility with the original behavior.
376 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
377 int csum_flags = 0;
379 if (m->m_pkthdr.csum_flags & CSUM_IP)
380 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
381 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
382 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
383 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
384 struct mbuf *n;
386 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
387 n->m_pkthdr.csum_flags |= csum_flags;
388 if (csum_flags & CSUM_DATA_VALID)
389 n->m_pkthdr.csum_data = 0xffff;
390 if_simloop(ifp, n, dst->sa_family, hlen);
391 } else
392 ifp->if_iqdrops++;
393 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
394 ETHER_ADDR_LEN) == 0) {
395 m->m_pkthdr.csum_flags |= csum_flags;
396 if (csum_flags & CSUM_DATA_VALID)
397 m->m_pkthdr.csum_data = 0xffff;
398 if_simloop(ifp, m, dst->sa_family, hlen);
399 return (0); /* XXX */
403 #ifdef CARP
404 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL)))
405 goto bad;
406 #endif
409 /* Handle ng_ether(4) processing, if any */
410 if (ng_ether_output_p != NULL) {
411 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
412 goto bad;
413 if (m == NULL)
414 return (0);
417 /* Continue with link-layer output */
418 return ether_output_frame(ifp, m);
420 bad:
421 m_freem(m);
422 return (error);
426 * Ethernet link layer output routine to send a raw frame to the device.
428 * This assumes that the 14 byte Ethernet header is present and contiguous
429 * in the first mbuf.
432 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
434 struct ip_fw *rule = NULL;
435 int error = 0;
436 struct altq_pktattr pktattr;
437 struct m_tag *mtag;
439 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
441 /* Extract info from dummynet tag */
442 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
443 if (mtag != NULL) {
444 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
446 m_tag_delete(m, mtag);
447 mtag = NULL;
450 if (ifq_is_enabled(&ifp->if_snd))
451 altq_etherclassify(&ifp->if_snd, m, &pktattr);
452 crit_enter();
453 if (IPFW_LOADED && ether_ipfw != 0) {
454 struct ether_header save_eh, *eh;
456 eh = mtod(m, struct ether_header *);
457 save_eh = *eh;
458 m_adj(m, ETHER_HDR_LEN);
459 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
460 crit_exit();
461 if (m != NULL) {
462 m_freem(m);
463 return ENOBUFS; /* pkt dropped */
464 } else
465 return 0; /* consumed e.g. in a pipe */
468 /* packet was ok, restore the ethernet header */
469 ether_restore_header(&m, eh, &save_eh);
470 if (m == NULL) {
471 crit_exit();
472 return ENOBUFS;
475 crit_exit();
478 * Queue message on interface, update output statistics if
479 * successful, and start output if interface not yet active.
481 error = ifq_dispatch(ifp, m, &pktattr);
482 return (error);
486 * ipfw processing for ethernet packets (in and out).
487 * The second parameter is NULL from ether_demux(), and ifp from
488 * ether_output_frame().
490 static boolean_t
491 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
492 const struct ether_header *eh)
494 struct ether_header save_eh = *eh; /* might be a ptr in m */
495 struct ip_fw_args args;
496 struct m_tag *mtag;
497 int i;
499 if (*rule != NULL && fw_one_pass)
500 return TRUE; /* dummynet packet, already partially processed */
503 * I need some amount of data to be contiguous.
505 i = min((*m0)->m_pkthdr.len, max_protohdr);
506 if ((*m0)->m_len < i) {
507 *m0 = m_pullup(*m0, i);
508 if (*m0 == NULL)
509 return FALSE;
512 args.m = *m0; /* the packet we are looking at */
513 args.oif = dst; /* destination, if any */
514 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
515 m_tag_delete(*m0, mtag);
516 args.rule = *rule; /* matching rule to restart */
517 args.next_hop = NULL; /* we do not support forward yet */
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;
523 if ((i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL) /* drop */
524 return FALSE;
526 if (i == 0) /* a PASS rule. */
527 return TRUE;
529 if (i & IP_FW_PORT_DYNT_FLAG) {
531 * Pass the pkt to dummynet, which consumes it.
533 struct mbuf *m;
535 m = *m0; /* pass the original to dummynet */
536 *m0 = NULL; /* and nothing back to the caller */
538 ether_restore_header(&m, eh, &save_eh);
539 if (m == NULL)
540 return FALSE;
542 ip_fw_dn_io_ptr(m, (i & 0xffff),
543 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
544 return FALSE;
547 * XXX at some point add support for divert/forward actions.
548 * If none of the above matches, we have to drop the pkt.
550 return FALSE;
554 * Process a received Ethernet packet.
556 * The ethernet header is assumed to be in the mbuf so the caller
557 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
558 * bytes in the first mbuf.
560 * This allows us to concentrate in one place a bunch of code which
561 * is replicated in all device drivers. Also, many functions called
562 * from ether_input() try to put the eh back into the mbuf, so we
563 * can later propagate the 'contiguous packet' interface to them.
565 * NOTA BENE: for all drivers "eh" is a pointer into the first mbuf or
566 * cluster, right before m_data. So be very careful when working on m,
567 * as you could destroy *eh !!
569 * First we perform any link layer operations, then continue to the
570 * upper layers with ether_demux().
572 void
573 ether_input_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
575 struct ether_header *eh;
577 ASSERT_SERIALIZED(ifp->if_serializer);
578 M_ASSERTPKTHDR(m);
580 /* Discard packet if interface is not up */
581 if (!(ifp->if_flags & IFF_UP)) {
582 m_freem(m);
583 return;
586 if (m->m_len < sizeof(struct ether_header)) {
587 /* XXX error in the caller. */
588 m_freem(m);
589 return;
591 eh = mtod(m, struct ether_header *);
593 if (ntohs(eh->ether_type) == ETHERTYPE_VLAN &&
594 (m->m_flags & M_VLANTAG) == 0) {
596 * Extract vlan tag if hardware does not do it for us
598 vlan_ether_decap(&m);
599 if (m == NULL)
600 return;
601 eh = mtod(m, struct ether_header *);
604 m->m_pkthdr.rcvif = ifp;
606 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
607 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
608 ifp->if_addrlen) == 0)
609 m->m_flags |= M_BCAST;
610 else
611 m->m_flags |= M_MCAST;
612 ifp->if_imcasts++;
615 ETHER_BPF_MTAP(ifp, m);
617 ifp->if_ibytes += m->m_pkthdr.len;
619 if (ifp->if_flags & IFF_MONITOR) {
621 * Interface marked for monitoring; discard packet.
623 m_freem(m);
624 return;
628 * Tap the packet off here for a bridge. bridge_input()
629 * will return NULL if it has consumed the packet, otherwise
630 * it gets processed as normal. Note that bridge_input()
631 * will always return the original packet if we need to
632 * process it locally.
634 if (ifp->if_bridge) {
635 KASSERT(bridge_input_p != NULL,
636 ("%s: if_bridge not loaded!", __func__));
638 if(m->m_flags & M_PROTO1) {
639 m->m_flags &= ~M_PROTO1;
640 } else {
641 /* clear M_PROMISC, in case the packets comes from a vlan */
642 /* m->m_flags &= ~M_PROMISC; */
643 lwkt_serialize_exit(ifp->if_serializer);
644 m = bridge_input_p(ifp, m);
645 lwkt_serialize_enter(ifp->if_serializer);
646 if (m == NULL)
647 return;
649 KASSERT(ifp == m->m_pkthdr.rcvif,
650 ("bridge_input_p changed rcvif\n"));
652 /* 'm' may be changed by bridge_input_p() */
653 eh = mtod(m, struct ether_header *);
657 /* Handle ng_ether(4) processing, if any */
658 if (ng_ether_input_p != NULL) {
659 ng_ether_input_p(ifp, &m);
660 if (m == NULL)
661 return;
663 /* 'm' may be changed by ng_ether_input_p() */
664 eh = mtod(m, struct ether_header *);
667 /* Continue with upper layer processing */
668 ether_demux_chain(ifp, m, chain);
671 void
672 ether_input(struct ifnet *ifp, struct mbuf *m)
674 ether_input_chain(ifp, m, NULL);
678 * Upper layer processing for a received Ethernet packet.
680 static void
681 ether_demux_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
683 struct ether_header save_eh, *eh;
684 int isr;
685 u_short ether_type;
686 struct ip_fw *rule = NULL;
687 struct m_tag *mtag;
688 #ifdef NETATALK
689 struct llc *l;
690 #endif
692 M_ASSERTPKTHDR(m);
693 KASSERT(m->m_len >= ETHER_HDR_LEN,
694 ("ether header is no contiguous!\n"));
696 eh = mtod(m, struct ether_header *);
697 save_eh = *eh;
699 /* XXX old crufty stuff, needs to be removed */
700 m_adj(m, sizeof(struct ether_header));
702 /* Extract info from dummynet tag */
703 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
704 if (mtag != NULL) {
705 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
706 KKASSERT(ifp == NULL);
707 ifp = m->m_pkthdr.rcvif;
709 m_tag_delete(m, mtag);
710 mtag = NULL;
712 if (rule) /* packet is passing the second time */
713 goto post_stats;
715 #ifdef CARP
717 * XXX: Okay, we need to call carp_forus() and - if it is for
718 * us jump over code that does the normal check
719 * "ac_enaddr == ether_dhost". The check sequence is a bit
720 * different from OpenBSD, so we jump over as few code as
721 * possible, to catch _all_ sanity checks. This needs
722 * evaluation, to see if the carp ether_dhost values break any
723 * of these checks!
725 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
726 goto post_stats;
727 #endif
730 * Discard packet if upper layers shouldn't see it because
731 * it was unicast to a different Ethernet address. If the
732 * driver is working properly, then this situation can only
733 * happen when the interface is in promiscuous mode.
735 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
736 (eh->ether_dhost[0] & 1) == 0 &&
737 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
738 m_freem(m);
739 return;
742 post_stats:
743 if (IPFW_LOADED && ether_ipfw != 0) {
744 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
745 m_freem(m);
746 return;
750 ether_type = ntohs(save_eh.ether_type);
752 if (m->m_flags & M_VLANTAG) {
753 if (ether_type == ETHERTYPE_VLAN) {
755 * To prevent possible dangerous recursion,
756 * we don't do vlan-in-vlan
758 m->m_pkthdr.rcvif->if_noproto++;
759 m_freem(m);
760 return;
763 if (vlan_input_p != NULL) {
764 ether_restore_header(&m, eh, &save_eh);
765 if (m != NULL)
766 vlan_input_p(m, chain);
767 } else {
768 m->m_pkthdr.rcvif->if_noproto++;
769 m_freem(m);
771 return;
773 KKASSERT(ether_type != ETHERTYPE_VLAN);
775 switch (ether_type) {
776 #ifdef INET
777 case ETHERTYPE_IP:
778 if (ipflow_fastforward(m, ifp->if_serializer))
779 return;
780 isr = NETISR_IP;
781 break;
783 case ETHERTYPE_ARP:
784 if (ifp->if_flags & IFF_NOARP) {
785 /* Discard packet if ARP is disabled on interface */
786 m_freem(m);
787 return;
789 isr = NETISR_ARP;
790 break;
791 #endif
793 #ifdef INET6
794 case ETHERTYPE_IPV6:
795 isr = NETISR_IPV6;
796 break;
797 #endif
799 #ifdef IPX
800 case ETHERTYPE_IPX:
801 if (ef_inputp && ef_inputp(ifp, &save_eh, m) == 0)
802 return;
803 isr = NETISR_IPX;
804 break;
805 #endif
807 #ifdef NS
808 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
809 isr = NETISR_NS;
810 break;
812 #endif
814 #ifdef NETATALK
815 case ETHERTYPE_AT:
816 isr = NETISR_ATALK1;
817 break;
818 case ETHERTYPE_AARP:
819 isr = NETISR_AARP;
820 break;
821 #endif
823 #ifdef MPLS
824 case ETHERTYPE_MPLS:
825 case ETHERTYPE_MPLS_MCAST:
826 isr = NETISR_MPLS;
827 break;
828 #endif
830 default:
831 #ifdef IPX
832 if (ef_inputp && ef_inputp(ifp, &save_eh, m) == 0)
833 return;
834 #endif
835 #ifdef NS
836 checksum = mtod(m, ushort *);
837 /* Novell 802.3 */
838 if ((ether_type <= ETHERMTU) &&
839 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
840 if (*checksum == 0xE0E0) {
841 m->m_pkthdr.len -= 3;
842 m->m_len -= 3;
843 m->m_data += 3;
845 isr = NETISR_NS;
846 break;
848 #endif
849 #ifdef NETATALK
850 if (ether_type > ETHERMTU)
851 goto dropanyway;
852 l = mtod(m, struct llc *);
853 if (l->llc_dsap == LLC_SNAP_LSAP &&
854 l->llc_ssap == LLC_SNAP_LSAP &&
855 l->llc_control == LLC_UI) {
856 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
857 sizeof at_org_code) == 0 &&
858 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
859 m_adj(m, sizeof(struct llc));
860 isr = NETISR_ATALK2;
861 break;
863 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
864 sizeof aarp_org_code) == 0 &&
865 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
866 m_adj(m, sizeof(struct llc));
867 isr = NETISR_AARP;
868 break;
871 dropanyway:
872 #endif
873 if (ng_ether_input_orphan_p != NULL)
874 (*ng_ether_input_orphan_p)(ifp, m, &save_eh);
875 else
876 m_freem(m);
877 return;
880 #ifdef ETHER_INPUT_CHAIN
881 if (chain != NULL) {
882 struct mbuf_chain *c;
883 lwkt_port_t port;
884 int cpuid;
886 port = netisr_mport(isr, &m);
887 if (port == NULL)
888 return;
890 m->m_pkthdr.header = port; /* XXX */
891 cpuid = port->mpu_td->td_gd->gd_cpuid;
893 c = &chain[cpuid];
894 if (c->mc_head == NULL) {
895 c->mc_head = c->mc_tail = m;
896 } else {
897 c->mc_tail->m_nextpkt = m;
898 c->mc_tail = m;
900 m->m_nextpkt = NULL;
901 } else
902 #endif /* ETHER_INPUT_CHAIN */
903 netisr_dispatch(isr, m);
906 void
907 ether_demux(struct ifnet *ifp, struct mbuf *m)
909 ether_demux_chain(ifp, m, NULL);
913 * Perform common duties while attaching to interface list
916 void
917 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
919 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
920 serializer);
923 void
924 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
925 lwkt_serialize_t serializer)
927 struct sockaddr_dl *sdl;
929 ifp->if_type = IFT_ETHER;
930 ifp->if_addrlen = ETHER_ADDR_LEN;
931 ifp->if_hdrlen = ETHER_HDR_LEN;
932 if_attach(ifp, serializer);
933 ifp->if_mtu = ETHERMTU;
934 if (ifp->if_baudrate == 0)
935 ifp->if_baudrate = 10000000;
936 ifp->if_output = ether_output;
937 ifp->if_input = ether_input;
938 ifp->if_resolvemulti = ether_resolvemulti;
939 ifp->if_broadcastaddr = etherbroadcastaddr;
940 sdl = IF_LLSOCKADDR(ifp);
941 sdl->sdl_type = IFT_ETHER;
942 sdl->sdl_alen = ifp->if_addrlen;
943 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
945 * XXX Keep the current drivers happy.
946 * XXX Remove once all drivers have been cleaned up
948 if (lla != IFP2AC(ifp)->ac_enaddr)
949 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
950 bpfattach(ifp, dlt, hdrlen);
951 if (ng_ether_attach_p != NULL)
952 (*ng_ether_attach_p)(ifp);
954 if_printf(ifp, "MAC address: %6D\n", lla, ":");
958 * Perform common duties while detaching an Ethernet interface
960 void
961 ether_ifdetach(struct ifnet *ifp)
963 if_down(ifp);
965 if (ng_ether_detach_p != NULL)
966 (*ng_ether_detach_p)(ifp);
967 bpfdetach(ifp);
968 if_detach(ifp);
972 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
974 struct ifaddr *ifa = (struct ifaddr *) data;
975 struct ifreq *ifr = (struct ifreq *) data;
976 int error = 0;
978 #define IF_INIT(ifp) \
979 do { \
980 if (((ifp)->if_flags & IFF_UP) == 0) { \
981 (ifp)->if_flags |= IFF_UP; \
982 (ifp)->if_init((ifp)->if_softc); \
984 } while (0)
986 ASSERT_SERIALIZED(ifp->if_serializer);
988 switch (command) {
989 case SIOCSIFADDR:
990 switch (ifa->ifa_addr->sa_family) {
991 #ifdef INET
992 case AF_INET:
993 IF_INIT(ifp); /* before arpwhohas */
994 arp_ifinit(ifp, ifa);
995 break;
996 #endif
997 #ifdef IPX
999 * XXX - This code is probably wrong
1001 case AF_IPX:
1003 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
1004 struct arpcom *ac = IFP2AC(ifp);
1006 if (ipx_nullhost(*ina))
1007 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
1008 else
1009 bcopy(ina->x_host.c_host, ac->ac_enaddr,
1010 sizeof ac->ac_enaddr);
1012 IF_INIT(ifp); /* Set new address. */
1013 break;
1015 #endif
1016 #ifdef NS
1018 * XXX - This code is probably wrong
1020 case AF_NS:
1022 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
1023 struct arpcom *ac = IFP2AC(ifp);
1025 if (ns_nullhost(*ina))
1026 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
1027 else
1028 bcopy(ina->x_host.c_host, ac->ac_enaddr,
1029 sizeof ac->ac_enaddr);
1032 * Set new address
1034 IF_INIT(ifp);
1035 break;
1037 #endif
1038 default:
1039 IF_INIT(ifp);
1040 break;
1042 break;
1044 case SIOCGIFADDR:
1045 bcopy(IFP2AC(ifp)->ac_enaddr,
1046 ((struct sockaddr *)ifr->ifr_data)->sa_data,
1047 ETHER_ADDR_LEN);
1048 break;
1050 case SIOCSIFMTU:
1052 * Set the interface MTU.
1054 if (ifr->ifr_mtu > ETHERMTU) {
1055 error = EINVAL;
1056 } else {
1057 ifp->if_mtu = ifr->ifr_mtu;
1059 break;
1060 default:
1061 error = EINVAL;
1062 break;
1064 return (error);
1066 #undef IF_INIT
1070 ether_resolvemulti(
1071 struct ifnet *ifp,
1072 struct sockaddr **llsa,
1073 struct sockaddr *sa)
1075 struct sockaddr_dl *sdl;
1076 struct sockaddr_in *sin;
1077 #ifdef INET6
1078 struct sockaddr_in6 *sin6;
1079 #endif
1080 u_char *e_addr;
1082 switch(sa->sa_family) {
1083 case AF_LINK:
1085 * No mapping needed. Just check that it's a valid MC address.
1087 sdl = (struct sockaddr_dl *)sa;
1088 e_addr = LLADDR(sdl);
1089 if ((e_addr[0] & 1) != 1)
1090 return EADDRNOTAVAIL;
1091 *llsa = 0;
1092 return 0;
1094 #ifdef INET
1095 case AF_INET:
1096 sin = (struct sockaddr_in *)sa;
1097 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1098 return EADDRNOTAVAIL;
1099 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1100 M_WAITOK | M_ZERO);
1101 sdl->sdl_len = sizeof *sdl;
1102 sdl->sdl_family = AF_LINK;
1103 sdl->sdl_index = ifp->if_index;
1104 sdl->sdl_type = IFT_ETHER;
1105 sdl->sdl_alen = ETHER_ADDR_LEN;
1106 e_addr = LLADDR(sdl);
1107 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1108 *llsa = (struct sockaddr *)sdl;
1109 return 0;
1110 #endif
1111 #ifdef INET6
1112 case AF_INET6:
1113 sin6 = (struct sockaddr_in6 *)sa;
1114 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1116 * An IP6 address of 0 means listen to all
1117 * of the Ethernet multicast address used for IP6.
1118 * (This is used for multicast routers.)
1120 ifp->if_flags |= IFF_ALLMULTI;
1121 *llsa = 0;
1122 return 0;
1124 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1125 return EADDRNOTAVAIL;
1126 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1127 M_WAITOK | M_ZERO);
1128 sdl->sdl_len = sizeof *sdl;
1129 sdl->sdl_family = AF_LINK;
1130 sdl->sdl_index = ifp->if_index;
1131 sdl->sdl_type = IFT_ETHER;
1132 sdl->sdl_alen = ETHER_ADDR_LEN;
1133 e_addr = LLADDR(sdl);
1134 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1135 *llsa = (struct sockaddr *)sdl;
1136 return 0;
1137 #endif
1139 default:
1141 * Well, the text isn't quite right, but it's the name
1142 * that counts...
1144 return EAFNOSUPPORT;
1148 #if 0
1150 * This is for reference. We have a table-driven version
1151 * of the little-endian crc32 generator, which is faster
1152 * than the double-loop.
1154 uint32_t
1155 ether_crc32_le(const uint8_t *buf, size_t len)
1157 uint32_t c, crc, carry;
1158 size_t i, j;
1160 crc = 0xffffffffU; /* initial value */
1162 for (i = 0; i < len; i++) {
1163 c = buf[i];
1164 for (j = 0; j < 8; j++) {
1165 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1166 crc >>= 1;
1167 c >>= 1;
1168 if (carry)
1169 crc = (crc ^ ETHER_CRC_POLY_LE);
1173 return (crc);
1175 #else
1176 uint32_t
1177 ether_crc32_le(const uint8_t *buf, size_t len)
1179 static const uint32_t crctab[] = {
1180 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1181 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1182 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1183 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1185 uint32_t crc;
1186 size_t i;
1188 crc = 0xffffffffU; /* initial value */
1190 for (i = 0; i < len; i++) {
1191 crc ^= buf[i];
1192 crc = (crc >> 4) ^ crctab[crc & 0xf];
1193 crc = (crc >> 4) ^ crctab[crc & 0xf];
1196 return (crc);
1198 #endif
1200 uint32_t
1201 ether_crc32_be(const uint8_t *buf, size_t len)
1203 uint32_t c, crc, carry;
1204 size_t i, j;
1206 crc = 0xffffffffU; /* initial value */
1208 for (i = 0; i < len; i++) {
1209 c = buf[i];
1210 for (j = 0; j < 8; j++) {
1211 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1212 crc <<= 1;
1213 c >>= 1;
1214 if (carry)
1215 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1219 return (crc);
1223 * find the size of ethernet header, and call classifier
1225 void
1226 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
1227 struct altq_pktattr *pktattr)
1229 struct ether_header *eh;
1230 uint16_t ether_type;
1231 int hlen, af, hdrsize;
1232 caddr_t hdr;
1234 hlen = sizeof(struct ether_header);
1235 eh = mtod(m, struct ether_header *);
1237 ether_type = ntohs(eh->ether_type);
1238 if (ether_type < ETHERMTU) {
1239 /* ick! LLC/SNAP */
1240 struct llc *llc = (struct llc *)(eh + 1);
1241 hlen += 8;
1243 if (m->m_len < hlen ||
1244 llc->llc_dsap != LLC_SNAP_LSAP ||
1245 llc->llc_ssap != LLC_SNAP_LSAP ||
1246 llc->llc_control != LLC_UI)
1247 goto bad; /* not snap! */
1249 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
1252 if (ether_type == ETHERTYPE_IP) {
1253 af = AF_INET;
1254 hdrsize = 20; /* sizeof(struct ip) */
1255 #ifdef INET6
1256 } else if (ether_type == ETHERTYPE_IPV6) {
1257 af = AF_INET6;
1258 hdrsize = 40; /* sizeof(struct ip6_hdr) */
1259 #endif
1260 } else
1261 goto bad;
1263 while (m->m_len <= hlen) {
1264 hlen -= m->m_len;
1265 m = m->m_next;
1267 hdr = m->m_data + hlen;
1268 if (m->m_len < hlen + hdrsize) {
1270 * ip header is not in a single mbuf. this should not
1271 * happen in the current code.
1272 * (todo: use m_pulldown in the future)
1274 goto bad;
1276 m->m_data += hlen;
1277 m->m_len -= hlen;
1278 ifq_classify(ifq, m, af, pktattr);
1279 m->m_data -= hlen;
1280 m->m_len += hlen;
1282 return;
1284 bad:
1285 pktattr->pattr_class = NULL;
1286 pktattr->pattr_hdr = NULL;
1287 pktattr->pattr_af = AF_UNSPEC;
1290 static void
1291 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
1292 const struct ether_header *save_eh)
1294 struct mbuf *m = *m0;
1296 ether_restore_hdr++;
1299 * Prepend the header, optimize for the common case of
1300 * eh pointing into the mbuf.
1302 if ((const void *)(eh + 1) == (void *)m->m_data) {
1303 m->m_data -= ETHER_HDR_LEN;
1304 m->m_len += ETHER_HDR_LEN;
1305 m->m_pkthdr.len += ETHER_HDR_LEN;
1306 } else {
1307 ether_prepend_hdr++;
1309 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1310 if (m != NULL) {
1311 bcopy(save_eh, mtod(m, struct ether_header *),
1312 ETHER_HDR_LEN);
1315 *m0 = m;
1318 #ifdef ETHER_INPUT_CHAIN
1320 static void
1321 ether_input_ipifunc(void *arg)
1323 struct mbuf *m, *next;
1324 lwkt_port_t port;
1326 m = arg;
1327 do {
1328 next = m->m_nextpkt;
1329 m->m_nextpkt = NULL;
1331 port = m->m_pkthdr.header;
1332 m->m_pkthdr.header = NULL;
1334 lwkt_sendmsg(port,
1335 &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1337 m = next;
1338 } while (m != NULL);
1341 void
1342 ether_input_dispatch(struct mbuf_chain *chain)
1344 #ifdef SMP
1345 int i;
1347 for (i = 0; i < ncpus; ++i) {
1348 if (chain[i].mc_head != NULL) {
1349 lwkt_send_ipiq(globaldata_find(i),
1350 ether_input_ipifunc, chain[i].mc_head);
1353 #else
1354 if (chain->mc_head != NULL)
1355 ether_input_ipifunc(chain->mc_head);
1356 #endif
1359 void
1360 ether_input_chain_init(struct mbuf_chain *chain)
1362 #ifdef SMP
1363 int i;
1365 for (i = 0; i < ncpus; ++i)
1366 chain[i].mc_head = chain[i].mc_tail = NULL;
1367 #else
1368 chain->mc_head = chain->mc_tail = NULL;
1369 #endif
1372 #endif /* ETHER_INPUT_CHAIN */
1374 #ifdef ETHER_INPUT2
1376 static void
1377 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
1379 struct ether_header *eh;
1380 int isr, redispatch;
1381 u_short ether_type;
1382 struct ip_fw *rule = NULL;
1383 struct m_tag *mtag;
1384 #ifdef NETATALK
1385 struct llc *l;
1386 #endif
1388 M_ASSERTPKTHDR(m);
1389 KASSERT(m->m_len >= ETHER_HDR_LEN,
1390 ("ether header is no contiguous!\n"));
1392 eh = mtod(m, struct ether_header *);
1394 /* Extract info from dummynet tag */
1395 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1396 if (mtag != NULL) {
1397 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1398 KKASSERT(ifp == NULL);
1399 ifp = m->m_pkthdr.rcvif;
1401 m_tag_delete(m, mtag);
1402 mtag = NULL;
1404 if (rule) /* packet is passing the second time */
1405 goto post_stats;
1407 #ifdef CARP
1409 * XXX: Okay, we need to call carp_forus() and - if it is for
1410 * us jump over code that does the normal check
1411 * "ac_enaddr == ether_dhost". The check sequence is a bit
1412 * different from OpenBSD, so we jump over as few code as
1413 * possible, to catch _all_ sanity checks. This needs
1414 * evaluation, to see if the carp ether_dhost values break any
1415 * of these checks!
1417 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
1418 goto post_stats;
1419 #endif
1422 * Discard packet if upper layers shouldn't see it because
1423 * it was unicast to a different Ethernet address. If the
1424 * driver is working properly, then this situation can only
1425 * happen when the interface is in promiscuous mode.
1427 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1428 (eh->ether_dhost[0] & 1) == 0 &&
1429 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1430 m_freem(m);
1431 return;
1434 post_stats:
1435 if (IPFW_LOADED && ether_ipfw != 0) {
1436 struct ether_header save_eh = *eh;
1438 /* XXX old crufty stuff, needs to be removed */
1439 m_adj(m, sizeof(struct ether_header));
1441 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1442 m_freem(m);
1443 return;
1446 ether_restore_header(&m, eh, &save_eh);
1447 if (m == NULL)
1448 return;
1449 eh = mtod(m, struct ether_header *);
1452 ether_type = ntohs(eh->ether_type);
1453 KKASSERT(ether_type != ETHERTYPE_VLAN);
1455 if (m->m_flags & M_VLANTAG) {
1456 if (vlan_input2_p != NULL) {
1457 vlan_input2_p(m);
1458 } else {
1459 m->m_pkthdr.rcvif->if_noproto++;
1460 m_freem(m);
1462 return;
1465 m_adj(m, sizeof(struct ether_header));
1466 redispatch = 0;
1468 switch (ether_type) {
1469 #ifdef INET
1470 case ETHERTYPE_IP:
1471 #ifdef notyet
1472 if (ipflow_fastforward(m, ifp->if_serializer))
1473 return;
1474 #endif
1475 isr = NETISR_IP;
1476 break;
1478 case ETHERTYPE_ARP:
1479 if (ifp->if_flags & IFF_NOARP) {
1480 /* Discard packet if ARP is disabled on interface */
1481 m_freem(m);
1482 return;
1484 isr = NETISR_ARP;
1485 break;
1486 #endif
1488 #ifdef INET6
1489 case ETHERTYPE_IPV6:
1490 isr = NETISR_IPV6;
1491 break;
1492 #endif
1494 #ifdef IPX
1495 case ETHERTYPE_IPX:
1496 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
1497 return;
1498 isr = NETISR_IPX;
1499 break;
1500 #endif
1502 #ifdef NS
1503 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1504 isr = NETISR_NS;
1505 break;
1507 #endif
1509 #ifdef NETATALK
1510 case ETHERTYPE_AT:
1511 isr = NETISR_ATALK1;
1512 break;
1513 case ETHERTYPE_AARP:
1514 isr = NETISR_AARP;
1515 break;
1516 #endif
1518 #ifdef MPLS
1519 case ETHERTYPE_MPLS:
1520 case ETHERTYPE_MPLS_MCAST:
1521 isr = NETISR_MPLS;
1522 break;
1523 #endif
1525 default:
1527 * The accurate msgport is not determined before
1528 * we reach here, so redo the dispatching
1530 redispatch = 1;
1531 #ifdef IPX
1532 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
1533 return;
1534 #endif
1535 #ifdef NS
1536 checksum = mtod(m, ushort *);
1537 /* Novell 802.3 */
1538 if ((ether_type <= ETHERMTU) &&
1539 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
1540 if (*checksum == 0xE0E0) {
1541 m->m_pkthdr.len -= 3;
1542 m->m_len -= 3;
1543 m->m_data += 3;
1545 isr = NETISR_NS;
1546 break;
1548 #endif
1549 #ifdef NETATALK
1550 if (ether_type > ETHERMTU)
1551 goto dropanyway;
1552 l = mtod(m, struct llc *);
1553 if (l->llc_dsap == LLC_SNAP_LSAP &&
1554 l->llc_ssap == LLC_SNAP_LSAP &&
1555 l->llc_control == LLC_UI) {
1556 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
1557 sizeof at_org_code) == 0 &&
1558 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
1559 m_adj(m, sizeof(struct llc));
1560 isr = NETISR_ATALK2;
1561 break;
1563 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
1564 sizeof aarp_org_code) == 0 &&
1565 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
1566 m_adj(m, sizeof(struct llc));
1567 isr = NETISR_AARP;
1568 break;
1571 dropanyway:
1572 #endif
1573 if (ng_ether_input_orphan_p != NULL)
1574 ng_ether_input_orphan_p(ifp, m, eh);
1575 else
1576 m_freem(m);
1577 return;
1580 if (!redispatch)
1581 netisr_run(isr, m);
1582 else
1583 netisr_dispatch(isr, m);
1586 void
1587 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1589 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1591 * Receiving interface's flags are changed, when this
1592 * packet is waiting for processing; discard it.
1594 m_freem(m);
1595 return;
1599 * Tap the packet off here for a bridge. bridge_input()
1600 * will return NULL if it has consumed the packet, otherwise
1601 * it gets processed as normal. Note that bridge_input()
1602 * will always return the original packet if we need to
1603 * process it locally.
1605 if (ifp->if_bridge) {
1606 KASSERT(bridge_input_p != NULL,
1607 ("%s: if_bridge not loaded!", __func__));
1609 if(m->m_flags & M_PROTO1) {
1610 m->m_flags &= ~M_PROTO1;
1611 } else {
1612 /* clear M_PROMISC, in case the packets comes from a vlan */
1613 /* m->m_flags &= ~M_PROMISC; */
1614 m = bridge_input_p(ifp, m);
1615 if (m == NULL)
1616 return;
1618 KASSERT(ifp == m->m_pkthdr.rcvif,
1619 ("bridge_input_p changed rcvif\n"));
1623 /* Handle ng_ether(4) processing, if any */
1624 if (ng_ether_input_p != NULL) {
1625 ng_ether_input_p(ifp, &m);
1626 if (m == NULL)
1627 return;
1630 /* Continue with upper layer processing */
1631 ether_demux_oncpu(ifp, m);
1634 static void
1635 ether_input_handler(struct netmsg *nmsg)
1637 struct netmsg_packet *nmp = (struct netmsg_packet *)nmsg;
1638 struct ifnet *ifp;
1639 struct mbuf *m;
1641 m = nmp->nm_packet;
1642 M_ASSERTPKTHDR(m);
1643 ifp = m->m_pkthdr.rcvif;
1645 ether_input_oncpu(ifp, m);
1648 static __inline void
1649 ether_init_netpacket(int num, struct mbuf *m)
1651 struct netmsg_packet *pmsg;
1653 pmsg = &m->m_hdr.mh_netmsg;
1654 netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport, 0,
1655 ether_input_handler);
1656 pmsg->nm_packet = m;
1657 pmsg->nm_netmsg.nm_lmsg.u.ms_result = num;
1660 static __inline struct lwkt_port *
1661 ether_mport(int num, struct mbuf **m0)
1663 struct lwkt_port *port;
1664 struct mbuf *m = *m0;
1666 if (num == NETISR_MAX) {
1668 * All packets whose target msgports can't be
1669 * determined here are dispatched to netisr0,
1670 * where further dispatching may happen.
1672 return cpu_portfn(0);
1675 port = netisr_find_port(num, &m);
1676 if (port == NULL)
1677 return NULL;
1679 *m0 = m;
1680 return port;
1683 void
1684 ether_input_chain2(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
1686 struct ether_header *eh, *save_eh, save_eh0;
1687 struct lwkt_port *port;
1688 uint16_t ether_type;
1689 int isr;
1691 ASSERT_SERIALIZED(ifp->if_serializer);
1692 M_ASSERTPKTHDR(m);
1694 /* Discard packet if interface is not up */
1695 if (!(ifp->if_flags & IFF_UP)) {
1696 m_freem(m);
1697 return;
1700 if (m->m_len < sizeof(struct ether_header)) {
1701 /* XXX error in the caller. */
1702 m_freem(m);
1703 return;
1705 eh = mtod(m, struct ether_header *);
1707 m->m_pkthdr.rcvif = ifp;
1709 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1710 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1711 ifp->if_addrlen) == 0)
1712 m->m_flags |= M_BCAST;
1713 else
1714 m->m_flags |= M_MCAST;
1715 ifp->if_imcasts++;
1718 ETHER_BPF_MTAP(ifp, m);
1720 ifp->if_ibytes += m->m_pkthdr.len;
1722 if (ifp->if_flags & IFF_MONITOR) {
1724 * Interface marked for monitoring; discard packet.
1726 m_freem(m);
1727 return;
1730 if (ntohs(eh->ether_type) == ETHERTYPE_VLAN &&
1731 (m->m_flags & M_VLANTAG) == 0) {
1733 * Extract vlan tag if hardware does not do it for us
1735 vlan_ether_decap(&m);
1736 if (m == NULL)
1737 return;
1738 eh = mtod(m, struct ether_header *);
1740 ether_type = ntohs(eh->ether_type);
1742 if ((m->m_flags & M_VLANTAG) && ether_type == ETHERTYPE_VLAN) {
1744 * To prevent possible dangerous recursion,
1745 * we don't do vlan-in-vlan
1747 ifp->if_noproto++;
1748 m_freem(m);
1749 return;
1751 KKASSERT(ether_type != ETHERTYPE_VLAN);
1754 * Map ether type to netisr id.
1756 switch (ether_type) {
1757 #ifdef INET
1758 case ETHERTYPE_IP:
1759 isr = NETISR_IP;
1760 break;
1762 case ETHERTYPE_ARP:
1763 isr = NETISR_ARP;
1764 break;
1765 #endif
1767 #ifdef INET6
1768 case ETHERTYPE_IPV6:
1769 isr = NETISR_IPV6;
1770 break;
1771 #endif
1773 #ifdef IPX
1774 case ETHERTYPE_IPX:
1775 isr = NETISR_IPX;
1776 break;
1777 #endif
1779 #ifdef NS
1780 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1781 isr = NETISR_NS;
1782 break;
1783 #endif
1785 #ifdef NETATALK
1786 case ETHERTYPE_AT:
1787 isr = NETISR_ATALK1;
1788 break;
1789 case ETHERTYPE_AARP:
1790 isr = NETISR_AARP;
1791 break;
1792 #endif
1794 #ifdef MPLS
1795 case ETHERTYPE_MPLS:
1796 case ETHERTYPE_MPLS_MCAST:
1797 isr = NETISR_MPLS;
1798 break;
1799 #endif
1801 default:
1803 * NETISR_MAX is an invalid value; it is chosen to let
1804 * ether_mport() know that we are not able to decide
1805 * this packet's msgport here.
1807 isr = NETISR_MAX;
1808 break;
1812 * If the packet is in contiguous memory, following
1813 * m_adj() could ensure that the hidden ether header
1814 * will not be destroyed, else we will have to save
1815 * the ether header for the later restoration.
1817 if (m->m_pkthdr.len != m->m_len) {
1818 save_eh0 = *eh;
1819 save_eh = &save_eh0;
1820 } else {
1821 save_eh = NULL;
1825 * Temporarily remove ether header; ether_mport()
1826 * expects a packet without ether header.
1828 m_adj(m, sizeof(struct ether_header));
1831 * Find the packet's target msgport.
1833 port = ether_mport(isr, &m);
1834 if (port == NULL) {
1835 KKASSERT(m == NULL);
1836 return;
1840 * Restore ether header.
1842 if (save_eh != NULL) {
1843 ether_restore_header(&m, eh, save_eh);
1844 if (m == NULL)
1845 return;
1846 } else {
1847 m->m_data -= ETHER_HDR_LEN;
1848 m->m_len += ETHER_HDR_LEN;
1849 m->m_pkthdr.len += ETHER_HDR_LEN;
1853 * Initialize mbuf's netmsg packet _after_ possible
1854 * ether header restoration, else the initialized
1855 * netmsg packet may be lost during ether header
1856 * restoration.
1858 ether_init_netpacket(isr, m);
1860 #ifdef ETHER_INPUT_CHAIN
1861 if (chain != NULL) {
1862 struct mbuf_chain *c;
1863 int cpuid;
1865 m->m_pkthdr.header = port; /* XXX */
1866 cpuid = port->mpu_td->td_gd->gd_cpuid;
1868 c = &chain[cpuid];
1869 if (c->mc_head == NULL) {
1870 c->mc_head = c->mc_tail = m;
1871 } else {
1872 c->mc_tail->m_nextpkt = m;
1873 c->mc_tail = m;
1875 m->m_nextpkt = NULL;
1876 } else
1877 #endif /* ETHER_INPUT_CHAIN */
1878 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1881 #endif /* ETHER_INPUT2 */