xref: /dragonfly/sys/net/if_ethersubr.c (revision 23265324)
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.39 2006/12/22 23:44:54 swildner 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 		kprintf("%s: can't handle af%d\n", ifp->if_xname,
294 			dst->sa_family);
295 		gotoerr(EAFNOSUPPORT);
296 	}
297 
298 	if (dst->sa_family == pseudo_AF_HDRCMPLT)	/* unlikely */
299 		memcpy(eh->ether_shost,
300 		       ((struct ether_header *)dst->sa_data)->ether_shost,
301 		       ETHER_ADDR_LEN);
302 	else
303 		memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
304 
305 	/*
306 	 * Bridges require special output handling.
307 	 */
308 	if (ifp->if_bridge) {
309 		KASSERT(bridge_output_p != NULL,("ether_input: if_bridge not loaded!"));
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,("ether_input: if_bridge not loaded!"));
593 
594 		if(m->m_flags & M_PROTO1) {
595 			m->m_flags &= ~M_PROTO1;
596 		} else {
597 			/* clear M_PROMISC, in case the packets comes from a vlan */
598 			/* m->m_flags &= ~M_PROMISC; */
599 			lwkt_serialize_exit(ifp->if_serializer);
600 			m = (*bridge_input_p)(ifp, m);
601 			lwkt_serialize_enter(ifp->if_serializer);
602 			if (m == NULL)
603 				return;
604 
605 			/*
606 			 * Bridge has determined that the packet is for us.
607 			 * Update our interface pointer -- we may have had
608 			 * to "bridge" the packet locally.
609 			 */
610 			ifp = m->m_pkthdr.rcvif;
611 		}
612 	}
613 
614 	/* XXX old crufty stuff, needs to be removed */
615 	m_adj(m, sizeof(struct ether_header));
616 	/* XXX */
617 	/* m->m_pkthdr.len = m->m_len; */
618 
619 	/* Handle ng_ether(4) processing, if any */
620 	if (ng_ether_input_p != NULL) {
621 		lwkt_serialize_exit(ifp->if_serializer);
622 		(*ng_ether_input_p)(ifp, &m, eh);
623 		lwkt_serialize_enter(ifp->if_serializer);
624 		if (m == NULL)
625 			return;
626 	}
627 
628 	/* Continue with upper layer processing */
629 	ether_demux(ifp, eh, m);
630 }
631 
632 /*
633  * Upper layer processing for a received Ethernet packet.
634  */
635 void
636 ether_demux(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
637 {
638 	int isr;
639 	u_short ether_type;
640 	struct ip_fw *rule = NULL;
641 #ifdef NETATALK
642 	struct llc *l;
643 #endif
644 
645 	/* Extract info from dummynet tag, ignore others */
646 	while (m->m_type == MT_TAG) {
647 		if (m->m_flags == PACKET_TAG_DUMMYNET) {
648 			rule = ((struct dn_pkt *)m)->rule;
649 			ifp = m->m_next->m_pkthdr.rcvif;
650 			break;
651 		}
652 		m = m->m_next;
653 	}
654 	if (rule)	/* packet is passing the second time */
655 		goto post_stats;
656 
657 	/*
658 	 * Discard packet if upper layers shouldn't see it because
659 	 * it was unicast to a different Ethernet address.  If the
660 	 * driver is working properly, then this situation can only
661 	 * happen when the interface is in promiscuous mode.
662 	 */
663 	if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
664 	    (eh->ether_dhost[0] & 1) == 0 &&
665 	    bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
666 		m_freem(m);
667 		return;
668 	}
669 	/* Discard packet if interface is not up */
670 	if (!(ifp->if_flags & IFF_UP)) {
671 		m_freem(m);
672 		return;
673 	}
674 	if (eh->ether_dhost[0] & 1) {
675 		if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
676 			 ifp->if_addrlen) == 0)
677 			m->m_flags |= M_BCAST;
678 		else
679 			m->m_flags |= M_MCAST;
680 		ifp->if_imcasts++;
681 	}
682 
683 post_stats:
684 	if (IPFW_LOADED && ether_ipfw != 0) {
685 		if (!ether_ipfw_chk(&m, NULL, &rule, eh, FALSE)) {
686 			m_freem(m);
687 			return;
688 		}
689 		eh = mtod(m, struct ether_header *);
690 	}
691 
692 	ether_type = ntohs(eh->ether_type);
693 
694 	switch (ether_type) {
695 #ifdef INET
696 	case ETHERTYPE_IP:
697 		if (ipflow_fastforward(m, ifp->if_serializer))
698 			return;
699 		isr = NETISR_IP;
700 		break;
701 
702 	case ETHERTYPE_ARP:
703 		if (ifp->if_flags & IFF_NOARP) {
704 			/* Discard packet if ARP is disabled on interface */
705 			m_freem(m);
706 			return;
707 		}
708 		isr = NETISR_ARP;
709 		break;
710 #endif
711 
712 #ifdef INET6
713 	case ETHERTYPE_IPV6:
714 		isr = NETISR_IPV6;
715 		break;
716 #endif
717 
718 #ifdef IPX
719 	case ETHERTYPE_IPX:
720 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
721 			return;
722 		isr = NETISR_IPX;
723 		break;
724 #endif
725 
726 #ifdef NS
727 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
728 		isr = NETISR_NS;
729 		break;
730 
731 #endif
732 
733 #ifdef NETATALK
734 	case ETHERTYPE_AT:
735 		isr = NETISR_ATALK1;
736 		break;
737 	case ETHERTYPE_AARP:
738 		isr = NETISR_AARP;
739 		break;
740 #endif
741 
742 	case ETHERTYPE_VLAN:
743 		if (vlan_input_p != NULL)
744 			(*vlan_input_p)(eh, m);
745 		else {
746 			m->m_pkthdr.rcvif->if_noproto++;
747 			m_freem(m);
748 		}
749 		return;
750 
751 	default:
752 #ifdef IPX
753 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
754 			return;
755 #endif
756 #ifdef NS
757 		checksum = mtod(m, ushort *);
758 		/* Novell 802.3 */
759 		if ((ether_type <= ETHERMTU) &&
760 		    ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
761 			if (*checksum == 0xE0E0) {
762 				m->m_pkthdr.len -= 3;
763 				m->m_len -= 3;
764 				m->m_data += 3;
765 			}
766 			isr = NETISR_NS;
767 			break;
768 		}
769 #endif
770 #ifdef NETATALK
771 		if (ether_type > ETHERMTU)
772 			goto dropanyway;
773 		l = mtod(m, struct llc *);
774 		if (l->llc_dsap == LLC_SNAP_LSAP &&
775 		    l->llc_ssap == LLC_SNAP_LSAP &&
776 		    l->llc_control == LLC_UI) {
777 			if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
778 				 sizeof at_org_code) == 0 &&
779 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
780 				m_adj(m, sizeof(struct llc));
781 				isr = NETISR_ATALK2;
782 				break;
783 			}
784 			if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
785 				 sizeof aarp_org_code) == 0 &&
786 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
787 				m_adj(m, sizeof(struct llc));
788 				isr = NETISR_AARP;
789 				break;
790 			}
791 		}
792 dropanyway:
793 #endif
794 		if (ng_ether_input_orphan_p != NULL)
795 			(*ng_ether_input_orphan_p)(ifp, m, eh);
796 		else
797 			m_freem(m);
798 		return;
799 	}
800 	netisr_dispatch(isr, m);
801 }
802 
803 /*
804  * Perform common duties while attaching to interface list
805  */
806 
807 void
808 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
809 {
810 	ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
811 			   serializer);
812 }
813 
814 void
815 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
816 		   lwkt_serialize_t serializer)
817 {
818 	struct sockaddr_dl *sdl;
819 
820 	ifp->if_type = IFT_ETHER;
821 	ifp->if_addrlen = ETHER_ADDR_LEN;
822 	ifp->if_hdrlen = ETHER_HDR_LEN;
823 	if_attach(ifp, serializer);
824 	ifp->if_mtu = ETHERMTU;
825 	if (ifp->if_baudrate == 0)
826 		ifp->if_baudrate = 10000000;
827 	ifp->if_output = ether_output;
828 	ifp->if_input = ether_input_internal;
829 	ifp->if_resolvemulti = ether_resolvemulti;
830 	ifp->if_broadcastaddr = etherbroadcastaddr;
831 	sdl = IF_LLSOCKADDR(ifp);
832 	sdl->sdl_type = IFT_ETHER;
833 	sdl->sdl_alen = ifp->if_addrlen;
834 	bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
835 	/*
836 	 * XXX Keep the current drivers happy.
837 	 * XXX Remove once all drivers have been cleaned up
838 	 */
839 	if (lla != IFP2AC(ifp)->ac_enaddr)
840 		bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
841 	bpfattach(ifp, dlt, hdrlen);
842 	if (ng_ether_attach_p != NULL)
843 		(*ng_ether_attach_p)(ifp);
844 
845 	if_printf(ifp, "MAC address: %6D\n", lla, ":");
846 }
847 
848 /*
849  * Perform common duties while detaching an Ethernet interface
850  */
851 void
852 ether_ifdetach(struct ifnet *ifp)
853 {
854 	if_down(ifp);
855 
856 	if (ng_ether_detach_p != NULL)
857 		(*ng_ether_detach_p)(ifp);
858 	bpfdetach(ifp);
859 	if_detach(ifp);
860 }
861 
862 int
863 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
864 {
865 	struct ifaddr *ifa = (struct ifaddr *) data;
866 	struct ifreq *ifr = (struct ifreq *) data;
867 	int error = 0;
868 
869 	ASSERT_SERIALIZED(ifp->if_serializer);
870 
871 	switch (command) {
872 	case SIOCSIFADDR:
873 		ifp->if_flags |= IFF_UP;
874 
875 		switch (ifa->ifa_addr->sa_family) {
876 #ifdef INET
877 		case AF_INET:
878 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
879 			arp_ifinit(ifp, ifa);
880 			break;
881 #endif
882 #ifdef IPX
883 		/*
884 		 * XXX - This code is probably wrong
885 		 */
886 		case AF_IPX:
887 			{
888 			struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
889 			struct arpcom *ac = IFP2AC(ifp);
890 
891 			if (ipx_nullhost(*ina))
892 				ina->x_host = *(union ipx_host *) ac->ac_enaddr;
893 			else
894 				bcopy(ina->x_host.c_host, ac->ac_enaddr,
895 				      sizeof ac->ac_enaddr);
896 
897 			ifp->if_init(ifp->if_softc);	/* Set new address. */
898 			break;
899 			}
900 #endif
901 #ifdef NS
902 		/*
903 		 * XXX - This code is probably wrong
904 		 */
905 		case AF_NS:
906 		{
907 			struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
908 			struct arpcom *ac = IFP2AC(ifp);
909 
910 			if (ns_nullhost(*ina))
911 				ina->x_host = *(union ns_host *)(ac->ac_enaddr);
912 			else
913 				bcopy(ina->x_host.c_host, ac->ac_enaddr,
914 				      sizeof ac->ac_enaddr);
915 
916 			/*
917 			 * Set new address
918 			 */
919 			ifp->if_init(ifp->if_softc);
920 			break;
921 		}
922 #endif
923 		default:
924 			ifp->if_init(ifp->if_softc);
925 			break;
926 		}
927 		break;
928 
929 	case SIOCGIFADDR:
930 		bcopy(IFP2AC(ifp)->ac_enaddr,
931 		      ((struct sockaddr *)ifr->ifr_data)->sa_data,
932 		      ETHER_ADDR_LEN);
933 		break;
934 
935 	case SIOCSIFMTU:
936 		/*
937 		 * Set the interface MTU.
938 		 */
939 		if (ifr->ifr_mtu > ETHERMTU) {
940 			error = EINVAL;
941 		} else {
942 			ifp->if_mtu = ifr->ifr_mtu;
943 		}
944 		break;
945 	default:
946 		error = EINVAL;
947 		break;
948 	}
949 	return (error);
950 }
951 
952 int
953 ether_resolvemulti(
954 	struct ifnet *ifp,
955 	struct sockaddr **llsa,
956 	struct sockaddr *sa)
957 {
958 	struct sockaddr_dl *sdl;
959 	struct sockaddr_in *sin;
960 #ifdef INET6
961 	struct sockaddr_in6 *sin6;
962 #endif
963 	u_char *e_addr;
964 
965 	switch(sa->sa_family) {
966 	case AF_LINK:
967 		/*
968 		 * No mapping needed. Just check that it's a valid MC address.
969 		 */
970 		sdl = (struct sockaddr_dl *)sa;
971 		e_addr = LLADDR(sdl);
972 		if ((e_addr[0] & 1) != 1)
973 			return EADDRNOTAVAIL;
974 		*llsa = 0;
975 		return 0;
976 
977 #ifdef INET
978 	case AF_INET:
979 		sin = (struct sockaddr_in *)sa;
980 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
981 			return EADDRNOTAVAIL;
982 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
983 		       M_WAITOK | M_ZERO);
984 		sdl->sdl_len = sizeof *sdl;
985 		sdl->sdl_family = AF_LINK;
986 		sdl->sdl_index = ifp->if_index;
987 		sdl->sdl_type = IFT_ETHER;
988 		sdl->sdl_alen = ETHER_ADDR_LEN;
989 		e_addr = LLADDR(sdl);
990 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
991 		*llsa = (struct sockaddr *)sdl;
992 		return 0;
993 #endif
994 #ifdef INET6
995 	case AF_INET6:
996 		sin6 = (struct sockaddr_in6 *)sa;
997 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
998 			/*
999 			 * An IP6 address of 0 means listen to all
1000 			 * of the Ethernet multicast address used for IP6.
1001 			 * (This is used for multicast routers.)
1002 			 */
1003 			ifp->if_flags |= IFF_ALLMULTI;
1004 			*llsa = 0;
1005 			return 0;
1006 		}
1007 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1008 			return EADDRNOTAVAIL;
1009 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1010 		       M_WAITOK | M_ZERO);
1011 		sdl->sdl_len = sizeof *sdl;
1012 		sdl->sdl_family = AF_LINK;
1013 		sdl->sdl_index = ifp->if_index;
1014 		sdl->sdl_type = IFT_ETHER;
1015 		sdl->sdl_alen = ETHER_ADDR_LEN;
1016 		e_addr = LLADDR(sdl);
1017 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1018 		*llsa = (struct sockaddr *)sdl;
1019 		return 0;
1020 #endif
1021 
1022 	default:
1023 		/*
1024 		 * Well, the text isn't quite right, but it's the name
1025 		 * that counts...
1026 		 */
1027 		return EAFNOSUPPORT;
1028 	}
1029 }
1030 
1031 #if 0
1032 /*
1033  * This is for reference.  We have a table-driven version
1034  * of the little-endian crc32 generator, which is faster
1035  * than the double-loop.
1036  */
1037 uint32_t
1038 ether_crc32_le(const uint8_t *buf, size_t len)
1039 {
1040 	uint32_t c, crc, carry;
1041 	size_t i, j;
1042 
1043 	crc = 0xffffffffU;	/* initial value */
1044 
1045 	for (i = 0; i < len; i++) {
1046 		c = buf[i];
1047 		for (j = 0; j < 8; j++) {
1048 			carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1049 			crc >>= 1;
1050 			c >>= 1;
1051 			if (carry)
1052 				crc = (crc ^ ETHER_CRC_POLY_LE);
1053 		}
1054 	}
1055 
1056 	return (crc);
1057 }
1058 #else
1059 uint32_t
1060 ether_crc32_le(const uint8_t *buf, size_t len)
1061 {
1062 	static const uint32_t crctab[] = {
1063 		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1064 		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1065 		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1066 		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1067 	};
1068 	uint32_t crc;
1069 	size_t i;
1070 
1071 	crc = 0xffffffffU;	/* initial value */
1072 
1073 	for (i = 0; i < len; i++) {
1074 		crc ^= buf[i];
1075 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1076 		crc = (crc >> 4) ^ crctab[crc & 0xf];
1077 	}
1078 
1079 	return (crc);
1080 }
1081 #endif
1082 
1083 uint32_t
1084 ether_crc32_be(const uint8_t *buf, size_t len)
1085 {
1086 	uint32_t c, crc, carry;
1087 	size_t i, j;
1088 
1089 	crc = 0xffffffffU;	/* initial value */
1090 
1091 	for (i = 0; i < len; i++) {
1092 		c = buf[i];
1093 		for (j = 0; j < 8; j++) {
1094 			carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1095 			crc <<= 1;
1096 			c >>= 1;
1097 			if (carry)
1098 				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1099 		}
1100 	}
1101 
1102 	return (crc);
1103 }
1104 
1105 /*
1106  * find the size of ethernet header, and call classifier
1107  */
1108 void
1109 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
1110 		   struct altq_pktattr *pktattr)
1111 {
1112 	struct ether_header *eh;
1113 	uint16_t ether_type;
1114 	int hlen, af, hdrsize;
1115 	caddr_t hdr;
1116 
1117 	hlen = sizeof(struct ether_header);
1118 	eh = mtod(m, struct ether_header *);
1119 
1120 	ether_type = ntohs(eh->ether_type);
1121 	if (ether_type < ETHERMTU) {
1122 		/* ick! LLC/SNAP */
1123 		struct llc *llc = (struct llc *)(eh + 1);
1124 		hlen += 8;
1125 
1126 		if (m->m_len < hlen ||
1127 		    llc->llc_dsap != LLC_SNAP_LSAP ||
1128 		    llc->llc_ssap != LLC_SNAP_LSAP ||
1129 		    llc->llc_control != LLC_UI)
1130 			goto bad;  /* not snap! */
1131 
1132 		ether_type = ntohs(llc->llc_un.type_snap.ether_type);
1133 	}
1134 
1135 	if (ether_type == ETHERTYPE_IP) {
1136 		af = AF_INET;
1137 		hdrsize = 20;  /* sizeof(struct ip) */
1138 #ifdef INET6
1139 	} else if (ether_type == ETHERTYPE_IPV6) {
1140 		af = AF_INET6;
1141 		hdrsize = 40;  /* sizeof(struct ip6_hdr) */
1142 #endif
1143 	} else
1144 		goto bad;
1145 
1146 	while (m->m_len <= hlen) {
1147 		hlen -= m->m_len;
1148 		m = m->m_next;
1149 	}
1150 	hdr = m->m_data + hlen;
1151 	if (m->m_len < hlen + hdrsize) {
1152 		/*
1153 		 * ip header is not in a single mbuf.  this should not
1154 		 * happen in the current code.
1155 		 * (todo: use m_pulldown in the future)
1156 		 */
1157 		goto bad;
1158 	}
1159 	m->m_data += hlen;
1160 	m->m_len -= hlen;
1161 	ifq_classify(ifq, m, af, pktattr);
1162 	m->m_data -= hlen;
1163 	m->m_len += hlen;
1164 
1165 	return;
1166 
1167 bad:
1168 	pktattr->pattr_class = NULL;
1169 	pktattr->pattr_hdr = NULL;
1170 	pktattr->pattr_af = AF_UNSPEC;
1171 }
1172