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