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