protocol.c 37 KB

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