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