[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_icmp.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 | * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94
|
---|
| 79 | * $FreeBSD: src/sys/netinet/ip_icmp.c,v 1.33.2.3 1999/10/14 11:49:38 des Exp $
|
---|
| 80 | */
|
---|
| 81 |
|
---|
| 82 | #include <string.h>
|
---|
| 83 |
|
---|
| 84 | #ifdef TARGET_KERNEL_ASP
|
---|
| 85 |
|
---|
| 86 | #include <kernel.h>
|
---|
| 87 | #include <sil.h>
|
---|
| 88 | #include <t_syslog.h>
|
---|
| 89 | #include "kernel_cfg.h"
|
---|
| 90 | #include "tinet_cfg.h"
|
---|
| 91 |
|
---|
| 92 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
| 93 |
|
---|
| 94 | #ifdef TARGET_KERNEL_JSP
|
---|
| 95 |
|
---|
| 96 | #include <s_services.h>
|
---|
| 97 | #include <t_services.h>
|
---|
| 98 | #include "tinet_id.h"
|
---|
| 99 |
|
---|
| 100 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
| 101 |
|
---|
| 102 | #include <tinet_defs.h>
|
---|
| 103 | #include <tinet_config.h>
|
---|
| 104 |
|
---|
| 105 | #include <net/if.h>
|
---|
| 106 | #include <net/if_loop.h>
|
---|
| 107 | #include <net/if_ppp.h>
|
---|
| 108 | #include <net/ethernet.h>
|
---|
| 109 | #include <net/ppp_ipcp.h>
|
---|
| 110 | #include <net/net.h>
|
---|
| 111 | #include <net/net_endian.h>
|
---|
| 112 | #include <net/net_buf.h>
|
---|
| 113 | #include <net/net_timer.h>
|
---|
| 114 | #include <net/net_count.h>
|
---|
| 115 |
|
---|
| 116 | #include <netinet/in.h>
|
---|
| 117 | #include <netinet/in_var.h>
|
---|
| 118 | #include <netinet/ip.h>
|
---|
| 119 | #include <netinet/ip_var.h>
|
---|
| 120 | #include <netinet/ip_icmp.h>
|
---|
| 121 | #include <netinet/icmp_var.h>
|
---|
| 122 | #include <netinet/tcp.h>
|
---|
| 123 | #include <netinet/tcp_var.h>
|
---|
| 124 | #include <netinet/udp_var.h>
|
---|
| 125 |
|
---|
| 126 | #include <net/if_var.h>
|
---|
| 127 |
|
---|
| 128 | #if defined(_IP4_CFG)
|
---|
| 129 |
|
---|
| 130 | /*
|
---|
| 131 | * å¤æ°
|
---|
| 132 | */
|
---|
| 133 |
|
---|
| 134 | #ifdef SUPPORT_MIB
|
---|
| 135 |
|
---|
| 136 | /*
|
---|
| 137 | * SNMP 㮠管çæ
|
---|
| 138 | å ±ãã¼ã¹ (MIB)
|
---|
| 139 | */
|
---|
| 140 |
|
---|
| 141 | T_ICMP_STATS icmp_stats;
|
---|
| 142 |
|
---|
| 143 | #endif /* of #ifdef SUPPORT_MIB */
|
---|
| 144 |
|
---|
| 145 | /*
|
---|
| 146 | * é¢æ°
|
---|
| 147 | */
|
---|
| 148 |
|
---|
| 149 | static void icmp_echo (T_NET_BUF *input, uint_t ihoff);
|
---|
| 150 | static void icmp_unreach (T_NET_BUF *input, uint_t ihoff);
|
---|
| 151 |
|
---|
| 152 | #if defined(NUM_REDIRECT_ROUTE_ENTRY)
|
---|
| 153 | #if NUM_REDIRECT_ROUTE_ENTRY > 0
|
---|
| 154 |
|
---|
| 155 | static void icmp_redirect (T_NET_BUF *input, uint_t ihoff);
|
---|
| 156 |
|
---|
| 157 | #endif /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
|
---|
| 158 | #endif /* of #if defined(NUM_REDIRECT_ROUTE_ENTRY) */
|
---|
| 159 |
|
---|
| 160 | /*
|
---|
| 161 | * icmp_echo -- ã¨ã³ã¼è¦æ±ãåä¿¡ããã¨ãã®å¦ç
|
---|
| 162 | *
|
---|
| 163 | * input ã«ã¯ IF ããã㨠IP ããããå«ã¾ãã¦ããã
|
---|
| 164 | */
|
---|
| 165 |
|
---|
| 166 | static void
|
---|
| 167 | icmp_echo (T_NET_BUF *input, uint_t ihoff)
|
---|
| 168 | {
|
---|
| 169 | T_IP4_HDR *ip4h;
|
---|
| 170 | T_ICMP4_HDR *icmp4h;
|
---|
| 171 | T_IFNET *ifp = IF_GET_IFNET();
|
---|
| 172 |
|
---|
| 173 | NET_COUNT_MIB(icmp_stats.icmpInEchos, 1);
|
---|
| 174 |
|
---|
| 175 | /* IPv4 ã¢ãã¬ã¹ãæªå®ç¾©ã®æã¯å¿çããªãã*/
|
---|
| 176 | if (!IN4_IS_ADDR_ANY(&ifp->in4_ifaddr.addr)) {
|
---|
| 177 |
|
---|
| 178 | /*
|
---|
| 179 | * ã¡ãã»ã¼ã¸ã®åãã¨ã³ã¼è¦æ± (8) ãã
|
---|
| 180 | * ã¨ã³ã¼å¿ç (0) ã«å¤æ´ãã¦éãè¿ãã
|
---|
| 181 | */
|
---|
| 182 |
|
---|
| 183 | icmp4h = GET_ICMP4_HDR(input, ihoff);
|
---|
| 184 | icmp4h->type = ICMP4_ECHO_REPLY;
|
---|
| 185 |
|
---|
| 186 | /*
|
---|
| 187 | * å®å
|
---|
| 188 | ã¢ãã¬ã¹ã¯åä¿¡ããã¡ãã»ã¼ã¸ã®éä¿¡å
|
---|
| 189 | ã¢ãã¬ã¹ã
|
---|
| 190 | * éä¿¡å
|
---|
| 191 | ã¢ãã¬ã¹ã¯èª IPv4 ã¢ãã¬ã¹ã
|
---|
| 192 | */
|
---|
| 193 | ip4h = GET_IP4_HDR(input);
|
---|
| 194 | ip4h->dst = ip4h->src;
|
---|
| 195 | ip4h->src = htonl(ifp->in4_ifaddr.addr);
|
---|
| 196 |
|
---|
| 197 | /* ãã§ãã¯ãµã ãè¨ç®ããã*/
|
---|
| 198 | icmp4h->sum = 0;
|
---|
| 199 | icmp4h->sum = in_cksum(icmp4h,
|
---|
| 200 | (uint_t)(((input->len - GET_IF_IP4_HDR_SIZE(input)) + 3) >> 2 << 2));
|
---|
| 201 |
|
---|
| 202 | /* éä¿¡ããã*/
|
---|
| 203 | NET_COUNT_ICMP4(net_count_icmp4.out_octets,
|
---|
| 204 | input->len - GET_IF_IP4_HDR_SIZE(input));
|
---|
| 205 | NET_COUNT_ICMP4(net_count_icmp4.out_packets, 1);
|
---|
| 206 | NET_COUNT_MIB(icmp_stats.icmpOutMsgs, 1);
|
---|
| 207 | NET_COUNT_MIB(icmp_stats.icmpOutEchoReps, 1);
|
---|
| 208 | ip_output(input, TMO_ICMP_OUTPUT);
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | /*
|
---|
| 213 | * icmp_unreach -- ICMP4_UNREACH ãåä¿¡ããã¨ãã®å¦ç
|
---|
| 214 | */
|
---|
| 215 |
|
---|
| 216 | static const int8_t code2error[] = {
|
---|
| 217 | EV_HURCH, /* ICMP4_UNREACH_NET 0 */
|
---|
| 218 | EV_HURCH, /* ICMP4_UNREACH_HOST 1 */
|
---|
| 219 | EV_CNNRF, /* ICMP4_UNREACH_PROTOCOL 2 */
|
---|
| 220 | EV_CNNRF, /* ICMP4_UNREACH_PORT 3 */
|
---|
| 221 | EV_MSIZE, /* ICMP4_UNREACH_FLAG 4 */
|
---|
| 222 | EV_HURCH, /* ICMP4_UNREACH_SRC_FAIL 5 */
|
---|
| 223 | EV_HURCH, /* ICMP4_UNREACH_NET_UNKNOWN 6 */
|
---|
| 224 | EV_HURCH, /* ICMP4_UNREACH_HOST_UNKNOWN 7 */
|
---|
| 225 | EV_HURCH, /* ICMP4_UNREACH_ISOLATED 8 */
|
---|
| 226 | EV_HURCH, /* ICMP4_UNREACH_NET_PROHIB 9 */
|
---|
| 227 | EV_HURCH, /* ICMP4_UNREACH_HOST_PROHIB 10 */
|
---|
| 228 | EV_HURCH, /* ICMP4_UNREACH_TOS_NET 11 */
|
---|
| 229 | EV_HURCH, /* ICMP4_UNREACH_TOS_HOST 12 */
|
---|
| 230 | EV_HURCH, /* ICMP4_UNREACH_FILTER_PROHIB 13 */
|
---|
| 231 | EV_HURCH, /* ICMP4_UNREACH_HOST_PRECEDENCE 14 */
|
---|
| 232 | EV_HURCH, /* ICMP4_UNREACH_PRECEDENCE_CUTOFF 15 */
|
---|
| 233 | };
|
---|
| 234 |
|
---|
| 235 | static void
|
---|
| 236 | icmp_unreach (T_NET_BUF *input, uint_t ihoff)
|
---|
| 237 | {
|
---|
| 238 | T_IP4_HDR *ip4h;
|
---|
| 239 | uint8_t code;
|
---|
| 240 | ER error;
|
---|
| 241 |
|
---|
| 242 | NET_COUNT_MIB(icmp_stats.icmpInDestUnreachs, 1);
|
---|
| 243 |
|
---|
| 244 | ip4h = (T_IP4_HDR*)GET_ICMP4_SDU(input, ihoff);
|
---|
| 245 | code = GET_ICMP4_HDR(input, ihoff)->code;
|
---|
| 246 | error = code2error[code];
|
---|
| 247 |
|
---|
| 248 | /* æçµãããã TCP/UDP ã®ã¿å¯¾å¿ããã*/
|
---|
| 249 | if (ip4h->proto == IPPROTO_TCP || ip4h->proto == IPPROTO_UDP) {
|
---|
| 250 |
|
---|
| 251 | memcpy(GET_IP4_HDR(input), ip4h, input->len - (IP4_HDR_SIZE + ICMP4_HDR_SIZE));
|
---|
| 252 | input->len -= IP4_HDR_SIZE + ICMP4_HDR_SIZE;
|
---|
| 253 |
|
---|
| 254 | #if defined(SUPPORT_TCP)
|
---|
| 255 |
|
---|
| 256 | if (ip4h->proto == IPPROTO_TCP)
|
---|
| 257 | tcp_notify(input, error);
|
---|
| 258 |
|
---|
| 259 | #endif /* of #if defined(SUPPORT_TCP) */
|
---|
| 260 |
|
---|
| 261 | #if defined(SUPPORT_UDP) && TNUM_UDP4_CEPID > 0
|
---|
| 262 |
|
---|
| 263 | if (ip4h->proto == IPPROTO_UDP)
|
---|
| 264 | udp4_notify(input, error);
|
---|
| 265 |
|
---|
| 266 | #endif /* of #if defined(SUPPORT_UDP) && TNUM_UDP4_CEPID > 0 */
|
---|
| 267 |
|
---|
| 268 | }
|
---|
| 269 | else
|
---|
| 270 | syslog(LOG_NOTICE, "[ICMP] error, code: %d.", code);
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | #if defined(NUM_REDIRECT_ROUTE_ENTRY)
|
---|
| 274 | #if NUM_REDIRECT_ROUTE_ENTRY > 0
|
---|
| 275 |
|
---|
| 276 | /*
|
---|
| 277 | * icmp_redirect -- åãç´ãã¡ãã»ã¼ã¸ãåä¿¡ããã¨ãã®å¦ç
|
---|
| 278 | *
|
---|
| 279 | * input ã«ã¯ IF ããã㨠IP ããããå«ã¾ãã¦ããã
|
---|
| 280 | */
|
---|
| 281 |
|
---|
| 282 | static void
|
---|
| 283 | icmp_redirect (T_NET_BUF *input, uint_t ihoff)
|
---|
| 284 | {
|
---|
| 285 | T_IP4_HDR *ip4h, *sip4h;
|
---|
| 286 | T_ICMP4_HDR *icmp4h;
|
---|
| 287 |
|
---|
| 288 | ip4h = GET_IP4_HDR(input);
|
---|
| 289 | icmp4h = GET_ICMP4_HDR(input, ihoff);
|
---|
| 290 | sip4h = (T_IP4_HDR *)GET_ICMP4_SDU(input, ihoff);
|
---|
| 291 |
|
---|
| 292 | /*
|
---|
| 293 | * å
|
---|
| 294 | 容ãã§ãã¯ã以ä¸ã®å ´åã¯ã¨ã©ã¼
|
---|
| 295 | *
|
---|
| 296 | * ã»ICMP ã³ã¼ãããICMP4_REDIRECT_PORT_AND_HOST (3) 以ä¸
|
---|
| 297 | * ã»ãã¼ã¿ã°ã©ã é·ããICMP ã®æå°é·ããçã (IP ããã + ICMP ããã + IP ããã + 8)
|
---|
| 298 | * ã»ãã¼ã¿ã°ã©ã é·ããICMP ã®æ大é·ããçã
|
---|
| 299 | * ã»
|
---|
| 300 | */
|
---|
| 301 | if (icmp4h->code > ICMP4_REDIRECT_PORT_AND_HOST ||
|
---|
| 302 | ip4h->len < GET_IP4_ICMP4_HDR_SIZE(input) + 8 + IP4_HDR_SIZE ||
|
---|
| 303 | ip4h->len < GET_IP4_ICMP4_HDR_SIZE(input) + 8 + (IP4_VHL_HL(sip4h->vhl) << 2)) {
|
---|
| 304 | NET_COUNT_ICMP4(net_count_icmp4.in_err_packets, 1);
|
---|
| 305 | return;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | in4_rtredirect(icmp4h->data.addr, sip4h->dst, IN_RTF_REDIRECT, TMO_IN_REDIRECT);
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | #endif /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
|
---|
| 312 | #endif /* of #if defined(NUM_REDIRECT_ROUTE_ENTRY) */
|
---|
| 313 |
|
---|
| 314 | /*
|
---|
| 315 | * icmp_input -- ICMP ã®å
|
---|
| 316 | ¥åé¢æ°
|
---|
| 317 | *
|
---|
| 318 | * input ã«ã¯ IF ããã㨠IP ããããå«ã¾ãã¦ããã
|
---|
| 319 | */
|
---|
| 320 |
|
---|
| 321 | uint_t
|
---|
| 322 | icmp_input (T_NET_BUF **inputp, uint_t *offp, uint_t *nextp)
|
---|
| 323 | {
|
---|
| 324 | T_NET_BUF *input = *inputp;
|
---|
| 325 | T_ICMP4_HDR *icmp4h;
|
---|
| 326 | T_IN4_ADDR addr;
|
---|
| 327 | uint_t len, align;
|
---|
| 328 |
|
---|
| 329 | NET_COUNT_ICMP4(net_count_icmp4.in_octets,
|
---|
| 330 | input->len - GET_IF_IP4_HDR_SIZE(input));
|
---|
| 331 | NET_COUNT_ICMP4(net_count_icmp4.in_packets, 1);
|
---|
| 332 |
|
---|
| 333 | /* ICMP ãããã®é·ãããã§ãã¯ããã*/
|
---|
| 334 | if (input->len < IF_IP4_ICMP4_HDR_SIZE) {
|
---|
| 335 | NET_COUNT_ICMP4(net_count_icmp4.in_err_packets, 1);
|
---|
| 336 | NET_COUNT_MIB(icmp_stats.icmpInErrors, 1);
|
---|
| 337 | goto buf_rel;
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | icmp4h = (T_ICMP4_HDR *)(input->buf + *offp);
|
---|
| 341 |
|
---|
| 342 | /* 4 ãªã¯ãããå¢çã®ãã¼ã¿é· */
|
---|
| 343 | len = input->len - *offp;
|
---|
| 344 | align = (len + 3) >> 2 << 2;
|
---|
| 345 |
|
---|
| 346 | /* 4 ãªã¯ãããå¢çã¾ã§ããã£ã³ã°ã§åããã*/
|
---|
| 347 | if (align > len)
|
---|
| 348 | memset((uint8_t*)input->buf + input->len, 0, (size_t)(align - len));
|
---|
| 349 |
|
---|
| 350 | /* ãã§ãã¯ãµã ãè¨ç®ããã*/
|
---|
| 351 | if (in_cksum(icmp4h, align) != 0) {
|
---|
| 352 | NET_COUNT_ICMP4(net_count_icmp4.in_err_packets, 1);
|
---|
| 353 | goto buf_rel;
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | /* ã¡ãã»ã¼ã¸ã®åã«ããåå²ããã*/
|
---|
| 357 | switch (icmp4h->type) {
|
---|
| 358 | case ICMP4_ECHO_REQUEST:
|
---|
| 359 | icmp_echo(input, *offp);
|
---|
| 360 | return IPPROTO_DONE;
|
---|
| 361 | break;
|
---|
| 362 | case ICMP4_ECHO_REPLY:
|
---|
| 363 |
|
---|
| 364 | #if defined(SUPPORT_INET4) && defined(ICMP_CFG_CALLBACK_ECHO_REPLY)
|
---|
| 365 |
|
---|
| 366 | icmp_echo_reply(input, *offp);
|
---|
| 367 |
|
---|
| 368 | #endif /* of #if defined(SUPPORT_INET4) && defined(ICMP_CFG_CALLBACK_ECHO_REPLY) */
|
---|
| 369 |
|
---|
| 370 | break;
|
---|
| 371 | case ICMP4_UNREACH:
|
---|
| 372 | icmp_unreach(input, *offp);
|
---|
| 373 | break;
|
---|
| 374 | case ICMP4_REDIRECT:
|
---|
| 375 |
|
---|
| 376 | #if defined(NUM_REDIRECT_ROUTE_ENTRY)
|
---|
| 377 | #if NUM_REDIRECT_ROUTE_ENTRY > 0
|
---|
| 378 |
|
---|
| 379 | addr = ntohl(icmp4h->data.addr);
|
---|
| 380 | syslog(LOG_INFO, "[ICMP] redirect, addr: %s.", ip2str(NULL, &addr));
|
---|
| 381 | icmp_redirect(input, *offp);
|
---|
| 382 |
|
---|
| 383 | #else /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
|
---|
| 384 |
|
---|
| 385 | addr = ntohl(icmp4h->data.addr);
|
---|
| 386 | syslog(LOG_INFO, "[ICMP] redirect ignored, addr: %s.", ip2str(NULL, &addr));
|
---|
| 387 |
|
---|
| 388 | #endif /* of #if NUM_REDIRECT_ROUTE_ENTRY > 0 */
|
---|
| 389 | #else /* of #if defined(NUM_REDIRECT_ROUTE_ENTRY) */
|
---|
| 390 |
|
---|
| 391 | addr = ntohl(icmp4h->data.addr);
|
---|
| 392 | syslog(LOG_INFO, "[ICMP] redirect ignored, addr: %s.", ip2str(NULL, &addr));
|
---|
| 393 |
|
---|
| 394 | #endif /* of #if defined(NUM_REDIRECT_ROUTE_ENTRY) */
|
---|
| 395 |
|
---|
| 396 | break;
|
---|
| 397 | default:
|
---|
| 398 | syslog(LOG_INFO, "[ICMP] unknown type: %d.", icmp4h->type);
|
---|
| 399 | NET_COUNT_ICMP4(net_count_icmp4.in_err_packets, 1);
|
---|
| 400 | break;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | buf_rel:
|
---|
| 404 | syscall(rel_net_buf(input));
|
---|
| 405 | return IPPROTO_DONE;
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | #ifdef ICMP_REPLY_ERROR
|
---|
| 409 |
|
---|
| 410 | /*
|
---|
| 411 | * icmp_error -- ICMP ã¨ã©ã¼éä¿¡é¢æ°ã
|
---|
| 412 | *
|
---|
| 413 | * input ã«ã¯ IF ããã㨠IP ããããå«ã¾ãã¦ããã
|
---|
| 414 | * icmp_error ã§ã¯ããããã¯ã¼ã¯ãããã¡ input ãè¿å´ããªãã®ã§ã
|
---|
| 415 | * å¼åºãå´ã§ãéæ¾ãã¦ããçµäºããã
|
---|
| 416 | */
|
---|
| 417 |
|
---|
| 418 | void
|
---|
| 419 | icmp_error (uint8_t code, T_NET_BUF *input)
|
---|
| 420 | {
|
---|
| 421 | T_IP4_HDR *ip4h;
|
---|
| 422 | T_ICMP4_HDR *icmp4h;
|
---|
| 423 | T_NET_BUF *output;
|
---|
| 424 | T_IN4_ADDR saddr;
|
---|
| 425 | uint_t len, ip4hl, align;
|
---|
| 426 |
|
---|
| 427 | ip4h = GET_IP4_HDR(input);
|
---|
| 428 | ip4hl = GET_IP4_HDR_SIZE(input);
|
---|
| 429 |
|
---|
| 430 | /* éä¿¡ç¨ã® IP ãã¼ã¿ã°ã©ã ãç²å¾ããã*/
|
---|
| 431 | if (input->len - ip4hl < 8)
|
---|
| 432 | len = input->len - ip4hl;
|
---|
| 433 | else
|
---|
| 434 | len = 8;
|
---|
| 435 |
|
---|
| 436 | saddr = ntohl(ip4h->src);
|
---|
| 437 | if (in4_get_datagram(&output, (uint_t)(ICMP4_HDR_SIZE + ip4hl + len), 0,
|
---|
| 438 | &saddr, NULL, IPPROTO_ICMP, IP4_DEFTTL,
|
---|
| 439 | NBA_SEARCH_ASCENT, TMO_ICMP_OUTPUT) != E_OK)
|
---|
| 440 | return;
|
---|
| 441 |
|
---|
| 442 | /* ICMP ããããè¨å®ããã*/
|
---|
| 443 | icmp4h = GET_ICMP4_HDR(output, IF_IP4_ICMP4_HDR_OFFSET);
|
---|
| 444 | icmp4h->type = ICMP4_UNREACH;
|
---|
| 445 | icmp4h->code = code;
|
---|
| 446 | icmp4h->data.addr= 0;
|
---|
| 447 |
|
---|
| 448 | /* ã¨ã©ã¼ãçºçãã IP ããã㨠ãã¼ã¿ 8 ãªã¯ããããã³ãã¼ããã*/
|
---|
| 449 | memcpy(GET_ICMP4_SDU(output, IF_IP4_ICMP4_HDR_OFFSET),
|
---|
| 450 | GET_IP4_HDR(input), (size_t)(ip4hl + len));
|
---|
| 451 |
|
---|
| 452 | /* 4 ãªã¯ãããå¢çã®ãã¼ã¿é· */
|
---|
| 453 | align = (len + 3) >> 2 << 2;
|
---|
| 454 |
|
---|
| 455 | /* 4 ãªã¯ãããå¢çã¾ã§ããã£ã³ã°ã§åããã*/
|
---|
| 456 | if (align > len)
|
---|
| 457 | memset((uint8_t*)GET_ICMP4_SDU(output, IF_IP4_ICMP4_HDR_OFFSET) + ip4hl + len,
|
---|
| 458 | 0, (size_t)(align - len));
|
---|
| 459 |
|
---|
| 460 | /* ãã§ãã¯ãµã ãè¨ç®ããã*/
|
---|
| 461 | icmp4h->sum = 0;
|
---|
| 462 | icmp4h->sum = in_cksum(icmp4h, (uint_t)(ICMP4_HDR_SIZE + ip4hl + align));
|
---|
| 463 |
|
---|
| 464 | /* éä¿¡ããã*/
|
---|
| 465 | NET_COUNT_ICMP4(net_count_icmp4.out_octets,
|
---|
| 466 | output->len - GET_IF_IP4_HDR_SIZE(output));
|
---|
| 467 | NET_COUNT_ICMP4(net_count_icmp4.out_packets, 1);
|
---|
| 468 | NET_COUNT_MIB(icmp_stats.icmpOutMsgs, 1);
|
---|
| 469 | NET_COUNT_MIB(icmp_stats.icmpOutDestUnreachs, 1);
|
---|
| 470 | ip_output(output, TMO_ICMP_OUTPUT);
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | #endif /* of #ifdef ICMP_REPLY_ERROR */
|
---|
| 474 |
|
---|
| 475 | #endif /* of #if defined(_IP4_CFG) */
|
---|