Fix typo.
[dragonfly.git] / sys / net / if_ethersubr.c
blob292a36291c4d7c2989576190cc1bf0e7e99e0c77
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.74 2008/06/25 11:45:07 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_netgraph.h"
43 #include "opt_carp.h"
44 #include "opt_ethernet.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/globaldata.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/msgport.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/sysctl.h>
56 #include <sys/thread.h>
57 #include <sys/thread2.h>
59 #include <net/if.h>
60 #include <net/netisr.h>
61 #include <net/route.h>
62 #include <net/if_llc.h>
63 #include <net/if_dl.h>
64 #include <net/if_types.h>
65 #include <net/ifq_var.h>
66 #include <net/bpf.h>
67 #include <net/ethernet.h>
68 #include <net/vlan/if_vlan_ether.h>
69 #include <net/netmsg2.h>
71 #if defined(INET) || defined(INET6)
72 #include <netinet/in.h>
73 #include <netinet/in_var.h>
74 #include <netinet/if_ether.h>
75 #include <net/ipfw/ip_fw.h>
76 #include <net/dummynet/ip_dummynet.h>
77 #endif
78 #ifdef INET6
79 #include <netinet6/nd6.h>
80 #endif
82 #ifdef CARP
83 #include <netinet/ip_carp.h>
84 #endif
86 #ifdef IPX
87 #include <netproto/ipx/ipx.h>
88 #include <netproto/ipx/ipx_if.h>
89 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
90 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
91 short *tp, int *hlen);
92 #endif
94 #ifdef NS
95 #include <netns/ns.h>
96 #include <netns/ns_if.h>
97 ushort ns_nettype;
98 int ether_outputdebug = 0;
99 int ether_inputdebug = 0;
100 #endif
102 #ifdef NETATALK
103 #include <netproto/atalk/at.h>
104 #include <netproto/atalk/at_var.h>
105 #include <netproto/atalk/at_extern.h>
107 #define llc_snap_org_code llc_un.type_snap.org_code
108 #define llc_snap_ether_type llc_un.type_snap.ether_type
110 extern u_char at_org_code[3];
111 extern u_char aarp_org_code[3];
112 #endif /* NETATALK */
114 /* netgraph node hooks for ng_ether(4) */
115 void (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
116 void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
117 struct mbuf *m, const struct ether_header *eh);
118 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
119 void (*ng_ether_attach_p)(struct ifnet *ifp);
120 void (*ng_ether_detach_p)(struct ifnet *ifp);
122 int (*vlan_input_p)(struct mbuf *, struct mbuf_chain *);
123 void (*vlan_input2_p)(struct mbuf *);
125 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
126 struct rtentry *);
127 static void ether_restore_header(struct mbuf **, const struct ether_header *,
128 const struct ether_header *);
129 static void ether_demux_chain(struct ifnet *, struct mbuf *,
130 struct mbuf_chain *);
133 * if_bridge support
135 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
136 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
137 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
139 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
140 struct sockaddr *);
142 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
143 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
146 #define gotoerr(e) do { error = (e); goto bad; } while (0)
147 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
149 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
150 struct ip_fw **rule,
151 const struct ether_header *eh);
153 static int ether_ipfw;
154 static u_int ether_restore_hdr;
155 static u_int ether_prepend_hdr;
157 SYSCTL_DECL(_net_link);
158 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
159 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
160 &ether_ipfw, 0, "Pass ether pkts through firewall");
161 SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
162 &ether_restore_hdr, 0, "# of ether header restoration");
163 SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
164 &ether_prepend_hdr, 0,
165 "# of ether header restoration which prepends mbuf");
168 * Ethernet output routine.
169 * Encapsulate a packet of type family for the local net.
170 * Use trailer local net encapsulation if enough data in first
171 * packet leaves a multiple of 512 bytes of data in remainder.
172 * Assumes that ifp is actually pointer to arpcom structure.
174 static int
175 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
176 struct rtentry *rt)
178 struct ether_header *eh, *deh;
179 u_char *edst;
180 int loop_copy = 0;
181 int hlen = ETHER_HDR_LEN; /* link layer header length */
182 struct arpcom *ac = IFP2AC(ifp);
183 int error;
185 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
187 if (ifp->if_flags & IFF_MONITOR)
188 gotoerr(ENETDOWN);
189 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
190 gotoerr(ENETDOWN);
192 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
193 if (m == NULL)
194 return (ENOBUFS);
195 eh = mtod(m, struct ether_header *);
196 edst = eh->ether_dhost;
199 * Fill in the destination ethernet address and frame type.
201 switch (dst->sa_family) {
202 #ifdef INET
203 case AF_INET:
204 if (!arpresolve(ifp, rt, m, dst, edst))
205 return (0); /* if not yet resolved */
206 eh->ether_type = htons(ETHERTYPE_IP);
207 break;
208 #endif
209 #ifdef INET6
210 case AF_INET6:
211 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
212 return (0); /* Something bad happenned. */
213 eh->ether_type = htons(ETHERTYPE_IPV6);
214 break;
215 #endif
216 #ifdef IPX
217 case AF_IPX:
218 if (ef_outputp != NULL) {
219 error = ef_outputp(ifp, &m, dst, &eh->ether_type,
220 &hlen);
221 if (error)
222 goto bad;
223 } else {
224 eh->ether_type = htons(ETHERTYPE_IPX);
225 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
226 edst, ETHER_ADDR_LEN);
228 break;
229 #endif
230 #ifdef NETATALK
231 case AF_APPLETALK: {
232 struct at_ifaddr *aa;
234 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
235 error = 0; /* XXX */
236 goto bad;
239 * In the phase 2 case, need to prepend an mbuf for
240 * the llc header. Since we must preserve the value
241 * of m, which is passed to us by value, we m_copy()
242 * the first mbuf, and use it for our llc header.
244 if (aa->aa_flags & AFA_PHASE2) {
245 struct llc llc;
247 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
248 eh = mtod(m, struct ether_header *);
249 edst = eh->ether_dhost;
250 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
251 llc.llc_control = LLC_UI;
252 bcopy(at_org_code, llc.llc_snap_org_code,
253 sizeof at_org_code);
254 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
255 bcopy(&llc,
256 mtod(m, caddr_t) + sizeof(struct ether_header),
257 sizeof(struct llc));
258 eh->ether_type = htons(m->m_pkthdr.len);
259 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
260 } else {
261 eh->ether_type = htons(ETHERTYPE_AT);
263 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
264 return (0);
265 break;
267 #endif
268 #ifdef NS
269 case AF_NS:
270 switch(ns_nettype) {
271 default:
272 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
273 eh->ether_type = 0x8137;
274 break;
275 case 0x0: /* Novell 802.3 */
276 eh->ether_type = htons(m->m_pkthdr.len);
277 break;
278 case 0xe0e0: /* Novell 802.2 and Token-Ring */
279 M_PREPEND(m, 3, MB_DONTWAIT);
280 eh = mtod(m, struct ether_header *);
281 edst = eh->ether_dhost;
282 eh->ether_type = htons(m->m_pkthdr.len);
283 cp = mtod(m, u_char *) + sizeof(struct ether_header);
284 *cp++ = 0xE0;
285 *cp++ = 0xE0;
286 *cp++ = 0x03;
287 break;
289 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
290 ETHER_ADDR_LEN);
292 * XXX if ns_thishost is the same as the node's ethernet
293 * address then just the default code will catch this anyhow.
294 * So I'm not sure if this next clause should be here at all?
295 * [JRE]
297 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
298 m->m_pkthdr.rcvif = ifp;
299 netisr_dispatch(NETISR_NS, m);
300 return (error);
302 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
303 m->m_flags |= M_BCAST;
304 break;
305 #endif
306 case pseudo_AF_HDRCMPLT:
307 case AF_UNSPEC:
308 loop_copy = -1; /* if this is for us, don't do it */
309 deh = (struct ether_header *)dst->sa_data;
310 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
311 eh->ether_type = deh->ether_type;
312 break;
314 default:
315 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
316 gotoerr(EAFNOSUPPORT);
319 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
320 memcpy(eh->ether_shost,
321 ((struct ether_header *)dst->sa_data)->ether_shost,
322 ETHER_ADDR_LEN);
323 else
324 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
327 * Bridges require special output handling.
329 if (ifp->if_bridge) {
330 KASSERT(bridge_output_p != NULL,
331 ("%s: if_bridge not loaded!", __func__));
332 return bridge_output_p(ifp, m);
336 * If a simplex interface, and the packet is being sent to our
337 * Ethernet address or a broadcast address, loopback a copy.
338 * XXX To make a simplex device behave exactly like a duplex
339 * device, we should copy in the case of sending to our own
340 * ethernet address (thus letting the original actually appear
341 * on the wire). However, we don't do that here for security
342 * reasons and compatibility with the original behavior.
344 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
345 int csum_flags = 0;
347 if (m->m_pkthdr.csum_flags & CSUM_IP)
348 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
349 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
350 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
351 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
352 struct mbuf *n;
354 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
355 n->m_pkthdr.csum_flags |= csum_flags;
356 if (csum_flags & CSUM_DATA_VALID)
357 n->m_pkthdr.csum_data = 0xffff;
358 if_simloop(ifp, n, dst->sa_family, hlen);
359 } else
360 ifp->if_iqdrops++;
361 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
362 ETHER_ADDR_LEN) == 0) {
363 m->m_pkthdr.csum_flags |= csum_flags;
364 if (csum_flags & CSUM_DATA_VALID)
365 m->m_pkthdr.csum_data = 0xffff;
366 if_simloop(ifp, m, dst->sa_family, hlen);
367 return (0); /* XXX */
371 #ifdef CARP
372 if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL)))
373 goto bad;
374 #endif
377 /* Handle ng_ether(4) processing, if any */
378 if (ng_ether_output_p != NULL) {
379 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
380 goto bad;
381 if (m == NULL)
382 return (0);
385 /* Continue with link-layer output */
386 return ether_output_frame(ifp, m);
388 bad:
389 m_freem(m);
390 return (error);
394 * Ethernet link layer output routine to send a raw frame to the device.
396 * This assumes that the 14 byte Ethernet header is present and contiguous
397 * in the first mbuf.
400 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
402 struct ip_fw *rule = NULL;
403 int error = 0;
404 struct altq_pktattr pktattr;
405 struct m_tag *mtag;
407 ASSERT_NOT_SERIALIZED(ifp->if_serializer);
409 /* Extract info from dummynet tag */
410 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
411 if (mtag != NULL) {
412 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
414 m_tag_delete(m, mtag);
415 mtag = NULL;
418 if (ifq_is_enabled(&ifp->if_snd))
419 altq_etherclassify(&ifp->if_snd, m, &pktattr);
420 crit_enter();
421 if (IPFW_LOADED && ether_ipfw != 0) {
422 struct ether_header save_eh, *eh;
424 eh = mtod(m, struct ether_header *);
425 save_eh = *eh;
426 m_adj(m, ETHER_HDR_LEN);
427 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
428 crit_exit();
429 if (m != NULL) {
430 m_freem(m);
431 return ENOBUFS; /* pkt dropped */
432 } else
433 return 0; /* consumed e.g. in a pipe */
436 /* packet was ok, restore the ethernet header */
437 ether_restore_header(&m, eh, &save_eh);
438 if (m == NULL) {
439 crit_exit();
440 return ENOBUFS;
443 crit_exit();
446 * Queue message on interface, update output statistics if
447 * successful, and start output if interface not yet active.
449 error = ifq_dispatch(ifp, m, &pktattr);
450 return (error);
454 * ipfw processing for ethernet packets (in and out).
455 * The second parameter is NULL from ether_demux(), and ifp from
456 * ether_output_frame().
458 static boolean_t
459 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
460 const struct ether_header *eh)
462 struct ether_header save_eh = *eh; /* might be a ptr in m */
463 struct ip_fw_args args;
464 struct m_tag *mtag;
465 int i;
467 if (*rule != NULL && fw_one_pass)
468 return TRUE; /* dummynet packet, already partially processed */
471 * I need some amount of data to be contiguous.
473 i = min((*m0)->m_pkthdr.len, max_protohdr);
474 if ((*m0)->m_len < i) {
475 *m0 = m_pullup(*m0, i);
476 if (*m0 == NULL)
477 return FALSE;
480 args.m = *m0; /* the packet we are looking at */
481 args.oif = dst; /* destination, if any */
482 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
483 m_tag_delete(*m0, mtag);
484 args.rule = *rule; /* matching rule to restart */
485 args.next_hop = NULL; /* we do not support forward yet */
486 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
487 i = ip_fw_chk_ptr(&args);
488 *m0 = args.m;
489 *rule = args.rule;
491 if ((i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL) /* drop */
492 return FALSE;
494 if (i == 0) /* a PASS rule. */
495 return TRUE;
497 if (i & IP_FW_PORT_DYNT_FLAG) {
499 * Pass the pkt to dummynet, which consumes it.
501 struct mbuf *m;
503 m = *m0; /* pass the original to dummynet */
504 *m0 = NULL; /* and nothing back to the caller */
506 ether_restore_header(&m, eh, &save_eh);
507 if (m == NULL)
508 return FALSE;
510 ip_fw_dn_io_ptr(m, (i & 0xffff),
511 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
512 return FALSE;
515 * XXX at some point add support for divert/forward actions.
516 * If none of the above matches, we have to drop the pkt.
518 return FALSE;
522 * Process a received Ethernet packet.
524 * The ethernet header is assumed to be in the mbuf so the caller
525 * MUST MAKE SURE that there are at least sizeof(struct ether_header)
526 * bytes in the first mbuf.
528 * This allows us to concentrate in one place a bunch of code which
529 * is replicated in all device drivers. Also, many functions called
530 * from ether_input() try to put the eh back into the mbuf, so we
531 * can later propagate the 'contiguous packet' interface to them.
533 * NOTA BENE: for all drivers "eh" is a pointer into the first mbuf or
534 * cluster, right before m_data. So be very careful when working on m,
535 * as you could destroy *eh !!
537 * First we perform any link layer operations, then continue to the
538 * upper layers with ether_demux().
540 void
541 ether_input_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
543 struct ether_header *eh;
545 ASSERT_SERIALIZED(ifp->if_serializer);
546 M_ASSERTPKTHDR(m);
548 /* Discard packet if interface is not up */
549 if (!(ifp->if_flags & IFF_UP)) {
550 m_freem(m);
551 return;
554 if (m->m_len < sizeof(struct ether_header)) {
555 /* XXX error in the caller. */
556 m_freem(m);
557 return;
559 eh = mtod(m, struct ether_header *);
561 if (ntohs(eh->ether_type) == ETHERTYPE_VLAN &&
562 (m->m_flags & M_VLANTAG) == 0) {
564 * Extract vlan tag if hardware does not do it for us
566 vlan_ether_decap(&m);
567 if (m == NULL)
568 return;
569 eh = mtod(m, struct ether_header *);
572 m->m_pkthdr.rcvif = ifp;
574 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
575 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
576 ifp->if_addrlen) == 0)
577 m->m_flags |= M_BCAST;
578 else
579 m->m_flags |= M_MCAST;
580 ifp->if_imcasts++;
583 ETHER_BPF_MTAP(ifp, m);
585 ifp->if_ibytes += m->m_pkthdr.len;
587 if (ifp->if_flags & IFF_MONITOR) {
589 * Interface marked for monitoring; discard packet.
591 m_freem(m);
592 return;
596 * Tap the packet off here for a bridge. bridge_input()
597 * will return NULL if it has consumed the packet, otherwise
598 * it gets processed as normal. Note that bridge_input()
599 * will always return the original packet if we need to
600 * process it locally.
602 if (ifp->if_bridge) {
603 KASSERT(bridge_input_p != NULL,
604 ("%s: if_bridge not loaded!", __func__));
606 if(m->m_flags & M_PROTO1) {
607 m->m_flags &= ~M_PROTO1;
608 } else {
609 /* clear M_PROMISC, in case the packets comes from a vlan */
610 /* m->m_flags &= ~M_PROMISC; */
611 lwkt_serialize_exit(ifp->if_serializer);
612 m = bridge_input_p(ifp, m);
613 lwkt_serialize_enter(ifp->if_serializer);
614 if (m == NULL)
615 return;
617 KASSERT(ifp == m->m_pkthdr.rcvif,
618 ("bridge_input_p changed rcvif\n"));
620 /* 'm' may be changed by bridge_input_p() */
621 eh = mtod(m, struct ether_header *);
625 /* Handle ng_ether(4) processing, if any */
626 if (ng_ether_input_p != NULL) {
627 ng_ether_input_p(ifp, &m);
628 if (m == NULL)
629 return;
631 /* 'm' may be changed by ng_ether_input_p() */
632 eh = mtod(m, struct ether_header *);
635 /* Continue with upper layer processing */
636 ether_demux_chain(ifp, m, chain);
639 void
640 ether_input(struct ifnet *ifp, struct mbuf *m)
642 ether_input_chain(ifp, m, NULL);
646 * Upper layer processing for a received Ethernet packet.
648 static void
649 ether_demux_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
651 struct ether_header save_eh, *eh;
652 int isr;
653 u_short ether_type;
654 struct ip_fw *rule = NULL;
655 struct m_tag *mtag;
656 #ifdef NETATALK
657 struct llc *l;
658 #endif
660 M_ASSERTPKTHDR(m);
661 KASSERT(m->m_len >= ETHER_HDR_LEN,
662 ("ether header is no contiguous!\n"));
664 eh = mtod(m, struct ether_header *);
665 save_eh = *eh;
667 /* XXX old crufty stuff, needs to be removed */
668 m_adj(m, sizeof(struct ether_header));
670 /* Extract info from dummynet tag */
671 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
672 if (mtag != NULL) {
673 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
674 KKASSERT(ifp == NULL);
675 ifp = m->m_pkthdr.rcvif;
677 m_tag_delete(m, mtag);
678 mtag = NULL;
680 if (rule) /* packet is passing the second time */
681 goto post_stats;
683 #ifdef CARP
685 * XXX: Okay, we need to call carp_forus() and - if it is for
686 * us jump over code that does the normal check
687 * "ac_enaddr == ether_dhost". The check sequence is a bit
688 * different from OpenBSD, so we jump over as few code as
689 * possible, to catch _all_ sanity checks. This needs
690 * evaluation, to see if the carp ether_dhost values break any
691 * of these checks!
693 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
694 goto post_stats;
695 #endif
698 * Discard packet if upper layers shouldn't see it because
699 * it was unicast to a different Ethernet address. If the
700 * driver is working properly, then this situation can only
701 * happen when the interface is in promiscuous mode.
703 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
704 (eh->ether_dhost[0] & 1) == 0 &&
705 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
706 m_freem(m);
707 return;
710 post_stats:
711 if (IPFW_LOADED && ether_ipfw != 0) {
712 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
713 m_freem(m);
714 return;
718 ether_type = ntohs(save_eh.ether_type);
720 if (m->m_flags & M_VLANTAG) {
721 if (ether_type == ETHERTYPE_VLAN) {
723 * To prevent possible dangerous recursion,
724 * we don't do vlan-in-vlan
726 m->m_pkthdr.rcvif->if_noproto++;
727 m_freem(m);
728 return;
731 if (vlan_input_p != NULL) {
732 ether_restore_header(&m, eh, &save_eh);
733 if (m != NULL)
734 vlan_input_p(m, chain);
735 } else {
736 m->m_pkthdr.rcvif->if_noproto++;
737 m_freem(m);
739 return;
741 KKASSERT(ether_type != ETHERTYPE_VLAN);
743 switch (ether_type) {
744 #ifdef INET
745 case ETHERTYPE_IP:
746 if (ipflow_fastforward(m, ifp->if_serializer))
747 return;
748 isr = NETISR_IP;
749 break;
751 case ETHERTYPE_ARP:
752 if (ifp->if_flags & IFF_NOARP) {
753 /* Discard packet if ARP is disabled on interface */
754 m_freem(m);
755 return;
757 isr = NETISR_ARP;
758 break;
759 #endif
761 #ifdef INET6
762 case ETHERTYPE_IPV6:
763 isr = NETISR_IPV6;
764 break;
765 #endif
767 #ifdef IPX
768 case ETHERTYPE_IPX:
769 if (ef_inputp && ef_inputp(ifp, &save_eh, m) == 0)
770 return;
771 isr = NETISR_IPX;
772 break;
773 #endif
775 #ifdef NS
776 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
777 isr = NETISR_NS;
778 break;
780 #endif
782 #ifdef NETATALK
783 case ETHERTYPE_AT:
784 isr = NETISR_ATALK1;
785 break;
786 case ETHERTYPE_AARP:
787 isr = NETISR_AARP;
788 break;
789 #endif
791 default:
792 #ifdef IPX
793 if (ef_inputp && ef_inputp(ifp, &save_eh, m) == 0)
794 return;
795 #endif
796 #ifdef NS
797 checksum = mtod(m, ushort *);
798 /* Novell 802.3 */
799 if ((ether_type <= ETHERMTU) &&
800 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
801 if (*checksum == 0xE0E0) {
802 m->m_pkthdr.len -= 3;
803 m->m_len -= 3;
804 m->m_data += 3;
806 isr = NETISR_NS;
807 break;
809 #endif
810 #ifdef NETATALK
811 if (ether_type > ETHERMTU)
812 goto dropanyway;
813 l = mtod(m, struct llc *);
814 if (l->llc_dsap == LLC_SNAP_LSAP &&
815 l->llc_ssap == LLC_SNAP_LSAP &&
816 l->llc_control == LLC_UI) {
817 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
818 sizeof at_org_code) == 0 &&
819 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
820 m_adj(m, sizeof(struct llc));
821 isr = NETISR_ATALK2;
822 break;
824 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
825 sizeof aarp_org_code) == 0 &&
826 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
827 m_adj(m, sizeof(struct llc));
828 isr = NETISR_AARP;
829 break;
832 dropanyway:
833 #endif
834 if (ng_ether_input_orphan_p != NULL)
835 (*ng_ether_input_orphan_p)(ifp, m, &save_eh);
836 else
837 m_freem(m);
838 return;
841 #ifdef ETHER_INPUT_CHAIN
842 if (chain != NULL) {
843 struct mbuf_chain *c;
844 lwkt_port_t port;
845 int cpuid;
847 port = netisr_mport(isr, &m);
848 if (port == NULL)
849 return;
851 m->m_pkthdr.header = port; /* XXX */
852 cpuid = port->mpu_td->td_gd->gd_cpuid;
854 c = &chain[cpuid];
855 if (c->mc_head == NULL) {
856 c->mc_head = c->mc_tail = m;
857 } else {
858 c->mc_tail->m_nextpkt = m;
859 c->mc_tail = m;
861 m->m_nextpkt = NULL;
862 } else
863 #endif /* ETHER_INPUT_CHAIN */
864 netisr_dispatch(isr, m);
867 void
868 ether_demux(struct ifnet *ifp, struct mbuf *m)
870 ether_demux_chain(ifp, m, NULL);
874 * Perform common duties while attaching to interface list
877 void
878 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
880 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
881 serializer);
884 void
885 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
886 lwkt_serialize_t serializer)
888 struct sockaddr_dl *sdl;
890 ifp->if_type = IFT_ETHER;
891 ifp->if_addrlen = ETHER_ADDR_LEN;
892 ifp->if_hdrlen = ETHER_HDR_LEN;
893 if_attach(ifp, serializer);
894 ifp->if_mtu = ETHERMTU;
895 if (ifp->if_baudrate == 0)
896 ifp->if_baudrate = 10000000;
897 ifp->if_output = ether_output;
898 ifp->if_input = ether_input;
899 ifp->if_resolvemulti = ether_resolvemulti;
900 ifp->if_broadcastaddr = etherbroadcastaddr;
901 sdl = IF_LLSOCKADDR(ifp);
902 sdl->sdl_type = IFT_ETHER;
903 sdl->sdl_alen = ifp->if_addrlen;
904 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
906 * XXX Keep the current drivers happy.
907 * XXX Remove once all drivers have been cleaned up
909 if (lla != IFP2AC(ifp)->ac_enaddr)
910 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
911 bpfattach(ifp, dlt, hdrlen);
912 if (ng_ether_attach_p != NULL)
913 (*ng_ether_attach_p)(ifp);
915 if_printf(ifp, "MAC address: %6D\n", lla, ":");
919 * Perform common duties while detaching an Ethernet interface
921 void
922 ether_ifdetach(struct ifnet *ifp)
924 if_down(ifp);
926 if (ng_ether_detach_p != NULL)
927 (*ng_ether_detach_p)(ifp);
928 bpfdetach(ifp);
929 if_detach(ifp);
933 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
935 struct ifaddr *ifa = (struct ifaddr *) data;
936 struct ifreq *ifr = (struct ifreq *) data;
937 int error = 0;
939 #define IF_INIT(ifp) \
940 do { \
941 if (((ifp)->if_flags & IFF_UP) == 0) { \
942 (ifp)->if_flags |= IFF_UP; \
943 (ifp)->if_init((ifp)->if_softc); \
945 } while (0)
947 ASSERT_SERIALIZED(ifp->if_serializer);
949 switch (command) {
950 case SIOCSIFADDR:
951 switch (ifa->ifa_addr->sa_family) {
952 #ifdef INET
953 case AF_INET:
954 IF_INIT(ifp); /* before arpwhohas */
955 arp_ifinit(ifp, ifa);
956 break;
957 #endif
958 #ifdef IPX
960 * XXX - This code is probably wrong
962 case AF_IPX:
964 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
965 struct arpcom *ac = IFP2AC(ifp);
967 if (ipx_nullhost(*ina))
968 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
969 else
970 bcopy(ina->x_host.c_host, ac->ac_enaddr,
971 sizeof ac->ac_enaddr);
973 IF_INIT(ifp); /* Set new address. */
974 break;
976 #endif
977 #ifdef NS
979 * XXX - This code is probably wrong
981 case AF_NS:
983 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
984 struct arpcom *ac = IFP2AC(ifp);
986 if (ns_nullhost(*ina))
987 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
988 else
989 bcopy(ina->x_host.c_host, ac->ac_enaddr,
990 sizeof ac->ac_enaddr);
993 * Set new address
995 IF_INIT(ifp);
996 break;
998 #endif
999 default:
1000 IF_INIT(ifp);
1001 break;
1003 break;
1005 case SIOCGIFADDR:
1006 bcopy(IFP2AC(ifp)->ac_enaddr,
1007 ((struct sockaddr *)ifr->ifr_data)->sa_data,
1008 ETHER_ADDR_LEN);
1009 break;
1011 case SIOCSIFMTU:
1013 * Set the interface MTU.
1015 if (ifr->ifr_mtu > ETHERMTU) {
1016 error = EINVAL;
1017 } else {
1018 ifp->if_mtu = ifr->ifr_mtu;
1020 break;
1021 default:
1022 error = EINVAL;
1023 break;
1025 return (error);
1027 #undef IF_INIT
1031 ether_resolvemulti(
1032 struct ifnet *ifp,
1033 struct sockaddr **llsa,
1034 struct sockaddr *sa)
1036 struct sockaddr_dl *sdl;
1037 struct sockaddr_in *sin;
1038 #ifdef INET6
1039 struct sockaddr_in6 *sin6;
1040 #endif
1041 u_char *e_addr;
1043 switch(sa->sa_family) {
1044 case AF_LINK:
1046 * No mapping needed. Just check that it's a valid MC address.
1048 sdl = (struct sockaddr_dl *)sa;
1049 e_addr = LLADDR(sdl);
1050 if ((e_addr[0] & 1) != 1)
1051 return EADDRNOTAVAIL;
1052 *llsa = 0;
1053 return 0;
1055 #ifdef INET
1056 case AF_INET:
1057 sin = (struct sockaddr_in *)sa;
1058 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1059 return EADDRNOTAVAIL;
1060 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1061 M_WAITOK | M_ZERO);
1062 sdl->sdl_len = sizeof *sdl;
1063 sdl->sdl_family = AF_LINK;
1064 sdl->sdl_index = ifp->if_index;
1065 sdl->sdl_type = IFT_ETHER;
1066 sdl->sdl_alen = ETHER_ADDR_LEN;
1067 e_addr = LLADDR(sdl);
1068 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1069 *llsa = (struct sockaddr *)sdl;
1070 return 0;
1071 #endif
1072 #ifdef INET6
1073 case AF_INET6:
1074 sin6 = (struct sockaddr_in6 *)sa;
1075 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1077 * An IP6 address of 0 means listen to all
1078 * of the Ethernet multicast address used for IP6.
1079 * (This is used for multicast routers.)
1081 ifp->if_flags |= IFF_ALLMULTI;
1082 *llsa = 0;
1083 return 0;
1085 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1086 return EADDRNOTAVAIL;
1087 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1088 M_WAITOK | M_ZERO);
1089 sdl->sdl_len = sizeof *sdl;
1090 sdl->sdl_family = AF_LINK;
1091 sdl->sdl_index = ifp->if_index;
1092 sdl->sdl_type = IFT_ETHER;
1093 sdl->sdl_alen = ETHER_ADDR_LEN;
1094 e_addr = LLADDR(sdl);
1095 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1096 *llsa = (struct sockaddr *)sdl;
1097 return 0;
1098 #endif
1100 default:
1102 * Well, the text isn't quite right, but it's the name
1103 * that counts...
1105 return EAFNOSUPPORT;
1109 #if 0
1111 * This is for reference. We have a table-driven version
1112 * of the little-endian crc32 generator, which is faster
1113 * than the double-loop.
1115 uint32_t
1116 ether_crc32_le(const uint8_t *buf, size_t len)
1118 uint32_t c, crc, carry;
1119 size_t i, j;
1121 crc = 0xffffffffU; /* initial value */
1123 for (i = 0; i < len; i++) {
1124 c = buf[i];
1125 for (j = 0; j < 8; j++) {
1126 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1127 crc >>= 1;
1128 c >>= 1;
1129 if (carry)
1130 crc = (crc ^ ETHER_CRC_POLY_LE);
1134 return (crc);
1136 #else
1137 uint32_t
1138 ether_crc32_le(const uint8_t *buf, size_t len)
1140 static const uint32_t crctab[] = {
1141 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1142 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1143 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1144 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1146 uint32_t crc;
1147 size_t i;
1149 crc = 0xffffffffU; /* initial value */
1151 for (i = 0; i < len; i++) {
1152 crc ^= buf[i];
1153 crc = (crc >> 4) ^ crctab[crc & 0xf];
1154 crc = (crc >> 4) ^ crctab[crc & 0xf];
1157 return (crc);
1159 #endif
1161 uint32_t
1162 ether_crc32_be(const uint8_t *buf, size_t len)
1164 uint32_t c, crc, carry;
1165 size_t i, j;
1167 crc = 0xffffffffU; /* initial value */
1169 for (i = 0; i < len; i++) {
1170 c = buf[i];
1171 for (j = 0; j < 8; j++) {
1172 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1173 crc <<= 1;
1174 c >>= 1;
1175 if (carry)
1176 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1180 return (crc);
1184 * find the size of ethernet header, and call classifier
1186 void
1187 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
1188 struct altq_pktattr *pktattr)
1190 struct ether_header *eh;
1191 uint16_t ether_type;
1192 int hlen, af, hdrsize;
1193 caddr_t hdr;
1195 hlen = sizeof(struct ether_header);
1196 eh = mtod(m, struct ether_header *);
1198 ether_type = ntohs(eh->ether_type);
1199 if (ether_type < ETHERMTU) {
1200 /* ick! LLC/SNAP */
1201 struct llc *llc = (struct llc *)(eh + 1);
1202 hlen += 8;
1204 if (m->m_len < hlen ||
1205 llc->llc_dsap != LLC_SNAP_LSAP ||
1206 llc->llc_ssap != LLC_SNAP_LSAP ||
1207 llc->llc_control != LLC_UI)
1208 goto bad; /* not snap! */
1210 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
1213 if (ether_type == ETHERTYPE_IP) {
1214 af = AF_INET;
1215 hdrsize = 20; /* sizeof(struct ip) */
1216 #ifdef INET6
1217 } else if (ether_type == ETHERTYPE_IPV6) {
1218 af = AF_INET6;
1219 hdrsize = 40; /* sizeof(struct ip6_hdr) */
1220 #endif
1221 } else
1222 goto bad;
1224 while (m->m_len <= hlen) {
1225 hlen -= m->m_len;
1226 m = m->m_next;
1228 hdr = m->m_data + hlen;
1229 if (m->m_len < hlen + hdrsize) {
1231 * ip header is not in a single mbuf. this should not
1232 * happen in the current code.
1233 * (todo: use m_pulldown in the future)
1235 goto bad;
1237 m->m_data += hlen;
1238 m->m_len -= hlen;
1239 ifq_classify(ifq, m, af, pktattr);
1240 m->m_data -= hlen;
1241 m->m_len += hlen;
1243 return;
1245 bad:
1246 pktattr->pattr_class = NULL;
1247 pktattr->pattr_hdr = NULL;
1248 pktattr->pattr_af = AF_UNSPEC;
1251 static void
1252 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
1253 const struct ether_header *save_eh)
1255 struct mbuf *m = *m0;
1257 ether_restore_hdr++;
1260 * Prepend the header, optimize for the common case of
1261 * eh pointing into the mbuf.
1263 if ((const void *)(eh + 1) == (void *)m->m_data) {
1264 m->m_data -= ETHER_HDR_LEN;
1265 m->m_len += ETHER_HDR_LEN;
1266 m->m_pkthdr.len += ETHER_HDR_LEN;
1267 } else {
1268 ether_prepend_hdr++;
1270 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1271 if (m != NULL) {
1272 bcopy(save_eh, mtod(m, struct ether_header *),
1273 ETHER_HDR_LEN);
1276 *m0 = m;
1279 #ifdef ETHER_INPUT_CHAIN
1281 static void
1282 ether_input_ipifunc(void *arg)
1284 struct mbuf *m, *next;
1285 lwkt_port_t port;
1287 m = arg;
1288 do {
1289 next = m->m_nextpkt;
1290 m->m_nextpkt = NULL;
1292 port = m->m_pkthdr.header;
1293 m->m_pkthdr.header = NULL;
1295 lwkt_sendmsg(port,
1296 &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1298 m = next;
1299 } while (m != NULL);
1302 void
1303 ether_input_dispatch(struct mbuf_chain *chain)
1305 #ifdef SMP
1306 int i;
1308 for (i = 0; i < ncpus; ++i) {
1309 if (chain[i].mc_head != NULL) {
1310 lwkt_send_ipiq(globaldata_find(i),
1311 ether_input_ipifunc, chain[i].mc_head);
1314 #else
1315 if (chain->mc_head != NULL)
1316 ether_input_ipifunc(chain->mc_head);
1317 #endif
1320 void
1321 ether_input_chain_init(struct mbuf_chain *chain)
1323 #ifdef SMP
1324 int i;
1326 for (i = 0; i < ncpus; ++i)
1327 chain[i].mc_head = chain[i].mc_tail = NULL;
1328 #else
1329 chain->mc_head = chain->mc_tail = NULL;
1330 #endif
1333 #endif /* ETHER_INPUT_CHAIN */
1335 #ifdef ETHER_INPUT2
1337 static void
1338 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
1340 struct ether_header *eh;
1341 int isr, redispatch;
1342 u_short ether_type;
1343 struct ip_fw *rule = NULL;
1344 struct m_tag *mtag;
1345 #ifdef NETATALK
1346 struct llc *l;
1347 #endif
1349 M_ASSERTPKTHDR(m);
1350 KASSERT(m->m_len >= ETHER_HDR_LEN,
1351 ("ether header is no contiguous!\n"));
1353 eh = mtod(m, struct ether_header *);
1355 /* Extract info from dummynet tag */
1356 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1357 if (mtag != NULL) {
1358 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1359 KKASSERT(ifp == NULL);
1360 ifp = m->m_pkthdr.rcvif;
1362 m_tag_delete(m, mtag);
1363 mtag = NULL;
1365 if (rule) /* packet is passing the second time */
1366 goto post_stats;
1368 #ifdef CARP
1370 * XXX: Okay, we need to call carp_forus() and - if it is for
1371 * us jump over code that does the normal check
1372 * "ac_enaddr == ether_dhost". The check sequence is a bit
1373 * different from OpenBSD, so we jump over as few code as
1374 * possible, to catch _all_ sanity checks. This needs
1375 * evaluation, to see if the carp ether_dhost values break any
1376 * of these checks!
1378 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
1379 goto post_stats;
1380 #endif
1383 * Discard packet if upper layers shouldn't see it because
1384 * it was unicast to a different Ethernet address. If the
1385 * driver is working properly, then this situation can only
1386 * happen when the interface is in promiscuous mode.
1388 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1389 (eh->ether_dhost[0] & 1) == 0 &&
1390 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1391 m_freem(m);
1392 return;
1395 post_stats:
1396 if (IPFW_LOADED && ether_ipfw != 0) {
1397 struct ether_header save_eh = *eh;
1399 /* XXX old crufty stuff, needs to be removed */
1400 m_adj(m, sizeof(struct ether_header));
1402 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1403 m_freem(m);
1404 return;
1407 ether_restore_header(&m, eh, &save_eh);
1408 if (m == NULL)
1409 return;
1410 eh = mtod(m, struct ether_header *);
1413 ether_type = ntohs(eh->ether_type);
1414 KKASSERT(ether_type != ETHERTYPE_VLAN);
1416 if (m->m_flags & M_VLANTAG) {
1417 if (vlan_input2_p != NULL) {
1418 vlan_input2_p(m);
1419 } else {
1420 m->m_pkthdr.rcvif->if_noproto++;
1421 m_freem(m);
1423 return;
1426 m_adj(m, sizeof(struct ether_header));
1427 redispatch = 0;
1429 switch (ether_type) {
1430 #ifdef INET
1431 case ETHERTYPE_IP:
1432 #ifdef notyet
1433 if (ipflow_fastforward(m, ifp->if_serializer))
1434 return;
1435 #endif
1436 isr = NETISR_IP;
1437 break;
1439 case ETHERTYPE_ARP:
1440 if (ifp->if_flags & IFF_NOARP) {
1441 /* Discard packet if ARP is disabled on interface */
1442 m_freem(m);
1443 return;
1445 isr = NETISR_ARP;
1446 break;
1447 #endif
1449 #ifdef INET6
1450 case ETHERTYPE_IPV6:
1451 isr = NETISR_IPV6;
1452 break;
1453 #endif
1455 #ifdef IPX
1456 case ETHERTYPE_IPX:
1457 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
1458 return;
1459 isr = NETISR_IPX;
1460 break;
1461 #endif
1463 #ifdef NS
1464 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1465 isr = NETISR_NS;
1466 break;
1468 #endif
1470 #ifdef NETATALK
1471 case ETHERTYPE_AT:
1472 isr = NETISR_ATALK1;
1473 break;
1474 case ETHERTYPE_AARP:
1475 isr = NETISR_AARP;
1476 break;
1477 #endif
1479 default:
1481 * The accurate msgport is not determined before
1482 * we reach here, so redo the dispatching
1484 redispatch = 1;
1485 #ifdef IPX
1486 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
1487 return;
1488 #endif
1489 #ifdef NS
1490 checksum = mtod(m, ushort *);
1491 /* Novell 802.3 */
1492 if ((ether_type <= ETHERMTU) &&
1493 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
1494 if (*checksum == 0xE0E0) {
1495 m->m_pkthdr.len -= 3;
1496 m->m_len -= 3;
1497 m->m_data += 3;
1499 isr = NETISR_NS;
1500 break;
1502 #endif
1503 #ifdef NETATALK
1504 if (ether_type > ETHERMTU)
1505 goto dropanyway;
1506 l = mtod(m, struct llc *);
1507 if (l->llc_dsap == LLC_SNAP_LSAP &&
1508 l->llc_ssap == LLC_SNAP_LSAP &&
1509 l->llc_control == LLC_UI) {
1510 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
1511 sizeof at_org_code) == 0 &&
1512 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
1513 m_adj(m, sizeof(struct llc));
1514 isr = NETISR_ATALK2;
1515 break;
1517 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
1518 sizeof aarp_org_code) == 0 &&
1519 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
1520 m_adj(m, sizeof(struct llc));
1521 isr = NETISR_AARP;
1522 break;
1525 dropanyway:
1526 #endif
1527 if (ng_ether_input_orphan_p != NULL)
1528 ng_ether_input_orphan_p(ifp, m, eh);
1529 else
1530 m_freem(m);
1531 return;
1534 if (!redispatch)
1535 netisr_run(isr, m);
1536 else
1537 netisr_dispatch(isr, m);
1540 void
1541 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1543 if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1545 * Receiving interface's flags are changed, when this
1546 * packet is waiting for processing; discard it.
1548 m_freem(m);
1549 return;
1553 * Tap the packet off here for a bridge. bridge_input()
1554 * will return NULL if it has consumed the packet, otherwise
1555 * it gets processed as normal. Note that bridge_input()
1556 * will always return the original packet if we need to
1557 * process it locally.
1559 if (ifp->if_bridge) {
1560 KASSERT(bridge_input_p != NULL,
1561 ("%s: if_bridge not loaded!", __func__));
1563 if(m->m_flags & M_PROTO1) {
1564 m->m_flags &= ~M_PROTO1;
1565 } else {
1566 /* clear M_PROMISC, in case the packets comes from a vlan */
1567 /* m->m_flags &= ~M_PROMISC; */
1568 m = bridge_input_p(ifp, m);
1569 if (m == NULL)
1570 return;
1572 KASSERT(ifp == m->m_pkthdr.rcvif,
1573 ("bridge_input_p changed rcvif\n"));
1577 /* Handle ng_ether(4) processing, if any */
1578 if (ng_ether_input_p != NULL) {
1579 ng_ether_input_p(ifp, &m);
1580 if (m == NULL)
1581 return;
1584 /* Continue with upper layer processing */
1585 ether_demux_oncpu(ifp, m);
1588 static void
1589 ether_input_handler(struct netmsg *nmsg)
1591 struct netmsg_packet *nmp = (struct netmsg_packet *)nmsg;
1592 struct ifnet *ifp;
1593 struct mbuf *m;
1595 m = nmp->nm_packet;
1596 M_ASSERTPKTHDR(m);
1597 ifp = m->m_pkthdr.rcvif;
1599 ether_input_oncpu(ifp, m);
1602 static __inline void
1603 ether_init_netpacket(int num, struct mbuf *m)
1605 struct netmsg_packet *pmsg;
1607 pmsg = &m->m_hdr.mh_netmsg;
1608 netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport, 0,
1609 ether_input_handler);
1610 pmsg->nm_packet = m;
1611 pmsg->nm_netmsg.nm_lmsg.u.ms_result = num;
1614 static __inline struct lwkt_port *
1615 ether_mport(int num, struct mbuf **m0)
1617 struct lwkt_port *port;
1618 struct mbuf *m = *m0;
1620 if (num == NETISR_MAX) {
1622 * All packets whose target msgports can't be
1623 * determined here are dispatched to netisr0,
1624 * where further dispatching may happen.
1626 return cpu_portfn(0);
1629 port = netisr_find_port(num, &m);
1630 if (port == NULL)
1631 return NULL;
1633 *m0 = m;
1634 return port;
1637 void
1638 ether_input_chain2(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
1640 struct ether_header *eh, *save_eh, save_eh0;
1641 struct lwkt_port *port;
1642 uint16_t ether_type;
1643 int isr;
1645 ASSERT_SERIALIZED(ifp->if_serializer);
1646 M_ASSERTPKTHDR(m);
1648 /* Discard packet if interface is not up */
1649 if (!(ifp->if_flags & IFF_UP)) {
1650 m_freem(m);
1651 return;
1654 if (m->m_len < sizeof(struct ether_header)) {
1655 /* XXX error in the caller. */
1656 m_freem(m);
1657 return;
1659 eh = mtod(m, struct ether_header *);
1661 m->m_pkthdr.rcvif = ifp;
1663 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1664 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1665 ifp->if_addrlen) == 0)
1666 m->m_flags |= M_BCAST;
1667 else
1668 m->m_flags |= M_MCAST;
1669 ifp->if_imcasts++;
1672 ETHER_BPF_MTAP(ifp, m);
1674 ifp->if_ibytes += m->m_pkthdr.len;
1676 if (ifp->if_flags & IFF_MONITOR) {
1678 * Interface marked for monitoring; discard packet.
1680 m_freem(m);
1681 return;
1684 if (ntohs(eh->ether_type) == ETHERTYPE_VLAN &&
1685 (m->m_flags & M_VLANTAG) == 0) {
1687 * Extract vlan tag if hardware does not do it for us
1689 vlan_ether_decap(&m);
1690 if (m == NULL)
1691 return;
1692 eh = mtod(m, struct ether_header *);
1694 ether_type = ntohs(eh->ether_type);
1696 if ((m->m_flags & M_VLANTAG) && ether_type == ETHERTYPE_VLAN) {
1698 * To prevent possible dangerous recursion,
1699 * we don't do vlan-in-vlan
1701 ifp->if_noproto++;
1702 m_freem(m);
1703 return;
1705 KKASSERT(ether_type != ETHERTYPE_VLAN);
1708 * Map ether type to netisr id.
1710 switch (ether_type) {
1711 #ifdef INET
1712 case ETHERTYPE_IP:
1713 isr = NETISR_IP;
1714 break;
1716 case ETHERTYPE_ARP:
1717 isr = NETISR_ARP;
1718 break;
1719 #endif
1721 #ifdef INET6
1722 case ETHERTYPE_IPV6:
1723 isr = NETISR_IPV6;
1724 break;
1725 #endif
1727 #ifdef IPX
1728 case ETHERTYPE_IPX:
1729 isr = NETISR_IPX;
1730 break;
1731 #endif
1733 #ifdef NS
1734 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1735 isr = NETISR_NS;
1736 break;
1737 #endif
1739 #ifdef NETATALK
1740 case ETHERTYPE_AT:
1741 isr = NETISR_ATALK1;
1742 break;
1743 case ETHERTYPE_AARP:
1744 isr = NETISR_AARP;
1745 break;
1746 #endif
1748 default:
1750 * NETISR_MAX is an invalid value; it is chosen to let
1751 * ether_mport() know that we are not able to decide
1752 * this packet's msgport here.
1754 isr = NETISR_MAX;
1755 break;
1759 * If the packet is in contiguous memory, following
1760 * m_adj() could ensure that the hidden ether header
1761 * will not be destroyed, else we will have to save
1762 * the ether header for the later restoration.
1764 if (m->m_pkthdr.len != m->m_len) {
1765 save_eh0 = *eh;
1766 save_eh = &save_eh0;
1767 } else {
1768 save_eh = NULL;
1772 * Temporarily remove ether header; ether_mport()
1773 * expects a packet without ether header.
1775 m_adj(m, sizeof(struct ether_header));
1778 * Find the packet's target msgport.
1780 port = ether_mport(isr, &m);
1781 if (port == NULL) {
1782 KKASSERT(m == NULL);
1783 return;
1787 * Restore ether header.
1789 if (save_eh != NULL) {
1790 ether_restore_header(&m, eh, save_eh);
1791 if (m == NULL)
1792 return;
1793 } else {
1794 m->m_data -= ETHER_HDR_LEN;
1795 m->m_len += ETHER_HDR_LEN;
1796 m->m_pkthdr.len += ETHER_HDR_LEN;
1800 * Initialize mbuf's netmsg packet _after_ possible
1801 * ether header restoration, else the initialized
1802 * netmsg packet may be lost during ether header
1803 * restoration.
1805 ether_init_netpacket(isr, m);
1807 #ifdef ETHER_INPUT_CHAIN
1808 if (chain != NULL) {
1809 struct mbuf_chain *c;
1810 int cpuid;
1812 m->m_pkthdr.header = port; /* XXX */
1813 cpuid = port->mpu_td->td_gd->gd_cpuid;
1815 c = &chain[cpuid];
1816 if (c->mc_head == NULL) {
1817 c->mc_head = c->mc_tail = m;
1818 } else {
1819 c->mc_tail->m_nextpkt = m;
1820 c->mc_tail = m;
1822 m->m_nextpkt = NULL;
1823 } else
1824 #endif /* ETHER_INPUT_CHAIN */
1825 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1828 #endif /* ETHER_INPUT2 */