protocol.c 37 KB

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