xref: /dragonfly/sys/net/if_ethersubr.c (revision 2d8a3be7)
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.7 2003/09/15 23:38:13 hsu 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/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 <net/ipfw/ip_fw.h>
69 #include <net/dummynet/ip_dummynet.h>
70 #endif
71 #ifdef INET6
72 #include <netinet6/nd6.h>
73 #endif
74 
75 #ifdef IPX
76 #include <netproto/ipx/ipx.h>
77 #include <netproto/ipx/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 <netproto/atalk/at.h>
93 #include <netproto/atalk/at_var.h>
94 #include <netproto/atalk/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 	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 	struct rtentry *rt;
152 	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 			netisr_dispatch(NETISR_NS, m);
276 			return (error);
277 		}
278 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost, sizeof(edst))){
279 			m->m_flags |= M_BCAST;
280 		}
281 		break;
282 #endif /* NS */
283 
284 	case pseudo_AF_HDRCMPLT:
285 		hdrcmplt = 1;
286 		eh = (struct ether_header *)dst->sa_data;
287 		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
288 		/* FALLTHROUGH */
289 
290 	case AF_UNSPEC:
291 		loop_copy = -1; /* if this is for us, don't do it */
292 		eh = (struct ether_header *)dst->sa_data;
293  		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
294 		type = eh->ether_type;
295 		break;
296 
297 	default:
298 		printf("%s%d: can't handle af%d\n", ifp->if_name, ifp->if_unit,
299 			dst->sa_family);
300 		senderr(EAFNOSUPPORT);
301 	}
302 
303 	/*
304 	 * Add local net header.  If no space in first mbuf,
305 	 * allocate another.
306 	 */
307 	M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
308 	if (m == 0)
309 		senderr(ENOBUFS);
310 	eh = mtod(m, struct ether_header *);
311 	(void)memcpy(&eh->ether_type, &type,
312 		sizeof(eh->ether_type));
313  	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
314 	if (hdrcmplt)
315 		(void)memcpy(eh->ether_shost, esrc,
316 			sizeof(eh->ether_shost));
317 	else
318 		(void)memcpy(eh->ether_shost, ac->ac_enaddr,
319 			sizeof(eh->ether_shost));
320 
321 	/*
322 	 * If a simplex interface, and the packet is being sent to our
323 	 * Ethernet address or a broadcast address, loopback a copy.
324 	 * XXX To make a simplex device behave exactly like a duplex
325 	 * device, we should copy in the case of sending to our own
326 	 * ethernet address (thus letting the original actually appear
327 	 * on the wire). However, we don't do that here for security
328 	 * reasons and compatibility with the original behavior.
329 	 */
330 	if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
331 		int csum_flags = 0;
332 
333 		if (m->m_pkthdr.csum_flags & CSUM_IP)
334 			csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
335 		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
336 			csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
337 		if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
338 			struct mbuf *n;
339 
340 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
341 				n->m_pkthdr.csum_flags |= csum_flags;
342 				if (csum_flags & CSUM_DATA_VALID)
343 					n->m_pkthdr.csum_data = 0xffff;
344 				(void)if_simloop(ifp, n, dst->sa_family, hlen);
345 			} else
346 				ifp->if_iqdrops++;
347 		} else if (bcmp(eh->ether_dhost,
348 		    eh->ether_shost, ETHER_ADDR_LEN) == 0) {
349 			m->m_pkthdr.csum_flags |= csum_flags;
350 			if (csum_flags & CSUM_DATA_VALID)
351 				m->m_pkthdr.csum_data = 0xffff;
352 			(void) if_simloop(ifp, m, dst->sa_family, hlen);
353 			return (0);	/* XXX */
354 		}
355 	}
356 
357 	/* Handle ng_ether(4) processing, if any */
358 	if (ng_ether_output_p != NULL) {
359 		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
360 bad:			if (m != NULL)
361 				m_freem(m);
362 			return (error);
363 		}
364 		if (m == NULL)
365 			return (0);
366 	}
367 
368 	/* Continue with link-layer output */
369 	return ether_output_frame(ifp, m);
370 }
371 
372 /*
373  * Ethernet link layer output routine to send a raw frame to the device.
374  *
375  * This assumes that the 14 byte Ethernet header is present and contiguous
376  * in the first mbuf (if BRIDGE'ing).
377  */
378 int
379 ether_output_frame(ifp, m)
380 	struct ifnet *ifp;
381 	struct mbuf *m;
382 {
383 	int error = 0;
384 	int s;
385 	struct ip_fw *rule = NULL;
386 
387 	/* Extract info from dummynet tag, ignore others */
388 	for (; m->m_type == MT_TAG; m = m->m_next)
389 		if (m->m_flags == PACKET_TAG_DUMMYNET)
390 			rule = ((struct dn_pkt *)m)->rule;
391 
392 	if (rule)	/* packet was already bridged */
393 		goto no_bridge;
394 
395 	if (BDG_ACTIVE(ifp) ) {
396 		struct ether_header *eh; /* a ptr suffices */
397 
398 		m->m_pkthdr.rcvif = NULL;
399 		eh = mtod(m, struct ether_header *);
400 		m_adj(m, ETHER_HDR_LEN);
401 		m = bdg_forward_ptr(m, eh, ifp);
402 		if (m != NULL)
403 			m_freem(m);
404 		return (0);
405 	}
406 
407 no_bridge:
408 	s = splimp();
409 	if (IPFW_LOADED && ether_ipfw != 0) {
410 		struct ether_header save_eh, *eh;
411 
412 		eh = mtod(m, struct ether_header *);
413 		save_eh = *eh;
414 		m_adj(m, ETHER_HDR_LEN);
415 		if (ether_ipfw_chk(&m, ifp, &rule, eh, 0) == 0) {
416 			if (m) {
417 				m_freem(m);
418 				return ENOBUFS;	/* pkt dropped */
419 			} else
420 				return 0;	/* consumed e.g. in a pipe */
421 		}
422 		/* packet was ok, restore the ethernet header */
423 		if ( (void *)(eh + 1) == (void *)m->m_data) {
424 			m->m_data -= ETHER_HDR_LEN ;
425 			m->m_len += ETHER_HDR_LEN ;
426 			m->m_pkthdr.len += ETHER_HDR_LEN ;
427 		} else {
428 			M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
429 			if (m == NULL) /* nope... */
430 				return ENOBUFS;
431 			bcopy(&save_eh, mtod(m, struct ether_header *),
432 			    ETHER_HDR_LEN);
433 		}
434 	}
435 
436 	/*
437 	 * Queue message on interface, update output statistics if
438 	 * successful, and start output if interface not yet active.
439 	 */
440 	if (!IF_HANDOFF(&ifp->if_snd, m, ifp))
441 		error = ENOBUFS;
442 	splx(s);
443 	return (error);
444 }
445 
446 /*
447  * ipfw processing for ethernet packets (in and out).
448  * The second parameter is NULL from ether_demux, and ifp from
449  * ether_output_frame. This section of code could be used from
450  * bridge.c as well as long as we use some extra info
451  * to distinguish that case from ether_output_frame();
452  */
453 int
454 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
455 	struct ip_fw **rule, struct ether_header *eh, int shared)
456 {
457 	struct ether_header save_eh = *eh;	/* might be a ptr in m */
458 	int i;
459 	struct ip_fw_args args;
460 
461 	if (*rule != NULL && fw_one_pass)
462 		return 1; /* dummynet packet, already partially processed */
463 
464 	/*
465 	 * I need some amt of data to be contiguous, and in case others need
466 	 * the packet (shared==1) also better be in the first mbuf.
467 	 */
468 	i = min( (*m0)->m_pkthdr.len, max_protohdr);
469 	if ( shared || (*m0)->m_len < i) {
470 		*m0 = m_pullup(*m0, i);
471 		if (*m0 == NULL)
472 			return 0;
473 	}
474 
475 	args.m = *m0;		/* the packet we are looking at		*/
476 	args.oif = dst;		/* destination, if any			*/
477 	args.divert_rule = 0;	/* we do not support divert yet		*/
478 	args.rule = *rule;	/* matching rule to restart		*/
479 	args.next_hop = NULL;	/* we do not support forward yet	*/
480 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
481 	i = ip_fw_chk_ptr(&args);
482 	*m0 = args.m;
483 	*rule = args.rule;
484 
485 	if ( (i & IP_FW_PORT_DENY_FLAG) || *m0 == 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 		struct mbuf *m ;
497 
498 		if (shared) {
499 			m = m_copypacket(*m0, M_DONTWAIT);
500 			if (m == NULL)
501 				return 0;
502 		} else {
503 			m = *m0 ; /* pass the original to dummynet */
504 			*m0 = NULL ; /* and nothing back to the caller */
505 		}
506 		/*
507 		 * Prepend the header, optimize for the common case of
508 		 * eh pointing into the mbuf.
509 		 */
510 		if ( (void *)(eh + 1) == (void *)m->m_data) {
511 			m->m_data -= ETHER_HDR_LEN ;
512 			m->m_len += ETHER_HDR_LEN ;
513 			m->m_pkthdr.len += ETHER_HDR_LEN ;
514 		} else {
515 			M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
516 			if (m == NULL) /* nope... */
517 				return 0;
518 			bcopy(&save_eh, mtod(m, struct ether_header *),
519 			    ETHER_HDR_LEN);
520 		}
521 		ip_dn_io_ptr(m, (i & 0xffff),
522 			dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
523 		return 0;
524 	}
525 	/*
526 	 * XXX at some point add support for divert/forward actions.
527 	 * If none of the above matches, we have to drop the pkt.
528 	 */
529 	return 0;
530 }
531 
532 /*
533  * Process a received Ethernet packet. We have two different interfaces:
534  * one (conventional) assumes the packet in the mbuf, with the ethernet
535  * header provided separately in *eh. The second one (new) has everything
536  * in the mbuf, and we can tell it because eh == NULL.
537  * The caller MUST MAKE SURE that there are at least
538  * sizeof(struct ether_header) bytes in the first mbuf.
539  *
540  * This allows us to concentrate in one place a bunch of code which
541  * is replicated in all device drivers. Also, many functions called
542  * from ether_input() try to put the eh back into the mbuf, so we
543  * can later propagate the 'contiguous packet' interface to them,
544  * and handle the old interface just here.
545  *
546  * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or
547  * cluster, right before m_data. So be very careful when working on m,
548  * as you could destroy *eh !!
549  *
550  * First we perform any link layer operations, then continue
551  * to the upper layers with ether_demux().
552  */
553 void
554 ether_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
555 {
556 	struct ether_header save_eh;
557 
558 	if (eh == NULL) {
559 		if (m->m_len < sizeof(struct ether_header)) {
560 			/* XXX error in the caller. */
561 			m_freem(m);
562 			return;
563 		}
564 		m->m_pkthdr.rcvif = ifp;
565 		eh = mtod(m, struct ether_header *);
566 		m->m_data += sizeof(struct ether_header);
567 		m->m_len -= sizeof(struct ether_header);
568 		m->m_pkthdr.len = m->m_len;
569 	}
570 
571 	/* Check for a BPF tap */
572 	if (ifp->if_bpf != NULL) {
573 		struct m_hdr mh;
574 
575 		/* This kludge is OK; BPF treats the "mbuf" as read-only */
576 		mh.mh_next = m;
577 		mh.mh_data = (char *)eh;
578 		mh.mh_len = ETHER_HDR_LEN;
579 		bpf_mtap(ifp, (struct mbuf *)&mh);
580 	}
581 
582 	ifp->if_ibytes += m->m_pkthdr.len + sizeof (*eh);
583 
584 	/* Handle ng_ether(4) processing, if any */
585 	if (ng_ether_input_p != NULL) {
586 		(*ng_ether_input_p)(ifp, &m, eh);
587 		if (m == NULL)
588 			return;
589 	}
590 
591 	/* Check for bridging mode */
592 	if (BDG_ACTIVE(ifp) ) {
593 		struct ifnet *bif;
594 
595 		/* Check with bridging code */
596 		if ((bif = bridge_in_ptr(ifp, eh)) == BDG_DROP) {
597 			m_freem(m);
598 			return;
599 		}
600 		if (bif != BDG_LOCAL) {
601 			save_eh = *eh ; /* because it might change */
602 			m = bdg_forward_ptr(m, eh, bif); /* needs forwarding */
603 			/*
604 			 * Do not continue if bdg_forward_ptr() processed our
605 			 * packet (and cleared the mbuf pointer m) or if
606 			 * it dropped (m_free'd) the packet itself.
607 			 */
608 			if (m == NULL) {
609 			    if (bif == BDG_BCAST || bif == BDG_MCAST)
610 				printf("bdg_forward drop MULTICAST PKT\n");
611 			    return;
612 			}
613 			eh = &save_eh ;
614 		}
615 		if (bif == BDG_LOCAL
616 		    || bif == BDG_BCAST
617 		    || bif == BDG_MCAST)
618 			goto recvLocal;			/* receive locally */
619 
620 		/* If not local and not multicast, just drop it */
621 		if (m != NULL)
622 			m_freem(m);
623 		return;
624        }
625 
626 recvLocal:
627 	/* Continue with upper layer processing */
628 	ether_demux(ifp, eh, m);
629 }
630 
631 /*
632  * Upper layer processing for a received Ethernet packet.
633  */
634 void
635 ether_demux(ifp, eh, m)
636 	struct ifnet *ifp;
637 	struct ether_header *eh;
638 	struct mbuf *m;
639 {
640 	int isr;
641 	u_short ether_type;
642 #if defined(NETATALK)
643 	struct llc *l;
644 #endif
645 	struct ip_fw *rule = NULL;
646 
647 	/* Extract info from dummynet tag, ignore others */
648 	for (;m->m_type == MT_TAG; m = m->m_next)
649 		if (m->m_flags == PACKET_TAG_DUMMYNET) {
650 			rule = ((struct dn_pkt *)m)->rule;
651 			ifp = m->m_next->m_pkthdr.rcvif;
652 		}
653 
654 	if (rule)	/* packet was already bridged */
655 		goto post_stats;
656 
657     if (! (BDG_ACTIVE(ifp) ) )
658 	/* Discard packet if upper layers shouldn't see it because it was
659 	   unicast to a different Ethernet address. If the driver is working
660 	   properly, then this situation can only happen when the interface
661 	   is in promiscuous mode. */
662 	if ((ifp->if_flags & IFF_PROMISC) != 0
663 	    && (eh->ether_dhost[0] & 1) == 0
664 	    && bcmp(eh->ether_dhost,
665 	      IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0
666 	    && (ifp->if_ipending & IFF_PPROMISC) == 0) {
667 		m_freem(m);
668 		return;
669 	}
670 
671 	/* Discard packet if interface is not up */
672 	if ((ifp->if_flags & IFF_UP) == 0) {
673 		m_freem(m);
674 		return;
675 	}
676 	if (eh->ether_dhost[0] & 1) {
677 		if (bcmp((caddr_t)etherbroadcastaddr, (caddr_t)eh->ether_dhost,
678 			 sizeof(etherbroadcastaddr)) == 0)
679 			m->m_flags |= M_BCAST;
680 		else
681 			m->m_flags |= M_MCAST;
682 	}
683 	if (m->m_flags & (M_BCAST|M_MCAST))
684 		ifp->if_imcasts++;
685 
686 post_stats:
687 	if (IPFW_LOADED && ether_ipfw != 0) {
688 		if (ether_ipfw_chk(&m, NULL, &rule, eh, 0 ) == 0) {
689 			if (m)
690 				m_freem(m);
691 			return;
692 		}
693 	}
694 
695 	ether_type = ntohs(eh->ether_type);
696 
697 	switch (ether_type) {
698 #ifdef INET
699 	case ETHERTYPE_IP:
700 		if (ipflow_fastforward(m))
701 			return;
702 		isr = NETISR_IP;
703 		break;
704 
705 	case ETHERTYPE_ARP:
706 		if (ifp->if_flags & IFF_NOARP) {
707 			/* Discard packet if ARP is disabled on interface */
708 			m_freem(m);
709 			return;
710 		}
711 		isr = NETISR_ARP;
712 		break;
713 #endif
714 #ifdef IPX
715 	case ETHERTYPE_IPX:
716 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
717 			return;
718 		isr = NETISR_IPX;
719 		break;
720 #endif
721 #ifdef INET6
722 	case ETHERTYPE_IPV6:
723 		isr = NETISR_IPV6;
724 		break;
725 #endif
726 #ifdef NS
727 	case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
728 		isr = NETISR_NS;
729 		break;
730 
731 #endif /* NS */
732 #ifdef NETATALK
733         case ETHERTYPE_AT:
734 		isr = NETISR_ATALK1;
735                 break;
736         case ETHERTYPE_AARP:
737 		isr = NETISR_AARP;
738                 break;
739 #endif /* NETATALK */
740 	case ETHERTYPE_VLAN:
741 		/* XXX lock ? */
742 		if (vlan_input_p != NULL)
743 			(*vlan_input_p)(eh, m);
744 		else {
745 			m->m_pkthdr.rcvif->if_noproto++;
746 			m_freem(m);
747 		}
748 		/* XXX unlock ? */
749 		return;
750 	default:
751 #ifdef IPX
752 		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
753 			return;
754 #endif /* IPX */
755 #ifdef NS
756 		checksum = mtod(m, ushort *);
757 		/* Novell 802.3 */
758 		if ((ether_type <= ETHERMTU) &&
759 		    ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
760 			if (*checksum == 0xE0E0) {
761 				m->m_pkthdr.len -= 3;
762 				m->m_len -= 3;
763 				m->m_data += 3;
764 			}
765 			isr = NETISR_NS;
766 			break;
767 		}
768 #endif /* NS */
769 #ifdef NETATALK
770 		if (ether_type > ETHERMTU)
771 			goto dropanyway;
772 		l = mtod(m, struct llc *);
773 		if (l->llc_dsap == LLC_SNAP_LSAP &&
774 		    l->llc_ssap == LLC_SNAP_LSAP &&
775 		    l->llc_control == LLC_UI) {
776                         if (Bcmp(&(l->llc_snap_org_code)[0], at_org_code,
777 			    sizeof(at_org_code)) == 0 &&
778 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
779 				m_adj(m, sizeof(struct llc));
780 				isr = NETISR_ATALK2;
781 				break;
782 			}
783 			if (Bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
784 			    sizeof(aarp_org_code)) == 0 &&
785 			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
786 				m_adj(m, sizeof(struct llc));
787 				isr = NETISR_AARP;
788 				break;
789 			}
790 		}
791 dropanyway:
792 #endif /* NETATALK */
793 		if (ng_ether_input_orphan_p != NULL)
794 			(*ng_ether_input_orphan_p)(ifp, m, eh);
795 		else
796 			m_freem(m);
797 		return;
798 	}
799 	netisr_dispatch(isr, m);
800 }
801 
802 /*
803  * Perform common duties while attaching to interface list
804  */
805 void
806 ether_ifattach(ifp, bpf)
807 	struct ifnet *ifp;
808 	int bpf;
809 {
810 	struct ifaddr *ifa;
811 	struct sockaddr_dl *sdl;
812 
813 	ifp->if_type = IFT_ETHER;
814 	ifp->if_addrlen = 6;
815 	ifp->if_hdrlen = 14;
816 	if_attach(ifp);
817 	ifp->if_mtu = ETHERMTU;
818 	ifp->if_resolvemulti = ether_resolvemulti;
819 	if (ifp->if_baudrate == 0)
820 	    ifp->if_baudrate = 10000000;
821 	ifa = ifnet_addrs[ifp->if_index - 1];
822 	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
823 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
824 	sdl->sdl_type = IFT_ETHER;
825 	sdl->sdl_alen = ifp->if_addrlen;
826 	bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
827 	if (bpf)
828 		bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
829 	if (ng_ether_attach_p != NULL)
830 		(*ng_ether_attach_p)(ifp);
831 	if (BDG_LOADED)
832 		bdgtakeifaces_ptr();
833 }
834 
835 /*
836  * Perform common duties while detaching an Ethernet interface
837  */
838 void
839 ether_ifdetach(ifp, bpf)
840 	struct ifnet *ifp;
841 	int bpf;
842 {
843 	if (ng_ether_detach_p != NULL)
844 		(*ng_ether_detach_p)(ifp);
845 	if (bpf)
846 		bpfdetach(ifp);
847 	if_detach(ifp);
848 	if (BDG_LOADED)
849 		bdgtakeifaces_ptr();
850 }
851 
852 SYSCTL_DECL(_net_link);
853 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
854 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
855 	    &ether_ipfw,0,"Pass ether pkts through firewall");
856 
857 int
858 ether_ioctl(ifp, command, data)
859 	struct ifnet *ifp;
860 	int command;
861 	caddr_t data;
862 {
863 	struct ifaddr *ifa = (struct ifaddr *) data;
864 	struct ifreq *ifr = (struct ifreq *) data;
865 	int error = 0;
866 
867 	switch (command) {
868 	case SIOCSIFADDR:
869 		ifp->if_flags |= IFF_UP;
870 
871 		switch (ifa->ifa_addr->sa_family) {
872 #ifdef INET
873 		case AF_INET:
874 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
875 			arp_ifinit(ifp, ifa);
876 			break;
877 #endif
878 #ifdef IPX
879 		/*
880 		 * XXX - This code is probably wrong
881 		 */
882 		case AF_IPX:
883 			{
884 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
885 			struct arpcom *ac = IFP2AC(ifp);
886 
887 			if (ipx_nullhost(*ina))
888 				ina->x_host =
889 				    *(union ipx_host *)
890 			            ac->ac_enaddr;
891 			else {
892 				bcopy((caddr_t) ina->x_host.c_host,
893 				      (caddr_t) ac->ac_enaddr,
894 				      sizeof(ac->ac_enaddr));
895 			}
896 
897 			/*
898 			 * Set new address
899 			 */
900 			ifp->if_init(ifp->if_softc);
901 			break;
902 			}
903 #endif
904 #ifdef NS
905 		/*
906 		 * XXX - This code is probably wrong
907 		 */
908 		case AF_NS:
909 		{
910 			struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
911 			struct arpcom *ac = IFP2AC(ifp);
912 
913 			if (ns_nullhost(*ina))
914 				ina->x_host =
915 				    *(union ns_host *) (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 	}
956 	return (error);
957 }
958 
959 int
960 ether_resolvemulti(ifp, llsa, sa)
961 	struct ifnet *ifp;
962 	struct sockaddr **llsa;
963 	struct sockaddr *sa;
964 {
965 	struct sockaddr_dl *sdl;
966 	struct sockaddr_in *sin;
967 #ifdef INET6
968 	struct sockaddr_in6 *sin6;
969 #endif
970 	u_char *e_addr;
971 
972 	switch(sa->sa_family) {
973 	case AF_LINK:
974 		/*
975 		 * No mapping needed. Just check that it's a valid MC address.
976 		 */
977 		sdl = (struct sockaddr_dl *)sa;
978 		e_addr = LLADDR(sdl);
979 		if ((e_addr[0] & 1) != 1)
980 			return EADDRNOTAVAIL;
981 		*llsa = 0;
982 		return 0;
983 
984 #ifdef INET
985 	case AF_INET:
986 		sin = (struct sockaddr_in *)sa;
987 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
988 			return EADDRNOTAVAIL;
989 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
990 		       M_WAITOK|M_ZERO);
991 		sdl->sdl_len = sizeof *sdl;
992 		sdl->sdl_family = AF_LINK;
993 		sdl->sdl_index = ifp->if_index;
994 		sdl->sdl_type = IFT_ETHER;
995 		sdl->sdl_alen = ETHER_ADDR_LEN;
996 		e_addr = LLADDR(sdl);
997 		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
998 		*llsa = (struct sockaddr *)sdl;
999 		return 0;
1000 #endif
1001 #ifdef INET6
1002 	case AF_INET6:
1003 		sin6 = (struct sockaddr_in6 *)sa;
1004 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1005 			/*
1006 			 * An IP6 address of 0 means listen to all
1007 			 * of the Ethernet multicast address used for IP6.
1008 			 * (This is used for multicast routers.)
1009 			 */
1010 			ifp->if_flags |= IFF_ALLMULTI;
1011 			*llsa = 0;
1012 			return 0;
1013 		}
1014 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1015 			return EADDRNOTAVAIL;
1016 		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1017 		       M_WAITOK|M_ZERO);
1018 		sdl->sdl_len = sizeof *sdl;
1019 		sdl->sdl_family = AF_LINK;
1020 		sdl->sdl_index = ifp->if_index;
1021 		sdl->sdl_type = IFT_ETHER;
1022 		sdl->sdl_alen = ETHER_ADDR_LEN;
1023 		e_addr = LLADDR(sdl);
1024 		ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1025 		*llsa = (struct sockaddr *)sdl;
1026 		return 0;
1027 #endif
1028 
1029 	default:
1030 		/*
1031 		 * Well, the text isn't quite right, but it's the name
1032 		 * that counts...
1033 		 */
1034 		return EAFNOSUPPORT;
1035 	}
1036 }
1037