]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_ethersubr.c
xnu-201.14.tar.gz
[apple/xnu.git] / bsd / net / if_ethersubr.c
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
80 #if INET
81 #include <netinet/in.h>
82 #include <netinet/in_var.h>
83 #include <netinet/if_ether.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #endif
87
88 #if IPX
89 #include <netipx/ipx.h>
90 #include <netipx/ipx_if.h>
91 #endif
92
93 #if NS
94 #include <netns/ns.h>
95 #include <netns/ns_if.h>
96 ushort ns_nettype;
97 int ether_outputdebug = 0;
98 int ether_inputdebug = 0;
99 #endif
100
101 #if ISO
102 #include <netiso/argo_debug.h>
103 #include <netiso/iso.h>
104 #include <netiso/iso_var.h>
105 #include <netiso/iso_snpac.h>
106 #endif
107
108 /*#if LLC
109 #include <netccitt/dll.h>
110 #include <netccitt/llc_var.h>
111 #endif*/
112
113 #include <sys/socketvar.h>
114
115 #if LLC && CCITT
116 extern struct ifqueue pkintrq;
117 #endif
118
119 #if BRIDGE
120 #include <net/bridge.h>
121 #endif
122
123 /* #include "vlan.h" */
124 #if NVLAN > 0
125 #include <net/if_vlan_var.h>
126 #endif /* NVLAN > 0 */
127
128 static int ether_resolvemulti __P((struct ifnet *, struct sockaddr **,
129 struct sockaddr *));
130 extern u_char etherbroadcastaddr[];
131 #define senderr(e) do { error = (e); goto bad;} while (0)
132 #define IFP2AC(IFP) ((struct arpcom *)IFP)
133
134 /*
135 * Perform common duties while attaching to interface list
136 */
137
138
139 void
140 ether_ifattach(ifp)
141 register struct ifnet *ifp;
142 {
143 register struct ifaddr *ifa;
144 register struct sockaddr_dl *sdl;
145 boolean_t funnel_state;
146
147 funnel_state = thread_funnel_set(network_flock, TRUE);
148 printf("ether_ifattach called for %s\n", ifp->if_name);
149 ether_family_init();
150
151 ifp->if_name = "en";
152 ifp->if_family = APPLE_IF_FAM_ETHERNET;
153 ifp->if_type = IFT_ETHER;
154 ifp->if_addrlen = 6;
155 ifp->if_hdrlen = 14;
156 ifp->if_mtu = ETHERMTU;
157 ifp->if_resolvemulti = ether_resolvemulti;
158 if (ifp->if_baudrate == 0)
159 ifp->if_baudrate = 10000000;
160
161 dlil_if_attach(ifp);
162 ifa = ifnet_addrs[ifp->if_index - 1];
163 if (ifa == 0) {
164 printf("ether_ifattach: no lladdr!\n");
165 (void) thread_funnel_set(network_flock, funnel_state);
166 return;
167 }
168 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
169 sdl->sdl_type = IFT_ETHER;
170 sdl->sdl_alen = ifp->if_addrlen;
171 bcopy((IFP2AC(ifp))->ac_enaddr, LLADDR(sdl), ifp->if_addrlen);
172 #ifdef INET6
173 in6_ifattach_getifid(ifp);
174 #endif
175 (void) thread_funnel_set(network_flock, funnel_state);
176 }
177
178 SYSCTL_DECL(_net_link);
179 SYSCTL_NODE(_net_link, IFT_ETHER, ether, CTLFLAG_RW, 0, "Ethernet");
180
181 int
182 ether_resolvemulti(ifp, llsa, sa)
183 struct ifnet *ifp;
184 struct sockaddr **llsa;
185 struct sockaddr *sa;
186 {
187 struct sockaddr_dl *sdl;
188 struct sockaddr_in *sin;
189 u_char *e_addr;
190 #if INET6
191 struct sockaddr_in6 *sin6;
192 #endif
193
194
195 switch(sa->sa_family) {
196 case AF_UNSPEC:
197 /* AppleTalk uses AF_UNSPEC for multicast registration.
198 * No mapping needed. Just check that it's a valid MC address.
199 */
200 e_addr = &sa->sa_data[0];
201 if ((e_addr[0] & 1) != 1)
202 return EADDRNOTAVAIL;
203 *llsa = 0;
204 return 0;
205
206 case AF_LINK:
207 /*
208 * No mapping needed. Just check that it's a valid MC address.
209 */
210 sdl = (struct sockaddr_dl *)sa;
211 e_addr = LLADDR(sdl);
212 if ((e_addr[0] & 1) != 1)
213 return EADDRNOTAVAIL;
214 *llsa = 0;
215 return 0;
216
217 #if INET
218 case AF_INET:
219 sin = (struct sockaddr_in *)sa;
220 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
221 return EADDRNOTAVAIL;
222 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
223 M_WAITOK);
224 sdl->sdl_len = sizeof *sdl;
225 sdl->sdl_family = AF_LINK;
226 sdl->sdl_index = ifp->if_index;
227 sdl->sdl_type = IFT_ETHER;
228 sdl->sdl_nlen = 0;
229 sdl->sdl_alen = ETHER_ADDR_LEN;
230 sdl->sdl_slen = 0;
231 e_addr = LLADDR(sdl);
232 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
233 *llsa = (struct sockaddr *)sdl;
234 return 0;
235 #endif
236 #if INET6
237 case AF_INET6:
238 sin6 = (struct sockaddr_in6 *)sa;
239 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
240 /*
241 * An IP6 address of 0 means listen to all
242 * of the Ethernet multicast address used for IP6.
243 * (This is used for multicast routers.)
244 */
245 ifp->if_flags |= IFF_ALLMULTI;
246 *llsa = 0;
247 return 0;
248 }
249 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
250 M_WAITOK);
251 sdl->sdl_len = sizeof *sdl;
252 sdl->sdl_family = AF_LINK;
253 sdl->sdl_index = ifp->if_index;
254 sdl->sdl_type = IFT_ETHER;
255 sdl->sdl_nlen = 0;
256 sdl->sdl_alen = ETHER_ADDR_LEN;
257 sdl->sdl_slen = 0;
258 e_addr = LLADDR(sdl);
259 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
260 kprintf("ether_resolvemulti Adding %x:%x:%x:%x:%x:%x\n",
261 e_addr[0], e_addr[1], e_addr[2], e_addr[3], e_addr[4], e_addr[5]);
262 *llsa = (struct sockaddr *)sdl;
263 return 0;
264 #endif
265
266 default:
267 /*
268 * Well, the text isn't quite right, but it's the name
269 * that counts...
270 */
271 return EAFNOSUPPORT;
272 }
273 }
274
275
276
277
278
279 u_char ether_ipmulticast_min[6] = { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 };
280 u_char ether_ipmulticast_max[6] = { 0x01, 0x00, 0x5e, 0x7f, 0xff, 0xff };
281 /*
282 * Add an Ethernet multicast address or range of addresses to the list for a
283 * given interface.
284 */
285 int
286 ether_addmulti(ifr, ac)
287 struct ifreq *ifr;
288 register struct arpcom *ac;
289 {
290 register struct ether_multi *enm;
291 struct sockaddr_in *sin;
292 u_char addrlo[6];
293 u_char addrhi[6];
294 int s = splimp();
295
296 switch (ifr->ifr_addr.sa_family) {
297
298 case AF_UNSPEC:
299 bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
300 bcopy(addrlo, addrhi, 6);
301 break;
302
303 #if INET
304 case AF_INET:
305 sin = (struct sockaddr_in *)&(ifr->ifr_addr);
306 if (sin->sin_addr.s_addr == INADDR_ANY) {
307 /*
308 * An IP address of INADDR_ANY means listen to all
309 * of the Ethernet multicast addresses used for IP.
310 * (This is for the sake of IP multicast routers.)
311 */
312 bcopy(ether_ipmulticast_min, addrlo, 6);
313 bcopy(ether_ipmulticast_max, addrhi, 6);
314 }
315 else {
316 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
317 bcopy(addrlo, addrhi, 6);
318 }
319 break;
320 #endif
321
322 default:
323 splx(s);
324 return (EAFNOSUPPORT);
325 }
326
327 /*
328 * Verify that we have valid Ethernet multicast addresses.
329 */
330 if ((addrlo[0] & 0x01) != 1 || (addrhi[0] & 0x01) != 1) {
331 splx(s);
332 return (EINVAL);
333 }
334 /*
335 * See if the address range is already in the list.
336 */
337 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
338 if (enm != NULL) {
339 /*
340 * Found it; just increment the reference count.
341 */
342 ++enm->enm_refcount;
343 splx(s);
344 return (0);
345 }
346 /*
347 * New address or range; malloc a new multicast record
348 * and link it into the interface's multicast list.
349 */
350 enm = (struct ether_multi *)_MALLOC(sizeof(*enm), M_IFMADDR, M_WAITOK);
351 if (enm == NULL) {
352 splx(s);
353 return (ENOBUFS);
354 }
355 bcopy(addrlo, enm->enm_addrlo, 6);
356 bcopy(addrhi, enm->enm_addrhi, 6);
357 enm->enm_ac = ac;
358 enm->enm_refcount = 1;
359 enm->enm_next = ac->ac_multiaddrs;
360 ac->ac_multiaddrs = enm;
361 splx(s);
362 /*
363 * Return ENETRESET to inform the driver that the list has changed
364 * and its reception filter should be adjusted accordingly.
365 */
366 return (ENETRESET);
367 }
368
369 /*
370 * Delete a multicast address record.
371 */
372 int
373 ether_delmulti(ifr, ac, ret_mca)
374 struct ifreq *ifr;
375 register struct arpcom *ac;
376 struct ether_addr * ret_mca;
377 {
378 register struct ether_multi *enm;
379 register struct ether_multi **p;
380 struct sockaddr_in *sin;
381 u_char addrlo[6];
382 u_char addrhi[6];
383 int s = splimp();
384
385 switch (ifr->ifr_addr.sa_family) {
386
387 case AF_UNSPEC:
388 bcopy(ifr->ifr_addr.sa_data, addrlo, 6);
389 bcopy(addrlo, addrhi, 6);
390 break;
391
392 #if INET
393 case AF_INET:
394 sin = (struct sockaddr_in *)&(ifr->ifr_addr);
395 if (sin->sin_addr.s_addr == INADDR_ANY) {
396 /*
397 * An IP address of INADDR_ANY means stop listening
398 * to the range of Ethernet multicast addresses used
399 * for IP.
400 */
401 bcopy(ether_ipmulticast_min, addrlo, 6);
402 bcopy(ether_ipmulticast_max, addrhi, 6);
403 }
404 else {
405 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, addrlo);
406 bcopy(addrlo, addrhi, 6);
407 }
408 break;
409 #endif
410
411 default:
412 splx(s);
413 return (EAFNOSUPPORT);
414 }
415
416 /*
417 * Look up the address in our list.
418 */
419 ETHER_LOOKUP_MULTI(addrlo, addrhi, ac, enm);
420 if (enm == NULL) {
421 splx(s);
422 return (ENXIO);
423 }
424 if (--enm->enm_refcount != 0) {
425 /*
426 * Still some claims to this record.
427 */
428 splx(s);
429 return (0);
430 }
431
432 /* save the low and high address of the range before deletion */
433 if (ret_mca) {
434 *ret_mca = *((struct ether_addr *)addrlo);
435 *(ret_mca + 1) = *((struct ether_addr *)addrhi);
436 }
437
438 /*
439 * No remaining claims to this record; unlink and free it.
440 */
441 for (p = &enm->enm_ac->ac_multiaddrs;
442 *p != enm;
443 p = &(*p)->enm_next)
444 continue;
445 *p = (*p)->enm_next;
446 FREE(enm, M_IFMADDR);
447 splx(s);
448 /*
449 * Return ENETRESET to inform the driver that the list has changed
450 * and its reception filter should be adjusted accordingly.
451 */
452 return (ENETRESET);
453 }
454
455 /*
456 * Convert Ethernet address to printable (loggable) representation.
457 */
458 static u_char digits[] = "0123456789abcdef";
459 char *
460 ether_sprintf(p, ap)
461 register u_char *p;
462 register u_char *ap;
463 { register char *cp;
464 register i;
465
466 for (cp = p, i = 0; i < 6; i++) {
467 *cp++ = digits[*ap >> 4];
468 *cp++ = digits[*ap++ & 0xf];
469 *cp++ = ':';
470 }
471 *--cp = 0;
472 return (p);
473 }