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