[317] | 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 | * 上記著作権者は,以下の (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 | * @(#)input.c 8.2 (Berkeley) 1/4/94
|
---|
| 67 | * $FreeBSD: src/sys/netinet/input.c,v 1.111.2.4 1999/11/01 22:23:53 des Exp $
|
---|
| 68 | * $ANA: input.c,v 1.5 1996/09/18 14:34:59 wollman Exp $
|
---|
| 69 | */
|
---|
| 70 |
|
---|
| 71 | #include <string.h>
|
---|
| 72 |
|
---|
| 73 | #ifdef TARGET_KERNEL_ASP
|
---|
| 74 |
|
---|
| 75 | #include <kernel.h>
|
---|
| 76 | #include <sil.h>
|
---|
| 77 | #include <t_syslog.h>
|
---|
| 78 | #include "kernel_cfg.h"
|
---|
| 79 | #include "tinet_cfg.h"
|
---|
| 80 |
|
---|
| 81 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
| 82 |
|
---|
| 83 | #ifdef TARGET_KERNEL_JSP
|
---|
| 84 |
|
---|
| 85 | #include <s_services.h>
|
---|
| 86 | #include <t_services.h>
|
---|
| 87 | #include "kernel_id.h"
|
---|
| 88 | #include "tinet_id.h"
|
---|
| 89 |
|
---|
| 90 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
| 91 |
|
---|
| 92 | #include <tinet_defs.h>
|
---|
| 93 | #include <tinet_config.h>
|
---|
| 94 |
|
---|
| 95 | #include <net/if.h>
|
---|
| 96 | #include <net/if_ppp.h>
|
---|
| 97 | #include <net/if_loop.h>
|
---|
| 98 | #include <net/ethernet.h>
|
---|
| 99 | #include <net/ppp_ipcp.h>
|
---|
| 100 | #include <net/net.h>
|
---|
| 101 | #include <net/net_endian.h>
|
---|
| 102 | #include <net/net_buf.h>
|
---|
| 103 | #include <net/net_count.h>
|
---|
| 104 | #include <net/net_timer.h>
|
---|
| 105 |
|
---|
| 106 | #include <netinet/in.h>
|
---|
| 107 | #include <netinet/in_var.h>
|
---|
| 108 | #include <netinet/ip.h>
|
---|
| 109 | #include <netinet/ip_var.h>
|
---|
| 110 | #include <netinet/ip_icmp.h>
|
---|
| 111 | #include <netinet/tcp.h>
|
---|
| 112 | #include <netinet/tcp_var.h>
|
---|
| 113 | #include <netinet/udp.h>
|
---|
| 114 | #include <netinet/udp_var.h>
|
---|
| 115 |
|
---|
| 116 | #if defined(SUPPORT_IPSEC)
|
---|
| 117 | //#include <stdio.h>
|
---|
| 118 | #include <sys/types.h>
|
---|
| 119 | #include <netinet6/ipsec.h>
|
---|
| 120 | #include <netinet6/esp.h>
|
---|
| 121 | #include <netkey/key.h>
|
---|
| 122 | #include <netkey/key_debug.h>
|
---|
| 123 | #endif /* of defined(SUPPORT_IPSEC) */
|
---|
| 124 |
|
---|
| 125 | #include <net/if_var.h>
|
---|
| 126 |
|
---|
| 127 | #if defined(_IP4_CFG)
|
---|
| 128 |
|
---|
| 129 | /*
|
---|
| 130 | * 変数
|
---|
| 131 | */
|
---|
| 132 |
|
---|
| 133 | #ifdef SUPPORT_MIB
|
---|
| 134 |
|
---|
| 135 | /*
|
---|
| 136 | * SNMP の 管理情報ベース (MIB)
|
---|
| 137 | */
|
---|
| 138 |
|
---|
| 139 | T_IP_STATS ip_stats;
|
---|
| 140 |
|
---|
| 141 | #endif /* of #ifdef SUPPORT_MIB */
|
---|
| 142 |
|
---|
| 143 | #ifdef IP4_CFG_FRAGMENT
|
---|
| 144 |
|
---|
| 145 | /*
|
---|
| 146 | * データグラム再構成キュー
|
---|
| 147 | */
|
---|
| 148 |
|
---|
| 149 | static T_NET_BUF *ip_frag_queue[NUM_IP4_FRAG_QUEUE];
|
---|
| 150 | static T_IN4_ADDR frag_dst [NUM_IP4_FRAG_QUEUE];
|
---|
| 151 |
|
---|
| 152 | /*
|
---|
| 153 | * 関数
|
---|
| 154 | */
|
---|
| 155 |
|
---|
| 156 | static void ip_freef (int_t ix);
|
---|
| 157 | static T_NET_BUF *ip_reass (T_IP4_HDR *ip4h, T_NET_BUF *input);
|
---|
| 158 |
|
---|
| 159 | /*
|
---|
| 160 | * ip_get_frag_queue -- データグラム再構成キューを獲得する。
|
---|
| 161 | */
|
---|
| 162 |
|
---|
| 163 | const T_NET_BUF **
|
---|
| 164 | ip_get_frag_queue (void)
|
---|
| 165 | {
|
---|
| 166 | return (const T_NET_BUF **)ip_frag_queue;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /*
|
---|
| 170 | * ip_freef -- データグラム再構成キューを解放する。
|
---|
| 171 | */
|
---|
| 172 |
|
---|
| 173 | static void
|
---|
| 174 | ip_freef (int_t ix)
|
---|
| 175 | {
|
---|
| 176 | T_NET_BUF *frag, *next;
|
---|
| 177 |
|
---|
| 178 | frag = ip_frag_queue[ix];
|
---|
| 179 | while (frag != NULL) {
|
---|
| 180 | next = GET_QIP4_HDR(frag)->next;
|
---|
| 181 | syscall(rel_net_buf(frag));
|
---|
| 182 | frag = next;
|
---|
| 183 | }
|
---|
| 184 | ip_frag_queue[ix] = NULL;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | /*
|
---|
| 188 | * ip_frag_timer -- データグラム再構成管理タイマ
|
---|
| 189 | */
|
---|
| 190 |
|
---|
| 191 | void
|
---|
| 192 | ip_frag_timer (void)
|
---|
| 193 | {
|
---|
| 194 | T_NET_BUF *frag;
|
---|
| 195 | int_t ix;
|
---|
| 196 |
|
---|
| 197 | syscall(wai_sem(SEM_IP4_FRAG_QUEUE));
|
---|
| 198 | for (ix = NUM_IP4_FRAG_QUEUE; ix -- > 0; ) {
|
---|
| 199 | frag = ip_frag_queue[ix];
|
---|
| 200 | if (frag != NULL && GET_QIP4_HDR(frag)->ttl > 0 &&
|
---|
| 201 | -- GET_QIP4_HDR(frag)->ttl == 0) {
|
---|
| 202 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_IN_TMOUT], 1);
|
---|
| 203 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_IN_DROP], 1);
|
---|
| 204 | ip_freef(ix);
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | syscall(sig_sem(SEM_IP4_FRAG_QUEUE));
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | /*
|
---|
| 211 | * ip_reass -- データグラムを再構成する。
|
---|
| 212 | */
|
---|
| 213 |
|
---|
| 214 | static T_NET_BUF *
|
---|
| 215 | ip_reass (T_IP4_HDR *ip4h, T_NET_BUF *input)
|
---|
| 216 | {
|
---|
| 217 | T_NET_BUF *frag, *prev;
|
---|
| 218 | T_IN4_ADDR dst, src;
|
---|
| 219 | int_t ix;
|
---|
| 220 | uint_t id, off, len;
|
---|
| 221 |
|
---|
| 222 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_IN_FRAGS], 1);
|
---|
| 223 | NET_COUNT_MIB(ip_stats.ipReasmReqds, 1);
|
---|
| 224 |
|
---|
| 225 | src = ntohl(ip4h->src);
|
---|
| 226 | dst = ntohl(ip4h->dst);
|
---|
| 227 | NTOHS(ip4h->id);
|
---|
| 228 | NTOHS(ip4h->flg_off);
|
---|
| 229 | id = ip4h->id;
|
---|
| 230 | ix = id % NUM_IP4_FRAG_QUEUE;
|
---|
| 231 |
|
---|
| 232 | syscall(wai_sem(SEM_IP4_FRAG_QUEUE));
|
---|
| 233 |
|
---|
| 234 | /*
|
---|
| 235 | * ID、IPアドレス、上位プロトコルが異なるフラグメントがキューに有れば破棄する。
|
---|
| 236 | */
|
---|
| 237 | frag = ip_frag_queue[ix];
|
---|
| 238 | if (frag != NULL &&
|
---|
| 239 | (id != GET_IP4_HDR(frag)->id ||
|
---|
| 240 | dst != frag_dst[ix] ||
|
---|
| 241 | src != ntohl(GET_IP4_HDR(frag)->src) ||
|
---|
| 242 | ip4h->proto != GET_IP4_HDR(frag)->proto)) {
|
---|
| 243 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_IN_DROP], 1);
|
---|
| 244 | NET_COUNT_MIB(ip_stats.ipReasmFails, 1);
|
---|
| 245 | ip_freef(ix);
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | frag = ip_frag_queue[ix];
|
---|
| 249 | if (frag == NULL) {
|
---|
| 250 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_IN], 1);
|
---|
| 251 |
|
---|
| 252 | /* 新規の ID なら、宛先アドレスを保存して、キューにつなぐ。*/
|
---|
| 253 | frag_dst [ix] = dst;
|
---|
| 254 | ip_frag_queue[ix] = input;
|
---|
| 255 | ((T_QIP4_HDR *)ip4h)->next = NULL;
|
---|
| 256 | input = NULL;
|
---|
| 257 |
|
---|
| 258 | /* 再構成タイムアウトを設定する。*/
|
---|
| 259 | ip4h->ttl = IP4_FRAGTTL;
|
---|
| 260 | }
|
---|
| 261 | else {
|
---|
| 262 |
|
---|
| 263 | /* 正しい位置に挿入する。*/
|
---|
| 264 | prev = NULL;
|
---|
| 265 | while (frag != NULL &&
|
---|
| 266 | IP4_FLGOFF_OFF(ip4h->flg_off) >
|
---|
| 267 | IP4_FLGOFF_OFF(GET_QIP4_HDR(frag)->flg_off)) {
|
---|
| 268 | prev = frag;
|
---|
| 269 | frag = GET_QIP4_HDR(frag)->next;
|
---|
| 270 | }
|
---|
| 271 | ((T_QIP4_HDR *)ip4h)->next = frag;
|
---|
| 272 | if (prev == NULL) {
|
---|
| 273 | ip4h->ttl = GET_QIP4_HDR(frag)->ttl;
|
---|
| 274 | ip_frag_queue[ix] = input;
|
---|
| 275 | }
|
---|
| 276 | else
|
---|
| 277 | GET_QIP4_HDR(prev)->next = input;
|
---|
| 278 | input = NULL;
|
---|
| 279 |
|
---|
| 280 | /* 全てのフラグメントが揃っているか調べる。*/
|
---|
| 281 | off = 0;
|
---|
| 282 | for (frag = ip_frag_queue[ix]; frag != NULL; frag = GET_QIP4_HDR(frag)->next) {
|
---|
| 283 | if ((IP4_FLGOFF_OFF(GET_QIP4_HDR(frag)->flg_off) << 3) != off) {
|
---|
| 284 | /* 途中が抜けていたら終了する。*/
|
---|
| 285 | syscall(sig_sem(SEM_IP4_FRAG_QUEUE));
|
---|
| 286 | return NULL;
|
---|
| 287 | }
|
---|
| 288 | off += ntohs(GET_QIP4_HDR(frag)->len) - (IP4_VHL_HL(GET_QIP4_HDR(frag)->vhl) << 2);
|
---|
| 289 | prev = frag;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | /* 全てのフラグメントが揃ったら再構成する。*/
|
---|
| 293 | if ((GET_QIP4_HDR(prev)->flg_off & IP4_MF) == 0) {
|
---|
| 294 |
|
---|
| 295 | /* ネットワークバッファを獲得する。*/
|
---|
| 296 | if (tget_net_buf(&input, IF_IP4_HDR_SIZE + off, TMO_IP4_FRAG_GET_NET_BUF) == E_OK) {
|
---|
| 297 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_IN_OK], 1);
|
---|
| 298 | NET_COUNT_MIB(ip_stats.ipReasmOKs, 1);
|
---|
| 299 |
|
---|
| 300 | /* IPv4 ヘッダを設定する。*/
|
---|
| 301 | frag = ip_frag_queue[ix];
|
---|
| 302 | ip4h = GET_IP4_HDR(input);
|
---|
| 303 | *ip4h = *GET_IP4_HDR(frag);
|
---|
| 304 | ip4h->dst = htonl(frag_dst[ix]);
|
---|
| 305 | ip4h->len = htons(IP4_HDR_SIZE + off);
|
---|
| 306 | ip4h->vhl = IP4_MAKE_VHL(IPV4_VERSION, IP4_HDR_SIZE >> 2);
|
---|
| 307 | ip4h->ttl = GET_QIP4_HDR(prev)->ttl;
|
---|
| 308 | ip4h->flg_off = ip4h->id = 0;
|
---|
| 309 |
|
---|
| 310 | /* データグラムを再構成する。*/
|
---|
| 311 | off = IP4_HDR_SIZE;
|
---|
| 312 | while (frag != NULL) {
|
---|
| 313 | len = ntohs(GET_QIP4_HDR(frag)->len) - (IP4_VHL_HL(GET_QIP4_HDR(frag)->vhl) << 2);
|
---|
| 314 | memcpy((uint8_t *)ip4h + off, GET_QIP4_SDU(frag), len);
|
---|
| 315 | off += len;
|
---|
| 316 | frag = GET_QIP4_HDR(frag)->next;
|
---|
| 317 | }
|
---|
| 318 | }
|
---|
| 319 | else {
|
---|
| 320 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_IN_NO_BUF], 1);
|
---|
| 321 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_IN_DROP], 1);
|
---|
| 322 | NET_COUNT_MIB(ip_stats.ipReasmFails, 1);
|
---|
| 323 | }
|
---|
| 324 | /* キューを空にする。*/
|
---|
| 325 | ip_freef(ix);
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
| 328 | syscall(sig_sem(SEM_IP4_FRAG_QUEUE));
|
---|
| 329 | return input;
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | #endif /* of #ifdef IP4_CFG_FRAGMENT */
|
---|
| 333 |
|
---|
| 334 | /*
|
---|
| 335 | * ip_init -- IP の初期化
|
---|
| 336 | */
|
---|
| 337 |
|
---|
| 338 | void
|
---|
| 339 | ip_init (void)
|
---|
| 340 | {
|
---|
| 341 | in4_init();
|
---|
| 342 | }
|
---|
| 343 |
|
---|
| 344 | /*
|
---|
| 345 | * ip_remove_options -- IPv4 ヘッダのオプションを削除し、以降を前に詰める。
|
---|
| 346 | */
|
---|
| 347 |
|
---|
| 348 | ER
|
---|
| 349 | ip_remove_options (T_NET_BUF *nbuf)
|
---|
| 350 | {
|
---|
| 351 | T_IP4_HDR *iph;
|
---|
| 352 | uint_t hdr_size;
|
---|
| 353 |
|
---|
| 354 | iph = GET_IP4_HDR(nbuf);
|
---|
| 355 | hdr_size = GET_IP4_HDR_SIZE(nbuf);
|
---|
| 356 |
|
---|
| 357 | if (hdr_size > IP4_HDR_SIZE) {
|
---|
| 358 | memmove((char *)iph + IP4_HDR_SIZE, GET_IP4_SDU(nbuf),
|
---|
| 359 | (size_t)(iph->len - hdr_size));
|
---|
| 360 | iph->vhl = IP4_MAKE_VHL(IPV4_VERSION, IP4_HDR_SIZE >> 2);
|
---|
| 361 | iph->len -= (uint16_t)(hdr_size - IP4_HDR_SIZE);
|
---|
| 362 | nbuf->len -= (uint16_t)(hdr_size - IP4_HDR_SIZE);
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 | return E_OK;
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | /*
|
---|
| 369 | * ip_input -- IP の入力関数
|
---|
| 370 | */
|
---|
| 371 |
|
---|
| 372 | #if defined(SUPPORT_IPSEC)
|
---|
| 373 | /*
|
---|
| 374 | * TODO: IPsec SPを検索し、パケットをリジェクトするかどうか判定する処理を実装する
|
---|
| 375 | */
|
---|
| 376 | #endif /* of defined(SUPPORT_IPSEC) */
|
---|
| 377 |
|
---|
| 378 | void
|
---|
| 379 | ip_input (T_NET_BUF *input)
|
---|
| 380 | {
|
---|
| 381 | T_IP4_HDR *ip4h;
|
---|
| 382 | T_IFNET *ifp = IF_GET_IFNET();
|
---|
| 383 | T_IN4_ADDR dst, src, bc;
|
---|
| 384 | uint_t hlen, off;
|
---|
| 385 |
|
---|
| 386 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_OCTETS], input->len - IF_HDR_SIZE);
|
---|
| 387 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_PACKETS], 1);
|
---|
| 388 | NET_COUNT_MIB(ip_stats.ipInReceives, 1);
|
---|
| 389 |
|
---|
| 390 | /* IP ヘッダの長さをチェックする。*/
|
---|
| 391 | if (input->len < IF_IP4_HDR_SIZE) {
|
---|
| 392 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_SHORT], 1);
|
---|
| 393 | NET_COUNT_MIB(ip_stats.ipInHdrErrors, 1);
|
---|
| 394 | goto buf_rel;
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | ip4h = GET_IP4_HDR(input);
|
---|
| 398 | hlen = GET_IP4_HDR_SIZE(input);
|
---|
| 399 |
|
---|
| 400 | /* バージョンをチェックする。*/
|
---|
| 401 | if (IP4_VHL_V(ip4h->vhl) != IPV4_VERSION) {
|
---|
| 402 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_VER], 1);
|
---|
| 403 | NET_COUNT_MIB(ip_stats.ipInHdrErrors, 1);
|
---|
| 404 | goto buf_rel;
|
---|
| 405 | }
|
---|
| 406 |
|
---|
| 407 | /* IP ヘッダの長さをチェックし、オプションを解析する。*/
|
---|
| 408 | if (hlen > IP4_HDR_SIZE) {
|
---|
| 409 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OPTS], 1);
|
---|
| 410 | /* %%% オプションの解析 %%% */
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | /* データグラム長をチェックする。*/
|
---|
| 414 | if (ntohs(ip4h->len) > input->len - IF_HDR_SIZE) {
|
---|
| 415 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_SHORT], 1);
|
---|
| 416 | NET_COUNT_MIB(ip_stats.ipInHdrErrors, 1);
|
---|
| 417 | goto buf_rel;
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | /* ネットワークバッファの長さをデータグラム長に調整する。*/
|
---|
| 421 | input->len = (uint16_t)(ntohs(ip4h->len) + IF_HDR_SIZE);
|
---|
| 422 |
|
---|
| 423 | /* チェックサムをチェックする。*/
|
---|
| 424 | if (in_cksum(ip4h, hlen) != 0) {
|
---|
| 425 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_CKSUM], 1);
|
---|
| 426 | NET_COUNT_MIB(ip_stats.ipInHdrErrors, 1);
|
---|
| 427 | goto buf_rel;
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 | /* IP ヘッダの長さをチェックし、上位が ICMP 以外はオプションを消去する。*/
|
---|
| 431 | if (hlen > IP4_HDR_SIZE && ip4h->proto != IPPROTO_ICMP) {
|
---|
| 432 | memset((uint8_t*)ip4h + IP4_HDR_SIZE, 0, hlen - IP4_HDR_SIZE);
|
---|
| 433 | }
|
---|
| 434 |
|
---|
| 435 | /* 送信元アドレスをチェックする。*/
|
---|
| 436 | src = ntohl(ip4h->src);
|
---|
| 437 | bc = (ifp->in4_ifaddr.addr & ifp->in4_ifaddr.mask) | ~ifp->in4_ifaddr.mask;
|
---|
| 438 |
|
---|
| 439 | #ifdef SUPPORT_LOOP
|
---|
| 440 |
|
---|
| 441 | if (src == bc || src == IPV4_ADDR_BROADCAST || src == IPV4_ADDRANY) {
|
---|
| 442 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_ADDR], 1);
|
---|
| 443 | NET_COUNT_MIB(ip_stats.ipInAddrErrors, 1);
|
---|
| 444 | goto buf_rel;
|
---|
| 445 | }
|
---|
| 446 |
|
---|
| 447 | #else /* of #ifdef SUPPORT_LOOP */
|
---|
| 448 |
|
---|
| 449 | if (src == ifp->in4_ifaddr.addr || src == bc || src == IPV4_ADDR_BROADCAST || src == IPV4_ADDRANY) {
|
---|
| 450 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_ADDR], 1);
|
---|
| 451 | NET_COUNT_MIB(ip_stats.ipInAddrErrors, 1);
|
---|
| 452 | goto buf_rel;
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | #endif /* of #ifdef SUPPORT_LOOP */
|
---|
| 456 |
|
---|
| 457 | /* あて先アドレスをチェックする。*/
|
---|
| 458 | dst = ntohl(ip4h->dst);
|
---|
| 459 |
|
---|
| 460 | #ifdef DHCP_CFG
|
---|
| 461 |
|
---|
| 462 | /*
|
---|
| 463 | * DHCP_CFG が定義されているときは、ローカルアドレスが未定義の
|
---|
| 464 | * 場合もデータグラムを受信する。
|
---|
| 465 | */
|
---|
| 466 |
|
---|
| 467 | if ((ifp->in4_ifaddr.addr != IPV4_ADDRANY) &&
|
---|
| 468 | (!(dst == ifp->in4_ifaddr.addr || dst == bc ||
|
---|
| 469 | dst == IPV4_ADDR_BROADCAST || dst == IPV4_ADDRANY))) {
|
---|
| 470 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_ADDR], 1);
|
---|
| 471 | NET_COUNT_MIB(ip_stats.ipInAddrErrors, 1);
|
---|
| 472 | goto buf_rel;
|
---|
| 473 | }
|
---|
| 474 |
|
---|
| 475 | #else /* of #ifdef DHCP_CFG */
|
---|
| 476 |
|
---|
| 477 | if (!(dst == ifp->in4_ifaddr.addr || dst == bc ||
|
---|
| 478 | dst == IPV4_ADDR_BROADCAST || dst == IPV4_ADDRANY)) {
|
---|
| 479 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_ADDR], 1);
|
---|
| 480 | NET_COUNT_MIB(ip_stats.ipInAddrErrors, 1);
|
---|
| 481 | goto buf_rel;
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | #endif /* of #ifdef DHCP_CFG */
|
---|
| 485 |
|
---|
| 486 | #ifdef IP4_CFG_FRAGMENT
|
---|
| 487 |
|
---|
| 488 | /* 分割されているかチェックする。*/
|
---|
| 489 | if (ntohs(ip4h->flg_off) & (IP4_MF | IP4_OFFMASK)) {
|
---|
| 490 | if ((input = ip_reass(ip4h, input)) == NULL)
|
---|
| 491 | return;
|
---|
| 492 | }
|
---|
| 493 |
|
---|
| 494 | #else /* of #ifdef IP4_CFG_FRAGMENT */
|
---|
| 495 |
|
---|
| 496 | /* 分割されているかチェックする。*/
|
---|
| 497 | if (ntohs(ip4h->flg_off) & (IP4_MF | IP4_OFFMASK)) {
|
---|
| 498 | T_IN4_ADDR src;
|
---|
| 499 |
|
---|
| 500 | NET_COUNT_MIB(ip_stats.ipReasmReqds, 1);
|
---|
| 501 | if ((ntohs(ip4h->flg_off) & IP4_OFFMASK) == 0) {
|
---|
| 502 | NET_COUNT_MIB(ip_stats.ipReasmFails, 1);
|
---|
| 503 | }
|
---|
| 504 | src = ntohl(ip4h->src);
|
---|
| 505 | syslog(LOG_WARNING, "[IP] flaged src: %s.", ip2str(NULL, &src));
|
---|
| 506 | goto buf_rel;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | #endif /* of #ifdef IP4_CFG_FRAGMENT */
|
---|
| 510 |
|
---|
| 511 | off = (uint_t)(GET_IP4_SDU(input) - input->buf);
|
---|
| 512 |
|
---|
| 513 | #if defined(SUPPORT_IPSEC)
|
---|
| 514 | /* ここでipsec4_in_rejectを実行する */
|
---|
| 515 | if ((ip4h->proto != IPPROTO_ESP) && ipsec4_in_reject (input)) {
|
---|
| 516 | goto buf_rel;
|
---|
| 517 | }
|
---|
| 518 | #endif /* of #if defined(SUPPORT_IPSEC) */
|
---|
| 519 |
|
---|
| 520 | /* プロトコルを選択する */
|
---|
| 521 | switch (ip4h->proto) {
|
---|
| 522 |
|
---|
| 523 | #if defined(SUPPORT_TCP)
|
---|
| 524 | case IPPROTO_TCP:
|
---|
| 525 | NET_COUNT_MIB(ip_stats.ipInDelivers, 1);
|
---|
| 526 | tcp_input(&input, &off, NULL);
|
---|
| 527 | return;
|
---|
| 528 | break;
|
---|
| 529 | #endif /* of #if defined(SUPPORT_TCP) */
|
---|
| 530 |
|
---|
| 531 | #if defined(SUPPORT_UDP) && ( (TNUM_UDP4_CEPID > 0) || \
|
---|
| 532 | ((TNUM_UDP6_CEPID > 0) && defined(API_CFG_IP4MAPPED_ADDR)))
|
---|
| 533 | case IPPROTO_UDP:
|
---|
| 534 | NET_COUNT_MIB(ip_stats.ipInDelivers, 1);
|
---|
| 535 | udp4_input(&input, &off, NULL);
|
---|
| 536 | return;
|
---|
| 537 | break;
|
---|
| 538 | #endif /* of #if defined(SUPPORT_UDP) && TNUM_UDP4_CEPID > 0 */
|
---|
| 539 |
|
---|
| 540 | case IPPROTO_ICMP:
|
---|
| 541 | NET_COUNT_MIB(ip_stats.ipInDelivers, 1);
|
---|
| 542 | icmp_input(&input, &off, NULL);
|
---|
| 543 | return;
|
---|
| 544 | break;
|
---|
| 545 |
|
---|
| 546 | #if defined(SUPPORT_IPSEC)
|
---|
| 547 | case IPPROTO_ESP:
|
---|
| 548 | NET_COUNT_MIB(ip_stats.ipInDelivers, 1);
|
---|
| 549 | esp4_input(input, &off);
|
---|
| 550 | return;
|
---|
| 551 | break;
|
---|
| 552 | #endif /* of #if defined(SUPPORT_IPSEC) */
|
---|
| 553 |
|
---|
| 554 | default:
|
---|
| 555 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_PROTO], 1);
|
---|
| 556 | NET_COUNT_MIB(ip_stats.ipInUnknownProtos, 1);
|
---|
| 557 |
|
---|
| 558 | /* ローカル IP アドレスに届いたデータグラムのみ ICMP エラーを通知する。*/
|
---|
| 559 | if (dst == ifp->in4_ifaddr.addr) {
|
---|
| 560 | T_IN4_ADDR src;
|
---|
| 561 |
|
---|
| 562 | src = ntohl(ip4h->src);
|
---|
| 563 | syslog(LOG_INFO, "[IP] unexp proto: %d, src=%s.", ip4h->proto, ip2str(NULL, &src));
|
---|
| 564 | icmp_error(ICMP4_UNREACH_PROTOCOL, input);
|
---|
| 565 | }
|
---|
| 566 | /*
|
---|
| 567 | * icmp_error では、ネットワークバッファ input を返却しないので
|
---|
| 568 | * 開放してから終了する。
|
---|
| 569 | */
|
---|
| 570 | break;
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 | buf_rel:
|
---|
| 574 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_PACKETS], 1);
|
---|
| 575 | syscall(rel_net_buf(input));
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | #endif /* of #if defined(_IP4_CFG) */
|
---|