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