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