protocol.c 41 KB

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