More minor IPI work.
[dragonfly/vkernel-mp.git] / sys / net / if_ethersubr.c
blob99c502232fc594bb30369be767b66552aa47c8bd
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.41 2007/06/05 13:41:39 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"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
53 #include <net/if.h>
54 #include <net/netisr.h>
55 #include <net/route.h>
56 #include <net/if_llc.h>
57 #include <net/if_dl.h>
58 #include <net/if_types.h>
59 #include <net/ifq_var.h>
60 #include <net/bpf.h>
61 #include <net/ethernet.h>
63 #if defined(INET) || defined(INET6)
64 #include <netinet/in.h>
65 #include <netinet/in_var.h>
66 #include <netinet/if_ether.h>
67 #include <net/ipfw/ip_fw.h>
68 #include <net/dummynet/ip_dummynet.h>
69 #endif
70 #ifdef INET6
71 #include <netinet6/nd6.h>
72 #endif
74 #ifdef IPX
75 #include <netproto/ipx/ipx.h>
76 #include <netproto/ipx/ipx_if.h>
77 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
78 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
79 short *tp, int *hlen);
80 #endif
82 #ifdef NS
83 #include <netns/ns.h>
84 #include <netns/ns_if.h>
85 ushort ns_nettype;
86 int ether_outputdebug = 0;
87 int ether_inputdebug = 0;
88 #endif
90 #ifdef NETATALK
91 #include <netproto/atalk/at.h>
92 #include <netproto/atalk/at_var.h>
93 #include <netproto/atalk/at_extern.h>
95 #define llc_snap_org_code llc_un.type_snap.org_code
96 #define llc_snap_ether_type llc_un.type_snap.ether_type
98 extern u_char at_org_code[3];
99 extern u_char aarp_org_code[3];
100 #endif /* NETATALK */
102 /* netgraph node hooks for ng_ether(4) */
103 void (*ng_ether_input_p)(struct ifnet *ifp,
104 struct mbuf **mp, struct ether_header *eh);
105 void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
106 struct mbuf *m, struct ether_header *eh);
107 int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
108 void (*ng_ether_attach_p)(struct ifnet *ifp);
109 void (*ng_ether_detach_p)(struct ifnet *ifp);
111 int (*vlan_input_p)(struct ether_header *eh, struct mbuf *m);
112 int (*vlan_input_tag_p)(struct mbuf *m, uint16_t t);
114 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
115 struct rtentry *);
118 * if_bridge support
120 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
121 int (*bridge_output_p)(struct ifnet *, struct mbuf *,
122 struct sockaddr *, struct rtentry *);
123 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
125 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
126 struct sockaddr *);
128 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
129 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
132 #define gotoerr(e) do { error = (e); goto bad; } while (0)
133 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
135 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
136 struct ip_fw **rule, struct ether_header *eh,
137 boolean_t shared);
139 static int ether_ipfw;
140 SYSCTL_DECL(_net_link);
141 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
142 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
143 &ether_ipfw, 0, "Pass ether pkts through firewall");
146 * Ethernet output routine.
147 * Encapsulate a packet of type family for the local net.
148 * Use trailer local net encapsulation if enough data in first
149 * packet leaves a multiple of 512 bytes of data in remainder.
150 * Assumes that ifp is actually pointer to arpcom structure.
152 static int
153 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
154 struct rtentry *rt)
156 struct ether_header *eh, *deh;
157 u_char *edst;
158 int loop_copy = 0;
159 int hlen = ETHER_HDR_LEN; /* link layer header length */
160 struct arpcom *ac = IFP2AC(ifp);
161 int error;
163 ASSERT_SERIALIZED(ifp->if_serializer);
165 if (ifp->if_flags & IFF_MONITOR)
166 gotoerr(ENETDOWN);
167 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
168 gotoerr(ENETDOWN);
170 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
171 if (m == NULL)
172 return (ENOBUFS);
173 eh = mtod(m, struct ether_header *);
174 edst = eh->ether_dhost;
177 * Fill in the destination ethernet address and frame type.
179 switch (dst->sa_family) {
180 #ifdef INET
181 case AF_INET:
182 if (!arpresolve(ifp, rt, m, dst, edst))
183 return (0); /* if not yet resolved */
184 eh->ether_type = htons(ETHERTYPE_IP);
185 break;
186 #endif
187 #ifdef INET6
188 case AF_INET6:
189 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
190 return (0); /* Something bad happenned. */
191 eh->ether_type = htons(ETHERTYPE_IPV6);
192 break;
193 #endif
194 #ifdef IPX
195 case AF_IPX:
196 if (ef_outputp != NULL) {
197 error = ef_outputp(ifp, &m, dst, &eh->ether_type,
198 &hlen);
199 if (error)
200 goto bad;
201 } else {
202 eh->ether_type = htons(ETHERTYPE_IPX);
203 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
204 edst, ETHER_ADDR_LEN);
206 break;
207 #endif
208 #ifdef NETATALK
209 case AF_APPLETALK: {
210 struct at_ifaddr *aa;
212 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
213 error = 0; /* XXX */
214 goto bad;
217 * In the phase 2 case, need to prepend an mbuf for
218 * the llc header. Since we must preserve the value
219 * of m, which is passed to us by value, we m_copy()
220 * the first mbuf, and use it for our llc header.
222 if (aa->aa_flags & AFA_PHASE2) {
223 struct llc llc;
225 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
226 eh = mtod(m, struct ether_header *);
227 edst = eh->ether_dhost;
228 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
229 llc.llc_control = LLC_UI;
230 bcopy(at_org_code, llc.llc_snap_org_code,
231 sizeof at_org_code);
232 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
233 bcopy(&llc,
234 mtod(m, caddr_t) + sizeof(struct ether_header),
235 sizeof(struct llc));
236 eh->ether_type = htons(m->m_pkthdr.len);
237 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
238 } else {
239 eh->ether_type = htons(ETHERTYPE_AT);
241 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
242 return (0);
243 break;
245 #endif
246 #ifdef NS
247 case AF_NS:
248 switch(ns_nettype) {
249 default:
250 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
251 eh->ether_type = 0x8137;
252 break;
253 case 0x0: /* Novell 802.3 */
254 eh->ether_type = htons(m->m_pkthdr.len);
255 break;
256 case 0xe0e0: /* Novell 802.2 and Token-Ring */
257 M_PREPEND(m, 3, MB_DONTWAIT);
258 eh = mtod(m, struct ether_header *);
259 edst = eh->ether_dhost;
260 eh->ether_type = htons(m->m_pkthdr.len);
261 cp = mtod(m, u_char *) + sizeof(struct ether_header);
262 *cp++ = 0xE0;
263 *cp++ = 0xE0;
264 *cp++ = 0x03;
265 break;
267 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
268 ETHER_ADDR_LEN);
270 * XXX if ns_thishost is the same as the node's ethernet
271 * address then just the default code will catch this anyhow.
272 * So I'm not sure if this next clause should be here at all?
273 * [JRE]
275 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
276 m->m_pkthdr.rcvif = ifp;
277 netisr_dispatch(NETISR_NS, m);
278 return (error);
280 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
281 m->m_flags |= M_BCAST;
282 break;
283 #endif
284 case pseudo_AF_HDRCMPLT:
285 case AF_UNSPEC:
286 loop_copy = -1; /* if this is for us, don't do it */
287 deh = (struct ether_header *)dst->sa_data;
288 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
289 eh->ether_type = deh->ether_type;
290 break;
292 default:
293 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
294 gotoerr(EAFNOSUPPORT);
297 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
298 memcpy(eh->ether_shost,
299 ((struct ether_header *)dst->sa_data)->ether_shost,
300 ETHER_ADDR_LEN);
301 else
302 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
305 * Bridges require special output handling.
307 if (ifp->if_bridge) {
308 KASSERT(bridge_output_p != NULL,
309 ("%s: if_bridge not loaded!", __func__));
310 return ((*bridge_output_p)(ifp, m, NULL, NULL));
314 * If a simplex interface, and the packet is being sent to our
315 * Ethernet address or a broadcast address, loopback a copy.
316 * XXX To make a simplex device behave exactly like a duplex
317 * device, we should copy in the case of sending to our own
318 * ethernet address (thus letting the original actually appear
319 * on the wire). However, we don't do that here for security
320 * reasons and compatibility with the original behavior.
322 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
323 int csum_flags = 0;
325 if (m->m_pkthdr.csum_flags & CSUM_IP)
326 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
327 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
328 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
329 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
330 struct mbuf *n;
332 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
333 n->m_pkthdr.csum_flags |= csum_flags;
334 if (csum_flags & CSUM_DATA_VALID)
335 n->m_pkthdr.csum_data = 0xffff;
336 if_simloop(ifp, n, dst->sa_family, hlen);
337 } else
338 ifp->if_iqdrops++;
339 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
340 ETHER_ADDR_LEN) == 0) {
341 m->m_pkthdr.csum_flags |= csum_flags;
342 if (csum_flags & CSUM_DATA_VALID)
343 m->m_pkthdr.csum_data = 0xffff;
344 if_simloop(ifp, m, dst->sa_family, hlen);
345 return (0); /* XXX */
349 /* Handle ng_ether(4) processing, if any */
350 if (ng_ether_output_p != NULL) {
351 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
352 goto bad;
353 if (m == NULL)
354 return (0);
357 /* Continue with link-layer output */
358 return ether_output_frame(ifp, m);
360 bad:
361 m_freem(m);
362 return (error);
366 * Ethernet link layer output routine to send a raw frame to the device.
368 * This assumes that the 14 byte Ethernet header is present and contiguous
369 * in the first mbuf.
372 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
374 struct ip_fw *rule = NULL;
375 int error = 0;
376 struct altq_pktattr pktattr;
378 ASSERT_SERIALIZED(ifp->if_serializer);
380 /* Extract info from dummynet tag, ignore others */
381 while (m->m_type == MT_TAG) {
382 if (m->m_flags == PACKET_TAG_DUMMYNET) {
383 rule = ((struct dn_pkt *)m)->rule;
384 break;
386 m = m->m_next;
389 if (ifq_is_enabled(&ifp->if_snd))
390 altq_etherclassify(&ifp->if_snd, m, &pktattr);
391 crit_enter();
392 if (IPFW_LOADED && ether_ipfw != 0) {
393 struct ether_header save_eh, *eh;
395 eh = mtod(m, struct ether_header *);
396 save_eh = *eh;
397 m_adj(m, ETHER_HDR_LEN);
398 if (!ether_ipfw_chk(&m, ifp, &rule, eh, FALSE)) {
399 crit_exit();
400 if (m != NULL) {
401 m_freem(m);
402 return ENOBUFS; /* pkt dropped */
403 } else
404 return 0; /* consumed e.g. in a pipe */
406 eh = mtod(m, struct ether_header *);
407 /* packet was ok, restore the ethernet header */
408 if ((void *)(eh + 1) == (void *)m->m_data) {
409 m->m_data -= ETHER_HDR_LEN ;
410 m->m_len += ETHER_HDR_LEN ;
411 m->m_pkthdr.len += ETHER_HDR_LEN ;
412 } else {
413 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
414 if (m == NULL) /* nope... */ {
415 crit_exit();
416 return ENOBUFS;
418 bcopy(&save_eh, mtod(m, struct ether_header *),
419 ETHER_HDR_LEN);
422 crit_exit();
425 * Queue message on interface, update output statistics if
426 * successful, and start output if interface not yet active.
428 error = ifq_handoff(ifp, m, &pktattr);
429 return (error);
433 * ipfw processing for ethernet packets (in and out).
434 * The second parameter is NULL from ether_demux(), and ifp from
435 * ether_output_frame().
437 static boolean_t
438 ether_ipfw_chk(
439 struct mbuf **m0,
440 struct ifnet *dst,
441 struct ip_fw **rule,
442 struct ether_header *eh,
443 boolean_t shared)
445 struct ether_header save_eh = *eh; /* might be a ptr in m */
446 struct ip_fw_args args;
447 struct m_tag *mtag;
448 int i;
450 if (*rule != NULL && fw_one_pass)
451 return TRUE; /* dummynet packet, already partially processed */
454 * I need some amount of data to be contiguous, and in case others
455 * need the packet (shared==TRUE), it also better be in the first mbuf.
457 i = min((*m0)->m_pkthdr.len, max_protohdr);
458 if (shared || (*m0)->m_len < i) {
459 *m0 = m_pullup(*m0, i);
460 if (*m0 == NULL)
461 return FALSE;
464 args.m = *m0; /* the packet we are looking at */
465 args.oif = dst; /* destination, if any */
466 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
467 m_tag_delete(*m0, mtag);
468 args.rule = *rule; /* matching rule to restart */
469 args.next_hop = NULL; /* we do not support forward yet */
470 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
471 i = ip_fw_chk_ptr(&args);
472 *m0 = args.m;
473 *rule = args.rule;
475 if ((i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL) /* drop */
476 return FALSE;
478 if (i == 0) /* a PASS rule. */
479 return TRUE;
481 if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
483 * Pass the pkt to dummynet, which consumes it.
484 * If shared, make a copy and keep the original.
486 struct mbuf *m ;
488 if (shared) {
489 m = m_copypacket(*m0, MB_DONTWAIT);
490 if (m == NULL)
491 return FALSE;
492 } else {
493 m = *m0 ; /* pass the original to dummynet */
494 *m0 = NULL ; /* and nothing back to the caller */
497 * Prepend the header, optimize for the common case of
498 * eh pointing into the mbuf.
500 if ((void *)(eh + 1) == (void *)m->m_data) {
501 m->m_data -= ETHER_HDR_LEN ;
502 m->m_len += ETHER_HDR_LEN ;
503 m->m_pkthdr.len += ETHER_HDR_LEN ;
504 } else {
505 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
506 if (m == NULL)
507 return FALSE;
508 bcopy(&save_eh, mtod(m, struct ether_header *),
509 ETHER_HDR_LEN);
511 ip_dn_io_ptr(m, (i & 0xffff),
512 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
513 return FALSE;
516 * XXX at some point add support for divert/forward actions.
517 * If none of the above matches, we have to drop the pkt.
519 return FALSE;
523 * XXX merge this function with ether_input.
525 static void
526 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
528 ether_input(ifp, NULL, m);
532 * Process a received Ethernet packet. We have two different interfaces:
533 * one (conventional) assumes the packet in the mbuf, with the ethernet
534 * header provided separately in *eh. The second one (new) has everything
535 * in the mbuf, and we can tell it because eh == NULL.
536 * The caller MUST MAKE SURE that there are at least
537 * sizeof(struct ether_header) bytes in the first mbuf.
539 * This allows us to concentrate in one place a bunch of code which
540 * is replicated in all device drivers. Also, many functions called
541 * from ether_input() try to put the eh back into the mbuf, so we
542 * can later propagate the 'contiguous packet' interface to them,
543 * and handle the old interface just here.
545 * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or
546 * cluster, right before m_data. So be very careful when working on m,
547 * as you could destroy *eh !!
549 * First we perform any link layer operations, then continue
550 * to the upper layers with ether_demux().
552 void
553 ether_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
555 ASSERT_SERIALIZED(ifp->if_serializer);
557 /* XXX old crufty stuff, needs to be removed */
558 if (eh != NULL) {
559 kprintf("ether_input got mbuf without embedded ethernet header");
560 m_free(m);
561 return;
564 if (m->m_len < sizeof(struct ether_header)) {
565 /* XXX error in the caller. */
566 m_freem(m);
567 return;
569 m->m_pkthdr.rcvif = ifp;
570 eh = mtod(m, struct ether_header *);
572 BPF_MTAP(ifp, m);
574 ifp->if_ibytes += m->m_pkthdr.len;
576 if (ifp->if_flags & IFF_MONITOR) {
578 * Interface marked for monitoring; discard packet.
580 m_freem(m);
581 return;
585 * Tap the packet off here for a bridge. bridge_input()
586 * will return NULL if it has consumed the packet, otherwise
587 * it gets processed as normal. Note that bridge_input()
588 * will always return the original packet if we need to
589 * process it locally.
591 if (ifp->if_bridge) {
592 KASSERT(bridge_input_p != NULL,
593 ("%s: if_bridge not loaded!", __func__));
595 if(m->m_flags & M_PROTO1) {
596 m->m_flags &= ~M_PROTO1;
597 } else {
598 /* clear M_PROMISC, in case the packets comes from a vlan */
599 /* m->m_flags &= ~M_PROMISC; */
600 lwkt_serialize_exit(ifp->if_serializer);
601 m = (*bridge_input_p)(ifp, m);
602 lwkt_serialize_enter(ifp->if_serializer);
603 if (m == NULL)
604 return;
606 KASSERT(ifp == m->m_pkthdr.rcvif,
607 ("bridge_input_p changed rcvif\n"));
611 /* XXX old crufty stuff, needs to be removed */
612 m_adj(m, sizeof(struct ether_header));
613 /* XXX */
614 /* m->m_pkthdr.len = m->m_len; */
616 /* Handle ng_ether(4) processing, if any */
617 if (ng_ether_input_p != NULL) {
618 lwkt_serialize_exit(ifp->if_serializer);
619 (*ng_ether_input_p)(ifp, &m, eh);
620 lwkt_serialize_enter(ifp->if_serializer);
621 if (m == NULL)
622 return;
625 /* Continue with upper layer processing */
626 ether_demux(ifp, eh, m);
630 * Upper layer processing for a received Ethernet packet.
632 void
633 ether_demux(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
635 int isr;
636 u_short ether_type;
637 struct ip_fw *rule = NULL;
638 #ifdef NETATALK
639 struct llc *l;
640 #endif
642 /* Extract info from dummynet tag, ignore others */
643 while (m->m_type == MT_TAG) {
644 if (m->m_flags == PACKET_TAG_DUMMYNET) {
645 rule = ((struct dn_pkt *)m)->rule;
646 ifp = m->m_next->m_pkthdr.rcvif;
647 break;
649 m = m->m_next;
651 if (rule) /* packet is passing the second time */
652 goto post_stats;
655 * Discard packet if upper layers shouldn't see it because
656 * it was unicast to a different Ethernet address. If the
657 * driver is working properly, then this situation can only
658 * happen when the interface is in promiscuous mode.
660 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
661 (eh->ether_dhost[0] & 1) == 0 &&
662 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
663 m_freem(m);
664 return;
666 /* Discard packet if interface is not up */
667 if (!(ifp->if_flags & IFF_UP)) {
668 m_freem(m);
669 return;
671 if (eh->ether_dhost[0] & 1) {
672 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
673 ifp->if_addrlen) == 0)
674 m->m_flags |= M_BCAST;
675 else
676 m->m_flags |= M_MCAST;
677 ifp->if_imcasts++;
680 post_stats:
681 if (IPFW_LOADED && ether_ipfw != 0) {
682 if (!ether_ipfw_chk(&m, NULL, &rule, eh, FALSE)) {
683 m_freem(m);
684 return;
686 eh = mtod(m, struct ether_header *);
689 ether_type = ntohs(eh->ether_type);
691 switch (ether_type) {
692 #ifdef INET
693 case ETHERTYPE_IP:
694 if (ipflow_fastforward(m, ifp->if_serializer))
695 return;
696 isr = NETISR_IP;
697 break;
699 case ETHERTYPE_ARP:
700 if (ifp->if_flags & IFF_NOARP) {
701 /* Discard packet if ARP is disabled on interface */
702 m_freem(m);
703 return;
705 isr = NETISR_ARP;
706 break;
707 #endif
709 #ifdef INET6
710 case ETHERTYPE_IPV6:
711 isr = NETISR_IPV6;
712 break;
713 #endif
715 #ifdef IPX
716 case ETHERTYPE_IPX:
717 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
718 return;
719 isr = NETISR_IPX;
720 break;
721 #endif
723 #ifdef NS
724 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
725 isr = NETISR_NS;
726 break;
728 #endif
730 #ifdef NETATALK
731 case ETHERTYPE_AT:
732 isr = NETISR_ATALK1;
733 break;
734 case ETHERTYPE_AARP:
735 isr = NETISR_AARP;
736 break;
737 #endif
739 case ETHERTYPE_VLAN:
740 if (vlan_input_p != NULL)
741 (*vlan_input_p)(eh, m);
742 else {
743 m->m_pkthdr.rcvif->if_noproto++;
744 m_freem(m);
746 return;
748 default:
749 #ifdef IPX
750 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
751 return;
752 #endif
753 #ifdef NS
754 checksum = mtod(m, ushort *);
755 /* Novell 802.3 */
756 if ((ether_type <= ETHERMTU) &&
757 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
758 if (*checksum == 0xE0E0) {
759 m->m_pkthdr.len -= 3;
760 m->m_len -= 3;
761 m->m_data += 3;
763 isr = NETISR_NS;
764 break;
766 #endif
767 #ifdef NETATALK
768 if (ether_type > ETHERMTU)
769 goto dropanyway;
770 l = mtod(m, struct llc *);
771 if (l->llc_dsap == LLC_SNAP_LSAP &&
772 l->llc_ssap == LLC_SNAP_LSAP &&
773 l->llc_control == LLC_UI) {
774 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
775 sizeof at_org_code) == 0 &&
776 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
777 m_adj(m, sizeof(struct llc));
778 isr = NETISR_ATALK2;
779 break;
781 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
782 sizeof aarp_org_code) == 0 &&
783 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
784 m_adj(m, sizeof(struct llc));
785 isr = NETISR_AARP;
786 break;
789 dropanyway:
790 #endif
791 if (ng_ether_input_orphan_p != NULL)
792 (*ng_ether_input_orphan_p)(ifp, m, eh);
793 else
794 m_freem(m);
795 return;
797 netisr_dispatch(isr, m);
801 * Perform common duties while attaching to interface list
804 void
805 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
807 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
808 serializer);
811 void
812 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
813 lwkt_serialize_t serializer)
815 struct sockaddr_dl *sdl;
817 ifp->if_type = IFT_ETHER;
818 ifp->if_addrlen = ETHER_ADDR_LEN;
819 ifp->if_hdrlen = ETHER_HDR_LEN;
820 if_attach(ifp, serializer);
821 ifp->if_mtu = ETHERMTU;
822 if (ifp->if_baudrate == 0)
823 ifp->if_baudrate = 10000000;
824 ifp->if_output = ether_output;
825 ifp->if_input = ether_input_internal;
826 ifp->if_resolvemulti = ether_resolvemulti;
827 ifp->if_broadcastaddr = etherbroadcastaddr;
828 sdl = IF_LLSOCKADDR(ifp);
829 sdl->sdl_type = IFT_ETHER;
830 sdl->sdl_alen = ifp->if_addrlen;
831 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
833 * XXX Keep the current drivers happy.
834 * XXX Remove once all drivers have been cleaned up
836 if (lla != IFP2AC(ifp)->ac_enaddr)
837 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
838 bpfattach(ifp, dlt, hdrlen);
839 if (ng_ether_attach_p != NULL)
840 (*ng_ether_attach_p)(ifp);
842 if_printf(ifp, "MAC address: %6D\n", lla, ":");
846 * Perform common duties while detaching an Ethernet interface
848 void
849 ether_ifdetach(struct ifnet *ifp)
851 if_down(ifp);
853 if (ng_ether_detach_p != NULL)
854 (*ng_ether_detach_p)(ifp);
855 bpfdetach(ifp);
856 if_detach(ifp);
860 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
862 struct ifaddr *ifa = (struct ifaddr *) data;
863 struct ifreq *ifr = (struct ifreq *) data;
864 int error = 0;
866 ASSERT_SERIALIZED(ifp->if_serializer);
868 switch (command) {
869 case SIOCSIFADDR:
870 ifp->if_flags |= IFF_UP;
872 switch (ifa->ifa_addr->sa_family) {
873 #ifdef INET
874 case AF_INET:
875 ifp->if_init(ifp->if_softc); /* before arpwhohas */
876 arp_ifinit(ifp, ifa);
877 break;
878 #endif
879 #ifdef IPX
881 * XXX - This code is probably wrong
883 case AF_IPX:
885 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
886 struct arpcom *ac = IFP2AC(ifp);
888 if (ipx_nullhost(*ina))
889 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
890 else
891 bcopy(ina->x_host.c_host, ac->ac_enaddr,
892 sizeof ac->ac_enaddr);
894 ifp->if_init(ifp->if_softc); /* Set new address. */
895 break;
897 #endif
898 #ifdef NS
900 * XXX - This code is probably wrong
902 case AF_NS:
904 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
905 struct arpcom *ac = IFP2AC(ifp);
907 if (ns_nullhost(*ina))
908 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
909 else
910 bcopy(ina->x_host.c_host, ac->ac_enaddr,
911 sizeof ac->ac_enaddr);
914 * Set new address
916 ifp->if_init(ifp->if_softc);
917 break;
919 #endif
920 default:
921 ifp->if_init(ifp->if_softc);
922 break;
924 break;
926 case SIOCGIFADDR:
927 bcopy(IFP2AC(ifp)->ac_enaddr,
928 ((struct sockaddr *)ifr->ifr_data)->sa_data,
929 ETHER_ADDR_LEN);
930 break;
932 case SIOCSIFMTU:
934 * Set the interface MTU.
936 if (ifr->ifr_mtu > ETHERMTU) {
937 error = EINVAL;
938 } else {
939 ifp->if_mtu = ifr->ifr_mtu;
941 break;
942 default:
943 error = EINVAL;
944 break;
946 return (error);
950 ether_resolvemulti(
951 struct ifnet *ifp,
952 struct sockaddr **llsa,
953 struct sockaddr *sa)
955 struct sockaddr_dl *sdl;
956 struct sockaddr_in *sin;
957 #ifdef INET6
958 struct sockaddr_in6 *sin6;
959 #endif
960 u_char *e_addr;
962 switch(sa->sa_family) {
963 case AF_LINK:
965 * No mapping needed. Just check that it's a valid MC address.
967 sdl = (struct sockaddr_dl *)sa;
968 e_addr = LLADDR(sdl);
969 if ((e_addr[0] & 1) != 1)
970 return EADDRNOTAVAIL;
971 *llsa = 0;
972 return 0;
974 #ifdef INET
975 case AF_INET:
976 sin = (struct sockaddr_in *)sa;
977 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
978 return EADDRNOTAVAIL;
979 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
980 M_WAITOK | M_ZERO);
981 sdl->sdl_len = sizeof *sdl;
982 sdl->sdl_family = AF_LINK;
983 sdl->sdl_index = ifp->if_index;
984 sdl->sdl_type = IFT_ETHER;
985 sdl->sdl_alen = ETHER_ADDR_LEN;
986 e_addr = LLADDR(sdl);
987 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
988 *llsa = (struct sockaddr *)sdl;
989 return 0;
990 #endif
991 #ifdef INET6
992 case AF_INET6:
993 sin6 = (struct sockaddr_in6 *)sa;
994 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
996 * An IP6 address of 0 means listen to all
997 * of the Ethernet multicast address used for IP6.
998 * (This is used for multicast routers.)
1000 ifp->if_flags |= IFF_ALLMULTI;
1001 *llsa = 0;
1002 return 0;
1004 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1005 return EADDRNOTAVAIL;
1006 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1007 M_WAITOK | M_ZERO);
1008 sdl->sdl_len = sizeof *sdl;
1009 sdl->sdl_family = AF_LINK;
1010 sdl->sdl_index = ifp->if_index;
1011 sdl->sdl_type = IFT_ETHER;
1012 sdl->sdl_alen = ETHER_ADDR_LEN;
1013 e_addr = LLADDR(sdl);
1014 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1015 *llsa = (struct sockaddr *)sdl;
1016 return 0;
1017 #endif
1019 default:
1021 * Well, the text isn't quite right, but it's the name
1022 * that counts...
1024 return EAFNOSUPPORT;
1028 #if 0
1030 * This is for reference. We have a table-driven version
1031 * of the little-endian crc32 generator, which is faster
1032 * than the double-loop.
1034 uint32_t
1035 ether_crc32_le(const uint8_t *buf, size_t len)
1037 uint32_t c, crc, carry;
1038 size_t i, j;
1040 crc = 0xffffffffU; /* initial value */
1042 for (i = 0; i < len; i++) {
1043 c = buf[i];
1044 for (j = 0; j < 8; j++) {
1045 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1046 crc >>= 1;
1047 c >>= 1;
1048 if (carry)
1049 crc = (crc ^ ETHER_CRC_POLY_LE);
1053 return (crc);
1055 #else
1056 uint32_t
1057 ether_crc32_le(const uint8_t *buf, size_t len)
1059 static const uint32_t crctab[] = {
1060 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1061 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1062 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1063 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1065 uint32_t crc;
1066 size_t i;
1068 crc = 0xffffffffU; /* initial value */
1070 for (i = 0; i < len; i++) {
1071 crc ^= buf[i];
1072 crc = (crc >> 4) ^ crctab[crc & 0xf];
1073 crc = (crc >> 4) ^ crctab[crc & 0xf];
1076 return (crc);
1078 #endif
1080 uint32_t
1081 ether_crc32_be(const uint8_t *buf, size_t len)
1083 uint32_t c, crc, carry;
1084 size_t i, j;
1086 crc = 0xffffffffU; /* initial value */
1088 for (i = 0; i < len; i++) {
1089 c = buf[i];
1090 for (j = 0; j < 8; j++) {
1091 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1092 crc <<= 1;
1093 c >>= 1;
1094 if (carry)
1095 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1099 return (crc);
1103 * find the size of ethernet header, and call classifier
1105 void
1106 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
1107 struct altq_pktattr *pktattr)
1109 struct ether_header *eh;
1110 uint16_t ether_type;
1111 int hlen, af, hdrsize;
1112 caddr_t hdr;
1114 hlen = sizeof(struct ether_header);
1115 eh = mtod(m, struct ether_header *);
1117 ether_type = ntohs(eh->ether_type);
1118 if (ether_type < ETHERMTU) {
1119 /* ick! LLC/SNAP */
1120 struct llc *llc = (struct llc *)(eh + 1);
1121 hlen += 8;
1123 if (m->m_len < hlen ||
1124 llc->llc_dsap != LLC_SNAP_LSAP ||
1125 llc->llc_ssap != LLC_SNAP_LSAP ||
1126 llc->llc_control != LLC_UI)
1127 goto bad; /* not snap! */
1129 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
1132 if (ether_type == ETHERTYPE_IP) {
1133 af = AF_INET;
1134 hdrsize = 20; /* sizeof(struct ip) */
1135 #ifdef INET6
1136 } else if (ether_type == ETHERTYPE_IPV6) {
1137 af = AF_INET6;
1138 hdrsize = 40; /* sizeof(struct ip6_hdr) */
1139 #endif
1140 } else
1141 goto bad;
1143 while (m->m_len <= hlen) {
1144 hlen -= m->m_len;
1145 m = m->m_next;
1147 hdr = m->m_data + hlen;
1148 if (m->m_len < hlen + hdrsize) {
1150 * ip header is not in a single mbuf. this should not
1151 * happen in the current code.
1152 * (todo: use m_pulldown in the future)
1154 goto bad;
1156 m->m_data += hlen;
1157 m->m_len -= hlen;
1158 ifq_classify(ifq, m, af, pktattr);
1159 m->m_data -= hlen;
1160 m->m_len += hlen;
1162 return;
1164 bad:
1165 pktattr->pattr_class = NULL;
1166 pktattr->pattr_hdr = NULL;
1167 pktattr->pattr_af = AF_UNSPEC;