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