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