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