source: azure_iot_hub/trunk/asp3_dcre/tinet/netinet6/ip6_input.c@ 388

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

Azure IoT Hub Device C SDK を使ったサンプルの追加

  • 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.9 KB
Line 
1/*
2 * TINET (TCP/IP Protocol Stack)
3 *
4 * Copyright (C) 2001-2017 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 388 2019-05-22 11:25:18Z 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#include <t_syslog.h>
120#include "tinet_cfg.h"
121
122#endif /* of #ifdef TARGET_KERNEL_ASP */
123
124#ifdef TARGET_KERNEL_JSP
125
126#include <s_services.h>
127#include <t_services.h>
128#include "tinet_id.h"
129
130#endif /* of #ifdef TARGET_KERNEL_JSP */
131
132#include <tinet_defs.h>
133#include <tinet_config.h>
134
135#include <net/if.h>
136#include <net/if_loop.h>
137#include <net/if_ppp.h>
138#include <net/ethernet.h>
139#include <net/ppp_ipcp.h>
140#include <net/net.h>
141#include <net/net_endian.h>
142#include <net/net_buf.h>
143#include <net/net_count.h>
144
145#include <netinet/in.h>
146#include <netinet/in_var.h>
147#include <netinet/ip.h>
148#include <netinet/ip_var.h>
149#include <netinet/ip_icmp.h>
150#include <netinet/tcp.h>
151#include <netinet/tcp_var.h>
152#include <netinet/udp_var.h>
153
154#include <netinet6/nd6.h>
155#include <netinet6/ah6.h>
156#include <netinet6/esp6.h>
157
158#include <net/if_var.h>
159
160#ifdef _IP6_CFG
161
162/*
163 * 関数
164 */
165
166static uint_t ip6_no_header_input (T_NET_BUF **inputp, uint_t *offp, uint_t *nextp);
167
168/*
169 * 上位プロトコル選択構造体
170 */
171
172typedef struct t_proto_switch {
173 uint_t (*func)(T_NET_BUF **, uint_t *, uint_t *); /* å…
174¥åŠ›é–¢æ•° */
175 uint_t proto; /* プロトコル番号 */
176 } T_PROTO_SWITCH;
177
178static const T_PROTO_SWITCH proto_switch[] = {
179 { esp6_input, IPPROTO_ESP }, /* IPv6 暗号化ヘッダ */
180 { ah6_input, IPPROTO_AH }, /* IPv6 認証ヘッダ */
181 { frag6_input, IPPROTO_FRAGMENT }, /* 断片ヘッダ */
182 { route6_input, IPPROTO_ROUTING }, /* 経路制御ヘッダ */
183 { dest6_input, IPPROTO_DSTOPTS }, /* IPv6 終点オプション */
184 { ip6_no_header_input,
185 IPPROTO_NONE }, /* 次ヘッダ無し */
186 { icmp6_input, IPPROTO_ICMPV6 },
187
188#if defined(SUPPORT_TCP)
189
190 { tcp_input, IPPROTO_TCP },
191
192#endif /* of #if defined(SUPPORT_TCP) */
193
194#if defined(SUPPORT_UDP) && TNUM_UDP6_CEPID > 0
195
196 { udp6_input, IPPROTO_UDP },
197
198#endif /* of #if defined(SUPPORT_UDP) && TNUM_UDP6_CEPID > 0 */
199
200 };
201
202#ifdef SUPPORT_MIB
203
204T_IN6_IFSTAT in6_ifstat;
205
206#endif /* of #ifdef SUPPORT_MIB */
207
208/*
209 * get_upper_proto -- 上位プロトコル処理関数を返す。
210 */
211
212static uint_t (*
213get_upper_proto(uint_t proto))(T_NET_BUF **, uint_t *, uint_t *)
214{
215 int_t ix;
216
217 for (ix = sizeof(proto_switch) / sizeof(T_PROTO_SWITCH); ix -- > 0; )
218 if (proto_switch[ix].proto == proto)
219 return proto_switch[ix].func;
220 return NULL;
221 }
222
223/*
224 * ip6_no_header_input -- 次ヘッダが無しの場合のå…
225¥åŠ›å‡¦ç†
226 */
227
228static uint_t
229ip6_no_header_input (T_NET_BUF **input, uint_t *offp, uint_t *nextp)
230{
231 syscall(rel_net_buf(*input));
232 return IPPROTO_DONE;
233 }
234
235/*
236 * ip6_hopopts_input -- 中継点オプションå…
237¥åŠ›å‡¦ç†
238 *
239 * 本実装
240では、パディング以外のオプションをå…
241¨ã¦æœªçŸ¥ã‚ªãƒ—ションとして処理する。
242 *
243 * 戻り値:
244 *
245 * 0 以上 正常
246 * IP6_OPT_RET_ERR (-1) エラー、呼出側でネットワークバッファを開放する。
247 * IP6_OPT_RET_REL (-2) エラー、ネットワークバッファは開放されている。
248 */
249
250static ER_UINT
251ip6_hopopts_input (T_NET_BUF *input, uint_t *offp, uint_t *nextp)
252{
253 T_IP6_HBH_HDR *hbh;
254 ER_UINT optlen;
255 uint_t len;
256 uint8_t *opt;
257
258 hbh = (T_IP6_HBH_HDR *)(input->buf + *offp);
259 len = (hbh->len + 1) << 3;
260 *nextp = *offp - IF_HDR_SIZE + offsetof(T_IP6_HBH_HDR, next);
261 *offp += len;
262 opt = (uint8_t *)hbh + sizeof(T_IP6_HBH_HDR);
263
264 for (len -= sizeof(T_IP6_HBH_HDR); len > 0; len -= optlen) {
265 switch (*opt) {
266 case IP6OPT_PAD1:
267 optlen = 1;
268 break;
269 case IP6OPT_PADN:
270 if (len < IP6OPT_MINLEN)
271 return IP6_OPT_RET_ERR;
272 optlen = *(opt + 1) + 2;
273 break;
274 case IP6OPT_JUMBO:
275 case IP6OPT_NSAP_ADDR:
276 case IP6OPT_TUNNEL_LIMIT:
277 case IP6OPT_RTR_ALERT:
278 default:
279 if (len < IP6OPT_MINLEN)
280 return IP6_OPT_RET_ERR;
281 optlen = ip6_unknown_opt(input, opt);
282 if (optlen < 0)
283 return optlen;
284 optlen += 2;
285 break;
286 }
287
288 opt += optlen;
289 }
290
291 return *offp;
292 }
293
294/*
295 * ip6_get_prev_hdr -- 直前の拡張ヘッダの next フィールドのアドレスを返す。
296 *
297 * 注意: off には IF_HDR_SIZE も含まれている。
298 */
299
300uint8_t *
301ip6_get_prev_hdr (T_NET_BUF *nbuf, uint_t off)
302{
303 T_IP6_HDR *ip6h;
304 T_IP6_EXT_HDR *ip6eh;
305 uint_t len;
306 uint8_t next;
307
308 ip6h = GET_IP6_HDR(nbuf);
309 if (off == IF_HDR_SIZE + sizeof(T_IP6_HDR))
310 return &ip6h->next;
311 else {
312 next = ip6h->next;
313 len = IF_HDR_SIZE + sizeof(T_IP6_HDR);
314 ip6eh = NULL;
315
316 while (len < off) {
317 ip6eh = (T_IP6_EXT_HDR *)(nbuf->buf + len);
318
319 switch (next) {
320 case IPPROTO_FRAGMENT:
321 len += sizeof(T_IP6_FRAG_HDR);
322 break;
323 case IPPROTO_AH:
324 len += (ip6eh->len + 2) << 2;
325 break;
326 default:
327 len += (ip6eh->len + 2) << 3;
328 break;
329 }
330 next = ip6eh->next;
331 }
332
333 if (ip6eh == NULL)
334 return NULL;
335 else
336 return &ip6eh->next;
337 }
338 }
339
340/*
341 * ip6_init -- IPv6 の初期化関数
342 */
343
344void
345ip6_init (void)
346{
347 in6_init();
348 in6_ifainit();
349 }
350
351/*
352 * ip6_input -- IPv6 のå…
353¥åŠ›é–¢æ•°
354 */
355
356void
357ip6_input (T_NET_BUF *input)
358{
359 T_IP6_HDR *ip6h;
360 T_IN6_IFADDR *ia6;
361 T_IFNET *ifp;
362 ER_UINT noff;
363 uint_t next, nest, plen, offp, nextp;
364 uint_t (*func)(T_NET_BUF **, uint_t *, uint_t *);
365
366 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_OCTETS], input->len - IF_HDR_SIZE);
367 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_PACKETS], 1);
368 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInReceives, 1);
369
370 /* IP ヘッダの長さをチェックする。*/
371 if (input->len < IF_IP6_HDR_SIZE) {
372 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_SHORT], 1);
373 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInHdrErrors, 1);
374 goto buf_rel;
375 }
376
377 ip6h = GET_IP6_HDR(input);
378
379 /* バージョンをチェックする。*/
380 if (IP6_VCF_V(ntohl(ip6h->vcf)) != IPV6_VERSION) {
381 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_VER], 1);
382 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInHdrErrors, 1);
383 goto buf_rel;
384 }
385
386 /*
387 * 次のデータグラムは破棄する。
388 *
389 * ・始点アドレスがマルチキャスト
390 * ・終点アドレスが未定義
391 *
392 * ネットワークインタフェースがループバックでないとき、
393 * 次のデータグラムも破棄する。
394 *
395 * ・始点アドレスがループバック
396 * ・終点アドレスがループバック
397 *
398 * 悪意のあるユーザが TCP/UDP スタックの混乱や
399 * セキュリティチェックをバイパスするため、IPv4
400 * マップアドレスを悪用することを防ぐため
401 * 以下のデータグラムは破棄する。
402 *
403 * ・始点アドレスが ::ffff:127.0.0.1
404 * ・終点アドレスが ::ffff:127.0.0.1
405 */
406
407 if (IN6_IS_ADDR_MULTICAST(&ip6h->src) ||
408 IN6_IS_ADDR_UNSPECIFIED(&ip6h->dst)) {
409 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_ADDR], 1);
410 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInAddrErrors, 1);
411 goto buf_rel;
412 }
413
414 if (IN6_IS_ADDR_V4MAPPED(&ip6h->src) ||
415 IN6_IS_ADDR_V4MAPPED(&ip6h->dst)) {
416 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_ADDR], 1);
417 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInAddrErrors, 1);
418 goto buf_rel;
419 }
420
421#ifdef SUPPORT_LOOP
422
423#else /* of #ifdef SUPPORT_LOOP */
424
425 if (IN6_IS_ADDR_LOOPBACK(&ip6h->src) ||
426 IN6_IS_ADDR_LOOPBACK(&ip6h->dst)) {
427 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_ADDR], 1);
428 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInAddrErrors, 1);
429 goto buf_rel;
430 }
431
432#endif /* of #ifdef SUPPORT_LOOP */
433
434 ifp = IF_GET_IFNET();
435
436 if (IN6_IS_ADDR_MULTICAST(&ip6h->dst)) {
437 /* 宛å…
438ˆãŒãƒžãƒ«ãƒã‚­ãƒ£ã‚¹ãƒˆã®å ´åˆã®å‡¦ç† */
439 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInMcastPkts, 1);
440 if (!in6_lookup_multi(ifp, &ip6h->dst)) {
441 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_ADDR], 1);
442 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInAddrErrors, 1);
443 goto buf_rel;
444 }
445 }
446 else {
447 if ((ia6 = in6_lookup_ifaddr(ifp, &ip6h->dst)) == NULL) {
448 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_ADDR], 1);
449 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInAddrErrors, 1);
450 goto buf_rel;
451 }
452
453 /* アドレスが未解決の場合はデータグラムを破棄する。*/
454 if (IFA6_IS_NOTREADY(ia6)) {
455 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_ADDR], 1);
456 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInAddrErrors, 1);
457 goto buf_rel;
458 }
459 }
460
461 /* オフセットを設定する。*/
462 offp = GET_IP6_NEXT_HDR(input) - input->buf;
463
464 /* ペイロード長を取り出す。*/
465 plen = ntohs(ip6h->plen);
466
467
468 /* 次ヘッダの位置を初期化する。*/
469 nextp = offsetof(T_IP6_HDR, next);
470
471 /*
472 * 中継点 (Hop-by-Hop) オプションヘッダのチェック
473 */
474 if (ip6h->next == IPPROTO_HOPOPTS) {
475
476 /* 中継点ヘッダの長さをチェックする。*/
477 if (plen < sizeof(T_IP6_HBH_HDR)) {
478 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_SHORT], 1);
479 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInHdrErrors, 1);
480 goto buf_rel;
481 }
482
483 if ((noff = ip6_hopopts_input(input, &offp, &nextp)) < 0) {
484 if (noff == IP6_OPT_RET_ERR) {
485 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_PACKETS], 1);
486 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInHdrErrors, 1);
487 goto buf_rel;
488 }
489 else if (noff == IP6_OPT_RET_REL) {
490 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_PACKETS], 1);
491 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInHdrErrors, 1);
492 return;
493 }
494 }
495
496 /*
497 * ペイロード長が 0 で、中継点オプションがあるときは、
498 * 巨大ペイロード・オプションが含まれなければならない。
499 * このとき、巨大ペイロード・オプションにある
500 * ペイロード長をチェックする必
501要があるが、本実装
502では、
503 * 巨大ペイロード・オプションを未知のオプションとして
504 * 扱うので、何もしない。
505 */
506 next = ((T_IP6_HBH_HDR *)(ip6h + 1))->next;
507 }
508 else
509 next = ip6h->next;
510
511 /* IPv6 データグラム長をチェックする。*/
512 if (input->len - IF_IP6_HDR_SIZE < plen) {
513 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_SHORT], 1);
514 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInTruncatedPkts, 1);
515 goto buf_rel;
516 }
517 else if (input->len > IF_IP6_HDR_SIZE + plen)
518 input->len = IF_IP6_HDR_SIZE + plen;
519
520 /* 上位プロトコル処理関数を呼び出す。*/
521 nest = 0;
522 while (next != IPPROTO_DONE) {
523 if (++ nest > IP6_HDR_NEST_LIMIT) {
524 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_PROTO], 1);
525 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInUnknownProtos, 1);
526 goto buf_rel;
527 }
528 else if ((func = get_upper_proto(next)) == NULL) {
529 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_PROTO], 1);
530 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_PACKETS], 1);
531 NET_COUNT_MIB(in6_ifstat.ipv6IfStatsInUnknownProtos, 1);
532
533 /* icmp6_error でネットワークバッファを開放する。*/
534 icmp6_error(input, ICMP6_PARAM_PROB,
535 ICMP6_PARAMPROB_NEXT_HEADER, nextp);
536 return;
537 }
538 else {
539 next = (*func)(&input, &offp, &nextp);
540 }
541 }
542 return;
543
544buf_rel:
545 NET_COUNT_IP6(net_count_ip6[NC_IP6_IN_ERR_PACKETS], 1);
546 syscall(rel_net_buf(input));
547 }
548
549/*
550 * ip6_nexthdr -- 次のヘッダのオフセットを返す。
551 *
552 * 戻り値 == -1 : 最後のヘッダ、またはエラー。
553 * 戻り値 > 0 : さらにヘッダが続く。
554 */
555
556ER_UINT
557ip6_nexthdr (T_NET_BUF *nbuf, uint_t off, uint_t proto, uint_t *nextp)
558{
559 T_IP6_FRAG_HDR *ip6fh;
560 T_IP6_EXT_HDR *ip6eh;
561
562 switch (proto) {
563
564 case IPPROTO_IPV6: /* IPv6 */
565 if (nbuf->len < off + IP6_HDR_SIZE)
566 return (ER_UINT)-1;
567 *nextp = ((T_IP6_HDR *)(nbuf->buf + off))->next;
568 return off + IP6_HDR_SIZE;
569 break;
570
571 case IPPROTO_FRAGMENT: /* 断片ヘッダ */
572 if (nbuf->len < off + sizeof(T_IP6_FRAG_HDR))
573 return (ER_UINT)-1;
574
575 /* 中間のフラグメントの場合は処理をやめる。*/
576 ip6fh = (T_IP6_FRAG_HDR *)(nbuf->buf + off);
577 if (ntohs(ip6fh->off_flag) & IP6F_MORE_FRAG)
578 return (ER_UINT)-1;
579
580 *nextp = ip6fh->next;
581 return off + sizeof(T_IP6_FRAG_HDR);
582 break;
583
584 case IPPROTO_AH: /* IPv6 認証ヘッダ */
585 case IPPROTO_HOPOPTS: /* IPv6 中継点 (Hop-by-Hop) オプション */
586 case IPPROTO_ROUTING: /* 経路制御ヘッダ */
587 case IPPROTO_DSTOPTS: /* IPv6 終点オプション */
588 if (nbuf->len < off + sizeof(T_IP6_EXT_HDR))
589 return (ER_UINT)-1;
590
591 ip6eh = (T_IP6_EXT_HDR *)(nbuf->buf + off);
592 *nextp = ip6eh->next;
593 if (proto == IPPROTO_AH)
594 return off + ((ip6eh->len + 2) << 2);
595 else
596 return off + ((ip6eh->len + 1) << 3);
597 break;
598 }
599
600 return (ER_UINT)-1;
601 }
602
603/*
604 * ip6_lasthdr -- 最後のヘッダのオフセットを返す。
605 */
606
607ER_UINT
608ip6_lasthdr (T_NET_BUF *nbuf, uint_t off, uint_t proto, uint_t *nextp)
609{
610 ER_UINT newoff;
611
612 while (1) {
613 newoff = ip6_nexthdr(nbuf, off, proto, nextp);
614 if (newoff < 0)
615 return (ER_UINT)off;
616 off = newoff;
617 proto = *nextp;
618 }
619 return (ER_UINT)0u;
620 }
621
622/*
623 * ip6_unknown_opt -- 未知のオプションの処理
624 *
625 * 戻り値:
626 *
627 * 0 以上 正常、オプション長。
628 * IP6_OPT_RET_ERR (-1) エラー、呼出側でネットワークバッファを開放する。
629 * IP6_OPT_RET_REL (-2) エラー、ネットワークバッファは開放されている。
630 */
631
632ER_UINT
633ip6_unknown_opt (T_NET_BUF *input, uint8_t *opt)
634{
635 switch (IP6OPT_TYPE(*opt)) {
636 case IP6OPT_TYPE_SKIP: /* 無視する。 */
637 return *(opt + 1);
638 break;
639 case IP6OPT_TYPE_ICMP: /* 破棄して、マルチキャストでなければ ICMP を返送する。 */
640 if (IN6_IS_ADDR_MULTICAST(&GET_IP6_HDR(input)->dst))
641 return IP6_OPT_RET_ERR;
642 /* no break; */
643 case IP6OPT_TYPE_FORCEICMP: /* 破棄して、ICMP を返送する。 */
644 icmp6_error(input, ICMP6_PARAM_PROB,
645 ICMP6_PARAMPROB_OPTION,
646 (opt - input->buf) - IF_HDR_SIZE);
647 return IP6_OPT_RET_REL;
648 break;
649 /*case IP6OPT_TYPE_DISCARD: データグラムを破棄する。 */
650 }
651
652 return IP6_OPT_RET_ERR;
653 }
654
655/*
656 * ip6_remove_exthdrs -- IPv6 拡張ヘッダを削除し、以降を前に詰める。
657 */
658
659ER
660ip6_remove_exthdrs (T_NET_BUF *nbuf)
661{
662 ER_UINT off;
663 uint_t next;
664
665 /* 開始オフセットを IP ヘッダに設定する。*/
666 off = ((uint8_t *)GET_IP6_HDR(nbuf)) - nbuf->buf;
667
668 /* 最終ヘッダを探索する。*/
669 off = ip6_lasthdr(nbuf, off, IPPROTO_IPV6, &next);
670
671 if (off <= 0)
672 return E_OBJ;
673 else {
674 uint_t sdu;
675
676 sdu = GET_IP6_SDU(nbuf) - nbuf->buf;
677 if (off > sdu) {
678 memmove(nbuf->buf + sdu, nbuf->buf + off, off - sdu);
679 GET_IP6_HDR(nbuf)->plen -= off - sdu;
680 nbuf->len -= off - sdu;
681 }
682 }
683 return E_OK;
684 }
685
686#endif /* of #ifdef _IP6_CFG */
Note: See TracBrowser for help on using the repository browser.