Delete unused variable
[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 $
6e8af7f0 35 * $DragonFly: src/sys/net/if_ethersubr.c,v 1.37 2006/07/02 00:49:22 corecode Exp $
984263bc
MD
36 */
37
38#include "opt_atalk.h"
39#include "opt_inet.h"
40#include "opt_inet6.h"
41#include "opt_ipx.h"
984263bc
MD
42#include "opt_netgraph.h"
43
44#include <sys/param.h>
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/malloc.h>
48#include <sys/mbuf.h>
49#include <sys/socket.h>
50#include <sys/sockio.h>
51#include <sys/sysctl.h>
52
53#include <net/if.h>
54#include <net/netisr.h>
55#include <net/route.h>
56#include <net/if_llc.h>
57#include <net/if_dl.h>
58#include <net/if_types.h>
4d723e5a 59#include <net/ifq_var.h>
984263bc
MD
60#include <net/bpf.h>
61#include <net/ethernet.h>
984263bc
MD
62
63#if defined(INET) || defined(INET6)
64#include <netinet/in.h>
65#include <netinet/in_var.h>
66#include <netinet/if_ether.h>
1f2de5d4
MD
67#include <net/ipfw/ip_fw.h>
68#include <net/dummynet/ip_dummynet.h>
984263bc
MD
69#endif
70#ifdef INET6
71#include <netinet6/nd6.h>
72#endif
73
74#ifdef IPX
d2438d69
MD
75#include <netproto/ipx/ipx.h>
76#include <netproto/ipx/ipx_if.h>
984263bc 77int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
f23061d4
JH
78int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
79 short *tp, int *hlen);
984263bc
MD
80#endif
81
82#ifdef NS
83#include <netns/ns.h>
84#include <netns/ns_if.h>
85ushort ns_nettype;
86int ether_outputdebug = 0;
87int ether_inputdebug = 0;
88#endif
89
90#ifdef NETATALK
d2438d69
MD
91#include <netproto/atalk/at.h>
92#include <netproto/atalk/at_var.h>
93#include <netproto/atalk/at_extern.h>
984263bc 94
f23061d4
JH
95#define llc_snap_org_code llc_un.type_snap.org_code
96#define llc_snap_ether_type llc_un.type_snap.ether_type
984263bc
MD
97
98extern u_char at_org_code[3];
99extern u_char aarp_org_code[3];
100#endif /* NETATALK */
101
102/* netgraph node hooks for ng_ether(4) */
103void (*ng_ether_input_p)(struct ifnet *ifp,
104 struct mbuf **mp, struct ether_header *eh);
105void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
106 struct mbuf *m, struct ether_header *eh);
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
111int (*vlan_input_p)(struct ether_header *eh, struct mbuf *m);
3013ac0e
JS
112int (*vlan_input_tag_p)(struct mbuf *m, uint16_t t);
113
5fe66e68
JH
114static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
115 struct rtentry *);
984263bc 116
f23061d4 117/*
a8d45119 118 * if_bridge support
f23061d4 119 */
db37145f
SS
120struct mbuf *(*bridge_input_p)(struct ifnet *, struct mbuf *);
121int (*bridge_output_p)(struct ifnet *, struct mbuf *,
122 struct sockaddr *, struct rtentry *);
123void (*bridge_dn_p)(struct mbuf *, struct ifnet *);
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
JH
135static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
136 struct ip_fw **rule, struct ether_header *eh,
137 boolean_t shared);
138
984263bc 139static int ether_ipfw;
5fe66e68
JH
140SYSCTL_DECL(_net_link);
141SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
142SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
143 &ether_ipfw, 0, "Pass ether pkts through firewall");
984263bc
MD
144
145/*
146 * Ethernet output routine.
147 * Encapsulate a packet of type family for the local net.
148 * Use trailer local net encapsulation if enough data in first
149 * packet leaves a multiple of 512 bytes of data in remainder.
150 * Assumes that ifp is actually pointer to arpcom structure.
151 */
3013ac0e
JS
152static int
153ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
f23061d4 154 struct rtentry *rt)
984263bc 155{
f23061d4
JH
156 struct ether_header *eh, *deh;
157 u_char *edst;
984263bc 158 int loop_copy = 0;
f23061d4 159 int hlen = ETHER_HDR_LEN; /* link layer header length */
984263bc 160 struct arpcom *ac = IFP2AC(ifp);
f23061d4 161 int error;
984263bc 162
78195a76
MD
163 ASSERT_SERIALIZED(ifp->if_serializer);
164
f23061d4
JH
165 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING))
166 gotoerr(ENETDOWN);
167
168 M_PREPEND(m, sizeof(struct ether_header), MB_DONTWAIT);
169 if (m == NULL)
5fe66e68 170 return (ENOBUFS);
f23061d4
JH
171 eh = mtod(m, struct ether_header *);
172 edst = eh->ether_dhost;
173
5fe66e68
JH
174 /*
175 * Fill in the destination ethernet address and frame type.
176 */
984263bc
MD
177 switch (dst->sa_family) {
178#ifdef INET
179 case AF_INET:
f23061d4 180 if (!arpresolve(ifp, rt, m, dst, edst))
984263bc 181 return (0); /* if not yet resolved */
f23061d4 182 eh->ether_type = htons(ETHERTYPE_IP);
984263bc
MD
183 break;
184#endif
185#ifdef INET6
186 case AF_INET6:
f23061d4 187 if (!nd6_storelladdr(&ac->ac_if, rt, m, dst, edst))
5fe66e68 188 return (0); /* Something bad happenned. */
f23061d4 189 eh->ether_type = htons(ETHERTYPE_IPV6);
984263bc
MD
190 break;
191#endif
192#ifdef IPX
193 case AF_IPX:
f23061d4
JH
194 if (ef_outputp != NULL) {
195 error = ef_outputp(ifp, &m, dst, &eh->ether_type,
196 &hlen);
197 if (error)
198 goto bad;
199 } else {
200 eh->ether_type = htons(ETHERTYPE_IPX);
201 bcopy(&(((struct sockaddr_ipx *)dst)->sipx_addr.x_host),
202 edst, ETHER_ADDR_LEN);
203 }
984263bc
MD
204 break;
205#endif
206#ifdef NETATALK
f23061d4
JH
207 case AF_APPLETALK: {
208 struct at_ifaddr *aa;
209
210 if ((aa = at_ifawithnet((struct sockaddr_at *)dst)) == NULL) {
211 error = 0; /* XXX */
212 goto bad;
213 }
214 /*
215 * In the phase 2 case, need to prepend an mbuf for
216 * the llc header. Since we must preserve the value
217 * of m, which is passed to us by value, we m_copy()
218 * the first mbuf, and use it for our llc header.
219 */
220 if (aa->aa_flags & AFA_PHASE2) {
221 struct llc llc;
222
223 M_PREPEND(m, sizeof(struct llc), MB_DONTWAIT);
5fe66e68
JH
224 eh = mtod(m, struct ether_header *);
225 edst = eh->ether_dhost;
f23061d4
JH
226 llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
227 llc.llc_control = LLC_UI;
228 bcopy(at_org_code, llc.llc_snap_org_code,
229 sizeof at_org_code);
230 llc.llc_snap_ether_type = htons(ETHERTYPE_AT);
5fe66e68
JH
231 bcopy(&llc,
232 mtod(m, caddr_t) + sizeof(struct ether_header),
233 sizeof(struct llc));
f23061d4
JH
234 eh->ether_type = htons(m->m_pkthdr.len);
235 hlen = sizeof(struct llc) + ETHER_HDR_LEN;
236 } else {
237 eh->ether_type = htons(ETHERTYPE_AT);
238 }
239 if (!aarpresolve(ac, m, (struct sockaddr_at *)dst, edst))
240 return (0);
241 break;
984263bc 242 }
5fe66e68 243#endif
984263bc
MD
244#ifdef NS
245 case AF_NS:
f23061d4 246 switch(ns_nettype) {
984263bc 247 default:
f23061d4
JH
248 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
249 eh->ether_type = 0x8137;
984263bc 250 break;
f23061d4
JH
251 case 0x0: /* Novell 802.3 */
252 eh->ether_type = htons(m->m_pkthdr.len);
984263bc 253 break;
f23061d4
JH
254 case 0xe0e0: /* Novell 802.2 and Token-Ring */
255 M_PREPEND(m, 3, MB_DONTWAIT);
5fe66e68
JH
256 eh = mtod(m, struct ether_header *);
257 edst = eh->ether_dhost;
f23061d4 258 eh->ether_type = htons(m->m_pkthdr.len);
5fe66e68 259 cp = mtod(m, u_char *) + sizeof(struct ether_header);
984263bc
MD
260 *cp++ = 0xE0;
261 *cp++ = 0xE0;
262 *cp++ = 0x03;
263 break;
264 }
f23061d4
JH
265 bcopy(&(((struct sockaddr_ns *)dst)->sns_addr.x_host), edst,
266 ETHER_ADDR_LEN);
984263bc
MD
267 /*
268 * XXX if ns_thishost is the same as the node's ethernet
269 * address then just the default code will catch this anyhow.
270 * So I'm not sure if this next clause should be here at all?
271 * [JRE]
272 */
f23061d4 273 if (bcmp(edst, &ns_thishost, ETHER_ADDR_LEN) == 0) {
984263bc 274 m->m_pkthdr.rcvif = ifp;
8bde602d 275 netisr_dispatch(NETISR_NS, m);
984263bc
MD
276 return (error);
277 }
f23061d4 278 if (bcmp(edst, &ns_broadhost, ETHER_ADDR_LEN) == 0)
984263bc 279 m->m_flags |= M_BCAST;
984263bc 280 break;
5fe66e68 281#endif
984263bc 282 case pseudo_AF_HDRCMPLT:
984263bc
MD
283 case AF_UNSPEC:
284 loop_copy = -1; /* if this is for us, don't do it */
f23061d4
JH
285 deh = (struct ether_header *)dst->sa_data;
286 memcpy(edst, deh->ether_dhost, ETHER_ADDR_LEN);
287 eh->ether_type = deh->ether_type;
984263bc
MD
288 break;
289
290 default:
3e4a09e7 291 printf("%s: can't handle af%d\n", ifp->if_xname,
984263bc 292 dst->sa_family);
f23061d4 293 gotoerr(EAFNOSUPPORT);
984263bc
MD
294 }
295
f23061d4
JH
296 if (dst->sa_family == pseudo_AF_HDRCMPLT) /* unlikely */
297 memcpy(eh->ether_shost,
298 ((struct ether_header *)dst->sa_data)->ether_shost,
299 ETHER_ADDR_LEN);
984263bc 300 else
f23061d4 301 memcpy(eh->ether_shost, ac->ac_enaddr, ETHER_ADDR_LEN);
984263bc 302
db37145f
SS
303 /*
304 * Bridges require special output handling.
305 */
306 if (ifp->if_bridge) {
307 KASSERT(bridge_output_p != NULL,("ether_input: if_bridge not loaded!"));
308 return ((*bridge_output_p)(ifp, m, NULL, NULL));
309 }
310
984263bc
MD
311 /*
312 * If a simplex interface, and the packet is being sent to our
313 * Ethernet address or a broadcast address, loopback a copy.
314 * XXX To make a simplex device behave exactly like a duplex
315 * device, we should copy in the case of sending to our own
316 * ethernet address (thus letting the original actually appear
317 * on the wire). However, we don't do that here for security
318 * reasons and compatibility with the original behavior.
319 */
320 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
321 int csum_flags = 0;
322
323 if (m->m_pkthdr.csum_flags & CSUM_IP)
f23061d4 324 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
984263bc 325 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
f23061d4 326 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
984263bc
MD
327 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
328 struct mbuf *n;
329
f23061d4 330 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
984263bc
MD
331 n->m_pkthdr.csum_flags |= csum_flags;
332 if (csum_flags & CSUM_DATA_VALID)
333 n->m_pkthdr.csum_data = 0xffff;
f23061d4 334 if_simloop(ifp, n, dst->sa_family, hlen);
984263bc
MD
335 } else
336 ifp->if_iqdrops++;
f23061d4
JH
337 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
338 ETHER_ADDR_LEN) == 0) {
984263bc
MD
339 m->m_pkthdr.csum_flags |= csum_flags;
340 if (csum_flags & CSUM_DATA_VALID)
341 m->m_pkthdr.csum_data = 0xffff;
f23061d4 342 if_simloop(ifp, m, dst->sa_family, hlen);
984263bc
MD
343 return (0); /* XXX */
344 }
345 }
346
347 /* Handle ng_ether(4) processing, if any */
348 if (ng_ether_output_p != NULL) {
0c3c561c
JH
349 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
350 goto bad;
984263bc
MD
351 if (m == NULL)
352 return (0);
353 }
354
355 /* Continue with link-layer output */
356 return ether_output_frame(ifp, m);
0c3c561c
JH
357
358bad:
359 m_freem(m);
360 return (error);
984263bc
MD
361}
362
363/*
364 * Ethernet link layer output routine to send a raw frame to the device.
365 *
366 * This assumes that the 14 byte Ethernet header is present and contiguous
a8d45119 367 * in the first mbuf.
984263bc
MD
368 */
369int
f23061d4 370ether_output_frame(struct ifnet *ifp, struct mbuf *m)
984263bc 371{
f23061d4 372 struct ip_fw *rule = NULL;
984263bc 373 int error = 0;
4d723e5a 374 struct altq_pktattr pktattr;
984263bc 375
78195a76
MD
376 ASSERT_SERIALIZED(ifp->if_serializer);
377
984263bc 378 /* Extract info from dummynet tag, ignore others */
5fe66e68
JH
379 while (m->m_type == MT_TAG) {
380 if (m->m_flags == PACKET_TAG_DUMMYNET) {
984263bc 381 rule = ((struct dn_pkt *)m)->rule;
5fe66e68
JH
382 break;
383 }
384 m = m->m_next;
385 }
984263bc 386
4d723e5a
JS
387 if (ifq_is_enabled(&ifp->if_snd))
388 altq_etherclassify(&ifp->if_snd, m, &pktattr);
4986965b 389 crit_enter();
984263bc
MD
390 if (IPFW_LOADED && ether_ipfw != 0) {
391 struct ether_header save_eh, *eh;
392
393 eh = mtod(m, struct ether_header *);
394 save_eh = *eh;
395 m_adj(m, ETHER_HDR_LEN);
5fe66e68 396 if (!ether_ipfw_chk(&m, ifp, &rule, eh, FALSE)) {
4986965b 397 crit_exit();
5fe66e68 398 if (m != NULL) {
984263bc 399 m_freem(m);
f23061d4 400 return ENOBUFS; /* pkt dropped */
984263bc
MD
401 } else
402 return 0; /* consumed e.g. in a pipe */
403 }
5fe66e68 404 eh = mtod(m, struct ether_header *);
984263bc 405 /* packet was ok, restore the ethernet header */
f23061d4 406 if ((void *)(eh + 1) == (void *)m->m_data) {
984263bc
MD
407 m->m_data -= ETHER_HDR_LEN ;
408 m->m_len += ETHER_HDR_LEN ;
409 m->m_pkthdr.len += ETHER_HDR_LEN ;
410 } else {
74f1caca 411 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
4986965b
JS
412 if (m == NULL) /* nope... */ {
413 crit_exit();
984263bc 414 return ENOBUFS;
4986965b 415 }
984263bc 416 bcopy(&save_eh, mtod(m, struct ether_header *),
5fe66e68 417 ETHER_HDR_LEN);
984263bc
MD
418 }
419 }
78195a76 420 crit_exit();
984263bc
MD
421
422 /*
423 * Queue message on interface, update output statistics if
424 * successful, and start output if interface not yet active.
425 */
4d723e5a 426 error = ifq_handoff(ifp, m, &pktattr);
984263bc
MD
427 return (error);
428}
429
430/*
431 * ipfw processing for ethernet packets (in and out).
5fe66e68 432 * The second parameter is NULL from ether_demux(), and ifp from
a8d45119 433 * ether_output_frame().
984263bc 434 */
5fe66e68 435static boolean_t
f23061d4
JH
436ether_ipfw_chk(
437 struct mbuf **m0,
438 struct ifnet *dst,
439 struct ip_fw **rule,
440 struct ether_header *eh,
5fe66e68 441 boolean_t shared)
984263bc
MD
442{
443 struct ether_header save_eh = *eh; /* might be a ptr in m */
984263bc 444 struct ip_fw_args args;
e5ecc832 445 struct m_tag *mtag;
f23061d4 446 int i;
984263bc
MD
447
448 if (*rule != NULL && fw_one_pass)
5fe66e68 449 return TRUE; /* dummynet packet, already partially processed */
984263bc
MD
450
451 /*
5fe66e68
JH
452 * I need some amount of data to be contiguous, and in case others
453 * need the packet (shared==TRUE), it also better be in the first mbuf.
984263bc 454 */
f23061d4
JH
455 i = min((*m0)->m_pkthdr.len, max_protohdr);
456 if (shared || (*m0)->m_len < i) {
984263bc
MD
457 *m0 = m_pullup(*m0, i);
458 if (*m0 == NULL)
5fe66e68 459 return FALSE;
984263bc
MD
460 }
461
462 args.m = *m0; /* the packet we are looking at */
463 args.oif = dst; /* destination, if any */
e5ecc832
JS
464 if ((mtag = m_tag_find(*m0, PACKET_TAG_IPFW_DIVERT, NULL)) != NULL)
465 m_tag_delete(*m0, mtag);
984263bc
MD
466 args.rule = *rule; /* matching rule to restart */
467 args.next_hop = NULL; /* we do not support forward yet */
468 args.eh = &save_eh; /* MAC header for bridged/MAC packets */
469 i = ip_fw_chk_ptr(&args);
470 *m0 = args.m;
471 *rule = args.rule;
472
5fe66e68
JH
473 if ((i & IP_FW_PORT_DENY_FLAG) || *m0 == NULL) /* drop */
474 return FALSE;
984263bc 475
5fe66e68
JH
476 if (i == 0) /* a PASS rule. */
477 return TRUE;
984263bc
MD
478
479 if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG)) {
480 /*
481 * Pass the pkt to dummynet, which consumes it.
482 * If shared, make a copy and keep the original.
483 */
484 struct mbuf *m ;
485
486 if (shared) {
74f1caca 487 m = m_copypacket(*m0, MB_DONTWAIT);
984263bc 488 if (m == NULL)
5fe66e68 489 return FALSE;
984263bc 490 } else {
5fe66e68
JH
491 m = *m0 ; /* pass the original to dummynet */
492 *m0 = NULL ; /* and nothing back to the caller */
984263bc
MD
493 }
494 /*
495 * Prepend the header, optimize for the common case of
496 * eh pointing into the mbuf.
497 */
f23061d4 498 if ((void *)(eh + 1) == (void *)m->m_data) {
984263bc
MD
499 m->m_data -= ETHER_HDR_LEN ;
500 m->m_len += ETHER_HDR_LEN ;
501 m->m_pkthdr.len += ETHER_HDR_LEN ;
502 } else {
74f1caca 503 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
5fe66e68
JH
504 if (m == NULL)
505 return FALSE;
984263bc 506 bcopy(&save_eh, mtod(m, struct ether_header *),
5fe66e68 507 ETHER_HDR_LEN);
984263bc
MD
508 }
509 ip_dn_io_ptr(m, (i & 0xffff),
5fe66e68
JH
510 dst ? DN_TO_ETH_OUT: DN_TO_ETH_DEMUX, &args);
511 return FALSE;
984263bc
MD
512 }
513 /*
514 * XXX at some point add support for divert/forward actions.
515 * If none of the above matches, we have to drop the pkt.
516 */
5fe66e68 517 return FALSE;
984263bc
MD
518}
519
3013ac0e
JS
520/*
521 * XXX merge this function with ether_input.
522 */
523static void
524ether_input_internal(struct ifnet *ifp, struct mbuf *m)
525{
526 ether_input(ifp, NULL, m);
527}
528
984263bc
MD
529/*
530 * Process a received Ethernet packet. We have two different interfaces:
531 * one (conventional) assumes the packet in the mbuf, with the ethernet
532 * header provided separately in *eh. The second one (new) has everything
533 * in the mbuf, and we can tell it because eh == NULL.
534 * The caller MUST MAKE SURE that there are at least
535 * sizeof(struct ether_header) bytes in the first mbuf.
536 *
537 * This allows us to concentrate in one place a bunch of code which
538 * is replicated in all device drivers. Also, many functions called
539 * from ether_input() try to put the eh back into the mbuf, so we
540 * can later propagate the 'contiguous packet' interface to them,
541 * and handle the old interface just here.
542 *
543 * NOTA BENE: for many drivers "eh" is a pointer into the first mbuf or
544 * cluster, right before m_data. So be very careful when working on m,
545 * as you could destroy *eh !!
546 *
547 * First we perform any link layer operations, then continue
548 * to the upper layers with ether_demux().
549 */
550void
551ether_input(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
552{
78195a76
MD
553 ASSERT_SERIALIZED(ifp->if_serializer);
554
db37145f
SS
555 /* XXX old crufty stuff, needs to be removed */
556 if (eh != NULL) {
557 printf("ether_input got mbuf without embedded ethernet header");
558 m_free(m);
559 return;
984263bc
MD
560 }
561
db37145f
SS
562 if (m->m_len < sizeof(struct ether_header)) {
563 /* XXX error in the caller. */
564 m_freem(m);
565 return;
566 }
567 m->m_pkthdr.rcvif = ifp;
568 eh = mtod(m, struct ether_header *);
569
570 BPF_MTAP(ifp, m);
571
572 ifp->if_ibytes += m->m_pkthdr.len;
573
574 /*
575 * Tap the packet off here for a bridge. bridge_input()
576 * will return NULL if it has consumed the packet, otherwise
577 * it gets processed as normal. Note that bridge_input()
578 * will always return the original packet if we need to
579 * process it locally.
580 */
581 if (ifp->if_bridge) {
582 KASSERT(bridge_input_p != NULL,("ether_input: if_bridge not loaded!"));
583
584 if(m->m_flags & M_PROTO1) {
585 m->m_flags &= ~M_PROTO1;
586 } else {
587 /* clear M_PROMISC, in case the packets comes from a vlan */
588 /* m->m_flags &= ~M_PROMISC; */
589 lwkt_serialize_exit(ifp->if_serializer);
590 m = (*bridge_input_p)(ifp, m);
591 lwkt_serialize_enter(ifp->if_serializer);
592 if (m == NULL)
593 return;
594
595 /*
596 * Bridge has determined that the packet is for us.
597 * Update our interface pointer -- we may have had
598 * to "bridge" the packet locally.
599 */
600 ifp = m->m_pkthdr.rcvif;
601 }
602 }
984263bc 603
db37145f
SS
604 /* XXX old crufty stuff, needs to be removed */
605 m_adj(m, sizeof(struct ether_header));
606 /* XXX */
607 /* m->m_pkthdr.len = m->m_len; */
984263bc
MD
608
609 /* Handle ng_ether(4) processing, if any */
610 if (ng_ether_input_p != NULL) {
78195a76 611 lwkt_serialize_exit(ifp->if_serializer);
984263bc 612 (*ng_ether_input_p)(ifp, &m, eh);
78195a76 613 lwkt_serialize_enter(ifp->if_serializer);
984263bc
MD
614 if (m == NULL)
615 return;
616 }
617
984263bc
MD
618 /* Continue with upper layer processing */
619 ether_demux(ifp, eh, m);
620}
621
622/*
623 * Upper layer processing for a received Ethernet packet.
624 */
625void
f23061d4 626ether_demux(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
984263bc 627{
8bde602d 628 int isr;
984263bc 629 u_short ether_type;
5fe66e68
JH
630 struct ip_fw *rule = NULL;
631#ifdef NETATALK
82ed7fc2 632 struct llc *l;
984263bc 633#endif
984263bc
MD
634
635 /* Extract info from dummynet tag, ignore others */
5fe66e68 636 while (m->m_type == MT_TAG) {
984263bc
MD
637 if (m->m_flags == PACKET_TAG_DUMMYNET) {
638 rule = ((struct dn_pkt *)m)->rule;
639 ifp = m->m_next->m_pkthdr.rcvif;
5fe66e68 640 break;
984263bc 641 }
5fe66e68
JH
642 m = m->m_next;
643 }
a8d45119 644 if (rule) /* packet is passing the second time */
984263bc
MD
645 goto post_stats;
646
f23061d4
JH
647 /*
648 * Discard packet if upper layers shouldn't see it because
649 * it was unicast to a different Ethernet address. If the
650 * driver is working properly, then this situation can only
651 * happen when the interface is in promiscuous mode.
652 */
a8d45119 653 if (((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
f23061d4 654 (eh->ether_dhost[0] & 1) == 0 &&
1694af7d 655 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
984263bc
MD
656 m_freem(m);
657 return;
658 }
984263bc 659 /* Discard packet if interface is not up */
f23061d4 660 if (!(ifp->if_flags & IFF_UP)) {
984263bc
MD
661 m_freem(m);
662 return;
663 }
664 if (eh->ether_dhost[0] & 1) {
c401f0fd
JS
665 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
666 ifp->if_addrlen) == 0)
984263bc
MD
667 m->m_flags |= M_BCAST;
668 else
669 m->m_flags |= M_MCAST;
984263bc 670 ifp->if_imcasts++;
5fe66e68 671 }
984263bc
MD
672
673post_stats:
674 if (IPFW_LOADED && ether_ipfw != 0) {
5fe66e68 675 if (!ether_ipfw_chk(&m, NULL, &rule, eh, FALSE)) {
0c3c561c 676 m_freem(m);
984263bc
MD
677 return;
678 }
5fe66e68 679 eh = mtod(m, struct ether_header *);
984263bc
MD
680 }
681
682 ether_type = ntohs(eh->ether_type);
683
684 switch (ether_type) {
685#ifdef INET
686 case ETHERTYPE_IP:
78195a76 687 if (ipflow_fastforward(m, ifp->if_serializer))
984263bc 688 return;
8bde602d 689 isr = NETISR_IP;
984263bc
MD
690 break;
691
692 case ETHERTYPE_ARP:
693 if (ifp->if_flags & IFF_NOARP) {
694 /* Discard packet if ARP is disabled on interface */
695 m_freem(m);
696 return;
697 }
8bde602d 698 isr = NETISR_ARP;
984263bc
MD
699 break;
700#endif
5fe66e68
JH
701
702#ifdef INET6
703 case ETHERTYPE_IPV6:
704 isr = NETISR_IPV6;
705 break;
706#endif
707
984263bc
MD
708#ifdef IPX
709 case ETHERTYPE_IPX:
710 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
711 return;
8bde602d 712 isr = NETISR_IPX;
984263bc
MD
713 break;
714#endif
5fe66e68 715
984263bc
MD
716#ifdef NS
717 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
8bde602d 718 isr = NETISR_NS;
984263bc
MD
719 break;
720
5fe66e68
JH
721#endif
722
984263bc 723#ifdef NETATALK
f23061d4 724 case ETHERTYPE_AT:
8bde602d 725 isr = NETISR_ATALK1;
f23061d4
JH
726 break;
727 case ETHERTYPE_AARP:
8bde602d 728 isr = NETISR_AARP;
f23061d4 729 break;
5fe66e68
JH
730#endif
731
984263bc 732 case ETHERTYPE_VLAN:
984263bc
MD
733 if (vlan_input_p != NULL)
734 (*vlan_input_p)(eh, m);
735 else {
736 m->m_pkthdr.rcvif->if_noproto++;
737 m_freem(m);
738 }
984263bc 739 return;
5fe66e68 740
984263bc
MD
741 default:
742#ifdef IPX
743 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
744 return;
5fe66e68 745#endif
984263bc
MD
746#ifdef NS
747 checksum = mtod(m, ushort *);
748 /* Novell 802.3 */
749 if ((ether_type <= ETHERMTU) &&
8bde602d
JH
750 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
751 if (*checksum == 0xE0E0) {
984263bc
MD
752 m->m_pkthdr.len -= 3;
753 m->m_len -= 3;
754 m->m_data += 3;
755 }
8bde602d
JH
756 isr = NETISR_NS;
757 break;
984263bc 758 }
5fe66e68 759#endif
8bde602d 760#ifdef NETATALK
984263bc
MD
761 if (ether_type > ETHERMTU)
762 goto dropanyway;
763 l = mtod(m, struct llc *);
8bde602d
JH
764 if (l->llc_dsap == LLC_SNAP_LSAP &&
765 l->llc_ssap == LLC_SNAP_LSAP &&
766 l->llc_control == LLC_UI) {
f23061d4 767 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
0c3c561c 768 sizeof at_org_code) == 0 &&
8bde602d
JH
769 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
770 m_adj(m, sizeof(struct llc));
771 isr = NETISR_ATALK2;
772 break;
984263bc 773 }
2e9572df 774 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
0c3c561c 775 sizeof aarp_org_code) == 0 &&
8bde602d
JH
776 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
777 m_adj(m, sizeof(struct llc));
778 isr = NETISR_AARP;
779 break;
984263bc 780 }
984263bc 781 }
8bde602d 782dropanyway:
5fe66e68 783#endif
984263bc
MD
784 if (ng_ether_input_orphan_p != NULL)
785 (*ng_ether_input_orphan_p)(ifp, m, eh);
786 else
787 m_freem(m);
788 return;
984263bc 789 }
8bde602d 790 netisr_dispatch(isr, m);
984263bc
MD
791}
792
793/*
794 * Perform common duties while attaching to interface list
795 */
c0f6c904 796
984263bc 797void
78195a76 798ether_ifattach(struct ifnet *ifp, uint8_t *lla, lwkt_serialize_t serializer)
c0f6c904 799{
78195a76
MD
800 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header),
801 serializer);
c0f6c904
JS
802}
803
804void
78195a76
MD
805ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen,
806 lwkt_serialize_t serializer)
984263bc 807{
82ed7fc2 808 struct sockaddr_dl *sdl;
984263bc
MD
809
810 ifp->if_type = IFT_ETHER;
c401f0fd 811 ifp->if_addrlen = ETHER_ADDR_LEN;
5fe66e68 812 ifp->if_hdrlen = ETHER_HDR_LEN;
78195a76 813 if_attach(ifp, serializer);
984263bc 814 ifp->if_mtu = ETHERMTU;
984263bc 815 if (ifp->if_baudrate == 0)
f23061d4 816 ifp->if_baudrate = 10000000;
5fe66e68
JH
817 ifp->if_output = ether_output;
818 ifp->if_input = ether_input_internal;
819 ifp->if_resolvemulti = ether_resolvemulti;
820 ifp->if_broadcastaddr = etherbroadcastaddr;
f2682cb9 821 sdl = IF_LLSOCKADDR(ifp);
984263bc
MD
822 sdl->sdl_type = IFT_ETHER;
823 sdl->sdl_alen = ifp->if_addrlen;
0a8b5977 824 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
c568d5be
JS
825 /*
826 * XXX Keep the current drivers happy.
827 * XXX Remove once all drivers have been cleaned up
828 */
829 if (lla != IFP2AC(ifp)->ac_enaddr)
830 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
c0f6c904 831 bpfattach(ifp, dlt, hdrlen);
984263bc
MD
832 if (ng_ether_attach_p != NULL)
833 (*ng_ether_attach_p)(ifp);
267caeeb
JS
834
835 if_printf(ifp, "MAC address: %6D\n", lla, ":");
984263bc
MD
836}
837
838/*
839 * Perform common duties while detaching an Ethernet interface
840 */
841void
0a8b5977 842ether_ifdetach(struct ifnet *ifp)
984263bc 843{
45b8be9e 844 if_down(ifp);
45b8be9e 845
984263bc
MD
846 if (ng_ether_detach_p != NULL)
847 (*ng_ether_detach_p)(ifp);
0a8b5977 848 bpfdetach(ifp);
984263bc 849 if_detach(ifp);
984263bc
MD
850}
851
984263bc 852int
f23061d4 853ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
984263bc
MD
854{
855 struct ifaddr *ifa = (struct ifaddr *) data;
856 struct ifreq *ifr = (struct ifreq *) data;
857 int error = 0;
858
78195a76
MD
859 ASSERT_SERIALIZED(ifp->if_serializer);
860
984263bc
MD
861 switch (command) {
862 case SIOCSIFADDR:
863 ifp->if_flags |= IFF_UP;
864
865 switch (ifa->ifa_addr->sa_family) {
866#ifdef INET
867 case AF_INET:
868 ifp->if_init(ifp->if_softc); /* before arpwhohas */
869 arp_ifinit(ifp, ifa);
870 break;
871#endif
872#ifdef IPX
873 /*
874 * XXX - This code is probably wrong
875 */
876 case AF_IPX:
877 {
f23061d4 878 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
984263bc
MD
879 struct arpcom *ac = IFP2AC(ifp);
880
881 if (ipx_nullhost(*ina))
f23061d4
JH
882 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
883 else
884 bcopy(ina->x_host.c_host, ac->ac_enaddr,
885 sizeof ac->ac_enaddr);
984263bc 886
f23061d4 887 ifp->if_init(ifp->if_softc); /* Set new address. */
984263bc
MD
888 break;
889 }
890#endif
891#ifdef NS
892 /*
893 * XXX - This code is probably wrong
894 */
895 case AF_NS:
896 {
82ed7fc2 897 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
984263bc
MD
898 struct arpcom *ac = IFP2AC(ifp);
899
900 if (ns_nullhost(*ina))
f23061d4
JH
901 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
902 else
903 bcopy(ina->x_host.c_host, ac->ac_enaddr,
904 sizeof ac->ac_enaddr);
984263bc
MD
905
906 /*
907 * Set new address
908 */
909 ifp->if_init(ifp->if_softc);
910 break;
911 }
912#endif
913 default:
914 ifp->if_init(ifp->if_softc);
915 break;
916 }
917 break;
918
919 case SIOCGIFADDR:
f23061d4
JH
920 bcopy(IFP2AC(ifp)->ac_enaddr,
921 ((struct sockaddr *)ifr->ifr_data)->sa_data,
922 ETHER_ADDR_LEN);
984263bc
MD
923 break;
924
925 case SIOCSIFMTU:
926 /*
927 * Set the interface MTU.
928 */
929 if (ifr->ifr_mtu > ETHERMTU) {
930 error = EINVAL;
931 } else {
932 ifp->if_mtu = ifr->ifr_mtu;
933 }
934 break;
c2d9fd91
JS
935 default:
936 error = EINVAL;
937 break;
984263bc
MD
938 }
939 return (error);
940}
941
942int
f23061d4
JH
943ether_resolvemulti(
944 struct ifnet *ifp,
945 struct sockaddr **llsa,
946 struct sockaddr *sa)
984263bc
MD
947{
948 struct sockaddr_dl *sdl;
949 struct sockaddr_in *sin;
950#ifdef INET6
951 struct sockaddr_in6 *sin6;
952#endif
953 u_char *e_addr;
954
955 switch(sa->sa_family) {
956 case AF_LINK:
957 /*
958 * No mapping needed. Just check that it's a valid MC address.
959 */
960 sdl = (struct sockaddr_dl *)sa;
961 e_addr = LLADDR(sdl);
962 if ((e_addr[0] & 1) != 1)
963 return EADDRNOTAVAIL;
964 *llsa = 0;
965 return 0;
966
967#ifdef INET
968 case AF_INET:
969 sin = (struct sockaddr_in *)sa;
970 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
971 return EADDRNOTAVAIL;
972 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
5fe66e68 973 M_WAITOK | M_ZERO);
984263bc
MD
974 sdl->sdl_len = sizeof *sdl;
975 sdl->sdl_family = AF_LINK;
976 sdl->sdl_index = ifp->if_index;
977 sdl->sdl_type = IFT_ETHER;
978 sdl->sdl_alen = ETHER_ADDR_LEN;
979 e_addr = LLADDR(sdl);
980 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
981 *llsa = (struct sockaddr *)sdl;
982 return 0;
983#endif
984#ifdef INET6
985 case AF_INET6:
986 sin6 = (struct sockaddr_in6 *)sa;
987 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
988 /*
989 * An IP6 address of 0 means listen to all
990 * of the Ethernet multicast address used for IP6.
991 * (This is used for multicast routers.)
992 */
993 ifp->if_flags |= IFF_ALLMULTI;
994 *llsa = 0;
995 return 0;
996 }
997 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
998 return EADDRNOTAVAIL;
999 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
5fe66e68 1000 M_WAITOK | M_ZERO);
984263bc
MD
1001 sdl->sdl_len = sizeof *sdl;
1002 sdl->sdl_family = AF_LINK;
1003 sdl->sdl_index = ifp->if_index;
1004 sdl->sdl_type = IFT_ETHER;
1005 sdl->sdl_alen = ETHER_ADDR_LEN;
1006 e_addr = LLADDR(sdl);
1007 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1008 *llsa = (struct sockaddr *)sdl;
1009 return 0;
1010#endif
1011
1012 default:
1013 /*
1014 * Well, the text isn't quite right, but it's the name
1015 * that counts...
1016 */
1017 return EAFNOSUPPORT;
1018 }
1019}
d6018c31
JS
1020
1021#if 0
1022/*
1023 * This is for reference. We have a table-driven version
1024 * of the little-endian crc32 generator, which is faster
1025 * than the double-loop.
1026 */
1027uint32_t
1028ether_crc32_le(const uint8_t *buf, size_t len)
1029{
1030 uint32_t c, crc, carry;
1031 size_t i, j;
1032
1033 crc = 0xffffffffU; /* initial value */
1034
1035 for (i = 0; i < len; i++) {
1036 c = buf[i];
1037 for (j = 0; j < 8; j++) {
1038 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1039 crc >>= 1;
1040 c >>= 1;
1041 if (carry)
1042 crc = (crc ^ ETHER_CRC_POLY_LE);
1043 }
1044 }
1045
f23061d4 1046 return (crc);
d6018c31
JS
1047}
1048#else
1049uint32_t
1050ether_crc32_le(const uint8_t *buf, size_t len)
1051{
1052 static const uint32_t crctab[] = {
1053 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1054 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1055 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1056 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1057 };
1058 uint32_t crc;
1059 size_t i;
1060
1061 crc = 0xffffffffU; /* initial value */
1062
1063 for (i = 0; i < len; i++) {
1064 crc ^= buf[i];
1065 crc = (crc >> 4) ^ crctab[crc & 0xf];
1066 crc = (crc >> 4) ^ crctab[crc & 0xf];
1067 }
1068
f23061d4 1069 return (crc);
d6018c31
JS
1070}
1071#endif
1072
1073uint32_t
1074ether_crc32_be(const uint8_t *buf, size_t len)
1075{
1076 uint32_t c, crc, carry;
1077 size_t i, j;
1078
1079 crc = 0xffffffffU; /* initial value */
1080
1081 for (i = 0; i < len; i++) {
1082 c = buf[i];
1083 for (j = 0; j < 8; j++) {
1084 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1085 crc <<= 1;
1086 c >>= 1;
1087 if (carry)
1088 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1089 }
1090 }
1091
f23061d4 1092 return (crc);
d6018c31 1093}
4d723e5a 1094
4d723e5a
JS
1095/*
1096 * find the size of ethernet header, and call classifier
1097 */
1098void
1099altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
1100 struct altq_pktattr *pktattr)
1101{
1102 struct ether_header *eh;
1103 uint16_t ether_type;
1104 int hlen, af, hdrsize;
1105 caddr_t hdr;
1106
1107 hlen = sizeof(struct ether_header);
1108 eh = mtod(m, struct ether_header *);
1109
1110 ether_type = ntohs(eh->ether_type);
1111 if (ether_type < ETHERMTU) {
1112 /* ick! LLC/SNAP */
1113 struct llc *llc = (struct llc *)(eh + 1);
1114 hlen += 8;
1115
1116 if (m->m_len < hlen ||
1117 llc->llc_dsap != LLC_SNAP_LSAP ||
1118 llc->llc_ssap != LLC_SNAP_LSAP ||
1119 llc->llc_control != LLC_UI)
1120 goto bad; /* not snap! */
1121
1122 ether_type = ntohs(llc->llc_un.type_snap.ether_type);
1123 }
1124
1125 if (ether_type == ETHERTYPE_IP) {
1126 af = AF_INET;
1127 hdrsize = 20; /* sizeof(struct ip) */
1128#ifdef INET6
1129 } else if (ether_type == ETHERTYPE_IPV6) {
1130 af = AF_INET6;
1131 hdrsize = 40; /* sizeof(struct ip6_hdr) */
1132#endif
1133 } else
1134 goto bad;
1135
1136 while (m->m_len <= hlen) {
1137 hlen -= m->m_len;
1138 m = m->m_next;
1139 }
1140 hdr = m->m_data + hlen;
1141 if (m->m_len < hlen + hdrsize) {
1142 /*
1143 * ip header is not in a single mbuf. this should not
1144 * happen in the current code.
1145 * (todo: use m_pulldown in the future)
1146 */
1147 goto bad;
1148 }
1149 m->m_data += hlen;
1150 m->m_len -= hlen;
1151 ifq_classify(ifq, m, af, pktattr);
1152 m->m_data -= hlen;
1153 m->m_len += hlen;
1154
1155 return;
1156
1157bad:
1158 pktattr->pattr_class = NULL;
1159 pktattr->pattr_hdr = NULL;
1160 pktattr->pattr_af = AF_UNSPEC;
1161}