protocol.c 37 KB

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