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