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