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