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