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