]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_ethersubr.c
This commit was generated by cvs2svn to compensate for changes in r165009,
[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                         if ((n = m_dup(m, M_DONTWAIT)) != NULL) {
318                                 n->m_pkthdr.csum_flags |= csum_flags;
319                                 if (csum_flags & CSUM_DATA_VALID)
320                                         n->m_pkthdr.csum_data = 0xffff;
321                                 (void)if_simloop(ifp, n, dst->sa_family, hlen);
322                         } else
323                                 ifp->if_iqdrops++;
324                 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
325                                 ETHER_ADDR_LEN) == 0) {
326                         m->m_pkthdr.csum_flags |= csum_flags;
327                         if (csum_flags & CSUM_DATA_VALID)
328                                 m->m_pkthdr.csum_data = 0xffff;
329                         (void) if_simloop(ifp, m, dst->sa_family, hlen);
330                         return (0);     /* XXX */
331                 }
332         }
333
334        /*
335         * Bridges require special output handling.
336         */
337         if (ifp->if_bridge) {
338                 BRIDGE_OUTPUT(ifp, m, error);
339                 return (error);
340         }
341
342 #ifdef DEV_CARP
343         if (ifp->if_carp &&
344             (error = carp_output(ifp, m, dst, NULL)))
345                 goto bad;
346 #endif
347
348         /* Handle ng_ether(4) processing, if any */
349         if (IFP2AC(ifp)->ac_netgraph != NULL) {
350                 KASSERT(ng_ether_output_p != NULL,
351                     ("ng_ether_output_p is NULL"));
352                 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
353 bad:                    if (m != NULL)
354                                 m_freem(m);
355                         return (error);
356                 }
357                 if (m == NULL)
358                         return (0);
359         }
360
361         /* Continue with link-layer output */
362         return ether_output_frame(ifp, m);
363 }
364
365 /*
366  * Ethernet link layer output routine to send a raw frame to the device.
367  *
368  * This assumes that the 14 byte Ethernet header is present and contiguous
369  * in the first mbuf (if BRIDGE'ing).
370  */
371 int
372 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
373 {
374         int error;
375 #if defined(INET) || defined(INET6)
376         struct ip_fw *rule = ip_dn_claim_rule(m);
377
378         if (IPFW_LOADED && ether_ipfw != 0) {
379                 if (ether_ipfw_chk(&m, ifp, &rule, 0) == 0) {
380                         if (m) {
381                                 m_freem(m);
382                                 return EACCES;  /* pkt dropped */
383                         } else
384                                 return 0;       /* consumed e.g. in a pipe */
385                 }
386         }
387 #endif
388
389         /*
390          * Queue message on interface, update output statistics if
391          * successful, and start output if interface not yet active.
392          */
393         IFQ_HANDOFF(ifp, m, error);
394         return (error);
395 }
396
397 #if defined(INET) || defined(INET6)
398 /*
399  * ipfw processing for ethernet packets (in and out).
400  * The second parameter is NULL from ether_demux, and ifp from
401  * ether_output_frame.
402  */
403 int
404 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
405         struct ip_fw **rule, int shared)
406 {
407         struct ether_header *eh;
408         struct ether_header save_eh;
409         struct mbuf *m;
410         int i;
411         struct ip_fw_args args;
412
413         if (*rule != NULL && fw_one_pass)
414                 return 1; /* dummynet packet, already partially processed */
415
416         /*
417          * I need some amt of data to be contiguous, and in case others need
418          * the packet (shared==1) also better be in the first mbuf.
419          */
420         m = *m0;
421         i = min( m->m_pkthdr.len, max_protohdr);
422         if ( shared || m->m_len < i) {
423                 m = m_pullup(m, i);
424                 if (m == NULL) {
425                         *m0 = m;
426                         return 0;
427                 }
428         }
429         eh = mtod(m, struct ether_header *);
430         save_eh = *eh;                  /* save copy for restore below */
431         m_adj(m, ETHER_HDR_LEN);        /* strip ethernet header */
432
433         args.m = m;             /* the packet we are looking at         */
434         args.oif = dst;         /* destination, if any                  */
435         args.rule = *rule;      /* matching rule to restart             */
436         args.next_hop = NULL;   /* we do not support forward yet        */
437         args.eh = &save_eh;     /* MAC header for bridged/MAC packets   */
438         args.inp = NULL;        /* used by ipfw uid/gid/jail rules      */
439         i = ip_fw_chk_ptr(&args);
440         m = args.m;
441         if (m != NULL) {
442                 /*
443                  * Restore Ethernet header, as needed, in case the
444                  * mbuf chain was replaced by ipfw.
445                  */
446                 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
447                 if (m == NULL) {
448                         *m0 = m;
449                         return 0;
450                 }
451                 if (eh != mtod(m, struct ether_header *))
452                         bcopy(&save_eh, mtod(m, struct ether_header *),
453                                 ETHER_HDR_LEN);
454         }
455         *m0 = m;
456         *rule = args.rule;
457
458         if (i == IP_FW_DENY) /* drop */
459                 return 0;
460
461         KASSERT(m != NULL, ("ether_ipfw_chk: m is NULL"));
462
463         if (i == IP_FW_PASS) /* a PASS rule.  */
464                 return 1;
465
466         if (DUMMYNET_LOADED && (i == IP_FW_DUMMYNET)) {
467                 /*
468                  * Pass the pkt to dummynet, which consumes it.
469                  * If shared, make a copy and keep the original.
470                  */
471                 if (shared) {
472                         m = m_copypacket(m, M_DONTWAIT);
473                         if (m == NULL)
474                                 return 0;
475                 } else {
476                         /*
477                          * Pass the original to dummynet and
478                          * nothing back to the caller
479                          */
480                         *m0 = NULL ;
481                 }
482                 ip_dn_io_ptr(m, dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
483                 return 0;
484         }
485         /*
486          * XXX at some point add support for divert/forward actions.
487          * If none of the above matches, we have to drop the pkt.
488          */
489         return 0;
490 }
491 #endif
492
493 /*
494  * Process a received Ethernet packet; the packet is in the
495  * mbuf chain m with the ethernet header at the front.
496  */
497 static void
498 ether_input(struct ifnet *ifp, struct mbuf *m)
499 {
500         struct ether_header *eh;
501         u_short etype;
502
503         /*
504          * Do consistency checks to verify assumptions
505          * made by code past this point.
506          */
507         if ((m->m_flags & M_PKTHDR) == 0) {
508                 if_printf(ifp, "discard frame w/o packet header\n");
509                 ifp->if_ierrors++;
510                 m_freem(m);
511                 return;
512         }
513         if (m->m_len < ETHER_HDR_LEN) {
514                 /* XXX maybe should pullup? */
515                 if_printf(ifp, "discard frame w/o leading ethernet "
516                                 "header (len %u pkt len %u)\n",
517                                 m->m_len, m->m_pkthdr.len);
518                 ifp->if_ierrors++;
519                 m_freem(m);
520                 return;
521         }
522         eh = mtod(m, struct ether_header *);
523         etype = ntohs(eh->ether_type);
524         if (m->m_pkthdr.len >
525             ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
526                 if_printf(ifp, "discard oversize frame "
527                                 "(ether type %x flags %x len %u > max %lu)\n",
528                                 etype, m->m_flags, m->m_pkthdr.len,
529                                 ETHER_MAX_FRAME(ifp, etype,
530                                                 m->m_flags & M_HASFCS));
531                 ifp->if_ierrors++;
532                 m_freem(m);
533                 return;
534         }
535         if (m->m_pkthdr.rcvif == NULL) {
536                 if_printf(ifp, "discard frame w/o interface pointer\n");
537                 ifp->if_ierrors++;
538                 m_freem(m);
539                 return;
540         }
541 #ifdef DIAGNOSTIC
542         if (m->m_pkthdr.rcvif != ifp) {
543                 if_printf(ifp, "Warning, frame marked as received on %s\n",
544                         m->m_pkthdr.rcvif->if_xname);
545         }
546 #endif
547
548 #ifdef MAC
549         /*
550          * Tag the mbuf with an appropriate MAC label before any other
551          * consumers can get to it.
552          */
553         mac_create_mbuf_from_ifnet(ifp, m);
554 #endif
555
556         /*
557          * Give bpf a chance at the packet.
558          */
559         BPF_MTAP(ifp, m);
560
561         /* If the CRC is still on the packet, trim it off. */
562         if (m->m_flags & M_HASFCS) {
563                 m_adj(m, -ETHER_CRC_LEN);
564                 m->m_flags &= ~M_HASFCS;
565         }
566
567         ifp->if_ibytes += m->m_pkthdr.len;
568
569         if (ifp->if_flags & IFF_MONITOR) {
570                 /*
571                  * Interface marked for monitoring; discard packet.
572                  */
573                 m_freem(m);
574                 return;
575         }
576
577         /* Handle ng_ether(4) processing, if any */
578         if (IFP2AC(ifp)->ac_netgraph != NULL) {
579                 KASSERT(ng_ether_input_p != NULL,
580                     ("ng_ether_input_p is NULL"));
581                 (*ng_ether_input_p)(ifp, &m);
582                 if (m == NULL)
583                         return;
584         }
585
586         /*
587          * Tap the packet off here for a bridge.  bridge_input()
588          * will return NULL if it has consumed the packet, otherwise
589          * it gets processed as normal.  Note that bridge_input()
590          * will always return the original packet if we need to
591          * process it locally.
592          * 
593          * XXX: When changing the below block, please also look
594          * at the src/sys/netgraph/ng_ether.c:ng_ether_rcv_upper()
595          */
596         if (ifp->if_bridge) {
597                 BRIDGE_INPUT(ifp, m);
598                 if (m == NULL)
599                         return;
600         }
601
602         /* First chunk of an mbuf contains good entropy */
603         if (harvest.ethernet)
604                 random_harvest(m, 16, 3, 0, RANDOM_NET);
605         ether_demux(ifp, m);
606 }
607
608 /*
609  * Upper layer processing for a received Ethernet packet.
610  */
611 void
612 ether_demux(struct ifnet *ifp, struct mbuf *m)
613 {
614         struct ether_header *eh;
615         int isr;
616         u_short ether_type;
617 #if defined(NETATALK)
618         struct llc *l;
619 #endif
620 #if defined(INET) || defined(INET6)
621         struct ip_fw *rule = ip_dn_claim_rule(m);
622 #endif
623
624         KASSERT(ifp != NULL, ("ether_demux: NULL interface pointer"));
625
626         eh = mtod(m, struct ether_header *);
627         ether_type = ntohs(eh->ether_type);
628
629 #if defined(INET) || defined(INET6)
630         if (rule)       /* packet was already bridged */
631                 goto post_stats;
632 #endif
633
634         if (!(ifp->if_bridge) &&
635             !((ether_type == ETHERTYPE_VLAN || m->m_flags & M_VLANTAG) &&
636             ifp->if_vlantrunk != NULL)) {
637 #ifdef DEV_CARP
638                 /*
639                  * XXX: Okay, we need to call carp_forus() and - if it is for
640                  * us jump over code that does the normal check
641                  * "IF_LLADDR(ifp) == ether_dhost". The check sequence is a bit
642                  * different from OpenBSD, so we jump over as few code as
643                  * possible, to catch _all_ sanity checks. This needs
644                  * evaluation, to see if the carp ether_dhost values break any
645                  * of these checks!
646                  */
647                 if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
648                         goto pre_stats;
649 #endif
650                 /*
651                  * Discard packet if upper layers shouldn't see it because it
652                  * was unicast to a different Ethernet address. If the driver
653                  * is working properly, then this situation can only happen
654                  * when the interface is in promiscuous mode.
655                  *
656                  * If VLANs are active, and this packet has a VLAN tag, do
657                  * not drop it here but pass it on to the VLAN layer, to
658                  * give them a chance to consider it as well (e. g. in case
659                  * bridging is only active on a VLAN).  They will drop it if
660                  * it's undesired.
661                  */
662                 if ((ifp->if_flags & IFF_PROMISC) != 0
663                     && !ETHER_IS_MULTICAST(eh->ether_dhost)
664                     && bcmp(eh->ether_dhost,
665                       IF_LLADDR(ifp), ETHER_ADDR_LEN) != 0
666                     && (ifp->if_flags & IFF_PPROMISC) == 0) {
667                             m_freem(m);
668                             return;
669                 }
670         }
671
672 #ifdef DEV_CARP
673 pre_stats:
674 #endif
675         /* Discard packet if interface is not up */
676         if ((ifp->if_flags & IFF_UP) == 0) {
677                 m_freem(m);
678                 return;
679         }
680         if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
681                 if (bcmp(etherbroadcastaddr, eh->ether_dhost,
682                     sizeof(etherbroadcastaddr)) == 0)
683                         m->m_flags |= M_BCAST;
684                 else
685                         m->m_flags |= M_MCAST;
686         }
687         if (m->m_flags & (M_BCAST|M_MCAST))
688                 ifp->if_imcasts++;
689
690 #if defined(INET) || defined(INET6)
691 post_stats:
692         if (IPFW_LOADED && ether_ipfw != 0) {
693                 if (ether_ipfw_chk(&m, NULL, &rule, 0) == 0) {
694                         if (m)
695                                 m_freem(m);
696                         return;
697                 }
698         }
699 #endif
700
701         /*
702          * Check to see if the device performed the VLAN decapsulation and
703          * provided us with the tag.
704          */
705         if (m->m_flags & M_VLANTAG) {
706                 /*
707                  * If no VLANs are configured, drop.
708                  */
709                 if (ifp->if_vlantrunk == NULL) {
710                         ifp->if_noproto++;
711                         m_freem(m);
712                         return;
713                 }
714                 /*
715                  * vlan_input() will either recursively call ether_input()
716                  * or drop the packet.
717                  */
718                 KASSERT(vlan_input_p != NULL,("ether_input: VLAN not loaded!"));
719                 (*vlan_input_p)(ifp, m);
720                 return;
721         }
722
723         /*
724          * Handle protocols that expect to have the Ethernet header
725          * (and possibly FCS) intact.
726          */
727         switch (ether_type) {
728         case ETHERTYPE_VLAN:
729                 if (ifp->if_vlantrunk != NULL) {
730                         KASSERT(vlan_input_p,("ether_input: VLAN not loaded!"));
731                         (*vlan_input_p)(ifp, m);
732                 } else {
733                         ifp->if_noproto++;
734                         m_freem(m);
735                 }
736                 return;
737         }
738
739         /* Strip off Ethernet header. */
740         m_adj(m, ETHER_HDR_LEN);
741
742         /* If the CRC is still on the packet, trim it off. */
743         if (m->m_flags & M_HASFCS) {
744                 m_adj(m, -ETHER_CRC_LEN);
745                 m->m_flags &= ~M_HASFCS;
746         }
747
748         /* Reset layer specific mbuf flags to avoid confusing upper layers. */
749         m->m_flags &= ~(M_PROTOFLAGS);
750
751         switch (ether_type) {
752 #ifdef INET
753         case ETHERTYPE_IP:
754                 if ((m = ip_fastforward(m)) == NULL)
755                         return;
756                 isr = NETISR_IP;
757                 break;
758
759         case ETHERTYPE_ARP:
760                 if (ifp->if_flags & IFF_NOARP) {
761                         /* Discard packet if ARP is disabled on interface */
762                         m_freem(m);
763                         return;
764                 }
765                 isr = NETISR_ARP;
766                 break;
767 #endif
768 #ifdef IPX
769         case ETHERTYPE_IPX:
770                 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
771                         return;
772                 isr = NETISR_IPX;
773                 break;
774 #endif
775 #ifdef INET6
776         case ETHERTYPE_IPV6:
777                 isr = NETISR_IPV6;
778                 break;
779 #endif
780 #ifdef NETATALK
781         case ETHERTYPE_AT:
782                 isr = NETISR_ATALK1;
783                 break;
784         case ETHERTYPE_AARP:
785                 isr = NETISR_AARP;
786                 break;
787 #endif /* NETATALK */
788         default:
789 #ifdef IPX
790                 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
791                         return;
792 #endif /* IPX */
793 #if defined(NETATALK)
794                 if (ether_type > ETHERMTU)
795                         goto discard;
796                 l = mtod(m, struct llc *);
797                 if (l->llc_dsap == LLC_SNAP_LSAP &&
798                     l->llc_ssap == LLC_SNAP_LSAP &&
799                     l->llc_control == LLC_UI) {
800                         if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
801                             sizeof(at_org_code)) == 0 &&
802                             ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
803                                 m_adj(m, LLC_SNAPFRAMELEN);
804                                 isr = NETISR_ATALK2;
805                                 break;
806                         }
807                         if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
808                             sizeof(aarp_org_code)) == 0 &&
809                             ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
810                                 m_adj(m, LLC_SNAPFRAMELEN);
811                                 isr = NETISR_AARP;
812                                 break;
813                         }
814                 }
815 #endif /* NETATALK */
816                 goto discard;
817         }
818         netisr_dispatch(isr, m);
819         return;
820
821 discard:
822         /*
823          * Packet is to be discarded.  If netgraph is present,
824          * hand the packet to it for last chance processing;
825          * otherwise dispose of it.
826          */
827         if (IFP2AC(ifp)->ac_netgraph != NULL) {
828                 KASSERT(ng_ether_input_orphan_p != NULL,
829                     ("ng_ether_input_orphan_p is NULL"));
830                 /*
831                  * Put back the ethernet header so netgraph has a
832                  * consistent view of inbound packets.
833                  */
834                 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT);
835                 (*ng_ether_input_orphan_p)(ifp, m);
836                 return;
837         }
838         m_freem(m);
839 }
840
841 /*
842  * Convert Ethernet address to printable (loggable) representation.
843  * This routine is for compatibility; it's better to just use
844  *
845  *      printf("%6D", <pointer to address>, ":");
846  *
847  * since there's no static buffer involved.
848  */
849 char *
850 ether_sprintf(const u_char *ap)
851 {
852         static char etherbuf[18];
853         snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
854         return (etherbuf);
855 }
856
857 /*
858  * Perform common duties while attaching to interface list
859  */
860 void
861 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
862 {
863         int i;
864         struct ifaddr *ifa;
865         struct sockaddr_dl *sdl;
866
867         ifp->if_addrlen = ETHER_ADDR_LEN;
868         ifp->if_hdrlen = ETHER_HDR_LEN;
869         if_attach(ifp);
870         ifp->if_mtu = ETHERMTU;
871         ifp->if_output = ether_output;
872         ifp->if_input = ether_input;
873         ifp->if_resolvemulti = ether_resolvemulti;
874         if (ifp->if_baudrate == 0)
875                 ifp->if_baudrate = IF_Mbps(10);         /* just a default */
876         ifp->if_broadcastaddr = etherbroadcastaddr;
877
878         ifa = ifp->if_addr;
879         KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
880         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
881         sdl->sdl_type = IFT_ETHER;
882         sdl->sdl_alen = ifp->if_addrlen;
883         bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
884
885         bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
886         if (ng_ether_attach_p != NULL)
887                 (*ng_ether_attach_p)(ifp);
888
889         /* Announce Ethernet MAC address if non-zero. */
890         for (i = 0; i < ifp->if_addrlen; i++)
891                 if (lla[i] != 0)
892                         break; 
893         if (i != ifp->if_addrlen)
894                 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
895         if (debug_mpsafenet && (ifp->if_flags & IFF_NEEDSGIANT) != 0)
896                 if_printf(ifp, "if_start running deferred for Giant\n");
897 }
898
899 /*
900  * Perform common duties while detaching an Ethernet interface
901  */
902 void
903 ether_ifdetach(struct ifnet *ifp)
904 {
905         if (IFP2AC(ifp)->ac_netgraph != NULL) {
906                 KASSERT(ng_ether_detach_p != NULL,
907                     ("ng_ether_detach_p is NULL"));
908                 (*ng_ether_detach_p)(ifp);
909         }
910
911         bpfdetach(ifp);
912         if_detach(ifp);
913 }
914
915 SYSCTL_DECL(_net_link);
916 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
917 #if defined(INET) || defined(INET6)
918 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
919             &ether_ipfw,0,"Pass ether pkts through firewall");
920 #endif
921
922 #if 0
923 /*
924  * This is for reference.  We have a table-driven version
925  * of the little-endian crc32 generator, which is faster
926  * than the double-loop.
927  */
928 uint32_t
929 ether_crc32_le(const uint8_t *buf, size_t len)
930 {
931         size_t i;
932         uint32_t crc;
933         int bit;
934         uint8_t data;
935
936         crc = 0xffffffff;       /* initial value */
937
938         for (i = 0; i < len; i++) {
939                 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1)
940                         carry = (crc ^ data) & 1;
941                         crc >>= 1;
942                         if (carry)
943                                 crc = (crc ^ ETHER_CRC_POLY_LE);
944         }
945
946         return (crc);
947 }
948 #else
949 uint32_t
950 ether_crc32_le(const uint8_t *buf, size_t len)
951 {
952         static const uint32_t crctab[] = {
953                 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
954                 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
955                 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
956                 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
957         };
958         size_t i;
959         uint32_t crc;
960
961         crc = 0xffffffff;       /* initial value */
962
963         for (i = 0; i < len; i++) {
964                 crc ^= buf[i];
965                 crc = (crc >> 4) ^ crctab[crc & 0xf];
966                 crc = (crc >> 4) ^ crctab[crc & 0xf];
967         }
968
969         return (crc);
970 }
971 #endif
972
973 uint32_t
974 ether_crc32_be(const uint8_t *buf, size_t len)
975 {
976         size_t i;
977         uint32_t crc, carry;
978         int bit;
979         uint8_t data;
980
981         crc = 0xffffffff;       /* initial value */
982
983         for (i = 0; i < len; i++) {
984                 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
985                         carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
986                         crc <<= 1;
987                         if (carry)
988                                 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
989                 }
990         }
991
992         return (crc);
993 }
994
995 int
996 ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
997 {
998         struct ifaddr *ifa = (struct ifaddr *) data;
999         struct ifreq *ifr = (struct ifreq *) data;
1000         int error = 0;
1001
1002         switch (command) {
1003         case SIOCSIFADDR:
1004                 ifp->if_flags |= IFF_UP;
1005
1006                 switch (ifa->ifa_addr->sa_family) {
1007 #ifdef INET
1008                 case AF_INET:
1009                         ifp->if_init(ifp->if_softc);    /* before arpwhohas */
1010                         arp_ifinit(ifp, ifa);
1011                         break;
1012 #endif
1013 #ifdef IPX
1014                 /*
1015                  * XXX - This code is probably wrong
1016                  */
1017                 case AF_IPX:
1018                         {
1019                         struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
1020
1021                         if (ipx_nullhost(*ina))
1022                                 ina->x_host =
1023                                     *(union ipx_host *)
1024                                     IF_LLADDR(ifp);
1025                         else {
1026                                 bcopy((caddr_t) ina->x_host.c_host,
1027                                       (caddr_t) IF_LLADDR(ifp),
1028                                       ETHER_ADDR_LEN);
1029                         }
1030
1031                         /*
1032                          * Set new address
1033                          */
1034                         ifp->if_init(ifp->if_softc);
1035                         break;
1036                         }
1037 #endif
1038                 default:
1039                         ifp->if_init(ifp->if_softc);
1040                         break;
1041                 }
1042                 break;
1043
1044         case SIOCGIFADDR:
1045                 {
1046                         struct sockaddr *sa;
1047
1048                         sa = (struct sockaddr *) & ifr->ifr_data;
1049                         bcopy(IF_LLADDR(ifp),
1050                               (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1051                 }
1052                 break;
1053
1054         case SIOCSIFMTU:
1055                 /*
1056                  * Set the interface MTU.
1057                  */
1058                 if (ifr->ifr_mtu > ETHERMTU) {
1059                         error = EINVAL;
1060                 } else {
1061                         ifp->if_mtu = ifr->ifr_mtu;
1062                 }
1063                 break;
1064         default:
1065                 error = EINVAL;                 /* XXX netbsd has ENOTTY??? */
1066                 break;
1067         }
1068         return (error);
1069 }
1070
1071 static int
1072 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1073         struct sockaddr *sa)
1074 {
1075         struct sockaddr_dl *sdl;
1076 #ifdef INET
1077         struct sockaddr_in *sin;
1078 #endif
1079 #ifdef INET6
1080         struct sockaddr_in6 *sin6;
1081 #endif
1082         u_char *e_addr;
1083
1084         switch(sa->sa_family) {
1085         case AF_LINK:
1086                 /*
1087                  * No mapping needed. Just check that it's a valid MC address.
1088                  */
1089                 sdl = (struct sockaddr_dl *)sa;
1090                 e_addr = LLADDR(sdl);
1091                 if (!ETHER_IS_MULTICAST(e_addr))
1092                         return EADDRNOTAVAIL;
1093                 *llsa = 0;
1094                 return 0;
1095
1096 #ifdef INET
1097         case AF_INET:
1098                 sin = (struct sockaddr_in *)sa;
1099                 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1100                         return EADDRNOTAVAIL;
1101                 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1102                        M_NOWAIT|M_ZERO);
1103                 if (sdl == NULL)
1104                         return ENOMEM;
1105                 sdl->sdl_len = sizeof *sdl;
1106                 sdl->sdl_family = AF_LINK;
1107                 sdl->sdl_index = ifp->if_index;
1108                 sdl->sdl_type = IFT_ETHER;
1109                 sdl->sdl_alen = ETHER_ADDR_LEN;
1110                 e_addr = LLADDR(sdl);
1111                 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1112                 *llsa = (struct sockaddr *)sdl;
1113                 return 0;
1114 #endif
1115 #ifdef INET6
1116         case AF_INET6:
1117                 sin6 = (struct sockaddr_in6 *)sa;
1118                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1119                         /*
1120                          * An IP6 address of 0 means listen to all
1121                          * of the Ethernet multicast address used for IP6.
1122                          * (This is used for multicast routers.)
1123                          */
1124                         ifp->if_flags |= IFF_ALLMULTI;
1125                         *llsa = 0;
1126                         return 0;
1127                 }
1128                 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1129                         return EADDRNOTAVAIL;
1130                 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
1131                        M_NOWAIT|M_ZERO);
1132                 if (sdl == NULL)
1133                         return (ENOMEM);
1134                 sdl->sdl_len = sizeof *sdl;
1135                 sdl->sdl_family = AF_LINK;
1136                 sdl->sdl_index = ifp->if_index;
1137                 sdl->sdl_type = IFT_ETHER;
1138                 sdl->sdl_alen = ETHER_ADDR_LEN;
1139                 e_addr = LLADDR(sdl);
1140                 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1141                 *llsa = (struct sockaddr *)sdl;
1142                 return 0;
1143 #endif
1144
1145         default:
1146                 /*
1147                  * Well, the text isn't quite right, but it's the name
1148                  * that counts...
1149                  */
1150                 return EAFNOSUPPORT;
1151         }
1152 }
1153
1154 static void*
1155 ether_alloc(u_char type, struct ifnet *ifp)
1156 {
1157         struct arpcom   *ac;
1158         
1159         ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1160         ac->ac_ifp = ifp;
1161
1162         return (ac);
1163 }
1164
1165 static void
1166 ether_free(void *com, u_char type)
1167 {
1168
1169         free(com, M_ARPCOM);
1170 }
1171
1172 static int
1173 ether_modevent(module_t mod, int type, void *data)
1174 {
1175
1176         switch (type) {
1177         case MOD_LOAD:
1178                 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1179                 break;
1180         case MOD_UNLOAD:
1181                 if_deregister_com_alloc(IFT_ETHER);
1182                 break;
1183         default:
1184                 return EOPNOTSUPP;
1185         }
1186
1187         return (0);
1188 }
1189
1190 static moduledata_t ether_mod = {
1191         "ether",
1192         ether_modevent,
1193         0
1194 };
1195
1196 void
1197 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1198 {
1199         struct ether_vlan_header vlan;
1200         struct mbuf mv, mb;
1201
1202         KASSERT((m->m_flags & M_VLANTAG) != 0,
1203             ("%s: vlan information not present", __func__));
1204         KASSERT(m->m_len >= sizeof(struct ether_header),
1205             ("%s: mbuf not large enough for header", __func__));
1206         bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1207         vlan.evl_proto = vlan.evl_encap_proto;
1208         vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1209         vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1210         m->m_len -= sizeof(struct ether_header);
1211         m->m_data += sizeof(struct ether_header);
1212         /*
1213          * If a data link has been supplied by the caller, then we will need to
1214          * re-create a stack allocated mbuf chain with the following structure:
1215          *
1216          * (1) mbuf #1 will contain the supplied data link
1217          * (2) mbuf #2 will contain the vlan header
1218          * (3) mbuf #3 will contain the original mbuf's packet data
1219          *
1220          * Otherwise, submit the packet and vlan header via bpf_mtap2().
1221          */
1222         if (data != NULL) {
1223                 mv.m_next = m;
1224                 mv.m_data = (caddr_t)&vlan;
1225                 mv.m_len = sizeof(vlan);
1226                 mb.m_next = &mv;
1227                 mb.m_data = data;
1228                 mb.m_len = dlen;
1229                 bpf_mtap(bp, &mb);
1230         } else
1231                 bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1232         m->m_len += sizeof(struct ether_header);
1233         m->m_data -= sizeof(struct ether_header);
1234 }
1235
1236 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1237 MODULE_VERSION(ether, 1);