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