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