[313] | 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_output.c 313 2017-07-23 04:50:32Z coas-nagasima $
|
---|
| 44 | */
|
---|
| 45 |
|
---|
| 46 | /*
|
---|
| 47 | * Copyright (c) 1982, 1986, 1988, 1990, 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 | * @(#)output.c 8.3 (Berkeley) 1/21/94
|
---|
| 79 | * $FreeBSD: src/sys/netinet/output.c,v 1.85.2.4 1999/08/29 16:29:49 peter Exp $
|
---|
| 80 | */
|
---|
| 81 |
|
---|
| 82 | #include <string.h>
|
---|
| 83 |
|
---|
| 84 | #ifdef TARGET_KERNEL_ASP
|
---|
| 85 |
|
---|
| 86 | #include <kernel.h>
|
---|
| 87 | #include <sil.h>
|
---|
| 88 |
|
---|
| 89 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
| 90 |
|
---|
| 91 | #ifdef TARGET_KERNEL_JSP
|
---|
| 92 |
|
---|
| 93 | #include <s_services.h>
|
---|
| 94 | #include <t_services.h>
|
---|
| 95 |
|
---|
| 96 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
| 97 |
|
---|
| 98 | #include <tinet_defs.h>
|
---|
| 99 | #include <tinet_config.h>
|
---|
| 100 |
|
---|
| 101 | #include <net/if.h>
|
---|
| 102 | #include <net/if_loop.h>
|
---|
| 103 | #include <net/if_ppp.h>
|
---|
| 104 | #include <net/ethernet.h>
|
---|
| 105 | #include <net/ppp.h>
|
---|
| 106 | #include <net/net.h>
|
---|
| 107 | #include <net/net_endian.h>
|
---|
| 108 | #include <net/net_buf.h>
|
---|
| 109 | #include <net/net_count.h>
|
---|
| 110 |
|
---|
| 111 | #include <netinet/in.h>
|
---|
| 112 | #include <netinet/in_var.h>
|
---|
| 113 | #include <netinet/ip.h>
|
---|
| 114 | #include <netinet/ip_var.h>
|
---|
| 115 |
|
---|
| 116 | #if defined(SUPPORT_IPSEC)
|
---|
| 117 | #include <netinet6/ipsec.h>
|
---|
| 118 | #endif
|
---|
| 119 |
|
---|
| 120 | #if defined(_IP4_CFG)
|
---|
| 121 |
|
---|
| 122 | static uint16_t frag_id = 0;
|
---|
| 123 |
|
---|
| 124 | /*
|
---|
| 125 | * IP output -- IP ã®åºåé¢æ°
|
---|
| 126 | *
|
---|
| 127 | * 注æ: ãã¼ã¸ã§ã³ã¨ãããé·ã¯ä¸ä½å±¤ã§è¨å®ããã
|
---|
| 128 | */
|
---|
| 129 |
|
---|
| 130 | ER
|
---|
| 131 | ip_output (T_NET_BUF *output, TMO tmout)
|
---|
| 132 | {
|
---|
| 133 | T_IP4_HDR *ip4h;
|
---|
| 134 | ER error = E_OK;
|
---|
| 135 | T_IN4_ADDR gw;
|
---|
| 136 |
|
---|
| 137 | #ifdef SUPPORT_IPSEC
|
---|
| 138 | T_SECPOLICY* sp;
|
---|
| 139 | int ipsec_error;
|
---|
| 140 | int policy;
|
---|
| 141 | #endif /* SUPPORT_IPSEC */
|
---|
| 142 |
|
---|
| 143 | NET_COUNT_MIB(ip_stats.ipOutRequests, 1);
|
---|
| 144 |
|
---|
| 145 | #ifdef SUPPORT_IPSEC
|
---|
| 146 | /* XXX: IPsecã®å¦çã¯ãã±ããæçåã®åã«è¡ããã */
|
---|
| 147 |
|
---|
| 148 | /* SPDãåå¾ãã */
|
---|
| 149 | sp = ipsec4_getpolicybyaddr(output, IPSEC_DIR_OUTBOUND, &ipsec_error);
|
---|
| 150 | if (sp==NULL) {
|
---|
| 151 | error = ipsec_error;
|
---|
| 152 | return error;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /* ããªã·ããã§ãã¯ãã*/
|
---|
| 156 | #if 0
|
---|
| 157 | switch (sp->policy) {
|
---|
| 158 |
|
---|
| 159 | case IPSEC_POLICY_BYPASS:
|
---|
| 160 | case IPSEC_POLICY_NONE:
|
---|
| 161 | case IPSEC_POLICY_TCP:
|
---|
| 162 | goto skip_ipsec;
|
---|
| 163 |
|
---|
| 164 | break; /* NOTREACHED */
|
---|
| 165 |
|
---|
| 166 | case IPSEC_POLICY_IPSEC:
|
---|
| 167 | /* XXX: SH2ã§ã¯ããã§illegal Instructionãçºçãããã³ã³ãã¤ã©ã®ãã°? */
|
---|
| 168 | if (sp->req == NULL) {
|
---|
| 169 | /* éµäº¤æãã¼ã¢ã³ã«å¯¾ãã¦SAã®åå¾ãè¦æ±ãã */
|
---|
| 170 | goto bad;
|
---|
| 171 | }
|
---|
| 172 | break;
|
---|
| 173 |
|
---|
| 174 | default:
|
---|
| 175 | /* ä¸æ£ãªããªã· */
|
---|
| 176 | goto bad;
|
---|
| 177 | break;
|
---|
| 178 | }
|
---|
| 179 | #else /* 0 */
|
---|
| 180 | /* XXX: ã³ã³ãã¤ã©ã®ä¸å
|
---|
| 181 | ·ååé¿ã®ãã switchãããifæã§æ¸ãç´ãã */
|
---|
| 182 | policy = sp->spinitb->policy;
|
---|
| 183 |
|
---|
| 184 | if (policy == IPSEC_POLICY_BYPASS ||
|
---|
| 185 | policy == IPSEC_POLICY_NONE ||
|
---|
| 186 | policy == IPSEC_POLICY_TCP)
|
---|
| 187 | goto skip_ipsec;
|
---|
| 188 | else if (policy == IPSEC_POLICY_IPSEC) {
|
---|
| 189 | if (sp->req == NULL)
|
---|
| 190 | /* éµäº¤æãã¼ã¢ã³ã«å¯¾ãã¦SAã®åå¾ãè¦æ±ãã */
|
---|
| 191 | goto bad;
|
---|
| 192 | } else
|
---|
| 193 | goto bad;
|
---|
| 194 |
|
---|
| 195 | #endif /* 0 */
|
---|
| 196 |
|
---|
| 197 | {
|
---|
| 198 | T_IPSEC_OUTPUT_STATE state;
|
---|
| 199 |
|
---|
| 200 | state.m = output;
|
---|
| 201 | /* XXX: ipsec4_output()å
|
---|
| 202 | ã§ã¯ãip->{len,off}ããããã¯ã¼ã¯ãã¤ããªã¼ãã¨ãã */
|
---|
| 203 | error = ipsec4_output (&state, sp, 0 /* flags */ );
|
---|
| 204 | output = state.m;
|
---|
| 205 |
|
---|
| 206 | if (error) {
|
---|
| 207 | /* net_buf ã¯ipsec4_outputã«ãã£ã¦æ¢ã«è§£æ¾ããã¦ãã */
|
---|
| 208 | switch (error) {
|
---|
| 209 | default:
|
---|
| 210 | syslog (LOG_NOTICE, "ip_output:%d error", __LINE__);
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | /* ipsec4_outputã«å½±é¿ãåããå¤ãæ´æ°ãã */
|
---|
| 216 | /* XXX: å¿
|
---|
| 217 | è¦ã? */
|
---|
| 218 |
|
---|
| 219 | skip_ipsec:
|
---|
| 220 | #endif /* SUPPORT_IPSEC */
|
---|
| 221 |
|
---|
| 222 | #ifdef IP4_CFG_FRAGMENT
|
---|
| 223 |
|
---|
| 224 | ip4h = GET_IP4_HDR(output);
|
---|
| 225 |
|
---|
| 226 | gw = in4_rtalloc(ntohl(ip4h->dst));
|
---|
| 227 |
|
---|
| 228 | /*
|
---|
| 229 | * ãã¼ã¿ã°ã©ã ãµã¤ãºããããã¯ã¼ã¯ã® MTU ãè¶
|
---|
| 230 | ãã¦ããã°ã
|
---|
| 231 | * åå²ãã¦éä¿¡ããã
|
---|
| 232 | */
|
---|
| 233 | if (ntohs(ip4h->len) > IF_MTU) {
|
---|
| 234 | T_NET_BUF *frag;
|
---|
| 235 | T_IP4_HDR *fip4h;
|
---|
| 236 | uint_t off, hlen, len, flen, align;
|
---|
| 237 |
|
---|
| 238 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_OUT], 1);
|
---|
| 239 | NET_COUNT_MIB(ip_stats.ipFragCreates, 1);
|
---|
| 240 |
|
---|
| 241 | len = ntohs(ip4h->len);
|
---|
| 242 | off = hlen = GET_IP4_HDR_SIZE(output);
|
---|
| 243 | while (off < len) {
|
---|
| 244 | if (off + (IF_MTU - IP4_HDR_SIZE) < len)
|
---|
| 245 | flen = IF_MTU - IP4_HDR_SIZE;
|
---|
| 246 | else
|
---|
| 247 | flen = len - off;
|
---|
| 248 |
|
---|
| 249 | /* ãã¼ã¿é·ã¯ 4 ãªã¯ãããå¢çã«èª¿æ´ããã */
|
---|
| 250 | align = (flen + 3) >> 2 << 2;
|
---|
| 251 |
|
---|
| 252 | /* ãããã¯ã¼ã¯ãããã¡ãç²å¾ããã*/
|
---|
| 253 | if (tget_net_buf(&frag, align + IF_IP4_HDR_SIZE, TMO_IP4_FRAG_GET_NET_BUF) == E_OK) {
|
---|
| 254 |
|
---|
| 255 | /* ãã©ã°ã¡ã³ããã³ãã¼ããã*/
|
---|
| 256 | memcpy(GET_IP4_SDU(frag), (uint8_t *)ip4h + off, flen);
|
---|
| 257 |
|
---|
| 258 | /* ããããåããã*/
|
---|
| 259 | fip4h = GET_IP4_HDR(frag);
|
---|
| 260 | *fip4h = *ip4h;
|
---|
| 261 | fip4h->flg_off = htons(IP4_MAKE_FLGOFF(off + flen == len ? 0 : IP4_MF,
|
---|
| 262 | (off - hlen) >> 3));
|
---|
| 263 | fip4h->len = htons(flen + IP4_HDR_SIZE);
|
---|
| 264 | fip4h->id = htons(frag_id);
|
---|
| 265 | fip4h->sum = 0;
|
---|
| 266 | fip4h->sum = in_cksum(fip4h, GET_IP4_HDR_SIZE(frag));
|
---|
| 267 |
|
---|
| 268 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_OCTETS], ntohs(fip4h->len));
|
---|
| 269 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_PACKETS], 1);
|
---|
| 270 | NET_COUNT_IP4(net_count_ip4[NC_IP4_FRAG_OUT_FRAGS], 1);
|
---|
| 271 |
|
---|
| 272 | IF_SET_PROTO(frag, IF_PROTO_IP);
|
---|
| 273 | if ((error = IF_OUTPUT(frag, &gw, NULL, tmout)) != E_OK) {
|
---|
| 274 | syscall(rel_net_buf(output));
|
---|
| 275 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_ERR_PACKETS], 1);
|
---|
| 276 | NET_COUNT_MIB(ip_stats.ipFragFails, 1);
|
---|
| 277 | return error;
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 | else {
|
---|
| 281 | /* ç²å¾ã§ããªããã°ãéä¿¡ããããããã*/
|
---|
| 282 | syscall(rel_net_buf(output));
|
---|
| 283 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_ERR_PACKETS], 1);
|
---|
| 284 | NET_COUNT_MIB(ip_stats.ipFragFails, 1);
|
---|
| 285 | return E_NOMEM;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | off += IF_MTU - IP4_HDR_SIZE;
|
---|
| 289 | }
|
---|
| 290 | syscall(rel_net_buf(output));
|
---|
| 291 | frag_id ++;
|
---|
| 292 | NET_COUNT_MIB(ip_stats.ipFragOKs, 1);
|
---|
| 293 | }
|
---|
| 294 | else {
|
---|
| 295 |
|
---|
| 296 | /* ããããåããã*/
|
---|
| 297 | ip4h->id = htons(frag_id);
|
---|
| 298 | frag_id ++;
|
---|
| 299 | ip4h->sum = 0;
|
---|
| 300 | ip4h->sum = in_cksum(ip4h, GET_IP4_HDR_SIZE(output));
|
---|
| 301 |
|
---|
| 302 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_OCTETS], ntohs(ip4h->len));
|
---|
| 303 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_PACKETS], 1);
|
---|
| 304 |
|
---|
| 305 | IF_SET_PROTO(output, IF_PROTO_IP);
|
---|
| 306 | if ((error = IF_OUTPUT(output, &gw, NULL, tmout)) != E_OK)
|
---|
| 307 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_ERR_PACKETS], 1);
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | #else /* of #ifdef IP4_CFG_FRAGMENT */
|
---|
| 311 |
|
---|
| 312 | ip4h = GET_IP4_HDR(output);
|
---|
| 313 |
|
---|
| 314 | /* ãã¼ã¿ã°ã©ã ãµã¤ãºããããã¯ã¼ã¯ã® MTU ãè¶
|
---|
| 315 | ãã¦ããã°ã¨ã©ã¼ */
|
---|
| 316 | if (ntohs(ip4h->len) > IF_MTU)
|
---|
| 317 | return E_PAR;
|
---|
| 318 |
|
---|
| 319 | /* ããããåããã*/
|
---|
| 320 | ip4h->id = htons(frag_id);
|
---|
| 321 | frag_id ++;
|
---|
| 322 | ip4h->sum = 0;
|
---|
| 323 | ip4h->sum = in_cksum(ip4h, (uint_t)GET_IP4_HDR_SIZE(output));
|
---|
| 324 |
|
---|
| 325 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_OCTETS], ntohs(ip4h->len));
|
---|
| 326 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_PACKETS], 1);
|
---|
| 327 |
|
---|
| 328 | gw = in4_rtalloc(ntohl(ip4h->dst));
|
---|
| 329 | IF_SET_PROTO(output, IF_PROTO_IP);
|
---|
| 330 | if ((error = IF_OUTPUT(output, &gw, NULL, tmout)) != E_OK)
|
---|
| 331 | NET_COUNT_IP4(net_count_ip4[NC_IP4_OUT_ERR_PACKETS], 1);
|
---|
| 332 | NET_COUNT_MIB(ip_stats.ipOutDiscards, 1);
|
---|
| 333 |
|
---|
| 334 | #endif /* of #ifdef IP4_CFG_FRAGMENT */
|
---|
| 335 |
|
---|
| 336 | #ifdef SUPPORT_IPSEC
|
---|
| 337 | bad:
|
---|
| 338 | #endif /* SUPPORT_IPSEC */
|
---|
| 339 |
|
---|
| 340 | return error;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | #endif /* of #if defined(_IP4_CFG) */
|
---|