1 /* $KAME: dccp_usrreq.c,v 1.67 2005/11/03 16:05:04 nishida Exp $ */ 2 /* $NetBSD: dccp_usrreq.c,v 1.16 2017/05/07 21:36:23 rjs Exp $ */ 3 4 /* 5 * Copyright (c) 2003 Joacim H�ggmark, Magnus Erixzon, Nils-Erik Mattsson 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * Id: dccp_usrreq.c,v 1.47 2003/07/31 11:23:08 joahag-9 Exp 32 */ 33 34 /* 35 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. All advertising materials mentioning features or use of this software 47 * must display the following acknowledgement: 48 * This product includes software developed by the University of 49 * California, Berkeley and its contributors. 50 * 4. Neither the name of the University nor the names of its contributors 51 * may be used to endorse or promote products derived from this software 52 * without specific prior written permission. 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 64 * SUCH DAMAGE. 65 * 66 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 67 */ 68 69 #include <sys/cdefs.h> 70 __KERNEL_RCSID(0, "$NetBSD: dccp_usrreq.c,v 1.16 2017/05/07 21:36:23 rjs Exp $"); 71 72 #ifdef _KERNEL_OPT 73 #include "opt_inet.h" 74 #include "opt_dccp.h" 75 #endif 76 77 #include <sys/param.h> 78 #include <sys/systm.h> 79 #include <sys/domain.h> 80 #include <sys/kernel.h> 81 #include <sys/pool.h> 82 #include <sys/lock.h> 83 #include <sys/malloc.h> 84 #include <sys/mbuf.h> 85 #include <sys/proc.h> 86 #include <sys/protosw.h> 87 #include <sys/signalvar.h> 88 #include <sys/socket.h> 89 #include <sys/socketvar.h> 90 #include <sys/mutex.h> 91 #include <sys/sysctl.h> 92 #include <sys/syslog.h> 93 #include <sys/queue.h> 94 95 #include <net/if.h> 96 97 #include <netinet/in.h> 98 #include <netinet/in_systm.h> 99 #include <netinet/ip.h> 100 #include <netinet/in_pcb.h> 101 #include <netinet/in_var.h> 102 #ifdef INET6 103 #include <netinet/ip6.h> 104 #endif 105 #include <netinet/ip_icmp.h> 106 #include <netinet/icmp_var.h> 107 #include <netinet/ip_var.h> 108 #ifdef INET6 109 #include <netinet6/in6_pcb.h> 110 #include <netinet6/ip6_var.h> 111 #include <netinet6/dccp6_var.h> 112 #endif 113 #include <netinet/dccp.h> 114 #include <netinet/dccp_var.h> 115 #include <netinet/dccp_cc_sw.h> 116 117 #define DEFAULT_CCID 2 118 119 #define INP_INFO_LOCK_INIT(x,y) 120 #define INP_INFO_WLOCK(x) 121 #define INP_INFO_WUNLOCK(x) 122 #define INP_INFO_RLOCK(x) 123 #define INP_INFO_RUNLOCK(x) 124 #define INP_LOCK(x) 125 #define IN6P_LOCK(x) 126 #define INP_UNLOCK(x) 127 #define IN6P_UNLOCK(x) 128 129 /* Congestion control switch table */ 130 extern struct dccp_cc_sw cc_sw[]; 131 132 int dccp_log_in_vain = 1; 133 int dccp_do_feature_nego = 1; 134 135 struct inpcbhead dccpb; /* from dccp_var.h */ 136 #ifdef __FreeBSD__ 137 struct inpcbinfo dccpbinfo; 138 #else 139 struct inpcbtable dccpbtable; 140 #endif 141 142 #ifndef DCCPBHASHSIZE 143 #define DCCPBHASHSIZE 16 144 #endif 145 146 struct pool dccpcb_pool; 147 148 u_long dccp_sendspace = 32768; 149 u_long dccp_recvspace = 65536; 150 151 struct dccpstat dccpstat; /* from dccp_var.h */ 152 153 static struct dccpcb * dccp_close(struct dccpcb *); 154 static int dccp_disconnect2(struct dccpcb *); 155 int dccp_get_option(char *, int, int, char *,int); 156 void dccp_parse_options(struct dccpcb *, char *, int); 157 int dccp_remove_feature(struct dccpcb *, u_int8_t, u_int8_t); 158 int dccp_add_feature_option(struct dccpcb *, u_int8_t, u_int8_t, char *, u_int8_t); 159 void dccp_feature_neg(struct dccpcb *, u_int8_t, u_int8_t, u_int8_t, char *); 160 void dccp_close_t(void *); 161 void dccp_timewait_t(void *); 162 163 /* Ack Vector functions */ 164 #define DCCP_VECTORSIZE 512 /* initial ack and cwnd-vector size. Multiple of 8 ! */ 165 void dccp_use_ackvector(struct dccpcb *); 166 void dccp_update_ackvector(struct dccpcb *, u_int64_t); 167 void dccp_increment_ackvector(struct dccpcb *, u_int64_t); 168 u_int16_t dccp_generate_ackvector(struct dccpcb *, u_char *); 169 u_char dccp_ackvector_state(struct dccpcb *, u_int64_t); 170 171 /* 172 * DCCP initialization 173 */ 174 void 175 dccp_init(void) 176 { 177 pool_init(&dccpcb_pool, sizeof(struct dccpcb), 0, 0, 0, "dccpcbpl", 178 NULL, IPL_SOFTNET); 179 180 in_pcbinit(&dccpbtable, DCCPBHASHSIZE, DCCPBHASHSIZE); 181 } 182 183 void 184 dccp_input(struct mbuf *m, ...) 185 { 186 int iphlen; 187 struct ip *ip = NULL; 188 struct dccphdr *dh; 189 struct dccplhdr *dlh; 190 struct inpcb *inp = NULL, *oinp = NULL; 191 struct in6pcb *in6p = NULL, *oin6p = NULL; 192 struct dccpcb *dp; 193 struct ipovly *ipov = NULL; 194 struct dccp_requesthdr *drqh; 195 struct dccp_ackhdr *dah = NULL; 196 struct dccp_acklhdr *dalh = NULL; 197 struct dccp_resethdr *drth; 198 struct socket *so; 199 u_char *optp = NULL; 200 struct mbuf *opts = 0; 201 int len, data_off, extrah_len, optlen; 202 /*struct ip save_ip;*/ 203 char options[DCCP_MAX_OPTIONS]; 204 char test[2]; 205 u_int32_t cslen; 206 dccp_seq seqnr, low_seqnr, high_seqnr; 207 int isipv6 = 0; 208 int is_shortseq; /* Is this shortseq packet? */ 209 #ifdef INET6 210 struct ip6_hdr *ip6 = NULL; 211 #endif 212 213 int off; 214 va_list ap; 215 216 va_start(ap, m); 217 iphlen = off = va_arg(ap, int); 218 va_end(ap); 219 220 DCCP_DEBUG((LOG_INFO, "Got DCCP packet!\n")); 221 222 dccpstat.dccps_ipackets++; 223 dccpstat.dccps_ibytes += m->m_pkthdr.len; 224 225 #ifdef INET6 226 isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; 227 #endif 228 229 #ifdef INET6 230 if (isipv6) { 231 DCCP_DEBUG((LOG_INFO, "Got DCCP ipv6 packet, iphlen = %u!\n", iphlen)); 232 ip6 = mtod(m, struct ip6_hdr *); 233 IP6_EXTHDR_GET(dh, struct dccphdr *, m, iphlen, sizeof(*dh)); 234 if (dh == NULL) { 235 dccpstat.dccps_badlen++; 236 return; 237 } 238 } else 239 #endif 240 { 241 /* 242 * Strip IP options, if any; should skip this, 243 * make available to user, and use on returned packets, 244 * but we don't yet have a way to check the checksum 245 * with options still present. 246 */ 247 if (iphlen > sizeof (struct ip)) { 248 DCCP_DEBUG((LOG_INFO, "Need to strip options\n")); 249 #if 0 /* XXX */ 250 ip_stripoptions(m, (struct mbuf *)0); 251 #endif 252 iphlen = sizeof(struct ip); 253 } 254 255 /* 256 * Get IP and DCCP header together in first mbuf. 257 */ 258 ip = mtod(m, struct ip *); 259 IP6_EXTHDR_GET(dh, struct dccphdr *, m, iphlen, sizeof(*dh)); 260 if (dh == NULL) { 261 dccpstat.dccps_badlen++; 262 return; 263 } 264 } 265 dlh = (struct dccplhdr*)dh; 266 is_shortseq = !dh->dh_x; 267 268 if (!is_shortseq) { 269 DCCP_DEBUG((LOG_INFO, 270 "Header info: cslen = %u, off = %u, type = %u, reserved = %u, seq = %u.%lu\n", 271 dlh->dh_cscov, dlh->dh_off, dlh->dh_type, dlh->dh_res, ntohs(dlh->dh_seq), 272 (unsigned long)ntohl(dlh->dh_seq2))); 273 } else { 274 DCCP_DEBUG((LOG_INFO, 275 "Header info(short): cslen = %u, off = %u, type = %u, reserved = %u, seq = %u\n", 276 dh->dh_cscov, dh->dh_off, dh->dh_type, dh->dh_res, ntohl(dh->dh_seq))); 277 } 278 279 /* 280 * Make mbuf data length reflect DCCP length. 281 * If not enough data to reflect DCCP length, drop. 282 */ 283 284 #ifdef INET6 285 if (isipv6) 286 len = m->m_pkthdr.len - off; 287 else 288 #endif 289 { 290 len = ntohs(ip->ip_len); 291 len -= ip->ip_hl << 2; 292 } 293 294 if (len < sizeof(struct dccphdr)) { 295 DCCP_DEBUG((LOG_INFO, "Dropping DCCP packet!\n")); 296 dccpstat.dccps_badlen++; 297 goto badunlocked; 298 } 299 /* 300 * Save a copy of the IP header in case we want restore it 301 * for sending a DCCP reset packet in response. 302 */ 303 if (!isipv6) { 304 /*save_ip = *ip;*/ 305 ipov = (struct ipovly *)ip; 306 } 307 308 if (dh->dh_cscov == 0) { 309 cslen = len; 310 } else { 311 cslen = dh->dh_off * 4 + (dh->dh_cscov - 1) * 4; 312 if (cslen > len) 313 cslen = len; 314 } 315 316 /* 317 * Checksum extended DCCP header and data. 318 */ 319 320 #ifdef INET6 321 if (isipv6) { 322 if (in6_cksum(m, IPPROTO_DCCP, off, cslen) != 0) { 323 dccpstat.dccps_badsum++; 324 goto badunlocked; 325 } 326 } else 327 #endif 328 { 329 memset(ipov->ih_x1, 0, sizeof(ipov->ih_x1)); 330 ip->ip_len = htons(m->m_pkthdr.len); 331 dh->dh_sum = in4_cksum(m, IPPROTO_DCCP, off, cslen); 332 333 if (dh->dh_sum) { 334 dccpstat.dccps_badsum++; 335 goto badunlocked; 336 } 337 } 338 339 INP_INFO_WLOCK(&dccpbinfo); 340 341 /* 342 * Locate pcb for datagram. 343 */ 344 #ifdef INET6 345 if (isipv6) { 346 in6p = in6_pcblookup_connect(&dccpbtable, &ip6->ip6_src, 347 dh->dh_sport, &ip6->ip6_dst, dh->dh_dport, 0, 0); 348 if (in6p == 0) { 349 /* XXX stats increment? */ 350 in6p = in6_pcblookup_bind(&dccpbtable, &ip6->ip6_dst, 351 dh->dh_dport, 0); 352 } 353 } else 354 #endif 355 { 356 inp = in_pcblookup_connect(&dccpbtable, ip->ip_src, 357 dh->dh_sport, ip->ip_dst, dh->dh_dport, 0); 358 if (inp == NULL) { 359 /* XXX stats increment? */ 360 inp = in_pcblookup_bind(&dccpbtable, ip->ip_dst, 361 dh->dh_dport); 362 } 363 } 364 if (isipv6) { 365 DCCP_DEBUG((LOG_INFO, "in6p=%p\n", in6p)); 366 } else { 367 DCCP_DEBUG((LOG_INFO, "inp=%p\n", inp)); 368 } 369 370 if (isipv6 ? in6p == NULL : inp == NULL) { 371 if (dccp_log_in_vain) { 372 #ifdef INET6 373 char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2]; 374 #else 375 char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"]; 376 #endif 377 378 #ifdef INET6 379 if (isipv6) { 380 char ip6buf[INET6_ADDRSTRLEN]; 381 strlcpy(dbuf, "[", sizeof dbuf); 382 strlcat(dbuf, IN6_PRINT(ip6buf, &ip6->ip6_dst), sizeof dbuf); 383 strlcat(dbuf, "]", sizeof dbuf); 384 strlcpy(sbuf, "[", sizeof sbuf); 385 strlcat(sbuf, IN6_PRINT(ip6buf, &ip6->ip6_src), sizeof sbuf); 386 strlcat(sbuf, "]", sizeof sbuf); 387 } else 388 #endif 389 { 390 strlcpy(dbuf, inet_ntoa(ip->ip_dst), sizeof dbuf); 391 strlcpy(sbuf, inet_ntoa(ip->ip_src), sizeof sbuf); 392 } 393 log(LOG_INFO, 394 "Connection attempt to DCCP %s:%d from %s:%d\n", 395 dbuf, ntohs(dh->dh_dport), sbuf, 396 ntohs(dh->dh_sport)); 397 } 398 dccpstat.dccps_noport++; 399 400 /* 401 * We should send DCCP reset here but we can't call dccp_output 402 * since we have no dccpcb. A icmp unreachable works great but 403 * the specs says DCCP reset :( 404 * 405 * if (!isipv6) { 406 * *ip = save_ip; 407 * ip->ip_len += iphlen; 408 * icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 409 * } 410 */ 411 412 INP_INFO_WUNLOCK(&dccpbinfo); 413 goto badunlocked; 414 } 415 INP_LOCK(inp); 416 417 #ifdef INET6 418 if (isipv6) 419 dp = in6todccpcb(in6p); 420 else 421 #endif 422 dp = intodccpcb(inp); 423 424 if (dp == 0) { 425 INP_UNLOCK(inp); 426 INP_INFO_WUNLOCK(&dccpbinfo); 427 goto badunlocked; 428 } 429 430 if (dp->state == DCCPS_CLOSED) { 431 DCCP_DEBUG((LOG_INFO, "We are in closed state, dropping packet and sending reset!\n")); 432 if (dh->dh_type != DCCP_TYPE_RESET) 433 dccp_output(dp, DCCP_TYPE_RESET + 2); 434 INP_UNLOCK(inp); 435 INP_INFO_WUNLOCK(&dccpbinfo); 436 goto badunlocked; 437 } 438 439 #if defined(INET6) 440 if (isipv6) 441 so = in6p->in6p_socket; 442 else 443 #endif 444 so = inp->inp_socket; 445 446 if (so->so_options & SO_ACCEPTCONN) { 447 DCCP_DEBUG((LOG_INFO, "so->options & SO_ACCEPTCONN! dp->state = %i\n", dp->state)); 448 so = sonewconn(so, SS_ISCONNECTED); 449 if (so == 0) { 450 DCCP_DEBUG((LOG_INFO, "Error, sonewconn failed!\n")); 451 INP_UNLOCK(inp); 452 INP_INFO_WUNLOCK(&dccpbinfo); 453 goto badunlocked; 454 } 455 456 /* INP_LOCK(inp); XXX */ 457 458 #if defined(INET6) 459 if (isipv6) 460 oin6p = in6p; 461 else 462 #endif 463 oinp = inp; 464 465 #ifdef INET6 466 if (isipv6) { 467 in6p = sotoin6pcb(so); 468 in6p->in6p_laddr = ip6->ip6_dst; 469 in6p->in6p_faddr = ip6->ip6_src; 470 in6p->in6p_lport = dh->dh_dport; 471 in6p->in6p_fport = dh->dh_sport; 472 in6_pcbstate(in6p, IN6P_CONNECTED); 473 } else 474 #endif 475 { 476 inp = sotoinpcb(so); 477 inp->inp_laddr = ip->ip_dst; 478 inp->inp_faddr = ip->ip_src; 479 inp->inp_lport = dh->dh_dport; 480 inp->inp_fport = dh->dh_sport; 481 } 482 483 if (!isipv6) 484 in_pcbstate(inp, INP_BOUND); 485 486 #if defined(INET6) 487 if (isipv6) 488 dp = (struct dccpcb *)in6p->in6p_ppcb; 489 else 490 #endif 491 dp = (struct dccpcb *)inp->inp_ppcb; 492 493 dp->state = DCCPS_LISTEN; 494 dp->who = DCCP_SERVER; 495 #if defined(INET6) 496 if (isipv6) { 497 dp->cslen = ((struct dccpcb *)oin6p->in6p_ppcb)->cslen; 498 dp->avgpsize = ((struct dccpcb *)oin6p->in6p_ppcb)->avgpsize; 499 dp->scode = ((struct dccpcb *)oin6p->in6p_ppcb)->scode; 500 } else 501 #endif 502 { 503 dp->cslen = ((struct dccpcb *)oinp->inp_ppcb)->cslen; 504 dp->avgpsize = ((struct dccpcb *)oinp->inp_ppcb)->avgpsize; 505 dp->scode = ((struct dccpcb *)oinp->inp_ppcb)->scode; 506 } 507 dp->seq_snd = (((u_int64_t)random() << 32) | random()) % 281474976710656LL; 508 dp->ref_seq.hi = dp->seq_snd >> 24; 509 dp->ref_seq.lo = (u_int64_t)(dp->seq_snd & 0xffffff); 510 INP_UNLOCK(oinp); 511 DCCP_DEBUG((LOG_INFO, "New dp = %u, dp->state = %u!\n", (int)dp, dp->state)); 512 } 513 514 INP_INFO_WUNLOCK(&dccpbinfo); 515 516 /* 517 * Check if sequence number is inside the loss window 518 */ 519 if (!is_shortseq) { 520 DHDR_TO_DSEQ(seqnr, dlh); 521 } else { 522 /* shortseq */ 523 seqnr = CONVERT_TO_LONGSEQ((ntohl(dh->dh_seq) >> 8), dp->ref_pseq); 524 DCCP_DEBUG((LOG_INFO, "short seq conversion %x, %u %u\n", 525 ntohl(dh->dh_seq) >> 8, dp->ref_pseq.hi, dp->ref_pseq.lo)); 526 } 527 528 DCCP_DEBUG((LOG_INFO, "Received DCCP packet with sequence number = %llu , gsn_rcv %llu\n", seqnr, dp->gsn_rcv)); 529 530 /* store ccval */ 531 dp->ccval = dh->dh_ccval; 532 533 if (dp->gsn_rcv == 281474976710656LL) dp->gsn_rcv = seqnr; 534 if (dp->gsn_rcv > (dp->loss_window / 4)) 535 low_seqnr = (dp->gsn_rcv - (dp->loss_window / 4)) % 281474976710656LL; 536 else 537 low_seqnr = 0ll; 538 high_seqnr = (dp->gsn_rcv + (dp->loss_window / 4 * 3)) % 281474976710656LL; 539 540 if (! (DCCP_SEQ_GT(seqnr, low_seqnr) && DCCP_SEQ_LT(seqnr, high_seqnr))) { 541 dccpstat.dccps_badseq++; 542 DCCP_DEBUG((LOG_INFO, "Recieved DCCP packet with bad sequence number = %llu (low_seqnr = %llu, high_seqnr = %llu)\n", seqnr, low_seqnr, high_seqnr)); 543 INP_UNLOCK(inp); 544 goto badunlocked; 545 } 546 547 /* dp->gsn_rcv should always be the highest received valid sequence number */ 548 if (DCCP_SEQ_GT(seqnr, dp->gsn_rcv)) 549 dp->gsn_rcv = seqnr; 550 551 /* Just ignore DCCP-Move for now */ 552 if (dlh->dh_type == DCCP_TYPE_DATA) { 553 extrah_len = 0; 554 if (!is_shortseq) 555 optp = (u_char *)(dlh + 1); 556 else 557 optp = (u_char *)(dh + 1); 558 } else if (dh->dh_type == DCCP_TYPE_REQUEST) { 559 drqh = (struct dccp_requesthdr *)(dlh + 1); 560 if (drqh->drqh_scode != dp->scode){ 561 DCCP_DEBUG((LOG_INFO, "service code in request packet doesn't match! %x %x\n", drqh->drqh_scode, dp->scode)); 562 INP_UNLOCK(inp); 563 dp->state = DCCPS_SERVER_CLOSE; /* So disconnect2 doesn't send CLOSEREQ */ 564 dccp_disconnect2(dp); 565 dccp_output(dp, DCCP_TYPE_RESET + 2); 566 dccp_close(dp); 567 goto badunlocked; 568 } 569 optp = (u_char *)(drqh + 1); 570 extrah_len = 4; 571 572 /* store reference peer sequence number */ 573 dp->ref_pseq.hi = seqnr >> 24; 574 dp->ref_pseq.lo = (u_int64_t)(seqnr & 0xffffff); 575 576 } else if (dh->dh_type == DCCP_TYPE_RESET) { 577 extrah_len = 8 ; 578 drth = (struct dccp_resethdr *)(dlh + 1); 579 optp = (u_char *)(drth + 1); 580 } else { 581 if (!is_shortseq){ 582 extrah_len = 8; 583 dalh = (struct dccp_acklhdr *)(dlh + 1); 584 if (dh->dh_type == DCCP_TYPE_RESPONSE) { 585 extrah_len += 4; 586 drqh = (struct dccp_requesthdr *)(dalh + 1); 587 if (drqh->drqh_scode != dp->scode){ 588 DCCP_DEBUG((LOG_INFO, "service code in response packet doesn't match! %x %x\n", drqh->drqh_scode, dp->scode)); 589 INP_UNLOCK(inp); 590 dp->state = DCCPS_CLIENT_CLOSE; /* So disconnect2 doesn't send CLOSEREQ */ 591 dccp_disconnect2(dp); 592 dccp_output(dp, DCCP_TYPE_RESET + 2); 593 dccp_close(dp); 594 goto badunlocked; 595 } 596 optp = (u_char *)(drqh + 1); 597 598 /* store reference peer sequence number */ 599 dp->ref_pseq.hi = seqnr >> 24; 600 dp->ref_pseq.lo = (u_int64_t)(seqnr & 0xffffff); 601 } else 602 optp = (u_char *)(dalh + 1); 603 } else { 604 extrah_len = 4; 605 dah = (struct dccp_ackhdr *)(dh + 1); 606 optp = (u_char *)(dah + 1); 607 } 608 609 } 610 611 data_off = (dh->dh_off << 2); 612 613 dp->seq_rcv = seqnr; 614 dp->ack_rcv = 0; /* Clear it for now */ 615 dp->type_rcv = dh->dh_type; 616 dp->len_rcv = m->m_len - data_off - iphlen; /* Correct length ? */ 617 618 if (!is_shortseq) 619 optlen = data_off - (sizeof(struct dccplhdr) + extrah_len); 620 else 621 optlen = data_off - (sizeof(struct dccphdr) + extrah_len); 622 623 if (optlen < 0) { 624 DCCP_DEBUG((LOG_INFO, "Data offset is smaller then it could be, optlen = %i data_off = %i, m_len = %i, iphlen = %i extrah_len = %i !\n", optlen, data_off, m->m_len, iphlen, extrah_len)); 625 INP_UNLOCK(inp); 626 goto badunlocked; 627 } 628 629 if (optlen > 0) { 630 if (optlen > DCCP_MAX_OPTIONS) { 631 DCCP_DEBUG((LOG_INFO, "Error, more options (%i) then DCCP_MAX_OPTIONS options!\n", optlen)); 632 INP_UNLOCK(inp); 633 goto badunlocked; 634 } 635 636 DCCP_DEBUG((LOG_INFO, "Parsing DCCP options, optlen = %i\n", optlen)); 637 memcpy(options, optp, optlen); 638 dccp_parse_options(dp, options, optlen); 639 } 640 641 DCCP_DEBUG((LOG_INFO, "BEFORE state check, Got a %u packet while in %u state, who = %u!\n", dh->dh_type, dp->state, dp->who)); 642 643 if (dp->state == DCCPS_LISTEN) { 644 switch (dh->dh_type) { 645 646 case DCCP_TYPE_REQUEST: 647 DCCP_DEBUG((LOG_INFO, "Got DCCP REQUEST\n")); 648 dp->state = DCCPS_REQUEST; 649 if (dp->cc_in_use[1] < 0) { 650 test[0] = DEFAULT_CCID; 651 test[1] = 3; 652 dccp_add_feature(dp, DCCP_OPT_CHANGE_R, DCCP_FEATURE_CC, test, 2); 653 } 654 if (len > data_off) { 655 DCCP_DEBUG((LOG_INFO, "XXX: len=%d, data_off=%d\n", len, data_off)); 656 dccp_add_option(dp, DCCP_OPT_DATA_DISCARD, test, 0); 657 } 658 callout_reset(&dp->connect_timer, DCCP_CONNECT_TIMER, 659 dccp_connect_t, dp); 660 dccp_output(dp, 0); 661 break; 662 663 664 /* These are ok if the sender has a valid init Cookie */ 665 case DCCP_TYPE_ACK: 666 case DCCP_TYPE_DATAACK: 667 case DCCP_TYPE_DATA: 668 DCCP_DEBUG((LOG_INFO, "Got DCCP ACK/DATAACK/DATA, should check init cookie...\n")); 669 dccp_output(dp, DCCP_TYPE_RESET + 2); 670 break; 671 672 case DCCP_TYPE_RESET: 673 DCCP_DEBUG((LOG_INFO, "Got DCCP RESET\n")); 674 dp->state = DCCPS_TIME_WAIT; 675 dp = dccp_close(dp); 676 return; 677 678 default: 679 DCCP_DEBUG((LOG_INFO, "Got a %u packet while in listen stage!\n", dh->dh_type)); 680 /* Force send reset. */ 681 dccp_output(dp, DCCP_TYPE_RESET + 2); 682 } 683 } else if (dp->state == DCCPS_REQUEST) { 684 switch (dh->dh_type) { 685 case DCCP_TYPE_RESPONSE: 686 DAHDR_TO_DSEQ(dp->ack_rcv, ((struct dccp_acklhdr*)dalh)->dash); 687 dp->ack_snd = dp->seq_rcv; 688 DCCP_DEBUG((LOG_INFO, "Got DCCP REPSONSE %x %llx\n", dp, dp->ack_snd)); 689 690 callout_stop(&dp->retrans_timer); 691 callout_stop(&dp->connect_timer); 692 693 /* First check if we have negotiated a cc */ 694 if (dp->cc_in_use[0] > 0 && dp->cc_in_use[1] > 0) { 695 DCCP_DEBUG((LOG_INFO, "Setting DCCPS_ESTAB & soisconnected\n")); 696 dp->state = DCCPS_ESTAB; 697 dccpstat.dccps_connects++; 698 #if defined(INET6) 699 if (isipv6) 700 soisconnected(in6p->in6p_socket); 701 else 702 #endif 703 soisconnected(inp->inp_socket); 704 } else { 705 dp->state = DCCPS_RESPOND; 706 DCCP_DEBUG((LOG_INFO, "CC negotiation is not finished, cc_in_use[0] = %u, cc_in_use[1] = %u\n",dp->cc_in_use[0], dp->cc_in_use[1])); 707 708 } 709 dccp_output(dp, 0); 710 break; 711 712 case DCCP_TYPE_RESET: 713 DCCP_DEBUG((LOG_INFO, "Got DCCP RESET\n")); 714 dp->state = DCCPS_TIME_WAIT; 715 dp = dccp_close(dp); 716 return; 717 718 default: 719 DCCP_DEBUG((LOG_INFO, "Got a %u packet while in REQUEST stage!\n", dh->dh_type)); 720 /* Force send reset. */ 721 dccp_output(dp, DCCP_TYPE_RESET + 2); 722 if (dh->dh_type == DCCP_TYPE_CLOSE) { 723 dp = dccp_close(dp); 724 return; 725 } else { 726 callout_stop(&dp->retrans_timer); 727 dp->state = DCCPS_TIME_WAIT; 728 } 729 } 730 } else if (dp->state == DCCPS_RESPOND) { 731 switch (dh->dh_type) { 732 733 case DCCP_TYPE_REQUEST: 734 break; 735 case DCCP_TYPE_ACK: 736 case DCCP_TYPE_DATAACK: 737 DCCP_DEBUG((LOG_INFO, "Got DCCP ACK/DATAACK\n")); 738 739 callout_stop(&dp->connect_timer); 740 741 if (!is_shortseq) { 742 DAHDR_TO_DSEQ(dp->ack_rcv, ((struct dccp_acklhdr*)dalh)->dash); 743 } else { 744 /* shortseq XXX */ 745 dp->ack_rcv = CONVERT_TO_LONGSEQ((ntohl(dah->dash.dah_ack) >> 8), dp->ref_seq); 746 } 747 748 if (dp->cc_in_use[0] > 0 && dp->cc_in_use[1] > 0) { 749 DCCP_DEBUG((LOG_INFO, "Setting DCCPS_ESTAB & soisconnected\n")); 750 dp->state = DCCPS_ESTAB; 751 dccpstat.dccps_connects++; 752 #if defined(INET6) 753 if (isipv6) 754 soisconnected(in6p->in6p_socket); 755 else 756 #endif 757 soisconnected(inp->inp_socket); 758 } else { 759 DCCP_DEBUG((LOG_INFO, "CC negotiation is not finished, cc_in_use[0] = %u, cc_in_use[1] = %u\n",dp->cc_in_use[0], dp->cc_in_use[1])); 760 /* Force an output!!! */ 761 dp->ack_snd = dp->seq_rcv; 762 dccp_output(dp, 0); 763 } 764 765 if (dh->dh_type == DCCP_TYPE_DATAACK && dp->cc_in_use[1] > 0) { 766 if (!dp->ack_snd) dp->ack_snd = dp->seq_rcv; 767 DCCP_DEBUG((LOG_INFO, "Calling *cc_sw[%u].cc_recv_packet_recv!\n", dp->cc_in_use[1])); 768 (*cc_sw[dp->cc_in_use[1]].cc_recv_packet_recv)(dp->cc_state[1], options, optlen); 769 } 770 break; 771 772 case DCCP_TYPE_CLOSE: 773 dccp_output(dp, DCCP_TYPE_CLOSE + 1); 774 dp = dccp_close(dp); 775 goto badunlocked; 776 777 case DCCP_TYPE_RESET: 778 dp->state = DCCPS_TIME_WAIT; 779 callout_stop(&dp->retrans_timer); 780 break; 781 782 default: 783 DCCP_DEBUG((LOG_INFO, "Got a %u packet while in response stage!\n", dh->dh_type)); 784 /* Force send reset. */ 785 dccp_output(dp, DCCP_TYPE_RESET + 2); 786 } 787 } else if (dp->state == DCCPS_ESTAB) { 788 switch (dh->dh_type) { 789 790 case DCCP_TYPE_DATA: 791 DCCP_DEBUG((LOG_INFO, "Got DCCP DATA, state = %i, cc_in_use[1] = %u\n", dp->state, dp->cc_in_use[1])); 792 793 if (dp->cc_in_use[1] > 0) { 794 if (!dp->ack_snd) dp->ack_snd = dp->seq_rcv; 795 DCCP_DEBUG((LOG_INFO, "Calling data *cc_sw[%u].cc_recv_packet_recv! %llx %llx dp=%x\n", dp->cc_in_use[1], dp->ack_snd, dp->seq_rcv, dp)); 796 (*cc_sw[dp->cc_in_use[1]].cc_recv_packet_recv)(dp->cc_state[1], options, optlen); 797 } 798 break; 799 800 case DCCP_TYPE_ACK: 801 DCCP_DEBUG((LOG_INFO, "Got DCCP ACK\n")); 802 if (!is_shortseq) { 803 DAHDR_TO_DSEQ(dp->ack_rcv, ((struct dccp_acklhdr*)dalh)->dash); 804 } else { 805 /* shortseq */ 806 dp->ack_rcv = CONVERT_TO_LONGSEQ((ntohl(dah->dash.dah_ack) >> 8), dp->ref_seq); 807 } 808 809 if (dp->cc_in_use[1] > 0) { 810 /* This is called so Acks on Acks can be handled */ 811 if (!dp->ack_snd) dp->ack_snd = dp->seq_rcv; 812 DCCP_DEBUG((LOG_INFO, "Calling ACK *cc_sw[%u].cc_recv_packet_recv! %llx %llx\n", dp->cc_in_use[1], dp->ack_snd, dp->seq_rcv)); 813 (*cc_sw[dp->cc_in_use[1]].cc_recv_packet_recv)(dp->cc_state[1], options, optlen); 814 } 815 break; 816 817 case DCCP_TYPE_DATAACK: 818 DCCP_DEBUG((LOG_INFO, "Got DCCP DATAACK\n")); 819 820 if (!is_shortseq) { 821 DAHDR_TO_DSEQ(dp->ack_rcv, ((struct dccp_acklhdr*)dalh)->dash); 822 } else { 823 /* shortseq */ 824 dp->ack_rcv = CONVERT_TO_LONGSEQ((ntohl(dah->dash.dah_ack) >> 8), dp->ref_seq); 825 } 826 827 if (dp->cc_in_use[1] > 0) { 828 if (!dp->ack_snd) dp->ack_snd = dp->seq_rcv; 829 DCCP_DEBUG((LOG_INFO, "Calling *cc_sw[%u].cc_recv_packet_recv! %llx %llx\n", dp->cc_in_use[1], dp->ack_snd, dp->seq_rcv)); 830 (*cc_sw[dp->cc_in_use[1]].cc_recv_packet_recv)(dp->cc_state[1], options, optlen); 831 } 832 break; 833 834 case DCCP_TYPE_CLOSEREQ: 835 DCCP_DEBUG((LOG_INFO, "Got DCCP CLOSEREQ, state = estab\n")); 836 if (dp->who == DCCP_CLIENT) { 837 dccp_disconnect2(dp); 838 } else { 839 dccp_output(dp, DCCP_TYPE_RESET + 2); 840 } 841 break; 842 843 case DCCP_TYPE_CLOSE: 844 DCCP_DEBUG((LOG_INFO, "Got DCCP CLOSE, state = estab\n")); 845 dp->state = DCCPS_SERVER_CLOSE; /* So disconnect2 doesn't send CLOSEREQ */ 846 dccp_disconnect2(dp); 847 dccp_output(dp, DCCP_TYPE_RESET + 2); 848 dccp_close(dp); 849 goto badunlocked; 850 break; 851 852 case DCCP_TYPE_RESET: 853 DCCP_DEBUG((LOG_INFO, "Got DCCP RESET\n")); 854 dp->state = DCCPS_TIME_WAIT; 855 callout_stop(&dp->retrans_timer); 856 callout_reset(&dp->timewait_timer, DCCP_TIMEWAIT_TIMER, 857 dccp_timewait_t, dp); 858 break; 859 860 case DCCP_TYPE_MOVE: 861 DCCP_DEBUG((LOG_INFO, "Got DCCP MOVE\n")); 862 break; 863 864 default: 865 DCCP_DEBUG((LOG_INFO, "Got a %u packet while in established stage!\n", dh->dh_type)); 866 } 867 868 } else if (dp->state == DCCPS_SERVER_CLOSE) { 869 /* Server */ 870 switch (dh->dh_type) { 871 case DCCP_TYPE_CLOSE: 872 DCCP_DEBUG((LOG_INFO, "Got DCCP CLOSE (State DCCPS_SERVER_CLOSE)\n")); 873 callout_stop(&dp->retrans_timer); 874 dccp_output(dp, DCCP_TYPE_RESET + 2); 875 dp = dccp_close(dp); 876 goto badunlocked; 877 878 case DCCP_TYPE_RESET: 879 DCCP_DEBUG((LOG_INFO, "Got DCCP RESET\n")); 880 callout_stop(&dp->retrans_timer); 881 dccp_output(dp, DCCP_TYPE_RESET + 2); 882 dp->state = DCCPS_TIME_WAIT; 883 break; 884 default: 885 DCCP_DEBUG((LOG_INFO, "Got a %u packet while in server_close stage!\n", dh->dh_type)); 886 } 887 888 } else if (dp->state == DCCPS_CLIENT_CLOSE) { 889 /* Client */ 890 switch (dh->dh_type) { 891 case DCCP_TYPE_CLOSE: 892 /* Ignore */ 893 break; 894 case DCCP_TYPE_CLOSEREQ: 895 DCCP_DEBUG((LOG_INFO, "Got DCCP CLOSEREQ, state = DCCPS_CLIENT_CLOSE\n")); 896 /* Just resend close */ 897 dccp_output(dp, 0); 898 break; 899 case DCCP_TYPE_RESET: 900 DCCP_DEBUG((LOG_INFO, "Got DCCP RESET\n")); 901 callout_stop(&dp->retrans_timer); 902 dp->state = DCCPS_TIME_WAIT; 903 callout_reset(&dp->timewait_timer, DCCP_TIMEWAIT_TIMER, 904 dccp_timewait_t, dp); 905 break; 906 default: 907 DCCP_DEBUG((LOG_INFO, "Got a %u packet while in client_close stage!\n", dh->dh_type)); 908 909 } 910 } else { 911 DCCP_DEBUG((LOG_INFO, "Got a %u packet while in %u state!\n", dh->dh_type, dp->state)); 912 if (dh->dh_type != DCCP_TYPE_RESET) { 913 /* Force send reset. */ 914 DCCP_DEBUG((LOG_INFO, "Force sending a request!\n")); 915 dccp_output(dp, DCCP_TYPE_RESET + 2); 916 } 917 } 918 919 if (dh->dh_type == DCCP_TYPE_DATA || 920 dh->dh_type == DCCP_TYPE_ACK || 921 dh->dh_type == DCCP_TYPE_DATAACK) { 922 if (dp->cc_in_use[0] > 0) { 923 (*cc_sw[dp->cc_in_use[0]].cc_send_packet_recv)(dp->cc_state[0],options, optlen); 924 } 925 926 } 927 928 if (dh->dh_type == DCCP_TYPE_DATA || dh->dh_type == DCCP_TYPE_DATAACK) { 929 if (so->so_state & SS_CANTRCVMORE) 930 { 931 DCCP_DEBUG((LOG_INFO, "state & SS_CANTRCVMORE...!\n")); 932 m_freem(m); 933 if (opts) 934 m_freem(opts); 935 } else { 936 m_adj(m, (iphlen + data_off)); 937 DCCP_DEBUG((LOG_INFO, "Calling sbappend!\n")); 938 sbappend(&so->so_rcv, m); 939 } 940 DCCP_DEBUG((LOG_INFO, "Calling sorwakeup...!\n")); 941 sorwakeup(so); 942 } else { 943 m_freem(m); 944 if (opts) 945 m_freem(opts); 946 } 947 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 948 if (dp) 949 INP_UNLOCK(inp); 950 #endif 951 952 return; 953 954 badunlocked: 955 m_freem(m); 956 if (opts) 957 m_freem(opts); 958 return; 959 } 960 961 /* 962 * Notify a dccp user of an asynchronous error; 963 * just wake up so that he can collect error status. 964 */ 965 void 966 dccp_notify(struct inpcb *inp, int errno) 967 { 968 inp->inp_socket->so_error = errno; 969 sorwakeup(inp->inp_socket); 970 sowwakeup(inp->inp_socket); 971 return; 972 } 973 974 /* 975 * Called when we get ICMP errors (destination unrechable, 976 * parameter problem, source quench, time exceeded and redirects) 977 */ 978 void * 979 dccp_ctlinput(int cmd, const struct sockaddr *sa, void *vip) 980 { 981 struct ip *ip = vip; 982 struct dccphdr *dh; 983 void (*notify)(struct inpcb *, int) = dccp_notify; 984 struct in_addr faddr; 985 struct inpcb *inp = NULL; 986 987 faddr = ((const struct sockaddr_in *)sa)->sin_addr; 988 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 989 return NULL; 990 991 if (PRC_IS_REDIRECT(cmd)) { 992 ip = 0; 993 notify = in_rtchange; 994 } else if (cmd == PRC_HOSTDEAD) 995 ip = 0; 996 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 997 return NULL; 998 if (ip) { 999 /*s = splsoftnet();*/ 1000 dh = (struct dccphdr *)((vaddr_t)ip + (ip->ip_hl << 2)); 1001 INP_INFO_RLOCK(&dccpbinfo); 1002 in_pcbnotify(&dccpbtable, faddr, dh->dh_dport, 1003 ip->ip_src, dh->dh_sport, inetctlerrmap[cmd], notify); 1004 if (inp != NULL) { 1005 INP_LOCK(inp); 1006 if (inp->inp_socket != NULL) { 1007 (*notify)(inp, inetctlerrmap[cmd]); 1008 } 1009 INP_UNLOCK(inp); 1010 } 1011 INP_INFO_RUNLOCK(&dccpbinfo); 1012 /*splx(s);*/ 1013 } else 1014 in_pcbnotifyall(&dccpbtable, faddr, inetctlerrmap[cmd], notify); 1015 1016 return NULL; 1017 } 1018 1019 static int 1020 dccp_optsset(struct dccpcb *dp, struct sockopt *sopt) 1021 { 1022 int optval; 1023 int error = 0; 1024 1025 switch (sopt->sopt_name) { 1026 case DCCP_CCID: 1027 error = sockopt_getint(sopt, &optval); 1028 /* Add check that optval is a CCID we support!!! */ 1029 if (optval == 2 || optval == 3 || optval == 0) { 1030 dp->pref_cc = optval; 1031 } else { 1032 error = EINVAL; 1033 } 1034 break; 1035 case DCCP_CSLEN: 1036 error = sockopt_getint(sopt, &optval); 1037 if (optval > 15 || optval < 0) { 1038 error = EINVAL; 1039 } else { 1040 dp->cslen = optval; 1041 } 1042 break; 1043 case DCCP_MAXSEG: 1044 error = sockopt_getint(sopt, &optval); 1045 if (optval > 0 && optval <= dp->d_maxseg) { 1046 dp->d_maxseg = optval; 1047 } else { 1048 error = EINVAL; 1049 } 1050 break; 1051 case DCCP_SERVICE: 1052 error = sockopt_getint(sopt, &optval); 1053 dp->scode = optval; 1054 break; 1055 1056 default: 1057 error = ENOPROTOOPT; 1058 } 1059 1060 return error; 1061 } 1062 1063 static int 1064 dccp_optsget(struct dccpcb *dp, struct sockopt *sopt) 1065 { 1066 int optval = 0; 1067 int error = 0; 1068 1069 switch (sopt->sopt_name) { 1070 case DCCP_CCID: 1071 optval = dp->pref_cc; 1072 error = sockopt_set(sopt, &optval, sizeof(optval)); 1073 break; 1074 case DCCP_CSLEN: 1075 optval = dp->cslen; 1076 error = sockopt_set(sopt, &optval, sizeof(optval)); 1077 break; 1078 case DCCP_MAXSEG: 1079 optval = dp->d_maxseg; 1080 error = sockopt_set(sopt, &optval, sizeof(optval)); 1081 break; 1082 case DCCP_SERVICE: 1083 optval = dp->scode; 1084 error = sockopt_set(sopt, &optval, sizeof(optval)); 1085 break; 1086 default: 1087 error = ENOPROTOOPT; 1088 } 1089 1090 return error; 1091 } 1092 1093 /* 1094 * Called by getsockopt and setsockopt. 1095 * 1096 */ 1097 int 1098 dccp_ctloutput(int op, struct socket *so, struct sockopt *sopt) 1099 { 1100 int s, error = 0; 1101 struct inpcb *inp; 1102 #if defined(INET6) 1103 struct in6pcb *in6p; 1104 #endif 1105 struct dccpcb *dp; 1106 int family; /* family of the socket */ 1107 1108 family = so->so_proto->pr_domain->dom_family; 1109 error = 0; 1110 1111 s = splsoftnet(); 1112 INP_INFO_RLOCK(&dccpbinfo); 1113 switch (family) { 1114 case PF_INET: 1115 inp = sotoinpcb(so); 1116 #if defined(INET6) 1117 in6p = NULL; 1118 #endif 1119 break; 1120 #if defined(INET6) 1121 case PF_INET6: 1122 inp = NULL; 1123 in6p = sotoin6pcb(so); 1124 break; 1125 #endif 1126 default: 1127 INP_INFO_RUNLOCK(&dccpbinfo); 1128 splx(s); 1129 return EAFNOSUPPORT; 1130 } 1131 #if defined(INET6) 1132 if (inp == NULL && in6p == NULL) 1133 #else 1134 if (inp == NULL) 1135 #endif 1136 { 1137 INP_INFO_RUNLOCK(&dccpbinfo); 1138 splx(s); 1139 return (ECONNRESET); 1140 } 1141 /* 1142 if (inp) 1143 INP_LOCK(inp); 1144 else 1145 IN6P_LOCK(in6p); 1146 INP_INFO_RUNLOCK(&dccpbinfo); 1147 */ 1148 if (sopt->sopt_level != IPPROTO_DCCP) { 1149 switch (family) { 1150 case PF_INET: 1151 error = ip_ctloutput(op, so, sopt); 1152 break; 1153 #if defined(INET6) 1154 case PF_INET6: 1155 error = ip6_ctloutput(op, so, sopt); 1156 break; 1157 #endif 1158 } 1159 splx(s); 1160 return (error); 1161 } 1162 1163 if (inp) 1164 dp = intodccpcb(inp); 1165 #if defined(INET6) 1166 else if (in6p) 1167 dp = in6todccpcb(in6p); 1168 #endif 1169 else 1170 dp = NULL; 1171 1172 if (op == PRCO_SETOPT) { 1173 error = dccp_optsset(dp, sopt); 1174 } else if (op == PRCO_GETOPT) { 1175 error = dccp_optsget(dp, sopt); 1176 } else { 1177 error = EINVAL; 1178 } 1179 /* 1180 if (inp) 1181 INP_UNLOCK(inp); 1182 else 1183 IN6P_UNLOCK(in6p); 1184 */ 1185 splx(s); 1186 return error; 1187 } 1188 1189 int 1190 dccp_output(struct dccpcb *dp, u_int8_t extra) 1191 { 1192 struct inpcb *inp; 1193 struct in6pcb *in6p = NULL; 1194 struct socket *so; 1195 struct mbuf *m; 1196 1197 struct ip *ip = NULL; 1198 struct dccphdr *dh; 1199 struct dccplhdr *dlh; 1200 struct dccp_requesthdr *drqh; 1201 struct dccp_ackhdr *dah; 1202 struct dccp_acklhdr *dalh; 1203 struct dccp_resethdr *drth; 1204 u_char *optp = NULL; 1205 int error = 0; 1206 int off, sendalot, t, i; 1207 u_int32_t hdrlen, optlen, extrah_len, cslen; 1208 u_int8_t type; 1209 char options[DCCP_MAX_OPTIONS *2]; 1210 long len, pktlen; 1211 int isipv6 = 0; 1212 int use_shortseq = 0; 1213 #ifdef INET6 1214 struct ip6_hdr *ip6 = NULL; 1215 #endif 1216 1217 DCCP_DEBUG((LOG_INFO, "dccp_output start!\n")); 1218 1219 isipv6 = (dp->inp_vflag & INP_IPV6) != 0; 1220 1221 DCCP_DEBUG((LOG_INFO, "Going to send a DCCP packet!\n")); 1222 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 1223 KASSERT(mutex_assert(&dp->d_inpcb->inp_mtx, MA_OWNED)); 1224 #endif 1225 1226 #if defined(INET6) 1227 if (isipv6) { 1228 inp = 0; 1229 in6p = dp->d_in6pcb; 1230 so = in6p->in6p_socket; 1231 } else 1232 #endif 1233 { 1234 inp = dp->d_inpcb; 1235 so = inp->inp_socket; 1236 } 1237 1238 if (dp->state != DCCPS_ESTAB && extra == 1) { 1239 /* Only let cc decide when to resend if we are in establised state */ 1240 return 0; 1241 } 1242 1243 if (so->so_snd.sb_cc){ 1244 pktlen = dp->pktlen[dp->pktlenidx]; 1245 } else 1246 pktlen = 0; 1247 1248 /* Check with CC if we can send... */ 1249 if (pktlen && dp->cc_in_use[0] > 0 && dp->state == DCCPS_ESTAB) { 1250 if (!(*cc_sw[dp->cc_in_use[0]].cc_send_packet)(dp->cc_state[0], pktlen)) { 1251 DCCP_DEBUG((LOG_INFO, "Not allowed to send right now\n")); 1252 return 0; 1253 } 1254 } 1255 1256 if (pktlen) { 1257 dp->pktcnt --; 1258 dp->pktlenidx = (dp->pktlenidx +1) % DCCP_MAX_PKTS; 1259 } 1260 1261 again: 1262 sendalot = 0; 1263 1264 /* 1265 * off not needed for dccp because we do not need to wait for ACK 1266 * before removing the packet 1267 */ 1268 off = 0; 1269 optlen = 0; 1270 1271 if (pktlen > dp->d_maxseg) { 1272 /* this should not happen */ 1273 DCCP_DEBUG((LOG_INFO, "packet will be fragmented! maxseg %d\n", dp->d_maxseg)); 1274 len = dp->d_maxseg; 1275 pktlen -= len; 1276 sendalot = 1; 1277 } else 1278 len = pktlen; 1279 1280 if (extra == DCCP_TYPE_RESET + 2) { 1281 DCCP_DEBUG((LOG_INFO, "Force sending of DCCP TYPE_RESET! seq=%llu\n", dp->seq_snd)); 1282 type = DCCP_TYPE_RESET; 1283 extrah_len = 12; 1284 } else if (dp->state <= DCCPS_REQUEST && dp->who == DCCP_CLIENT) { 1285 DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_REQUEST!\n")); 1286 type = DCCP_TYPE_REQUEST; 1287 dp->state = DCCPS_REQUEST; 1288 extrah_len = 4; 1289 } else if (dp->state == DCCPS_REQUEST && dp->who == DCCP_SERVER) { 1290 DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_RESPONSE!\n")); 1291 type = DCCP_TYPE_RESPONSE; 1292 dp->state = DCCPS_RESPOND; 1293 extrah_len = 12; 1294 } else if (dp->state == DCCPS_RESPOND) { 1295 DCCP_DEBUG((LOG_INFO, "Still in feature neg, sending DCCP TYPE_ACK!\n")); 1296 type = DCCP_TYPE_ACK; 1297 if (!dp->shortseq) 1298 extrah_len = 8; 1299 else { 1300 extrah_len = 4; 1301 use_shortseq = 1; 1302 } 1303 } else if (dp->state == DCCPS_ESTAB) { 1304 if (dp->ack_snd && len) { 1305 DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_DATAACK!\n")); 1306 type = DCCP_TYPE_DATAACK; 1307 /*(u_int32_t *)&extrah = dp->seq_rcv; */ 1308 if (!dp->shortseq) 1309 extrah_len = 8; 1310 else { 1311 extrah_len = 4; 1312 use_shortseq = 1; 1313 } 1314 } else if (dp->ack_snd) { 1315 DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_ACK!\n")); 1316 type = DCCP_TYPE_ACK; 1317 if (!dp->shortseq) 1318 extrah_len = 8; 1319 else { 1320 extrah_len = 4; 1321 use_shortseq = 1; 1322 } 1323 } else if (len) { 1324 DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_DATA!\n")); 1325 type = DCCP_TYPE_DATA; 1326 extrah_len = 0; 1327 } else { 1328 DCCP_DEBUG((LOG_INFO, "No ack or data to send!\n")); 1329 return 0; 1330 } 1331 } else if (dp->state == DCCPS_CLIENT_CLOSE) { 1332 DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_CLOSE!\n")); 1333 type = DCCP_TYPE_CLOSE; 1334 extrah_len = 8; 1335 } else if (dp->state == DCCPS_SERVER_CLOSE) { 1336 DCCP_DEBUG((LOG_INFO, "Sending DCCP TYPE_CLOSEREQ!\n")); 1337 type = DCCP_TYPE_CLOSEREQ; 1338 extrah_len = 8; 1339 } else { 1340 DCCP_DEBUG((LOG_INFO, "Hey, we should never get here, state = %u\n", dp->state)); 1341 return 1; 1342 } 1343 1344 /* Adding options. */ 1345 if (dp->optlen) { 1346 DCCP_DEBUG((LOG_INFO, "Copying options from dp->options! %u\n", dp->optlen)); 1347 memcpy(options, dp->options, dp->optlen); 1348 optlen = dp->optlen; 1349 dp->optlen = 0; 1350 } 1351 1352 if (dp->featlen && (optlen + dp->featlen < DCCP_MAX_OPTIONS)) { 1353 DCCP_DEBUG((LOG_INFO, "Copying options from dp->features! %u\n", dp->featlen)); 1354 memcpy(options + optlen, dp->features, dp->featlen); 1355 optlen += dp->featlen; 1356 } 1357 1358 t = optlen % 4; 1359 1360 if (t) { 1361 t = 4 - t; 1362 for (i = 0 ; i<t; i++) { 1363 options[optlen] = 0; 1364 optlen++; 1365 } 1366 } 1367 1368 #ifdef INET6 1369 if (isipv6) { 1370 DCCP_DEBUG((LOG_INFO, "Sending ipv6 packet...\n")); 1371 if (!use_shortseq) 1372 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct dccplhdr) + 1373 extrah_len + optlen; 1374 else 1375 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct dccphdr) + 1376 extrah_len + optlen; 1377 } else 1378 #endif 1379 { 1380 if (!use_shortseq) 1381 hdrlen = sizeof(struct ip) + sizeof(struct dccplhdr) + 1382 extrah_len + optlen; 1383 else 1384 hdrlen = sizeof(struct ip) + sizeof(struct dccphdr) + 1385 extrah_len + optlen; 1386 } 1387 DCCP_DEBUG((LOG_INFO, "Pkt headerlen %u\n", hdrlen)); 1388 1389 if (len > (dp->d_maxseg - extrah_len - optlen)) { 1390 len = dp->d_maxseg - extrah_len - optlen; 1391 sendalot = 1; 1392 } 1393 1394 MGETHDR(m, M_DONTWAIT, MT_HEADER); 1395 if (m == NULL) { 1396 error = ENOBUFS; 1397 goto release; 1398 } 1399 if (MHLEN < hdrlen + max_linkhdr) { 1400 MCLGET(m, M_DONTWAIT); 1401 if ((m->m_flags & M_EXT) == 0) { 1402 error = ENOBUFS; 1403 goto release; 1404 } 1405 } 1406 1407 m->m_data += max_linkhdr; 1408 m->m_len = hdrlen; 1409 1410 if (len) { /* We have data to send */ 1411 if (len <= M_TRAILINGSPACE(m) - hdrlen) { 1412 m_copydata(so->so_snd.sb_mb, off, (int) len, 1413 mtod(m, char *) + hdrlen); 1414 m->m_len += len; 1415 } else { 1416 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); 1417 if (m->m_next == 0) { 1418 error = ENOBUFS; 1419 goto release; 1420 } 1421 } 1422 } else { 1423 dp->ndp++; 1424 } 1425 1426 m_reset_rcvif(m); 1427 1428 if (!isipv6 && (len + hdrlen) > IP_MAXPACKET) { 1429 error = EMSGSIZE; 1430 goto release; 1431 } 1432 1433 /* 1434 * Fill in mbuf with extended DCCP header 1435 * and addresses and length put into network format. 1436 */ 1437 #ifdef INET6 1438 if (isipv6) { 1439 ip6 = mtod(m, struct ip6_hdr *); 1440 dh = (struct dccphdr *)(ip6 + 1); 1441 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 1442 (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK); 1443 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 1444 (IPV6_VERSION & IPV6_VERSION_MASK); 1445 ip6->ip6_nxt = IPPROTO_DCCP; 1446 ip6->ip6_src = in6p->in6p_laddr; 1447 ip6->ip6_dst = in6p->in6p_faddr; 1448 } else 1449 #endif 1450 { 1451 ip = mtod(m, struct ip *); 1452 dh = (struct dccphdr *)(ip + 1); 1453 memset(ip, 0, sizeof(struct ip)); 1454 ip->ip_p = IPPROTO_DCCP; 1455 ip->ip_src = inp->inp_laddr; 1456 ip->ip_dst = inp->inp_faddr; 1457 } 1458 dlh = (struct dccplhdr *)dh; 1459 1460 if (inp) { 1461 dh->dh_sport = inp->inp_lport; 1462 dh->dh_dport = inp->inp_fport; 1463 } 1464 #ifdef INET6 1465 else if (in6p) { 1466 dh->dh_sport = in6p->in6p_lport; 1467 dh->dh_dport = in6p->in6p_fport; 1468 } 1469 #endif 1470 dh->dh_cscov = dp->cslen; 1471 dh->dh_ccval = dp->ccval; 1472 dh->dh_type = type; 1473 dh->dh_res = 0; /* Reserved field should be zero */ 1474 if (!use_shortseq) { 1475 dlh->dh_res2 = 0; /* Reserved field should be zero */ 1476 dh->dh_off = 4 + (extrah_len / 4) + (optlen / 4); 1477 } else 1478 dh->dh_off = 3 + (extrah_len / 4) + (optlen / 4); 1479 1480 dp->seq_snd = (dp->seq_snd +1) % 281474976710656LL; 1481 if (!use_shortseq) { 1482 DSEQ_TO_DHDR(dlh, dp->seq_snd); 1483 dlh->dh_x = 1; 1484 } else { 1485 /* short sequene number */ 1486 dh->dh_seq = htonl(dp->seq_snd) >> 8; 1487 dh->dh_x = 0; 1488 } 1489 1490 if (!use_shortseq) { 1491 DCCP_DEBUG((LOG_INFO, "Sending with seq %x.%x, (dp->seq_snd = %llu)\n\n", dlh->dh_seq, dlh->dh_seq2, dp->seq_snd)); 1492 } else { 1493 DCCP_DEBUG((LOG_INFO, "Sending with seq %x, (dp->seq_snd = %llu)\n\n", dh->dh_seq, dp->seq_snd)); 1494 } 1495 1496 if (dh->dh_type == DCCP_TYPE_REQUEST) { 1497 drqh = (struct dccp_requesthdr *)(dlh + 1); 1498 drqh->drqh_scode = dp->scode; 1499 optp = (u_char *)(drqh + 1); 1500 } else if (dh->dh_type == DCCP_TYPE_RESET) { 1501 drth = (struct dccp_resethdr *)(dlh + 1); 1502 drth->drth_dash.dah_res = 0; 1503 DSEQ_TO_DAHDR(drth->drth_dash, dp->seq_rcv); 1504 if (dp->state == DCCPS_SERVER_CLOSE) 1505 drth->drth_reason = 1; 1506 else 1507 drth->drth_reason = 2; 1508 drth->drth_data1 = 0; 1509 drth->drth_data2 = 0; 1510 drth->drth_data3 = 0; 1511 optp = (u_char *)(drth + 1); 1512 } else if (extrah_len) { 1513 if (!use_shortseq){ 1514 dalh = (struct dccp_acklhdr *)(dlh + 1); 1515 dalh->dash.dah_res = 0; /* Reserved field should be zero */ 1516 1517 if (dp->state == DCCPS_ESTAB) { 1518 DSEQ_TO_DAHDR(dalh->dash, dp->ack_snd); 1519 dp->ack_snd = 0; 1520 } else { 1521 DSEQ_TO_DAHDR(dalh->dash, dp->seq_rcv); 1522 } 1523 1524 if (dh->dh_type == DCCP_TYPE_RESPONSE) { 1525 DCCP_DEBUG((LOG_INFO, "Sending dccp type response\n")); 1526 drqh = (struct dccp_requesthdr *)(dalh + 1); 1527 drqh->drqh_scode = dp->scode; 1528 optp = (u_char *)(drqh + 1); 1529 } else 1530 optp = (u_char *)(dalh + 1); 1531 } else { 1532 /* XXX shortseq */ 1533 dah = (struct dccp_ackhdr *)(dh + 1); 1534 dah->dash.dah_res = 0; /* Reserved field should be zero */ 1535 dah->dash.dah_ack = htonl(dp->seq_rcv) >> 8; 1536 optp = (u_char *)(dah + 1); 1537 } 1538 1539 } else { 1540 optp = (u_char *)(dlh + 1); 1541 } 1542 1543 if (optlen) 1544 memcpy(optp, options, optlen); 1545 1546 m->m_pkthdr.len = hdrlen + len; 1547 1548 if (dh->dh_cscov == 0) { 1549 #ifdef INET6 1550 if (isipv6) 1551 cslen = (hdrlen - sizeof(struct ip6_hdr)) + len; 1552 else 1553 cslen = (hdrlen - sizeof(struct ip)) + len; 1554 #else 1555 cslen = (hdrlen - sizeof(struct ip)) + len; 1556 #endif 1557 } else { 1558 cslen = dh->dh_off * 4 + (dh->dh_cscov - 1) * 4; 1559 #ifdef INET6 1560 if (isipv6) { 1561 if (cslen > (hdrlen - sizeof(struct ip6_hdr)) + len) 1562 cslen = (hdrlen - sizeof(struct ip6_hdr)) + len; 1563 } else { 1564 if (cslen > (hdrlen - sizeof(struct ip)) + len) 1565 cslen = (hdrlen - sizeof(struct ip)) + len; 1566 } 1567 #else 1568 if (cslen > (hdrlen - sizeof(struct ip)) + len) 1569 cslen = (hdrlen - sizeof(struct ip)) + len; 1570 #endif 1571 } 1572 1573 /* 1574 * Set up checksum 1575 */ 1576 m->m_pkthdr.csum_flags = 0; 1577 1578 dh->dh_sum = 0; 1579 #ifdef INET6 1580 if (isipv6) { 1581 dh->dh_sum = in6_cksum(m, IPPROTO_DCCP, sizeof(struct ip6_hdr), 1582 cslen); 1583 } else 1584 #endif 1585 { 1586 ip->ip_len = htons(hdrlen + len); 1587 ip->ip_ttl = dp->inp_ip_ttl; /* XXX */ 1588 ip->ip_tos = dp->inp_ip_tos; /* XXX */ 1589 1590 dh->dh_sum = in4_cksum(m, IPPROTO_DCCP, sizeof(struct ip), 1591 cslen); 1592 #ifndef __OpenBSD__ 1593 m->m_pkthdr.csum_data = offsetof(struct dccphdr, dh_sum); 1594 #endif 1595 } 1596 1597 dccpstat.dccps_opackets++; 1598 dccpstat.dccps_obytes += m->m_pkthdr.len; 1599 1600 #ifdef INET6 1601 if (isipv6) { 1602 DCCP_DEBUG((LOG_INFO, "Calling ip_output6, mbuf->m_len = %u, mbuf->m_pkthdr.len = %u\n", m->m_len, m->m_pkthdr.len)); 1603 1604 error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route, 1605 (in6p->in6p_socket->so_options & SO_DONTROUTE), NULL, NULL, 1606 NULL); 1607 } else 1608 #endif 1609 { 1610 DCCP_DEBUG((LOG_INFO, "Calling ip_output, mbuf->m_len = %u, mbuf->m_pkthdr.len = %u\n", m->m_len, m->m_pkthdr.len)); 1611 error = ip_output(m, inp->inp_options, &inp->inp_route, 1612 (inp->inp_socket->so_options & SO_DONTROUTE), 0, 1613 inp); 1614 } 1615 1616 if (error) { 1617 DCCP_DEBUG((LOG_INFO, "IP output failed! %d\n", error)); 1618 return (error); 1619 } 1620 1621 #if defined(INET6) 1622 if (isipv6) { 1623 sbdrop(&in6p->in6p_socket->so_snd, len); 1624 sowwakeup(in6p->in6p_socket); 1625 } else 1626 #endif 1627 { 1628 sbdrop(&inp->inp_socket->so_snd, len); 1629 sowwakeup(inp->inp_socket); 1630 } 1631 1632 if (dp->cc_in_use[0] > 0 && dp->state == DCCPS_ESTAB) { 1633 DCCP_DEBUG((LOG_INFO, "Calling *cc_sw[%u].cc_send_packet_sent!\n", dp->cc_in_use[0])); 1634 if (sendalot) { 1635 (*cc_sw[dp->cc_in_use[0]].cc_send_packet_sent)(dp->cc_state[0], 1,len); 1636 goto again; 1637 } else { 1638 (*cc_sw[dp->cc_in_use[0]].cc_send_packet_sent)(dp->cc_state[0], 0,len); 1639 } 1640 } else { 1641 if (sendalot) 1642 goto again; 1643 } 1644 1645 DCCP_DEBUG((LOG_INFO, "dccp_output finished\n")); 1646 1647 return (0); 1648 1649 release: 1650 m_freem(m); 1651 return (error); 1652 } 1653 1654 int 1655 dccp_abort(struct socket *so) 1656 { 1657 struct inpcb *inp = 0; 1658 struct in6pcb *in6p; 1659 struct dccpcb *dp; 1660 1661 DCCP_DEBUG((LOG_INFO, "Entering dccp_abort!\n")); 1662 INP_INFO_WLOCK(&dccpbinfo); 1663 if (so->so_proto->pr_domain->dom_family == PF_INET6) { 1664 in6p = sotoin6pcb(so); 1665 if (in6p == 0) { 1666 return EINVAL; 1667 } 1668 inp = 0; 1669 dp = (struct dccpcb *)in6p->in6p_ppcb; 1670 } else { 1671 inp = sotoinpcb(so); 1672 if (inp == 0) { 1673 INP_INFO_WUNLOCK(&dccpbinfo); 1674 return EINVAL; 1675 } 1676 dp = (struct dccpcb *)inp->inp_ppcb; 1677 } 1678 1679 dccp_disconnect2(dp); 1680 1681 INP_INFO_WUNLOCK(&dccpbinfo); 1682 return 0; 1683 } 1684 1685 static struct dccpcb * 1686 dccp_close(struct dccpcb *dp) 1687 { 1688 struct socket *so; 1689 struct inpcb *inp = dp->d_inpcb; 1690 struct in6pcb *in6p = dp->d_in6pcb; 1691 so = dptosocket(dp); 1692 1693 DCCP_DEBUG((LOG_INFO, "Entering dccp_close!\n")); 1694 1695 /* Stop all timers */ 1696 callout_stop(&dp->connect_timer); 1697 callout_stop(&dp->retrans_timer); 1698 callout_stop(&dp->close_timer); 1699 callout_stop(&dp->timewait_timer); 1700 1701 if (dp->cc_in_use[0] > 0) 1702 (*cc_sw[dp->cc_in_use[0]].cc_send_free)(dp->cc_state[0]); 1703 if (dp->cc_in_use[1] > 0) 1704 (*cc_sw[dp->cc_in_use[1]].cc_recv_free)(dp->cc_state[1]); 1705 1706 pool_put(&dccpcb_pool, dp); 1707 if (inp) { 1708 inp->inp_ppcb = NULL; 1709 soisdisconnected(so); 1710 in_pcbdetach(inp); 1711 } 1712 #if defined(INET6) 1713 else if (in6p) { 1714 in6p->in6p_ppcb = 0; 1715 soisdisconnected(so); 1716 in6_pcbdetach(in6p); 1717 } 1718 #endif 1719 return ((struct dccpcb *)0); 1720 } 1721 1722 /* 1723 * Runs when a new socket is created with the 1724 * socket system call or sonewconn. 1725 */ 1726 int 1727 dccp_attach(struct socket *so, int proto) 1728 { 1729 struct inpcb *inp = 0; 1730 struct in6pcb *in6p = 0; 1731 struct dccpcb *dp; 1732 int s, family, error = 0; 1733 1734 DCCP_DEBUG((LOG_INFO, "Entering dccp_attach(proto=%d)!\n", proto)); 1735 INP_INFO_WLOCK(&dccpbinfo); 1736 s = splsoftnet(); 1737 sosetlock(so); 1738 1739 family = so->so_proto->pr_domain->dom_family; 1740 switch (family) { 1741 case PF_INET: 1742 inp = sotoinpcb(so); 1743 if (inp != 0) { 1744 error = EINVAL; 1745 goto out; 1746 } 1747 error = soreserve(so, dccp_sendspace, dccp_recvspace); 1748 if (error) 1749 goto out; 1750 error = in_pcballoc(so, &dccpbtable); 1751 if (error) 1752 goto out; 1753 inp = sotoinpcb(so); 1754 break; 1755 #if defined(INET6) 1756 case PF_INET6: 1757 in6p = sotoin6pcb(so); 1758 if (in6p != 0) { 1759 error = EINVAL; 1760 goto out; 1761 } 1762 error = soreserve(so, dccp_sendspace, dccp_recvspace); 1763 if (error) 1764 goto out; 1765 error = in6_pcballoc(so, &dccpbtable); 1766 if (error) 1767 goto out; 1768 in6p = sotoin6pcb(so); 1769 break; 1770 #endif 1771 default: 1772 error = EAFNOSUPPORT; 1773 goto out; 1774 } 1775 1776 if (inp) 1777 dp = dccp_newdccpcb(PF_INET, (void *)inp); 1778 else if (in6p) 1779 dp = dccp_newdccpcb(PF_INET6, (void *)in6p); 1780 else 1781 dp = NULL; 1782 1783 if (dp == 0) { 1784 int nofd = so->so_state & SS_NOFDREF; 1785 so->so_state &= ~SS_NOFDREF; 1786 #if defined(INET6) 1787 if (proto == PF_INET6) { 1788 in6_pcbdetach(in6p); 1789 } else 1790 #endif 1791 in_pcbdetach(inp); 1792 so->so_state |= nofd; 1793 error = ENOBUFS; 1794 goto out; 1795 } 1796 1797 #ifdef INET6 1798 if (proto == PF_INET6) { 1799 DCCP_DEBUG((LOG_INFO, "We are a ipv6 socket!!!\n")); 1800 dp->inp_vflag |= INP_IPV6; 1801 } else 1802 #endif 1803 dp->inp_vflag |= INP_IPV4; 1804 dp->inp_ip_ttl = ip_defttl; 1805 1806 dp->state = DCCPS_CLOSED; 1807 out: 1808 splx(s); 1809 INP_INFO_WUNLOCK(&dccpbinfo); 1810 return error; 1811 } 1812 1813 static int 1814 dccp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l) 1815 { 1816 struct inpcb *inp; 1817 int error; 1818 int s; 1819 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 1820 1821 DCCP_DEBUG((LOG_INFO, "Entering dccp_bind!\n")); 1822 INP_INFO_WLOCK(&dccpbinfo); 1823 inp = sotoinpcb(so); 1824 if (inp == 0) { 1825 INP_INFO_WUNLOCK(&dccpbinfo); 1826 return EINVAL; 1827 } 1828 1829 /* Do not bind to multicast addresses! */ 1830 if (sin->sin_family == AF_INET && 1831 IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 1832 INP_INFO_WUNLOCK(&dccpbinfo); 1833 return EAFNOSUPPORT; 1834 } 1835 INP_LOCK(inp); 1836 s = splsoftnet(); 1837 error = in_pcbbind(inp, sin, l); 1838 splx(s); 1839 INP_UNLOCK(inp); 1840 INP_INFO_WUNLOCK(&dccpbinfo); 1841 return error; 1842 } 1843 1844 /* 1845 * Initiates a connection to a server 1846 * Called by the connect system call. 1847 */ 1848 static int 1849 dccp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l) 1850 { 1851 struct inpcb *inp; 1852 struct dccpcb *dp; 1853 int error; 1854 struct sockaddr_in *sin; 1855 char test[2]; 1856 1857 DCCP_DEBUG((LOG_INFO, "Entering dccp_connect!\n")); 1858 1859 INP_INFO_WLOCK(&dccpbinfo); 1860 inp = sotoinpcb(so); 1861 if (inp == 0) { 1862 INP_INFO_WUNLOCK(&dccpbinfo); 1863 return EINVAL; 1864 } 1865 INP_LOCK(inp); 1866 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1867 INP_UNLOCK(inp); 1868 INP_INFO_WUNLOCK(&dccpbinfo); 1869 return EISCONN; 1870 } 1871 1872 dp = (struct dccpcb *)inp->inp_ppcb; 1873 1874 if (dp->state == DCCPS_ESTAB) { 1875 DCCP_DEBUG((LOG_INFO, "Why are we in connect when we already have a established connection?\n")); 1876 } 1877 1878 dp->who = DCCP_CLIENT; 1879 dp->seq_snd = (((u_int64_t)random() << 32) | random()) % 281474976710656LL; 1880 dp->ref_seq.hi = dp->seq_snd >> 24; 1881 dp->ref_seq.lo = (u_int64_t)(dp->seq_snd & 0xffffff); 1882 DCCP_DEBUG((LOG_INFO, "dccp_connect seq_snd %llu\n", dp->seq_snd)); 1883 1884 dccpstat.dccps_connattempt++; 1885 1886 sin = (struct sockaddr_in *)nam; 1887 if (sin->sin_family == AF_INET 1888 && IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) { 1889 error = EAFNOSUPPORT; 1890 goto bad; 1891 } 1892 1893 error = dccp_doconnect(so, nam, l, 0); 1894 1895 if (error != 0) 1896 goto bad; 1897 1898 callout_reset(&dp->retrans_timer, dp->retrans, dccp_retrans_t, dp); 1899 callout_reset(&dp->connect_timer, DCCP_CONNECT_TIMER, dccp_connect_t, dp); 1900 1901 if (dccp_do_feature_nego){ 1902 test[0] = dp->pref_cc; 1903 dccp_add_feature(dp, DCCP_OPT_CHANGE_R, DCCP_FEATURE_CC, test, 1); 1904 } 1905 1906 error = dccp_output(dp, 0); 1907 1908 bad: 1909 INP_UNLOCK(inp); 1910 INP_INFO_WUNLOCK(&dccpbinfo); 1911 return error; 1912 } 1913 1914 static int 1915 dccp_connect2(struct socket *so, struct socket *so2) 1916 { 1917 KASSERT(solocked(so)); 1918 1919 return EOPNOTSUPP; 1920 } 1921 1922 /* 1923 * 1924 * 1925 */ 1926 int 1927 dccp_doconnect(struct socket *so, struct sockaddr *nam, 1928 struct lwp *l, int isipv6) 1929 { 1930 struct inpcb *inp; 1931 #ifdef INET6 1932 struct in6pcb *in6p; 1933 #endif 1934 int error = 0; 1935 1936 DCCP_DEBUG((LOG_INFO, "Entering dccp_doconnect!\n")); 1937 1938 #if defined(INET6) 1939 if (isipv6) { 1940 in6p = sotoin6pcb(so); 1941 inp = 0; 1942 } else 1943 #endif 1944 { 1945 inp = sotoinpcb(so); 1946 in6p = 0; 1947 } 1948 1949 #if !defined(__NetBSD__) || !defined(INET6) 1950 if (inp->inp_lport == 0) { 1951 #else 1952 if (isipv6 ? in6p->in6p_lport == 0 : inp->inp_lport == 0) { 1953 #endif 1954 #ifdef INET6 1955 if (isipv6) { 1956 DCCP_DEBUG((LOG_INFO, "Running in6_pcbbind!\n")); 1957 error = in6_pcbbind(in6p, NULL, l); 1958 } else 1959 #endif /* INET6 */ 1960 { 1961 error = in_pcbbind(inp, NULL, l); 1962 } 1963 if (error) { 1964 DCCP_DEBUG((LOG_INFO, "in_pcbbind=%d\n",error)); 1965 return error; 1966 } 1967 } 1968 1969 #ifdef INET6 1970 if (isipv6) { 1971 error = in6_pcbconnect(in6p, (struct sockaddr_in6 *)nam, l); 1972 DCCP_DEBUG((LOG_INFO, "in6_pcbconnect=%d\n",error)); 1973 } else 1974 #endif 1975 error = in_pcbconnect(inp, (struct sockaddr_in *)nam, l); 1976 if (error) { 1977 DCCP_DEBUG((LOG_INFO, "in_pcbconnect=%d\n",error)); 1978 return error; 1979 } 1980 1981 soisconnecting(so); 1982 return error; 1983 } 1984 1985 /* 1986 * Detaches the DCCP protocol from the socket. 1987 * 1988 */ 1989 int 1990 dccp_detach(struct socket *so) 1991 { 1992 struct inpcb *inp; 1993 struct in6pcb *in6p; 1994 struct dccpcb *dp; 1995 1996 DCCP_DEBUG((LOG_INFO, "Entering dccp_detach!\n")); 1997 #ifdef INET6 1998 if (so->so_proto->pr_domain->dom_family == AF_INET6) { 1999 in6p = sotoin6pcb(so); 2000 if (in6p == 0) { 2001 return EINVAL; 2002 } 2003 dp = (struct dccpcb *)in6p->in6p_ppcb; 2004 } else 2005 #endif 2006 { 2007 inp = sotoinpcb(so); 2008 if (inp == 0) { 2009 return EINVAL; 2010 } 2011 dp = (struct dccpcb *)inp->inp_ppcb; 2012 } 2013 if (! dccp_disconnect2(dp)) { 2014 INP_UNLOCK(inp); 2015 } 2016 INP_INFO_WUNLOCK(&dccpbinfo); 2017 return 0; 2018 } 2019 2020 /* 2021 * 2022 * 2023 */ 2024 int 2025 dccp_disconnect(struct socket *so) 2026 { 2027 struct inpcb *inp; 2028 struct in6pcb *in6p; 2029 struct dccpcb *dp; 2030 2031 DCCP_DEBUG((LOG_INFO, "Entering dccp_disconnect!\n")); 2032 INP_INFO_WLOCK(&dccpbinfo); 2033 #ifndef __NetBSD__ 2034 inp = sotoinpcb(so); 2035 if (inp == 0) { 2036 INP_INFO_WUNLOCK(&dccpbinfo); 2037 return EINVAL; 2038 } 2039 INP_LOCK(inp); 2040 if (inp->inp_faddr.s_addr == INADDR_ANY) { 2041 INP_INFO_WUNLOCK(&dccpbinfo); 2042 INP_UNLOCK(inp); 2043 return ENOTCONN; 2044 } 2045 2046 dp = (struct dccpcb *)inp->inp_ppcb; 2047 #else /* NetBSD */ 2048 #ifdef INET6 2049 if (so->so_proto->pr_domain->dom_family == AF_INET6) { 2050 in6p = sotoin6pcb(so); 2051 if (in6p == 0) { 2052 INP_INFO_WUNLOCK(&dccpbinfo); 2053 return EINVAL; 2054 } 2055 dp = (struct dccpcb *)in6p->in6p_ppcb; 2056 } else 2057 #endif 2058 { 2059 inp = sotoinpcb(so); 2060 if (inp == 0) { 2061 return EINVAL; 2062 } 2063 dp = (struct dccpcb *)inp->inp_ppcb; 2064 } 2065 #endif 2066 if (!dccp_disconnect2(dp)) { 2067 INP_UNLOCK(inp); 2068 } 2069 INP_INFO_WUNLOCK(&dccpbinfo); 2070 return 0; 2071 } 2072 2073 /* 2074 * If we have don't have a established connection 2075 * we can call dccp_close, otherwise we can just 2076 * set SS_ISDISCONNECTED and flush the receive queue. 2077 */ 2078 static int 2079 dccp_disconnect2(struct dccpcb *dp) 2080 { 2081 struct socket *so = dptosocket(dp); 2082 2083 DCCP_DEBUG((LOG_INFO, "Entering dccp_disconnect2!\n")); 2084 2085 if (dp->state < DCCPS_ESTAB) { 2086 dccp_close(dp); 2087 return 1; 2088 } else { 2089 soisdisconnecting(so); 2090 sbflush(&so->so_rcv); 2091 if (dp->state == DCCPS_ESTAB) { 2092 dp->retrans = 100; 2093 callout_reset(&dp->retrans_timer, dp->retrans, 2094 dccp_retrans_t, dp); 2095 callout_reset(&dp->close_timer, DCCP_CLOSE_TIMER, 2096 dccp_close_t, dp); 2097 if (dp->who == DCCP_CLIENT) { 2098 dp->state = DCCPS_CLIENT_CLOSE; 2099 } else { 2100 dp->state = DCCPS_SERVER_CLOSE; 2101 } 2102 dccp_output(dp, 0); 2103 } 2104 } 2105 return 0; 2106 } 2107 2108 int 2109 dccp_send(struct socket *so, struct mbuf *m, struct sockaddr *addr, 2110 struct mbuf *control, struct lwp *l) 2111 { 2112 struct inpcb *inp; 2113 struct dccpcb *dp; 2114 int error = 0; 2115 int isipv6 = 0; 2116 2117 DCCP_DEBUG((LOG_INFO, "Entering dccp_send!\n")); 2118 KASSERT(solocked(so)); 2119 KASSERT(m != NULL); 2120 2121 if (control && control->m_len) { 2122 m_freem(control); 2123 m_freem(m); 2124 return EINVAL; 2125 } 2126 2127 #ifdef INET6 2128 isipv6 = addr && addr->sa_family == AF_INET6; 2129 #endif 2130 2131 #if defined(INET6) 2132 if (so->so_proto->pr_domain->dom_family == AF_INET6) { 2133 struct in6pcb *in6p; 2134 in6p = sotoin6pcb(so); 2135 if (in6p == 0) { 2136 error = EINVAL; 2137 goto release; 2138 } 2139 dp = (struct dccpcb *)in6p->in6p_ppcb; 2140 } else 2141 #endif 2142 { 2143 INP_INFO_WLOCK(&dccpbinfo); 2144 inp = sotoinpcb(so); 2145 if (inp == 0) { 2146 error = EINVAL; 2147 goto release; 2148 } 2149 INP_LOCK(inp); 2150 dp = (struct dccpcb *)inp->inp_ppcb; 2151 } 2152 if (dp->state != DCCPS_ESTAB) { 2153 DCCP_DEBUG((LOG_INFO, "We have no established connection!\n")); 2154 } 2155 2156 if (control != NULL) { 2157 DCCP_DEBUG((LOG_INFO, "We got a control message!\n")); 2158 /* Are we going to use control messages??? */ 2159 if (control->m_len) { 2160 m_freem(control); 2161 } 2162 } 2163 2164 if (sbspace(&so->so_snd) < -512) { 2165 INP_UNLOCK(inp); 2166 error = ENOBUFS; 2167 goto release; 2168 } 2169 2170 if (m->m_pkthdr.len > dp->d_maxseg) { 2171 /* XXX we should calculate packet size more carefully */ 2172 INP_UNLOCK(inp); 2173 error = EINVAL; 2174 goto release; 2175 } 2176 2177 if (dp->pktcnt >= DCCP_MAX_PKTS) { 2178 INP_UNLOCK(inp); 2179 error = ENOBUFS; 2180 goto release; 2181 } 2182 2183 sbappend(&so->so_snd, m); 2184 dp->pktlen[(dp->pktlenidx + dp->pktcnt) % DCCP_MAX_PKTS] = m->m_pkthdr.len; 2185 dp->pktcnt ++; 2186 2187 if (addr && dp->state == DCCPS_CLOSED) { 2188 error = dccp_doconnect(so, addr, l, isipv6); 2189 if (error) 2190 goto out; 2191 } 2192 2193 error = dccp_output(dp, 0); 2194 2195 out: 2196 INP_UNLOCK(inp); 2197 INP_INFO_WUNLOCK(&dccpbinfo); 2198 return error; 2199 2200 release: 2201 INP_INFO_WUNLOCK(&dccpbinfo); 2202 m_freem(m); 2203 return (error); 2204 } 2205 2206 /* 2207 * Sets socket to SS_CANTSENDMORE 2208 */ 2209 int 2210 dccp_shutdown(struct socket *so) 2211 { 2212 struct inpcb *inp; 2213 2214 DCCP_DEBUG((LOG_INFO, "Entering dccp_shutdown!\n")); 2215 INP_INFO_RLOCK(&dccpbinfo); 2216 inp = sotoinpcb(so); 2217 if (inp == 0) { 2218 INP_INFO_RUNLOCK(&dccpbinfo); 2219 return EINVAL; 2220 } 2221 INP_LOCK(inp); 2222 INP_INFO_RUNLOCK(&dccpbinfo); 2223 socantsendmore(so); 2224 INP_UNLOCK(inp); 2225 return 0; 2226 } 2227 2228 static int 2229 dccp_listen(struct socket *so, struct lwp *td) 2230 { 2231 struct inpcb *inp; 2232 struct dccpcb *dp; 2233 int error = 0; 2234 2235 DCCP_DEBUG((LOG_INFO, "Entering dccp_listen!\n")); 2236 2237 INP_INFO_RLOCK(&dccpbinfo); 2238 inp = sotoinpcb(so); 2239 if (inp == 0) { 2240 INP_INFO_RUNLOCK(&dccpbinfo); 2241 return EINVAL; 2242 } 2243 INP_LOCK(inp); 2244 INP_INFO_RUNLOCK(&dccpbinfo); 2245 dp = (struct dccpcb *)inp->inp_ppcb; 2246 if (inp->inp_lport == 0) 2247 error = in_pcbbind(inp, NULL, td); 2248 if (error == 0) { 2249 dp->state = DCCPS_LISTEN; 2250 dp->who = DCCP_LISTENER; 2251 } 2252 INP_UNLOCK(inp); 2253 return error; 2254 } 2255 2256 /* 2257 * Accepts a connection (accept system call) 2258 */ 2259 static int 2260 dccp_accept(struct socket *so, struct sockaddr *nam) 2261 { 2262 struct inpcb *inp = NULL; 2263 int error = 0; 2264 2265 DCCP_DEBUG((LOG_INFO, "Entering dccp_accept!\n")); 2266 2267 if (nam == NULL) { 2268 return EINVAL; 2269 } 2270 if (so->so_state & SS_ISDISCONNECTED) { 2271 DCCP_DEBUG((LOG_INFO, "so_state && SS_ISDISCONNECTED!, so->state = %i\n", so->so_state)); 2272 return ECONNABORTED; 2273 } 2274 2275 INP_INFO_RLOCK(&dccpbinfo); 2276 inp = sotoinpcb(so); 2277 if (inp == 0) { 2278 INP_INFO_RUNLOCK(&dccpbinfo); 2279 return EINVAL; 2280 } 2281 INP_LOCK(inp); 2282 INP_INFO_RUNLOCK(&dccpbinfo); 2283 in_setpeeraddr(inp, (struct sockaddr_in *)nam); 2284 2285 return error; 2286 } 2287 2288 /* 2289 * Initializes a new DCCP control block 2290 * (in_pcballoc in attach has already allocated memory for it) 2291 */ 2292 struct dccpcb * 2293 dccp_newdccpcb(int family, void *aux) 2294 { 2295 struct inpcb *inp; 2296 struct in6pcb *in6p; 2297 struct dccpcb *dp; 2298 2299 DCCP_DEBUG((LOG_INFO, "Creating a new dccpcb!\n")); 2300 2301 dp = pool_get(&dccpcb_pool, PR_NOWAIT); 2302 if (dp == NULL) 2303 return NULL; 2304 memset((char *) dp, 0, sizeof(struct dccpcb)); 2305 2306 callout_init(&dp->connect_timer, 0); 2307 callout_init(&dp->retrans_timer, 0); 2308 callout_init(&dp->close_timer, 0); 2309 callout_init(&dp->timewait_timer, 0); 2310 2311 dp->ndp = 0; 2312 dp->loss_window = 1000; 2313 dp->cslen = 0; 2314 dp->pref_cc = DEFAULT_CCID; 2315 dp->who = DCCP_UNDEF; 2316 dp->seq_snd = 0; 2317 dp->seq_rcv = 0; 2318 dp->shortseq = 0; 2319 dp->gsn_rcv = 281474976710656LL; 2320 dp->optlen = 0; 2321 if (dccp_do_feature_nego){ 2322 dp->cc_in_use[0] = -1; 2323 dp->cc_in_use[1] = -1; 2324 } else { 2325 /* for compatibility with linux */ 2326 dp->cc_in_use[0] = 4; 2327 dp->cc_in_use[1] = 4; 2328 } 2329 dp->av_size = 0; /* no ack vector initially */ 2330 dp->remote_ackvector = 0; /* no ack vector on remote side initially */ 2331 dp->retrans = 200; 2332 dp->avgpsize = 0; 2333 dp->d_maxseg = 1400; 2334 dp->ref_pseq.hi = 0; 2335 dp->ref_pseq.lo = 0; 2336 dp->pktlenidx = 0; 2337 dp->pktcnt = 0; 2338 2339 switch (family) { 2340 case PF_INET: 2341 inp = (struct inpcb *)aux; 2342 dp->d_inpcb = inp; 2343 inp->inp_ip.ip_ttl = ip_defttl; 2344 inp->inp_ppcb = dp; 2345 break; 2346 case PF_INET6: 2347 in6p = (struct in6pcb *)aux; 2348 dp->d_in6pcb = in6p; 2349 in6p->in6p_ip6.ip6_hlim = in6_selecthlim_rt(in6p); 2350 in6p->in6p_ppcb = dp; 2351 break; 2352 } 2353 2354 if (!dccp_do_feature_nego){ 2355 dp->cc_state[0] = (*cc_sw[4].cc_send_init)(dp); 2356 dp->cc_state[1] = (*cc_sw[4].cc_recv_init)(dp); 2357 } 2358 2359 return dp; 2360 } 2361 2362 int 2363 dccp_add_option(struct dccpcb *dp, u_int8_t opt, char *val, u_int8_t val_len) 2364 { 2365 return dccp_add_feature_option(dp, opt, 0, val, val_len); 2366 } 2367 2368 int 2369 dccp_add_feature_option(struct dccpcb *dp, u_int8_t opt, u_int8_t feature, char *val, u_int8_t val_len) 2370 { 2371 int i; 2372 DCCP_DEBUG((LOG_INFO, "Entering dccp_add_feature_option, opt = %u, val_len = %u optlen %u\n", opt, val_len, dp->optlen)); 2373 2374 if (DCCP_MAX_OPTIONS > (dp->optlen + val_len + 2)) { 2375 dp->options[dp->optlen] = opt; 2376 if (opt < 32) { 2377 dp->optlen++; 2378 } else { 2379 if (opt == DCCP_OPT_CONFIRM_L && val_len) { 2380 dp->options[dp->optlen + 1] = val_len + 3; 2381 dp->options[dp->optlen +2] = feature; 2382 dp->optlen += 3; 2383 } else { 2384 dp->options[dp->optlen + 1] = val_len + 2; 2385 dp->optlen += 2; 2386 } 2387 2388 for (i = 0; i<val_len; i++) { 2389 dp->options[dp->optlen] = val[i]; 2390 dp->optlen++; 2391 } 2392 } 2393 } else { 2394 DCCP_DEBUG((LOG_INFO, "No room for more options, optlen = %u\n", dp->optlen)); 2395 return -1; 2396 } 2397 2398 return 0; 2399 } 2400 2401 /* 2402 * Searches "options" for given option type. if found, the data is copied to buffer 2403 * and returns the data length. 2404 * Returns 0 if option type not found 2405 */ 2406 int 2407 dccp_get_option(char *options, int optlen, int type, char *buffer, int buflen) 2408 { 2409 int i, j, size; 2410 u_int8_t t; 2411 2412 for (i=0; i < optlen;) { 2413 t = options[i++]; 2414 if (t >= 32) { 2415 size = options[i++] - 2; 2416 if (t == type) { 2417 if (size > buflen) 2418 return 0; 2419 for (j = 0; j < size; j++) 2420 buffer[j] = options[i++]; 2421 return size; 2422 } 2423 i += size; 2424 } 2425 } 2426 /* If we get here the options was not found */ 2427 DCCP_DEBUG((LOG_INFO, "dccp_get_option option(%d) not found\n", type)); 2428 return 0; 2429 } 2430 2431 void 2432 dccp_parse_options(struct dccpcb *dp, char *options, int optlen) 2433 { 2434 u_int8_t opt, size, i, j; 2435 char val[8]; 2436 2437 for (i = 0; i < optlen; i++) { 2438 opt = options[i]; 2439 2440 DCCP_DEBUG((LOG_INFO, "Parsing opt: 0x%02x\n", opt)); 2441 2442 if (opt < 32) { 2443 switch (opt) { 2444 case DCCP_OPT_PADDING: 2445 DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_PADDING!\n")); 2446 break; 2447 case DCCP_OPT_DATA_DISCARD: 2448 DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_DATA_DISCARD!\n")); 2449 break; 2450 case DCCP_OPT_SLOW_RECV: 2451 DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_SLOW_RECV!\n")); 2452 break; 2453 case DCCP_OPT_BUF_CLOSED: 2454 DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_BUF_CLOSED!\n")); 2455 break; 2456 default: 2457 DCCP_DEBUG((LOG_INFO, "Got an unknown option, option = %u!\n", opt)); 2458 } 2459 } else if (opt > 32 && opt < 36) { 2460 size = options[i+ 1]; 2461 if (size < 3 || size > 10) { 2462 DCCP_DEBUG((LOG_INFO, "Error, option size = %u\n", size)); 2463 return; 2464 } 2465 /* Feature negotiations are options 33 to 35 */ 2466 DCCP_DEBUG((LOG_INFO, "Got option %u, size = %u, feature = %u\n", opt, size, options[i+2])); 2467 memcpy(val, options + i + 3, size -3); 2468 DCCP_DEBUG((LOG_INFO, "Calling dccp_feature neg(%u, %u, options[%u + 1], %u)!\n", (u_int)dp, opt, i+ 1, (size - 3))); 2469 dccp_feature_neg(dp, opt, options[i+2], (size -3) , val); 2470 i += size - 1; 2471 2472 } else if (opt < 128) { 2473 size = options[i+ 1]; 2474 if (size < 3 || size > 10) { 2475 DCCP_DEBUG((LOG_INFO, "Error, option size = %u\n", size)); 2476 return; 2477 } 2478 2479 switch (opt) { 2480 case DCCP_OPT_RECV_BUF_DROPS: 2481 DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_RECV_BUF_DROPS, size = %u!\n", size)); 2482 for (j=2; j < size; j++) { 2483 DCCP_DEBUG((LOG_INFO, "val[%u] = %u ", j-1, options[i+j])); 2484 } 2485 DCCP_DEBUG((LOG_INFO, "\n")); 2486 break; 2487 2488 case DCCP_OPT_TIMESTAMP: 2489 DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_TIMESTAMP, size = %u\n", size)); 2490 2491 /* Adding TimestampEcho to next outgoing */ 2492 memcpy(val, options + i + 2, 4); 2493 memset(val + 4, 0, 4); 2494 dccp_add_option(dp, DCCP_OPT_TIMESTAMP_ECHO, val, 8); 2495 break; 2496 2497 case DCCP_OPT_TIMESTAMP_ECHO: 2498 DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_TIMESTAMP_ECHO, size = %u\n",size)); 2499 for (j=2; j < size; j++) { 2500 DCCP_DEBUG((LOG_INFO, "val[%u] = %u ", j-1, options[i+j])); 2501 } 2502 DCCP_DEBUG((LOG_INFO, "\n")); 2503 2504 /* 2505 memcpy(&(dp->timestamp_echo), options + i + 2, 4); 2506 memcpy(&(dp->timestamp_elapsed), options + i + 6, 4); 2507 ACK_DEBUG((LOG_INFO, "DATA; echo = %u , elapsed = %u\n", 2508 dp->timestamp_echo, dp->timestamp_elapsed)); 2509 */ 2510 2511 break; 2512 2513 case DCCP_OPT_ACK_VECTOR0: 2514 case DCCP_OPT_ACK_VECTOR1: 2515 case DCCP_OPT_ELAPSEDTIME: 2516 /* Dont do nothing here. Let the CC deal with it */ 2517 break; 2518 2519 default: 2520 DCCP_DEBUG((LOG_INFO, "Got an unknown option, option = %u, size = %u!\n", opt, size)); 2521 break; 2522 2523 } 2524 i += size - 1; 2525 2526 } else { 2527 DCCP_DEBUG((LOG_INFO, "Got a CCID option (%d), do nothing!\n", opt)); 2528 size = options[i+ 1]; 2529 if (size < 3 || size > 10) { 2530 DCCP_DEBUG((LOG_INFO, "Error, option size = %u\n", size)); 2531 return; 2532 } 2533 i += size - 1; 2534 } 2535 } 2536 2537 } 2538 2539 int 2540 dccp_add_feature(struct dccpcb *dp, u_int8_t opt, u_int8_t feature, char *val, u_int8_t val_len) 2541 { 2542 int i; 2543 DCCP_DEBUG((LOG_INFO, "Entering dccp_add_feature, opt = %u, feature = %u, val_len = %u\n", opt, feature, val_len)); 2544 2545 if (DCCP_MAX_OPTIONS > (dp->featlen + val_len + 3)) { 2546 dp->features[dp->featlen] = opt; 2547 dp->features[dp->featlen + 1] = val_len + 3; 2548 dp->features[dp->featlen +2] = feature; 2549 dp->featlen += 3; 2550 for (i = 0; i<val_len; i++) { 2551 dp->features[dp->featlen] = val[i]; 2552 dp->featlen++; 2553 } 2554 } else { 2555 DCCP_DEBUG((LOG_INFO, "No room for more features, featlen = %u\n", dp->featlen)); 2556 return -1; 2557 } 2558 2559 return 0; 2560 } 2561 2562 int 2563 dccp_remove_feature(struct dccpcb *dp, u_int8_t opt, u_int8_t feature) 2564 { 2565 int i = 0, j = 0, k; 2566 u_int8_t t_opt, t_feature, len; 2567 DCCP_DEBUG((LOG_INFO, "Entering dccp_remove_feature, featlen = %u, opt = %u, feature = %u\n", dp->featlen, opt, feature)); 2568 2569 while (i < dp->featlen) { 2570 t_opt = dp->features[i]; 2571 len = dp->features[i+ 1]; 2572 2573 if (i + len > dp->featlen) { 2574 DCCP_DEBUG((LOG_INFO, "Error, len = %u and i(%u) + len > dp->featlen (%u)\n", len, i, dp->featlen)); 2575 return 1; 2576 } 2577 t_feature = dp->features[i+2]; 2578 2579 if (t_opt == opt && t_feature == feature) { 2580 i += len; 2581 } else { 2582 if (i != j) { 2583 for (k = 0; k < len; k++) { 2584 dp->features[j+k] = dp->features[i+k]; 2585 } 2586 } 2587 i += len; 2588 j += len; 2589 } 2590 } 2591 dp->featlen = j; 2592 DCCP_DEBUG((LOG_INFO, "Exiting dccp_remove_feature, featlen = %u\n", dp->featlen)); 2593 return 0; 2594 } 2595 2596 void 2597 dccp_feature_neg(struct dccpcb *dp, u_int8_t opt, u_int8_t feature, u_int8_t val_len, char *val) 2598 { 2599 DCCP_DEBUG((LOG_INFO, "Running dccp_feature_neg, opt = %u, feature = %u len = %u ", opt, feature, val_len)); 2600 2601 switch (feature) { 2602 case DCCP_FEATURE_CC: 2603 DCCP_DEBUG((LOG_INFO, "Got CCID negotiation, opt = %u, val[0] = %u\n", opt, val[0])); 2604 if (opt == DCCP_OPT_CHANGE_R) { 2605 if (val[0] == 2 || val[0] == 3 || val[0] == 0) { 2606 /* try to use preferable CCID */ 2607 int i; 2608 for (i = 1; i < val_len; i ++) if (val[i] == dp->pref_cc) val[0] = dp->pref_cc; 2609 DCCP_DEBUG((LOG_INFO, "Sending DCCP_OPT_CONFIRM_L on CCID %u\n", val[0])); 2610 dccp_remove_feature(dp, DCCP_OPT_CONFIRM_L, DCCP_FEATURE_CC); 2611 dccp_add_feature_option(dp, DCCP_OPT_CONFIRM_L, DCCP_FEATURE_CC , val, 1); 2612 if (dp->cc_in_use[0] < 1) { 2613 dp->cc_state[0] = (*cc_sw[val[0] + 1].cc_send_init)(dp); 2614 dp->cc_in_use[0] = val[0] + 1; 2615 } else { 2616 DCCP_DEBUG((LOG_INFO, "We already have negotiated a CC!!!\n")); 2617 } 2618 } 2619 } else if (opt == DCCP_OPT_CONFIRM_L) { 2620 DCCP_DEBUG((LOG_INFO, "Got DCCP_OPT_CONFIRM_L on CCID %u\n", val[0])); 2621 dccp_remove_feature(dp, DCCP_OPT_CHANGE_R, DCCP_FEATURE_CC); 2622 if (dp->cc_in_use[1] < 1) { 2623 dp->cc_state[1] = (*cc_sw[val[0] + 1].cc_recv_init)(dp); 2624 dp->cc_in_use[1] = val[0] + 1; 2625 DCCP_DEBUG((LOG_INFO, "confirmed cc_in_use[1] = %d\n", dp->cc_in_use[1])); 2626 } else { 2627 DCCP_DEBUG((LOG_INFO, "We already have negotiated a CC!!! (confirm) %d\n", dp->cc_in_use[1])); 2628 } 2629 } 2630 2631 break; 2632 2633 case DCCP_FEATURE_ACKVECTOR: 2634 ACK_DEBUG((LOG_INFO, "Got _Use Ack Vector_\n")); 2635 if (opt == DCCP_OPT_CHANGE_R) { 2636 if (val[0] == 1) { 2637 dccp_use_ackvector(dp); 2638 dccp_remove_feature(dp, DCCP_OPT_CONFIRM_L, DCCP_FEATURE_ACKVECTOR); 2639 dccp_add_feature_option(dp, DCCP_OPT_CONFIRM_L, DCCP_FEATURE_ACKVECTOR , val, 1); 2640 } else { 2641 ACK_DEBUG((LOG_INFO, "ERROR. Strange val %u\n", val[0])); 2642 } 2643 } else if (opt == DCCP_OPT_CONFIRM_L) { 2644 dccp_remove_feature(dp, DCCP_OPT_CONFIRM_L, DCCP_FEATURE_ACKVECTOR); 2645 if (val[0] == 1) { 2646 dp->remote_ackvector = 1; 2647 ACK_DEBUG((LOG_INFO,"Remote side confirmed AckVector usage\n")); 2648 } else { 2649 ACK_DEBUG((LOG_INFO, "ERROR. Strange val %u\n", val[0])); 2650 } 2651 } 2652 break; 2653 2654 case DCCP_FEATURE_ACKRATIO: 2655 if (opt == DCCP_OPT_CHANGE_R) { 2656 memcpy(&(dp->ack_ratio), val, 1); 2657 ACK_DEBUG((LOG_INFO, "Feature: Change Ack Ratio to %u\n", dp->ack_ratio)); 2658 } 2659 break; 2660 2661 case DCCP_FEATURE_ECN: 2662 case DCCP_FEATURE_MOBILITY: 2663 default: 2664 /* we should send back empty CONFIRM_L for unknown feature unless it's not mandatory */ 2665 dccp_add_option(dp, DCCP_OPT_CONFIRM_L, NULL, 0); 2666 break; 2667 2668 } 2669 } 2670 2671 #ifdef __FreeBSD__ 2672 static int 2673 dccp_pcblist(SYSCTL_HANDLER_ARGS) 2674 { 2675 2676 int error, i, n, s; 2677 struct inpcb *inp, **inp_list; 2678 inp_gen_t gencnt; 2679 struct xinpgen xig; 2680 2681 /* 2682 * The process of preparing the TCB list is too time-consuming and 2683 * resource-intensive to repeat twice on every request. 2684 */ 2685 if (req->oldptr == 0) { 2686 n = dccpbinfo.ipi_count; 2687 req->oldidx = 2 * (sizeof xig) 2688 + (n + n/8) * sizeof(struct xdccpcb); 2689 return 0; 2690 } 2691 2692 2693 if (req->newptr != 0) 2694 return EPERM; 2695 2696 2697 /* 2698 * OK, now we're committed to doing something. 2699 */ 2700 s = splnet(); 2701 gencnt = dccpbinfo.ipi_gencnt; 2702 n = dccpbinfo.ipi_count; 2703 splx(s); 2704 2705 #if __FreeBSD_version >= 500000 2706 sysctl_wire_old_buffer(req, 2 * (sizeof xig) 2707 + n * sizeof(struct xdccpcb)); 2708 #endif 2709 2710 xig.xig_len = sizeof xig; 2711 xig.xig_count = n; 2712 xig.xig_gen = gencnt; 2713 xig.xig_sogen = so_gencnt; 2714 error = SYSCTL_OUT(req, &xig, sizeof xig); 2715 if (error) 2716 return error; 2717 2718 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 2719 if (inp_list == 0) 2720 return ENOMEM; 2721 2722 s = splsoftnet(); 2723 INP_INFO_RLOCK(&dccpbinfo); 2724 2725 for (inp = LIST_FIRST(dccpbinfo.listhead), i = 0; inp && i < n; 2726 inp = LIST_NEXT(inp, inp_list)) { 2727 INP_LOCK(inp); 2728 if (inp->inp_gencnt <= gencnt && 2729 #if __FreeBSD_version >= 500000 2730 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) 2731 #else 2732 !prison_xinpcb(req->p, inp)) 2733 #endif 2734 inp_list[i++] = inp; 2735 INP_UNLOCK(inp); 2736 } 2737 INP_INFO_RUNLOCK(&dccpbinfo); 2738 splx(s); 2739 n = i; 2740 2741 error = 0; 2742 for (i = 0; i < n; i++) { 2743 inp = inp_list[i]; 2744 INP_LOCK(inp); 2745 2746 if (inp->inp_gencnt <= gencnt) { 2747 struct xdccpcb xd; 2748 vaddr_t inp_ppcb; 2749 xd.xd_len = sizeof xd; 2750 /* XXX should avoid extra copy */ 2751 memcpy(&xd.xd_inp, inp, sizeof *inp); 2752 inp_ppcb = inp->inp_ppcb; 2753 if (inp_ppcb != NULL) 2754 memcpy(&xd.xd_dp, inp_ppcb, sizeof xd.xd_dp); 2755 else 2756 memset((char *) &xd.xd_dp, 0, sizeof xd.xd_dp); 2757 if (inp->inp_socket) 2758 sotoxsocket(inp->inp_socket, &xd.xd_socket); 2759 error = SYSCTL_OUT(req, &xd, sizeof xd); 2760 } 2761 INP_UNLOCK(inp); 2762 } 2763 if (!error) { 2764 /* 2765 * Give the user an updated idea of our state. 2766 * If the generation differs from what we told 2767 * her before, she knows that something happened 2768 * while we were processing this request, and it 2769 * might be necessary to retry. 2770 */ 2771 s = splnet(); 2772 INP_INFO_RLOCK(&dccpbinfo); 2773 xig.xig_gen = dccpbinfo.ipi_gencnt; 2774 xig.xig_sogen = so_gencnt; 2775 xig.xig_count = dccpbinfo.ipi_count; 2776 2777 2778 INP_INFO_RUNLOCK(&dccpbinfo); 2779 splx(s); 2780 error = SYSCTL_OUT(req, &xig, sizeof xig); 2781 } 2782 free(inp_list, M_TEMP); 2783 return error; 2784 } 2785 #endif 2786 2787 #ifdef __FreeBSD__ 2788 SYSCTL_PROC(_net_inet_dccp, DCCPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 2789 dccp_pcblist, "S,xdccpcb", "List of active DCCP sockets"); 2790 #endif 2791 2792 void 2793 dccp_timewait_t(void *dcb) 2794 { 2795 struct dccpcb *dp = dcb; 2796 2797 DCCP_DEBUG((LOG_INFO, "Entering dccp_timewait_t!\n")); 2798 mutex_enter(softnet_lock); 2799 INP_INFO_WLOCK(&dccpbinfo); 2800 INP_LOCK(dp->d_inpcb); 2801 dccp_close(dp); 2802 INP_INFO_WUNLOCK(&dccpbinfo); 2803 mutex_exit(softnet_lock); 2804 } 2805 2806 void 2807 dccp_connect_t(void *dcb) 2808 { 2809 struct dccpcb *dp = dcb; 2810 2811 DCCP_DEBUG((LOG_INFO, "Entering dccp_connect_t!\n")); 2812 mutex_enter(softnet_lock); 2813 INP_INFO_WLOCK(&dccpbinfo); 2814 INP_LOCK(dp->d_inpcb); 2815 dccp_close(dp); 2816 INP_INFO_WUNLOCK(&dccpbinfo); 2817 mutex_exit(softnet_lock); 2818 } 2819 2820 void 2821 dccp_close_t(void *dcb) 2822 { 2823 struct dccpcb *dp = dcb; 2824 2825 DCCP_DEBUG((LOG_INFO, "Entering dccp_close_t!\n")); 2826 mutex_enter(softnet_lock); 2827 INP_INFO_WLOCK(&dccpbinfo); 2828 dp->state = DCCPS_TIME_WAIT; /* HMM */ 2829 if (dp->who == DCCP_SERVER) { 2830 INP_LOCK(dp->d_inpcb); 2831 KERNEL_LOCK(1, NULL); 2832 dccp_output(dp, DCCP_TYPE_RESET + 2); 2833 KERNEL_UNLOCK_ONE(NULL); 2834 dccp_close(dp); 2835 } else { 2836 INP_LOCK(dp->d_inpcb); 2837 dccp_output(dp, DCCP_TYPE_RESET + 2); 2838 /*dp->state = DCCPS_TIME_WAIT; */ 2839 callout_reset(&dp->timewait_timer, DCCP_TIMEWAIT_TIMER, 2840 dccp_timewait_t, dp); 2841 INP_UNLOCK(dp->d_inpcb); 2842 } 2843 INP_INFO_WUNLOCK(&dccpbinfo); 2844 mutex_exit(softnet_lock); 2845 } 2846 2847 void 2848 dccp_retrans_t(void *dcb) 2849 { 2850 struct dccpcb *dp = dcb; 2851 /*struct inpcb *inp;*/ 2852 2853 DCCP_DEBUG((LOG_INFO, "Entering dccp_retrans_t!\n")); 2854 mutex_enter(softnet_lock); 2855 INP_INFO_RLOCK(&dccpbinfo); 2856 /*inp = dp->d_inpcb;*/ 2857 INP_LOCK(inp); 2858 INP_INFO_RUNLOCK(&dccpbinfo); 2859 callout_stop(&dp->retrans_timer); 2860 KERNEL_LOCK(1, NULL); 2861 dccp_output(dp, 0); 2862 KERNEL_UNLOCK_ONE(NULL); 2863 dp->retrans = dp->retrans * 2; 2864 callout_reset(&dp->retrans_timer, dp->retrans, dccp_retrans_t, dp); 2865 INP_UNLOCK(inp); 2866 mutex_exit(softnet_lock); 2867 } 2868 2869 static int 2870 dccp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp) 2871 { 2872 int error = 0; 2873 int family; 2874 2875 family = so->so_proto->pr_domain->dom_family; 2876 switch (family) { 2877 case PF_INET: 2878 error = in_control(so, cmd, nam, ifp); 2879 break; 2880 #ifdef INET6 2881 case PF_INET6: 2882 error = in6_control(so, cmd, nam, ifp); 2883 break; 2884 #endif 2885 default: 2886 error = EAFNOSUPPORT; 2887 } 2888 return (error); 2889 } 2890 2891 static int 2892 dccp_stat(struct socket *so, struct stat *ub) 2893 { 2894 return 0; 2895 } 2896 2897 static int 2898 dccp_peeraddr(struct socket *so, struct sockaddr *nam) 2899 { 2900 2901 KASSERT(solocked(so)); 2902 KASSERT(sotoinpcb(so) != NULL); 2903 KASSERT(nam != NULL); 2904 2905 in_setpeeraddr(sotoinpcb(so), (struct sockaddr_in *)nam); 2906 return 0; 2907 } 2908 2909 static int 2910 dccp_sockaddr(struct socket *so, struct sockaddr *nam) 2911 { 2912 2913 KASSERT(solocked(so)); 2914 KASSERT(sotoinpcb(so) != NULL); 2915 KASSERT(nam != NULL); 2916 2917 in_setsockaddr(sotoinpcb(so), (struct sockaddr_in *)nam); 2918 return 0; 2919 } 2920 2921 static int 2922 dccp_rcvd(struct socket *so, int flags, struct lwp *l) 2923 { 2924 KASSERT(solocked(so)); 2925 2926 return EOPNOTSUPP; 2927 } 2928 2929 static int 2930 dccp_recvoob(struct socket *so, struct mbuf *m, int flags) 2931 { 2932 KASSERT(solocked(so)); 2933 2934 return EOPNOTSUPP; 2935 } 2936 2937 static int 2938 dccp_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control) 2939 { 2940 KASSERT(solocked(so)); 2941 2942 m_freem(m); 2943 m_freem(control); 2944 2945 return EOPNOTSUPP; 2946 } 2947 2948 static int 2949 dccp_purgeif(struct socket *so, struct ifnet *ifp) 2950 { 2951 int s; 2952 2953 s = splsoftnet(); 2954 mutex_enter(softnet_lock); 2955 in_pcbpurgeif0(&dccpbtable, ifp); 2956 in_purgeif(ifp); 2957 in_pcbpurgeif(&dccpbtable, ifp); 2958 mutex_exit(softnet_lock); 2959 splx(s); 2960 2961 return 0; 2962 } 2963 2964 /****** Ack Vector functions *********/ 2965 2966 /** 2967 * Initialize and allocate mem for Ack Vector 2968 **/ 2969 void 2970 dccp_use_ackvector(struct dccpcb *dp) 2971 { 2972 DCCP_DEBUG((LOG_INFO,"Initializing AckVector\n")); 2973 if (dp->ackvector != 0) { 2974 DCCP_DEBUG((LOG_INFO, "It was already initialized!!!\n")); 2975 return; 2976 } 2977 dp->av_size = DCCP_VECTORSIZE; 2978 /* need 2 bits per entry */ 2979 dp->ackvector = malloc(dp->av_size/4, M_PCB, M_NOWAIT | M_ZERO); 2980 if (dp->ackvector == 0) { 2981 DCCP_DEBUG((LOG_INFO, "Unable to allocate memory for ackvector\n")); 2982 /* What to do now? */ 2983 dp->av_size = 0; 2984 return; 2985 } 2986 memset(dp->ackvector, 0xff, dp->av_size/4); 2987 dp->av_hs = dp->av_ts = 0; 2988 dp->av_hp = dp->ackvector; 2989 } 2990 2991 /** 2992 * Set 'seqnr' as the new head in ackvector 2993 **/ 2994 void 2995 dccp_update_ackvector(struct dccpcb *dp, u_int64_t seqnr) 2996 { 2997 int64_t gap; 2998 u_char *t; 2999 3000 /* Ignore wrapping for now */ 3001 3002 ACK_DEBUG((LOG_INFO,"New head in ackvector: %u\n", seqnr)); 3003 3004 if (dp->av_size == 0) { 3005 ACK_DEBUG((LOG_INFO, "Update: AckVector NOT YET INITIALIZED!!!\n")); 3006 dccp_use_ackvector(dp); 3007 } 3008 3009 if (seqnr > dp->av_hs) { 3010 gap = seqnr - dp->av_hs; 3011 } else { 3012 /* We received obsolete information */ 3013 return; 3014 } 3015 3016 t = dp->av_hp + (gap/4); 3017 if (t >= (dp->ackvector + (dp->av_size/4))) 3018 t -= (dp->av_size / 4); /* ackvector wrapped */ 3019 dp->av_hp = t; 3020 dp->av_hs = seqnr; 3021 } 3022 3023 /** 3024 * We've received a packet. store in local av so it's included in 3025 * next Ack Vector sent 3026 **/ 3027 void 3028 dccp_increment_ackvector(struct dccpcb *dp, u_int64_t seqnr) 3029 { 3030 u_int64_t offset, dc; 3031 int64_t gap; 3032 u_char *t, *n; 3033 3034 DCCP_DEBUG((LOG_INFO, "Entering dccp_increment_ackvecktor %d\n", dp->av_size)); 3035 if (dp->av_size == 0) { 3036 DCCP_DEBUG((LOG_INFO, "Increment: AckVector NOT YET INITIALIZED!!!\n")); 3037 dccp_use_ackvector(dp); 3038 } 3039 3040 if (dp->av_hs == dp->av_ts) { 3041 /* Empty ack vector */ 3042 dp->av_hs = dp->av_ts = seqnr; 3043 } 3044 3045 /* Check for wrapping */ 3046 if (seqnr >= dp->av_hs) { 3047 /* Not wrapped */ 3048 gap = seqnr - dp->av_hs; 3049 } else { 3050 /* Wrapped */ 3051 gap = seqnr + 0x1000000000000LL - dp->av_hs; /* seqnr = 48 bits */ 3052 } 3053 DCCP_DEBUG((LOG_INFO, "dccp_increment_ackvecktor gap=%llu av_size %d\n", gap, dp->av_size)); 3054 3055 if (gap >= dp->av_size) { 3056 /* gap is bigger than ackvector size? baaad */ 3057 /* maybe we should increase the ackvector here */ 3058 DCCP_DEBUG((LOG_INFO, "increment_ackvector error. gap: %llu, av_size: %d, seqnr: %d\n", 3059 gap, dp->av_size, seqnr)); 3060 return; 3061 } 3062 3063 offset = gap % 4; /* hi or low 2 bits to mark */ 3064 t = dp->av_hp + (gap/4); 3065 if (t >= (dp->ackvector + (dp->av_size/4))) 3066 t -= (dp->av_size / 4); /* ackvector wrapped */ 3067 3068 *t = *t & (~(0x03 << (offset *2))); /* turn off bits, 00 is rcvd, 11 is missing */ 3069 3070 dp->av_ts = seqnr + 1; 3071 if (dp->av_ts == 0x1000000000000LL) 3072 dp->av_ts = 0; 3073 3074 if (gap > (dp->av_size - 128)) { 3075 n = malloc(dp->av_size/2, M_PCB, M_NOWAIT | M_ZERO); /* old size * 2 */ 3076 memset (n + dp->av_size / 4, 0xff, dp->av_size / 4); /* new half all missing */ 3077 dc = (dp->ackvector + (dp->av_size/4)) - dp->av_hp; 3078 memcpy (n, dp->av_hp, dc); /* tail to end */ 3079 memcpy (n+dc, dp->ackvector, dp->av_hp - dp->ackvector); /* start to tail */ 3080 dp->av_size = dp->av_size * 2; /* counted in items, so it';s a doubling */ 3081 free (dp->ackvector, M_PCB); 3082 dp->av_hp = dp->ackvector = n; 3083 } 3084 } 3085 3086 /** 3087 * Generates the ack vector to send in outgoing packet. 3088 * These are backwards (first packet in ack vector is packet indicated by Ack Number, 3089 * subsequent are older packets). 3090 **/ 3091 3092 u_int16_t 3093 dccp_generate_ackvector(struct dccpcb *dp, u_char *buf) 3094 { 3095 int64_t j; 3096 u_int64_t i; 3097 u_int16_t cnt, oldlen, bufsize; 3098 u_char oldstate, st; 3099 3100 bufsize = 16; 3101 cnt = 0; 3102 3103 oldstate = 0x04; /* bad value */ 3104 oldlen = 0; 3105 3106 if (dp->av_size == 0) { 3107 ACK_DEBUG((LOG_INFO, "Generate: AckVector NOT YET INITIALIZED!!!\n")); 3108 return 0; 3109 } 3110 3111 if (dp->seq_rcv > dp->av_ts) { 3112 /* AckNum is beyond our av-list , so we'll start with some 3113 * 0x3 (Packet not yet received) */ 3114 j = dp->seq_rcv - dp->av_ts -1; 3115 do { 3116 /* state | length */ 3117 oldstate = 0x03; 3118 if (j > 63) 3119 oldlen = 63; 3120 else 3121 oldlen = j; 3122 3123 buf[cnt] = (0x03 << 6) | oldlen; 3124 cnt++; 3125 if (cnt == bufsize) { 3126 /* I've skipped the realloc bshit */ 3127 /* PANIC */ 3128 } 3129 j-=63; 3130 } while (j > 0); 3131 } 3132 3133 /* Ok now we're at dp->av_ts (unless AckNum is lower) */ 3134 i = (dp->seq_rcv < dp->av_ts) ? dp->seq_rcv : dp->av_ts; 3135 st = dccp_ackvector_state(dp, i); 3136 3137 if (st == oldstate) { 3138 cnt--; 3139 oldlen++; 3140 } else { 3141 oldlen = 0; 3142 oldstate = st; 3143 } 3144 3145 if (dp->av_ts > dp->av_hs) { 3146 do { 3147 i--; 3148 st = dccp_ackvector_state(dp, i); 3149 if (st == oldstate && oldlen < 64) { 3150 oldlen++; 3151 } else { 3152 buf[cnt] = (oldstate << 6) | (oldlen & 0x3f); 3153 cnt++; 3154 oldlen = 0; 3155 oldstate = st; 3156 if (cnt == bufsize) { 3157 /* PANIC */ 3158 } 3159 } 3160 3161 } while (i > dp->av_hs); 3162 } else { 3163 /* It's wrapped */ 3164 do { 3165 i--; 3166 st = dccp_ackvector_state(dp, i); 3167 if (st == oldstate && oldlen < 64) { 3168 oldlen++; 3169 } else { 3170 buf[cnt] = (oldstate << 6) | (oldlen & 0x3f); 3171 cnt++; 3172 oldlen = 0; 3173 oldstate = st; 3174 if (cnt == bufsize) { 3175 /* PANIC */ 3176 } 3177 } 3178 3179 } while (i > 0); 3180 i = 0x1000000; 3181 do { 3182 i--; 3183 st = dccp_ackvector_state(dp, i); 3184 if (st == oldstate && oldlen < 64) { 3185 oldlen++; 3186 } else { 3187 buf[cnt] = (oldstate << 6) | (oldlen & 0x3f); 3188 cnt++; 3189 oldlen = 0; 3190 oldstate = st; 3191 if (cnt == bufsize) { 3192 /* PANIC */ 3193 } 3194 } 3195 } while (i > dp->av_hs); 3196 } 3197 3198 /* add the last one */ 3199 buf[cnt] = (oldstate << 6) | (oldlen & 0x3f); 3200 cnt++; 3201 3202 return cnt; 3203 } 3204 3205 u_char 3206 dccp_ackvector_state(struct dccpcb *dp, u_int64_t seqnr) 3207 { 3208 u_int64_t gap, offset; 3209 u_char *t; 3210 3211 /* Check for wrapping */ 3212 if (seqnr >= dp->av_hs) { 3213 /* Not wrapped */ 3214 gap = seqnr - dp->av_hs; 3215 } else { 3216 /* Wrapped */ 3217 gap = seqnr + 0x1000000000000LL - dp->av_hs; /* seq nr = 48 bits */ 3218 } 3219 3220 if (gap >= dp->av_size) { 3221 /* gap is bigger than ackvector size? baaad */ 3222 return 0x03; 3223 } 3224 3225 offset = gap % 4 *2; 3226 t = dp->av_hp + (gap/4); 3227 if (t >= (dp->ackvector + (dp->av_size/4))) 3228 t -= (dp->av_size / 4); /* wrapped */ 3229 3230 return ((*t & (0x03 << offset)) >> offset); 3231 } 3232 3233 /****** End of Ack Vector functions *********/ 3234 3235 /* No cc functions */ 3236 void * 3237 dccp_nocc_init(struct dccpcb *pcb) 3238 { 3239 return (void*) 1; 3240 } 3241 3242 void 3243 dccp_nocc_free(void *ccb) 3244 { 3245 } 3246 3247 int 3248 dccp_nocc_send_packet(void *ccb, long size) 3249 { 3250 return 1; 3251 } 3252 3253 void 3254 dccp_nocc_send_packet_sent(void *ccb, int moreToSend, long size) 3255 { 3256 } 3257 3258 void 3259 dccp_nocc_packet_recv(void *ccb, char* options ,int optlen) 3260 { 3261 } 3262 3263 void 3264 dccp_log(int level, const char *format, ...) 3265 { 3266 va_list ap; 3267 3268 va_start(ap, format); 3269 vprintf(format, ap); 3270 va_end(ap); 3271 return; 3272 } 3273 3274 /* 3275 * Sysctl for dccp variables. 3276 */ 3277 SYSCTL_SETUP(sysctl_net_inet_dccp_setup, "sysctl net.inet.dccp subtree setup") 3278 { 3279 3280 sysctl_createv(clog, 0, NULL, NULL, 3281 CTLFLAG_PERMANENT, 3282 CTLTYPE_NODE, "net", NULL, 3283 NULL, 0, NULL, 0, 3284 CTL_NET, CTL_EOL); 3285 3286 sysctl_createv(clog, 0, NULL, NULL, 3287 CTLFLAG_PERMANENT, 3288 CTLTYPE_NODE, "inet", NULL, 3289 NULL, 0, NULL, 0, 3290 CTL_NET, PF_INET, CTL_EOL); 3291 3292 sysctl_createv(clog, 0, NULL, NULL, 3293 CTLFLAG_PERMANENT, 3294 CTLTYPE_NODE, "dccp", 3295 SYSCTL_DESCR("DCCPv4 related settings"), 3296 NULL, 0, NULL, 0, 3297 CTL_NET, PF_INET, IPPROTO_DCCP, CTL_EOL); 3298 3299 sysctl_createv(clog, 0, NULL, NULL, 3300 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3301 CTLTYPE_INT, "dccp_log_in_vain", 3302 SYSCTL_DESCR("log all connection attempt"), 3303 NULL, 0, &dccp_log_in_vain, 0, 3304 CTL_NET, PF_INET, IPPROTO_DCCP, DCCPCTL_LOGINVAIN, 3305 CTL_EOL); 3306 3307 sysctl_createv(clog, 0, NULL, NULL, 3308 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 3309 CTLTYPE_INT, "do_feature_nego", 3310 SYSCTL_DESCR("enable feature negotiation"), 3311 NULL, 0, &dccp_do_feature_nego, 0, 3312 CTL_NET, PF_INET, IPPROTO_DCCP, DCCPCTL_DOFEATURENEGO, 3313 CTL_EOL); 3314 } 3315 3316 PR_WRAP_USRREQS(dccp) 3317 #define dccp_attach dccp_attach_wrapper 3318 #define dccp_detach dccp_detach_wrapper 3319 #define dccp_accept dccp_accept_wrapper 3320 #define dccp_bind dccp_bind_wrapper 3321 #define dccp_listen dccp_listen_wrapper 3322 #define dccp_connect dccp_connect_wrapper 3323 #define dccp_connect2 dccp_connect2_wrapper 3324 #define dccp_disconnect dccp_disconnect_wrapper 3325 #define dccp_shutdown dccp_shutdown_wrapper 3326 #define dccp_abort dccp_abort_wrapper 3327 #define dccp_ioctl dccp_ioctl_wrapper 3328 #define dccp_stat dccp_stat_wrapper 3329 #define dccp_peeraddr dccp_peeraddr_wrapper 3330 #define dccp_sockaddr dccp_sockaddr_wrapper 3331 #define dccp_rcvd dccp_rcvd_wrapper 3332 #define dccp_recvoob dccp_recvoob_wrapper 3333 #define dccp_send dccp_send_wrapper 3334 #define dccp_sendoob dccp_sendoob_wrapper 3335 #define dccp_purgeif dccp_purgeif_wrapper 3336 3337 const struct pr_usrreqs dccp_usrreqs = { 3338 .pr_attach = dccp_attach, 3339 .pr_detach = dccp_detach, 3340 .pr_accept = dccp_accept, 3341 .pr_bind = dccp_bind, 3342 .pr_listen = dccp_listen, 3343 .pr_connect = dccp_connect, 3344 .pr_connect2 = dccp_connect2, 3345 .pr_disconnect = dccp_disconnect, 3346 .pr_shutdown = dccp_shutdown, 3347 .pr_abort = dccp_abort, 3348 .pr_ioctl = dccp_ioctl, 3349 .pr_stat = dccp_stat, 3350 .pr_peeraddr = dccp_peeraddr, 3351 .pr_sockaddr = dccp_sockaddr, 3352 .pr_rcvd = dccp_rcvd, 3353 .pr_recvoob = dccp_recvoob, 3354 .pr_send = dccp_send, 3355 .pr_sendoob = dccp_sendoob, 3356 .pr_purgeif = dccp_purgeif, 3357 }; 3358