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