Remove 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 $
1f8e62c9 35 * $DragonFly: src/sys/net/if_ethersubr.c,v 1.26 2005/01/26 00:37:39 joerg 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"
42#include "opt_bdg.h"
43#include "opt_netgraph.h"
44
45#include <sys/param.h>
46#include <sys/systm.h>
47#include <sys/kernel.h>
48#include <sys/malloc.h>
49#include <sys/mbuf.h>
50#include <sys/socket.h>
51#include <sys/sockio.h>
52#include <sys/sysctl.h>
53
54#include <net/if.h>
55#include <net/netisr.h>
56#include <net/route.h>
57#include <net/if_llc.h>
58#include <net/if_dl.h>
59#include <net/if_types.h>
60#include <net/bpf.h>
61#include <net/ethernet.h>
1f2de5d4 62#include <net/bridge/bridge.h>
984263bc
MD
63
64#if defined(INET) || defined(INET6)
65#include <netinet/in.h>
66#include <netinet/in_var.h>
67#include <netinet/if_ether.h>
1f2de5d4
MD
68#include <net/ipfw/ip_fw.h>
69#include <net/dummynet/ip_dummynet.h>
984263bc
MD
70#endif
71#ifdef INET6
72#include <netinet6/nd6.h>
73#endif
74
75#ifdef IPX
d2438d69
MD
76#include <netproto/ipx/ipx.h>
77#include <netproto/ipx/ipx_if.h>
984263bc 78int (*ef_inputp)(struct ifnet*, struct ether_header *eh, struct mbuf *m);
f23061d4
JH
79int (*ef_outputp)(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
80 short *tp, int *hlen);
984263bc
MD
81#endif
82
83#ifdef NS
84#include <netns/ns.h>
85#include <netns/ns_if.h>
86ushort ns_nettype;
87int ether_outputdebug = 0;
88int ether_inputdebug = 0;
89#endif
90
91#ifdef NETATALK
d2438d69
MD
92#include <netproto/atalk/at.h>
93#include <netproto/atalk/at_var.h>
94#include <netproto/atalk/at_extern.h>
984263bc 95
f23061d4
JH
96#define llc_snap_org_code llc_un.type_snap.org_code
97#define llc_snap_ether_type llc_un.type_snap.ether_type
984263bc
MD
98
99extern u_char at_org_code[3];
100extern u_char aarp_org_code[3];
101#endif /* NETATALK */
102
103/* netgraph node hooks for ng_ether(4) */
104void (*ng_ether_input_p)(struct ifnet *ifp,
105 struct mbuf **mp, struct ether_header *eh);
106void (*ng_ether_input_orphan_p)(struct ifnet *ifp,
107 struct mbuf *m, struct ether_header *eh);
108int (*ng_ether_output_p)(struct ifnet *ifp, struct mbuf **mp);
109void (*ng_ether_attach_p)(struct ifnet *ifp);
110void (*ng_ether_detach_p)(struct ifnet *ifp);
111
112int (*vlan_input_p)(struct ether_header *eh, struct mbuf *m);
3013ac0e
JS
113int (*vlan_input_tag_p)(struct mbuf *m, uint16_t t);
114
5fe66e68
JH
115static int ether_output(struct ifnet *, struct mbuf *, struct sockaddr *,
116 struct rtentry *);
984263bc 117
f23061d4
JH
118/*
119 * bridge support
120 */
984263bc
MD
121int do_bridge;
122bridge_in_t *bridge_in_ptr;
123bdg_forward_t *bdg_forward_ptr;
124bdgtakeifaces_t *bdgtakeifaces_ptr;
125struct bdg_softc *ifp2sc;
126
5fe66e68
JH
127static int ether_resolvemulti(struct ifnet *, struct sockaddr **,
128 struct sockaddr *);
129
130const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
c401f0fd
JS
131 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
132};
133
5fe66e68 134#define gotoerr(e) do { error = (e); goto bad; } while (0)
f23061d4 135#define IFP2AC(ifp) ((struct arpcom *)(ifp))
984263bc 136
5fe66e68
JH
137static boolean_t ether_ipfw_chk(struct mbuf **m0, struct ifnet *dst,
138 struct ip_fw **rule, struct ether_header *eh,
139 boolean_t shared);
140
984263bc 141static int ether_ipfw;
5fe66e68
JH
142SYSCTL_DECL(_net_link);
143SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
144SYSCTL_INT(_net_link_ether, OID_AUTO, ipfw, CTLFLAG_RW,
145 &ether_ipfw, 0, "Pass ether pkts through firewall");
984263bc
MD
146
147/*
148 * Ethernet output routine.
149 * Encapsulate a packet of type family for the local net.
150 * Use trailer local net encapsulation if enough data in first
151 * packet leaves a multiple of 512 bytes of data in remainder.
152 * Assumes that ifp is actually pointer to arpcom structure.
153 */
3013ac0e
JS
154static int
155ether_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
f23061d4 156 struct rtentry *rt)
984263bc 157{
f23061d4
JH
158 struct ether_header *eh, *deh;
159 u_char *edst;
984263bc 160 int loop_copy = 0;
f23061d4 161 int hlen = ETHER_HDR_LEN; /* link layer header length */
984263bc 162 struct arpcom *ac = IFP2AC(ifp);
f23061d4 163 int error;
984263bc 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
MD
302
303 /*
304 * If a simplex interface, and the packet is being sent to our
305 * Ethernet address or a broadcast address, loopback a copy.
306 * XXX To make a simplex device behave exactly like a duplex
307 * device, we should copy in the case of sending to our own
308 * ethernet address (thus letting the original actually appear
309 * on the wire). However, we don't do that here for security
310 * reasons and compatibility with the original behavior.
311 */
312 if ((ifp->if_flags & IFF_SIMPLEX) && (loop_copy != -1)) {
313 int csum_flags = 0;
314
315 if (m->m_pkthdr.csum_flags & CSUM_IP)
f23061d4 316 csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
984263bc 317 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
f23061d4 318 csum_flags |= (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
984263bc
MD
319 if ((m->m_flags & M_BCAST) || (loop_copy > 0)) {
320 struct mbuf *n;
321
f23061d4 322 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) {
984263bc
MD
323 n->m_pkthdr.csum_flags |= csum_flags;
324 if (csum_flags & CSUM_DATA_VALID)
325 n->m_pkthdr.csum_data = 0xffff;
f23061d4 326 if_simloop(ifp, n, dst->sa_family, hlen);
984263bc
MD
327 } else
328 ifp->if_iqdrops++;
f23061d4
JH
329 } else if (bcmp(eh->ether_dhost, eh->ether_shost,
330 ETHER_ADDR_LEN) == 0) {
984263bc
MD
331 m->m_pkthdr.csum_flags |= csum_flags;
332 if (csum_flags & CSUM_DATA_VALID)
333 m->m_pkthdr.csum_data = 0xffff;
f23061d4 334 if_simloop(ifp, m, dst->sa_family, hlen);
984263bc
MD
335 return (0); /* XXX */
336 }
337 }
338
339 /* Handle ng_ether(4) processing, if any */
340 if (ng_ether_output_p != NULL) {
0c3c561c
JH
341 if ((error = (*ng_ether_output_p)(ifp, &m)) != 0)
342 goto bad;
984263bc
MD
343 if (m == NULL)
344 return (0);
345 }
346
347 /* Continue with link-layer output */
348 return ether_output_frame(ifp, m);
0c3c561c
JH
349
350bad:
351 m_freem(m);
352 return (error);
984263bc
MD
353}
354
355/*
356 * Ethernet link layer output routine to send a raw frame to the device.
357 *
358 * This assumes that the 14 byte Ethernet header is present and contiguous
359 * in the first mbuf (if BRIDGE'ing).
360 */
361int
f23061d4 362ether_output_frame(struct ifnet *ifp, struct mbuf *m)
984263bc 363{
f23061d4 364 struct ip_fw *rule = NULL;
984263bc
MD
365 int error = 0;
366 int s;
984263bc
MD
367
368 /* Extract info from dummynet tag, ignore others */
5fe66e68
JH
369 while (m->m_type == MT_TAG) {
370 if (m->m_flags == PACKET_TAG_DUMMYNET) {
984263bc 371 rule = ((struct dn_pkt *)m)->rule;
5fe66e68
JH
372 break;
373 }
374 m = m->m_next;
375 }
376 if (rule != NULL) /* packet was already bridged */
984263bc
MD
377 goto no_bridge;
378
5fe66e68
JH
379 if (BDG_ACTIVE(ifp)) {
380 struct ether_header *eh; /* a pointer suffices */
984263bc
MD
381
382 m->m_pkthdr.rcvif = NULL;
383 eh = mtod(m, struct ether_header *);
384 m_adj(m, ETHER_HDR_LEN);
385 m = bdg_forward_ptr(m, eh, ifp);
0c3c561c 386 m_freem(m);
984263bc
MD
387 return (0);
388 }
389
390no_bridge:
391 s = splimp();
392 if (IPFW_LOADED && ether_ipfw != 0) {
393 struct ether_header save_eh, *eh;
394
395 eh = mtod(m, struct ether_header *);
396 save_eh = *eh;
397 m_adj(m, ETHER_HDR_LEN);
5fe66e68
JH
398 if (!ether_ipfw_chk(&m, ifp, &rule, eh, FALSE)) {
399 if (m != NULL) {
984263bc 400 m_freem(m);
f23061d4 401 return ENOBUFS; /* pkt dropped */
984263bc
MD
402 } else
403 return 0; /* consumed e.g. in a pipe */
404 }
5fe66e68 405 eh = mtod(m, struct ether_header *);
984263bc 406 /* packet was ok, restore the ethernet header */
f23061d4 407 if ((void *)(eh + 1) == (void *)m->m_data) {
984263bc
MD
408 m->m_data -= ETHER_HDR_LEN ;
409 m->m_len += ETHER_HDR_LEN ;
410 m->m_pkthdr.len += ETHER_HDR_LEN ;
411 } else {
74f1caca 412 M_PREPEND(m, ETHER_HDR_LEN, MB_DONTWAIT);
984263bc
MD
413 if (m == NULL) /* nope... */
414 return ENOBUFS;
415 bcopy(&save_eh, mtod(m, struct ether_header *),
5fe66e68 416 ETHER_HDR_LEN);
984263bc
MD
417 }
418 }
419
420 /*
421 * Queue message on interface, update output statistics if
422 * successful, and start output if interface not yet active.
423 */
424 if (!IF_HANDOFF(&ifp->if_snd, m, ifp))
425 error = ENOBUFS;
426 splx(s);
427 return (error);
428}
429
430/*
431 * ipfw processing for ethernet packets (in and out).
5fe66e68
JH
432 * The second parameter is NULL from ether_demux(), and ifp from
433 * ether_output_frame(). This section of code could be used from
984263bc 434 * bridge.c as well as long as we use some extra info
5fe66e68 435 * to distinguish that case from ether_output_frame().
984263bc 436 */
5fe66e68 437static boolean_t
f23061d4
JH
438ether_ipfw_chk(
439 struct mbuf **m0,
440 struct ifnet *dst,
441 struct ip_fw **rule,
442 struct ether_header *eh,
5fe66e68 443 boolean_t shared)
984263bc
MD
444{
445 struct ether_header save_eh = *eh; /* might be a ptr in m */
984263bc 446 struct ip_fw_args args;
f23061d4 447 int i;
984263bc
MD
448
449 if (*rule != NULL && fw_one_pass)
5fe66e68 450 return TRUE; /* dummynet packet, already partially processed */
984263bc
MD
451
452 /*
5fe66e68
JH
453 * I need some amount of data to be contiguous, and in case others
454 * need the packet (shared==TRUE), it also better be in the first mbuf.
984263bc 455 */
f23061d4
JH
456 i = min((*m0)->m_pkthdr.len, max_protohdr);
457 if (shared || (*m0)->m_len < i) {
984263bc
MD
458 *m0 = m_pullup(*m0, i);
459 if (*m0 == NULL)
5fe66e68 460 return FALSE;
984263bc
MD
461 }
462
463 args.m = *m0; /* the packet we are looking at */
464 args.oif = dst; /* destination, if any */
465 args.divert_rule = 0; /* we do not support divert yet */
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{
553 struct ether_header save_eh;
554
555 if (eh == NULL) {
556 if (m->m_len < sizeof(struct ether_header)) {
557 /* XXX error in the caller. */
558 m_freem(m);
559 return;
560 }
561 m->m_pkthdr.rcvif = ifp;
562 eh = mtod(m, struct ether_header *);
3013ac0e
JS
563 m_adj(m, sizeof(struct ether_header));
564 /* XXX */
565 /* m->m_pkthdr.len = m->m_len; */
984263bc
MD
566 }
567
1f8e62c9
JS
568 if (ifp->if_bpf)
569 bpf_ptap(ifp->if_bpf, m, eh, ETHER_HDR_LEN);
984263bc 570
0c3c561c 571 ifp->if_ibytes += m->m_pkthdr.len + (sizeof *eh);
984263bc
MD
572
573 /* Handle ng_ether(4) processing, if any */
574 if (ng_ether_input_p != NULL) {
575 (*ng_ether_input_p)(ifp, &m, eh);
576 if (m == NULL)
577 return;
578 }
579
580 /* Check for bridging mode */
5fe66e68 581 if (BDG_ACTIVE(ifp)) {
984263bc
MD
582 struct ifnet *bif;
583
584 /* Check with bridging code */
585 if ((bif = bridge_in_ptr(ifp, eh)) == BDG_DROP) {
586 m_freem(m);
587 return;
588 }
589 if (bif != BDG_LOCAL) {
984263bc
MD
590 save_eh = *eh ; /* because it might change */
591 m = bdg_forward_ptr(m, eh, bif); /* needs forwarding */
592 /*
593 * Do not continue if bdg_forward_ptr() processed our
594 * packet (and cleared the mbuf pointer m) or if
595 * it dropped (m_free'd) the packet itself.
596 */
597 if (m == NULL) {
598 if (bif == BDG_BCAST || bif == BDG_MCAST)
599 printf("bdg_forward drop MULTICAST PKT\n");
600 return;
601 }
602 eh = &save_eh ;
603 }
f23061d4
JH
604 if (bif == BDG_LOCAL || bif == BDG_BCAST || bif == BDG_MCAST)
605 goto recvLocal; /* receive locally */
984263bc
MD
606
607 /* If not local and not multicast, just drop it */
0c3c561c 608 m_freem(m);
984263bc 609 return;
f23061d4 610 }
984263bc
MD
611
612recvLocal:
613 /* Continue with upper layer processing */
614 ether_demux(ifp, eh, m);
615}
616
617/*
618 * Upper layer processing for a received Ethernet packet.
619 */
620void
f23061d4 621ether_demux(struct ifnet *ifp, struct ether_header *eh, struct mbuf *m)
984263bc 622{
8bde602d 623 int isr;
984263bc 624 u_short ether_type;
5fe66e68
JH
625 struct ip_fw *rule = NULL;
626#ifdef NETATALK
82ed7fc2 627 struct llc *l;
984263bc 628#endif
984263bc
MD
629
630 /* Extract info from dummynet tag, ignore others */
5fe66e68 631 while (m->m_type == MT_TAG) {
984263bc
MD
632 if (m->m_flags == PACKET_TAG_DUMMYNET) {
633 rule = ((struct dn_pkt *)m)->rule;
634 ifp = m->m_next->m_pkthdr.rcvif;
5fe66e68 635 break;
984263bc 636 }
5fe66e68
JH
637 m = m->m_next;
638 }
984263bc
MD
639 if (rule) /* packet was already bridged */
640 goto post_stats;
641
f23061d4
JH
642 /*
643 * Discard packet if upper layers shouldn't see it because
644 * it was unicast to a different Ethernet address. If the
645 * driver is working properly, then this situation can only
646 * happen when the interface is in promiscuous mode.
647 */
648 if (!BDG_ACTIVE(ifp) &&
5380e0e9 649 ((ifp->if_flags & (IFF_PROMISC | IFF_PPROMISC)) == IFF_PROMISC) &&
f23061d4 650 (eh->ether_dhost[0] & 1) == 0 &&
1694af7d 651 bcmp(eh->ether_dhost, IFP2AC(ifp)->ac_enaddr, ETHER_ADDR_LEN)) {
984263bc
MD
652 m_freem(m);
653 return;
654 }
984263bc 655 /* Discard packet if interface is not up */
f23061d4 656 if (!(ifp->if_flags & IFF_UP)) {
984263bc
MD
657 m_freem(m);
658 return;
659 }
660 if (eh->ether_dhost[0] & 1) {
c401f0fd
JS
661 if (bcmp(ifp->if_broadcastaddr, eh->ether_dhost,
662 ifp->if_addrlen) == 0)
984263bc
MD
663 m->m_flags |= M_BCAST;
664 else
665 m->m_flags |= M_MCAST;
984263bc 666 ifp->if_imcasts++;
5fe66e68 667 }
984263bc
MD
668
669post_stats:
670 if (IPFW_LOADED && ether_ipfw != 0) {
5fe66e68 671 if (!ether_ipfw_chk(&m, NULL, &rule, eh, FALSE)) {
0c3c561c 672 m_freem(m);
984263bc
MD
673 return;
674 }
5fe66e68 675 eh = mtod(m, struct ether_header *);
984263bc
MD
676 }
677
678 ether_type = ntohs(eh->ether_type);
679
680 switch (ether_type) {
681#ifdef INET
682 case ETHERTYPE_IP:
683 if (ipflow_fastforward(m))
684 return;
8bde602d 685 isr = NETISR_IP;
984263bc
MD
686 break;
687
688 case ETHERTYPE_ARP:
689 if (ifp->if_flags & IFF_NOARP) {
690 /* Discard packet if ARP is disabled on interface */
691 m_freem(m);
692 return;
693 }
8bde602d 694 isr = NETISR_ARP;
984263bc
MD
695 break;
696#endif
5fe66e68
JH
697
698#ifdef INET6
699 case ETHERTYPE_IPV6:
700 isr = NETISR_IPV6;
701 break;
702#endif
703
984263bc
MD
704#ifdef IPX
705 case ETHERTYPE_IPX:
706 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
707 return;
8bde602d 708 isr = NETISR_IPX;
984263bc
MD
709 break;
710#endif
5fe66e68 711
984263bc
MD
712#ifdef NS
713 case 0x8137: /* Novell Ethernet_II Ethernet TYPE II */
8bde602d 714 isr = NETISR_NS;
984263bc
MD
715 break;
716
5fe66e68
JH
717#endif
718
984263bc 719#ifdef NETATALK
f23061d4 720 case ETHERTYPE_AT:
8bde602d 721 isr = NETISR_ATALK1;
f23061d4
JH
722 break;
723 case ETHERTYPE_AARP:
8bde602d 724 isr = NETISR_AARP;
f23061d4 725 break;
5fe66e68
JH
726#endif
727
984263bc 728 case ETHERTYPE_VLAN:
984263bc
MD
729 if (vlan_input_p != NULL)
730 (*vlan_input_p)(eh, m);
731 else {
732 m->m_pkthdr.rcvif->if_noproto++;
733 m_freem(m);
734 }
984263bc 735 return;
5fe66e68 736
984263bc
MD
737 default:
738#ifdef IPX
739 if (ef_inputp && ef_inputp(ifp, eh, m) == 0)
740 return;
5fe66e68 741#endif
984263bc
MD
742#ifdef NS
743 checksum = mtod(m, ushort *);
744 /* Novell 802.3 */
745 if ((ether_type <= ETHERMTU) &&
8bde602d
JH
746 ((*checksum == 0xffff) || (*checksum == 0xE0E0))) {
747 if (*checksum == 0xE0E0) {
984263bc
MD
748 m->m_pkthdr.len -= 3;
749 m->m_len -= 3;
750 m->m_data += 3;
751 }
8bde602d
JH
752 isr = NETISR_NS;
753 break;
984263bc 754 }
5fe66e68 755#endif
8bde602d 756#ifdef NETATALK
984263bc
MD
757 if (ether_type > ETHERMTU)
758 goto dropanyway;
759 l = mtod(m, struct llc *);
8bde602d
JH
760 if (l->llc_dsap == LLC_SNAP_LSAP &&
761 l->llc_ssap == LLC_SNAP_LSAP &&
762 l->llc_control == LLC_UI) {
f23061d4 763 if (bcmp(&(l->llc_snap_org_code)[0], at_org_code,
0c3c561c 764 sizeof at_org_code) == 0 &&
8bde602d
JH
765 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AT) {
766 m_adj(m, sizeof(struct llc));
767 isr = NETISR_ATALK2;
768 break;
984263bc 769 }
2e9572df 770 if (bcmp(&(l->llc_snap_org_code)[0], aarp_org_code,
0c3c561c 771 sizeof aarp_org_code) == 0 &&
8bde602d
JH
772 ntohs(l->llc_snap_ether_type) == ETHERTYPE_AARP) {
773 m_adj(m, sizeof(struct llc));
774 isr = NETISR_AARP;
775 break;
984263bc 776 }
984263bc 777 }
8bde602d 778dropanyway:
5fe66e68 779#endif
984263bc
MD
780 if (ng_ether_input_orphan_p != NULL)
781 (*ng_ether_input_orphan_p)(ifp, m, eh);
782 else
783 m_freem(m);
784 return;
984263bc 785 }
8bde602d 786 netisr_dispatch(isr, m);
984263bc
MD
787}
788
789/*
790 * Perform common duties while attaching to interface list
791 */
c0f6c904 792
984263bc 793void
0a8b5977 794ether_ifattach(struct ifnet *ifp, uint8_t *lla)
c0f6c904
JS
795{
796 ether_ifattach_bpf(ifp, lla, DLT_EN10MB, sizeof(struct ether_header));
797}
798
799void
800ether_ifattach_bpf(struct ifnet *ifp, uint8_t *lla, u_int dlt, u_int hdrlen)
984263bc 801{
82ed7fc2
RG
802 struct ifaddr *ifa;
803 struct sockaddr_dl *sdl;
984263bc
MD
804
805 ifp->if_type = IFT_ETHER;
c401f0fd 806 ifp->if_addrlen = ETHER_ADDR_LEN;
5fe66e68 807 ifp->if_hdrlen = ETHER_HDR_LEN;
984263bc
MD
808 if_attach(ifp);
809 ifp->if_mtu = ETHERMTU;
984263bc 810 if (ifp->if_baudrate == 0)
f23061d4 811 ifp->if_baudrate = 10000000;
5fe66e68
JH
812 ifp->if_output = ether_output;
813 ifp->if_input = ether_input_internal;
814 ifp->if_resolvemulti = ether_resolvemulti;
815 ifp->if_broadcastaddr = etherbroadcastaddr;
984263bc
MD
816 ifa = ifnet_addrs[ifp->if_index - 1];
817 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __FUNCTION__));
818 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
819 sdl->sdl_type = IFT_ETHER;
820 sdl->sdl_alen = ifp->if_addrlen;
0a8b5977 821 bcopy(lla, LLADDR(sdl), ifp->if_addrlen);
c568d5be
JS
822 /*
823 * XXX Keep the current drivers happy.
824 * XXX Remove once all drivers have been cleaned up
825 */
826 if (lla != IFP2AC(ifp)->ac_enaddr)
827 bcopy(lla, IFP2AC(ifp)->ac_enaddr, ifp->if_addrlen);
c0f6c904 828 bpfattach(ifp, dlt, hdrlen);
984263bc
MD
829 if (ng_ether_attach_p != NULL)
830 (*ng_ether_attach_p)(ifp);
831 if (BDG_LOADED)
832 bdgtakeifaces_ptr();
267caeeb
JS
833
834 if_printf(ifp, "MAC address: %6D\n", lla, ":");
984263bc
MD
835}
836
837/*
838 * Perform common duties while detaching an Ethernet interface
839 */
840void
0a8b5977 841ether_ifdetach(struct ifnet *ifp)
984263bc 842{
45b8be9e
JS
843 int s;
844
845 s = splnet();
846 if_down(ifp);
847 splx(s);
848
984263bc
MD
849 if (ng_ether_detach_p != NULL)
850 (*ng_ether_detach_p)(ifp);
0a8b5977 851 bpfdetach(ifp);
984263bc
MD
852 if_detach(ifp);
853 if (BDG_LOADED)
854 bdgtakeifaces_ptr();
855}
856
984263bc 857int
f23061d4 858ether_ioctl(struct ifnet *ifp, int command, caddr_t data)
984263bc
MD
859{
860 struct ifaddr *ifa = (struct ifaddr *) data;
861 struct ifreq *ifr = (struct ifreq *) data;
862 int error = 0;
863
864 switch (command) {
865 case SIOCSIFADDR:
866 ifp->if_flags |= IFF_UP;
867
868 switch (ifa->ifa_addr->sa_family) {
869#ifdef INET
870 case AF_INET:
871 ifp->if_init(ifp->if_softc); /* before arpwhohas */
872 arp_ifinit(ifp, ifa);
873 break;
874#endif
875#ifdef IPX
876 /*
877 * XXX - This code is probably wrong
878 */
879 case AF_IPX:
880 {
f23061d4 881 struct ipx_addr *ina = &IA_SIPX(ifa)->sipx_addr;
984263bc
MD
882 struct arpcom *ac = IFP2AC(ifp);
883
884 if (ipx_nullhost(*ina))
f23061d4
JH
885 ina->x_host = *(union ipx_host *) ac->ac_enaddr;
886 else
887 bcopy(ina->x_host.c_host, ac->ac_enaddr,
888 sizeof ac->ac_enaddr);
984263bc 889
f23061d4 890 ifp->if_init(ifp->if_softc); /* Set new address. */
984263bc
MD
891 break;
892 }
893#endif
894#ifdef NS
895 /*
896 * XXX - This code is probably wrong
897 */
898 case AF_NS:
899 {
82ed7fc2 900 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
984263bc
MD
901 struct arpcom *ac = IFP2AC(ifp);
902
903 if (ns_nullhost(*ina))
f23061d4
JH
904 ina->x_host = *(union ns_host *)(ac->ac_enaddr);
905 else
906 bcopy(ina->x_host.c_host, ac->ac_enaddr,
907 sizeof ac->ac_enaddr);
984263bc
MD
908
909 /*
910 * Set new address
911 */
912 ifp->if_init(ifp->if_softc);
913 break;
914 }
915#endif
916 default:
917 ifp->if_init(ifp->if_softc);
918 break;
919 }
920 break;
921
922 case SIOCGIFADDR:
f23061d4
JH
923 bcopy(IFP2AC(ifp)->ac_enaddr,
924 ((struct sockaddr *)ifr->ifr_data)->sa_data,
925 ETHER_ADDR_LEN);
984263bc
MD
926 break;
927
928 case SIOCSIFMTU:
929 /*
930 * Set the interface MTU.
931 */
932 if (ifr->ifr_mtu > ETHERMTU) {
933 error = EINVAL;
934 } else {
935 ifp->if_mtu = ifr->ifr_mtu;
936 }
937 break;
c2d9fd91
JS
938 default:
939 error = EINVAL;
940 break;
984263bc
MD
941 }
942 return (error);
943}
944
945int
f23061d4
JH
946ether_resolvemulti(
947 struct ifnet *ifp,
948 struct sockaddr **llsa,
949 struct sockaddr *sa)
984263bc
MD
950{
951 struct sockaddr_dl *sdl;
952 struct sockaddr_in *sin;
953#ifdef INET6
954 struct sockaddr_in6 *sin6;
955#endif
956 u_char *e_addr;
957
958 switch(sa->sa_family) {
959 case AF_LINK:
960 /*
961 * No mapping needed. Just check that it's a valid MC address.
962 */
963 sdl = (struct sockaddr_dl *)sa;
964 e_addr = LLADDR(sdl);
965 if ((e_addr[0] & 1) != 1)
966 return EADDRNOTAVAIL;
967 *llsa = 0;
968 return 0;
969
970#ifdef INET
971 case AF_INET:
972 sin = (struct sockaddr_in *)sa;
973 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
974 return EADDRNOTAVAIL;
975 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
5fe66e68 976 M_WAITOK | M_ZERO);
984263bc
MD
977 sdl->sdl_len = sizeof *sdl;
978 sdl->sdl_family = AF_LINK;
979 sdl->sdl_index = ifp->if_index;
980 sdl->sdl_type = IFT_ETHER;
981 sdl->sdl_alen = ETHER_ADDR_LEN;
982 e_addr = LLADDR(sdl);
983 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
984 *llsa = (struct sockaddr *)sdl;
985 return 0;
986#endif
987#ifdef INET6
988 case AF_INET6:
989 sin6 = (struct sockaddr_in6 *)sa;
990 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
991 /*
992 * An IP6 address of 0 means listen to all
993 * of the Ethernet multicast address used for IP6.
994 * (This is used for multicast routers.)
995 */
996 ifp->if_flags |= IFF_ALLMULTI;
997 *llsa = 0;
998 return 0;
999 }
1000 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1001 return EADDRNOTAVAIL;
1002 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
5fe66e68 1003 M_WAITOK | M_ZERO);
984263bc
MD
1004 sdl->sdl_len = sizeof *sdl;
1005 sdl->sdl_family = AF_LINK;
1006 sdl->sdl_index = ifp->if_index;
1007 sdl->sdl_type = IFT_ETHER;
1008 sdl->sdl_alen = ETHER_ADDR_LEN;
1009 e_addr = LLADDR(sdl);
1010 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
1011 *llsa = (struct sockaddr *)sdl;
1012 return 0;
1013#endif
1014
1015 default:
1016 /*
1017 * Well, the text isn't quite right, but it's the name
1018 * that counts...
1019 */
1020 return EAFNOSUPPORT;
1021 }
1022}
d6018c31
JS
1023
1024#if 0
1025/*
1026 * This is for reference. We have a table-driven version
1027 * of the little-endian crc32 generator, which is faster
1028 * than the double-loop.
1029 */
1030uint32_t
1031ether_crc32_le(const uint8_t *buf, size_t len)
1032{
1033 uint32_t c, crc, carry;
1034 size_t i, j;
1035
1036 crc = 0xffffffffU; /* initial value */
1037
1038 for (i = 0; i < len; i++) {
1039 c = buf[i];
1040 for (j = 0; j < 8; j++) {
1041 carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1042 crc >>= 1;
1043 c >>= 1;
1044 if (carry)
1045 crc = (crc ^ ETHER_CRC_POLY_LE);
1046 }
1047 }
1048
f23061d4 1049 return (crc);
d6018c31
JS
1050}
1051#else
1052uint32_t
1053ether_crc32_le(const uint8_t *buf, size_t len)
1054{
1055 static const uint32_t crctab[] = {
1056 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1057 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1058 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1059 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1060 };
1061 uint32_t crc;
1062 size_t i;
1063
1064 crc = 0xffffffffU; /* initial value */
1065
1066 for (i = 0; i < len; i++) {
1067 crc ^= buf[i];
1068 crc = (crc >> 4) ^ crctab[crc & 0xf];
1069 crc = (crc >> 4) ^ crctab[crc & 0xf];
1070 }
1071
f23061d4 1072 return (crc);
d6018c31
JS
1073}
1074#endif
1075
1076uint32_t
1077ether_crc32_be(const uint8_t *buf, size_t len)
1078{
1079 uint32_t c, crc, carry;
1080 size_t i, j;
1081
1082 crc = 0xffffffffU; /* initial value */
1083
1084 for (i = 0; i < len; i++) {
1085 c = buf[i];
1086 for (j = 0; j < 8; j++) {
1087 carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1088 crc <<= 1;
1089 c >>= 1;
1090 if (carry)
1091 crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1092 }
1093 }
1094
f23061d4 1095 return (crc);
d6018c31 1096}