[388] | 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: ip_input.c 388 2019-05-22 11:25:18Z 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 | * @(#)input.c 8.2 (Berkeley) 1/4/94
|
---|
| 79 | * $FreeBSD: src/sys/netinet/input.c,v 1.111.2.4 1999/11/01 22:23:53 des Exp $
|
---|
| 80 | * $ANA: input.c,v 1.5 1996/09/18 14:34:59 wollman Exp $
|
---|
| 81 | */
|
---|
| 82 |
|
---|
| 83 | #include <string.h>
|
---|
| 84 |
|
---|
| 85 | #ifdef TARGET_KERNEL_ASP
|
---|
| 86 |
|
---|
| 87 | #include <kernel.h>
|
---|
| 88 | #include <sil.h>
|
---|
| 89 | #include <t_syslog.h>
|
---|
| 90 | #include "kernel_cfg.h"
|
---|
| 91 | #include "tinet_cfg.h"
|
---|
| 92 |
|
---|
| 93 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
| 94 |
|
---|
| 95 | #ifdef TARGET_KERNEL_JSP
|
---|
| 96 |
|
---|
| 97 | #include <s_services.h>
|
---|
| 98 | #include <t_services.h>
|
---|
| 99 | #include "kernel_id.h"
|
---|
| 100 | #include "tinet_id.h"
|
---|
| 101 |
|
---|
| 102 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
| 103 |
|
---|
| 104 | #include <tinet_defs.h>
|
---|
| 105 | #include <tinet_config.h>
|
---|
| 106 |
|
---|
| 107 | #include <net/if.h>
|
---|
| 108 | #include <net/if_ppp.h>
|
---|
| 109 | #include <net/if_loop.h>
|
---|
| 110 | #include <net/ethernet.h>
|
---|
| 111 | #include <net/ppp_ipcp.h>
|
---|
| 112 | #include <net/net.h>
|
---|
| 113 | #include <net/net_endian.h>
|
---|
| 114 | #include <net/net_buf.h>
|
---|
| 115 | #include <net/net_count.h>
|
---|
| 116 | #include <net/net_timer.h>
|
---|
| 117 |
|
---|
| 118 | #include <netinet/in.h>
|
---|
| 119 | #include <netinet/in_var.h>
|
---|
| 120 | #include <netinet/ip.h>
|
---|
| 121 | #include <netinet/ip_var.h>
|
---|
| 122 | #include <netinet/ip_icmp.h>
|
---|
| 123 | #include <netinet/tcp.h>
|
---|
| 124 | #include <netinet/tcp_var.h>
|
---|
| 125 | #include <netinet/udp.h>
|
---|
| 126 | #include <netinet/udp_var.h>
|
---|
| 127 | #include <netinet/ip_igmp.h>
|
---|
| 128 |
|
---|
| 129 | #if defined(SUPPORT_IPSEC)
|
---|
| 130 | //#include <stdio.h>
|
---|
| 131 | #include <sys/types.h>
|
---|
| 132 | #include <netinet6/ipsec.h>
|
---|
| 133 | #include <netinet6/esp.h>
|
---|
| 134 | #include <netkey/key.h>
|
---|
| 135 | #include <netkey/key_debug.h>
|
---|
| 136 | #endif /* of defined(SUPPORT_IPSEC) */
|
---|
| 137 |
|
---|
| 138 | #include <net/if_var.h>
|
---|
| 139 |
|
---|
| 140 | #if defined(_IP4_CFG)
|
---|
| 141 |
|
---|
| 142 | /*
|
---|
| 143 | * å¤æ°
|
---|
| 144 | */
|
---|
| 145 |
|
---|
| 146 | #ifdef SUPPORT_MIB
|
---|
| 147 |
|
---|
| 148 | /*
|
---|
| 149 | * SNMP 㮠管çæ
|
---|
| 150 | å ±ãã¼ã¹ (MIB)
|
---|
| 151 | */
|
---|
| 152 |
|
---|
| 153 | T_IP_STATS ip_stats;
|
---|
| 154 |
|
---|
| 155 | #endif /* of #ifdef SUPPORT_MIB */
|
---|
| 156 |
|
---|
| 157 | #ifdef IP4_CFG_FRAGMENT
|
---|
| 158 |
|
---|
| 159 | /*
|
---|
| 160 | * ãã¼ã¿ã°ã©ã åæ§æãã¥ã¼
|
---|
| 161 | */
|
---|
| 162 |
|
---|
| 163 | static T_NET_BUF *ip_frag_queue[NUM_IP4_FRAG_QUEUE];
|
---|
| 164 | static T_IN4_ADDR frag_dst [NUM_IP4_FRAG_QUEUE];
|
---|
| 165 |
|
---|
| 166 | /*
|
---|
| 167 | * é¢æ°
|
---|
| 168 | */
|
---|
| 169 |
|
---|
| 170 | static void ip_freef (int_t ix);
|
---|
| 171 | static T_NET_BUF *ip_reass (T_IP4_HDR *ip4h, T_NET_BUF *input);
|
---|
| 172 |
|
---|
| 173 | /*
|
---|
| 174 | * ip_get_frag_queue -- ãã¼ã¿ã°ã©ã åæ§æãã¥ã¼ãç²å¾ããã
|
---|
| 175 | */
|
---|
| 176 |
|
---|
| 177 | const T_NET_BUF **
|
---|
| 178 | ip_get_frag_queue (void)
|
---|
| 179 | {
|
---|
| 180 | return (const T_NET_BUF **)ip_frag_queue;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /*
|
---|
| 184 | * ip_freef -- ãã¼ã¿ã°ã©ã åæ§æãã¥ã¼ã解æ¾ããã
|
---|
| 185 | */
|
---|
| 186 |
|
---|
| 187 | static void
|
---|
| 188 | ip_freef (int_t ix)
|
---|
| 189 | {
|
---|
| 190 | T_NET_BUF *frag, *next;
|
---|
| 191 |
|
---|
| 192 | frag = ip_frag_queue[ix];
|
---|
| 193 | while (frag != NULL) {
|
---|
| 194 | next = GET_QIP4_HDR(frag)->next;
|
---|
| 195 | syscall(rel_net_buf(frag));
|
---|
| 196 | frag = next;
|
---|
| 197 | }
|
---|
| 198 | ip_frag_queue[ix] = NULL;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /*
|
---|
| 202 | * ip_frag_timer -- ãã¼ã¿ã°ã©ã åæ§æ管çã¿ã¤ã
|
---|
| 203 | */
|
---|
| 204 |
|
---|
| 205 | void
|
---|
| 206 | ip_frag_timer (void)
|
---|
| 207 | {
|
---|
| 208 | T_NET_BUF *frag;
|
---|
| 209 | int_t ix;
|
---|
| 210 |
|
---|
| 211 | syscall(wai_sem(SEM_IP4_FRAG_QUEUE));
|
---|
| 212 | for (ix = NUM_IP4_FRAG_QUEUE; ix -- > 0; ) {
|
---|
| 213 | frag = ip_frag_queue[ix];
|
---|
| 214 | if (frag != NULL && GET_QIP4_HDR(frag)->ttl > 0 &&
|
---|
| 215 | -- GET_QIP4_HDR(frag)->ttl == 0) {
|
---|
| 216 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_IN_TMOUT], 1);
|
---|
| 217 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_IN_DROP], 1);
|
---|
| 218 | ip_freef(ix);
|
---|
| 219 | }
|
---|
| 220 | }
|
---|
| 221 | syscall(sig_sem(SEM_IP4_FRAG_QUEUE));
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | /*
|
---|
| 225 | * ip_reass -- ãã¼ã¿ã°ã©ã ãåæ§æããã
|
---|
| 226 | */
|
---|
| 227 |
|
---|
| 228 | static T_NET_BUF *
|
---|
| 229 | ip_reass (T_IP4_HDR *ip4h, T_NET_BUF *input)
|
---|
| 230 | {
|
---|
| 231 | T_NET_BUF *frag, *prev;
|
---|
| 232 | T_IN4_ADDR dst, src;
|
---|
| 233 | int_t ix;
|
---|
| 234 | uint_t id, off, len;
|
---|
| 235 |
|
---|
| 236 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_IN_FRAGS], 1);
|
---|
| 237 | NET_COUNT_MIB(ip_stats.ipReasmReqds, 1);
|
---|
| 238 |
|
---|
| 239 | src = ntohl(ip4h->src);
|
---|
| 240 | dst = ntohl(ip4h->dst);
|
---|
| 241 | NTOHS(ip4h->id);
|
---|
| 242 | NTOHS(ip4h->flg_off);
|
---|
| 243 | id = ip4h->id;
|
---|
| 244 | ix = id % NUM_IP4_FRAG_QUEUE;
|
---|
| 245 |
|
---|
| 246 | syscall(wai_sem(SEM_IP4_FRAG_QUEUE));
|
---|
| 247 |
|
---|
| 248 | /*
|
---|
| 249 | * IDãIPã¢ãã¬ã¹ãä¸ä½ãããã³ã«ãç°ãªããã©ã°ã¡ã³ãããã¥ã¼ã«æãã°ç ´æ£ããã
|
---|
| 250 | */
|
---|
| 251 | frag = ip_frag_queue[ix];
|
---|
| 252 | if (frag != NULL &&
|
---|
| 253 | (id != GET_IP4_HDR(frag)->id ||
|
---|
| 254 | dst != frag_dst[ix] ||
|
---|
| 255 | src != ntohl(GET_IP4_HDR(frag)->src) ||
|
---|
| 256 | ip4h->proto != GET_IP4_HDR(frag)->proto)) {
|
---|
| 257 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_IN_DROP], 1);
|
---|
| 258 | NET_COUNT_MIB(ip_stats.ipReasmFails, 1);
|
---|
| 259 | ip_freef(ix);
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | frag = ip_frag_queue[ix];
|
---|
| 263 | if (frag == NULL) {
|
---|
| 264 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_IN], 1);
|
---|
| 265 |
|
---|
| 266 | /* æ°è¦ã® ID ãªããå®å
|
---|
| 267 | ã¢ãã¬ã¹ãä¿åãã¦ããã¥ã¼ã«ã¤ãªãã*/
|
---|
| 268 | frag_dst [ix] = dst;
|
---|
| 269 | ip_frag_queue[ix] = input;
|
---|
| 270 | ((T_QIP4_HDR *)ip4h)->next = NULL;
|
---|
| 271 | input = NULL;
|
---|
| 272 |
|
---|
| 273 | /* åæ§æã¿ã¤ã ã¢ã¦ããè¨å®ããã*/
|
---|
| 274 | ip4h->ttl = IP4_FRAGTTL;
|
---|
| 275 | }
|
---|
| 276 | else {
|
---|
| 277 |
|
---|
| 278 | /* æ£ããä½ç½®ã«æ¿å
|
---|
| 279 | ¥ããã*/
|
---|
| 280 | prev = NULL;
|
---|
| 281 | while (frag != NULL &&
|
---|
| 282 | IP4_FLGOFF_OFF(ip4h->flg_off) >
|
---|
| 283 | IP4_FLGOFF_OFF(GET_QIP4_HDR(frag)->flg_off)) {
|
---|
| 284 | prev = frag;
|
---|
| 285 | frag = GET_QIP4_HDR(frag)->next;
|
---|
| 286 | }
|
---|
| 287 | ((T_QIP4_HDR *)ip4h)->next = frag;
|
---|
| 288 | if (prev == NULL) {
|
---|
| 289 | ip4h->ttl = GET_QIP4_HDR(frag)->ttl;
|
---|
| 290 | ip_frag_queue[ix] = input;
|
---|
| 291 | }
|
---|
| 292 | else
|
---|
| 293 | GET_QIP4_HDR(prev)->next = input;
|
---|
| 294 | input = NULL;
|
---|
| 295 |
|
---|
| 296 | /* å
|
---|
| 297 | ¨ã¦ã®ãã©ã°ã¡ã³ããæã£ã¦ããã調ã¹ãã*/
|
---|
| 298 | off = 0;
|
---|
| 299 | for (frag = ip_frag_queue[ix]; frag != NULL; frag = GET_QIP4_HDR(frag)->next) {
|
---|
| 300 | if ((IP4_FLGOFF_OFF(GET_QIP4_HDR(frag)->flg_off) << 3) != off) {
|
---|
| 301 | /* éä¸ãæãã¦ãããçµäºããã*/
|
---|
| 302 | syscall(sig_sem(SEM_IP4_FRAG_QUEUE));
|
---|
| 303 | return NULL;
|
---|
| 304 | }
|
---|
| 305 | off += ntohs(GET_QIP4_HDR(frag)->len) - (IP4_VHL_HL(GET_QIP4_HDR(frag)->vhl) << 2);
|
---|
| 306 | prev = frag;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | /* å
|
---|
| 310 | ¨ã¦ã®ãã©ã°ã¡ã³ããæã£ããåæ§æããã*/
|
---|
| 311 | if ((GET_QIP4_HDR(prev)->flg_off & IP4_MF) == 0) {
|
---|
| 312 |
|
---|
| 313 | /* ãããã¯ã¼ã¯ãããã¡ãç²å¾ããã*/
|
---|
| 314 | if (tget_net_buf(&input, IF_IP4_HDR_SIZE + off, TMO_IP4_FRAG_GET_NET_BUF) == E_OK) {
|
---|
| 315 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_IN_OK], 1);
|
---|
| 316 | NET_COUNT_MIB(ip_stats.ipReasmOKs, 1);
|
---|
| 317 |
|
---|
| 318 | /* IPv4 ããããè¨å®ããã*/
|
---|
| 319 | frag = ip_frag_queue[ix];
|
---|
| 320 | ip4h = GET_IP4_HDR(input);
|
---|
| 321 | *ip4h = *GET_IP4_HDR(frag);
|
---|
| 322 | ip4h->dst = htonl(frag_dst[ix]);
|
---|
| 323 | ip4h->len = htons(IP4_HDR_SIZE + off);
|
---|
| 324 | ip4h->vhl = IP4_MAKE_VHL(IPV4_VERSION, IP4_HDR_SIZE >> 2);
|
---|
| 325 | ip4h->ttl = GET_QIP4_HDR(prev)->ttl;
|
---|
| 326 | ip4h->flg_off = ip4h->id = 0;
|
---|
| 327 |
|
---|
| 328 | /* ãã¼ã¿ã°ã©ã ãåæ§æããã*/
|
---|
| 329 | off = IP4_HDR_SIZE;
|
---|
| 330 | while (frag != NULL) {
|
---|
| 331 | len = ntohs(GET_QIP4_HDR(frag)->len) - (IP4_VHL_HL(GET_QIP4_HDR(frag)->vhl) << 2);
|
---|
| 332 | memcpy((uint8_t *)ip4h + off, GET_QIP4_SDU(frag), len);
|
---|
| 333 | off += len;
|
---|
| 334 | frag = GET_QIP4_HDR(frag)->next;
|
---|
| 335 | }
|
---|
| 336 | }
|
---|
| 337 | else {
|
---|
| 338 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_IN_NO_BUF], 1);
|
---|
| 339 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_IN_DROP], 1);
|
---|
| 340 | NET_COUNT_MIB(ip_stats.ipReasmFails, 1);
|
---|
| 341 | }
|
---|
| 342 | /* ãã¥ã¼ã空ã«ããã*/
|
---|
| 343 | ip_freef(ix);
|
---|
| 344 | }
|
---|
| 345 | }
|
---|
| 346 | syscall(sig_sem(SEM_IP4_FRAG_QUEUE));
|
---|
| 347 | return input;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | #endif /* of #ifdef IP4_CFG_FRAGMENT */
|
---|
| 351 |
|
---|
| 352 | /*
|
---|
| 353 | * ip_init -- IP ã®åæå
|
---|
| 354 | */
|
---|
| 355 |
|
---|
| 356 | void
|
---|
| 357 | ip_init (void)
|
---|
| 358 | {
|
---|
| 359 | in4_init();
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | /*
|
---|
| 363 | * ip_remove_options -- IPv4 ãããã®ãªãã·ã§ã³ãåé¤ãã以éãåã«è©°ããã
|
---|
| 364 | */
|
---|
| 365 |
|
---|
| 366 | ER
|
---|
| 367 | ip_remove_options (T_NET_BUF *nbuf)
|
---|
| 368 | {
|
---|
| 369 | T_IP4_HDR *iph;
|
---|
| 370 | uint_t hdr_size;
|
---|
| 371 |
|
---|
| 372 | iph = GET_IP4_HDR(nbuf);
|
---|
| 373 | hdr_size = GET_IP4_HDR_SIZE(nbuf);
|
---|
| 374 |
|
---|
| 375 | if (hdr_size > IP4_HDR_SIZE) {
|
---|
| 376 | memmove((char *)iph + IP4_HDR_SIZE, GET_IP4_SDU(nbuf),
|
---|
| 377 | (size_t)(iph->len - hdr_size));
|
---|
| 378 | iph->vhl = IP4_MAKE_VHL(IPV4_VERSION, IP4_HDR_SIZE >> 2);
|
---|
| 379 | iph->len -= (uint16_t)(hdr_size - IP4_HDR_SIZE);
|
---|
| 380 | nbuf->len -= (uint16_t)(hdr_size - IP4_HDR_SIZE);
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | return E_OK;
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | /*
|
---|
| 387 | * ip_input -- IP ã®å
|
---|
| 388 | ¥åé¢æ°
|
---|
| 389 | */
|
---|
| 390 |
|
---|
| 391 | #if defined(SUPPORT_IPSEC)
|
---|
| 392 | /*
|
---|
| 393 | * TODO: IPsec SPãæ¤ç´¢ãããã±ããããªã¸ã§ã¯ããããã©ããå¤å®ããå¦çãå®è£
|
---|
| 394 | ãã
|
---|
| 395 | */
|
---|
| 396 | #endif /* of defined(SUPPORT_IPSEC) */
|
---|
| 397 |
|
---|
| 398 | void
|
---|
| 399 | ip_input (T_NET_BUF *input)
|
---|
| 400 | {
|
---|
| 401 | T_IP4_HDR *ip4h;
|
---|
| 402 | T_IFNET *ifp = IF_GET_IFNET();
|
---|
| 403 | T_IN4_ADDR dst, src, bc;
|
---|
| 404 | uint_t hlen, off;
|
---|
| 405 |
|
---|
| 406 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_OCTETS], input->len - IF_HDR_SIZE);
|
---|
| 407 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_PACKETS], 1);
|
---|
| 408 | NET_COUNT_MIB(ip_stats.ipInReceives, 1);
|
---|
| 409 |
|
---|
| 410 | /* IP ãããã®é·ãããã§ãã¯ããã*/
|
---|
| 411 | if (input->len < IF_IP4_HDR_SIZE) {
|
---|
| 412 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_SHORT], 1);
|
---|
| 413 | NET_COUNT_MIB(ip_stats.ipInHdrErrors, 1);
|
---|
| 414 | goto buf_rel;
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 | ip4h = GET_IP4_HDR(input);
|
---|
| 418 | hlen = GET_IP4_HDR_SIZE(input);
|
---|
| 419 |
|
---|
| 420 | /* ãã¼ã¸ã§ã³ããã§ãã¯ããã*/
|
---|
| 421 | if (IP4_VHL_V(ip4h->vhl) != IPV4_VERSION) {
|
---|
| 422 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_VER], 1);
|
---|
| 423 | NET_COUNT_MIB(ip_stats.ipInHdrErrors, 1);
|
---|
| 424 | goto buf_rel;
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | /* IP ãããã®é·ãããã§ãã¯ãããªãã·ã§ã³ã解æããã*/
|
---|
| 428 | if (hlen > IP4_HDR_SIZE) {
|
---|
| 429 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OPTS], 1);
|
---|
| 430 | /* %%% ãªãã·ã§ã³ã®è§£æ %%% */
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | /* ãã¼ã¿ã°ã©ã é·ããã§ãã¯ããã*/
|
---|
| 434 | if (ntohs(ip4h->len) > input->len - IF_HDR_SIZE) {
|
---|
| 435 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_SHORT], 1);
|
---|
| 436 | NET_COUNT_MIB(ip_stats.ipInHdrErrors, 1);
|
---|
| 437 | goto buf_rel;
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | /* ãããã¯ã¼ã¯ãããã¡ã®é·ãããã¼ã¿ã°ã©ã é·ã«èª¿æ´ããã*/
|
---|
| 441 | input->len = (uint16_t)(ntohs(ip4h->len) + IF_HDR_SIZE);
|
---|
| 442 |
|
---|
| 443 | /* ãã§ãã¯ãµã ããã§ãã¯ããã*/
|
---|
| 444 | if (in_cksum(ip4h, hlen) != 0) {
|
---|
| 445 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_CKSUM], 1);
|
---|
| 446 | NET_COUNT_MIB(ip_stats.ipInHdrErrors, 1);
|
---|
| 447 | goto buf_rel;
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | /* IP ãããã®é·ãããã§ãã¯ããä¸ä½ã ICMP 以å¤ã¯ãªãã·ã§ã³ãæ¶å»ããã*/
|
---|
| 451 | if (hlen > IP4_HDR_SIZE && ip4h->proto != IPPROTO_ICMP) {
|
---|
| 452 | memset((uint8_t*)ip4h + IP4_HDR_SIZE, 0, hlen - IP4_HDR_SIZE);
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | /* éä¿¡å
|
---|
| 456 | ã¢ãã¬ã¹ããã§ãã¯ããã*/
|
---|
| 457 | src = ntohl(ip4h->src);
|
---|
| 458 | bc = (ifp->in4_ifaddr.addr & ifp->in4_ifaddr.mask) | ~ifp->in4_ifaddr.mask;
|
---|
| 459 |
|
---|
| 460 | #ifdef SUPPORT_LOOP
|
---|
| 461 |
|
---|
| 462 | if (src == bc || src == IPV4_ADDR_BROADCAST || src == IPV4_ADDRANY) {
|
---|
| 463 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_ADDR], 1);
|
---|
| 464 | NET_COUNT_MIB(ip_stats.ipInAddrErrors, 1);
|
---|
| 465 | goto buf_rel;
|
---|
| 466 | }
|
---|
| 467 |
|
---|
| 468 | #else /* of #ifdef SUPPORT_LOOP */
|
---|
| 469 |
|
---|
| 470 | if (src == ifp->in4_ifaddr.addr || src == bc || src == IPV4_ADDR_BROADCAST || src == IPV4_ADDRANY) {
|
---|
| 471 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_ADDR], 1);
|
---|
| 472 | NET_COUNT_MIB(ip_stats.ipInAddrErrors, 1);
|
---|
| 473 | goto buf_rel;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | #endif /* of #ifdef SUPPORT_LOOP */
|
---|
| 477 |
|
---|
| 478 | /* ãã¦å
|
---|
| 479 | ã¢ãã¬ã¹ããã§ãã¯ããã*/
|
---|
| 480 | dst = ntohl(ip4h->dst);
|
---|
| 481 |
|
---|
| 482 | #ifdef DHCP_CFG
|
---|
| 483 |
|
---|
| 484 | /*
|
---|
| 485 | * DHCP_CFG ãå®ç¾©ããã¦ããã¨ãã¯ããã¼ã«ã«ã¢ãã¬ã¹ãæªå®ç¾©ã®
|
---|
| 486 | * å ´åããã¼ã¿ã°ã©ã ãåä¿¡ããã
|
---|
| 487 | */
|
---|
| 488 |
|
---|
| 489 | #if defined(ETHER_CFG_MULTICAST)
|
---|
| 490 | if ((ifp->in4_ifaddr.addr != IPV4_ADDRANY) &&
|
---|
| 491 | (!(dst == ifp->in4_ifaddr.addr || dst == bc ||
|
---|
| 492 | dst == IPV4_ADDR_BROADCAST || dst == IPV4_ADDRANY ||
|
---|
| 493 | IN4_IS_ADDR_MULTICAST(dst)))) {
|
---|
| 494 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_ADDR], 1);
|
---|
| 495 | NET_COUNT_MIB(ip_stats.ipInAddrErrors, 1);
|
---|
| 496 | goto buf_rel;
|
---|
| 497 | }
|
---|
| 498 | #else
|
---|
| 499 | if ((ifp->in4_ifaddr.addr != IPV4_ADDRANY) &&
|
---|
| 500 | (!(dst == ifp->in4_ifaddr.addr || dst == bc ||
|
---|
| 501 | dst == IPV4_ADDR_BROADCAST || dst == IPV4_ADDRANY))) {
|
---|
| 502 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_ADDR], 1);
|
---|
| 503 | NET_COUNT_MIB(ip_stats.ipInAddrErrors, 1);
|
---|
| 504 | goto buf_rel;
|
---|
| 505 | }
|
---|
| 506 | #endif
|
---|
| 507 |
|
---|
| 508 | #else /* of #ifdef DHCP_CFG */
|
---|
| 509 |
|
---|
| 510 | #if defined(ETHER_CFG_MULTICAST)
|
---|
| 511 | if (!(dst == ifp->in4_ifaddr.addr || dst == bc ||
|
---|
| 512 | dst == IPV4_ADDR_BROADCAST || dst == IPV4_ADDRANY ||
|
---|
| 513 | IN4_IS_ADDR_MULTICAST(dst))) {
|
---|
| 514 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_ADDR], 1);
|
---|
| 515 | NET_COUNT_MIB(ip_stats.ipInAddrErrors, 1);
|
---|
| 516 | goto buf_rel;
|
---|
| 517 | }
|
---|
| 518 | #else
|
---|
| 519 | if (!(dst == ifp->in4_ifaddr.addr || dst == bc ||
|
---|
| 520 | dst == IPV4_ADDR_BROADCAST || dst == IPV4_ADDRANY)) {
|
---|
| 521 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_ADDR], 1);
|
---|
| 522 | NET_COUNT_MIB(ip_stats.ipInAddrErrors, 1);
|
---|
| 523 | goto buf_rel;
|
---|
| 524 | }
|
---|
| 525 | #endif
|
---|
| 526 |
|
---|
| 527 | #endif /* of #ifdef DHCP_CFG */
|
---|
| 528 |
|
---|
| 529 | #ifdef IP4_CFG_FRAGMENT
|
---|
| 530 |
|
---|
| 531 | /* åå²ããã¦ããããã§ãã¯ããã*/
|
---|
| 532 | if (ntohs(ip4h->flg_off) & (IP4_MF | IP4_OFFMASK)) {
|
---|
| 533 | if ((input = ip_reass(ip4h, input)) == NULL)
|
---|
| 534 | return;
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | #else /* of #ifdef IP4_CFG_FRAGMENT */
|
---|
| 538 |
|
---|
| 539 | /* åå²ããã¦ããããã§ãã¯ããã*/
|
---|
| 540 | if (ntohs(ip4h->flg_off) & (IP4_MF | IP4_OFFMASK)) {
|
---|
| 541 | T_IN4_ADDR src;
|
---|
| 542 |
|
---|
| 543 | NET_COUNT_MIB(ip_stats.ipReasmReqds, 1);
|
---|
| 544 | if ((ntohs(ip4h->flg_off) & IP4_OFFMASK) == 0) {
|
---|
| 545 | NET_COUNT_MIB(ip_stats.ipReasmFails, 1);
|
---|
| 546 | }
|
---|
| 547 | src = ntohl(ip4h->src);
|
---|
| 548 | syslog(LOG_WARNING, "[IP] flaged src: %s.", ip2str(NULL, &src));
|
---|
| 549 | goto buf_rel;
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | #endif /* of #ifdef IP4_CFG_FRAGMENT */
|
---|
| 553 |
|
---|
| 554 | off = (uint_t)(GET_IP4_SDU(input) - input->buf);
|
---|
| 555 |
|
---|
| 556 | #if defined(SUPPORT_IPSEC)
|
---|
| 557 | /* ããã§ipsec4_in_rejectãå®è¡ãã */
|
---|
| 558 | if ((ip4h->proto != IPPROTO_ESP) && ipsec4_in_reject (input)) {
|
---|
| 559 | goto buf_rel;
|
---|
| 560 | }
|
---|
| 561 | #endif /* of #if defined(SUPPORT_IPSEC) */
|
---|
| 562 |
|
---|
| 563 | /* ãããã³ã«ãé¸æãã */
|
---|
| 564 | switch (ip4h->proto) {
|
---|
| 565 |
|
---|
| 566 | #if defined(SUPPORT_TCP)
|
---|
| 567 | case IPPROTO_TCP:
|
---|
| 568 | NET_COUNT_MIB(ip_stats.ipInDelivers, 1);
|
---|
| 569 | tcp_input(&input, &off, NULL);
|
---|
| 570 | return;
|
---|
| 571 | break;
|
---|
| 572 | #endif /* of #if defined(SUPPORT_TCP) */
|
---|
| 573 |
|
---|
| 574 | #if defined(SUPPORT_UDP) && ( (TNUM_UDP4_CEPID > 0) || \
|
---|
| 575 | ((TNUM_UDP6_CEPID > 0) && defined(API_CFG_IP4MAPPED_ADDR)))
|
---|
| 576 | case IPPROTO_UDP:
|
---|
| 577 | NET_COUNT_MIB(ip_stats.ipInDelivers, 1);
|
---|
| 578 | udp4_input(&input, &off, NULL);
|
---|
| 579 | return;
|
---|
| 580 | break;
|
---|
| 581 | #endif /* of #if defined(SUPPORT_UDP) && TNUM_UDP4_CEPID > 0 */
|
---|
| 582 |
|
---|
| 583 | case IPPROTO_ICMP:
|
---|
| 584 | NET_COUNT_MIB(ip_stats.ipInDelivers, 1);
|
---|
| 585 | icmp_input(&input, &off, NULL);
|
---|
| 586 | return;
|
---|
| 587 | break;
|
---|
| 588 |
|
---|
| 589 | #if defined(SUPPORT_IGMP)
|
---|
| 590 | case IPPROTO_IGMP:
|
---|
| 591 | NET_COUNT_MIB(ip_stats.ipInDelivers, 1);
|
---|
| 592 | igmp_input(&input, &off, NULL);
|
---|
| 593 | return;
|
---|
| 594 | break;
|
---|
| 595 | #endif /* of #ifdef UDP_CFG_EXTENTIONS */
|
---|
| 596 |
|
---|
| 597 | #if defined(SUPPORT_IPSEC)
|
---|
| 598 | case IPPROTO_ESP:
|
---|
| 599 | NET_COUNT_MIB(ip_stats.ipInDelivers, 1);
|
---|
| 600 | esp4_input(input, &off);
|
---|
| 601 | return;
|
---|
| 602 | break;
|
---|
| 603 | #endif /* of #if defined(SUPPORT_IPSEC) */
|
---|
| 604 |
|
---|
| 605 | default:
|
---|
| 606 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_PROTO], 1);
|
---|
| 607 | NET_COUNT_MIB(ip_stats.ipInUnknownProtos, 1);
|
---|
| 608 |
|
---|
| 609 | /* ãã¼ã«ã« IP ã¢ãã¬ã¹ã«å±ãããã¼ã¿ã°ã©ã ã®ã¿ ICMP ã¨ã©ã¼ãéç¥ããã*/
|
---|
| 610 | if (dst == ifp->in4_ifaddr.addr) {
|
---|
| 611 | T_IN4_ADDR src;
|
---|
| 612 |
|
---|
| 613 | src = ntohl(ip4h->src);
|
---|
| 614 | syslog(LOG_INFO, "[IP] unexp proto: %d, src=%s.", ip4h->proto, ip2str(NULL, &src));
|
---|
| 615 | icmp_error(ICMP4_UNREACH_PROTOCOL, input);
|
---|
| 616 | }
|
---|
| 617 | /*
|
---|
| 618 | * icmp_error ã§ã¯ããããã¯ã¼ã¯ãããã¡ input ãè¿å´ããªãã®ã§
|
---|
| 619 | * éæ¾ãã¦ããçµäºããã
|
---|
| 620 | */
|
---|
| 621 | break;
|
---|
| 622 | }
|
---|
| 623 |
|
---|
| 624 | buf_rel:
|
---|
| 625 | NET_COUNT_IP4(net_count_ip4[NC_IP4_IN_ERR_PACKETS], 1);
|
---|
| 626 | syscall(rel_net_buf(input));
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | #endif /* of #if defined(_IP4_CFG) */
|
---|