protocol.c 36 KB

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