xref: /dflybsd-src/sys/net/if_ethersubr.c (revision 1de703daf67320d643c7695d9bf7ec54c33d5512)
1 /*
2  * Copyright (c) 1982, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
34  * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
35  * $DragonFly: src/sys/net/if_ethersubr.c,v 1.2 2003/06/17 04:28:47 dillon Exp $
36  */
37 
38 #include "opt_atalk.h"
39 #include "opt_inet.h"
40 #include "opt_inet6.h"
41 #include "opt_ipx.h"
42 #include "opt_bdg.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/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysctl.h>
53 
54 #include <net/if.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
57 #include <net/if_llc.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/bpf.h>
61 #include <net/ethernet.h>
62 #include <net/bridge.h>
63 
64 #if defined(INET) || defined(INET6)
65 #include <netinet/in.h>
66 #include <netinet/in_var.h>
67 #include <netinet/if_ether.h>
68 #include <netinet/ip_fw.h>
69 #include <netinet/ip_dummynet.h>
70 #endif
71 #ifdef INET6
72 #include <netinet6/nd6.h>
73 #endif
74 
75 #ifdef IPX
76 #include <netipx/ipx.h>
77 #include <netipx/ipx_if.h>
78 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
79 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
80 		struct sockaddr *dst, short *tp, int *hlen);
81 #endif
82 
83 #ifdef NS
84 #include <netns/ns.h>
85 #include <netns/ns_if.h>
86 ushort ns_nettype;
87 int ether_outputdebug = 0;
88 int ether_inputdebug = 0;
89 #endif
90 
91 #ifdef NETATALK
92 #include <netatalk/at.h>
93 #include <netatalk/at_var.h>
94 #include <netatalk/at_extern.h>
95 
96 #define llc_snap_org_code llc_un.type_snap.org_code
97 #define llc_snap_ether_type llc_un.type_snap.ether_type
98 
99 extern u_char	at_org_code[3];
100 extern u_char	aarp_org_code[3];
101 #endif /* NETATALK */
102 
103 /* netgraph node hooks for ng_ether(4) */
104 void	(*ng_ether_input_p)(struct ifnet *ifp,
105 		struct mbuf **mp, struct ether_header *eh);
106 void	(*ng_ether_input_orphan_p)(struct ifnet *ifp,
107 		struct mbuf *m, struct ether_header *eh);
108 int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
109 void	(*ng_ether_attach_p)(struct ifnet *ifp);
110 void	(*ng_ether_detach_p)(struct ifnet *ifp);
111 
112 int	(*vlan_input_p)(struct ether_header *eh, struct mbuf *m);
113 int	(*vlan_input_tag_p)(struct ether_header *eh, struct mbuf *m,
114 		u_int16_t t);
115 
116 /* bridge support */
117 int do_bridge;
118 bridge_in_t *bridge_in_ptr;
119 bdg_forward_t *bdg_forward_ptr;
120 bdgtakeifaces_t *bdgtakeifaces_ptr;
121 struct bdg_softc *ifp2sc;
122 
123 static	int ether_resolvemulti(struct ifnet *, struct sockaddr **,
124 		struct sockaddr *);
125 u_char	etherbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
126 #define senderr(e) do { error = (e); goto bad;} while (0)
127 #define IFP2AC(IFP) ((struct arpcom *)IFP)
128 
129 int
130 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
131 	struct ip_fw **rule, struct ether_header *eh, int shared);
132 static int ether_ipfw;
133 
134 /*
135  * Ethernet output routine.
136  * Encapsulate a packet of type family for the local net.
137  * Use trailer local net encapsulation if enough data in first
138  * packet leaves a multiple of 512 bytes of data in remainder.
139  * Assumes that ifp is actually pointer to arpcom structure.
140  */
141 int
142 ether_output(ifp, m, dst, rt0)
143 	register struct ifnet *ifp;
144 	struct mbuf *m;
145 	struct sockaddr *dst;
146 	struct rtentry *rt0;
147 {
148 	short type;
149 	int error = 0, hdrcmplt = 0;
150  	u_char esrc[6], edst[6];
151 	register struct rtentry *rt;
152 	register struct ether_header *eh;
153 	int loop_copy = 0;
154 	int hlen;	/* link layer header lenght */
155 	struct arpcom *ac = IFP2AC(ifp);
156 
157 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
158 		senderr(ENETDOWN);
159 	rt = rt0;
160 	if (rt) {
161 		if ((rt->rt_flags & RTF_UP) == 0) {
162 			rt0 = rt = rtalloc1(dst, 1, 0UL);
163 			if (rt0)
164 				rt->rt_refcnt--;
165 			else
166 				senderr(EHOSTUNREACH);
167 		}
168 		if (rt->rt_flags & RTF_GATEWAY) {
169 			if (rt->rt_gwroute == 0)
170 				goto lookup;
171 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
172 				rtfree(rt); rt = rt0;
173 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1,
174 							  0UL);
175 				if ((rt = rt->rt_gwroute) == 0)
176 					senderr(EHOSTUNREACH);
177 			}
178 		}
179 		if (rt->rt_flags & RTF_REJECT)
180 			if (rt->rt_rmx.rmx_expire == 0 ||
181 			    time_second < rt->rt_rmx.rmx_expire)
182 				senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
183 	}
184 	hlen = ETHER_HDR_LEN;
185 	switch (dst->sa_family) {
186 #ifdef INET
187 	case AF_INET:
188 		if (!arpresolve(ifp, rt, m, dst, edst, rt0))
189 			return (0);	/* if not yet resolved */
190 		type = htons(ETHERTYPE_IP);
191 		break;
192 #endif
193 #ifdef INET6
194 	case AF_INET6:
195 		if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, (u_char *)edst)) {
196 			/* Something bad happened */
197 			return(0);
198 		}
199 		type = htons(ETHERTYPE_IPV6);
200 		break;
201 #endif
202 #ifdef IPX
203 	case AF_IPX:
204 		if (ef_outputp) {
205 		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
206 		    if (error)
207 			goto bad;
208 		} else
209 		    type = htons(ETHERTYPE_IPX);
210  		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
211 		    (caddr_t)edst, sizeof (edst));
212 		break;
213 #endif
214 #ifdef NETATALK
215 	case AF_APPLETALK:
216 	  {
217 	    struct at_ifaddr *aa;
218 
219 	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
220 		    goto bad;
221 	    }
222 	    if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
223 		    return (0);
224 	    /*
225 	     * In the phase 2 case, need to prepend an mbuf for the llc header.
226 	     * Since we must preserve the value of m, which is passed to us by
227 	     * value, we m_copy() the first mbuf, and use it for our llc header.
228 	     */
229 	    if ( aa->aa_flags & AFA_PHASE2 ) {
230 		struct llc llc;
231 
232 		M_PREPEND(m, sizeof(struct llc), M_WAIT);
233 		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
234 		llc.llc_control = LLC_UI;
235 		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
236 		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
237 		bcopy(&llc, mtod(m, caddr_t), sizeof(struct llc));
238 		type = htons(m->m_pkthdr.len);
239 		hlen = sizeof(struct llc) + ETHER_HDR_LEN;
240 	    } else {
241 		type = htons(ETHERTYPE_AT);
242 	    }
243 	    break;
244 	  }
245 #endif /* NETATALK */
246 #ifdef NS
247 	case AF_NS:
248 		switch(ns_nettype){
249 		default:
250 		case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
251 			type = 0x8137;
252 			break;
253 		case 0x0: /* Novell 802.3 */
254 			type = htons( m->m_pkthdr.len);
255 			break;
256 		case 0xe0e0: /* Novell 802.2 and Token-Ring */
257 			M_PREPEND(m, 3, M_WAIT);
258 			type = htons( m->m_pkthdr.len);
259 			cp = mtod(m, u_char *);
260 			*cp++ = 0xE0;
261 			*cp++ = 0xE0;
262 			*cp++ = 0x03;
263 			break;
264 		}
265  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
266 		    (caddr_t)edst, sizeof (edst));
267 		/*
268 		 * XXX if ns_thishost is the same as the node's ethernet
269 		 * address then just the default code will catch this anyhow.
270 		 * So I'm not sure if this next clause should be here at all?
271 		 * [JRE]
272 		 */
273 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost, sizeof(edst))){
274 			m->m_pkthdr.rcvif = ifp;
275 			inq = &nsintrq;
276 			if (IF_HANDOFF(inq, m, NULL))
277 				schednetisr(NETISR_NS);
278 			return (error);
279 		}
280 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
281 			m->m_flags |= M_BCAST;
282 		}
283 		break;
284 #endif /* NS */
285 
286 	case pseudo_AF_HDRCMPLT:
287 		hdrcmplt = 1;
288 		eh = (struct ether_header *)dst->sa_data;
289 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
290 		/* FALLTHROUGH */
291 
292 	case AF_UNSPEC:
293 		loop_copy = -1; /* if this is for us, don't do it */
294 		eh = (struct ether_header *)dst->sa_data;
295  		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
296 		type = eh->ether_type;
297 		break;
298 
299 	default:
300 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
301 			dst->sa_family);
302 		senderr(EAFNOSUPPORT);
303 	}
304 
305 	/*
306 	 * Add local net header.  If no space in first mbuf,
307 	 * allocate another.
308 	 */
309 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
310 	if (m == 0)
311 		senderr(ENOBUFS);
312 	eh = mtod(m, struct ether_header *);
313 	(void)memcpy(&eh->ether_type, &type,
314 		sizeof(eh->ether_type));
315  	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
316 	if (hdrcmplt)
317 		(void)memcpy(eh->ether_shost, esrc,
318 			sizeof(eh->ether_shost));
319 	else
320 		(void)memcpy(eh->ether_shost, ac->ac_enaddr,
321 			sizeof(eh->ether_shost));
322 
323 	/*
324 	 * If a simplex interface, and the packet is being sent to our
325 	 * Ethernet address or a broadcast address, loopback a copy.
326 	 * XXX To make a simplex device behave exactly like a duplex
327 	 * device, we should copy in the case of sending to our own
328 	 * ethernet address (thus letting the original actually appear
329 	 * on the wire). However, we don't do that here for security
330 	 * reasons and compatibility with the original behavior.
331 	 */
332 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
333 		int csum_flags = 0;
334 
335 		if (m->m_pkthdr.csum_flags & CSUM_IP)
336 			csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
337 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
338 			csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
339 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
340 			struct mbuf *n;
341 
342 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
343 				n->m_pkthdr.csum_flags |= csum_flags;
344 				if (csum_flags & CSUM_DATA_VALID)
345 					n->m_pkthdr.csum_data = 0xffff;
346 				(void)if_simloop(ifp, n, dst->sa_family, hlen);
347 			} else
348 				ifp->if_iqdrops++;
349 		} else if (bcmp(eh->ether_dhost,
350 		    eh->ether_shost, ETHER_ADDR_LEN) == 0) {
351 			m->m_pkthdr.csum_flags |= csum_flags;
352 			if (csum_flags & CSUM_DATA_VALID)
353 				m->m_pkthdr.csum_data = 0xffff;
354 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
355 			return (0);	/* XXX */
356 		}
357 	}
358 
359 	/* Handle ng_ether(4) processing, if any */
360 	if (ng_ether_output_p != NULL) {
361 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
362 bad:			if (m != NULL)
363 				m_freem(m);
364 			return (error);
365 		}
366 		if (m == NULL)
367 			return (0);
368 	}
369 
370 	/* Continue with link-layer output */
371 	return ether_output_frame(ifp, m);
372 }
373 
374 /*
375  * Ethernet link layer output routine to send a raw frame to the device.
376  *
377  * This assumes that the 14 byte Ethernet header is present and contiguous
378  * in the first mbuf (if BRIDGE'ing).
379  */
380 int
381 ether_output_frame(ifp, m)
382 	struct ifnet *ifp;
383 	struct mbuf *m;
384 {
385 	int error = 0;
386 	int s;
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)	/* packet was already bridged */
395 		goto no_bridge;
396 
397 	if (BDG_ACTIVE(ifp) ) {
398 		struct ether_header *eh; /* a ptr suffices */
399 
400 		m->m_pkthdr.rcvif = NULL;
401 		eh = mtod(m, struct ether_header *);
402 		m_adj(m, ETHER_HDR_LEN);
403 		m = bdg_forward_ptr(m, eh, ifp);
404 		if (m != NULL)
405 			m_freem(m);
406 		return (0);
407 	}
408 
409 no_bridge:
410 	s = splimp();
411 	if (IPFW_LOADED && ether_ipfw != 0) {
412 		struct ether_header save_eh, *eh;
413 
414 		eh = mtod(m, struct ether_header *);
415 		save_eh = *eh;
416 		m_adj(m, ETHER_HDR_LEN);
417 		if (ether_ipfw_chk(&m, ifp, &rule, eh, 0) == 0) {
418 			if (m) {
419 				m_freem(m);
420 				return ENOBUFS;	/* pkt dropped */
421 			} else
422 				return 0;	/* consumed e.g. in a pipe */
423 		}
424 		/* packet was ok, restore the ethernet header */
425 		if ( (void *)(eh + 1) == (void *)m->m_data) {
426 			m->m_data -= ETHER_HDR_LEN ;
427 			m->m_len += ETHER_HDR_LEN ;
428 			m->m_pkthdr.len += ETHER_HDR_LEN ;
429 		} else {
430 			M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
431 			if (m == NULL) /* nope... */
432 				return ENOBUFS;
433 			bcopy(&save_eh, mtod(m, struct ether_header *),
434 			    ETHER_HDR_LEN);
435 		}
436 	}
437 
438 	/*
439 	 * Queue message on interface, update output statistics if
440 	 * successful, and start output if interface not yet active.
441 	 */
442 	if (!IF_HANDOFF(&ifp->if_snd, m, ifp))
443 		error = ENOBUFS;
444 	splx(s);
445 	return (error);
446 }
447 
448 /*
449  * ipfw processing for ethernet packets (in and out).
450  * The second parameter is NULL from ether_demux, and ifp from
451  * ether_output_frame. This section of code could be used from
452  * bridge.c as well as long as we use some extra info
453  * to distinguish that case from ether_output_frame();
454  */
455 int
456 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
457 	struct ip_fw **rule, struct ether_header *eh, int shared)
458 {
459 	struct ether_header save_eh = *eh;	/* might be a ptr in m */
460 	int i;
461 	struct ip_fw_args args;
462 
463 	if (*rule != NULL && fw_one_pass)
464 		return 1; /* dummynet packet, already partially processed */
465 
466 	/*
467 	 * I need some amt of data to be contiguous, and in case others need
468 	 * the packet (shared==1) also better be in the first mbuf.
469 	 */
470 	i = min( (*m0)->m_pkthdr.len, max_protohdr);
471 	if ( shared || (*m0)->m_len < i) {
472 		*m0 = m_pullup(*m0, i);
473 		if (*m0 == NULL)
474 			return 0;
475 	}
476 
477 	args.m = *m0;		/* the packet we are looking at		*/
478 	args.oif = dst;		/* destination, if any			*/
479 	args.divert_rule = 0;	/* we do not support divert yet		*/
480 	args.rule = *rule;	/* matching rule to restart		*/
481 	args.next_hop = NULL;	/* we do not support forward yet	*/
482 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
483 	i = ip_fw_chk_ptr(&args);
484 	*m0 = args.m;
485 	*rule = args.rule;
486 
487 	if ( (i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL) /* drop */
488 		return 0;
489 
490 	if (i == 0) /* a PASS rule.  */
491 		return 1;
492 
493 	if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
494 		/*
495 		 * Pass the pkt to dummynet, which consumes it.
496 		 * If shared, make a copy and keep the original.
497 		 */
498 		struct mbuf *m ;
499 
500 		if (shared) {
501 			m = m_copypacket(*m0, M_DONTWAIT);
502 			if (m == NULL)
503 				return 0;
504 		} else {
505 			m = *m0 ; /* pass the original to dummynet */
506 			*m0 = NULL ; /* and nothing back to the caller */
507 		}
508 		/*
509 		 * Prepend the header, optimize for the common case of
510 		 * eh pointing into the mbuf.
511 		 */
512 		if ( (void *)(eh + 1) == (void *)m->m_data) {
513 			m->m_data -= ETHER_HDR_LEN ;
514 			m->m_len += ETHER_HDR_LEN ;
515 			m->m_pkthdr.len += ETHER_HDR_LEN ;
516 		} else {
517 			M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
518 			if (m == NULL) /* nope... */
519 				return 0;
520 			bcopy(&save_eh, mtod(m, struct ether_header *),
521 			    ETHER_HDR_LEN);
522 		}
523 		ip_dn_io_ptr(m, (i & 0xffff),
524 			dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
525 		return 0;
526 	}
527 	/*
528 	 * XXX at some point add support for divert/forward actions.
529 	 * If none of the above matches, we have to drop the pkt.
530 	 */
531 	return 0;
532 }
533 
534 /*
535  * Process a received Ethernet packet. We have two different interfaces:
536  * one (conventional) assumes the packet in the mbuf, with the ethernet
537  * header provided separately in *eh. The second one (new) has everything
538  * in the mbuf, and we can tell it because eh == NULL.
539  * The caller MUST MAKE SURE that there are at least
540  * sizeof(struct ether_header) bytes in the first mbuf.
541  *
542  * This allows us to concentrate in one place a bunch of code which
543  * is replicated in all device drivers. Also, many functions called
544  * from ether_input() try to put the eh back into the mbuf, so we
545  * can later propagate the 'contiguous packet' interface to them,
546  * and handle the old interface just here.
547  *
548  * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or
549  * cluster, right before m_data. So be very careful when working on m,
550  * as you could destroy *eh !!
551  *
552  * First we perform any link layer operations, then continue
553  * to the upper layers with ether_demux().
554  */
555 void
556 ether_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
557 {
558 	struct ether_header save_eh;
559 
560 	if (eh == NULL) {
561 		if (m->m_len < sizeof(struct ether_header)) {
562 			/* XXX error in the caller. */
563 			m_freem(m);
564 			return;
565 		}
566 		m->m_pkthdr.rcvif = ifp;
567 		eh = mtod(m, struct ether_header *);
568 		m->m_data += sizeof(struct ether_header);
569 		m->m_len -= sizeof(struct ether_header);
570 		m->m_pkthdr.len = m->m_len;
571 	}
572 
573 	/* Check for a BPF tap */
574 	if (ifp->if_bpf != NULL) {
575 		struct m_hdr mh;
576 
577 		/* This kludge is OK; BPF treats the "mbuf" as read-only */
578 		mh.mh_next = m;
579 		mh.mh_data = (char *)eh;
580 		mh.mh_len = ETHER_HDR_LEN;
581 		bpf_mtap(ifp, (struct mbuf *)&mh);
582 	}
583 
584 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
585 
586 	/* Handle ng_ether(4) processing, if any */
587 	if (ng_ether_input_p != NULL) {
588 		(*ng_ether_input_p)(ifp, &m, eh);
589 		if (m == NULL)
590 			return;
591 	}
592 
593 	/* Check for bridging mode */
594 	if (BDG_ACTIVE(ifp) ) {
595 		struct ifnet *bif;
596 
597 		/* Check with bridging code */
598 		if ((bif = bridge_in_ptr(ifp, eh)) == BDG_DROP) {
599 			m_freem(m);
600 			return;
601 		}
602 		if (bif != BDG_LOCAL) {
603 			struct mbuf *oldm = m ;
604 
605 			save_eh = *eh ; /* because it might change */
606 			m = bdg_forward_ptr(m, eh, bif); /* needs forwarding */
607 			/*
608 			 * Do not continue if bdg_forward_ptr() processed our
609 			 * packet (and cleared the mbuf pointer m) or if
610 			 * it dropped (m_free'd) the packet itself.
611 			 */
612 			if (m == NULL) {
613 			    if (bif == BDG_BCAST || bif == BDG_MCAST)
614 				printf("bdg_forward drop MULTICAST PKT\n");
615 			    return;
616 			}
617 			eh = &save_eh ;
618 		}
619 		if (bif == BDG_LOCAL
620 		    || bif == BDG_BCAST
621 		    || bif == BDG_MCAST)
622 			goto recvLocal;			/* receive locally */
623 
624 		/* If not local and not multicast, just drop it */
625 		if (m != NULL)
626 			m_freem(m);
627 		return;
628        }
629 
630 recvLocal:
631 	/* Continue with upper layer processing */
632 	ether_demux(ifp, eh, m);
633 }
634 
635 /*
636  * Upper layer processing for a received Ethernet packet.
637  */
638 void
639 ether_demux(ifp, eh, m)
640 	struct ifnet *ifp;
641 	struct ether_header *eh;
642 	struct mbuf *m;
643 {
644 	struct ifqueue *inq;
645 	u_short ether_type;
646 #if defined(NETATALK)
647 	register struct llc *l;
648 #endif
649 	struct ip_fw *rule = NULL;
650 
651 	/* Extract info from dummynet tag, ignore others */
652 	for (;m->m_type == MT_TAG; m = m->m_next)
653 		if (m->m_flags == PACKET_TAG_DUMMYNET) {
654 			rule = ((struct dn_pkt *)m)->rule;
655 			ifp = m->m_next->m_pkthdr.rcvif;
656 		}
657 
658 	if (rule)	/* packet was already bridged */
659 		goto post_stats;
660 
661     if (! (BDG_ACTIVE(ifp) ) )
662 	/* Discard packet if upper layers shouldn't see it because it was
663 	   unicast to a different Ethernet address. If the driver is working
664 	   properly, then this situation can only happen when the interface
665 	   is in promiscuous mode. */
666 	if ((ifp->if_flags & IFF_PROMISC) != 0
667 	    && (eh->ether_dhost[0] & 1) == 0
668 	    && bcmp(eh->ether_dhost,
669 	      IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0
670 	    && (ifp->if_ipending & IFF_PPROMISC) == 0) {
671 		m_freem(m);
672 		return;
673 	}
674 
675 	/* Discard packet if interface is not up */
676 	if ((ifp->if_flags & IFF_UP) == 0) {
677 		m_freem(m);
678 		return;
679 	}
680 	if (eh->ether_dhost[0] & 1) {
681 		if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
682 			 sizeof(etherbroadcastaddr)) == 0)
683 			m->m_flags |= M_BCAST;
684 		else
685 			m->m_flags |= M_MCAST;
686 	}
687 	if (m->m_flags & (M_BCAST|M_MCAST))
688 		ifp->if_imcasts++;
689 
690 post_stats:
691 	if (IPFW_LOADED && ether_ipfw != 0) {
692 		if (ether_ipfw_chk(&m, NULL, &rule, eh, 0 ) == 0) {
693 			if (m)
694 				m_freem(m);
695 			return;
696 		}
697 	}
698 
699 	ether_type = ntohs(eh->ether_type);
700 
701 	switch (ether_type) {
702 #ifdef INET
703 	case ETHERTYPE_IP:
704 		if (ipflow_fastforward(m))
705 			return;
706 		schednetisr(NETISR_IP);
707 		inq = &ipintrq;
708 		break;
709 
710 	case ETHERTYPE_ARP:
711 		if (ifp->if_flags & IFF_NOARP) {
712 			/* Discard packet if ARP is disabled on interface */
713 			m_freem(m);
714 			return;
715 		}
716 		schednetisr(NETISR_ARP);
717 		inq = &arpintrq;
718 		break;
719 #endif
720 #ifdef IPX
721 	case ETHERTYPE_IPX:
722 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
723 			return;
724 		schednetisr(NETISR_IPX);
725 		inq = &ipxintrq;
726 		break;
727 #endif
728 #ifdef INET6
729 	case ETHERTYPE_IPV6:
730 		schednetisr(NETISR_IPV6);
731 		inq = &ip6intrq;
732 		break;
733 #endif
734 #ifdef NS
735 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
736 		schednetisr(NETISR_NS);
737 		inq = &nsintrq;
738 		break;
739 
740 #endif /* NS */
741 #ifdef NETATALK
742         case ETHERTYPE_AT:
743                 schednetisr(NETISR_ATALK);
744                 inq = &atintrq1;
745                 break;
746         case ETHERTYPE_AARP:
747 		/* probably this should be done with a NETISR as well */
748                 aarpinput(IFP2AC(ifp), m); /* XXX */
749                 return;
750 #endif /* NETATALK */
751 	case ETHERTYPE_VLAN:
752 		/* XXX lock ? */
753 		if (vlan_input_p != NULL)
754 			(*vlan_input_p)(eh, m);
755 		else {
756 			m->m_pkthdr.rcvif->if_noproto++;
757 			m_freem(m);
758 		}
759 		/* XXX unlock ? */
760 		return;
761 	default:
762 #ifdef IPX
763 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
764 			return;
765 #endif /* IPX */
766 #ifdef NS
767 		checksum = mtod(m, ushort *);
768 		/* Novell 802.3 */
769 		if ((ether_type <= ETHERMTU) &&
770 			((*checksum == 0xffff) || (*checksum == 0xE0E0))){
771 			if(*checksum == 0xE0E0) {
772 				m->m_pkthdr.len -= 3;
773 				m->m_len -= 3;
774 				m->m_data += 3;
775 			}
776 				schednetisr(NETISR_NS);
777 				inq = &nsintrq;
778 				break;
779 		}
780 #endif /* NS */
781 #if defined(NETATALK)
782 		if (ether_type > ETHERMTU)
783 			goto dropanyway;
784 		l = mtod(m, struct llc *);
785 		switch (l->llc_dsap) {
786 		case LLC_SNAP_LSAP:
787 		    switch (l->llc_control) {
788 		    case LLC_UI:
789 			if (l->llc_ssap != LLC_SNAP_LSAP)
790 			    goto dropanyway;
791 
792 			if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
793 				   sizeof(at_org_code)) == 0 &&
794 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
795 			    inq = &atintrq2;
796 			    m_adj( m, sizeof( struct llc ));
797 			    schednetisr(NETISR_ATALK);
798 			    break;
799 			}
800 
801 			if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
802 				   sizeof(aarp_org_code)) == 0 &&
803 			     ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
804 			    m_adj( m, sizeof( struct llc ));
805 			    aarpinput(IFP2AC(ifp), m); /* XXX */
806 			    return;
807 			}
808 
809 		    default:
810 			goto dropanyway;
811 		    }
812 		    break;
813 		dropanyway:
814 		default:
815 			if (ng_ether_input_orphan_p != NULL)
816 				(*ng_ether_input_orphan_p)(ifp, m, eh);
817 			else
818 				m_freem(m);
819 			return;
820 		}
821 #else /* NETATALK */
822 		if (ng_ether_input_orphan_p != NULL)
823 			(*ng_ether_input_orphan_p)(ifp, m, eh);
824 		else
825 			m_freem(m);
826 		return;
827 #endif /* NETATALK */
828 	}
829 
830 	(void) IF_HANDOFF(inq, m, NULL);
831 }
832 
833 /*
834  * Perform common duties while attaching to interface list
835  */
836 void
837 ether_ifattach(ifp, bpf)
838 	register struct ifnet *ifp;
839 	int bpf;
840 {
841 	register struct ifaddr *ifa;
842 	register struct sockaddr_dl *sdl;
843 
844 	ifp->if_type = IFT_ETHER;
845 	ifp->if_addrlen = 6;
846 	ifp->if_hdrlen = 14;
847 	if_attach(ifp);
848 	ifp->if_mtu = ETHERMTU;
849 	ifp->if_resolvemulti = ether_resolvemulti;
850 	if (ifp->if_baudrate == 0)
851 	    ifp->if_baudrate = 10000000;
852 	ifa = ifnet_addrs[ifp->if_index - 1];
853 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
854 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
855 	sdl->sdl_type = IFT_ETHER;
856 	sdl->sdl_alen = ifp->if_addrlen;
857 	bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
858 	if (bpf)
859 		bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
860 	if (ng_ether_attach_p != NULL)
861 		(*ng_ether_attach_p)(ifp);
862 	if (BDG_LOADED)
863 		bdgtakeifaces_ptr();
864 }
865 
866 /*
867  * Perform common duties while detaching an Ethernet interface
868  */
869 void
870 ether_ifdetach(ifp, bpf)
871 	struct ifnet *ifp;
872 	int bpf;
873 {
874 	if (ng_ether_detach_p != NULL)
875 		(*ng_ether_detach_p)(ifp);
876 	if (bpf)
877 		bpfdetach(ifp);
878 	if_detach(ifp);
879 	if (BDG_LOADED)
880 		bdgtakeifaces_ptr();
881 }
882 
883 SYSCTL_DECL(_net_link);
884 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
885 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
886 	    &ether_ipfw,0,"Pass ether pkts through firewall");
887 
888 int
889 ether_ioctl(ifp, command, data)
890 	struct ifnet *ifp;
891 	int command;
892 	caddr_t data;
893 {
894 	struct ifaddr *ifa = (struct ifaddr *) data;
895 	struct ifreq *ifr = (struct ifreq *) data;
896 	int error = 0;
897 
898 	switch (command) {
899 	case SIOCSIFADDR:
900 		ifp->if_flags |= IFF_UP;
901 
902 		switch (ifa->ifa_addr->sa_family) {
903 #ifdef INET
904 		case AF_INET:
905 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
906 			arp_ifinit(ifp, ifa);
907 			break;
908 #endif
909 #ifdef IPX
910 		/*
911 		 * XXX - This code is probably wrong
912 		 */
913 		case AF_IPX:
914 			{
915 			register struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
916 			struct arpcom *ac = IFP2AC(ifp);
917 
918 			if (ipx_nullhost(*ina))
919 				ina->x_host =
920 				    *(union ipx_host *)
921 			            ac->ac_enaddr;
922 			else {
923 				bcopy((caddr_t) ina->x_host.c_host,
924 				      (caddr_t) ac->ac_enaddr,
925 				      sizeof(ac->ac_enaddr));
926 			}
927 
928 			/*
929 			 * Set new address
930 			 */
931 			ifp->if_init(ifp->if_softc);
932 			break;
933 			}
934 #endif
935 #ifdef NS
936 		/*
937 		 * XXX - This code is probably wrong
938 		 */
939 		case AF_NS:
940 		{
941 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
942 			struct arpcom *ac = IFP2AC(ifp);
943 
944 			if (ns_nullhost(*ina))
945 				ina->x_host =
946 				    *(union ns_host *) (ac->ac_enaddr);
947 			else {
948 				bcopy((caddr_t) ina->x_host.c_host,
949 				      (caddr_t) ac->ac_enaddr,
950 				      sizeof(ac->ac_enaddr));
951 			}
952 
953 			/*
954 			 * Set new address
955 			 */
956 			ifp->if_init(ifp->if_softc);
957 			break;
958 		}
959 #endif
960 		default:
961 			ifp->if_init(ifp->if_softc);
962 			break;
963 		}
964 		break;
965 
966 	case SIOCGIFADDR:
967 		{
968 			struct sockaddr *sa;
969 
970 			sa = (struct sockaddr *) & ifr->ifr_data;
971 			bcopy(IFP2AC(ifp)->ac_enaddr,
972 			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
973 		}
974 		break;
975 
976 	case SIOCSIFMTU:
977 		/*
978 		 * Set the interface MTU.
979 		 */
980 		if (ifr->ifr_mtu > ETHERMTU) {
981 			error = EINVAL;
982 		} else {
983 			ifp->if_mtu = ifr->ifr_mtu;
984 		}
985 		break;
986 	}
987 	return (error);
988 }
989 
990 int
991 ether_resolvemulti(ifp, llsa, sa)
992 	struct ifnet *ifp;
993 	struct sockaddr **llsa;
994 	struct sockaddr *sa;
995 {
996 	struct sockaddr_dl *sdl;
997 	struct sockaddr_in *sin;
998 #ifdef INET6
999 	struct sockaddr_in6 *sin6;
1000 #endif
1001 	u_char *e_addr;
1002 
1003 	switch(sa->sa_family) {
1004 	case AF_LINK:
1005 		/*
1006 		 * No mapping needed. Just check that it's a valid MC address.
1007 		 */
1008 		sdl = (struct sockaddr_dl *)sa;
1009 		e_addr = LLADDR(sdl);
1010 		if ((e_addr[0] & 1) != 1)
1011 			return EADDRNOTAVAIL;
1012 		*llsa = 0;
1013 		return 0;
1014 
1015 #ifdef INET
1016 	case AF_INET:
1017 		sin = (struct sockaddr_in *)sa;
1018 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1019 			return EADDRNOTAVAIL;
1020 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1021 		       M_WAITOK|M_ZERO);
1022 		sdl->sdl_len = sizeof *sdl;
1023 		sdl->sdl_family = AF_LINK;
1024 		sdl->sdl_index = ifp->if_index;
1025 		sdl->sdl_type = IFT_ETHER;
1026 		sdl->sdl_alen = ETHER_ADDR_LEN;
1027 		e_addr = LLADDR(sdl);
1028 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1029 		*llsa = (struct sockaddr *)sdl;
1030 		return 0;
1031 #endif
1032 #ifdef INET6
1033 	case AF_INET6:
1034 		sin6 = (struct sockaddr_in6 *)sa;
1035 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1036 			/*
1037 			 * An IP6 address of 0 means listen to all
1038 			 * of the Ethernet multicast address used for IP6.
1039 			 * (This is used for multicast routers.)
1040 			 */
1041 			ifp->if_flags |= IFF_ALLMULTI;
1042 			*llsa = 0;
1043 			return 0;
1044 		}
1045 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1046 			return EADDRNOTAVAIL;
1047 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1048 		       M_WAITOK|M_ZERO);
1049 		sdl->sdl_len = sizeof *sdl;
1050 		sdl->sdl_family = AF_LINK;
1051 		sdl->sdl_index = ifp->if_index;
1052 		sdl->sdl_type = IFT_ETHER;
1053 		sdl->sdl_alen = ETHER_ADDR_LEN;
1054 		e_addr = LLADDR(sdl);
1055 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1056 		*llsa = (struct sockaddr *)sdl;
1057 		return 0;
1058 #endif
1059 
1060 	default:
1061 		/*
1062 		 * Well, the text isn't quite right, but it's the name
1063 		 * that counts...
1064 		 */
1065 		return EAFNOSUPPORT;
1066 	}
1067 }
1068