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

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

prototoolに関連するプロジェクトをnewlibからmuslを使うよう変更・更新
ntshellをnewlibの下位の実装から、muslのsyscallの実装に変更・更新
以下のOSSをアップデート
・mruby-1.3.0
・musl-1.1.18
・onigmo-6.1.3
・tcc-0.9.27
以下のOSSを追加
・openssl-1.1.0e
・curl-7.57.0
・zlib-1.2.11
以下のmrbgemsを追加
・iij/mruby-digest
・iij/mruby-env
・iij/mruby-errno
・iij/mruby-iijson
・iij/mruby-ipaddr
・iij/mruby-mock
・iij/mruby-require
・iij/mruby-tls-openssl

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