]> git.saurik.com Git - apple/xnu.git/blame - bsd/net/if_ethersubr.c
xnu-792.6.56.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 *
ff6e181a
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
1c79356b 12 *
ff6e181a
A
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
ff6e181a
A
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
1c79356b
A
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * Copyright (c) 1982, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 *
55 * @(#)if_ethersubr.c 8.1 (Berkeley) 6/10/93
9bccf70c 56 * $FreeBSD: src/sys/net/if_ethersubr.c,v 1.70.2.17 2001/08/01 00:47:49 fenner Exp $
1c79356b
A
57 */
58
1c79356b
A
59#include <sys/param.h>
60#include <sys/systm.h>
61#include <sys/kernel.h>
62#include <sys/malloc.h>
63#include <sys/mbuf.h>
64#include <sys/socket.h>
65#include <sys/sockio.h>
66#include <sys/sysctl.h>
67
68#include <net/if.h>
1c79356b
A
69#include <net/route.h>
70#include <net/if_llc.h>
71#include <net/if_dl.h>
72#include <net/if_types.h>
1c79356b 73
9bccf70c 74#if INET || INET6
1c79356b
A
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
1c79356b
A
87#include <sys/socketvar.h>
88
89#if LLC && CCITT
90extern 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
1c79356b
A
102extern u_char etherbroadcastaddr[];
103#define senderr(e) do { error = (e); goto bad;} while (0)
1c79356b
A
104
105/*
106 * Perform common duties while attaching to interface list
107 */
108
1c79356b 109int
91447636
A
110ether_resolvemulti(
111 struct ifnet *ifp,
112 struct sockaddr **llsa,
113 struct sockaddr *sa)
1c79356b
A
114{
115 struct sockaddr_dl *sdl;
116 struct sockaddr_in *sin;
117 u_char *e_addr;
118#if INET6
119 struct sockaddr_in6 *sin6;
120#endif
121
122
123 switch(sa->sa_family) {
124 case AF_UNSPEC:
125 /* AppleTalk uses AF_UNSPEC for multicast registration.
126 * No mapping needed. Just check that it's a valid MC address.
127 */
128 e_addr = &sa->sa_data[0];
129 if ((e_addr[0] & 1) != 1)
130 return EADDRNOTAVAIL;
131 *llsa = 0;
132 return 0;
133
134 case AF_LINK:
135 /*
136 * No mapping needed. Just check that it's a valid MC address.
137 */
138 sdl = (struct sockaddr_dl *)sa;
139 e_addr = LLADDR(sdl);
140 if ((e_addr[0] & 1) != 1)
141 return EADDRNOTAVAIL;
142 *llsa = 0;
143 return 0;
144
145#if INET
146 case AF_INET:
147 sin = (struct sockaddr_in *)sa;
148 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
149 return EADDRNOTAVAIL;
150 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
151 M_WAITOK);
152 sdl->sdl_len = sizeof *sdl;
153 sdl->sdl_family = AF_LINK;
154 sdl->sdl_index = ifp->if_index;
155 sdl->sdl_type = IFT_ETHER;
156 sdl->sdl_nlen = 0;
157 sdl->sdl_alen = ETHER_ADDR_LEN;
158 sdl->sdl_slen = 0;
159 e_addr = LLADDR(sdl);
160 ETHER_MAP_IP_MULTICAST(&sin->sin_addr, e_addr);
161 *llsa = (struct sockaddr *)sdl;
162 return 0;
163#endif
164#if INET6
165 case AF_INET6:
166 sin6 = (struct sockaddr_in6 *)sa;
167 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
168 /*
169 * An IP6 address of 0 means listen to all
170 * of the Ethernet multicast address used for IP6.
171 * (This is used for multicast routers.)
172 */
173 ifp->if_flags |= IFF_ALLMULTI;
174 *llsa = 0;
175 return 0;
176 }
177 MALLOC(sdl, struct sockaddr_dl *, sizeof *sdl, M_IFMADDR,
178 M_WAITOK);
179 sdl->sdl_len = sizeof *sdl;
180 sdl->sdl_family = AF_LINK;
181 sdl->sdl_index = ifp->if_index;
182 sdl->sdl_type = IFT_ETHER;
183 sdl->sdl_nlen = 0;
184 sdl->sdl_alen = ETHER_ADDR_LEN;
185 sdl->sdl_slen = 0;
186 e_addr = LLADDR(sdl);
187 ETHER_MAP_IPV6_MULTICAST(&sin6->sin6_addr, e_addr);
188 kprintf("ether_resolvemulti Adding %x:%x:%x:%x:%x:%x\n",
189 e_addr[0], e_addr[1], e_addr[2], e_addr[3], e_addr[4], e_addr[5]);
190 *llsa = (struct sockaddr *)sdl;
191 return 0;
192#endif
193
194 default:
195 /*
196 * Well, the text isn't quite right, but it's the name
197 * that counts...
198 */
199 return EAFNOSUPPORT;
200 }
201}
202
203
1c79356b
A
204/*
205 * Convert Ethernet address to printable (loggable) representation.
206 */
207static u_char digits[] = "0123456789abcdef";
208char *
209ether_sprintf(p, ap)
210 register u_char *p;
211 register u_char *ap;
212{ register char *cp;
213 register i;
214
215 for (cp = p, i = 0; i < 6; i++) {
216 *cp++ = digits[*ap >> 4];
217 *cp++ = digits[*ap++ & 0xf];
218 *cp++ = ':';
219 }
220 *--cp = 0;
221 return (p);
222}