protocol.c 37 KB

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