protocol.c 36 KB

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