xref: /freebsd/sys/net/if_ethersubr.c (revision e0c27215058b5786c78fcfb3963eebe61a989511)
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$
35  */
36 
37 #include "opt_atalk.h"
38 #include "opt_inet.h"
39 #include "opt_inet6.h"
40 #include "opt_ipx.h"
41 #include "opt_bdg.h"
42 #include "opt_mac.h"
43 #include "opt_netgraph.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/mac.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/random.h>
52 #include <sys/socket.h>
53 #include <sys/sockio.h>
54 #include <sys/sysctl.h>
55 
56 #include <net/if.h>
57 #include <net/netisr.h>
58 #include <net/route.h>
59 #include <net/if_llc.h>
60 #include <net/if_dl.h>
61 #include <net/if_types.h>
62 #include <net/bpf.h>
63 #include <net/ethernet.h>
64 #include <net/bridge.h>
65 #include <net/if_vlan_var.h>
66 
67 #if defined(INET) || defined(INET6)
68 #include <netinet/in.h>
69 #include <netinet/in_var.h>
70 #include <netinet/if_ether.h>
71 #include <netinet/ip_fw.h>
72 #include <netinet/ip_dummynet.h>
73 #endif
74 #ifdef INET6
75 #include <netinet6/nd6.h>
76 #endif
77 
78 #ifdef IPX
79 #include <netipx/ipx.h>
80 #include <netipx/ipx_if.h>
81 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
82 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
83 		struct sockaddr *dst, short *tp, int *hlen);
84 #endif
85 
86 #ifdef NETATALK
87 #include <netatalk/at.h>
88 #include <netatalk/at_var.h>
89 #include <netatalk/at_extern.h>
90 
91 #define llc_snap_org_code llc_un.type_snap.org_code
92 #define llc_snap_ether_type llc_un.type_snap.ether_type
93 
94 extern u_char	at_org_code[3];
95 extern u_char	aarp_org_code[3];
96 #endif /* NETATALK */
97 
98 /* netgraph node hooks for ng_ether(4) */
99 void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
100 void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
101 int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
102 void	(*ng_ether_attach_p)(struct ifnet *ifp);
103 void	(*ng_ether_detach_p)(struct ifnet *ifp);
104 
105 void	(*vlan_input_p)(struct ifnet *, struct mbuf *);
106 
107 /* bridge support */
108 int do_bridge;
109 bridge_in_t *bridge_in_ptr;
110 bdg_forward_t *bdg_forward_ptr;
111 bdgtakeifaces_t *bdgtakeifaces_ptr;
112 struct bdg_softc *ifp2sc;
113 
114 static u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
115 			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
116 
117 static	int ether_resolvemulti(struct ifnet *, struct sockaddr **,
118 		struct sockaddr *);
119 
120 #define senderr(e) do { error = (e); goto bad;} while (0)
121 #define IFP2AC(IFP) ((struct arpcom *)IFP)
122 
123 int
124 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
125 	struct ip_fw **rule, int shared);
126 static int ether_ipfw;
127 
128 /*
129  * Ethernet output routine.
130  * Encapsulate a packet of type family for the local net.
131  * Use trailer local net encapsulation if enough data in first
132  * packet leaves a multiple of 512 bytes of data in remainder.
133  * Assumes that ifp is actually pointer to arpcom structure.
134  */
135 int
136 ether_output(ifp, m, dst, rt0)
137 	struct ifnet *ifp;
138 	struct mbuf *m;
139 	struct sockaddr *dst;
140 	struct rtentry *rt0;
141 {
142 	short type;
143 	int error = 0, hdrcmplt = 0;
144  	u_char esrc[6], edst[6];
145 	struct rtentry *rt;
146 	struct ether_header *eh;
147 	int loop_copy = 0;
148 	int hlen;	/* link layer header lenght */
149 	struct arpcom *ac = IFP2AC(ifp);
150 
151 #ifdef MAC
152 	error = mac_check_ifnet_transmit(ifp, m);
153 	if (error)
154 		senderr(error);
155 #endif
156 
157 	if (ifp->if_flags & IFF_MONITOR)
158 		senderr(ENETDOWN);
159 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
160 		senderr(ENETDOWN);
161 
162 	error = rt_check(&rt, &rt0, dst);
163 	if (error)
164 		goto bad;
165 
166 	hlen = ETHER_HDR_LEN;
167 	switch (dst->sa_family) {
168 #ifdef INET
169 	case AF_INET:
170 		if (!arpresolve(ifp, rt, m, dst, edst, rt0))
171 			return (0);	/* if not yet resolved */
172 		type = htons(ETHERTYPE_IP);
173 		break;
174 #endif
175 #ifdef INET6
176 	case AF_INET6:
177 		if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
178 			/* Something bad happened */
179 			return(0);
180 		}
181 		type = htons(ETHERTYPE_IPV6);
182 		break;
183 #endif
184 #ifdef IPX
185 	case AF_IPX:
186 		if (ef_outputp) {
187 		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
188 		    if (error)
189 			goto bad;
190 		} else
191 		    type = htons(ETHERTYPE_IPX);
192  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
193 		    (caddr_t)edst, sizeof (edst));
194 		break;
195 #endif
196 #ifdef NETATALK
197 	case AF_APPLETALK:
198 	  {
199 	    struct at_ifaddr *aa;
200 
201 	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
202 		    goto bad;
203 	    }
204 	    if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
205 		    return (0);
206 	    /*
207 	     * In the phase 2 case, need to prepend an mbuf for the llc header.
208 	     * Since we must preserve the value of m, which is passed to us by
209 	     * value, we m_copy() the first mbuf, and use it for our llc header.
210 	     */
211 	    if ( aa->aa_flags & AFA_PHASE2 ) {
212 		struct llc llc;
213 
214 		M_PREPEND(m, LLC_SNAPFRAMELEN, M_TRYWAIT);
215 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
216 		llc.llc_control = LLC_UI;
217 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
218 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
219 		bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
220 		type = htons(m->m_pkthdr.len);
221 		hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
222 	    } else {
223 		type = htons(ETHERTYPE_AT);
224 	    }
225 	    break;
226 	  }
227 #endif /* NETATALK */
228 
229 	case pseudo_AF_HDRCMPLT:
230 		hdrcmplt = 1;
231 		eh = (struct ether_header *)dst->sa_data;
232 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
233 		/* FALLTHROUGH */
234 
235 	case AF_UNSPEC:
236 		loop_copy = -1; /* if this is for us, don't do it */
237 		eh = (struct ether_header *)dst->sa_data;
238  		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
239 		type = eh->ether_type;
240 		break;
241 
242 	default:
243 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
244 		senderr(EAFNOSUPPORT);
245 	}
246 
247 	/*
248 	 * Add local net header.  If no space in first mbuf,
249 	 * allocate another.
250 	 */
251 	M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
252 	if (m == 0)
253 		senderr(ENOBUFS);
254 	eh = mtod(m, struct ether_header *);
255 	(void)memcpy(&eh->ether_type, &type,
256 		sizeof(eh->ether_type));
257  	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
258 	if (hdrcmplt)
259 		(void)memcpy(eh->ether_shost, esrc,
260 			sizeof(eh->ether_shost));
261 	else
262 		(void)memcpy(eh->ether_shost, ac->ac_enaddr,
263 			sizeof(eh->ether_shost));
264 
265 	/*
266 	 * If a simplex interface, and the packet is being sent to our
267 	 * Ethernet address or a broadcast address, loopback a copy.
268 	 * XXX To make a simplex device behave exactly like a duplex
269 	 * device, we should copy in the case of sending to our own
270 	 * ethernet address (thus letting the original actually appear
271 	 * on the wire). However, we don't do that here for security
272 	 * reasons and compatibility with the original behavior.
273 	 */
274 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
275 		int csum_flags = 0;
276 
277 		if (m->m_pkthdr.csum_flags & CSUM_IP)
278 			csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
279 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
280 			csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
281 
282 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
283 			struct mbuf *n;
284 
285 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
286 				n->m_pkthdr.csum_flags |= csum_flags;
287 				if (csum_flags & CSUM_DATA_VALID)
288 					n->m_pkthdr.csum_data = 0xffff;
289 				(void)if_simloop(ifp, n, dst->sa_family, hlen);
290 			} else
291 				ifp->if_iqdrops++;
292 		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
293 				ETHER_ADDR_LEN) == 0) {
294 			m->m_pkthdr.csum_flags |= csum_flags;
295 			if (csum_flags & CSUM_DATA_VALID)
296 				m->m_pkthdr.csum_data = 0xffff;
297 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
298 			return (0);	/* XXX */
299 		}
300 	}
301 
302 	/* Handle ng_ether(4) processing, if any */
303 	if (ng_ether_output_p != NULL) {
304 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
305 bad:			if (m != NULL)
306 				m_freem(m);
307 			return (error);
308 		}
309 		if (m == NULL)
310 			return (0);
311 	}
312 
313 	/* Continue with link-layer output */
314 	return ether_output_frame(ifp, m);
315 }
316 
317 /*
318  * Ethernet link layer output routine to send a raw frame to the device.
319  *
320  * This assumes that the 14 byte Ethernet header is present and contiguous
321  * in the first mbuf (if BRIDGE'ing).
322  */
323 int
324 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
325 {
326 	struct ip_fw *rule = NULL;
327 
328 	/* Extract info from dummynet tag, ignore others */
329 	for (; m->m_type == MT_TAG; m = m->m_next)
330 		if (m->m_flags == PACKET_TAG_DUMMYNET)
331 			rule = ((struct dn_pkt *)m)->rule;
332 
333 	if (rule == NULL && BDG_ACTIVE(ifp)) {
334 		/*
335 		 * Beware, the bridge code notices the null rcvif and
336 		 * uses that identify that it's being called from
337 		 * ether_output as opposd to ether_input.  Yech.
338 		 */
339 		m->m_pkthdr.rcvif = NULL;
340 		m = bdg_forward_ptr(m, ifp);
341 		if (m != NULL)
342 			m_freem(m);
343 		return (0);
344 	}
345 	if (IPFW_LOADED && ether_ipfw != 0) {
346 		if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
347 			if (m) {
348 				m_freem(m);
349 				return ENOBUFS;	/* pkt dropped */
350 			} else
351 				return 0;	/* consumed e.g. in a pipe */
352 		}
353 	}
354 
355 	/*
356 	 * Queue message on interface, update output statistics if
357 	 * successful, and start output if interface not yet active.
358 	 */
359 	return (IF_HANDOFF(&ifp->if_snd, m, ifp) ? 0 : ENOBUFS);
360 }
361 
362 /*
363  * ipfw processing for ethernet packets (in and out).
364  * The second parameter is NULL from ether_demux, and ifp from
365  * ether_output_frame. This section of code could be used from
366  * bridge.c as well as long as we use some extra info
367  * to distinguish that case from ether_output_frame();
368  */
369 int
370 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
371 	struct ip_fw **rule, int shared)
372 {
373 	struct ether_header *eh;
374 	struct ether_header save_eh;
375 	struct mbuf *m;
376 	int i;
377 	struct ip_fw_args args;
378 
379 	if (*rule != NULL && fw_one_pass)
380 		return 1; /* dummynet packet, already partially processed */
381 
382 	/*
383 	 * I need some amt of data to be contiguous, and in case others need
384 	 * the packet (shared==1) also better be in the first mbuf.
385 	 */
386 	m = *m0;
387 	i = min( m->m_pkthdr.len, max_protohdr);
388 	if ( shared || m->m_len < i) {
389 		m = m_pullup(m, i);
390 		if (m == NULL) {
391 			*m0 = m;
392 			return 0;
393 		}
394 	}
395 	eh = mtod(m, struct ether_header *);
396 	save_eh = *eh;			/* save copy for restore below */
397 	m_adj(m, ETHER_HDR_LEN);	/* strip ethernet header */
398 
399 	args.m = m;		/* the packet we are looking at		*/
400 	args.oif = dst;		/* destination, if any			*/
401 	args.divert_rule = 0;	/* we do not support divert yet		*/
402 	args.rule = *rule;	/* matching rule to restart		*/
403 	args.next_hop = NULL;	/* we do not support forward yet	*/
404 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
405 	i = ip_fw_chk_ptr(&args);
406 	m = args.m;
407 	if (m != NULL) {
408 		/*
409 		 * Restore Ethernet header, as needed, in case the
410 		 * mbuf chain was replaced by ipfw.
411 		 */
412 		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
413 		if (m == NULL) {
414 			*m0 = m;
415 			return 0;
416 		}
417 		if (eh != mtod(m, struct ether_header *))
418 			bcopy(&save_eh, mtod(m, struct ether_header *),
419 				ETHER_HDR_LEN);
420 	}
421 	*m0 = m;
422 	*rule = args.rule;
423 
424 	if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) /* drop */
425 		return 0;
426 
427 	if (i == 0) /* a PASS rule.  */
428 		return 1;
429 
430 	if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
431 		/*
432 		 * Pass the pkt to dummynet, which consumes it.
433 		 * If shared, make a copy and keep the original.
434 		 */
435 		if (shared) {
436 			m = m_copypacket(m, M_DONTWAIT);
437 			if (m == NULL)
438 				return 0;
439 		} else {
440 			/*
441 			 * Pass the original to dummynet and
442 			 * nothing back to the caller
443 			 */
444 			*m0 = NULL ;
445 		}
446 		ip_dn_io_ptr(m, (i & 0xffff),
447 			dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
448 		return 0;
449 	}
450 	/*
451 	 * XXX at some point add support for divert/forward actions.
452 	 * If none of the above matches, we have to drop the pkt.
453 	 */
454 	return 0;
455 }
456 
457 /*
458  * Process a received Ethernet packet; the packet is in the
459  * mbuf chain m with the ethernet header at the front.
460  */
461 static void
462 ether_input(struct ifnet *ifp, struct mbuf *m)
463 {
464 	struct ether_header *eh;
465 	u_short etype;
466 
467 	/*
468 	 * Do consistency checks to verify assumptions
469 	 * made by code past this point.
470 	 */
471 	if ((m->m_flags & M_PKTHDR) == 0) {
472 		if_printf(ifp, "discard frame w/o packet header\n");
473 		ifp->if_ierrors++;
474 		m_freem(m);
475 		return;
476 	}
477 	if (m->m_len < ETHER_HDR_LEN) {
478 		/* XXX maybe should pullup? */
479 		if_printf(ifp, "discard frame w/o leading ethernet "
480 				"header (len %u pkt len %u)\n",
481 				m->m_len, m->m_pkthdr.len);
482 		ifp->if_ierrors++;
483 		m_freem(m);
484 		return;
485 	}
486 	eh = mtod(m, struct ether_header *);
487 	etype = ntohs(eh->ether_type);
488 	if (m->m_pkthdr.len >
489 	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
490 		if_printf(ifp, "discard oversize frame "
491 				"(ether type %x flags %x len %u > max %lu)\n",
492 				etype, m->m_flags, m->m_pkthdr.len,
493 				ETHER_MAX_FRAME(ifp, etype,
494 						m->m_flags & M_HASFCS));
495 		ifp->if_ierrors++;
496 		m_freem(m);
497 		return;
498 	}
499 	if (m->m_pkthdr.rcvif == NULL) {
500 		if_printf(ifp, "discard frame w/o interface pointer\n");
501 		ifp->if_ierrors++;
502 		m_freem(m);
503 		return;
504 	}
505 #ifdef DIAGNOSTIC
506 	if (m->m_pkthdr.rcvif != ifp) {
507 		if_printf(ifp, "Warning, frame marked as received on %s%u\n",
508 			m->m_pkthdr.rcvif->if_name,
509 			m->m_pkthdr.rcvif->if_unit);
510 	}
511 #endif
512 
513 #ifdef MAC
514 	/*
515 	 * Tag the mbuf with an appropriate MAC label before any other
516 	 * consumers can get to it.
517 	 */
518 	mac_create_mbuf_from_ifnet(ifp, m);
519 #endif
520 
521 	/*
522 	 * Give bpf a chance at the packet.
523 	 */
524 	BPF_MTAP(ifp, m);
525 
526 	if (ifp->if_flags & IFF_MONITOR) {
527 		/*
528 		 * Interface marked for monitoring; discard packet.
529 		 */
530 		m_freem(m);
531 		return;
532 	}
533 
534 	/* If the CRC is still on the packet, trim it off. */
535 	if (m->m_flags & M_HASFCS) {
536 		m_adj(m, -ETHER_CRC_LEN);
537 		m->m_flags &= ~M_HASFCS;
538 	}
539 
540 	ifp->if_ibytes += m->m_pkthdr.len;
541 
542 	/* Handle ng_ether(4) processing, if any */
543 	if (ng_ether_input_p != NULL) {
544 		(*ng_ether_input_p)(ifp, &m);
545 		if (m == NULL)
546 			return;
547 	}
548 
549 	/* Check for bridging mode */
550 	if (BDG_ACTIVE(ifp) ) {
551 		struct ifnet *bif;
552 
553 		/*
554 		 * Check with bridging code to see how the packet
555 		 * should be handled.  Possibilities are:
556 		 *
557 		 *    BDG_BCAST		broadcast
558 		 *    BDG_MCAST		multicast
559 		 *    BDG_LOCAL		for local address, don't forward
560 		 *    BDG_DROP		discard
561 		 *    ifp		forward only to specified interface(s)
562 		 *
563 		 * Non-local destinations are handled by passing the
564 		 * packet back to the bridge code.
565 		 */
566 		bif = bridge_in_ptr(ifp, eh);
567 		if (bif == BDG_DROP) {		/* discard packet */
568 			m_freem(m);
569 			return;
570 		}
571 		if (bif != BDG_LOCAL) {		/* non-local, forward */
572 			m = bdg_forward_ptr(m, bif);
573 			/*
574 			 * The bridge may consume the packet if it's not
575 			 * supposed to be passed up or if a problem occurred
576 			 * while doing its job.  This is reflected by it
577 			 * returning a NULL mbuf pointer.
578 			 */
579 			if (m == NULL) {
580 				if (bif == BDG_BCAST || bif == BDG_MCAST)
581 					if_printf(ifp,
582 						"bridge dropped %s packet\n",
583 						bif == BDG_BCAST ? "broadcast" :
584 								   "multicast");
585 				return;
586 			}
587 			/*
588 			 * But in some cases the bridge may return the
589 			 * packet for us to free; sigh.
590 			 */
591 			if (bif != BDG_BCAST && bif != BDG_MCAST) {
592 				m_freem(m);
593 				return;
594 			}
595 		}
596 	}
597 
598 	ether_demux(ifp, m);
599 	/* First chunk of an mbuf contains good entropy */
600 	if (harvest.ethernet)
601 		random_harvest(m, 16, 3, 0, RANDOM_NET);
602 }
603 
604 /*
605  * Upper layer processing for a received Ethernet packet.
606  */
607 void
608 ether_demux(struct ifnet *ifp, struct mbuf *m)
609 {
610 	struct ether_header *eh;
611 	int isr;
612 	u_short ether_type;
613 #if defined(NETATALK)
614 	struct llc *l;
615 #endif
616 	struct ip_fw *rule = NULL;
617 
618 	/* Extract info from dummynet tag, ignore others */
619 	for (;m->m_type == MT_TAG; m = m->m_next)
620 		if (m->m_flags == PACKET_TAG_DUMMYNET) {
621 			rule = ((struct dn_pkt *)m)->rule;
622 			ifp = m->m_next->m_pkthdr.rcvif;
623 		}
624 
625 	KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer"));
626 
627 	eh = mtod(m, struct ether_header *);
628 
629 	if (rule)	/* packet was already bridged */
630 		goto post_stats;
631 
632 	if (!(BDG_ACTIVE(ifp))) {
633 		/*
634 		 * Discard packet if upper layers shouldn't see it because it
635 		 * was unicast to a different Ethernet address. If the driver
636 		 * is working properly, then this situation can only happen
637 		 * when the interface is in promiscuous mode.
638 		 */
639 		if ((ifp->if_flags & IFF_PROMISC) != 0
640 		    && (eh->ether_dhost[0] & 1) == 0
641 		    && bcmp(eh->ether_dhost,
642 		      IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0
643 		    && (ifp->if_flags & IFF_PPROMISC) == 0) {
644 			    m_freem(m);
645 			    return;
646 		}
647 	}
648 
649 	/* Discard packet if interface is not up */
650 	if ((ifp->if_flags & IFF_UP) == 0) {
651 		m_freem(m);
652 		return;
653 	}
654 	if (eh->ether_dhost[0] & 1) {
655 		if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
656 			 sizeof(etherbroadcastaddr)) == 0)
657 			m->m_flags |= M_BCAST;
658 		else
659 			m->m_flags |= M_MCAST;
660 	}
661 	if (m->m_flags & (M_BCAST|M_MCAST))
662 		ifp->if_imcasts++;
663 
664 post_stats:
665 	if (IPFW_LOADED && ether_ipfw != 0) {
666 		if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
667 			if (m)
668 				m_freem(m);
669 			return;
670 		}
671 	}
672 
673 	/*
674 	 * If VLANs are configured on the interface, check to
675 	 * see if the device performed the decapsulation and
676 	 * provided us with the tag.
677 	 */
678 	if (ifp->if_nvlans &&
679 	    m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL) != NULL) {
680 		/*
681 		 * vlan_input() will either recursively call ether_input()
682 		 * or drop the packet.
683 		 */
684 		KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!"));
685 		(*vlan_input_p)(ifp, m);
686 		return;
687 	}
688 
689 	ether_type = ntohs(eh->ether_type);
690 
691 	/*
692 	 * Handle protocols that expect to have the Ethernet header
693 	 * (and possibly FCS) intact.
694 	 */
695 	switch (ether_type) {
696 	case ETHERTYPE_VLAN:
697 		if (ifp->if_nvlans != 0) {
698 			KASSERT(vlan_input_p,("ether_input: VLAN not loaded!"));
699 			(*vlan_input_p)(ifp, m);
700 		} else {
701 			ifp->if_noproto++;
702 			m_freem(m);
703 		}
704 		return;
705 	}
706 
707 	/* Strip off Ethernet header. */
708 	m_adj(m, ETHER_HDR_LEN);
709 
710 	/* If the CRC is still on the packet, trim it off. */
711 	if (m->m_flags & M_HASFCS) {
712 		m_adj(m, -ETHER_CRC_LEN);
713 		m->m_flags &= ~M_HASFCS;
714 	}
715 
716 	switch (ether_type) {
717 #ifdef INET
718 	case ETHERTYPE_IP:
719 		if (ipflow_fastforward(m))
720 			return;
721 		isr = NETISR_IP;
722 		break;
723 
724 	case ETHERTYPE_ARP:
725 		if (ifp->if_flags & IFF_NOARP) {
726 			/* Discard packet if ARP is disabled on interface */
727 			m_freem(m);
728 			return;
729 		}
730 		isr = NETISR_ARP;
731 		break;
732 #endif
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 #ifdef INET6
741 	case ETHERTYPE_IPV6:
742 		isr = NETISR_IPV6;
743 		break;
744 #endif
745 #ifdef NETATALK
746 	case ETHERTYPE_AT:
747 		isr = NETISR_ATALK1;
748 		break;
749 	case ETHERTYPE_AARP:
750 		isr = NETISR_AARP;
751 		break;
752 #endif /* NETATALK */
753 	default:
754 #ifdef IPX
755 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
756 			return;
757 #endif /* IPX */
758 #if defined(NETATALK)
759 		if (ether_type > ETHERMTU)
760 			goto discard;
761 		l = mtod(m, struct llc *);
762 		if (l->llc_dsap == LLC_SNAP_LSAP &&
763 		    l->llc_ssap == LLC_SNAP_LSAP &&
764 		    l->llc_control == LLC_UI) {
765 			if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
766 			    sizeof(at_org_code)) == 0 &&
767 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
768 			    	m_adj(m, LLC_SNAPFRAMELEN);
769 				isr = NETISR_ATALK2;
770 			    	break;
771 			}
772 			if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
773 			    sizeof(aarp_org_code)) == 0 &&
774 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
775 				m_adj(m, LLC_SNAPFRAMELEN);
776 				isr = NETISR_AARP;
777 				break;
778 			}
779 		}
780 #endif /* NETATALK */
781 		goto discard;
782 	}
783 	netisr_dispatch(isr, m);
784 	return;
785 
786 discard:
787 	/*
788 	 * Packet is to be discarded.  If netgraph is present,
789 	 * hand the packet to it for last chance processing;
790 	 * otherwise dispose of it.
791 	 */
792 	if (ng_ether_input_orphan_p != NULL) {
793 		/*
794 		 * Put back the ethernet header so netgraph has a
795 		 * consistent view of inbound packets.
796 		 */
797 		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
798 		(*ng_ether_input_orphan_p)(ifp, m);
799 		return;
800 	}
801 	m_freem(m);
802 }
803 
804 /*
805  * Convert Ethernet address to printable (loggable) representation.
806  * This routine is for compatibility; it's better to just use
807  *
808  *	printf("%6D", <pointer to address>, ":");
809  *
810  * since there's no static buffer involved.
811  */
812 char *
813 ether_sprintf(const u_char *ap)
814 {
815 	static char etherbuf[18];
816 	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
817 	return (etherbuf);
818 }
819 
820 /*
821  * Perform common duties while attaching to interface list
822  */
823 void
824 ether_ifattach(struct ifnet *ifp, const u_int8_t *llc)
825 {
826 	struct ifaddr *ifa;
827 	struct sockaddr_dl *sdl;
828 
829 	ifp->if_type = IFT_ETHER;
830 	ifp->if_addrlen = ETHER_ADDR_LEN;
831 	ifp->if_hdrlen = ETHER_HDR_LEN;
832 	if_attach(ifp);
833 	ifp->if_mtu = ETHERMTU;
834 	ifp->if_output = ether_output;
835 	ifp->if_input = ether_input;
836 	ifp->if_resolvemulti = ether_resolvemulti;
837 	if (ifp->if_baudrate == 0)
838 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
839 	ifp->if_broadcastaddr = etherbroadcastaddr;
840 
841 	ifa = ifaddr_byindex(ifp->if_index);
842 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
843 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
844 	sdl->sdl_type = IFT_ETHER;
845 	sdl->sdl_alen = ifp->if_addrlen;
846 	bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
847 	/*
848 	 * XXX: This doesn't belong here; we do it until
849 	 * XXX:  all drivers are cleaned up
850 	 */
851 	if (llc != IFP2AC(ifp)->ac_enaddr)
852 		bcopy(llc, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
853 
854 	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
855 	if (ng_ether_attach_p != NULL)
856 		(*ng_ether_attach_p)(ifp);
857 	if (BDG_LOADED)
858 		bdgtakeifaces_ptr();
859 }
860 
861 /*
862  * Perform common duties while detaching an Ethernet interface
863  */
864 void
865 ether_ifdetach(struct ifnet *ifp)
866 {
867 	if (ng_ether_detach_p != NULL)
868 		(*ng_ether_detach_p)(ifp);
869 	bpfdetach(ifp);
870 	if_detach(ifp);
871 	if (BDG_LOADED)
872 		bdgtakeifaces_ptr();
873 }
874 
875 SYSCTL_DECL(_net_link);
876 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
877 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
878 	    &ether_ipfw,0,"Pass ether pkts through firewall");
879 
880 int
881 ether_ioctl(ifp, command, data)
882 	struct ifnet *ifp;
883 	int command;
884 	caddr_t data;
885 {
886 	struct ifaddr *ifa = (struct ifaddr *) data;
887 	struct ifreq *ifr = (struct ifreq *) data;
888 	int error = 0;
889 
890 	switch (command) {
891 	case SIOCSIFADDR:
892 		ifp->if_flags |= IFF_UP;
893 
894 		switch (ifa->ifa_addr->sa_family) {
895 #ifdef INET
896 		case AF_INET:
897 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
898 			arp_ifinit(ifp, ifa);
899 			break;
900 #endif
901 #ifdef IPX
902 		/*
903 		 * XXX - This code is probably wrong
904 		 */
905 		case AF_IPX:
906 			{
907 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
908 			struct arpcom *ac = IFP2AC(ifp);
909 
910 			if (ipx_nullhost(*ina))
911 				ina->x_host =
912 				    *(union ipx_host *)
913 			            ac->ac_enaddr;
914 			else {
915 				bcopy((caddr_t) ina->x_host.c_host,
916 				      (caddr_t) ac->ac_enaddr,
917 				      sizeof(ac->ac_enaddr));
918 			}
919 
920 			/*
921 			 * Set new address
922 			 */
923 			ifp->if_init(ifp->if_softc);
924 			break;
925 			}
926 #endif
927 		default:
928 			ifp->if_init(ifp->if_softc);
929 			break;
930 		}
931 		break;
932 
933 	case SIOCGIFADDR:
934 		{
935 			struct sockaddr *sa;
936 
937 			sa = (struct sockaddr *) & ifr->ifr_data;
938 			bcopy(IFP2AC(ifp)->ac_enaddr,
939 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
940 		}
941 		break;
942 
943 	case SIOCSIFMTU:
944 		/*
945 		 * Set the interface MTU.
946 		 */
947 		if (ifr->ifr_mtu > ETHERMTU) {
948 			error = EINVAL;
949 		} else {
950 			ifp->if_mtu = ifr->ifr_mtu;
951 		}
952 		break;
953 	default:
954 		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
955 		break;
956 	}
957 	return (error);
958 }
959 
960 static int
961 ether_resolvemulti(ifp, llsa, sa)
962 	struct ifnet *ifp;
963 	struct sockaddr **llsa;
964 	struct sockaddr *sa;
965 {
966 	struct sockaddr_dl *sdl;
967 	struct sockaddr_in *sin;
968 #ifdef INET6
969 	struct sockaddr_in6 *sin6;
970 #endif
971 	u_char *e_addr;
972 
973 	switch(sa->sa_family) {
974 	case AF_LINK:
975 		/*
976 		 * No mapping needed. Just check that it's a valid MC address.
977 		 */
978 		sdl = (struct sockaddr_dl *)sa;
979 		e_addr = LLADDR(sdl);
980 		if ((e_addr[0] & 1) != 1)
981 			return EADDRNOTAVAIL;
982 		*llsa = 0;
983 		return 0;
984 
985 #ifdef INET
986 	case AF_INET:
987 		sin = (struct sockaddr_in *)sa;
988 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
989 			return EADDRNOTAVAIL;
990 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
991 		       M_WAITOK|M_ZERO);
992 		sdl->sdl_len = sizeof *sdl;
993 		sdl->sdl_family = AF_LINK;
994 		sdl->sdl_index = ifp->if_index;
995 		sdl->sdl_type = IFT_ETHER;
996 		sdl->sdl_alen = ETHER_ADDR_LEN;
997 		e_addr = LLADDR(sdl);
998 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
999 		*llsa = (struct sockaddr *)sdl;
1000 		return 0;
1001 #endif
1002 #ifdef INET6
1003 	case AF_INET6:
1004 		sin6 = (struct sockaddr_in6 *)sa;
1005 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1006 			/*
1007 			 * An IP6 address of 0 means listen to all
1008 			 * of the Ethernet multicast address used for IP6.
1009 			 * (This is used for multicast routers.)
1010 			 */
1011 			ifp->if_flags |= IFF_ALLMULTI;
1012 			*llsa = 0;
1013 			return 0;
1014 		}
1015 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1016 			return EADDRNOTAVAIL;
1017 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1018 		       M_WAITOK|M_ZERO);
1019 		sdl->sdl_len = sizeof *sdl;
1020 		sdl->sdl_family = AF_LINK;
1021 		sdl->sdl_index = ifp->if_index;
1022 		sdl->sdl_type = IFT_ETHER;
1023 		sdl->sdl_alen = ETHER_ADDR_LEN;
1024 		e_addr = LLADDR(sdl);
1025 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1026 		*llsa = (struct sockaddr *)sdl;
1027 		return 0;
1028 #endif
1029 
1030 	default:
1031 		/*
1032 		 * Well, the text isn't quite right, but it's the name
1033 		 * that counts...
1034 		 */
1035 		return EAFNOSUPPORT;
1036 	}
1037 }
1038 
1039 static moduledata_t ether_mod = {
1040         "ether",
1041         NULL,
1042         0
1043 };
1044 
1045 DECLARE_MODULE(ether, ether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1046 MODULE_VERSION(ether, 1);
1047