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