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