[270] | 1 | /*
|
---|
| 2 | * TINET (TCP/IP Protocol Stack)
|
---|
| 3 | *
|
---|
| 4 | * Copyright (C) 2001-2009 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: tcp_input.c 270 2017-02-09 04:03:47Z coas-nagasima $
|
---|
| 44 | */
|
---|
| 45 |
|
---|
| 46 | /*
|
---|
| 47 | * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
|
---|
| 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 | * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
|
---|
| 79 | * $FreeBSD: src/sys/netinet/tcp_input.c,v 1.82.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 |
|
---|
| 91 | #endif /* of #ifdef TARGET_KERNEL_ASP */
|
---|
| 92 |
|
---|
| 93 | #ifdef TARGET_KERNEL_JSP
|
---|
| 94 |
|
---|
| 95 | #include <s_services.h>
|
---|
| 96 | #include <t_services.h>
|
---|
| 97 | #include "kernel_id.h"
|
---|
| 98 |
|
---|
| 99 | #endif /* of #ifdef TARGET_KERNEL_JSP */
|
---|
| 100 |
|
---|
| 101 | #include <tinet_defs.h>
|
---|
| 102 | #include <tinet_config.h>
|
---|
| 103 |
|
---|
| 104 | #include <net/if.h>
|
---|
| 105 | #include <net/if_ppp.h>
|
---|
| 106 | #include <net/if_loop.h>
|
---|
| 107 | #include <net/ethernet.h>
|
---|
| 108 | #include <net/net.h>
|
---|
| 109 | #include <net/net_var.h>
|
---|
| 110 | #include <net/net_buf.h>
|
---|
| 111 | #include <net/net_timer.h>
|
---|
| 112 | #include <net/net_count.h>
|
---|
| 113 |
|
---|
| 114 | #include <netinet/in.h>
|
---|
| 115 | #include <netinet/in_var.h>
|
---|
| 116 | #include <netinet6/in6.h>
|
---|
| 117 | #include <netinet6/in6_var.h>
|
---|
| 118 | #include <netinet6/nd6.h>
|
---|
| 119 | #include <netinet/in_itron.h>
|
---|
| 120 | #include <netinet/ip.h>
|
---|
| 121 | #include <netinet/ip_var.h>
|
---|
| 122 | #include <netinet/ip6.h>
|
---|
| 123 | #include <netinet6/ip6_var.h>
|
---|
| 124 | #include <netinet/ip_icmp.h>
|
---|
| 125 | #include <netinet/tcp.h>
|
---|
| 126 | #include <netinet/tcp_timer.h>
|
---|
| 127 | #include <netinet/tcp_var.h>
|
---|
| 128 | #include <netinet/tcp_fsm.h>
|
---|
| 129 | #include <netinet/tcp_seq.h>
|
---|
| 130 |
|
---|
| 131 | #ifdef SUPPORT_TCP
|
---|
| 132 |
|
---|
| 133 | /*
|
---|
| 134 | * æ»ãå¤
|
---|
| 135 | *
|
---|
| 136 | * RET_OK æ£å¸¸
|
---|
| 137 | * RET_DROP ã¨ã©ã¼ãã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 138 | * RET_RST_DROP ã¨ã©ã¼ãRST ãéä¿¡ããã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 139 | */
|
---|
| 140 |
|
---|
| 141 | #define RET_OK (0)
|
---|
| 142 | #define RET_NEED_OUTPUT (1)
|
---|
| 143 | #define RET_RETURN (2)
|
---|
| 144 | #define RET_DROP (-1)
|
---|
| 145 | #define RET_RST_DROP (-2)
|
---|
| 146 |
|
---|
| 147 | /*
|
---|
| 148 | * é¢æ°
|
---|
| 149 | */
|
---|
| 150 |
|
---|
| 151 | static void close_connection (T_TCP_CEP *cep, bool_t *needoutput);
|
---|
| 152 | static void set_rexmt_timer (T_TCP_CEP *cep, T_TCP_TIME rtt);
|
---|
| 153 | static uint8_t reassemble (T_NET_BUF *input, T_TCP_CEP *cep, uint_t thoff, uint8_t flags);
|
---|
| 154 | static ER drop_after_ack (T_NET_BUF *input, T_TCP_CEP *cep, uint_t thoff);
|
---|
| 155 | static ER listening (T_NET_BUF *input, T_TCP_CEP *cep, uint_t thoff, T_TCP_SEQ iss);
|
---|
| 156 | static ER proc_ack1 (T_NET_BUF *input, T_TCP_CEP *cep, uint_t thoff, bool_t *needoutput);
|
---|
| 157 | static ER proc_ack2 (T_NET_BUF *input, T_TCP_CEP *cep, uint_t thoff, bool_t *needoutput);
|
---|
| 158 | static ER syn_sent (T_TCP_HDR *tcph, T_TCP_CEP *cep);
|
---|
| 159 | static void trim_length (T_TCP_HDR *tcph, T_TCP_CEP *cep);
|
---|
| 160 | static void parse_option (T_TCP_HDR *tcph, T_TCP_CEP *cep);
|
---|
| 161 | static bool_t update_wnd (T_TCP_HDR *tcph, T_TCP_CEP *cep);
|
---|
| 162 | static void proc_urg (T_TCP_HDR *tcph, T_TCP_CEP *cep);
|
---|
| 163 |
|
---|
| 164 | #if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0
|
---|
| 165 |
|
---|
| 166 | /*
|
---|
| 167 | * ã¿ã¹ã¯ããã® Time Wait ç¶æ
|
---|
| 168 | CEP åé¢æ©è½
|
---|
| 169 | */
|
---|
| 170 |
|
---|
| 171 | /*
|
---|
| 172 | * é¢æ°
|
---|
| 173 | */
|
---|
| 174 |
|
---|
| 175 | static T_TCP_TWCEP*tcp_find_twcep (T_IN_ADDR *dstaddr, uint16_t dstport,
|
---|
| 176 | T_IN_ADDR *peeraddr, uint16_t peerport);
|
---|
| 177 |
|
---|
| 178 | /*
|
---|
| 179 | * å¤æ°
|
---|
| 180 | */
|
---|
| 181 |
|
---|
| 182 | T_TCP_TWCEP tcp_twcep[NUM_TCP_TW_CEP_ENTRY];
|
---|
| 183 |
|
---|
| 184 | /*
|
---|
| 185 | * tcp_find_twcep -- ãã¼ãçªå·ãã Time Wait ç¨ TCP é信端ç¹ãå¾ãã
|
---|
| 186 | *
|
---|
| 187 | * 注æ: dstaddr ã¯ããããã¯ã¼ã¯ãã¤ããªã¼ã
|
---|
| 188 | */
|
---|
| 189 |
|
---|
| 190 | static T_TCP_TWCEP*
|
---|
| 191 | tcp_find_twcep (T_IN_ADDR *dstaddr, uint16_t dstport, T_IN_ADDR *peeraddr, uint16_t peerport)
|
---|
| 192 | {
|
---|
| 193 | T_TCP_TWCEP* twcep;
|
---|
| 194 |
|
---|
| 195 | /*
|
---|
| 196 | * ç¶æ
|
---|
| 197 | ã TIME WAIT ã§ã
|
---|
| 198 | * IP ã¢ãã¬ã¹ã¨ãã¼ãçªå·ãä¸è´ããé信端ç¹ãæ¢ç´¢ããã
|
---|
| 199 | */
|
---|
| 200 | for (twcep = &tcp_twcep[NUM_TCP_TW_CEP_ENTRY]; twcep -- != tcp_twcep; ) {
|
---|
| 201 | if (twcep->fsm_state == TCP_FSM_TIME_WAIT &&
|
---|
| 202 | IN_IS_DSTADDR_ACCEPT (&twcep->myaddr.ipaddr, dstaddr) &&
|
---|
| 203 | IN_ARE_NET_ADDR_EQUAL(&twcep->dstaddr.ipaddr, peeraddr) &&
|
---|
| 204 | dstport == twcep->myaddr.portno &&
|
---|
| 205 | peerport == twcep->dstaddr.portno)
|
---|
| 206 | return twcep;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | return NULL;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | /*
|
---|
| 213 | * å¿
|
---|
| 214 | è¦ãªæ
|
---|
| 215 | å ±ã Time Wait ç¨ TCP é信端ç¹ã«ç§»ãã¦ã
|
---|
| 216 | * æ¨æºã® TCP é信端ç¹ãéæ¾ããã
|
---|
| 217 | */
|
---|
| 218 |
|
---|
| 219 | void
|
---|
| 220 | tcp_move_twcep (T_TCP_CEP *cep)
|
---|
| 221 | {
|
---|
| 222 | T_TCP_TWCEP* twcep;
|
---|
| 223 |
|
---|
| 224 | /* 空ãã® Time Wait ç¨ TCP é信端ç¹ãæ¢ç´¢ããã*/
|
---|
| 225 | for (twcep = &tcp_twcep[NUM_TCP_TW_CEP_ENTRY]; twcep -- != tcp_twcep; ) {
|
---|
| 226 | if (twcep->fsm_state != TCP_FSM_TIME_WAIT) {
|
---|
| 227 |
|
---|
| 228 | /*
|
---|
| 229 | * é信端ç¹ãããã¯ãã
|
---|
| 230 | * å¿
|
---|
| 231 | è¦ãªæ
|
---|
| 232 | å ±ã Time Wait ç¨ TCP é信端ç¹ã«ç§»ãã
|
---|
| 233 | */
|
---|
| 234 | syscall(wai_sem(cep->semid_lock));
|
---|
| 235 | twcep->rbufsz = cep->rbufsz;
|
---|
| 236 | twcep->dstaddr = cep->dstaddr;
|
---|
| 237 | twcep->myaddr = cep->myaddr;
|
---|
| 238 | twcep->snd_una = cep->snd_una;
|
---|
| 239 | twcep->rcv_nxt = cep->rcv_nxt;
|
---|
| 240 | twcep->rwbuf_count = cep->rwbuf_count;
|
---|
| 241 | twcep->fsm_state = cep->fsm_state;
|
---|
| 242 | twcep->timer_2msl = cep->timer[TCP_TIM_2MSL];
|
---|
| 243 |
|
---|
| 244 | /* é信端ç¹ãããã¯ã解é¤ããã*/
|
---|
| 245 | syscall(sig_sem(cep->semid_lock));
|
---|
| 246 |
|
---|
| 247 | /* æ¨æº TCP é信端ç¹ãéæ¾ããã*/
|
---|
| 248 | tcp_close(cep);
|
---|
| 249 |
|
---|
| 250 | break;
|
---|
| 251 | }
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | #endif /* of #if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0 */
|
---|
| 256 |
|
---|
| 257 | /*
|
---|
| 258 | * parse_option -- TCP ãããã®ãªãã·ã§ã³ã解æããã
|
---|
| 259 | */
|
---|
| 260 |
|
---|
| 261 | static void
|
---|
| 262 | parse_option (T_TCP_HDR *tcph, T_TCP_CEP *cep)
|
---|
| 263 | {
|
---|
| 264 | uint8_t *opt, type = 0;
|
---|
| 265 | uint_t len, ol, ssize;
|
---|
| 266 |
|
---|
| 267 | opt = (uint8_t*)tcph + TCP_HDR_SIZE;
|
---|
| 268 | len = (uint_t)(TCP_HDR_LEN(tcph->doff) - TCP_HDR_SIZE);
|
---|
| 269 | while (len > 0 && (type = *opt) != TCP_OPT_EOL) {
|
---|
| 270 | if (type == TCP_OPT_NOP)
|
---|
| 271 | ol = 1u;
|
---|
| 272 | else {
|
---|
| 273 | if (len < 2)
|
---|
| 274 | break;
|
---|
| 275 | ol = *(opt + 1);
|
---|
| 276 | if (ol < 2 || ol > len)
|
---|
| 277 | break;
|
---|
| 278 | }
|
---|
| 279 | switch (type) {
|
---|
| 280 | case TCP_OPT_NOP:
|
---|
| 281 | break;
|
---|
| 282 | case TCP_OPT_MAXSEG:
|
---|
| 283 | if ((ol == TCP_OPT_LEN_MAXSEG) && (tcph->flags & TCP_FLG_SYN)) {
|
---|
| 284 | ssize = ntohs(*(uint16_t*)(opt + 2));
|
---|
| 285 | if (ssize > MAX_TCP_SND_SEG)
|
---|
| 286 | cep->maxseg = MAX_TCP_SND_SEG;
|
---|
| 287 | else if (ssize < TCP_MINMSS)
|
---|
| 288 | cep->maxseg = TCP_MINMSS;
|
---|
| 289 | else
|
---|
| 290 | cep->maxseg = ssize;
|
---|
| 291 | }
|
---|
| 292 | break;
|
---|
| 293 |
|
---|
| 294 | case TCP_OPT_WINDOW:
|
---|
| 295 | case TCP_OPT_SACK_PERM:
|
---|
| 296 | case TCP_OPT_TIMESTAMP:
|
---|
| 297 | case TCP_OPT_CC:
|
---|
| 298 | case TCP_OPT_CCNEW:
|
---|
| 299 | case TCP_OPT_CCECHO:
|
---|
| 300 | syslog(LOG_INFO, "[TCP] unsup opt: %d.", type);
|
---|
| 301 | break;
|
---|
| 302 |
|
---|
| 303 | default:
|
---|
| 304 | syslog(LOG_NOTICE, "[TCP] unexp opt: %d.", type);
|
---|
| 305 | break;
|
---|
| 306 | }
|
---|
| 307 | opt += ol;
|
---|
| 308 | len -= ol;
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | /*
|
---|
| 313 | * set_rexmt_timer -- æ°ããå¾å¾©æéãåéããåéã¿ã¤ããæ´æ°ããã
|
---|
| 314 | */
|
---|
| 315 |
|
---|
| 316 | static void
|
---|
| 317 | set_rexmt_timer (T_TCP_CEP *cep, T_TCP_TIME rtt)
|
---|
| 318 | {
|
---|
| 319 | T_TCP_TIME delta;
|
---|
| 320 |
|
---|
| 321 | NET_COUNT_TCP(net_count_tcp[NC_TCP_RTT_UPDATES], 1);
|
---|
| 322 | if (cep->srtt != 0) {
|
---|
| 323 | /*
|
---|
| 324 | * srtt: å¹³æ»åããã RTT
|
---|
| 325 | *
|
---|
| 326 | * è¨æ¸¬ããã RTT (rtt) ã¨ç¾å¨ã®å¹³æ»åããã RTT (srtt) ã®å·® (delta) ãæ±ããã
|
---|
| 327 | *
|
---|
| 328 | * delta 㯠2 ãããå·¦ã·ãã ( 4 å) ããå¤ã§ä¿æããã
|
---|
| 329 | * srtt 㯠5 ãããå·¦ã·ãã (32 å) ããå¤ã§ä¿æããã¦ããã
|
---|
| 330 | *
|
---|
| 331 | * delta = rtt / 8 - srtt / 8
|
---|
| 332 | *
|
---|
| 333 | * æ°ãã srtt ã¯
|
---|
| 334 | *
|
---|
| 335 | * srtt = rtt / 8 + srtt * 7 / 8
|
---|
| 336 | * = srtt + (rtt / 8 - srtt / 8)
|
---|
| 337 | *
|
---|
| 338 | * ã§è¨ç®ããã
|
---|
| 339 | * ãã®ãããrtt ã 2 ãããå·¦ã·ããããsrtt ã (5 - 2) ãããå³ã·ããã㦠delta ãæ±ããã
|
---|
| 340 | */
|
---|
| 341 | delta = ((rtt - 1) << TCP_DELTA_SHIFT) - (cep->srtt >> (TCP_SRTT_SHIFT - TCP_DELTA_SHIFT));
|
---|
| 342 | cep->srtt += delta;
|
---|
| 343 | if (cep->srtt <= 0)
|
---|
| 344 | cep->srtt = 1;
|
---|
| 345 |
|
---|
| 346 | /*
|
---|
| 347 | * delta ã®çµ¶å¯¾å¤ | delta | ãæ±ããã
|
---|
| 348 | */
|
---|
| 349 | if (delta < 0)
|
---|
| 350 | delta = - delta;
|
---|
| 351 |
|
---|
| 352 | /*
|
---|
| 353 | * rttvar: å¹³æ»åãããåæ£
|
---|
| 354 | *
|
---|
| 355 | * rttvar 㯠4 ãããå·¦ã·ãã (16 å) ããå¤ã§ä¿æããã¦ããã
|
---|
| 356 | *
|
---|
| 357 | * delta = |delta| / 4 - rttvar / 4
|
---|
| 358 | *
|
---|
| 359 | * æ°ãã rttvar ã¯
|
---|
| 360 | *
|
---|
| 361 | * rttvar = |delta|/ 4 + rttvar * 3 /4
|
---|
| 362 | * = rttvar + (|delta| / 4 - rttvar / 4)
|
---|
| 363 | *
|
---|
| 364 | * ã§è¨ç®ããã
|
---|
| 365 | */
|
---|
| 366 | delta -= cep->rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
|
---|
| 367 | cep->rttvar += delta;
|
---|
| 368 | if (cep->rttvar <= 0)
|
---|
| 369 | cep->rttvar = 1;
|
---|
| 370 | }
|
---|
| 371 | else {
|
---|
| 372 | /*
|
---|
| 373 | * ã¾ã srtt ã®è¨å®ãè¡ããã¦ããªãã¨ãã¯ãä»åè¨æ¸¬ããã RTT ã使ç¨ããã
|
---|
| 374 | * å¹³æ»åããã RTT (srtt) ã«ã¯ãRTT ã 5 ãããå·¦ã·ãã (32å) ããå¤ã
|
---|
| 375 | * å¹³æ»åãããåæ£ (rttvar) ã«ã¯ãRTT ã® 1/2 ã 4 ãããå·¦ã·ãã (16å) ããå¤ã
|
---|
| 376 | */
|
---|
| 377 | cep->srtt = rtt << TCP_SRTT_SHIFT;
|
---|
| 378 | cep->rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
|
---|
| 379 | }
|
---|
| 380 |
|
---|
| 381 | /*
|
---|
| 382 | * rtt ã®æ¸¬å®ãçµäºããåéåæ°ããªã»ããããã
|
---|
| 383 | */
|
---|
| 384 | cep->rtt = cep->rxtshift = 0;
|
---|
| 385 |
|
---|
| 386 | /*
|
---|
| 387 | * RTT ã«è¨±ãããæå°å¤ 㨠rtt + 2 ã®å¤§ããªå¤ã®æ¹ãåéã¿ã¤ã ã¢ã¦ãã®æå°å¤ã«ããã
|
---|
| 388 | */
|
---|
| 389 | if (rtt + 2 < TCP_TVAL_MIN)
|
---|
| 390 | cep->rxtcur = tcp_range_set(tcp_rexmt_val(cep),
|
---|
| 391 | (T_TCP_TIME)TCP_TVAL_MIN,
|
---|
| 392 | (T_TCP_TIME)TCP_TVAL_MAX_REXMT);
|
---|
| 393 | else
|
---|
| 394 | cep->rxtcur = tcp_range_set(tcp_rexmt_val(cep),
|
---|
| 395 | (T_TCP_TIME)(rtt + 2),
|
---|
| 396 | (T_TCP_TIME)TCP_TVAL_MAX_REXMT);
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | /*
|
---|
| 400 | * reassemble -- åä¿¡ã»ã°ã¡ã³ããåæ§æãããé çªéãã«åä¿¡ããã¨ãã®å¦ç
|
---|
| 401 | */
|
---|
| 402 |
|
---|
| 403 | static uint8_t
|
---|
| 404 | reassemble (T_NET_BUF *input, T_TCP_CEP *cep, uint_t thoff, uint8_t flags)
|
---|
| 405 | {
|
---|
| 406 | T_TCP_Q_HDR *qhdr;
|
---|
| 407 | T_TCP_HDR *tcph;
|
---|
| 408 |
|
---|
| 409 | tcph = GET_TCP_HDR(input, thoff);
|
---|
| 410 | if (tcph->sum > cep->rbufsz - cep->rwbuf_count) {
|
---|
| 411 | /*
|
---|
| 412 | * åä¿¡ã¦ã£ã³ããããã¡ã«ç©ºãããªãã¨ãã¯ç ´æ£ããã
|
---|
| 413 | */
|
---|
| 414 | NET_COUNT_TCP(net_count_tcp[NC_TCP_RECV_DROP_SEGS], 1);
|
---|
| 415 | syscall(rel_net_buf(input));
|
---|
| 416 | cep->flags |= TCP_CEP_FLG_ACK_NOW;
|
---|
| 417 | flags &= ~TCP_FLG_FIN;
|
---|
| 418 | }
|
---|
| 419 | else if (tcph->seq == cep->rcv_nxt &&
|
---|
| 420 | cep->reassq == NULL &&
|
---|
| 421 | cep->fsm_state == TCP_FSM_ESTABLISHED) {
|
---|
| 422 | /*
|
---|
| 423 | * é çªéãã«ã»ã°ã¡ã³ããåä¿¡ããæã®å¦ç
|
---|
| 424 | * åä¿¡ã»ã°ã¡ã³ãã®ä¸¦ã¹æ¿ãã¯ä¸è¦ãªã®ã§
|
---|
| 425 | * ãã®ã¾ã¾åä¿¡ã¦ã£ã³ããããã¡ã«æ¸ãè¾¼ãã
|
---|
| 426 | */
|
---|
| 427 |
|
---|
| 428 | #ifdef TCP_CFG_DELAY_ACK
|
---|
| 429 |
|
---|
| 430 | cep->flags |= TCP_CEP_FLG_DEL_ACK;
|
---|
| 431 |
|
---|
| 432 | #else/* of #ifdef TCP_CFG_DELAY_ACK */
|
---|
| 433 |
|
---|
| 434 | cep->flags |= TCP_CEP_FLG_ACK_NOW;
|
---|
| 435 |
|
---|
| 436 | #endif/* of #ifdef TCP_CFG_DELAY_ACK */
|
---|
| 437 |
|
---|
| 438 | qhdr = GET_TCP_Q_HDR(input, thoff);
|
---|
| 439 |
|
---|
| 440 | /* TCP ãããã®ä½ç½®ãä¿åããã*/
|
---|
| 441 | GET_TCP_IP_Q_HDR(input)->thoff = thoff;
|
---|
| 442 |
|
---|
| 443 | /* SDU ã®ãªãã»ããï¼å
|
---|
| 444 | ã¯ã¦ã£ã³ããµã¤ãºï¼ããªã»ããããã*/
|
---|
| 445 | qhdr->soff = 0;
|
---|
| 446 |
|
---|
| 447 | /* ãã¼ã¿ãåä¿¡ã¦ã£ã³ããããã¡ã«æ¸ãè¾¼ãã*/
|
---|
| 448 | TCP_WRITE_RWBUF(cep, input, thoff);
|
---|
| 449 | }
|
---|
| 450 | else {
|
---|
| 451 | flags = tcp_write_raque(input, cep, thoff, flags);
|
---|
| 452 | cep->flags |= TCP_CEP_FLG_ACK_NOW;
|
---|
| 453 | }
|
---|
| 454 | return flags;
|
---|
| 455 | }
|
---|
| 456 |
|
---|
| 457 | /*
|
---|
| 458 | * listening -- ååãªã¼ãã³ãã¦ï¼ç¶æ
|
---|
| 459 | ã LISTEN ã®å¦ç
|
---|
| 460 | *
|
---|
| 461 | * æ»ãå¤:
|
---|
| 462 | * RET_OK æ£å¸¸
|
---|
| 463 | * RET_DROP ã¨ã©ã¼ãã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 464 | * RET_RST_DROP ã¨ã©ã¼ãRST ãéä¿¡ããã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 465 | */
|
---|
| 466 |
|
---|
| 467 | static ER
|
---|
| 468 | listening (T_NET_BUF *input, T_TCP_CEP *cep, uint_t thoff, T_TCP_SEQ iss)
|
---|
| 469 | {
|
---|
| 470 | T_IP_HDR *iph;
|
---|
| 471 | T_TCP_HDR *tcph;
|
---|
| 472 |
|
---|
| 473 | iph = GET_IP_HDR(input);
|
---|
| 474 | tcph = GET_TCP_HDR(input, thoff);
|
---|
| 475 |
|
---|
| 476 | /*
|
---|
| 477 | * ãã©ã°ã« RST ãã»ããããã¦ããã°ç ´æ£ããã
|
---|
| 478 | */
|
---|
| 479 | if (tcph->flags & TCP_FLG_RST)
|
---|
| 480 | return RET_DROP;
|
---|
| 481 |
|
---|
| 482 | /*
|
---|
| 483 | * ãã©ã°ã« ACK ãã»ãããã¦ãããã°ã
|
---|
| 484 | * ãªã»ãããéã£ã¦ç ´æ£ããã
|
---|
| 485 | */
|
---|
| 486 | if (tcph->flags & TCP_FLG_ACK)
|
---|
| 487 | return RET_RST_DROP;
|
---|
| 488 |
|
---|
| 489 | /*
|
---|
| 490 | * ãã©ã°ã« SYN ãã»ããããã¦ããªããã°ãã°ç ´æ£ããã
|
---|
| 491 | */
|
---|
| 492 | if ((tcph->flags & TCP_FLG_SYN) == 0)
|
---|
| 493 | return RET_DROP;
|
---|
| 494 |
|
---|
| 495 | #if defined(SUPPORT_INET4)
|
---|
| 496 |
|
---|
| 497 | #ifdef SUPPORT_LOOP
|
---|
| 498 |
|
---|
| 499 | /*
|
---|
| 500 | * 次ã®ã¨ãã¯ç ´æ£ããã
|
---|
| 501 | * ã»ãã¼ãçªå·ãåä¸ã§ãéåä¿¡ IP ã¢ãã¬ã¹ ãåä¸ã
|
---|
| 502 | * ãã ãããã¼ã«ã«ã«ã¼ããã㯠(127.0.0.1) ãªãè¯ãã
|
---|
| 503 | * ã»ãã«ããã£ã¹ãã¢ãã¬ã¹
|
---|
| 504 | */
|
---|
| 505 |
|
---|
| 506 | if (tcph->dport == tcph->sport &&
|
---|
| 507 | (iph->dst == iph->src && ntohl(iph->src) != IPV4_ADDR_LOOPBACK))
|
---|
| 508 | return RET_DROP;
|
---|
| 509 |
|
---|
| 510 | #else /* of #ifdef SUPPORT_LOOP */
|
---|
| 511 |
|
---|
| 512 | /*
|
---|
| 513 | * 次ã®ã¨ãã¯ç ´æ£ããã
|
---|
| 514 | * ã»ãã¼ãçªå·ãåä¸ã§ãéåä¿¡ IP ã¢ãã¬ã¹ ãåä¸ã
|
---|
| 515 | * ã»ãã«ããã£ã¹ãã¢ãã¬ã¹
|
---|
| 516 | */
|
---|
| 517 |
|
---|
| 518 | if (tcph->dport == tcph->sport && iph->dst == iph->src)
|
---|
| 519 | return RET_DROP;
|
---|
| 520 |
|
---|
| 521 | #endif /* of #ifdef SUPPORT_LOOP */
|
---|
| 522 |
|
---|
| 523 | #endif /* of #if defined(SUPPORT_INET4) */
|
---|
| 524 |
|
---|
| 525 | #if defined(SUPPORT_INET6)
|
---|
| 526 |
|
---|
| 527 | /*
|
---|
| 528 | * 次ã®ã¨ãã¯ç ´æ£ããã
|
---|
| 529 | * ã»ãã¼ãçªå·ãåä¸ã§ãéåä¿¡ IP ã¢ãã¬ã¹ ãåä¸ã
|
---|
| 530 | * ã»ãã«ããã£ã¹ãã¢ãã¬ã¹
|
---|
| 531 | */
|
---|
| 532 |
|
---|
| 533 | if (tcph->dport == tcph->sport && IN_ARE_ADDR_EQUAL(&iph->dst, &iph->src))
|
---|
| 534 | return RET_DROP;
|
---|
| 535 |
|
---|
| 536 | #endif /* of #if defined(SUPPORT_INET6) */
|
---|
| 537 |
|
---|
| 538 | if (IN_IS_NET_ADDR_MULTICAST(&iph->dst))
|
---|
| 539 | return RET_DROP;
|
---|
| 540 |
|
---|
| 541 | /* ç¸æã®ã¢ãã¬ã¹ãè¨é²ããã*/
|
---|
| 542 | IN_COPY_TO_HOST(&cep->dstaddr.ipaddr, &iph->src);
|
---|
| 543 | cep->dstaddr.portno = tcph->sport;
|
---|
| 544 |
|
---|
| 545 | /* ãªãã·ã§ã³ãå¦çããã*/
|
---|
| 546 | parse_option(tcph, cep);
|
---|
| 547 |
|
---|
| 548 | /* ã·ã¼ã±ã³ã¹çªå·ãåæåããã*/
|
---|
| 549 | if (tcp_iss == 0)
|
---|
| 550 | tcp_init_iss();
|
---|
| 551 |
|
---|
| 552 | /* èªåã®ã·ã¼ã±ã³ã¹çªå·ã®åæå¤ãè¨é²ããã*/
|
---|
| 553 | if (iss != 0)
|
---|
| 554 | cep->iss = iss;
|
---|
| 555 | else
|
---|
| 556 | cep->iss = tcp_iss;
|
---|
| 557 |
|
---|
| 558 | tcp_iss += TCP_ISS_INCR() / 4;
|
---|
| 559 |
|
---|
| 560 | /* ç¸æã®ã·ã¼ã±ã³ã¹çªå·ã®åæå¤ãè¨é²ããã*/
|
---|
| 561 | cep->irs = tcph->seq;
|
---|
| 562 |
|
---|
| 563 | /* éåä¿¡ã·ã¼ã±ã³ã¹çªå·ãåæåããã*/
|
---|
| 564 | init_send_seq(cep);
|
---|
| 565 | init_receive_seq(cep);
|
---|
| 566 |
|
---|
| 567 | /* éä¿¡ã¦ã¤ã³ããµã¤ãºãè¨å®ããã*/
|
---|
| 568 | cep->snd_wnd = tcph->win;
|
---|
| 569 |
|
---|
| 570 | /* æçµè¨å® */
|
---|
| 571 | cep->flags |= TCP_CEP_FLG_ACK_NOW;
|
---|
| 572 | cep->fsm_state = TCP_FSM_SYN_RECVD;
|
---|
| 573 | cep->timer[TCP_TIM_KEEP] = TCP_TVAL_KEEP_INIT;
|
---|
| 574 |
|
---|
| 575 | return RET_OK;
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | /*
|
---|
| 579 | * syn_sent -- è½åãªã¼ãã³ãã¦ãç¶æ
|
---|
| 580 | ã SYN éä¿¡æ¸ã®å¦ç
|
---|
| 581 | *
|
---|
| 582 | * æ»ãå¤:
|
---|
| 583 | * RET_OK æ£å¸¸
|
---|
| 584 | * RET_DROP ã¨ã©ã¼ãã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 585 | * RET_RST_DROP ã¨ã©ã¼ãRST ãéä¿¡ããã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 586 | */
|
---|
| 587 |
|
---|
| 588 | static ER
|
---|
| 589 | syn_sent (T_TCP_HDR *tcph, T_TCP_CEP *cep)
|
---|
| 590 | {
|
---|
| 591 | ER error = RET_OK;
|
---|
| 592 |
|
---|
| 593 | /*
|
---|
| 594 | * ç¸æããå信確èªãéããã¦æ¥ã¦ãã
|
---|
| 595 | *
|
---|
| 596 | * ACK <= iss && éä¿¡ããæ大 SEQ (snd_max) < ACK
|
---|
| 597 | *
|
---|
| 598 | * ãªãããªã»ãããéã£ã¦ã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 599 | */
|
---|
| 600 | if ((tcph->flags & TCP_FLG_ACK) &&
|
---|
| 601 | (SEQ_LE(tcph->ack, cep->iss) || SEQ_GT(tcph->ack, cep->snd_max)))
|
---|
| 602 | return RET_RST_DROP;
|
---|
| 603 |
|
---|
| 604 | /*
|
---|
| 605 | * RST/ACK ãã©ã°ã®å¿çãããã°ããã¼ããéãã¦ããªã
|
---|
| 606 | * ãã¨ãæå³ãã¦ããã
|
---|
| 607 | */
|
---|
| 608 | if (tcph->flags & TCP_FLG_RST) {
|
---|
| 609 | if (tcph->flags & TCP_FLG_ACK) {
|
---|
| 610 | cep->net_error = EV_CNNRF;
|
---|
| 611 | cep = tcp_drop(cep, E_CLS);
|
---|
| 612 | }
|
---|
| 613 | return RET_DROP;
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | /*
|
---|
| 617 | * SYN ãã©ã°ããªããã°ã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 618 | */
|
---|
| 619 | if ((tcph->flags & TCP_FLG_SYN) == 0)
|
---|
| 620 | return RET_DROP;
|
---|
| 621 |
|
---|
| 622 | cep->snd_wnd = tcph->win; /* snd_wnd: ç¸æã®åä¿¡å¯è½ã¦ã£ã³ããµã¤ãº */
|
---|
| 623 | cep->irs = tcph->seq; /* irs: ç¸æã®ã·ã¼ã±ã³ã¹çªå·ã®åæå¤ */
|
---|
| 624 | init_receive_seq(cep); /* éåä¿¡ã·ã¼ã±ã³ã¹çªå·ãåæåããã */
|
---|
| 625 |
|
---|
| 626 | if (tcph->flags & TCP_FLG_ACK) {
|
---|
| 627 | /*
|
---|
| 628 | * ACK ãã©ã°ãããã¨ãã®å¦ç
|
---|
| 629 | *
|
---|
| 630 | * åä¿¡ãæå¾
|
---|
| 631 | ãã¦ããæ大㮠SEQ (rcv_adv) ã
|
---|
| 632 | * åä¿¡å¯è½ãªã¦ã£ã³ããµã¤ãº (rcv_wnd) åé²ããã
|
---|
| 633 | */
|
---|
| 634 | cep->rcv_adv += cep->rcv_wnd; /* rcv_adv: åä¿¡ãæå¾
|
---|
| 635 | ãã¦ããæ大㮠SEQ */
|
---|
| 636 | /* rcv_wnd: åä¿¡å¯è½ãªã¦ã£ã³ããµã¤ãº */
|
---|
| 637 |
|
---|
| 638 | /* æªç¢ºèªã®æå°éä¿¡ SEQ (snd_una) ã SYN å (1 ãªã¯ããã) é²ããã*/
|
---|
| 639 | cep->snd_una ++;
|
---|
| 640 |
|
---|
| 641 | #ifdef TCP_CFG_DELAY_ACK
|
---|
| 642 |
|
---|
| 643 | if (tcph->sum != 0) /* tcph->sum 㯠SDU é· */
|
---|
| 644 | cep->flags |= TCP_CEP_FLG_DEL_ACK;
|
---|
| 645 | else
|
---|
| 646 | cep->flags |= TCP_CEP_FLG_ACK_NOW;
|
---|
| 647 |
|
---|
| 648 | #else/* of #ifdef TCP_CFG_DELAY_ACK */
|
---|
| 649 |
|
---|
| 650 | cep->flags |= TCP_CEP_FLG_ACK_NOW;
|
---|
| 651 |
|
---|
| 652 | #endif/* of #ifdef TCP_CFG_DELAY_ACK */
|
---|
| 653 |
|
---|
| 654 | if (cep->flags & TCP_CEP_FLG_NEED_FIN) {
|
---|
| 655 | /*
|
---|
| 656 | * CEP 㧠FIN éä¿¡ãè¦æ±ããã¦ããã°ã
|
---|
| 657 | * åæå¦çãéå§ãã
|
---|
| 658 | * CEP ã®ç¶æ
|
---|
| 659 | ã FIN Wait 1 ã«ããã
|
---|
| 660 | */
|
---|
| 661 | cep->fsm_state = TCP_FSM_FIN_WAIT_1;
|
---|
| 662 | cep->flags &= ~TCP_CEP_FLG_NEED_FIN;
|
---|
| 663 | tcph->flags &= ~TCP_FLG_SYN;
|
---|
| 664 | }
|
---|
| 665 | else {
|
---|
| 666 | /*
|
---|
| 667 | * ç¸æãã ACK ãå¿çãããã®ã§ã
|
---|
| 668 | * CEP ã®ç¶æ
|
---|
| 669 | ã ã³ãã¯ã·ã§ã³éè¨å®äºç¶æ
|
---|
| 670 | ã«ããã
|
---|
| 671 | */
|
---|
| 672 | cep->timer[TCP_TIM_KEEP] = TCP_TVAL_KEEP_IDLE;
|
---|
| 673 | cep->fsm_state = TCP_FSM_ESTABLISHED;
|
---|
| 674 | NET_COUNT_MIB(tcp_stats.tcpActiveOpens, 1);
|
---|
| 675 | syscall(set_flg(cep->est_flgid, TCP_CEP_EVT_ESTABLISHED));
|
---|
| 676 |
|
---|
| 677 | #ifdef TCP_CFG_NON_BLOCKING
|
---|
| 678 |
|
---|
| 679 | if (cep->snd_nblk_tfn == TFN_TCP_CON_CEP) {
|
---|
| 680 |
|
---|
| 681 | /* ç¸æã®ã¢ãã¬ã¹ãã³ãã¼ããã*/
|
---|
| 682 | *cep->p_dstaddr = cep->dstaddr;
|
---|
| 683 |
|
---|
| 684 | if (IS_PTR_DEFINED(cep->callback)) {
|
---|
| 685 |
|
---|
| 686 | #ifdef TCP_CFG_NON_BLOCKING_COMPAT14
|
---|
| 687 |
|
---|
| 688 | NET_COUNT_TCP(net_count_tcp[NC_TCP_CONNECTS], 1);
|
---|
| 689 | (*cep->callback)(GET_TCP_CEPID(cep), cep->snd_nblk_tfn, E_OK);
|
---|
| 690 |
|
---|
| 691 | #else /* of #ifdef TCP_CFG_NON_BLOCKING_COMPAT14 */
|
---|
| 692 |
|
---|
| 693 | ER error = E_OK;
|
---|
| 694 |
|
---|
| 695 | NET_COUNT_TCP(net_count_tcp[NC_TCP_CONNECTS], 1);
|
---|
| 696 | (*cep->callback)(GET_TCP_CEPID(cep), cep->snd_nblk_tfn, (void*)&error);
|
---|
| 697 |
|
---|
| 698 | #endif /* of #ifdef TCP_CFG_NON_BLOCKING_COMPAT14 */
|
---|
| 699 |
|
---|
| 700 | }
|
---|
| 701 | else {
|
---|
| 702 | syslog(LOG_WARNING, "[TCP] no call back, CEP: %d.", GET_TCP_CEPID(cep));
|
---|
| 703 | error = RET_RST_DROP;
|
---|
| 704 | }
|
---|
| 705 | cep->p_dstaddr = NULL;
|
---|
| 706 | cep->snd_tskid = TA_NULL;
|
---|
| 707 | cep->snd_tfn = cep->snd_nblk_tfn = TFN_TCP_UNDEF;
|
---|
| 708 | }
|
---|
| 709 | else
|
---|
| 710 |
|
---|
| 711 | #endif /* of #ifdef TCP_CFG_NON_BLOCKING */
|
---|
| 712 |
|
---|
| 713 | NET_COUNT_TCP(net_count_tcp[NC_TCP_CONNECTS], 1);
|
---|
| 714 | }
|
---|
| 715 | }
|
---|
| 716 | else {
|
---|
| 717 | /* ACK ãã©ã°ããªãã¨ãã¯ãACK ãéã£ã¦ãCEP ã®ç¶æ
|
---|
| 718 | ã SYN åä¿¡æ¸ã¿ã«ããã*/
|
---|
| 719 | cep->flags |= TCP_CEP_FLG_ACK_NOW;
|
---|
| 720 | cep->timer[TCP_TIM_REXMT] = 0;
|
---|
| 721 | cep->fsm_state = TCP_FSM_SYN_RECVD;
|
---|
| 722 | }
|
---|
| 723 |
|
---|
| 724 | return error;
|
---|
| 725 | }
|
---|
| 726 |
|
---|
| 727 | /*
|
---|
| 728 | * trim_length -- åä¿¡ãã SDU é·ã調æ´ããã
|
---|
| 729 | */
|
---|
| 730 |
|
---|
| 731 | static void
|
---|
| 732 | trim_length (T_TCP_HDR *tcph, T_TCP_CEP *cep)
|
---|
| 733 | {
|
---|
| 734 | tcph->seq ++;
|
---|
| 735 | if (tcph->sum > cep->rcv_wnd) { /* 注æ: tcph->sum 㯠SDU é· */
|
---|
| 736 | /*
|
---|
| 737 | * SDU é·ãåä¿¡ã¦ã£ã³ããµã¤ãºãã大ããã¨ãã¯ãåä¿¡ã¦ã£ã³ããµã¤ãºä»¥éã¯
|
---|
| 738 | * ç ´æ£ããFIN ã«å¿çããªããã¨ã§ãç ´æ£ãããã¼ã¿ãåéãããã
|
---|
| 739 | */
|
---|
| 740 | tcph->sum = (uint16_t)cep->rcv_wnd;
|
---|
| 741 | tcph->flags &= ~TCP_FLG_FIN;
|
---|
| 742 | }
|
---|
| 743 | cep->snd_wl1 = tcph->seq - 1; /* cep->snd_wl1: ã¦ã£ã³ãæ´æ° SEQ çªå· */
|
---|
| 744 |
|
---|
| 745 | #ifdef TCP_CFG_EXTENTIONS
|
---|
| 746 | cep->rcv_up = tcph->seq; /* cep->rcv_up : åä¿¡ããç·æ¥ãã¤ã³ã¿ */
|
---|
| 747 | #endif
|
---|
| 748 | }
|
---|
| 749 |
|
---|
| 750 | /*
|
---|
| 751 | * proc_ack2 -- ACK ã®å¦ç (2)
|
---|
| 752 | *
|
---|
| 753 | * æ»ãå¤
|
---|
| 754 | *
|
---|
| 755 | * RET_OK æ£å¸¸
|
---|
| 756 | * RET_RETURN æ£å¸¸ããªã¿ã¼ã³ããã
|
---|
| 757 | * RET_DROP ã¨ã©ã¼ãã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 758 | * RET_RST_DROP ã¨ã©ã¼ãRST ãéä¿¡ããã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 759 | */
|
---|
| 760 |
|
---|
| 761 | static ER
|
---|
| 762 | proc_ack2 (T_NET_BUF *input, T_TCP_CEP *cep, uint_t thoff, bool_t *needoutput)
|
---|
| 763 | {
|
---|
| 764 | T_TCP_HDR *tcph;
|
---|
| 765 | ER ret = RET_OK;
|
---|
| 766 | uint32_t acked;
|
---|
| 767 | bool_t ourfinisacked = false;
|
---|
| 768 |
|
---|
| 769 | tcph = GET_TCP_HDR(input, thoff);
|
---|
| 770 |
|
---|
| 771 | /*
|
---|
| 772 | * ç¸æã«å信確èªããã ACK ãããã¾ã 確èªããã¦ããªã
|
---|
| 773 | * æå°éä¿¡ SEQ (snd_una) ãå¼ãã¨ãéä¿¡ã¦ã£ã³ããããã¡ãã
|
---|
| 774 | * åé¤ãã¦ãããªã¯ãããæ° (acked) ã«ãªãã
|
---|
| 775 | */
|
---|
| 776 | acked = tcph->ack - cep->snd_una;
|
---|
| 777 | NET_COUNT_TCP(net_count_tcp[NC_TCP_RECV_ACKS], 1);
|
---|
| 778 |
|
---|
| 779 | /*
|
---|
| 780 | * å¾å¾©æéè¨æ¸¬ (rtt) ãè¨å®ããã¦ãã¦ãè¨æ¸¬éå§ SEQ ãã
|
---|
| 781 | * å¾ã® ACK ãåä¿¡ããããã¿ã¤ãããã¯ãªãããã£ã³ã»ã«ãã
|
---|
| 782 | * åéã¿ã¤ããåè¨å®ããã
|
---|
| 783 | */
|
---|
| 784 | if (cep->rtt && SEQ_GT(tcph->ack, cep->rtseq)) {
|
---|
| 785 | set_rexmt_timer(cep, cep->rtt);
|
---|
| 786 | }
|
---|
| 787 |
|
---|
| 788 | /*
|
---|
| 789 | * å
|
---|
| 790 | ¨ã¦ã®æªç¢ºèªãã¼ã¿ã ACK ãããããåéã¿ã¤ããåæ¢ãã
|
---|
| 791 | * åéãè¨æ¶ãã (ããã«åºåãæç¶)ã
|
---|
| 792 | * ãããACK ãã¹ããããã«å¤ãã®ãã¼ã¿ããããªããåéã¿ã¤ãã«
|
---|
| 793 | * ç¾å¨ã®åéã¿ã¤ã ã¢ã¦ããè¨å®ããã
|
---|
| 794 | */
|
---|
| 795 | if (tcph->ack == cep->snd_max) { /* cep->snd_max: éä¿¡ããæ大 SEQ */
|
---|
| 796 |
|
---|
| 797 | #ifdef TCP_CFG_SWBUF_CSAVE
|
---|
| 798 |
|
---|
| 799 | /*
|
---|
| 800 | * éä¿¡ã¦ã£ã³ããããã¡ã®çã³ãã¼æ©è½ãæå¹ã®å ´åã¯ã
|
---|
| 801 | * éä¿¡æ¸ã¿ã§ãACKãå®äºããã¾ã§åéã¿ã¤ããå¤æ´ããªãã
|
---|
| 802 | */
|
---|
| 803 | if ((cep->flags & TCP_CEP_FLG_WBCS_MASK) == TCP_CEP_FLG_WBCS_ACKED)
|
---|
| 804 | cep->timer[TCP_TIM_REXMT] = 0;
|
---|
| 805 |
|
---|
| 806 | #else /* of #ifdef TCP_CFG_SWBUF_CSAVE */
|
---|
| 807 |
|
---|
| 808 | cep->timer[TCP_TIM_REXMT] = 0;
|
---|
| 809 |
|
---|
| 810 | #endif /* of #ifdef TCP_CFG_SWBUF_CSAVE */
|
---|
| 811 |
|
---|
| 812 | *needoutput = true;
|
---|
| 813 | }
|
---|
| 814 | else if (cep->timer[TCP_TIM_PERSIST] == 0) {
|
---|
| 815 | cep->timer[TCP_TIM_REXMT] = cep->rxtcur; /* cep->rxtcur: ç¾å¨ã®åéã¿ã¤ã ã¢ã¦ã */
|
---|
| 816 | }
|
---|
| 817 |
|
---|
| 818 | /* ç¸æãå信確èªãããã¼ã¿ãããã¨ãã®å¦ç */
|
---|
| 819 | if (acked) {
|
---|
| 820 | uint32_t cw = cep->snd_cwnd; /* cep->snd_cwnd: 輻輳ã¦ã£ã³ããµã¤ãº */
|
---|
| 821 | uint32_t incr = cep->maxseg; /* cep->maxseg: æ大ã»ã°ã¡ã³ããµã¤ãº */
|
---|
| 822 |
|
---|
| 823 | /*
|
---|
| 824 | * æ°ãã«ç¸æãå信確èªãããã¼ã¿ããã£ãã¨ãã¯ã
|
---|
| 825 | * 輻輳ã¦ã£ã³ããµã¤ãºã大ããããã
|
---|
| 826 | * 輻輳ã¦ã£ã³ããµã¤ãº (snd_cwnd) ã
|
---|
| 827 | * 輻輳ã¦ã£ã³ããµã¤ãºã®ãããå¤ (snd_ssthresh) ãã大ããã¨ãã¯
|
---|
| 828 | * 輻輳åé¿å¶å¾¡ãè¡ãã
|
---|
| 829 | *
|
---|
| 830 | * snd_cwnd = snd_cwnd + maxseg * maxseg / snd_cwnd;
|
---|
| 831 | *
|
---|
| 832 | * çãããå°ããã¨ãã¯ãã¹ãã¼ã¹ã¿ã¼ãå¶å¾¡ãè¡ãã
|
---|
| 833 | *
|
---|
| 834 | * snd_cwnd = snd_cwnd + maxseg
|
---|
| 835 | *
|
---|
| 836 | */
|
---|
| 837 | if (cw > cep->snd_ssthresh)
|
---|
| 838 | /* 輻輳åé¿å¶å¾¡ */
|
---|
| 839 | incr = incr * incr / cw;
|
---|
| 840 |
|
---|
| 841 | if (cw + incr < MAX_TCP_WIN_SIZE)
|
---|
| 842 | cep->snd_cwnd = (uint16_t)(cw + incr);
|
---|
| 843 | else
|
---|
| 844 | cep->snd_cwnd = MAX_TCP_WIN_SIZE;
|
---|
| 845 |
|
---|
| 846 | /*
|
---|
| 847 | * éä¿¡ã¦ã£ã³ããããã¡ãããç¸æãå信確èªãããã¼ã¿æ° (acked) ã®ãã¼ã¿ãåé¤ããã
|
---|
| 848 | */
|
---|
| 849 | if (acked > cep->swbuf_count) {
|
---|
| 850 | cep->snd_wnd -= cep->swbuf_count;
|
---|
| 851 | TCP_DROP_SWBUF(cep, (uint_t)cep->swbuf_count);
|
---|
| 852 | ourfinisacked = true;
|
---|
| 853 | }
|
---|
| 854 | else {
|
---|
| 855 | cep->snd_wnd -= (uint16_t)acked;
|
---|
| 856 | TCP_DROP_SWBUF(cep, (uint_t)acked);
|
---|
| 857 | ourfinisacked = false;
|
---|
| 858 | }
|
---|
| 859 |
|
---|
| 860 | /* éä¿¡ã¦ã£ã³ããããã¡ã«ç©ºããã§ãããã¨ãç¥ãããã*/
|
---|
| 861 | syscall(set_flg(cep->snd_flgid, TCP_CEP_EVT_SWBUF_READY));
|
---|
| 862 |
|
---|
| 863 | /*
|
---|
| 864 | * éé確èªããã¦ããªãæå°éä¿¡ SEQ (snd_una) ã
|
---|
| 865 | * ä»åéé確èªããã ACK ã¾ã§é²ãã
|
---|
| 866 | * 次ã®éä¿¡ãã¼ã¿ã® SEQ (snd_nxt) ããæ°ãã
|
---|
| 867 | * éé確èªããã¦ããªãæå°éä¿¡ SEQ (snd_una)
|
---|
| 868 | * ã¾ã§é²ããã
|
---|
| 869 | */
|
---|
| 870 | cep->snd_una += acked;
|
---|
| 871 | if (SEQ_LT(cep->snd_nxt, cep->snd_una))
|
---|
| 872 | cep->snd_nxt = cep->snd_una;
|
---|
| 873 |
|
---|
| 874 | /*
|
---|
| 875 | * ç¶æ
|
---|
| 876 | ã«ããåå²
|
---|
| 877 | */
|
---|
| 878 | switch (cep->fsm_state) {
|
---|
| 879 | case TCP_FSM_FIN_WAIT_1: /* APP ãçµäºãFIN éä¿¡æ¸ã¿ãACK å¾
|
---|
| 880 | ã¡ */
|
---|
| 881 | if (ourfinisacked) {
|
---|
| 882 | cep->fsm_state = TCP_FSM_FIN_WAIT_2;
|
---|
| 883 | cep->timer[TCP_TIM_2MSL] = TCP_TVAL_KEEP_COUNT * TCP_TVAL_KEEP_INTERVAL;
|
---|
| 884 | }
|
---|
| 885 | break;
|
---|
| 886 | case TCP_FSM_CLOSING: /* åæã¯ãã¼ãºãFIN 交ææ¸ã¿ãACK å¾
|
---|
| 887 | ã¡ */
|
---|
| 888 | if (ourfinisacked) {
|
---|
| 889 | /*
|
---|
| 890 | * éä¿¡ãã FIN ã確èªããã¦ããã°ç¶æ
|
---|
| 891 | ãå¤æ´ãã
|
---|
| 892 | * ãã¹ã¦ã®ã¿ã¤ãããªã»ããããå¾ã2MSL ã¿ã¤ããè¨å®ããã
|
---|
| 893 | */
|
---|
| 894 | cep->fsm_state = TCP_FSM_TIME_WAIT;
|
---|
| 895 | tcp_cancel_timers(cep);
|
---|
| 896 | cep->timer[TCP_TIM_2MSL] = 2 * TCP_TVAL_MSL;
|
---|
| 897 | }
|
---|
| 898 | break;
|
---|
| 899 | case TCP_FSM_LAST_ACK: /* APP ãçµäºãACK å¾
|
---|
| 900 | ã¡ */
|
---|
| 901 | if (ourfinisacked) {
|
---|
| 902 | /*
|
---|
| 903 | * éä¿¡ãã FIN ã確èªããã¦ããã°ãcep ãã¯ãã¼ãºãã
|
---|
| 904 | * ã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 905 | */
|
---|
| 906 | cep = tcp_close(cep);
|
---|
| 907 | ret = RET_DROP;
|
---|
| 908 | }
|
---|
| 909 | break;
|
---|
| 910 | case TCP_FSM_TIME_WAIT: /* ç¸æããã® FIN åä¿¡æ¸ã¿ãæéå¾
|
---|
| 911 | ã¡ */
|
---|
| 912 | /*
|
---|
| 913 | * ç¸æãã FIN ãåéããããããä¸åº¦2MSL ã¿ã¤ããè¨å®ãã
|
---|
| 914 | * ACK éä¿¡å¾ãã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 915 | */
|
---|
| 916 | cep->timer[TCP_TIM_2MSL] = 2 * TCP_TVAL_MSL;
|
---|
| 917 | return drop_after_ack(input, cep, thoff);
|
---|
| 918 | }
|
---|
| 919 | }
|
---|
| 920 |
|
---|
| 921 | return ret;
|
---|
| 922 | }
|
---|
| 923 |
|
---|
| 924 | /*
|
---|
| 925 | * proc_ack1 -- ACK ã®å¦ç (1)
|
---|
| 926 | *
|
---|
| 927 | * æ»ãå¤:
|
---|
| 928 | * RET_OK æ£å¸¸
|
---|
| 929 | * RET_RETURN æ£å¸¸ããªã¿ã¼ã³ããã
|
---|
| 930 | * RET_DROP ã¨ã©ã¼ãã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 931 | * RET_RST_DROP ã¨ã©ã¼ãRST ãéä¿¡ããã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 932 | *
|
---|
| 933 | */
|
---|
| 934 |
|
---|
| 935 | static ER
|
---|
| 936 | proc_ack1 (T_NET_BUF *input, T_TCP_CEP *cep, uint_t thoff, bool_t *needoutput)
|
---|
| 937 | {
|
---|
| 938 | T_TCP_HDR *tcph = GET_TCP_HDR(input, thoff);
|
---|
| 939 |
|
---|
| 940 | switch (cep->fsm_state) {
|
---|
| 941 | case TCP_FSM_SYN_RECVD: /* SYN ãåä¿¡ããSYN éä¿¡æ¸ã¿ */
|
---|
| 942 |
|
---|
| 943 | /* ç¶æ
|
---|
| 944 | ãå¤æ´ããã*/
|
---|
| 945 | if (cep->flags & TCP_CEP_FLG_NEED_FIN) {
|
---|
| 946 | cep->fsm_state = TCP_FSM_FIN_WAIT_1;
|
---|
| 947 | cep->flags &= ~TCP_CEP_FLG_NEED_FIN;
|
---|
| 948 | }
|
---|
| 949 | else {
|
---|
| 950 | cep->timer[TCP_TIM_KEEP] = TCP_TVAL_KEEP_IDLE;
|
---|
| 951 | cep->fsm_state = TCP_FSM_ESTABLISHED;
|
---|
| 952 |
|
---|
| 953 | /* TCP é信端ç¹ããTCP åä»å£ã解æ¾ããã*/
|
---|
| 954 | cep->rep = NULL;
|
---|
| 955 |
|
---|
| 956 | syscall(set_flg(cep->est_flgid, TCP_CEP_EVT_ESTABLISHED));
|
---|
| 957 |
|
---|
| 958 | #ifdef TCP_CFG_NON_BLOCKING
|
---|
| 959 |
|
---|
| 960 | if (cep->rcv_nblk_tfn == TFN_TCP_ACP_CEP) {
|
---|
| 961 |
|
---|
| 962 | /* ç¸æã®ã¢ãã¬ã¹ãã³ãã¼ããã*/
|
---|
| 963 | *cep->p_dstaddr = cep->dstaddr;
|
---|
| 964 |
|
---|
| 965 | if (IS_PTR_DEFINED(cep->callback)) {
|
---|
| 966 |
|
---|
| 967 | #ifdef TCP_CFG_NON_BLOCKING_COMPAT14
|
---|
| 968 |
|
---|
| 969 | NET_COUNT_MIB(tcp_stats.tcpPassiveOpens, 1);
|
---|
| 970 | NET_COUNT_TCP(net_count_tcp[NC_TCP_ACCEPTS], 1);
|
---|
| 971 | (*cep->callback)(GET_TCP_CEPID(cep), cep->rcv_nblk_tfn, E_OK);
|
---|
| 972 |
|
---|
| 973 | #else /* of #ifdef TCP_CFG_NON_BLOCKING_COMPAT14 */
|
---|
| 974 |
|
---|
| 975 | ER error = E_OK;
|
---|
| 976 |
|
---|
| 977 | NET_COUNT_MIB(tcp_stats.tcpPassiveOpens, 1);
|
---|
| 978 | NET_COUNT_TCP(net_count_tcp[NC_TCP_ACCEPTS], 1);
|
---|
| 979 | (*cep->callback)(GET_TCP_CEPID(cep), cep->rcv_nblk_tfn, (void*)&error);
|
---|
| 980 |
|
---|
| 981 | #endif /* of #ifdef TCP_CFG_NON_BLOCKING_COMPAT14 */
|
---|
| 982 |
|
---|
| 983 | cep->p_dstaddr = NULL;
|
---|
| 984 | cep->rcv_tskid = TA_NULL;
|
---|
| 985 | cep->rcv_tfn = cep->rcv_nblk_tfn = TFN_TCP_UNDEF;
|
---|
| 986 | }
|
---|
| 987 | else {
|
---|
| 988 | syslog(LOG_WARNING, "[TCP] no call back, CEP: %d.", GET_TCP_CEPID(cep));
|
---|
| 989 | cep->p_dstaddr = NULL;
|
---|
| 990 | cep->rcv_tskid = TA_NULL;
|
---|
| 991 | cep->rcv_tfn = cep->rcv_nblk_tfn = TFN_TCP_UNDEF;
|
---|
| 992 | return RET_RST_DROP;
|
---|
| 993 | }
|
---|
| 994 | }
|
---|
| 995 |
|
---|
| 996 | if (cep->snd_nblk_tfn == TFN_TCP_CON_CEP) {
|
---|
| 997 |
|
---|
| 998 | /* ç¸æã®ã¢ãã¬ã¹ãã³ãã¼ããã*/
|
---|
| 999 | *cep->p_dstaddr = cep->dstaddr;
|
---|
| 1000 |
|
---|
| 1001 | if (IS_PTR_DEFINED(cep->callback)) {
|
---|
| 1002 |
|
---|
| 1003 | #ifdef TCP_CFG_NON_BLOCKING_COMPAT14
|
---|
| 1004 |
|
---|
| 1005 | NET_COUNT_MIB(tcp_stats.tcpActiveOpens, 1);
|
---|
| 1006 | NET_COUNT_TCP(net_count_tcp[NC_TCP_CONNECTS], 1);
|
---|
| 1007 | (*cep->callback)(GET_TCP_CEPID(cep), cep->snd_nblk_tfn, E_OK);
|
---|
| 1008 |
|
---|
| 1009 | #else /* of #ifdef TCP_CFG_NON_BLOCKING_COMPAT14 */
|
---|
| 1010 |
|
---|
| 1011 | ER error = E_OK;
|
---|
| 1012 |
|
---|
| 1013 | NET_COUNT_MIB(tcp_stats.tcpActiveOpens, 1);
|
---|
| 1014 | NET_COUNT_TCP(net_count_tcp[NC_TCP_CONNECTS], 1);
|
---|
| 1015 | (*cep->callback)(GET_TCP_CEPID(cep), cep->snd_nblk_tfn, (void*)&error);
|
---|
| 1016 |
|
---|
| 1017 | #endif /* of #ifdef TCP_CFG_NON_BLOCKING_COMPAT14 */
|
---|
| 1018 |
|
---|
| 1019 | cep->p_dstaddr = NULL;
|
---|
| 1020 | cep->snd_tskid = TA_NULL;
|
---|
| 1021 | cep->snd_tfn = cep->snd_nblk_tfn = TFN_TCP_UNDEF;
|
---|
| 1022 | }
|
---|
| 1023 | else {
|
---|
| 1024 | syslog(LOG_WARNING, "[TCP] no call back, CEP: %d.", GET_TCP_CEPID(cep));
|
---|
| 1025 | cep->p_dstaddr = NULL;
|
---|
| 1026 | cep->snd_tskid = TA_NULL;
|
---|
| 1027 | cep->snd_tfn = cep->snd_nblk_tfn = TFN_TCP_UNDEF;
|
---|
| 1028 | return RET_RST_DROP;
|
---|
| 1029 | }
|
---|
| 1030 | }
|
---|
| 1031 |
|
---|
| 1032 | #endif /* of #ifdef TCP_CFG_NON_BLOCKING */
|
---|
| 1033 |
|
---|
| 1034 | if (cep->rcv_tfn == TFN_TCP_ACP_CEP) {
|
---|
| 1035 | NET_COUNT_MIB(tcp_stats.tcpPassiveOpens, 1);
|
---|
| 1036 | NET_COUNT_TCP(net_count_tcp[NC_TCP_ACCEPTS], 1);
|
---|
| 1037 | }
|
---|
| 1038 |
|
---|
| 1039 | if (cep->snd_tfn == TFN_TCP_CON_CEP) {
|
---|
| 1040 | NET_COUNT_MIB(tcp_stats.tcpActiveOpens, 1);
|
---|
| 1041 | NET_COUNT_TCP(net_count_tcp[NC_TCP_CONNECTS], 1);
|
---|
| 1042 | }
|
---|
| 1043 | }
|
---|
| 1044 |
|
---|
| 1045 | /*
|
---|
| 1046 | * SDU ããªã FIN ãã¤ãã¦ããªããã°ãtcp_move_ra2rw() ãå¼åºãã
|
---|
| 1047 | */
|
---|
| 1048 | if (tcph->sum == 0 && (tcph->flags & TCP_FLG_FIN) == 0) /* tcph->sum 㯠SDU é· */
|
---|
| 1049 | tcph->flags = tcp_move_ra2rw(cep, tcph->flags);
|
---|
| 1050 |
|
---|
| 1051 | cep->snd_wl1 = tcph->seq - 1; /* snd_wl1: ã¦ã£ã³ãæ´æ° SEQ */
|
---|
| 1052 |
|
---|
| 1053 | /* break; ä¸ã«è½ã¡ãã*/
|
---|
| 1054 |
|
---|
| 1055 | case TCP_FSM_ESTABLISHED: /* ã³ãã¯ã·ã§ã³éè¨å®äº */
|
---|
| 1056 | case TCP_FSM_FIN_WAIT_1: /* çµäºãã¦ãFIN éä¿¡æ¸ã¿ */
|
---|
| 1057 | case TCP_FSM_FIN_WAIT_2: /* çµäºãFIN ä¼é確èªåä¿¡ãFINå¾
|
---|
| 1058 | ã¡*/
|
---|
| 1059 | case TCP_FSM_CLOSE_WAIT: /* FIN åä¿¡ãã¯ãã¼ãºå¾
|
---|
| 1060 | ã¡ */
|
---|
| 1061 | case TCP_FSM_CLOSING: /* çµäºãFIN 交ææ¸ã¿ãACK å¾
|
---|
| 1062 | ã¡ */
|
---|
| 1063 | case TCP_FSM_LAST_ACK: /* FIN åä¿¡ãçµäºãACK å¾
|
---|
| 1064 | ã¡ */
|
---|
| 1065 | case TCP_FSM_TIME_WAIT: /* çµäºãæéå¾
|
---|
| 1066 | ã¡ */
|
---|
| 1067 |
|
---|
| 1068 | if (SEQ_LE(tcph->ack, cep->snd_una)) {
|
---|
| 1069 |
|
---|
| 1070 | /*
|
---|
| 1071 | * åä¿¡ç¢ºèª ACK ã æªç¢ºèªã®æå°éä¿¡ SEQ (snd_una) ã¨åãã以åã®ã¨ãã®å¦ç
|
---|
| 1072 | * ã¤ã¾ããå¤éã« ACK ãåä¿¡ãããã¨ãæå³ãã¦ããã
|
---|
| 1073 | */
|
---|
| 1074 |
|
---|
| 1075 | if (tcph->sum == 0 && tcph->win == cep->snd_wnd) { /* tcph->sum 㯠SDU é· */
|
---|
| 1076 |
|
---|
| 1077 | /*
|
---|
| 1078 | * SDU ããªããç¸æã®ã¦ã£ã³ããµã¤ãºãå¤æ´ããã¦ããªããã°ã
|
---|
| 1079 | * ãã§ã«éä¿¡ããã»ã°ã¡ã³ãã®ä¸ã§ãACK (tcph->ack) ã¨
|
---|
| 1080 | * åã SEQ ããå§ã¾ãã»ã°ã¡ã³ãããéä¸ã§æ¶å¤±ããå¯è½æ§ãããã
|
---|
| 1081 | * ãã®å ´åã¯ãé«éå転éã¨é«éãªã«ããªãè¡ãã
|
---|
| 1082 | */
|
---|
| 1083 | NET_COUNT_TCP(net_count_tcp[NC_TCP_RECV_DUP_ACKS], 1);
|
---|
| 1084 |
|
---|
| 1085 | if (cep->timer[TCP_TIM_REXMT] == 0 || tcph->ack != cep->snd_una) {
|
---|
| 1086 |
|
---|
| 1087 | /*
|
---|
| 1088 | * åéã¿ã¤ããã»ããããã¦ããªãã¨ãã
|
---|
| 1089 | * ã¾ãã¯ãACK (tcph->ack) ã¨æªç¢ºèªã®æå°éä¿¡ SEQã
|
---|
| 1090 | * ä¸è´ããªãã¨ãã¯ãå¤é ACK æ°ã 0 ã«ããã
|
---|
| 1091 | */
|
---|
| 1092 | cep->dupacks = 0;
|
---|
| 1093 | }
|
---|
| 1094 |
|
---|
| 1095 | else if (++ cep->dupacks == MAX_TCP_REXMT_THRESH) {
|
---|
| 1096 |
|
---|
| 1097 | /*
|
---|
| 1098 | * å¤é ACK æ°ããããå¤ (æ¨æº 3) ã«ãªã£ãã
|
---|
| 1099 | * é«éå転éå¦çãéå§ããã
|
---|
| 1100 | */
|
---|
| 1101 | uint_t win;
|
---|
| 1102 |
|
---|
| 1103 | /*
|
---|
| 1104 | * 輻輳ã¦ã£ã³ããµã¤ãº(snd_cwnd)ã®ãããå¤ãè¨å®ããã
|
---|
| 1105 | *
|
---|
| 1106 | * ç¸æã®åä¿¡å¯è½ã¦ã£ã³ããµã¤ãº (snd_wnd) ã
|
---|
| 1107 | * 輻輳ã¦ã£ã³ããµã¤ãº (snd_cwnd) ã® 1/2ã
|
---|
| 1108 | * ãã ãã2 * maxseg 以ä¸ã
|
---|
| 1109 | *
|
---|
| 1110 | */
|
---|
| 1111 | if (cep->snd_wnd < cep->snd_cwnd)
|
---|
| 1112 | win = cep->snd_wnd / 2 / cep->maxseg;
|
---|
| 1113 | else
|
---|
| 1114 | win = cep->snd_cwnd / 2 / cep->maxseg;
|
---|
| 1115 | if (win < 2)
|
---|
| 1116 | win = 2;
|
---|
| 1117 | cep->snd_ssthresh = win * cep->maxseg;
|
---|
| 1118 |
|
---|
| 1119 | /* åéã¿ã¤ãã¨å¾å¾©æéããªã»ããããã*/
|
---|
| 1120 | cep->timer[TCP_TIM_REXMT] = 0;
|
---|
| 1121 | cep->rtt = 0;
|
---|
| 1122 |
|
---|
| 1123 | /* æ¶å¤±ããã»ã°ã¡ã³ããéä¿¡ããã*/
|
---|
| 1124 | cep->snd_old_nxt = cep->snd_nxt;
|
---|
| 1125 | cep->snd_nxt = tcph->ack;
|
---|
| 1126 | cep->snd_cwnd = cep->maxseg;
|
---|
| 1127 |
|
---|
| 1128 | /*
|
---|
| 1129 | * snd_nxt ãå
|
---|
| 1130 | ã«æ»ãããã«è¨å®ãã¦
|
---|
| 1131 | * éä¿¡ãæ示ããã
|
---|
| 1132 | */
|
---|
| 1133 | cep->flags |= TCP_CEP_FLG_POST_OUTPUT |
|
---|
| 1134 | TCP_CEP_FLG_FORCE |
|
---|
| 1135 | TCP_CEP_FLG_FORCE_CLEAR |
|
---|
| 1136 | TCP_CEP_FLG_RESTORE_NEXT_OUTPUT;
|
---|
| 1137 | sig_sem(SEM_TCP_POST_OUTPUT);
|
---|
| 1138 |
|
---|
| 1139 | /* 輻輳ã¦ã£ã³ããµã¤ãºãæ´æ°ããã*/
|
---|
| 1140 | cep->snd_cwnd = (uint16_t)(cep->snd_ssthresh
|
---|
| 1141 | + cep->maxseg * cep->dupacks);
|
---|
| 1142 |
|
---|
| 1143 | return RET_DROP;
|
---|
| 1144 | }
|
---|
| 1145 |
|
---|
| 1146 | else if (cep->dupacks > MAX_TCP_REXMT_THRESH) {
|
---|
| 1147 |
|
---|
| 1148 | /*
|
---|
| 1149 | * å¤é ACK æ°ããããå¤ (æ¨æº 3) ãè¶
|
---|
| 1150 | ããã
|
---|
| 1151 | * 輻輳ã¦ã£ã³ããµã¤ãºãå¢å ããªããåéããã
|
---|
| 1152 | */
|
---|
| 1153 | cep->snd_cwnd += cep->maxseg;
|
---|
| 1154 |
|
---|
| 1155 | /* éä¿¡ãæ示ããã*/
|
---|
| 1156 | cep->flags |= TCP_CEP_FLG_POST_OUTPUT;
|
---|
| 1157 | sig_sem(SEM_TCP_POST_OUTPUT);
|
---|
| 1158 |
|
---|
| 1159 | return RET_DROP;
|
---|
| 1160 | }
|
---|
| 1161 | }
|
---|
| 1162 | else
|
---|
| 1163 | cep->dupacks = 0;
|
---|
| 1164 | break;
|
---|
| 1165 | }
|
---|
| 1166 |
|
---|
| 1167 | /*
|
---|
| 1168 | * åä¿¡ç¢ºèª ACK ã æªç¢ºèªã®æå°éä¿¡ SEQ (snd_una) 以éã®ã¨ãã®å¦ç
|
---|
| 1169 | */
|
---|
| 1170 | if (cep->dupacks >= MAX_TCP_REXMT_THRESH && cep->snd_cwnd > cep->snd_ssthresh)
|
---|
| 1171 | /*
|
---|
| 1172 | * é«éå転éãè¡ã£ã¦ããã¨ãã¯ã輻輳ã¦ã£ã³ããµã¤ãºããããå¤ã¾ã§æ»ãã
|
---|
| 1173 | */
|
---|
| 1174 | cep->snd_cwnd = (uint16_t)cep->snd_ssthresh;
|
---|
| 1175 |
|
---|
| 1176 | cep->dupacks = 0;
|
---|
| 1177 |
|
---|
| 1178 | if (SEQ_GT(tcph->ack, cep->snd_max))
|
---|
| 1179 | /*
|
---|
| 1180 | * åä¿¡ãã ACK ãéä¿¡ããæ大 SEQ ãè¶
|
---|
| 1181 | ãã¦ããã¨ãã®å¦ç
|
---|
| 1182 | */
|
---|
| 1183 | return drop_after_ack(input, cep, thoff);
|
---|
| 1184 |
|
---|
| 1185 | if (cep->flags & TCP_CEP_FLG_NEED_SYN) {
|
---|
| 1186 | /*
|
---|
| 1187 | * SYN éä¿¡è¦æ±ãåãæ¶ãã¦ãæªç¢ºèªã®æå°éä¿¡ SEQ ãé²ããã
|
---|
| 1188 | */
|
---|
| 1189 | cep->flags &= ~TCP_CEP_FLG_NEED_SYN;
|
---|
| 1190 | cep->snd_una ++;
|
---|
| 1191 | }
|
---|
| 1192 |
|
---|
| 1193 | return proc_ack2(input, cep, thoff, needoutput);
|
---|
| 1194 | break;
|
---|
| 1195 | }
|
---|
| 1196 | return RET_OK;
|
---|
| 1197 | }
|
---|
| 1198 |
|
---|
| 1199 | /*
|
---|
| 1200 | * update_wnd -- ã¦ã£ã³ããµã¤ãºãæ´æ°ããã
|
---|
| 1201 | *
|
---|
| 1202 | * æ»ãå¤: éä¿¡ãå¿
|
---|
| 1203 | è¦ãªã true ãè¿ãã
|
---|
| 1204 | */
|
---|
| 1205 |
|
---|
| 1206 | static bool_t
|
---|
| 1207 | update_wnd (T_TCP_HDR *tcph, T_TCP_CEP *cep)
|
---|
| 1208 | {
|
---|
| 1209 |
|
---|
| 1210 | /*
|
---|
| 1211 | * æ´æ°æ¡ä»¶
|
---|
| 1212 | *
|
---|
| 1213 | * ACK ãã©ã°ãã»ããããã¦ãã &&
|
---|
| 1214 | * (ååã¦ã£ã³ããæ´æ°ãã SEQ (snd_wl1) ã SEQ ããå ||
|
---|
| 1215 | * ååã¦ã£ã³ããæ´æ°ãã SEQ (snd_wl1) ã SEQ ã¨åã &&
|
---|
| 1216 | * (ååã¦ã£ã³ããæ´æ°ãã ACK (snd_wl2) ã ACK ããå ||
|
---|
| 1217 | * (ååã¦ã£ã³ããæ´æ°ãã ACK (snd_wl2) ã ACK ã¨åã &&
|
---|
| 1218 | * WIN ãç¸æã®åä¿¡å¯è½ã¦ã£ã³ããµã¤ãº (snd_wnd) ãã大ãã
|
---|
| 1219 | * )
|
---|
| 1220 | * )
|
---|
| 1221 | * )
|
---|
| 1222 | */
|
---|
| 1223 | if ((tcph->flags & TCP_FLG_ACK) &&
|
---|
| 1224 | (SEQ_LT(cep->snd_wl1, tcph->seq) ||
|
---|
| 1225 | (cep->snd_wl1 == tcph->seq &&
|
---|
| 1226 | (SEQ_LT(cep->snd_wl2, tcph->ack) ||
|
---|
| 1227 | (cep->snd_wl2 == tcph->ack && tcph->win > cep->snd_wnd))))) {
|
---|
| 1228 |
|
---|
| 1229 | cep->snd_wnd = tcph->win;
|
---|
| 1230 | cep->snd_wl1 = tcph->seq;
|
---|
| 1231 | cep->snd_wl2 = tcph->ack;
|
---|
| 1232 |
|
---|
| 1233 | if (cep->snd_wnd > cep->max_sndwnd)
|
---|
| 1234 | /* ä»ã¾ã§ã®æ大éä¿¡ã¦ã£ã³ããµã¤ãºãæ´æ°ããã*/
|
---|
| 1235 | cep->max_sndwnd = cep->snd_wnd;
|
---|
| 1236 |
|
---|
| 1237 | #ifdef TCP_CFG_SWBUF_CSAVE
|
---|
| 1238 |
|
---|
| 1239 | if ((cep->flags & TCP_CEP_FLG_WBCS_MASK) == TCP_CEP_FLG_WBCS_WOPEN_PEND) {
|
---|
| 1240 |
|
---|
| 1241 | /*
|
---|
| 1242 | * éä¿¡ã¦ã£ã³ããããã¡ç¨ã®ãããã¯ã¼ã¯ãããã¡å²ãå½ã¦ä¸ã§ã
|
---|
| 1243 | * ç¸æã®åä¿¡ã¦ã£ã³ãã空ãã®ãå¾
|
---|
| 1244 | ã£ã¦ããã¨ãã®å¦ç
|
---|
| 1245 | */
|
---|
| 1246 | if (cep->snd_wnd > 0) {
|
---|
| 1247 |
|
---|
| 1248 | /*
|
---|
| 1249 | * ç¸æã®åä¿¡ã¦ã£ã³ãã空ããã¨ãã¯ã
|
---|
| 1250 | * éä¿¡ã¦ã£ã³ããããã¡ç¨ã®ãããã¯ã¼ã¯ãããã¡å²ãå½ã¦ãåéããã
|
---|
| 1251 | */
|
---|
| 1252 | cep->flags = (cep->flags & ~TCP_CEP_FLG_WBCS_MASK)
|
---|
| 1253 | | TCP_CEP_FLG_WBCS_FREE | TCP_CEP_FLG_POST_OUTPUT;
|
---|
| 1254 | sig_sem(SEM_TCP_POST_OUTPUT);
|
---|
| 1255 | }
|
---|
| 1256 | }
|
---|
| 1257 |
|
---|
| 1258 | #endif /* of #ifdef TCP_CFG_SWBUF_CSAVE */
|
---|
| 1259 |
|
---|
| 1260 | return true;
|
---|
| 1261 | }
|
---|
| 1262 | else
|
---|
| 1263 | return false;
|
---|
| 1264 | }
|
---|
| 1265 |
|
---|
| 1266 | /*
|
---|
| 1267 | * proc_urg -- ç·æ¥ãã¼ã¿ã¤ãã®ã»ã°ã¡ã³ãã®å¦ç
|
---|
| 1268 | */
|
---|
| 1269 |
|
---|
| 1270 | #ifdef TCP_CFG_EXTENTIONS
|
---|
| 1271 |
|
---|
| 1272 | static void
|
---|
| 1273 | proc_urg (T_TCP_HDR *tcph, T_TCP_CEP *cep)
|
---|
| 1274 | {
|
---|
| 1275 | if ((tcph->flags & TCP_FLG_URG) && VALID_URG_POINTER(tcph->urp) &&
|
---|
| 1276 | TCP_FSM_HAVE_RCVD_FIN(cep->fsm_state) == 0) {
|
---|
| 1277 |
|
---|
| 1278 | /* ç·æ¥ãã¼ã¿ã¤ãã®ã»ã°ã¡ã³ãã®å¦ç */
|
---|
| 1279 |
|
---|
| 1280 | NET_COUNT_TCP(net_count_tcp[NC_TCP_RECV_URG_SEGS], 1);
|
---|
| 1281 | if (tcph->urp + cep->rwbuf_count > cep->rbufsz) {
|
---|
| 1282 |
|
---|
| 1283 | /*
|
---|
| 1284 | * ç·æ¥ãã¤ã³ã¿ã®ä½ç½®ãåä¿¡ã¦ã£ã³ããããã¡ã®
|
---|
| 1285 | * ç¯å²ãè¶
|
---|
| 1286 | ããã¨ãã¯ä½ãããªãã
|
---|
| 1287 | */
|
---|
| 1288 | tcph->urp = 0;
|
---|
| 1289 | tcph->flags &= ~TCP_FLG_URG;
|
---|
| 1290 | }
|
---|
| 1291 |
|
---|
| 1292 | if (SEQ_GT(tcph->seq + tcph->urp, cep->rcv_up))
|
---|
| 1293 | /* ç·æ¥ãã¤ã³ã¿ãæ´æ°ãããã¨ãã®å¦ç */
|
---|
| 1294 | cep->rcv_up = tcph->seq + tcph->urp;
|
---|
| 1295 |
|
---|
| 1296 | if ((tcph->flags & TCP_FLG_URG) && (tcph->urp + TCP_CFG_URG_OFFSET) < tcph->sum) { /* tcph->sum 㯠TCP ã® SDU é· */
|
---|
| 1297 |
|
---|
| 1298 | /*
|
---|
| 1299 | * ç·æ¥ãã¤ã³ã¿ã®ä½ç½®ããä»ååä¿¡ããã»ã°ã¡ã³ãå
|
---|
| 1300 | ã®å ´åã¯ã
|
---|
| 1301 | * ã³ã¼ã«ããã¯é¢æ°ãå¼ã³åºãã
|
---|
| 1302 | */
|
---|
| 1303 | cep->urg_tcph = tcph;
|
---|
| 1304 | if (IS_PTR_DEFINED(cep->callback)) {
|
---|
| 1305 |
|
---|
| 1306 | #ifdef TCP_CFG_NON_BLOCKING_COMPAT14
|
---|
| 1307 |
|
---|
| 1308 | (*cep->callback)(GET_TCP_CEPID(cep), TEV_TCP_RCV_OOB, (void*)(uint32_t)1);
|
---|
| 1309 |
|
---|
| 1310 | #else /* of #ifdef TCP_CFG_NON_BLOCKING_COMPAT14 */
|
---|
| 1311 |
|
---|
| 1312 | uint32_t len = 1;
|
---|
| 1313 |
|
---|
| 1314 | (*cep->callback)(GET_TCP_CEPID(cep), TEV_TCP_RCV_OOB, (void*)&len);
|
---|
| 1315 |
|
---|
| 1316 | #endif /* of #ifdef TCP_CFG_NON_BLOCKING_COMPAT14 */
|
---|
| 1317 |
|
---|
| 1318 | }
|
---|
| 1319 | else {
|
---|
| 1320 | syslog(LOG_WARNING, "[TCP] no call back for OOB, CEP: %d.", GET_TCP_CEPID(cep));
|
---|
| 1321 | }
|
---|
| 1322 |
|
---|
| 1323 | if (cep->urg_tcph != NULL) {
|
---|
| 1324 | /* ã³ã¼ã«ããã¯é¢æ°å
|
---|
| 1325 | 㧠tcp_rcv_oob() ãå¼åºããªãã£ãã*/
|
---|
| 1326 | cep->urg_tcph = NULL;
|
---|
| 1327 | tcph->urp = 0;
|
---|
| 1328 | }
|
---|
| 1329 | else {
|
---|
| 1330 | /*
|
---|
| 1331 | * ã³ã¼ã«ããã¯é¢æ°å
|
---|
| 1332 | 㧠tcp_rcv_oob() ãå¼åºããæã¯ã
|
---|
| 1333 | * SDU é·ã®è£æ£å¤ãè¨å®ããã
|
---|
| 1334 | */
|
---|
| 1335 | tcph->urp = 1;
|
---|
| 1336 | }
|
---|
| 1337 | }
|
---|
| 1338 | else if (tcph->urp > 0) {
|
---|
| 1339 | tcph->urp = 0;
|
---|
| 1340 | }
|
---|
| 1341 |
|
---|
| 1342 | }
|
---|
| 1343 | else if (SEQ_GT(cep->rcv_nxt, cep->rcv_up)) {
|
---|
| 1344 | cep->rcv_up = cep->rcv_nxt;
|
---|
| 1345 | tcph->urp = 0;
|
---|
| 1346 | }
|
---|
| 1347 | }
|
---|
| 1348 |
|
---|
| 1349 | #else /* of #ifdef TCP_CFG_EXTENTIONS */
|
---|
| 1350 |
|
---|
| 1351 | static void
|
---|
| 1352 | proc_urg (T_TCP_HDR *tcph, T_TCP_CEP *cep)
|
---|
| 1353 | {
|
---|
| 1354 | tcph->urp = 0;
|
---|
| 1355 | }
|
---|
| 1356 |
|
---|
| 1357 | #endif /* of #ifdef TCP_CFG_EXTENTIONS */
|
---|
| 1358 |
|
---|
| 1359 | /*
|
---|
| 1360 | * drop_after_ack -- åä¿¡ã»ã°ã¡ã³ããç ´æ£ããå¾ãACK ãè¿ã (注æ: ååã¨ã¯åã£ã¦ããªã)ã
|
---|
| 1361 | *
|
---|
| 1362 | * æ»ãå¤:
|
---|
| 1363 | * RET_RETURN æ£å¸¸ããªã¿ã¼ã³ããã
|
---|
| 1364 | * RET_RST_DROP ã¨ã©ã¼ãRST ãéä¿¡ããã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 1365 | */
|
---|
| 1366 |
|
---|
| 1367 | static ER
|
---|
| 1368 | drop_after_ack (T_NET_BUF *input, T_TCP_CEP *cep, uint_t thoff)
|
---|
| 1369 | {
|
---|
| 1370 | T_TCP_HDR *tcph = GET_TCP_HDR(input, thoff);
|
---|
| 1371 |
|
---|
| 1372 | /*
|
---|
| 1373 | * SYN åä¿¡ç¶æ
|
---|
| 1374 | ã§ãACK ãéé確èªããã¦ããªãæå°éä¿¡ SEQ (snd_una) ãã
|
---|
| 1375 | * åã®å¤ããéä¿¡ãããæ大 SEQ (snd_max) ããå¾ã®å¤ã®å ´åã¯ãç¸æã« RST ã
|
---|
| 1376 | * éã£ã¦çµäºãããããã¯ã"LAND" DoS æ»æã¸ã®é²å¾¡ã§ãããå½é ããã SYN
|
---|
| 1377 | * ã»ã°ã¡ã³ããéä¿¡ãã¤ã¥ãããã¼ãéã§ã® ACK ã¹ãã¼ã ãé²ãã
|
---|
| 1378 | */
|
---|
| 1379 | if (cep->fsm_state == TCP_FSM_SYN_RECVD && (tcph->flags & TCP_FLG_ACK) &&
|
---|
| 1380 | (SEQ_GT(cep->snd_una, tcph->ack) ||
|
---|
| 1381 | SEQ_GT(tcph->ack, cep->snd_max)))
|
---|
| 1382 | return RET_RST_DROP;
|
---|
| 1383 |
|
---|
| 1384 | syscall(rel_net_buf(input));
|
---|
| 1385 |
|
---|
| 1386 | /* éä¿¡ãæ示ããã*/
|
---|
| 1387 | cep->flags |= TCP_CEP_FLG_ACK_NOW | TCP_CEP_FLG_POST_OUTPUT;
|
---|
| 1388 | sig_sem(SEM_TCP_POST_OUTPUT);
|
---|
| 1389 | return RET_RETURN;
|
---|
| 1390 | }
|
---|
| 1391 |
|
---|
| 1392 | /*
|
---|
| 1393 | * close_connection -- ã³ãã¯ã·ã§ã³éæ¾å¦çãç¸æãã FIN ãåä¿¡ããã
|
---|
| 1394 | */
|
---|
| 1395 |
|
---|
| 1396 | static void
|
---|
| 1397 | close_connection (T_TCP_CEP *cep, bool_t *needoutput)
|
---|
| 1398 | {
|
---|
| 1399 | if (TCP_FSM_HAVE_RCVD_FIN(cep->fsm_state) == 0) {
|
---|
| 1400 |
|
---|
| 1401 | #ifdef TCP_CFG_DELAY_ACK
|
---|
| 1402 |
|
---|
| 1403 | if (cep->flags & TCP_CEP_FLG_NEED_SYN)
|
---|
| 1404 | cep->flags |= TCP_CEP_FLG_DEL_ACK;
|
---|
| 1405 | else
|
---|
| 1406 | cep->flags |= TCP_CEP_FLG_ACK_NOW;
|
---|
| 1407 |
|
---|
| 1408 | #else/* of #ifdef TCP_CFG_DELAY_ACK */
|
---|
| 1409 |
|
---|
| 1410 | cep->flags |= TCP_CEP_FLG_ACK_NOW;
|
---|
| 1411 |
|
---|
| 1412 | #endif/* of #ifdef TCP_CFG_DELAY_ACK */
|
---|
| 1413 |
|
---|
| 1414 | cep->rcv_nxt ++;
|
---|
| 1415 | }
|
---|
| 1416 |
|
---|
| 1417 | switch (cep->fsm_state) {
|
---|
| 1418 | case TCP_FSM_SYN_RECVD: /* SYN ãåä¿¡ããSYN éä¿¡æ¸ã¿ */
|
---|
| 1419 | case TCP_FSM_ESTABLISHED: /* ã³ãã¯ã·ã§ã³éè¨å®äº */
|
---|
| 1420 | cep->fsm_state = TCP_FSM_CLOSE_WAIT;
|
---|
| 1421 | break;
|
---|
| 1422 |
|
---|
| 1423 | case TCP_FSM_FIN_WAIT_1: /* APP ãçµäºãFIN éä¿¡æ¸ã¿ãACK å¾
|
---|
| 1424 | ã¡ */
|
---|
| 1425 | cep->fsm_state = TCP_FSM_CLOSING;
|
---|
| 1426 | break;
|
---|
| 1427 |
|
---|
| 1428 | case TCP_FSM_FIN_WAIT_2: /* ç¸æããã® FIN å¾
|
---|
| 1429 | ã¡ */
|
---|
| 1430 | cep->fsm_state = TCP_FSM_TIME_WAIT;
|
---|
| 1431 | tcp_cancel_timers(cep);
|
---|
| 1432 | cep->timer[TCP_TIM_2MSL] = 2 * TCP_TVAL_MSL;
|
---|
| 1433 |
|
---|
| 1434 | /*
|
---|
| 1435 | * FIN WAIT 2 ç¶æ
|
---|
| 1436 | ã§ã¯ã
|
---|
| 1437 | * åä¿¡ã¯å¯è½ã§ãããããã§ã«éä¿¡ã¯çµäºãã¦ããã
|
---|
| 1438 | * ç¸æã®éä¿¡ãçµäºããã®ã§ãå
|
---|
| 1439 | ¥åã¿ã¹ã¯ã®ã¿èµ·åºããã
|
---|
| 1440 | */
|
---|
| 1441 | syscall(set_flg(cep->snd_flgid, TCP_CEP_EVT_SWBUF_READY));
|
---|
| 1442 |
|
---|
| 1443 | #if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0
|
---|
| 1444 |
|
---|
| 1445 | /* ç¸æããã® FIN ã«å¯¾ãã¦å¿çãè¿ãã*/
|
---|
| 1446 | tcp_respond(NULL, cep, cep->rcv_nxt, cep->snd_una,
|
---|
| 1447 | cep->rbufsz - cep->rwbuf_count, TCP_FLG_ACK);
|
---|
| 1448 | cep->flags &= ~TCP_CEP_FLG_ACK_NOW;
|
---|
| 1449 | *needoutput = false;
|
---|
| 1450 |
|
---|
| 1451 | /*
|
---|
| 1452 | * å¿
|
---|
| 1453 | è¦ãªæ
|
---|
| 1454 | å ±ã Time Wait ç¨ TCP é信端ç¹ã«ç§»ãã¦ã
|
---|
| 1455 | * æ¨æºã® TCP é信端ç¹ãéæ¾ããã
|
---|
| 1456 | */
|
---|
| 1457 | tcp_move_twcep(cep);
|
---|
| 1458 |
|
---|
| 1459 | #endif /* of #if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0 */
|
---|
| 1460 |
|
---|
| 1461 | break;
|
---|
| 1462 |
|
---|
| 1463 | case TCP_FSM_TIME_WAIT: /* ç¸æããã® FIN åä¿¡æ¸ã¿ãæéå¾
|
---|
| 1464 | ã¡ */
|
---|
| 1465 | cep->timer[TCP_TIM_2MSL] = 2 * TCP_TVAL_MSL;
|
---|
| 1466 | break;
|
---|
| 1467 | }
|
---|
| 1468 | }
|
---|
| 1469 |
|
---|
| 1470 | /*
|
---|
| 1471 | * tcp_input -- TCP ã®å
|
---|
| 1472 | ¥åé¢æ°
|
---|
| 1473 | *
|
---|
| 1474 | * 注æ: input ã«ã¯ IF ããã㨠IP ããããå
|
---|
| 1475 | é ã«ããã
|
---|
| 1476 | */
|
---|
| 1477 |
|
---|
| 1478 | uint_t
|
---|
| 1479 | tcp_input (T_NET_BUF **inputp, uint_t *offp, uint_t *nextp)
|
---|
| 1480 | {
|
---|
| 1481 | T_NET_BUF *input = *inputp;
|
---|
| 1482 | T_IP_HDR *iph;
|
---|
| 1483 | T_TCP_HDR *tcph;
|
---|
| 1484 | T_TCP_CEP *cep = NULL;
|
---|
| 1485 | T_TCP_SEQ iss = 0;
|
---|
| 1486 | ER ret;
|
---|
| 1487 | bool_t needoutput = false;
|
---|
| 1488 | int_t rbfree;
|
---|
| 1489 | int32_t todrop, win;
|
---|
| 1490 | uint16_t seglen;
|
---|
| 1491 | uint8_t flags;
|
---|
| 1492 |
|
---|
| 1493 | #if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0
|
---|
| 1494 | T_TCP_TWCEP *twcep;
|
---|
| 1495 | #endif /* of #if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0 */
|
---|
| 1496 |
|
---|
| 1497 | NET_COUNT_TCP(net_count_tcp[NC_TCP_RECV_OCTETS],
|
---|
| 1498 | input->len - GET_IF_IP_HDR_SIZE(input));
|
---|
| 1499 | NET_COUNT_TCP(net_count_tcp[NC_TCP_RECV_SEGS], 1);
|
---|
| 1500 | NET_COUNT_MIB(tcp_stats.tcpInSegs, 1);
|
---|
| 1501 |
|
---|
| 1502 | /* ãããé·ããã§ãã¯ããã*/
|
---|
| 1503 | if (input->len < IF_IP_TCP_HDR_SIZE) {
|
---|
| 1504 | NET_COUNT_TCP(net_count_tcp[NC_TCP_RECV_BAD_HEADERS], 1);
|
---|
| 1505 | goto drop;
|
---|
| 1506 | }
|
---|
| 1507 |
|
---|
| 1508 | iph = GET_IP_HDR(input);
|
---|
| 1509 | tcph = GET_TCP_HDR(input, *offp);
|
---|
| 1510 |
|
---|
| 1511 | seglen = input->len - *offp; /* TCP ã®ã»ã°ã¡ã³ãé· */
|
---|
| 1512 |
|
---|
| 1513 | if (IN_CKSUM(input, IPPROTO_TCP, *offp, (uint_t)seglen) != 0) {
|
---|
| 1514 | NET_COUNT_TCP(net_count_tcp[NC_TCP_RECV_BAD_CKSUMS], 1);
|
---|
| 1515 | goto drop;
|
---|
| 1516 | }
|
---|
| 1517 |
|
---|
| 1518 | /* TCP ãããé·ããã§ãã¯ããã*/
|
---|
| 1519 | if (TCP_HDR_LEN(tcph->doff) < TCP_HDR_SIZE || TCP_HDR_LEN(tcph->doff) > seglen) {
|
---|
| 1520 | NET_COUNT_TCP(net_count_tcp[NC_TCP_RECV_BAD_HEADERS], 1);
|
---|
| 1521 | goto drop;
|
---|
| 1522 | }
|
---|
| 1523 | tcph->sum = seglen - TCP_HDR_LEN(tcph->doff); /* ãããã tcph->sum 㯠TCP ã® SDU é· */
|
---|
| 1524 |
|
---|
| 1525 | /*
|
---|
| 1526 | * SYN 㨠FIN ã®ä¸¡ããããã»ããããã¦ããã°ç ´æ£ãããnmap çã®å¯¾ç
|
---|
| 1527 | * ãã ããRFC1644 T/TCP æ¡å¼µæ©è½ã¨ç«¶åããã
|
---|
| 1528 | */
|
---|
| 1529 | if ((tcph->flags & (TCP_FLG_SYN | TCP_FLG_FIN)) == (TCP_FLG_SYN | TCP_FLG_FIN))
|
---|
| 1530 | goto drop;
|
---|
| 1531 |
|
---|
| 1532 | /* ãããã¯ã¼ã¯ãªã¼ãã¼ãããã¹ããªã¼ãã¼ã«å¤æããã*/
|
---|
| 1533 |
|
---|
| 1534 | NTOHL(tcph->seq);
|
---|
| 1535 | NTOHL(tcph->ack);
|
---|
| 1536 | NTOHS(tcph->win);
|
---|
| 1537 | NTOHS(tcph->urp);
|
---|
| 1538 | NTOHS(tcph->sport);
|
---|
| 1539 | NTOHS(tcph->dport);
|
---|
| 1540 |
|
---|
| 1541 | find_cep:
|
---|
| 1542 |
|
---|
| 1543 | #if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0
|
---|
| 1544 |
|
---|
| 1545 | /*
|
---|
| 1546 | * ç¶æ
|
---|
| 1547 | ã Time Wait ä¸ã® CEP ãæ¢ç´¢ããã
|
---|
| 1548 | */
|
---|
| 1549 | twcep = tcp_find_twcep(&iph->dst, tcph->dport, &iph->src, tcph->sport);
|
---|
| 1550 | if (twcep != NULL) {
|
---|
| 1551 |
|
---|
| 1552 | if (tcph->flags & TCP_FLG_RST) /* RST ãã©ã°ãåä¿¡ããã¨ãã¯ç¡è¦ããã*/
|
---|
| 1553 | goto drop;
|
---|
| 1554 | else {
|
---|
| 1555 |
|
---|
| 1556 | /*
|
---|
| 1557 | * TCP é信端ç¹ã Time Wait ã®æãç¸æãã¹ãããã»ã°ã¡ã³ããæ¥ãã¨ãã¯ã
|
---|
| 1558 | * ç¸æãã¹ãã® FIN ã«å¯¾ããèªãã¹ãã® ACK ã»ã°ã¡ã³ããéä¸ã§
|
---|
| 1559 | * æ失ãããã¨ãæå³ãã¦ããã®ã§ãACK ã»ã°ã¡ã³ããåéããã
|
---|
| 1560 | */
|
---|
| 1561 |
|
---|
| 1562 | /* ãã¹ããªã¼ãã¼ãããããã¯ã¼ã¯ãªã¼ãã¼ã«æ»ãã*/
|
---|
| 1563 | HTONS(tcph->sport);
|
---|
| 1564 | HTONS(tcph->dport);
|
---|
| 1565 |
|
---|
| 1566 | tcp_respond(input, NULL, twcep->rcv_nxt, twcep->snd_una, twcep->rbufsz - twcep->rwbuf_count, TCP_FLG_ACK);
|
---|
| 1567 | }
|
---|
| 1568 | return IPPROTO_DONE;
|
---|
| 1569 | }
|
---|
| 1570 | else
|
---|
| 1571 | /* æ¨æºã® TCP é信端ç¹ãå¾ãã*/
|
---|
| 1572 | cep = tcp_find_cep(&iph->dst, tcph->dport, &iph->src, tcph->sport);
|
---|
| 1573 |
|
---|
| 1574 | #else /* of #if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0 */
|
---|
| 1575 |
|
---|
| 1576 | /* TCP é信端ç¹ãå¾ãã*/
|
---|
| 1577 | cep = tcp_find_cep(&iph->dst, tcph->dport, &iph->src, tcph->sport);
|
---|
| 1578 |
|
---|
| 1579 | #endif /* of #if defined(NUM_TCP_TW_CEP_ENTRY) && NUM_TCP_TW_CEP_ENTRY > 0 */
|
---|
| 1580 |
|
---|
| 1581 | /*
|
---|
| 1582 | * TCP é信端ç¹ããªãå ´å㨠CEP ã®ç¶æ
|
---|
| 1583 | ãã¯ãã¼ãºãªãç ´æ£ããã
|
---|
| 1584 | */
|
---|
| 1585 | if (cep == NULL) {
|
---|
| 1586 | syslog(LOG_INFO, "[TCP] unexp port: %d.", tcph->dport);
|
---|
| 1587 | goto reset_drop;
|
---|
| 1588 | }
|
---|
| 1589 |
|
---|
| 1590 | #ifdef TCP_CFG_TRACE
|
---|
| 1591 |
|
---|
| 1592 | tcp_input_trace(input, cep);
|
---|
| 1593 |
|
---|
| 1594 | #endif /* of #ifdef TCP_CFG_TRACE */
|
---|
| 1595 |
|
---|
| 1596 |
|
---|
| 1597 | if (cep->fsm_state == TCP_FSM_CLOSED)
|
---|
| 1598 | goto drop;
|
---|
| 1599 |
|
---|
| 1600 | /*
|
---|
| 1601 | * ã³ãã¯ã·ã§ã³éè¨æ¸ã¿ã§ã»ã°ã¡ã³ããåä¿¡ããã¨ãã¯ã
|
---|
| 1602 | * ã¢ã¤ãã«æéã¨çå確èªã¿ã¤ãããªã»ããããã
|
---|
| 1603 | */
|
---|
| 1604 | cep->idle = 0;
|
---|
| 1605 | if (TCP_FSM_HAVE_ESTABLISHED(cep->fsm_state)) {
|
---|
| 1606 | cep->timer[TCP_TIM_KEEP] = TCP_TVAL_KEEP_IDLE;
|
---|
| 1607 | }
|
---|
| 1608 |
|
---|
| 1609 | /* CEP ã®ç¶æ
|
---|
| 1610 | ã LISTEN 以å¤ã®æã¯ããªãã·ã§ã³ãå¦çããã*/
|
---|
| 1611 | if (cep->fsm_state != TCP_FSM_LISTEN)
|
---|
| 1612 | parse_option(tcph, cep);
|
---|
| 1613 |
|
---|
| 1614 | /*
|
---|
| 1615 | * åä¿¡å¯è½ã¦ã£ã³ããµã¤ãºãè¨ç®ããã
|
---|
| 1616 | *
|
---|
| 1617 | * rcv_nxt: åä¿¡ãæå¾
|
---|
| 1618 | ãã¦ããæå°ã® SEQï¼ãã以åã¯åä¿¡æ¸ã¿ï¼
|
---|
| 1619 | * rcv_adv: åä¿¡ãæå¾
|
---|
| 1620 | ãã¦ããæ大㮠SEQ
|
---|
| 1621 | * rbufsz: åä¿¡ã¦ã£ã³ããããã¡ãµã¤ãº
|
---|
| 1622 | * rwbuf_count: åä¿¡ã¦ã£ã³ããããã¡ã«ãããã¼ã¿é
|
---|
| 1623 | * tcph->sum: ä»ååä¿¡ãã SDU ãµã¤ãº
|
---|
| 1624 | *
|
---|
| 1625 | * ä»ååä¿¡ããã»ã°ã¡ã³ããé åºæ´åãã¥ã¼ã«é£çµãã
|
---|
| 1626 | * å¯è½æ§ãããã®ã§ tcph->sum ãèæ
|
---|
| 1627 | ®ããã
|
---|
| 1628 | *
|
---|
| 1629 | */
|
---|
| 1630 | win = cep->rbufsz - (cep->rwbuf_count + tcph->sum);
|
---|
| 1631 | if (win < 0)
|
---|
| 1632 | win = 0;
|
---|
| 1633 | if (win > (int32_t)(cep->rcv_adv - cep->rcv_nxt))
|
---|
| 1634 | cep->rcv_wnd = win;
|
---|
| 1635 | else
|
---|
| 1636 | cep->rcv_wnd = cep->rcv_adv - cep->rcv_nxt;
|
---|
| 1637 |
|
---|
| 1638 | /* CEP ã®ç¶æ
|
---|
| 1639 | ã«ããå¦çãè¡ãã*/
|
---|
| 1640 |
|
---|
| 1641 | if (cep->fsm_state == TCP_FSM_LISTEN) { /* ååãªã¼ãã³ (LISTEN) ã®å¦çã*/
|
---|
| 1642 | if ((ret = listening(input, cep, *offp, iss)) == RET_RST_DROP)
|
---|
| 1643 | goto reset_drop;
|
---|
| 1644 | else if (ret == RET_DROP)
|
---|
| 1645 | goto drop;
|
---|
| 1646 | trim_length(tcph, cep); /* åä¿¡ãã SDU é·ã調æ´ããã*/
|
---|
| 1647 |
|
---|
| 1648 | if (tcph->flags & TCP_FLG_ACK) { /* ACK ãã©ã°ã®å¦ç */
|
---|
| 1649 | if ((ret = proc_ack2(input, cep, *offp, &needoutput)) == RET_DROP)
|
---|
| 1650 | goto drop;
|
---|
| 1651 | else if (ret == RET_RST_DROP)
|
---|
| 1652 | goto reset_drop;
|
---|
| 1653 | else if (ret == RET_RETURN)
|
---|
| 1654 | return IPPROTO_DONE;
|
---|
| 1655 | }
|
---|
| 1656 | }
|
---|
| 1657 | else if (cep->fsm_state == TCP_FSM_SYN_SENT) { /* è½åãªã¼ãã³ãSYN éä¿¡æ¸ã¿ */
|
---|
| 1658 | if ((ret = syn_sent(tcph, cep)) == RET_RST_DROP)
|
---|
| 1659 | goto reset_drop;
|
---|
| 1660 | else if (ret == RET_DROP)
|
---|
| 1661 | goto drop;
|
---|
| 1662 | trim_length(tcph, cep); /* åä¿¡ãã SDU é·ã調æ´ããã*/
|
---|
| 1663 |
|
---|
| 1664 | if (tcph->flags & TCP_FLG_ACK) { /* ACK ãã©ã°ã®å¦ç */
|
---|
| 1665 | if ((ret = proc_ack2(input, cep, *offp, &needoutput)) == RET_DROP)
|
---|
| 1666 | goto drop;
|
---|
| 1667 | else if (ret == RET_RST_DROP)
|
---|
| 1668 | goto reset_drop;
|
---|
| 1669 | else if (ret == RET_RETURN)
|
---|
| 1670 | return IPPROTO_DONE;
|
---|
| 1671 | }
|
---|
| 1672 | }
|
---|
| 1673 | else {
|
---|
| 1674 | if (cep->fsm_state == TCP_FSM_SYN_RECVD) { /* SYN ãåä¿¡ãSYN éä¿¡æ¸ã¿ */
|
---|
| 1675 | /*
|
---|
| 1676 | * ç¸æããå信確èªãéããã¦æ¥ã¦ãã
|
---|
| 1677 | *
|
---|
| 1678 | * ACK <= æªç¢ºèªã®æå°éä¿¡ SEQ (snd_una) &&
|
---|
| 1679 | * éä¿¡ããæ大 SEQ (snd_max) < ACK
|
---|
| 1680 | *
|
---|
| 1681 | * ãªãããªã»ãããéã£ã¦ã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 1682 | */
|
---|
| 1683 | if ((tcph->flags & TCP_FLG_ACK) &&
|
---|
| 1684 | (SEQ_LE(tcph->ack, cep->snd_una) ||
|
---|
| 1685 | SEQ_GT(tcph->ack, cep->snd_max)))
|
---|
| 1686 | goto reset_drop;
|
---|
| 1687 | }
|
---|
| 1688 |
|
---|
| 1689 | /*
|
---|
| 1690 | * RST ãã©ã°ãåä¿¡ããã¨ãã®å¦ç (ç°å¸¸åæ)
|
---|
| 1691 | */
|
---|
| 1692 | if (tcph->flags & TCP_FLG_RST) {
|
---|
| 1693 | if (SEQ_GE(tcph->seq, cep->last_ack_sent) &&
|
---|
| 1694 | SEQ_LT(tcph->seq, cep->last_ack_sent + cep->rcv_wnd)) {
|
---|
| 1695 | /*
|
---|
| 1696 | * åä¿¡ããã»ã°ã¡ã³ãã® SEQ ããæå¾ã«éä¿¡ãã ACK (last_ack_sent)
|
---|
| 1697 | * ãããåä¿¡ã¦ã¤ã³ãã¦ãµã¤ãºã¾ã§ã®éã®å¦ç
|
---|
| 1698 | */
|
---|
| 1699 | switch (cep->fsm_state) {
|
---|
| 1700 | case TCP_FSM_SYN_RECVD: /* SYN ãåä¿¡ããSYN éä¿¡æ¸ã¿ */
|
---|
| 1701 |
|
---|
| 1702 | cep->net_error = EV_CNNRF; /* æ¥ç¶ä¸è½ */
|
---|
| 1703 | cep->error = E_CLS;
|
---|
| 1704 | NET_COUNT_TCP(net_count_tcp[NC_TCP_RECV_RSTS], 1);
|
---|
| 1705 | NET_COUNT_MIB(tcp_stats.tcpAttemptFails, 1);
|
---|
| 1706 | cep = tcp_close(cep);
|
---|
| 1707 | break;
|
---|
| 1708 |
|
---|
| 1709 | case TCP_FSM_ESTABLISHED: /* ã³ãã¯ã·ã§ã³éè¨å®äº */
|
---|
| 1710 | case TCP_FSM_CLOSE_WAIT: /* FIN åä¿¡ãã¯ãã¼ãºå¾
|
---|
| 1711 | ã¡ */
|
---|
| 1712 | NET_COUNT_MIB(tcp_stats.tcpEstabResets, 1);
|
---|
| 1713 | /* fallthrough */
|
---|
| 1714 |
|
---|
| 1715 | case TCP_FSM_FIN_WAIT_1: /* çµäºãã¦ãFIN éä¿¡æ¸ã¿ */
|
---|
| 1716 | case TCP_FSM_FIN_WAIT_2: /* çµäºãFIN ä¼é確èªåä¿¡ãFINå¾
|
---|
| 1717 | ã¡ */
|
---|
| 1718 |
|
---|
| 1719 | cep->net_error = EV_CNRST; /* æ¥ç¶ãªã»ãã */
|
---|
| 1720 | cep->error = E_CLS;
|
---|
| 1721 | NET_COUNT_TCP(net_count_tcp[NC_TCP_RECV_RSTS], 1);
|
---|
| 1722 | /* no break; */
|
---|
| 1723 |
|
---|
| 1724 | case TCP_FSM_CLOSING: /* çµäºãFIN 交ææ¸ã¿ãACK å¾
|
---|
| 1725 | ã¡ */
|
---|
| 1726 | case TCP_FSM_LAST_ACK: /* FIN åä¿¡ãçµäºãACK å¾
|
---|
| 1727 | ã¡ */
|
---|
| 1728 |
|
---|
| 1729 | cep = tcp_close(cep);
|
---|
| 1730 | break;
|
---|
| 1731 | }
|
---|
| 1732 | }
|
---|
| 1733 | goto drop;
|
---|
| 1734 | }
|
---|
| 1735 |
|
---|
| 1736 | /*
|
---|
| 1737 | * CEP ã®ç¶æ
|
---|
| 1738 | ã SYN ãåä¿¡ããSYN éä¿¡æ¸ã¿ã®å ´åã¯ã
|
---|
| 1739 | * åä¿¡ã¦ã£ã³ãã«åã¾ãããã«ãã¼ã¿ã
|
---|
| 1740 | * 調æ´ããåã«ããã®æ¥ç¶ã«ãããã±ãããã©ãããæ¤è¨¼ããã
|
---|
| 1741 | *
|
---|
| 1742 | * åä¿¡ããç¸æã® SEQ < ç¸æã® SEQ ã®åæå¤ (irs)
|
---|
| 1743 | *
|
---|
| 1744 | * ããã¯ã"LAND" DoS æ»æã®é²å¾¡ã§ããã
|
---|
| 1745 | */
|
---|
| 1746 | if (cep->fsm_state == TCP_FSM_SYN_RECVD && SEQ_LT(tcph->seq, cep->irs)) {
|
---|
| 1747 | goto reset_drop;
|
---|
| 1748 | }
|
---|
| 1749 |
|
---|
| 1750 | /*
|
---|
| 1751 | * åä¿¡ãæå¾
|
---|
| 1752 | ãã¦ããæå°ã® SEQ (rcv_nxt) - åä¿¡ããç¸æã® SEQ ã
|
---|
| 1753 | * æ£ãªããrcv_nxt 以åã®ãã¼ã¿ã¯ãã§ã«åä¿¡ãã¦ããã®ã§ããã®é¨åã
|
---|
| 1754 | * åé¤ããã
|
---|
| 1755 | * <---------- rcv_wnd --------->
|
---|
| 1756 | * rcv_nxt rcv_nxt + rcv_wnd
|
---|
| 1757 | * v v
|
---|
| 1758 | * -----+----------------------------+-----
|
---|
| 1759 | * | |
|
---|
| 1760 | * -----+----------------------------+-----
|
---|
| 1761 | * +----------------------+
|
---|
| 1762 | * |***************| |
|
---|
| 1763 | * +----------------------+
|
---|
| 1764 | * ^ ^
|
---|
| 1765 | * seq seq + len
|
---|
| 1766 | * <---------------> åé¤ããã
|
---|
| 1767 | */
|
---|
| 1768 | todrop = cep->rcv_nxt - tcph->seq;
|
---|
| 1769 | if (todrop > 0) {
|
---|
| 1770 |
|
---|
| 1771 | /*
|
---|
| 1772 | * SYN ãã©ã°ãã¤ãã¦ããã¨ãã¯ããã®å (1 ãªã¯ããã)
|
---|
| 1773 | * SEQ ãé²ããç·æ¥ãã¤ã³ã¿ã¨åé¤ããé·ãã調æ´ããã
|
---|
| 1774 | */
|
---|
| 1775 | if (tcph->flags & TCP_FLG_SYN) {
|
---|
| 1776 | tcph->flags &= ~TCP_FLG_SYN;
|
---|
| 1777 | tcph->seq ++;
|
---|
| 1778 | if (tcph->urp > 1)
|
---|
| 1779 | tcph->urp --;
|
---|
| 1780 | else
|
---|
| 1781 | tcph->flags &= ~TCP_FLG_URG;
|
---|
| 1782 | todrop --;
|
---|
| 1783 | }
|
---|
| 1784 |
|
---|
| 1785 | /*
|
---|
| 1786 | * åé¤ããé·ãã SDU ããé·ããã¤ã¾ããåä¿¡ãæå¾
|
---|
| 1787 | ãã¦ãã
|
---|
| 1788 | * æå°ã® SEQ (rcv_nxt) ã«éãã¦ããªããã
|
---|
| 1789 | * åé¤ããé·ãã SDU ã¨åãã§ãFIN ãã©ã°ãã¤ãã¦ãªããã°
|
---|
| 1790 | * å
|
---|
| 1791 | ¨ã¦åé¤ããã
|
---|
| 1792 | */
|
---|
| 1793 | if ( todrop > tcph->sum || /* tcph->sum 㯠TCP ã® SDU é· */
|
---|
| 1794 | (todrop == tcph->sum && (tcph->flags & TCP_FLG_FIN) == 0)) {
|
---|
| 1795 | tcph->flags &= ~TCP_FLG_FIN;
|
---|
| 1796 | cep->flags |= TCP_CEP_FLG_ACK_NOW;
|
---|
| 1797 | todrop = tcph->sum; /* tcph->sum 㯠TCP ã® SDU é· */
|
---|
| 1798 | }
|
---|
| 1799 |
|
---|
| 1800 | /*
|
---|
| 1801 | * SDU ãåã«è©°ããã
|
---|
| 1802 | */
|
---|
| 1803 | if (todrop < tcph->sum) { /* tcph->sum 㯠TCP ã® SDU é· */
|
---|
| 1804 | memcpy(GET_TCP_SDU(input, *offp),
|
---|
| 1805 | GET_TCP_SDU(input, *offp) + todrop, (size_t)(tcph->sum - todrop));
|
---|
| 1806 | }
|
---|
| 1807 |
|
---|
| 1808 | /*
|
---|
| 1809 | * SEQ 㨠SDU é·ã調æ´ããã
|
---|
| 1810 | */
|
---|
| 1811 | tcph->seq += todrop;
|
---|
| 1812 | tcph->sum -= (uint16_t)todrop; /* tcph->sum 㯠TCP ã® SDU é· */
|
---|
| 1813 |
|
---|
| 1814 | /*
|
---|
| 1815 | * ç·æ¥ãã¤ã³ã¿ã調æ´ããã
|
---|
| 1816 | */
|
---|
| 1817 | if (tcph->urp > todrop)
|
---|
| 1818 | tcph->urp -= (uint16_t)todrop;
|
---|
| 1819 | else {
|
---|
| 1820 | tcph->flags &= ~TCP_FLG_URG;
|
---|
| 1821 | tcph->urp = 0;
|
---|
| 1822 | }
|
---|
| 1823 |
|
---|
| 1824 | NET_COUNT_TCP(net_count_tcp[NC_TCP_RECV_DUP_SEGS], 1);
|
---|
| 1825 | }
|
---|
| 1826 |
|
---|
| 1827 | /*
|
---|
| 1828 | * ããã¦ã¼ã¶ã¿ã¹ã¯ãçµäºããå¾ã«ããã¼ã¿ãåä¿¡ãã
|
---|
| 1829 | * å ´åã¯ãRST ãéãã
|
---|
| 1830 | */
|
---|
| 1831 | if (cep->fsm_state == TCP_FSM_LAST_ACK && tcph->sum > 0) { /* tcph->sum 㯠TCP ã® SDU é· */
|
---|
| 1832 | cep = tcp_close(cep);
|
---|
| 1833 | goto reset_drop;
|
---|
| 1834 | }
|
---|
| 1835 |
|
---|
| 1836 | /*
|
---|
| 1837 | * åä¿¡ã»ã°ã¡ã³ããåä¿¡ã¦ã£ã³ããè¶
|
---|
| 1838 | ããå ´åã¯ã
|
---|
| 1839 | * è¶
|
---|
| 1840 | ããåãåãã
|
---|
| 1841 | *
|
---|
| 1842 | * <---------- rcv_wnd --------->
|
---|
| 1843 | * rcv_nxt (rcv_nxt + rcv_wnd)
|
---|
| 1844 | * v v
|
---|
| 1845 | * -----+----------------------------+-----
|
---|
| 1846 | * | |
|
---|
| 1847 | * -----+----------------------------+-----
|
---|
| 1848 | * +----------------------+
|
---|
| 1849 | * | |******|
|
---|
| 1850 | * +----------------------+
|
---|
| 1851 | * ^ ^
|
---|
| 1852 | * seq seq + len
|
---|
| 1853 | * <-----> åé¤ããã
|
---|
| 1854 | */
|
---|
| 1855 | todrop = (tcph->seq + tcph->sum) - (cep->rcv_nxt + cep->rcv_wnd); /* tcph->sum 㯠TCP ã® SDU é· */
|
---|
| 1856 | if (todrop > 0) {
|
---|
| 1857 | if (todrop > tcph->sum) { /* tcph->sum 㯠TCP ã® SDU é· */
|
---|
| 1858 | /*
|
---|
| 1859 | * åä¿¡ãã SDU ã®å
|
---|
| 1860 | ¨ã¦ãåä¿¡ã¦ã£ã³ããè¶
|
---|
| 1861 | ããå ´åã
|
---|
| 1862 | *
|
---|
| 1863 | * TIME_WAIT ä¸ã«ãæ°ããªæ¥ç¶è¦æ±ãåä¿¡ããã
|
---|
| 1864 | * å¤ãæ¥ç¶ãç ´æ£ããæ°ããªæ¥ç¶ãéå§ããã
|
---|
| 1865 | * ãã ããSEQ ã¯åããé²ãã§ããªããã°ãªããªãã
|
---|
| 1866 | */
|
---|
| 1867 | if ((tcph->flags & TCP_FLG_SYN) &&
|
---|
| 1868 | cep->fsm_state == TCP_FSM_TIME_WAIT &&
|
---|
| 1869 | SEQ_GT(tcph->seq, cep->rcv_nxt)) {
|
---|
| 1870 | iss = cep->snd_nxt + TCP_ISS_INCR();
|
---|
| 1871 | tcp_close(cep);
|
---|
| 1872 | syscall(dly_tsk(0));
|
---|
| 1873 | goto find_cep;
|
---|
| 1874 | }
|
---|
| 1875 |
|
---|
| 1876 | /*
|
---|
| 1877 | * åä¿¡ã¦ã£ã³ãã 0 ã§ãåä¿¡ãã SEQ ã¨
|
---|
| 1878 | * åä¿¡ãæå¾
|
---|
| 1879 | ãã¦ããæå°ã® SEQ ãä¸è´ããã¨ãã¯
|
---|
| 1880 | * ACK ãè¿ãããã以å¤ã¯ãã¼ã¿ãç ´æ£ããACK ãè¿ãã
|
---|
| 1881 | */
|
---|
| 1882 | if (cep->rcv_wnd == 0 && (tcph->seq == cep->rcv_nxt || tcph->sum == 0)) {
|
---|
| 1883 | cep->flags |= TCP_CEP_FLG_ACK_NOW;
|
---|
| 1884 | }
|
---|
| 1885 | else if (drop_after_ack(input, cep, *offp) == RET_RST_DROP)
|
---|
| 1886 | goto reset_drop;
|
---|
| 1887 | else {
|
---|
| 1888 | return IPPROTO_DONE;
|
---|
| 1889 | }
|
---|
| 1890 | }
|
---|
| 1891 | tcph->sum -= (uint16_t)todrop; /* tcph->sum 㯠TCP ã® SDU é· */
|
---|
| 1892 | tcph->flags &= ~(TCP_FLG_PUSH | TCP_FLG_FIN);
|
---|
| 1893 | }
|
---|
| 1894 |
|
---|
| 1895 | /*
|
---|
| 1896 | * ãããSYN ãã»ããããã¦ããã°ã
|
---|
| 1897 | * ã¨ã©ã¼ãªã®ã§ RST ãéããæ¥ç¶ãç ´æ£ããã
|
---|
| 1898 | */
|
---|
| 1899 | if (tcph->flags & TCP_FLG_SYN) {
|
---|
| 1900 | cep->net_error = EV_CNRST;
|
---|
| 1901 | cep = tcp_drop(cep, E_CLS);
|
---|
| 1902 | goto reset_drop;
|
---|
| 1903 | }
|
---|
| 1904 |
|
---|
| 1905 | /*
|
---|
| 1906 | * ãããACK ãã»ããããã¦ããªãå ´åã¯ã
|
---|
| 1907 | * ç¶æ
|
---|
| 1908 | ã SYN åä¿¡æ¸ã¿ã
|
---|
| 1909 | * SYN ãéä¿¡ãããã¨ãã¦ããã°ãå¦çãç¶ãããã
|
---|
| 1910 | * ãã以å¤ã¯ã»ã°ã¡ã³ããç ´æ£ãã¦çµäºããã
|
---|
| 1911 | */
|
---|
| 1912 | if ((tcph->flags & TCP_FLG_ACK) == 0) {
|
---|
| 1913 | if (!(cep->fsm_state == TCP_FSM_SYN_RECVD || (cep->flags & TCP_CEP_FLG_NEED_SYN)))
|
---|
| 1914 | goto drop;
|
---|
| 1915 | }
|
---|
| 1916 | else {
|
---|
| 1917 | /*
|
---|
| 1918 | * ACK ã®å¦ç
|
---|
| 1919 | */
|
---|
| 1920 | ret = proc_ack1(input, cep, *offp, &needoutput);
|
---|
| 1921 | if (ret == RET_DROP)
|
---|
| 1922 | goto drop;
|
---|
| 1923 | else if (ret == RET_RST_DROP)
|
---|
| 1924 | goto reset_drop;
|
---|
| 1925 | else if (ret == RET_RETURN)
|
---|
| 1926 | return IPPROTO_DONE;
|
---|
| 1927 | }
|
---|
| 1928 | }
|
---|
| 1929 |
|
---|
| 1930 | /* step 6 */
|
---|
| 1931 |
|
---|
| 1932 | /* éä¿¡ã¦ã£ã³ããæ´æ°ããã*/
|
---|
| 1933 | if (update_wnd(tcph, cep) == true)
|
---|
| 1934 | needoutput = true;
|
---|
| 1935 |
|
---|
| 1936 | /* ç·æ¥ãã¼ã¿ãå¦çããã*/
|
---|
| 1937 | proc_urg(tcph, cep);
|
---|
| 1938 |
|
---|
| 1939 | /* do data */
|
---|
| 1940 |
|
---|
| 1941 | /*
|
---|
| 1942 | * SDU ãããããFIN ãæªåä¿¡ã®ç¶æ
|
---|
| 1943 | ã§ãæåã« FIN ãåä¿¡ããã¨ãã
|
---|
| 1944 | * åä¿¡ã»ã°ã¡ã³ããã¥ã¼ã« net_buf ã追å ããã
|
---|
| 1945 | * ãã以å¤ã®å ´åã¯ãã»ã°ã¡ã³ããç ´æ£ããã
|
---|
| 1946 | */
|
---|
| 1947 | flags = tcph->flags;
|
---|
| 1948 | if ((tcph->sum > 0 || (flags & TCP_FLG_FIN)) && /* tcph->sum 㯠TCP ã® SDU é· */
|
---|
| 1949 | TCP_FSM_HAVE_RCVD_FIN(cep->fsm_state) == 0) {
|
---|
| 1950 | flags = reassemble(input, cep, *offp, flags);
|
---|
| 1951 | }
|
---|
| 1952 | else {
|
---|
| 1953 | syscall(rel_net_buf(input));
|
---|
| 1954 | flags &= ~TCP_FLG_FIN;
|
---|
| 1955 | }
|
---|
| 1956 |
|
---|
| 1957 | /*
|
---|
| 1958 | * FIN ãåä¿¡ãããã³ãã¯ã·ã§ã³ãã¯ãã¼ãºããã
|
---|
| 1959 | */
|
---|
| 1960 | if (flags & TCP_FLG_FIN)
|
---|
| 1961 | close_connection(cep, &needoutput);
|
---|
| 1962 |
|
---|
| 1963 | /* åºåãè¡ã£ãå¾çµäºããã*/
|
---|
| 1964 | if (needoutput == true || (cep->flags & TCP_CEP_FLG_ACK_NOW)) {
|
---|
| 1965 | /* éä¿¡ãæ示ããã*/
|
---|
| 1966 | cep->flags |= TCP_CEP_FLG_POST_OUTPUT;
|
---|
| 1967 | sig_sem(SEM_TCP_POST_OUTPUT);
|
---|
| 1968 | }
|
---|
| 1969 |
|
---|
| 1970 | return IPPROTO_DONE;
|
---|
| 1971 |
|
---|
| 1972 | reset_drop:
|
---|
| 1973 | /*
|
---|
| 1974 | * RST éä¿¡å¦ç
|
---|
| 1975 | */
|
---|
| 1976 |
|
---|
| 1977 | if ((tcph->flags & TCP_FLG_RST) || IN_IS_NET_ADDR_MULTICAST(&iph->dst))
|
---|
| 1978 | goto drop;
|
---|
| 1979 |
|
---|
| 1980 | /* ãã¹ããªã¼ãã¼ãããããã¯ã¼ã¯ãªã¼ãã¼ã«æ»ãã*/
|
---|
| 1981 |
|
---|
| 1982 | HTONS(tcph->sport);
|
---|
| 1983 | HTONS(tcph->dport);
|
---|
| 1984 |
|
---|
| 1985 | if (cep == NULL)
|
---|
| 1986 | rbfree = 0;
|
---|
| 1987 | else
|
---|
| 1988 | rbfree = cep->rbufsz - cep->rwbuf_count;
|
---|
| 1989 |
|
---|
| 1990 | if (tcph->flags & TCP_FLG_ACK) {
|
---|
| 1991 | tcp_respond(input, cep, 0, tcph->ack, rbfree, TCP_FLG_RST);
|
---|
| 1992 | }
|
---|
| 1993 | else {
|
---|
| 1994 | if (tcph->flags & TCP_FLG_SYN)
|
---|
| 1995 | tcph->sum ++; /* tcph->sum 㯠SDU é· */
|
---|
| 1996 | tcp_respond(input, cep, tcph->seq + tcph->sum, 0, rbfree, TCP_FLG_RST | TCP_FLG_ACK);
|
---|
| 1997 | }
|
---|
| 1998 |
|
---|
| 1999 | /* input 㯠tcp_respoond ã§è¿å´ãããã*/
|
---|
| 2000 | NET_COUNT_TCP(net_count_tcp[NC_TCP_SEND_RSTS], 1);
|
---|
| 2001 | NET_COUNT_MIB(tcp_stats.tcpOutRsts, 1);
|
---|
| 2002 | return IPPROTO_DONE;
|
---|
| 2003 |
|
---|
| 2004 | drop:
|
---|
| 2005 | NET_COUNT_TCP(net_count_tcp[NC_TCP_RECV_DROP_SEGS], 1);
|
---|
| 2006 | NET_COUNT_MIB(tcp_stats.tcpInErrs, 1);
|
---|
| 2007 | syscall(rel_net_buf(input));
|
---|
| 2008 | return IPPROTO_DONE;
|
---|
| 2009 | }
|
---|
| 2010 |
|
---|
| 2011 | #endif /* of #ifdef SUPPORT_TCP */
|
---|