Put ifnet.if_serializer assertion under FIX_SERIALIZE_ASSERT
[dragonfly.git] / sys / net / if_ethersubr.c
blobcd06c9ad634622a56e8094feecfe4e86b7dd9461
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.96 2008/11/22 04:00:53 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_rss.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/globaldata.h>
50 #include <sys/kernel.h>
51 #include <sys/ktr.h>
52 #include <sys/lock.h>
53 #include <sys/malloc.h>
54 #include <sys/mbuf.h>
55 #include <sys/msgport.h>
56 #include <sys/socket.h>
57 #include <sys/sockio.h>
58 #include <sys/sysctl.h>
59 #include <sys/thread.h>
60 #include <sys/thread2.h>
62 #include <net/if.h>
63 #include <net/netisr.h>
64 #include <net/route.h>
65 #include <net/if_llc.h>
66 #include <net/if_dl.h>
67 #include <net/if_types.h>
68 #include <net/ifq_var.h>
69 #include <net/bpf.h>
70 #include <net/ethernet.h>
71 #include <net/vlan/if_vlan_ether.h>
72 #include <net/netmsg2.h>
74 #if defined(INET) || defined(INET6)
75 #include <netinet/in.h>
76 #include <netinet/ip_var.h>
77 #include <netinet/if_ether.h>
78 #include <netinet/ip_flow.h>
79 #include <net/ipfw/ip_fw.h>
80 #include <net/dummynet/ip_dummynet.h>
81 #endif
82 #ifdef INET6
83 #include <netinet6/nd6.h>
84 #endif
86 #ifdef CARP
87 #include <netinet/ip_carp.h>
88 #endif
90 #ifdef IPX
91 #include <netproto/ipx/ipx.h>
92 #include <netproto/ipx/ipx_if.h>
93 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
94 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
95 short *tp, int *hlen);
96 #endif
98 #ifdef NS
99 #include <netns/ns.h>
100 #include <netns/ns_if.h>
101 ushort ns_nettype;
102 int ether_outputdebug = 0;
103 int ether_inputdebug = 0;
104 #endif
106 #ifdef NETATALK
107 #include <netproto/atalk/at.h>
108 #include <netproto/atalk/at_var.h>
109 #include <netproto/atalk/at_extern.h>
111 #define llc_snap_org_code llc_un.type_snap.org_code
112 #define llc_snap_ether_type llc_un.type_snap.ether_type
114 extern u_char at_org_code[3];
115 extern u_char aarp_org_code[3];
116 #endif /* NETATALK */
118 #ifdef MPLS
119 #include <netproto/mpls/mpls.h>
120 #endif
122 /* netgraph node hooks for ng_ether(4) */
123 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
124 void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
125 struct mbuf *m, const struct ether_header *eh);
126 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
127 void (*ng_ether_attach_p)(struct ifnet *ifp);
128 void (*ng_ether_detach_p)(struct ifnet *ifp);
130 void (*vlan_input_p)(struct mbuf *);
132 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
133 struct rtentry *);
134 static void ether_restore_header(struct mbuf **, const struct ether_header *,
135 const struct ether_header *);
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 #ifdef RSS_DEBUG
163 static u_int ether_pktinfo_try;
164 static u_int ether_pktinfo_hit;
165 static u_int ether_rss_nopi;
166 static u_int ether_rss_nohash;
167 #endif
169 SYSCTL_DECL(_net_link);
170 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
171 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
172 &ether_ipfw, 0, "Pass ether pkts through firewall");
173 SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
174 &ether_restore_hdr, 0, "# of ether header restoration");
175 SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
176 &ether_prepend_hdr, 0,
177 "# of ether header restoration which prepends mbuf");
178 #ifdef RSS_DEBUG
179 SYSCTL_UINT(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
180 &ether_rss_nopi, 0, "# of packets do not have pktinfo");
181 SYSCTL_UINT(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
182 &ether_rss_nohash, 0, "# of packets do not have hash");
183 SYSCTL_UINT(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
184 &ether_pktinfo_try, 0,
185 "# of tries to find packets' msgport using pktinfo");
186 SYSCTL_UINT(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
187 &ether_pktinfo_hit, 0,
188 "# of packets whose msgport are found using pktinfo");
189 #endif
191 #define ETHER_KTR_STR "ifp=%p"
192 #define ETHER_KTR_ARG_SIZE (sizeof(void *))
193 #ifndef KTR_ETHERNET
194 #define KTR_ETHERNET KTR_ALL
195 #endif
196 KTR_INFO_MASTER(ether);
197 KTR_INFO(KTR_ETHERNET, ether, chain_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
198 KTR_INFO(KTR_ETHERNET, ether, chain_end, 1, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
199 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
200 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARG_SIZE);
201 #define logether(name, arg) KTR_LOG(ether_ ## name, arg)
204 * Ethernet output routine.
205 * Encapsulate a packet of type family for the local net.
206 * Use trailer local net encapsulation if enough data in first
207 * packet leaves a multiple of 512 bytes of data in remainder.
208 * Assumes that ifp is actually pointer to arpcom structure.
210 static int
211 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
212 struct rtentry *rt)
214 struct ether_header *eh, *deh;
215 u_char *edst;
216 int loop_copy = 0;
217 int hlen = ETHER_HDR_LEN; /* link layer header length */
218 struct arpcom *ac = IFP2AC(ifp);
219 int error;
221 #ifdef FIX_SERIALIZE_ASSERT
222 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
223 #endif
225 if (ifp->if_flags & IFF_MONITOR)
226 gotoerr(ENETDOWN);
227 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
228 gotoerr(ENETDOWN);
230 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
231 if (m == NULL)
232 return (ENOBUFS);
233 eh = mtod(m, struct ether_header *);
234 edst = eh->ether_dhost;
237 * Fill in the destination ethernet address and frame type.
239 switch (dst->sa_family) {
240 #ifdef INET
241 case AF_INET:
242 if (!arpresolve(ifp, rt, m, dst, edst))
243 return (0); /* if not yet resolved */
244 #ifdef MPLS
245 if (m->m_flags & M_MPLSLABELED)
246 eh->ether_type = htons(ETHERTYPE_MPLS);
247 else
248 #endif
249 eh->ether_type = htons(ETHERTYPE_IP);
250 break;
251 #endif
252 #ifdef INET6
253 case AF_INET6:
254 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
255 return (0); /* Something bad happenned. */
256 eh->ether_type = htons(ETHERTYPE_IPV6);
257 break;
258 #endif
259 #ifdef IPX
260 case AF_IPX:
261 if (ef_outputp != NULL) {
263 * Hold BGL and recheck ef_outputp
265 get_mplock();
266 if (ef_outputp != NULL) {
267 error = ef_outputp(ifp, &m, dst,
268 &eh->ether_type, &hlen);
269 rel_mplock();
270 if (error)
271 goto bad;
272 else
273 break;
275 rel_mplock();
277 eh->ether_type = htons(ETHERTYPE_IPX);
278 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
279 edst, ETHER_ADDR_LEN);
280 break;
281 #endif
282 #ifdef NETATALK
283 case AF_APPLETALK: {
284 struct at_ifaddr *aa;
287 * Hold BGL
289 get_mplock();
291 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
292 error = 0; /* XXX */
293 rel_mplock();
294 goto bad;
297 * In the phase 2 case, need to prepend an mbuf for
298 * the llc header. Since we must preserve the value
299 * of m, which is passed to us by value, we m_copy()
300 * the first mbuf, and use it for our llc header.
302 if (aa->aa_flags & AFA_PHASE2) {
303 struct llc llc;
305 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
306 eh = mtod(m, struct ether_header *);
307 edst = eh->ether_dhost;
308 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
309 llc.llc_control = LLC_UI;
310 bcopy(at_org_code, llc.llc_snap_org_code,
311 sizeof at_org_code);
312 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
313 bcopy(&llc,
314 mtod(m, caddr_t) + sizeof(struct ether_header),
315 sizeof(struct llc));
316 eh->ether_type = htons(m->m_pkthdr.len);
317 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
318 } else {
319 eh->ether_type = htons(ETHERTYPE_AT);
321 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst)) {
322 rel_mplock();
323 return (0);
326 rel_mplock();
327 break;
329 #endif
330 #ifdef NS
331 case AF_NS:
332 switch(ns_nettype) {
333 default:
334 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
335 eh->ether_type = 0x8137;
336 break;
337 case 0x0: /* Novell 802.3 */
338 eh->ether_type = htons(m->m_pkthdr.len);
339 break;
340 case 0xe0e0: /* Novell 802.2 and Token-Ring */
341 M_PREPEND(m, 3, MB_DONTWAIT);
342 eh = mtod(m, struct ether_header *);
343 edst = eh->ether_dhost;
344 eh->ether_type = htons(m->m_pkthdr.len);
345 cp = mtod(m, u_char *) + sizeof(struct ether_header);
346 *cp++ = 0xE0;
347 *cp++ = 0xE0;
348 *cp++ = 0x03;
349 break;
351 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
352 ETHER_ADDR_LEN);
354 * XXX if ns_thishost is the same as the node's ethernet
355 * address then just the default code will catch this anyhow.
356 * So I'm not sure if this next clause should be here at all?
357 * [JRE]
359 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
360 m->m_pkthdr.rcvif = ifp;
361 netisr_dispatch(NETISR_NS, m);
362 return (error);
364 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
365 m->m_flags |= M_BCAST;
366 break;
367 #endif
368 case pseudo_AF_HDRCMPLT:
369 case AF_UNSPEC:
370 loop_copy = -1; /* if this is for us, don't do it */
371 deh = (struct ether_header *)dst->sa_data;
372 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
373 eh->ether_type = deh->ether_type;
374 break;
376 default:
377 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
378 gotoerr(EAFNOSUPPORT);
381 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
382 memcpy(eh->ether_shost,
383 ((struct ether_header *)dst->sa_data)->ether_shost,
384 ETHER_ADDR_LEN);
385 else
386 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
389 * Bridges require special output handling.
391 if (ifp->if_bridge) {
392 KASSERT(bridge_output_p != NULL,
393 ("%s: if_bridge not loaded!", __func__));
394 return bridge_output_p(ifp, m);
398 * If a simplex interface, and the packet is being sent to our
399 * Ethernet address or a broadcast address, loopback a copy.
400 * XXX To make a simplex device behave exactly like a duplex
401 * device, we should copy in the case of sending to our own
402 * ethernet address (thus letting the original actually appear
403 * on the wire). However, we don't do that here for security
404 * reasons and compatibility with the original behavior.
406 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
407 int csum_flags = 0;
409 if (m->m_pkthdr.csum_flags & CSUM_IP)
410 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
411 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
412 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
413 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
414 struct mbuf *n;
416 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
417 n->m_pkthdr.csum_flags |= csum_flags;
418 if (csum_flags & CSUM_DATA_VALID)
419 n->m_pkthdr.csum_data = 0xffff;
420 if_simloop(ifp, n, dst->sa_family, hlen);
421 } else
422 ifp->if_iqdrops++;
423 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
424 ETHER_ADDR_LEN) == 0) {
425 m->m_pkthdr.csum_flags |= csum_flags;
426 if (csum_flags & CSUM_DATA_VALID)
427 m->m_pkthdr.csum_data = 0xffff;
428 if_simloop(ifp, m, dst->sa_family, hlen);
429 return (0); /* XXX */
433 #ifdef CARP
434 if (ifp->if_carp) {
436 * Hold BGL and recheck ifp->if_carp
438 get_mplock();
439 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL))) {
440 rel_mplock();
441 goto bad;
443 rel_mplock();
445 #endif
448 /* Handle ng_ether(4) processing, if any */
449 if (ng_ether_output_p != NULL) {
451 * Hold BGL and recheck ng_ether_output_p
453 get_mplock();
454 if (ng_ether_output_p != NULL) {
455 if ((error = ng_ether_output_p(ifp, &m)) != 0) {
456 rel_mplock();
457 goto bad;
459 if (m == NULL) {
460 rel_mplock();
461 return (0);
464 rel_mplock();
467 /* Continue with link-layer output */
468 return ether_output_frame(ifp, m);
470 bad:
471 m_freem(m);
472 return (error);
476 * Ethernet link layer output routine to send a raw frame to the device.
478 * This assumes that the 14 byte Ethernet header is present and contiguous
479 * in the first mbuf.
482 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
484 struct ip_fw *rule = NULL;
485 int error = 0;
486 struct altq_pktattr pktattr;
488 #ifdef FIX_SERIALIZE_ASSERT
489 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
490 #endif
492 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
493 struct m_tag *mtag;
495 /* Extract info from dummynet tag */
496 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
497 KKASSERT(mtag != NULL);
498 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
499 KKASSERT(rule != NULL);
501 m_tag_delete(m, mtag);
502 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
505 if (ifq_is_enabled(&ifp->if_snd))
506 altq_etherclassify(&ifp->if_snd, m, &pktattr);
507 crit_enter();
508 if (IPFW_LOADED && ether_ipfw != 0) {
509 struct ether_header save_eh, *eh;
511 eh = mtod(m, struct ether_header *);
512 save_eh = *eh;
513 m_adj(m, ETHER_HDR_LEN);
514 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
515 crit_exit();
516 if (m != NULL) {
517 m_freem(m);
518 return ENOBUFS; /* pkt dropped */
519 } else
520 return 0; /* consumed e.g. in a pipe */
523 /* packet was ok, restore the ethernet header */
524 ether_restore_header(&m, eh, &save_eh);
525 if (m == NULL) {
526 crit_exit();
527 return ENOBUFS;
530 crit_exit();
533 * Queue message on interface, update output statistics if
534 * successful, and start output if interface not yet active.
536 error = ifq_dispatch(ifp, m, &pktattr);
537 return (error);
541 * ipfw processing for ethernet packets (in and out).
542 * The second parameter is NULL from ether_demux(), and ifp from
543 * ether_output_frame().
545 static boolean_t
546 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
547 const struct ether_header *eh)
549 struct ether_header save_eh = *eh; /* might be a ptr in *m0 */
550 struct ip_fw_args args;
551 struct m_tag *mtag;
552 struct mbuf *m;
553 int i;
555 if (*rule != NULL && fw_one_pass)
556 return TRUE; /* dummynet packet, already partially processed */
559 * I need some amount of data to be contiguous.
561 i = min((*m0)->m_pkthdr.len, max_protohdr);
562 if ((*m0)->m_len < i) {
563 *m0 = m_pullup(*m0, i);
564 if (*m0 == NULL)
565 return FALSE;
569 * Clean up tags
571 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
572 m_tag_delete(*m0, mtag);
573 if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
574 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
575 KKASSERT(mtag != NULL);
576 m_tag_delete(*m0, mtag);
577 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
580 args.m = *m0; /* the packet we are looking at */
581 args.oif = dst; /* destination, if any */
582 args.rule = *rule; /* matching rule to restart */
583 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
584 i = ip_fw_chk_ptr(&args);
585 *m0 = args.m;
586 *rule = args.rule;
588 if (*m0 == NULL)
589 return FALSE;
591 switch (i) {
592 case IP_FW_PASS:
593 return TRUE;
595 case IP_FW_DIVERT:
596 case IP_FW_TEE:
597 case IP_FW_DENY:
599 * XXX at some point add support for divert/forward actions.
600 * If none of the above matches, we have to drop the pkt.
602 return FALSE;
604 case IP_FW_DUMMYNET:
606 * Pass the pkt to dummynet, which consumes it.
608 m = *m0; /* pass the original to dummynet */
609 *m0 = NULL; /* and nothing back to the caller */
611 ether_restore_header(&m, eh, &save_eh);
612 if (m == NULL)
613 return FALSE;
615 ip_fw_dn_io_ptr(m, args.cookie,
616 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
617 ip_dn_queue(m);
618 return FALSE;
620 default:
621 panic("unknown ipfw return value: %d\n", i);
625 static void
626 ether_input(struct ifnet *ifp, struct mbuf *m)
628 ether_input_chain(ifp, m, NULL, NULL);
632 * Perform common duties while attaching to interface list
634 void
635 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
637 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
638 serializer);
641 void
642 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
643 lwkt_serialize_t serializer)
645 struct sockaddr_dl *sdl;
647 ifp->if_type = IFT_ETHER;
648 ifp->if_addrlen = ETHER_ADDR_LEN;
649 ifp->if_hdrlen = ETHER_HDR_LEN;
650 if_attach(ifp, serializer);
651 ifp->if_mtu = ETHERMTU;
652 if (ifp->if_baudrate == 0)
653 ifp->if_baudrate = 10000000;
654 ifp->if_output = ether_output;
655 ifp->if_input = ether_input;
656 ifp->if_resolvemulti = ether_resolvemulti;
657 ifp->if_broadcastaddr = etherbroadcastaddr;
658 sdl = IF_LLSOCKADDR(ifp);
659 sdl->sdl_type = IFT_ETHER;
660 sdl->sdl_alen = ifp->if_addrlen;
661 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
663 * XXX Keep the current drivers happy.
664 * XXX Remove once all drivers have been cleaned up
666 if (lla != IFP2AC(ifp)->ac_enaddr)
667 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
668 bpfattach(ifp, dlt, hdrlen);
669 if (ng_ether_attach_p != NULL)
670 (*ng_ether_attach_p)(ifp);
672 if_printf(ifp, "MAC address: %6D\n", lla, ":");
676 * Perform common duties while detaching an Ethernet interface
678 void
679 ether_ifdetach(struct ifnet *ifp)
681 if_down(ifp);
683 if (ng_ether_detach_p != NULL)
684 (*ng_ether_detach_p)(ifp);
685 bpfdetach(ifp);
686 if_detach(ifp);
690 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
692 struct ifaddr *ifa = (struct ifaddr *) data;
693 struct ifreq *ifr = (struct ifreq *) data;
694 int error = 0;
696 #define IF_INIT(ifp) \
697 do { \
698 if (((ifp)->if_flags & IFF_UP) == 0) { \
699 (ifp)->if_flags |= IFF_UP; \
700 (ifp)->if_init((ifp)->if_softc); \
702 } while (0)
704 #ifdef FIX_SERIALIZE_ASSERT
705 ASSERT_SERIALIZED(ifp->if_serializer);
706 #endif
708 switch (command) {
709 case SIOCSIFADDR:
710 switch (ifa->ifa_addr->sa_family) {
711 #ifdef INET
712 case AF_INET:
713 IF_INIT(ifp); /* before arpwhohas */
714 arp_ifinit(ifp, ifa);
715 break;
716 #endif
717 #ifdef IPX
719 * XXX - This code is probably wrong
721 case AF_IPX:
723 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
724 struct arpcom *ac = IFP2AC(ifp);
726 if (ipx_nullhost(*ina))
727 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
728 else
729 bcopy(ina->x_host.c_host, ac->ac_enaddr,
730 sizeof ac->ac_enaddr);
732 IF_INIT(ifp); /* Set new address. */
733 break;
735 #endif
736 #ifdef NS
738 * XXX - This code is probably wrong
740 case AF_NS:
742 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
743 struct arpcom *ac = IFP2AC(ifp);
745 if (ns_nullhost(*ina))
746 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
747 else
748 bcopy(ina->x_host.c_host, ac->ac_enaddr,
749 sizeof ac->ac_enaddr);
752 * Set new address
754 IF_INIT(ifp);
755 break;
757 #endif
758 default:
759 IF_INIT(ifp);
760 break;
762 break;
764 case SIOCGIFADDR:
765 bcopy(IFP2AC(ifp)->ac_enaddr,
766 ((struct sockaddr *)ifr->ifr_data)->sa_data,
767 ETHER_ADDR_LEN);
768 break;
770 case SIOCSIFMTU:
772 * Set the interface MTU.
774 if (ifr->ifr_mtu > ETHERMTU) {
775 error = EINVAL;
776 } else {
777 ifp->if_mtu = ifr->ifr_mtu;
779 break;
780 default:
781 error = EINVAL;
782 break;
784 return (error);
786 #undef IF_INIT
790 ether_resolvemulti(
791 struct ifnet *ifp,
792 struct sockaddr **llsa,
793 struct sockaddr *sa)
795 struct sockaddr_dl *sdl;
796 struct sockaddr_in *sin;
797 #ifdef INET6
798 struct sockaddr_in6 *sin6;
799 #endif
800 u_char *e_addr;
802 switch(sa->sa_family) {
803 case AF_LINK:
805 * No mapping needed. Just check that it's a valid MC address.
807 sdl = (struct sockaddr_dl *)sa;
808 e_addr = LLADDR(sdl);
809 if ((e_addr[0] & 1) != 1)
810 return EADDRNOTAVAIL;
811 *llsa = 0;
812 return 0;
814 #ifdef INET
815 case AF_INET:
816 sin = (struct sockaddr_in *)sa;
817 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
818 return EADDRNOTAVAIL;
819 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
820 M_WAITOK | M_ZERO);
821 sdl->sdl_len = sizeof *sdl;
822 sdl->sdl_family = AF_LINK;
823 sdl->sdl_index = ifp->if_index;
824 sdl->sdl_type = IFT_ETHER;
825 sdl->sdl_alen = ETHER_ADDR_LEN;
826 e_addr = LLADDR(sdl);
827 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
828 *llsa = (struct sockaddr *)sdl;
829 return 0;
830 #endif
831 #ifdef INET6
832 case AF_INET6:
833 sin6 = (struct sockaddr_in6 *)sa;
834 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
836 * An IP6 address of 0 means listen to all
837 * of the Ethernet multicast address used for IP6.
838 * (This is used for multicast routers.)
840 ifp->if_flags |= IFF_ALLMULTI;
841 *llsa = 0;
842 return 0;
844 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
845 return EADDRNOTAVAIL;
846 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
847 M_WAITOK | M_ZERO);
848 sdl->sdl_len = sizeof *sdl;
849 sdl->sdl_family = AF_LINK;
850 sdl->sdl_index = ifp->if_index;
851 sdl->sdl_type = IFT_ETHER;
852 sdl->sdl_alen = ETHER_ADDR_LEN;
853 e_addr = LLADDR(sdl);
854 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
855 *llsa = (struct sockaddr *)sdl;
856 return 0;
857 #endif
859 default:
861 * Well, the text isn't quite right, but it's the name
862 * that counts...
864 return EAFNOSUPPORT;
868 #if 0
870 * This is for reference. We have a table-driven version
871 * of the little-endian crc32 generator, which is faster
872 * than the double-loop.
874 uint32_t
875 ether_crc32_le(const uint8_t *buf, size_t len)
877 uint32_t c, crc, carry;
878 size_t i, j;
880 crc = 0xffffffffU; /* initial value */
882 for (i = 0; i < len; i++) {
883 c = buf[i];
884 for (j = 0; j < 8; j++) {
885 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
886 crc >>= 1;
887 c >>= 1;
888 if (carry)
889 crc = (crc ^ ETHER_CRC_POLY_LE);
893 return (crc);
895 #else
896 uint32_t
897 ether_crc32_le(const uint8_t *buf, size_t len)
899 static const uint32_t crctab[] = {
900 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
901 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
902 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
903 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
905 uint32_t crc;
906 size_t i;
908 crc = 0xffffffffU; /* initial value */
910 for (i = 0; i < len; i++) {
911 crc ^= buf[i];
912 crc = (crc >> 4) ^ crctab[crc & 0xf];
913 crc = (crc >> 4) ^ crctab[crc & 0xf];
916 return (crc);
918 #endif
920 uint32_t
921 ether_crc32_be(const uint8_t *buf, size_t len)
923 uint32_t c, crc, carry;
924 size_t i, j;
926 crc = 0xffffffffU; /* initial value */
928 for (i = 0; i < len; i++) {
929 c = buf[i];
930 for (j = 0; j < 8; j++) {
931 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
932 crc <<= 1;
933 c >>= 1;
934 if (carry)
935 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
939 return (crc);
943 * find the size of ethernet header, and call classifier
945 void
946 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
947 struct altq_pktattr *pktattr)
949 struct ether_header *eh;
950 uint16_t ether_type;
951 int hlen, af, hdrsize;
952 caddr_t hdr;
954 hlen = sizeof(struct ether_header);
955 eh = mtod(m, struct ether_header *);
957 ether_type = ntohs(eh->ether_type);
958 if (ether_type < ETHERMTU) {
959 /* ick! LLC/SNAP */
960 struct llc *llc = (struct llc *)(eh + 1);
961 hlen += 8;
963 if (m->m_len < hlen ||
964 llc->llc_dsap != LLC_SNAP_LSAP ||
965 llc->llc_ssap != LLC_SNAP_LSAP ||
966 llc->llc_control != LLC_UI)
967 goto bad; /* not snap! */
969 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
972 if (ether_type == ETHERTYPE_IP) {
973 af = AF_INET;
974 hdrsize = 20; /* sizeof(struct ip) */
975 #ifdef INET6
976 } else if (ether_type == ETHERTYPE_IPV6) {
977 af = AF_INET6;
978 hdrsize = 40; /* sizeof(struct ip6_hdr) */
979 #endif
980 } else
981 goto bad;
983 while (m->m_len <= hlen) {
984 hlen -= m->m_len;
985 m = m->m_next;
987 hdr = m->m_data + hlen;
988 if (m->m_len < hlen + hdrsize) {
990 * ip header is not in a single mbuf. this should not
991 * happen in the current code.
992 * (todo: use m_pulldown in the future)
994 goto bad;
996 m->m_data += hlen;
997 m->m_len -= hlen;
998 ifq_classify(ifq, m, af, pktattr);
999 m->m_data -= hlen;
1000 m->m_len += hlen;
1002 return;
1004 bad:
1005 pktattr->pattr_class = NULL;
1006 pktattr->pattr_hdr = NULL;
1007 pktattr->pattr_af = AF_UNSPEC;
1010 static void
1011 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
1012 const struct ether_header *save_eh)
1014 struct mbuf *m = *m0;
1016 ether_restore_hdr++;
1019 * Prepend the header, optimize for the common case of
1020 * eh pointing into the mbuf.
1022 if ((const void *)(eh + 1) == (void *)m->m_data) {
1023 m->m_data -= ETHER_HDR_LEN;
1024 m->m_len += ETHER_HDR_LEN;
1025 m->m_pkthdr.len += ETHER_HDR_LEN;
1026 } else {
1027 ether_prepend_hdr++;
1029 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1030 if (m != NULL) {
1031 bcopy(save_eh, mtod(m, struct ether_header *),
1032 ETHER_HDR_LEN);
1035 *m0 = m;
1038 static void
1039 ether_input_ipifunc(void *arg)
1041 struct mbuf *m, *next;
1042 lwkt_port_t port;
1044 m = arg;
1045 do {
1046 next = m->m_nextpkt;
1047 m->m_nextpkt = NULL;
1049 port = m->m_pkthdr.header;
1050 m->m_pkthdr.header = NULL;
1052 lwkt_sendmsg(port,
1053 &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1055 m = next;
1056 } while (m != NULL);
1059 void
1060 ether_input_dispatch(struct mbuf_chain *chain)
1062 #ifdef SMP
1063 int i;
1065 logether(disp_beg, NULL);
1066 for (i = 0; i < ncpus; ++i) {
1067 if (chain[i].mc_head != NULL) {
1068 lwkt_send_ipiq(globaldata_find(i),
1069 ether_input_ipifunc, chain[i].mc_head);
1072 #else
1073 logether(disp_beg, NULL);
1074 if (chain->mc_head != NULL)
1075 ether_input_ipifunc(chain->mc_head);
1076 #endif
1077 logether(disp_end, NULL);
1080 void
1081 ether_input_chain_init(struct mbuf_chain *chain)
1083 #ifdef SMP
1084 int i;
1086 for (i = 0; i < ncpus; ++i)
1087 chain[i].mc_head = chain[i].mc_tail = NULL;
1088 #else
1089 chain->mc_head = chain->mc_tail = NULL;
1090 #endif
1094 * Upper layer processing for a received Ethernet packet.
1096 void
1097 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
1099 struct ether_header *eh;
1100 int isr, redispatch, discard = 0;
1101 u_short ether_type;
1102 struct ip_fw *rule = NULL;
1103 #ifdef NETATALK
1104 struct llc *l;
1105 #endif
1107 M_ASSERTPKTHDR(m);
1108 KASSERT(m->m_len >= ETHER_HDR_LEN,
1109 ("ether header is no contiguous!\n"));
1111 eh = mtod(m, struct ether_header *);
1113 if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
1114 struct m_tag *mtag;
1116 /* Extract info from dummynet tag */
1117 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1118 KKASSERT(mtag != NULL);
1119 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1120 KKASSERT(rule != NULL);
1122 m_tag_delete(m, mtag);
1123 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1125 /* packet is passing the second time */
1126 goto post_stats;
1129 #ifdef CARP
1131 * XXX: Okay, we need to call carp_forus() and - if it is for
1132 * us jump over code that does the normal check
1133 * "ac_enaddr == ether_dhost". The check sequence is a bit
1134 * different from OpenBSD, so we jump over as few code as
1135 * possible, to catch _all_ sanity checks. This needs
1136 * evaluation, to see if the carp ether_dhost values break any
1137 * of these checks!
1139 if (ifp->if_carp) {
1141 * Hold BGL and recheck ifp->if_carp
1143 get_mplock();
1144 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
1145 rel_mplock();
1146 goto post_stats;
1148 rel_mplock();
1150 #endif
1153 * We got a packet which was unicast to a different Ethernet
1154 * address. If the driver is working properly, then this
1155 * situation can only happen when the interface is in
1156 * promiscuous mode. We defer the packet discarding until the
1157 * vlan processing is done, so that vlan/bridge or vlan/netgraph
1158 * could work.
1160 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1161 !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1162 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN))
1163 discard = 1;
1165 post_stats:
1166 if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1167 struct ether_header save_eh = *eh;
1169 /* XXX old crufty stuff, needs to be removed */
1170 m_adj(m, sizeof(struct ether_header));
1172 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1173 m_freem(m);
1174 return;
1177 ether_restore_header(&m, eh, &save_eh);
1178 if (m == NULL)
1179 return;
1180 eh = mtod(m, struct ether_header *);
1183 ether_type = ntohs(eh->ether_type);
1184 KKASSERT(ether_type != ETHERTYPE_VLAN);
1186 if (m->m_flags & M_VLANTAG) {
1187 void (*vlan_input_func)(struct mbuf *);
1189 vlan_input_func = vlan_input_p;
1190 if (vlan_input_func != NULL) {
1191 vlan_input_func(m);
1192 } else {
1193 m->m_pkthdr.rcvif->if_noproto++;
1194 m_freem(m);
1196 return;
1200 * If we have been asked to discard this packet
1201 * (e.g. not for us), drop it before entering
1202 * the upper layer.
1204 if (discard) {
1205 m_freem(m);
1206 return;
1210 * Clear protocol specific flags,
1211 * before entering the upper layer.
1213 m->m_flags &= ~M_ETHER_FLAGS;
1215 /* Strip ethernet header. */
1216 m_adj(m, sizeof(struct ether_header));
1219 * By default, we don't need to do the redispatch; for the
1220 * most common packet types, e.g. IPv4, ether_input_chain()
1221 * has already picked up the correct target network msgport.
1223 redispatch = 0;
1225 switch (ether_type) {
1226 #ifdef INET
1227 case ETHERTYPE_IP:
1228 if ((m->m_flags & M_LENCHECKED) == 0) {
1229 if (!ip_lengthcheck(&m))
1230 return;
1232 if (ipflow_fastforward(m))
1233 return;
1234 isr = NETISR_IP;
1235 break;
1237 case ETHERTYPE_ARP:
1238 if (ifp->if_flags & IFF_NOARP) {
1239 /* Discard packet if ARP is disabled on interface */
1240 m_freem(m);
1241 return;
1243 isr = NETISR_ARP;
1244 break;
1245 #endif
1247 #ifdef INET6
1248 case ETHERTYPE_IPV6:
1249 isr = NETISR_IPV6;
1250 break;
1251 #endif
1253 #ifdef IPX
1254 case ETHERTYPE_IPX:
1255 if (ef_inputp) {
1257 * Hold BGL and recheck ef_inputp
1259 get_mplock();
1260 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1261 rel_mplock();
1262 return;
1264 rel_mplock();
1266 isr = NETISR_IPX;
1267 break;
1268 #endif
1270 #ifdef NS
1271 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1272 isr = NETISR_NS;
1273 break;
1275 #endif
1277 #ifdef NETATALK
1278 case ETHERTYPE_AT:
1279 isr = NETISR_ATALK1;
1280 break;
1281 case ETHERTYPE_AARP:
1282 isr = NETISR_AARP;
1283 break;
1284 #endif
1286 #ifdef MPLS
1287 case ETHERTYPE_MPLS:
1288 case ETHERTYPE_MPLS_MCAST:
1289 /* Should have been set by ether_input_chain(). */
1290 KKASSERT(m->m_flags & M_MPLSLABELED);
1291 isr = NETISR_MPLS;
1292 break;
1293 #endif
1295 default:
1297 * The accurate msgport is not determined before
1298 * we reach here, so redo the dispatching
1300 redispatch = 1;
1301 #ifdef IPX
1302 if (ef_inputp) {
1304 * Hold BGL and recheck ef_inputp
1306 get_mplock();
1307 if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1308 rel_mplock();
1309 return;
1311 rel_mplock();
1313 #endif
1314 #ifdef NS
1315 checksum = mtod(m, ushort *);
1316 /* Novell 802.3 */
1317 if ((ether_type <= ETHERMTU) &&
1318 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
1319 if (*checksum == 0xE0E0) {
1320 m->m_pkthdr.len -= 3;
1321 m->m_len -= 3;
1322 m->m_data += 3;
1324 isr = NETISR_NS;
1325 break;
1327 #endif
1328 #ifdef NETATALK
1329 if (ether_type > ETHERMTU)
1330 goto dropanyway;
1331 l = mtod(m, struct llc *);
1332 if (l->llc_dsap == LLC_SNAP_LSAP &&
1333 l->llc_ssap == LLC_SNAP_LSAP &&
1334 l->llc_control == LLC_UI) {
1335 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
1336 sizeof at_org_code) == 0 &&
1337 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
1338 m_adj(m, sizeof(struct llc));
1339 isr = NETISR_ATALK2;
1340 break;
1342 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
1343 sizeof aarp_org_code) == 0 &&
1344 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
1345 m_adj(m, sizeof(struct llc));
1346 isr = NETISR_AARP;
1347 break;
1350 dropanyway:
1351 #endif
1352 if (ng_ether_input_orphan_p != NULL) {
1354 * Hold BGL and recheck ng_ether_input_orphan_p
1356 get_mplock();
1357 if (ng_ether_input_orphan_p != NULL) {
1358 ng_ether_input_orphan_p(ifp, m, eh);
1359 rel_mplock();
1360 return;
1362 rel_mplock();
1364 m_freem(m);
1365 return;
1368 if (!redispatch)
1369 netisr_run(isr, m);
1370 else
1371 netisr_dispatch(isr, m);
1375 * First we perform any link layer operations, then continue to the
1376 * upper layers with ether_demux_oncpu().
1378 void
1379 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1381 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1383 * Receiving interface's flags are changed, when this
1384 * packet is waiting for processing; discard it.
1386 m_freem(m);
1387 return;
1391 * Tap the packet off here for a bridge. bridge_input()
1392 * will return NULL if it has consumed the packet, otherwise
1393 * it gets processed as normal. Note that bridge_input()
1394 * will always return the original packet if we need to
1395 * process it locally.
1397 if (ifp->if_bridge) {
1398 KASSERT(bridge_input_p != NULL,
1399 ("%s: if_bridge not loaded!", __func__));
1401 if(m->m_flags & M_ETHER_BRIDGED) {
1402 m->m_flags &= ~M_ETHER_BRIDGED;
1403 } else {
1404 m = bridge_input_p(ifp, m);
1405 if (m == NULL)
1406 return;
1408 KASSERT(ifp == m->m_pkthdr.rcvif,
1409 ("bridge_input_p changed rcvif\n"));
1413 /* Handle ng_ether(4) processing, if any */
1414 if (ng_ether_input_p != NULL) {
1416 * Hold BGL and recheck ng_ether_input_p
1418 get_mplock();
1419 if (ng_ether_input_p != NULL)
1420 ng_ether_input_p(ifp, &m);
1421 rel_mplock();
1423 if (m == NULL)
1424 return;
1427 /* Continue with upper layer processing */
1428 ether_demux_oncpu(ifp, m);
1432 * Perform certain functions of ether_input_chain():
1433 * - Test IFF_UP
1434 * - Update statistics
1435 * - Run bpf(4) tap if requested
1436 * Then pass the packet to ether_input_oncpu().
1438 * This function should be used by pseudo interface (e.g. vlan(4)),
1439 * when it tries to claim that the packet is received by it.
1441 void
1442 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int run_bpf)
1444 /* Discard packet if interface is not up */
1445 if (!(ifp->if_flags & IFF_UP)) {
1446 m_freem(m);
1447 return;
1450 /* Change receiving interface */
1451 m->m_pkthdr.rcvif = ifp;
1453 /* Update statistics */
1454 ifp->if_ipackets++;
1455 ifp->if_ibytes += m->m_pkthdr.len;
1456 if (m->m_flags & (M_MCAST | M_BCAST))
1457 ifp->if_imcasts++;
1459 if (run_bpf)
1460 BPF_MTAP(ifp, m);
1462 ether_input_oncpu(ifp, m);
1465 static __inline boolean_t
1466 ether_vlancheck(struct mbuf **m0)
1468 struct mbuf *m = *m0;
1469 struct ether_header *eh;
1470 uint16_t ether_type;
1472 eh = mtod(m, struct ether_header *);
1473 ether_type = ntohs(eh->ether_type);
1475 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1477 * Extract vlan tag if hardware does not do it for us
1479 vlan_ether_decap(&m);
1480 if (m == NULL)
1481 goto failed;
1483 eh = mtod(m, struct ether_header *);
1484 ether_type = ntohs(eh->ether_type);
1487 if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1489 * To prevent possible dangerous recursion,
1490 * we don't do vlan-in-vlan
1492 m->m_pkthdr.rcvif->if_noproto++;
1493 goto failed;
1495 KKASSERT(ether_type != ETHERTYPE_VLAN);
1497 m->m_flags |= M_ETHER_VLANCHECKED;
1498 *m0 = m;
1499 return TRUE;
1500 failed:
1501 if (m != NULL)
1502 m_freem(m);
1503 *m0 = NULL;
1504 return FALSE;
1507 static void
1508 ether_input_handler(struct netmsg *nmsg)
1510 struct netmsg_packet *nmp = (struct netmsg_packet *)nmsg;
1511 struct ether_header *eh;
1512 struct ifnet *ifp;
1513 struct mbuf *m;
1515 m = nmp->nm_packet;
1516 M_ASSERTPKTHDR(m);
1517 ifp = m->m_pkthdr.rcvif;
1519 eh = mtod(m, struct ether_header *);
1520 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1521 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1522 ifp->if_addrlen) == 0)
1523 m->m_flags |= M_BCAST;
1524 else
1525 m->m_flags |= M_MCAST;
1526 ifp->if_imcasts++;
1529 if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1530 if (!ether_vlancheck(&m)) {
1531 KKASSERT(m == NULL);
1532 return;
1536 ether_input_oncpu(ifp, m);
1539 static __inline void
1540 ether_init_netpacket(int num, struct mbuf *m)
1542 struct netmsg_packet *pmsg;
1544 pmsg = &m->m_hdr.mh_netmsg;
1545 netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport, MSGF_MPSAFE,
1546 ether_input_handler);
1547 pmsg->nm_packet = m;
1548 pmsg->nm_netmsg.nm_lmsg.u.ms_result = num;
1551 static __inline struct lwkt_port *
1552 ether_mport(int num, struct mbuf **m)
1554 if (num == NETISR_MAX) {
1556 * All packets whose target msgports can't be
1557 * determined here are dispatched to netisr0,
1558 * where further dispatching may happen.
1560 return cpu_portfn(0);
1562 return netisr_find_port(num, m);
1566 * Send the packet to the target msgport or
1567 * queue it into 'chain'.
1569 static void
1570 ether_dispatch(int isr, struct lwkt_port *port, struct mbuf *m,
1571 struct mbuf_chain *chain)
1573 ether_init_netpacket(isr, m);
1575 if (chain != NULL) {
1576 struct mbuf_chain *c;
1577 int cpuid;
1579 m->m_pkthdr.header = port; /* XXX */
1580 cpuid = port->mpu_td->td_gd->gd_cpuid;
1582 c = &chain[cpuid];
1583 if (c->mc_head == NULL) {
1584 c->mc_head = c->mc_tail = m;
1585 } else {
1586 c->mc_tail->m_nextpkt = m;
1587 c->mc_tail = m;
1589 m->m_nextpkt = NULL;
1590 } else {
1591 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1596 * Process a received Ethernet packet.
1598 * The ethernet header is assumed to be in the mbuf so the caller
1599 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1600 * bytes in the first mbuf.
1602 * We first try to find the target msgport for this ether frame, if
1603 * there is no target msgport for it, this ether frame is discarded,
1604 * else we do following processing according to whether 'chain' is
1605 * NULL or not:
1606 * - If 'chain' is NULL, this ether frame is sent to the target msgport
1607 * immediately. This situation happens when ether_input_chain is
1608 * accessed through ifnet.if_input.
1609 * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1610 * bucket indexed by the target msgport's cpuid and the target msgport
1611 * is saved in mbuf's m_pkthdr.m_head. Caller of ether_input_chain
1612 * must initialize 'chain' by calling ether_input_chain_init().
1613 * ether_input_dispatch must be called later to send ether frames
1614 * queued on 'chain' to their target msgport.
1616 void
1617 ether_input_chain(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi,
1618 struct mbuf_chain *chain)
1620 struct ether_header *eh, *save_eh, save_eh0;
1621 struct lwkt_port *port;
1622 uint16_t ether_type;
1623 int isr;
1625 #ifdef FIX_SERIALIZE_ASSERT
1626 ASSERT_SERIALIZED(ifp->if_serializer);
1627 #endif
1628 M_ASSERTPKTHDR(m);
1630 /* Discard packet if interface is not up */
1631 if (!(ifp->if_flags & IFF_UP)) {
1632 m_freem(m);
1633 return;
1636 if (m->m_len < sizeof(struct ether_header)) {
1637 /* XXX error in the caller. */
1638 m_freem(m);
1639 return;
1642 m->m_pkthdr.rcvif = ifp;
1644 logether(chain_beg, ifp);
1646 ETHER_BPF_MTAP(ifp, m);
1648 ifp->if_ibytes += m->m_pkthdr.len;
1650 if (ifp->if_flags & IFF_MONITOR) {
1651 eh = mtod(m, struct ether_header *);
1652 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1653 ifp->if_imcasts++;
1656 * Interface marked for monitoring; discard packet.
1658 m_freem(m);
1660 logether(chain_end, ifp);
1661 return;
1664 if (pi != NULL && (m->m_flags & M_HASH)) {
1665 #ifdef RSS_DEBUG
1666 ether_pktinfo_try++;
1667 #endif
1668 /* Try finding the port using the packet info */
1669 port = netisr_find_pktinfo_port(pi, m);
1670 if (port != NULL) {
1671 #ifdef RSS_DEBUG
1672 ether_pktinfo_hit++;
1673 #endif
1674 ether_dispatch(pi->pi_netisr, port, m, chain);
1676 logether(chain_end, ifp);
1677 return;
1681 * The packet info does not contain enough
1682 * information, we will have to check the
1683 * packet content.
1686 #ifdef RSS_DEBUG
1687 else if (ifp->if_capenable & IFCAP_RSS) {
1688 if (pi == NULL)
1689 ether_rss_nopi++;
1690 else
1691 ether_rss_nohash++;
1693 #endif
1696 * Packet hash will be recalculated by software,
1697 * so clear the M_HASH flag set by the driver;
1698 * the hash value calculated by the hardware may
1699 * not be exactly what we want.
1701 m->m_flags &= ~M_HASH;
1703 if (!ether_vlancheck(&m)) {
1704 KKASSERT(m == NULL);
1705 logether(chain_end, ifp);
1706 return;
1708 eh = mtod(m, struct ether_header *);
1709 ether_type = ntohs(eh->ether_type);
1712 * Map ether type to netisr id.
1714 switch (ether_type) {
1715 #ifdef INET
1716 case ETHERTYPE_IP:
1717 isr = NETISR_IP;
1718 break;
1720 case ETHERTYPE_ARP:
1721 isr = NETISR_ARP;
1722 break;
1723 #endif
1725 #ifdef INET6
1726 case ETHERTYPE_IPV6:
1727 isr = NETISR_IPV6;
1728 break;
1729 #endif
1731 #ifdef IPX
1732 case ETHERTYPE_IPX:
1733 isr = NETISR_IPX;
1734 break;
1735 #endif
1737 #ifdef NS
1738 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1739 isr = NETISR_NS;
1740 break;
1741 #endif
1743 #ifdef NETATALK
1744 case ETHERTYPE_AT:
1745 isr = NETISR_ATALK1;
1746 break;
1747 case ETHERTYPE_AARP:
1748 isr = NETISR_AARP;
1749 break;
1750 #endif
1752 #ifdef MPLS
1753 case ETHERTYPE_MPLS:
1754 case ETHERTYPE_MPLS_MCAST:
1755 m->m_flags |= M_MPLSLABELED;
1756 isr = NETISR_MPLS;
1757 break;
1758 #endif
1760 default:
1762 * NETISR_MAX is an invalid value; it is chosen to let
1763 * ether_mport() know that we are not able to decide
1764 * this packet's msgport here.
1766 isr = NETISR_MAX;
1767 break;
1771 * If the packet is in contiguous memory, following
1772 * m_adj() could ensure that the hidden ether header
1773 * will not be destroyed, else we will have to save
1774 * the ether header for the later restoration.
1776 if (m->m_pkthdr.len != m->m_len) {
1777 save_eh0 = *eh;
1778 save_eh = &save_eh0;
1779 } else {
1780 save_eh = NULL;
1784 * Temporarily remove ether header; ether_mport()
1785 * expects a packet without ether header.
1787 m_adj(m, sizeof(struct ether_header));
1790 * Find the packet's target msgport.
1792 port = ether_mport(isr, &m);
1793 if (port == NULL) {
1794 KKASSERT(m == NULL);
1795 logether(chain_end, ifp);
1796 return;
1800 * Restore ether header.
1802 if (save_eh != NULL) {
1803 ether_restore_header(&m, eh, save_eh);
1804 if (m == NULL) {
1805 logether(chain_end, ifp);
1806 return;
1808 } else {
1809 m->m_data -= ETHER_HDR_LEN;
1810 m->m_len += ETHER_HDR_LEN;
1811 m->m_pkthdr.len += ETHER_HDR_LEN;
1814 ether_dispatch(isr, port, m, chain);
1816 logether(chain_end, ifp);