protocol.c 36 KB

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