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