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