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