protocol.c 36 KB

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