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