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