protocol.c 37 KB

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