Always enable ETHER_INPUT_CHAIN support
[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.89 2008/09/17 08:51:29 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
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/globaldata.h>
49 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/msgport.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/sysctl.h>
56 #include <sys/thread.h>
57 #include <sys/thread2.h>
58
59 #include <net/if.h>
60 #include <net/netisr.h>
61 #include <net/route.h>
62 #include <net/if_llc.h>
63 #include <net/if_dl.h>
64 #include <net/if_types.h>
65 #include <net/ifq_var.h>
66 #include <net/bpf.h>
67 #include <net/ethernet.h>
68 #include <net/vlan/if_vlan_ether.h>
69 #include <net/netmsg2.h>
70
71 #if defined(INET) || defined(INET6)
72 #include <netinet/in.h>
73 #include <netinet/in_var.h>
74 #include <netinet/if_ether.h>
75 #include <net/ipfw/ip_fw.h>
76 #include <net/dummynet/ip_dummynet.h>
77 #endif
78 #ifdef INET6
79 #include <netinet6/nd6.h>
80 #endif
81
82 #ifdef CARP
83 #include <netinet/ip_carp.h>
84 #endif
85
86 #ifdef IPX
87 #include <netproto/ipx/ipx.h>
88 #include <netproto/ipx/ipx_if.h>
89 int (*ef_inputp)(struct ifnet*, const struct ether_header *eh, struct mbuf *m);
90 int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
91                   short *tp, int *hlen);
92 #endif
93
94 #ifdef NS
95 #include <netns/ns.h>
96 #include <netns/ns_if.h>
97 ushort ns_nettype;
98 int ether_outputdebug = 0;
99 int ether_inputdebug = 0;
100 #endif
101
102 #ifdef NETATALK
103 #include <netproto/atalk/at.h>
104 #include <netproto/atalk/at_var.h>
105 #include <netproto/atalk/at_extern.h>
106
107 #define llc_snap_org_code       llc_un.type_snap.org_code
108 #define llc_snap_ether_type     llc_un.type_snap.ether_type
109
110 extern u_char   at_org_code[3];
111 extern u_char   aarp_org_code[3];
112 #endif /* NETATALK */
113
114 #ifdef MPLS
115 #include <netproto/mpls/mpls.h>
116 #endif
117
118 /* netgraph node hooks for ng_ether(4) */
119 void    (*ng_ether_input_p)(struct ifnet *ifp, struct mbuf **mp);
120 void    (*ng_ether_input_orphan_p)(struct ifnet *ifp,
121                 struct mbuf *m, const struct ether_header *eh);
122 int     (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
123 void    (*ng_ether_attach_p)(struct ifnet *ifp);
124 void    (*ng_ether_detach_p)(struct ifnet *ifp);
125
126 void    (*vlan_input_p)(struct mbuf *);
127
128 static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
129                         struct rtentry *);
130 static void ether_restore_header(struct mbuf **, const struct ether_header *,
131                                  const struct ether_header *);
132
133 /*
134  * if_bridge support
135  */
136 struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
137 int (*bridge_output_p)(struct ifnet *, struct mbuf *);
138 void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
139
140 static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
141                               struct sockaddr *);
142
143 const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
144         0xff, 0xff, 0xff, 0xff, 0xff, 0xff
145 };
146
147 #define gotoerr(e) do { error = (e); goto bad; } while (0)
148 #define IFP2AC(ifp) ((struct arpcom *)(ifp))
149
150 static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
151                                 struct ip_fw **rule,
152                                 const struct ether_header *eh);
153
154 static int ether_ipfw;
155 static u_int ether_restore_hdr;
156 static u_int ether_prepend_hdr;
157
158 SYSCTL_DECL(_net_link);
159 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
160 SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
161            &ether_ipfw, 0, "Pass ether pkts through firewall");
162 SYSCTL_UINT(_net_link_ether, OID_AUTO, restore_hdr, CTLFLAG_RW,
163             &ether_restore_hdr, 0, "# of ether header restoration");
164 SYSCTL_UINT(_net_link_ether, OID_AUTO, prepend_hdr, CTLFLAG_RW,
165             &ether_prepend_hdr, 0,
166             "# of ether header restoration which prepends mbuf");
167
168 /*
169  * Ethernet output routine.
170  * Encapsulate a packet of type family for the local net.
171  * Use trailer local net encapsulation if enough data in first
172  * packet leaves a multiple of 512 bytes of data in remainder.
173  * Assumes that ifp is actually pointer to arpcom structure.
174  */
175 static int
176 ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
177              struct rtentry *rt)
178 {
179         struct ether_header *eh, *deh;
180         u_char *edst;
181         int loop_copy = 0;
182         int hlen = ETHER_HDR_LEN;       /* link layer header length */
183         struct arpcom *ac = IFP2AC(ifp);
184         int error;
185
186         ASSERT_NOT_SERIALIZED(ifp->if_serializer);
187
188         if (ifp->if_flags & IFF_MONITOR)
189                 gotoerr(ENETDOWN);
190         if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
191                 gotoerr(ENETDOWN);
192
193         M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
194         if (m == NULL)
195                 return (ENOBUFS);
196         eh = mtod(m, struct ether_header *);
197         edst = eh->ether_dhost;
198
199         /*
200          * Fill in the destination ethernet address and frame type.
201          */
202         switch (dst->sa_family) {
203 #ifdef INET
204         case AF_INET:
205                 if (!arpresolve(ifp, rt, m, dst, edst))
206                         return (0);     /* if not yet resolved */
207 #ifdef MPLS
208                 if (m->m_flags & M_MPLSLABELED)
209                         eh->ether_type = htons(ETHERTYPE_MPLS);
210                 else
211 #endif
212                         eh->ether_type = htons(ETHERTYPE_IP);
213                 break;
214 #endif
215 #ifdef INET6
216         case AF_INET6:
217                 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
218                         return (0);             /* Something bad happenned. */
219                 eh->ether_type = htons(ETHERTYPE_IPV6);
220                 break;
221 #endif
222 #ifdef IPX
223         case AF_IPX:
224                 if (ef_outputp != NULL) {
225                         error = ef_outputp(ifp, &m, dst, &eh->ether_type,
226                                            &hlen);
227                         if (error)
228                                 goto bad;
229                 } else {
230                         eh->ether_type = htons(ETHERTYPE_IPX);
231                         bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
232                               edst, ETHER_ADDR_LEN);
233                 }
234                 break;
235 #endif
236 #ifdef NETATALK
237         case AF_APPLETALK: {
238                 struct at_ifaddr *aa;
239
240                 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
241                         error = 0;      /* XXX */
242                         goto bad;
243                 }
244                 /*
245                  * In the phase 2 case, need to prepend an mbuf for
246                  * the llc header.  Since we must preserve the value
247                  * of m, which is passed to us by value, we m_copy()
248                  * the first mbuf, and use it for our llc header.
249                  */
250                 if (aa->aa_flags & AFA_PHASE2) {
251                         struct llc llc;
252
253                         M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
254                         eh = mtod(m, struct ether_header *);
255                         edst = eh->ether_dhost;
256                         llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
257                         llc.llc_control = LLC_UI;
258                         bcopy(at_org_code, llc.llc_snap_org_code,
259                               sizeof at_org_code);
260                         llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
261                         bcopy(&llc,
262                               mtod(m, caddr_t) + sizeof(struct ether_header),
263                               sizeof(struct llc));
264                         eh->ether_type = htons(m->m_pkthdr.len);
265                         hlen = sizeof(struct llc) + ETHER_HDR_LEN;
266                 } else {
267                         eh->ether_type = htons(ETHERTYPE_AT);
268                 }
269                 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
270                         return (0);
271                 break;
272           }
273 #endif
274 #ifdef NS
275         case AF_NS:
276                 switch(ns_nettype) {
277                 default:
278                 case 0x8137:    /* Novell Ethernet_II Ethernet TYPE II */
279                         eh->ether_type = 0x8137;
280                         break;
281                 case 0x0:       /* Novell 802.3 */
282                         eh->ether_type = htons(m->m_pkthdr.len);
283                         break;
284                 case 0xe0e0:    /* Novell 802.2 and Token-Ring */
285                         M_PREPEND(m, 3, MB_DONTWAIT);
286                         eh = mtod(m, struct ether_header *);
287                         edst = eh->ether_dhost;
288                         eh->ether_type = htons(m->m_pkthdr.len);
289                         cp = mtod(m, u_char *) + sizeof(struct ether_header);
290                         *cp++ = 0xE0;
291                         *cp++ = 0xE0;
292                         *cp++ = 0x03;
293                         break;
294                 }
295                 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
296                       ETHER_ADDR_LEN);
297                 /*
298                  * XXX if ns_thishost is the same as the node's ethernet
299                  * address then just the default code will catch this anyhow.
300                  * So I'm not sure if this next clause should be here at all?
301                  * [JRE]
302                  */
303                 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
304                         m->m_pkthdr.rcvif = ifp;
305                         netisr_dispatch(NETISR_NS, m);
306                         return (error);
307                 }
308                 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
309                         m->m_flags |= M_BCAST;
310                 break;
311 #endif
312         case pseudo_AF_HDRCMPLT:
313         case AF_UNSPEC:
314                 loop_copy = -1; /* if this is for us, don't do it */
315                 deh = (struct ether_header *)dst->sa_data;
316                 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
317                 eh->ether_type = deh->ether_type;
318                 break;
319
320         default:
321                 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
322                 gotoerr(EAFNOSUPPORT);
323         }
324
325         if (dst->sa_family == pseudo_AF_HDRCMPLT)       /* unlikely */
326                 memcpy(eh->ether_shost,
327                        ((struct ether_header *)dst->sa_data)->ether_shost,
328                        ETHER_ADDR_LEN);
329         else
330                 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
331
332         /*
333          * Bridges require special output handling.
334          */
335         if (ifp->if_bridge) {
336                 KASSERT(bridge_output_p != NULL,
337                         ("%s: if_bridge not loaded!", __func__));
338                 return bridge_output_p(ifp, m);
339         }
340
341         /*
342          * If a simplex interface, and the packet is being sent to our
343          * Ethernet address or a broadcast address, loopback a copy.
344          * XXX To make a simplex device behave exactly like a duplex
345          * device, we should copy in the case of sending to our own
346          * ethernet address (thus letting the original actually appear
347          * on the wire). However, we don't do that here for security
348          * reasons and compatibility with the original behavior.
349          */
350         if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
351                 int csum_flags = 0;
352
353                 if (m->m_pkthdr.csum_flags & CSUM_IP)
354                         csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
355                 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
356                         csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
357                 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
358                         struct mbuf *n;
359
360                         if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
361                                 n->m_pkthdr.csum_flags |= csum_flags;
362                                 if (csum_flags & CSUM_DATA_VALID)
363                                         n->m_pkthdr.csum_data = 0xffff;
364                                 if_simloop(ifp, n, dst->sa_family, hlen);
365                         } else
366                                 ifp->if_iqdrops++;
367                 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
368                                 ETHER_ADDR_LEN) == 0) {
369                         m->m_pkthdr.csum_flags |= csum_flags;
370                         if (csum_flags & CSUM_DATA_VALID)
371                                 m->m_pkthdr.csum_data = 0xffff;
372                         if_simloop(ifp, m, dst->sa_family, hlen);
373                         return (0);     /* XXX */
374                 }
375         }
376
377 #ifdef CARP
378         if (ifp->if_carp && (error = carp_output(ifp, m, dst, NULL)))
379                 goto bad;
380 #endif
381  
382
383         /* Handle ng_ether(4) processing, if any */
384         if (ng_ether_output_p != NULL) {
385                 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
386                         goto bad;
387                 if (m == NULL)
388                         return (0);
389         }
390
391         /* Continue with link-layer output */
392         return ether_output_frame(ifp, m);
393
394 bad:
395         m_freem(m);
396         return (error);
397 }
398
399 /*
400  * Ethernet link layer output routine to send a raw frame to the device.
401  *
402  * This assumes that the 14 byte Ethernet header is present and contiguous
403  * in the first mbuf.
404  */
405 int
406 ether_output_frame(struct ifnet *ifp, struct mbuf *m)
407 {
408         struct ip_fw *rule = NULL;
409         int error = 0;
410         struct altq_pktattr pktattr;
411
412         ASSERT_NOT_SERIALIZED(ifp->if_serializer);
413
414         if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
415                 struct m_tag *mtag;
416
417                 /* Extract info from dummynet tag */
418                 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
419                 KKASSERT(mtag != NULL);
420                 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
421                 KKASSERT(rule != NULL);
422
423                 m_tag_delete(m, mtag);
424                 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
425         }
426
427         if (ifq_is_enabled(&ifp->if_snd))
428                 altq_etherclassify(&ifp->if_snd, m, &pktattr);
429         crit_enter();
430         if (IPFW_LOADED && ether_ipfw != 0) {
431                 struct ether_header save_eh, *eh;
432
433                 eh = mtod(m, struct ether_header *);
434                 save_eh = *eh;
435                 m_adj(m, ETHER_HDR_LEN);
436                 if (!ether_ipfw_chk(&m, ifp, &rule, eh)) {
437                         crit_exit();
438                         if (m != NULL) {
439                                 m_freem(m);
440                                 return ENOBUFS; /* pkt dropped */
441                         } else
442                                 return 0;       /* consumed e.g. in a pipe */
443                 }
444
445                 /* packet was ok, restore the ethernet header */
446                 ether_restore_header(&m, eh, &save_eh);
447                 if (m == NULL) {
448                         crit_exit();
449                         return ENOBUFS;
450                 }
451         }
452         crit_exit();
453
454         /*
455          * Queue message on interface, update output statistics if
456          * successful, and start output if interface not yet active.
457          */
458         error = ifq_dispatch(ifp, m, &pktattr);
459         return (error);
460 }
461
462 /*
463  * ipfw processing for ethernet packets (in and out).
464  * The second parameter is NULL from ether_demux(), and ifp from
465  * ether_output_frame().
466  */
467 static boolean_t
468 ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst, struct ip_fw **rule,
469                const struct ether_header *eh)
470 {
471         struct ether_header save_eh = *eh;      /* might be a ptr in *m0 */
472         struct ip_fw_args args;
473         struct m_tag *mtag;
474         struct mbuf *m;
475         int i;
476
477         if (*rule != NULL && fw_one_pass)
478                 return TRUE; /* dummynet packet, already partially processed */
479
480         /*
481          * I need some amount of data to be contiguous.
482          */
483         i = min((*m0)->m_pkthdr.len, max_protohdr);
484         if ((*m0)->m_len < i) {
485                 *m0 = m_pullup(*m0, i);
486                 if (*m0 == NULL)
487                         return FALSE;
488         }
489
490         /*
491          * Clean up tags
492          */
493         if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
494                 m_tag_delete(*m0, mtag);
495         if ((*m0)->m_pkthdr.fw_flags & IPFORWARD_MBUF_TAGGED) {
496                 mtag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
497                 KKASSERT(mtag != NULL);
498                 m_tag_delete(*m0, mtag);
499                 (*m0)->m_pkthdr.fw_flags &= ~IPFORWARD_MBUF_TAGGED;
500         }
501
502         args.m = *m0;           /* the packet we are looking at         */
503         args.oif = dst;         /* destination, if any                  */
504         args.rule = *rule;      /* matching rule to restart             */
505         args.eh = &save_eh;     /* MAC header for bridged/MAC packets   */
506         i = ip_fw_chk_ptr(&args);
507         *m0 = args.m;
508         *rule = args.rule;
509
510         if (*m0 == NULL)
511                 return FALSE;
512
513         switch (i) {
514         case IP_FW_PASS:
515                 return TRUE;
516
517         case IP_FW_DIVERT:
518         case IP_FW_TEE:
519         case IP_FW_DENY:
520                 /*
521                  * XXX at some point add support for divert/forward actions.
522                  * If none of the above matches, we have to drop the pkt.
523                  */
524                 return FALSE;
525
526         case IP_FW_DUMMYNET:
527                 /*
528                  * Pass the pkt to dummynet, which consumes it.
529                  */
530                 m = *m0;        /* pass the original to dummynet */
531                 *m0 = NULL;     /* and nothing back to the caller */
532
533                 ether_restore_header(&m, eh, &save_eh);
534                 if (m == NULL)
535                         return FALSE;
536
537                 ip_fw_dn_io_ptr(m, args.cookie,
538                                 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
539                 ip_dn_queue(m);
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_chain(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 static void
959 ether_input_ipifunc(void *arg)
960 {
961         struct mbuf *m, *next;
962         lwkt_port_t port;
963
964         m = arg;
965         do {
966                 next = m->m_nextpkt;
967                 m->m_nextpkt = NULL;
968
969                 port = m->m_pkthdr.header;
970                 m->m_pkthdr.header = NULL;
971
972                 lwkt_sendmsg(port,
973                 &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
974
975                 m = next;
976         } while (m != NULL);
977 }
978
979 void
980 ether_input_dispatch(struct mbuf_chain *chain)
981 {
982 #ifdef SMP
983         int i;
984
985         for (i = 0; i < ncpus; ++i) {
986                 if (chain[i].mc_head != NULL) {
987                         lwkt_send_ipiq(globaldata_find(i),
988                         ether_input_ipifunc, chain[i].mc_head);
989                 }
990         }
991 #else
992         if (chain->mc_head != NULL)
993                 ether_input_ipifunc(chain->mc_head);
994 #endif
995 }
996
997 void
998 ether_input_chain_init(struct mbuf_chain *chain)
999 {
1000 #ifdef SMP
1001         int i;
1002
1003         for (i = 0; i < ncpus; ++i)
1004                 chain[i].mc_head = chain[i].mc_tail = NULL;
1005 #else
1006         chain->mc_head = chain->mc_tail = NULL;
1007 #endif
1008 }
1009
1010 /*
1011  * Upper layer processing for a received Ethernet packet.
1012  */
1013 void
1014 ether_demux_oncpu(struct ifnet *ifp, struct mbuf *m)
1015 {
1016         struct ether_header *eh;
1017         int isr, redispatch;
1018         u_short ether_type;
1019         struct ip_fw *rule = NULL;
1020 #ifdef NETATALK
1021         struct llc *l;
1022 #endif
1023
1024         M_ASSERTPKTHDR(m);
1025         KASSERT(m->m_len >= ETHER_HDR_LEN,
1026                 ("ether header is no contiguous!\n"));
1027
1028         eh = mtod(m, struct ether_header *);
1029
1030         if (m->m_pkthdr.fw_flags & DUMMYNET_MBUF_TAGGED) {
1031                 struct m_tag *mtag;
1032
1033                 /* Extract info from dummynet tag */
1034                 mtag = m_tag_find(m, PACKET_TAG_DUMMYNET, NULL);
1035                 KKASSERT(mtag != NULL);
1036                 rule = ((struct dn_pkt *)m_tag_data(mtag))->dn_priv;
1037                 KKASSERT(rule != NULL);
1038
1039                 m_tag_delete(m, mtag);
1040                 m->m_pkthdr.fw_flags &= ~DUMMYNET_MBUF_TAGGED;
1041
1042                 /* packet is passing the second time */
1043                 goto post_stats;
1044         }
1045
1046 #ifdef CARP
1047         /*
1048          * XXX: Okay, we need to call carp_forus() and - if it is for
1049          * us jump over code that does the normal check
1050          * "ac_enaddr == ether_dhost". The check sequence is a bit
1051          * different from OpenBSD, so we jump over as few code as
1052          * possible, to catch _all_ sanity checks. This needs
1053          * evaluation, to see if the carp ether_dhost values break any
1054          * of these checks!
1055          */
1056         if (ifp->if_carp && carp_forus(ifp->if_carp, eh->ether_dhost))
1057                 goto post_stats;
1058 #endif
1059
1060         /*
1061          * Discard packet if upper layers shouldn't see it because
1062          * it was unicast to a different Ethernet address.  If the
1063          * driver is working properly, then this situation can only
1064          * happen when the interface is in promiscuous mode.
1065          */
1066         if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
1067             (eh->ether_dhost[0] & 1) == 0 &&
1068             bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
1069                 m_freem(m);
1070                 return;
1071         }
1072
1073 post_stats:
1074         if (IPFW_LOADED && ether_ipfw != 0) {
1075                 struct ether_header save_eh = *eh;
1076
1077                 /* XXX old crufty stuff, needs to be removed */
1078                 m_adj(m, sizeof(struct ether_header));
1079
1080                 if (!ether_ipfw_chk(&m, NULL, &rule, eh)) {
1081                         m_freem(m);
1082                         return;
1083                 }
1084
1085                 ether_restore_header(&m, eh, &save_eh);
1086                 if (m == NULL)
1087                         return;
1088                 eh = mtod(m, struct ether_header *);
1089         }
1090
1091         ether_type = ntohs(eh->ether_type);
1092         KKASSERT(ether_type != ETHERTYPE_VLAN);
1093
1094         if (m->m_flags & M_VLANTAG) {
1095                 if (vlan_input_p != NULL) {
1096                         vlan_input_p(m);
1097                 } else {
1098                         m->m_pkthdr.rcvif->if_noproto++;
1099                         m_freem(m);
1100                 }
1101                 return;
1102         }
1103
1104         m_adj(m, sizeof(struct ether_header));
1105         redispatch = 0;
1106
1107         switch (ether_type) {
1108 #ifdef INET
1109         case ETHERTYPE_IP:
1110                 if (ipflow_fastforward(m))
1111                         return;
1112                 isr = NETISR_IP;
1113                 break;
1114
1115         case ETHERTYPE_ARP:
1116                 if (ifp->if_flags & IFF_NOARP) {
1117                         /* Discard packet if ARP is disabled on interface */
1118                         m_freem(m);
1119                         return;
1120                 }
1121                 isr = NETISR_ARP;
1122                 break;
1123 #endif
1124
1125 #ifdef INET6
1126         case ETHERTYPE_IPV6:
1127                 isr = NETISR_IPV6;
1128                 break;
1129 #endif
1130
1131 #ifdef IPX
1132         case ETHERTYPE_IPX:
1133                 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
1134                         return;
1135                 isr = NETISR_IPX;
1136                 break;
1137 #endif
1138
1139 #ifdef NS
1140         case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1141                 isr = NETISR_NS;
1142                 break;
1143
1144 #endif
1145
1146 #ifdef NETATALK
1147         case ETHERTYPE_AT:
1148                 isr = NETISR_ATALK1;
1149                 break;
1150         case ETHERTYPE_AARP:
1151                 isr = NETISR_AARP;
1152                 break;
1153 #endif
1154
1155 #ifdef MPLS
1156         case ETHERTYPE_MPLS:
1157         case ETHERTYPE_MPLS_MCAST:
1158                 /* Should have been set by ether_input_chain(). */
1159                 KKASSERT(m->m_flags & M_MPLSLABELED);
1160                 isr = NETISR_MPLS;
1161                 break;
1162 #endif
1163
1164         default:
1165                 /*
1166                  * The accurate msgport is not determined before
1167                  * we reach here, so redo the dispatching
1168                  */
1169                 redispatch = 1;
1170 #ifdef IPX
1171                 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
1172                         return;
1173 #endif
1174 #ifdef NS
1175                 checksum = mtod(m, ushort *);
1176                 /* Novell 802.3 */
1177                 if ((ether_type <= ETHERMTU) &&
1178                     ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
1179                         if (*checksum == 0xE0E0) {
1180                                 m->m_pkthdr.len -= 3;
1181                                 m->m_len -= 3;
1182                                 m->m_data += 3;
1183                         }
1184                         isr = NETISR_NS;
1185                         break;
1186                 }
1187 #endif
1188 #ifdef NETATALK
1189                 if (ether_type > ETHERMTU)
1190                         goto dropanyway;
1191                 l = mtod(m, struct llc *);
1192                 if (l->llc_dsap == LLC_SNAP_LSAP &&
1193                     l->llc_ssap == LLC_SNAP_LSAP &&
1194                     l->llc_control == LLC_UI) {
1195                         if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
1196                                  sizeof at_org_code) == 0 &&
1197                             ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
1198                                 m_adj(m, sizeof(struct llc));
1199                                 isr = NETISR_ATALK2;
1200                                 break;
1201                         }
1202                         if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
1203                                  sizeof aarp_org_code) == 0 &&
1204                             ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
1205                                 m_adj(m, sizeof(struct llc));
1206                                 isr = NETISR_AARP;
1207                                 break;
1208                         }
1209                 }
1210 dropanyway:
1211 #endif
1212                 if (ng_ether_input_orphan_p != NULL)
1213                         ng_ether_input_orphan_p(ifp, m, eh);
1214                 else
1215                         m_freem(m);
1216                 return;
1217         }
1218
1219         if (!redispatch)
1220                 netisr_run(isr, m);
1221         else
1222                 netisr_dispatch(isr, m);
1223 }
1224
1225 /*
1226  * First we perform any link layer operations, then continue to the
1227  * upper layers with ether_demux_oncpu().
1228  */
1229 void
1230 ether_input_oncpu(struct ifnet *ifp, struct mbuf *m)
1231 {
1232         if ((ifp->if_flags & (IFF_UP | IFF_MONITOR)) != IFF_UP) {
1233                 /*
1234                  * Receiving interface's flags are changed, when this
1235                  * packet is waiting for processing; discard it.
1236                  */
1237                 m_freem(m);
1238                 return;
1239         }
1240
1241         /*
1242          * Tap the packet off here for a bridge.  bridge_input()
1243          * will return NULL if it has consumed the packet, otherwise
1244          * it gets processed as normal.  Note that bridge_input()
1245          * will always return the original packet if we need to
1246          * process it locally.
1247          */
1248         if (ifp->if_bridge) {
1249                 KASSERT(bridge_input_p != NULL,
1250                         ("%s: if_bridge not loaded!", __func__));
1251
1252                 if(m->m_flags & M_PROTO1) {
1253                         m->m_flags &= ~M_PROTO1;
1254                 } else {
1255                         /* clear M_PROMISC, in case the packets comes from a vlan */
1256                         /* m->m_flags &= ~M_PROMISC; */
1257                         m = bridge_input_p(ifp, m);
1258                         if (m == NULL)
1259                                 return;
1260
1261                         KASSERT(ifp == m->m_pkthdr.rcvif,
1262                                 ("bridge_input_p changed rcvif\n"));
1263                 }
1264         }
1265
1266         /* Handle ng_ether(4) processing, if any */
1267         if (ng_ether_input_p != NULL) {
1268                 ng_ether_input_p(ifp, &m);
1269                 if (m == NULL)
1270                         return;
1271         }
1272
1273         /* Continue with upper layer processing */
1274         ether_demux_oncpu(ifp, m);
1275 }
1276
1277 static void
1278 ether_input_handler(struct netmsg *nmsg)
1279 {
1280         struct netmsg_packet *nmp = (struct netmsg_packet *)nmsg;
1281         struct ifnet *ifp;
1282         struct mbuf *m;
1283
1284         m = nmp->nm_packet;
1285         M_ASSERTPKTHDR(m);
1286         ifp = m->m_pkthdr.rcvif;
1287
1288         ether_input_oncpu(ifp, m);
1289 }
1290
1291 static __inline void
1292 ether_init_netpacket(int num, struct mbuf *m)
1293 {
1294         struct netmsg_packet *pmsg;
1295
1296         pmsg = &m->m_hdr.mh_netmsg;
1297         netmsg_init(&pmsg->nm_netmsg, &netisr_apanic_rport, 0,
1298                     ether_input_handler);
1299         pmsg->nm_packet = m;
1300         pmsg->nm_netmsg.nm_lmsg.u.ms_result = num;
1301 }
1302
1303 static __inline struct lwkt_port *
1304 ether_mport(int num, struct mbuf **m)
1305 {
1306         if (num == NETISR_MAX) {
1307                 /*
1308                  * All packets whose target msgports can't be
1309                  * determined here are dispatched to netisr0,
1310                  * where further dispatching may happen.
1311                  */
1312                 return cpu_portfn(0);
1313         }
1314         return netisr_find_port(num, m);
1315 }
1316
1317 /*
1318  * Process a received Ethernet packet.
1319  *
1320  * The ethernet header is assumed to be in the mbuf so the caller
1321  * MUST MAKE SURE that there are at least sizeof(struct ether_header)
1322  * bytes in the first mbuf.
1323  *
1324  * We first try to find the target msgport for this ether frame, if
1325  * there is no target msgport for it, this ether frame is discarded,
1326  * else we do following processing according to whether 'chain' is
1327  * NULL or not:
1328  * - If 'chain' is NULL, this ether frame is sent to the target msgport
1329  *   immediately.  This situation happens when ether_input_chain is
1330  *   accessed through ifnet.if_input.
1331  * - If 'chain' is not NULL, this ether frame is queued to the 'chain'
1332  *   bucket indexed by the target msgport's cpuid and the target msgport
1333  *   is saved in mbuf's m_pkthdr.m_head.  Caller of ether_input_chain
1334  *   must initialize 'chain' by calling ether_input_chain_init().
1335  *   ether_input_dispatch must be called later to send ether frames
1336  *   queued on 'chain' to their target msgport.
1337  */
1338 void
1339 ether_input_chain(struct ifnet *ifp, struct mbuf *m, struct mbuf_chain *chain)
1340 {
1341         struct ether_header *eh, *save_eh, save_eh0;
1342         struct lwkt_port *port;
1343         uint16_t ether_type;
1344         int isr;
1345
1346         ASSERT_SERIALIZED(ifp->if_serializer);
1347         M_ASSERTPKTHDR(m);
1348
1349         /* Discard packet if interface is not up */
1350         if (!(ifp->if_flags & IFF_UP)) {
1351                 m_freem(m);
1352                 return;
1353         }
1354
1355         if (m->m_len < sizeof(struct ether_header)) {
1356                 /* XXX error in the caller. */
1357                 m_freem(m);
1358                 return;
1359         }
1360         eh = mtod(m, struct ether_header *);
1361
1362         m->m_pkthdr.rcvif = ifp;
1363
1364         if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
1365                 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
1366                          ifp->if_addrlen) == 0)
1367                         m->m_flags |= M_BCAST;
1368                 else
1369                         m->m_flags |= M_MCAST;
1370                 ifp->if_imcasts++;
1371         }
1372
1373         ETHER_BPF_MTAP(ifp, m);
1374
1375         ifp->if_ibytes += m->m_pkthdr.len;
1376
1377         if (ifp->if_flags & IFF_MONITOR) {
1378                 /*
1379                  * Interface marked for monitoring; discard packet.
1380                  */
1381                 m_freem(m);
1382                 return;
1383         }
1384
1385         if (ntohs(eh->ether_type) == ETHERTYPE_VLAN &&
1386             (m->m_flags & M_VLANTAG) == 0) {
1387                 /*
1388                  * Extract vlan tag if hardware does not do it for us
1389                  */
1390                 vlan_ether_decap(&m);
1391                 if (m == NULL)
1392                         return;
1393                 eh = mtod(m, struct ether_header *);
1394         }
1395         ether_type = ntohs(eh->ether_type);
1396
1397         if ((m->m_flags & M_VLANTAG) && ether_type == ETHERTYPE_VLAN) {
1398                 /*
1399                  * To prevent possible dangerous recursion,
1400                  * we don't do vlan-in-vlan
1401                  */
1402                 ifp->if_noproto++;
1403                 m_freem(m);
1404                 return;
1405         }
1406         KKASSERT(ether_type != ETHERTYPE_VLAN);
1407
1408         /*
1409          * Map ether type to netisr id.
1410          */
1411         switch (ether_type) {
1412 #ifdef INET
1413         case ETHERTYPE_IP:
1414                 isr = NETISR_IP;
1415                 break;
1416
1417         case ETHERTYPE_ARP:
1418                 isr = NETISR_ARP;
1419                 break;
1420 #endif
1421
1422 #ifdef INET6
1423         case ETHERTYPE_IPV6:
1424                 isr = NETISR_IPV6;
1425                 break;
1426 #endif
1427
1428 #ifdef IPX
1429         case ETHERTYPE_IPX:
1430                 isr = NETISR_IPX;
1431                 break;
1432 #endif
1433
1434 #ifdef NS
1435         case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
1436                 isr = NETISR_NS;
1437                 break;
1438 #endif
1439
1440 #ifdef NETATALK
1441         case ETHERTYPE_AT:
1442                 isr = NETISR_ATALK1;
1443                 break;
1444         case ETHERTYPE_AARP:
1445                 isr = NETISR_AARP;
1446                 break;
1447 #endif
1448
1449 #ifdef MPLS
1450         case ETHERTYPE_MPLS:
1451         case ETHERTYPE_MPLS_MCAST:
1452                 m->m_flags |= M_MPLSLABELED;
1453                 isr = NETISR_MPLS;
1454                 break;
1455 #endif
1456
1457         default:
1458                 /*
1459                  * NETISR_MAX is an invalid value; it is chosen to let
1460                  * ether_mport() know that we are not able to decide
1461                  * this packet's msgport here.
1462                  */
1463                 isr = NETISR_MAX;
1464                 break;
1465         }
1466
1467         /*
1468          * If the packet is in contiguous memory, following
1469          * m_adj() could ensure that the hidden ether header
1470          * will not be destroyed, else we will have to save
1471          * the ether header for the later restoration.
1472          */
1473         if (m->m_pkthdr.len != m->m_len) {
1474                 save_eh0 = *eh;
1475                 save_eh = &save_eh0;
1476         } else {
1477                 save_eh = NULL;
1478         }
1479
1480         /*
1481          * Temporarily remove ether header; ether_mport()
1482          * expects a packet without ether header.
1483          */
1484         m_adj(m, sizeof(struct ether_header));
1485
1486         /*
1487          * Find the packet's target msgport.
1488          */
1489         port = ether_mport(isr, &m);
1490         if (port == NULL) {
1491                 KKASSERT(m == NULL);
1492                 return;
1493         }
1494
1495         /*
1496          * Restore ether header.
1497          */
1498         if (save_eh != NULL) {
1499                 ether_restore_header(&m, eh, save_eh);
1500                 if (m == NULL)
1501                         return;
1502         } else {
1503                 m->m_data -= ETHER_HDR_LEN;
1504                 m->m_len += ETHER_HDR_LEN;
1505                 m->m_pkthdr.len += ETHER_HDR_LEN;
1506         }
1507
1508         /*
1509          * Initialize mbuf's netmsg packet _after_ possible
1510          * ether header restoration, else the initialized
1511          * netmsg packet may be lost during ether header
1512          * restoration.
1513          */
1514         ether_init_netpacket(isr, m);
1515
1516         if (chain != NULL) {
1517                 struct mbuf_chain *c;
1518                 int cpuid;
1519
1520                 m->m_pkthdr.header = port; /* XXX */
1521                 cpuid = port->mpu_td->td_gd->gd_cpuid;
1522
1523                 c = &chain[cpuid];
1524                 if (c->mc_head == NULL) {
1525                         c->mc_head = c->mc_tail = m;
1526                 } else {
1527                         c->mc_tail->m_nextpkt = m;
1528                         c->mc_tail = m;
1529                 }
1530                 m->m_nextpkt = NULL;
1531         } else {
1532                 lwkt_sendmsg(port, &m->m_hdr.mh_netmsg.nm_netmsg.nm_lmsg);
1533         }
1534 }