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