[331] | 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>
|
---|
[429] | 105 | #include <net/net.h>
|
---|
| 106 | #include <net/net_endian.h>
|
---|
| 107 | #include <net/net_buf.h>
|
---|
| 108 | #include <net/net_timer.h>
|
---|
| 109 |
|
---|
[331] | 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 |
|
---|
| 130 | void
|
---|
| 131 | in_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 |
|
---|
| 146 | T_IN4_RTENTRY *
|
---|
| 147 | in_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 |
|
---|
| 200 | void
|
---|
| 201 | in_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 |
|
---|
| 224 | uint_t
|
---|
| 225 | in4_get_maxnum_ifaddr (void)
|
---|
| 226 | {
|
---|
| 227 | return 1;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | /*
|
---|
| 231 | * in4_get_ifaddr -- ã¤ã³ã¿ãã§ã¼ã¹ã«è¨å®ããã¦ããã¢ãã¬ã¹ãè¿ãã
|
---|
| 232 | */
|
---|
| 233 |
|
---|
| 234 | const T_IN4_ADDR *
|
---|
| 235 | in4_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 |
|
---|
| 246 | ER
|
---|
| 247 | in4_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 |
|
---|
| 276 | ER
|
---|
| 277 | in4_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),
|
---|
[429] | 289 | (uint_t)(IF_IP4_HDR_SIZE + maxlen), nbatr, tmout)) != E_OK)
|
---|
| 290 | return error;
|
---|
| 291 |
|
---|
[331] | 292 | /*
|
---|
[429] | 293 | * ãã大ããªãµã¤ãºã®ãããã¯ã¼ã¯ãããã¡ãç²å¾ããå ´åã®ã¿é·ãã調æ´ããã
|
---|
[331] | 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 |
|
---|
| 320 | uint16_t
|
---|
| 321 | in4_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 |
|
---|
| 352 | uint16_t
|
---|
| 353 | in_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 |
|
---|
| 370 | bool_t
|
---|
| 371 | in4_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 |
|
---|
| 388 | bool_t
|
---|
| 389 | inn4_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 |
|
---|
| 409 | T_IN4_ADDR *
|
---|
| 410 | in4_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 |
|
---|
| 420 | ER
|
---|
| 421 | in4_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 |
|
---|
| 436 | ER
|
---|
| 437 | in4_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 |
|
---|
| 456 | T_IN4_ADDR
|
---|
| 457 | in4_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 |
|
---|
| 484 | void
|
---|
| 485 | in4_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 |
|
---|
| 504 | static void
|
---|
| 505 | in4_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 |
|
---|
| 527 | void
|
---|
| 528 | in4_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) */
|
---|