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