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