protocol.c 36 KB

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