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