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