xref: /freebsd/sys/net/if_ethersubr.c (revision 63f9a4cb2684a303e3eb2ffed39c03a2e2b28ae0)
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_PORT_DENY_FLAG) || m == NULL) /* drop */
444 		return 0;
445 
446 	if (i == 0) /* a PASS rule.  */
447 		return 1;
448 
449 	if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
450 		/*
451 		 * Pass the pkt to dummynet, which consumes it.
452 		 * If shared, make a copy and keep the original.
453 		 */
454 		if (shared) {
455 			m = m_copypacket(m, M_DONTWAIT);
456 			if (m == NULL)
457 				return 0;
458 		} else {
459 			/*
460 			 * Pass the original to dummynet and
461 			 * nothing back to the caller
462 			 */
463 			*m0 = NULL ;
464 		}
465 		ip_dn_io_ptr(m, (i & 0xffff),
466 			dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
467 		return 0;
468 	}
469 	/*
470 	 * XXX at some point add support for divert/forward actions.
471 	 * If none of the above matches, we have to drop the pkt.
472 	 */
473 	return 0;
474 }
475 #endif
476 
477 /*
478  * Process a received Ethernet packet; the packet is in the
479  * mbuf chain m with the ethernet header at the front.
480  */
481 static void
482 ether_input(struct ifnet *ifp, struct mbuf *m)
483 {
484 	struct ether_header *eh;
485 	u_short etype;
486 
487 	/*
488 	 * Do consistency checks to verify assumptions
489 	 * made by code past this point.
490 	 */
491 	if ((m->m_flags & M_PKTHDR) == 0) {
492 		if_printf(ifp, "discard frame w/o packet header\n");
493 		ifp->if_ierrors++;
494 		m_freem(m);
495 		return;
496 	}
497 	if (m->m_len < ETHER_HDR_LEN) {
498 		/* XXX maybe should pullup? */
499 		if_printf(ifp, "discard frame w/o leading ethernet "
500 				"header (len %u pkt len %u)\n",
501 				m->m_len, m->m_pkthdr.len);
502 		ifp->if_ierrors++;
503 		m_freem(m);
504 		return;
505 	}
506 	eh = mtod(m, struct ether_header *);
507 	etype = ntohs(eh->ether_type);
508 	if (m->m_pkthdr.len >
509 	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
510 		if_printf(ifp, "discard oversize frame "
511 				"(ether type %x flags %x len %u > max %lu)\n",
512 				etype, m->m_flags, m->m_pkthdr.len,
513 				ETHER_MAX_FRAME(ifp, etype,
514 						m->m_flags & M_HASFCS));
515 		ifp->if_ierrors++;
516 		m_freem(m);
517 		return;
518 	}
519 	if (m->m_pkthdr.rcvif == NULL) {
520 		if_printf(ifp, "discard frame w/o interface pointer\n");
521 		ifp->if_ierrors++;
522 		m_freem(m);
523 		return;
524 	}
525 #ifdef DIAGNOSTIC
526 	if (m->m_pkthdr.rcvif != ifp) {
527 		if_printf(ifp, "Warning, frame marked as received on %s\n",
528 			m->m_pkthdr.rcvif->if_xname);
529 	}
530 #endif
531 
532 #ifdef MAC
533 	/*
534 	 * Tag the mbuf with an appropriate MAC label before any other
535 	 * consumers can get to it.
536 	 */
537 	mac_create_mbuf_from_ifnet(ifp, m);
538 #endif
539 
540 	/*
541 	 * Give bpf a chance at the packet.
542 	 */
543 	BPF_MTAP(ifp, m);
544 
545 	if (ifp->if_flags & IFF_MONITOR) {
546 		/*
547 		 * Interface marked for monitoring; discard packet.
548 		 */
549 		m_freem(m);
550 		return;
551 	}
552 
553 	/* If the CRC is still on the packet, trim it off. */
554 	if (m->m_flags & M_HASFCS) {
555 		m_adj(m, -ETHER_CRC_LEN);
556 		m->m_flags &= ~M_HASFCS;
557 	}
558 
559 	ifp->if_ibytes += m->m_pkthdr.len;
560 
561 	/* Handle ng_ether(4) processing, if any */
562 	if (ng_ether_input_p != NULL) {
563 		(*ng_ether_input_p)(ifp, &m);
564 		if (m == NULL)
565 			return;
566 	}
567 
568 	/* Check for bridging mode */
569 	if (BDG_ACTIVE(ifp) )
570 		if ((m = bridge_in_ptr(ifp, m)) == NULL)
571 			return;
572 
573 	/* First chunk of an mbuf contains good entropy */
574 	if (harvest.ethernet)
575 		random_harvest(m, 16, 3, 0, RANDOM_NET);
576 	ether_demux(ifp, m);
577 }
578 
579 /*
580  * Upper layer processing for a received Ethernet packet.
581  */
582 void
583 ether_demux(struct ifnet *ifp, struct mbuf *m)
584 {
585 	struct ether_header *eh;
586 	int isr;
587 	u_short ether_type;
588 #if defined(NETATALK)
589 	struct llc *l;
590 #endif
591 #if defined(INET) || defined(INET6)
592 	struct ip_fw *rule = ip_dn_claim_rule(m);
593 #endif
594 
595 	KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer"));
596 
597 	eh = mtod(m, struct ether_header *);
598 	ether_type = ntohs(eh->ether_type);
599 
600 #if defined(INET) || defined(INET6)
601 	if (rule)	/* packet was already bridged */
602 		goto post_stats;
603 #endif
604 
605 	if (!(BDG_ACTIVE(ifp)) &&
606 	    !(ether_type == ETHERTYPE_VLAN && ifp->if_nvlans > 0)) {
607 		/*
608 		 * Discard packet if upper layers shouldn't see it because it
609 		 * was unicast to a different Ethernet address. If the driver
610 		 * is working properly, then this situation can only happen
611 		 * when the interface is in promiscuous mode.
612 		 *
613 		 * If VLANs are active, and this packet has a VLAN tag, do
614 		 * not drop it here but pass it on to the VLAN layer, to
615 		 * give them a chance to consider it as well (e. g. in case
616 		 * bridging is only active on a VLAN).  They will drop it if
617 		 * it's undesired.
618 		 */
619 		if ((ifp->if_flags & IFF_PROMISC) != 0
620 		    && (eh->ether_dhost[0] & 1) == 0
621 		    && bcmp(eh->ether_dhost,
622 		      IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0
623 		    && (ifp->if_flags & IFF_PPROMISC) == 0) {
624 			    m_freem(m);
625 			    return;
626 		}
627 	}
628 
629 	/* Discard packet if interface is not up */
630 	if ((ifp->if_flags & IFF_UP) == 0) {
631 		m_freem(m);
632 		return;
633 	}
634 	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
635 		if (bcmp(etherbroadcastaddr, eh->ether_dhost,
636 		    sizeof(etherbroadcastaddr)) == 0)
637 			m->m_flags |= M_BCAST;
638 		else
639 			m->m_flags |= M_MCAST;
640 	}
641 	if (m->m_flags & (M_BCAST|M_MCAST))
642 		ifp->if_imcasts++;
643 
644 #if defined(INET) || defined(INET6)
645 post_stats:
646 	if (IPFW_LOADED && ether_ipfw != 0) {
647 		if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
648 			if (m)
649 				m_freem(m);
650 			return;
651 		}
652 	}
653 #endif
654 
655 	/*
656 	 * If VLANs are configured on the interface, check to
657 	 * see if the device performed the decapsulation and
658 	 * provided us with the tag.
659 	 */
660 	if (ifp->if_nvlans &&
661 	    m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL) != NULL) {
662 		/*
663 		 * vlan_input() will either recursively call ether_input()
664 		 * or drop the packet.
665 		 */
666 		KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!"));
667 		(*vlan_input_p)(ifp, m);
668 		return;
669 	}
670 
671 	/*
672 	 * Handle protocols that expect to have the Ethernet header
673 	 * (and possibly FCS) intact.
674 	 */
675 	switch (ether_type) {
676 	case ETHERTYPE_VLAN:
677 		if (ifp->if_nvlans != 0) {
678 			KASSERT(vlan_input_p,("ether_input: VLAN not loaded!"));
679 			(*vlan_input_p)(ifp, m);
680 		} else {
681 			ifp->if_noproto++;
682 			m_freem(m);
683 		}
684 		return;
685 	}
686 
687 	/* Strip off Ethernet header. */
688 	m_adj(m, ETHER_HDR_LEN);
689 
690 	/* If the CRC is still on the packet, trim it off. */
691 	if (m->m_flags & M_HASFCS) {
692 		m_adj(m, -ETHER_CRC_LEN);
693 		m->m_flags &= ~M_HASFCS;
694 	}
695 
696 	switch (ether_type) {
697 #ifdef INET
698 	case ETHERTYPE_IP:
699 		if (ip_fastforward(m))
700 			return;
701 		isr = NETISR_IP;
702 		break;
703 
704 	case ETHERTYPE_ARP:
705 		if (ifp->if_flags & IFF_NOARP) {
706 			/* Discard packet if ARP is disabled on interface */
707 			m_freem(m);
708 			return;
709 		}
710 		isr = NETISR_ARP;
711 		break;
712 #endif
713 #ifdef IPX
714 	case ETHERTYPE_IPX:
715 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
716 			return;
717 		isr = NETISR_IPX;
718 		break;
719 #endif
720 #ifdef INET6
721 	case ETHERTYPE_IPV6:
722 		isr = NETISR_IPV6;
723 		break;
724 #endif
725 #ifdef NETATALK
726 	case ETHERTYPE_AT:
727 		isr = NETISR_ATALK1;
728 		break;
729 	case ETHERTYPE_AARP:
730 		isr = NETISR_AARP;
731 		break;
732 #endif /* NETATALK */
733 	default:
734 #ifdef IPX
735 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
736 			return;
737 #endif /* IPX */
738 #if defined(NETATALK)
739 		if (ether_type > ETHERMTU)
740 			goto discard;
741 		l = mtod(m, struct llc *);
742 		if (l->llc_dsap == LLC_SNAP_LSAP &&
743 		    l->llc_ssap == LLC_SNAP_LSAP &&
744 		    l->llc_control == LLC_UI) {
745 			if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
746 			    sizeof(at_org_code)) == 0 &&
747 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
748 				m_adj(m, LLC_SNAPFRAMELEN);
749 				isr = NETISR_ATALK2;
750 				break;
751 			}
752 			if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
753 			    sizeof(aarp_org_code)) == 0 &&
754 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
755 				m_adj(m, LLC_SNAPFRAMELEN);
756 				isr = NETISR_AARP;
757 				break;
758 			}
759 		}
760 #endif /* NETATALK */
761 		goto discard;
762 	}
763 	netisr_dispatch(isr, m);
764 	return;
765 
766 discard:
767 	/*
768 	 * Packet is to be discarded.  If netgraph is present,
769 	 * hand the packet to it for last chance processing;
770 	 * otherwise dispose of it.
771 	 */
772 	if (ng_ether_input_orphan_p != NULL) {
773 		/*
774 		 * Put back the ethernet header so netgraph has a
775 		 * consistent view of inbound packets.
776 		 */
777 		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
778 		(*ng_ether_input_orphan_p)(ifp, m);
779 		return;
780 	}
781 	m_freem(m);
782 }
783 
784 /*
785  * Convert Ethernet address to printable (loggable) representation.
786  * This routine is for compatibility; it's better to just use
787  *
788  *	printf("%6D", <pointer to address>, ":");
789  *
790  * since there's no static buffer involved.
791  */
792 char *
793 ether_sprintf(const u_char *ap)
794 {
795 	static char etherbuf[18];
796 	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
797 	return (etherbuf);
798 }
799 
800 /*
801  * Perform common duties while attaching to interface list
802  */
803 void
804 ether_ifattach(struct ifnet *ifp, const u_int8_t *llc)
805 {
806 	int i;
807 	struct ifaddr *ifa;
808 	struct sockaddr_dl *sdl;
809 
810 	ifp->if_type = IFT_ETHER;
811 	ifp->if_addrlen = ETHER_ADDR_LEN;
812 	ifp->if_hdrlen = ETHER_HDR_LEN;
813 	if_attach(ifp);
814 	ifp->if_mtu = ETHERMTU;
815 	ifp->if_output = ether_output;
816 	ifp->if_input = ether_input;
817 	ifp->if_resolvemulti = ether_resolvemulti;
818 	if (ifp->if_baudrate == 0)
819 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
820 	ifp->if_broadcastaddr = etherbroadcastaddr;
821 
822 	ifa = ifaddr_byindex(ifp->if_index);
823 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
824 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
825 	sdl->sdl_type = IFT_ETHER;
826 	sdl->sdl_alen = ifp->if_addrlen;
827 	bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
828 	/*
829 	 * XXX: This doesn't belong here; we do it until
830 	 * XXX:  all drivers are cleaned up
831 	 */
832 	if (llc != IFP2AC(ifp)->ac_enaddr)
833 		bcopy(llc, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
834 
835 	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
836 	if (ng_ether_attach_p != NULL)
837 		(*ng_ether_attach_p)(ifp);
838 	if (BDG_LOADED)
839 		bdgtakeifaces_ptr();
840 
841 	/* Announce Ethernet MAC address if non-zero. */
842 	for (i = 0; i < ifp->if_addrlen; i++)
843 		if (llc[i] != 0)
844 			break;
845 	if (i != ifp->if_addrlen)
846 		if_printf(ifp, "Ethernet address: %6D\n", llc, ":");
847 	if (debug_mpsafenet && (ifp->if_flags & IFF_NEEDSGIANT) != 0)
848 		if_printf(ifp, "if_start running deferred for Giant\n");
849 }
850 
851 /*
852  * Perform common duties while detaching an Ethernet interface
853  */
854 void
855 ether_ifdetach(struct ifnet *ifp)
856 {
857 	if (ng_ether_detach_p != NULL)
858 		(*ng_ether_detach_p)(ifp);
859 	bpfdetach(ifp);
860 	if_detach(ifp);
861 	if (BDG_LOADED)
862 		bdgtakeifaces_ptr();
863 }
864 
865 SYSCTL_DECL(_net_link);
866 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
867 #if defined(INET) || defined(INET6)
868 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
869 	    &ether_ipfw,0,"Pass ether pkts through firewall");
870 #endif
871 
872 #if 0
873 /*
874  * This is for reference.  We have a table-driven version
875  * of the little-endian crc32 generator, which is faster
876  * than the double-loop.
877  */
878 uint32_t
879 ether_crc32_le(const uint8_t *buf, size_t len)
880 {
881 	size_t i;
882 	uint32_t crc;
883 	int bit;
884 	uint8_t data;
885 
886 	crc = 0xffffffff;	/* initial value */
887 
888 	for (i = 0; i < len; i++) {
889 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
890 			carry = (crc ^ data) & 1;
891 			crc >>= 1;
892 			if (carry)
893 				crc = (crc ^ ETHER_CRC_POLY_LE);
894 	}
895 
896 	return (crc);
897 }
898 #else
899 uint32_t
900 ether_crc32_le(const uint8_t *buf, size_t len)
901 {
902 	static const uint32_t crctab[] = {
903 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
904 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
905 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
906 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
907 	};
908 	size_t i;
909 	uint32_t crc;
910 
911 	crc = 0xffffffff;	/* initial value */
912 
913 	for (i = 0; i < len; i++) {
914 		crc ^= buf[i];
915 		crc = (crc >> 4) ^ crctab[crc & 0xf];
916 		crc = (crc >> 4) ^ crctab[crc & 0xf];
917 	}
918 
919 	return (crc);
920 }
921 #endif
922 
923 uint32_t
924 ether_crc32_be(const uint8_t *buf, size_t len)
925 {
926 	size_t i;
927 	uint32_t crc, carry;
928 	int bit;
929 	uint8_t data;
930 
931 	crc = 0xffffffff;	/* initial value */
932 
933 	for (i = 0; i < len; i++) {
934 		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
935 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
936 			crc <<= 1;
937 			if (carry)
938 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
939 		}
940 	}
941 
942 	return (crc);
943 }
944 
945 int
946 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
947 {
948 	struct ifaddr *ifa = (struct ifaddr *) data;
949 	struct ifreq *ifr = (struct ifreq *) data;
950 	int error = 0;
951 
952 	switch (command) {
953 	case SIOCSIFADDR:
954 		ifp->if_flags |= IFF_UP;
955 
956 		switch (ifa->ifa_addr->sa_family) {
957 #ifdef INET
958 		case AF_INET:
959 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
960 			arp_ifinit(ifp, ifa);
961 			break;
962 #endif
963 #ifdef IPX
964 		/*
965 		 * XXX - This code is probably wrong
966 		 */
967 		case AF_IPX:
968 			{
969 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
970 			struct arpcom *ac = IFP2AC(ifp);
971 
972 			if (ipx_nullhost(*ina))
973 				ina->x_host =
974 				    *(union ipx_host *)
975 				    ac->ac_enaddr;
976 			else {
977 				bcopy((caddr_t) ina->x_host.c_host,
978 				      (caddr_t) ac->ac_enaddr,
979 				      sizeof(ac->ac_enaddr));
980 			}
981 
982 			/*
983 			 * Set new address
984 			 */
985 			ifp->if_init(ifp->if_softc);
986 			break;
987 			}
988 #endif
989 		default:
990 			ifp->if_init(ifp->if_softc);
991 			break;
992 		}
993 		break;
994 
995 	case SIOCGIFADDR:
996 		{
997 			struct sockaddr *sa;
998 
999 			sa = (struct sockaddr *) & ifr->ifr_data;
1000 			bcopy(IFP2AC(ifp)->ac_enaddr,
1001 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1002 		}
1003 		break;
1004 
1005 	case SIOCSIFMTU:
1006 		/*
1007 		 * Set the interface MTU.
1008 		 */
1009 		if (ifr->ifr_mtu > ETHERMTU) {
1010 			error = EINVAL;
1011 		} else {
1012 			ifp->if_mtu = ifr->ifr_mtu;
1013 		}
1014 		break;
1015 	default:
1016 		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
1017 		break;
1018 	}
1019 	return (error);
1020 }
1021 
1022 static int
1023 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1024 	struct sockaddr *sa)
1025 {
1026 	struct sockaddr_dl *sdl;
1027 #ifdef INET
1028 	struct sockaddr_in *sin;
1029 #endif
1030 #ifdef INET6
1031 	struct sockaddr_in6 *sin6;
1032 #endif
1033 	u_char *e_addr;
1034 
1035 	switch(sa->sa_family) {
1036 	case AF_LINK:
1037 		/*
1038 		 * No mapping needed. Just check that it's a valid MC address.
1039 		 */
1040 		sdl = (struct sockaddr_dl *)sa;
1041 		e_addr = LLADDR(sdl);
1042 		if (!ETHER_IS_MULTICAST(e_addr))
1043 			return EADDRNOTAVAIL;
1044 		*llsa = 0;
1045 		return 0;
1046 
1047 #ifdef INET
1048 	case AF_INET:
1049 		sin = (struct sockaddr_in *)sa;
1050 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1051 			return EADDRNOTAVAIL;
1052 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1053 		       M_WAITOK|M_ZERO);
1054 		sdl->sdl_len = sizeof *sdl;
1055 		sdl->sdl_family = AF_LINK;
1056 		sdl->sdl_index = ifp->if_index;
1057 		sdl->sdl_type = IFT_ETHER;
1058 		sdl->sdl_alen = ETHER_ADDR_LEN;
1059 		e_addr = LLADDR(sdl);
1060 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1061 		*llsa = (struct sockaddr *)sdl;
1062 		return 0;
1063 #endif
1064 #ifdef INET6
1065 	case AF_INET6:
1066 		sin6 = (struct sockaddr_in6 *)sa;
1067 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1068 			/*
1069 			 * An IP6 address of 0 means listen to all
1070 			 * of the Ethernet multicast address used for IP6.
1071 			 * (This is used for multicast routers.)
1072 			 */
1073 			ifp->if_flags |= IFF_ALLMULTI;
1074 			*llsa = 0;
1075 			return 0;
1076 		}
1077 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1078 			return EADDRNOTAVAIL;
1079 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1080 		       M_WAITOK|M_ZERO);
1081 		sdl->sdl_len = sizeof *sdl;
1082 		sdl->sdl_family = AF_LINK;
1083 		sdl->sdl_index = ifp->if_index;
1084 		sdl->sdl_type = IFT_ETHER;
1085 		sdl->sdl_alen = ETHER_ADDR_LEN;
1086 		e_addr = LLADDR(sdl);
1087 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1088 		*llsa = (struct sockaddr *)sdl;
1089 		return 0;
1090 #endif
1091 
1092 	default:
1093 		/*
1094 		 * Well, the text isn't quite right, but it's the name
1095 		 * that counts...
1096 		 */
1097 		return EAFNOSUPPORT;
1098 	}
1099 }
1100 
1101 static moduledata_t ether_mod = {
1102 	"ether",
1103 	NULL,
1104 	0
1105 };
1106 
1107 DECLARE_MODULE(ether, ether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1108 MODULE_VERSION(ether, 1);
1109