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