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