[270] | 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: in_subr.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 |
|
---|
| 79 | #include <string.h>
|
---|
| 80 |
|
---|
| 81 | #ifdef TARGET_KERNEL_ASP
|
---|
| 82 |
|
---|
| 83 | #include <kernel.h>
|
---|
| 84 | #include <sil.h>
|
---|
| 85 | #include <t_syslog.h>
|
---|
| 86 | #include "kernel_cfg.h"
|
---|
| 87 |
|
---|
| 88 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
| 89 |
|
---|
| 90 | #ifdef TARGET_KERNEL_JSP
|
---|
| 91 |
|
---|
| 92 | #include <s_services.h>
|
---|
| 93 | #include <t_services.h>
|
---|
| 94 | #include "kernel_id.h"
|
---|
| 95 |
|
---|
| 96 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
| 97 |
|
---|
| 98 | #include <tinet_defs.h>
|
---|
| 99 | #include <tinet_config.h>
|
---|
| 100 |
|
---|
| 101 | #include <net/if.h>
|
---|
| 102 | #include <net/if_loop.h>
|
---|
| 103 | #include <net/if_ppp.h>
|
---|
| 104 | #include <net/ethernet.h>
|
---|
| 105 | #include <net/ppp_ipcp.h>
|
---|
| 106 | #include <net/net.h>
|
---|
| 107 | #include <net/net_var.h>
|
---|
| 108 | #include <net/net_buf.h>
|
---|
| 109 | #include <net/net_timer.h>
|
---|
| 110 |
|
---|
| 111 | #include <netinet/in.h>
|
---|
| 112 | #include <netinet6/in6.h>
|
---|
| 113 | #include <netinet/in_var.h>
|
---|
| 114 | #include <netinet/in_itron.h>
|
---|
| 115 | #include <netinet/ip.h>
|
---|
| 116 | #include <netinet/ip_var.h>
|
---|
| 117 | #include <netinet/tcp_timer.h>
|
---|
| 118 |
|
---|
| 119 | #include <net/if_var.h>
|
---|
| 120 |
|
---|
| 121 | #if defined(SUPPORT_INET4)
|
---|
| 122 |
|
---|
| 123 | /*
|
---|
| 124 | * in4_get_ifaddr -- ã¤ã³ã¿ãã§ã¼ã¹ã«è¨å®ããã¦ããã¢ãã¬ã¹ãè¿ãã
|
---|
| 125 | */
|
---|
| 126 |
|
---|
| 127 | const T_IN4_ADDR *
|
---|
| 128 | in4_get_ifaddr (int_t index)
|
---|
| 129 | {
|
---|
| 130 | T_IFNET *ifp = IF_GET_IFNET();
|
---|
| 131 |
|
---|
| 132 | return &ifp->in_ifaddr.addr;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /*
|
---|
| 136 | * ip2str -- IPv4 ã¢ãã¬ã¹ãæååã«å¤æããã
|
---|
| 137 | */
|
---|
| 138 |
|
---|
| 139 | char *
|
---|
| 140 | ip2str (char *buf, const T_IN4_ADDR *ipaddr)
|
---|
| 141 | {
|
---|
| 142 | static char addr_sbuf[NUM_IPV4ADDR_STR_BUFF][sizeof("123.123.123.123")];
|
---|
| 143 | static int_t bix = NUM_IPV4ADDR_STR_BUFF;
|
---|
| 144 |
|
---|
| 145 | char *start;
|
---|
| 146 |
|
---|
| 147 | if (buf == NULL) {
|
---|
| 148 | syscall(wai_sem(SEM_IP2STR_BUFF_LOCK));
|
---|
| 149 | buf = addr_sbuf[-- bix];
|
---|
| 150 | if (bix <= 0)
|
---|
| 151 | bix = NUM_IPV4ADDR_STR_BUFF;
|
---|
| 152 | syscall(sig_sem(SEM_IP2STR_BUFF_LOCK));
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | start = buf;
|
---|
| 156 | buf += convert_hexdigit(buf, (uint_t)((*ipaddr >> 24) & 0xff), 10, 0, ' ');
|
---|
| 157 | *(buf ++) = '.';
|
---|
| 158 | buf += convert_hexdigit(buf, (uint_t)((*ipaddr >> 16) & 0xff), 10, 0, ' ');
|
---|
| 159 | *(buf ++) = '.';
|
---|
| 160 | buf += convert_hexdigit(buf, (uint_t)((*ipaddr >> 8) & 0xff), 10, 0, ' ');
|
---|
| 161 | *(buf ++) = '.';
|
---|
| 162 | buf += convert_hexdigit(buf, (uint_t)((*ipaddr ) & 0xff), 10, 0, ' ');
|
---|
| 163 | *buf = '\0';
|
---|
| 164 | return start;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /*
|
---|
| 168 | * in4_set_header -- IPv4 ããããè¨å®ããã
|
---|
| 169 | */
|
---|
| 170 |
|
---|
| 171 | ER
|
---|
| 172 | in4_set_header (T_NET_BUF *nbuf, uint_t len,
|
---|
| 173 | T_IN4_ADDR *dstaddr, T_IN4_ADDR *srcaddr, uint8_t proto, uint8_t ttl)
|
---|
| 174 | {
|
---|
| 175 | T_IP4_HDR *ip4h = GET_IP4_HDR(nbuf);
|
---|
| 176 | T_IFNET *ifp = IF_GET_IFNET();
|
---|
| 177 |
|
---|
| 178 | /* IP ããããè¨å®ããã*/
|
---|
| 179 | ip4h->vhl = IP4_MAKE_VHL(IPV4_VERSION, IP4_HDR_SIZE >> 2);
|
---|
| 180 | ip4h->len = htons(IP4_HDR_SIZE + len);
|
---|
| 181 | ip4h->proto = proto;
|
---|
| 182 | ip4h->ttl = ttl;
|
---|
| 183 | ip4h->type = 0;
|
---|
| 184 | ip4h->id = ip4h->flg_off = ip4h->sum = 0;
|
---|
| 185 |
|
---|
| 186 | /* IP ã¢ãã¬ã¹ãè¨å®ããã*/
|
---|
| 187 | ip4h->dst = htonl(*dstaddr);
|
---|
| 188 |
|
---|
| 189 | if (srcaddr == NULL || *srcaddr == IPV4_ADDRANY)
|
---|
| 190 | ip4h->src = htonl(ifp->in_ifaddr.addr);
|
---|
| 191 | else
|
---|
| 192 | ip4h->src = htonl(*srcaddr);
|
---|
| 193 |
|
---|
| 194 | return E_OK;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | /*
|
---|
| 198 | * in4_get_datagram -- IPv4 ãã¼ã¿ã°ã©ã ãç²å¾ããããããè¨å®ããã
|
---|
| 199 | */
|
---|
| 200 |
|
---|
| 201 | ER
|
---|
| 202 | in4_get_datagram (T_NET_BUF **nbuf, uint_t len, uint_t maxlen,
|
---|
| 203 | T_IN4_ADDR *dstaddr, T_IN4_ADDR *srcaddr,
|
---|
| 204 | uint8_t proto, uint8_t ttl, ATR nbatr, TMO tmout)
|
---|
| 205 | {
|
---|
| 206 | ER error;
|
---|
| 207 | uint_t align;
|
---|
| 208 |
|
---|
| 209 | /* ãã¼ã¿é·ã 4 ãªã¯ãããå¢çã«èª¿æ´ããã*/
|
---|
| 210 | align = (len + 3) >> 2 << 2;
|
---|
| 211 |
|
---|
| 212 | /* ãããã¯ã¼ã¯ãããã¡ãç²å¾ããã*/
|
---|
| 213 | if ((error = tget_net_buf_ex(nbuf, (uint_t)(IF_IP4_HDR_SIZE + align),
|
---|
| 214 | (uint_t)(IF_IP4_HDR_SIZE + maxlen), nbatr, tmout)) != E_OK)
|
---|
| 215 | return error;
|
---|
| 216 |
|
---|
| 217 | /*
|
---|
| 218 | * ãã大ããªãµã¤ãºã®ãããã¯ã¼ã¯ãããã¡ãç²å¾ããå ´åã®ã¿é·ãã調æ´ããã
|
---|
| 219 | * ããå°ããªãµã¤ãºã®ãããã¯ã¼ã¯ãããã¡ã®ç²å¾ã¯ãéä¿¡ã¦ã£ã³ããããã¡ã®
|
---|
| 220 | * çã³ãã¼æ©è½ã§ä½¿ç¨ãããå®éã«éä¿¡ããã¾ã§ããã¼ã¿ãµã¤ãºã¯æ±ºå®ã§ããªãã
|
---|
| 221 | */
|
---|
| 222 | if ((nbatr & NBA_SEARCH_ASCENT) != 0)
|
---|
| 223 | (*nbuf)->len = (uint16_t)(IF_IP4_HDR_SIZE + len);
|
---|
| 224 |
|
---|
| 225 | /* IP ããããè¨å®ããã*/
|
---|
| 226 | if ((error = in4_set_header(*nbuf, len, dstaddr, srcaddr, proto, ttl)) != E_OK)
|
---|
| 227 | return error;
|
---|
| 228 |
|
---|
| 229 | /* 4 ãªã¯ãããå¢çã¾ã§ããã£ã³ã°ã§åããã*/
|
---|
| 230 | if (align > len)
|
---|
| 231 | memset((GET_IP4_SDU(*nbuf) + len), 0, (size_t)(align - len));
|
---|
| 232 |
|
---|
| 233 | return E_OK;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /*
|
---|
| 237 | * in4_cksum -- IPv4 ã®ãã©ã³ã¹ãã¼ã層ãããã®ãã§ãã¯ãµã ãè¨ç®ããã
|
---|
| 238 | *
|
---|
| 239 | * 注æ: æ»ãå¤ã¯ãããã¯ã¼ã¯ãã¤ããªã¼ã
|
---|
| 240 | */
|
---|
| 241 |
|
---|
| 242 | uint16_t
|
---|
| 243 | in4_cksum (T_NET_BUF *nbuf, uint8_t proto, uint_t off, uint_t len)
|
---|
| 244 | {
|
---|
| 245 | uint32_t sum;
|
---|
| 246 | uint_t align;
|
---|
| 247 |
|
---|
| 248 | /* 4 ãªã¯ãããå¢çã®ãã¼ã¿é· */
|
---|
| 249 | align = (len + 3) >> 2 << 2;
|
---|
| 250 |
|
---|
| 251 | /* 4 ãªã¯ãããå¢çã¾ã§ããã£ã³ã°ã§åããã*/
|
---|
| 252 | if (align > len)
|
---|
| 253 | memset((uint8_t*)nbuf->buf + off + len, 0, (size_t)(align - len));
|
---|
| 254 |
|
---|
| 255 | sum = in_cksum_sum(nbuf->buf + off, align)
|
---|
| 256 | + in_cksum_sum(&GET_IP4_HDR(nbuf)->src, sizeof(T_IN4_ADDR) * 2)
|
---|
| 257 | + len + proto;
|
---|
| 258 | sum = in_cksum_carry(sum);
|
---|
| 259 |
|
---|
| 260 | return (uint16_t)(~htons((uint16_t)sum));
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | /*
|
---|
| 264 | * in_cksum -- ãã§ãã¯ãµã è¨ç®é¢æ°ãIPv4ãICMPv4 ç¨
|
---|
| 265 | *
|
---|
| 266 | * 注æ: data 㯠4 ãªã¯ãããåä½ã§ããã£ã³ã°ãããã¨ã
|
---|
| 267 | * data ã 2 ãªã¯ãããåä½ã«ã¢ã©ã¤ã³ããã¦ããªãã¨
|
---|
| 268 | * ä¾å¤ãçºçããå¯è½æ§ãããã
|
---|
| 269 | * len 㯠4 ãªã¯ãããåä½ã«ã¢ã©ã¤ã³ããã¦ãããã¨ã
|
---|
| 270 | *
|
---|
| 271 | * æ»ãå¤ã¯ãããã¯ã¼ã¯ãã¤ããªã¼ã
|
---|
| 272 | */
|
---|
| 273 |
|
---|
| 274 | uint16_t
|
---|
| 275 | in_cksum (void *data, uint_t len /*ãªã¯ãããåä½*/)
|
---|
| 276 | {
|
---|
| 277 | uint16_t sum;
|
---|
| 278 |
|
---|
| 279 | sum = (uint16_t)in_cksum_carry(in_cksum_sum(data, len));
|
---|
| 280 | return (uint16_t)(~htons(sum));
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | /*
|
---|
| 284 | * in4_is_dstaddr_accept -- å®å
|
---|
| 285 | ã¢ãã¬ã¹ã¨ãã¦æ£ããããã§ãã¯ããã
|
---|
| 286 | *
|
---|
| 287 | * 注æ: dstaddr ã¯ã
|
---|
| 288 | * TINET-1.2 ãããããã¯ã¼ã¯ãã¤ããªã¼ãã
|
---|
| 289 | * TINET-1.1 ã¾ã§ã¯ããã¹ããã¤ããªã¼ã
|
---|
| 290 | */
|
---|
| 291 |
|
---|
| 292 | bool_t
|
---|
| 293 | in4_is_dstaddr_accept (T_IN4_ADDR *myaddr, T_IN4_ADDR *dstaddr)
|
---|
| 294 | {
|
---|
| 295 | if (*myaddr == IPV4_ADDRANY)
|
---|
| 296 | return ntohl(*dstaddr) == IF_GET_IFNET()->in_ifaddr.addr;
|
---|
| 297 | else
|
---|
| 298 | return ntohl(*dstaddr) == *myaddr;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | /*
|
---|
| 302 | * in4_ifawithifp -- å®å
|
---|
| 303 | ã¢ãã¬ã¹ã«ãµããããéä¿¡å
|
---|
| 304 | ã¢ãã¬ã¹ãã
|
---|
| 305 | * ãããã¯ã¼ã¯ã¤ã³ã¿ãã§ã¼ã¹ããæ¢ç´¢ããã
|
---|
| 306 | * in6_ifawithifp ãã·ãã¥ã¬ã¼ãããã ãã§ã
|
---|
| 307 | * ã¨ã©ã¼ãè¿ããã¨ã¯ãªãã
|
---|
| 308 | */
|
---|
| 309 |
|
---|
| 310 | T_IN4_IFADDR *
|
---|
| 311 | in4_ifawithifp (T_IFNET *ifp, T_IN4_ADDR *dst)
|
---|
| 312 | {
|
---|
| 313 | return &ifp->in_ifaddr;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | /*
|
---|
| 317 | * in4_add_ifaddr -- ã¤ã³ã¿ãã§ã¼ã¹ã« IPv4 ã¢ãã¬ã¹ãè¨å®ããã
|
---|
| 318 | */
|
---|
| 319 |
|
---|
| 320 | ER
|
---|
| 321 | in4_add_ifaddr (T_IN4_ADDR addr, T_IN4_ADDR mask)
|
---|
| 322 | {
|
---|
| 323 | T_IFNET *ifp = IF_GET_IFNET();
|
---|
| 324 |
|
---|
| 325 | ifp->in_ifaddr.addr = addr;
|
---|
| 326 | ifp->in_ifaddr.mask = mask;
|
---|
| 327 | return E_OK;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | #if NUM_ROUTE_ENTRY > 0
|
---|
| 331 |
|
---|
| 332 | /*
|
---|
| 333 | * in4_add_route -- çµè·¯è¡¨ã«ã¨ã³ããªãè¨å®ããã
|
---|
| 334 | */
|
---|
| 335 |
|
---|
| 336 | ER
|
---|
| 337 | in4_add_route (int_t index, T_IN4_ADDR target, T_IN4_ADDR mask, T_IN4_ADDR gateway)
|
---|
| 338 | {
|
---|
| 339 |
|
---|
| 340 | if (0 <= index && index < NUM_STATIC_ROUTE_ENTRY) {
|
---|
| 341 | routing_tbl[index].target = target;
|
---|
| 342 | routing_tbl[index].mask = mask;
|
---|
| 343 | routing_tbl[index].gateway = gateway;
|
---|
| 344 | return E_OK;
|
---|
| 345 | }
|
---|
| 346 | else
|
---|
| 347 | return E_PAR;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | #endif /* of #if NUM_ROUTE_ENTRY > 0 */
|
---|
| 351 |
|
---|
| 352 | /*
|
---|
| 353 | * in4_rtalloc -- ã«ã¼ãã£ã³ã°è¡¨ãæ¢ç´¢ããã
|
---|
| 354 | */
|
---|
| 355 |
|
---|
| 356 | T_IN4_ADDR
|
---|
| 357 | in4_rtalloc (T_IN4_ADDR dst)
|
---|
| 358 | {
|
---|
| 359 | int_t ix;
|
---|
| 360 |
|
---|
| 361 | for (ix = NUM_ROUTE_ENTRY; ix --; )
|
---|
| 362 | if ((routing_tbl[ix].flags & IN_RTF_DEFINED) &&
|
---|
| 363 | (dst & routing_tbl[ix].mask) == routing_tbl[ix].target) {
|
---|
| 364 | if (routing_tbl[ix].gateway == 0)
|
---|
| 365 | return dst;
|
---|
| 366 | else {
|
---|
| 367 | return routing_tbl[ix].gateway;
|
---|
| 368 | }
|
---|
| 369 | }
|
---|
| 370 | return dst;
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | #if NUM_REDIRECT_ROUTE_ENTRY > 0
|
---|
| 374 |
|
---|
| 375 | /*
|
---|
| 376 | * in4_rtredirect -- ã«ã¼ãã£ã³ã°è¡¨ã«ã¨ã³ããªãç»é²ããã
|
---|
| 377 | *
|
---|
| 378 | * 注æ: å¼æ° tmo ã®åä½ã¯ [ms]ã
|
---|
| 379 | */
|
---|
| 380 |
|
---|
| 381 | void
|
---|
| 382 | in4_rtredirect (T_IN4_ADDR gateway, T_IN4_ADDR target, uint8_t flags, uint32_t tmo)
|
---|
| 383 | {
|
---|
| 384 | T_IN_RTENTRY *frt;
|
---|
| 385 |
|
---|
| 386 | frt = in_rtnewentry(flags, tmo);
|
---|
| 387 | frt->gateway = gateway;
|
---|
| 388 | frt->target = target;
|
---|
| 389 | frt->mask = 0xffffffff;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | #endif /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
|
---|
| 393 |
|
---|
| 394 | /*
|
---|
| 395 | * in4_timer -- IPv4 å
|
---|
| 396 | ±éã¿ã¤ãã¼
|
---|
| 397 | *
|
---|
| 398 | * 1ç§å¨æã§èµ·åãããã
|
---|
| 399 | */
|
---|
| 400 |
|
---|
| 401 | static void
|
---|
| 402 | in4_timer (void *ignore)
|
---|
| 403 | {
|
---|
| 404 | #if NUM_REDIRECT_ROUTE_ENTRY > 0
|
---|
| 405 |
|
---|
| 406 | in_rttimer();
|
---|
| 407 |
|
---|
| 408 | #endif /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
|
---|
| 409 |
|
---|
| 410 | #ifdef IP4_CFG_FRAGMENT
|
---|
| 411 |
|
---|
| 412 | ip_frag_timer();
|
---|
| 413 |
|
---|
| 414 | #endif /* of #ifdef IP4_CFG_FRAGMENT */
|
---|
| 415 |
|
---|
| 416 | timeout(in4_timer, NULL, IN_TIMER_TMO);
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | /*
|
---|
| 420 | * in4_init -- IPv4 å
|
---|
| 421 | ±éæ©è½ãåæåããã
|
---|
| 422 | */
|
---|
| 423 |
|
---|
| 424 | void
|
---|
| 425 | in4_init (void)
|
---|
| 426 | {
|
---|
| 427 | #if NUM_REDIRECT_ROUTE_ENTRY > 0
|
---|
| 428 |
|
---|
| 429 | in_rtinit();
|
---|
| 430 |
|
---|
| 431 | #endif /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
|
---|
| 432 |
|
---|
| 433 | timeout(in4_timer, NULL, IN_TIMER_TMO);
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
| 437 |
|
---|
| 438 | #if NUM_REDIRECT_ROUTE_ENTRY > 0
|
---|
| 439 |
|
---|
| 440 | /*
|
---|
| 441 | * in_rtinit -- ã«ã¼ãã£ã³ã°è¡¨ãåæåããã
|
---|
| 442 | */
|
---|
| 443 |
|
---|
| 444 | void
|
---|
| 445 | in_rtinit (void)
|
---|
| 446 | {
|
---|
| 447 | int_t ix;
|
---|
| 448 |
|
---|
| 449 | for (ix = 0; ix < NUM_STATIC_ROUTE_ENTRY; ix ++)
|
---|
| 450 | routing_tbl[ix].flags = IN_RTF_DEFINED;
|
---|
| 451 |
|
---|
| 452 | for ( ; ix < NUM_ROUTE_ENTRY; ix ++)
|
---|
| 453 | routing_tbl[ix].flags = 0;
|
---|
| 454 | }
|
---|
| 455 |
|
---|
| 456 | /*
|
---|
| 457 | * in_rtnewentry -- æ°ããã¨ã³ããªãç²å¾ããã
|
---|
| 458 | */
|
---|
| 459 |
|
---|
| 460 | T_IN_RTENTRY *
|
---|
| 461 | in_rtnewentry (uint8_t flags, uint32_t tmo)
|
---|
| 462 | {
|
---|
| 463 | SYSTIM now;
|
---|
| 464 | T_IN_RTENTRY *rt, *frt = NULL;
|
---|
| 465 | int_t ix;
|
---|
| 466 |
|
---|
| 467 | /* 空ãã¨ã³ããªãæ¢ãã*/
|
---|
| 468 | for (ix = NUM_STATIC_ROUTE_ENTRY; ix < NUM_ROUTE_ENTRY; ix ++) {
|
---|
| 469 | rt = &routing_tbl[ix];
|
---|
| 470 | if ((routing_tbl[ix].flags & IN_RTF_DEFINED) == 0) {
|
---|
| 471 | frt = rt;
|
---|
| 472 | break;
|
---|
| 473 | }
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | /* expire ã®åä½ã¯ [s]ã*/
|
---|
| 477 | syscall(get_tim(&now));
|
---|
| 478 | now /= SYSTIM_HZ;
|
---|
| 479 |
|
---|
| 480 | if (frt == NULL) {
|
---|
| 481 | /* 空ãããªããã°ãæå¹æéããã£ã¨ãçãã¨ã³ããªã空ãã«ããã*/
|
---|
| 482 | T_IN_RTENTRY *srt = NULL;
|
---|
| 483 | int_t diff, sdiff = INT_MAX;
|
---|
| 484 |
|
---|
| 485 | syscall(wai_sem(SEM_IN_ROUTING_TBL));
|
---|
| 486 | for (ix = NUM_STATIC_ROUTE_ENTRY; ix < NUM_ROUTE_ENTRY; ix ++) {
|
---|
| 487 | rt = &routing_tbl[ix];
|
---|
| 488 | diff = (int_t)(rt->expire - now);
|
---|
| 489 | if (diff <= 0) { /* rt->expire <= now */
|
---|
| 490 | /* æ¢ã«ãæå¹æéãéãã¦ããã*/
|
---|
| 491 | frt = rt;
|
---|
| 492 | break;
|
---|
| 493 | }
|
---|
| 494 | else if (diff < sdiff) {
|
---|
| 495 | srt = rt;
|
---|
| 496 | sdiff = diff;
|
---|
| 497 | }
|
---|
| 498 | }
|
---|
| 499 | if (frt == NULL)
|
---|
| 500 | frt = srt;
|
---|
| 501 | frt->flags = 0;
|
---|
| 502 | syscall(sig_sem(SEM_IN_ROUTING_TBL));
|
---|
| 503 | }
|
---|
| 504 |
|
---|
| 505 | frt->flags = (uint8_t)(flags | IN_RTF_DEFINED);
|
---|
| 506 | frt->expire = now + tmo / SYSTIM_HZ;
|
---|
| 507 | return frt;
|
---|
| 508 | }
|
---|
| 509 |
|
---|
| 510 | /*
|
---|
| 511 | * in_rttimer -- ã«ã¼ãã£ã³ã°è¡¨ã®ç®¡çã¿ã¤ãã¼
|
---|
| 512 | */
|
---|
| 513 |
|
---|
| 514 | void
|
---|
| 515 | in_rttimer (void)
|
---|
| 516 | {
|
---|
| 517 | SYSTIM now;
|
---|
| 518 | int_t ix;
|
---|
| 519 |
|
---|
| 520 | /* expire ã®åä½ã¯ [s]ã*/
|
---|
| 521 | syscall(get_tim(&now));
|
---|
| 522 | now /= SYSTIM_HZ;
|
---|
| 523 |
|
---|
| 524 | syscall(wai_sem(SEM_IN_ROUTING_TBL));
|
---|
| 525 | for (ix = NUM_STATIC_ROUTE_ENTRY; ix < NUM_ROUTE_ENTRY; ix ++)
|
---|
| 526 | if ((routing_tbl[ix].flags & IN_RTF_DEFINED) &&
|
---|
| 527 | (int_t)(routing_tbl[ix].expire - now) <= 0)
|
---|
| 528 | routing_tbl[ix].flags = 0;
|
---|
| 529 | syscall(sig_sem(SEM_IN_ROUTING_TBL));
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | #endif /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
|
---|
| 533 |
|
---|
| 534 | /*
|
---|
| 535 | * in_cksum_sum -- ãã§ãã¯ãµã ã®åè¨è¨ç®é¢æ°
|
---|
| 536 | *
|
---|
| 537 | * 注æ: data 㯠4 ãªã¯ãããåä½ã§ããã£ã³ã°ãããã¨ã
|
---|
| 538 | * data ã 2 ãªã¯ãããåä½ã«ã¢ã©ã¤ã³ããã¦ããªãã¨
|
---|
| 539 | * ä¾å¤ãçºçããå¯è½æ§ãããã
|
---|
| 540 | * len 㯠4 ãªã¯ãããåä½ã«ã¢ã©ã¤ã³ããã¦ãããã¨ã
|
---|
| 541 | *
|
---|
| 542 | * æ»ãå¤ã¯ãã¹ããã¤ããªã¼ã
|
---|
| 543 | */
|
---|
| 544 |
|
---|
| 545 | uint32_t
|
---|
| 546 | in_cksum_sum (void *data, uint_t len /*ãªã¯ãããåä½*/)
|
---|
| 547 | {
|
---|
| 548 | uint32_t sum = 0;
|
---|
| 549 |
|
---|
| 550 | for ( ; len > 0; len -= 2) {
|
---|
| 551 | sum += *((uint16_t*)data);
|
---|
| 552 | data = (uint8_t*)data + 2;
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | #if _NET_CFG_BYTE_ORDER == _NET_CFG_BIG_ENDIAN
|
---|
| 556 | return sum;
|
---|
| 557 | #elif _NET_CFG_BYTE_ORDER == _NET_CFG_LITTLE_ENDIAN
|
---|
| 558 | return ((sum >> 8) & 0xffff) + ((sum & 0xff) << 8) + ((sum >> 24) & 0xff);
|
---|
| 559 | #endif /* #if _NET_CFG_BYTE_ORDER == _NET_CFG_BIG_ENDIAN */
|
---|
| 560 |
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 | /*
|
---|
| 564 | * in_cksum_carry -- ãã§ãã¯ãµã ã®æ¡ä¸ãè¨ç®é¢æ°
|
---|
| 565 | *
|
---|
| 566 | * 注æ: data 㯠4 ãªã¯ãããåä½ã§ããã£ã³ã°ãããã¨ã
|
---|
| 567 | * data ã 2 ãªã¯ãããåä½ã«ã¢ã©ã¤ã³ããã¦ããªãã¨
|
---|
| 568 | * ä¾å¤ãçºçããå¯è½æ§ãããã
|
---|
| 569 | * len 㯠4 ãªã¯ãããåä½ã«ã¢ã©ã¤ã³ããã¦ãããã¨ã
|
---|
| 570 | */
|
---|
| 571 |
|
---|
| 572 | uint32_t
|
---|
| 573 | in_cksum_carry (uint32_t sum)
|
---|
| 574 | {
|
---|
| 575 | uint32_t carry;
|
---|
| 576 |
|
---|
| 577 | while (sum >= 0x00010000) {
|
---|
| 578 | carry = sum >> 16;
|
---|
| 579 | sum = (sum & 0xffff) + carry;
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 | return sum;
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 | /*
|
---|
| 586 | * in_strtfn -- API æ©è½ã³ã¼ãã®æå表ç¾ãè¿ãã
|
---|
| 587 | */
|
---|
| 588 |
|
---|
| 589 | const char *
|
---|
| 590 | in_strtfn (FN fncd)
|
---|
| 591 | {
|
---|
| 592 | switch (fncd) {
|
---|
| 593 |
|
---|
| 594 | /* TCP é¢ä¿ */
|
---|
| 595 |
|
---|
| 596 | case TFN_TCP_CRE_REP:
|
---|
| 597 | return "TFN_TCP_CRE_REP";
|
---|
| 598 | break;
|
---|
| 599 | case TFN_TCP_DEL_REP:
|
---|
| 600 | return "TFN_TCP_DEL_REP";
|
---|
| 601 | break;
|
---|
| 602 | case TFN_TCP_CRE_CEP:
|
---|
| 603 | return "TFN_TCP_CRE_CEP";
|
---|
| 604 | break;
|
---|
| 605 | case TFN_TCP_DEL_CEP:
|
---|
| 606 | return "TFN_TCP_DEL_CEP";
|
---|
| 607 | break;
|
---|
| 608 | case TFN_TCP_ACP_CEP:
|
---|
| 609 | return "TFN_TCP_ACP_CEP";
|
---|
| 610 | break;
|
---|
| 611 | case TFN_TCP_CON_CEP:
|
---|
| 612 | return "TFN_TCP_CON_CEP";
|
---|
| 613 | break;
|
---|
| 614 | case TFN_TCP_SHT_CEP:
|
---|
| 615 | return "TFN_TCP_SHT_CEP";
|
---|
| 616 | break;
|
---|
| 617 | case TFN_TCP_CLS_CEP:
|
---|
| 618 | return "TFN_TCP_CLS_CEP";
|
---|
| 619 | break;
|
---|
| 620 | case TFN_TCP_SND_DAT:
|
---|
| 621 | return "TFN_TCP_SND_DAT";
|
---|
| 622 | break;
|
---|
| 623 | case TFN_TCP_RCV_DAT:
|
---|
| 624 | return "TFN_TCP_RCV_DAT";
|
---|
| 625 | break;
|
---|
| 626 | case TFN_TCP_GET_BUF:
|
---|
| 627 | return "TFN_TCP_GET_BUF";
|
---|
| 628 | break;
|
---|
| 629 | case TFN_TCP_SND_BUF:
|
---|
| 630 | return "TFN_TCP_SND_BUF";
|
---|
| 631 | break;
|
---|
| 632 | case TFN_TCP_RCV_BUF:
|
---|
| 633 | return "TFN_TCP_RCV_BUF";
|
---|
| 634 | break;
|
---|
| 635 | case TFN_TCP_REL_BUF:
|
---|
| 636 | return "TFN_TCP_REL_BUF";
|
---|
| 637 | break;
|
---|
| 638 | case TFN_TCP_SND_OOB:
|
---|
| 639 | return "TFN_TCP_SND_OOB";
|
---|
| 640 | break;
|
---|
| 641 | case TFN_TCP_RCV_OOB:
|
---|
| 642 | return "TFN_TCP_RCV_OOB";
|
---|
| 643 | break;
|
---|
| 644 | case TFN_TCP_CAN_CEP:
|
---|
| 645 | return "TFN_TCP_CAN_CEP";
|
---|
| 646 | break;
|
---|
| 647 | case TFN_TCP_SET_OPT:
|
---|
| 648 | return "TFN_TCP_SET_OPT";
|
---|
| 649 | break;
|
---|
| 650 | case TFN_TCP_GET_OPT:
|
---|
| 651 | return "TFN_TCP_GET_OPT";
|
---|
| 652 | break;
|
---|
| 653 | case TFN_TCP_ALL:
|
---|
| 654 | return "ALL";
|
---|
| 655 | break;
|
---|
| 656 |
|
---|
| 657 | /* UDP é¢ä¿ */
|
---|
| 658 |
|
---|
| 659 | case TFN_UDP_CRE_CEP:
|
---|
| 660 | return "TFN_UDP_CRE_CEP";
|
---|
| 661 | break;
|
---|
| 662 | case TFN_UDP_DEL_CEP:
|
---|
| 663 | return "TFN_UDP_DEL_CEP";
|
---|
| 664 | break;
|
---|
| 665 | case TFN_UDP_SND_DAT:
|
---|
| 666 | return "TFN_UDP_SND_DAT";
|
---|
| 667 | break;
|
---|
| 668 | case TFN_UDP_RCV_DAT:
|
---|
| 669 | return "TFN_UDP_RCV_DAT";
|
---|
| 670 | break;
|
---|
| 671 | case TFN_UDP_CAN_CEP:
|
---|
| 672 | return "TFN_UDP_CAN_CEP";
|
---|
| 673 | break;
|
---|
| 674 | case TFN_UDP_SET_OPT:
|
---|
| 675 | return "TFN_UDP_SET_OPT";
|
---|
| 676 | break;
|
---|
| 677 | case TFN_UDP_GET_OPT:
|
---|
| 678 | return "TFN_UDP_GET_OPT";
|
---|
| 679 | break;
|
---|
| 680 |
|
---|
| 681 | default:
|
---|
| 682 | return "unknown TFN";
|
---|
| 683 | }
|
---|
| 684 | }
|
---|