source: EcnlProtoTool/trunk/asp3_dcre/tinet/netinet6/ip6_input.c@ 270

Last change on this file since 270 was 270, checked in by coas-nagasima, 7 years ago

mruby版ECNLプロトタイピング・ツールを追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 19.8 KB
Line 
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: ip6_input.c 270 2017-02-09 04:03:47Z coas-nagasima $
44 */
45
46/* $FreeBSD: src/sys/netinet6/ip6_input.c,v 1.11.2.10 2001/07/24 19:10:18 brooks Exp $ */
47/* $KAME: ip6_input.c,v 1.194 2001/05/27 13:28:35 itojun Exp $ */
48
49/*
50 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
51 * All rights reserved.
52 *
53 * Redistribution and use in source and binary forms, with or without
54 * modification, are permitted provided that the following conditions
55 * are met:
56 * 1. Redistributions of source code must retain the above copyright
57 * notice, this list of conditions and the following disclaimer.
58 * 2. Redistributions in binary form must reproduce the above copyright
59 * notice, this list of conditions and the following disclaimer in the
60 * documentation and/or other materials provided with the distribution.
61 * 3. Neither the name of the project nor the names of its contributors
62 * may be used to endorse or promote products derived from this software
63 * without specific prior written permission.
64 *
65 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
66 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
67 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
68 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
69 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
70 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
71 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
72 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
73 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
74 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75 * SUCH DAMAGE.
76 */
77
78/*
79 * Copyright (c) 1982, 1986, 1988, 1993
80 * The Regents of the University of California. All rights reserved.
81 *
82 * Redistribution and use in source and binary forms, with or without
83 * modification, are permitted provided that the following conditions
84 * are met:
85 * 1. Redistributions of source code must retain the above copyright
86 * notice, this list of conditions and the following disclaimer.
87 * 2. Redistributions in binary form must reproduce the above copyright
88 * notice, this list of conditions and the following disclaimer in the
89 * documentation and/or other materials provided with the distribution.
90 * 3. All advertising materials mentioning features or use of this software
91 * must display the following acknowledgement:
92 * This product includes software developed by the University of
93 * California, Berkeley and its contributors.
94 * 4. Neither the name of the University nor the names of its contributors
95 * may be used to endorse or promote products derived from this software
96 * without specific prior written permission.
97 *
98 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
99 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
102 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108 * SUCH DAMAGE.
109 *
110 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
111 */
112
113#include <string.h>
114
115#ifdef TARGET_KERNEL_ASP
116
117#include <kernel.h>
118#include <sil.h>
119
120#endif /* of #ifdef TARGET_KERNEL_ASP */
121
122#ifdef TARGET_KERNEL_JSP
123
124#include <s_services.h>
125#include <t_services.h>
126
127#endif /* of #ifdef TARGET_KERNEL_JSP */
128
129#include <tinet_defs.h>
130#include <tinet_config.h>
131
132#include <net/if.h>
133#include <net/if_loop.h>
134#include <net/if_ppp.h>
135#include <net/ethernet.h>
136#include <net/ppp_ipcp.h>
137#include <net/net.h>
138#include <net/net_buf.h>
139#include <net/net_count.h>
140
141#include <netinet/in.h>
142#include <netinet6/in6.h>
143#include <netinet6/in6_var.h>
144#include <netinet6/nd6.h>
145#include <netinet/ip6.h>
146#include <netinet6/ip6_var.h>
147#include <netinet/icmp6.h>
148#include <netinet6/ah6.h>
149#include <netinet6/esp6.h>
150#include <netinet/tcp.h>
151#include <netinet/tcp_timer.h>
152#include <netinet/tcp_var.h>
153#include <netinet/udp_var.h>
154
155#include <net/if6_var.h>
156
157#ifdef SUPPORT_INET6
158
159/*
160 * 関数
161 */
162
163static uint_t ip6_no_header_input (T_NET_BUF **inputp, uint_t *offp, uint_t *nextp);
164
165/*
166 * 上位プロトコル選択構造体
167 */
168
169typedef struct t_proto_switch {
170 uint_t (*func)(T_NET_BUF **, uint_t *, uint_t *); /* å…
171¥åŠ›é–¢æ•° */
172 uint_t proto; /* プロトコル番号 */
173 } T_PROTO_SWITCH;
174
175static const T_PROTO_SWITCH proto_switch[] = {
176 { esp6_input, IPPROTO_ESP }, /* IPv6 暗号化ヘッダ */
177 { ah6_input, IPPROTO_AH }, /* IPv6 認証ヘッダ */
178 { frag6_input, IPPROTO_FRAGMENT }, /* 断片ヘッダ */
179 { route6_input, IPPROTO_ROUTING }, /* 経路制御ヘッダ */
180 { dest6_input, IPPROTO_DSTOPTS }, /* IPv6 終点オプション */
181 { ip6_no_header_input,
182 IPPROTO_NONE }, /* 次ヘッダ無し */
183 { icmp6_input, IPPROTO_ICMPV6 },
184
185#ifdef SUPPORT_UDP
186
187 { udp_input, IPPROTO_UDP },
188
189#endif /* of #ifdef SUPPORT_UDP */
190
191#ifdef SUPPORT_TCP
192
193 { tcp_input, IPPROTO_TCP },
194
195#endif /* of #ifdef SUPPORT_TCP */
196
197 };
198
199#ifdef SUPPORT_MIB
200
201T_IN6_IFSTAT in6_ifstat;
202
203#endif /* of #ifdef SUPPORT_MIB */
204
205/*
206 * get_upper_proto -- 上位プロトコル処理関数を返す。
207 */
208
209static uint_t (*
210get_upper_proto(uint_t proto))(T_NET_BUF **, uint_t *, uint_t *)
211{
212 int_t ix;
213
214 for (ix = sizeof(proto_switch) / sizeof(T_PROTO_SWITCH); ix -- > 0; )
215 if (proto_switch[ix].proto == proto)
216 return proto_switch[ix].func;
217 return NULL;
218 }
219
220/*
221 * ip6_no_header_input -- 次ヘッダが無しの場合のå…
222¥åŠ›å‡¦ç†
223 */
224
225static uint_t
226ip6_no_header_input (T_NET_BUF **input, uint_t *offp, uint_t *nextp)
227{
228 syscall(rel_net_buf(*input));
229 return IPPROTO_DONE;
230 }
231
232/*
233 * ip6_hopopts_input -- 中継点オプションå…
234¥åŠ›å‡¦ç†
235 *
236 * 本実装
237では、パディング以外のオプションをå…
238¨ã¦æœªçŸ¥ã‚ªãƒ—ションとして処理する。
239 *
240 * 戻り値:
241 *
242 * 0 以上 正常
243 * IP6_OPT_RET_ERR (-1) エラー、呼出側でネットワークバッファを開放する。
244 * IP6_OPT_RET_REL (-2) エラー、ネットワークバッファは開放されている。
245 */
246
247static ER_UINT
248ip6_hopopts_input (T_NET_BUF *input, uint_t *offp, uint_t *nextp)
249{
250 T_IP6_HBH_HDR *hbh;
251 ER_UINT optlen;
252 uint_t len;
253 uint8_t *opt;
254
255 hbh = (T_IP6_HBH_HDR *)(input->buf + *offp);
256 len = (hbh->len + 1) << 3;
257 *nextp = *offp - IF_HDR_SIZE + offsetof(T_IP6_HBH_HDR, next);
258 *offp += len;
259 opt = (uint8_t *)hbh + sizeof(T_IP6_HBH_HDR);
260
261 for (len -= sizeof(T_IP6_HBH_HDR); len > 0; len -= optlen) {
262 switch (*opt) {
263 case IP6OPT_PAD1:
264 optlen = 1;
265 break;
266 case IP6OPT_PADN:
267 if (len < IP6OPT_MINLEN)
268 return IP6_OPT_RET_ERR;
269 optlen = *(opt + 1) + 2;
270 break;
271 case IP6OPT_JUMBO:
272 case IP6OPT_NSAP_ADDR:
273 case IP6OPT_TUNNEL_LIMIT:
274 case IP6OPT_RTR_ALERT:
275 default:
276 if (len < IP6OPT_MINLEN)
277 return IP6_OPT_RET_ERR;
278 optlen = ip6_unknown_opt(input, opt);
279 if (optlen < 0)
280 return optlen;
281 optlen += 2;
282 break;
283 }
284
285 opt += optlen;
286 }
287
288 return *offp;
289 }
290
291/*
292 * ip6_get_prev_hdr -- 直前の拡張ヘッダの next フィールドのアドレスを返す。
293 *
294 * 注意: off には IF_HDR_SIZE も含まれている。
295 */
296
297uint8_t *
298ip6_get_prev_hdr (T_NET_BUF *nbuf, uint_t off)
299{
300 T_IP6_HDR *ip6h;
301 T_IP6_EXT_HDR *ip6eh;
302 uint_t len;
303 uint8_t next;
304
305 ip6h = GET_IP6_HDR(nbuf);
306 if (off == IF_HDR_SIZE + sizeof(T_IP6_HDR))
307 return &ip6h->next;
308 else {
309 next = ip6h->next;
310 len = IF_HDR_SIZE + sizeof(T_IP6_HDR);
311 ip6eh = NULL;
312
313 while (len < off) {
314 ip6eh = (T_IP6_EXT_HDR *)(nbuf->buf + len);
315
316 switch (next) {
317 case IPPROTO_FRAGMENT:
318 len += sizeof(T_IP6_FRAG_HDR);
319 break;
320 case IPPROTO_AH:
321 len += (ip6eh->len + 2) << 2;
322 break;
323 default:
324 len += (ip6eh->len + 2) << 3;
325 break;
326 }
327 next = ip6eh->next;
328 }
329
330 if (ip6eh == NULL)
331 return NULL;
332 else
333 return &ip6eh->next;
334 }
335 }
336
337/*
338 * ip6_init -- IPv6 の初期化関数
339 */
340
341void
342ip6_init (void)
343{
344 in6_init();
345 in6_ifainit();
346 }
347
348/*
349 * ip6_input -- IPv6 のå…
350¥åŠ›é–¢æ•°
351 */
352
353void
354ip6_input (T_NET_BUF *input)
355{
356 T_IP6_HDR *ip6h;
357 T_IN6_IFADDR *ia6;
358 T_IFNET *ifp;
359 ER_UINT noff;
360 uint_t next, nest, plen, offp, nextp;
361 uint_t (*func)(T_NET_BUF **, uint_t *, uint_t *);
362
363 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_OCTETS], input->len - IF_HDR_SIZE);
364 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_PACKETS], 1);
365 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInReceives, 1);
366
367 /* IP ヘッダの長さをチェックする。*/
368 if (input->len < IF_IP6_HDR_SIZE) {
369 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_SHORT], 1);
370 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInHdrErrors, 1);
371 goto buf_rel;
372 }
373
374 ip6h = GET_IP6_HDR(input);
375
376 /* バージョンをチェックする。*/
377 if (IP6_VCF_V(ntohl(ip6h->vcf)) != IPV6_VERSION) {
378 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_VER], 1);
379 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInHdrErrors, 1);
380 goto buf_rel;
381 }
382
383 /*
384 * 次のデータグラムは破棄する。
385 *
386 * ・始点アドレスがマルチキャスト
387 * ・終点アドレスが未定義
388 *
389 * ネットワークインタフェースがループバックでないとき、
390 * 次のデータグラムも破棄する。
391 *
392 * ・始点アドレスがループバック
393 * ・終点アドレスがループバック
394 *
395 * 悪意のあるユーザが TCP/UDP スタックの混乱や
396 * セキュリティチェックをバイパスするため、IPv4
397 * マップアドレスを悪用することを防ぐため
398 * 以下のデータグラムは破棄する。
399 *
400 * ・始点アドレスが ::ffff:127.0.0.1
401 * ・終点アドレスが ::ffff:127.0.0.1
402 */
403
404 if (IN6_IS_ADDR_MULTICAST(&ip6h->src) ||
405 IN6_IS_ADDR_UNSPECIFIED(&ip6h->dst)) {
406 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_ADDR], 1);
407 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInAddrErrors, 1);
408 goto buf_rel;
409 }
410
411 if (IN6_IS_ADDR_V4MAPPED(&ip6h->src) ||
412 IN6_IS_ADDR_V4MAPPED(&ip6h->dst)) {
413 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_ADDR], 1);
414 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInAddrErrors, 1);
415 goto buf_rel;
416 }
417
418#ifdef SUPPORT_LOOP
419
420#else /* of #ifdef SUPPORT_LOOP */
421
422 if (IN6_IS_ADDR_LOOPBACK(&ip6h->src) ||
423 IN6_IS_ADDR_LOOPBACK(&ip6h->dst)) {
424 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_ADDR], 1);
425 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInAddrErrors, 1);
426 goto buf_rel;
427 }
428
429#endif /* of #ifdef SUPPORT_LOOP */
430
431 ifp = IF_GET_IFNET();
432
433 if (IN6_IS_ADDR_MULTICAST(&ip6h->dst)) {
434 /* 宛å…
435ˆãŒãƒžãƒ«ãƒã‚­ãƒ£ã‚¹ãƒˆã®å ´åˆã®å‡¦ç† */
436 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInMcastPkts, 1);
437 if (!in6_lookup_multi(ifp, &ip6h->dst)) {
438 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_ADDR], 1);
439 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInAddrErrors, 1);
440 goto buf_rel;
441 }
442 }
443 else {
444 if ((ia6 = in6_lookup_ifaddr(ifp, &ip6h->dst)) == NULL) {
445 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_ADDR], 1);
446 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInAddrErrors, 1);
447 goto buf_rel;
448 }
449
450 /* アドレスが未解決の場合はデータグラムを破棄する。*/
451 if (IFA6_IS_NOTREADY(ia6)) {
452 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_ADDR], 1);
453 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInAddrErrors, 1);
454 goto buf_rel;
455 }
456 }
457
458 /* オフセットを設定する。*/
459 offp = GET_IP6_NEXT_HDR(input) - input->buf;
460
461 /* ペイロード長を取り出す。*/
462 plen = ntohs(ip6h->plen);
463
464
465 /* 次ヘッダの位置を初期化する。*/
466 nextp = offsetof(T_IP6_HDR, next);
467
468 /*
469 * 中継点 (Hop-by-Hop) オプションヘッダのチェック
470 */
471 if (ip6h->next == IPPROTO_HOPOPTS) {
472
473 /* 中継点ヘッダの長さをチェックする。*/
474 if (plen < sizeof(T_IP6_HBH_HDR)) {
475 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_SHORT], 1);
476 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInHdrErrors, 1);
477 goto buf_rel;
478 }
479
480 if ((noff = ip6_hopopts_input(input, &offp, &nextp)) < 0) {
481 if (noff == IP6_OPT_RET_ERR) {
482 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_PACKETS], 1);
483 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInHdrErrors, 1);
484 goto buf_rel;
485 }
486 else if (noff == IP6_OPT_RET_REL) {
487 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_PACKETS], 1);
488 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInHdrErrors, 1);
489 return;
490 }
491 }
492
493 /*
494 * ペイロード長が 0 で、中継点オプションがあるときは、
495 * 巨大ペイロード・オプションが含まれなければならない。
496 * このとき、巨大ペイロード・オプションにある
497 * ペイロード長をチェックする必
498要があるが、本実装
499では、
500 * 巨大ペイロード・オプションを未知のオプションとして
501 * 扱うので、何もしない。
502 */
503 next = ((T_IP6_HBH_HDR *)(ip6h + 1))->next;
504 }
505 else
506 next = ip6h->next;
507
508 /* IPv6 データグラム長をチェックする。*/
509 if (input->len - IF_IP6_HDR_SIZE < plen) {
510 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_SHORT], 1);
511 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInTruncatedPkts, 1);
512 goto buf_rel;
513 }
514 else if (input->len > IF_IP6_HDR_SIZE + plen)
515 input->len = IF_IP6_HDR_SIZE + plen;
516
517 /* 上位プロトコル処理関数を呼び出す。*/
518 nest = 0;
519 while (next != IPPROTO_DONE) {
520 if (++ nest > IP6_HDR_NEST_LIMIT) {
521 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_PROTO], 1);
522 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInUnknownProtos, 1);
523 goto buf_rel;
524 }
525 else if ((func = get_upper_proto(next)) == NULL) {
526 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_PROTO], 1);
527 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_PACKETS], 1);
528 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInUnknownProtos, 1);
529
530 /* icmp6_error でネットワークバッファを開放する。*/
531 icmp6_error(input, ICMP6_PARAM_PROB,
532 ICMP6_PARAMPROB_NEXT_HEADER, nextp);
533 return;
534 }
535 else {
536 next = (*func)(&input, &offp, &nextp);
537 }
538 }
539 return;
540
541buf_rel:
542 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_PACKETS], 1);
543 syscall(rel_net_buf(input));
544 }
545
546/*
547 * ip6_nexthdr -- 次のヘッダのオフセットを返す。
548 *
549 * 戻り値 == -1 : 最後のヘッダ、またはエラー。
550 * 戻り値 > 0 : さらにヘッダが続く。
551 */
552
553ER_UINT
554ip6_nexthdr (T_NET_BUF *nbuf, uint_t off, uint_t proto, uint_t *nextp)
555{
556 T_IP6_FRAG_HDR *ip6fh;
557 T_IP6_EXT_HDR *ip6eh;
558
559 switch (proto) {
560
561 case IPPROTO_IPV6: /* IPv6 */
562 if (nbuf->len < off + IP6_HDR_SIZE)
563 return (ER_UINT)-1;
564 *nextp = ((T_IP6_HDR *)(nbuf->buf + off))->next;
565 return off + IP6_HDR_SIZE;
566 break;
567
568 case IPPROTO_FRAGMENT: /* 断片ヘッダ */
569 if (nbuf->len < off + sizeof(T_IP6_FRAG_HDR))
570 return (ER_UINT)-1;
571
572 /* 中間のフラグメントの場合は処理をやめる。*/
573 ip6fh = (T_IP6_FRAG_HDR *)(nbuf->buf + off);
574 if (ntohs(ip6fh->off_flag) & IP6F_MORE_FRAG)
575 return (ER_UINT)-1;
576
577 *nextp = ip6fh->next;
578 return off + sizeof(T_IP6_FRAG_HDR);
579 break;
580
581 case IPPROTO_AH: /* IPv6 認証ヘッダ */
582 case IPPROTO_HOPOPTS: /* IPv6 中継点 (Hop-by-Hop) オプション */
583 case IPPROTO_ROUTING: /* 経路制御ヘッダ */
584 case IPPROTO_DSTOPTS: /* IPv6 終点オプション */
585 if (nbuf->len < off + sizeof(T_IP6_EXT_HDR))
586 return (ER_UINT)-1;
587
588 ip6eh = (T_IP6_EXT_HDR *)(nbuf->buf + off);
589 *nextp = ip6eh->next;
590 if (proto == IPPROTO_AH)
591 return off + ((ip6eh->len + 2) << 2);
592 else
593 return off + ((ip6eh->len + 1) << 3);
594 break;
595 }
596
597 return (ER_UINT)-1;
598 }
599
600/*
601 * ip6_lasthdr -- 最後のヘッダのオフセットを返す。
602 */
603
604ER_UINT
605ip6_lasthdr (T_NET_BUF *nbuf, uint_t off, uint_t proto, uint_t *nextp)
606{
607 ER_UINT newoff;
608
609 while (1) {
610 newoff = ip6_nexthdr(nbuf, off, proto, nextp);
611 if (newoff < 0)
612 return (ER_UINT)off;
613 off = newoff;
614 proto = *nextp;
615 }
616 return (ER_UINT)0u;
617 }
618
619/*
620 * ip6_unknown_opt -- 未知のオプションの処理
621 *
622 * 戻り値:
623 *
624 * 0 以上 正常、オプション長。
625 * IP6_OPT_RET_ERR (-1) エラー、呼出側でネットワークバッファを開放する。
626 * IP6_OPT_RET_REL (-2) エラー、ネットワークバッファは開放されている。
627 */
628
629ER_UINT
630ip6_unknown_opt (T_NET_BUF *input, uint8_t *opt)
631{
632 switch (IP6OPT_TYPE(*opt)) {
633 case IP6OPT_TYPE_SKIP: /* 無視する。 */
634 return *(opt + 1);
635 break;
636 case IP6OPT_TYPE_ICMP: /* 破棄して、マルチキャストでなければ ICMP を返送する。 */
637 if (IN6_IS_ADDR_MULTICAST(&GET_IP6_HDR(input)->dst))
638 return IP6_OPT_RET_ERR;
639 /* no break; */
640 case IP6OPT_TYPE_FORCEICMP: /* 破棄して、ICMP を返送する。 */
641 icmp6_error(input, ICMP6_PARAM_PROB,
642 ICMP6_PARAMPROB_OPTION,
643 (opt - input->buf) - IF_HDR_SIZE);
644 return IP6_OPT_RET_REL;
645 break;
646 /*case IP6OPT_TYPE_DISCARD: データグラムを破棄する。 */
647 }
648
649 return IP6_OPT_RET_ERR;
650 }
651
652/*
653 * ip6_remove_exthdrs -- IPv6 拡張ヘッダを削除し、以降を前に詰める。
654 */
655
656ER
657ip6_remove_exthdrs (T_NET_BUF *nbuf)
658{
659 ER_UINT off;
660 uint_t next;
661
662 /* 開始オフセットを IP ヘッダに設定する。*/
663 off = ((uint8_t *)GET_IP_HDR(nbuf)) - nbuf->buf;
664
665 /* 最終ヘッダを探索する。*/
666 off = ip6_lasthdr(nbuf, off, IPPROTO_IPV6, &next);
667
668 if (off <= 0)
669 return E_OBJ;
670 else {
671 uint_t sdu;
672
673 sdu = GET_IP_SDU(nbuf) - nbuf->buf;
674 if (off > sdu) {
675 memmove(nbuf->buf + sdu, nbuf->buf + off, off - sdu);
676 GET_IP_HDR(nbuf)->plen -= off - sdu;
677 nbuf->len -= off - sdu;
678 }
679 }
680 return E_OK;
681 }
682
683#endif /* of #ifdef SUPPORT_INET6 */
Note: See TracBrowser for help on using the repository browser.