xref: /freebsd/sys/net/if_ethersubr.c (revision 729362425c09cf6b362366aabc6fb547eee8035a)
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 			n = m_copy(m, 0, (int)M_COPYALL);
285 
286 			n->m_pkthdr.csum_flags |= csum_flags;
287 			if (csum_flags & CSUM_DATA_VALID)
288 				n->m_pkthdr.csum_data = 0xffff;
289 
290 			(void) if_simloop(ifp, n, dst->sa_family, hlen);
291 		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
292 				ETHER_ADDR_LEN) == 0) {
293 			m->m_pkthdr.csum_flags |= csum_flags;
294 			if (csum_flags & CSUM_DATA_VALID)
295 				m->m_pkthdr.csum_data = 0xffff;
296 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
297 			return (0);	/* XXX */
298 		}
299 	}
300 
301 	/* Handle ng_ether(4) processing, if any */
302 	if (ng_ether_output_p != NULL) {
303 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
304 bad:			if (m != NULL)
305 				m_freem(m);
306 			return (error);
307 		}
308 		if (m == NULL)
309 			return (0);
310 	}
311 
312 	/* Continue with link-layer output */
313 	return ether_output_frame(ifp, m);
314 }
315 
316 /*
317  * Ethernet link layer output routine to send a raw frame to the device.
318  *
319  * This assumes that the 14 byte Ethernet header is present and contiguous
320  * in the first mbuf (if BRIDGE'ing).
321  */
322 int
323 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
324 {
325 	struct ip_fw *rule = NULL;
326 
327 	/* Extract info from dummynet tag, ignore others */
328 	for (; m->m_type == MT_TAG; m = m->m_next)
329 		if (m->m_flags == PACKET_TAG_DUMMYNET)
330 			rule = ((struct dn_pkt *)m)->rule;
331 
332 	if (rule == NULL && BDG_ACTIVE(ifp)) {
333 		/*
334 		 * Beware, the bridge code notices the null rcvif and
335 		 * uses that identify that it's being called from
336 		 * ether_output as opposd to ether_input.  Yech.
337 		 */
338 		m->m_pkthdr.rcvif = NULL;
339 		m = bdg_forward_ptr(m, ifp);
340 		if (m != NULL)
341 			m_freem(m);
342 		return (0);
343 	}
344 	if (IPFW_LOADED && ether_ipfw != 0) {
345 		if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
346 			if (m) {
347 				m_freem(m);
348 				return ENOBUFS;	/* pkt dropped */
349 			} else
350 				return 0;	/* consumed e.g. in a pipe */
351 		}
352 	}
353 
354 	/*
355 	 * Queue message on interface, update output statistics if
356 	 * successful, and start output if interface not yet active.
357 	 */
358 	return (IF_HANDOFF(&ifp->if_snd, m, ifp) ? 0 : ENOBUFS);
359 }
360 
361 /*
362  * ipfw processing for ethernet packets (in and out).
363  * The second parameter is NULL from ether_demux, and ifp from
364  * ether_output_frame. This section of code could be used from
365  * bridge.c as well as long as we use some extra info
366  * to distinguish that case from ether_output_frame();
367  */
368 int
369 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
370 	struct ip_fw **rule, int shared)
371 {
372 	struct ether_header *eh;
373 	struct ether_header save_eh;
374 	struct mbuf *m;
375 	int i;
376 	struct ip_fw_args args;
377 
378 	if (*rule != NULL && fw_one_pass)
379 		return 1; /* dummynet packet, already partially processed */
380 
381 	/*
382 	 * I need some amt of data to be contiguous, and in case others need
383 	 * the packet (shared==1) also better be in the first mbuf.
384 	 */
385 	m = *m0;
386 	i = min( m->m_pkthdr.len, max_protohdr);
387 	if ( shared || m->m_len < i) {
388 		m = m_pullup(m, i);
389 		if (m == NULL) {
390 			*m0 = m;
391 			return 0;
392 		}
393 	}
394 	eh = mtod(m, struct ether_header *);
395 	save_eh = *eh;			/* save copy for restore below */
396 	m_adj(m, ETHER_HDR_LEN);	/* strip ethernet header */
397 
398 	args.m = m;		/* the packet we are looking at		*/
399 	args.oif = dst;		/* destination, if any			*/
400 	args.divert_rule = 0;	/* we do not support divert yet		*/
401 	args.rule = *rule;	/* matching rule to restart		*/
402 	args.next_hop = NULL;	/* we do not support forward yet	*/
403 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
404 	i = ip_fw_chk_ptr(&args);
405 	m = args.m;
406 	if (m != NULL) {
407 		/*
408 		 * Restore Ethernet header, as needed, in case the
409 		 * mbuf chain was replaced by ipfw.
410 		 */
411 		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
412 		if (m == NULL) {
413 			*m0 = m;
414 			return 0;
415 		}
416 		if (eh != mtod(m, struct ether_header *))
417 			bcopy(&save_eh, mtod(m, struct ether_header *),
418 				ETHER_HDR_LEN);
419 	}
420 	*m0 = m;
421 	*rule = args.rule;
422 
423 	if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) /* drop */
424 		return 0;
425 
426 	if (i == 0) /* a PASS rule.  */
427 		return 1;
428 
429 	if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
430 		/*
431 		 * Pass the pkt to dummynet, which consumes it.
432 		 * If shared, make a copy and keep the original.
433 		 */
434 		if (shared) {
435 			m = m_copypacket(m, M_DONTWAIT);
436 			if (m == NULL)
437 				return 0;
438 		} else {
439 			/*
440 			 * Pass the original to dummynet and
441 			 * nothing back to the caller
442 			 */
443 			*m0 = NULL ;
444 		}
445 		ip_dn_io_ptr(m, (i & 0xffff),
446 			dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
447 		return 0;
448 	}
449 	/*
450 	 * XXX at some point add support for divert/forward actions.
451 	 * If none of the above matches, we have to drop the pkt.
452 	 */
453 	return 0;
454 }
455 
456 /*
457  * Process a received Ethernet packet; the packet is in the
458  * mbuf chain m with the ethernet header at the front.
459  */
460 static void
461 ether_input(struct ifnet *ifp, struct mbuf *m)
462 {
463 	struct ether_header *eh;
464 	u_short etype;
465 
466 	/*
467 	 * Do consistency checks to verify assumptions
468 	 * made by code past this point.
469 	 */
470 	if ((m->m_flags & M_PKTHDR) == 0) {
471 		if_printf(ifp, "discard frame w/o packet header\n");
472 		ifp->if_ierrors++;
473 		m_freem(m);
474 		return;
475 	}
476 	if (m->m_len < ETHER_HDR_LEN) {
477 		/* XXX maybe should pullup? */
478 		if_printf(ifp, "discard frame w/o leading ethernet "
479 				"header (len %u pkt len %u)\n",
480 				m->m_len, m->m_pkthdr.len);
481 		ifp->if_ierrors++;
482 		m_freem(m);
483 		return;
484 	}
485 	eh = mtod(m, struct ether_header *);
486 	etype = ntohs(eh->ether_type);
487 	if (m->m_pkthdr.len >
488 	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
489 		if_printf(ifp, "discard oversize frame "
490 				"(ether type %x flags %x len %u > max %lu)\n",
491 				etype, m->m_flags, m->m_pkthdr.len,
492 				ETHER_MAX_FRAME(ifp, etype,
493 						m->m_flags & M_HASFCS));
494 		ifp->if_ierrors++;
495 		m_freem(m);
496 		return;
497 	}
498 	if (m->m_pkthdr.rcvif == NULL) {
499 		if_printf(ifp, "discard frame w/o interface pointer\n");
500 		ifp->if_ierrors++;
501 		m_freem(m);
502 		return;
503 	}
504 #ifdef DIAGNOSTIC
505 	if (m->m_pkthdr.rcvif != ifp) {
506 		if_printf(ifp, "Warning, frame marked as received on %s%u\n",
507 			m->m_pkthdr.rcvif->if_name,
508 			m->m_pkthdr.rcvif->if_unit);
509 	}
510 #endif
511 
512 	/*
513 	 * Give bpf a chance at the packet.
514 	 */
515 	BPF_MTAP(ifp, m);
516 
517 	if (ifp->if_flags & IFF_MONITOR) {
518 		/*
519 		 * Interface marked for monitoring; discard packet.
520 		 */
521 		m_freem(m);
522 		return;
523 	}
524 
525 	/* If the CRC is still on the packet, trim it off. */
526 	if (m->m_flags & M_HASFCS) {
527 		m_adj(m, -ETHER_CRC_LEN);
528 		m->m_flags &= ~M_HASFCS;
529 	}
530 
531 #ifdef MAC
532 	mac_create_mbuf_from_ifnet(ifp, m);
533 #endif
534 
535 	ifp->if_ibytes += m->m_pkthdr.len;
536 
537 	/* Handle ng_ether(4) processing, if any */
538 	if (ng_ether_input_p != NULL) {
539 		(*ng_ether_input_p)(ifp, &m);
540 		if (m == NULL)
541 			return;
542 	}
543 
544 	/* Check for bridging mode */
545 	if (BDG_ACTIVE(ifp) ) {
546 		struct ifnet *bif;
547 
548 		/*
549 		 * Check with bridging code to see how the packet
550 		 * should be handled.  Possibilities are:
551 		 *
552 		 *    BDG_BCAST		broadcast
553 		 *    BDG_MCAST		multicast
554 		 *    BDG_LOCAL		for local address, don't forward
555 		 *    BDG_DROP		discard
556 		 *    ifp		forward only to specified interface(s)
557 		 *
558 		 * Non-local destinations are handled by passing the
559 		 * packet back to the bridge code.
560 		 */
561 		bif = bridge_in_ptr(ifp, eh);
562 		if (bif == BDG_DROP) {		/* discard packet */
563 			m_freem(m);
564 			return;
565 		}
566 		if (bif != BDG_LOCAL) {		/* non-local, forward */
567 			m = bdg_forward_ptr(m, bif);
568 			/*
569 			 * The bridge may consume the packet if it's not
570 			 * supposed to be passed up or if a problem occurred
571 			 * while doing its job.  This is reflected by it
572 			 * returning a NULL mbuf pointer.
573 			 */
574 			if (m == NULL) {
575 				if (bif == BDG_BCAST || bif == BDG_MCAST)
576 					if_printf(ifp,
577 						"bridge dropped %s packet\n",
578 						bif == BDG_BCAST ? "broadcast" :
579 								   "multicast");
580 				return;
581 			}
582 			/*
583 			 * But in some cases the bridge may return the
584 			 * packet for us to free; sigh.
585 			 */
586 			if (bif != BDG_BCAST && bif != BDG_MCAST) {
587 				m_freem(m);
588 				return;
589 			}
590 		}
591 	}
592 
593 	ether_demux(ifp, m);
594 	/* First chunk of an mbuf contains good entropy */
595 	if (harvest.ethernet)
596 		random_harvest(m, 16, 3, 0, RANDOM_NET);
597 }
598 
599 /*
600  * Upper layer processing for a received Ethernet packet.
601  */
602 void
603 ether_demux(struct ifnet *ifp, struct mbuf *m)
604 {
605 	struct ether_header *eh;
606 	int isr;
607 	u_short ether_type;
608 #if defined(NETATALK)
609 	struct llc *l;
610 #endif
611 	struct ip_fw *rule = NULL;
612 
613 	/* Extract info from dummynet tag, ignore others */
614 	for (;m->m_type == MT_TAG; m = m->m_next)
615 		if (m->m_flags == PACKET_TAG_DUMMYNET) {
616 			rule = ((struct dn_pkt *)m)->rule;
617 			ifp = m->m_next->m_pkthdr.rcvif;
618 		}
619 
620 	KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer"));
621 
622 	eh = mtod(m, struct ether_header *);
623 
624 	if (rule)	/* packet was already bridged */
625 		goto post_stats;
626 
627 	if (!(BDG_ACTIVE(ifp))) {
628 		/*
629 		 * Discard packet if upper layers shouldn't see it because it
630 		 * was unicast to a different Ethernet address. If the driver
631 		 * is working properly, then this situation can only happen
632 		 * when the interface is in promiscuous mode.
633 		 */
634 		if ((ifp->if_flags & IFF_PROMISC) != 0
635 		    && (eh->ether_dhost[0] & 1) == 0
636 		    && bcmp(eh->ether_dhost,
637 		      IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0
638 		    && (ifp->if_flags & IFF_PPROMISC) == 0) {
639 			    m_freem(m);
640 			    return;
641 		}
642 	}
643 
644 	/* Discard packet if interface is not up */
645 	if ((ifp->if_flags & IFF_UP) == 0) {
646 		m_freem(m);
647 		return;
648 	}
649 	if (eh->ether_dhost[0] & 1) {
650 		if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
651 			 sizeof(etherbroadcastaddr)) == 0)
652 			m->m_flags |= M_BCAST;
653 		else
654 			m->m_flags |= M_MCAST;
655 	}
656 	if (m->m_flags & (M_BCAST|M_MCAST))
657 		ifp->if_imcasts++;
658 
659 post_stats:
660 	if (IPFW_LOADED && ether_ipfw != 0) {
661 		if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
662 			if (m)
663 				m_freem(m);
664 			return;
665 		}
666 	}
667 
668 	/*
669 	 * If VLANs are configured on the interface, check to
670 	 * see if the device performed the decapsulation and
671 	 * provided us with the tag.
672 	 */
673 	if (ifp->if_nvlans &&
674 	    m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL) != NULL) {
675 		/*
676 		 * vlan_input() will either recursively call ether_input()
677 		 * or drop the packet.
678 		 */
679 		KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!"));
680 		(*vlan_input_p)(ifp, m);
681 		return;
682 	}
683 
684 	ether_type = ntohs(eh->ether_type);
685 
686 	/*
687 	 * Handle protocols that expect to have the Ethernet header
688 	 * (and possibly FCS) intact.
689 	 */
690 	switch (ether_type) {
691 	case ETHERTYPE_VLAN:
692 		if (ifp->if_nvlans != 0) {
693 			KASSERT(vlan_input_p,("ether_input: VLAN not loaded!"));
694 			(*vlan_input_p)(ifp, m);
695 		} else {
696 			ifp->if_noproto++;
697 			m_freem(m);
698 		}
699 		return;
700 	}
701 
702 	/* Strip off Ethernet header. */
703 	m_adj(m, ETHER_HDR_LEN);
704 
705 	/* If the CRC is still on the packet, trim it off. */
706 	if (m->m_flags & M_HASFCS) {
707 		m_adj(m, -ETHER_CRC_LEN);
708 		m->m_flags &= ~M_HASFCS;
709 	}
710 
711 	switch (ether_type) {
712 #ifdef INET
713 	case ETHERTYPE_IP:
714 		if (ipflow_fastforward(m))
715 			return;
716 		isr = NETISR_IP;
717 		break;
718 
719 	case ETHERTYPE_ARP:
720 		if (ifp->if_flags & IFF_NOARP) {
721 			/* Discard packet if ARP is disabled on interface */
722 			m_freem(m);
723 			return;
724 		}
725 		isr = NETISR_ARP;
726 		break;
727 #endif
728 #ifdef IPX
729 	case ETHERTYPE_IPX:
730 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
731 			return;
732 		isr = NETISR_IPX;
733 		break;
734 #endif
735 #ifdef INET6
736 	case ETHERTYPE_IPV6:
737 		isr = NETISR_IPV6;
738 		break;
739 #endif
740 #ifdef NETATALK
741 	case ETHERTYPE_AT:
742 		isr = NETISR_ATALK1;
743 		break;
744 	case ETHERTYPE_AARP:
745 		isr = NETISR_AARP;
746 		break;
747 #endif /* NETATALK */
748 	default:
749 #ifdef IPX
750 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
751 			return;
752 #endif /* IPX */
753 #if defined(NETATALK)
754 		if (ether_type > ETHERMTU)
755 			goto discard;
756 		l = mtod(m, struct llc *);
757 		if (l->llc_dsap == LLC_SNAP_LSAP &&
758 		    l->llc_ssap == LLC_SNAP_LSAP &&
759 		    l->llc_control == LLC_UI) {
760 			if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
761 			    sizeof(at_org_code)) == 0 &&
762 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
763 			    	m_adj(m, LLC_SNAPFRAMELEN);
764 				isr = NETISR_ATALK2;
765 			    	break;
766 			}
767 			if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
768 			    sizeof(aarp_org_code)) == 0 &&
769 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
770 				m_adj(m, LLC_SNAPFRAMELEN);
771 				isr = NETISR_AARP;
772 				break;
773 			}
774 		}
775 #endif /* NETATALK */
776 		goto discard;
777 	}
778 	netisr_dispatch(isr, m);
779 	return;
780 
781 discard:
782 	/*
783 	 * Packet is to be discarded.  If netgraph is present,
784 	 * hand the packet to it for last chance processing;
785 	 * otherwise dispose of it.
786 	 */
787 	if (ng_ether_input_orphan_p != NULL) {
788 		/*
789 		 * Put back the ethernet header so netgraph has a
790 		 * consistent view of inbound packets.
791 		 */
792 		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
793 		(*ng_ether_input_orphan_p)(ifp, m);
794 		return;
795 	}
796 	m_freem(m);
797 }
798 
799 /*
800  * Convert Ethernet address to printable (loggable) representation.
801  * This routine is for compatibility; it's better to just use
802  *
803  *	printf("%6D", <pointer to address>, ":");
804  *
805  * since there's no static buffer involved.
806  */
807 char *
808 ether_sprintf(const u_char *ap)
809 {
810 	static char etherbuf[18];
811 	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
812 	return (etherbuf);
813 }
814 
815 /*
816  * Perform common duties while attaching to interface list
817  */
818 void
819 ether_ifattach(struct ifnet *ifp, const u_int8_t *llc)
820 {
821 	struct ifaddr *ifa;
822 	struct sockaddr_dl *sdl;
823 
824 	ifp->if_type = IFT_ETHER;
825 	ifp->if_addrlen = ETHER_ADDR_LEN;
826 	ifp->if_hdrlen = ETHER_HDR_LEN;
827 	if_attach(ifp);
828 	ifp->if_mtu = ETHERMTU;
829 	ifp->if_output = ether_output;
830 	ifp->if_input = ether_input;
831 	ifp->if_resolvemulti = ether_resolvemulti;
832 	if (ifp->if_baudrate == 0)
833 		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
834 	ifp->if_broadcastaddr = etherbroadcastaddr;
835 
836 	ifa = ifaddr_byindex(ifp->if_index);
837 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
838 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
839 	sdl->sdl_type = IFT_ETHER;
840 	sdl->sdl_alen = ifp->if_addrlen;
841 	bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
842 	/*
843 	 * XXX: This doesn't belong here; we do it until
844 	 * XXX:  all drivers are cleaned up
845 	 */
846 	if (llc != IFP2AC(ifp)->ac_enaddr)
847 		bcopy(llc, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
848 
849 	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
850 	if (ng_ether_attach_p != NULL)
851 		(*ng_ether_attach_p)(ifp);
852 	if (BDG_LOADED)
853 		bdgtakeifaces_ptr();
854 }
855 
856 /*
857  * Perform common duties while detaching an Ethernet interface
858  */
859 void
860 ether_ifdetach(struct ifnet *ifp)
861 {
862 	if (ng_ether_detach_p != NULL)
863 		(*ng_ether_detach_p)(ifp);
864 	bpfdetach(ifp);
865 	if_detach(ifp);
866 	if (BDG_LOADED)
867 		bdgtakeifaces_ptr();
868 }
869 
870 SYSCTL_DECL(_net_link);
871 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
872 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
873 	    &ether_ipfw,0,"Pass ether pkts through firewall");
874 
875 int
876 ether_ioctl(ifp, command, data)
877 	struct ifnet *ifp;
878 	int command;
879 	caddr_t data;
880 {
881 	struct ifaddr *ifa = (struct ifaddr *) data;
882 	struct ifreq *ifr = (struct ifreq *) data;
883 	int error = 0;
884 
885 	switch (command) {
886 	case SIOCSIFADDR:
887 		ifp->if_flags |= IFF_UP;
888 
889 		switch (ifa->ifa_addr->sa_family) {
890 #ifdef INET
891 		case AF_INET:
892 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
893 			arp_ifinit(ifp, ifa);
894 			break;
895 #endif
896 #ifdef IPX
897 		/*
898 		 * XXX - This code is probably wrong
899 		 */
900 		case AF_IPX:
901 			{
902 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
903 			struct arpcom *ac = IFP2AC(ifp);
904 
905 			if (ipx_nullhost(*ina))
906 				ina->x_host =
907 				    *(union ipx_host *)
908 			            ac->ac_enaddr;
909 			else {
910 				bcopy((caddr_t) ina->x_host.c_host,
911 				      (caddr_t) ac->ac_enaddr,
912 				      sizeof(ac->ac_enaddr));
913 			}
914 
915 			/*
916 			 * Set new address
917 			 */
918 			ifp->if_init(ifp->if_softc);
919 			break;
920 			}
921 #endif
922 		default:
923 			ifp->if_init(ifp->if_softc);
924 			break;
925 		}
926 		break;
927 
928 	case SIOCGIFADDR:
929 		{
930 			struct sockaddr *sa;
931 
932 			sa = (struct sockaddr *) & ifr->ifr_data;
933 			bcopy(IFP2AC(ifp)->ac_enaddr,
934 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
935 		}
936 		break;
937 
938 	case SIOCSIFMTU:
939 		/*
940 		 * Set the interface MTU.
941 		 */
942 		if (ifr->ifr_mtu > ETHERMTU) {
943 			error = EINVAL;
944 		} else {
945 			ifp->if_mtu = ifr->ifr_mtu;
946 		}
947 		break;
948 	default:
949 		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
950 		break;
951 	}
952 	return (error);
953 }
954 
955 static int
956 ether_resolvemulti(ifp, llsa, sa)
957 	struct ifnet *ifp;
958 	struct sockaddr **llsa;
959 	struct sockaddr *sa;
960 {
961 	struct sockaddr_dl *sdl;
962 	struct sockaddr_in *sin;
963 #ifdef INET6
964 	struct sockaddr_in6 *sin6;
965 #endif
966 	u_char *e_addr;
967 
968 	switch(sa->sa_family) {
969 	case AF_LINK:
970 		/*
971 		 * No mapping needed. Just check that it's a valid MC address.
972 		 */
973 		sdl = (struct sockaddr_dl *)sa;
974 		e_addr = LLADDR(sdl);
975 		if ((e_addr[0] & 1) != 1)
976 			return EADDRNOTAVAIL;
977 		*llsa = 0;
978 		return 0;
979 
980 #ifdef INET
981 	case AF_INET:
982 		sin = (struct sockaddr_in *)sa;
983 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
984 			return EADDRNOTAVAIL;
985 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
986 		       M_WAITOK|M_ZERO);
987 		sdl->sdl_len = sizeof *sdl;
988 		sdl->sdl_family = AF_LINK;
989 		sdl->sdl_index = ifp->if_index;
990 		sdl->sdl_type = IFT_ETHER;
991 		sdl->sdl_alen = ETHER_ADDR_LEN;
992 		e_addr = LLADDR(sdl);
993 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
994 		*llsa = (struct sockaddr *)sdl;
995 		return 0;
996 #endif
997 #ifdef INET6
998 	case AF_INET6:
999 		sin6 = (struct sockaddr_in6 *)sa;
1000 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1001 			/*
1002 			 * An IP6 address of 0 means listen to all
1003 			 * of the Ethernet multicast address used for IP6.
1004 			 * (This is used for multicast routers.)
1005 			 */
1006 			ifp->if_flags |= IFF_ALLMULTI;
1007 			*llsa = 0;
1008 			return 0;
1009 		}
1010 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1011 			return EADDRNOTAVAIL;
1012 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1013 		       M_WAITOK|M_ZERO);
1014 		sdl->sdl_len = sizeof *sdl;
1015 		sdl->sdl_family = AF_LINK;
1016 		sdl->sdl_index = ifp->if_index;
1017 		sdl->sdl_type = IFT_ETHER;
1018 		sdl->sdl_alen = ETHER_ADDR_LEN;
1019 		e_addr = LLADDR(sdl);
1020 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1021 		*llsa = (struct sockaddr *)sdl;
1022 		return 0;
1023 #endif
1024 
1025 	default:
1026 		/*
1027 		 * Well, the text isn't quite right, but it's the name
1028 		 * that counts...
1029 		 */
1030 		return EAFNOSUPPORT;
1031 	}
1032 }
1033 
1034 static moduledata_t ether_mod = {
1035         "ether",
1036         NULL,
1037         0
1038 };
1039 
1040 DECLARE_MODULE(ether, ether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1041 MODULE_VERSION(ether, 1);
1042