1 /*- 2 * Copyright (c) 2001-2006, Cisco Systems, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * a) Redistributions of source code must retain the above copyright notice, 8 * this list of conditions and the following disclaimer. 9 * 10 * b) Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in 12 * the documentation and/or other materials provided with the distribution. 13 * 14 * c) Neither the name of Cisco Systems, Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 * THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 /* $KAME: sctp_pcb.h,v 1.21 2005/07/16 01:18:47 suz Exp $ */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #ifndef __sctp_pcb_h__ 37 #define __sctp_pcb_h__ 38 39 40 41 /* 42 * We must have V6 so the size of the proto can be calculated. Otherwise we 43 * would not allocate enough for Net/Open BSD :-< 44 */ 45 46 #if defined(_KERNEL) 47 #include <net/pfil.h> 48 #endif 49 50 #include <sys/socket.h> 51 #include <sys/socketvar.h> 52 #include <net/if.h> 53 #include <net/if_types.h> 54 #include <net/if_var.h> 55 #include <net/route.h> 56 #include <netinet/in.h> 57 #include <netinet/ip6.h> 58 #include <netinet6/ip6_var.h> 59 #include <netinet6/ip6protosw.h> 60 #include <netinet6/in6_var.h> 61 #include <netinet6/in6_pcb.h> 62 63 #ifndef in6pcb 64 #define in6pcb inpcb 65 #endif 66 67 #include <netinet/sctp.h> 68 #include <netinet/sctp_os.h> 69 #include <netinet/sctp_constants.h> 70 71 LIST_HEAD(sctppcbhead, sctp_inpcb); 72 LIST_HEAD(sctpasochead, sctp_tcb); 73 LIST_HEAD(sctpladdr, sctp_laddr); 74 LIST_HEAD(sctpvtaghead, sctp_tagblock); 75 TAILQ_HEAD(sctp_readhead, sctp_queued_to_read); 76 TAILQ_HEAD(sctp_streamhead, sctp_stream_queue_pending); 77 78 #include <netinet/sctp_structs.h> 79 #include <netinet/sctp_uio.h> 80 #include <netinet/sctp_auth.h> 81 82 /* 83 * PCB flags (in sctp_flags bitmask) 84 */ 85 #define SCTP_PCB_FLAGS_UDPTYPE 0x00000001 86 #define SCTP_PCB_FLAGS_TCPTYPE 0x00000002 87 #define SCTP_PCB_FLAGS_BOUNDALL 0x00000004 88 #define SCTP_PCB_FLAGS_ACCEPTING 0x00000008 89 #define SCTP_PCB_FLAGS_UNBOUND 0x00000010 90 #define SCTP_PCB_FLAGS_CLOSE_IP 0x00040000 91 #define SCTP_PCB_FLAGS_WAS_CONNECTED 0x00080000 92 #define SCTP_PCB_FLAGS_WAS_ABORTED 0x00100000 93 /* TCP model support */ 94 95 #define SCTP_PCB_FLAGS_CONNECTED 0x00200000 96 #define SCTP_PCB_FLAGS_IN_TCPPOOL 0x00400000 97 #define SCTP_PCB_FLAGS_DONT_WAKE 0x00800000 98 #define SCTP_PCB_FLAGS_WAKEOUTPUT 0x01000000 99 #define SCTP_PCB_FLAGS_WAKEINPUT 0x02000000 100 #define SCTP_PCB_FLAGS_BOUND_V6 0x04000000 101 #define SCTP_PCB_FLAGS_NEEDS_MAPPED_V4 0x08000000 102 #define SCTP_PCB_FLAGS_BLOCKING_IO 0x10000000 103 #define SCTP_PCB_FLAGS_SOCKET_GONE 0x20000000 104 #define SCTP_PCB_FLAGS_SOCKET_ALLGONE 0x40000000 105 /* flags to copy to new PCB */ 106 #define SCTP_PCB_COPY_FLAGS 0x0e000004 107 108 109 /* 110 * PCB Features (in sctp_features bitmask) 111 */ 112 #define SCTP_PCB_FLAGS_EXT_RCVINFO 0x00000004 113 #define SCTP_PCB_FLAGS_DONOT_HEARTBEAT 0x00000008 114 #define SCTP_PCB_FLAGS_FRAG_INTERLEAVE 0x00000010 115 #define SCTP_PCB_FLAGS_DO_ASCONF 0x00000020 116 #define SCTP_PCB_FLAGS_AUTO_ASCONF 0x00000040 117 /* socket options */ 118 #define SCTP_PCB_FLAGS_NODELAY 0x00000100 119 #define SCTP_PCB_FLAGS_AUTOCLOSE 0x00000200 120 #define SCTP_PCB_FLAGS_RECVDATAIOEVNT 0x00000400 121 #define SCTP_PCB_FLAGS_RECVASSOCEVNT 0x00000800 122 #define SCTP_PCB_FLAGS_RECVPADDREVNT 0x00001000 123 #define SCTP_PCB_FLAGS_RECVPEERERR 0x00002000 124 #define SCTP_PCB_FLAGS_RECVSENDFAILEVNT 0x00004000 125 #define SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT 0x00008000 126 #define SCTP_PCB_FLAGS_ADAPTATIONEVNT 0x00010000 127 #define SCTP_PCB_FLAGS_PDAPIEVNT 0x00020000 128 #define SCTP_PCB_FLAGS_AUTHEVNT 0x00040000 129 #define SCTP_PCB_FLAGS_STREAM_RESETEVNT 0x00080000 130 #define SCTP_PCB_FLAGS_NO_FRAGMENT 0x00100000 131 #define SCTP_PCB_FLAGS_EXPLICIT_EOR 0x00200000 132 133 134 #define SCTP_PCBHASH_ALLADDR(port, mask) (port & mask) 135 #define SCTP_PCBHASH_ASOC(tag, mask) (tag & mask) 136 137 struct sctp_laddr { 138 LIST_ENTRY(sctp_laddr) sctp_nxt_addr; /* next in list */ 139 struct ifaddr *ifa; 140 int action; /* Only used in delayed asconf stuff */ 141 }; 142 143 struct sctp_block_entry { 144 int error; 145 }; 146 147 struct sctp_timewait { 148 uint32_t tv_sec_at_expire; /* the seconds from boot to expire */ 149 uint32_t v_tag; /* the vtag that can not be reused */ 150 }; 151 152 struct sctp_tagblock { 153 LIST_ENTRY(sctp_tagblock) sctp_nxt_tagblock; 154 struct sctp_timewait vtag_block[SCTP_NUMBER_IN_VTAG_BLOCK]; 155 }; 156 157 158 struct sctp_epinfo { 159 struct sctpasochead *sctp_asochash; 160 u_long hashasocmark; 161 162 struct sctppcbhead *sctp_ephash; 163 u_long hashmark; 164 165 struct sctpasochead *sctp_restarthash; 166 u_long hashrestartmark; 167 /* 168 * The TCP model represents a substantial overhead in that we get an 169 * additional hash table to keep explicit connections in. The 170 * listening TCP endpoint will exist in the usual ephash above and 171 * accept only INIT's. It will be incapable of sending off an INIT. 172 * When a dg arrives we must look in the normal ephash. If we find a 173 * TCP endpoint that will tell us to go to the specific endpoint 174 * hash and re-hash to find the right assoc/socket. If we find a UDP 175 * model socket we then must complete the lookup. If this fails, 176 * i.e. no association can be found then we must continue to see if 177 * a sctp_peeloff()'d socket is in the tcpephash (a spun off socket 178 * acts like a TCP model connected socket). 179 */ 180 struct sctppcbhead *sctp_tcpephash; 181 u_long hashtcpmark; 182 uint32_t hashtblsize; 183 184 struct sctppcbhead listhead; 185 struct sctpladdr addr_wq; 186 187 struct sctpiterators iteratorhead; 188 189 /* ep zone info */ 190 sctp_zone_t ipi_zone_ep; 191 sctp_zone_t ipi_zone_asoc; 192 sctp_zone_t ipi_zone_laddr; 193 sctp_zone_t ipi_zone_net; 194 sctp_zone_t ipi_zone_chunk; 195 sctp_zone_t ipi_zone_readq; 196 sctp_zone_t ipi_zone_strmoq; 197 198 struct mtx ipi_ep_mtx; 199 struct mtx it_mtx; 200 struct mtx ipi_addr_mtx; 201 uint32_t ipi_count_ep; 202 203 /* assoc/tcb zone info */ 204 uint32_t ipi_count_asoc; 205 206 /* local addrlist zone info */ 207 uint32_t ipi_count_laddr; 208 209 /* remote addrlist zone info */ 210 uint32_t ipi_count_raddr; 211 212 /* chunk structure list for output */ 213 uint32_t ipi_count_chunk; 214 215 /* socket queue zone info */ 216 uint32_t ipi_count_readq; 217 218 /* socket queue zone info */ 219 uint32_t ipi_count_strmoq; 220 221 /* system wide number of free chunks hanging around */ 222 uint32_t ipi_free_chunks; 223 uint32_t ipi_free_strmoq; 224 225 struct sctpvtaghead vtag_timewait[SCTP_STACK_VTAG_HASH_SIZE]; 226 227 228 struct sctp_timer addr_wq_timer; 229 230 /* for port allocations */ 231 uint16_t lastport; 232 uint16_t lastlow; 233 uint16_t lasthi; 234 235 }; 236 237 extern struct sctpstat sctpstat; 238 239 /* 240 * Here we have all the relevant information for each SCTP entity created. We 241 * will need to modify this as approprate. We also need to figure out how to 242 * access /dev/random. 243 */ 244 struct sctp_pcb { 245 unsigned int time_of_secret_change; /* number of seconds from 246 * timeval.tv_sec */ 247 uint32_t secret_key[SCTP_HOW_MANY_SECRETS][SCTP_NUMBER_OF_SECRETS]; 248 unsigned int size_of_a_cookie; 249 250 unsigned int sctp_timeoutticks[SCTP_NUM_TMRS]; 251 unsigned int sctp_minrto; 252 unsigned int sctp_maxrto; 253 unsigned int initial_rto; 254 255 int initial_init_rto_max; 256 257 uint32_t sctp_sws_sender; 258 uint32_t sctp_sws_receiver; 259 260 /* authentication related fields */ 261 struct sctp_keyhead shared_keys; 262 sctp_auth_chklist_t *local_auth_chunks; 263 sctp_hmaclist_t *local_hmacs; 264 uint16_t default_keyid; 265 266 /* various thresholds */ 267 /* Max times I will init at a guy */ 268 uint16_t max_init_times; 269 270 /* Max times I will send before we consider someone dead */ 271 uint16_t max_send_times; 272 273 uint16_t def_net_failure; 274 275 /* number of streams to pre-open on a association */ 276 uint16_t pre_open_stream_count; 277 uint16_t max_open_streams_intome; 278 279 /* random number generator */ 280 uint32_t random_counter; 281 uint8_t random_numbers[SCTP_SIGNATURE_ALOC_SIZE]; 282 uint8_t random_store[SCTP_SIGNATURE_ALOC_SIZE]; 283 284 /* 285 * This timer is kept running per endpoint. When it fires it will 286 * change the secret key. The default is once a hour 287 */ 288 struct sctp_timer signature_change; 289 int def_cookie_life; 290 /* defaults to 0 */ 291 int auto_close_time; 292 uint32_t initial_sequence_debug; 293 uint32_t adaptation_layer_indicator; 294 char store_at; 295 uint8_t max_burst; 296 char current_secret_number; 297 char last_secret_number; 298 }; 299 300 #ifndef SCTP_ALIGNMENT 301 #define SCTP_ALIGNMENT 32 302 #endif 303 304 #ifndef SCTP_ALIGNM1 305 #define SCTP_ALIGNM1 (SCTP_ALIGNMENT-1) 306 #endif 307 308 #define sctp_lport ip_inp.inp.inp_lport 309 310 struct sctp_inpcb { 311 /* 312 * put an inpcb in front of it all, kind of a waste but we need to 313 * for compatability with all the other stuff. 314 */ 315 union { 316 struct inpcb inp; 317 char align[(sizeof(struct in6pcb) + SCTP_ALIGNM1) & 318 ~SCTP_ALIGNM1]; 319 } ip_inp; 320 321 322 /* Socket buffer lock protects read_queue and of course sb_cc */ 323 struct sctp_readhead read_queue; 324 325 LIST_ENTRY(sctp_inpcb) sctp_list; /* lists all endpoints */ 326 /* hash of all endpoints for model */ 327 LIST_ENTRY(sctp_inpcb) sctp_hash; 328 /* count of local addresses bound, 0 if bound all */ 329 int laddr_count; 330 /* list of addrs in use by the EP */ 331 struct sctpladdr sctp_addr_list; 332 /* used for source address selection rotation */ 333 struct sctp_laddr *next_addr_touse; 334 struct ifnet *next_ifn_touse; 335 /* back pointer to our socket */ 336 struct socket *sctp_socket; 337 uint32_t sctp_flags; /* INP state flag set */ 338 uint32_t sctp_features; /* Feature flags */ 339 struct sctp_pcb sctp_ep;/* SCTP ep data */ 340 /* head of the hash of all associations */ 341 struct sctpasochead *sctp_tcbhash; 342 u_long sctp_hashmark; 343 /* head of the list of all associations */ 344 struct sctpasochead sctp_asoc_list; 345 struct sctp_iterator *inp_starting_point_for_iterator; 346 uint32_t sctp_frag_point; 347 uint32_t partial_delivery_point; 348 uint32_t sctp_context; 349 struct sctp_sndrcvinfo def_send; 350 /* 351 * These three are here for the sosend_dgram (pkt, pkt_last and 352 * control). routine. However, I don't think anyone in the current 353 * FreeBSD kernel calls this. So they are candidates with sctp_sendm 354 * for de-supporting. 355 */ 356 struct mbuf *pkt, *pkt_last; 357 struct mbuf *control; 358 struct mtx inp_mtx; 359 struct mtx inp_create_mtx; 360 struct mtx inp_rdata_mtx; 361 int32_t refcount; 362 }; 363 364 struct sctp_tcb { 365 struct socket *sctp_socket; /* back pointer to socket */ 366 struct sctp_inpcb *sctp_ep; /* back pointer to ep */ 367 LIST_ENTRY(sctp_tcb) sctp_tcbhash; /* next link in hash 368 * table */ 369 LIST_ENTRY(sctp_tcb) sctp_tcblist; /* list of all of the 370 * TCB's */ 371 LIST_ENTRY(sctp_tcb) sctp_tcbrestarhash; /* next link in restart 372 * hash table */ 373 LIST_ENTRY(sctp_tcb) sctp_asocs; /* vtag hash list */ 374 struct sctp_block_entry *block_entry; /* pointer locked by socket 375 * send buffer */ 376 struct sctp_association asoc; 377 /* 378 * freed_by_sorcv_sincelast is protected by the sockbuf_lock NOT the 379 * tcb_lock. Its special in this way to help avoid extra mutex calls 380 * in the reading of data. 381 */ 382 uint32_t freed_by_sorcv_sincelast; 383 uint16_t rport; /* remote port in network format */ 384 uint16_t resv; 385 struct mtx tcb_mtx; 386 struct mtx tcb_send_mtx; 387 }; 388 389 390 391 #include <netinet/sctp_lock_bsd.h> 392 393 394 395 #if defined(_KERNEL) 396 397 extern struct sctp_epinfo sctppcbinfo; 398 extern int sctp_auto_asconf; 399 400 int SCTP6_ARE_ADDR_EQUAL(struct in6_addr *a, struct in6_addr *b); 401 402 void sctp_fill_pcbinfo(struct sctp_pcbinfo *); 403 404 struct sctp_nets *sctp_findnet(struct sctp_tcb *, struct sockaddr *); 405 406 struct sctp_inpcb *sctp_pcb_findep(struct sockaddr *, int, int); 407 408 int sctp_inpcb_bind(struct socket *, struct sockaddr *, struct thread *); 409 410 411 struct sctp_tcb * 412 sctp_findassociation_addr(struct mbuf *, int, int, 413 struct sctphdr *, struct sctp_chunkhdr *, struct sctp_inpcb **, 414 struct sctp_nets **); 415 416 struct sctp_tcb * 417 sctp_findassociation_addr_sa(struct sockaddr *, 418 struct sockaddr *, struct sctp_inpcb **, struct sctp_nets **, int); 419 420 void 421 sctp_move_pcb_and_assoc(struct sctp_inpcb *, struct sctp_inpcb *, 422 struct sctp_tcb *); 423 424 /* 425 * For this call ep_addr, the to is the destination endpoint address of the 426 * peer (relative to outbound). The from field is only used if the TCP model 427 * is enabled and helps distingush amongst the subset bound (non-boundall). 428 * The TCP model MAY change the actual ep field, this is why it is passed. 429 */ 430 struct sctp_tcb * 431 sctp_findassociation_ep_addr(struct sctp_inpcb **, 432 struct sockaddr *, struct sctp_nets **, struct sockaddr *, 433 struct sctp_tcb *); 434 435 struct sctp_tcb * 436 sctp_findassociation_ep_asocid(struct sctp_inpcb *, 437 sctp_assoc_t, int); 438 439 struct sctp_tcb * 440 sctp_findassociation_ep_asconf(struct mbuf *, int, int, 441 struct sctphdr *, struct sctp_inpcb **, struct sctp_nets **); 442 443 int sctp_inpcb_alloc(struct socket *); 444 445 int sctp_is_address_on_local_host(struct sockaddr *addr); 446 447 void sctp_inpcb_free(struct sctp_inpcb *, int, int); 448 449 struct sctp_tcb * 450 sctp_aloc_assoc(struct sctp_inpcb *, struct sockaddr *, 451 int, int *, uint32_t); 452 453 int sctp_free_assoc(struct sctp_inpcb *, struct sctp_tcb *, int); 454 455 int sctp_add_local_addr_ep(struct sctp_inpcb *, struct ifaddr *); 456 457 int sctp_insert_laddr(struct sctpladdr *, struct ifaddr *); 458 459 void sctp_remove_laddr(struct sctp_laddr *); 460 461 int sctp_del_local_addr_ep(struct sctp_inpcb *, struct ifaddr *); 462 463 int sctp_del_local_addr_ep_sa(struct sctp_inpcb *, struct sockaddr *); 464 465 int sctp_add_remote_addr(struct sctp_tcb *, struct sockaddr *, int, int); 466 467 void sctp_remove_net(struct sctp_tcb *, struct sctp_nets *); 468 469 int sctp_del_remote_addr(struct sctp_tcb *, struct sockaddr *); 470 471 void sctp_pcb_init(void); 472 473 int sctp_add_local_addr_assoc(struct sctp_tcb *, struct ifaddr *); 474 475 int sctp_del_local_addr_assoc(struct sctp_tcb *, struct ifaddr *); 476 477 int sctp_del_local_addr_assoc_sa(struct sctp_tcb *, struct sockaddr *); 478 479 int 480 sctp_load_addresses_from_init(struct sctp_tcb *, struct mbuf *, int, int, 481 int, struct sctphdr *, struct sockaddr *); 482 483 int 484 sctp_set_primary_addr(struct sctp_tcb *, struct sockaddr *, 485 struct sctp_nets *); 486 487 int sctp_is_vtag_good(struct sctp_inpcb *, uint32_t, struct timeval *); 488 489 /* void sctp_drain(void); */ 490 491 int sctp_destination_is_reachable(struct sctp_tcb *, struct sockaddr *); 492 493 /* 494 * Null in last arg inpcb indicate run on ALL ep's. Specific inp in last arg 495 * indicates run on ONLY assoc's of the specified endpoint. 496 */ 497 int 498 sctp_initiate_iterator(inp_func inpf, asoc_func af, uint32_t, uint32_t, 499 uint32_t, void *, uint32_t, end_func ef, struct sctp_inpcb *, uint8_t co_off); 500 501 502 503 #endif /* _KERNEL */ 504 #endif /* !__sctp_pcb_h__ */ 505