source: EcnlProtoTool/trunk/asp3_dcre/tinet/netinet/ip_icmp.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: 13.1 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: ip_icmp.c 270 2017-02-09 04:03:47Z coas-nagasima $
44 */
45
46/*
47 * Copyright (c) 1982, 1986, 1988, 1993
48 * The Regents of the University of California. 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. All advertising materials mentioning features or use of this software
59 * must display the following acknowledgement:
60 * This product includes software developed by the University of
61 * California, Berkeley and its contributors.
62 * 4. Neither the name of the University nor the names of its contributors
63 * may be used to endorse or promote products derived from this software
64 * without specific prior written permission.
65 *
66 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 * SUCH DAMAGE.
77 *
78 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
79 * $FreeBSD: src/sys/netinet/ip_icmp.c,v 1.33.2.3 1999/10/14 11:49:38 des Exp $
80 */
81
82#include <string.h>
83
84#ifdef TARGET_KERNEL_ASP
85
86#include <kernel.h>
87#include <sil.h>
88#include <t_syslog.h>
89#include "kernel_cfg.h"
90
91#endif /* of #ifdef TARGET_KERNEL_ASP */
92
93#ifdef TARGET_KERNEL_JSP
94
95#include <s_services.h>
96#include <t_services.h>
97
98#endif /* of #ifdef TARGET_KERNEL_JSP */
99
100#include <tinet_defs.h>
101#include <tinet_config.h>
102
103#include <net/if.h>
104#include <net/if_loop.h>
105#include <net/if_ppp.h>
106#include <net/ethernet.h>
107#include <net/ppp_ipcp.h>
108#include <net/net.h>
109#include <net/net_buf.h>
110#include <net/net_timer.h>
111#include <net/net_count.h>
112
113#include <netinet/in.h>
114#include <netinet/in_var.h>
115#include <netinet/ip.h>
116#include <netinet/ip_var.h>
117#include <netinet/ip_icmp.h>
118#include <netinet/icmp_var.h>
119#include <netinet/tcp.h>
120#include <netinet/tcp_timer.h>
121#include <netinet/tcp_var.h>
122
123/*
124 * 変数
125 */
126
127#ifdef SUPPORT_MIB
128
129/*
130 * SNMP の 管理情
131報ベース (MIB)
132 */
133
134T_ICMP_STATS icmp_stats;
135
136#endif /* of #ifdef SUPPORT_MIB */
137
138/*
139 * 関数
140 */
141
142static void icmp_echo (T_NET_BUF *input, uint_t ihoff);
143static void icmp_unreach (T_NET_BUF *input, uint_t ihoff);
144
145#if NUM_REDIRECT_ROUTE_ENTRY > 0
146
147static void icmp_redirect (T_NET_BUF *input, uint_t ihoff);
148
149#endif /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
150
151/*
152 * icmp_echo -- エコー要求を受信したときの処理
153 *
154 * input には IF ヘッダと IP ヘッダも含まれている。
155 */
156
157static void
158icmp_echo (T_NET_BUF *input, uint_t ihoff)
159{
160 T_IP4_HDR *ip4h;
161 T_ICMP4_HDR *icmp4h;
162 T_IN4_ADDR addr;
163
164 NET_COUNT_MIB(icmp_stats.icmpInEchos, 1);
165
166 /* メッセージの型をエコー要求 (8) から エコー応答 (0) に */
167 /* 変更して送り返す。 */
168
169 icmp4h = GET_ICMP4_HDR(input, ihoff);
170 icmp4h->type = ICMP4_ECHO_REPLY;
171
172 /* IP ヘッダの宛å…
173ˆã¨ç™ºä¿¡å…
174ƒã‚’交換する。*/
175 ip4h = GET_IP4_HDR(input);
176 addr = ip4h->src;
177 ip4h->src = ip4h->dst;
178 ip4h->dst = addr;
179
180 /* チェックサムを計算する。*/
181 icmp4h->sum = 0;
182 icmp4h->sum = in_cksum(icmp4h,
183 (uint_t)(((input->len - GET_IF_IP4_HDR_SIZE(input)) + 3) >> 2 << 2));
184
185 /* 送信する。*/
186 NET_COUNT_ICMP4(net_count_icmp4.out_octets,
187 input->len - GET_IF_IP4_HDR_SIZE(input));
188 NET_COUNT_ICMP4(net_count_icmp4.out_packets, 1);
189 NET_COUNT_MIB(icmp_stats.icmpOutMsgs, 1);
190 NET_COUNT_MIB(icmp_stats.icmpOutEchoReps, 1);
191 ip_output(input, TMO_ICMP_OUTPUT);
192 }
193
194/*
195 * icmp_unreach -- ICMP4_UNREACH を受信したときの処理
196 */
197
198static const int8_t code2error[] = {
199 EV_HURCH, /* ICMP4_UNREACH_NET 0 */
200 EV_HURCH, /* ICMP4_UNREACH_HOST 1 */
201 EV_CNNRF, /* ICMP4_UNREACH_PROTOCOL 2 */
202 EV_CNNRF, /* ICMP4_UNREACH_PORT 3 */
203 EV_MSIZE, /* ICMP4_UNREACH_FLAG 4 */
204 EV_HURCH, /* ICMP4_UNREACH_SRC_FAIL 5 */
205 EV_HURCH, /* ICMP4_UNREACH_NET_UNKNOWN 6 */
206 EV_HURCH, /* ICMP4_UNREACH_HOST_UNKNOWN 7 */
207 EV_HURCH, /* ICMP4_UNREACH_ISOLATED 8 */
208 EV_HURCH, /* ICMP4_UNREACH_NET_PROHIB 9 */
209 EV_HURCH, /* ICMP4_UNREACH_HOST_PROHIB 10 */
210 EV_HURCH, /* ICMP4_UNREACH_TOS_NET 11 */
211 EV_HURCH, /* ICMP4_UNREACH_TOS_HOST 12 */
212 EV_HURCH, /* ICMP4_UNREACH_FILTER_PROHIB 13 */
213 EV_HURCH, /* ICMP4_UNREACH_HOST_PRECEDENCE 14 */
214 EV_HURCH, /* ICMP4_UNREACH_PRECEDENCE_CUTOFF 15 */
215 };
216
217static void
218icmp_unreach (T_NET_BUF *input, uint_t ihoff)
219{
220 T_IP4_HDR *ip4h;
221 uint8_t code;
222 ER error;
223
224 NET_COUNT_MIB(icmp_stats.icmpInDestUnreachs, 1);
225
226 ip4h = (T_IP4_HDR*)GET_ICMP4_SDU(input, ihoff);
227 code = GET_ICMP4_HDR(input, ihoff)->code;
228 error = code2error[code];
229 if (ip4h->proto == IPPROTO_TCP) {
230
231#ifdef SUPPORT_TCP
232
233 memcpy(GET_IP4_HDR(input), ip4h, input->len - (IP4_HDR_SIZE + ICMP4_HDR_SIZE));
234 input->len -= IP4_HDR_SIZE + ICMP4_HDR_SIZE;
235 tcp_notify(input, error);
236
237#endif /* of #ifdef SUPPORT_TCP */
238
239 }
240 else
241 syslog(LOG_NOTICE, "[ICMP] error, code: %d.", code);
242 }
243
244#if NUM_REDIRECT_ROUTE_ENTRY > 0
245
246/*
247 * icmp_redirect -- 向け直しメッセージを受信したときの処理
248 *
249 * input には IF ヘッダと IP ヘッダも含まれている。
250 */
251
252static void
253icmp_redirect (T_NET_BUF *input, uint_t ihoff)
254{
255 T_IP4_HDR *ip4h, *sip4h;
256 T_ICMP4_HDR *icmp4h;
257
258 ip4h = GET_IP4_HDR(input);
259 icmp4h = GET_ICMP4_HDR(input, ihoff);
260 sip4h = (T_IP4_HDR *)GET_ICMP4_SDU(input, ihoff);
261
262 /*
263 * 内
264容チェック、以下の場合はエラー
265 *
266 * ・ICMP コードが、ICMP4_REDIRECT_PORT_AND_HOST (3) 以上
267 * ・データグラム長が、ICMP の最小長より短い (IP ヘッダ + ICMP ヘッダ + IP ヘッダ + 8)
268 * ・データグラム長が、ICMP の最大長より短い
269 * ・
270 */
271 if (icmp4h->code > ICMP4_REDIRECT_PORT_AND_HOST ||
272 ip4h->len < GET_IP4_ICMP4_HDR_SIZE(input) + 8 + IP4_HDR_SIZE ||
273 ip4h->len < GET_IP4_ICMP4_HDR_SIZE(input) + 8 + (IP4_VHL_HL(sip4h->vhl) << 2)) {
274 NET_COUNT_ICMP4(net_count_icmp4.in_err_packets, 1);
275 return;
276 }
277
278 in4_rtredirect(icmp4h->data.addr, sip4h->dst, IN_RTF_REDIRECT, TMO_IN_REDIRECT);
279 }
280
281#endif /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
282
283/*
284 * icmp_input -- ICMP のå…
285¥åŠ›é–¢æ•°
286 *
287 * input には IF ヘッダと IP ヘッダも含まれている。
288 */
289
290uint_t
291icmp_input (T_NET_BUF **inputp, uint_t *offp, uint_t *nextp)
292{
293 T_NET_BUF *input = *inputp;
294 T_ICMP4_HDR *icmp4h;
295 T_IN4_ADDR addr;
296 uint_t len, align;
297
298 NET_COUNT_ICMP4(net_count_icmp4.in_octets,
299 input->len - GET_IF_IP4_HDR_SIZE(input));
300 NET_COUNT_ICMP4(net_count_icmp4.in_packets, 1);
301
302 /* ICMP ヘッダの長さをチェックする。*/
303 if (input->len < IF_IP4_ICMP4_HDR_SIZE) {
304 NET_COUNT_ICMP4(net_count_icmp4.in_err_packets, 1);
305 NET_COUNT_MIB(icmp_stats.icmpInErrors, 1);
306 goto buf_rel;
307 }
308
309 icmp4h = (T_ICMP4_HDR *)(input->buf + *offp);
310
311 /* 4 オクテット境界のデータ長 */
312 len = input->len - *offp;
313 align = (len + 3) >> 2 << 2;
314
315 /* 4 オクテット境界までパディングで埋める。*/
316 if (align > len)
317 memset((uint8_t*)input->buf + input->len, 0, (size_t)(align - len));
318
319 /* チェックサムを計算する。*/
320 if (in_cksum(icmp4h, align) != 0) {
321 NET_COUNT_ICMP4(net_count_icmp4.in_err_packets, 1);
322 goto buf_rel;
323 }
324
325 /* メッセージの型により分岐する。*/
326 switch (icmp4h->type) {
327 case ICMP4_ECHO_REQUEST:
328 icmp_echo(input, *offp);
329 return IPPROTO_DONE;
330 break;
331 case ICMP4_ECHO_REPLY:
332
333#ifdef ICMP_CFG_CALLBACK_ECHO_REPLY
334
335 icmp_echo_reply(input, *offp);
336
337#endif /* of #ifdef ICMP_CFG_CALLBACK_ECHO_REPLY */
338
339 break;
340 case ICMP4_UNREACH:
341 icmp_unreach(input, *offp);
342 break;
343 case ICMP4_REDIRECT:
344
345#if NUM_REDIRECT_ROUTE_ENTRY > 0
346
347 addr = ntohl(icmp4h->data.addr);
348 syslog(LOG_INFO, "[ICMP] redirect, addr: %s.", ip2str(NULL, &addr));
349 icmp_redirect(input, *offp);
350
351#else /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
352
353 addr = ntohl(icmp4h->data.addr);
354 syslog(LOG_INFO, "[ICMP] redirect ignored, addr: %s.", ip2str(NULL, &addr));
355
356#endif /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
357
358 break;
359 default:
360 syslog(LOG_INFO, "[ICMP] unknown type: %d.", icmp4h->type);
361 NET_COUNT_ICMP4(net_count_icmp4.in_err_packets, 1);
362 break;
363 }
364
365buf_rel:
366 syscall(rel_net_buf(input));
367 return IPPROTO_DONE;
368 }
369
370#ifdef ICMP_REPLY_ERROR
371
372/*
373 * icmp_error -- ICMP エラー送信関数。
374 *
375 * input には IF ヘッダと IP ヘッダも含まれている。
376 * icmp_error では、ネットワークバッファ input を返却しないので、
377 * 呼出し側で、開放してから終了する。
378 */
379
380void
381icmp_error (uint8_t code, T_NET_BUF *input)
382{
383 T_IP4_HDR *ip4h;
384 T_ICMP4_HDR *icmp4h;
385 T_NET_BUF *output;
386 T_IN4_ADDR saddr;
387 uint_t len, ip4hl, align;
388
389 ip4h = GET_IP4_HDR(input);
390 ip4hl = GET_IP4_HDR_SIZE(ip4h);
391
392 /* 送信用の IP データグラムを獲得する。*/
393 if (input->len - ip4hl < 8)
394 len = input->len - ip4hl;
395 else
396 len = 8;
397
398 saddr = ntohl(ip4h->src);
399 if (in4_get_datagram(&output, (uint_t)(ICMP4_HDR_SIZE + ip4hl + len), 0,
400 &saddr, NULL, IPPROTO_ICMP, IP4_DEFTTL,
401 NBA_SEARCH_ASCENT, TMO_ICMP_OUTPUT) != E_OK)
402 return;
403
404 /* ICMP ヘッダを設定する。*/
405 icmp4h = GET_ICMP4_HDR(output, IF_IP4_ICMP4_HDR_OFFSET);
406 icmp4h->type = ICMP4_UNREACH;
407 icmp4h->code = code;
408 icmp4h->data.addr= 0;
409
410 /* エラーが発生した IP ヘッダと データ 8 オクテットをコピーする。*/
411 memcpy(GET_ICMP4_SDU(output, IF_IP4_ICMP4_HDR_OFFSET),
412 GET_IP4_HDR(input), (size_t)(ip4hl + len));
413
414 /* 4 オクテット境界のデータ長 */
415 align = (len + 3) >> 2 << 2;
416
417 /* 4 オクテット境界までパディングで埋める。*/
418 if (align > len)
419 memset((uint8_t*)GET_ICMP4_SDU(output, IF_IP4_ICMP4_HDR_OFFSET) + ip4hl + len,
420 0, (size_t)(align - len));
421
422 /* チェックサムを計算する。*/
423 icmp4h->sum = 0;
424 icmp4h->sum = in_cksum(icmp4h, (uint_t)(ICMP4_HDR_SIZE + ip4hl + align));
425
426 /* 送信する。*/
427 NET_COUNT_ICMP4(net_count_icmp4.out_octets,
428 output->len - GET_IF_IP4_HDR_SIZE(output));
429 NET_COUNT_ICMP4(net_count_icmp4.out_packets, 1);
430 NET_COUNT_MIB(icmp_stats.icmpOutMsgs, 1);
431 NET_COUNT_MIB(icmp_stats.icmpOutDestUnreachs, 1);
432 ip_output(output, TMO_ICMP_OUTPUT);
433 }
434
435#endif /* of #ifdef ICMP_REPLY_ERROR */
Note: See TracBrowser for help on using the repository browser.