netisr: Add netisr_hashport, which finds netisr msgport based on pkt hash
[dragonfly.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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)if_ethersubr.c      8.1 (Berkeley) 6/10/93
34  * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.33 2003/04/28 15:45:53 archie Exp $
35  */
36
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39 #include "opt_ipx.h"
40 #include "opt_mpls.h"
41 #include "opt_netgraph.h"
42 #include "opt_carp.h"
43 #include "opt_rss.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/globaldata.h>
48 #include <sys/kernel.h>
49 #include <sys/ktr.h>
50 #include <sys/lock.h>
51 #include <sys/malloc.h>
52 #include <sys/mbuf.h>
53 #include <sys/msgport.h>
54 #include <sys/socket.h>
55 #include <sys/sockio.h>
56 #include <sys/sysctl.h>
57 #include <sys/thread.h>
58
59 #include <sys/thread2.h>
60 #include <sys/mplock2.h>
61
62 #include <net/if.h>
63 #include <net/netisr.h>
64 #include <net/route.h>
65 #include <net/if_llc.h>
66 #include <net/if_dl.h>
67 #include <net/if_types.h>
68 #include <net/ifq_var.h>
69 #include <net/bpf.h>
70 #include <net/ethernet.h>
71 #include <net/vlan/if_vlan_ether.h>
72 #include <net/vlan/if_vlan_var.h>
73 #include <net/netmsg2.h>
74 #include <net/netisr2.h>
75
76 #if defined(INET) || defined(INET6)
77 #include <netinet/in.h>
78 #include <netinet/ip_var.h>
79 #include <netinet/tcp_var.h>
80 #include <netinet/if_ether.h>
81 #include <netinet/ip_flow.h>
82 #include <net/ipfw/ip_fw.h>
83 #include <net/dummynet/ip_dummynet.h>
84 #endif
85 #ifdef INET6
86 #include <netinet6/nd6.h>
87 #endif
88
89 #ifdef CARP
90 #include <netinet/ip_carp.h>
91 #endif
92
93 #ifdef IPX
94 #include <netproto/ipx/ipx.h>
95 #include <netproto/ipx/ipx_if.h>
96 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
97 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
98                   short *tp, int *hlen);
99 #endif
100
101 #ifdef MPLS
102 #include <netproto/mpls/mpls.h>
103 #endif
104
105 /* netgraph node hooks for ng_ether(4) */
106 void    (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
107 void    (*ng_ether_input_orphan_p)(struct ifnet *ifp, struct mbuf *m);
108 int     (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
109 void    (*ng_ether_attach_p)(struct ifnet *ifp);
110 void    (*ng_ether_detach_p)(struct ifnet *ifp);
111
112 void    (*vlan_input_p)(struct mbuf *);
113
114 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
115                         struct rtentry *);
116 static void ether_restore_header(struct mbuf **, const struct ether_header *,
117                                  const struct ether_header *);
118 static int ether_characterize(struct mbuf **);
119 static void ether_dispatch(int, struct mbuf *);
120
121 /*
122  * if_bridge support
123  */
124 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
125 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
126 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
127 struct ifnet *(*bridge_interface_p)(void *if_bridge);
128
129 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
130                               struct sockaddr *);
131
132 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
133         0xff, 0xff, 0xff, 0xff, 0xff, 0xff
134 };
135
136 #define gotoerr(e) do { error = (e); goto bad; } while (0)
137 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
138
139 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
140                                 struct ip_fw **rule,
141                                 const struct ether_header *eh);
142
143 static int ether_ipfw;
144 static u_long ether_restore_hdr;
145 static u_long ether_prepend_hdr;
146 static u_long ether_input_wronghash;
147 static int ether_debug;
148
149 #ifdef RSS_DEBUG
150 static u_long ether_pktinfo_try;
151 static u_long ether_pktinfo_hit;
152 static u_long ether_rss_nopi;
153 static u_long ether_rss_nohash;
154 static u_long ether_input_requeue;
155 #endif
156 static u_long ether_input_wronghwhash;
157 static int ether_input_ckhash;
158
159 #define ETHER_TSOLEN_DEFAULT    (4 * ETHERMTU)
160
161 static int ether_tsolen_default = ETHER_TSOLEN_DEFAULT;
162 TUNABLE_INT("net.link.ether.tsolen", &ether_tsolen_default);
163
164 SYSCTL_DECL(_net_link);
165 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
166 SYSCTL_INT(_net_link_ether, OID_AUTO, debug, CTLFLAG_RW,
167     &ether_debug, 0, "Ether debug");
168 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
169     &ether_ipfw, 0, "Pass ether pkts through firewall");
170 SYSCTL_ULONG(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
171     &ether_restore_hdr, 0, "# of ether header restoration");
172 SYSCTL_ULONG(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
173     &ether_prepend_hdr, 0,
174     "# of ether header restoration which prepends mbuf");
175 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghash, CTLFLAG_RW,
176     &ether_input_wronghash, 0, "# of input packets with wrong hash");
177 SYSCTL_INT(_net_link_ether, OID_AUTO, tsolen, CTLFLAG_RW,
178     &ether_tsolen_default, 0, "Default max TSO length");
179
180 #ifdef RSS_DEBUG
181 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nopi, CTLFLAG_RW,
182     &ether_rss_nopi, 0, "# of packets do not have pktinfo");
183 SYSCTL_ULONG(_net_link_ether, OID_AUTO, rss_nohash, CTLFLAG_RW,
184     &ether_rss_nohash, 0, "# of packets do not have hash");
185 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_try, CTLFLAG_RW,
186     &ether_pktinfo_try, 0,
187     "# of tries to find packets' msgport using pktinfo");
188 SYSCTL_ULONG(_net_link_ether, OID_AUTO, pktinfo_hit, CTLFLAG_RW,
189     &ether_pktinfo_hit, 0,
190     "# of packets whose msgport are found using pktinfo");
191 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_requeue, CTLFLAG_RW,
192     &ether_input_requeue, 0, "# of input packets gets requeued");
193 #endif
194 SYSCTL_ULONG(_net_link_ether, OID_AUTO, input_wronghwhash, CTLFLAG_RW,
195     &ether_input_wronghwhash, 0, "# of input packets with wrong hw hash");
196 SYSCTL_INT(_net_link_ether, OID_AUTO, always_ckhash, CTLFLAG_RW,
197     &ether_input_ckhash, 0, "always check hash");
198
199 #define ETHER_KTR_STR           "ifp=%p"
200 #define ETHER_KTR_ARGS  struct ifnet *ifp
201 #ifndef KTR_ETHERNET
202 #define KTR_ETHERNET            KTR_ALL
203 #endif
204 KTR_INFO_MASTER(ether);
205 KTR_INFO(KTR_ETHERNET, ether, pkt_beg, 0, ETHER_KTR_STR, ETHER_KTR_ARGS);
206 KTR_INFO(KTR_ETHERNET, ether, pkt_end, 1, ETHER_KTR_STR, ETHER_KTR_ARGS);
207 KTR_INFO(KTR_ETHERNET, ether, disp_beg, 2, ETHER_KTR_STR, ETHER_KTR_ARGS);
208 KTR_INFO(KTR_ETHERNET, ether, disp_end, 3, ETHER_KTR_STR, ETHER_KTR_ARGS);
209 #define logether(name, arg)     KTR_LOG(ether_ ## name, arg)
210
211 /*
212  * Ethernet output routine.
213  * Encapsulate a packet of type family for the local net.
214  * Use trailer local net encapsulation if enough data in first
215  * packet leaves a multiple of 512 bytes of data in remainder.
216  * Assumes that ifp is actually pointer to arpcom structure.
217  */
218 static int
219 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
220              struct rtentry *rt)
221 {
222         struct ether_header *eh, *deh;
223         u_char *edst;
224         int loop_copy = 0;
225         int hlen = ETHER_HDR_LEN;       /* link layer header length */
226         struct arpcom *ac = IFP2AC(ifp);
227         int error;
228
229         ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
230
231         if (ifp->if_flags & IFF_MONITOR)
232                 gotoerr(ENETDOWN);
233         if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
234                 gotoerr(ENETDOWN);
235
236         M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
237         if (m == NULL)
238                 return (ENOBUFS);
239         m->m_pkthdr.csum_lhlen = sizeof(struct ether_header);
240         eh = mtod(m, struct ether_header *);
241         edst = eh->ether_dhost;
242
243         /*
244          * Fill in the destination ethernet address and frame type.
245          */
246         switch (dst->sa_family) {
247 #ifdef INET
248         case AF_INET:
249                 if (!arpresolve(ifp, rt, m, dst, edst))
250                         return (0);     /* if not yet resolved */
251 #ifdef MPLS
252                 if (m->m_flags & M_MPLSLABELED)
253                         eh->ether_type = htons(ETHERTYPE_MPLS);
254                 else
255 #endif
256                         eh->ether_type = htons(ETHERTYPE_IP);
257                 break;
258 #endif
259 #ifdef INET6
260         case AF_INET6:
261                 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
262                         return (0);             /* Something bad happenned. */
263                 eh->ether_type = htons(ETHERTYPE_IPV6);
264                 break;
265 #endif
266 #ifdef IPX
267         case AF_IPX:
268                 if (ef_outputp != NULL) {
269                         /*
270                          * Hold BGL and recheck ef_outputp
271                          */
272                         get_mplock();
273                         if (ef_outputp != NULL) {
274                                 error = ef_outputp(ifp, &m, dst,
275                                                    &eh->ether_type, &hlen);
276                                 rel_mplock();
277                                 if (error)
278                                         goto bad;
279                                 else
280                                         break;
281                         }
282                         rel_mplock();
283                 }
284                 eh->ether_type = htons(ETHERTYPE_IPX);
285                 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
286                       edst, ETHER_ADDR_LEN);
287                 break;
288 #endif
289         case pseudo_AF_HDRCMPLT:
290         case AF_UNSPEC:
291                 loop_copy = -1; /* if this is for us, don't do it */
292                 deh = (struct ether_header *)dst->sa_data;
293                 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
294                 eh->ether_type = deh->ether_type;
295                 break;
296
297         default:
298                 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
299                 gotoerr(EAFNOSUPPORT);
300         }
301
302         if (dst->sa_family == pseudo_AF_HDRCMPLT)       /* unlikely */
303                 memcpy(eh->ether_shost,
304                        ((struct ether_header *)dst->sa_data)->ether_shost,
305                        ETHER_ADDR_LEN);
306         else
307                 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
308
309         /*
310          * Bridges require special output handling.
311          */
312         if (ifp->if_bridge) {
313                 KASSERT(bridge_output_p != NULL,
314                         ("%s: if_bridge not loaded!", __func__));
315                 return bridge_output_p(ifp, m);
316         }
317
318         /*
319          * If a simplex interface, and the packet is being sent to our
320          * Ethernet address or a broadcast address, loopback a copy.
321          * XXX To make a simplex device behave exactly like a duplex
322          * device, we should copy in the case of sending to our own
323          * ethernet address (thus letting the original actually appear
324          * on the wire). However, we don't do that here for security
325          * reasons and compatibility with the original behavior.
326          */
327         if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
328                 int csum_flags = 0;
329
330                 if (m->m_pkthdr.csum_flags & CSUM_IP)
331                         csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
332                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
333                         csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
334                 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
335                         struct mbuf *n;
336
337                         if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
338                                 n->m_pkthdr.csum_flags |= csum_flags;
339                                 if (csum_flags & CSUM_DATA_VALID)
340                                         n->m_pkthdr.csum_data = 0xffff;
341                                 if_simloop(ifp, n, dst->sa_family, hlen);
342                         } else
343                                 IFNET_STAT_INC(ifp, iqdrops, 1);
344                 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
345                                 ETHER_ADDR_LEN) == 0) {
346                         m->m_pkthdr.csum_flags |= csum_flags;
347                         if (csum_flags & CSUM_DATA_VALID)
348                                 m->m_pkthdr.csum_data = 0xffff;
349                         if_simloop(ifp, m, dst->sa_family, hlen);
350                         return (0);     /* XXX */
351                 }
352         }
353
354 #ifdef CARP
355         if (ifp->if_type == IFT_CARP) {
356                 ifp = carp_parent(ifp);
357                 if (ifp == NULL)
358                         gotoerr(ENETUNREACH);
359
360                 ac = IFP2AC(ifp);
361
362                 /*
363                  * Check precondition again
364                  */
365                 ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
366
367                 if (ifp->if_flags & IFF_MONITOR)
368                         gotoerr(ENETDOWN);
369                 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) !=
370                     (IFF_UP | IFF_RUNNING))
371                         gotoerr(ENETDOWN);
372         }
373 #endif
374
375         /* Handle ng_ether(4) processing, if any */
376         if (ng_ether_output_p != NULL) {
377                 /*
378                  * Hold BGL and recheck ng_ether_output_p
379                  */
380                 get_mplock();
381                 if (ng_ether_output_p != NULL) {
382                         if ((error = ng_ether_output_p(ifp, &m)) != 0) {
383                                 rel_mplock();
384                                 goto bad;
385                         }
386                         if (m == NULL) {
387                                 rel_mplock();
388                                 return (0);
389                         }
390                 }
391                 rel_mplock();
392         }
393
394         /* Continue with link-layer output */
395         return ether_output_frame(ifp, m);
396
397 bad:
398         m_freem(m);
399         return (error);
400 }
401
402 /*
403  * Returns the bridge interface an ifp is associated
404  * with.
405  *
406  * Only call if ifp->if_bridge != NULL.
407  */
408 struct ifnet *
409 ether_bridge_interface(struct ifnet *ifp)
410 {
411         if (bridge_interface_p)
412                 return(bridge_interface_p(ifp->if_bridge));
413         return (ifp);
414 }
415
416 /*
417  * Ethernet link layer output routine to send a raw frame to the device.
418  *
419  * This assumes that the 14 byte Ethernet header is present and contiguous
420  * in the first mbuf.
421  */
422 int
423 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
424 {
425         struct ip_fw *rule = NULL;
426         int error = 0;
427         struct altq_pktattr pktattr;
428
429         ASSERT_IFNET_NOT_SERIALIZED_ALL(ifp);
430
431         if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
432                 struct m_tag *mtag;
433
434                 /* Extract info from dummynet tag */
435                 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
436                 KKASSERT(mtag != NULL);
437                 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
438                 KKASSERT(rule != NULL);
439
440                 m_tag_delete(m, mtag);
441                 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
442         }
443
444         if (ifq_is_enabled(&ifp->if_snd))
445                 altq_etherclassify(&ifp->if_snd, m, &pktattr);
446         crit_enter();
447         if (IPFW_LOADED && ether_ipfw != 0) {
448                 struct ether_header save_eh, *eh;
449
450                 eh = mtod(m, struct ether_header *);
451                 save_eh = *eh;
452                 m_adj(m, ETHER_HDR_LEN);
453                 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
454                         crit_exit();
455                         if (m != NULL) {
456                                 m_freem(m);
457                                 return ENOBUFS; /* pkt dropped */
458                         } else
459                                 return 0;       /* consumed e.g. in a pipe */
460                 }
461
462                 /* packet was ok, restore the ethernet header */
463                 ether_restore_header(&m, eh, &save_eh);
464                 if (m == NULL) {
465                         crit_exit();
466                         return ENOBUFS;
467                 }
468         }
469         crit_exit();
470
471         /*
472          * Queue message on interface, update output statistics if
473          * successful, and start output if interface not yet active.
474          */
475         error = ifq_dispatch(ifp, m, &pktattr);
476         return (error);
477 }
478
479 /*
480  * ipfw processing for ethernet packets (in and out).
481  * The second parameter is NULL from ether_demux(), and ifp from
482  * ether_output_frame().
483  */
484 static boolean_t
485 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
486                const struct ether_header *eh)
487 {
488         struct ether_header save_eh = *eh;      /* might be a ptr in *m0 */
489         struct ip_fw_args args;
490         struct m_tag *mtag;
491         struct mbuf *m;
492         int i;
493
494         if (*rule != NULL && fw_one_pass)
495                 return TRUE; /* dummynet packet, already partially processed */
496
497         /*
498          * I need some amount of data to be contiguous.
499          */
500         i = min((*m0)->m_pkthdr.len, max_protohdr);
501         if ((*m0)->m_len < i) {
502                 *m0 = m_pullup(*m0, i);
503                 if (*m0 == NULL)
504                         return FALSE;
505         }
506
507         /*
508          * Clean up tags
509          */
510         if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
511                 m_tag_delete(*m0, mtag);
512         if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
513                 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
514                 KKASSERT(mtag != NULL);
515                 m_tag_delete(*m0, mtag);
516                 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
517         }
518
519         args.m = *m0;           /* the packet we are looking at         */
520         args.oif = dst;         /* destination, if any                  */
521         args.rule = *rule;      /* matching rule to restart             */
522         args.eh = &save_eh;     /* MAC header for bridged/MAC packets   */
523         i = ip_fw_chk_ptr(&args);
524         *m0 = args.m;
525         *rule = args.rule;
526
527         if (*m0 == NULL)
528                 return FALSE;
529
530         switch (i) {
531         case IP_FW_PASS:
532                 return TRUE;
533
534         case IP_FW_DIVERT:
535         case IP_FW_TEE:
536         case IP_FW_DENY:
537                 /*
538                  * XXX at some point add support for divert/forward actions.
539                  * If none of the above matches, we have to drop the pkt.
540                  */
541                 return FALSE;
542
543         case IP_FW_DUMMYNET:
544                 /*
545                  * Pass the pkt to dummynet, which consumes it.
546                  */
547                 m = *m0;        /* pass the original to dummynet */
548                 *m0 = NULL;     /* and nothing back to the caller */
549
550                 ether_restore_header(&m, eh, &save_eh);
551                 if (m == NULL)
552                         return FALSE;
553
554                 ip_fw_dn_io_ptr(m, args.cookie,
555                                 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
556                 ip_dn_queue(m);
557                 return FALSE;
558
559         default:
560                 panic("unknown ipfw return value: %d", i);
561         }
562 }
563
564 static void
565 ether_input(struct ifnet *ifp, struct mbuf *m)
566 {
567         ether_input_pkt(ifp, m, NULL);
568 }
569
570 /*
571  * Perform common duties while attaching to interface list
572  */
573 void
574 ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
575 {
576         ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
577                            serializer);
578 }
579
580 void
581 ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
582                    lwkt_serialize_t serializer)
583 {
584         struct sockaddr_dl *sdl;
585         char ethstr[ETHER_ADDRSTRLEN + 1];
586
587         ifp->if_type = IFT_ETHER;
588         ifp->if_addrlen = ETHER_ADDR_LEN;
589         ifp->if_hdrlen = ETHER_HDR_LEN;
590         if_attach(ifp, serializer);
591         ifp->if_mtu = ETHERMTU;
592         if (ifp->if_tsolen <= 0) {
593                 if ((ether_tsolen_default / ETHERMTU) < 2) {
594                         kprintf("ether TSO maxlen %d -> %d\n",
595                             ether_tsolen_default, ETHER_TSOLEN_DEFAULT);
596                         ether_tsolen_default = ETHER_TSOLEN_DEFAULT;
597                 }
598                 ifp->if_tsolen = ether_tsolen_default;
599         }
600         if (ifp->if_baudrate == 0)
601                 ifp->if_baudrate = 10000000;
602         ifp->if_output = ether_output;
603         ifp->if_input = ether_input;
604         ifp->if_resolvemulti = ether_resolvemulti;
605         ifp->if_broadcastaddr = etherbroadcastaddr;
606         sdl = IF_LLSOCKADDR(ifp);
607         sdl->sdl_type = IFT_ETHER;
608         sdl->sdl_alen = ifp->if_addrlen;
609         bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
610         /*
611          * XXX Keep the current drivers happy.
612          * XXX Remove once all drivers have been cleaned up
613          */
614         if (lla != IFP2AC(ifp)->ac_enaddr)
615                 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
616         bpfattach(ifp, dlt, hdrlen);
617         if (ng_ether_attach_p != NULL)
618                 (*ng_ether_attach_p)(ifp);
619
620         if_printf(ifp, "MAC address: %s\n", kether_ntoa(lla, ethstr));
621 }
622
623 /*
624  * Perform common duties while detaching an Ethernet interface
625  */
626 void
627 ether_ifdetach(struct ifnet *ifp)
628 {
629         if_down(ifp);
630
631         if (ng_ether_detach_p != NULL)
632                 (*ng_ether_detach_p)(ifp);
633         bpfdetach(ifp);
634         if_detach(ifp);
635 }
636
637 int
638 ether_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
639 {
640         struct ifaddr *ifa = (struct ifaddr *) data;
641         struct ifreq *ifr = (struct ifreq *) data;
642         int error = 0;
643
644 #define IF_INIT(ifp) \
645 do { \
646         if (((ifp)->if_flags & IFF_UP) == 0) { \
647                 (ifp)->if_flags |= IFF_UP; \
648                 (ifp)->if_init((ifp)->if_softc); \
649         } \
650 } while (0)
651
652         ASSERT_IFNET_SERIALIZED_ALL(ifp);
653
654         switch (command) {
655         case SIOCSIFADDR:
656                 switch (ifa->ifa_addr->sa_family) {
657 #ifdef INET
658                 case AF_INET:
659                         IF_INIT(ifp);   /* before arpwhohas */
660                         arp_ifinit(ifp, ifa);
661                         break;
662 #endif
663 #ifdef IPX
664                 /*
665                  * XXX - This code is probably wrong
666                  */
667                 case AF_IPX:
668                         {
669                         struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
670                         struct arpcom *ac = IFP2AC(ifp);
671
672                         if (ipx_nullhost(*ina))
673                                 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
674                         else
675                                 bcopy(ina->x_host.c_host, ac->ac_enaddr,
676                                       sizeof ac->ac_enaddr);
677
678                         IF_INIT(ifp);   /* Set new address. */
679                         break;
680                         }
681 #endif
682                 default:
683                         IF_INIT(ifp);
684                         break;
685                 }
686                 break;
687
688         case SIOCGIFADDR:
689                 bcopy(IFP2AC(ifp)->ac_enaddr,
690                       ((struct sockaddr *)ifr->ifr_data)->sa_data,
691                       ETHER_ADDR_LEN);
692                 break;
693
694         case SIOCSIFMTU:
695                 /*
696                  * Set the interface MTU.
697                  */
698                 if (ifr->ifr_mtu > ETHERMTU) {
699                         error = EINVAL;
700                 } else {
701                         ifp->if_mtu = ifr->ifr_mtu;
702                 }
703                 break;
704         default:
705                 error = EINVAL;
706                 break;
707         }
708         return (error);
709
710 #undef IF_INIT
711 }
712
713 int
714 ether_resolvemulti(
715         struct ifnet *ifp,
716         struct sockaddr **llsa,
717         struct sockaddr *sa)
718 {
719         struct sockaddr_dl *sdl;
720 #ifdef INET
721         struct sockaddr_in *sin;
722 #endif
723 #ifdef INET6
724         struct sockaddr_in6 *sin6;
725 #endif
726         u_char *e_addr;
727
728         switch(sa->sa_family) {
729         case AF_LINK:
730                 /*
731                  * No mapping needed. Just check that it's a valid MC address.
732                  */
733                 sdl = (struct sockaddr_dl *)sa;
734                 e_addr = LLADDR(sdl);
735                 if ((e_addr[0] & 1) != 1)
736                         return EADDRNOTAVAIL;
737                 *llsa = NULL;
738                 return 0;
739
740 #ifdef INET
741         case AF_INET:
742                 sin = (struct sockaddr_in *)sa;
743                 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
744                         return EADDRNOTAVAIL;
745                 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
746                 sdl->sdl_len = sizeof *sdl;
747                 sdl->sdl_family = AF_LINK;
748                 sdl->sdl_index = ifp->if_index;
749                 sdl->sdl_type = IFT_ETHER;
750                 sdl->sdl_alen = ETHER_ADDR_LEN;
751                 e_addr = LLADDR(sdl);
752                 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
753                 *llsa = (struct sockaddr *)sdl;
754                 return 0;
755 #endif
756 #ifdef INET6
757         case AF_INET6:
758                 sin6 = (struct sockaddr_in6 *)sa;
759                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
760                         /*
761                          * An IP6 address of 0 means listen to all
762                          * of the Ethernet multicast address used for IP6.
763                          * (This is used for multicast routers.)
764                          */
765                         ifp->if_flags |= IFF_ALLMULTI;
766                         *llsa = NULL;
767                         return 0;
768                 }
769                 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
770                         return EADDRNOTAVAIL;
771                 sdl = kmalloc(sizeof *sdl, M_IFMADDR, M_WAITOK | M_ZERO);
772                 sdl->sdl_len = sizeof *sdl;
773                 sdl->sdl_family = AF_LINK;
774                 sdl->sdl_index = ifp->if_index;
775                 sdl->sdl_type = IFT_ETHER;
776                 sdl->sdl_alen = ETHER_ADDR_LEN;
777                 e_addr = LLADDR(sdl);
778                 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
779                 *llsa = (struct sockaddr *)sdl;
780                 return 0;
781 #endif
782
783         default:
784                 /*
785                  * Well, the text isn't quite right, but it's the name
786                  * that counts...
787                  */
788                 return EAFNOSUPPORT;
789         }
790 }
791
792 #if 0
793 /*
794  * This is for reference.  We have a table-driven version
795  * of the little-endian crc32 generator, which is faster
796  * than the double-loop.
797  */
798 uint32_t
799 ether_crc32_le(const uint8_t *buf, size_t len)
800 {
801         uint32_t c, crc, carry;
802         size_t i, j;
803
804         crc = 0xffffffffU;      /* initial value */
805
806         for (i = 0; i < len; i++) {
807                 c = buf[i];
808                 for (j = 0; j < 8; j++) {
809                         carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
810                         crc >>= 1;
811                         c >>= 1;
812                         if (carry)
813                                 crc = (crc ^ ETHER_CRC_POLY_LE);
814                 }
815         }
816
817         return (crc);
818 }
819 #else
820 uint32_t
821 ether_crc32_le(const uint8_t *buf, size_t len)
822 {
823         static const uint32_t crctab[] = {
824                 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
825                 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
826                 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
827                 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
828         };
829         uint32_t crc;
830         size_t i;
831
832         crc = 0xffffffffU;      /* initial value */
833
834         for (i = 0; i < len; i++) {
835                 crc ^= buf[i];
836                 crc = (crc >> 4) ^ crctab[crc & 0xf];
837                 crc = (crc >> 4) ^ crctab[crc & 0xf];
838         }
839
840         return (crc);
841 }
842 #endif
843
844 uint32_t
845 ether_crc32_be(const uint8_t *buf, size_t len)
846 {
847         uint32_t c, crc, carry;
848         size_t i, j;
849
850         crc = 0xffffffffU;      /* initial value */
851
852         for (i = 0; i < len; i++) {
853                 c = buf[i];
854                 for (j = 0; j < 8; j++) {
855                         carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
856                         crc <<= 1;
857                         c >>= 1;
858                         if (carry)
859                                 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
860                 }
861         }
862
863         return (crc);
864 }
865
866 /*
867  * find the size of ethernet header, and call classifier
868  */
869 void
870 altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
871                    struct altq_pktattr *pktattr)
872 {
873         struct ether_header *eh;
874         uint16_t ether_type;
875         int hlen, af, hdrsize;
876
877         hlen = sizeof(struct ether_header);
878         eh = mtod(m, struct ether_header *);
879
880         ether_type = ntohs(eh->ether_type);
881         if (ether_type < ETHERMTU) {
882                 /* ick! LLC/SNAP */
883                 struct llc *llc = (struct llc *)(eh + 1);
884                 hlen += 8;
885
886                 if (m->m_len < hlen ||
887                     llc->llc_dsap != LLC_SNAP_LSAP ||
888                     llc->llc_ssap != LLC_SNAP_LSAP ||
889                     llc->llc_control != LLC_UI)
890                         goto bad;  /* not snap! */
891
892                 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
893         }
894
895         if (ether_type == ETHERTYPE_IP) {
896                 af = AF_INET;
897                 hdrsize = 20;  /* sizeof(struct ip) */
898 #ifdef INET6
899         } else if (ether_type == ETHERTYPE_IPV6) {
900                 af = AF_INET6;
901                 hdrsize = 40;  /* sizeof(struct ip6_hdr) */
902 #endif
903         } else
904                 goto bad;
905
906         while (m->m_len <= hlen) {
907                 hlen -= m->m_len;
908                 m = m->m_next;
909         }
910         if (m->m_len < hlen + hdrsize) {
911                 /*
912                  * ip header is not in a single mbuf.  this should not
913                  * happen in the current code.
914                  * (todo: use m_pulldown in the future)
915                  */
916                 goto bad;
917         }
918         m->m_data += hlen;
919         m->m_len -= hlen;
920         ifq_classify(ifq, m, af, pktattr);
921         m->m_data -= hlen;
922         m->m_len += hlen;
923
924         return;
925
926 bad:
927         pktattr->pattr_class = NULL;
928         pktattr->pattr_hdr = NULL;
929         pktattr->pattr_af = AF_UNSPEC;
930 }
931
932 static void
933 ether_restore_header(struct mbuf **m0, const struct ether_header *eh,
934                      const struct ether_header *save_eh)
935 {
936         struct mbuf *m = *m0;
937
938         ether_restore_hdr++;
939
940         /*
941          * Prepend the header, optimize for the common case of
942          * eh pointing into the mbuf.
943          */
944         if ((const void *)(eh + 1) == (void *)m->m_data) {
945                 m->m_data -= ETHER_HDR_LEN;
946                 m->m_len += ETHER_HDR_LEN;
947                 m->m_pkthdr.len += ETHER_HDR_LEN;
948         } else {
949                 ether_prepend_hdr++;
950
951                 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
952                 if (m != NULL) {
953                         bcopy(save_eh, mtod(m, struct ether_header *),
954                               ETHER_HDR_LEN);
955                 }
956         }
957         *m0 = m;
958 }
959
960 /*
961  * Upper layer processing for a received Ethernet packet.
962  */
963 void
964 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
965 {
966         struct ether_header *eh;
967         int isr, discard = 0;
968         u_short ether_type;
969         struct ip_fw *rule = NULL;
970
971         M_ASSERTPKTHDR(m);
972         KASSERT(m->m_len >= ETHER_HDR_LEN,
973                 ("ether header is not contiguous!"));
974
975         eh = mtod(m, struct ether_header *);
976
977         if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
978                 struct m_tag *mtag;
979
980                 /* Extract info from dummynet tag */
981                 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
982                 KKASSERT(mtag != NULL);
983                 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
984                 KKASSERT(rule != NULL);
985
986                 m_tag_delete(m, mtag);
987                 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
988
989                 /* packet is passing the second time */
990                 goto post_stats;
991         }
992
993         /*
994          * We got a packet which was unicast to a different Ethernet
995          * address.  If the driver is working properly, then this
996          * situation can only happen when the interface is in
997          * promiscuous mode.  We defer the packet discarding until the
998          * vlan processing is done, so that vlan/bridge or vlan/netgraph
999          * could work.
1000          */
1001         if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1002             !ETHER_IS_MULTICAST(eh->ether_dhost) &&
1003             bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1004                 if (ether_debug & 1) {
1005                         kprintf("%02x:%02x:%02x:%02x:%02x:%02x "
1006                                 "%02x:%02x:%02x:%02x:%02x:%02x "
1007                                 "%04x vs %02x:%02x:%02x:%02x:%02x:%02x\n",
1008                                 eh->ether_dhost[0],
1009                                 eh->ether_dhost[1],
1010                                 eh->ether_dhost[2],
1011                                 eh->ether_dhost[3],
1012                                 eh->ether_dhost[4],
1013                                 eh->ether_dhost[5],
1014                                 eh->ether_shost[0],
1015                                 eh->ether_shost[1],
1016                                 eh->ether_shost[2],
1017                                 eh->ether_shost[3],
1018                                 eh->ether_shost[4],
1019                                 eh->ether_shost[5],
1020                                 eh->ether_type,
1021                                 ((u_char *)IFP2AC(ifp)->ac_enaddr)[0],
1022                                 ((u_char *)IFP2AC(ifp)->ac_enaddr)[1],
1023                                 ((u_char *)IFP2AC(ifp)->ac_enaddr)[2],
1024                                 ((u_char *)IFP2AC(ifp)->ac_enaddr)[3],
1025                                 ((u_char *)IFP2AC(ifp)->ac_enaddr)[4],
1026                                 ((u_char *)IFP2AC(ifp)->ac_enaddr)[5]
1027                         );
1028                 }
1029                 if ((ether_debug & 2) == 0)
1030                         discard = 1;
1031         }
1032
1033 post_stats:
1034         if (IPFW_LOADED && ether_ipfw != 0 && !discard) {
1035                 struct ether_header save_eh = *eh;
1036
1037                 /* XXX old crufty stuff, needs to be removed */
1038                 m_adj(m, sizeof(struct ether_header));
1039
1040                 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1041                         m_freem(m);
1042                         return;
1043                 }
1044
1045                 ether_restore_header(&m, eh, &save_eh);
1046                 if (m == NULL)
1047                         return;
1048                 eh = mtod(m, struct ether_header *);
1049         }
1050
1051         ether_type = ntohs(eh->ether_type);
1052         KKASSERT(ether_type != ETHERTYPE_VLAN);
1053
1054         if (m->m_flags & M_VLANTAG) {
1055                 void (*vlan_input_func)(struct mbuf *);
1056
1057                 vlan_input_func = vlan_input_p;
1058                 if (vlan_input_func != NULL) {
1059                         vlan_input_func(m);
1060                 } else {
1061                         IFNET_STAT_INC(m->m_pkthdr.rcvif, noproto, 1);
1062                         m_freem(m);
1063                 }
1064                 return;
1065         }
1066
1067         /*
1068          * If we have been asked to discard this packet
1069          * (e.g. not for us), drop it before entering
1070          * the upper layer.
1071          */
1072         if (discard) {
1073                 m_freem(m);
1074                 return;
1075         }
1076
1077         /*
1078          * Clear protocol specific flags,
1079          * before entering the upper layer.
1080          */
1081         m->m_flags &= ~M_ETHER_FLAGS;
1082
1083         /* Strip ethernet header. */
1084         m_adj(m, sizeof(struct ether_header));
1085
1086         switch (ether_type) {
1087 #ifdef INET
1088         case ETHERTYPE_IP:
1089                 if ((m->m_flags & M_LENCHECKED) == 0) {
1090                         if (!ip_lengthcheck(&m, 0))
1091                                 return;
1092                 }
1093                 if (ipflow_fastforward(m))
1094                         return;
1095                 isr = NETISR_IP;
1096                 break;
1097
1098         case ETHERTYPE_ARP:
1099                 if (ifp->if_flags & IFF_NOARP) {
1100                         /* Discard packet if ARP is disabled on interface */
1101                         m_freem(m);
1102                         return;
1103                 }
1104                 isr = NETISR_ARP;
1105                 break;
1106 #endif
1107
1108 #ifdef INET6
1109         case ETHERTYPE_IPV6:
1110                 isr = NETISR_IPV6;
1111                 break;
1112 #endif
1113
1114 #ifdef IPX
1115         case ETHERTYPE_IPX:
1116                 if (ef_inputp) {
1117                         /*
1118                          * Hold BGL and recheck ef_inputp
1119                          */
1120                         get_mplock();
1121                         if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1122                                 rel_mplock();
1123                                 return;
1124                         }
1125                         rel_mplock();
1126                 }
1127                 isr = NETISR_IPX;
1128                 break;
1129 #endif
1130
1131 #ifdef MPLS
1132         case ETHERTYPE_MPLS:
1133         case ETHERTYPE_MPLS_MCAST:
1134                 /* Should have been set by ether_input_pkt(). */
1135                 KKASSERT(m->m_flags & M_MPLSLABELED);
1136                 isr = NETISR_MPLS;
1137                 break;
1138 #endif
1139
1140         default:
1141                 /*
1142                  * The accurate msgport is not determined before
1143                  * we reach here, so recharacterize packet.
1144                  */
1145                 m->m_flags &= ~M_HASH;
1146 #ifdef IPX
1147                 if (ef_inputp) {
1148                         /*
1149                          * Hold BGL and recheck ef_inputp
1150                          */
1151                         get_mplock();
1152                         if (ef_inputp && ef_inputp(ifp, eh, m) == 0) {
1153                                 rel_mplock();
1154                                 return;
1155                         }
1156                         rel_mplock();
1157                 }
1158 #endif
1159                 if (ng_ether_input_orphan_p != NULL) {
1160                         /*
1161                          * Put back the ethernet header so netgraph has a
1162                          * consistent view of inbound packets.
1163                          */
1164                         M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
1165                         if (m == NULL) {
1166                                 /*
1167                                  * M_PREPEND frees the mbuf in case of failure.
1168                                  */
1169                                 return;
1170                         }
1171                         /*
1172                          * Hold BGL and recheck ng_ether_input_orphan_p
1173                          */
1174                         get_mplock();
1175                         if (ng_ether_input_orphan_p != NULL) {
1176                                 ng_ether_input_orphan_p(ifp, m);
1177                                 rel_mplock();
1178                                 return;
1179                         }
1180                         rel_mplock();
1181                 }
1182                 m_freem(m);
1183                 return;
1184         }
1185
1186         if (m->m_flags & M_HASH) {
1187                 if (&curthread->td_msgport ==
1188                     netisr_hashport(m->m_pkthdr.hash)) {
1189                         netisr_handle(isr, m);
1190                         return;
1191                 } else {
1192                         /*
1193                          * XXX Something is wrong,
1194                          * we probably should panic here!
1195                          */
1196                         m->m_flags &= ~M_HASH;
1197                         atomic_add_long(&ether_input_wronghash, 1);
1198                 }
1199         }
1200 #ifdef RSS_DEBUG
1201         atomic_add_long(&ether_input_requeue, 1);
1202 #endif
1203         netisr_queue(isr, m);
1204 }
1205
1206 /*
1207  * First we perform any link layer operations, then continue to the
1208  * upper layers with ether_demux_oncpu().
1209  */
1210 static void
1211 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1212 {
1213 #ifdef CARP
1214         void *carp;
1215 #endif
1216
1217         if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1218                 /*
1219                  * Receiving interface's flags are changed, when this
1220                  * packet is waiting for processing; discard it.
1221                  */
1222                 m_freem(m);
1223                 return;
1224         }
1225
1226         /*
1227          * Tap the packet off here for a bridge.  bridge_input()
1228          * will return NULL if it has consumed the packet, otherwise
1229          * it gets processed as normal.  Note that bridge_input()
1230          * will always return the original packet if we need to
1231          * process it locally.
1232          */
1233         if (ifp->if_bridge) {
1234                 KASSERT(bridge_input_p != NULL,
1235                         ("%s: if_bridge not loaded!", __func__));
1236
1237                 if(m->m_flags & M_ETHER_BRIDGED) {
1238                         m->m_flags &= ~M_ETHER_BRIDGED;
1239                 } else {
1240                         m = bridge_input_p(ifp, m);
1241                         if (m == NULL)
1242                                 return;
1243
1244                         KASSERT(ifp == m->m_pkthdr.rcvif,
1245                                 ("bridge_input_p changed rcvif"));
1246                 }
1247         }
1248
1249 #ifdef CARP
1250         carp = ifp->if_carp;
1251         if (carp) {
1252                 m = carp_input(carp, m);
1253                 if (m == NULL)
1254                         return;
1255                 KASSERT(ifp == m->m_pkthdr.rcvif,
1256                     ("carp_input changed rcvif"));
1257         }
1258 #endif
1259
1260         /* Handle ng_ether(4) processing, if any */
1261         if (ng_ether_input_p != NULL) {
1262                 /*
1263                  * Hold BGL and recheck ng_ether_input_p
1264                  */
1265                 get_mplock();
1266                 if (ng_ether_input_p != NULL)
1267                         ng_ether_input_p(ifp, &m);
1268                 rel_mplock();
1269
1270                 if (m == NULL)
1271                         return;
1272         }
1273
1274         /* Continue with upper layer processing */
1275         ether_demux_oncpu(ifp, m);
1276 }
1277
1278 /*
1279  * Perform certain functions of ether_input_pkt():
1280  * - Test IFF_UP
1281  * - Update statistics
1282  * - Run bpf(4) tap if requested
1283  * Then pass the packet to ether_input_oncpu().
1284  *
1285  * This function should be used by pseudo interface (e.g. vlan(4)),
1286  * when it tries to claim that the packet is received by it.
1287  *
1288  * REINPUT_KEEPRCVIF
1289  * REINPUT_RUNBPF
1290  */
1291 void
1292 ether_reinput_oncpu(struct ifnet *ifp, struct mbuf *m, int reinput_flags)
1293 {
1294         /* Discard packet if interface is not up */
1295         if (!(ifp->if_flags & IFF_UP)) {
1296                 m_freem(m);
1297                 return;
1298         }
1299
1300         /*
1301          * Change receiving interface.  The bridge will often pass a flag to
1302          * ask that this not be done so ARPs get applied to the correct
1303          * side.
1304          */
1305         if ((reinput_flags & REINPUT_KEEPRCVIF) == 0 ||
1306             m->m_pkthdr.rcvif == NULL) {
1307                 m->m_pkthdr.rcvif = ifp;
1308         }
1309
1310         /* Update statistics */
1311         IFNET_STAT_INC(ifp, ipackets, 1);
1312         IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
1313         if (m->m_flags & (M_MCAST | M_BCAST))
1314                 IFNET_STAT_INC(ifp, imcasts, 1);
1315
1316         if (reinput_flags & REINPUT_RUNBPF)
1317                 BPF_MTAP(ifp, m);
1318
1319         ether_input_oncpu(ifp, m);
1320 }
1321
1322 static __inline boolean_t
1323 ether_vlancheck(struct mbuf **m0)
1324 {
1325         struct mbuf *m = *m0;
1326         struct ether_header *eh;
1327         uint16_t ether_type;
1328
1329         eh = mtod(m, struct ether_header *);
1330         ether_type = ntohs(eh->ether_type);
1331
1332         if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG) == 0) {
1333                 /*
1334                  * Extract vlan tag if hardware does not do it for us
1335                  */
1336                 vlan_ether_decap(&m);
1337                 if (m == NULL)
1338                         goto failed;
1339
1340                 eh = mtod(m, struct ether_header *);
1341                 ether_type = ntohs(eh->ether_type);
1342         }
1343
1344         if (ether_type == ETHERTYPE_VLAN && (m->m_flags & M_VLANTAG)) {
1345                 /*
1346                  * To prevent possible dangerous recursion,
1347                  * we don't do vlan-in-vlan
1348                  */
1349                 IFNET_STAT_INC(m->m_pkthdr.rcvif, noproto, 1);
1350                 goto failed;
1351         }
1352         KKASSERT(ether_type != ETHERTYPE_VLAN);
1353
1354         m->m_flags |= M_ETHER_VLANCHECKED;
1355         *m0 = m;
1356         return TRUE;
1357 failed:
1358         if (m != NULL)
1359                 m_freem(m);
1360         *m0 = NULL;
1361         return FALSE;
1362 }
1363
1364 static void
1365 ether_input_handler(netmsg_t nmsg)
1366 {
1367         struct netmsg_packet *nmp = &nmsg->packet;      /* actual size */
1368         struct ether_header *eh;
1369         struct ifnet *ifp;
1370         struct mbuf *m;
1371
1372         m = nmp->nm_packet;
1373         M_ASSERTPKTHDR(m);
1374
1375         if ((m->m_flags & M_ETHER_VLANCHECKED) == 0) {
1376                 if (!ether_vlancheck(&m)) {
1377                         KKASSERT(m == NULL);
1378                         return;
1379                 }
1380         }
1381         if ((m->m_flags & (M_HASH | M_CKHASH)) == (M_HASH | M_CKHASH) ||
1382             __predict_false(ether_input_ckhash)) {
1383                 int isr;
1384
1385                 /*
1386                  * Need to verify the hash supplied by the hardware
1387                  * which could be wrong.
1388                  */
1389                 m->m_flags &= ~(M_HASH | M_CKHASH);
1390                 isr = ether_characterize(&m);
1391                 if (m == NULL)
1392                         return;
1393                 KKASSERT(m->m_flags & M_HASH);
1394
1395                 if (m->m_pkthdr.hash != mycpuid) {
1396                         /*
1397                          * Wrong hardware supplied hash; redispatch
1398                          */
1399                         ether_dispatch(isr, m);
1400                         if (__predict_false(ether_input_ckhash))
1401                                 atomic_add_long(&ether_input_wronghwhash, 1);
1402                         return;
1403                 }
1404         }
1405         ifp = m->m_pkthdr.rcvif;
1406
1407         eh = mtod(m, struct ether_header *);
1408         if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1409                 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1410                          ifp->if_addrlen) == 0)
1411                         m->m_flags |= M_BCAST;
1412                 else
1413                         m->m_flags |= M_MCAST;
1414                 IFNET_STAT_INC(ifp, imcasts, 1);
1415         }
1416
1417         ether_input_oncpu(ifp, m);
1418 }
1419
1420 /*
1421  * Send the packet to the target msgport
1422  *
1423  * At this point the packet had better be characterized (M_HASH set),
1424  * so we know which cpu to send it to.
1425  */
1426 static void
1427 ether_dispatch(int isr, struct mbuf *m)
1428 {
1429         struct netmsg_packet *pmsg;
1430
1431         KKASSERT(m->m_flags & M_HASH);
1432         pmsg = &m->m_hdr.mh_netmsg;
1433         netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1434                     0, ether_input_handler);
1435         pmsg->nm_packet = m;
1436         pmsg->base.lmsg.u.ms_result = isr;
1437
1438         logether(disp_beg, NULL);
1439         lwkt_sendmsg(netisr_hashport(m->m_pkthdr.hash), &pmsg->base.lmsg);
1440         logether(disp_end, NULL);
1441 }
1442
1443 /*
1444  * Process a received Ethernet packet.
1445  *
1446  * The ethernet header is assumed to be in the mbuf so the caller
1447  * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1448  * bytes in the first mbuf.
1449  */
1450 void
1451 ether_input_pkt(struct ifnet *ifp, struct mbuf *m, const struct pktinfo *pi)
1452 {
1453         int isr;
1454
1455         M_ASSERTPKTHDR(m);
1456
1457         /* Discard packet if interface is not up */
1458         if (!(ifp->if_flags & IFF_UP)) {
1459                 m_freem(m);
1460                 return;
1461         }
1462
1463         if (m->m_len < sizeof(struct ether_header)) {
1464                 /* XXX error in the caller. */
1465                 m_freem(m);
1466                 return;
1467         }
1468
1469         m->m_pkthdr.rcvif = ifp;
1470
1471         logether(pkt_beg, ifp);
1472
1473         ETHER_BPF_MTAP(ifp, m);
1474
1475         IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len);
1476
1477         if (ifp->if_flags & IFF_MONITOR) {
1478                 struct ether_header *eh;
1479
1480                 eh = mtod(m, struct ether_header *);
1481                 if (ETHER_IS_MULTICAST(eh->ether_dhost))
1482                         IFNET_STAT_INC(ifp, imcasts, 1);
1483
1484                 /*
1485                  * Interface marked for monitoring; discard packet.
1486                  */
1487                 m_freem(m);
1488
1489                 logether(pkt_end, ifp);
1490                 return;
1491         }
1492
1493         /*
1494          * If the packet has been characterized (pi->pi_netisr / M_HASH)
1495          * we can dispatch it immediately without further inspection.
1496          */
1497         if (pi != NULL && (m->m_flags & M_HASH)) {
1498 #ifdef RSS_DEBUG
1499                 atomic_add_long(&ether_pktinfo_try, 1);
1500 #endif
1501                 netisr_hashcheck(pi->pi_netisr, m, pi);
1502                 if (m->m_flags & M_HASH) {
1503                         ether_dispatch(pi->pi_netisr, m);
1504 #ifdef RSS_DEBUG
1505                         atomic_add_long(&ether_pktinfo_hit, 1);
1506 #endif
1507                         logether(pkt_end, ifp);
1508                         return;
1509                 }
1510         }
1511 #ifdef RSS_DEBUG
1512         else if (ifp->if_capenable & IFCAP_RSS) {
1513                 if (pi == NULL)
1514                         atomic_add_long(&ether_rss_nopi, 1);
1515                 else
1516                         atomic_add_long(&ether_rss_nohash, 1);
1517         }
1518 #endif
1519
1520         /*
1521          * Packet hash will be recalculated by software, so clear
1522          * the M_HASH and M_CKHASH flag set by the driver; the hash
1523          * value calculated by the hardware may not be exactly what
1524          * we want.
1525          */
1526         m->m_flags &= ~(M_HASH | M_CKHASH);
1527
1528         if (!ether_vlancheck(&m)) {
1529                 KKASSERT(m == NULL);
1530                 logether(pkt_end, ifp);
1531                 return;
1532         }
1533
1534         isr = ether_characterize(&m);
1535         if (m == NULL) {
1536                 logether(pkt_end, ifp);
1537                 return;
1538         }
1539
1540         /*
1541          * Finally dispatch it
1542          */
1543         ether_dispatch(isr, m);
1544
1545         logether(pkt_end, ifp);
1546 }
1547
1548 static int
1549 ether_characterize(struct mbuf **m0)
1550 {
1551         struct mbuf *m = *m0;
1552         struct ether_header *eh;
1553         uint16_t ether_type;
1554         int isr;
1555
1556         eh = mtod(m, struct ether_header *);
1557         ether_type = ntohs(eh->ether_type);
1558
1559         /*
1560          * Map ether type to netisr id.
1561          */
1562         switch (ether_type) {
1563 #ifdef INET
1564         case ETHERTYPE_IP:
1565                 isr = NETISR_IP;
1566                 break;
1567
1568         case ETHERTYPE_ARP:
1569                 isr = NETISR_ARP;
1570                 break;
1571 #endif
1572
1573 #ifdef INET6
1574         case ETHERTYPE_IPV6:
1575                 isr = NETISR_IPV6;
1576                 break;
1577 #endif
1578
1579 #ifdef IPX
1580         case ETHERTYPE_IPX:
1581                 isr = NETISR_IPX;
1582                 break;
1583 #endif
1584
1585 #ifdef MPLS
1586         case ETHERTYPE_MPLS:
1587         case ETHERTYPE_MPLS_MCAST:
1588                 m->m_flags |= M_MPLSLABELED;
1589                 isr = NETISR_MPLS;
1590                 break;
1591 #endif
1592
1593         default:
1594                 /*
1595                  * NETISR_MAX is an invalid value; it is chosen to let
1596                  * netisr_characterize() know that we have no clear
1597                  * idea where this packet should go.
1598                  */
1599                 isr = NETISR_MAX;
1600                 break;
1601         }
1602
1603         /*
1604          * Ask the isr to characterize the packet since we couldn't.
1605          * This is an attempt to optimally get us onto the correct protocol
1606          * thread.
1607          */
1608         netisr_characterize(isr, &m, sizeof(struct ether_header));
1609
1610         *m0 = m;
1611         return isr;
1612 }
1613
1614 static void
1615 ether_demux_handler(netmsg_t nmsg)
1616 {
1617         struct netmsg_packet *nmp = &nmsg->packet;      /* actual size */
1618         struct ifnet *ifp;
1619         struct mbuf *m;
1620
1621         m = nmp->nm_packet;
1622         M_ASSERTPKTHDR(m);
1623         ifp = m->m_pkthdr.rcvif;
1624
1625         ether_demux_oncpu(ifp, m);
1626 }
1627
1628 void
1629 ether_demux(struct mbuf *m)
1630 {
1631         struct netmsg_packet *pmsg;
1632         int isr;
1633
1634         isr = ether_characterize(&m);
1635         if (m == NULL)
1636                 return;
1637
1638         KKASSERT(m->m_flags & M_HASH);
1639         pmsg = &m->m_hdr.mh_netmsg;
1640         netmsg_init(&pmsg->base, NULL, &netisr_apanic_rport,
1641             0, ether_demux_handler);
1642         pmsg->nm_packet = m;
1643         pmsg->base.lmsg.u.ms_result = isr;
1644
1645         lwkt_sendmsg(netisr_hashport(m->m_pkthdr.hash), &pmsg->base.lmsg);
1646 }
1647
1648 boolean_t
1649 ether_tso_pullup(struct mbuf **mp, int *hoff0, struct ip **ip, int *iphlen,
1650     struct tcphdr **th, int *thoff)
1651 {
1652         struct mbuf *m = *mp;
1653         struct ether_header *eh;
1654         uint16_t type;
1655         int hoff;
1656
1657         KASSERT(M_WRITABLE(m), ("not writable"));
1658
1659         hoff = ETHER_HDR_LEN;
1660         if (m->m_len < hoff) {
1661                 m = m_pullup(m, hoff);
1662                 if (m == NULL)
1663                         goto failed;
1664         }
1665         eh = mtod(m, struct ether_header *);
1666         type = eh->ether_type;
1667
1668         if (type == htons(ETHERTYPE_VLAN)) {
1669                 struct ether_vlan_header *evh;
1670
1671                 hoff += EVL_ENCAPLEN;
1672                 if (m->m_len < hoff) {
1673                         m = m_pullup(m, hoff);
1674                         if (m == NULL)
1675                                 goto failed;
1676                 }
1677                 evh = mtod(m, struct ether_vlan_header *);
1678                 type = evh->evl_proto;
1679         }
1680         KASSERT(type == htons(ETHERTYPE_IP), ("not IP %d", ntohs(type)));
1681
1682         *mp = m;
1683         *hoff0 = hoff;
1684         return tcp_tso_pullup(mp, hoff, ip, iphlen, th, thoff);
1685
1686 failed:
1687         if (m != NULL)
1688                 m_freem(m);
1689         *mp = NULL;
1690         return FALSE;
1691 }
1692
1693 u_char *
1694 kether_aton(const char *macstr, u_char *addr)
1695 {
1696         unsigned int o0, o1, o2, o3, o4, o5;
1697         int n;
1698
1699         if (macstr == NULL || addr == NULL)
1700                 return NULL;
1701
1702         n = ksscanf(macstr, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2,
1703             &o3, &o4, &o5);
1704         if (n != 6)
1705                 return NULL;
1706
1707         addr[0] = o0;
1708         addr[1] = o1;
1709         addr[2] = o2;
1710         addr[3] = o3;
1711         addr[4] = o4;
1712         addr[5] = o5;
1713
1714         return addr;
1715 }
1716
1717 char *
1718 kether_ntoa(const u_char *addr, char *buf)
1719 {
1720         int len = ETHER_ADDRSTRLEN + 1;
1721         int n;
1722
1723         n = ksnprintf(buf, len, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0],
1724             addr[1], addr[2], addr[3], addr[4], addr[5]);
1725
1726         if (n < 17)
1727                 return NULL;
1728
1729         return buf;
1730 }
1731
1732 MODULE_VERSION(ether, 1);