xref: /dflybsd-src/sys/net/if_ethersubr.c (revision ad30b684748061ca0c68e4a5ca21b45c240c52c5)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
34  * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
35  * $DragonFly: src/sys/net/if_ethersubr.c,v 1.40 2007/06/02 09:08:26 sephe Exp $
36  */
37 
38 #include "opt_atalk.h"
39 #include "opt_inet.h"
40 #include "opt_inet6.h"
41 #include "opt_ipx.h"
42 #include "opt_netgraph.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.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/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/ifq_var.h>
60 #include <net/bpf.h>
61 #include <net/ethernet.h>
62 
63 #if defined(INET) || defined(INET6)
64 #include <netinet/in.h>
65 #include <netinet/in_var.h>
66 #include <netinet/if_ether.h>
67 #include <net/ipfw/ip_fw.h>
68 #include <net/dummynet/ip_dummynet.h>
69 #endif
70 #ifdef INET6
71 #include <netinet6/nd6.h>
72 #endif
73 
74 #ifdef IPX
75 #include <netproto/ipx/ipx.h>
76 #include <netproto/ipx/ipx_if.h>
77 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
78 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
79 		  short *tp, int *hlen);
80 #endif
81 
82 #ifdef NS
83 #include <netns/ns.h>
84 #include <netns/ns_if.h>
85 ushort ns_nettype;
86 int ether_outputdebug = 0;
87 int ether_inputdebug = 0;
88 #endif
89 
90 #ifdef NETATALK
91 #include <netproto/atalk/at.h>
92 #include <netproto/atalk/at_var.h>
93 #include <netproto/atalk/at_extern.h>
94 
95 #define	llc_snap_org_code	llc_un.type_snap.org_code
96 #define	llc_snap_ether_type	llc_un.type_snap.ether_type
97 
98 extern u_char	at_org_code[3];
99 extern u_char	aarp_org_code[3];
100 #endif /* NETATALK */
101 
102 /* netgraph node hooks for ng_ether(4) */
103 void	(*ng_ether_input_p)(struct ifnet *ifp,
104 		struct mbuf **mp, struct ether_header *eh);
105 void	(*ng_ether_input_orphan_p)(struct ifnet *ifp,
106 		struct mbuf *m, struct ether_header *eh);
107 int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
108 void	(*ng_ether_attach_p)(struct ifnet *ifp);
109 void	(*ng_ether_detach_p)(struct ifnet *ifp);
110 
111 int	(*vlan_input_p)(struct ether_header *eh, struct mbuf *m);
112 int	(*vlan_input_tag_p)(struct mbuf *m, uint16_t t);
113 
114 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
115 			struct rtentry *);
116 
117 /*
118  * if_bridge support
119  */
120 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
121 int (*bridge_output_p)(struct ifnet *, struct mbuf *,
122 		       struct sockaddr *, struct rtentry *);
123 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
124 
125 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
126 			      struct sockaddr *);
127 
128 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
129 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff
130 };
131 
132 #define gotoerr(e) do { error = (e); goto bad; } while (0)
133 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
134 
135 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
136 				struct ip_fw **rule, struct ether_header *eh,
137 				boolean_t shared);
138 
139 static int ether_ipfw;
140 SYSCTL_DECL(_net_link);
141 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
142 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
143 	   &ether_ipfw, 0, "Pass ether pkts through firewall");
144 
145 /*
146  * Ethernet output routine.
147  * Encapsulate a packet of type family for the local net.
148  * Use trailer local net encapsulation if enough data in first
149  * packet leaves a multiple of 512 bytes of data in remainder.
150  * Assumes that ifp is actually pointer to arpcom structure.
151  */
152 static int
153 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
154 	     struct rtentry *rt)
155 {
156 	struct ether_header *eh, *deh;
157 	u_char *edst;
158 	int loop_copy = 0;
159 	int hlen = ETHER_HDR_LEN;	/* link layer header length */
160 	struct arpcom *ac = IFP2AC(ifp);
161 	int error;
162 
163 	ASSERT_SERIALIZED(ifp->if_serializer);
164 
165 	if (ifp->if_flags & IFF_MONITOR)
166 		gotoerr(ENETDOWN);
167 	if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
168 		gotoerr(ENETDOWN);
169 
170 	M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
171 	if (m == NULL)
172 		return (ENOBUFS);
173 	eh = mtod(m, struct ether_header *);
174 	edst = eh->ether_dhost;
175 
176 	/*
177 	 * Fill in the destination ethernet address and frame type.
178 	 */
179 	switch (dst->sa_family) {
180 #ifdef INET
181 	case AF_INET:
182 		if (!arpresolve(ifp, rt, m, dst, edst))
183 			return (0);	/* if not yet resolved */
184 		eh->ether_type = htons(ETHERTYPE_IP);
185 		break;
186 #endif
187 #ifdef INET6
188 	case AF_INET6:
189 		if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
190 			return (0);		/* Something bad happenned. */
191 		eh->ether_type = htons(ETHERTYPE_IPV6);
192 		break;
193 #endif
194 #ifdef IPX
195 	case AF_IPX:
196 		if (ef_outputp != NULL) {
197 			error = ef_outputp(ifp, &m, dst, &eh->ether_type,
198 					   &hlen);
199 			if (error)
200 				goto bad;
201 		} else {
202 			eh->ether_type = htons(ETHERTYPE_IPX);
203 			bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
204 			      edst, ETHER_ADDR_LEN);
205 		}
206 		break;
207 #endif
208 #ifdef NETATALK
209 	case AF_APPLETALK: {
210 		struct at_ifaddr *aa;
211 
212 		if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
213 			error = 0;	/* XXX */
214 			goto bad;
215 		}
216 		/*
217 		 * In the phase 2 case, need to prepend an mbuf for
218 		 * the llc header.  Since we must preserve the value
219 		 * of m, which is passed to us by value, we m_copy()
220 		 * the first mbuf, and use it for our llc header.
221 		 */
222 		if (aa->aa_flags & AFA_PHASE2) {
223 			struct llc llc;
224 
225 			M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
226 			eh = mtod(m, struct ether_header *);
227 			edst = eh->ether_dhost;
228 			llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
229 			llc.llc_control = LLC_UI;
230 			bcopy(at_org_code, llc.llc_snap_org_code,
231 			      sizeof at_org_code);
232 			llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
233 			bcopy(&llc,
234 			      mtod(m, caddr_t) + sizeof(struct ether_header),
235 			      sizeof(struct llc));
236 			eh->ether_type = htons(m->m_pkthdr.len);
237 			hlen = sizeof(struct llc) + ETHER_HDR_LEN;
238 		} else {
239 			eh->ether_type = htons(ETHERTYPE_AT);
240 		}
241 		if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
242 			return (0);
243 		break;
244 	  }
245 #endif
246 #ifdef NS
247 	case AF_NS:
248 		switch(ns_nettype) {
249 		default:
250 		case 0x8137:	/* Novell Ethernet_II Ethernet TYPE II */
251 			eh->ether_type = 0x8137;
252 			break;
253 		case 0x0:	/* Novell 802.3 */
254 			eh->ether_type = htons(m->m_pkthdr.len);
255 			break;
256 		case 0xe0e0:	/* Novell 802.2 and Token-Ring */
257 			M_PREPEND(m, 3, MB_DONTWAIT);
258 			eh = mtod(m, struct ether_header *);
259 			edst = eh->ether_dhost;
260 			eh->ether_type = htons(m->m_pkthdr.len);
261 			cp = mtod(m, u_char *) + sizeof(struct ether_header);
262 			*cp++ = 0xE0;
263 			*cp++ = 0xE0;
264 			*cp++ = 0x03;
265 			break;
266 		}
267 		bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
268 		      ETHER_ADDR_LEN);
269 		/*
270 		 * XXX if ns_thishost is the same as the node's ethernet
271 		 * address then just the default code will catch this anyhow.
272 		 * So I'm not sure if this next clause should be here at all?
273 		 * [JRE]
274 		 */
275 		if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
276 			m->m_pkthdr.rcvif = ifp;
277 			netisr_dispatch(NETISR_NS, m);
278 			return (error);
279 		}
280 		if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
281 			m->m_flags |= M_BCAST;
282 		break;
283 #endif
284 	case pseudo_AF_HDRCMPLT:
285 	case AF_UNSPEC:
286 		loop_copy = -1; /* if this is for us, don't do it */
287 		deh = (struct ether_header *)dst->sa_data;
288 		memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
289 		eh->ether_type = deh->ether_type;
290 		break;
291 
292 	default:
293 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
294 		gotoerr(EAFNOSUPPORT);
295 	}
296 
297 	if (dst->sa_family == pseudo_AF_HDRCMPLT)	/* unlikely */
298 		memcpy(eh->ether_shost,
299 		       ((struct ether_header *)dst->sa_data)->ether_shost,
300 		       ETHER_ADDR_LEN);
301 	else
302 		memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
303 
304 	/*
305 	 * Bridges require special output handling.
306 	 */
307 	if (ifp->if_bridge) {
308 		KASSERT(bridge_output_p != NULL,
309 			("%s: if_bridge not loaded!", __func__));
310 		return ((*bridge_output_p)(ifp, m, NULL, NULL));
311 	}
312 
313 	/*
314 	 * If a simplex interface, and the packet is being sent to our
315 	 * Ethernet address or a broadcast address, loopback a copy.
316 	 * XXX To make a simplex device behave exactly like a duplex
317 	 * device, we should copy in the case of sending to our own
318 	 * ethernet address (thus letting the original actually appear
319 	 * on the wire). However, we don't do that here for security
320 	 * reasons and compatibility with the original behavior.
321 	 */
322 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
323 		int csum_flags = 0;
324 
325 		if (m->m_pkthdr.csum_flags & CSUM_IP)
326 			csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
327 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
328 			csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
329 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
330 			struct mbuf *n;
331 
332 			if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
333 				n->m_pkthdr.csum_flags |= csum_flags;
334 				if (csum_flags & CSUM_DATA_VALID)
335 					n->m_pkthdr.csum_data = 0xffff;
336 				if_simloop(ifp, n, dst->sa_family, hlen);
337 			} else
338 				ifp->if_iqdrops++;
339 		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
340 				ETHER_ADDR_LEN) == 0) {
341 			m->m_pkthdr.csum_flags |= csum_flags;
342 			if (csum_flags & CSUM_DATA_VALID)
343 				m->m_pkthdr.csum_data = 0xffff;
344 			if_simloop(ifp, m, dst->sa_family, hlen);
345 			return (0);	/* XXX */
346 		}
347 	}
348 
349 	/* Handle ng_ether(4) processing, if any */
350 	if (ng_ether_output_p != NULL) {
351 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
352 			goto bad;
353 		if (m == NULL)
354 			return (0);
355 	}
356 
357 	/* Continue with link-layer output */
358 	return ether_output_frame(ifp, m);
359 
360 bad:
361 	m_freem(m);
362 	return (error);
363 }
364 
365 /*
366  * Ethernet link layer output routine to send a raw frame to the device.
367  *
368  * This assumes that the 14 byte Ethernet header is present and contiguous
369  * in the first mbuf.
370  */
371 int
372 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
373 {
374 	struct ip_fw *rule = NULL;
375 	int error = 0;
376 	struct altq_pktattr pktattr;
377 
378 	ASSERT_SERIALIZED(ifp->if_serializer);
379 
380 	/* Extract info from dummynet tag, ignore others */
381 	while (m->m_type == MT_TAG) {
382 		if (m->m_flags == PACKET_TAG_DUMMYNET) {
383 			rule = ((struct dn_pkt *)m)->rule;
384 			break;
385 		}
386 		m = m->m_next;
387 	}
388 
389 	if (ifq_is_enabled(&ifp->if_snd))
390 		altq_etherclassify(&ifp->if_snd, m, &pktattr);
391 	crit_enter();
392 	if (IPFW_LOADED && ether_ipfw != 0) {
393 		struct ether_header save_eh, *eh;
394 
395 		eh = mtod(m, struct ether_header *);
396 		save_eh = *eh;
397 		m_adj(m, ETHER_HDR_LEN);
398 		if (!ether_ipfw_chk(&m, ifp, &rule, eh, FALSE)) {
399 			crit_exit();
400 			if (m != NULL) {
401 				m_freem(m);
402 				return ENOBUFS; /* pkt dropped */
403 			} else
404 				return 0;	/* consumed e.g. in a pipe */
405 		}
406 		eh = mtod(m, struct ether_header *);
407 		/* packet was ok, restore the ethernet header */
408 		if ((void *)(eh + 1) == (void *)m->m_data) {
409 			m->m_data -= ETHER_HDR_LEN ;
410 			m->m_len += ETHER_HDR_LEN ;
411 			m->m_pkthdr.len += ETHER_HDR_LEN ;
412 		} else {
413 			M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
414 			if (m == NULL) /* nope... */ {
415 				crit_exit();
416 				return ENOBUFS;
417 			}
418 			bcopy(&save_eh, mtod(m, struct ether_header *),
419 			      ETHER_HDR_LEN);
420 		}
421 	}
422 	crit_exit();
423 
424 	/*
425 	 * Queue message on interface, update output statistics if
426 	 * successful, and start output if interface not yet active.
427 	 */
428 	error = ifq_handoff(ifp, m, &pktattr);
429 	return (error);
430 }
431 
432 /*
433  * ipfw processing for ethernet packets (in and out).
434  * The second parameter is NULL from ether_demux(), and ifp from
435  * ether_output_frame().
436  */
437 static boolean_t
438 ether_ipfw_chk(
439 	struct mbuf **m0,
440 	struct ifnet *dst,
441 	struct ip_fw **rule,
442 	struct ether_header *eh,
443 	boolean_t shared)
444 {
445 	struct ether_header save_eh = *eh;	/* might be a ptr in m */
446 	struct ip_fw_args args;
447 	struct m_tag *mtag;
448 	int i;
449 
450 	if (*rule != NULL && fw_one_pass)
451 		return TRUE; /* dummynet packet, already partially processed */
452 
453 	/*
454 	 * I need some amount of data to be contiguous, and in case others
455 	 * need the packet (shared==TRUE), it also better be in the first mbuf.
456 	 */
457 	i = min((*m0)->m_pkthdr.len, max_protohdr);
458 	if (shared || (*m0)->m_len < i) {
459 		*m0 = m_pullup(*m0, i);
460 		if (*m0 == NULL)
461 			return FALSE;
462 	}
463 
464 	args.m = *m0;		/* the packet we are looking at		*/
465 	args.oif = dst;		/* destination, if any			*/
466 	if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
467 		m_tag_delete(*m0, mtag);
468 	args.rule = *rule;	/* matching rule to restart		*/
469 	args.next_hop = NULL;	/* we do not support forward yet	*/
470 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
471 	i = ip_fw_chk_ptr(&args);
472 	*m0 = args.m;
473 	*rule = args.rule;
474 
475 	if ((i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL)	/* drop */
476 		return FALSE;
477 
478 	if (i == 0)					/* a PASS rule.  */
479 		return TRUE;
480 
481 	if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
482 		/*
483 		 * Pass the pkt to dummynet, which consumes it.
484 		 * If shared, make a copy and keep the original.
485 		 */
486 		struct mbuf *m ;
487 
488 		if (shared) {
489 			m = m_copypacket(*m0, MB_DONTWAIT);
490 			if (m == NULL)
491 				return FALSE;
492 		} else {
493 			m = *m0 ;	/* pass the original to dummynet */
494 			*m0 = NULL ;	/* and nothing back to the caller */
495 		}
496 		/*
497 		 * Prepend the header, optimize for the common case of
498 		 * eh pointing into the mbuf.
499 		 */
500 		if ((void *)(eh + 1) == (void *)m->m_data) {
501 			m->m_data -= ETHER_HDR_LEN ;
502 			m->m_len += ETHER_HDR_LEN ;
503 			m->m_pkthdr.len += ETHER_HDR_LEN ;
504 		} else {
505 			M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
506 			if (m == NULL)
507 				return FALSE;
508 			bcopy(&save_eh, mtod(m, struct ether_header *),
509 			      ETHER_HDR_LEN);
510 		}
511 		ip_dn_io_ptr(m, (i & 0xffff),
512 			     dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
513 		return FALSE;
514 	}
515 	/*
516 	 * XXX at some point add support for divert/forward actions.
517 	 * If none of the above matches, we have to drop the pkt.
518 	 */
519 	return FALSE;
520 }
521 
522 /*
523  * XXX merge this function with ether_input.
524  */
525 static void
526 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
527 {
528 	ether_input(ifp, NULL, m);
529 }
530 
531 /*
532  * Process a received Ethernet packet. We have two different interfaces:
533  * one (conventional) assumes the packet in the mbuf, with the ethernet
534  * header provided separately in *eh. The second one (new) has everything
535  * in the mbuf, and we can tell it because eh == NULL.
536  * The caller MUST MAKE SURE that there are at least
537  * sizeof(struct ether_header) bytes in the first mbuf.
538  *
539  * This allows us to concentrate in one place a bunch of code which
540  * is replicated in all device drivers. Also, many functions called
541  * from ether_input() try to put the eh back into the mbuf, so we
542  * can later propagate the 'contiguous packet' interface to them,
543  * and handle the old interface just here.
544  *
545  * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or
546  * cluster, right before m_data. So be very careful when working on m,
547  * as you could destroy *eh !!
548  *
549  * First we perform any link layer operations, then continue
550  * to the upper layers with ether_demux().
551  */
552 void
553 ether_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
554 {
555 	ASSERT_SERIALIZED(ifp->if_serializer);
556 
557 	/* XXX old crufty stuff, needs to be removed */
558 	if (eh != NULL) {
559 		kprintf("ether_input got mbuf without embedded ethernet header");
560 		m_free(m);
561 		return;
562 	}
563 
564 	if (m->m_len < sizeof(struct ether_header)) {
565 		/* XXX error in the caller. */
566 		m_freem(m);
567 		return;
568 	}
569 	m->m_pkthdr.rcvif = ifp;
570 	eh = mtod(m, struct ether_header *);
571 
572 	BPF_MTAP(ifp, m);
573 
574 	ifp->if_ibytes += m->m_pkthdr.len;
575 
576 	if (ifp->if_flags & IFF_MONITOR) {
577 		/*
578 		 * Interface marked for monitoring; discard packet.
579 		 */
580 		 m_freem(m);
581 		 return;
582 	}
583 
584 	/*
585 	 * Tap the packet off here for a bridge.  bridge_input()
586 	 * will return NULL if it has consumed the packet, otherwise
587 	 * it gets processed as normal.  Note that bridge_input()
588 	 * will always return the original packet if we need to
589 	 * process it locally.
590 	 */
591 	if (ifp->if_bridge) {
592 		KASSERT(bridge_input_p != NULL,
593 			("%s: if_bridge not loaded!", __func__));
594 
595 		if(m->m_flags & M_PROTO1) {
596 			m->m_flags &= ~M_PROTO1;
597 		} else {
598 			/* clear M_PROMISC, in case the packets comes from a vlan */
599 			/* m->m_flags &= ~M_PROMISC; */
600 			lwkt_serialize_exit(ifp->if_serializer);
601 			m = (*bridge_input_p)(ifp, m);
602 			lwkt_serialize_enter(ifp->if_serializer);
603 			if (m == NULL)
604 				return;
605 
606 			/*
607 			 * Bridge has determined that the packet is for us.
608 			 * Update our interface pointer -- we may have had
609 			 * to "bridge" the packet locally.
610 			 */
611 			ifp = m->m_pkthdr.rcvif;
612 		}
613 	}
614 
615 	/* XXX old crufty stuff, needs to be removed */
616 	m_adj(m, sizeof(struct ether_header));
617 	/* XXX */
618 	/* m->m_pkthdr.len = m->m_len; */
619 
620 	/* Handle ng_ether(4) processing, if any */
621 	if (ng_ether_input_p != NULL) {
622 		lwkt_serialize_exit(ifp->if_serializer);
623 		(*ng_ether_input_p)(ifp, &m, eh);
624 		lwkt_serialize_enter(ifp->if_serializer);
625 		if (m == NULL)
626 			return;
627 	}
628 
629 	/* Continue with upper layer processing */
630 	ether_demux(ifp, eh, m);
631 }
632 
633 /*
634  * Upper layer processing for a received Ethernet packet.
635  */
636 void
637 ether_demux(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
638 {
639 	int isr;
640 	u_short ether_type;
641 	struct ip_fw *rule = NULL;
642 #ifdef NETATALK
643 	struct llc *l;
644 #endif
645 
646 	/* Extract info from dummynet tag, ignore others */
647 	while (m->m_type == MT_TAG) {
648 		if (m->m_flags == PACKET_TAG_DUMMYNET) {
649 			rule = ((struct dn_pkt *)m)->rule;
650 			ifp = m->m_next->m_pkthdr.rcvif;
651 			break;
652 		}
653 		m = m->m_next;
654 	}
655 	if (rule)	/* packet is passing the second time */
656 		goto post_stats;
657 
658 	/*
659 	 * Discard packet if upper layers shouldn't see it because
660 	 * it was unicast to a different Ethernet address.  If the
661 	 * driver is working properly, then this situation can only
662 	 * happen when the interface is in promiscuous mode.
663 	 */
664 	if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
665 	    (eh->ether_dhost[0] & 1) == 0 &&
666 	    bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
667 		m_freem(m);
668 		return;
669 	}
670 	/* Discard packet if interface is not up */
671 	if (!(ifp->if_flags & IFF_UP)) {
672 		m_freem(m);
673 		return;
674 	}
675 	if (eh->ether_dhost[0] & 1) {
676 		if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
677 			 ifp->if_addrlen) == 0)
678 			m->m_flags |= M_BCAST;
679 		else
680 			m->m_flags |= M_MCAST;
681 		ifp->if_imcasts++;
682 	}
683 
684 post_stats:
685 	if (IPFW_LOADED && ether_ipfw != 0) {
686 		if (!ether_ipfw_chk(&m, NULL, &rule, eh, FALSE)) {
687 			m_freem(m);
688 			return;
689 		}
690 		eh = mtod(m, struct ether_header *);
691 	}
692 
693 	ether_type = ntohs(eh->ether_type);
694 
695 	switch (ether_type) {
696 #ifdef INET
697 	case ETHERTYPE_IP:
698 		if (ipflow_fastforward(m, ifp->if_serializer))
699 			return;
700 		isr = NETISR_IP;
701 		break;
702 
703 	case ETHERTYPE_ARP:
704 		if (ifp->if_flags & IFF_NOARP) {
705 			/* Discard packet if ARP is disabled on interface */
706 			m_freem(m);
707 			return;
708 		}
709 		isr = NETISR_ARP;
710 		break;
711 #endif
712 
713 #ifdef INET6
714 	case ETHERTYPE_IPV6:
715 		isr = NETISR_IPV6;
716 		break;
717 #endif
718 
719 #ifdef IPX
720 	case ETHERTYPE_IPX:
721 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
722 			return;
723 		isr = NETISR_IPX;
724 		break;
725 #endif
726 
727 #ifdef NS
728 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
729 		isr = NETISR_NS;
730 		break;
731 
732 #endif
733 
734 #ifdef NETATALK
735 	case ETHERTYPE_AT:
736 		isr = NETISR_ATALK1;
737 		break;
738 	case ETHERTYPE_AARP:
739 		isr = NETISR_AARP;
740 		break;
741 #endif
742 
743 	case ETHERTYPE_VLAN:
744 		if (vlan_input_p != NULL)
745 			(*vlan_input_p)(eh, m);
746 		else {
747 			m->m_pkthdr.rcvif->if_noproto++;
748 			m_freem(m);
749 		}
750 		return;
751 
752 	default:
753 #ifdef IPX
754 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
755 			return;
756 #endif
757 #ifdef NS
758 		checksum = mtod(m, ushort *);
759 		/* Novell 802.3 */
760 		if ((ether_type <= ETHERMTU) &&
761 		    ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
762 			if (*checksum == 0xE0E0) {
763 				m->m_pkthdr.len -= 3;
764 				m->m_len -= 3;
765 				m->m_data += 3;
766 			}
767 			isr = NETISR_NS;
768 			break;
769 		}
770 #endif
771 #ifdef NETATALK
772 		if (ether_type > ETHERMTU)
773 			goto dropanyway;
774 		l = mtod(m, struct llc *);
775 		if (l->llc_dsap == LLC_SNAP_LSAP &&
776 		    l->llc_ssap == LLC_SNAP_LSAP &&
777 		    l->llc_control == LLC_UI) {
778 			if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
779 				 sizeof at_org_code) == 0 &&
780 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
781 				m_adj(m, sizeof(struct llc));
782 				isr = NETISR_ATALK2;
783 				break;
784 			}
785 			if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
786 				 sizeof aarp_org_code) == 0 &&
787 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
788 				m_adj(m, sizeof(struct llc));
789 				isr = NETISR_AARP;
790 				break;
791 			}
792 		}
793 dropanyway:
794 #endif
795 		if (ng_ether_input_orphan_p != NULL)
796 			(*ng_ether_input_orphan_p)(ifp, m, eh);
797 		else
798 			m_freem(m);
799 		return;
800 	}
801 	netisr_dispatch(isr, m);
802 }
803 
804 /*
805  * Perform common duties while attaching to interface list
806  */
807 
808 void
809 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
810 {
811 	ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
812 			   serializer);
813 }
814 
815 void
816 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
817 		   lwkt_serialize_t serializer)
818 {
819 	struct sockaddr_dl *sdl;
820 
821 	ifp->if_type = IFT_ETHER;
822 	ifp->if_addrlen = ETHER_ADDR_LEN;
823 	ifp->if_hdrlen = ETHER_HDR_LEN;
824 	if_attach(ifp, serializer);
825 	ifp->if_mtu = ETHERMTU;
826 	if (ifp->if_baudrate == 0)
827 		ifp->if_baudrate = 10000000;
828 	ifp->if_output = ether_output;
829 	ifp->if_input = ether_input_internal;
830 	ifp->if_resolvemulti = ether_resolvemulti;
831 	ifp->if_broadcastaddr = etherbroadcastaddr;
832 	sdl = IF_LLSOCKADDR(ifp);
833 	sdl->sdl_type = IFT_ETHER;
834 	sdl->sdl_alen = ifp->if_addrlen;
835 	bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
836 	/*
837 	 * XXX Keep the current drivers happy.
838 	 * XXX Remove once all drivers have been cleaned up
839 	 */
840 	if (lla != IFP2AC(ifp)->ac_enaddr)
841 		bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
842 	bpfattach(ifp, dlt, hdrlen);
843 	if (ng_ether_attach_p != NULL)
844 		(*ng_ether_attach_p)(ifp);
845 
846 	if_printf(ifp, "MAC address: %6D\n", lla, ":");
847 }
848 
849 /*
850  * Perform common duties while detaching an Ethernet interface
851  */
852 void
853 ether_ifdetach(struct ifnet *ifp)
854 {
855 	if_down(ifp);
856 
857 	if (ng_ether_detach_p != NULL)
858 		(*ng_ether_detach_p)(ifp);
859 	bpfdetach(ifp);
860 	if_detach(ifp);
861 }
862 
863 int
864 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
865 {
866 	struct ifaddr *ifa = (struct ifaddr *) data;
867 	struct ifreq *ifr = (struct ifreq *) data;
868 	int error = 0;
869 
870 	ASSERT_SERIALIZED(ifp->if_serializer);
871 
872 	switch (command) {
873 	case SIOCSIFADDR:
874 		ifp->if_flags |= IFF_UP;
875 
876 		switch (ifa->ifa_addr->sa_family) {
877 #ifdef INET
878 		case AF_INET:
879 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
880 			arp_ifinit(ifp, ifa);
881 			break;
882 #endif
883 #ifdef IPX
884 		/*
885 		 * XXX - This code is probably wrong
886 		 */
887 		case AF_IPX:
888 			{
889 			struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
890 			struct arpcom *ac = IFP2AC(ifp);
891 
892 			if (ipx_nullhost(*ina))
893 				ina->x_host = *(union ipx_host *) ac->ac_enaddr;
894 			else
895 				bcopy(ina->x_host.c_host, ac->ac_enaddr,
896 				      sizeof ac->ac_enaddr);
897 
898 			ifp->if_init(ifp->if_softc);	/* Set new address. */
899 			break;
900 			}
901 #endif
902 #ifdef NS
903 		/*
904 		 * XXX - This code is probably wrong
905 		 */
906 		case AF_NS:
907 		{
908 			struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
909 			struct arpcom *ac = IFP2AC(ifp);
910 
911 			if (ns_nullhost(*ina))
912 				ina->x_host = *(union ns_host *)(ac->ac_enaddr);
913 			else
914 				bcopy(ina->x_host.c_host, ac->ac_enaddr,
915 				      sizeof ac->ac_enaddr);
916 
917 			/*
918 			 * Set new address
919 			 */
920 			ifp->if_init(ifp->if_softc);
921 			break;
922 		}
923 #endif
924 		default:
925 			ifp->if_init(ifp->if_softc);
926 			break;
927 		}
928 		break;
929 
930 	case SIOCGIFADDR:
931 		bcopy(IFP2AC(ifp)->ac_enaddr,
932 		      ((struct sockaddr *)ifr->ifr_data)->sa_data,
933 		      ETHER_ADDR_LEN);
934 		break;
935 
936 	case SIOCSIFMTU:
937 		/*
938 		 * Set the interface MTU.
939 		 */
940 		if (ifr->ifr_mtu > ETHERMTU) {
941 			error = EINVAL;
942 		} else {
943 			ifp->if_mtu = ifr->ifr_mtu;
944 		}
945 		break;
946 	default:
947 		error = EINVAL;
948 		break;
949 	}
950 	return (error);
951 }
952 
953 int
954 ether_resolvemulti(
955 	struct ifnet *ifp,
956 	struct sockaddr **llsa,
957 	struct sockaddr *sa)
958 {
959 	struct sockaddr_dl *sdl;
960 	struct sockaddr_in *sin;
961 #ifdef INET6
962 	struct sockaddr_in6 *sin6;
963 #endif
964 	u_char *e_addr;
965 
966 	switch(sa->sa_family) {
967 	case AF_LINK:
968 		/*
969 		 * No mapping needed. Just check that it's a valid MC address.
970 		 */
971 		sdl = (struct sockaddr_dl *)sa;
972 		e_addr = LLADDR(sdl);
973 		if ((e_addr[0] & 1) != 1)
974 			return EADDRNOTAVAIL;
975 		*llsa = 0;
976 		return 0;
977 
978 #ifdef INET
979 	case AF_INET:
980 		sin = (struct sockaddr_in *)sa;
981 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
982 			return EADDRNOTAVAIL;
983 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
984 		       M_WAITOK | M_ZERO);
985 		sdl->sdl_len = sizeof *sdl;
986 		sdl->sdl_family = AF_LINK;
987 		sdl->sdl_index = ifp->if_index;
988 		sdl->sdl_type = IFT_ETHER;
989 		sdl->sdl_alen = ETHER_ADDR_LEN;
990 		e_addr = LLADDR(sdl);
991 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
992 		*llsa = (struct sockaddr *)sdl;
993 		return 0;
994 #endif
995 #ifdef INET6
996 	case AF_INET6:
997 		sin6 = (struct sockaddr_in6 *)sa;
998 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
999 			/*
1000 			 * An IP6 address of 0 means listen to all
1001 			 * of the Ethernet multicast address used for IP6.
1002 			 * (This is used for multicast routers.)
1003 			 */
1004 			ifp->if_flags |= IFF_ALLMULTI;
1005 			*llsa = 0;
1006 			return 0;
1007 		}
1008 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1009 			return EADDRNOTAVAIL;
1010 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1011 		       M_WAITOK | M_ZERO);
1012 		sdl->sdl_len = sizeof *sdl;
1013 		sdl->sdl_family = AF_LINK;
1014 		sdl->sdl_index = ifp->if_index;
1015 		sdl->sdl_type = IFT_ETHER;
1016 		sdl->sdl_alen = ETHER_ADDR_LEN;
1017 		e_addr = LLADDR(sdl);
1018 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1019 		*llsa = (struct sockaddr *)sdl;
1020 		return 0;
1021 #endif
1022 
1023 	default:
1024 		/*
1025 		 * Well, the text isn't quite right, but it's the name
1026 		 * that counts...
1027 		 */
1028 		return EAFNOSUPPORT;
1029 	}
1030 }
1031 
1032 #if 0
1033 /*
1034  * This is for reference.  We have a table-driven version
1035  * of the little-endian crc32 generator, which is faster
1036  * than the double-loop.
1037  */
1038 uint32_t
1039 ether_crc32_le(const uint8_t *buf, size_t len)
1040 {
1041 	uint32_t c, crc, carry;
1042 	size_t i, j;
1043 
1044 	crc = 0xffffffffU;	/* initial value */
1045 
1046 	for (i = 0; i < len; i++) {
1047 		c = buf[i];
1048 		for (j = 0; j < 8; j++) {
1049 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1050 			crc >>= 1;
1051 			c >>= 1;
1052 			if (carry)
1053 				crc = (crc ^ ETHER_CRC_POLY_LE);
1054 		}
1055 	}
1056 
1057 	return (crc);
1058 }
1059 #else
1060 uint32_t
1061 ether_crc32_le(const uint8_t *buf, size_t len)
1062 {
1063 	static const uint32_t crctab[] = {
1064 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1065 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1066 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1067 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1068 	};
1069 	uint32_t crc;
1070 	size_t i;
1071 
1072 	crc = 0xffffffffU;	/* initial value */
1073 
1074 	for (i = 0; i < len; i++) {
1075 		crc ^= buf[i];
1076 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1077 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1078 	}
1079 
1080 	return (crc);
1081 }
1082 #endif
1083 
1084 uint32_t
1085 ether_crc32_be(const uint8_t *buf, size_t len)
1086 {
1087 	uint32_t c, crc, carry;
1088 	size_t i, j;
1089 
1090 	crc = 0xffffffffU;	/* initial value */
1091 
1092 	for (i = 0; i < len; i++) {
1093 		c = buf[i];
1094 		for (j = 0; j < 8; j++) {
1095 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1096 			crc <<= 1;
1097 			c >>= 1;
1098 			if (carry)
1099 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1100 		}
1101 	}
1102 
1103 	return (crc);
1104 }
1105 
1106 /*
1107  * find the size of ethernet header, and call classifier
1108  */
1109 void
1110 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
1111 		   struct altq_pktattr *pktattr)
1112 {
1113 	struct ether_header *eh;
1114 	uint16_t ether_type;
1115 	int hlen, af, hdrsize;
1116 	caddr_t hdr;
1117 
1118 	hlen = sizeof(struct ether_header);
1119 	eh = mtod(m, struct ether_header *);
1120 
1121 	ether_type = ntohs(eh->ether_type);
1122 	if (ether_type < ETHERMTU) {
1123 		/* ick! LLC/SNAP */
1124 		struct llc *llc = (struct llc *)(eh + 1);
1125 		hlen += 8;
1126 
1127 		if (m->m_len < hlen ||
1128 		    llc->llc_dsap != LLC_SNAP_LSAP ||
1129 		    llc->llc_ssap != LLC_SNAP_LSAP ||
1130 		    llc->llc_control != LLC_UI)
1131 			goto bad;  /* not snap! */
1132 
1133 		ether_type = ntohs(llc->llc_un.type_snap.ether_type);
1134 	}
1135 
1136 	if (ether_type == ETHERTYPE_IP) {
1137 		af = AF_INET;
1138 		hdrsize = 20;  /* sizeof(struct ip) */
1139 #ifdef INET6
1140 	} else if (ether_type == ETHERTYPE_IPV6) {
1141 		af = AF_INET6;
1142 		hdrsize = 40;  /* sizeof(struct ip6_hdr) */
1143 #endif
1144 	} else
1145 		goto bad;
1146 
1147 	while (m->m_len <= hlen) {
1148 		hlen -= m->m_len;
1149 		m = m->m_next;
1150 	}
1151 	hdr = m->m_data + hlen;
1152 	if (m->m_len < hlen + hdrsize) {
1153 		/*
1154 		 * ip header is not in a single mbuf.  this should not
1155 		 * happen in the current code.
1156 		 * (todo: use m_pulldown in the future)
1157 		 */
1158 		goto bad;
1159 	}
1160 	m->m_data += hlen;
1161 	m->m_len -= hlen;
1162 	ifq_classify(ifq, m, af, pktattr);
1163 	m->m_data -= hlen;
1164 	m->m_len += hlen;
1165 
1166 	return;
1167 
1168 bad:
1169 	pktattr->pattr_class = NULL;
1170 	pktattr->pattr_hdr = NULL;
1171 	pktattr->pattr_af = AF_UNSPEC;
1172 }
1173