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