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