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