protocol.c 41 KB

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