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