]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_ethersubr.c
Merge projects/bhyve_svm into HEAD.
[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_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_netgraph.h"
36 #include "opt_mbuf_profiling.h"
37 #include "opt_rss.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mbuf.h>
46 #include <sys/random.h>
47 #include <sys/socket.h>
48 #include <sys/sockio.h>
49 #include <sys/sysctl.h>
50 #include <sys/uuid.h>
51
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/if_arp.h>
55 #include <net/netisr.h>
56 #include <net/route.h>
57 #include <net/if_llc.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/bpf.h>
61 #include <net/ethernet.h>
62 #include <net/if_bridgevar.h>
63 #include <net/if_vlan_var.h>
64 #include <net/if_llatbl.h>
65 #include <net/pfil.h>
66 #include <net/vnet.h>
67
68 #include <netpfil/pf/pf_mtag.h>
69
70 #if defined(INET) || defined(INET6)
71 #include <netinet/in.h>
72 #include <netinet/in_var.h>
73 #include <netinet/if_ether.h>
74 #include <netinet/in_rss.h>
75 #include <netinet/ip_carp.h>
76 #include <netinet/ip_var.h>
77 #endif
78 #ifdef INET6
79 #include <netinet6/nd6.h>
80 #endif
81
82 int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
83 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp,
84                 const struct sockaddr *dst, short *tp, int *hlen);
85
86 #include <security/mac/mac_framework.h>
87
88 #ifdef CTASSERT
89 CTASSERT(sizeof (struct ether_header) == ETHER_ADDR_LEN * 2 + 2);
90 CTASSERT(sizeof (struct ether_addr) == ETHER_ADDR_LEN);
91 #endif
92
93 VNET_DEFINE(struct pfil_head, link_pfil_hook);  /* Packet filter hooks */
94
95 /* netgraph node hooks for ng_ether(4) */
96 void    (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
97 void    (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
98 int     (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
99 void    (*ng_ether_attach_p)(struct ifnet *ifp);
100 void    (*ng_ether_detach_p)(struct ifnet *ifp);
101
102 void    (*vlan_input_p)(struct ifnet *, struct mbuf *);
103
104 /* if_bridge(4) support */
105 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *); 
106 int     (*bridge_output_p)(struct ifnet *, struct mbuf *, 
107                 struct sockaddr *, struct rtentry *);
108 void    (*bridge_dn_p)(struct mbuf *, struct ifnet *);
109
110 /* if_lagg(4) support */
111 struct mbuf *(*lagg_input_p)(struct ifnet *, struct mbuf *); 
112
113 static const u_char etherbroadcastaddr[ETHER_ADDR_LEN] =
114                         { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
115
116 static  int ether_resolvemulti(struct ifnet *, struct sockaddr **,
117                 struct sockaddr *);
118 #ifdef VIMAGE
119 static  void ether_reassign(struct ifnet *, struct vnet *, char *);
120 #endif
121
122 /* XXX: should be in an arp support file, not here */
123 static MALLOC_DEFINE(M_ARPCOM, "arpcom", "802.* interface internals");
124
125 #define ETHER_IS_BROADCAST(addr) \
126         (bcmp(etherbroadcastaddr, (addr), ETHER_ADDR_LEN) == 0)
127
128 #define senderr(e) do { error = (e); goto bad;} while (0)
129
130 static void
131 update_mbuf_csumflags(struct mbuf *src, struct mbuf *dst)
132 {
133         int csum_flags = 0;
134
135         if (src->m_pkthdr.csum_flags & CSUM_IP)
136                 csum_flags |= (CSUM_IP_CHECKED|CSUM_IP_VALID);
137         if (src->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
138                 csum_flags |= (CSUM_DATA_VALID|CSUM_PSEUDO_HDR);
139         if (src->m_pkthdr.csum_flags & CSUM_SCTP)
140                 csum_flags |= CSUM_SCTP_VALID;
141         dst->m_pkthdr.csum_flags |= csum_flags;
142         if (csum_flags & CSUM_DATA_VALID)
143                 dst->m_pkthdr.csum_data = 0xffff;
144 }
145
146 /*
147  * Ethernet output routine.
148  * Encapsulate a packet of type family for the local net.
149  * Use trailer local net encapsulation if enough data in first
150  * packet leaves a multiple of 512 bytes of data in remainder.
151  */
152 int
153 ether_output(struct ifnet *ifp, struct mbuf *m,
154         const struct sockaddr *dst, struct route *ro)
155 {
156         short type;
157         int error = 0, hdrcmplt = 0;
158         u_char esrc[ETHER_ADDR_LEN], edst[ETHER_ADDR_LEN];
159         struct llentry *lle = NULL;
160         struct rtentry *rt0 = NULL;
161         struct ether_header *eh;
162         struct pf_mtag *t;
163         int loop_copy = 1;
164         int hlen;       /* link layer header length */
165
166         if (ro != NULL) {
167                 if (!(m->m_flags & (M_BCAST | M_MCAST)))
168                         lle = ro->ro_lle;
169                 rt0 = ro->ro_rt;
170         }
171 #ifdef MAC
172         error = mac_ifnet_check_transmit(ifp, m);
173         if (error)
174                 senderr(error);
175 #endif
176
177         M_PROFILE(m);
178         if (ifp->if_flags & IFF_MONITOR)
179                 senderr(ENETDOWN);
180         if (!((ifp->if_flags & IFF_UP) &&
181             (ifp->if_drv_flags & IFF_DRV_RUNNING)))
182                 senderr(ENETDOWN);
183
184         hlen = ETHER_HDR_LEN;
185         switch (dst->sa_family) {
186 #ifdef INET
187         case AF_INET:
188                 if (lle != NULL && (lle->la_flags & LLE_VALID))
189                         memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
190                 else
191                         error = arpresolve(ifp, rt0, m, dst, edst, &lle);
192                 if (error)
193                         return (error == EWOULDBLOCK ? 0 : error);
194                 type = htons(ETHERTYPE_IP);
195                 break;
196         case AF_ARP:
197         {
198                 struct arphdr *ah;
199                 ah = mtod(m, struct arphdr *);
200                 ah->ar_hrd = htons(ARPHRD_ETHER);
201
202                 loop_copy = 0; /* if this is for us, don't do it */
203
204                 switch(ntohs(ah->ar_op)) {
205                 case ARPOP_REVREQUEST:
206                 case ARPOP_REVREPLY:
207                         type = htons(ETHERTYPE_REVARP);
208                         break;
209                 case ARPOP_REQUEST:
210                 case ARPOP_REPLY:
211                 default:
212                         type = htons(ETHERTYPE_ARP);
213                         break;
214                 }
215
216                 if (m->m_flags & M_BCAST)
217                         bcopy(ifp->if_broadcastaddr, edst, ETHER_ADDR_LEN);
218                 else
219                         bcopy(ar_tha(ah), edst, ETHER_ADDR_LEN);
220
221         }
222         break;
223 #endif
224 #ifdef INET6
225         case AF_INET6:
226                 if (lle != NULL && (lle->la_flags & LLE_VALID))
227                         memcpy(edst, &lle->ll_addr.mac16, sizeof(edst));
228                 else
229                         error = nd6_storelladdr(ifp, m, dst, (u_char *)edst, &lle);
230                 if (error)
231                         return error;
232                 type = htons(ETHERTYPE_IPV6);
233                 break;
234 #endif
235         case pseudo_AF_HDRCMPLT:
236             {
237                 const struct ether_header *eh;
238                 
239                 hdrcmplt = 1;
240                 eh = (const struct ether_header *)dst->sa_data;
241                 (void)memcpy(esrc, eh->ether_shost, sizeof (esrc));
242                 /* FALLTHROUGH */
243
244         case AF_UNSPEC:
245                 loop_copy = 0; /* if this is for us, don't do it */
246                 eh = (const struct ether_header *)dst->sa_data;
247                 (void)memcpy(edst, eh->ether_dhost, sizeof (edst));
248                 type = eh->ether_type;
249                 break;
250             }
251         default:
252                 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
253                 senderr(EAFNOSUPPORT);
254         }
255
256         if (lle != NULL && (lle->la_flags & LLE_IFADDR)) {
257                 update_mbuf_csumflags(m, m);
258                 return (if_simloop(ifp, m, dst->sa_family, 0));
259         }
260
261         /*
262          * Add local net header.  If no space in first mbuf,
263          * allocate another.
264          */
265         M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
266         if (m == NULL)
267                 senderr(ENOBUFS);
268         eh = mtod(m, struct ether_header *);
269         (void)memcpy(&eh->ether_type, &type,
270                 sizeof(eh->ether_type));
271         (void)memcpy(eh->ether_dhost, edst, sizeof (edst));
272         if (hdrcmplt)
273                 (void)memcpy(eh->ether_shost, esrc,
274                         sizeof(eh->ether_shost));
275         else
276                 (void)memcpy(eh->ether_shost, IF_LLADDR(ifp),
277                         sizeof(eh->ether_shost));
278
279         /*
280          * If a simplex interface, and the packet is being sent to our
281          * Ethernet address or a broadcast address, loopback a copy.
282          * XXX To make a simplex device behave exactly like a duplex
283          * device, we should copy in the case of sending to our own
284          * ethernet address (thus letting the original actually appear
285          * on the wire). However, we don't do that here for security
286          * reasons and compatibility with the original behavior.
287          */
288         if ((ifp->if_flags & IFF_SIMPLEX) && loop_copy &&
289             ((t = pf_find_mtag(m)) == NULL || !t->routed)) {
290                 if (m->m_flags & M_BCAST) {
291                         struct mbuf *n;
292
293                         /*
294                          * Because if_simloop() modifies the packet, we need a
295                          * writable copy through m_dup() instead of a readonly
296                          * one as m_copy[m] would give us. The alternative would
297                          * be to modify if_simloop() to handle the readonly mbuf,
298                          * but performancewise it is mostly equivalent (trading
299                          * extra data copying vs. extra locking).
300                          *
301                          * XXX This is a local workaround.  A number of less
302                          * often used kernel parts suffer from the same bug.
303                          * See PR kern/105943 for a proposed general solution.
304                          */
305                         if ((n = m_dup(m, M_NOWAIT)) != NULL) {
306                                 update_mbuf_csumflags(m, n);
307                                 (void)if_simloop(ifp, n, dst->sa_family, hlen);
308                         } else
309                                 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
310                 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
311                                 ETHER_ADDR_LEN) == 0) {
312                         update_mbuf_csumflags(m, m);
313                         (void) if_simloop(ifp, m, dst->sa_family, hlen);
314                         return (0);     /* XXX */
315                 }
316         }
317
318        /*
319         * Bridges require special output handling.
320         */
321         if (ifp->if_bridge) {
322                 BRIDGE_OUTPUT(ifp, m, error);
323                 return (error);
324         }
325
326 #if defined(INET) || defined(INET6)
327         if (ifp->if_carp &&
328             (error = (*carp_output_p)(ifp, m, dst)))
329                 goto bad;
330 #endif
331
332         /* Handle ng_ether(4) processing, if any */
333         if (IFP2AC(ifp)->ac_netgraph != NULL) {
334                 KASSERT(ng_ether_output_p != NULL,
335                     ("ng_ether_output_p is NULL"));
336                 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0) {
337 bad:                    if (m != NULL)
338                                 m_freem(m);
339                         return (error);
340                 }
341                 if (m == NULL)
342                         return (0);
343         }
344
345         /* Continue with link-layer output */
346         return ether_output_frame(ifp, m);
347 }
348
349 /*
350  * Ethernet link layer output routine to send a raw frame to the device.
351  *
352  * This assumes that the 14 byte Ethernet header is present and contiguous
353  * in the first mbuf (if BRIDGE'ing).
354  */
355 int
356 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
357 {
358         int i;
359
360         if (PFIL_HOOKED(&V_link_pfil_hook)) {
361                 i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_OUT, NULL);
362
363                 if (i != 0)
364                         return (EACCES);
365
366                 if (m == NULL)
367                         return (0);
368         }
369
370         /*
371          * Queue message on interface, update output statistics if
372          * successful, and start output if interface not yet active.
373          */
374         return ((ifp->if_transmit)(ifp, m));
375 }
376
377 #if defined(INET) || defined(INET6)
378 #endif
379
380 /*
381  * Process a received Ethernet packet; the packet is in the
382  * mbuf chain m with the ethernet header at the front.
383  */
384 static void
385 ether_input_internal(struct ifnet *ifp, struct mbuf *m)
386 {
387         struct ether_header *eh;
388         u_short etype;
389
390         if ((ifp->if_flags & IFF_UP) == 0) {
391                 m_freem(m);
392                 return;
393         }
394 #ifdef DIAGNOSTIC
395         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
396                 if_printf(ifp, "discard frame at !IFF_DRV_RUNNING\n");
397                 m_freem(m);
398                 return;
399         }
400 #endif
401         /*
402          * Do consistency checks to verify assumptions
403          * made by code past this point.
404          */
405         if ((m->m_flags & M_PKTHDR) == 0) {
406                 if_printf(ifp, "discard frame w/o packet header\n");
407                 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
408                 m_freem(m);
409                 return;
410         }
411         if (m->m_len < ETHER_HDR_LEN) {
412                 /* XXX maybe should pullup? */
413                 if_printf(ifp, "discard frame w/o leading ethernet "
414                                 "header (len %u pkt len %u)\n",
415                                 m->m_len, m->m_pkthdr.len);
416                 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
417                 m_freem(m);
418                 return;
419         }
420         eh = mtod(m, struct ether_header *);
421         etype = ntohs(eh->ether_type);
422         if (m->m_pkthdr.rcvif == NULL) {
423                 if_printf(ifp, "discard frame w/o interface pointer\n");
424                 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
425                 m_freem(m);
426                 return;
427         }
428 #ifdef DIAGNOSTIC
429         if (m->m_pkthdr.rcvif != ifp) {
430                 if_printf(ifp, "Warning, frame marked as received on %s\n",
431                         m->m_pkthdr.rcvif->if_xname);
432         }
433 #endif
434
435         CURVNET_SET_QUIET(ifp->if_vnet);
436
437         if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
438                 if (ETHER_IS_BROADCAST(eh->ether_dhost))
439                         m->m_flags |= M_BCAST;
440                 else
441                         m->m_flags |= M_MCAST;
442                 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
443         }
444
445 #ifdef MAC
446         /*
447          * Tag the mbuf with an appropriate MAC label before any other
448          * consumers can get to it.
449          */
450         mac_ifnet_create_mbuf(ifp, m);
451 #endif
452
453         /*
454          * Give bpf a chance at the packet.
455          */
456         ETHER_BPF_MTAP(ifp, m);
457
458         /*
459          * If the CRC is still on the packet, trim it off. We do this once
460          * and once only in case we are re-entered. Nothing else on the
461          * Ethernet receive path expects to see the FCS.
462          */
463         if (m->m_flags & M_HASFCS) {
464                 m_adj(m, -ETHER_CRC_LEN);
465                 m->m_flags &= ~M_HASFCS;
466         }
467
468         if (!(ifp->if_capenable & IFCAP_HWSTATS))
469                 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len);
470
471         /* Allow monitor mode to claim this frame, after stats are updated. */
472         if (ifp->if_flags & IFF_MONITOR) {
473                 m_freem(m);
474                 CURVNET_RESTORE();
475                 return;
476         }
477
478         /* Handle input from a lagg(4) port */
479         if (ifp->if_type == IFT_IEEE8023ADLAG) {
480                 KASSERT(lagg_input_p != NULL,
481                     ("%s: if_lagg not loaded!", __func__));
482                 m = (*lagg_input_p)(ifp, m);
483                 if (m != NULL)
484                         ifp = m->m_pkthdr.rcvif;
485                 else {
486                         CURVNET_RESTORE();
487                         return;
488                 }
489         }
490
491         /*
492          * If the hardware did not process an 802.1Q tag, do this now,
493          * to allow 802.1P priority frames to be passed to the main input
494          * path correctly.
495          * TODO: Deal with Q-in-Q frames, but not arbitrary nesting levels.
496          */
497         if ((m->m_flags & M_VLANTAG) == 0 && etype == ETHERTYPE_VLAN) {
498                 struct ether_vlan_header *evl;
499
500                 if (m->m_len < sizeof(*evl) &&
501                     (m = m_pullup(m, sizeof(*evl))) == NULL) {
502 #ifdef DIAGNOSTIC
503                         if_printf(ifp, "cannot pullup VLAN header\n");
504 #endif
505                         if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
506                         m_freem(m);
507                         CURVNET_RESTORE();
508                         return;
509                 }
510
511                 evl = mtod(m, struct ether_vlan_header *);
512                 m->m_pkthdr.ether_vtag = ntohs(evl->evl_tag);
513                 m->m_flags |= M_VLANTAG;
514
515                 bcopy((char *)evl, (char *)evl + ETHER_VLAN_ENCAP_LEN,
516                     ETHER_HDR_LEN - ETHER_TYPE_LEN);
517                 m_adj(m, ETHER_VLAN_ENCAP_LEN);
518                 eh = mtod(m, struct ether_header *);
519         }
520
521         M_SETFIB(m, ifp->if_fib);
522
523         /* Allow ng_ether(4) to claim this frame. */
524         if (IFP2AC(ifp)->ac_netgraph != NULL) {
525                 KASSERT(ng_ether_input_p != NULL,
526                     ("%s: ng_ether_input_p is NULL", __func__));
527                 m->m_flags &= ~M_PROMISC;
528                 (*ng_ether_input_p)(ifp, &m);
529                 if (m == NULL) {
530                         CURVNET_RESTORE();
531                         return;
532                 }
533                 eh = mtod(m, struct ether_header *);
534         }
535
536         /*
537          * Allow if_bridge(4) to claim this frame.
538          * The BRIDGE_INPUT() macro will update ifp if the bridge changed it
539          * and the frame should be delivered locally.
540          */
541         if (ifp->if_bridge != NULL) {
542                 m->m_flags &= ~M_PROMISC;
543                 BRIDGE_INPUT(ifp, m);
544                 if (m == NULL) {
545                         CURVNET_RESTORE();
546                         return;
547                 }
548                 eh = mtod(m, struct ether_header *);
549         }
550
551 #if defined(INET) || defined(INET6)
552         /*
553          * Clear M_PROMISC on frame so that carp(4) will see it when the
554          * mbuf flows up to Layer 3.
555          * FreeBSD's implementation of carp(4) uses the inprotosw
556          * to dispatch IPPROTO_CARP. carp(4) also allocates its own
557          * Ethernet addresses of the form 00:00:5e:00:01:xx, which
558          * is outside the scope of the M_PROMISC test below.
559          * TODO: Maintain a hash table of ethernet addresses other than
560          * ether_dhost which may be active on this ifp.
561          */
562         if (ifp->if_carp && (*carp_forus_p)(ifp, eh->ether_dhost)) {
563                 m->m_flags &= ~M_PROMISC;
564         } else
565 #endif
566         {
567                 /*
568                  * If the frame received was not for our MAC address, set the
569                  * M_PROMISC flag on the mbuf chain. The frame may need to
570                  * be seen by the rest of the Ethernet input path in case of
571                  * re-entry (e.g. bridge, vlan, netgraph) but should not be
572                  * seen by upper protocol layers.
573                  */
574                 if (!ETHER_IS_MULTICAST(eh->ether_dhost) &&
575                     bcmp(IF_LLADDR(ifp), eh->ether_dhost, ETHER_ADDR_LEN) != 0)
576                         m->m_flags |= M_PROMISC;
577         }
578
579         if (harvest.ethernet)
580                 random_harvest(&(m->m_data), 12, 2, RANDOM_NET_ETHER);
581
582         ether_demux(ifp, m);
583         CURVNET_RESTORE();
584 }
585
586 /*
587  * Ethernet input dispatch; by default, direct dispatch here regardless of
588  * global configuration.  However, if RSS is enabled, hook up RSS affinity
589  * so that when deferred or hybrid dispatch is enabled, we can redistribute
590  * load based on RSS.
591  *
592  * XXXRW: Would be nice if the ifnet passed up a flag indicating whether or
593  * not it had already done work distribution via multi-queue.  Then we could
594  * direct dispatch in the event load balancing was already complete and
595  * handle the case of interfaces with different capabilities better.
596  *
597  * XXXRW: Sort of want an M_DISTRIBUTED flag to avoid multiple distributions
598  * at multiple layers?
599  *
600  * XXXRW: For now, enable all this only if RSS is compiled in, although it
601  * works fine without RSS.  Need to characterise the performance overhead
602  * of the detour through the netisr code in the event the result is always
603  * direct dispatch.
604  */
605 static void
606 ether_nh_input(struct mbuf *m)
607 {
608
609         ether_input_internal(m->m_pkthdr.rcvif, m);
610 }
611
612 static struct netisr_handler    ether_nh = {
613         .nh_name = "ether",
614         .nh_handler = ether_nh_input,
615         .nh_proto = NETISR_ETHER,
616 #ifdef RSS
617         .nh_policy = NETISR_POLICY_CPU,
618         .nh_dispatch = NETISR_DISPATCH_DIRECT,
619         .nh_m2cpuid = rss_m2cpuid,
620 #else
621         .nh_policy = NETISR_POLICY_SOURCE,
622         .nh_dispatch = NETISR_DISPATCH_DIRECT,
623 #endif
624 };
625
626 static void
627 ether_init(__unused void *arg)
628 {
629
630         netisr_register(&ether_nh);
631 }
632 SYSINIT(ether, SI_SUB_INIT_IF, SI_ORDER_ANY, ether_init, NULL);
633
634 static void
635 vnet_ether_init(__unused void *arg)
636 {
637         int i;
638
639         /* Initialize packet filter hooks. */
640         V_link_pfil_hook.ph_type = PFIL_TYPE_AF;
641         V_link_pfil_hook.ph_af = AF_LINK;
642         if ((i = pfil_head_register(&V_link_pfil_hook)) != 0)
643                 printf("%s: WARNING: unable to register pfil link hook, "
644                         "error %d\n", __func__, i);
645 }
646 VNET_SYSINIT(vnet_ether_init, SI_SUB_PROTO_IF, SI_ORDER_ANY,
647     vnet_ether_init, NULL);
648  
649 static void
650 vnet_ether_destroy(__unused void *arg)
651 {
652         int i;
653
654         if ((i = pfil_head_unregister(&V_link_pfil_hook)) != 0)
655                 printf("%s: WARNING: unable to unregister pfil link hook, "
656                         "error %d\n", __func__, i);
657 }
658 VNET_SYSUNINIT(vnet_ether_uninit, SI_SUB_PROTO_IF, SI_ORDER_ANY,
659     vnet_ether_destroy, NULL);
660
661
662
663 static void
664 ether_input(struct ifnet *ifp, struct mbuf *m)
665 {
666
667         struct mbuf *mn;
668
669         /*
670          * The drivers are allowed to pass in a chain of packets linked with
671          * m_nextpkt. We split them up into separate packets here and pass
672          * them up. This allows the drivers to amortize the receive lock.
673          */
674         while (m) {
675                 mn = m->m_nextpkt;
676                 m->m_nextpkt = NULL;
677
678                 /*
679                  * We will rely on rcvif being set properly in the deferred context,
680                  * so assert it is correct here.
681                  */
682                 KASSERT(m->m_pkthdr.rcvif == ifp, ("%s: ifnet mismatch", __func__));
683                 netisr_dispatch(NETISR_ETHER, m);
684                 m = mn;
685         }
686 }
687
688 /*
689  * Upper layer processing for a received Ethernet packet.
690  */
691 void
692 ether_demux(struct ifnet *ifp, struct mbuf *m)
693 {
694         struct ether_header *eh;
695         int i, isr;
696         u_short ether_type;
697
698         KASSERT(ifp != NULL, ("%s: NULL interface pointer", __func__));
699
700         /* Do not grab PROMISC frames in case we are re-entered. */
701         if (PFIL_HOOKED(&V_link_pfil_hook) && !(m->m_flags & M_PROMISC)) {
702                 i = pfil_run_hooks(&V_link_pfil_hook, &m, ifp, PFIL_IN, NULL);
703
704                 if (i != 0 || m == NULL)
705                         return;
706         }
707
708         eh = mtod(m, struct ether_header *);
709         ether_type = ntohs(eh->ether_type);
710
711         /*
712          * If this frame has a VLAN tag other than 0, call vlan_input()
713          * if its module is loaded. Otherwise, drop.
714          */
715         if ((m->m_flags & M_VLANTAG) &&
716             EVL_VLANOFTAG(m->m_pkthdr.ether_vtag) != 0) {
717                 if (ifp->if_vlantrunk == NULL) {
718                         if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
719                         m_freem(m);
720                         return;
721                 }
722                 KASSERT(vlan_input_p != NULL,("%s: VLAN not loaded!",
723                     __func__));
724                 /* Clear before possibly re-entering ether_input(). */
725                 m->m_flags &= ~M_PROMISC;
726                 (*vlan_input_p)(ifp, m);
727                 return;
728         }
729
730         /*
731          * Pass promiscuously received frames to the upper layer if the user
732          * requested this by setting IFF_PPROMISC. Otherwise, drop them.
733          */
734         if ((ifp->if_flags & IFF_PPROMISC) == 0 && (m->m_flags & M_PROMISC)) {
735                 m_freem(m);
736                 return;
737         }
738
739         /*
740          * Reset layer specific mbuf flags to avoid confusing upper layers.
741          * Strip off Ethernet header.
742          */
743         m->m_flags &= ~M_VLANTAG;
744         m_clrprotoflags(m);
745         m_adj(m, ETHER_HDR_LEN);
746
747         /*
748          * Dispatch frame to upper layer.
749          */
750         switch (ether_type) {
751 #ifdef INET
752         case ETHERTYPE_IP:
753                 if ((m = ip_fastforward(m)) == NULL)
754                         return;
755                 isr = NETISR_IP;
756                 break;
757
758         case ETHERTYPE_ARP:
759                 if (ifp->if_flags & IFF_NOARP) {
760                         /* Discard packet if ARP is disabled on interface */
761                         m_freem(m);
762                         return;
763                 }
764                 isr = NETISR_ARP;
765                 break;
766 #endif
767 #ifdef INET6
768         case ETHERTYPE_IPV6:
769                 isr = NETISR_IPV6;
770                 break;
771 #endif
772         default:
773                 goto discard;
774         }
775         netisr_dispatch(isr, m);
776         return;
777
778 discard:
779         /*
780          * Packet is to be discarded.  If netgraph is present,
781          * hand the packet to it for last chance processing;
782          * otherwise dispose of it.
783          */
784         if (IFP2AC(ifp)->ac_netgraph != NULL) {
785                 KASSERT(ng_ether_input_orphan_p != NULL,
786                     ("ng_ether_input_orphan_p is NULL"));
787                 /*
788                  * Put back the ethernet header so netgraph has a
789                  * consistent view of inbound packets.
790                  */
791                 M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
792                 (*ng_ether_input_orphan_p)(ifp, m);
793                 return;
794         }
795         m_freem(m);
796 }
797
798 /*
799  * Convert Ethernet address to printable (loggable) representation.
800  * This routine is for compatibility; it's better to just use
801  *
802  *      printf("%6D", <pointer to address>, ":");
803  *
804  * since there's no static buffer involved.
805  */
806 char *
807 ether_sprintf(const u_char *ap)
808 {
809         static char etherbuf[18];
810         snprintf(etherbuf, sizeof (etherbuf), "%6D", ap, ":");
811         return (etherbuf);
812 }
813
814 /*
815  * Perform common duties while attaching to interface list
816  */
817 void
818 ether_ifattach(struct ifnet *ifp, const u_int8_t *lla)
819 {
820         int i;
821         struct ifaddr *ifa;
822         struct sockaddr_dl *sdl;
823
824         ifp->if_addrlen = ETHER_ADDR_LEN;
825         ifp->if_hdrlen = ETHER_HDR_LEN;
826         if_attach(ifp);
827         ifp->if_mtu = ETHERMTU;
828         ifp->if_output = ether_output;
829         ifp->if_input = ether_input;
830         ifp->if_resolvemulti = ether_resolvemulti;
831 #ifdef VIMAGE
832         ifp->if_reassign = ether_reassign;
833 #endif
834         if (ifp->if_baudrate == 0)
835                 ifp->if_baudrate = IF_Mbps(10);         /* just a default */
836         ifp->if_broadcastaddr = etherbroadcastaddr;
837
838         ifa = ifp->if_addr;
839         KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
840         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
841         sdl->sdl_type = IFT_ETHER;
842         sdl->sdl_alen = ifp->if_addrlen;
843         bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
844
845         bpfattach(ifp, DLT_EN10MB, ETHER_HDR_LEN);
846         if (ng_ether_attach_p != NULL)
847                 (*ng_ether_attach_p)(ifp);
848
849         /* Announce Ethernet MAC address if non-zero. */
850         for (i = 0; i < ifp->if_addrlen; i++)
851                 if (lla[i] != 0)
852                         break; 
853         if (i != ifp->if_addrlen)
854                 if_printf(ifp, "Ethernet address: %6D\n", lla, ":");
855
856         uuid_ether_add(LLADDR(sdl));
857 }
858
859 /*
860  * Perform common duties while detaching an Ethernet interface
861  */
862 void
863 ether_ifdetach(struct ifnet *ifp)
864 {
865         struct sockaddr_dl *sdl;
866
867         sdl = (struct sockaddr_dl *)(ifp->if_addr->ifa_addr);
868         uuid_ether_del(LLADDR(sdl));
869
870         if (IFP2AC(ifp)->ac_netgraph != NULL) {
871                 KASSERT(ng_ether_detach_p != NULL,
872                     ("ng_ether_detach_p is NULL"));
873                 (*ng_ether_detach_p)(ifp);
874         }
875
876         bpfdetach(ifp);
877         if_detach(ifp);
878 }
879
880 #ifdef VIMAGE
881 void
882 ether_reassign(struct ifnet *ifp, struct vnet *new_vnet, char *unused __unused)
883 {
884
885         if (IFP2AC(ifp)->ac_netgraph != NULL) {
886                 KASSERT(ng_ether_detach_p != NULL,
887                     ("ng_ether_detach_p is NULL"));
888                 (*ng_ether_detach_p)(ifp);
889         }
890
891         if (ng_ether_attach_p != NULL) {
892                 CURVNET_SET_QUIET(new_vnet);
893                 (*ng_ether_attach_p)(ifp);
894                 CURVNET_RESTORE();
895         }
896 }
897 #endif
898
899 SYSCTL_DECL(_net_link);
900 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
901
902 #if 0
903 /*
904  * This is for reference.  We have a table-driven version
905  * of the little-endian crc32 generator, which is faster
906  * than the double-loop.
907  */
908 uint32_t
909 ether_crc32_le(const uint8_t *buf, size_t len)
910 {
911         size_t i;
912         uint32_t crc;
913         int bit;
914         uint8_t data;
915
916         crc = 0xffffffff;       /* initial value */
917
918         for (i = 0; i < len; i++) {
919                 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
920                         carry = (crc ^ data) & 1;
921                         crc >>= 1;
922                         if (carry)
923                                 crc = (crc ^ ETHER_CRC_POLY_LE);
924                 }
925         }
926
927         return (crc);
928 }
929 #else
930 uint32_t
931 ether_crc32_le(const uint8_t *buf, size_t len)
932 {
933         static const uint32_t crctab[] = {
934                 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
935                 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
936                 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
937                 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
938         };
939         size_t i;
940         uint32_t crc;
941
942         crc = 0xffffffff;       /* initial value */
943
944         for (i = 0; i < len; i++) {
945                 crc ^= buf[i];
946                 crc = (crc >> 4) ^ crctab[crc & 0xf];
947                 crc = (crc >> 4) ^ crctab[crc & 0xf];
948         }
949
950         return (crc);
951 }
952 #endif
953
954 uint32_t
955 ether_crc32_be(const uint8_t *buf, size_t len)
956 {
957         size_t i;
958         uint32_t crc, carry;
959         int bit;
960         uint8_t data;
961
962         crc = 0xffffffff;       /* initial value */
963
964         for (i = 0; i < len; i++) {
965                 for (data = *buf++, bit = 0; bit < 8; bit++, data >>= 1) {
966                         carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
967                         crc <<= 1;
968                         if (carry)
969                                 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
970                 }
971         }
972
973         return (crc);
974 }
975
976 int
977 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
978 {
979         struct ifaddr *ifa = (struct ifaddr *) data;
980         struct ifreq *ifr = (struct ifreq *) data;
981         int error = 0;
982
983         switch (command) {
984         case SIOCSIFADDR:
985                 ifp->if_flags |= IFF_UP;
986
987                 switch (ifa->ifa_addr->sa_family) {
988 #ifdef INET
989                 case AF_INET:
990                         ifp->if_init(ifp->if_softc);    /* before arpwhohas */
991                         arp_ifinit(ifp, ifa);
992                         break;
993 #endif
994                 default:
995                         ifp->if_init(ifp->if_softc);
996                         break;
997                 }
998                 break;
999
1000         case SIOCGIFADDR:
1001                 {
1002                         struct sockaddr *sa;
1003
1004                         sa = (struct sockaddr *) & ifr->ifr_data;
1005                         bcopy(IF_LLADDR(ifp),
1006                               (caddr_t) sa->sa_data, ETHER_ADDR_LEN);
1007                 }
1008                 break;
1009
1010         case SIOCSIFMTU:
1011                 /*
1012                  * Set the interface MTU.
1013                  */
1014                 if (ifr->ifr_mtu > ETHERMTU) {
1015                         error = EINVAL;
1016                 } else {
1017                         ifp->if_mtu = ifr->ifr_mtu;
1018                 }
1019                 break;
1020         default:
1021                 error = EINVAL;                 /* XXX netbsd has ENOTTY??? */
1022                 break;
1023         }
1024         return (error);
1025 }
1026
1027 static int
1028 ether_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1029         struct sockaddr *sa)
1030 {
1031         struct sockaddr_dl *sdl;
1032 #ifdef INET
1033         struct sockaddr_in *sin;
1034 #endif
1035 #ifdef INET6
1036         struct sockaddr_in6 *sin6;
1037 #endif
1038         u_char *e_addr;
1039
1040         switch(sa->sa_family) {
1041         case AF_LINK:
1042                 /*
1043                  * No mapping needed. Just check that it's a valid MC address.
1044                  */
1045                 sdl = (struct sockaddr_dl *)sa;
1046                 e_addr = LLADDR(sdl);
1047                 if (!ETHER_IS_MULTICAST(e_addr))
1048                         return EADDRNOTAVAIL;
1049                 *llsa = 0;
1050                 return 0;
1051
1052 #ifdef INET
1053         case AF_INET:
1054                 sin = (struct sockaddr_in *)sa;
1055                 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1056                         return EADDRNOTAVAIL;
1057                 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1058                 sdl->sdl_alen = ETHER_ADDR_LEN;
1059                 e_addr = LLADDR(sdl);
1060                 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
1061                 *llsa = (struct sockaddr *)sdl;
1062                 return 0;
1063 #endif
1064 #ifdef INET6
1065         case AF_INET6:
1066                 sin6 = (struct sockaddr_in6 *)sa;
1067                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1068                         /*
1069                          * An IP6 address of 0 means listen to all
1070                          * of the Ethernet multicast address used for IP6.
1071                          * (This is used for multicast routers.)
1072                          */
1073                         ifp->if_flags |= IFF_ALLMULTI;
1074                         *llsa = 0;
1075                         return 0;
1076                 }
1077                 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1078                         return EADDRNOTAVAIL;
1079                 sdl = link_init_sdl(ifp, *llsa, IFT_ETHER);
1080                 sdl->sdl_alen = ETHER_ADDR_LEN;
1081                 e_addr = LLADDR(sdl);
1082                 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1083                 *llsa = (struct sockaddr *)sdl;
1084                 return 0;
1085 #endif
1086
1087         default:
1088                 /*
1089                  * Well, the text isn't quite right, but it's the name
1090                  * that counts...
1091                  */
1092                 return EAFNOSUPPORT;
1093         }
1094 }
1095
1096 static void*
1097 ether_alloc(u_char type, struct ifnet *ifp)
1098 {
1099         struct arpcom   *ac;
1100         
1101         ac = malloc(sizeof(struct arpcom), M_ARPCOM, M_WAITOK | M_ZERO);
1102         ac->ac_ifp = ifp;
1103
1104         return (ac);
1105 }
1106
1107 static void
1108 ether_free(void *com, u_char type)
1109 {
1110
1111         free(com, M_ARPCOM);
1112 }
1113
1114 static int
1115 ether_modevent(module_t mod, int type, void *data)
1116 {
1117
1118         switch (type) {
1119         case MOD_LOAD:
1120                 if_register_com_alloc(IFT_ETHER, ether_alloc, ether_free);
1121                 break;
1122         case MOD_UNLOAD:
1123                 if_deregister_com_alloc(IFT_ETHER);
1124                 break;
1125         default:
1126                 return EOPNOTSUPP;
1127         }
1128
1129         return (0);
1130 }
1131
1132 static moduledata_t ether_mod = {
1133         "ether",
1134         ether_modevent,
1135         0
1136 };
1137
1138 void
1139 ether_vlan_mtap(struct bpf_if *bp, struct mbuf *m, void *data, u_int dlen)
1140 {
1141         struct ether_vlan_header vlan;
1142         struct mbuf mv, mb;
1143
1144         KASSERT((m->m_flags & M_VLANTAG) != 0,
1145             ("%s: vlan information not present", __func__));
1146         KASSERT(m->m_len >= sizeof(struct ether_header),
1147             ("%s: mbuf not large enough for header", __func__));
1148         bcopy(mtod(m, char *), &vlan, sizeof(struct ether_header));
1149         vlan.evl_proto = vlan.evl_encap_proto;
1150         vlan.evl_encap_proto = htons(ETHERTYPE_VLAN);
1151         vlan.evl_tag = htons(m->m_pkthdr.ether_vtag);
1152         m->m_len -= sizeof(struct ether_header);
1153         m->m_data += sizeof(struct ether_header);
1154         /*
1155          * If a data link has been supplied by the caller, then we will need to
1156          * re-create a stack allocated mbuf chain with the following structure:
1157          *
1158          * (1) mbuf #1 will contain the supplied data link
1159          * (2) mbuf #2 will contain the vlan header
1160          * (3) mbuf #3 will contain the original mbuf's packet data
1161          *
1162          * Otherwise, submit the packet and vlan header via bpf_mtap2().
1163          */
1164         if (data != NULL) {
1165                 mv.m_next = m;
1166                 mv.m_data = (caddr_t)&vlan;
1167                 mv.m_len = sizeof(vlan);
1168                 mb.m_next = &mv;
1169                 mb.m_data = data;
1170                 mb.m_len = dlen;
1171                 bpf_mtap(bp, &mb);
1172         } else
1173                 bpf_mtap2(bp, &vlan, sizeof(vlan), m);
1174         m->m_len += sizeof(struct ether_header);
1175         m->m_data -= sizeof(struct ether_header);
1176 }
1177
1178 struct mbuf *
1179 ether_vlanencap(struct mbuf *m, uint16_t tag)
1180 {
1181         struct ether_vlan_header *evl;
1182
1183         M_PREPEND(m, ETHER_VLAN_ENCAP_LEN, M_NOWAIT);
1184         if (m == NULL)
1185                 return (NULL);
1186         /* M_PREPEND takes care of m_len, m_pkthdr.len for us */
1187
1188         if (m->m_len < sizeof(*evl)) {
1189                 m = m_pullup(m, sizeof(*evl));
1190                 if (m == NULL)
1191                         return (NULL);
1192         }
1193
1194         /*
1195          * Transform the Ethernet header into an Ethernet header
1196          * with 802.1Q encapsulation.
1197          */
1198         evl = mtod(m, struct ether_vlan_header *);
1199         bcopy((char *)evl + ETHER_VLAN_ENCAP_LEN,
1200             (char *)evl, ETHER_HDR_LEN - ETHER_TYPE_LEN);
1201         evl->evl_encap_proto = htons(ETHERTYPE_VLAN);
1202         evl->evl_tag = htons(tag);
1203         return (m);
1204 }
1205
1206 DECLARE_MODULE(ether, ether_mod, SI_SUB_INIT_IF, SI_ORDER_ANY);
1207 MODULE_VERSION(ether, 1);