if_ethersubr.c revision 167716
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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)if_ethersubr.c	8.1 (Berkeley) 6/10/93
30 * $FreeBSD: head/sys/net/if_ethersubr.c 167716 2007-03-19 18:39:36Z bms $
31 */
32
33#include "opt_atalk.h"
34#include "opt_inet.h"
35#include "opt_inet6.h"
36#include "opt_ipx.h"
37#include "opt_mac.h"
38#include "opt_netgraph.h"
39#include "opt_carp.h"
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/malloc.h>
45#include <sys/module.h>
46#include <sys/mbuf.h>
47#include <sys/random.h>
48#include <sys/socket.h>
49#include <sys/sockio.h>
50#include <sys/sysctl.h>
51
52#include <net/if.h>
53#include <net/if_arp.h>
54#include <net/netisr.h>
55#include <net/route.h>
56#include <net/if_llc.h>
57#include <net/if_dl.h>
58#include <net/if_types.h>
59#include <net/bpf.h>
60#include <net/ethernet.h>
61#include <net/if_bridgevar.h>
62#include <net/if_vlan_var.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 DEV_CARP
76#include <netinet/ip_carp.h>
77#endif
78
79#ifdef IPX
80#include <netipx/ipx.h>
81#include <netipx/ipx_if.h>
82#endif
83int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
84int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
85		struct sockaddr *dst, short *tp, int *hlen);
86
87#ifdef NETATALK
88#include <netatalk/at.h>
89#include <netatalk/at_var.h>
90#include <netatalk/at_extern.h>
91
92#define llc_snap_org_code llc_un.type_snap.org_code
93#define llc_snap_ether_type llc_un.type_snap.ether_type
94
95extern u_char	at_org_code[3];
96extern u_char	aarp_org_code[3];
97#endif /* NETATALK */
98
99#include <security/mac/mac_framework.h>
100
101/* netgraph node hooks for ng_ether(4) */
102void	(*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
103void	(*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
104int	(*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
105void	(*ng_ether_attach_p)(struct ifnet *ifp);
106void	(*ng_ether_detach_p)(struct ifnet *ifp);
107
108void	(*vlan_input_p)(struct ifnet *, struct mbuf *);
109
110/* if_bridge(4) support */
111struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
112int	(*bridge_output_p)(struct ifnet *, struct mbuf *,
113		struct sockaddr *, struct rtentry *);
114void	(*bridge_dn_p)(struct mbuf *, struct ifnet *);
115
116static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
117			{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
118
119static	int ether_resolvemulti(struct ifnet *, struct sockaddr **,
120		struct sockaddr *);
121
122/* XXX: should be in an arp support file, not here */
123MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
124
125#define	ETHER_IS_BROADCAST(addr) \
126	(bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
127
128#define senderr(e) do { error = (e); goto bad;} while (0)
129
130#if defined(INET) || defined(INET6)
131int
132ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
133	struct ip_fw **rule, int shared);
134static int ether_ipfw;
135#endif
136
137/*
138 * Ethernet output routine.
139 * Encapsulate a packet of type family for the local net.
140 * Use trailer local net encapsulation if enough data in first
141 * packet leaves a multiple of 512 bytes of data in remainder.
142 */
143int
144ether_output(struct ifnet *ifp, struct mbuf *m,
145	struct sockaddr *dst, struct rtentry *rt0)
146{
147	short type;
148	int error, hdrcmplt = 0;
149	u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
150	struct ether_header *eh;
151	int loop_copy = 1;
152	int hlen;	/* link layer header length */
153
154#ifdef MAC
155	error = mac_check_ifnet_transmit(ifp, m);
156	if (error)
157		senderr(error);
158#endif
159
160	if (ifp->if_flags & IFF_MONITOR)
161		senderr(ENETDOWN);
162	if (!((ifp->if_flags & IFF_UP) &&
163	    (ifp->if_drv_flags & IFF_DRV_RUNNING)))
164		senderr(ENETDOWN);
165
166	hlen = ETHER_HDR_LEN;
167	switch (dst->sa_family) {
168#ifdef INET
169	case AF_INET:
170		error = arpresolve(ifp, rt0, m, dst, edst);
171		if (error)
172			return (error == EWOULDBLOCK ? 0 : error);
173		type = htons(ETHERTYPE_IP);
174		break;
175	case AF_ARP:
176	{
177		struct arphdr *ah;
178		ah = mtod(m, struct arphdr *);
179		ah->ar_hrd = htons(ARPHRD_ETHER);
180
181		loop_copy = 0; /* if this is for us, don't do it */
182
183		switch(ntohs(ah->ar_op)) {
184		case ARPOP_REVREQUEST:
185		case ARPOP_REVREPLY:
186			type = htons(ETHERTYPE_REVARP);
187			break;
188		case ARPOP_REQUEST:
189		case ARPOP_REPLY:
190		default:
191			type = htons(ETHERTYPE_ARP);
192			break;
193		}
194
195		if (m->m_flags & M_BCAST)
196			bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
197		else
198			bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
199
200	}
201	break;
202#endif
203#ifdef INET6
204	case AF_INET6:
205		error = nd6_storelladdr(ifp, rt0, m, dst, (u_char *)edst);
206		if (error)
207			return error;
208		type = htons(ETHERTYPE_IPV6);
209		break;
210#endif
211#ifdef IPX
212	case AF_IPX:
213		if (ef_outputp) {
214		    error = ef_outputp(ifp, &m, dst, &type, &hlen);
215		    if (error)
216			goto bad;
217		} else
218		    type = htons(ETHERTYPE_IPX);
219		bcopy((caddr_t)&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
220		    (caddr_t)edst, sizeof (edst));
221		break;
222#endif
223#ifdef NETATALK
224	case AF_APPLETALK:
225	  {
226	    struct at_ifaddr *aa;
227
228	    if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL)
229		    senderr(EHOSTUNREACH); /* XXX */
230	    if (!aarpresolve(ifp, m, (struct sockaddr_at *)dst, edst))
231		    return (0);
232	    /*
233	     * In the phase 2 case, need to prepend an mbuf for the llc header.
234	     */
235	    if ( aa->aa_flags & AFA_PHASE2 ) {
236		struct llc llc;
237
238		M_PREPEND(m, LLC_SNAPFRAMELEN, M_DONTWAIT);
239		if (m == NULL)
240			senderr(ENOBUFS);
241		llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
242		llc.llc_control = LLC_UI;
243		bcopy(at_org_code, llc.llc_snap_org_code, sizeof(at_org_code));
244		llc.llc_snap_ether_type = htons( ETHERTYPE_AT );
245		bcopy(&llc, mtod(m, caddr_t), LLC_SNAPFRAMELEN);
246		type = htons(m->m_pkthdr.len);
247		hlen = LLC_SNAPFRAMELEN + ETHER_HDR_LEN;
248	    } else {
249		type = htons(ETHERTYPE_AT);
250	    }
251	    break;
252	  }
253#endif /* NETATALK */
254
255	case pseudo_AF_HDRCMPLT:
256		hdrcmplt = 1;
257		eh = (struct ether_header *)dst->sa_data;
258		(void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
259		/* FALLTHROUGH */
260
261	case AF_UNSPEC:
262		loop_copy = 0; /* if this is for us, don't do it */
263		eh = (struct ether_header *)dst->sa_data;
264		(void)memcpy(edst, eh->ether_dhost, sizeof (edst));
265		type = eh->ether_type;
266		break;
267
268	default:
269		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
270		senderr(EAFNOSUPPORT);
271	}
272
273	/*
274	 * Add local net header.  If no space in first mbuf,
275	 * allocate another.
276	 */
277	M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
278	if (m == NULL)
279		senderr(ENOBUFS);
280	eh = mtod(m, struct ether_header *);
281	(void)memcpy(&eh->ether_type, &type,
282		sizeof(eh->ether_type));
283	(void)memcpy(eh->ether_dhost, edst, sizeof (edst));
284	if (hdrcmplt)
285		(void)memcpy(eh->ether_shost, esrc,
286			sizeof(eh->ether_shost));
287	else
288		(void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
289			sizeof(eh->ether_shost));
290
291	/*
292	 * If a simplex interface, and the packet is being sent to our
293	 * Ethernet address or a broadcast address, loopback a copy.
294	 * XXX To make a simplex device behave exactly like a duplex
295	 * device, we should copy in the case of sending to our own
296	 * ethernet address (thus letting the original actually appear
297	 * on the wire). However, we don't do that here for security
298	 * reasons and compatibility with the original behavior.
299	 */
300	if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
301	    m_tag_find(m, PACKET_TAG_PF_ROUTED, NULL) == NULL) {
302		int csum_flags = 0;
303
304		if (m->m_pkthdr.csum_flags & CSUM_IP)
305			csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
306		if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
307			csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
308
309		if (m->m_flags & M_BCAST) {
310			struct mbuf *n;
311
312			/*
313			 * Because if_simloop() modifies the packet, we need a
314			 * writable copy through m_dup() instead of a readonly
315			 * one as m_copy[m] would give us. The alternative would
316			 * be to modify if_simloop() to handle the readonly mbuf,
317			 * but performancewise it is mostly equivalent (trading
318			 * extra data copying vs. extra locking).
319			 *
320			 * XXX This is a local workaround.  A number of less
321			 * often used kernel parts suffer from the same bug.
322			 * See PR kern/105943 for a proposed general solution.
323			 */
324			if ((n = m_dup(m, M_DONTWAIT)) != NULL) {
325				n->m_pkthdr.csum_flags |= csum_flags;
326				if (csum_flags & CSUM_DATA_VALID)
327					n->m_pkthdr.csum_data = 0xffff;
328				(void)if_simloop(ifp, n, dst->sa_family, hlen);
329			} else
330				ifp->if_iqdrops++;
331		} else if (bcmp(eh->ether_dhost, eh->ether_shost,
332				ETHER_ADDR_LEN) == 0) {
333			m->m_pkthdr.csum_flags |= csum_flags;
334			if (csum_flags & CSUM_DATA_VALID)
335				m->m_pkthdr.csum_data = 0xffff;
336			(void) if_simloop(ifp, m, dst->sa_family, hlen);
337			return (0);	/* XXX */
338		}
339	}
340
341       /*
342	* Bridges require special output handling.
343	*/
344	if (ifp->if_bridge) {
345		BRIDGE_OUTPUT(ifp, m, error);
346		return (error);
347	}
348
349#ifdef DEV_CARP
350	if (ifp->if_carp &&
351	    (error = carp_output(ifp, m, dst, NULL)))
352		goto bad;
353#endif
354
355	/* Handle ng_ether(4) processing, if any */
356	if (IFP2AC(ifp)->ac_netgraph != NULL) {
357		KASSERT(ng_ether_output_p != NULL,
358		    ("ng_ether_output_p is NULL"));
359		if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
360bad:			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 */
378int
379ether_output_frame(struct ifnet *ifp, struct mbuf *m)
380{
381	int error;
382#if defined(INET) || defined(INET6)
383	struct ip_fw *rule = ip_dn_claim_rule(m);
384
385	if (IPFW_LOADED && ether_ipfw != 0) {
386		if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
387			if (m) {
388				m_freem(m);
389				return EACCES;	/* pkt dropped */
390			} else
391				return 0;	/* consumed e.g. in a pipe */
392		}
393	}
394#endif
395
396	/*
397	 * Queue message on interface, update output statistics if
398	 * successful, and start output if interface not yet active.
399	 */
400	IFQ_HANDOFF(ifp, m, error);
401	return (error);
402}
403
404#if defined(INET) || defined(INET6)
405/*
406 * ipfw processing for ethernet packets (in and out).
407 * The second parameter is NULL from ether_demux, and ifp from
408 * ether_output_frame.
409 */
410int
411ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
412	struct ip_fw **rule, int shared)
413{
414	struct ether_header *eh;
415	struct ether_header save_eh;
416	struct mbuf *m;
417	int i;
418	struct ip_fw_args args;
419
420	if (*rule != NULL && fw_one_pass)
421		return 1; /* dummynet packet, already partially processed */
422
423	/*
424	 * I need some amt of data to be contiguous, and in case others need
425	 * the packet (shared==1) also better be in the first mbuf.
426	 */
427	m = *m0;
428	i = min( m->m_pkthdr.len, max_protohdr);
429	if ( shared || m->m_len < i) {
430		m = m_pullup(m, i);
431		if (m == NULL) {
432			*m0 = m;
433			return 0;
434		}
435	}
436	eh = mtod(m, struct ether_header *);
437	save_eh = *eh;			/* save copy for restore below */
438	m_adj(m, ETHER_HDR_LEN);	/* strip ethernet header */
439
440	args.m = m;		/* the packet we are looking at		*/
441	args.oif = dst;		/* destination, if any			*/
442	args.rule = *rule;	/* matching rule to restart		*/
443	args.next_hop = NULL;	/* we do not support forward yet	*/
444	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
445	args.inp = NULL;	/* used by ipfw uid/gid/jail rules	*/
446	i = ip_fw_chk_ptr(&args);
447	m = args.m;
448	if (m != NULL) {
449		/*
450		 * Restore Ethernet header, as needed, in case the
451		 * mbuf chain was replaced by ipfw.
452		 */
453		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
454		if (m == NULL) {
455			*m0 = m;
456			return 0;
457		}
458		if (eh != mtod(m, struct ether_header *))
459			bcopy(&save_eh, mtod(m, struct ether_header *),
460				ETHER_HDR_LEN);
461	}
462	*m0 = m;
463	*rule = args.rule;
464
465	if (i == IP_FW_DENY) /* drop */
466		return 0;
467
468	KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL"));
469
470	if (i == IP_FW_PASS) /* a PASS rule.  */
471		return 1;
472
473	if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
474		/*
475		 * Pass the pkt to dummynet, which consumes it.
476		 * If shared, make a copy and keep the original.
477		 */
478		if (shared) {
479			m = m_copypacket(m, M_DONTWAIT);
480			if (m == NULL)
481				return 0;
482		} else {
483			/*
484			 * Pass the original to dummynet and
485			 * nothing back to the caller
486			 */
487			*m0 = NULL ;
488		}
489		ip_dn_io_ptr(m, dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
490		return 0;
491	}
492	/*
493	 * XXX at some point add support for divert/forward actions.
494	 * If none of the above matches, we have to drop the pkt.
495	 */
496	return 0;
497}
498#endif
499
500/*
501 * Process a received Ethernet packet; the packet is in the
502 * mbuf chain m with the ethernet header at the front.
503 */
504static void
505ether_input(struct ifnet *ifp, struct mbuf *m)
506{
507	struct ether_header *eh;
508	u_short etype;
509
510	if ((ifp->if_flags & IFF_UP) == 0) {
511		m_freem(m);
512		return;
513	}
514#ifdef DIAGNOSTIC
515	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
516		if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
517		m_freem(m);
518		return;
519	}
520#endif
521	/*
522	 * Do consistency checks to verify assumptions
523	 * made by code past this point.
524	 */
525	if ((m->m_flags & M_PKTHDR) == 0) {
526		if_printf(ifp, "discard frame w/o packet header\n");
527		ifp->if_ierrors++;
528		m_freem(m);
529		return;
530	}
531	if (m->m_len < ETHER_HDR_LEN) {
532		/* XXX maybe should pullup? */
533		if_printf(ifp, "discard frame w/o leading ethernet "
534				"header (len %u pkt len %u)\n",
535				m->m_len, m->m_pkthdr.len);
536		ifp->if_ierrors++;
537		m_freem(m);
538		return;
539	}
540	eh = mtod(m, struct ether_header *);
541	etype = ntohs(eh->ether_type);
542	if (m->m_pkthdr.len >
543	    ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
544		if_printf(ifp, "discard oversize frame "
545				"(ether type %x flags %x len %u > max %lu)\n",
546				etype, m->m_flags, m->m_pkthdr.len,
547				ETHER_MAX_FRAME(ifp, etype,
548						m->m_flags & M_HASFCS));
549		ifp->if_ierrors++;
550		m_freem(m);
551		return;
552	}
553	if (m->m_pkthdr.rcvif == NULL) {
554		if_printf(ifp, "discard frame w/o interface pointer\n");
555		ifp->if_ierrors++;
556		m_freem(m);
557		return;
558	}
559#ifdef DIAGNOSTIC
560	if (m->m_pkthdr.rcvif != ifp) {
561		if_printf(ifp, "Warning, frame marked as received on %s\n",
562			m->m_pkthdr.rcvif->if_xname);
563	}
564#endif
565
566	if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
567		if (ETHER_IS_BROADCAST(eh->ether_dhost))
568			m->m_flags |= M_BCAST;
569		else
570			m->m_flags |= M_MCAST;
571		ifp->if_imcasts++;
572	}
573
574#ifdef MAC
575	/*
576	 * Tag the mbuf with an appropriate MAC label before any other
577	 * consumers can get to it.
578	 */
579	mac_create_mbuf_from_ifnet(ifp, m);
580#endif
581
582	/*
583	 * Give bpf a chance at the packet.
584	 */
585	ETHER_BPF_MTAP(ifp, m);
586
587	/*
588	 * If the CRC is still on the packet, trim it off. We do this once
589	 * and once only in case we are re-entered. Nothing else on the
590	 * Ethernet receive path expects to see the FCS.
591	 */
592	if (m->m_flags & M_HASFCS) {
593		m_adj(m, -ETHER_CRC_LEN);
594		m->m_flags &= ~M_HASFCS;
595	}
596
597	ifp->if_ibytes += m->m_pkthdr.len;
598
599	/* Allow monitor mode to claim this frame, after stats are updated. */
600	if (ifp->if_flags & IFF_MONITOR) {
601		m_freem(m);
602		return;
603	}
604
605	/*
606	 * If the hardware did not process an 802.1Q tag, do this now,
607	 * to allow 802.1P priority frames to be passed to the main input
608	 * path correctly.
609	 * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
610	 */
611	if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
612		struct ether_vlan_header *evl;
613
614		if (m->m_len < sizeof(*evl) &&
615		    (m = m_pullup(m, sizeof(*evl))) == NULL) {
616			if_printf(ifp, "cannot pullup VLAN header\n");
617			ifp->if_ierrors++;
618			m_freem(m);
619			return;
620		}
621
622		evl = mtod(m, struct ether_vlan_header *);
623		m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
624		m->m_flags |= M_VLANTAG;
625
626		bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
627		    ETHER_HDR_LEN - ETHER_TYPE_LEN);
628		m_adj(m, ETHER_VLAN_ENCAP_LEN);
629	}
630
631	/* Allow ng_ether(4) to claim this frame. */
632	if (IFP2AC(ifp)->ac_netgraph != NULL) {
633		KASSERT(ng_ether_input_p != NULL,
634		    ("%s: ng_ether_input_p is NULL", __func__));
635		m->m_flags &= ~M_PROMISC;
636		(*ng_ether_input_p)(ifp, &m);
637		if (m == NULL)
638			return;
639	}
640
641	/*
642	 * Allow if_bridge(4) to claim this frame.
643	 * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
644	 * and the frame should be delivered locally.
645	 */
646	if (ifp->if_bridge != NULL) {
647		m->m_flags &= ~M_PROMISC;
648		BRIDGE_INPUT(ifp, m);
649		if (m == NULL)
650			return;
651	}
652
653#ifdef DEV_CARP
654	/*
655	 * Clear M_PROMISC on frame so that carp(4) will see it when the
656	 * mbuf flows up to Layer 3.
657	 * FreeBSD's implementation of carp(4) uses the inprotosw
658	 * to dispatch IPPROTO_CARP. carp(4) also allocates its own
659	 * Ethernet addresses of the form 00:00:5e:00:01:xx, which
660	 * is outside the scope of the M_PROMISC test below.
661	 * TODO: Maintain a hash table of ethernet addresses other than
662	 * ether_dhost which may be active on this ifp.
663	 */
664	if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost)) {
665		m->m_flags &= ~M_PROMISC;
666	} else
667#endif
668	{
669		/*
670		 * If the frame was received promiscuously, set the
671		 * M_PROMISC flag on the mbuf chain. The frame may need to
672		 * be seen by the rest of the Ethernet input path in case of
673		 * re-entry (e.g. bridge, vlan, netgraph) but should not be
674		 * seen by upper protocol layers.
675		 */
676		if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
677		    (ifp->if_flags & IFF_PROMISC) != 0 &&
678		    !bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN))
679			m->m_flags |= M_PROMISC;
680	}
681
682	/* First chunk of an mbuf contains good entropy */
683	if (harvest.ethernet)
684		random_harvest(m, 16, 3, 0, RANDOM_NET);
685
686	ether_demux(ifp, m);
687}
688
689/*
690 * Upper layer processing for a received Ethernet packet.
691 */
692void
693ether_demux(struct ifnet *ifp, struct mbuf *m)
694{
695	struct ether_header *eh;
696	int isr;
697	u_short ether_type;
698#if defined(NETATALK)
699	struct llc *l;
700#endif
701
702	KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
703
704#if defined(INET) || defined(INET6)
705	/*
706	 * Allow dummynet and/or ipfw to claim the frame.
707	 * Do not do this for PROMISC frames in case we are re-entered.
708	 */
709	if (IPFW_LOADED && ether_ipfw != 0 && !(m->m_flags & M_PROMISC)) {
710		struct ip_fw *rule = ip_dn_claim_rule(m);
711
712		if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
713			if (m)
714				m_freem(m);	/* dropped; free mbuf chain */
715			return;			/* consumed */
716		}
717	}
718#endif
719	eh = mtod(m, struct ether_header *);
720	ether_type = ntohs(eh->ether_type);
721
722	/*
723	 * If this frame has a VLAN tag other than 0, call vlan_input()
724	 * if its module is loaded. Otherwise, drop.
725	 */
726	if ((m->m_flags & M_VLANTAG) &&
727	    EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
728		if (ifp->if_vlantrunk == NULL) {
729			ifp->if_noproto++;
730			m_freem(m);
731			return;
732		}
733		KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
734		    __func__));
735		/* Clear before possibly re-entering ether_input(). */
736		m->m_flags &= ~M_PROMISC;
737		(*vlan_input_p)(ifp, m);
738		return;
739	}
740
741	/*
742	 * Pass promiscuously received frames to the upper layer if the user
743	 * requested this by setting IFF_PPROMISC. Otherwise, drop them.
744	 */
745	if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
746		m_freem(m);
747		return;
748	}
749
750	/*
751	 * Reset layer specific mbuf flags to avoid confusing upper layers.
752	 * Strip off Ethernet header.
753	 */
754	m->m_flags &= ~M_VLANTAG;
755	m->m_flags &= ~(M_PROTOFLAGS);
756	m_adj(m, ETHER_HDR_LEN);
757
758	/*
759	 * Dispatch frame to upper layer.
760	 */
761	switch (ether_type) {
762#ifdef INET
763	case ETHERTYPE_IP:
764		if ((m = ip_fastforward(m)) == NULL)
765			return;
766		isr = NETISR_IP;
767		break;
768
769	case ETHERTYPE_ARP:
770		if (ifp->if_flags & IFF_NOARP) {
771			/* Discard packet if ARP is disabled on interface */
772			m_freem(m);
773			return;
774		}
775		isr = NETISR_ARP;
776		break;
777#endif
778#ifdef IPX
779	case ETHERTYPE_IPX:
780		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
781			return;
782		isr = NETISR_IPX;
783		break;
784#endif
785#ifdef INET6
786	case ETHERTYPE_IPV6:
787		isr = NETISR_IPV6;
788		break;
789#endif
790#ifdef NETATALK
791	case ETHERTYPE_AT:
792		isr = NETISR_ATALK1;
793		break;
794	case ETHERTYPE_AARP:
795		isr = NETISR_AARP;
796		break;
797#endif /* NETATALK */
798	default:
799#ifdef IPX
800		if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
801			return;
802#endif /* IPX */
803#if defined(NETATALK)
804		if (ether_type > ETHERMTU)
805			goto discard;
806		l = mtod(m, struct llc *);
807		if (l->llc_dsap == LLC_SNAP_LSAP &&
808		    l->llc_ssap == LLC_SNAP_LSAP &&
809		    l->llc_control == LLC_UI) {
810			if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
811			    sizeof(at_org_code)) == 0 &&
812			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
813				m_adj(m, LLC_SNAPFRAMELEN);
814				isr = NETISR_ATALK2;
815				break;
816			}
817			if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
818			    sizeof(aarp_org_code)) == 0 &&
819			    ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
820				m_adj(m, LLC_SNAPFRAMELEN);
821				isr = NETISR_AARP;
822				break;
823			}
824		}
825#endif /* NETATALK */
826		goto discard;
827	}
828	netisr_dispatch(isr, m);
829	return;
830
831discard:
832	/*
833	 * Packet is to be discarded.  If netgraph is present,
834	 * hand the packet to it for last chance processing;
835	 * otherwise dispose of it.
836	 */
837	if (IFP2AC(ifp)->ac_netgraph != NULL) {
838		KASSERT(ng_ether_input_orphan_p != NULL,
839		    ("ng_ether_input_orphan_p is NULL"));
840		/*
841		 * Put back the ethernet header so netgraph has a
842		 * consistent view of inbound packets.
843		 */
844		M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
845		(*ng_ether_input_orphan_p)(ifp, m);
846		return;
847	}
848	m_freem(m);
849}
850
851/*
852 * Convert Ethernet address to printable (loggable) representation.
853 * This routine is for compatibility; it's better to just use
854 *
855 *	printf("%6D", <pointer to address>, ":");
856 *
857 * since there's no static buffer involved.
858 */
859char *
860ether_sprintf(const u_char *ap)
861{
862	static char etherbuf[18];
863	snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
864	return (etherbuf);
865}
866
867/*
868 * Perform common duties while attaching to interface list
869 */
870void
871ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
872{
873	int i;
874	struct ifaddr *ifa;
875	struct sockaddr_dl *sdl;
876
877	ifp->if_addrlen = ETHER_ADDR_LEN;
878	ifp->if_hdrlen = ETHER_HDR_LEN;
879	if_attach(ifp);
880	ifp->if_mtu = ETHERMTU;
881	ifp->if_output = ether_output;
882	ifp->if_input = ether_input;
883	ifp->if_resolvemulti = ether_resolvemulti;
884	if (ifp->if_baudrate == 0)
885		ifp->if_baudrate = IF_Mbps(10);		/* just a default */
886	ifp->if_broadcastaddr = etherbroadcastaddr;
887
888	ifa = ifp->if_addr;
889	KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
890	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
891	sdl->sdl_type = IFT_ETHER;
892	sdl->sdl_alen = ifp->if_addrlen;
893	bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
894
895	bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
896	if (ng_ether_attach_p != NULL)
897		(*ng_ether_attach_p)(ifp);
898
899	/* Announce Ethernet MAC address if non-zero. */
900	for (i = 0; i < ifp->if_addrlen; i++)
901		if (lla[i] != 0)
902			break;
903	if (i != ifp->if_addrlen)
904		if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
905	if (debug_mpsafenet && (ifp->if_flags & IFF_NEEDSGIANT) != 0)
906		if_printf(ifp, "if_start running deferred for Giant\n");
907}
908
909/*
910 * Perform common duties while detaching an Ethernet interface
911 */
912void
913ether_ifdetach(struct ifnet *ifp)
914{
915	if (IFP2AC(ifp)->ac_netgraph != NULL) {
916		KASSERT(ng_ether_detach_p != NULL,
917		    ("ng_ether_detach_p is NULL"));
918		(*ng_ether_detach_p)(ifp);
919	}
920
921	bpfdetach(ifp);
922	if_detach(ifp);
923}
924
925SYSCTL_DECL(_net_link);
926SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
927#if defined(INET) || defined(INET6)
928SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
929	    &ether_ipfw,0,"Pass ether pkts through firewall");
930#endif
931
932#if 0
933/*
934 * This is for reference.  We have a table-driven version
935 * of the little-endian crc32 generator, which is faster
936 * than the double-loop.
937 */
938uint32_t
939ether_crc32_le(const uint8_t *buf, size_t len)
940{
941	size_t i;
942	uint32_t crc;
943	int bit;
944	uint8_t data;
945
946	crc = 0xffffffff;	/* initial value */
947
948	for (i = 0; i < len; i++) {
949		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
950			carry = (crc ^ data) & 1;
951			crc >>= 1;
952			if (carry)
953				crc = (crc ^ ETHER_CRC_POLY_LE);
954	}
955
956	return (crc);
957}
958#else
959uint32_t
960ether_crc32_le(const uint8_t *buf, size_t len)
961{
962	static const uint32_t crctab[] = {
963		0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
964		0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
965		0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
966		0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
967	};
968	size_t i;
969	uint32_t crc;
970
971	crc = 0xffffffff;	/* initial value */
972
973	for (i = 0; i < len; i++) {
974		crc ^= buf[i];
975		crc = (crc >> 4) ^ crctab[crc & 0xf];
976		crc = (crc >> 4) ^ crctab[crc & 0xf];
977	}
978
979	return (crc);
980}
981#endif
982
983uint32_t
984ether_crc32_be(const uint8_t *buf, size_t len)
985{
986	size_t i;
987	uint32_t crc, carry;
988	int bit;
989	uint8_t data;
990
991	crc = 0xffffffff;	/* initial value */
992
993	for (i = 0; i < len; i++) {
994		for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
995			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
996			crc <<= 1;
997			if (carry)
998				crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
999		}
1000	}
1001
1002	return (crc);
1003}
1004
1005int
1006ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
1007{
1008	struct ifaddr *ifa = (struct ifaddr *) data;
1009	struct ifreq *ifr = (struct ifreq *) data;
1010	int error = 0;
1011
1012	switch (command) {
1013	case SIOCSIFADDR:
1014		ifp->if_flags |= IFF_UP;
1015
1016		switch (ifa->ifa_addr->sa_family) {
1017#ifdef INET
1018		case AF_INET:
1019			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
1020			arp_ifinit(ifp, ifa);
1021			break;
1022#endif
1023#ifdef IPX
1024		/*
1025		 * XXX - This code is probably wrong
1026		 */
1027		case AF_IPX:
1028			{
1029			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1030
1031			if (ipx_nullhost(*ina))
1032				ina->x_host =
1033				    *(union ipx_host *)
1034				    IF_LLADDR(ifp);
1035			else {
1036				bcopy((caddr_t) ina->x_host.c_host,
1037				      (caddr_t) IF_LLADDR(ifp),
1038				      ETHER_ADDR_LEN);
1039			}
1040
1041			/*
1042			 * Set new address
1043			 */
1044			ifp->if_init(ifp->if_softc);
1045			break;
1046			}
1047#endif
1048		default:
1049			ifp->if_init(ifp->if_softc);
1050			break;
1051		}
1052		break;
1053
1054	case SIOCGIFADDR:
1055		{
1056			struct sockaddr *sa;
1057
1058			sa = (struct sockaddr *) & ifr->ifr_data;
1059			bcopy(IF_LLADDR(ifp),
1060			      (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1061		}
1062		break;
1063
1064	case SIOCSIFMTU:
1065		/*
1066		 * Set the interface MTU.
1067		 */
1068		if (ifr->ifr_mtu > ETHERMTU) {
1069			error = EINVAL;
1070		} else {
1071			ifp->if_mtu = ifr->ifr_mtu;
1072		}
1073		break;
1074	default:
1075		error = EINVAL;			/* XXX netbsd has ENOTTY??? */
1076		break;
1077	}
1078	return (error);
1079}
1080
1081static int
1082ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1083	struct sockaddr *sa)
1084{
1085	struct sockaddr_dl *sdl;
1086#ifdef INET
1087	struct sockaddr_in *sin;
1088#endif
1089#ifdef INET6
1090	struct sockaddr_in6 *sin6;
1091#endif
1092	u_char *e_addr;
1093
1094	switch(sa->sa_family) {
1095	case AF_LINK:
1096		/*
1097		 * No mapping needed. Just check that it's a valid MC address.
1098		 */
1099		sdl = (struct sockaddr_dl *)sa;
1100		e_addr = LLADDR(sdl);
1101		if (!ETHER_IS_MULTICAST(e_addr))
1102			return EADDRNOTAVAIL;
1103		*llsa = 0;
1104		return 0;
1105
1106#ifdef INET
1107	case AF_INET:
1108		sin = (struct sockaddr_in *)sa;
1109		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1110			return EADDRNOTAVAIL;
1111		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1112		       M_NOWAIT|M_ZERO);
1113		if (sdl == NULL)
1114			return ENOMEM;
1115		sdl->sdl_len = sizeof *sdl;
1116		sdl->sdl_family = AF_LINK;
1117		sdl->sdl_index = ifp->if_index;
1118		sdl->sdl_type = IFT_ETHER;
1119		sdl->sdl_alen = ETHER_ADDR_LEN;
1120		e_addr = LLADDR(sdl);
1121		ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1122		*llsa = (struct sockaddr *)sdl;
1123		return 0;
1124#endif
1125#ifdef INET6
1126	case AF_INET6:
1127		sin6 = (struct sockaddr_in6 *)sa;
1128		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1129			/*
1130			 * An IP6 address of 0 means listen to all
1131			 * of the Ethernet multicast address used for IP6.
1132			 * (This is used for multicast routers.)
1133			 */
1134			ifp->if_flags |= IFF_ALLMULTI;
1135			*llsa = 0;
1136			return 0;
1137		}
1138		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1139			return EADDRNOTAVAIL;
1140		MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1141		       M_NOWAIT|M_ZERO);
1142		if (sdl == NULL)
1143			return (ENOMEM);
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
1164static void*
1165ether_alloc(u_char type, struct ifnet *ifp)
1166{
1167	struct arpcom	*ac;
1168
1169	ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1170	ac->ac_ifp = ifp;
1171
1172	return (ac);
1173}
1174
1175static void
1176ether_free(void *com, u_char type)
1177{
1178
1179	free(com, M_ARPCOM);
1180}
1181
1182static int
1183ether_modevent(module_t mod, int type, void *data)
1184{
1185
1186	switch (type) {
1187	case MOD_LOAD:
1188		if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1189		break;
1190	case MOD_UNLOAD:
1191		if_deregister_com_alloc(IFT_ETHER);
1192		break;
1193	default:
1194		return EOPNOTSUPP;
1195	}
1196
1197	return (0);
1198}
1199
1200static moduledata_t ether_mod = {
1201	"ether",
1202	ether_modevent,
1203	0
1204};
1205
1206void
1207ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1208{
1209	struct ether_vlan_header vlan;
1210	struct mbuf mv, mb;
1211
1212	KASSERT((m->m_flags & M_VLANTAG) != 0,
1213	    ("%s: vlan information not present", __func__));
1214	KASSERT(m->m_len >= sizeof(struct ether_header),
1215	    ("%s: mbuf not large enough for header", __func__));
1216	bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1217	vlan.evl_proto = vlan.evl_encap_proto;
1218	vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1219	vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1220	m->m_len -= sizeof(struct ether_header);
1221	m->m_data += sizeof(struct ether_header);
1222	/*
1223	 * If a data link has been supplied by the caller, then we will need to
1224	 * re-create a stack allocated mbuf chain with the following structure:
1225	 *
1226	 * (1) mbuf #1 will contain the supplied data link
1227	 * (2) mbuf #2 will contain the vlan header
1228	 * (3) mbuf #3 will contain the original mbuf's packet data
1229	 *
1230	 * Otherwise, submit the packet and vlan header via bpf_mtap2().
1231	 */
1232	if (data != NULL) {
1233		mv.m_next = m;
1234		mv.m_data = (caddr_t)&vlan;
1235		mv.m_len = sizeof(vlan);
1236		mb.m_next = &mv;
1237		mb.m_data = data;
1238		mb.m_len = dlen;
1239		bpf_mtap(bp, &mb);
1240	} else
1241		bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1242	m->m_len += sizeof(struct ether_header);
1243	m->m_data -= sizeof(struct ether_header);
1244}
1245
1246DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1247MODULE_VERSION(ether, 1);
1248