1 | /*
|
---|
2 | * TINET (TCP/IP Protocol Stack)
|
---|
3 | *
|
---|
4 | * Copyright (C) 2001-2009 by Dep. of Computer Science and Engineering
|
---|
5 | * Tomakomai National College of Technology, JAPAN
|
---|
6 | *
|
---|
7 | * ä¸è¨èä½æ¨©è
|
---|
8 | ã¯ï¼ä»¥ä¸ã® (1)ï½(4) ã®æ¡ä»¶ãï¼Free Software Foundation
|
---|
9 | * ã«ãã£ã¦å
|
---|
10 | ¬è¡¨ããã¦ãã GNU General Public License ã® Version 2 ã«è¨
|
---|
11 | * è¿°ããã¦ããæ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§ã¢ï¼æ¬ã½ããã¦ã§ã¢
|
---|
12 | * ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹å¤ã»åé
|
---|
13 | å¸ï¼ä»¥ä¸ï¼
|
---|
14 | * å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
15 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
16 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
17 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
18 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
19 | * ç¨ã§ããå½¢ã§åé
|
---|
20 | å¸ããå ´åã«ã¯ï¼åé
|
---|
21 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
22 | * è
|
---|
23 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®èä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
24 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
25 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
26 | * ç¨ã§ããªãå½¢ã§åé
|
---|
27 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®æ¡ä»¶ãæºãããã¨ï¼
|
---|
28 | * (a) åé
|
---|
29 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
30 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
31 | * ä½æ¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
32 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
33 | * 害ãããï¼ä¸è¨èä½æ¨©è
|
---|
34 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
35 | 責ãããã¨ï¼
|
---|
36 | *
|
---|
37 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨èä½æ¨©è
|
---|
38 | ã
|
---|
39 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ãã®é©ç¨å¯è½æ§ã
|
---|
40 | * å«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´
|
---|
41 | * æ¥çã¾ãã¯éæ¥çã«çãããããªãæ害ã«é¢ãã¦ãï¼ãã®è²¬ä»»ãè² ããªãï¼
|
---|
42 | *
|
---|
43 | * @(#) $Id: in6_subr.c 270 2017-02-09 04:03:47Z coas-nagasima $
|
---|
44 | */
|
---|
45 |
|
---|
46 | /*
|
---|
47 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
|
---|
48 | * All rights reserved.
|
---|
49 | *
|
---|
50 | * Redistribution and use in source and binary forms, with or without
|
---|
51 | * modification, are permitted provided that the following conditions
|
---|
52 | * are met:
|
---|
53 | * 1. Redistributions of source code must retain the above copyright
|
---|
54 | * notice, this list of conditions and the following disclaimer.
|
---|
55 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
56 | * notice, this list of conditions and the following disclaimer in the
|
---|
57 | * documentation and/or other materials provided with the distribution.
|
---|
58 | * 3. Neither the name of the project nor the names of its contributors
|
---|
59 | * may be used to endorse or promote products derived from this software
|
---|
60 | * without specific prior written permission.
|
---|
61 | *
|
---|
62 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
---|
63 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
64 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
65 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
---|
66 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
67 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
68 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
69 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
70 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
71 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
72 | * SUCH DAMAGE.
|
---|
73 | */
|
---|
74 |
|
---|
75 | /*
|
---|
76 | * Copyright (c) 1985, 1986, 1993
|
---|
77 | * The Regents of the University of California. All rights reserved.
|
---|
78 | *
|
---|
79 | * Redistribution and use in source and binary forms, with or without
|
---|
80 | * modification, are permitted provided that the following conditions
|
---|
81 | * are met:
|
---|
82 | * 1. Redistributions of source code must retain the above copyright
|
---|
83 | * notice, this list of conditions and the following disclaimer.
|
---|
84 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
85 | * notice, this list of conditions and the following disclaimer in the
|
---|
86 | * documentation and/or other materials provided with the distribution.
|
---|
87 | * 3. All advertising materials mentioning features or use of this software
|
---|
88 | * must display the following acknowledgement:
|
---|
89 | * This product includes software developed by the University of
|
---|
90 | * California, Berkeley and its contributors.
|
---|
91 | * 4. Neither the name of the University nor the names of its contributors
|
---|
92 | * may be used to endorse or promote products derived from this software
|
---|
93 | * without specific prior written permission.
|
---|
94 | *
|
---|
95 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
96 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
97 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
98 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
99 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
100 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
101 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
102 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
103 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
104 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
105 | * SUCH DAMAGE.
|
---|
106 | */
|
---|
107 |
|
---|
108 | #include <string.h>
|
---|
109 |
|
---|
110 | #ifdef TARGET_KERNEL_ASP
|
---|
111 |
|
---|
112 | #include <kernel.h>
|
---|
113 | #include <sil.h>
|
---|
114 | #include "kernel_cfg.h"
|
---|
115 |
|
---|
116 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
117 |
|
---|
118 | #ifdef TARGET_KERNEL_JSP
|
---|
119 |
|
---|
120 | #include <s_services.h>
|
---|
121 | #include <t_services.h>
|
---|
122 | #include "kernel_id.h"
|
---|
123 | #include "tinet_id.h"
|
---|
124 |
|
---|
125 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
126 |
|
---|
127 | #include <tinet_defs.h>
|
---|
128 | #include <tinet_config.h>
|
---|
129 |
|
---|
130 | #include <net/if.h>
|
---|
131 | #include <net/if_loop.h>
|
---|
132 | #include <net/if_ppp.h>
|
---|
133 | #include <net/ethernet.h>
|
---|
134 | #include <net/if_arp.h>
|
---|
135 | #include <net/net.h>
|
---|
136 | #include <net/net_var.h>
|
---|
137 | #include <net/net_buf.h>
|
---|
138 | #include <net/net_timer.h>
|
---|
139 |
|
---|
140 | #include <netinet/in.h>
|
---|
141 | #include <netinet/in_var.h>
|
---|
142 |
|
---|
143 | #include <netinet6/in6.h>
|
---|
144 | #include <netinet6/in6_var.h>
|
---|
145 | #include <netinet/ip6.h>
|
---|
146 | #include <netinet6/ip6_var.h>
|
---|
147 | #include <netinet6/ah.h>
|
---|
148 | #include <netinet6/nd6.h>
|
---|
149 |
|
---|
150 | #include <net/if6_var.h>
|
---|
151 |
|
---|
152 | #ifdef SUPPORT_INET6
|
---|
153 |
|
---|
154 | #if NUM_IN6_HOSTCACHE_ENTRY > 0
|
---|
155 |
|
---|
156 | /*
|
---|
157 | * ãã¹ããã£ãã·ã¥
|
---|
158 | */
|
---|
159 |
|
---|
160 | static T_IN6_HOSTCACHE_ENTRY in6_hostcache[NUM_IN6_HOSTCACHE_ENTRY];
|
---|
161 |
|
---|
162 | #endif /* of #if NUM_IN6_HOSTCACHE_ENTRY > 0 */
|
---|
163 |
|
---|
164 | /*
|
---|
165 | * in6_lookup_ifaddr -- ãããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹ã«å²ãå½ã¦ããã¦ããã¢ãã¬ã¹ãæ¢ç´¢ããã
|
---|
166 | */
|
---|
167 |
|
---|
168 | T_IN6_IFADDR *
|
---|
169 | in6_lookup_ifaddr (T_IFNET *ifp, T_IN6_ADDR *addr)
|
---|
170 | {
|
---|
171 | int_t ix;
|
---|
172 |
|
---|
173 | for (ix = NUM_IN6_IFADDR_ENTRY; ix -- > 0; ) {
|
---|
174 | if ((ifp->in_ifaddrs[ix].flags & IN6_IFF_DEFINED) &&
|
---|
175 | IN6_ARE_ADDR_EQUAL(addr, &ifp->in_ifaddrs[ix].addr))
|
---|
176 | return &ifp->in_ifaddrs[ix];
|
---|
177 | }
|
---|
178 | return NULL;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /*
|
---|
182 | * in6_lookup_multi -- ãããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹ã®ãã«ããã£ã¹ãã¢ãã¬ã¹ãæ¤ç´¢ããã
|
---|
183 | */
|
---|
184 |
|
---|
185 | bool_t
|
---|
186 | in6_lookup_multi (T_IFNET *ifp, T_IN6_ADDR *maddr)
|
---|
187 | {
|
---|
188 | int_t ix;
|
---|
189 |
|
---|
190 | for (ix = MAX_IN6_MADDR_CNT; ix -- > 0; )
|
---|
191 | if (IN6_ARE_ADDR_EQUAL(maddr, &ifp->in_maddrs[ix]))
|
---|
192 | return true;
|
---|
193 | return false;
|
---|
194 | }
|
---|
195 |
|
---|
196 | /*
|
---|
197 | * in6_set_header -- IPv6 ããããè¨å®ããã
|
---|
198 | */
|
---|
199 |
|
---|
200 | ER
|
---|
201 | in6_set_header (T_NET_BUF *nbuf, uint_t len,
|
---|
202 | T_IN6_ADDR *dstaddr, T_IN6_ADDR *srcaddr,
|
---|
203 | uint8_t next, uint8_t hlim)
|
---|
204 | {
|
---|
205 | T_IFNET *ifp = IF_GET_IFNET();
|
---|
206 | T_IP6_HDR *ip6h;
|
---|
207 | T_IN6_IFADDR *ia;
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * å®å
|
---|
211 | ã¢ãã¬ã¹ã«ãµããããéä¿¡å
|
---|
212 | ã¢ãã¬ã¹ãã
|
---|
213 | * ãããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹ããæ¢ç´¢ãã¦å©ç¨ããã
|
---|
214 | */
|
---|
215 | if (srcaddr == NULL || !IN6_IS_ADDR_UNSPECIFIED(srcaddr))
|
---|
216 | ;
|
---|
217 | else if ((ia = in6_ifawithifp(ifp, dstaddr)) == NULL)
|
---|
218 | return E_SYS;
|
---|
219 | else
|
---|
220 | srcaddr = &ia->addr;
|
---|
221 |
|
---|
222 | /* IPv6 ããããè¨å®ããã*/
|
---|
223 | ip6h = GET_IP6_HDR(nbuf);
|
---|
224 | ip6h->vcf = htonl(IP6_MAKE_VCF(IPV6_VERSION, 0));
|
---|
225 | ip6h->plen = htons(len);
|
---|
226 | ip6h->next = next;
|
---|
227 | ip6h->hlim = hlim;
|
---|
228 |
|
---|
229 | if (dstaddr == NULL)
|
---|
230 | memset(&ip6h->dst, 0, sizeof(T_IN6_ADDR));
|
---|
231 | else
|
---|
232 | ip6h->dst = *dstaddr;
|
---|
233 |
|
---|
234 | if (srcaddr == NULL)
|
---|
235 | memset(&ip6h->src, 0, sizeof(T_IN6_ADDR));
|
---|
236 | else
|
---|
237 | ip6h->src = *srcaddr;
|
---|
238 |
|
---|
239 | return E_OK;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /*
|
---|
243 | * in6_get_datagram -- IPv6 ãã¼ã¿ã°ã©ã ãç²å¾ããããããè¨å®ããã
|
---|
244 | */
|
---|
245 |
|
---|
246 | ER
|
---|
247 | in6_get_datagram (T_NET_BUF **nbuf, uint_t len, uint_t maxlen,
|
---|
248 | T_IN6_ADDR *dstaddr, T_IN6_ADDR *srcaddr,
|
---|
249 | uint8_t next, uint8_t hlim, ATR nbatr, TMO tmout)
|
---|
250 | {
|
---|
251 | ER error;
|
---|
252 | uint_t align;
|
---|
253 |
|
---|
254 | /* ãã¼ã¿é·ã¯ 4 ãªã¯ãããå¢çã«èª¿æ´ããã */
|
---|
255 | align = (len + 3) >> 2 << 2;
|
---|
256 |
|
---|
257 | /* ãããã¯ã¼ã¯ãããã¡ãç²å¾ããã */
|
---|
258 | if ((error = tget_net_buf_ex(nbuf, IF_IP6_HDR_SIZE + align,
|
---|
259 | IF_IP6_HDR_SIZE + maxlen, nbatr, tmout)) != E_OK)
|
---|
260 | return error;
|
---|
261 |
|
---|
262 | /*
|
---|
263 | * ãã大ããªãµã¤ãºã®ãããã¯ã¼ã¯ãããã¡ãç²å¾ããå ´åã®ã¿é·ãã調æ´ããã
|
---|
264 | * ããå°ããªãµã¤ãºã®ãããã¯ã¼ã¯ãããã¡ã®ç²å¾ã¯ãéä¿¡ã¦ã£ã³ããããã¡ã®
|
---|
265 | * çã³ãã¼æ©è½ã§ä½¿ç¨ãããå®éã«éä¿¡ããã¾ã§ããã¼ã¿ãµã¤ãºã¯æ±ºå®ã§ããªãã
|
---|
266 | */
|
---|
267 | if ((nbatr & NBA_SEARCH_ASCENT) != 0)
|
---|
268 | (*nbuf)->len = IF_IP6_HDR_SIZE + len;
|
---|
269 |
|
---|
270 | /* IPv6 ããããè¨å®ããã*/
|
---|
271 | if ((error = in6_set_header(*nbuf, len, dstaddr, srcaddr, next, hlim)) != E_OK)
|
---|
272 | return error;
|
---|
273 |
|
---|
274 | /* 4 ãªã¯ãããå¢çã¾ã§ããã£ã³ã°ã§åããã*/
|
---|
275 | if (align > len)
|
---|
276 | memset(GET_IP6_SDU(*nbuf) + len, 0, align - len);
|
---|
277 |
|
---|
278 | return E_OK;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /*
|
---|
282 | * in6_get_maxnum_ifaddr -- ã¤ã³ã¿ãã§ã¼ã¹ã«è¨å®å¯è½ãªæ大ã¢ãã¬ã¹æ°ãè¿ãã
|
---|
283 | */
|
---|
284 |
|
---|
285 | ER_UINT
|
---|
286 | in6_get_maxnum_ifaddr (void)
|
---|
287 | {
|
---|
288 | return NUM_IN6_IFADDR_ENTRY;
|
---|
289 | }
|
---|
290 |
|
---|
291 | /*
|
---|
292 | * in6_get_ifaddr -- ã¤ã³ã¿ãã§ã¼ã¹ã«è¨å®ããã¦ããã¢ãã¬ã¹ãè¿ãã
|
---|
293 | */
|
---|
294 |
|
---|
295 | const T_IN6_ADDR *
|
---|
296 | in6_get_ifaddr (int_t index)
|
---|
297 | {
|
---|
298 | T_IFNET *ifp = IF_GET_IFNET();
|
---|
299 |
|
---|
300 | if (index < NUM_IN6_IFADDR_ENTRY &&
|
---|
301 | (ifp->in_ifaddrs[index].flags & IN6_IFF_DEFINED))
|
---|
302 | return &ifp->in_ifaddrs[index].addr;
|
---|
303 | else
|
---|
304 | return NULL;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /*
|
---|
308 | * ipv62str -- IPv6 ã¢ãã¬ã¹ãæååã«å¤æããã
|
---|
309 | */
|
---|
310 |
|
---|
311 | char *
|
---|
312 | ipv62str (char *buf, const T_IN6_ADDR *p_ip6addr)
|
---|
313 | {
|
---|
314 | static char addr_sbuf[NUM_IPV6ADDR_STR_BUFF][sizeof("0123:4567:89ab:cdef:0123:4567:89ab:cdef")];
|
---|
315 | static int_t bix = NUM_IPV6ADDR_STR_BUFF;
|
---|
316 |
|
---|
317 | bool_t omit = false, zero = false;
|
---|
318 | char *start;
|
---|
319 | int_t ix;
|
---|
320 |
|
---|
321 | if (buf == NULL) {
|
---|
322 | syscall(wai_sem(SEM_IP2STR_BUFF_LOCK));
|
---|
323 | buf = addr_sbuf[-- bix];
|
---|
324 | if (bix <= 0)
|
---|
325 | bix = NUM_IPV6ADDR_STR_BUFF;
|
---|
326 | syscall(sig_sem(SEM_IP2STR_BUFF_LOCK));
|
---|
327 | }
|
---|
328 |
|
---|
329 | start = buf;
|
---|
330 | if (p_ip6addr == NULL) {
|
---|
331 | *buf ++ = ':';
|
---|
332 | *buf ++ = ':';
|
---|
333 | }
|
---|
334 | else {
|
---|
335 | for (ix = 0; ix < sizeof(T_IN6_ADDR) / 2; ix ++) {
|
---|
336 | if (omit) {
|
---|
337 | buf += convert_hexdigit(buf, ntohs(p_ip6addr->s6_addr16[ix]), 16, 0, ' ');
|
---|
338 | if (ix < 7)
|
---|
339 | *buf ++ = ':';
|
---|
340 | }
|
---|
341 | else if (ix > 0 && ix < 7 && p_ip6addr->s6_addr16[ix] == 0)
|
---|
342 | zero = true;
|
---|
343 | else {
|
---|
344 | if (zero) {
|
---|
345 | omit = true;
|
---|
346 | *buf ++ = ':';
|
---|
347 | }
|
---|
348 | buf += convert_hexdigit(buf, ntohs(p_ip6addr->s6_addr16[ix]), 16, 0, ' ');
|
---|
349 | if (ix < 7)
|
---|
350 | *buf ++ = ':';
|
---|
351 | }
|
---|
352 | }
|
---|
353 | }
|
---|
354 | *buf = '\0';
|
---|
355 | return start;
|
---|
356 | }
|
---|
357 |
|
---|
358 | /*
|
---|
359 | * in6_cksum -- IPv6 ã®ãã©ã³ã¹ãã¼ã層ãããã®ãã§ãã¯ãµã ãè¨ç®ããã
|
---|
360 | *
|
---|
361 | * 注æ: æ»ãå¤ã¯ãããã¯ã¼ã¯ãã¤ããªã¼ã
|
---|
362 | */
|
---|
363 |
|
---|
364 | uint16_t
|
---|
365 | in6_cksum (T_NET_BUF *nbuf, uint8_t proto, uint_t off, uint_t len)
|
---|
366 | {
|
---|
367 | uint32_t sum;
|
---|
368 | uint_t align;
|
---|
369 |
|
---|
370 | /* 4 ãªã¯ãããå¢çã®ãã¼ã¿é· */
|
---|
371 | align = (len + 3) >> 2 << 2;
|
---|
372 |
|
---|
373 | /* 4 ãªã¯ãããå¢çã¾ã§ããã£ã³ã°ã§åããã*/
|
---|
374 | if (align > len)
|
---|
375 | memset((uint8_t*)nbuf->buf + off + len, 0, align - len);
|
---|
376 |
|
---|
377 | sum = in_cksum_sum(nbuf->buf + off, align)
|
---|
378 | + in_cksum_sum(&GET_IP6_HDR(nbuf)->src, sizeof(T_IN6_ADDR) * 2)
|
---|
379 | + len + proto;
|
---|
380 | sum = in_cksum_carry(sum);
|
---|
381 |
|
---|
382 | return ~htons((uint16_t)sum);
|
---|
383 | }
|
---|
384 |
|
---|
385 | /*
|
---|
386 | * in6_is_dstaddr_accept -- å®å
|
---|
387 | ã¢ãã¬ã¹ã¨ãã¦æ£ããããã§ãã¯ããã
|
---|
388 | */
|
---|
389 |
|
---|
390 | bool_t
|
---|
391 | in6_is_dstaddr_accept (T_IN6_ADDR *myaddr, T_IN6_ADDR *dstaddr)
|
---|
392 | {
|
---|
393 | if (IN6_IS_ADDR_UNSPECIFIED(myaddr))
|
---|
394 | return in6_lookup_ifaddr(IF_GET_IFNET(), dstaddr) != NULL;
|
---|
395 | else
|
---|
396 | return IN6_ARE_ADDR_EQUAL(dstaddr, myaddr);
|
---|
397 | }
|
---|
398 |
|
---|
399 | /*
|
---|
400 | * get_ip6_hdr_size -- æ¡å¼µããããå«ãã IPv6 ãããé·ãè¿ãã
|
---|
401 | */
|
---|
402 |
|
---|
403 | uint_t
|
---|
404 | get_ip6_hdr_size (T_IP6_HDR *iph)
|
---|
405 | {
|
---|
406 | uint_t size = IP6_HDR_SIZE, hsize;
|
---|
407 | uint8_t curr = iph->next, next;
|
---|
408 | uint8_t *hdr = ((uint8_t *)iph) + IP6_HDR_SIZE;
|
---|
409 |
|
---|
410 | while (1) {
|
---|
411 | next = *hdr;
|
---|
412 | if (curr ==IPPROTO_NONE) /* 次ãããç¡ã */
|
---|
413 | break;
|
---|
414 | else if (curr == IPPROTO_FRAGMENT) /* æçå */
|
---|
415 | hsize = sizeof(T_IP6_FRAG_HDR);
|
---|
416 | else if (curr == IPPROTO_AH) /* IPv6 èªè¨¼ */
|
---|
417 | hsize = (((T_IP6_AH_HDR *)hdr)->len + 2) * 4;
|
---|
418 | else if (curr == IPPROTO_HOPOPTS || /* ä¸ç¶ç¹ (Hop-by-Hop) ãªãã·ã§ã³ */
|
---|
419 | curr == IPPROTO_DSTOPTS || /* IPv6 çµç¹ãªãã·ã§ã³ */
|
---|
420 | curr == IPPROTO_ROUTING) /* çµè·¯å¶å¾¡ */
|
---|
421 | hsize = (((T_IP6_EXT_HDR *)hdr)->len + 1) * 8;
|
---|
422 | else
|
---|
423 | break;
|
---|
424 | size += hsize;
|
---|
425 | hdr += hsize;
|
---|
426 | curr = next;
|
---|
427 | }
|
---|
428 | return size;
|
---|
429 | }
|
---|
430 |
|
---|
431 | /*
|
---|
432 | * in6_plen2pmask -- ãã¬ãã£ãã¯ã¹é·ãããã¹ã¯ãçæããã
|
---|
433 | */
|
---|
434 |
|
---|
435 | void
|
---|
436 | in6_plen2pmask (T_IN6_ADDR *mask, uint_t prefix_len)
|
---|
437 | {
|
---|
438 | uint8_t *ptr, bit;
|
---|
439 |
|
---|
440 | memset(mask->s6_addr + prefix_len / 8, 0, (128 - prefix_len + 7) / 8);
|
---|
441 | for (ptr = mask->s6_addr; prefix_len > 0; ptr ++) {
|
---|
442 | for (bit = 0x80; bit && (prefix_len > 0); prefix_len --) {
|
---|
443 | *ptr |= bit;
|
---|
444 | bit >>= 1;
|
---|
445 | }
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|
449 | /*
|
---|
450 | * in6_rtalloc -- ã«ã¼ãã£ã³ã°è¡¨ãæ¢ç´¢ããã
|
---|
451 | */
|
---|
452 |
|
---|
453 | T_IN6_ADDR *
|
---|
454 | in6_rtalloc (T_IFNET *ifp, T_IN6_ADDR *dst)
|
---|
455 | {
|
---|
456 | if (IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst))
|
---|
457 | return dst;
|
---|
458 | else {
|
---|
459 | T_IN6_IFADDR *ia;
|
---|
460 | SYSTIM now;
|
---|
461 | int_t ix;
|
---|
462 |
|
---|
463 | /*
|
---|
464 | * ãµã¤ããã¼ã«ã«ã¢ãã¬ã¹ãéç´å¯è½ï¼ã°ãã¼ãã«ï¼ã¢ãã¬ã¹ã®å ´åã¯ã
|
---|
465 | * åä¸ãªã³ã¯å
|
---|
466 | ã®ãã¼ãã§ãããããã§ãã¯ããã
|
---|
467 | */
|
---|
468 |
|
---|
469 | syscall(get_tim(&now));
|
---|
470 | now /= SYSTIM_HZ;
|
---|
471 |
|
---|
472 | /* æ¨å¥¨æå¹æéå
|
---|
473 | ã®ã¢ãã¬ã¹ãæ¢ç´¢ããã*/
|
---|
474 | for (ix = NUM_IN6_IFADDR_ENTRY; ix -- > 0; ) {
|
---|
475 | ia = &ifp->in_ifaddrs[ix];
|
---|
476 | if (IFA6_IS_READY(ia) &&
|
---|
477 | in6_are_prefix_equal(dst, &ia->addr, ia->prefix_len) &&
|
---|
478 | (int32_t)(ia->lifetime.preferred - now) > 0) {
|
---|
479 | /* ia->lifetime.preferred > now */
|
---|
480 | return dst;
|
---|
481 | }
|
---|
482 | }
|
---|
483 |
|
---|
484 | /* æå¹æéå
|
---|
485 | ã®ã¢ãã¬ã¹ãæ¢ç´¢ããã*/
|
---|
486 | for (ix = NUM_IN6_IFADDR_ENTRY; ix -- > 0; ) {
|
---|
487 | ia = &ifp->in_ifaddrs[ix];
|
---|
488 | if (IFA6_IS_READY(ia) &&
|
---|
489 | in6_are_prefix_equal(dst, &ia->addr, ia->prefix_len) &&
|
---|
490 | (int32_t)(ia->lifetime.expire - now) > 0)
|
---|
491 | /* ia->lifetime.expire > now */
|
---|
492 | return dst;
|
---|
493 | }
|
---|
494 |
|
---|
495 | /*
|
---|
496 | * ãã¬ãã£ãã¯ã¹ãæ¢ç´¢ããã
|
---|
497 | */
|
---|
498 | if (nd6_onlink_prefix_lookup (dst) != NULL)
|
---|
499 | return dst;
|
---|
500 |
|
---|
501 | /*
|
---|
502 | * éççµè·¯è¡¨ãæ¢ç´¢ããã
|
---|
503 | */
|
---|
504 |
|
---|
505 | #if NUM_ROUTE_ENTRY > 0
|
---|
506 |
|
---|
507 | syscall(wai_sem(SEM_IN_ROUTING_TBL));
|
---|
508 | for (ix = NUM_ROUTE_ENTRY; ix --; ) {
|
---|
509 | if ((routing_tbl[ix].flags & IN_RTF_DEFINED) &&
|
---|
510 | in6_are_prefix_equal(dst, &routing_tbl[ix].target,
|
---|
511 | routing_tbl[ix].prefix_len)) {
|
---|
512 |
|
---|
513 | /*
|
---|
514 | * åãç´ãã«ããã¨ã³ããªã¯ãæå¹æéãåããæå»ã延é·ããã
|
---|
515 | * expire ã®åä½ã¯ [s]ã
|
---|
516 | * TMO_IN_REDIRECT ã®åä½ã¯ [ms]ã
|
---|
517 | */
|
---|
518 | if (ix > NUM_STATIC_ROUTE_ENTRY) {
|
---|
519 | SYSTIM now;
|
---|
520 |
|
---|
521 | syscall(get_tim(&now));
|
---|
522 | routing_tbl[ix].expire = now / SYSTIM_HZ + TMO_IN_REDIRECT / 1000;
|
---|
523 | }
|
---|
524 |
|
---|
525 | syscall(sig_sem(SEM_IN_ROUTING_TBL));
|
---|
526 | return &routing_tbl[ix].gateway;
|
---|
527 | }
|
---|
528 | }
|
---|
529 | syscall(sig_sem(SEM_IN_ROUTING_TBL));
|
---|
530 |
|
---|
531 | #endif /* of #if NUM_ROUTE_ENTRY > 0 */
|
---|
532 |
|
---|
533 | /*
|
---|
534 | * ãã£ãã©ã«ãã«ã¼ã¿ã»ãªã¹ããæ¢ç´¢ããã
|
---|
535 | */
|
---|
536 | return nd6_router_lookup();
|
---|
537 | }
|
---|
538 | }
|
---|
539 |
|
---|
540 | #if NUM_REDIRECT_ROUTE_ENTRY > 0
|
---|
541 |
|
---|
542 | /*
|
---|
543 | * in6_gateway_lookup -- ã«ã¼ãã£ã³ã°è¡¨ã®ã«ã¼ã¿ãæ¢ç´¢ããã
|
---|
544 | */
|
---|
545 |
|
---|
546 | T_IN_RTENTRY *
|
---|
547 | in6_gateway_lookup (T_IN6_ADDR *gw)
|
---|
548 | {
|
---|
549 | int_t ix;
|
---|
550 |
|
---|
551 | for (ix = NUM_ROUTE_ENTRY; ix --; )
|
---|
552 | if ((routing_tbl[ix].flags & IN_RTF_DEFINED) &&
|
---|
553 | IN6_ARE_ADDR_EQUAL(&routing_tbl[ix].gateway, gw))
|
---|
554 | return &routing_tbl[ix];
|
---|
555 | return NULL;
|
---|
556 | }
|
---|
557 |
|
---|
558 | /*
|
---|
559 | * in6_rtredirect -- ã«ã¼ãã£ã³ã°è¡¨ã«ã¨ã³ããªãç»é²ããã
|
---|
560 | *
|
---|
561 | * 注æ: å¼æ° tmo ã®åä½ã¯ [ms]ã
|
---|
562 | */
|
---|
563 |
|
---|
564 | void
|
---|
565 | in6_rtredirect (T_IN6_ADDR *gateway, T_IN6_ADDR *target, uint_t prefix_len, uint8_t flags, uint32_t tmo)
|
---|
566 | {
|
---|
567 | T_IN_RTENTRY *frt;
|
---|
568 |
|
---|
569 | frt = in_rtnewentry(flags, tmo);
|
---|
570 | frt->gateway = *gateway;
|
---|
571 | frt->target = *target;
|
---|
572 | frt->prefix_len = prefix_len;
|
---|
573 | }
|
---|
574 |
|
---|
575 | #endif /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
|
---|
576 |
|
---|
577 | #if NUM_IN6_HOSTCACHE_ENTRY > 0
|
---|
578 |
|
---|
579 | /*
|
---|
580 | * in6_hostcache_lookup -- IPv6 ãã¹ããã£ãã·ã¥ãæ¢ç´¢ããã
|
---|
581 | */
|
---|
582 |
|
---|
583 | static T_IN6_HOSTCACHE_ENTRY*
|
---|
584 | in6_hostcache_lookup (T_IN6_ADDR *dst)
|
---|
585 | {
|
---|
586 | int_t ix;
|
---|
587 |
|
---|
588 | for (ix = NUM_IN6_HOSTCACHE_ENTRY; ix -- > 0; ) {
|
---|
589 | if (IN6_ARE_ADDR_EQUAL(dst, &in6_hostcache[ix].dst))
|
---|
590 | return &in6_hostcache[ix];
|
---|
591 | }
|
---|
592 | return NULL;
|
---|
593 | }
|
---|
594 |
|
---|
595 | /*
|
---|
596 | * in6_hostcache_update -- IPv6 ãã¹ããã£ãã·ã¥ãæ´æ°ããã
|
---|
597 | */
|
---|
598 |
|
---|
599 | void
|
---|
600 | in6_hostcache_update (T_IN6_ADDR *dst, uint32_t mtu)
|
---|
601 | {
|
---|
602 | T_IN6_HOSTCACHE_ENTRY *hc;
|
---|
603 | SYSTIM now, old;
|
---|
604 | int_t ix, oix;
|
---|
605 |
|
---|
606 | syscall(get_tim(&now));
|
---|
607 |
|
---|
608 | /* æ¢ã«ç»é²ããã¦ãããæ¢ç´¢ããã*/
|
---|
609 | if ((hc = in6_hostcache_lookup (dst)) == NULL) {
|
---|
610 |
|
---|
611 | /* 空ãã®ãã¹ããã£ãã·ã¥ãæ¢ãã*/
|
---|
612 | for (ix = NUM_IN6_HOSTCACHE_ENTRY; ix -- > 0; ) {
|
---|
613 | if (IN6_IS_HOSTCACHE_FREE(&in6_hostcache[ix]))
|
---|
614 | break;
|
---|
615 | }
|
---|
616 |
|
---|
617 | if (ix < 0) {
|
---|
618 | /*
|
---|
619 | * 空ããç¡ãæã¯ãæå¹æéã®åããæéãã
|
---|
620 | * æãçããã¹ããã£ãã·ã¥ãç½®æããã
|
---|
621 | */
|
---|
622 | old = now - 1;
|
---|
623 | oix = 0;
|
---|
624 | for (ix = NUM_IN6_HOSTCACHE_ENTRY; ix -- > 0; ) {
|
---|
625 | hc = &in6_hostcache[ix];
|
---|
626 | if (IN6_IS_HOSTCACHE_BUSY(hc) && (int32_t)(hc->expire - old) < 0) {
|
---|
627 | /* hc->expire < old */
|
---|
628 | oix = ix;
|
---|
629 | old = hc->expire;
|
---|
630 | }
|
---|
631 | }
|
---|
632 | ix = oix;
|
---|
633 | }
|
---|
634 | hc = &in6_hostcache[ix];
|
---|
635 | }
|
---|
636 | else if (mtu > hc->mtu &&
|
---|
637 | (int32_t)(hc->expire - now) >= (IN6_HOSTCACHE_EXPIRE - IN6_HOSTCACHE_INCREASE)) {
|
---|
638 |
|
---|
639 | /*
|
---|
640 | * æ¢ã«ç»é²ããã¦ãã¦ãæ°ãã MTU ããç»é²ããã¦ãã MTU ãã大ããã¨ãã
|
---|
641 | * IN6_HOSTCACHE_INCREASEï¼æ¨å¥¨ 10 åï¼éã¯æ´æ°ããªãã
|
---|
642 | */
|
---|
643 | return;
|
---|
644 | }
|
---|
645 | hc->dst = *dst;
|
---|
646 | hc->expire = now + IN6_HOSTCACHE_EXPIRE;
|
---|
647 | hc->mtu = mtu;
|
---|
648 | }
|
---|
649 |
|
---|
650 | /*
|
---|
651 | * in6_hostcache_getmtu -- IPv6 ãã¹ããã£ãã·ã¥ããã MTU ãåå¾ããã
|
---|
652 | *
|
---|
653 | * æ»ãå¤ã 0 ã§ããã°ããã¹ããã£ãã·ã¥ã«ç»é²ããã¦ããªãã
|
---|
654 | */
|
---|
655 |
|
---|
656 | uint32_t
|
---|
657 | in6_hostcache_getmtu (T_IN6_ADDR *dst)
|
---|
658 | {
|
---|
659 | T_IN6_HOSTCACHE_ENTRY *hc;
|
---|
660 |
|
---|
661 | /* æ¢ã«ç»é²ããã¦ãããæ¢ç´¢ããã*/
|
---|
662 | if ((hc = in6_hostcache_lookup(dst)) == NULL)
|
---|
663 | return 0;
|
---|
664 | else
|
---|
665 | return hc->mtu;
|
---|
666 | }
|
---|
667 |
|
---|
668 | /*
|
---|
669 | * in6_hostcache_timer -- IPv6 ãã¹ããã£ãã·ã¥æ´æ°ã¿ã¤ãã¼
|
---|
670 | *
|
---|
671 | * 1ç§å¨æã§èµ·åãããã
|
---|
672 | */
|
---|
673 |
|
---|
674 | static void
|
---|
675 | in6_hostcache_timer (void)
|
---|
676 | {
|
---|
677 | static int_t interval = IN6_HOSTCACHE_PRUNE / SYSTIM_HZ;
|
---|
678 |
|
---|
679 | SYSTIM now;
|
---|
680 |
|
---|
681 | interval --;
|
---|
682 | if (interval <= 0) {
|
---|
683 | syscall(get_tim(&now));
|
---|
684 | interval = IN6_HOSTCACHE_PRUNE / SYSTIM_HZ;
|
---|
685 | }
|
---|
686 | }
|
---|
687 |
|
---|
688 | #endif /* of #if NUM_IN6_HOSTCACHE_ENTRY > 0 */
|
---|
689 |
|
---|
690 | /*
|
---|
691 | * in6_timer -- IPv6 å
|
---|
692 | ±éã¿ã¤ãã¼
|
---|
693 | *
|
---|
694 | * 1ç§å¨æã§èµ·åãããã
|
---|
695 | */
|
---|
696 |
|
---|
697 | static void
|
---|
698 | in6_timer (void)
|
---|
699 | {
|
---|
700 | #if NUM_REDIRECT_ROUTE_ENTRY > 0
|
---|
701 |
|
---|
702 | in_rttimer();
|
---|
703 |
|
---|
704 | #endif /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
|
---|
705 |
|
---|
706 | #ifdef IP6_CFG_FRAGMENT
|
---|
707 |
|
---|
708 | frag6_timer();
|
---|
709 |
|
---|
710 | #endif /* of #ifdef IP6_CFG_FRAGMENT */
|
---|
711 |
|
---|
712 | #if NUM_IN6_HOSTCACHE_ENTRY > 0
|
---|
713 |
|
---|
714 | in6_hostcache_timer();
|
---|
715 |
|
---|
716 | #endif /* of #if NUM_IN6_HOSTCACHE_ENTRY > 0 */
|
---|
717 |
|
---|
718 | timeout((callout_func)in6_timer, NULL, IN_TIMER_TMO);
|
---|
719 | }
|
---|
720 |
|
---|
721 | /*
|
---|
722 | * in6_init -- IPv6 å
|
---|
723 | ±éæ©è½ãåæåããã
|
---|
724 | */
|
---|
725 |
|
---|
726 | void
|
---|
727 | in6_init (void)
|
---|
728 | {
|
---|
729 | #if NUM_REDIRECT_ROUTE_ENTRY > 0
|
---|
730 |
|
---|
731 | in_rtinit();
|
---|
732 |
|
---|
733 | #endif /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
|
---|
734 |
|
---|
735 | timeout((callout_func)nd6_timer, NULL, ND6_TIMER_TMO);
|
---|
736 | timeout((callout_func)in6_timer, NULL, IN_TIMER_TMO);
|
---|
737 | }
|
---|
738 |
|
---|
739 | #endif /* of #ifdef SUPPORT_INET6 */
|
---|