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