1 /* SCTP kernel reference Implementation 2 * (C) Copyright IBM Corp. 2001, 2004 3 * Copyright (c) 1999-2000 Cisco, Inc. 4 * Copyright (c) 1999-2001 Motorola, Inc. 5 * Copyright (c) 2001 Intel Corp. 6 * Copyright (c) 2001 Nokia, Inc. 7 * Copyright (c) 2001 La Monte H.P. Yarroll 8 * 9 * This file is part of the SCTP kernel reference Implementation 10 * 11 * Initialization/cleanup for SCTP protocol support. 12 * 13 * The SCTP reference implementation is free software; 14 * you can redistribute it and/or modify it under the terms of 15 * the GNU General Public License as published by 16 * the Free Software Foundation; either version 2, or (at your option) 17 * any later version. 18 * 19 * The SCTP reference implementation is distributed in the hope that it 20 * will be useful, but WITHOUT ANY WARRANTY; without even the implied 21 * ************************ 22 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 23 * See the GNU General Public License for more details. 24 * 25 * You should have received a copy of the GNU General Public License 26 * along with GNU CC; see the file COPYING. If not, write to 27 * the Free Software Foundation, 59 Temple Place - Suite 330, 28 * Boston, MA 02111-1307, USA. 29 * 30 * Please send any bug reports or fixes you make to the 31 * email address(es): 32 * lksctp developers <lksctp-developers@lists.sourceforge.net> 33 * 34 * Or submit a bug report through the following website: 35 * http://www.sf.net/projects/lksctp 36 * 37 * Written or modified by: 38 * La Monte H.P. Yarroll <piggy@acm.org> 39 * Karl Knutson <karl@athena.chicago.il.us> 40 * Jon Grimm <jgrimm@us.ibm.com> 41 * Sridhar Samudrala <sri@us.ibm.com> 42 * Daisy Chang <daisyc@us.ibm.com> 43 * Ardelle Fan <ardelle.fan@intel.com> 44 * 45 * Any bugs reported given to us we will try to fix... any fixes shared will 46 * be incorporated into the next SCTP release. 47 */ 48 49 #include <linux/module.h> 50 #include <linux/init.h> 51 #include <linux/netdevice.h> 52 #include <linux/inetdevice.h> 53 #include <linux/seq_file.h> 54 #include <net/protocol.h> 55 #include <net/ip.h> 56 #include <net/ipv6.h> 57 #include <net/route.h> 58 #include <net/sctp/sctp.h> 59 #include <net/addrconf.h> 60 #include <net/inet_common.h> 61 #include <net/inet_ecn.h> 62 63 /* Global data structures. */ 64 struct sctp_globals sctp_globals __read_mostly; 65 struct proc_dir_entry *proc_net_sctp; 66 DEFINE_SNMP_STAT(struct sctp_mib, sctp_statistics) __read_mostly; 67 68 struct idr sctp_assocs_id; 69 DEFINE_SPINLOCK(sctp_assocs_id_lock); 70 71 /* This is the global socket data structure used for responding to 72 * the Out-of-the-blue (OOTB) packets. A control sock will be created 73 * for this socket at the initialization time. 74 */ 75 static struct socket *sctp_ctl_socket; 76 77 static struct sctp_pf *sctp_pf_inet6_specific; 78 static struct sctp_pf *sctp_pf_inet_specific; 79 static struct sctp_af *sctp_af_v4_specific; 80 static struct sctp_af *sctp_af_v6_specific; 81 82 struct kmem_cache *sctp_chunk_cachep __read_mostly; 83 struct kmem_cache *sctp_bucket_cachep __read_mostly; 84 85 /* Return the address of the control sock. */ 86 struct sock *sctp_get_ctl_sock(void) 87 { 88 return sctp_ctl_socket->sk; 89 } 90 91 /* Set up the proc fs entry for the SCTP protocol. */ 92 static __init int sctp_proc_init(void) 93 { 94 if (!proc_net_sctp) { 95 struct proc_dir_entry *ent; 96 ent = proc_mkdir("net/sctp", NULL); 97 if (ent) { 98 ent->owner = THIS_MODULE; 99 proc_net_sctp = ent; 100 } else 101 goto out_nomem; 102 } 103 104 if (sctp_snmp_proc_init()) 105 goto out_nomem; 106 if (sctp_eps_proc_init()) 107 goto out_nomem; 108 if (sctp_assocs_proc_init()) 109 goto out_nomem; 110 111 return 0; 112 113 out_nomem: 114 return -ENOMEM; 115 } 116 117 /* Clean up the proc fs entry for the SCTP protocol. 118 * Note: Do not make this __exit as it is used in the init error 119 * path. 120 */ 121 static void sctp_proc_exit(void) 122 { 123 sctp_snmp_proc_exit(); 124 sctp_eps_proc_exit(); 125 sctp_assocs_proc_exit(); 126 127 if (proc_net_sctp) { 128 proc_net_sctp = NULL; 129 remove_proc_entry("net/sctp", NULL); 130 } 131 } 132 133 /* Private helper to extract ipv4 address and stash them in 134 * the protocol structure. 135 */ 136 static void sctp_v4_copy_addrlist(struct list_head *addrlist, 137 struct net_device *dev) 138 { 139 struct in_device *in_dev; 140 struct in_ifaddr *ifa; 141 struct sctp_sockaddr_entry *addr; 142 143 rcu_read_lock(); 144 if ((in_dev = __in_dev_get_rcu(dev)) == NULL) { 145 rcu_read_unlock(); 146 return; 147 } 148 149 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 150 /* Add the address to the local list. */ 151 addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC); 152 if (addr) { 153 addr->a.v4.sin_family = AF_INET; 154 addr->a.v4.sin_port = 0; 155 addr->a.v4.sin_addr.s_addr = ifa->ifa_local; 156 list_add_tail(&addr->list, addrlist); 157 } 158 } 159 160 rcu_read_unlock(); 161 } 162 163 /* Extract our IP addresses from the system and stash them in the 164 * protocol structure. 165 */ 166 static void __sctp_get_local_addr_list(void) 167 { 168 struct net_device *dev; 169 struct list_head *pos; 170 struct sctp_af *af; 171 172 read_lock(&dev_base_lock); 173 for (dev = dev_base; dev; dev = dev->next) { 174 __list_for_each(pos, &sctp_address_families) { 175 af = list_entry(pos, struct sctp_af, list); 176 af->copy_addrlist(&sctp_local_addr_list, dev); 177 } 178 } 179 read_unlock(&dev_base_lock); 180 } 181 182 static void sctp_get_local_addr_list(void) 183 { 184 unsigned long flags; 185 186 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags); 187 __sctp_get_local_addr_list(); 188 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags); 189 } 190 191 /* Free the existing local addresses. */ 192 static void __sctp_free_local_addr_list(void) 193 { 194 struct sctp_sockaddr_entry *addr; 195 struct list_head *pos, *temp; 196 197 list_for_each_safe(pos, temp, &sctp_local_addr_list) { 198 addr = list_entry(pos, struct sctp_sockaddr_entry, list); 199 list_del(pos); 200 kfree(addr); 201 } 202 } 203 204 /* Free the existing local addresses. */ 205 static void sctp_free_local_addr_list(void) 206 { 207 unsigned long flags; 208 209 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags); 210 __sctp_free_local_addr_list(); 211 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags); 212 } 213 214 /* Copy the local addresses which are valid for 'scope' into 'bp'. */ 215 int sctp_copy_local_addr_list(struct sctp_bind_addr *bp, sctp_scope_t scope, 216 gfp_t gfp, int copy_flags) 217 { 218 struct sctp_sockaddr_entry *addr; 219 int error = 0; 220 struct list_head *pos; 221 unsigned long flags; 222 223 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags); 224 list_for_each(pos, &sctp_local_addr_list) { 225 addr = list_entry(pos, struct sctp_sockaddr_entry, list); 226 if (sctp_in_scope(&addr->a, scope)) { 227 /* Now that the address is in scope, check to see if 228 * the address type is really supported by the local 229 * sock as well as the remote peer. 230 */ 231 if ((((AF_INET == addr->a.sa.sa_family) && 232 (copy_flags & SCTP_ADDR4_PEERSUPP))) || 233 (((AF_INET6 == addr->a.sa.sa_family) && 234 (copy_flags & SCTP_ADDR6_ALLOWED) && 235 (copy_flags & SCTP_ADDR6_PEERSUPP)))) { 236 error = sctp_add_bind_addr(bp, &addr->a, 1, 237 GFP_ATOMIC); 238 if (error) 239 goto end_copy; 240 } 241 } 242 } 243 244 end_copy: 245 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags); 246 return error; 247 } 248 249 /* Initialize a sctp_addr from in incoming skb. */ 250 static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb, 251 int is_saddr) 252 { 253 void *from; 254 __be16 *port; 255 struct sctphdr *sh; 256 257 port = &addr->v4.sin_port; 258 addr->v4.sin_family = AF_INET; 259 260 sh = (struct sctphdr *) skb->h.raw; 261 if (is_saddr) { 262 *port = sh->source; 263 from = &skb->nh.iph->saddr; 264 } else { 265 *port = sh->dest; 266 from = &skb->nh.iph->daddr; 267 } 268 memcpy(&addr->v4.sin_addr.s_addr, from, sizeof(struct in_addr)); 269 } 270 271 /* Initialize an sctp_addr from a socket. */ 272 static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk) 273 { 274 addr->v4.sin_family = AF_INET; 275 addr->v4.sin_port = 0; 276 addr->v4.sin_addr.s_addr = inet_sk(sk)->rcv_saddr; 277 } 278 279 /* Initialize sk->sk_rcv_saddr from sctp_addr. */ 280 static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk) 281 { 282 inet_sk(sk)->rcv_saddr = addr->v4.sin_addr.s_addr; 283 } 284 285 /* Initialize sk->sk_daddr from sctp_addr. */ 286 static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk) 287 { 288 inet_sk(sk)->daddr = addr->v4.sin_addr.s_addr; 289 } 290 291 /* Initialize a sctp_addr from an address parameter. */ 292 static void sctp_v4_from_addr_param(union sctp_addr *addr, 293 union sctp_addr_param *param, 294 __be16 port, int iif) 295 { 296 addr->v4.sin_family = AF_INET; 297 addr->v4.sin_port = port; 298 addr->v4.sin_addr.s_addr = param->v4.addr.s_addr; 299 } 300 301 /* Initialize an address parameter from a sctp_addr and return the length 302 * of the address parameter. 303 */ 304 static int sctp_v4_to_addr_param(const union sctp_addr *addr, 305 union sctp_addr_param *param) 306 { 307 int length = sizeof(sctp_ipv4addr_param_t); 308 309 param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS; 310 param->v4.param_hdr.length = htons(length); 311 param->v4.addr.s_addr = addr->v4.sin_addr.s_addr; 312 313 return length; 314 } 315 316 /* Initialize a sctp_addr from a dst_entry. */ 317 static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct dst_entry *dst, 318 __be16 port) 319 { 320 struct rtable *rt = (struct rtable *)dst; 321 saddr->v4.sin_family = AF_INET; 322 saddr->v4.sin_port = port; 323 saddr->v4.sin_addr.s_addr = rt->rt_src; 324 } 325 326 /* Compare two addresses exactly. */ 327 static int sctp_v4_cmp_addr(const union sctp_addr *addr1, 328 const union sctp_addr *addr2) 329 { 330 if (addr1->sa.sa_family != addr2->sa.sa_family) 331 return 0; 332 if (addr1->v4.sin_port != addr2->v4.sin_port) 333 return 0; 334 if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr) 335 return 0; 336 337 return 1; 338 } 339 340 /* Initialize addr struct to INADDR_ANY. */ 341 static void sctp_v4_inaddr_any(union sctp_addr *addr, __be16 port) 342 { 343 addr->v4.sin_family = AF_INET; 344 addr->v4.sin_addr.s_addr = INADDR_ANY; 345 addr->v4.sin_port = port; 346 } 347 348 /* Is this a wildcard address? */ 349 static int sctp_v4_is_any(const union sctp_addr *addr) 350 { 351 return INADDR_ANY == addr->v4.sin_addr.s_addr; 352 } 353 354 /* This function checks if the address is a valid address to be used for 355 * SCTP binding. 356 * 357 * Output: 358 * Return 0 - If the address is a non-unicast or an illegal address. 359 * Return 1 - If the address is a unicast. 360 */ 361 static int sctp_v4_addr_valid(union sctp_addr *addr, 362 struct sctp_sock *sp, 363 const struct sk_buff *skb) 364 { 365 /* Is this a non-unicast address or a unusable SCTP address? */ 366 if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr)) 367 return 0; 368 369 /* Is this a broadcast address? */ 370 if (skb && ((struct rtable *)skb->dst)->rt_flags & RTCF_BROADCAST) 371 return 0; 372 373 return 1; 374 } 375 376 /* Should this be available for binding? */ 377 static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp) 378 { 379 int ret = inet_addr_type(addr->v4.sin_addr.s_addr); 380 381 382 if (addr->v4.sin_addr.s_addr != INADDR_ANY && 383 ret != RTN_LOCAL && 384 !sp->inet.freebind && 385 !sysctl_ip_nonlocal_bind) 386 return 0; 387 388 return 1; 389 } 390 391 /* Checking the loopback, private and other address scopes as defined in 392 * RFC 1918. The IPv4 scoping is based on the draft for SCTP IPv4 393 * scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>. 394 * 395 * Level 0 - unusable SCTP addresses 396 * Level 1 - loopback address 397 * Level 2 - link-local addresses 398 * Level 3 - private addresses. 399 * Level 4 - global addresses 400 * For INIT and INIT-ACK address list, let L be the level of 401 * of requested destination address, sender and receiver 402 * SHOULD include all of its addresses with level greater 403 * than or equal to L. 404 */ 405 static sctp_scope_t sctp_v4_scope(union sctp_addr *addr) 406 { 407 sctp_scope_t retval; 408 409 /* Should IPv4 scoping be a sysctl configurable option 410 * so users can turn it off (default on) for certain 411 * unconventional networking environments? 412 */ 413 414 /* Check for unusable SCTP addresses. */ 415 if (IS_IPV4_UNUSABLE_ADDRESS(&addr->v4.sin_addr.s_addr)) { 416 retval = SCTP_SCOPE_UNUSABLE; 417 } else if (LOOPBACK(addr->v4.sin_addr.s_addr)) { 418 retval = SCTP_SCOPE_LOOPBACK; 419 } else if (IS_IPV4_LINK_ADDRESS(&addr->v4.sin_addr.s_addr)) { 420 retval = SCTP_SCOPE_LINK; 421 } else if (IS_IPV4_PRIVATE_ADDRESS(&addr->v4.sin_addr.s_addr)) { 422 retval = SCTP_SCOPE_PRIVATE; 423 } else { 424 retval = SCTP_SCOPE_GLOBAL; 425 } 426 427 return retval; 428 } 429 430 /* Returns a valid dst cache entry for the given source and destination ip 431 * addresses. If an association is passed, trys to get a dst entry with a 432 * source address that matches an address in the bind address list. 433 */ 434 static struct dst_entry *sctp_v4_get_dst(struct sctp_association *asoc, 435 union sctp_addr *daddr, 436 union sctp_addr *saddr) 437 { 438 struct rtable *rt; 439 struct flowi fl; 440 struct sctp_bind_addr *bp; 441 rwlock_t *addr_lock; 442 struct sctp_sockaddr_entry *laddr; 443 struct list_head *pos; 444 struct dst_entry *dst = NULL; 445 union sctp_addr dst_saddr; 446 447 memset(&fl, 0x0, sizeof(struct flowi)); 448 fl.fl4_dst = daddr->v4.sin_addr.s_addr; 449 fl.proto = IPPROTO_SCTP; 450 if (asoc) { 451 fl.fl4_tos = RT_CONN_FLAGS(asoc->base.sk); 452 fl.oif = asoc->base.sk->sk_bound_dev_if; 453 } 454 if (saddr) 455 fl.fl4_src = saddr->v4.sin_addr.s_addr; 456 457 SCTP_DEBUG_PRINTK("%s: DST:%u.%u.%u.%u, SRC:%u.%u.%u.%u - ", 458 __FUNCTION__, NIPQUAD(fl.fl4_dst), 459 NIPQUAD(fl.fl4_src)); 460 461 if (!ip_route_output_key(&rt, &fl)) { 462 dst = &rt->u.dst; 463 } 464 465 /* If there is no association or if a source address is passed, no 466 * more validation is required. 467 */ 468 if (!asoc || saddr) 469 goto out; 470 471 bp = &asoc->base.bind_addr; 472 addr_lock = &asoc->base.addr_lock; 473 474 if (dst) { 475 /* Walk through the bind address list and look for a bind 476 * address that matches the source address of the returned dst. 477 */ 478 sctp_read_lock(addr_lock); 479 list_for_each(pos, &bp->address_list) { 480 laddr = list_entry(pos, struct sctp_sockaddr_entry, 481 list); 482 if (!laddr->use_as_src) 483 continue; 484 sctp_v4_dst_saddr(&dst_saddr, dst, htons(bp->port)); 485 if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a)) 486 goto out_unlock; 487 } 488 sctp_read_unlock(addr_lock); 489 490 /* None of the bound addresses match the source address of the 491 * dst. So release it. 492 */ 493 dst_release(dst); 494 dst = NULL; 495 } 496 497 /* Walk through the bind address list and try to get a dst that 498 * matches a bind address as the source address. 499 */ 500 sctp_read_lock(addr_lock); 501 list_for_each(pos, &bp->address_list) { 502 laddr = list_entry(pos, struct sctp_sockaddr_entry, list); 503 504 if ((laddr->use_as_src) && 505 (AF_INET == laddr->a.sa.sa_family)) { 506 fl.fl4_src = laddr->a.v4.sin_addr.s_addr; 507 if (!ip_route_output_key(&rt, &fl)) { 508 dst = &rt->u.dst; 509 goto out_unlock; 510 } 511 } 512 } 513 514 out_unlock: 515 sctp_read_unlock(addr_lock); 516 out: 517 if (dst) 518 SCTP_DEBUG_PRINTK("rt_dst:%u.%u.%u.%u, rt_src:%u.%u.%u.%u\n", 519 NIPQUAD(rt->rt_dst), NIPQUAD(rt->rt_src)); 520 else 521 SCTP_DEBUG_PRINTK("NO ROUTE\n"); 522 523 return dst; 524 } 525 526 /* For v4, the source address is cached in the route entry(dst). So no need 527 * to cache it separately and hence this is an empty routine. 528 */ 529 static void sctp_v4_get_saddr(struct sctp_association *asoc, 530 struct dst_entry *dst, 531 union sctp_addr *daddr, 532 union sctp_addr *saddr) 533 { 534 struct rtable *rt = (struct rtable *)dst; 535 536 if (!asoc) 537 return; 538 539 if (rt) { 540 saddr->v4.sin_family = AF_INET; 541 saddr->v4.sin_port = htons(asoc->base.bind_addr.port); 542 saddr->v4.sin_addr.s_addr = rt->rt_src; 543 } 544 } 545 546 /* What interface did this skb arrive on? */ 547 static int sctp_v4_skb_iif(const struct sk_buff *skb) 548 { 549 return ((struct rtable *)skb->dst)->rt_iif; 550 } 551 552 /* Was this packet marked by Explicit Congestion Notification? */ 553 static int sctp_v4_is_ce(const struct sk_buff *skb) 554 { 555 return INET_ECN_is_ce(skb->nh.iph->tos); 556 } 557 558 /* Create and initialize a new sk for the socket returned by accept(). */ 559 static struct sock *sctp_v4_create_accept_sk(struct sock *sk, 560 struct sctp_association *asoc) 561 { 562 struct inet_sock *inet = inet_sk(sk); 563 struct inet_sock *newinet; 564 struct sock *newsk = sk_alloc(PF_INET, GFP_KERNEL, sk->sk_prot, 1); 565 566 if (!newsk) 567 goto out; 568 569 sock_init_data(NULL, newsk); 570 571 newsk->sk_type = SOCK_STREAM; 572 573 newsk->sk_no_check = sk->sk_no_check; 574 newsk->sk_reuse = sk->sk_reuse; 575 newsk->sk_shutdown = sk->sk_shutdown; 576 577 newsk->sk_destruct = inet_sock_destruct; 578 newsk->sk_family = PF_INET; 579 newsk->sk_protocol = IPPROTO_SCTP; 580 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv; 581 sock_reset_flag(newsk, SOCK_ZAPPED); 582 583 newinet = inet_sk(newsk); 584 585 /* Initialize sk's sport, dport, rcv_saddr and daddr for 586 * getsockname() and getpeername() 587 */ 588 newinet->sport = inet->sport; 589 newinet->saddr = inet->saddr; 590 newinet->rcv_saddr = inet->rcv_saddr; 591 newinet->dport = htons(asoc->peer.port); 592 newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr; 593 newinet->pmtudisc = inet->pmtudisc; 594 newinet->id = asoc->next_tsn ^ jiffies; 595 596 newinet->uc_ttl = -1; 597 newinet->mc_loop = 1; 598 newinet->mc_ttl = 1; 599 newinet->mc_index = 0; 600 newinet->mc_list = NULL; 601 602 sk_refcnt_debug_inc(newsk); 603 604 if (newsk->sk_prot->init(newsk)) { 605 sk_common_release(newsk); 606 newsk = NULL; 607 } 608 609 out: 610 return newsk; 611 } 612 613 /* Map address, empty for v4 family */ 614 static void sctp_v4_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr) 615 { 616 /* Empty */ 617 } 618 619 /* Dump the v4 addr to the seq file. */ 620 static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr) 621 { 622 seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr)); 623 } 624 625 /* Event handler for inet address addition/deletion events. 626 * Basically, whenever there is an event, we re-build our local address list. 627 */ 628 int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev, 629 void *ptr) 630 { 631 unsigned long flags; 632 633 sctp_spin_lock_irqsave(&sctp_local_addr_lock, flags); 634 __sctp_free_local_addr_list(); 635 __sctp_get_local_addr_list(); 636 sctp_spin_unlock_irqrestore(&sctp_local_addr_lock, flags); 637 638 return NOTIFY_DONE; 639 } 640 641 /* 642 * Initialize the control inode/socket with a control endpoint data 643 * structure. This endpoint is reserved exclusively for the OOTB processing. 644 */ 645 static int sctp_ctl_sock_init(void) 646 { 647 int err; 648 sa_family_t family; 649 650 if (sctp_get_pf_specific(PF_INET6)) 651 family = PF_INET6; 652 else 653 family = PF_INET; 654 655 err = sock_create_kern(family, SOCK_SEQPACKET, IPPROTO_SCTP, 656 &sctp_ctl_socket); 657 if (err < 0) { 658 printk(KERN_ERR 659 "SCTP: Failed to create the SCTP control socket.\n"); 660 return err; 661 } 662 sctp_ctl_socket->sk->sk_allocation = GFP_ATOMIC; 663 inet_sk(sctp_ctl_socket->sk)->uc_ttl = -1; 664 665 return 0; 666 } 667 668 /* Register address family specific functions. */ 669 int sctp_register_af(struct sctp_af *af) 670 { 671 switch (af->sa_family) { 672 case AF_INET: 673 if (sctp_af_v4_specific) 674 return 0; 675 sctp_af_v4_specific = af; 676 break; 677 case AF_INET6: 678 if (sctp_af_v6_specific) 679 return 0; 680 sctp_af_v6_specific = af; 681 break; 682 default: 683 return 0; 684 } 685 686 INIT_LIST_HEAD(&af->list); 687 list_add_tail(&af->list, &sctp_address_families); 688 return 1; 689 } 690 691 /* Get the table of functions for manipulating a particular address 692 * family. 693 */ 694 struct sctp_af *sctp_get_af_specific(sa_family_t family) 695 { 696 switch (family) { 697 case AF_INET: 698 return sctp_af_v4_specific; 699 case AF_INET6: 700 return sctp_af_v6_specific; 701 default: 702 return NULL; 703 } 704 } 705 706 /* Common code to initialize a AF_INET msg_name. */ 707 static void sctp_inet_msgname(char *msgname, int *addr_len) 708 { 709 struct sockaddr_in *sin; 710 711 sin = (struct sockaddr_in *)msgname; 712 *addr_len = sizeof(struct sockaddr_in); 713 sin->sin_family = AF_INET; 714 memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 715 } 716 717 /* Copy the primary address of the peer primary address as the msg_name. */ 718 static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname, 719 int *addr_len) 720 { 721 struct sockaddr_in *sin, *sinfrom; 722 723 if (msgname) { 724 struct sctp_association *asoc; 725 726 asoc = event->asoc; 727 sctp_inet_msgname(msgname, addr_len); 728 sin = (struct sockaddr_in *)msgname; 729 sinfrom = &asoc->peer.primary_addr.v4; 730 sin->sin_port = htons(asoc->peer.port); 731 sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr; 732 } 733 } 734 735 /* Initialize and copy out a msgname from an inbound skb. */ 736 static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len) 737 { 738 struct sctphdr *sh; 739 struct sockaddr_in *sin; 740 741 if (msgname) { 742 sctp_inet_msgname(msgname, len); 743 sin = (struct sockaddr_in *)msgname; 744 sh = (struct sctphdr *)skb->h.raw; 745 sin->sin_port = sh->source; 746 sin->sin_addr.s_addr = skb->nh.iph->saddr; 747 } 748 } 749 750 /* Do we support this AF? */ 751 static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp) 752 { 753 /* PF_INET only supports AF_INET addresses. */ 754 return (AF_INET == family); 755 } 756 757 /* Address matching with wildcards allowed. */ 758 static int sctp_inet_cmp_addr(const union sctp_addr *addr1, 759 const union sctp_addr *addr2, 760 struct sctp_sock *opt) 761 { 762 /* PF_INET only supports AF_INET addresses. */ 763 if (addr1->sa.sa_family != addr2->sa.sa_family) 764 return 0; 765 if (INADDR_ANY == addr1->v4.sin_addr.s_addr || 766 INADDR_ANY == addr2->v4.sin_addr.s_addr) 767 return 1; 768 if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr) 769 return 1; 770 771 return 0; 772 } 773 774 /* Verify that provided sockaddr looks bindable. Common verification has 775 * already been taken care of. 776 */ 777 static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr) 778 { 779 return sctp_v4_available(addr, opt); 780 } 781 782 /* Verify that sockaddr looks sendable. Common verification has already 783 * been taken care of. 784 */ 785 static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr) 786 { 787 return 1; 788 } 789 790 /* Fill in Supported Address Type information for INIT and INIT-ACK 791 * chunks. Returns number of addresses supported. 792 */ 793 static int sctp_inet_supported_addrs(const struct sctp_sock *opt, 794 __be16 *types) 795 { 796 types[0] = SCTP_PARAM_IPV4_ADDRESS; 797 return 1; 798 } 799 800 /* Wrapper routine that calls the ip transmit routine. */ 801 static inline int sctp_v4_xmit(struct sk_buff *skb, 802 struct sctp_transport *transport, int ipfragok) 803 { 804 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, " 805 "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n", 806 __FUNCTION__, skb, skb->len, 807 NIPQUAD(((struct rtable *)skb->dst)->rt_src), 808 NIPQUAD(((struct rtable *)skb->dst)->rt_dst)); 809 810 SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS); 811 return ip_queue_xmit(skb, skb->sk, ipfragok); 812 } 813 814 static struct sctp_af sctp_ipv4_specific; 815 816 static struct sctp_pf sctp_pf_inet = { 817 .event_msgname = sctp_inet_event_msgname, 818 .skb_msgname = sctp_inet_skb_msgname, 819 .af_supported = sctp_inet_af_supported, 820 .cmp_addr = sctp_inet_cmp_addr, 821 .bind_verify = sctp_inet_bind_verify, 822 .send_verify = sctp_inet_send_verify, 823 .supported_addrs = sctp_inet_supported_addrs, 824 .create_accept_sk = sctp_v4_create_accept_sk, 825 .addr_v4map = sctp_v4_addr_v4map, 826 .af = &sctp_ipv4_specific, 827 }; 828 829 /* Notifier for inetaddr addition/deletion events. */ 830 static struct notifier_block sctp_inetaddr_notifier = { 831 .notifier_call = sctp_inetaddr_event, 832 }; 833 834 /* Socket operations. */ 835 static const struct proto_ops inet_seqpacket_ops = { 836 .family = PF_INET, 837 .owner = THIS_MODULE, 838 .release = inet_release, /* Needs to be wrapped... */ 839 .bind = inet_bind, 840 .connect = inet_dgram_connect, 841 .socketpair = sock_no_socketpair, 842 .accept = inet_accept, 843 .getname = inet_getname, /* Semantics are different. */ 844 .poll = sctp_poll, 845 .ioctl = inet_ioctl, 846 .listen = sctp_inet_listen, 847 .shutdown = inet_shutdown, /* Looks harmless. */ 848 .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */ 849 .getsockopt = sock_common_getsockopt, 850 .sendmsg = inet_sendmsg, 851 .recvmsg = sock_common_recvmsg, 852 .mmap = sock_no_mmap, 853 .sendpage = sock_no_sendpage, 854 #ifdef CONFIG_COMPAT 855 .compat_setsockopt = compat_sock_common_setsockopt, 856 .compat_getsockopt = compat_sock_common_getsockopt, 857 #endif 858 }; 859 860 /* Registration with AF_INET family. */ 861 static struct inet_protosw sctp_seqpacket_protosw = { 862 .type = SOCK_SEQPACKET, 863 .protocol = IPPROTO_SCTP, 864 .prot = &sctp_prot, 865 .ops = &inet_seqpacket_ops, 866 .capability = -1, 867 .no_check = 0, 868 .flags = SCTP_PROTOSW_FLAG 869 }; 870 static struct inet_protosw sctp_stream_protosw = { 871 .type = SOCK_STREAM, 872 .protocol = IPPROTO_SCTP, 873 .prot = &sctp_prot, 874 .ops = &inet_seqpacket_ops, 875 .capability = -1, 876 .no_check = 0, 877 .flags = SCTP_PROTOSW_FLAG 878 }; 879 880 /* Register with IP layer. */ 881 static struct net_protocol sctp_protocol = { 882 .handler = sctp_rcv, 883 .err_handler = sctp_v4_err, 884 .no_policy = 1, 885 }; 886 887 /* IPv4 address related functions. */ 888 static struct sctp_af sctp_ipv4_specific = { 889 .sa_family = AF_INET, 890 .sctp_xmit = sctp_v4_xmit, 891 .setsockopt = ip_setsockopt, 892 .getsockopt = ip_getsockopt, 893 .get_dst = sctp_v4_get_dst, 894 .get_saddr = sctp_v4_get_saddr, 895 .copy_addrlist = sctp_v4_copy_addrlist, 896 .from_skb = sctp_v4_from_skb, 897 .from_sk = sctp_v4_from_sk, 898 .to_sk_saddr = sctp_v4_to_sk_saddr, 899 .to_sk_daddr = sctp_v4_to_sk_daddr, 900 .from_addr_param = sctp_v4_from_addr_param, 901 .to_addr_param = sctp_v4_to_addr_param, 902 .dst_saddr = sctp_v4_dst_saddr, 903 .cmp_addr = sctp_v4_cmp_addr, 904 .addr_valid = sctp_v4_addr_valid, 905 .inaddr_any = sctp_v4_inaddr_any, 906 .is_any = sctp_v4_is_any, 907 .available = sctp_v4_available, 908 .scope = sctp_v4_scope, 909 .skb_iif = sctp_v4_skb_iif, 910 .is_ce = sctp_v4_is_ce, 911 .seq_dump_addr = sctp_v4_seq_dump_addr, 912 .net_header_len = sizeof(struct iphdr), 913 .sockaddr_len = sizeof(struct sockaddr_in), 914 #ifdef CONFIG_COMPAT 915 .compat_setsockopt = compat_ip_setsockopt, 916 .compat_getsockopt = compat_ip_getsockopt, 917 #endif 918 }; 919 920 struct sctp_pf *sctp_get_pf_specific(sa_family_t family) { 921 922 switch (family) { 923 case PF_INET: 924 return sctp_pf_inet_specific; 925 case PF_INET6: 926 return sctp_pf_inet6_specific; 927 default: 928 return NULL; 929 } 930 } 931 932 /* Register the PF specific function table. */ 933 int sctp_register_pf(struct sctp_pf *pf, sa_family_t family) 934 { 935 switch (family) { 936 case PF_INET: 937 if (sctp_pf_inet_specific) 938 return 0; 939 sctp_pf_inet_specific = pf; 940 break; 941 case PF_INET6: 942 if (sctp_pf_inet6_specific) 943 return 0; 944 sctp_pf_inet6_specific = pf; 945 break; 946 default: 947 return 0; 948 } 949 return 1; 950 } 951 952 static int __init init_sctp_mibs(void) 953 { 954 sctp_statistics[0] = alloc_percpu(struct sctp_mib); 955 if (!sctp_statistics[0]) 956 return -ENOMEM; 957 sctp_statistics[1] = alloc_percpu(struct sctp_mib); 958 if (!sctp_statistics[1]) { 959 free_percpu(sctp_statistics[0]); 960 return -ENOMEM; 961 } 962 return 0; 963 964 } 965 966 static void cleanup_sctp_mibs(void) 967 { 968 free_percpu(sctp_statistics[0]); 969 free_percpu(sctp_statistics[1]); 970 } 971 972 /* Initialize the universe into something sensible. */ 973 SCTP_STATIC __init int sctp_init(void) 974 { 975 int i; 976 int status = -EINVAL; 977 unsigned long goal; 978 int order; 979 980 /* SCTP_DEBUG sanity check. */ 981 if (!sctp_sanity_check()) 982 goto out; 983 984 status = proto_register(&sctp_prot, 1); 985 if (status) 986 goto out; 987 988 /* Add SCTP to inet_protos hash table. */ 989 status = -EAGAIN; 990 if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0) 991 goto err_add_protocol; 992 993 /* Add SCTP(TCP and UDP style) to inetsw linked list. */ 994 inet_register_protosw(&sctp_seqpacket_protosw); 995 inet_register_protosw(&sctp_stream_protosw); 996 997 /* Allocate a cache pools. */ 998 status = -ENOBUFS; 999 sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket", 1000 sizeof(struct sctp_bind_bucket), 1001 0, SLAB_HWCACHE_ALIGN, 1002 NULL, NULL); 1003 1004 if (!sctp_bucket_cachep) 1005 goto err_bucket_cachep; 1006 1007 sctp_chunk_cachep = kmem_cache_create("sctp_chunk", 1008 sizeof(struct sctp_chunk), 1009 0, SLAB_HWCACHE_ALIGN, 1010 NULL, NULL); 1011 if (!sctp_chunk_cachep) 1012 goto err_chunk_cachep; 1013 1014 /* Allocate and initialise sctp mibs. */ 1015 status = init_sctp_mibs(); 1016 if (status) 1017 goto err_init_mibs; 1018 1019 /* Initialize proc fs directory. */ 1020 status = sctp_proc_init(); 1021 if (status) 1022 goto err_init_proc; 1023 1024 /* Initialize object count debugging. */ 1025 sctp_dbg_objcnt_init(); 1026 1027 /* Initialize the SCTP specific PF functions. */ 1028 sctp_register_pf(&sctp_pf_inet, PF_INET); 1029 /* 1030 * 14. Suggested SCTP Protocol Parameter Values 1031 */ 1032 /* The following protocol parameters are RECOMMENDED: */ 1033 /* RTO.Initial - 3 seconds */ 1034 sctp_rto_initial = SCTP_RTO_INITIAL; 1035 /* RTO.Min - 1 second */ 1036 sctp_rto_min = SCTP_RTO_MIN; 1037 /* RTO.Max - 60 seconds */ 1038 sctp_rto_max = SCTP_RTO_MAX; 1039 /* RTO.Alpha - 1/8 */ 1040 sctp_rto_alpha = SCTP_RTO_ALPHA; 1041 /* RTO.Beta - 1/4 */ 1042 sctp_rto_beta = SCTP_RTO_BETA; 1043 1044 /* Valid.Cookie.Life - 60 seconds */ 1045 sctp_valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE; 1046 1047 /* Whether Cookie Preservative is enabled(1) or not(0) */ 1048 sctp_cookie_preserve_enable = 1; 1049 1050 /* Max.Burst - 4 */ 1051 sctp_max_burst = SCTP_MAX_BURST; 1052 1053 /* Association.Max.Retrans - 10 attempts 1054 * Path.Max.Retrans - 5 attempts (per destination address) 1055 * Max.Init.Retransmits - 8 attempts 1056 */ 1057 sctp_max_retrans_association = 10; 1058 sctp_max_retrans_path = 5; 1059 sctp_max_retrans_init = 8; 1060 1061 /* Sendbuffer growth - do per-socket accounting */ 1062 sctp_sndbuf_policy = 0; 1063 1064 /* Rcvbuffer growth - do per-socket accounting */ 1065 sctp_rcvbuf_policy = 0; 1066 1067 /* HB.interval - 30 seconds */ 1068 sctp_hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT; 1069 1070 /* delayed SACK timeout */ 1071 sctp_sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK; 1072 1073 /* Implementation specific variables. */ 1074 1075 /* Initialize default stream count setup information. */ 1076 sctp_max_instreams = SCTP_DEFAULT_INSTREAMS; 1077 sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS; 1078 1079 /* Initialize handle used for association ids. */ 1080 idr_init(&sctp_assocs_id); 1081 1082 /* Size and allocate the association hash table. 1083 * The methodology is similar to that of the tcp hash tables. 1084 */ 1085 if (num_physpages >= (128 * 1024)) 1086 goal = num_physpages >> (22 - PAGE_SHIFT); 1087 else 1088 goal = num_physpages >> (24 - PAGE_SHIFT); 1089 1090 for (order = 0; (1UL << order) < goal; order++) 1091 ; 1092 1093 do { 1094 sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE / 1095 sizeof(struct sctp_hashbucket); 1096 if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0) 1097 continue; 1098 sctp_assoc_hashtable = (struct sctp_hashbucket *) 1099 __get_free_pages(GFP_ATOMIC, order); 1100 } while (!sctp_assoc_hashtable && --order > 0); 1101 if (!sctp_assoc_hashtable) { 1102 printk(KERN_ERR "SCTP: Failed association hash alloc.\n"); 1103 status = -ENOMEM; 1104 goto err_ahash_alloc; 1105 } 1106 for (i = 0; i < sctp_assoc_hashsize; i++) { 1107 rwlock_init(&sctp_assoc_hashtable[i].lock); 1108 sctp_assoc_hashtable[i].chain = NULL; 1109 } 1110 1111 /* Allocate and initialize the endpoint hash table. */ 1112 sctp_ep_hashsize = 64; 1113 sctp_ep_hashtable = (struct sctp_hashbucket *) 1114 kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL); 1115 if (!sctp_ep_hashtable) { 1116 printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n"); 1117 status = -ENOMEM; 1118 goto err_ehash_alloc; 1119 } 1120 for (i = 0; i < sctp_ep_hashsize; i++) { 1121 rwlock_init(&sctp_ep_hashtable[i].lock); 1122 sctp_ep_hashtable[i].chain = NULL; 1123 } 1124 1125 /* Allocate and initialize the SCTP port hash table. */ 1126 do { 1127 sctp_port_hashsize = (1UL << order) * PAGE_SIZE / 1128 sizeof(struct sctp_bind_hashbucket); 1129 if ((sctp_port_hashsize > (64 * 1024)) && order > 0) 1130 continue; 1131 sctp_port_hashtable = (struct sctp_bind_hashbucket *) 1132 __get_free_pages(GFP_ATOMIC, order); 1133 } while (!sctp_port_hashtable && --order > 0); 1134 if (!sctp_port_hashtable) { 1135 printk(KERN_ERR "SCTP: Failed bind hash alloc."); 1136 status = -ENOMEM; 1137 goto err_bhash_alloc; 1138 } 1139 for (i = 0; i < sctp_port_hashsize; i++) { 1140 spin_lock_init(&sctp_port_hashtable[i].lock); 1141 sctp_port_hashtable[i].chain = NULL; 1142 } 1143 1144 spin_lock_init(&sctp_port_alloc_lock); 1145 sctp_port_rover = sysctl_local_port_range[0] - 1; 1146 1147 printk(KERN_INFO "SCTP: Hash tables configured " 1148 "(established %d bind %d)\n", 1149 sctp_assoc_hashsize, sctp_port_hashsize); 1150 1151 /* Disable ADDIP by default. */ 1152 sctp_addip_enable = 0; 1153 1154 /* Enable PR-SCTP by default. */ 1155 sctp_prsctp_enable = 1; 1156 1157 sctp_sysctl_register(); 1158 1159 INIT_LIST_HEAD(&sctp_address_families); 1160 sctp_register_af(&sctp_ipv4_specific); 1161 1162 status = sctp_v6_init(); 1163 if (status) 1164 goto err_v6_init; 1165 1166 /* Initialize the control inode/socket for handling OOTB packets. */ 1167 if ((status = sctp_ctl_sock_init())) { 1168 printk (KERN_ERR 1169 "SCTP: Failed to initialize the SCTP control sock.\n"); 1170 goto err_ctl_sock_init; 1171 } 1172 1173 /* Initialize the local address list. */ 1174 INIT_LIST_HEAD(&sctp_local_addr_list); 1175 spin_lock_init(&sctp_local_addr_lock); 1176 1177 /* Register notifier for inet address additions/deletions. */ 1178 register_inetaddr_notifier(&sctp_inetaddr_notifier); 1179 1180 sctp_get_local_addr_list(); 1181 1182 __unsafe(THIS_MODULE); 1183 status = 0; 1184 out: 1185 return status; 1186 err_ctl_sock_init: 1187 sctp_v6_exit(); 1188 err_v6_init: 1189 sctp_sysctl_unregister(); 1190 list_del(&sctp_ipv4_specific.list); 1191 free_pages((unsigned long)sctp_port_hashtable, 1192 get_order(sctp_port_hashsize * 1193 sizeof(struct sctp_bind_hashbucket))); 1194 err_bhash_alloc: 1195 kfree(sctp_ep_hashtable); 1196 err_ehash_alloc: 1197 free_pages((unsigned long)sctp_assoc_hashtable, 1198 get_order(sctp_assoc_hashsize * 1199 sizeof(struct sctp_hashbucket))); 1200 err_ahash_alloc: 1201 sctp_dbg_objcnt_exit(); 1202 err_init_proc: 1203 sctp_proc_exit(); 1204 cleanup_sctp_mibs(); 1205 err_init_mibs: 1206 kmem_cache_destroy(sctp_chunk_cachep); 1207 err_chunk_cachep: 1208 kmem_cache_destroy(sctp_bucket_cachep); 1209 err_bucket_cachep: 1210 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP); 1211 inet_unregister_protosw(&sctp_seqpacket_protosw); 1212 inet_unregister_protosw(&sctp_stream_protosw); 1213 err_add_protocol: 1214 proto_unregister(&sctp_prot); 1215 goto out; 1216 } 1217 1218 /* Exit handler for the SCTP protocol. */ 1219 SCTP_STATIC __exit void sctp_exit(void) 1220 { 1221 /* BUG. This should probably do something useful like clean 1222 * up all the remaining associations and all that memory. 1223 */ 1224 1225 /* Unregister notifier for inet address additions/deletions. */ 1226 unregister_inetaddr_notifier(&sctp_inetaddr_notifier); 1227 1228 /* Free the local address list. */ 1229 sctp_free_local_addr_list(); 1230 1231 /* Free the control endpoint. */ 1232 sock_release(sctp_ctl_socket); 1233 1234 sctp_v6_exit(); 1235 sctp_sysctl_unregister(); 1236 list_del(&sctp_ipv4_specific.list); 1237 1238 free_pages((unsigned long)sctp_assoc_hashtable, 1239 get_order(sctp_assoc_hashsize * 1240 sizeof(struct sctp_hashbucket))); 1241 kfree(sctp_ep_hashtable); 1242 free_pages((unsigned long)sctp_port_hashtable, 1243 get_order(sctp_port_hashsize * 1244 sizeof(struct sctp_bind_hashbucket))); 1245 1246 kmem_cache_destroy(sctp_chunk_cachep); 1247 kmem_cache_destroy(sctp_bucket_cachep); 1248 1249 sctp_dbg_objcnt_exit(); 1250 sctp_proc_exit(); 1251 cleanup_sctp_mibs(); 1252 1253 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP); 1254 inet_unregister_protosw(&sctp_seqpacket_protosw); 1255 inet_unregister_protosw(&sctp_stream_protosw); 1256 proto_unregister(&sctp_prot); 1257 } 1258 1259 module_init(sctp_init); 1260 module_exit(sctp_exit); 1261 1262 /* 1263 * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly. 1264 */ 1265 MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132"); 1266 MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>"); 1267 MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)"); 1268 MODULE_LICENSE("GPL"); 1269