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