protocol.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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_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 && skb->rtable->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. __func__, 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. sctp_v4_dst_saddr(&dst_saddr, dst, htons(bp->port));
  415. rcu_read_lock();
  416. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  417. if (!laddr->valid || (laddr->state != SCTP_ADDR_SRC))
  418. continue;
  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_sock *sk,
  459. struct sctp_association *asoc,
  460. struct dst_entry *dst,
  461. union sctp_addr *daddr,
  462. union sctp_addr *saddr)
  463. {
  464. struct rtable *rt = (struct rtable *)dst;
  465. if (!asoc)
  466. return;
  467. if (rt) {
  468. saddr->v4.sin_family = AF_INET;
  469. saddr->v4.sin_port = htons(asoc->base.bind_addr.port);
  470. saddr->v4.sin_addr.s_addr = rt->rt_src;
  471. }
  472. }
  473. /* What interface did this skb arrive on? */
  474. static int sctp_v4_skb_iif(const struct sk_buff *skb)
  475. {
  476. return skb->rtable->rt_iif;
  477. }
  478. /* Was this packet marked by Explicit Congestion Notification? */
  479. static int sctp_v4_is_ce(const struct sk_buff *skb)
  480. {
  481. return INET_ECN_is_ce(ip_hdr(skb)->tos);
  482. }
  483. /* Create and initialize a new sk for the socket returned by accept(). */
  484. static struct sock *sctp_v4_create_accept_sk(struct sock *sk,
  485. struct sctp_association *asoc)
  486. {
  487. struct inet_sock *inet = inet_sk(sk);
  488. struct inet_sock *newinet;
  489. struct sock *newsk = sk_alloc(sock_net(sk), PF_INET, GFP_KERNEL,
  490. sk->sk_prot);
  491. if (!newsk)
  492. goto out;
  493. sock_init_data(NULL, newsk);
  494. newsk->sk_type = SOCK_STREAM;
  495. newsk->sk_no_check = sk->sk_no_check;
  496. newsk->sk_reuse = sk->sk_reuse;
  497. newsk->sk_shutdown = sk->sk_shutdown;
  498. newsk->sk_destruct = inet_sock_destruct;
  499. newsk->sk_family = PF_INET;
  500. newsk->sk_protocol = IPPROTO_SCTP;
  501. newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  502. sock_reset_flag(newsk, SOCK_ZAPPED);
  503. newinet = inet_sk(newsk);
  504. /* Initialize sk's sport, dport, rcv_saddr and daddr for
  505. * getsockname() and getpeername()
  506. */
  507. newinet->sport = inet->sport;
  508. newinet->saddr = inet->saddr;
  509. newinet->rcv_saddr = inet->rcv_saddr;
  510. newinet->dport = htons(asoc->peer.port);
  511. newinet->daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
  512. newinet->pmtudisc = inet->pmtudisc;
  513. newinet->id = asoc->next_tsn ^ jiffies;
  514. newinet->uc_ttl = -1;
  515. newinet->mc_loop = 1;
  516. newinet->mc_ttl = 1;
  517. newinet->mc_index = 0;
  518. newinet->mc_list = NULL;
  519. sk_refcnt_debug_inc(newsk);
  520. if (newsk->sk_prot->init(newsk)) {
  521. sk_common_release(newsk);
  522. newsk = NULL;
  523. }
  524. out:
  525. return newsk;
  526. }
  527. /* Map address, empty for v4 family */
  528. static void sctp_v4_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
  529. {
  530. /* Empty */
  531. }
  532. /* Dump the v4 addr to the seq file. */
  533. static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
  534. {
  535. seq_printf(seq, "%d.%d.%d.%d ", NIPQUAD(addr->v4.sin_addr));
  536. }
  537. static void sctp_v4_ecn_capable(struct sock *sk)
  538. {
  539. INET_ECN_xmit(sk);
  540. }
  541. /* Event handler for inet address addition/deletion events.
  542. * The sctp_local_addr_list needs to be protocted by a spin lock since
  543. * multiple notifiers (say IPv4 and IPv6) may be running at the same
  544. * time and thus corrupt the list.
  545. * The reader side is protected with RCU.
  546. */
  547. static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
  548. void *ptr)
  549. {
  550. struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
  551. struct sctp_sockaddr_entry *addr = NULL;
  552. struct sctp_sockaddr_entry *temp;
  553. int found = 0;
  554. if (dev_net(ifa->ifa_dev->dev) != &init_net)
  555. return NOTIFY_DONE;
  556. switch (ev) {
  557. case NETDEV_UP:
  558. addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
  559. if (addr) {
  560. addr->a.v4.sin_family = AF_INET;
  561. addr->a.v4.sin_port = 0;
  562. addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
  563. addr->valid = 1;
  564. spin_lock_bh(&sctp_local_addr_lock);
  565. list_add_tail_rcu(&addr->list, &sctp_local_addr_list);
  566. spin_unlock_bh(&sctp_local_addr_lock);
  567. }
  568. break;
  569. case NETDEV_DOWN:
  570. spin_lock_bh(&sctp_local_addr_lock);
  571. list_for_each_entry_safe(addr, temp,
  572. &sctp_local_addr_list, list) {
  573. if (addr->a.sa.sa_family == AF_INET &&
  574. addr->a.v4.sin_addr.s_addr ==
  575. ifa->ifa_local) {
  576. found = 1;
  577. addr->valid = 0;
  578. list_del_rcu(&addr->list);
  579. break;
  580. }
  581. }
  582. spin_unlock_bh(&sctp_local_addr_lock);
  583. if (found)
  584. call_rcu(&addr->rcu, sctp_local_addr_free);
  585. break;
  586. }
  587. return NOTIFY_DONE;
  588. }
  589. /*
  590. * Initialize the control inode/socket with a control endpoint data
  591. * structure. This endpoint is reserved exclusively for the OOTB processing.
  592. */
  593. static int sctp_ctl_sock_init(void)
  594. {
  595. int err;
  596. sa_family_t family;
  597. if (sctp_get_pf_specific(PF_INET6))
  598. family = PF_INET6;
  599. else
  600. family = PF_INET;
  601. err = inet_ctl_sock_create(&sctp_ctl_sock, family,
  602. SOCK_SEQPACKET, IPPROTO_SCTP, &init_net);
  603. if (err < 0) {
  604. printk(KERN_ERR
  605. "SCTP: Failed to create the SCTP control socket.\n");
  606. return err;
  607. }
  608. return 0;
  609. }
  610. /* Register address family specific functions. */
  611. int sctp_register_af(struct sctp_af *af)
  612. {
  613. switch (af->sa_family) {
  614. case AF_INET:
  615. if (sctp_af_v4_specific)
  616. return 0;
  617. sctp_af_v4_specific = af;
  618. break;
  619. case AF_INET6:
  620. if (sctp_af_v6_specific)
  621. return 0;
  622. sctp_af_v6_specific = af;
  623. break;
  624. default:
  625. return 0;
  626. }
  627. INIT_LIST_HEAD(&af->list);
  628. list_add_tail(&af->list, &sctp_address_families);
  629. return 1;
  630. }
  631. /* Get the table of functions for manipulating a particular address
  632. * family.
  633. */
  634. struct sctp_af *sctp_get_af_specific(sa_family_t family)
  635. {
  636. switch (family) {
  637. case AF_INET:
  638. return sctp_af_v4_specific;
  639. case AF_INET6:
  640. return sctp_af_v6_specific;
  641. default:
  642. return NULL;
  643. }
  644. }
  645. /* Common code to initialize a AF_INET msg_name. */
  646. static void sctp_inet_msgname(char *msgname, int *addr_len)
  647. {
  648. struct sockaddr_in *sin;
  649. sin = (struct sockaddr_in *)msgname;
  650. *addr_len = sizeof(struct sockaddr_in);
  651. sin->sin_family = AF_INET;
  652. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  653. }
  654. /* Copy the primary address of the peer primary address as the msg_name. */
  655. static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname,
  656. int *addr_len)
  657. {
  658. struct sockaddr_in *sin, *sinfrom;
  659. if (msgname) {
  660. struct sctp_association *asoc;
  661. asoc = event->asoc;
  662. sctp_inet_msgname(msgname, addr_len);
  663. sin = (struct sockaddr_in *)msgname;
  664. sinfrom = &asoc->peer.primary_addr.v4;
  665. sin->sin_port = htons(asoc->peer.port);
  666. sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr;
  667. }
  668. }
  669. /* Initialize and copy out a msgname from an inbound skb. */
  670. static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len)
  671. {
  672. if (msgname) {
  673. struct sctphdr *sh = sctp_hdr(skb);
  674. struct sockaddr_in *sin = (struct sockaddr_in *)msgname;
  675. sctp_inet_msgname(msgname, len);
  676. sin->sin_port = sh->source;
  677. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  678. }
  679. }
  680. /* Do we support this AF? */
  681. static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp)
  682. {
  683. /* PF_INET only supports AF_INET addresses. */
  684. return (AF_INET == family);
  685. }
  686. /* Address matching with wildcards allowed. */
  687. static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
  688. const union sctp_addr *addr2,
  689. struct sctp_sock *opt)
  690. {
  691. /* PF_INET only supports AF_INET addresses. */
  692. if (addr1->sa.sa_family != addr2->sa.sa_family)
  693. return 0;
  694. if (htonl(INADDR_ANY) == addr1->v4.sin_addr.s_addr ||
  695. htonl(INADDR_ANY) == addr2->v4.sin_addr.s_addr)
  696. return 1;
  697. if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
  698. return 1;
  699. return 0;
  700. }
  701. /* Verify that provided sockaddr looks bindable. Common verification has
  702. * already been taken care of.
  703. */
  704. static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
  705. {
  706. return sctp_v4_available(addr, opt);
  707. }
  708. /* Verify that sockaddr looks sendable. Common verification has already
  709. * been taken care of.
  710. */
  711. static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
  712. {
  713. return 1;
  714. }
  715. /* Fill in Supported Address Type information for INIT and INIT-ACK
  716. * chunks. Returns number of addresses supported.
  717. */
  718. static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
  719. __be16 *types)
  720. {
  721. types[0] = SCTP_PARAM_IPV4_ADDRESS;
  722. return 1;
  723. }
  724. /* Wrapper routine that calls the ip transmit routine. */
  725. static inline int sctp_v4_xmit(struct sk_buff *skb,
  726. struct sctp_transport *transport, int ipfragok)
  727. {
  728. SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
  729. "src:%u.%u.%u.%u, dst:%u.%u.%u.%u\n",
  730. __func__, skb, skb->len,
  731. NIPQUAD(skb->rtable->rt_src),
  732. NIPQUAD(skb->rtable->rt_dst));
  733. SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
  734. return ip_queue_xmit(skb, ipfragok);
  735. }
  736. static struct sctp_af sctp_af_inet;
  737. static struct sctp_pf sctp_pf_inet = {
  738. .event_msgname = sctp_inet_event_msgname,
  739. .skb_msgname = sctp_inet_skb_msgname,
  740. .af_supported = sctp_inet_af_supported,
  741. .cmp_addr = sctp_inet_cmp_addr,
  742. .bind_verify = sctp_inet_bind_verify,
  743. .send_verify = sctp_inet_send_verify,
  744. .supported_addrs = sctp_inet_supported_addrs,
  745. .create_accept_sk = sctp_v4_create_accept_sk,
  746. .addr_v4map = sctp_v4_addr_v4map,
  747. .af = &sctp_af_inet
  748. };
  749. /* Notifier for inetaddr addition/deletion events. */
  750. static struct notifier_block sctp_inetaddr_notifier = {
  751. .notifier_call = sctp_inetaddr_event,
  752. };
  753. /* Socket operations. */
  754. static const struct proto_ops inet_seqpacket_ops = {
  755. .family = PF_INET,
  756. .owner = THIS_MODULE,
  757. .release = inet_release, /* Needs to be wrapped... */
  758. .bind = inet_bind,
  759. .connect = inet_dgram_connect,
  760. .socketpair = sock_no_socketpair,
  761. .accept = inet_accept,
  762. .getname = inet_getname, /* Semantics are different. */
  763. .poll = sctp_poll,
  764. .ioctl = inet_ioctl,
  765. .listen = sctp_inet_listen,
  766. .shutdown = inet_shutdown, /* Looks harmless. */
  767. .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */
  768. .getsockopt = sock_common_getsockopt,
  769. .sendmsg = inet_sendmsg,
  770. .recvmsg = sock_common_recvmsg,
  771. .mmap = sock_no_mmap,
  772. .sendpage = sock_no_sendpage,
  773. #ifdef CONFIG_COMPAT
  774. .compat_setsockopt = compat_sock_common_setsockopt,
  775. .compat_getsockopt = compat_sock_common_getsockopt,
  776. #endif
  777. };
  778. /* Registration with AF_INET family. */
  779. static struct inet_protosw sctp_seqpacket_protosw = {
  780. .type = SOCK_SEQPACKET,
  781. .protocol = IPPROTO_SCTP,
  782. .prot = &sctp_prot,
  783. .ops = &inet_seqpacket_ops,
  784. .capability = -1,
  785. .no_check = 0,
  786. .flags = SCTP_PROTOSW_FLAG
  787. };
  788. static struct inet_protosw sctp_stream_protosw = {
  789. .type = SOCK_STREAM,
  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. /* Register with IP layer. */
  798. static struct net_protocol sctp_protocol = {
  799. .handler = sctp_rcv,
  800. .err_handler = sctp_v4_err,
  801. .no_policy = 1,
  802. };
  803. /* IPv4 address related functions. */
  804. static struct sctp_af sctp_af_inet = {
  805. .sa_family = AF_INET,
  806. .sctp_xmit = sctp_v4_xmit,
  807. .setsockopt = ip_setsockopt,
  808. .getsockopt = ip_getsockopt,
  809. .get_dst = sctp_v4_get_dst,
  810. .get_saddr = sctp_v4_get_saddr,
  811. .copy_addrlist = sctp_v4_copy_addrlist,
  812. .from_skb = sctp_v4_from_skb,
  813. .from_sk = sctp_v4_from_sk,
  814. .to_sk_saddr = sctp_v4_to_sk_saddr,
  815. .to_sk_daddr = sctp_v4_to_sk_daddr,
  816. .from_addr_param = sctp_v4_from_addr_param,
  817. .to_addr_param = sctp_v4_to_addr_param,
  818. .dst_saddr = sctp_v4_dst_saddr,
  819. .cmp_addr = sctp_v4_cmp_addr,
  820. .addr_valid = sctp_v4_addr_valid,
  821. .inaddr_any = sctp_v4_inaddr_any,
  822. .is_any = sctp_v4_is_any,
  823. .available = sctp_v4_available,
  824. .scope = sctp_v4_scope,
  825. .skb_iif = sctp_v4_skb_iif,
  826. .is_ce = sctp_v4_is_ce,
  827. .seq_dump_addr = sctp_v4_seq_dump_addr,
  828. .ecn_capable = sctp_v4_ecn_capable,
  829. .net_header_len = sizeof(struct iphdr),
  830. .sockaddr_len = sizeof(struct sockaddr_in),
  831. #ifdef CONFIG_COMPAT
  832. .compat_setsockopt = compat_ip_setsockopt,
  833. .compat_getsockopt = compat_ip_getsockopt,
  834. #endif
  835. };
  836. struct sctp_pf *sctp_get_pf_specific(sa_family_t family) {
  837. switch (family) {
  838. case PF_INET:
  839. return sctp_pf_inet_specific;
  840. case PF_INET6:
  841. return sctp_pf_inet6_specific;
  842. default:
  843. return NULL;
  844. }
  845. }
  846. /* Register the PF specific function table. */
  847. int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
  848. {
  849. switch (family) {
  850. case PF_INET:
  851. if (sctp_pf_inet_specific)
  852. return 0;
  853. sctp_pf_inet_specific = pf;
  854. break;
  855. case PF_INET6:
  856. if (sctp_pf_inet6_specific)
  857. return 0;
  858. sctp_pf_inet6_specific = pf;
  859. break;
  860. default:
  861. return 0;
  862. }
  863. return 1;
  864. }
  865. static inline int init_sctp_mibs(void)
  866. {
  867. return snmp_mib_init((void**)sctp_statistics, sizeof(struct sctp_mib));
  868. }
  869. static inline void cleanup_sctp_mibs(void)
  870. {
  871. snmp_mib_free((void**)sctp_statistics);
  872. }
  873. static void sctp_v4_pf_init(void)
  874. {
  875. /* Initialize the SCTP specific PF functions. */
  876. sctp_register_pf(&sctp_pf_inet, PF_INET);
  877. sctp_register_af(&sctp_af_inet);
  878. }
  879. static void sctp_v4_pf_exit(void)
  880. {
  881. list_del(&sctp_af_inet.list);
  882. }
  883. static int sctp_v4_protosw_init(void)
  884. {
  885. int rc;
  886. rc = proto_register(&sctp_prot, 1);
  887. if (rc)
  888. return rc;
  889. /* Register SCTP(UDP and TCP style) with socket layer. */
  890. inet_register_protosw(&sctp_seqpacket_protosw);
  891. inet_register_protosw(&sctp_stream_protosw);
  892. return 0;
  893. }
  894. static void sctp_v4_protosw_exit(void)
  895. {
  896. inet_unregister_protosw(&sctp_stream_protosw);
  897. inet_unregister_protosw(&sctp_seqpacket_protosw);
  898. proto_unregister(&sctp_prot);
  899. }
  900. static int sctp_v4_add_protocol(void)
  901. {
  902. /* Register notifier for inet address additions/deletions. */
  903. register_inetaddr_notifier(&sctp_inetaddr_notifier);
  904. /* Register SCTP with inet layer. */
  905. if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
  906. return -EAGAIN;
  907. return 0;
  908. }
  909. static void sctp_v4_del_protocol(void)
  910. {
  911. inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
  912. unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
  913. }
  914. /* Initialize the universe into something sensible. */
  915. SCTP_STATIC __init int sctp_init(void)
  916. {
  917. int i;
  918. int status = -EINVAL;
  919. unsigned long goal;
  920. unsigned long limit;
  921. int max_share;
  922. int order;
  923. /* SCTP_DEBUG sanity check. */
  924. if (!sctp_sanity_check())
  925. goto out;
  926. /* Allocate bind_bucket and chunk caches. */
  927. status = -ENOBUFS;
  928. sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
  929. sizeof(struct sctp_bind_bucket),
  930. 0, SLAB_HWCACHE_ALIGN,
  931. NULL);
  932. if (!sctp_bucket_cachep)
  933. goto out;
  934. sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
  935. sizeof(struct sctp_chunk),
  936. 0, SLAB_HWCACHE_ALIGN,
  937. NULL);
  938. if (!sctp_chunk_cachep)
  939. goto err_chunk_cachep;
  940. /* Allocate and initialise sctp mibs. */
  941. status = init_sctp_mibs();
  942. if (status)
  943. goto err_init_mibs;
  944. /* Initialize proc fs directory. */
  945. status = sctp_proc_init();
  946. if (status)
  947. goto err_init_proc;
  948. /* Initialize object count debugging. */
  949. sctp_dbg_objcnt_init();
  950. /*
  951. * 14. Suggested SCTP Protocol Parameter Values
  952. */
  953. /* The following protocol parameters are RECOMMENDED: */
  954. /* RTO.Initial - 3 seconds */
  955. sctp_rto_initial = SCTP_RTO_INITIAL;
  956. /* RTO.Min - 1 second */
  957. sctp_rto_min = SCTP_RTO_MIN;
  958. /* RTO.Max - 60 seconds */
  959. sctp_rto_max = SCTP_RTO_MAX;
  960. /* RTO.Alpha - 1/8 */
  961. sctp_rto_alpha = SCTP_RTO_ALPHA;
  962. /* RTO.Beta - 1/4 */
  963. sctp_rto_beta = SCTP_RTO_BETA;
  964. /* Valid.Cookie.Life - 60 seconds */
  965. sctp_valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE;
  966. /* Whether Cookie Preservative is enabled(1) or not(0) */
  967. sctp_cookie_preserve_enable = 1;
  968. /* Max.Burst - 4 */
  969. sctp_max_burst = SCTP_DEFAULT_MAX_BURST;
  970. /* Association.Max.Retrans - 10 attempts
  971. * Path.Max.Retrans - 5 attempts (per destination address)
  972. * Max.Init.Retransmits - 8 attempts
  973. */
  974. sctp_max_retrans_association = 10;
  975. sctp_max_retrans_path = 5;
  976. sctp_max_retrans_init = 8;
  977. /* Sendbuffer growth - do per-socket accounting */
  978. sctp_sndbuf_policy = 0;
  979. /* Rcvbuffer growth - do per-socket accounting */
  980. sctp_rcvbuf_policy = 0;
  981. /* HB.interval - 30 seconds */
  982. sctp_hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
  983. /* delayed SACK timeout */
  984. sctp_sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK;
  985. /* Implementation specific variables. */
  986. /* Initialize default stream count setup information. */
  987. sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
  988. sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
  989. /* Initialize handle used for association ids. */
  990. idr_init(&sctp_assocs_id);
  991. /* Set the pressure threshold to be a fraction of global memory that
  992. * is up to 1/2 at 256 MB, decreasing toward zero with the amount of
  993. * memory, with a floor of 128 pages.
  994. * Note this initalizes the data in sctpv6_prot too
  995. * Unabashedly stolen from tcp_init
  996. */
  997. limit = min(num_physpages, 1UL<<(28-PAGE_SHIFT)) >> (20-PAGE_SHIFT);
  998. limit = (limit * (num_physpages >> (20-PAGE_SHIFT))) >> (PAGE_SHIFT-11);
  999. limit = max(limit, 128UL);
  1000. sysctl_sctp_mem[0] = limit / 4 * 3;
  1001. sysctl_sctp_mem[1] = limit;
  1002. sysctl_sctp_mem[2] = sysctl_sctp_mem[0] * 2;
  1003. /* Set per-socket limits to no more than 1/128 the pressure threshold*/
  1004. limit = (sysctl_sctp_mem[1]) << (PAGE_SHIFT - 7);
  1005. max_share = min(4UL*1024*1024, limit);
  1006. sysctl_sctp_rmem[0] = PAGE_SIZE; /* give each asoc 1 page min */
  1007. sysctl_sctp_rmem[1] = (1500 *(sizeof(struct sk_buff) + 1));
  1008. sysctl_sctp_rmem[2] = max(sysctl_sctp_rmem[1], max_share);
  1009. sysctl_sctp_wmem[0] = SK_MEM_QUANTUM;
  1010. sysctl_sctp_wmem[1] = 16*1024;
  1011. sysctl_sctp_wmem[2] = max(64*1024, max_share);
  1012. /* Size and allocate the association hash table.
  1013. * The methodology is similar to that of the tcp hash tables.
  1014. */
  1015. if (num_physpages >= (128 * 1024))
  1016. goal = num_physpages >> (22 - PAGE_SHIFT);
  1017. else
  1018. goal = num_physpages >> (24 - PAGE_SHIFT);
  1019. for (order = 0; (1UL << order) < goal; order++)
  1020. ;
  1021. do {
  1022. sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE /
  1023. sizeof(struct sctp_hashbucket);
  1024. if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0)
  1025. continue;
  1026. sctp_assoc_hashtable = (struct sctp_hashbucket *)
  1027. __get_free_pages(GFP_ATOMIC, order);
  1028. } while (!sctp_assoc_hashtable && --order > 0);
  1029. if (!sctp_assoc_hashtable) {
  1030. printk(KERN_ERR "SCTP: Failed association hash alloc.\n");
  1031. status = -ENOMEM;
  1032. goto err_ahash_alloc;
  1033. }
  1034. for (i = 0; i < sctp_assoc_hashsize; i++) {
  1035. rwlock_init(&sctp_assoc_hashtable[i].lock);
  1036. INIT_HLIST_HEAD(&sctp_assoc_hashtable[i].chain);
  1037. }
  1038. /* Allocate and initialize the endpoint hash table. */
  1039. sctp_ep_hashsize = 64;
  1040. sctp_ep_hashtable = (struct sctp_hashbucket *)
  1041. kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL);
  1042. if (!sctp_ep_hashtable) {
  1043. printk(KERN_ERR "SCTP: Failed endpoint_hash alloc.\n");
  1044. status = -ENOMEM;
  1045. goto err_ehash_alloc;
  1046. }
  1047. for (i = 0; i < sctp_ep_hashsize; i++) {
  1048. rwlock_init(&sctp_ep_hashtable[i].lock);
  1049. INIT_HLIST_HEAD(&sctp_ep_hashtable[i].chain);
  1050. }
  1051. /* Allocate and initialize the SCTP port hash table. */
  1052. do {
  1053. sctp_port_hashsize = (1UL << order) * PAGE_SIZE /
  1054. sizeof(struct sctp_bind_hashbucket);
  1055. if ((sctp_port_hashsize > (64 * 1024)) && order > 0)
  1056. continue;
  1057. sctp_port_hashtable = (struct sctp_bind_hashbucket *)
  1058. __get_free_pages(GFP_ATOMIC, order);
  1059. } while (!sctp_port_hashtable && --order > 0);
  1060. if (!sctp_port_hashtable) {
  1061. printk(KERN_ERR "SCTP: Failed bind hash alloc.");
  1062. status = -ENOMEM;
  1063. goto err_bhash_alloc;
  1064. }
  1065. for (i = 0; i < sctp_port_hashsize; i++) {
  1066. spin_lock_init(&sctp_port_hashtable[i].lock);
  1067. INIT_HLIST_HEAD(&sctp_port_hashtable[i].chain);
  1068. }
  1069. printk(KERN_INFO "SCTP: Hash tables configured "
  1070. "(established %d bind %d)\n",
  1071. sctp_assoc_hashsize, sctp_port_hashsize);
  1072. /* Disable ADDIP by default. */
  1073. sctp_addip_enable = 0;
  1074. sctp_addip_noauth = 0;
  1075. /* Enable PR-SCTP by default. */
  1076. sctp_prsctp_enable = 1;
  1077. /* Disable AUTH by default. */
  1078. sctp_auth_enable = 0;
  1079. sctp_sysctl_register();
  1080. INIT_LIST_HEAD(&sctp_address_families);
  1081. sctp_v4_pf_init();
  1082. sctp_v6_pf_init();
  1083. /* Initialize the local address list. */
  1084. INIT_LIST_HEAD(&sctp_local_addr_list);
  1085. spin_lock_init(&sctp_local_addr_lock);
  1086. sctp_get_local_addr_list();
  1087. status = sctp_v4_protosw_init();
  1088. if (status)
  1089. goto err_protosw_init;
  1090. status = sctp_v6_protosw_init();
  1091. if (status)
  1092. goto err_v6_protosw_init;
  1093. /* Initialize the control inode/socket for handling OOTB packets. */
  1094. if ((status = sctp_ctl_sock_init())) {
  1095. printk (KERN_ERR
  1096. "SCTP: Failed to initialize the SCTP control sock.\n");
  1097. goto err_ctl_sock_init;
  1098. }
  1099. status = sctp_v4_add_protocol();
  1100. if (status)
  1101. goto err_add_protocol;
  1102. /* Register SCTP with inet6 layer. */
  1103. status = sctp_v6_add_protocol();
  1104. if (status)
  1105. goto err_v6_add_protocol;
  1106. status = 0;
  1107. out:
  1108. return status;
  1109. err_v6_add_protocol:
  1110. sctp_v6_del_protocol();
  1111. err_add_protocol:
  1112. sctp_v4_del_protocol();
  1113. inet_ctl_sock_destroy(sctp_ctl_sock);
  1114. err_ctl_sock_init:
  1115. sctp_v6_protosw_exit();
  1116. err_v6_protosw_init:
  1117. sctp_v4_protosw_exit();
  1118. err_protosw_init:
  1119. sctp_free_local_addr_list();
  1120. sctp_v4_pf_exit();
  1121. sctp_v6_pf_exit();
  1122. sctp_sysctl_unregister();
  1123. list_del(&sctp_af_inet.list);
  1124. free_pages((unsigned long)sctp_port_hashtable,
  1125. get_order(sctp_port_hashsize *
  1126. sizeof(struct sctp_bind_hashbucket)));
  1127. err_bhash_alloc:
  1128. kfree(sctp_ep_hashtable);
  1129. err_ehash_alloc:
  1130. free_pages((unsigned long)sctp_assoc_hashtable,
  1131. get_order(sctp_assoc_hashsize *
  1132. sizeof(struct sctp_hashbucket)));
  1133. err_ahash_alloc:
  1134. sctp_dbg_objcnt_exit();
  1135. sctp_proc_exit();
  1136. err_init_proc:
  1137. cleanup_sctp_mibs();
  1138. err_init_mibs:
  1139. kmem_cache_destroy(sctp_chunk_cachep);
  1140. err_chunk_cachep:
  1141. kmem_cache_destroy(sctp_bucket_cachep);
  1142. goto out;
  1143. }
  1144. /* Exit handler for the SCTP protocol. */
  1145. SCTP_STATIC __exit void sctp_exit(void)
  1146. {
  1147. /* BUG. This should probably do something useful like clean
  1148. * up all the remaining associations and all that memory.
  1149. */
  1150. /* Unregister with inet6/inet layers. */
  1151. sctp_v6_del_protocol();
  1152. sctp_v4_del_protocol();
  1153. /* Free the control endpoint. */
  1154. inet_ctl_sock_destroy(sctp_ctl_sock);
  1155. /* Free protosw registrations */
  1156. sctp_v6_protosw_exit();
  1157. sctp_v4_protosw_exit();
  1158. /* Free the local address list. */
  1159. sctp_free_local_addr_list();
  1160. /* Unregister with socket layer. */
  1161. sctp_v6_pf_exit();
  1162. sctp_v4_pf_exit();
  1163. sctp_sysctl_unregister();
  1164. list_del(&sctp_af_inet.list);
  1165. free_pages((unsigned long)sctp_assoc_hashtable,
  1166. get_order(sctp_assoc_hashsize *
  1167. sizeof(struct sctp_hashbucket)));
  1168. kfree(sctp_ep_hashtable);
  1169. free_pages((unsigned long)sctp_port_hashtable,
  1170. get_order(sctp_port_hashsize *
  1171. sizeof(struct sctp_bind_hashbucket)));
  1172. sctp_dbg_objcnt_exit();
  1173. sctp_proc_exit();
  1174. cleanup_sctp_mibs();
  1175. kmem_cache_destroy(sctp_chunk_cachep);
  1176. kmem_cache_destroy(sctp_bucket_cachep);
  1177. }
  1178. module_init(sctp_init);
  1179. module_exit(sctp_exit);
  1180. /*
  1181. * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly.
  1182. */
  1183. MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132");
  1184. MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132");
  1185. MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>");
  1186. MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
  1187. MODULE_LICENSE("GPL");