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