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