Annotation of src/sys/net/if_ethersubr.c, Revision 1.205.2.1
1.205.2.1! skrll 1: /* $NetBSD: if_ethersubr.c,v 1.206 2015/04/03 07:55:18 ozaki-r Exp $ */
1.44 itojun 2:
3: /*
4: * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5: * All rights reserved.
1.120 perry 6: *
1.44 itojun 7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. Neither the name of the project nor the names of its contributors
16: * may be used to endorse or promote products derived from this software
17: * without specific prior written permission.
1.120 perry 18: *
1.44 itojun 19: * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: */
1.9 cgd 31:
1.1 cgd 32: /*
1.8 mycroft 33: * Copyright (c) 1982, 1989, 1993
34: * The Regents of the University of California. All rights reserved.
1.1 cgd 35: *
36: * Redistribution and use in source and binary forms, with or without
37: * modification, are permitted provided that the following conditions
38: * are met:
39: * 1. Redistributions of source code must retain the above copyright
40: * notice, this list of conditions and the following disclaimer.
41: * 2. Redistributions in binary form must reproduce the above copyright
42: * notice, this list of conditions and the following disclaimer in the
43: * documentation and/or other materials provided with the distribution.
1.113 agc 44: * 3. Neither the name of the University nor the names of its contributors
1.1 cgd 45: * may be used to endorse or promote products derived from this software
46: * without specific prior written permission.
47: *
48: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58: * SUCH DAMAGE.
59: *
1.27 fvdl 60: * @(#)if_ethersubr.c 8.2 (Berkeley) 4/4/96
1.1 cgd 61: */
1.90 lukem 62:
63: #include <sys/cdefs.h>
1.205.2.1! skrll 64: __KERNEL_RCSID(0, "$NetBSD: if_ethersubr.c,v 1.206 2015/04/03 07:55:18 ozaki-r Exp $");
1.1 cgd 65:
1.33 jonathan 66: #include "opt_inet.h"
67: #include "opt_atalk.h"
1.112 martin 68: #include "opt_ipx.h"
69: #include "opt_mbuftrace.h"
1.182 kefren 70: #include "opt_mpls.h"
1.30 matt 71: #include "opt_gateway.h"
1.135 pavel 72: #include "opt_pppoe.h"
1.205.2.1! skrll 73: #include "opt_net_mpsafe.h"
1.59 thorpej 74: #include "vlan.h"
1.81 martin 75: #include "pppoe.h"
1.78 thorpej 76: #include "bridge.h"
1.93 martin 77: #include "arp.h"
1.121 yamt 78: #include "agr.h"
1.30 matt 79:
1.4 mycroft 80: #include <sys/param.h>
81: #include <sys/systm.h>
1.200 joerg 82: #include <sys/sysctl.h>
1.4 mycroft 83: #include <sys/kernel.h>
1.81 martin 84: #include <sys/callout.h>
1.4 mycroft 85: #include <sys/malloc.h>
86: #include <sys/mbuf.h>
87: #include <sys/protosw.h>
88: #include <sys/socket.h>
89: #include <sys/ioctl.h>
90: #include <sys/errno.h>
91: #include <sys/syslog.h>
1.132 elad 92: #include <sys/kauth.h>
1.156 ad 93: #include <sys/cpu.h>
94: #include <sys/intr.h>
1.158 dyoung 95: #include <sys/device.h>
1.204 tls 96: #include <sys/rnd.h>
1.8 mycroft 97:
1.4 mycroft 98: #include <net/if.h>
99: #include <net/netisr.h>
100: #include <net/route.h>
101: #include <net/if_llc.h>
102: #include <net/if_dl.h>
1.8 mycroft 103: #include <net/if_types.h>
1.93 martin 104:
1.158 dyoung 105: #include <net/if_media.h>
106: #include <dev/mii/mii.h>
107: #include <dev/mii/miivar.h>
108:
1.93 martin 109: #if NARP == 0
110: /*
1.102 jmmv 111: * XXX there should really be a way to issue this warning from within config(8)
1.93 martin 112: */
1.111 martin 113: #error You have included NETATALK or a pseudo-device in your configuration that depends on the presence of ethernet interfaces, but have no such interfaces configured. Check if you really need pseudo-device bridge, pppoe, vlan or options NETATALK.
1.93 martin 114: #endif
1.1 cgd 115:
1.69 thorpej 116: #include <net/bpf.h>
117:
1.22 is 118: #include <net/if_ether.h>
1.59 thorpej 119: #include <net/if_vlanvar.h>
1.22 is 120:
1.81 martin 121: #if NPPPOE > 0
122: #include <net/if_pppoe.h>
123: #endif
124:
1.121 yamt 125: #if NAGR > 0
126: #include <net/agr/ieee8023_slowprotocols.h> /* XXX */
127: #include <net/agr/ieee8023ad.h>
128: #include <net/agr/if_agrvar.h>
129: #endif
130:
1.78 thorpej 131: #if NBRIDGE > 0
132: #include <net/if_bridgevar.h>
133: #endif
134:
1.15 phil 135: #include <netinet/in.h>
1.1 cgd 136: #ifdef INET
1.4 mycroft 137: #include <netinet/in_var.h>
1.1 cgd 138: #endif
1.22 is 139: #include <netinet/if_inarp.h>
1.1 cgd 140:
1.44 itojun 141: #ifdef INET6
142: #ifndef INET
143: #include <netinet/in.h>
144: #endif
145: #include <netinet6/in6_var.h>
146: #include <netinet6/nd6.h>
147: #endif
148:
1.1 cgd 149:
1.133 liamjfoy 150: #include "carp.h"
151: #if NCARP > 0
152: #include <netinet/ip_carp.h>
153: #endif
154:
1.32 christos 155: #ifdef IPX
156: #include <netipx/ipx.h>
157: #include <netipx/ipx_if.h>
158: #endif
159:
1.23 christos 160: #ifdef NETATALK
161: #include <netatalk/at.h>
162: #include <netatalk/at_var.h>
163: #include <netatalk/at_extern.h>
164:
165: #define llc_snap_org_code llc_un.type_snap.org_code
166: #define llc_snap_ether_type llc_un.type_snap.ether_type
167:
168: extern u_char at_org_code[3];
169: extern u_char aarp_org_code[3];
170: #endif /* NETATALK */
171:
1.182 kefren 172: #ifdef MPLS
173: #include <netmpls/mpls.h>
174: #include <netmpls/mpls_var.h>
175: #endif
176:
1.123 matt 177: static struct timeval bigpktppslim_last;
178: static int bigpktppslim = 2; /* XXX */
179: static int bigpktpps_count;
1.203 ozaki-r 180: static kmutex_t bigpktpps_lock __cacheline_aligned;
1.123 matt 181:
182:
1.118 yamt 183: const uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] =
184: { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1.121 yamt 185: const uint8_t ethermulticastaddr_slowprotocols[ETHER_ADDR_LEN] =
186: { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x02 };
1.8 mycroft 187: #define senderr(e) { error = (e); goto bad;}
1.1 cgd 188:
1.128 thorpej 189: static int ether_output(struct ifnet *, struct mbuf *,
1.145 dyoung 190: const struct sockaddr *, struct rtentry *);
1.42 thorpej 191:
1.1 cgd 192: /*
193: * Ethernet output routine.
194: * Encapsulate a packet of type family for the local net.
1.22 is 195: * Assumes that ifp is actually pointer to ethercom structure.
1.1 cgd 196: */
1.42 thorpej 197: static int
1.178 dyoung 198: ether_output(struct ifnet * const ifp0, struct mbuf * const m0,
199: const struct sockaddr * const dst,
1.58 matt 200: struct rtentry *rt0)
1.1 cgd 201: {
1.162 matt 202: uint16_t etype = 0;
1.122 christos 203: int error = 0, hdrcmplt = 0;
1.145 dyoung 204: uint8_t esrc[6], edst[6];
1.29 mrg 205: struct mbuf *m = m0;
206: struct rtentry *rt;
1.151 dyoung 207: struct mbuf *mcopy = NULL;
1.29 mrg 208: struct ether_header *eh;
1.133 liamjfoy 209: struct ifnet *ifp = ifp0;
1.72 thorpej 210: ALTQ_DECL(struct altq_pktattr pktattr;)
1.24 christos 211: #ifdef INET
1.22 is 212: struct arphdr *ah;
1.24 christos 213: #endif /* INET */
1.23 christos 214: #ifdef NETATALK
215: struct at_ifaddr *aa;
216: #endif /* NETATALK */
1.1 cgd 217:
1.205.2.1! skrll 218: #ifndef NET_MPSAFE
1.197 bouyer 219: KASSERT(KERNEL_LOCKED_P());
1.205.2.1! skrll 220: #endif
1.197 bouyer 221:
1.104 matt 222: #ifdef MBUFTRACE
1.116 jonathan 223: m_claimm(m, ifp->if_mowner);
1.104 matt 224: #endif
1.133 liamjfoy 225:
226: #if NCARP > 0
227: if (ifp->if_type == IFT_CARP) {
228: struct ifaddr *ifa;
229:
230: /* loop back if this is going to the carp interface */
231: if (dst != NULL && ifp0->if_link_state == LINK_STATE_UP &&
232: (ifa = ifa_ifwithaddr(dst)) != NULL &&
233: ifa->ifa_ifp == ifp0)
1.145 dyoung 234: return looutput(ifp0, m, dst, rt0);
1.133 liamjfoy 235:
236: ifp = ifp->if_carpdev;
237: /* ac = (struct arpcom *)ifp; */
238:
239: if ((ifp0->if_flags & (IFF_UP|IFF_RUNNING)) !=
240: (IFF_UP|IFF_RUNNING))
241: senderr(ENETDOWN);
242: }
243: #endif /* NCARP > 0 */
244:
1.8 mycroft 245: if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
246: senderr(ENETDOWN);
1.18 christos 247: if ((rt = rt0) != NULL) {
1.8 mycroft 248: if ((rt->rt_flags & RTF_UP) == 0) {
1.27 fvdl 249: if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) {
1.8 mycroft 250: rt->rt_refcnt--;
1.27 fvdl 251: if (rt->rt_ifp != ifp)
252: return (*rt->rt_ifp->if_output)
253: (ifp, m0, dst, rt);
1.120 perry 254: } else
1.8 mycroft 255: senderr(EHOSTUNREACH);
256: }
1.27 fvdl 257: if ((rt->rt_flags & RTF_GATEWAY) && dst->sa_family != AF_NS) {
1.166 dyoung 258: if (rt->rt_gwroute == NULL)
1.8 mycroft 259: goto lookup;
260: if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
261: rtfree(rt); rt = rt0;
262: lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
1.166 dyoung 263: if ((rt = rt->rt_gwroute) == NULL)
1.8 mycroft 264: senderr(EHOSTUNREACH);
1.27 fvdl 265: /* the "G" test below also prevents rt == rt0 */
266: if ((rt->rt_flags & RTF_GATEWAY) ||
267: (rt->rt_ifp != ifp)) {
268: rt->rt_refcnt--;
1.166 dyoung 269: rt0->rt_gwroute = NULL;
1.27 fvdl 270: senderr(EHOSTUNREACH);
271: }
1.8 mycroft 272: }
273: }
274: if (rt->rt_flags & RTF_REJECT)
275: if (rt->rt_rmx.rmx_expire == 0 ||
1.134 kardel 276: (u_long) time_second < rt->rt_rmx.rmx_expire)
1.8 mycroft 277: senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
1.1 cgd 278: }
1.72 thorpej 279:
1.1 cgd 280: switch (dst->sa_family) {
281:
282: #ifdef INET
283: case AF_INET:
1.22 is 284: if (m->m_flags & M_BCAST)
1.174 isaki 285: (void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
1.145 dyoung 286: else if (m->m_flags & M_MCAST)
287: ETHER_MAP_IP_MULTICAST(&satocsin(dst)->sin_addr, edst);
288: else if (!arpresolve(ifp, rt, m, dst, edst))
1.1 cgd 289: return (0); /* if not yet resolved */
1.3 hpeyerl 290: /* If broadcasting on a simplex interface, loopback a copy */
291: if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
1.1 cgd 292: mcopy = m_copy(m, 0, (int)M_COPYALL);
1.17 mycroft 293: etype = htons(ETHERTYPE_IP);
1.8 mycroft 294: break;
1.22 is 295:
296: case AF_ARP:
297: ah = mtod(m, struct arphdr *);
298: if (m->m_flags & M_BCAST)
1.174 isaki 299: (void)memcpy(edst, etherbroadcastaddr, sizeof(edst));
1.131 mrg 300: else {
1.147 christos 301: void *tha = ar_tha(ah);
1.131 mrg 302:
1.173 christos 303: if (tha == NULL) {
304: /* fake with ARPHDR_IEEE1394 */
305: return 0;
306: }
1.146 dyoung 307: memcpy(edst, tha, sizeof(edst));
1.131 mrg 308: }
1.120 perry 309:
1.22 is 310: ah->ar_hrd = htons(ARPHRD_ETHER);
311:
1.107 itojun 312: switch (ntohs(ah->ar_op)) {
1.22 is 313: case ARPOP_REVREQUEST:
314: case ARPOP_REVREPLY:
315: etype = htons(ETHERTYPE_REVARP);
316: break;
317:
318: case ARPOP_REQUEST:
319: case ARPOP_REPLY:
320: default:
321: etype = htons(ETHERTYPE_ARP);
322: }
323:
324: break;
1.1 cgd 325: #endif
1.44 itojun 326: #ifdef INET6
327: case AF_INET6:
1.166 dyoung 328: if (!nd6_storelladdr(ifp, rt, m, dst, edst, sizeof(edst))){
1.67 itojun 329: /* something bad happened */
1.99 itojun 330: return (0);
1.51 itojun 331: }
1.44 itojun 332: etype = htons(ETHERTYPE_IPV6);
333: break;
334: #endif
1.23 christos 335: #ifdef NETATALK
336: case AF_APPLETALK:
1.145 dyoung 337: if (!aarpresolve(ifp, m, (const struct sockaddr_at *)dst, edst)) {
1.23 christos 338: #ifdef NETATALKDEBUG
339: printf("aarpresolv failed\n");
340: #endif /* NETATALKDEBUG */
341: return (0);
342: }
343: /*
344: * ifaddr is the first thing in at_ifaddr
345: */
346: aa = (struct at_ifaddr *) at_ifawithnet(
1.145 dyoung 347: (const struct sockaddr_at *)dst, ifp);
1.23 christos 348: if (aa == NULL)
349: goto bad;
1.120 perry 350:
1.23 christos 351: /*
352: * In the phase 2 case, we need to prepend an mbuf for the
353: * llc header. Since we must preserve the value of m,
354: * which is passed to us by value, we m_copy() the first
355: * mbuf, and use it for our llc header.
356: */
357: if (aa->aa_flags & AFA_PHASE2) {
358: struct llc llc;
359:
1.43 bouyer 360: M_PREPEND(m, sizeof(struct llc), M_DONTWAIT);
1.23 christos 361: llc.llc_dsap = llc.llc_ssap = LLC_SNAP_LSAP;
362: llc.llc_control = LLC_UI;
1.146 dyoung 363: memcpy(llc.llc_snap_org_code, at_org_code,
1.23 christos 364: sizeof(llc.llc_snap_org_code));
1.38 kim 365: llc.llc_snap_ether_type = htons(ETHERTYPE_ATALK);
1.147 christos 366: memcpy(mtod(m, void *), &llc, sizeof(struct llc));
1.23 christos 367: } else {
1.38 kim 368: etype = htons(ETHERTYPE_ATALK);
1.23 christos 369: }
370: break;
371: #endif /* NETATALK */
1.32 christos 372: #ifdef IPX
373: case AF_IPX:
1.39 christos 374: etype = htons(ETHERTYPE_IPX);
1.146 dyoung 375: memcpy(edst,
376: &(((const struct sockaddr_ipx *)dst)->sipx_addr.x_host),
377: sizeof(edst));
1.32 christos 378: /* If broadcasting on a simplex interface, loopback a copy */
379: if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
380: mcopy = m_copy(m, 0, (int)M_COPYALL);
381: break;
382: #endif
1.31 thorpej 383: case pseudo_AF_HDRCMPLT:
384: hdrcmplt = 1;
1.146 dyoung 385: memcpy(esrc,
386: ((const struct ether_header *)dst->sa_data)->ether_shost,
387: sizeof(esrc));
1.31 thorpej 388: /* FALLTHROUGH */
389:
1.1 cgd 390: case AF_UNSPEC:
1.146 dyoung 391: memcpy(edst,
392: ((const struct ether_header *)dst->sa_data)->ether_dhost,
393: sizeof(edst));
1.8 mycroft 394: /* AF_UNSPEC doesn't swap the byte order of the ether_type. */
1.145 dyoung 395: etype = ((const struct ether_header *)dst->sa_data)->ether_type;
1.8 mycroft 396: break;
1.1 cgd 397:
398: default:
1.21 christos 399: printf("%s: can't handle af%d\n", ifp->if_xname,
1.1 cgd 400: dst->sa_family);
1.8 mycroft 401: senderr(EAFNOSUPPORT);
1.1 cgd 402: }
403:
1.182 kefren 404: #ifdef MPLS
1.184 dyoung 405: if (rt0 != NULL && rt_gettag(rt0) != NULL &&
1.188 kefren 406: rt_gettag(rt0)->sa_family == AF_MPLS &&
407: (m->m_flags & (M_MCAST | M_BCAST)) == 0) {
1.184 dyoung 408: union mpls_shim msh;
409: msh.s_addr = MPLS_GETSADDR(rt0);
410: if (msh.shim.label != MPLS_LABEL_IMPLNULL)
411: etype = htons(ETHERTYPE_MPLS);
412: }
1.182 kefren 413: #endif
414:
1.1 cgd 415: if (mcopy)
1.145 dyoung 416: (void)looutput(ifp, mcopy, dst, rt);
1.16 mycroft 417:
1.50 matt 418: /* If no ether type is set, this must be a 802.2 formatted packet.
419: */
420: if (etype == 0)
421: etype = htons(m->m_pkthdr.len);
1.1 cgd 422: /*
423: * Add local net header. If no space in first mbuf,
424: * allocate another.
425: */
426: M_PREPEND(m, sizeof (struct ether_header), M_DONTWAIT);
1.8 mycroft 427: if (m == 0)
428: senderr(ENOBUFS);
1.1 cgd 429: eh = mtod(m, struct ether_header *);
1.96 thorpej 430: /* Note: etype is already in network byte order. */
1.143 cbiere 431: (void)memcpy(&eh->ether_type, &etype, sizeof(eh->ether_type));
1.146 dyoung 432: memcpy(eh->ether_dhost, edst, sizeof(edst));
1.31 thorpej 433: if (hdrcmplt)
1.146 dyoung 434: memcpy(eh->ether_shost, esrc, sizeof(eh->ether_shost));
1.31 thorpej 435: else
1.153 dyoung 436: memcpy(eh->ether_shost, CLLADDR(ifp->if_sadl),
1.31 thorpej 437: sizeof(eh->ether_shost));
1.77 thorpej 438:
1.133 liamjfoy 439: #if NCARP > 0
440: if (ifp0 != ifp && ifp0->if_type == IFT_CARP) {
1.153 dyoung 441: memcpy(eh->ether_shost, CLLADDR(ifp0->if_sadl),
1.133 liamjfoy 442: sizeof(eh->ether_shost));
443: }
444: #endif /* NCARP > 0 */
445:
1.195 rmind 446: if ((error = pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
1.77 thorpej 447: return (error);
448: if (m == NULL)
449: return (0);
450:
1.78 thorpej 451: #if NBRIDGE > 0
452: /*
453: * Bridges require special output handling.
454: */
455: if (ifp->if_bridge)
456: return (bridge_output(ifp, m, NULL, NULL));
457: #endif
458:
1.133 liamjfoy 459: #if NCARP > 0
460: if (ifp != ifp0)
461: ifp0->if_obytes += m->m_pkthdr.len + ETHER_HDR_LEN;
462: #endif /* NCARP > 0 */
463:
1.77 thorpej 464: #ifdef ALTQ
465: /*
466: * If ALTQ is enabled on the parent interface, do
467: * classification; the queueing discipline might not
468: * require classification, but might require the
469: * address family/header pointer in the pktattr.
470: */
471: if (ALTQ_IS_ENABLED(&ifp->if_snd))
472: altq_etherclassify(&ifp->if_snd, m, &pktattr);
473: #endif
1.122 christos 474: return ifq_enqueue(ifp, m ALTQ_COMMA ALTQ_DECL(&pktattr));
1.1 cgd 475:
476: bad:
477: if (m)
478: m_freem(m);
479: return (error);
480: }
1.76 thorpej 481:
482: #ifdef ALTQ
483: /*
484: * This routine is a slight hack to allow a packet to be classified
485: * if the Ethernet headers are present. It will go away when ALTQ's
486: * classification engine understands link headers.
487: */
488: void
489: altq_etherclassify(struct ifaltq *ifq, struct mbuf *m,
490: struct altq_pktattr *pktattr)
491: {
492: struct ether_header *eh;
1.162 matt 493: uint16_t ether_type;
1.76 thorpej 494: int hlen, af, hdrsize;
1.147 christos 495: void *hdr;
1.76 thorpej 496:
497: hlen = ETHER_HDR_LEN;
498: eh = mtod(m, struct ether_header *);
499:
500: ether_type = htons(eh->ether_type);
501:
502: if (ether_type < ETHERMTU) {
503: /* LLC/SNAP */
504: struct llc *llc = (struct llc *)(eh + 1);
505: hlen += 8;
506:
507: if (m->m_len < hlen ||
508: llc->llc_dsap != LLC_SNAP_LSAP ||
509: llc->llc_ssap != LLC_SNAP_LSAP ||
510: llc->llc_control != LLC_UI) {
511: /* Not SNAP. */
512: goto bad;
513: }
514:
515: ether_type = htons(llc->llc_un.type_snap.ether_type);
516: }
517:
518: switch (ether_type) {
519: case ETHERTYPE_IP:
520: af = AF_INET;
521: hdrsize = 20; /* sizeof(struct ip) */
522: break;
523:
524: case ETHERTYPE_IPV6:
525: af = AF_INET6;
526: hdrsize = 40; /* sizeof(struct ip6_hdr) */
527: break;
528:
529: default:
530: af = AF_UNSPEC;
531: hdrsize = 0;
532: break;
533: }
534:
1.95 itojun 535: while (m->m_len <= hlen) {
536: hlen -= m->m_len;
537: m = m->m_next;
538: }
1.76 thorpej 539: if (m->m_len < (hlen + hdrsize)) {
540: /*
1.95 itojun 541: * protocol header not in a single mbuf.
542: * We can't cope with this situation right
1.76 thorpej 543: * now (but it shouldn't ever happen, really, anyhow).
544: */
1.92 itojun 545: #ifdef DEBUG
1.78 thorpej 546: printf("altq_etherclassify: headers span multiple mbufs: "
547: "%d < %d\n", m->m_len, (hlen + hdrsize));
1.92 itojun 548: #endif
1.76 thorpej 549: goto bad;
550: }
551:
552: m->m_data += hlen;
553: m->m_len -= hlen;
554:
1.147 christos 555: hdr = mtod(m, void *);
1.76 thorpej 556:
557: if (ALTQ_NEEDS_CLASSIFY(ifq))
558: pktattr->pattr_class =
559: (*ifq->altq_classify)(ifq->altq_clfier, m, af);
560: pktattr->pattr_af = af;
561: pktattr->pattr_hdr = hdr;
562:
563: m->m_data -= hlen;
564: m->m_len += hlen;
565:
566: return;
567:
568: bad:
569: pktattr->pattr_class = NULL;
570: pktattr->pattr_hdr = NULL;
571: pktattr->pattr_af = AF_UNSPEC;
572: }
573: #endif /* ALTQ */
1.1 cgd 574:
575: /*
576: * Process a received Ethernet packet;
1.42 thorpej 577: * the packet is in the mbuf chain m with
578: * the ether header.
1.1 cgd 579: */
1.133 liamjfoy 580: void
1.58 matt 581: ether_input(struct ifnet *ifp, struct mbuf *m)
1.1 cgd 582: {
1.91 thorpej 583: struct ethercom *ec = (struct ethercom *) ifp;
1.199 rmind 584: pktqueue_t *pktq = NULL;
585: struct ifqueue *inq = NULL;
1.162 matt 586: uint16_t etype;
1.42 thorpej 587: struct ether_header *eh;
1.187 matt 588: size_t ehlen;
1.204 tls 589: static int earlypkts;
1.198 msaitoh 590: int isr = 0;
1.194 joerg 591: #if defined (LLC) || defined(NETATALK)
1.29 mrg 592: struct llc *l;
1.18 christos 593: #endif
1.1 cgd 594:
1.8 mycroft 595: if ((ifp->if_flags & IFF_UP) == 0) {
596: m_freem(m);
597: return;
598: }
1.42 thorpej 599:
1.104 matt 600: #ifdef MBUFTRACE
1.116 jonathan 601: m_claimm(m, &ec->ec_rx_mowner);
1.104 matt 602: #endif
1.42 thorpej 603: eh = mtod(m, struct ether_header *);
1.63 thorpej 604: etype = ntohs(eh->ether_type);
1.187 matt 605: ehlen = sizeof(*eh);
1.63 thorpej 606:
1.204 tls 607: if(__predict_false(earlypkts < 100 || !rnd_initial_entropy)) {
608: rnd_add_data(NULL, eh, ehlen, 0);
609: earlypkts++;
610: }
611:
1.63 thorpej 612: /*
613: * Determine if the packet is within its size limits.
614: */
1.182 kefren 615: if (etype != ETHERTYPE_MPLS && m->m_pkthdr.len >
1.83 thorpej 616: ETHER_MAX_FRAME(ifp, etype, m->m_flags & M_HASFCS)) {
1.203 ozaki-r 617: mutex_enter(&bigpktpps_lock);
1.123 matt 618: if (ppsratecheck(&bigpktppslim_last, &bigpktpps_count,
619: bigpktppslim)) {
620: printf("%s: discarding oversize frame (len=%d)\n",
621: ifp->if_xname, m->m_pkthdr.len);
622: }
1.203 ozaki-r 623: mutex_exit(&bigpktpps_lock);
1.63 thorpej 624: m_freem(m);
625: return;
626: }
1.77 thorpej 627:
1.84 thorpej 628: if (ETHER_IS_MULTICAST(eh->ether_dhost)) {
629: /*
630: * If this is not a simplex interface, drop the packet
631: * if it came from us.
632: */
633: if ((ifp->if_flags & IFF_SIMPLEX) == 0 &&
1.153 dyoung 634: memcmp(CLLADDR(ifp->if_sadl), eh->ether_shost,
1.84 thorpej 635: ETHER_ADDR_LEN) == 0) {
636: m_freem(m);
637: return;
638: }
639:
640: if (memcmp(etherbroadcastaddr,
641: eh->ether_dhost, ETHER_ADDR_LEN) == 0)
642: m->m_flags |= M_BCAST;
643: else
644: m->m_flags |= M_MCAST;
645: ifp->if_imcasts++;
646: }
647:
1.79 thorpej 648: /* If the CRC is still on the packet, trim it off. */
649: if (m->m_flags & M_HASFCS) {
650: m_adj(m, -ETHER_CRC_LEN);
651: m->m_flags &= ~M_HASFCS;
652: }
653:
1.42 thorpej 654: ifp->if_ibytes += m->m_pkthdr.len;
1.78 thorpej 655:
1.201 ozaki-r 656: #if NCARP > 0
657: if (__predict_false(ifp->if_carp && ifp->if_type != IFT_CARP)) {
658: /*
659: * clear M_PROMISC, in case the packets comes from a
660: * vlan
661: */
1.137 rpaulo 662: m->m_flags &= ~M_PROMISC;
1.201 ozaki-r 663: if (carp_input(m, (uint8_t *)&eh->ether_shost,
664: (uint8_t *)&eh->ether_dhost, eh->ether_type) == 0)
1.137 rpaulo 665: return;
1.201 ozaki-r 666: }
1.133 liamjfoy 667: #endif /* NCARP > 0 */
1.201 ozaki-r 668: if ((m->m_flags & (M_BCAST|M_MCAST|M_PROMISC)) == 0 &&
669: (ifp->if_flags & IFF_PROMISC) != 0 &&
670: memcmp(CLLADDR(ifp->if_sadl), eh->ether_dhost,
671: ETHER_ADDR_LEN) != 0) {
672: m->m_flags |= M_PROMISC;
1.70 bouyer 673: }
1.78 thorpej 674:
1.106 bouyer 675: if ((m->m_flags & M_PROMISC) == 0) {
1.195 rmind 676: if (pfil_run_hooks(ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
1.106 bouyer 677: return;
678: if (m == NULL)
679: return;
1.78 thorpej 680:
1.106 bouyer 681: eh = mtod(m, struct ether_header *);
682: etype = ntohs(eh->ether_type);
1.187 matt 683: ehlen = sizeof(*eh);
1.106 bouyer 684: }
1.70 bouyer 685:
1.172 darran 686: #if NAGR > 0
687: if (ifp->if_agrprivate &&
688: __predict_true(etype != ETHERTYPE_SLOWPROTOCOLS)) {
689: m->m_flags &= ~M_PROMISC;
690: agr_input(ifp, m);
691: return;
692: }
693: #endif /* NAGR > 0 */
694:
1.91 thorpej 695: /*
696: * If VLANs are configured on the interface, check to
697: * see if the device performed the decapsulation and
698: * provided us with the tag.
699: */
1.114 simonb 700: if (ec->ec_nvlans && m_tag_find(m, PACKET_TAG_VLAN, NULL) != NULL) {
1.70 bouyer 701: #if NVLAN > 0
702: /*
703: * vlan_input() will either recursively call ether_input()
704: * or drop the packet.
705: */
1.91 thorpej 706: vlan_input(ifp, m);
707: #else
708: m_freem(m);
1.70 bouyer 709: #endif
1.61 thorpej 710: return;
711: }
1.1 cgd 712:
1.59 thorpej 713: /*
714: * Handle protocols that expect to have the Ethernet header
715: * (and possibly FCS) intact.
716: */
717: switch (etype) {
1.187 matt 718: case ETHERTYPE_VLAN: {
719: struct ether_vlan_header *evl = (void *)eh;
720: /*
721: * If there is a tag of 0, then the VLAN header was probably
722: * just being used to store the priority. Extract the ether
723: * type, and if IP or IPV6, let them deal with it.
724: */
725: if (m->m_len <= sizeof(*evl)
726: && EVL_VLANOFTAG(evl->evl_tag) == 0) {
727: etype = ntohs(evl->evl_proto);
728: ehlen = sizeof(*evl);
729: if ((m->m_flags & M_PROMISC) == 0
730: && (etype == ETHERTYPE_IP
731: || etype == ETHERTYPE_IPV6))
732: break;
733: }
1.59 thorpej 734: #if NVLAN > 0
735: /*
736: * vlan_input() will either recursively call ether_input()
737: * or drop the packet.
738: */
1.63 thorpej 739: if (((struct ethercom *)ifp)->ec_nvlans != 0)
740: vlan_input(ifp, m);
1.65 enami 741: else
1.187 matt 742: #endif /* NVLAN > 0 */
1.65 enami 743: m_freem(m);
1.59 thorpej 744: return;
1.187 matt 745: }
1.81 martin 746: #if NPPPOE > 0
747: case ETHERTYPE_PPPOEDISC:
748: case ETHERTYPE_PPPOE:
1.106 bouyer 749: if (m->m_flags & M_PROMISC) {
750: m_freem(m);
751: return;
752: }
1.105 aymeric 753: #ifndef PPPOE_SERVER
754: if (m->m_flags & (M_MCAST | M_BCAST)) {
755: m_freem(m);
756: return;
757: }
758: #endif
759:
1.120 perry 760: if (etype == ETHERTYPE_PPPOEDISC)
1.81 martin 761: inq = &ppoediscinq;
762: else
763: inq = &ppoeinq;
764: if (IF_QFULL(inq)) {
765: IF_DROP(inq);
766: m_freem(m);
1.202 ozaki-r 767: } else {
1.81 martin 768: IF_ENQUEUE(inq, m);
1.202 ozaki-r 769: softint_schedule(pppoe_softintr);
770: }
1.81 martin 771: return;
772: #endif /* NPPPOE > 0 */
1.121 yamt 773: case ETHERTYPE_SLOWPROTOCOLS: {
774: uint8_t subtype;
775:
776: #if defined(DIAGNOSTIC)
777: if (m->m_pkthdr.len < sizeof(*eh) + sizeof(subtype)) {
778: panic("ether_input: too short slow protocol packet");
779: }
780: #endif
781: m_copydata(m, sizeof(*eh), sizeof(subtype), &subtype);
782: switch (subtype) {
783: #if NAGR > 0
784: case SLOWPROTOCOLS_SUBTYPE_LACP:
785: if (ifp->if_agrprivate) {
786: ieee8023ad_lacp_input(ifp, m);
787: return;
788: }
789: break;
790:
791: case SLOWPROTOCOLS_SUBTYPE_MARKER:
792: if (ifp->if_agrprivate) {
793: ieee8023ad_marker_input(ifp, m);
794: return;
795: }
796: break;
797: #endif /* NAGR > 0 */
798: default:
799: if (subtype == 0 || subtype > 10) {
800: /* illegal value */
801: m_freem(m);
802: return;
803: }
804: /* unknown subtype */
805: break;
806: }
807: /* FALLTHROUGH */
808: }
1.59 thorpej 809: default:
1.106 bouyer 810: if (m->m_flags & M_PROMISC) {
811: m_freem(m);
812: return;
813: }
1.59 thorpej 814: }
1.42 thorpej 815:
1.45 thorpej 816: /* If the CRC is still on the packet, trim it off. */
1.79 thorpej 817: if (m->m_flags & M_HASFCS) {
1.45 thorpej 818: m_adj(m, -ETHER_CRC_LEN);
1.79 thorpej 819: m->m_flags &= ~M_HASFCS;
820: }
1.42 thorpej 821:
1.139 is 822: if (etype > ETHERMTU + sizeof (struct ether_header)) {
823: /* Strip off the Ethernet header. */
1.187 matt 824: m_adj(m, ehlen);
1.139 is 825:
826: switch (etype) {
1.1 cgd 827: #ifdef INET
1.139 is 828: case ETHERTYPE_IP:
1.30 matt 829: #ifdef GATEWAY
1.139 is 830: if (ipflow_fastforward(m))
831: return;
1.30 matt 832: #endif
1.199 rmind 833: pktq = ip_pktq;
1.139 is 834: break;
1.1 cgd 835:
1.139 is 836: case ETHERTYPE_ARP:
1.198 msaitoh 837: isr = NETISR_ARP;
1.139 is 838: inq = &arpintrq;
839: break;
1.7 glass 840:
1.139 is 841: case ETHERTYPE_REVARP:
842: revarpinput(m); /* XXX queue? */
843: return;
1.1 cgd 844: #endif
1.44 itojun 845: #ifdef INET6
1.139 is 846: case ETHERTYPE_IPV6:
1.196 pooka 847: if (__predict_false(!in6_present)) {
848: m_freem(m);
849: return;
850: }
1.148 liamjfoy 851: #ifdef GATEWAY
1.192 christos 852: if (ip6flow_fastforward(&m))
1.148 liamjfoy 853: return;
854: #endif
1.199 rmind 855: pktq = ip6_pktq;
1.139 is 856: break;
1.44 itojun 857: #endif
1.32 christos 858: #ifdef IPX
1.139 is 859: case ETHERTYPE_IPX:
1.198 msaitoh 860: isr = NETISR_IPX;
1.139 is 861: inq = &ipxintrq;
862: break;
1.1 cgd 863: #endif
1.23 christos 864: #ifdef NETATALK
1.174 isaki 865: case ETHERTYPE_ATALK:
1.198 msaitoh 866: isr = NETISR_ATALK;
1.174 isaki 867: inq = &atintrq1;
868: break;
869: case ETHERTYPE_AARP:
1.139 is 870: /* probably this should be done with a NETISR as well */
1.175 mbalmer 871: aarpinput(ifp, m); /* XXX */
1.174 isaki 872: return;
1.23 christos 873: #endif /* NETATALK */
1.182 kefren 874: #ifdef MPLS
875: case ETHERTYPE_MPLS:
1.198 msaitoh 876: isr = NETISR_MPLS;
1.182 kefren 877: inq = &mplsintrq;
878: break;
879: #endif
1.139 is 880: default:
881: m_freem(m);
882: return;
883: }
884: } else {
1.194 joerg 885: #if defined (LLC) || defined (NETATALK)
1.139 is 886: l = (struct llc *)(eh+1);
1.8 mycroft 887: switch (l->llc_dsap) {
1.23 christos 888: #ifdef NETATALK
889: case LLC_SNAP_LSAP:
890: switch (l->llc_control) {
891: case LLC_UI:
892: if (l->llc_ssap != LLC_SNAP_LSAP) {
893: goto dropanyway;
894: }
1.120 perry 895:
1.166 dyoung 896: if (memcmp(&(l->llc_snap_org_code)[0],
1.23 christos 897: at_org_code, sizeof(at_org_code)) == 0 &&
898: ntohs(l->llc_snap_ether_type) ==
1.38 kim 899: ETHERTYPE_ATALK) {
1.23 christos 900: inq = &atintrq2;
1.139 is 901: m_adj(m, sizeof(struct ether_header)
902: + sizeof(struct llc));
1.198 msaitoh 903: isr = NETISR_ATALK;
1.23 christos 904: break;
905: }
906:
1.166 dyoung 907: if (memcmp(&(l->llc_snap_org_code)[0],
1.23 christos 908: aarp_org_code,
909: sizeof(aarp_org_code)) == 0 &&
910: ntohs(l->llc_snap_ether_type) ==
911: ETHERTYPE_AARP) {
1.139 is 912: m_adj( m, sizeof(struct ether_header)
913: + sizeof(struct llc));
1.23 christos 914: aarpinput(ifp, m); /* XXX */
915: return;
916: }
1.120 perry 917:
1.23 christos 918: default:
919: goto dropanyway;
920: }
921: break;
1.1 cgd 922: dropanyway:
1.142 bouyer 923: #endif
1.1 cgd 924: default:
1.8 mycroft 925: m_freem(m);
926: return;
927: }
1.139 is 928: #else /* ISO || LLC || NETATALK*/
929: m_freem(m);
930: return;
1.23 christos 931: #endif /* ISO || LLC || NETATALK*/
1.1 cgd 932: }
933:
1.199 rmind 934: if (__predict_true(pktq)) {
935: const uint32_t h = pktq_rps_hash(m);
936: if (__predict_false(!pktq_enqueue(pktq, m, h))) {
937: m_freem(m);
938: }
939: return;
940: }
941:
942: if (__predict_false(!inq)) {
943: /* Should not happen. */
944: m_freem(m);
945: return;
946: }
1.1 cgd 947: if (IF_QFULL(inq)) {
948: IF_DROP(inq);
949: m_freem(m);
1.198 msaitoh 950: } else {
1.1 cgd 951: IF_ENQUEUE(inq, m);
1.198 msaitoh 952: schednetisr(isr);
953: }
1.1 cgd 954: }
955:
956: /*
957: * Convert Ethernet address to printable (loggable) representation.
958: */
959: char *
1.58 matt 960: ether_sprintf(const u_char *ap)
1.1 cgd 961: {
1.129 christos 962: static char etherbuf[3 * ETHER_ADDR_LEN];
963: return ether_snprintf(etherbuf, sizeof(etherbuf), ap);
964: }
965:
966: char *
967: ether_snprintf(char *buf, size_t len, const u_char *ap)
968: {
969: char *cp = buf;
970: size_t i;
1.1 cgd 971:
1.129 christos 972: for (i = 0; i < len / 3; i++) {
1.124 christos 973: *cp++ = hexdigits[*ap >> 4];
974: *cp++ = hexdigits[*ap++ & 0xf];
1.1 cgd 975: *cp++ = ':';
976: }
1.129 christos 977: *--cp = '\0';
978: return buf;
1.1 cgd 979: }
1.8 mycroft 980:
981: /*
982: * Perform common duties while attaching to interface list
983: */
984: void
1.162 matt 985: ether_ifattach(struct ifnet *ifp, const uint8_t *lla)
1.8 mycroft 986: {
1.104 matt 987: struct ethercom *ec = (struct ethercom *)ifp;
1.8 mycroft 988:
989: ifp->if_type = IFT_ETHER;
1.94 enami 990: ifp->if_hdrlen = ETHER_HDR_LEN;
1.73 thorpej 991: ifp->if_dlt = DLT_EN10MB;
1.8 mycroft 992: ifp->if_mtu = ETHERMTU;
1.12 mycroft 993: ifp->if_output = ether_output;
1.42 thorpej 994: ifp->if_input = ether_input;
1.54 thorpej 995: if (ifp->if_baudrate == 0)
996: ifp->if_baudrate = IF_Mbps(10); /* just a default */
1.75 thorpej 997:
1.170 dyoung 998: if_set_sadl(ifp, lla, ETHER_ADDR_LEN, !ETHER_IS_LOCAL(lla));
1.75 thorpej 999:
1.104 matt 1000: LIST_INIT(&ec->ec_multiaddrs);
1.26 is 1001: ifp->if_broadcastaddr = etherbroadcastaddr;
1.177 joerg 1002: bpf_attach(ifp, DLT_EN10MB, sizeof(struct ether_header));
1.104 matt 1003: #ifdef MBUFTRACE
1.108 itojun 1004: strlcpy(ec->ec_tx_mowner.mo_name, ifp->if_xname,
1005: sizeof(ec->ec_tx_mowner.mo_name));
1006: strlcpy(ec->ec_tx_mowner.mo_descr, "tx",
1007: sizeof(ec->ec_tx_mowner.mo_descr));
1008: strlcpy(ec->ec_rx_mowner.mo_name, ifp->if_xname,
1009: sizeof(ec->ec_rx_mowner.mo_name));
1010: strlcpy(ec->ec_rx_mowner.mo_descr, "rx",
1011: sizeof(ec->ec_rx_mowner.mo_descr));
1.104 matt 1012: MOWNER_ATTACH(&ec->ec_tx_mowner);
1013: MOWNER_ATTACH(&ec->ec_rx_mowner);
1014: ifp->if_mowner = &ec->ec_tx_mowner;
1015: #endif
1.52 thorpej 1016: }
1017:
1018: void
1.58 matt 1019: ether_ifdetach(struct ifnet *ifp)
1.52 thorpej 1020: {
1.63 thorpej 1021: struct ethercom *ec = (void *) ifp;
1022: struct ether_multi *enm;
1023: int s;
1.69 thorpej 1024:
1.190 christos 1025: /*
1026: * Prevent further calls to ioctl (for example turning off
1027: * promiscuous mode from the bridge code), which eventually can
1028: * call if_init() which can cause panics because the interface
1029: * is in the process of being detached. Return device not configured
1030: * instead.
1031: */
1032: ifp->if_ioctl = (int (*)(struct ifnet *, u_long, void *))enxio;
1033:
1.115 dyoung 1034: #if NBRIDGE > 0
1035: if (ifp->if_bridge)
1036: bridge_ifdetach(ifp);
1037: #endif
1038:
1.177 joerg 1039: bpf_detach(ifp);
1.64 thorpej 1040:
1041: #if NVLAN > 0
1042: if (ec->ec_nvlans)
1043: vlan_ifdetach(ifp);
1044: #endif
1.63 thorpej 1045:
1.79 thorpej 1046: s = splnet();
1.63 thorpej 1047: while ((enm = LIST_FIRST(&ec->ec_multiaddrs)) != NULL) {
1048: LIST_REMOVE(enm, enm_list);
1.100 jdolecek 1049: free(enm, M_IFMADDR);
1.63 thorpej 1050: ec->ec_multicnt--;
1051: }
1052: splx(s);
1.52 thorpej 1053:
1.189 chs 1054: ifp->if_mowner = NULL;
1.104 matt 1055: MOWNER_DETACH(&ec->ec_rx_mowner);
1056: MOWNER_DETACH(&ec->ec_tx_mowner);
1.53 thorpej 1057: }
1058:
1.56 thorpej 1059: #if 0
1060: /*
1061: * This is for reference. We have a table-driven version
1062: * of the little-endian crc32 generator, which is faster
1063: * than the double-loop.
1064: */
1.162 matt 1065: uint32_t
1066: ether_crc32_le(const uint8_t *buf, size_t len)
1.53 thorpej 1067: {
1.162 matt 1068: uint32_t c, crc, carry;
1.53 thorpej 1069: size_t i, j;
1070:
1071: crc = 0xffffffffU; /* initial value */
1072:
1073: for (i = 0; i < len; i++) {
1074: c = buf[i];
1075: for (j = 0; j < 8; j++) {
1076: carry = ((crc & 0x01) ? 1 : 0) ^ (c & 0x01);
1077: crc >>= 1;
1078: c >>= 1;
1079: if (carry)
1.56 thorpej 1080: crc = (crc ^ ETHER_CRC_POLY_LE);
1.53 thorpej 1081: }
1082: }
1083:
1084: return (crc);
1085: }
1.56 thorpej 1086: #else
1.162 matt 1087: uint32_t
1088: ether_crc32_le(const uint8_t *buf, size_t len)
1.56 thorpej 1089: {
1.162 matt 1090: static const uint32_t crctab[] = {
1.56 thorpej 1091: 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
1092: 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
1093: 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
1094: 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
1095: };
1.162 matt 1096: uint32_t crc;
1.98 thorpej 1097: size_t i;
1.56 thorpej 1098:
1099: crc = 0xffffffffU; /* initial value */
1100:
1101: for (i = 0; i < len; i++) {
1102: crc ^= buf[i];
1103: crc = (crc >> 4) ^ crctab[crc & 0xf];
1104: crc = (crc >> 4) ^ crctab[crc & 0xf];
1105: }
1106:
1107: return (crc);
1108: }
1109: #endif
1.53 thorpej 1110:
1.162 matt 1111: uint32_t
1112: ether_crc32_be(const uint8_t *buf, size_t len)
1.53 thorpej 1113: {
1.162 matt 1114: uint32_t c, crc, carry;
1.53 thorpej 1115: size_t i, j;
1116:
1117: crc = 0xffffffffU; /* initial value */
1118:
1119: for (i = 0; i < len; i++) {
1120: c = buf[i];
1121: for (j = 0; j < 8; j++) {
1122: carry = ((crc & 0x80000000U) ? 1 : 0) ^ (c & 0x01);
1123: crc <<= 1;
1124: c >>= 1;
1125: if (carry)
1126: crc = (crc ^ ETHER_CRC_POLY_BE) | carry;
1127: }
1128: }
1129:
1130: return (crc);
1.8 mycroft 1131: }
1132:
1.48 is 1133: #ifdef INET
1.118 yamt 1134: const uint8_t ether_ipmulticast_min[ETHER_ADDR_LEN] =
1135: { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
1136: const uint8_t ether_ipmulticast_max[ETHER_ADDR_LEN] =
1137: { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
1.48 is 1138: #endif
1.44 itojun 1139: #ifdef INET6
1.118 yamt 1140: const uint8_t ether_ip6multicast_min[ETHER_ADDR_LEN] =
1141: { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 };
1142: const uint8_t ether_ip6multicast_max[ETHER_ADDR_LEN] =
1143: { 0x33, 0x33, 0xff, 0xff, 0xff, 0xff };
1.44 itojun 1144: #endif
1.60 enami 1145:
1.3 hpeyerl 1146: /*
1.138 rpaulo 1147: * ether_aton implementation, not using a static buffer.
1148: */
1149: int
1.180 christos 1150: ether_aton_r(u_char *dest, size_t len, const char *str)
1.138 rpaulo 1151: {
1.180 christos 1152: const u_char *cp = (const void *)str;
1153: u_char *ep;
1154:
1.185 tsutsui 1155: #define atox(c) (((c) <= '9') ? ((c) - '0') : ((toupper(c) - 'A') + 10))
1.180 christos 1156:
1157: if (len < ETHER_ADDR_LEN)
1158: return ENOSPC;
1159:
1160: ep = dest + ETHER_ADDR_LEN;
1161:
1162: while (*cp) {
1163: if (!isxdigit(*cp))
1164: return EINVAL;
1165: *dest = atox(*cp);
1166: cp++;
1167: if (isxdigit(*cp)) {
1168: *dest = (*dest << 4) | atox(*cp);
1169: dest++;
1170: cp++;
1171: } else
1.181 christos 1172: dest++;
1.180 christos 1173: if (dest == ep)
1174: return *cp == '\0' ? 0 : ENAMETOOLONG;
1175: switch (*cp) {
1176: case ':':
1177: case '-':
1178: case '.':
1.179 jakllsch 1179: cp++;
1.180 christos 1180: break;
1.179 jakllsch 1181: }
1.180 christos 1182: }
1183: return ENOBUFS;
1.138 rpaulo 1184: }
1185:
1186: /*
1.60 enami 1187: * Convert a sockaddr into an Ethernet address or range of Ethernet
1188: * addresses.
1.3 hpeyerl 1189: */
1190: int
1.162 matt 1191: ether_multiaddr(const struct sockaddr *sa, uint8_t addrlo[ETHER_ADDR_LEN],
1192: uint8_t addrhi[ETHER_ADDR_LEN])
1.3 hpeyerl 1193: {
1.24 christos 1194: #ifdef INET
1.155 dyoung 1195: const struct sockaddr_in *sin;
1.24 christos 1196: #endif /* INET */
1.44 itojun 1197: #ifdef INET6
1.155 dyoung 1198: const struct sockaddr_in6 *sin6;
1.44 itojun 1199: #endif /* INET6 */
1.3 hpeyerl 1200:
1.60 enami 1201: switch (sa->sa_family) {
1.3 hpeyerl 1202:
1203: case AF_UNSPEC:
1.146 dyoung 1204: memcpy(addrlo, sa->sa_data, ETHER_ADDR_LEN);
1205: memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1.3 hpeyerl 1206: break;
1207:
1208: #ifdef INET
1209: case AF_INET:
1.155 dyoung 1210: sin = satocsin(sa);
1.3 hpeyerl 1211: if (sin->sin_addr.s_addr == INADDR_ANY) {
1212: /*
1.60 enami 1213: * An IP address of INADDR_ANY means listen to
1214: * or stop listening to all of the Ethernet
1215: * multicast addresses used for IP.
1.3 hpeyerl 1216: * (This is for the sake of IP multicast routers.)
1217: */
1.146 dyoung 1218: memcpy(addrlo, ether_ipmulticast_min, ETHER_ADDR_LEN);
1219: memcpy(addrhi, ether_ipmulticast_max, ETHER_ADDR_LEN);
1.3 hpeyerl 1220: }
1221: else {
1222: ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
1.146 dyoung 1223: memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1.3 hpeyerl 1224: }
1225: break;
1226: #endif
1.44 itojun 1227: #ifdef INET6
1228: case AF_INET6:
1.155 dyoung 1229: sin6 = satocsin6(sa);
1.47 itojun 1230: if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1.44 itojun 1231: /*
1.60 enami 1232: * An IP6 address of 0 means listen to or stop
1233: * listening to all of the Ethernet multicast
1234: * address used for IP6.
1.44 itojun 1235: * (This is used for multicast routers.)
1236: */
1.146 dyoung 1237: memcpy(addrlo, ether_ip6multicast_min, ETHER_ADDR_LEN);
1238: memcpy(addrhi, ether_ip6multicast_max, ETHER_ADDR_LEN);
1.44 itojun 1239: } else {
1240: ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, addrlo);
1.146 dyoung 1241: memcpy(addrhi, addrlo, ETHER_ADDR_LEN);
1.44 itojun 1242: }
1243: break;
1244: #endif
1.3 hpeyerl 1245:
1246: default:
1.146 dyoung 1247: return EAFNOSUPPORT;
1.60 enami 1248: }
1.146 dyoung 1249: return 0;
1.60 enami 1250: }
1251:
1252: /*
1253: * Add an Ethernet multicast address or range of addresses to the list for a
1254: * given interface.
1255: */
1256: int
1.155 dyoung 1257: ether_addmulti(const struct sockaddr *sa, struct ethercom *ec)
1.60 enami 1258: {
1259: struct ether_multi *enm;
1260: u_char addrlo[ETHER_ADDR_LEN];
1261: u_char addrhi[ETHER_ADDR_LEN];
1.79 thorpej 1262: int s = splnet(), error;
1.60 enami 1263:
1.155 dyoung 1264: error = ether_multiaddr(sa, addrlo, addrhi);
1.60 enami 1265: if (error != 0) {
1.3 hpeyerl 1266: splx(s);
1.146 dyoung 1267: return error;
1.3 hpeyerl 1268: }
1269:
1270: /*
1271: * Verify that we have valid Ethernet multicast addresses.
1272: */
1.186 yamt 1273: if (!ETHER_IS_MULTICAST(addrlo) || !ETHER_IS_MULTICAST(addrhi)) {
1.3 hpeyerl 1274: splx(s);
1.146 dyoung 1275: return EINVAL;
1.3 hpeyerl 1276: }
1277: /*
1278: * See if the address range is already in the list.
1279: */
1.22 is 1280: ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1.3 hpeyerl 1281: if (enm != NULL) {
1282: /*
1283: * Found it; just increment the reference count.
1284: */
1285: ++enm->enm_refcount;
1286: splx(s);
1.146 dyoung 1287: return 0;
1.3 hpeyerl 1288: }
1289: /*
1290: * New address or range; malloc a new multicast record
1291: * and link it into the interface's multicast list.
1292: */
1293: enm = (struct ether_multi *)malloc(sizeof(*enm), M_IFMADDR, M_NOWAIT);
1294: if (enm == NULL) {
1295: splx(s);
1.146 dyoung 1296: return ENOBUFS;
1.3 hpeyerl 1297: }
1.146 dyoung 1298: memcpy(enm->enm_addrlo, addrlo, 6);
1299: memcpy(enm->enm_addrhi, addrhi, 6);
1.3 hpeyerl 1300: enm->enm_refcount = 1;
1.22 is 1301: LIST_INSERT_HEAD(&ec->ec_multiaddrs, enm, enm_list);
1302: ec->ec_multicnt++;
1.3 hpeyerl 1303: splx(s);
1304: /*
1305: * Return ENETRESET to inform the driver that the list has changed
1306: * and its reception filter should be adjusted accordingly.
1307: */
1.146 dyoung 1308: return ENETRESET;
1.3 hpeyerl 1309: }
1310:
1311: /*
1312: * Delete a multicast address record.
1313: */
1314: int
1.155 dyoung 1315: ether_delmulti(const struct sockaddr *sa, struct ethercom *ec)
1.3 hpeyerl 1316: {
1.29 mrg 1317: struct ether_multi *enm;
1.60 enami 1318: u_char addrlo[ETHER_ADDR_LEN];
1319: u_char addrhi[ETHER_ADDR_LEN];
1.79 thorpej 1320: int s = splnet(), error;
1.3 hpeyerl 1321:
1.155 dyoung 1322: error = ether_multiaddr(sa, addrlo, addrhi);
1.60 enami 1323: if (error != 0) {
1.3 hpeyerl 1324: splx(s);
1.60 enami 1325: return (error);
1.3 hpeyerl 1326: }
1327:
1328: /*
1.66 thorpej 1329: * Look ur the address in our list.
1.3 hpeyerl 1330: */
1.22 is 1331: ETHER_LOOKUP_MULTI(addrlo, addrhi, ec, enm);
1.3 hpeyerl 1332: if (enm == NULL) {
1333: splx(s);
1334: return (ENXIO);
1335: }
1336: if (--enm->enm_refcount != 0) {
1337: /*
1338: * Still some claims to this record.
1339: */
1340: splx(s);
1341: return (0);
1342: }
1343: /*
1344: * No remaining claims to this record; unlink and free it.
1345: */
1.13 mycroft 1346: LIST_REMOVE(enm, enm_list);
1.3 hpeyerl 1347: free(enm, M_IFMADDR);
1.22 is 1348: ec->ec_multicnt--;
1.3 hpeyerl 1349: splx(s);
1350: /*
1351: * Return ENETRESET to inform the driver that the list has changed
1352: * and its reception filter should be adjusted accordingly.
1353: */
1354: return (ENETRESET);
1.66 thorpej 1355: }
1356:
1.170 dyoung 1357: void
1358: ether_set_ifflags_cb(struct ethercom *ec, ether_cb_t cb)
1359: {
1360: ec->ec_ifflags_cb = cb;
1361: }
1362:
1.66 thorpej 1363: /*
1364: * Common ioctls for Ethernet interfaces. Note, we must be
1365: * called at splnet().
1366: */
1367: int
1.147 christos 1368: ether_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1.66 thorpej 1369: {
1370: struct ethercom *ec = (void *) ifp;
1.193 msaitoh 1371: struct eccapreq *eccr;
1.66 thorpej 1372: struct ifreq *ifr = (struct ifreq *)data;
1.170 dyoung 1373: struct if_laddrreq *iflr = data;
1374: const struct sockaddr_dl *sdl;
1375: static const uint8_t zero[ETHER_ADDR_LEN];
1.169 dyoung 1376: int error;
1.66 thorpej 1377:
1378: switch (cmd) {
1.170 dyoung 1379: case SIOCINITIFADDR:
1.191 matt 1380: {
1381: struct ifaddr *ifa = (struct ifaddr *)data;
1382: if (ifa->ifa_addr->sa_family != AF_LINK
1383: && (ifp->if_flags & (IFF_UP|IFF_RUNNING)) !=
1384: (IFF_UP|IFF_RUNNING)) {
1.170 dyoung 1385: ifp->if_flags |= IFF_UP;
1386: if ((error = (*ifp->if_init)(ifp)) != 0)
1387: return error;
1388: }
1.66 thorpej 1389: #ifdef INET
1.191 matt 1390: if (ifa->ifa_addr->sa_family == AF_INET)
1391: arp_ifinit(ifp, ifa);
1.66 thorpej 1392: #endif /* INET */
1.169 dyoung 1393: return 0;
1.191 matt 1394: }
1.66 thorpej 1395:
1396: case SIOCSIFMTU:
1.82 thorpej 1397: {
1398: int maxmtu;
1399:
1400: if (ec->ec_capabilities & ETHERCAP_JUMBO_MTU)
1401: maxmtu = ETHERMTU_JUMBO;
1402: else
1403: maxmtu = ETHERMTU;
1404:
1405: if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > maxmtu)
1.169 dyoung 1406: return EINVAL;
1407: else if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
1408: return error;
1409: else if (ifp->if_flags & IFF_UP) {
1.88 thorpej 1410: /* Make sure the device notices the MTU change. */
1.169 dyoung 1411: return (*ifp->if_init)(ifp);
1412: } else
1413: return 0;
1.82 thorpej 1414: }
1.66 thorpej 1415:
1416: case SIOCSIFFLAGS:
1.170 dyoung 1417: if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1418: return error;
1.169 dyoung 1419: switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
1420: case IFF_RUNNING:
1.66 thorpej 1421: /*
1422: * If interface is marked down and it is running,
1423: * then stop and disable it.
1424: */
1425: (*ifp->if_stop)(ifp, 1);
1.169 dyoung 1426: break;
1427: case IFF_UP:
1.66 thorpej 1428: /*
1429: * If interface is marked up and it is stopped, then
1430: * start it.
1431: */
1.169 dyoung 1432: return (*ifp->if_init)(ifp);
1433: case IFF_UP|IFF_RUNNING:
1.170 dyoung 1434: error = 0;
1435: if (ec->ec_ifflags_cb == NULL ||
1436: (error = (*ec->ec_ifflags_cb)(ec)) == ENETRESET) {
1437: /*
1438: * Reset the interface to pick up
1439: * changes in any other flags that
1440: * affect the hardware state.
1441: */
1442: return (*ifp->if_init)(ifp);
1443: } else
1444: return error;
1.169 dyoung 1445: case 0:
1446: break;
1.66 thorpej 1447: }
1.169 dyoung 1448: return 0;
1.193 msaitoh 1449: case SIOCGETHERCAP:
1450: eccr = (struct eccapreq *)data;
1451: eccr->eccr_capabilities = ec->ec_capabilities;
1452: eccr->eccr_capenable = ec->ec_capenable;
1453: return 0;
1.66 thorpej 1454: case SIOCADDMULTI:
1.169 dyoung 1455: return ether_addmulti(ifreq_getaddr(cmd, ifr), ec);
1.66 thorpej 1456: case SIOCDELMULTI:
1.169 dyoung 1457: return ether_delmulti(ifreq_getaddr(cmd, ifr), ec);
1.160 dyoung 1458: case SIOCSIFMEDIA:
1459: case SIOCGIFMEDIA:
1460: if (ec->ec_mii == NULL)
1.169 dyoung 1461: return ENOTTY;
1462: return ifmedia_ioctl(ifp, ifr, &ec->ec_mii->mii_media, cmd);
1.170 dyoung 1463: case SIOCALIFADDR:
1464: sdl = satocsdl(sstocsa(&iflr->addr));
1465: if (sdl->sdl_family != AF_LINK)
1466: ;
1467: else if (ETHER_IS_MULTICAST(CLLADDR(sdl)))
1468: return EINVAL;
1469: else if (memcmp(zero, CLLADDR(sdl), sizeof(zero)) == 0)
1470: return EINVAL;
1471: /*FALLTHROUGH*/
1472: default:
1.161 dyoung 1473: return ifioctl_common(ifp, cmd, data);
1.66 thorpej 1474: }
1.169 dyoung 1475: return 0;
1.3 hpeyerl 1476: }
1.200 joerg 1477:
1478: static int
1479: ether_multicast_sysctl(SYSCTLFN_ARGS)
1480: {
1481: struct ether_multi *enm;
1482: struct ether_multi_sysctl addr;
1483: struct ifnet *ifp;
1484: struct ethercom *ec;
1485: int error;
1486: size_t written;
1487:
1488: if (namelen != 1)
1489: return EINVAL;
1490:
1491: ifp = if_byindex(name[0]);
1492: if (ifp == NULL)
1493: return ENODEV;
1494: if (ifp->if_type != IFT_ETHER) {
1495: *oldlenp = 0;
1496: return 0;
1497: }
1498: ec = (struct ethercom *)ifp;
1499:
1500: if (oldp == NULL) {
1501: *oldlenp = ec->ec_multicnt * sizeof(addr);
1502: return 0;
1503: }
1504:
1505: memset(&addr, 0, sizeof(addr));
1506: error = 0;
1507: written = 0;
1508:
1509: LIST_FOREACH(enm, &ec->ec_multiaddrs, enm_list) {
1510: if (written + sizeof(addr) > *oldlenp)
1511: break;
1512: addr.enm_refcount = enm->enm_refcount;
1513: memcpy(addr.enm_addrlo, enm->enm_addrlo, ETHER_ADDR_LEN);
1514: memcpy(addr.enm_addrhi, enm->enm_addrhi, ETHER_ADDR_LEN);
1515: error = sysctl_copyout(l, &addr, oldp, sizeof(addr));
1516: if (error)
1517: break;
1518: written += sizeof(addr);
1519: oldp = (char *)oldp + sizeof(addr);
1520: }
1521:
1522: *oldlenp = written;
1523: return error;
1524: }
1525:
1526: SYSCTL_SETUP(sysctl_net_ether_setup, "sysctl net.ether subtree setup")
1527: {
1528: const struct sysctlnode *rnode = NULL;
1529:
1530: sysctl_createv(clog, 0, NULL, &rnode,
1531: CTLFLAG_PERMANENT,
1532: CTLTYPE_NODE, "ether",
1533: SYSCTL_DESCR("Ethernet-specific information"),
1534: NULL, 0, NULL, 0,
1535: CTL_NET, CTL_CREATE, CTL_EOL);
1536:
1537: sysctl_createv(clog, 0, &rnode, NULL,
1538: CTLFLAG_PERMANENT,
1539: CTLTYPE_NODE, "multicast",
1540: SYSCTL_DESCR("multicast addresses"),
1541: ether_multicast_sysctl, 0, NULL, 0,
1542: CTL_CREATE, CTL_EOL);
1543: }
1.203 ozaki-r 1544:
1545: void
1546: etherinit(void)
1547: {
1548: mutex_init(&bigpktpps_lock, MUTEX_DEFAULT, IPL_NET);
1549: }
CVSweb <webmaster@jp.NetBSD.org>