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: src/sys/net/if_ethersubr.c,v 1.177.2.12 2006/03/04 09:24:30 oleg Exp $
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 (ng_ether_output_p != 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.len >
537 ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
538 if_printf(ifp, "discard oversize frame "
539 "(ether type %x flags %x len %u > max %lu)\n",
540 etype, m->m_flags, m->m_pkthdr.len,
541 ETHER_MAX_FRAME(ifp, etype,
542 m->m_flags & M_HASFCS));
543 ifp->if_ierrors++;
544 m_freem(m);
545 return;
546 }
547 if (m->m_pkthdr.rcvif == NULL) {
548 if_printf(ifp, "discard frame w/o interface pointer\n");
549 ifp->if_ierrors++;
550 m_freem(m);
551 return;
552 }
553 #ifdef DIAGNOSTIC
554 if (m->m_pkthdr.rcvif != ifp) {
555 if_printf(ifp, "Warning, frame marked as received on %s\n",
556 m->m_pkthdr.rcvif->if_xname);
557 }
558 #endif
559
560 #ifdef MAC
561 /*
562 * Tag the mbuf with an appropriate MAC label before any other
563 * consumers can get to it.
564 */
565 mac_create_mbuf_from_ifnet(ifp, m);
566 #endif
567
568 /*
569 * Give bpf a chance at the packet.
570 */
571 BPF_MTAP(ifp, m);
572
573 if (ifp->if_flags & IFF_MONITOR) {
574 /*
575 * Interface marked for monitoring; discard packet.
576 */
577 m_freem(m);
578 return;
579 }
580
581 /* If the CRC is still on the packet, trim it off. */
582 if (m->m_flags & M_HASFCS) {
583 m_adj(m, -ETHER_CRC_LEN);
584 m->m_flags &= ~M_HASFCS;
585 }
586
587 ifp->if_ibytes += m->m_pkthdr.len;
588
589 /* Handle ng_ether(4) processing, if any */
590 if (ng_ether_input_p != NULL) {
591 (*ng_ether_input_p)(ifp, &m);
592 if (m == NULL)
593 return;
594 }
595
596 /*
597 * Tap the packet off here for a bridge. bridge_input()
598 * will return NULL if it has consumed the packet, otherwise
599 * it gets processed as normal. Note that bridge_input()
600 * will always return the original packet if we need to
601 * process it locally.
602 */
603 if (ifp->if_bridge) {
604 BRIDGE_INPUT(ifp, m);
605 if (m == NULL)
606 return;
607 }
608
609 /* Check for bridging mode */
610 if (BDG_ACTIVE(ifp) ) {
611 struct ifnet *bif;
612
613 /*
614 * Check with bridging code to see how the packet
615 * should be handled. Possibilities are:
616 *
617 * BDG_BCAST broadcast
618 * BDG_MCAST multicast
619 * BDG_LOCAL for local address, don't forward
620 * BDG_DROP discard
621 * ifp forward only to specified interface(s)
622 *
623 * Non-local destinations are handled by passing the
624 * packet back to the bridge code.
625 */
626 bif = bridge_in_ptr(ifp, eh);
627 if (bif == BDG_DROP) { /* discard packet */
628 m_freem(m);
629 return;
630 }
631 if (bif != BDG_LOCAL) { /* non-local, forward */
632 m = bdg_forward_ptr(m, bif);
633 /*
634 * The bridge may consume the packet if it's not
635 * supposed to be passed up or if a problem occurred
636 * while doing its job. This is reflected by it
637 * returning a NULL mbuf pointer.
638 */
639 if (m == NULL) {
640 if (bif == BDG_BCAST || bif == BDG_MCAST)
641 if_printf(ifp,
642 "bridge dropped %s packet\n",
643 bif == BDG_BCAST ? "broadcast" :
644 "multicast");
645 return;
646 }
647 /*
648 * But in some cases the bridge may return the
649 * packet for us to free; sigh.
650 */
651 if (bif != BDG_BCAST && bif != BDG_MCAST) {
652 m_freem(m);
653 return;
654 }
655 }
656 }
657
658 /* First chunk of an mbuf contains good entropy */
659 if (harvest.ethernet)
660 random_harvest(m, 16, 3, 0, RANDOM_NET);
661 ether_demux(ifp, m);
662 }
663
664 /*
665 * Upper layer processing for a received Ethernet packet.
666 */
667 void
668 ether_demux(struct ifnet *ifp, struct mbuf *m)
669 {
670 struct ether_header *eh;
671 int isr;
672 u_short ether_type;
673 #if defined(NETATALK)
674 struct llc *l;
675 #endif
676 #if defined(INET) || defined(INET6)
677 struct ip_fw *rule = ip_dn_claim_rule(m);
678 #endif
679
680 KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer"));
681
682 eh = mtod(m, struct ether_header *);
683 ether_type = ntohs(eh->ether_type);
684
685 #if defined(INET) || defined(INET6)
686 if (rule) /* packet was already bridged */
687 goto post_stats;
688 #endif
689
690 if (!(BDG_ACTIVE(ifp)) && !(ifp->if_bridge) &&
691 !(ether_type == ETHERTYPE_VLAN && ifp->if_nvlans > 0)) {
692 #ifdef DEV_CARP
693 /*
694 * XXX: Okay, we need to call carp_forus() and - if it is for
695 * us jump over code that does the normal check
696 * "ac_enaddr == ether_dhost". The check sequence is a bit
697 * different from OpenBSD, so we jump over as few code as
698 * possible, to catch _all_ sanity checks. This needs
699 * evaluation, to see if the carp ether_dhost values break any
700 * of these checks!
701 */
702 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
703 goto pre_stats;
704 #endif
705 /*
706 * Discard packet if upper layers shouldn't see it because it
707 * was unicast to a different Ethernet address. If the driver
708 * is working properly, then this situation can only happen
709 * when the interface is in promiscuous mode.
710 *
711 * If VLANs are active, and this packet has a VLAN tag, do
712 * not drop it here but pass it on to the VLAN layer, to
713 * give them a chance to consider it as well (e. g. in case
714 * bridging is only active on a VLAN). They will drop it if
715 * it's undesired.
716 */
717 if ((ifp->if_flags & IFF_PROMISC) != 0
718 && (eh->ether_dhost[0] & 1) == 0
719 && bcmp(eh->ether_dhost,
720 IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN) != 0
721 && (ifp->if_flags & IFF_PPROMISC) == 0) {
722 m_freem(m);
723 return;
724 }
725 }
726
727 #ifdef DEV_CARP
728 pre_stats:
729 #endif
730 /* Discard packet if interface is not up */
731 if ((ifp->if_flags & IFF_UP) == 0) {
732 m_freem(m);
733 return;
734 }
735 if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
736 if (bcmp(etherbroadcastaddr, eh->ether_dhost,
737 sizeof(etherbroadcastaddr)) == 0)
738 m->m_flags |= M_BCAST;
739 else
740 m->m_flags |= M_MCAST;
741 }
742 if (m->m_flags & (M_BCAST|M_MCAST))
743 ifp->if_imcasts++;
744
745 #if defined(INET) || defined(INET6)
746 post_stats:
747 if (IPFW_LOADED && ether_ipfw != 0) {
748 if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
749 if (m)
750 m_freem(m);
751 return;
752 }
753 }
754 #endif
755
756 /*
757 * If VLANs are configured on the interface, check to
758 * see if the device performed the decapsulation and
759 * provided us with the tag.
760 */
761 if (ifp->if_nvlans &&
762 m_tag_locate(m, MTAG_VLAN, MTAG_VLAN_TAG, NULL) != NULL) {
763 /*
764 * vlan_input() will either recursively call ether_input()
765 * or drop the packet.
766 */
767 KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!"));
768 (*vlan_input_p)(ifp, m);
769 return;
770 }
771
772 /*
773 * Handle protocols that expect to have the Ethernet header
774 * (and possibly FCS) intact.
775 */
776 switch (ether_type) {
777 case ETHERTYPE_VLAN:
778 if (ifp->if_nvlans != 0) {
779 KASSERT(vlan_input_p,("ether_input: VLAN not loaded!"));
780 (*vlan_input_p)(ifp, m);
781 } else {
782 ifp->if_noproto++;
783 m_freem(m);
784 }
785 return;
786 }
787
788 /* Strip off Ethernet header. */
789 m_adj(m, ETHER_HDR_LEN);
790
791 /* If the CRC is still on the packet, trim it off. */
792 if (m->m_flags & M_HASFCS) {
793 m_adj(m, -ETHER_CRC_LEN);
794 m->m_flags &= ~M_HASFCS;
795 }
796
797 switch (ether_type) {
798 #ifdef INET
799 case ETHERTYPE_IP:
800 if (ip_fastforward(m))
801 return;
802 isr = NETISR_IP;
803 break;
804
805 case ETHERTYPE_ARP:
806 if (ifp->if_flags & IFF_NOARP) {
807 /* Discard packet if ARP is disabled on interface */
808 m_freem(m);
809 return;
810 }
811 isr = NETISR_ARP;
812 break;
813 #endif
814 #ifdef IPX
815 case ETHERTYPE_IPX:
816 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
817 return;
818 isr = NETISR_IPX;
819 break;
820 #endif
821 #ifdef INET6
822 case ETHERTYPE_IPV6:
823 isr = NETISR_IPV6;
824 break;
825 #endif
826 #ifdef NETATALK
827 case ETHERTYPE_AT:
828 isr = NETISR_ATALK1;
829 break;
830 case ETHERTYPE_AARP:
831 isr = NETISR_AARP;
832 break;
833 #endif /* NETATALK */
834 default:
835 #ifdef IPX
836 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
837 return;
838 #endif /* IPX */
839 #if defined(NETATALK)
840 if (ether_type > ETHERMTU)
841 goto discard;
842 l = mtod(m, struct llc *);
843 if (l->llc_dsap == LLC_SNAP_LSAP &&
844 l->llc_ssap == LLC_SNAP_LSAP &&
845 l->llc_control == LLC_UI) {
846 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
847 sizeof(at_org_code)) == 0 &&
848 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
849 m_adj(m, LLC_SNAPFRAMELEN);
850 isr = NETISR_ATALK2;
851 break;
852 }
853 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
854 sizeof(aarp_org_code)) == 0 &&
855 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
856 m_adj(m, LLC_SNAPFRAMELEN);
857 isr = NETISR_AARP;
858 break;
859 }
860 }
861 #endif /* NETATALK */
862 goto discard;
863 }
864 netisr_dispatch(isr, m);
865 return;
866
867 discard:
868 /*
869 * Packet is to be discarded. If netgraph is present,
870 * hand the packet to it for last chance processing;
871 * otherwise dispose of it.
872 */
873 if (ng_ether_input_orphan_p != NULL) {
874 /*
875 * Put back the ethernet header so netgraph has a
876 * consistent view of inbound packets.
877 */
878 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
879 (*ng_ether_input_orphan_p)(ifp, m);
880 return;
881 }
882 m_freem(m);
883 }
884
885 /*
886 * Convert Ethernet address to printable (loggable) representation.
887 * This routine is for compatibility; it's better to just use
888 *
889 * printf("%6D", <pointer to address>, ":");
890 *
891 * since there's no static buffer involved.
892 */
893 char *
894 ether_sprintf(const u_char *ap)
895 {
896 static char etherbuf[18];
897 snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
898 return (etherbuf);
899 }
900
901 /*
902 * Perform common duties while attaching to interface list
903 */
904 void
905 ether_ifattach(struct ifnet *ifp, const u_int8_t *llc)
906 {
907 int i;
908 struct ifaddr *ifa;
909 struct sockaddr_dl *sdl;
910
911 ifp->if_type = IFT_ETHER;
912 ifp->if_addrlen = ETHER_ADDR_LEN;
913 ifp->if_hdrlen = ETHER_HDR_LEN;
914 if_attach(ifp);
915 ifp->if_mtu = ETHERMTU;
916 ifp->if_output = ether_output;
917 ifp->if_input = ether_input;
918 ifp->if_resolvemulti = ether_resolvemulti;
919 if (ifp->if_baudrate == 0)
920 ifp->if_baudrate = IF_Mbps(10); /* just a default */
921 ifp->if_broadcastaddr = etherbroadcastaddr;
922
923 ifa = ifaddr_byindex(ifp->if_index);
924 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
925 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
926 sdl->sdl_type = IFT_ETHER;
927 sdl->sdl_alen = ifp->if_addrlen;
928 bcopy(llc, LLADDR(sdl), ifp->if_addrlen);
929 /*
930 * XXX: This doesn't belong here; we do it until
931 * XXX: all drivers are cleaned up
932 */
933 if (llc != IFP2AC(ifp)->ac_enaddr)
934 bcopy(llc, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
935
936 bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
937 if (ng_ether_attach_p != NULL)
938 (*ng_ether_attach_p)(ifp);
939 if (BDG_LOADED)
940 bdgtakeifaces_ptr();
941
942 /* Announce Ethernet MAC address if non-zero. */
943 for (i = 0; i < ifp->if_addrlen; i++)
944 if (llc[i] != 0)
945 break;
946 if (i != ifp->if_addrlen)
947 if_printf(ifp, "Ethernet address: %6D\n", llc, ":");
948 if (debug_mpsafenet && (ifp->if_flags & IFF_NEEDSGIANT) != 0)
949 if_printf(ifp, "if_start running deferred for Giant\n");
950 }
951
952 /*
953 * Perform common duties while detaching an Ethernet interface
954 */
955 void
956 ether_ifdetach(struct ifnet *ifp)
957 {
958 if (ng_ether_detach_p != NULL)
959 (*ng_ether_detach_p)(ifp);
960
961 bpfdetach(ifp);
962 if_detach(ifp);
963 if (BDG_LOADED)
964 bdgtakeifaces_ptr();
965 }
966
967 SYSCTL_DECL(_net_link);
968 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
969 #if defined(INET) || defined(INET6)
970 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
971 ðer_ipfw,0,"Pass ether pkts through firewall");
972 #endif
973
974 #if 0
975 /*
976 * This is for reference. We have a table-driven version
977 * of the little-endian crc32 generator, which is faster
978 * than the double-loop.
979 */
980 uint32_t
981 ether_crc32_le(const uint8_t *buf, size_t len)
982 {
983 size_t i;
984 uint32_t crc;
985 int bit;
986 uint8_t data;
987
988 crc = 0xffffffff; /* initial value */
989
990 for (i = 0; i < len; i++) {
991 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
992 carry = (crc ^ data) & 1;
993 crc >>= 1;
994 if (carry)
995 crc = (crc ^ ETHER_CRC_POLY_LE);
996 }
997
998 return (crc);
999 }
1000 #else
1001 uint32_t
1002 ether_crc32_le(const uint8_t *buf, size_t len)
1003 {
1004 static const uint32_t crctab[] = {
1005 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1006 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1007 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1008 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1009 };
1010 size_t i;
1011 uint32_t crc;
1012
1013 crc = 0xffffffff; /* initial value */
1014
1015 for (i = 0; i < len; i++) {
1016 crc ^= buf[i];
1017 crc = (crc >> 4) ^ crctab[crc & 0xf];
1018 crc = (crc >> 4) ^ crctab[crc & 0xf];
1019 }
1020
1021 return (crc);
1022 }
1023 #endif
1024
1025 uint32_t
1026 ether_crc32_be(const uint8_t *buf, size_t len)
1027 {
1028 size_t i;
1029 uint32_t crc, carry;
1030 int bit;
1031 uint8_t data;
1032
1033 crc = 0xffffffff; /* initial value */
1034
1035 for (i = 0; i < len; i++) {
1036 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
1037 carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
1038 crc <<= 1;
1039 if (carry)
1040 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1041 }
1042 }
1043
1044 return (crc);
1045 }
1046
1047 int
1048 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
1049 {
1050 struct ifaddr *ifa = (struct ifaddr *) data;
1051 struct ifreq *ifr = (struct ifreq *) data;
1052 int error = 0;
1053
1054 switch (command) {
1055 case SIOCSIFADDR:
1056 ifp->if_flags |= IFF_UP;
1057
1058 switch (ifa->ifa_addr->sa_family) {
1059 #ifdef INET
1060 case AF_INET:
1061 ifp->if_init(ifp->if_softc); /* before arpwhohas */
1062 arp_ifinit(ifp, ifa);
1063 break;
1064 #endif
1065 #ifdef IPX
1066 /*
1067 * XXX - This code is probably wrong
1068 */
1069 case AF_IPX:
1070 {
1071 struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1072 struct arpcom *ac = IFP2AC(ifp);
1073
1074 if (ipx_nullhost(*ina))
1075 ina->x_host =
1076 *(union ipx_host *)
1077 ac->ac_enaddr;
1078 else {
1079 bcopy((caddr_t) ina->x_host.c_host,
1080 (caddr_t) ac->ac_enaddr,
1081 sizeof(ac->ac_enaddr));
1082 }
1083
1084 /*
1085 * Set new address
1086 */
1087 ifp->if_init(ifp->if_softc);
1088 break;
1089 }
1090 #endif
1091 default:
1092 ifp->if_init(ifp->if_softc);
1093 break;
1094 }
1095 break;
1096
1097 case SIOCGIFADDR:
1098 {
1099 struct sockaddr *sa;
1100
1101 sa = (struct sockaddr *) & ifr->ifr_data;
1102 bcopy(IFP2AC(ifp)->ac_enaddr,
1103 (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1104 }
1105 break;
1106
1107 case SIOCSIFMTU:
1108 /*
1109 * Set the interface MTU.
1110 */
1111 if (ifr->ifr_mtu > ETHERMTU) {
1112 error = EINVAL;
1113 } else {
1114 ifp->if_mtu = ifr->ifr_mtu;
1115 }
1116 break;
1117 default:
1118 error = EINVAL; /* XXX netbsd has ENOTTY??? */
1119 break;
1120 }
1121 return (error);
1122 }
1123
1124 static int
1125 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1126 struct sockaddr *sa)
1127 {
1128 struct sockaddr_dl *sdl;
1129 #ifdef INET
1130 struct sockaddr_in *sin;
1131 #endif
1132 #ifdef INET6
1133 struct sockaddr_in6 *sin6;
1134 #endif
1135 u_char *e_addr;
1136
1137 switch(sa->sa_family) {
1138 case AF_LINK:
1139 /*
1140 * No mapping needed. Just check that it's a valid MC address.
1141 */
1142 sdl = (struct sockaddr_dl *)sa;
1143 e_addr = LLADDR(sdl);
1144 if (!ETHER_IS_MULTICAST(e_addr))
1145 return EADDRNOTAVAIL;
1146 *llsa = 0;
1147 return 0;
1148
1149 #ifdef INET
1150 case AF_INET:
1151 sin = (struct sockaddr_in *)sa;
1152 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1153 return EADDRNOTAVAIL;
1154 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1155 M_NOWAIT|M_ZERO);
1156 if (sdl == NULL)
1157 return ENOMEM;
1158 sdl->sdl_len = sizeof *sdl;
1159 sdl->sdl_family = AF_LINK;
1160 sdl->sdl_index = ifp->if_index;
1161 sdl->sdl_type = IFT_ETHER;
1162 sdl->sdl_alen = ETHER_ADDR_LEN;
1163 e_addr = LLADDR(sdl);
1164 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1165 *llsa = (struct sockaddr *)sdl;
1166 return 0;
1167 #endif
1168 #ifdef INET6
1169 case AF_INET6:
1170 sin6 = (struct sockaddr_in6 *)sa;
1171 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1172 /*
1173 * An IP6 address of 0 means listen to all
1174 * of the Ethernet multicast address used for IP6.
1175 * (This is used for multicast routers.)
1176 */
1177 ifp->if_flags |= IFF_ALLMULTI;
1178 *llsa = 0;
1179 return 0;
1180 }
1181 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1182 return EADDRNOTAVAIL;
1183 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1184 M_NOWAIT|M_ZERO);
1185 if (sdl == NULL)
1186 return (ENOMEM);
1187 sdl->sdl_len = sizeof *sdl;
1188 sdl->sdl_family = AF_LINK;
1189 sdl->sdl_index = ifp->if_index;
1190 sdl->sdl_type = IFT_ETHER;
1191 sdl->sdl_alen = ETHER_ADDR_LEN;
1192 e_addr = LLADDR(sdl);
1193 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1194 *llsa = (struct sockaddr *)sdl;
1195 return 0;
1196 #endif
1197
1198 default:
1199 /*
1200 * Well, the text isn't quite right, but it's the name
1201 * that counts...
1202 */
1203 return EAFNOSUPPORT;
1204 }
1205 }
1206
1207 static moduledata_t ether_mod = {
1208 "ether",
1209 NULL,
1210 0
1211 };
1212
1213 DECLARE_MODULE(ether, ether_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
1214 MODULE_VERSION(ether, 1);
Cache object: f41c3407f87bcb704b191148b6d33785
|