protocol.c 36 KB

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