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