xref: /freebsd/sys/net/if_ethersubr.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
1 /*-
2  * Copyright (c) 1982, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
30  * $FreeBSD$
31  */
32 
33 #include "opt_atalk.h"
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36 #include "opt_ipx.h"
37 #include "opt_bdg.h"
38 #include "opt_mac.h"
39 #include "opt_netgraph.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/mac.h>
45 #include <sys/malloc.h>
46 #include <sys/module.h>
47 #include <sys/mbuf.h>
48 #include <sys/random.h>
49 #include <sys/socket.h>
50 #include <sys/sockio.h>
51 #include <sys/sysctl.h>
52 
53 #include <net/if.h>
54 #include <net/if_arp.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
57 #include <net/if_llc.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/bpf.h>
61 #include <net/ethernet.h>
62 #include <net/bridge.h>
63 #include <net/if_vlan_var.h>
64 
65 #if defined(INET) || defined(INET6)
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/if_ether.h>
69 #include <netinet/ip_fw.h>
70 #include <netinet/ip_dummynet.h>
71 #endif
72 #ifdef INET6
73 #include <netinet6/nd6.h>
74 #endif
75 
76 #ifdef IPX
77 #include <netipx/ipx.h>
78 #include <netipx/ipx_if.h>
79 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
80 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
81 		struct sockaddr *dst, short *tp, int *hlen);
82 #endif
83 
84 #ifdef NETATALK
85 #include <netatalk/at.h>
86 #include <netatalk/at_var.h>
87 #include <netatalk/at_extern.h>
88 
89 #define llc_snap_org_code llc_un.type_snap.org_code
90 #define llc_snap_ether_type llc_un.type_snap.ether_type
91 
92 extern u_char	at_org_code[3];
93 extern u_char	aarp_org_code[3];
94 #endif /* NETATALK */
95 
96 /* netgraph node hooks for ng_ether(4) */
97 void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
98 void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
99 int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
100 void	(*ng_ether_attach_p)(struct ifnet *ifp);
101 void	(*ng_ether_detach_p)(struct ifnet *ifp);
102 
103 void	(*vlan_input_p)(struct ifnet *, struct mbuf *);
104 
105 /* bridge support */
106 int do_bridge;
107 bridge_in_t *bridge_in_ptr;
108 bdg_forward_t *bdg_forward_ptr;
109 bdgtakeifaces_t *bdgtakeifaces_ptr;
110 struct bdg_softc *ifp2sc;
111 
112 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
113 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
114 
115 static	int ether_resolvemulti(struct ifnet *, struct sockaddr **,
116 		struct sockaddr *);
117 
118 #define senderr(e) do { error = (e); goto bad;} while (0)
119 
120 #if defined(INET) || defined(INET6)
121 int
122 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
123 	struct ip_fw **rule, int shared);
124 static int ether_ipfw;
125 #endif
126 
127 /*
128  * Ethernet output routine.
129  * Encapsulate a packet of type family for the local net.
130  * Use trailer local net encapsulation if enough data in first
131  * packet leaves a multiple of 512 bytes of data in remainder.
132  * Assumes that ifp is actually pointer to arpcom structure.
133  */
134 int
135 ether_output(struct ifnet *ifp, struct mbuf *m,
136 	struct sockaddr *dst, struct rtentry *rt0)
137 {
138 	short type;
139 	int error, hdrcmplt = 0;
140 	u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
141 	struct ether_header *eh;
142 	int loop_copy = 0;
143 	int hlen;	/* link layer header length */
144 
145 #ifdef MAC
146 	error = mac_check_ifnet_transmit(ifp, m);
147 	if (error)
148 		senderr(error);
149 #endif
150 
151 	if (ifp->if_flags & IFF_MONITOR)
152 		senderr(ENETDOWN);
153 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
154 		senderr(ENETDOWN);
155 
156 	hlen = ETHER_HDR_LEN;
157 	switch (dst->sa_family) {
158 #ifdef INET
159 	case AF_INET:
160 		error = arpresolve(ifp, rt0, m, dst, edst);
161 		if (error)
162 			return (error == EWOULDBLOCK ? 0 : error);
163 		type = htons(ETHERTYPE_IP);
164 		break;
165 	case AF_ARP:
166 	{
167 		struct arphdr *ah;
168 		ah = mtod(m, struct arphdr *);
169 		ah->ar_hrd = htons(ARPHRD_ETHER);
170 
171 		loop_copy = -1; /* if this is for us, don't do it */
172 
173 		switch(ntohs(ah->ar_op)) {
174 		case ARPOP_REVREQUEST:
175 		case ARPOP_REVREPLY:
176 			type = htons(ETHERTYPE_REVARP);
177 			break;
178 		case ARPOP_REQUEST:
179 		case ARPOP_REPLY:
180 		default:
181 			type = htons(ETHERTYPE_ARP);
182 			break;
183 		}
184 
185 		if (m->m_flags & M_BCAST)
186 			bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
187 		else
188 			bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
189 
190 	}
191 	break;
192 #endif
193 #ifdef INET6
194 	case AF_INET6:
195 		error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst);
196 		if (error)
197 			return error;
198 		type = htons(ETHERTYPE_IPV6);
199 		break;
200 #endif
201 #ifdef IPX
202 	case AF_IPX:
203 		if (ef_outputp) {
204 		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
205 		    if (error)
206 			goto bad;
207 		} else
208 		    type = htons(ETHERTYPE_IPX);
209 		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
210 		    (caddr_t)edst, sizeof (edst));
211 		break;
212 #endif
213 #ifdef NETATALK
214 	case AF_APPLETALK:
215 	  {
216 	    struct at_ifaddr *aa;
217 
218 	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL)
219 		    senderr(EHOSTUNREACH); /* XXX */
220 	    if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst))
221 		    return (0);
222 	    /*
223 	     * In the phase 2 case, need to prepend an mbuf for the llc header.
224 	     */
225 	    if ( aa->aa_flags & AFA_PHASE2 ) {
226 		struct llc llc;
227 
228 		M_PREPEND(m, LLC_SNAPFRAMELEN, M_TRYWAIT);
229 		if (m == NULL)
230 			senderr(ENOBUFS);
231 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
232 		llc.llc_control = LLC_UI;
233 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
234 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
235 		bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
236 		type = htons(m->m_pkthdr.len);
237 		hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
238 	    } else {
239 		type = htons(ETHERTYPE_AT);
240 	    }
241 	    break;
242 	  }
243 #endif /* NETATALK */
244 
245 	case pseudo_AF_HDRCMPLT:
246 		hdrcmplt = 1;
247 		eh = (struct ether_header *)dst->sa_data;
248 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
249 		/* FALLTHROUGH */
250 
251 	case AF_UNSPEC:
252 		loop_copy = -1; /* if this is for us, don't do it */
253 		eh = (struct ether_header *)dst->sa_data;
254 		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
255 		type = eh->ether_type;
256 		break;
257 
258 	default:
259 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
260 		senderr(EAFNOSUPPORT);
261 	}
262 
263 	/*
264 	 * Add local net header.  If no space in first mbuf,
265 	 * allocate another.
266 	 */
267 	M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
268 	if (m == NULL)
269 		senderr(ENOBUFS);
270 	eh = mtod(m, struct ether_header *);
271 	(void)memcpy(&eh->ether_type, &type,
272 		sizeof(eh->ether_type));
273 	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
274 	if (hdrcmplt)
275 		(void)memcpy(eh->ether_shost, esrc,
276 			sizeof(eh->ether_shost));
277 	else
278 		(void)memcpy(eh->ether_shost, IFP2AC(ifp)->ac_enaddr,
279 			sizeof(eh->ether_shost));
280 
281 	/*
282 	 * If a simplex interface, and the packet is being sent to our
283 	 * Ethernet address or a broadcast address, loopback a copy.
284 	 * XXX To make a simplex device behave exactly like a duplex
285 	 * device, we should copy in the case of sending to our own
286 	 * ethernet address (thus letting the original actually appear
287 	 * on the wire). However, we don't do that here for security
288 	 * reasons and compatibility with the original behavior.
289 	 */
290 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
291 		int csum_flags = 0;
292 
293 		if (m->m_pkthdr.csum_flags & CSUM_IP)
294 			csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
295 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
296 			csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
297 
298 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
299 			struct mbuf *n;
300 
301 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
302 				n->m_pkthdr.csum_flags |= csum_flags;
303 				if (csum_flags & CSUM_DATA_VALID)
304 					n->m_pkthdr.csum_data = 0xffff;
305 				(void)if_simloop(ifp, n, dst->sa_family, hlen);
306 			} else
307 				ifp->if_iqdrops++;
308 		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
309 				ETHER_ADDR_LEN) == 0) {
310 			m->m_pkthdr.csum_flags |= csum_flags;
311 			if (csum_flags & CSUM_DATA_VALID)
312 				m->m_pkthdr.csum_data = 0xffff;
313 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
314 			return (0);	/* XXX */
315 		}
316 	}
317 
318 	/* Handle ng_ether(4) processing, if any */
319 	if (ng_ether_output_p != NULL) {
320 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
321 bad:			if (m != NULL)
322 				m_freem(m);
323 			return (error);
324 		}
325 		if (m == NULL)
326 			return (0);
327 	}
328 
329 	/* Continue with link-layer output */
330 	return ether_output_frame(ifp, m);
331 }
332 
333 /*
334  * Ethernet link layer output routine to send a raw frame to the device.
335  *
336  * This assumes that the 14 byte Ethernet header is present and contiguous
337  * in the first mbuf (if BRIDGE'ing).
338  */
339 int
340 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
341 {
342 #if defined(INET) || defined(INET6)
343 	struct ip_fw *rule = ip_dn_claim_rule(m);
344 #else
345 	void *rule = NULL;
346 #endif
347 	int error;
348 
349 	if (rule == NULL && BDG_ACTIVE(ifp)) {
350 		/*
351 		 * Beware, the bridge code notices the null rcvif and
352 		 * uses that identify that it's being called from
353 		 * ether_output as opposd to ether_input.  Yech.
354 		 */
355 		m->m_pkthdr.rcvif = NULL;
356 		m = bdg_forward_ptr(m, ifp);
357 		if (m != NULL)
358 			m_freem(m);
359 		return (0);
360 	}
361 #if defined(INET) || defined(INET6)
362 	if (IPFW_LOADED && ether_ipfw != 0) {
363 		if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
364 			if (m) {
365 				m_freem(m);
366 				return EACCES;	/* pkt dropped */
367 			} else
368 				return 0;	/* consumed e.g. in a pipe */
369 		}
370 	}
371 #endif
372 
373 	/*
374 	 * Queue message on interface, update output statistics if
375 	 * successful, and start output if interface not yet active.
376 	 */
377 	IFQ_HANDOFF(ifp, m, error);
378 	return (error);
379 }
380 
381 #if defined(INET) || defined(INET6)
382 /*
383  * ipfw processing for ethernet packets (in and out).
384  * The second parameter is NULL from ether_demux, and ifp from
385  * ether_output_frame. This section of code could be used from
386  * bridge.c as well as long as we use some extra info
387  * to distinguish that case from ether_output_frame();
388  */
389 int
390 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
391 	struct ip_fw **rule, int shared)
392 {
393 	struct ether_header *eh;
394 	struct ether_header save_eh;
395 	struct mbuf *m;
396 	int i;
397 	struct ip_fw_args args;
398 
399 	if (*rule != NULL && fw_one_pass)
400 		return 1; /* dummynet packet, already partially processed */
401 
402 	/*
403 	 * I need some amt of data to be contiguous, and in case others need
404 	 * the packet (shared==1) also better be in the first mbuf.
405 	 */
406 	m = *m0;
407 	i = min( m->m_pkthdr.len, max_protohdr);
408 	if ( shared || m->m_len < i) {
409 		m = m_pullup(m, i);
410 		if (m == NULL) {
411 			*m0 = m;
412 			return 0;
413 		}
414 	}
415 	eh = mtod(m, struct ether_header *);
416 	save_eh = *eh;			/* save copy for restore below */
417 	m_adj(m, ETHER_HDR_LEN);	/* strip ethernet header */
418 
419 	args.m = m;		/* the packet we are looking at		*/
420 	args.oif = dst;		/* destination, if any			*/
421 	args.rule = *rule;	/* matching rule to restart		*/
422 	args.next_hop = NULL;	/* we do not support forward yet	*/
423 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
424 	i = ip_fw_chk_ptr(&args);
425 	m = args.m;
426 	if (m != NULL) {
427 		/*
428 		 * Restore Ethernet header, as needed, in case the
429 		 * mbuf chain was replaced by ipfw.
430 		 */
431 		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
432 		if (m == NULL) {
433 			*m0 = m;
434 			return 0;
435 		}
436 		if (eh != mtod(m, struct ether_header *))
437 			bcopy(&save_eh, mtod(m, struct ether_header *),
438 				ETHER_HDR_LEN);
439 	}
440 	*m0 = m;
441 	*rule = args.rule;
442 
443 	if (i == IP_FW_DENY) /* drop */
444 		return 0;
445 
446 	KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL"));
447 
448 	if (i == IP_FW_PASS) /* a PASS rule.  */
449 		return 1;
450 
451 	if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
452 		/*
453 		 * Pass the pkt to dummynet, which consumes it.
454 		 * If shared, make a copy and keep the original.
455 		 */
456 		if (shared) {
457 			m = m_copypacket(m, M_DONTWAIT);
458 			if (m == NULL)
459 				return 0;
460 		} else {
461 			/*
462 			 * Pass the original to dummynet and
463 			 * nothing back to the caller
464 			 */
465 			*m0 = NULL ;
466 		}
467 		ip_dn_io_ptr(m, dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
468 		return 0;
469 	}
470 	/*
471 	 * XXX at some point add support for divert/forward actions.
472 	 * If none of the above matches, we have to drop the pkt.
473 	 */
474 	return 0;
475 }
476 #endif
477 
478 /*
479  * Process a received Ethernet packet; the packet is in the
480  * mbuf chain m with the ethernet header at the front.
481  */
482 static void
483 ether_input(struct ifnet *ifp, struct mbuf *m)
484 {
485 	struct ether_header *eh;
486 	u_short etype;
487 
488 	/*
489 	 * Do consistency checks to verify assumptions
490 	 * made by code past this point.
491 	 */
492 	if ((m->m_flags & M_PKTHDR) == 0) {
493 		if_printf(ifp, "discard frame w/o packet header\n");
494 		ifp->if_ierrors++;
495 		m_freem(m);
496 		return;
497 	}
498 	if (m->m_len < ETHER_HDR_LEN) {
499 		/* XXX maybe should pullup? */
500 		if_printf(ifp, "discard frame w/o leading ethernet "
501 				"header (len %u pkt len %u)\n",
502 				m->m_len, m->m_pkthdr.len);
503 		ifp->if_ierrors++;
504 		m_freem(m);
505 		return;
506 	}
507 	eh = mtod(m, struct ether_header *);
508 	etype = ntohs(eh->ether_type);
509 	if (m->m_pkthdr.len >
510 	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
511 		if_printf(ifp, "discard oversize frame "
512 				"(ether type %x flags %x len %u > max %lu)\n",
513 				etype, m->m_flags, m->m_pkthdr.len,
514 				ETHER_MAX_FRAME(ifp, etype,
515 						m->m_flags & M_HASFCS));
516 		ifp->if_ierrors++;
517 		m_freem(m);
518 		return;
519 	}
520 	if (m->m_pkthdr.rcvif == NULL) {
521 		if_printf(ifp, "discard frame w/o interface pointer\n");
522 		ifp->if_ierrors++;
523 		m_freem(m);
524 		return;
525 	}
526 #ifdef DIAGNOSTIC
527 	if (m->m_pkthdr.rcvif != ifp) {
528 		if_printf(ifp, "Warning, frame marked as received on %s\n",
529 			m->m_pkthdr.rcvif->if_xname);
530 	}
531 #endif
532 
533 #ifdef MAC
534 	/*
535 	 * Tag the mbuf with an appropriate MAC label before any other
536 	 * consumers can get to it.
537 	 */
538 	mac_create_mbuf_from_ifnet(ifp, m);
539 #endif
540 
541 	/*
542 	 * Give bpf a chance at the packet.
543 	 */
544 	BPF_MTAP(ifp, m);
545 
546 	if (ifp->if_flags & IFF_MONITOR) {
547 		/*
548 		 * Interface marked for monitoring; discard packet.
549 		 */
550 		m_freem(m);
551 		return;
552 	}
553 
554 	/* If the CRC is still on the packet, trim it off. */
555 	if (m->m_flags & M_HASFCS) {
556 		m_adj(m, -ETHER_CRC_LEN);
557 		m->m_flags &= ~M_HASFCS;
558 	}
559 
560 	ifp->if_ibytes += m->m_pkthdr.len;
561 
562 	/* Handle ng_ether(4) processing, if any */
563 	if (ng_ether_input_p != NULL) {
564 		(*ng_ether_input_p)(ifp, &m);
565 		if (m == NULL)
566 			return;
567 	}
568 
569 	/* Check for bridging mode */
570 	if (BDG_ACTIVE(ifp) )
571 		if ((m = bridge_in_ptr(ifp, m)) == NULL)
572 			return;
573 
574 	/* First chunk of an mbuf contains good entropy */
575 	if (harvest.ethernet)
576 		random_harvest(m, 16, 3, 0, RANDOM_NET);
577 	ether_demux(ifp, m);
578 }
579 
580 /*
581  * Upper layer processing for a received Ethernet packet.
582  */
583 void
584 ether_demux(struct ifnet *ifp, struct mbuf *m)
585 {
586 	struct ether_header *eh;
587 	int isr;
588 	u_short ether_type;
589 #if defined(NETATALK)
590 	struct llc *l;
591 #endif
592 #if defined(INET) || defined(INET6)
593 	struct ip_fw *rule = ip_dn_claim_rule(m);
594 #endif
595 
596 	KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer"));
597 
598 	eh = mtod(m, struct ether_header *);
599 	ether_type = ntohs(eh->ether_type);
600 
601 #if defined(INET) || defined(INET6)
602 	if (rule)	/* packet was already bridged */
603 		goto post_stats;
604 #endif
605 
606 	if (!(BDG_ACTIVE(ifp)) &&
607 	    !(ether_type == ETHERTYPE_VLAN && ifp->if_nvlans > 0)) {
608 		/*
609 		 * Discard packet if upper layers shouldn't see it because it
610 		 * was unicast to a different Ethernet address. If the driver
611 		 * is working properly, then this situation can only happen
612 		 * when the interface is in promiscuous mode.
613 		 *
614 		 * If VLANs are active, and this packet has a VLAN tag, do
615 		 * not drop it here but pass it on to the VLAN layer, to
616 		 * give them a chance to consider it as well (e. g. in case
617 		 * bridging is only active on a VLAN).  They will drop it if
618 		 * it's undesired.
619 		 */
620 		if ((ifp->if_flags & IFF_PROMISC) != 0
621 		    && (eh->ether_dhost[0] & 1) == 0
622 		    && bcmp(eh->ether_dhost,
623 		      IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0
624 		    && (ifp->if_flags & IFF_PPROMISC) == 0) {
625 			    m_freem(m);
626 			    return;
627 		}
628 	}
629 
630 	/* Discard packet if interface is not up */
631 	if ((ifp->if_flags & IFF_UP) == 0) {
632 		m_freem(m);
633 		return;
634 	}
635 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
636 		if (bcmp(etherbroadcastaddr, eh->ether_dhost,
637 		    sizeof(etherbroadcastaddr)) == 0)
638 			m->m_flags |= M_BCAST;
639 		else
640 			m->m_flags |= M_MCAST;
641 	}
642 	if (m->m_flags & (M_BCAST|M_MCAST))
643 		ifp->if_imcasts++;
644 
645 #if defined(INET) || defined(INET6)
646 post_stats:
647 	if (IPFW_LOADED && ether_ipfw != 0) {
648 		if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
649 			if (m)
650 				m_freem(m);
651 			return;
652 		}
653 	}
654 #endif
655 
656 	/*
657 	 * If VLANs are configured on the interface, check to
658 	 * see if the device performed the decapsulation and
659 	 * provided us with the tag.
660 	 */
661 	if (ifp->if_nvlans &&
662 	    m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL) != NULL) {
663 		/*
664 		 * vlan_input() will either recursively call ether_input()
665 		 * or drop the packet.
666 		 */
667 		KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!"));
668 		(*vlan_input_p)(ifp, m);
669 		return;
670 	}
671 
672 	/*
673 	 * Handle protocols that expect to have the Ethernet header
674 	 * (and possibly FCS) intact.
675 	 */
676 	switch (ether_type) {
677 	case ETHERTYPE_VLAN:
678 		if (ifp->if_nvlans != 0) {
679 			KASSERT(vlan_input_p,("ether_input: VLAN not loaded!"));
680 			(*vlan_input_p)(ifp, m);
681 		} else {
682 			ifp->if_noproto++;
683 			m_freem(m);
684 		}
685 		return;
686 	}
687 
688 	/* Strip off Ethernet header. */
689 	m_adj(m, ETHER_HDR_LEN);
690 
691 	/* If the CRC is still on the packet, trim it off. */
692 	if (m->m_flags & M_HASFCS) {
693 		m_adj(m, -ETHER_CRC_LEN);
694 		m->m_flags &= ~M_HASFCS;
695 	}
696 
697 	switch (ether_type) {
698 #ifdef INET
699 	case ETHERTYPE_IP:
700 		if (ip_fastforward(m))
701 			return;
702 		isr = NETISR_IP;
703 		break;
704 
705 	case ETHERTYPE_ARP:
706 		if (ifp->if_flags & IFF_NOARP) {
707 			/* Discard packet if ARP is disabled on interface */
708 			m_freem(m);
709 			return;
710 		}
711 		isr = NETISR_ARP;
712 		break;
713 #endif
714 #ifdef IPX
715 	case ETHERTYPE_IPX:
716 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
717 			return;
718 		isr = NETISR_IPX;
719 		break;
720 #endif
721 #ifdef INET6
722 	case ETHERTYPE_IPV6:
723 		isr = NETISR_IPV6;
724 		break;
725 #endif
726 #ifdef NETATALK
727 	case ETHERTYPE_AT:
728 		isr = NETISR_ATALK1;
729 		break;
730 	case ETHERTYPE_AARP:
731 		isr = NETISR_AARP;
732 		break;
733 #endif /* NETATALK */
734 	default:
735 #ifdef IPX
736 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
737 			return;
738 #endif /* IPX */
739 #if defined(NETATALK)
740 		if (ether_type > ETHERMTU)
741 			goto discard;
742 		l = mtod(m, struct llc *);
743 		if (l->llc_dsap == LLC_SNAP_LSAP &&
744 		    l->llc_ssap == LLC_SNAP_LSAP &&
745 		    l->llc_control == LLC_UI) {
746 			if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
747 			    sizeof(at_org_code)) == 0 &&
748 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
749 				m_adj(m, LLC_SNAPFRAMELEN);
750 				isr = NETISR_ATALK2;
751 				break;
752 			}
753 			if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
754 			    sizeof(aarp_org_code)) == 0 &&
755 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
756 				m_adj(m, LLC_SNAPFRAMELEN);
757 				isr = NETISR_AARP;
758 				break;
759 			}
760 		}
761 #endif /* NETATALK */
762 		goto discard;
763 	}
764 	netisr_dispatch(isr, m);
765 	return;
766 
767 discard:
768 	/*
769 	 * Packet is to be discarded.  If netgraph is present,
770 	 * hand the packet to it for last chance processing;
771 	 * otherwise dispose of it.
772 	 */
773 	if (ng_ether_input_orphan_p != NULL) {
774 		/*
775 		 * Put back the ethernet header so netgraph has a
776 		 * consistent view of inbound packets.
777 		 */
778 		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
779 		(*ng_ether_input_orphan_p)(ifp, m);
780 		return;
781 	}
782 	m_freem(m);
783 }
784 
785 /*
786  * Convert Ethernet address to printable (loggable) representation.
787  * This routine is for compatibility; it's better to just use
788  *
789  *	printf("%6D", <pointer to address>, ":");
790  *
791  * since there's no static buffer involved.
792  */
793 char *
794 ether_sprintf(const u_char *ap)
795 {
796 	static char etherbuf[18];
797 	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
798 	return (etherbuf);
799 }
800 
801 /*
802  * Perform common duties while attaching to interface list
803  */
804 void
805 ether_ifattach(struct ifnet *ifp, const u_int8_t *llc)
806 {
807 	int i;
808 	struct ifaddr *ifa;
809 	struct sockaddr_dl *sdl;
810 
811 	ifp->if_type = IFT_ETHER;
812 	ifp->if_addrlen = ETHER_ADDR_LEN;
813 	ifp->if_hdrlen = ETHER_HDR_LEN;
814 	if_attach(ifp);
815 	ifp->if_mtu = ETHERMTU;
816 	ifp->if_output = ether_output;
817 	ifp->if_input = ether_input;
818 	ifp->if_resolvemulti = ether_resolvemulti;
819 	if (ifp->if_baudrate == 0)
820 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
821 	ifp->if_broadcastaddr = etherbroadcastaddr;
822 
823 	ifa = ifaddr_byindex(ifp->if_index);
824 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
825 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
826 	sdl->sdl_type = IFT_ETHER;
827 	sdl->sdl_alen = ifp->if_addrlen;
828 	bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
829 	/*
830 	 * XXX: This doesn't belong here; we do it until
831 	 * XXX:  all drivers are cleaned up
832 	 */
833 	if (llc != IFP2AC(ifp)->ac_enaddr)
834 		bcopy(llc, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
835 
836 	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
837 	if (ng_ether_attach_p != NULL)
838 		(*ng_ether_attach_p)(ifp);
839 	if (BDG_LOADED)
840 		bdgtakeifaces_ptr();
841 
842 	/* Announce Ethernet MAC address if non-zero. */
843 	for (i = 0; i < ifp->if_addrlen; i++)
844 		if (llc[i] != 0)
845 			break;
846 	if (i != ifp->if_addrlen)
847 		if_printf(ifp, "Ethernet address: %6D\n", llc, ":");
848 	if (debug_mpsafenet && (ifp->if_flags & IFF_NEEDSGIANT) != 0)
849 		if_printf(ifp, "if_start running deferred for Giant\n");
850 }
851 
852 /*
853  * Perform common duties while detaching an Ethernet interface
854  */
855 void
856 ether_ifdetach(struct ifnet *ifp)
857 {
858 	if (ng_ether_detach_p != NULL)
859 		(*ng_ether_detach_p)(ifp);
860 	bpfdetach(ifp);
861 	if_detach(ifp);
862 	if (BDG_LOADED)
863 		bdgtakeifaces_ptr();
864 }
865 
866 SYSCTL_DECL(_net_link);
867 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
868 #if defined(INET) || defined(INET6)
869 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
870 	    &ether_ipfw,0,"Pass ether pkts through firewall");
871 #endif
872 
873 #if 0
874 /*
875  * This is for reference.  We have a table-driven version
876  * of the little-endian crc32 generator, which is faster
877  * than the double-loop.
878  */
879 uint32_t
880 ether_crc32_le(const uint8_t *buf, size_t len)
881 {
882 	size_t i;
883 	uint32_t crc;
884 	int bit;
885 	uint8_t data;
886 
887 	crc = 0xffffffff;	/* initial value */
888 
889 	for (i = 0; i < len; i++) {
890 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
891 			carry = (crc ^ data) & 1;
892 			crc >>= 1;
893 			if (carry)
894 				crc = (crc ^ ETHER_CRC_POLY_LE);
895 	}
896 
897 	return (crc);
898 }
899 #else
900 uint32_t
901 ether_crc32_le(const uint8_t *buf, size_t len)
902 {
903 	static const uint32_t crctab[] = {
904 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
905 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
906 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
907 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
908 	};
909 	size_t i;
910 	uint32_t crc;
911 
912 	crc = 0xffffffff;	/* initial value */
913 
914 	for (i = 0; i < len; i++) {
915 		crc ^= buf[i];
916 		crc = (crc >> 4) ^ crctab[crc & 0xf];
917 		crc = (crc >> 4) ^ crctab[crc & 0xf];
918 	}
919 
920 	return (crc);
921 }
922 #endif
923 
924 uint32_t
925 ether_crc32_be(const uint8_t *buf, size_t len)
926 {
927 	size_t i;
928 	uint32_t crc, carry;
929 	int bit;
930 	uint8_t data;
931 
932 	crc = 0xffffffff;	/* initial value */
933 
934 	for (i = 0; i < len; i++) {
935 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
936 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
937 			crc <<= 1;
938 			if (carry)
939 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
940 		}
941 	}
942 
943 	return (crc);
944 }
945 
946 int
947 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
948 {
949 	struct ifaddr *ifa = (struct ifaddr *) data;
950 	struct ifreq *ifr = (struct ifreq *) data;
951 	int error = 0;
952 
953 	switch (command) {
954 	case SIOCSIFADDR:
955 		ifp->if_flags |= IFF_UP;
956 
957 		switch (ifa->ifa_addr->sa_family) {
958 #ifdef INET
959 		case AF_INET:
960 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
961 			arp_ifinit(ifp, ifa);
962 			break;
963 #endif
964 #ifdef IPX
965 		/*
966 		 * XXX - This code is probably wrong
967 		 */
968 		case AF_IPX:
969 			{
970 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
971 			struct arpcom *ac = IFP2AC(ifp);
972 
973 			if (ipx_nullhost(*ina))
974 				ina->x_host =
975 				    *(union ipx_host *)
976 				    ac->ac_enaddr;
977 			else {
978 				bcopy((caddr_t) ina->x_host.c_host,
979 				      (caddr_t) ac->ac_enaddr,
980 				      sizeof(ac->ac_enaddr));
981 			}
982 
983 			/*
984 			 * Set new address
985 			 */
986 			ifp->if_init(ifp->if_softc);
987 			break;
988 			}
989 #endif
990 		default:
991 			ifp->if_init(ifp->if_softc);
992 			break;
993 		}
994 		break;
995 
996 	case SIOCGIFADDR:
997 		{
998 			struct sockaddr *sa;
999 
1000 			sa = (struct sockaddr *) & ifr->ifr_data;
1001 			bcopy(IFP2AC(ifp)->ac_enaddr,
1002 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1003 		}
1004 		break;
1005 
1006 	case SIOCSIFMTU:
1007 		/*
1008 		 * Set the interface MTU.
1009 		 */
1010 		if (ifr->ifr_mtu > ETHERMTU) {
1011 			error = EINVAL;
1012 		} else {
1013 			ifp->if_mtu = ifr->ifr_mtu;
1014 		}
1015 		break;
1016 	default:
1017 		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
1018 		break;
1019 	}
1020 	return (error);
1021 }
1022 
1023 static int
1024 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1025 	struct sockaddr *sa)
1026 {
1027 	struct sockaddr_dl *sdl;
1028 #ifdef INET
1029 	struct sockaddr_in *sin;
1030 #endif
1031 #ifdef INET6
1032 	struct sockaddr_in6 *sin6;
1033 #endif
1034 	u_char *e_addr;
1035 
1036 	switch(sa->sa_family) {
1037 	case AF_LINK:
1038 		/*
1039 		 * No mapping needed. Just check that it's a valid MC address.
1040 		 */
1041 		sdl = (struct sockaddr_dl *)sa;
1042 		e_addr = LLADDR(sdl);
1043 		if (!ETHER_IS_MULTICAST(e_addr))
1044 			return EADDRNOTAVAIL;
1045 		*llsa = 0;
1046 		return 0;
1047 
1048 #ifdef INET
1049 	case AF_INET:
1050 		sin = (struct sockaddr_in *)sa;
1051 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1052 			return EADDRNOTAVAIL;
1053 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1054 		       M_WAITOK|M_ZERO);
1055 		sdl->sdl_len = sizeof *sdl;
1056 		sdl->sdl_family = AF_LINK;
1057 		sdl->sdl_index = ifp->if_index;
1058 		sdl->sdl_type = IFT_ETHER;
1059 		sdl->sdl_alen = ETHER_ADDR_LEN;
1060 		e_addr = LLADDR(sdl);
1061 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1062 		*llsa = (struct sockaddr *)sdl;
1063 		return 0;
1064 #endif
1065 #ifdef INET6
1066 	case AF_INET6:
1067 		sin6 = (struct sockaddr_in6 *)sa;
1068 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1069 			/*
1070 			 * An IP6 address of 0 means listen to all
1071 			 * of the Ethernet multicast address used for IP6.
1072 			 * (This is used for multicast routers.)
1073 			 */
1074 			ifp->if_flags |= IFF_ALLMULTI;
1075 			*llsa = 0;
1076 			return 0;
1077 		}
1078 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1079 			return EADDRNOTAVAIL;
1080 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1081 		       M_WAITOK|M_ZERO);
1082 		sdl->sdl_len = sizeof *sdl;
1083 		sdl->sdl_family = AF_LINK;
1084 		sdl->sdl_index = ifp->if_index;
1085 		sdl->sdl_type = IFT_ETHER;
1086 		sdl->sdl_alen = ETHER_ADDR_LEN;
1087 		e_addr = LLADDR(sdl);
1088 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1089 		*llsa = (struct sockaddr *)sdl;
1090 		return 0;
1091 #endif
1092 
1093 	default:
1094 		/*
1095 		 * Well, the text isn't quite right, but it's the name
1096 		 * that counts...
1097 		 */
1098 		return EAFNOSUPPORT;
1099 	}
1100 }
1101 
1102 static moduledata_t ether_mod = {
1103 	"ether",
1104 	NULL,
1105 	0
1106 };
1107 
1108 DECLARE_MODULE(ether, ether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1109 MODULE_VERSION(ether, 1);
1110