in6_ifattach.c revision 1.1.2.1 1 1.1.2.1 itojun /*
2 1.1.2.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 1.1.2.1 itojun * All rights reserved.
4 1.1.2.1 itojun *
5 1.1.2.1 itojun * Redistribution and use in source and binary forms, with or without
6 1.1.2.1 itojun * modification, are permitted provided that the following conditions
7 1.1.2.1 itojun * are met:
8 1.1.2.1 itojun * 1. Redistributions of source code must retain the above copyright
9 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer.
10 1.1.2.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
11 1.1.2.1 itojun * notice, this list of conditions and the following disclaimer in the
12 1.1.2.1 itojun * documentation and/or other materials provided with the distribution.
13 1.1.2.1 itojun * 3. Neither the name of the project nor the names of its contributors
14 1.1.2.1 itojun * may be used to endorse or promote products derived from this software
15 1.1.2.1 itojun * without specific prior written permission.
16 1.1.2.1 itojun *
17 1.1.2.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 1.1.2.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1.2.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1.2.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 1.1.2.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1.2.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1.2.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1.2.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1.2.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1.2.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1.2.1 itojun * SUCH DAMAGE.
28 1.1.2.1 itojun */
29 1.1.2.1 itojun
30 1.1.2.1 itojun #include <sys/param.h>
31 1.1.2.1 itojun #include <sys/systm.h>
32 1.1.2.1 itojun #include <sys/malloc.h>
33 1.1.2.1 itojun #include <sys/socket.h>
34 1.1.2.1 itojun #include <sys/sockio.h>
35 1.1.2.1 itojun
36 1.1.2.1 itojun #include <net/if.h>
37 1.1.2.1 itojun #include <net/if_dl.h>
38 1.1.2.1 itojun #include <net/if_types.h>
39 1.1.2.1 itojun #include <net/route.h>
40 1.1.2.1 itojun
41 1.1.2.1 itojun #include <netinet/in.h>
42 1.1.2.1 itojun #include <netinet/in_var.h>
43 1.1.2.1 itojun #ifndef __NetBSD__
44 1.1.2.1 itojun #include <netinet/if_ether.h>
45 1.1.2.1 itojun #endif
46 1.1.2.1 itojun
47 1.1.2.1 itojun #include <netinet6/in6.h>
48 1.1.2.1 itojun #include <netinet6/ip6.h>
49 1.1.2.1 itojun #include <netinet6/ip6_var.h>
50 1.1.2.1 itojun #include <netinet6/in6_ifattach.h>
51 1.1.2.1 itojun #include <netinet6/ip6.h>
52 1.1.2.1 itojun #include <netinet6/ip6_var.h>
53 1.1.2.1 itojun #include <netinet6/nd6.h>
54 1.1.2.1 itojun
55 1.1.2.1 itojun static struct in6_addr llsol;
56 1.1.2.1 itojun
57 1.1.2.1 itojun struct in6_addr **in6_iflladdr = NULL;
58 1.1.2.1 itojun unsigned long in6_maxmtu = 0;
59 1.1.2.1 itojun
60 1.1.2.1 itojun int found_first_ifid = 0;
61 1.1.2.1 itojun #define IFID_LEN 8
62 1.1.2.1 itojun static char first_ifid[IFID_LEN];
63 1.1.2.1 itojun
64 1.1.2.1 itojun static void ieee802_to_eui64 __P((u_int8_t *, u_int8_t *));
65 1.1.2.1 itojun
66 1.1.2.1 itojun static void
67 1.1.2.1 itojun ieee802_to_eui64(dst, src)
68 1.1.2.1 itojun u_int8_t *dst;
69 1.1.2.1 itojun u_int8_t *src;
70 1.1.2.1 itojun {
71 1.1.2.1 itojun dst[0] = src[0];
72 1.1.2.1 itojun dst[1] = src[1];
73 1.1.2.1 itojun dst[2] = src[2];
74 1.1.2.1 itojun dst[3] = 0xff;
75 1.1.2.1 itojun dst[4] = 0xfe;
76 1.1.2.1 itojun dst[5] = src[3];
77 1.1.2.1 itojun dst[6] = src[4];
78 1.1.2.1 itojun dst[7] = src[5];
79 1.1.2.1 itojun }
80 1.1.2.1 itojun
81 1.1.2.1 itojun /*
82 1.1.2.1 itojun * find first ifid on list of interfaces.
83 1.1.2.1 itojun */
84 1.1.2.1 itojun int
85 1.1.2.1 itojun in6_ifattach_getifid(ifp0)
86 1.1.2.1 itojun struct ifnet *ifp0;
87 1.1.2.1 itojun {
88 1.1.2.1 itojun struct ifnet *ifp;
89 1.1.2.1 itojun struct ifaddr *ifa;
90 1.1.2.1 itojun u_int8_t *addr = NULL;
91 1.1.2.1 itojun int addrlen = 0;
92 1.1.2.1 itojun struct sockaddr_dl *sdl;
93 1.1.2.1 itojun
94 1.1.2.1 itojun if (found_first_ifid)
95 1.1.2.1 itojun return 0;
96 1.1.2.1 itojun
97 1.1.2.1 itojun for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) {
98 1.1.2.1 itojun if (ifp0 != NULL && ifp0 != ifp)
99 1.1.2.1 itojun continue;
100 1.1.2.1 itojun for (ifa = ifp->if_addrlist.tqh_first;
101 1.1.2.1 itojun ifa;
102 1.1.2.1 itojun ifa = ifa->ifa_list.tqe_next) {
103 1.1.2.1 itojun if (ifa->ifa_addr->sa_family != AF_LINK)
104 1.1.2.1 itojun continue;
105 1.1.2.1 itojun sdl = (struct sockaddr_dl *)ifa->ifa_addr;
106 1.1.2.1 itojun if (sdl == NULL)
107 1.1.2.1 itojun continue;
108 1.1.2.1 itojun if (sdl->sdl_alen == 0)
109 1.1.2.1 itojun continue;
110 1.1.2.1 itojun switch (ifp->if_type) {
111 1.1.2.1 itojun case IFT_ETHER:
112 1.1.2.1 itojun case IFT_FDDI:
113 1.1.2.1 itojun case IFT_ATM:
114 1.1.2.1 itojun /* what others? */
115 1.1.2.1 itojun addr = LLADDR(sdl);
116 1.1.2.1 itojun addrlen = sdl->sdl_alen;
117 1.1.2.1 itojun goto found;
118 1.1.2.1 itojun default:
119 1.1.2.1 itojun break;
120 1.1.2.1 itojun }
121 1.1.2.1 itojun }
122 1.1.2.1 itojun }
123 1.1.2.1 itojun printf("failed to get EUI64");
124 1.1.2.1 itojun return EADDRNOTAVAIL;
125 1.1.2.1 itojun
126 1.1.2.1 itojun found:
127 1.1.2.1 itojun switch (addrlen) {
128 1.1.2.1 itojun case 6:
129 1.1.2.1 itojun ieee802_to_eui64(first_ifid, addr);
130 1.1.2.1 itojun found_first_ifid = 1;
131 1.1.2.1 itojun break;
132 1.1.2.1 itojun case 8:
133 1.1.2.1 itojun bcopy(addr, first_ifid, 8);
134 1.1.2.1 itojun found_first_ifid = 1;
135 1.1.2.1 itojun break;
136 1.1.2.1 itojun default:
137 1.1.2.1 itojun break;
138 1.1.2.1 itojun }
139 1.1.2.1 itojun
140 1.1.2.1 itojun if (found_first_ifid) {
141 1.1.2.1 itojun printf("got EUI64 from %s, "
142 1.1.2.1 itojun "value %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
143 1.1.2.1 itojun ifp->if_xname,
144 1.1.2.1 itojun first_ifid[0] & 0xff, first_ifid[1] & 0xff,
145 1.1.2.1 itojun first_ifid[2] & 0xff, first_ifid[3] & 0xff,
146 1.1.2.1 itojun first_ifid[4] & 0xff, first_ifid[5] & 0xff,
147 1.1.2.1 itojun first_ifid[6] & 0xff, first_ifid[7] & 0xff);
148 1.1.2.1 itojun return 0;
149 1.1.2.1 itojun } else {
150 1.1.2.1 itojun printf("failed to get EUI64");
151 1.1.2.1 itojun return EADDRNOTAVAIL;
152 1.1.2.1 itojun }
153 1.1.2.1 itojun }
154 1.1.2.1 itojun
155 1.1.2.1 itojun /*
156 1.1.2.1 itojun * add link-local address to *pseudo* p2p interfaces.
157 1.1.2.1 itojun * get called when the first MAC address is made available in in6_ifattach().
158 1.1.2.1 itojun */
159 1.1.2.1 itojun void
160 1.1.2.1 itojun in6_ifattach_p2p()
161 1.1.2.1 itojun {
162 1.1.2.1 itojun struct ifnet *ifp;
163 1.1.2.1 itojun
164 1.1.2.1 itojun /* prevent infinite loop. just in case. */
165 1.1.2.1 itojun if (! found_first_ifid)
166 1.1.2.1 itojun return;
167 1.1.2.1 itojun
168 1.1.2.1 itojun for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next) {
169 1.1.2.1 itojun switch (ifp->if_type) {
170 1.1.2.1 itojun case IFT_DUMMY:
171 1.1.2.1 itojun case IFT_GIF:
172 1.1.2.1 itojun case IFT_FAITH:
173 1.1.2.1 itojun /* pseudo interfaces - safe to initialize here */
174 1.1.2.1 itojun in6_ifattach(ifp, IN6_IFT_P2P, 0, 0);
175 1.1.2.1 itojun break;
176 1.1.2.1 itojun case IFT_SLIP:
177 1.1.2.1 itojun /* IPv6 is not supported */
178 1.1.2.1 itojun break;
179 1.1.2.1 itojun case IFT_PPP:
180 1.1.2.1 itojun /* this is not a pseudo interface, skip it */
181 1.1.2.1 itojun break;
182 1.1.2.1 itojun default:
183 1.1.2.1 itojun break;
184 1.1.2.1 itojun }
185 1.1.2.1 itojun }
186 1.1.2.1 itojun }
187 1.1.2.1 itojun
188 1.1.2.1 itojun void
189 1.1.2.1 itojun in6_ifattach(ifp, type, laddr, noloop)
190 1.1.2.1 itojun struct ifnet *ifp;
191 1.1.2.1 itojun u_int type;
192 1.1.2.1 itojun caddr_t laddr;
193 1.1.2.1 itojun int noloop;
194 1.1.2.1 itojun /* xxx sizeof(laddr) */
195 1.1.2.1 itojun {
196 1.1.2.1 itojun static size_t if_indexlim = 8;
197 1.1.2.1 itojun struct sockaddr_in6 mltaddr;
198 1.1.2.1 itojun struct sockaddr_in6 mltmask;
199 1.1.2.1 itojun struct sockaddr_in6 gate;
200 1.1.2.1 itojun struct sockaddr_in6 mask;
201 1.1.2.1 itojun
202 1.1.2.1 itojun struct in6_ifaddr *ia, *ib, *oia;
203 1.1.2.1 itojun struct ifaddr *ifa;
204 1.1.2.1 itojun int rtflag = 0;
205 1.1.2.1 itojun
206 1.1.2.1 itojun if (type == IN6_IFT_P2P && found_first_ifid == 0) {
207 1.1.2.1 itojun printf("%s: no ifid available yet for IPv6 link-local address\n",
208 1.1.2.1 itojun ifp->if_xname);
209 1.1.2.1 itojun return;
210 1.1.2.1 itojun }
211 1.1.2.1 itojun
212 1.1.2.1 itojun if ((ifp->if_flags & IFF_MULTICAST) == 0) {
213 1.1.2.1 itojun printf("%s: no multicast allowed, IPv6 is not enabled\n",
214 1.1.2.1 itojun ifp->if_xname);
215 1.1.2.1 itojun return;
216 1.1.2.1 itojun }
217 1.1.2.1 itojun
218 1.1.2.1 itojun /*
219 1.1.2.1 itojun * We have some arrays that should be indexed by if_index.
220 1.1.2.1 itojun * since if_index will grow dynamically, they should grow too.
221 1.1.2.1 itojun * struct in6_addr **in6_iflladdr
222 1.1.2.1 itojun */
223 1.1.2.1 itojun if (in6_iflladdr == NULL || if_index >= if_indexlim) {
224 1.1.2.1 itojun size_t n;
225 1.1.2.1 itojun caddr_t q;
226 1.1.2.1 itojun
227 1.1.2.1 itojun while(if_index >= if_indexlim)
228 1.1.2.1 itojun if_indexlim <<= 1;
229 1.1.2.1 itojun
230 1.1.2.1 itojun /* grow in6_iflladdr */
231 1.1.2.1 itojun n = if_indexlim * sizeof(struct in6_addr *);
232 1.1.2.1 itojun q = (caddr_t)malloc(n, M_IFADDR, M_WAITOK);
233 1.1.2.1 itojun bzero(q, n);
234 1.1.2.1 itojun if (in6_iflladdr) {
235 1.1.2.1 itojun bcopy((caddr_t)in6_iflladdr, q, n/2);
236 1.1.2.1 itojun free((caddr_t)in6_iflladdr, M_IFADDR);
237 1.1.2.1 itojun }
238 1.1.2.1 itojun in6_iflladdr = (struct in6_addr **)q;
239 1.1.2.1 itojun }
240 1.1.2.1 itojun
241 1.1.2.1 itojun /*
242 1.1.2.1 itojun * To prevent to assign link-local address to PnP network
243 1.1.2.1 itojun * cards multiple times.
244 1.1.2.1 itojun * This is lengthy for P2P and LOOP but works.
245 1.1.2.1 itojun */
246 1.1.2.1 itojun ifa = TAILQ_FIRST(&ifp->if_addrlist);
247 1.1.2.1 itojun if (ifa != NULL) {
248 1.1.2.1 itojun for ( ; ifa; ifa = TAILQ_NEXT(ifa, ifa_list)) {
249 1.1.2.1 itojun if (ifa->ifa_addr->sa_family != AF_INET6)
250 1.1.2.1 itojun continue;
251 1.1.2.1 itojun if (IN6_IS_ADDR_LINKLOCAL(&satosin6(ifa->ifa_addr)->sin6_addr))
252 1.1.2.1 itojun return;
253 1.1.2.1 itojun }
254 1.1.2.1 itojun } else
255 1.1.2.1 itojun TAILQ_INIT(&ifp->if_addrlist);
256 1.1.2.1 itojun
257 1.1.2.1 itojun /*
258 1.1.2.1 itojun * link-local address
259 1.1.2.1 itojun */
260 1.1.2.1 itojun ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_WAITOK);
261 1.1.2.1 itojun bzero((caddr_t)ia, sizeof(*ia));
262 1.1.2.1 itojun ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
263 1.1.2.1 itojun ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
264 1.1.2.1 itojun ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask;
265 1.1.2.1 itojun ia->ia_ifp = ifp;
266 1.1.2.1 itojun TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
267 1.1.2.1 itojun /*
268 1.1.2.1 itojun * Also link into the IPv6 address chain beginning with in6_ifaddr.
269 1.1.2.1 itojun * kazu opposed it, but itojun & jinmei wanted.
270 1.1.2.1 itojun */
271 1.1.2.1 itojun if ((oia = in6_ifaddr) != NULL) {
272 1.1.2.1 itojun for (; oia->ia_next; oia = oia->ia_next)
273 1.1.2.1 itojun continue;
274 1.1.2.1 itojun oia->ia_next = ia;
275 1.1.2.1 itojun } else
276 1.1.2.1 itojun in6_ifaddr = ia;
277 1.1.2.1 itojun
278 1.1.2.1 itojun ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
279 1.1.2.1 itojun ia->ia_prefixmask.sin6_family = AF_INET6;
280 1.1.2.1 itojun ia->ia_prefixmask.sin6_addr = in6mask64;
281 1.1.2.1 itojun
282 1.1.2.1 itojun bzero(&ia->ia_addr, sizeof(struct sockaddr_in6));
283 1.1.2.1 itojun ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
284 1.1.2.1 itojun ia->ia_addr.sin6_family = AF_INET6;
285 1.1.2.1 itojun ia->ia_addr.sin6_addr.s6_addr16[0] = htons(0xfe80);
286 1.1.2.1 itojun ia->ia_addr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
287 1.1.2.1 itojun ia->ia_addr.sin6_addr.s6_addr32[1] = 0;
288 1.1.2.1 itojun
289 1.1.2.1 itojun switch (type) {
290 1.1.2.1 itojun case IN6_IFT_LOOP:
291 1.1.2.1 itojun ia->ia_addr.sin6_addr.s6_addr32[2] = 0;
292 1.1.2.1 itojun ia->ia_addr.sin6_addr.s6_addr32[3] = htonl(1);
293 1.1.2.1 itojun break;
294 1.1.2.1 itojun case IN6_IFT_802:
295 1.1.2.1 itojun ia->ia_ifa.ifa_rtrequest = nd6_rtrequest;
296 1.1.2.1 itojun ia->ia_ifa.ifa_flags |= RTF_CLONING;
297 1.1.2.1 itojun rtflag = RTF_CLONING;
298 1.1.2.1 itojun /* fall through */
299 1.1.2.1 itojun case IN6_IFT_P2P802:
300 1.1.2.1 itojun if (laddr == NULL)
301 1.1.2.1 itojun break;
302 1.1.2.1 itojun ieee802_to_eui64(&ia->ia_addr.sin6_addr.s6_addr8[8], laddr);
303 1.1.2.1 itojun /* set global bit */
304 1.1.2.1 itojun ia->ia_addr.sin6_addr.s6_addr8[8] |= 0x02;
305 1.1.2.1 itojun if (! found_first_ifid) {
306 1.1.2.1 itojun if (in6_ifattach_getifid(ifp) == 0)
307 1.1.2.1 itojun in6_ifattach_p2p();
308 1.1.2.1 itojun }
309 1.1.2.1 itojun break;
310 1.1.2.1 itojun case IN6_IFT_P2P:
311 1.1.2.1 itojun bcopy((caddr_t)first_ifid,
312 1.1.2.1 itojun (caddr_t)&ia->ia_addr.sin6_addr.s6_addr8[8],
313 1.1.2.1 itojun IFID_LEN);
314 1.1.2.1 itojun break;
315 1.1.2.1 itojun }
316 1.1.2.1 itojun
317 1.1.2.1 itojun ia->ia_ifa.ifa_metric = ifp->if_metric;
318 1.1.2.1 itojun
319 1.1.2.1 itojun if (ifp->if_ioctl != NULL) {
320 1.1.2.1 itojun int s;
321 1.1.2.1 itojun int error;
322 1.1.2.1 itojun
323 1.1.2.1 itojun /*
324 1.1.2.1 itojun * give the interface a chance to initialize, in case this
325 1.1.2.1 itojun * is the first address to be added.
326 1.1.2.1 itojun */
327 1.1.2.1 itojun s = splimp();
328 1.1.2.1 itojun error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
329 1.1.2.1 itojun splx(s);
330 1.1.2.1 itojun
331 1.1.2.1 itojun if (error) {
332 1.1.2.1 itojun switch (error) {
333 1.1.2.1 itojun case EAFNOSUPPORT:
334 1.1.2.1 itojun printf("%s: IPv6 is not supported\n",
335 1.1.2.1 itojun ifp->if_xname);
336 1.1.2.1 itojun break;
337 1.1.2.1 itojun default:
338 1.1.2.1 itojun printf("SIOCSIFADDR(%s) returned %d\n",
339 1.1.2.1 itojun ifp->if_xname, error);
340 1.1.2.1 itojun break;
341 1.1.2.1 itojun }
342 1.1.2.1 itojun
343 1.1.2.1 itojun /* undo changes */
344 1.1.2.1 itojun TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
345 1.1.2.1 itojun if (oia)
346 1.1.2.1 itojun oia->ia_next = ia->ia_next;
347 1.1.2.1 itojun else
348 1.1.2.1 itojun in6_ifaddr = ia->ia_next;
349 1.1.2.1 itojun free(ia, M_IFADDR);
350 1.1.2.1 itojun return;
351 1.1.2.1 itojun }
352 1.1.2.1 itojun }
353 1.1.2.1 itojun
354 1.1.2.1 itojun /* add route to the interface. */
355 1.1.2.1 itojun rtrequest(RTM_ADD,
356 1.1.2.1 itojun (struct sockaddr *)&ia->ia_addr,
357 1.1.2.1 itojun (struct sockaddr *)&ia->ia_addr,
358 1.1.2.1 itojun (struct sockaddr *)&ia->ia_prefixmask,
359 1.1.2.1 itojun RTF_UP|rtflag,
360 1.1.2.1 itojun (struct rtentry **)0);
361 1.1.2.1 itojun ia->ia_flags |= IFA_ROUTE;
362 1.1.2.1 itojun
363 1.1.2.1 itojun
364 1.1.2.1 itojun if (type == IN6_IFT_P2P || type == IN6_IFT_P2P802) {
365 1.1.2.1 itojun /*
366 1.1.2.1 itojun * route local address to loopback
367 1.1.2.1 itojun */
368 1.1.2.1 itojun bzero(&gate, sizeof(gate));
369 1.1.2.1 itojun gate.sin6_len = sizeof(struct sockaddr_in6);
370 1.1.2.1 itojun gate.sin6_family = AF_INET6;
371 1.1.2.1 itojun gate.sin6_addr = in6addr_loopback;
372 1.1.2.1 itojun bzero(&mask, sizeof(mask));
373 1.1.2.1 itojun mask.sin6_len = sizeof(struct sockaddr_in6);
374 1.1.2.1 itojun mask.sin6_family = AF_INET6;
375 1.1.2.1 itojun mask.sin6_addr = in6mask64;
376 1.1.2.1 itojun rtrequest(RTM_ADD,
377 1.1.2.1 itojun (struct sockaddr *)&ia->ia_addr,
378 1.1.2.1 itojun (struct sockaddr *)&gate,
379 1.1.2.1 itojun (struct sockaddr *)&mask,
380 1.1.2.1 itojun RTF_UP|RTF_HOST,
381 1.1.2.1 itojun (struct rtentry **)0);
382 1.1.2.1 itojun }
383 1.1.2.1 itojun
384 1.1.2.1 itojun /*
385 1.1.2.1 itojun * loopback address
386 1.1.2.1 itojun */
387 1.1.2.1 itojun ib = (struct in6_ifaddr *)NULL;
388 1.1.2.1 itojun if (type == IN6_IFT_LOOP) {
389 1.1.2.1 itojun ib = (struct in6_ifaddr *)
390 1.1.2.1 itojun malloc(sizeof(*ib), M_IFADDR, M_WAITOK);
391 1.1.2.1 itojun bzero((caddr_t)ib, sizeof(*ib));
392 1.1.2.1 itojun ib->ia_ifa.ifa_addr = (struct sockaddr *)&ib->ia_addr;
393 1.1.2.1 itojun ib->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ib->ia_dstaddr;
394 1.1.2.1 itojun ib->ia_ifa.ifa_netmask = (struct sockaddr *)&ib->ia_prefixmask;
395 1.1.2.1 itojun ib->ia_ifp = ifp;
396 1.1.2.1 itojun
397 1.1.2.1 itojun ia->ia_next = ib;
398 1.1.2.1 itojun TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ib,
399 1.1.2.1 itojun ifa_list);
400 1.1.2.1 itojun
401 1.1.2.1 itojun ib->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6);
402 1.1.2.1 itojun ib->ia_prefixmask.sin6_family = AF_INET6;
403 1.1.2.1 itojun ib->ia_prefixmask.sin6_addr = in6mask128;
404 1.1.2.1 itojun ib->ia_addr.sin6_len = sizeof(struct sockaddr_in6);
405 1.1.2.1 itojun ib->ia_addr.sin6_family = AF_INET6;
406 1.1.2.1 itojun ib->ia_addr.sin6_addr = in6addr_loopback;
407 1.1.2.1 itojun #ifdef __bsdi__
408 1.1.2.1 itojun /*
409 1.1.2.1 itojun * It is necessary to set the loopback address to the dstaddr
410 1.1.2.1 itojun * field at least for BSDI. Without this setting, the BSDI
411 1.1.2.1 itojun * version of ifa_ifwithroute() rejects to add a route
412 1.1.2.1 itojun * to the loopback interface.
413 1.1.2.1 itojun */
414 1.1.2.1 itojun ib->ia_dstaddr.sin6_len = sizeof(struct sockaddr_in6);
415 1.1.2.1 itojun ib->ia_dstaddr.sin6_family = AF_INET6;
416 1.1.2.1 itojun ib->ia_dstaddr.sin6_addr = in6addr_loopback;
417 1.1.2.1 itojun #endif /* __bsdi__ */
418 1.1.2.1 itojun
419 1.1.2.1 itojun ib->ia_ifa.ifa_metric = ifp->if_metric;
420 1.1.2.1 itojun
421 1.1.2.1 itojun rtrequest(RTM_ADD,
422 1.1.2.1 itojun (struct sockaddr *)&ib->ia_addr,
423 1.1.2.1 itojun (struct sockaddr *)&ib->ia_addr,
424 1.1.2.1 itojun (struct sockaddr *)&ib->ia_prefixmask,
425 1.1.2.1 itojun RTF_UP|RTF_HOST,
426 1.1.2.1 itojun (struct rtentry **)0);
427 1.1.2.1 itojun
428 1.1.2.1 itojun ib->ia_flags |= IFA_ROUTE;
429 1.1.2.1 itojun }
430 1.1.2.1 itojun
431 1.1.2.1 itojun /*
432 1.1.2.1 itojun * join multicast
433 1.1.2.1 itojun */
434 1.1.2.1 itojun if (ifp->if_flags & IFF_MULTICAST) {
435 1.1.2.1 itojun int error; /* not used */
436 1.1.2.1 itojun
437 1.1.2.1 itojun #if !defined(__FreeBSD__) || __FreeBSD__ < 3
438 1.1.2.1 itojun /* Restore saved multicast addresses(if any). */
439 1.1.2.1 itojun in6_restoremkludge(ia, ifp);
440 1.1.2.1 itojun #endif
441 1.1.2.1 itojun
442 1.1.2.1 itojun bzero(&mltmask, sizeof(mltmask));
443 1.1.2.1 itojun mltmask.sin6_len = sizeof(struct sockaddr_in6);
444 1.1.2.1 itojun mltmask.sin6_family = AF_INET6;
445 1.1.2.1 itojun mltmask.sin6_addr = in6mask32;
446 1.1.2.1 itojun
447 1.1.2.1 itojun /*
448 1.1.2.1 itojun * join link-local all-nodes address
449 1.1.2.1 itojun */
450 1.1.2.1 itojun bzero(&mltaddr, sizeof(mltaddr));
451 1.1.2.1 itojun mltaddr.sin6_len = sizeof(struct sockaddr_in6);
452 1.1.2.1 itojun mltaddr.sin6_family = AF_INET6;
453 1.1.2.1 itojun mltaddr.sin6_addr = in6addr_linklocal_allnodes;
454 1.1.2.1 itojun mltaddr.sin6_addr.s6_addr16[1] = htons(ifp->if_index);
455 1.1.2.1 itojun rtrequest(RTM_ADD,
456 1.1.2.1 itojun (struct sockaddr *)&mltaddr,
457 1.1.2.1 itojun (struct sockaddr *)&ia->ia_addr,
458 1.1.2.1 itojun (struct sockaddr *)&mltmask,
459 1.1.2.1 itojun RTF_UP|RTF_CLONING, /* xxx */
460 1.1.2.1 itojun (struct rtentry **)0);
461 1.1.2.1 itojun (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
462 1.1.2.1 itojun
463 1.1.2.1 itojun if (type == IN6_IFT_LOOP) {
464 1.1.2.1 itojun /*
465 1.1.2.1 itojun * join node-local all-nodes address
466 1.1.2.1 itojun */
467 1.1.2.1 itojun mltaddr.sin6_addr = in6addr_nodelocal_allnodes;
468 1.1.2.1 itojun rtrequest(RTM_ADD,
469 1.1.2.1 itojun (struct sockaddr *)&mltaddr,
470 1.1.2.1 itojun (struct sockaddr *)&ib->ia_addr,
471 1.1.2.1 itojun (struct sockaddr *)&mltmask,
472 1.1.2.1 itojun RTF_UP,
473 1.1.2.1 itojun (struct rtentry **)0);
474 1.1.2.1 itojun (void)in6_addmulti(&mltaddr.sin6_addr, ifp, &error);
475 1.1.2.1 itojun } else {
476 1.1.2.1 itojun /*
477 1.1.2.1 itojun * join solicited multicast address
478 1.1.2.1 itojun */
479 1.1.2.1 itojun bzero(&llsol, sizeof(llsol));
480 1.1.2.1 itojun llsol.s6_addr16[0] = htons(0xff02);
481 1.1.2.1 itojun llsol.s6_addr16[1] = htons(ifp->if_index);
482 1.1.2.1 itojun llsol.s6_addr32[1] = 0;
483 1.1.2.1 itojun llsol.s6_addr32[2] = htonl(1);
484 1.1.2.1 itojun llsol.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3];
485 1.1.2.1 itojun llsol.s6_addr8[12] = 0xff;
486 1.1.2.1 itojun (void)in6_addmulti(&llsol, ifp, &error);
487 1.1.2.1 itojun }
488 1.1.2.1 itojun }
489 1.1.2.1 itojun
490 1.1.2.1 itojun /* update dynamically. */
491 1.1.2.1 itojun in6_iflladdr[ifp->if_index] = &ia->ia_addr.sin6_addr;
492 1.1.2.1 itojun if (in6_maxmtu < ifp->if_mtu)
493 1.1.2.1 itojun in6_maxmtu = ifp->if_mtu;
494 1.1.2.1 itojun
495 1.1.2.1 itojun /* initialize NDP variables */
496 1.1.2.1 itojun nd6_ifattach(ifp);
497 1.1.2.1 itojun
498 1.1.2.1 itojun /* mark the address TENTATIVE, if needed. */
499 1.1.2.1 itojun switch (ifp->if_type) {
500 1.1.2.1 itojun case IFT_ETHER:
501 1.1.2.1 itojun case IFT_FDDI:
502 1.1.2.1 itojun #if 0
503 1.1.2.1 itojun case IFT_ATM:
504 1.1.2.1 itojun case IFT_SLIP:
505 1.1.2.1 itojun case IFT_PPP:
506 1.1.2.1 itojun #endif
507 1.1.2.1 itojun ia->ia6_flags |= IN6_IFF_TENTATIVE;
508 1.1.2.1 itojun /* nd6_dad_start() will be called in in6_if_up */
509 1.1.2.1 itojun break;
510 1.1.2.1 itojun case IFT_DUMMY:
511 1.1.2.1 itojun case IFT_GIF:
512 1.1.2.1 itojun case IFT_LOOP:
513 1.1.2.1 itojun case IFT_FAITH:
514 1.1.2.1 itojun default:
515 1.1.2.1 itojun break;
516 1.1.2.1 itojun }
517 1.1.2.1 itojun
518 1.1.2.1 itojun return;
519 1.1.2.1 itojun }
520 1.1.2.1 itojun
521 1.1.2.1 itojun void
522 1.1.2.1 itojun in6_ifdetach(ifp)
523 1.1.2.1 itojun struct ifnet *ifp;
524 1.1.2.1 itojun {
525 1.1.2.1 itojun struct in6_ifaddr *ia, *oia;
526 1.1.2.1 itojun struct ifaddr *ifa;
527 1.1.2.1 itojun struct rtentry *rt;
528 1.1.2.1 itojun short rtflags;
529 1.1.2.1 itojun
530 1.1.2.1 itojun for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next) {
531 1.1.2.1 itojun if (ifa->ifa_addr->sa_family != AF_INET6
532 1.1.2.1 itojun || !IN6_IS_ADDR_LINKLOCAL(&satosin6(&ifa->ifa_addr)->sin6_addr)) {
533 1.1.2.1 itojun continue;
534 1.1.2.1 itojun }
535 1.1.2.1 itojun
536 1.1.2.1 itojun ia = (struct in6_ifaddr *)ifa;
537 1.1.2.1 itojun
538 1.1.2.1 itojun /* remove from the routing table */
539 1.1.2.1 itojun if ((ia->ia_flags & IFA_ROUTE)
540 1.1.2.1 itojun && (rt = rtalloc1((struct sockaddr *)&ia->ia_addr, 0))) {
541 1.1.2.1 itojun rtflags = rt->rt_flags;
542 1.1.2.1 itojun rtfree(rt);
543 1.1.2.1 itojun rtrequest(RTM_DELETE,
544 1.1.2.1 itojun (struct sockaddr *)&ia->ia_addr,
545 1.1.2.1 itojun (struct sockaddr *)&ia->ia_addr,
546 1.1.2.1 itojun (struct sockaddr *)&ia->ia_prefixmask,
547 1.1.2.1 itojun rtflags, (struct rtentry **)0);
548 1.1.2.1 itojun }
549 1.1.2.1 itojun
550 1.1.2.1 itojun /* remove from the linked list */
551 1.1.2.1 itojun TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
552 1.1.2.1 itojun
553 1.1.2.1 itojun /* also remove from the IPv6 address chain(itojun&jinmei) */
554 1.1.2.1 itojun oia = ia;
555 1.1.2.1 itojun if (oia == (ia = in6_ifaddr))
556 1.1.2.1 itojun in6_ifaddr = ia->ia_next;
557 1.1.2.1 itojun else {
558 1.1.2.1 itojun while (ia->ia_next && (ia->ia_next != oia))
559 1.1.2.1 itojun ia = ia->ia_next;
560 1.1.2.1 itojun if (ia->ia_next)
561 1.1.2.1 itojun ia->ia_next = oia->ia_next;
562 1.1.2.1 itojun else
563 1.1.2.1 itojun printf("Didn't unlink in6ifaddr from list\n");
564 1.1.2.1 itojun }
565 1.1.2.1 itojun
566 1.1.2.1 itojun free(ia, M_IFADDR);
567 1.1.2.1 itojun }
568 1.1.2.1 itojun }
569