protocol.c 36 KB

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