source: EcnlProtoTool/trunk/asp3_dcre/tinet/netinet/ip_icmp.c@ 321

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

文字コードを設定

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
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 * 上記著作権者は,以下の (1)~(4) の条件か,Free Software Foundation
8 * によって公表されている GNU General Public License の Version 2 に記
9 * 述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
10 * を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
11 * 利用と呼ぶ)することを無償で許諾する.
12 * (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
13 * 権表示,この利用条件および下記の無保証規定が,そのままの形でソー
14 * スコード中に含まれていること.
15 * (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
16 * 用できる形で再配布する場合には,再配布に伴うドキュメント(利用
17 * 者マニュアルなど)に,上記の著作権表示,この利用条件および下記
18 * の無保証規定を掲載すること.
19 * (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
20 * 用できない形で再配布する場合には,次の条件を満たすこと.
21 * (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
22 * 作権表示,この利用条件および下記の無保証規定を掲載すること.
23 * (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
24 * 害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
25 *
26 * 本ソフトウェアは,無保証で提供されているものである.上記著作権者お
27 * よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
28 * 含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
29 * 接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
30 *
31 * @(#) $Id$
32 */
33
34/*
35 * Copyright (c) 1982, 1986, 1988, 1993
36 * The Regents of the University of California. All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
67 * $FreeBSD: src/sys/netinet/ip_icmp.c,v 1.33.2.3 1999/10/14 11:49:38 des Exp $
68 */
69
70#include <string.h>
71
72#ifdef TARGET_KERNEL_ASP
73
74#include <kernel.h>
75#include <sil.h>
76#include <t_syslog.h>
77#include "kernel_cfg.h"
78
79#endif /* of #ifdef TARGET_KERNEL_ASP */
80
81#ifdef TARGET_KERNEL_JSP
82
83#include <s_services.h>
84#include <t_services.h>
85
86#endif /* of #ifdef TARGET_KERNEL_JSP */
87
88#include <tinet_defs.h>
89#include <tinet_config.h>
90
91#include <net/if.h>
92#include <net/if_loop.h>
93#include <net/if_ppp.h>
94#include <net/ethernet.h>
95#include <net/ppp_ipcp.h>
96#include <net/net.h>
97#include <net/net_buf.h>
98#include <net/net_timer.h>
99#include <net/net_count.h>
100
101#include <netinet/in.h>
102#include <netinet/in_var.h>
103#include <netinet/ip.h>
104#include <netinet/ip_var.h>
105#include <netinet/ip_icmp.h>
106#include <netinet/icmp_var.h>
107#include <netinet/tcp.h>
108#include <netinet/tcp_timer.h>
109#include <netinet/tcp_var.h>
110
111/*
112 * 変数
113 */
114
115#ifdef SUPPORT_MIB
116
117/*
118 * SNMP の 管理情報ベース (MIB)
119 */
120
121T_ICMP_STATS icmp_stats;
122
123#endif /* of #ifdef SUPPORT_MIB */
124
125/*
126 * 関数
127 */
128
129static void icmp_echo (T_NET_BUF *input, uint_t ihoff);
130static void icmp_unreach (T_NET_BUF *input, uint_t ihoff);
131
132#if NUM_REDIRECT_ROUTE_ENTRY > 0
133
134static void icmp_redirect (T_NET_BUF *input, uint_t ihoff);
135
136#endif /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
137
138/*
139 * icmp_echo -- エコー要求を受信したときの処理
140 *
141 * input には IF ヘッダと IP ヘッダも含まれている。
142 */
143
144static void
145icmp_echo (T_NET_BUF *input, uint_t ihoff)
146{
147 T_IP4_HDR *ip4h;
148 T_ICMP4_HDR *icmp4h;
149 T_IN4_ADDR addr;
150
151 NET_COUNT_MIB(icmp_stats.icmpInEchos, 1);
152
153 /* メッセージの型をエコー要求 (8) から エコー応答 (0) に */
154 /* 変更して送り返す。 */
155
156 icmp4h = GET_ICMP4_HDR(input, ihoff);
157 icmp4h->type = ICMP4_ECHO_REPLY;
158
159 /* IP ヘッダの宛先と発信元を交換する。*/
160 ip4h = GET_IP4_HDR(input);
161 addr = ip4h->src;
162 ip4h->src = ip4h->dst;
163 ip4h->dst = addr;
164
165 /* チェックサムを計算する。*/
166 icmp4h->sum = 0;
167 icmp4h->sum = in_cksum(icmp4h,
168 (uint_t)(((input->len - GET_IF_IP4_HDR_SIZE(input)) + 3) >> 2 << 2));
169
170 /* 送信する。*/
171 NET_COUNT_ICMP4(net_count_icmp4.out_octets,
172 input->len - GET_IF_IP4_HDR_SIZE(input));
173 NET_COUNT_ICMP4(net_count_icmp4.out_packets, 1);
174 NET_COUNT_MIB(icmp_stats.icmpOutMsgs, 1);
175 NET_COUNT_MIB(icmp_stats.icmpOutEchoReps, 1);
176 ip_output(input, TMO_ICMP_OUTPUT);
177 }
178
179/*
180 * icmp_unreach -- ICMP4_UNREACH を受信したときの処理
181 */
182
183static const int8_t code2error[] = {
184 EV_HURCH, /* ICMP4_UNREACH_NET 0 */
185 EV_HURCH, /* ICMP4_UNREACH_HOST 1 */
186 EV_CNNRF, /* ICMP4_UNREACH_PROTOCOL 2 */
187 EV_CNNRF, /* ICMP4_UNREACH_PORT 3 */
188 EV_MSIZE, /* ICMP4_UNREACH_FLAG 4 */
189 EV_HURCH, /* ICMP4_UNREACH_SRC_FAIL 5 */
190 EV_HURCH, /* ICMP4_UNREACH_NET_UNKNOWN 6 */
191 EV_HURCH, /* ICMP4_UNREACH_HOST_UNKNOWN 7 */
192 EV_HURCH, /* ICMP4_UNREACH_ISOLATED 8 */
193 EV_HURCH, /* ICMP4_UNREACH_NET_PROHIB 9 */
194 EV_HURCH, /* ICMP4_UNREACH_HOST_PROHIB 10 */
195 EV_HURCH, /* ICMP4_UNREACH_TOS_NET 11 */
196 EV_HURCH, /* ICMP4_UNREACH_TOS_HOST 12 */
197 EV_HURCH, /* ICMP4_UNREACH_FILTER_PROHIB 13 */
198 EV_HURCH, /* ICMP4_UNREACH_HOST_PRECEDENCE 14 */
199 EV_HURCH, /* ICMP4_UNREACH_PRECEDENCE_CUTOFF 15 */
200 };
201
202static void
203icmp_unreach (T_NET_BUF *input, uint_t ihoff)
204{
205 T_IP4_HDR *ip4h;
206 uint8_t code;
207 ER error;
208
209 NET_COUNT_MIB(icmp_stats.icmpInDestUnreachs, 1);
210
211 ip4h = (T_IP4_HDR*)GET_ICMP4_SDU(input, ihoff);
212 code = GET_ICMP4_HDR(input, ihoff)->code;
213 error = code2error[code];
214 if (ip4h->proto == IPPROTO_TCP) {
215
216#ifdef SUPPORT_TCP
217
218 memcpy(GET_IP4_HDR(input), ip4h, input->len - (IP4_HDR_SIZE + ICMP4_HDR_SIZE));
219 input->len -= IP4_HDR_SIZE + ICMP4_HDR_SIZE;
220 tcp_notify(input, error);
221
222#endif /* of #ifdef SUPPORT_TCP */
223
224 }
225 else
226 syslog(LOG_NOTICE, "[ICMP] error, code: %d.", code);
227 }
228
229#if NUM_REDIRECT_ROUTE_ENTRY > 0
230
231/*
232 * icmp_redirect -- 向け直しメッセージを受信したときの処理
233 *
234 * input には IF ヘッダと IP ヘッダも含まれている。
235 */
236
237static void
238icmp_redirect (T_NET_BUF *input, uint_t ihoff)
239{
240 T_IP4_HDR *ip4h, *sip4h;
241 T_ICMP4_HDR *icmp4h;
242
243 ip4h = GET_IP4_HDR(input);
244 icmp4h = GET_ICMP4_HDR(input, ihoff);
245 sip4h = (T_IP4_HDR *)GET_ICMP4_SDU(input, ihoff);
246
247 /*
248 * 内容チェック、以下の場合はエラー
249 *
250 * ・ICMP コードが、ICMP4_REDIRECT_PORT_AND_HOST (3) 以上
251 * ・データグラム長が、ICMP の最小長より短い (IP ヘッダ + ICMP ヘッダ + IP ヘッダ + 8)
252 * ・データグラム長が、ICMP の最大長より短い
253 * ・
254 */
255 if (icmp4h->code > ICMP4_REDIRECT_PORT_AND_HOST ||
256 ip4h->len < GET_IP4_ICMP4_HDR_SIZE(input) + 8 + IP4_HDR_SIZE ||
257 ip4h->len < GET_IP4_ICMP4_HDR_SIZE(input) + 8 + (IP4_VHL_HL(sip4h->vhl) << 2)) {
258 NET_COUNT_ICMP4(net_count_icmp4.in_err_packets, 1);
259 return;
260 }
261
262 in4_rtredirect(icmp4h->data.addr, sip4h->dst, IN_RTF_REDIRECT, TMO_IN_REDIRECT);
263 }
264
265#endif /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
266
267/*
268 * icmp_input -- ICMP の入力関数
269 *
270 * input には IF ヘッダと IP ヘッダも含まれている。
271 */
272
273uint_t
274icmp_input (T_NET_BUF **inputp, uint_t *offp, uint_t *nextp)
275{
276 T_NET_BUF *input = *inputp;
277 T_ICMP4_HDR *icmp4h;
278 T_IN4_ADDR addr;
279 uint_t len, align;
280
281 NET_COUNT_ICMP4(net_count_icmp4.in_octets,
282 input->len - GET_IF_IP4_HDR_SIZE(input));
283 NET_COUNT_ICMP4(net_count_icmp4.in_packets, 1);
284
285 /* ICMP ヘッダの長さをチェックする。*/
286 if (input->len < IF_IP4_ICMP4_HDR_SIZE) {
287 NET_COUNT_ICMP4(net_count_icmp4.in_err_packets, 1);
288 NET_COUNT_MIB(icmp_stats.icmpInErrors, 1);
289 goto buf_rel;
290 }
291
292 icmp4h = (T_ICMP4_HDR *)(input->buf + *offp);
293
294 /* 4 オクテット境界のデータ長 */
295 len = input->len - *offp;
296 align = (len + 3) >> 2 << 2;
297
298 /* 4 オクテット境界までパディングで埋める。*/
299 if (align > len)
300 memset((uint8_t*)input->buf + input->len, 0, (size_t)(align - len));
301
302 /* チェックサムを計算する。*/
303 if (in_cksum(icmp4h, align) != 0) {
304 NET_COUNT_ICMP4(net_count_icmp4.in_err_packets, 1);
305 goto buf_rel;
306 }
307
308 /* メッセージの型により分岐する。*/
309 switch (icmp4h->type) {
310 case ICMP4_ECHO_REQUEST:
311 icmp_echo(input, *offp);
312 return IPPROTO_DONE;
313 break;
314 case ICMP4_ECHO_REPLY:
315
316#ifdef ICMP_CFG_CALLBACK_ECHO_REPLY
317
318 icmp_echo_reply(input, *offp);
319
320#endif /* of #ifdef ICMP_CFG_CALLBACK_ECHO_REPLY */
321
322 break;
323 case ICMP4_UNREACH:
324 icmp_unreach(input, *offp);
325 break;
326 case ICMP4_REDIRECT:
327
328#if NUM_REDIRECT_ROUTE_ENTRY > 0
329
330 addr = ntohl(icmp4h->data.addr);
331 syslog(LOG_INFO, "[ICMP] redirect, addr: %s.", ip2str(NULL, &addr));
332 icmp_redirect(input, *offp);
333
334#else /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
335
336 addr = ntohl(icmp4h->data.addr);
337 syslog(LOG_INFO, "[ICMP] redirect ignored, addr: %s.", ip2str(NULL, &addr));
338
339#endif /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
340
341 break;
342 default:
343 syslog(LOG_INFO, "[ICMP] unknown type: %d.", icmp4h->type);
344 NET_COUNT_ICMP4(net_count_icmp4.in_err_packets, 1);
345 break;
346 }
347
348buf_rel:
349 syscall(rel_net_buf(input));
350 return IPPROTO_DONE;
351 }
352
353#ifdef ICMP_REPLY_ERROR
354
355/*
356 * icmp_error -- ICMP エラー送信関数。
357 *
358 * input には IF ヘッダと IP ヘッダも含まれている。
359 * icmp_error では、ネットワークバッファ input を返却しないので、
360 * 呼出し側で、開放してから終了する。
361 */
362
363void
364icmp_error (uint8_t code, T_NET_BUF *input)
365{
366 T_IP4_HDR *ip4h;
367 T_ICMP4_HDR *icmp4h;
368 T_NET_BUF *output;
369 T_IN4_ADDR saddr;
370 uint_t len, ip4hl, align;
371
372 ip4h = GET_IP4_HDR(input);
373 ip4hl = GET_IP4_HDR_SIZE(ip4h);
374
375 /* 送信用の IP データグラムを獲得する。*/
376 if (input->len - ip4hl < 8)
377 len = input->len - ip4hl;
378 else
379 len = 8;
380
381 saddr = ntohl(ip4h->src);
382 if (in4_get_datagram(&output, (uint_t)(ICMP4_HDR_SIZE + ip4hl + len), 0,
383 &saddr, NULL, IPPROTO_ICMP, IP4_DEFTTL,
384 NBA_SEARCH_ASCENT, TMO_ICMP_OUTPUT) != E_OK)
385 return;
386
387 /* ICMP ヘッダを設定する。*/
388 icmp4h = GET_ICMP4_HDR(output, IF_IP4_ICMP4_HDR_OFFSET);
389 icmp4h->type = ICMP4_UNREACH;
390 icmp4h->code = code;
391 icmp4h->data.addr= 0;
392
393 /* エラーが発生した IP ヘッダと データ 8 オクテットをコピーする。*/
394 memcpy(GET_ICMP4_SDU(output, IF_IP4_ICMP4_HDR_OFFSET),
395 GET_IP4_HDR(input), (size_t)(ip4hl + len));
396
397 /* 4 オクテット境界のデータ長 */
398 align = (len + 3) >> 2 << 2;
399
400 /* 4 オクテット境界までパディングで埋める。*/
401 if (align > len)
402 memset((uint8_t*)GET_ICMP4_SDU(output, IF_IP4_ICMP4_HDR_OFFSET) + ip4hl + len,
403 0, (size_t)(align - len));
404
405 /* チェックサムを計算する。*/
406 icmp4h->sum = 0;
407 icmp4h->sum = in_cksum(icmp4h, (uint_t)(ICMP4_HDR_SIZE + ip4hl + align));
408
409 /* 送信する。*/
410 NET_COUNT_ICMP4(net_count_icmp4.out_octets,
411 output->len - GET_IF_IP4_HDR_SIZE(output));
412 NET_COUNT_ICMP4(net_count_icmp4.out_packets, 1);
413 NET_COUNT_MIB(icmp_stats.icmpOutMsgs, 1);
414 NET_COUNT_MIB(icmp_stats.icmpOutDestUnreachs, 1);
415 ip_output(output, TMO_ICMP_OUTPUT);
416 }
417
418#endif /* of #ifdef ICMP_REPLY_ERROR */
Note: See TracBrowser for help on using the repository browser.