protocol.c 37 KB

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