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