source: EcnlProtoTool/trunk/asp3_dcre/tinet/netinet/in4_subr.c@ 429

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

ASP3, TINET, mbed を更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc
File size: 14.5 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$
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
79#include <string.h>
80
81#ifdef TARGET_KERNEL_ASP
82
83#include <kernel.h>
84#include <sil.h>
85#include "kernel_cfg.h"
86
87#endif /* of #ifdef TARGET_KERNEL_ASP */
88
89#ifdef TARGET_KERNEL_JSP
90
91#include <s_services.h>
92#include <t_services.h>
93#include "kernel_id.h"
94
95#endif /* of #ifdef TARGET_KERNEL_JSP */
96
97#include <tinet_defs.h>
98#include <tinet_config.h>
99
100#include <net/if.h>
101#include <net/if_loop.h>
102#include <net/if_ppp.h>
103#include <net/ethernet.h>
104//#include <net/ppp_ipcp.h>
105#include <net/net.h>
106#include <net/net_endian.h>
107#include <net/net_buf.h>
108#include <net/net_timer.h>
109
110#include <netinet/in.h>
111#include <netinet/in_var.h>
112#include <netinet/ip.h>
113#include <netinet/ip_var.h>
114
115#include <net/if_var.h>
116
117#ifdef SUPPORT_IGMP
118#include <netinet/udp_var.h>
119#include <netinet/ip_igmp.h>
120#endif /* of #ifdef SUPPORT_IGMP */
121
122#if defined(_IP4_CFG)
123
124#if NUM_IN4_REDIRECT_ROUTE_ENTRY > 0
125
126/*
127 * in_rtinit -- ルーティング表を初期化する。
128 */
129
130void
131in_rtinit (void)
132{
133 int_t ix;
134
135 for (ix = 0; ix < NUM_IN4_STATIC_ROUTE_ENTRY; ix ++)
136 routing4_tbl[ix].flags = IN_RTF_DEFINED;
137
138 for ( ; ix < NUM_IN4_ROUTE_ENTRY; ix ++)
139 routing4_tbl[ix].flags = 0;
140 }
141
142/*
143 * in_rtnewentry -- 新しいエントリを獲得する。
144 */
145
146T_IN4_RTENTRY *
147in_rtnewentry (uint8_t flags, uint32_t tmo)
148{
149 SYSTIM now;
150 T_IN4_RTENTRY *rt, *frt = NULL;
151 int_t ix;
152
153 /* 空きエントリを探す。*/
154 for (ix = NUM_IN4_STATIC_ROUTE_ENTRY; ix < NUM_IN4_ROUTE_ENTRY; ix ++) {
155 rt = &routing4_tbl[ix];
156 if ((routing4_tbl[ix].flags & IN_RTF_DEFINED) == 0) {
157 frt = rt;
158 break;
159 }
160 }
161
162 /* expire の単位は [s]。*/
163 syscall(get_tim(&now));
164 now /= SYSTIM_HZ;
165
166 if (frt == NULL) {
167 /* 空きがなければ、有効時間がもっとも短いエントリを空きにする。*/
168 T_IN4_RTENTRY *srt = NULL;
169 int_t diff, sdiff = INT_MAX;
170
171 syscall(wai_sem(SEM_IN4_ROUTING_TBL));
172 for (ix = NUM_IN4_STATIC_ROUTE_ENTRY; ix < NUM_IN4_ROUTE_ENTRY; ix ++) {
173 rt = &routing4_tbl[ix];
174 diff = (int_t)(rt->expire - now);
175 if (diff <= 0) { /* rt->expire <= now */
176 /* 既に、有効時間が過ぎている。*/
177 frt = rt;
178 break;
179 }
180 else if (diff < sdiff) {
181 srt = rt;
182 sdiff = diff;
183 }
184 }
185 if (frt == NULL)
186 frt = srt;
187 frt->flags = 0;
188 syscall(sig_sem(SEM_IN4_ROUTING_TBL));
189 }
190
191 frt->flags = (uint8_t)(flags | IN_RTF_DEFINED);
192 frt->expire = now + tmo / SYSTIM_HZ;
193 return frt;
194 }
195
196/*
197 * in_rttimer -- ルーティング表の管理タイマー
198 */
199
200void
201in_rttimer (void)
202{
203 SYSTIM now;
204 int_t ix;
205
206 /* expire の単位は [s]。*/
207 syscall(get_tim(&now));
208 now /= SYSTIM_HZ;
209
210 syscall(wai_sem(SEM_IN4_ROUTING_TBL));
211 for (ix = NUM_IN4_STATIC_ROUTE_ENTRY; ix < NUM_IN4_ROUTE_ENTRY; ix ++)
212 if ((routing4_tbl[ix].flags & IN_RTF_DEFINED) &&
213 (int_t)(routing4_tbl[ix].expire - now) <= 0)
214 routing4_tbl[ix].flags = 0;
215 syscall(sig_sem(SEM_IN4_ROUTING_TBL));
216 }
217
218#endif /* of #if NUM_IN4_REDIRECT_ROUTE_ENTRY > 0 */
219
220/*
221 * in4_get_maxnum_ifaddr -- インタフェースに設定可能な最大アドレス数を返す。
222 */
223
224uint_t
225in4_get_maxnum_ifaddr (void)
226{
227 return 1;
228 }
229
230/*
231 * in4_get_ifaddr -- インタフェースに設定されているアドレスを返す。
232 */
233
234const T_IN4_ADDR *
235in4_get_ifaddr (int_t index)
236{
237 T_IFNET *ifp = IF_GET_IFNET();
238
239 return &ifp->in4_ifaddr.addr;
240 }
241
242/*
243 * in4_set_header -- IPv4 ヘッダを設定する。
244 */
245
246ER
247in4_set_header (T_NET_BUF *nbuf, uint_t len,
248 T_IN4_ADDR *dstaddr, T_IN4_ADDR *srcaddr, uint8_t proto, uint8_t ttl)
249{
250 T_IP4_HDR *ip4h = GET_IP4_HDR(nbuf);
251 T_IFNET *ifp = IF_GET_IFNET();
252
253 /* IP ヘッダを設定する。*/
254 ip4h->vhl = IP4_MAKE_VHL(IPV4_VERSION, IP4_HDR_SIZE >> 2);
255 ip4h->len = htons(IP4_HDR_SIZE + len);
256 ip4h->proto = proto;
257 ip4h->ttl = ttl;
258 ip4h->type = 0;
259 ip4h->id = ip4h->flg_off = ip4h->sum = 0;
260
261 /* IP アドレスを設定する。*/
262 ip4h->dst = htonl(*dstaddr);
263
264 if (srcaddr == NULL || *srcaddr == IPV4_ADDRANY)
265 ip4h->src = htonl(ifp->in4_ifaddr.addr);
266 else
267 ip4h->src = htonl(*srcaddr);
268
269 return E_OK;
270 }
271
272/*
273 * in4_get_datagram -- IPv4 データグラムを獲得し、ヘッダを設定する。
274 */
275
276ER
277in4_get_datagram (T_NET_BUF **nbuf, uint_t len, uint_t maxlen,
278 T_IN4_ADDR *dstaddr, T_IN4_ADDR *srcaddr,
279 uint8_t proto, uint8_t ttl, ATR nbatr, TMO tmout)
280{
281 ER error;
282 uint_t align;
283
284 /* データ長を 4 オクテット境界に調整する。*/
285 align = (len + 3) >> 2 << 2;
286
287 /* ネットワークバッファを獲得する。*/
288 if ((error = tget_net_buf_ex(nbuf, (uint_t)(IF_IP4_HDR_SIZE + align),
289 (uint_t)(IF_IP4_HDR_SIZE + maxlen), nbatr, tmout)) != E_OK)
290 return error;
291
292 /*
293 * より大きなサイズのネットワークバッファを獲得する場合のみ長さを調整する。
294 * より小さなサイズのネットワークバッファの獲得は、送信ウィンドバッファの
295 * 省コピー機能で使用され、実際に送信するまで、データサイズは決定できない。
296 */
297 if ((nbatr & NBA_SEARCH_ASCENT) != 0)
298 (*nbuf)->len = (uint16_t)(IF_IP4_HDR_SIZE + len);
299
300 /* IP ヘッダを設定する。*/
301 if ((error = in4_set_header(*nbuf, len, dstaddr, srcaddr, proto, ttl)) != E_OK) {
302 syscall(rel_net_buf(*nbuf));
303 *nbuf = NULL;
304 return error;
305 }
306
307 /* 4 オクテット境界までパディングで埋める。*/
308 if (align > len)
309 memset((GET_IP4_SDU(*nbuf) + len), 0, (size_t)(align - len));
310
311 return E_OK;
312 }
313
314/*
315 * in4_cksum -- IPv4 のトランスポート層ヘッダのチェックサムを計算する。
316 *
317 * 注意: 戻り値はネットワークバイトオーダ
318 */
319
320uint16_t
321in4_cksum (T_NET_BUF *nbuf, uint8_t proto, uint_t off, uint_t len)
322{
323 uint32_t sum;
324 uint_t align;
325
326 /* 4 オクテット境界のデータ長 */
327 align = (len + 3) >> 2 << 2;
328
329 /* 4 オクテット境界までパディングで埋める。*/
330 if (align > len)
331 memset((uint8_t*)nbuf->buf + off + len, 0, (size_t)(align - len));
332
333 sum = in_cksum_sum(nbuf->buf + off, align)
334 + in_cksum_sum(&GET_IP4_HDR(nbuf)->src, sizeof(T_IN4_ADDR) * 2)
335 + len + proto;
336 sum = in_cksum_carry(sum);
337
338 return (uint16_t)(~htons((uint16_t)sum));
339 }
340
341/*
342 * in_cksum -- チェックサム計算関数、IPv4、ICMPv4 用
343 *
344 * 注意: data は 4 オクテット単位でパディングすること。
345 * data が 2 オクテット単位にアラインされていないと
346 * 例外が発生する可能性がある。
347 * len は 4 オクテット単位にアラインされていること。
348 *
349 * 戻り値はネットワークバイトオーダ
350 */
351
352uint16_t
353in_cksum (void *data, uint_t len /*オクテット単位*/)
354{
355 uint16_t sum;
356
357 sum = (uint16_t)in_cksum_carry(in_cksum_sum(data, len));
358 return (uint16_t)(~htons(sum));
359 }
360
361/*
362 * in4_is_dstaddr_accept -- 宛å…
363ˆã‚¢ãƒ‰ãƒ¬ã‚¹ã¨ã—て正しいかチェックする。
364 *
365 * 注意: dstaddr は、
366 * TINET-1.2 からネットワークバイトオーダ、
367 * TINET-1.1 までは、ホストバイトオーダ
368 */
369
370bool_t
371in4_is_dstaddr_accept (T_IN4_ADDR *myaddr, T_IN4_ADDR *dstaddr)
372{
373 if (*myaddr == IPV4_ADDRANY)
374 return ntohl(*dstaddr) == IF_GET_IFNET()->in4_ifaddr.addr;
375 else
376 return ntohl(*dstaddr) == *myaddr;
377 }
378
379/*
380 * inn4_is_dstaddr_accept -- 宛å…
381ˆã‚¢ãƒ‰ãƒ¬ã‚¹ã¨ã—て正しいかチェックする。
382 *
383 * 注意: dstaddr は、
384 * TINET-1.2 からネットワークバイトオーダ、
385 * TINET-1.1 までは、ホストバイトオーダ
386 */
387
388bool_t
389inn4_is_dstaddr_accept (T_IN4_ADDR *myaddr, T_NET_BUF *nbuf)
390{
391 T_IP4_HDR *iph;
392
393 iph = GET_IP4_HDR(nbuf);
394 if (*myaddr == IPV4_ADDRANY)
395 return ntohl(iph->dst) == IF_GET_IFNET()->in4_ifaddr.addr;
396 else
397 return ntohl(iph->dst) == *myaddr;
398 }
399
400/*
401 * in4_addrwithifp -- 宛å…
402ˆã‚¢ãƒ‰ãƒ¬ã‚¹ã«ãµã•ã‚ã—い送信å…
403ƒã‚¢ãƒ‰ãƒ¬ã‚¹ã‚’、
404 * ネットワークインタフェースから探索する。
405 * in6_addrwithifp をシミュレートするだけで、
406 * エラーを返すことはない。
407 */
408
409T_IN4_ADDR *
410in4_addrwithifp (T_IFNET *ifp, T_IN4_ADDR *src, T_IN4_ADDR *dst)
411{
412 *src = ifp->in4_ifaddr.addr;
413 return src;
414 }
415
416/*
417 * in4_add_ifaddr -- インタフェースに IPv4 アドレスを設定する。
418 */
419
420ER
421in4_add_ifaddr (T_IN4_ADDR addr, T_IN4_ADDR mask)
422{
423 T_IFNET *ifp = IF_GET_IFNET();
424
425 ifp->in4_ifaddr.addr = addr;
426 ifp->in4_ifaddr.mask = mask;
427 return E_OK;
428 }
429
430#if NUM_IN4_ROUTE_ENTRY > 0
431
432/*
433 * in4_add_route -- 経路表にエントリを設定する。
434 */
435
436ER
437in4_add_route (int_t index, T_IN4_ADDR target, T_IN4_ADDR mask, T_IN4_ADDR gateway)
438{
439
440 if (0 <= index && index < NUM_IN4_STATIC_ROUTE_ENTRY) {
441 routing4_tbl[index].target = target;
442 routing4_tbl[index].mask = mask;
443 routing4_tbl[index].gateway = gateway;
444 return E_OK;
445 }
446 else
447 return E_PAR;
448 }
449
450#endif /* of #if NUM_IN4_ROUTE_ENTRY > 0 */
451
452/*
453 * in4_rtalloc -- ルーティング表を探索する。
454 */
455
456T_IN4_ADDR
457in4_rtalloc (T_IN4_ADDR dst)
458{
459 int_t ix;
460#ifdef ETHER_CFG_MULTICAST
461 if(IN4_IS_ADDR_MULTICAST(dst))
462 return dst;
463#endif /* of #ifdef ETHER_CFG_MULTICAST */
464 for (ix = NUM_IN4_ROUTE_ENTRY; ix --; )
465 if ((routing4_tbl[ix].flags & IN_RTF_DEFINED) &&
466 (dst & routing4_tbl[ix].mask) == routing4_tbl[ix].target) {
467 if (routing4_tbl[ix].gateway == 0)
468 return dst;
469 else {
470 return routing4_tbl[ix].gateway;
471 }
472 }
473 return dst;
474 }
475
476#if NUM_IN4_REDIRECT_ROUTE_ENTRY > 0
477
478/*
479 * in4_rtredirect -- ルーティング表にエントリを登録する。
480 *
481 * 注意: 引数 tmo の単位は [ms]。
482 */
483
484void
485in4_rtredirect (T_IN4_ADDR gateway, T_IN4_ADDR target, uint8_t flags, uint32_t tmo)
486{
487 T_IN4_RTENTRY *frt;
488
489 frt = in_rtnewentry(flags, tmo);
490 frt->gateway = gateway;
491 frt->target = target;
492 frt->mask = 0xffffffff;
493 }
494
495#endif /* of #if NUM_IN4_REDIRECT_ROUTE_ENTRY > 0 */
496
497/*
498 * in4_timer -- IPv4 å…
499±é€šã‚¿ã‚¤ãƒžãƒ¼
500 *
501 * 1秒周期で起動される。
502 */
503
504static void
505in4_timer (void *ignore)
506{
507#if NUM_IN4_REDIRECT_ROUTE_ENTRY > 0
508
509 in_rttimer();
510
511#endif /* of #if NUM_IN4_REDIRECT_ROUTE_ENTRY > 0 */
512
513#ifdef IP4_CFG_FRAGMENT
514
515 ip_frag_timer();
516
517#endif /* of #ifdef IP4_CFG_FRAGMENT */
518
519 timeout(in4_timer, NULL, IN_TIMER_TMO);
520 }
521
522/*
523 * in4_init -- IPv4 å…
524±é€šæ©Ÿèƒ½ã‚’初期化する。
525 */
526
527void
528in4_init (void)
529{
530#if NUM_IN4_REDIRECT_ROUTE_ENTRY > 0
531
532 in_rtinit();
533
534#endif /* of #if NUM_IN4_REDIRECT_ROUTE_ENTRY > 0 */
535
536#ifdef SUPPORT_IGMP
537 igmp_init();
538#endif /* of #ifdef SUPPORT_IGMP */
539
540 timeout(in4_timer, NULL, IN_TIMER_TMO);
541 }
542
543#endif /* of #if defined(_IP4_CFG) */
Note: See TracBrowser for help on using the repository browser.