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