protocol.c 37 KB

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