]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if_ethersubr.c
xnu-201.tar.gz
[apple/xnu.git] / bsd / net / if_ethersubr.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * Copyright (c) 1982, 1989, 1993
24 * The Regents of the University of California. All rights reserved.
25 *
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
28 * are met:
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 *
54 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
55 */
56
57#if NOTFB31
58#include "opt_atalk.h"
59#include "opt_inet.h"
60#include "opt_ipx.h"
61#include "opt_bdg.h"
62#endif
63
64#include <sys/param.h>
65#include <sys/systm.h>
66#include <sys/kernel.h>
67#include <sys/malloc.h>
68#include <sys/mbuf.h>
69#include <sys/socket.h>
70#include <sys/sockio.h>
71#include <sys/sysctl.h>
72
73#include <net/if.h>
74#include <net/netisr.h>
75#include <net/route.h>
76#include <net/if_llc.h>
77#include <net/if_dl.h>
78#include <net/if_types.h>
79#include <net/ndrv.h>
80
81#if INET
82#include <netinet/in.h>
83#include <netinet/in_var.h>
84#include <netinet/if_ether.h>
85#include <netinet/in_systm.h>
86#include <netinet/ip.h>
87#endif
88
89#if IPX
90#include <netipx/ipx.h>
91#include <netipx/ipx_if.h>
92#endif
93
94#if NS
95#include <netns/ns.h>
96#include <netns/ns_if.h>
97ushort ns_nettype;
98int ether_outputdebug = 0;
99int ether_inputdebug = 0;
100#endif
101
102#if ISO
103#include <netiso/argo_debug.h>
104#include <netiso/iso.h>
105#include <netiso/iso_var.h>
106#include <netiso/iso_snpac.h>
107#endif
108
109/*#if LLC
110#include <netccitt/dll.h>
111#include <netccitt/llc_var.h>
112#endif*/
113
114#include <sys/socketvar.h>
115
116#if LLC && CCITT
117extern struct ifqueue pkintrq;
118#endif
119
120#if BRIDGE
121#include <net/bridge.h>
122#endif
123
124/* #include "vlan.h" */
125#if NVLAN > 0
126#include <net/if_vlan_var.h>
127#endif /* NVLAN > 0 */
128
129static int ether_resolvemulti __P((struct ifnet *, struct sockaddr **,
130 struct sockaddr *));
131extern u_char etherbroadcastaddr[];
132#define senderr(e) do { error = (e); goto bad;} while (0)
133#define IFP2AC(IFP) ((struct arpcom *)IFP)
134
135/*
136 * Perform common duties while attaching to interface list
137 */
138
139
140void
141ether_ifattach(ifp)
142 register struct ifnet *ifp;
143{
144 register struct ifaddr *ifa;
145 register struct sockaddr_dl *sdl;
146 boolean_t funnel_state;
147
148 funnel_state = thread_funnel_set(network_flock, TRUE);
149 printf("ether_ifattach called for %s\n", ifp->if_name);
150 ether_family_init();
151
152 ifp->if_name = "en";
153 ifp->if_family = APPLE_IF_FAM_ETHERNET;
154 ifp->if_type = IFT_ETHER;
155 ifp->if_addrlen = 6;
156 ifp->if_hdrlen = 14;
157 ifp->if_mtu = ETHERMTU;
158 ifp->if_resolvemulti = ether_resolvemulti;
159 if (ifp->if_baudrate == 0)
160 ifp->if_baudrate = 10000000;
161
162 dlil_if_attach(ifp);
163 ifa = ifnet_addrs[ifp->if_index - 1];
164 if (ifa == 0) {
165 printf("ether_ifattach: no lladdr!\n");
166 (void) thread_funnel_set(network_flock, funnel_state);
167 return;
168 }
169 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
170 sdl->sdl_type = IFT_ETHER;
171 sdl->sdl_alen = ifp->if_addrlen;
172 bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
173#ifdef INET6
174 in6_ifattach_getifid(ifp);
175#endif
176 (void) thread_funnel_set(network_flock, funnel_state);
177}
178
179SYSCTL_DECL(_net_link);
180SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
181
182int
183ether_resolvemulti(ifp, llsa, sa)
184 struct ifnet *ifp;
185 struct sockaddr **llsa;
186 struct sockaddr *sa;
187{
188 struct sockaddr_dl *sdl;
189 struct sockaddr_in *sin;
190 u_char *e_addr;
191#if INET6
192 struct sockaddr_in6 *sin6;
193#endif
194
195
196 switch(sa->sa_family) {
197 case AF_UNSPEC:
198 /* AppleTalk uses AF_UNSPEC for multicast registration.
199 * No mapping needed. Just check that it's a valid MC address.
200 */
201 e_addr = &sa->sa_data[0];
202 if ((e_addr[0] & 1) != 1)
203 return EADDRNOTAVAIL;
204 *llsa = 0;
205 return 0;
206
207 case AF_LINK:
208 /*
209 * No mapping needed. Just check that it's a valid MC address.
210 */
211 sdl = (struct sockaddr_dl *)sa;
212 e_addr = LLADDR(sdl);
213 if ((e_addr[0] & 1) != 1)
214 return EADDRNOTAVAIL;
215 *llsa = 0;
216 return 0;
217
218#if INET
219 case AF_INET:
220 sin = (struct sockaddr_in *)sa;
221 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
222 return EADDRNOTAVAIL;
223 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
224 M_WAITOK);
225 sdl->sdl_len = sizeof *sdl;
226 sdl->sdl_family = AF_LINK;
227 sdl->sdl_index = ifp->if_index;
228 sdl->sdl_type = IFT_ETHER;
229 sdl->sdl_nlen = 0;
230 sdl->sdl_alen = ETHER_ADDR_LEN;
231 sdl->sdl_slen = 0;
232 e_addr = LLADDR(sdl);
233 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
234 *llsa = (struct sockaddr *)sdl;
235 return 0;
236#endif
237#if INET6
238 case AF_INET6:
239 sin6 = (struct sockaddr_in6 *)sa;
240 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
241 /*
242 * An IP6 address of 0 means listen to all
243 * of the Ethernet multicast address used for IP6.
244 * (This is used for multicast routers.)
245 */
246 ifp->if_flags |= IFF_ALLMULTI;
247 *llsa = 0;
248 return 0;
249 }
250 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
251 M_WAITOK);
252 sdl->sdl_len = sizeof *sdl;
253 sdl->sdl_family = AF_LINK;
254 sdl->sdl_index = ifp->if_index;
255 sdl->sdl_type = IFT_ETHER;
256 sdl->sdl_nlen = 0;
257 sdl->sdl_alen = ETHER_ADDR_LEN;
258 sdl->sdl_slen = 0;
259 e_addr = LLADDR(sdl);
260 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
261 kprintf("ether_resolvemulti Adding %x:%x:%x:%x:%x:%x\n",
262 e_addr[0], e_addr[1], e_addr[2], e_addr[3], e_addr[4], e_addr[5]);
263 *llsa = (struct sockaddr *)sdl;
264 return 0;
265#endif
266
267 default:
268 /*
269 * Well, the text isn't quite right, but it's the name
270 * that counts...
271 */
272 return EAFNOSUPPORT;
273 }
274}
275
276
277
278
279
280u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
281u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
282/*
283 * Add an Ethernet multicast address or range of addresses to the list for a
284 * given interface.
285 */
286int
287ether_addmulti(ifr, ac)
288 struct ifreq *ifr;
289 register struct arpcom *ac;
290{
291 register struct ether_multi *enm;
292 struct sockaddr_in *sin;
293 u_char addrlo[6];
294 u_char addrhi[6];
295 int s = splimp();
296
297 switch (ifr->ifr_addr.sa_family) {
298
299 case AF_UNSPEC:
300 bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
301 bcopy(addrlo, addrhi, 6);
302 break;
303
304#if INET
305 case AF_INET:
306 sin = (struct sockaddr_in *)&(ifr->ifr_addr);
307 if (sin->sin_addr.s_addr == INADDR_ANY) {
308 /*
309 * An IP address of INADDR_ANY means listen to all
310 * of the Ethernet multicast addresses used for IP.
311 * (This is for the sake of IP multicast routers.)
312 */
313 bcopy(ether_ipmulticast_min, addrlo, 6);
314 bcopy(ether_ipmulticast_max, addrhi, 6);
315 }
316 else {
317 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
318 bcopy(addrlo, addrhi, 6);
319 }
320 break;
321#endif
322
323 default:
324 splx(s);
325 return (EAFNOSUPPORT);
326 }
327
328 /*
329 * Verify that we have valid Ethernet multicast addresses.
330 */
331 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
332 splx(s);
333 return (EINVAL);
334 }
335 /*
336 * See if the address range is already in the list.
337 */
338 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
339 if (enm != NULL) {
340 /*
341 * Found it; just increment the reference count.
342 */
343 ++enm->enm_refcount;
344 splx(s);
345 return (0);
346 }
347 /*
348 * New address or range; malloc a new multicast record
349 * and link it into the interface's multicast list.
350 */
351 enm = (struct ether_multi *)_MALLOC(sizeof(*enm), M_IFMADDR, M_WAITOK);
352 if (enm == NULL) {
353 splx(s);
354 return (ENOBUFS);
355 }
356 bcopy(addrlo, enm->enm_addrlo, 6);
357 bcopy(addrhi, enm->enm_addrhi, 6);
358 enm->enm_ac = ac;
359 enm->enm_refcount = 1;
360 enm->enm_next = ac->ac_multiaddrs;
361 ac->ac_multiaddrs = enm;
362 splx(s);
363 /*
364 * Return ENETRESET to inform the driver that the list has changed
365 * and its reception filter should be adjusted accordingly.
366 */
367 return (ENETRESET);
368}
369
370/*
371 * Delete a multicast address record.
372 */
373int
374ether_delmulti(ifr, ac, ret_mca)
375 struct ifreq *ifr;
376 register struct arpcom *ac;
377 struct ether_addr * ret_mca;
378{
379 register struct ether_multi *enm;
380 register struct ether_multi **p;
381 struct sockaddr_in *sin;
382 u_char addrlo[6];
383 u_char addrhi[6];
384 int s = splimp();
385
386 switch (ifr->ifr_addr.sa_family) {
387
388 case AF_UNSPEC:
389 bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
390 bcopy(addrlo, addrhi, 6);
391 break;
392
393#if INET
394 case AF_INET:
395 sin = (struct sockaddr_in *)&(ifr->ifr_addr);
396 if (sin->sin_addr.s_addr == INADDR_ANY) {
397 /*
398 * An IP address of INADDR_ANY means stop listening
399 * to the range of Ethernet multicast addresses used
400 * for IP.
401 */
402 bcopy(ether_ipmulticast_min, addrlo, 6);
403 bcopy(ether_ipmulticast_max, addrhi, 6);
404 }
405 else {
406 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
407 bcopy(addrlo, addrhi, 6);
408 }
409 break;
410#endif
411
412 default:
413 splx(s);
414 return (EAFNOSUPPORT);
415 }
416
417 /*
418 * Look up the address in our list.
419 */
420 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
421 if (enm == NULL) {
422 splx(s);
423 return (ENXIO);
424 }
425 if (--enm->enm_refcount != 0) {
426 /*
427 * Still some claims to this record.
428 */
429 splx(s);
430 return (0);
431 }
432
433 /* save the low and high address of the range before deletion */
434 if (ret_mca) {
435 *ret_mca = *((struct ether_addr *)addrlo);
436 *(ret_mca + 1) = *((struct ether_addr *)addrhi);
437 }
438
439 /*
440 * No remaining claims to this record; unlink and free it.
441 */
442 for (p = &enm->enm_ac->ac_multiaddrs;
443 *p != enm;
444 p = &(*p)->enm_next)
445 continue;
446 *p = (*p)->enm_next;
447 FREE(enm, M_IFMADDR);
448 splx(s);
449 /*
450 * Return ENETRESET to inform the driver that the list has changed
451 * and its reception filter should be adjusted accordingly.
452 */
453 return (ENETRESET);
454}
455
456/*
457 * Convert Ethernet address to printable (loggable) representation.
458 */
459static u_char digits[] = "0123456789abcdef";
460char *
461ether_sprintf(p, ap)
462 register u_char *p;
463 register u_char *ap;
464{ register char *cp;
465 register i;
466
467 for (cp = p, i = 0; i < 6; i++) {
468 *cp++ = digits[*ap >> 4];
469 *cp++ = digits[*ap++ & 0xf];
470 *cp++ = ':';
471 }
472 *--cp = 0;
473 return (p);
474}