protocol.c 42 KB

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