protocol.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543
  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 __net_init int 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 = t_new(struct sctp_sockaddr_entry, 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. SCTP_DEBUG_PRINTK("%s: DST:%pI4, SRC:%pI4 - ",
  402. __func__, &fl4->daddr, &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. SCTP_DEBUG_PRINTK("rt_dst:%pI4, rt_src:%pI4\n",
  457. &fl4->daddr, &fl4->saddr);
  458. else
  459. SCTP_DEBUG_PRINTK("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. 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. SCTP_DEBUG_PRINTK_IPADDR("sctp_addrwq_timo_handler: the first ent in wq %p is ",
  529. " for cmd %d at entry %p\n", &net->sctp.addr_waitq, &addrw->a, addrw->state,
  530. 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. SCTP_DEBUG_PRINTK("sctp_timo_handler: this is on DAD, trying %d sec later\n",
  544. SCTP_ADDRESS_TICK_DELAY);
  545. timeo_val = jiffies;
  546. timeo_val += msecs_to_jiffies(SCTP_ADDRESS_TICK_DELAY);
  547. mod_timer(&net->sctp.addr_wq_timer, timeo_val);
  548. break;
  549. }
  550. }
  551. #endif
  552. list_for_each_entry(sp, &net->sctp.auto_asconf_splist, auto_asconf_list) {
  553. struct sock *sk;
  554. sk = sctp_opt2sk(sp);
  555. /* ignore bound-specific endpoints */
  556. if (!sctp_is_ep_boundall(sk))
  557. continue;
  558. sctp_bh_lock_sock(sk);
  559. if (sctp_asconf_mgmt(sp, addrw) < 0)
  560. SCTP_DEBUG_PRINTK("sctp_addrwq_timo_handler: sctp_asconf_mgmt failed\n");
  561. sctp_bh_unlock_sock(sk);
  562. }
  563. #if IS_ENABLED(CONFIG_IPV6)
  564. free_next:
  565. #endif
  566. list_del(&addrw->list);
  567. kfree(addrw);
  568. }
  569. spin_unlock_bh(&net->sctp.addr_wq_lock);
  570. }
  571. static void sctp_free_addr_wq(struct net *net)
  572. {
  573. struct sctp_sockaddr_entry *addrw;
  574. struct sctp_sockaddr_entry *temp;
  575. spin_lock_bh(&net->sctp.addr_wq_lock);
  576. del_timer(&net->sctp.addr_wq_timer);
  577. list_for_each_entry_safe(addrw, temp, &net->sctp.addr_waitq, list) {
  578. list_del(&addrw->list);
  579. kfree(addrw);
  580. }
  581. spin_unlock_bh(&net->sctp.addr_wq_lock);
  582. }
  583. /* lookup the entry for the same address in the addr_waitq
  584. * sctp_addr_wq MUST be locked
  585. */
  586. static struct sctp_sockaddr_entry *sctp_addr_wq_lookup(struct net *net,
  587. struct sctp_sockaddr_entry *addr)
  588. {
  589. struct sctp_sockaddr_entry *addrw;
  590. list_for_each_entry(addrw, &net->sctp.addr_waitq, list) {
  591. if (addrw->a.sa.sa_family != addr->a.sa.sa_family)
  592. continue;
  593. if (addrw->a.sa.sa_family == AF_INET) {
  594. if (addrw->a.v4.sin_addr.s_addr ==
  595. addr->a.v4.sin_addr.s_addr)
  596. return addrw;
  597. } else if (addrw->a.sa.sa_family == AF_INET6) {
  598. if (ipv6_addr_equal(&addrw->a.v6.sin6_addr,
  599. &addr->a.v6.sin6_addr))
  600. return addrw;
  601. }
  602. }
  603. return NULL;
  604. }
  605. void sctp_addr_wq_mgmt(struct net *net, struct sctp_sockaddr_entry *addr, int cmd)
  606. {
  607. struct sctp_sockaddr_entry *addrw;
  608. unsigned long timeo_val;
  609. /* first, we check if an opposite message already exist in the queue.
  610. * If we found such message, it is removed.
  611. * This operation is a bit stupid, but the DHCP client attaches the
  612. * new address after a couple of addition and deletion of that address
  613. */
  614. spin_lock_bh(&net->sctp.addr_wq_lock);
  615. /* Offsets existing events in addr_wq */
  616. addrw = sctp_addr_wq_lookup(net, addr);
  617. if (addrw) {
  618. if (addrw->state != cmd) {
  619. SCTP_DEBUG_PRINTK_IPADDR("sctp_addr_wq_mgmt offsets existing entry for %d ",
  620. " in wq %p\n", addrw->state, &addrw->a,
  621. &net->sctp.addr_waitq);
  622. list_del(&addrw->list);
  623. kfree(addrw);
  624. }
  625. spin_unlock_bh(&net->sctp.addr_wq_lock);
  626. return;
  627. }
  628. /* OK, we have to add the new address to the wait queue */
  629. addrw = kmemdup(addr, sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
  630. if (addrw == NULL) {
  631. spin_unlock_bh(&net->sctp.addr_wq_lock);
  632. return;
  633. }
  634. addrw->state = cmd;
  635. list_add_tail(&addrw->list, &net->sctp.addr_waitq);
  636. SCTP_DEBUG_PRINTK_IPADDR("sctp_addr_wq_mgmt add new entry for cmd:%d ",
  637. " in wq %p\n", addrw->state, &addrw->a, &net->sctp.addr_waitq);
  638. if (!timer_pending(&net->sctp.addr_wq_timer)) {
  639. timeo_val = jiffies;
  640. timeo_val += msecs_to_jiffies(SCTP_ADDRESS_TICK_DELAY);
  641. mod_timer(&net->sctp.addr_wq_timer, timeo_val);
  642. }
  643. spin_unlock_bh(&net->sctp.addr_wq_lock);
  644. }
  645. /* Event handler for inet address addition/deletion events.
  646. * The sctp_local_addr_list needs to be protocted by a spin lock since
  647. * multiple notifiers (say IPv4 and IPv6) may be running at the same
  648. * time and thus corrupt the list.
  649. * The reader side is protected with RCU.
  650. */
  651. static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
  652. void *ptr)
  653. {
  654. struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
  655. struct sctp_sockaddr_entry *addr = NULL;
  656. struct sctp_sockaddr_entry *temp;
  657. struct net *net = dev_net(ifa->ifa_dev->dev);
  658. int found = 0;
  659. switch (ev) {
  660. case NETDEV_UP:
  661. addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
  662. if (addr) {
  663. addr->a.v4.sin_family = AF_INET;
  664. addr->a.v4.sin_port = 0;
  665. addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
  666. addr->valid = 1;
  667. spin_lock_bh(&net->sctp.local_addr_lock);
  668. list_add_tail_rcu(&addr->list, &net->sctp.local_addr_list);
  669. sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_NEW);
  670. spin_unlock_bh(&net->sctp.local_addr_lock);
  671. }
  672. break;
  673. case NETDEV_DOWN:
  674. spin_lock_bh(&net->sctp.local_addr_lock);
  675. list_for_each_entry_safe(addr, temp,
  676. &net->sctp.local_addr_list, list) {
  677. if (addr->a.sa.sa_family == AF_INET &&
  678. addr->a.v4.sin_addr.s_addr ==
  679. ifa->ifa_local) {
  680. sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL);
  681. found = 1;
  682. addr->valid = 0;
  683. list_del_rcu(&addr->list);
  684. break;
  685. }
  686. }
  687. spin_unlock_bh(&net->sctp.local_addr_lock);
  688. if (found)
  689. kfree_rcu(addr, rcu);
  690. break;
  691. }
  692. return NOTIFY_DONE;
  693. }
  694. /*
  695. * Initialize the control inode/socket with a control endpoint data
  696. * structure. This endpoint is reserved exclusively for the OOTB processing.
  697. */
  698. static int sctp_ctl_sock_init(struct net *net)
  699. {
  700. int err;
  701. sa_family_t family = PF_INET;
  702. if (sctp_get_pf_specific(PF_INET6))
  703. family = PF_INET6;
  704. err = inet_ctl_sock_create(&net->sctp.ctl_sock, family,
  705. SOCK_SEQPACKET, IPPROTO_SCTP, net);
  706. /* If IPv6 socket could not be created, try the IPv4 socket */
  707. if (err < 0 && family == PF_INET6)
  708. err = inet_ctl_sock_create(&net->sctp.ctl_sock, AF_INET,
  709. SOCK_SEQPACKET, IPPROTO_SCTP,
  710. net);
  711. if (err < 0) {
  712. pr_err("Failed to create the SCTP control socket\n");
  713. return err;
  714. }
  715. return 0;
  716. }
  717. /* Register address family specific functions. */
  718. int sctp_register_af(struct sctp_af *af)
  719. {
  720. switch (af->sa_family) {
  721. case AF_INET:
  722. if (sctp_af_v4_specific)
  723. return 0;
  724. sctp_af_v4_specific = af;
  725. break;
  726. case AF_INET6:
  727. if (sctp_af_v6_specific)
  728. return 0;
  729. sctp_af_v6_specific = af;
  730. break;
  731. default:
  732. return 0;
  733. }
  734. INIT_LIST_HEAD(&af->list);
  735. list_add_tail(&af->list, &sctp_address_families);
  736. return 1;
  737. }
  738. /* Get the table of functions for manipulating a particular address
  739. * family.
  740. */
  741. struct sctp_af *sctp_get_af_specific(sa_family_t family)
  742. {
  743. switch (family) {
  744. case AF_INET:
  745. return sctp_af_v4_specific;
  746. case AF_INET6:
  747. return sctp_af_v6_specific;
  748. default:
  749. return NULL;
  750. }
  751. }
  752. /* Common code to initialize a AF_INET msg_name. */
  753. static void sctp_inet_msgname(char *msgname, int *addr_len)
  754. {
  755. struct sockaddr_in *sin;
  756. sin = (struct sockaddr_in *)msgname;
  757. *addr_len = sizeof(struct sockaddr_in);
  758. sin->sin_family = AF_INET;
  759. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  760. }
  761. /* Copy the primary address of the peer primary address as the msg_name. */
  762. static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname,
  763. int *addr_len)
  764. {
  765. struct sockaddr_in *sin, *sinfrom;
  766. if (msgname) {
  767. struct sctp_association *asoc;
  768. asoc = event->asoc;
  769. sctp_inet_msgname(msgname, addr_len);
  770. sin = (struct sockaddr_in *)msgname;
  771. sinfrom = &asoc->peer.primary_addr.v4;
  772. sin->sin_port = htons(asoc->peer.port);
  773. sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr;
  774. }
  775. }
  776. /* Initialize and copy out a msgname from an inbound skb. */
  777. static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len)
  778. {
  779. if (msgname) {
  780. struct sctphdr *sh = sctp_hdr(skb);
  781. struct sockaddr_in *sin = (struct sockaddr_in *)msgname;
  782. sctp_inet_msgname(msgname, len);
  783. sin->sin_port = sh->source;
  784. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  785. }
  786. }
  787. /* Do we support this AF? */
  788. static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp)
  789. {
  790. /* PF_INET only supports AF_INET addresses. */
  791. return AF_INET == family;
  792. }
  793. /* Address matching with wildcards allowed. */
  794. static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
  795. const union sctp_addr *addr2,
  796. struct sctp_sock *opt)
  797. {
  798. /* PF_INET only supports AF_INET addresses. */
  799. if (addr1->sa.sa_family != addr2->sa.sa_family)
  800. return 0;
  801. if (htonl(INADDR_ANY) == addr1->v4.sin_addr.s_addr ||
  802. htonl(INADDR_ANY) == addr2->v4.sin_addr.s_addr)
  803. return 1;
  804. if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
  805. return 1;
  806. return 0;
  807. }
  808. /* Verify that provided sockaddr looks bindable. Common verification has
  809. * already been taken care of.
  810. */
  811. static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
  812. {
  813. return sctp_v4_available(addr, opt);
  814. }
  815. /* Verify that sockaddr looks sendable. Common verification has already
  816. * been taken care of.
  817. */
  818. static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
  819. {
  820. return 1;
  821. }
  822. /* Fill in Supported Address Type information for INIT and INIT-ACK
  823. * chunks. Returns number of addresses supported.
  824. */
  825. static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
  826. __be16 *types)
  827. {
  828. types[0] = SCTP_PARAM_IPV4_ADDRESS;
  829. return 1;
  830. }
  831. /* Wrapper routine that calls the ip transmit routine. */
  832. static inline int sctp_v4_xmit(struct sk_buff *skb,
  833. struct sctp_transport *transport)
  834. {
  835. struct inet_sock *inet = inet_sk(skb->sk);
  836. SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, src:%pI4, dst:%pI4\n",
  837. __func__, skb, skb->len,
  838. &transport->fl.u.ip4.saddr,
  839. &transport->fl.u.ip4.daddr);
  840. inet->pmtudisc = transport->param_flags & SPP_PMTUD_ENABLE ?
  841. IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
  842. SCTP_INC_STATS(sock_net(&inet->sk), SCTP_MIB_OUTSCTPPACKS);
  843. return ip_queue_xmit(skb, &transport->fl);
  844. }
  845. static struct sctp_af sctp_af_inet;
  846. static struct sctp_pf sctp_pf_inet = {
  847. .event_msgname = sctp_inet_event_msgname,
  848. .skb_msgname = sctp_inet_skb_msgname,
  849. .af_supported = sctp_inet_af_supported,
  850. .cmp_addr = sctp_inet_cmp_addr,
  851. .bind_verify = sctp_inet_bind_verify,
  852. .send_verify = sctp_inet_send_verify,
  853. .supported_addrs = sctp_inet_supported_addrs,
  854. .create_accept_sk = sctp_v4_create_accept_sk,
  855. .addr_v4map = sctp_v4_addr_v4map,
  856. .af = &sctp_af_inet
  857. };
  858. /* Notifier for inetaddr addition/deletion events. */
  859. static struct notifier_block sctp_inetaddr_notifier = {
  860. .notifier_call = sctp_inetaddr_event,
  861. };
  862. /* Socket operations. */
  863. static const struct proto_ops inet_seqpacket_ops = {
  864. .family = PF_INET,
  865. .owner = THIS_MODULE,
  866. .release = inet_release, /* Needs to be wrapped... */
  867. .bind = inet_bind,
  868. .connect = inet_dgram_connect,
  869. .socketpair = sock_no_socketpair,
  870. .accept = inet_accept,
  871. .getname = inet_getname, /* Semantics are different. */
  872. .poll = sctp_poll,
  873. .ioctl = inet_ioctl,
  874. .listen = sctp_inet_listen,
  875. .shutdown = inet_shutdown, /* Looks harmless. */
  876. .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */
  877. .getsockopt = sock_common_getsockopt,
  878. .sendmsg = inet_sendmsg,
  879. .recvmsg = sock_common_recvmsg,
  880. .mmap = sock_no_mmap,
  881. .sendpage = sock_no_sendpage,
  882. #ifdef CONFIG_COMPAT
  883. .compat_setsockopt = compat_sock_common_setsockopt,
  884. .compat_getsockopt = compat_sock_common_getsockopt,
  885. #endif
  886. };
  887. /* Registration with AF_INET family. */
  888. static struct inet_protosw sctp_seqpacket_protosw = {
  889. .type = SOCK_SEQPACKET,
  890. .protocol = IPPROTO_SCTP,
  891. .prot = &sctp_prot,
  892. .ops = &inet_seqpacket_ops,
  893. .no_check = 0,
  894. .flags = SCTP_PROTOSW_FLAG
  895. };
  896. static struct inet_protosw sctp_stream_protosw = {
  897. .type = SOCK_STREAM,
  898. .protocol = IPPROTO_SCTP,
  899. .prot = &sctp_prot,
  900. .ops = &inet_seqpacket_ops,
  901. .no_check = 0,
  902. .flags = SCTP_PROTOSW_FLAG
  903. };
  904. /* Register with IP layer. */
  905. static const struct net_protocol sctp_protocol = {
  906. .handler = sctp_rcv,
  907. .err_handler = sctp_v4_err,
  908. .no_policy = 1,
  909. .netns_ok = 1,
  910. };
  911. /* IPv4 address related functions. */
  912. static struct sctp_af sctp_af_inet = {
  913. .sa_family = AF_INET,
  914. .sctp_xmit = sctp_v4_xmit,
  915. .setsockopt = ip_setsockopt,
  916. .getsockopt = ip_getsockopt,
  917. .get_dst = sctp_v4_get_dst,
  918. .get_saddr = sctp_v4_get_saddr,
  919. .copy_addrlist = sctp_v4_copy_addrlist,
  920. .from_skb = sctp_v4_from_skb,
  921. .from_sk = sctp_v4_from_sk,
  922. .to_sk_saddr = sctp_v4_to_sk_saddr,
  923. .to_sk_daddr = sctp_v4_to_sk_daddr,
  924. .from_addr_param = sctp_v4_from_addr_param,
  925. .to_addr_param = sctp_v4_to_addr_param,
  926. .cmp_addr = sctp_v4_cmp_addr,
  927. .addr_valid = sctp_v4_addr_valid,
  928. .inaddr_any = sctp_v4_inaddr_any,
  929. .is_any = sctp_v4_is_any,
  930. .available = sctp_v4_available,
  931. .scope = sctp_v4_scope,
  932. .skb_iif = sctp_v4_skb_iif,
  933. .is_ce = sctp_v4_is_ce,
  934. .seq_dump_addr = sctp_v4_seq_dump_addr,
  935. .ecn_capable = sctp_v4_ecn_capable,
  936. .net_header_len = sizeof(struct iphdr),
  937. .sockaddr_len = sizeof(struct sockaddr_in),
  938. #ifdef CONFIG_COMPAT
  939. .compat_setsockopt = compat_ip_setsockopt,
  940. .compat_getsockopt = compat_ip_getsockopt,
  941. #endif
  942. };
  943. struct sctp_pf *sctp_get_pf_specific(sa_family_t family) {
  944. switch (family) {
  945. case PF_INET:
  946. return sctp_pf_inet_specific;
  947. case PF_INET6:
  948. return sctp_pf_inet6_specific;
  949. default:
  950. return NULL;
  951. }
  952. }
  953. /* Register the PF specific function table. */
  954. int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
  955. {
  956. switch (family) {
  957. case PF_INET:
  958. if (sctp_pf_inet_specific)
  959. return 0;
  960. sctp_pf_inet_specific = pf;
  961. break;
  962. case PF_INET6:
  963. if (sctp_pf_inet6_specific)
  964. return 0;
  965. sctp_pf_inet6_specific = pf;
  966. break;
  967. default:
  968. return 0;
  969. }
  970. return 1;
  971. }
  972. static inline int init_sctp_mibs(struct net *net)
  973. {
  974. return snmp_mib_init((void __percpu **)net->sctp.sctp_statistics,
  975. sizeof(struct sctp_mib),
  976. __alignof__(struct sctp_mib));
  977. }
  978. static inline void cleanup_sctp_mibs(struct net *net)
  979. {
  980. snmp_mib_free((void __percpu **)net->sctp.sctp_statistics);
  981. }
  982. static void sctp_v4_pf_init(void)
  983. {
  984. /* Initialize the SCTP specific PF functions. */
  985. sctp_register_pf(&sctp_pf_inet, PF_INET);
  986. sctp_register_af(&sctp_af_inet);
  987. }
  988. static void sctp_v4_pf_exit(void)
  989. {
  990. list_del(&sctp_af_inet.list);
  991. }
  992. static int sctp_v4_protosw_init(void)
  993. {
  994. int rc;
  995. rc = proto_register(&sctp_prot, 1);
  996. if (rc)
  997. return rc;
  998. /* Register SCTP(UDP and TCP style) with socket layer. */
  999. inet_register_protosw(&sctp_seqpacket_protosw);
  1000. inet_register_protosw(&sctp_stream_protosw);
  1001. return 0;
  1002. }
  1003. static void sctp_v4_protosw_exit(void)
  1004. {
  1005. inet_unregister_protosw(&sctp_stream_protosw);
  1006. inet_unregister_protosw(&sctp_seqpacket_protosw);
  1007. proto_unregister(&sctp_prot);
  1008. }
  1009. static int sctp_v4_add_protocol(void)
  1010. {
  1011. /* Register notifier for inet address additions/deletions. */
  1012. register_inetaddr_notifier(&sctp_inetaddr_notifier);
  1013. /* Register SCTP with inet layer. */
  1014. if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
  1015. return -EAGAIN;
  1016. return 0;
  1017. }
  1018. static void sctp_v4_del_protocol(void)
  1019. {
  1020. inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
  1021. unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
  1022. }
  1023. static int sctp_net_init(struct net *net)
  1024. {
  1025. int status;
  1026. /*
  1027. * 14. Suggested SCTP Protocol Parameter Values
  1028. */
  1029. /* The following protocol parameters are RECOMMENDED: */
  1030. /* RTO.Initial - 3 seconds */
  1031. net->sctp.rto_initial = SCTP_RTO_INITIAL;
  1032. /* RTO.Min - 1 second */
  1033. net->sctp.rto_min = SCTP_RTO_MIN;
  1034. /* RTO.Max - 60 seconds */
  1035. net->sctp.rto_max = SCTP_RTO_MAX;
  1036. /* RTO.Alpha - 1/8 */
  1037. net->sctp.rto_alpha = SCTP_RTO_ALPHA;
  1038. /* RTO.Beta - 1/4 */
  1039. net->sctp.rto_beta = SCTP_RTO_BETA;
  1040. /* Valid.Cookie.Life - 60 seconds */
  1041. net->sctp.valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE;
  1042. /* Whether Cookie Preservative is enabled(1) or not(0) */
  1043. net->sctp.cookie_preserve_enable = 1;
  1044. /* Max.Burst - 4 */
  1045. net->sctp.max_burst = SCTP_DEFAULT_MAX_BURST;
  1046. /* Association.Max.Retrans - 10 attempts
  1047. * Path.Max.Retrans - 5 attempts (per destination address)
  1048. * Max.Init.Retransmits - 8 attempts
  1049. */
  1050. net->sctp.max_retrans_association = 10;
  1051. net->sctp.max_retrans_path = 5;
  1052. net->sctp.max_retrans_init = 8;
  1053. /* Sendbuffer growth - do per-socket accounting */
  1054. net->sctp.sndbuf_policy = 0;
  1055. /* Rcvbuffer growth - do per-socket accounting */
  1056. net->sctp.rcvbuf_policy = 0;
  1057. /* HB.interval - 30 seconds */
  1058. net->sctp.hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
  1059. /* delayed SACK timeout */
  1060. net->sctp.sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK;
  1061. /* Disable ADDIP by default. */
  1062. net->sctp.addip_enable = 0;
  1063. net->sctp.addip_noauth = 0;
  1064. net->sctp.default_auto_asconf = 0;
  1065. /* Enable PR-SCTP by default. */
  1066. net->sctp.prsctp_enable = 1;
  1067. /* Disable AUTH by default. */
  1068. net->sctp.auth_enable = 0;
  1069. /* Set SCOPE policy to enabled */
  1070. net->sctp.scope_policy = SCTP_SCOPE_POLICY_ENABLE;
  1071. /* Set the default rwnd update threshold */
  1072. net->sctp.rwnd_upd_shift = SCTP_DEFAULT_RWND_SHIFT;
  1073. /* Initialize maximum autoclose timeout. */
  1074. net->sctp.max_autoclose = INT_MAX / HZ;
  1075. status = sctp_sysctl_net_register(net);
  1076. if (status)
  1077. goto err_sysctl_register;
  1078. /* Allocate and initialise sctp mibs. */
  1079. status = init_sctp_mibs(net);
  1080. if (status)
  1081. goto err_init_mibs;
  1082. /* Initialize proc fs directory. */
  1083. status = sctp_proc_init(net);
  1084. if (status)
  1085. goto err_init_proc;
  1086. sctp_dbg_objcnt_init(net);
  1087. /* Initialize the control inode/socket for handling OOTB packets. */
  1088. if ((status = sctp_ctl_sock_init(net))) {
  1089. pr_err("Failed to initialize the SCTP control sock\n");
  1090. goto err_ctl_sock_init;
  1091. }
  1092. /* Initialize the local address list. */
  1093. INIT_LIST_HEAD(&net->sctp.local_addr_list);
  1094. spin_lock_init(&net->sctp.local_addr_lock);
  1095. sctp_get_local_addr_list(net);
  1096. /* Initialize the address event list */
  1097. INIT_LIST_HEAD(&net->sctp.addr_waitq);
  1098. INIT_LIST_HEAD(&net->sctp.auto_asconf_splist);
  1099. spin_lock_init(&net->sctp.addr_wq_lock);
  1100. net->sctp.addr_wq_timer.expires = 0;
  1101. setup_timer(&net->sctp.addr_wq_timer, sctp_addr_wq_timeout_handler,
  1102. (unsigned long)net);
  1103. return 0;
  1104. err_ctl_sock_init:
  1105. sctp_dbg_objcnt_exit(net);
  1106. sctp_proc_exit(net);
  1107. err_init_proc:
  1108. cleanup_sctp_mibs(net);
  1109. err_init_mibs:
  1110. sctp_sysctl_net_unregister(net);
  1111. err_sysctl_register:
  1112. return status;
  1113. }
  1114. static void sctp_net_exit(struct net *net)
  1115. {
  1116. /* Free the local address list */
  1117. sctp_free_addr_wq(net);
  1118. sctp_free_local_addr_list(net);
  1119. /* Free the control endpoint. */
  1120. inet_ctl_sock_destroy(net->sctp.ctl_sock);
  1121. sctp_dbg_objcnt_exit(net);
  1122. sctp_proc_exit(net);
  1123. cleanup_sctp_mibs(net);
  1124. sctp_sysctl_net_unregister(net);
  1125. }
  1126. static struct pernet_operations sctp_net_ops = {
  1127. .init = sctp_net_init,
  1128. .exit = sctp_net_exit,
  1129. };
  1130. /* Initialize the universe into something sensible. */
  1131. SCTP_STATIC __init int sctp_init(void)
  1132. {
  1133. int i;
  1134. int status = -EINVAL;
  1135. unsigned long goal;
  1136. unsigned long limit;
  1137. int max_share;
  1138. int order;
  1139. /* SCTP_DEBUG sanity check. */
  1140. if (!sctp_sanity_check())
  1141. goto out;
  1142. /* Allocate bind_bucket and chunk caches. */
  1143. status = -ENOBUFS;
  1144. sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
  1145. sizeof(struct sctp_bind_bucket),
  1146. 0, SLAB_HWCACHE_ALIGN,
  1147. NULL);
  1148. if (!sctp_bucket_cachep)
  1149. goto out;
  1150. sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
  1151. sizeof(struct sctp_chunk),
  1152. 0, SLAB_HWCACHE_ALIGN,
  1153. NULL);
  1154. if (!sctp_chunk_cachep)
  1155. goto err_chunk_cachep;
  1156. status = percpu_counter_init(&sctp_sockets_allocated, 0);
  1157. if (status)
  1158. goto err_percpu_counter_init;
  1159. /* Implementation specific variables. */
  1160. /* Initialize default stream count setup information. */
  1161. sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
  1162. sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
  1163. /* Initialize handle used for association ids. */
  1164. idr_init(&sctp_assocs_id);
  1165. limit = nr_free_buffer_pages() / 8;
  1166. limit = max(limit, 128UL);
  1167. sysctl_sctp_mem[0] = limit / 4 * 3;
  1168. sysctl_sctp_mem[1] = limit;
  1169. sysctl_sctp_mem[2] = sysctl_sctp_mem[0] * 2;
  1170. /* Set per-socket limits to no more than 1/128 the pressure threshold*/
  1171. limit = (sysctl_sctp_mem[1]) << (PAGE_SHIFT - 7);
  1172. max_share = min(4UL*1024*1024, limit);
  1173. sysctl_sctp_rmem[0] = SK_MEM_QUANTUM; /* give each asoc 1 page min */
  1174. sysctl_sctp_rmem[1] = 1500 * SKB_TRUESIZE(1);
  1175. sysctl_sctp_rmem[2] = max(sysctl_sctp_rmem[1], max_share);
  1176. sysctl_sctp_wmem[0] = SK_MEM_QUANTUM;
  1177. sysctl_sctp_wmem[1] = 16*1024;
  1178. sysctl_sctp_wmem[2] = max(64*1024, max_share);
  1179. /* Size and allocate the association hash table.
  1180. * The methodology is similar to that of the tcp hash tables.
  1181. */
  1182. if (totalram_pages >= (128 * 1024))
  1183. goal = totalram_pages >> (22 - PAGE_SHIFT);
  1184. else
  1185. goal = totalram_pages >> (24 - PAGE_SHIFT);
  1186. for (order = 0; (1UL << order) < goal; order++)
  1187. ;
  1188. do {
  1189. sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE /
  1190. sizeof(struct sctp_hashbucket);
  1191. if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0)
  1192. continue;
  1193. sctp_assoc_hashtable = (struct sctp_hashbucket *)
  1194. __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, order);
  1195. } while (!sctp_assoc_hashtable && --order > 0);
  1196. if (!sctp_assoc_hashtable) {
  1197. pr_err("Failed association hash alloc\n");
  1198. status = -ENOMEM;
  1199. goto err_ahash_alloc;
  1200. }
  1201. for (i = 0; i < sctp_assoc_hashsize; i++) {
  1202. rwlock_init(&sctp_assoc_hashtable[i].lock);
  1203. INIT_HLIST_HEAD(&sctp_assoc_hashtable[i].chain);
  1204. }
  1205. /* Allocate and initialize the endpoint hash table. */
  1206. sctp_ep_hashsize = 64;
  1207. sctp_ep_hashtable = (struct sctp_hashbucket *)
  1208. kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL);
  1209. if (!sctp_ep_hashtable) {
  1210. pr_err("Failed endpoint_hash alloc\n");
  1211. status = -ENOMEM;
  1212. goto err_ehash_alloc;
  1213. }
  1214. for (i = 0; i < sctp_ep_hashsize; i++) {
  1215. rwlock_init(&sctp_ep_hashtable[i].lock);
  1216. INIT_HLIST_HEAD(&sctp_ep_hashtable[i].chain);
  1217. }
  1218. /* Allocate and initialize the SCTP port hash table. */
  1219. do {
  1220. sctp_port_hashsize = (1UL << order) * PAGE_SIZE /
  1221. sizeof(struct sctp_bind_hashbucket);
  1222. if ((sctp_port_hashsize > (64 * 1024)) && order > 0)
  1223. continue;
  1224. sctp_port_hashtable = (struct sctp_bind_hashbucket *)
  1225. __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, order);
  1226. } while (!sctp_port_hashtable && --order > 0);
  1227. if (!sctp_port_hashtable) {
  1228. pr_err("Failed bind hash alloc\n");
  1229. status = -ENOMEM;
  1230. goto err_bhash_alloc;
  1231. }
  1232. for (i = 0; i < sctp_port_hashsize; i++) {
  1233. spin_lock_init(&sctp_port_hashtable[i].lock);
  1234. INIT_HLIST_HEAD(&sctp_port_hashtable[i].chain);
  1235. }
  1236. pr_info("Hash tables configured (established %d bind %d)\n",
  1237. sctp_assoc_hashsize, sctp_port_hashsize);
  1238. sctp_sysctl_register();
  1239. INIT_LIST_HEAD(&sctp_address_families);
  1240. sctp_v4_pf_init();
  1241. sctp_v6_pf_init();
  1242. status = sctp_v4_protosw_init();
  1243. if (status)
  1244. goto err_protosw_init;
  1245. status = sctp_v6_protosw_init();
  1246. if (status)
  1247. goto err_v6_protosw_init;
  1248. status = register_pernet_subsys(&sctp_net_ops);
  1249. if (status)
  1250. goto err_register_pernet_subsys;
  1251. status = sctp_v4_add_protocol();
  1252. if (status)
  1253. goto err_add_protocol;
  1254. /* Register SCTP with inet6 layer. */
  1255. status = sctp_v6_add_protocol();
  1256. if (status)
  1257. goto err_v6_add_protocol;
  1258. status = 0;
  1259. out:
  1260. return status;
  1261. err_v6_add_protocol:
  1262. sctp_v4_del_protocol();
  1263. err_add_protocol:
  1264. unregister_pernet_subsys(&sctp_net_ops);
  1265. err_register_pernet_subsys:
  1266. sctp_v6_protosw_exit();
  1267. err_v6_protosw_init:
  1268. sctp_v4_protosw_exit();
  1269. err_protosw_init:
  1270. sctp_v4_pf_exit();
  1271. sctp_v6_pf_exit();
  1272. sctp_sysctl_unregister();
  1273. free_pages((unsigned long)sctp_port_hashtable,
  1274. get_order(sctp_port_hashsize *
  1275. sizeof(struct sctp_bind_hashbucket)));
  1276. err_bhash_alloc:
  1277. kfree(sctp_ep_hashtable);
  1278. err_ehash_alloc:
  1279. free_pages((unsigned long)sctp_assoc_hashtable,
  1280. get_order(sctp_assoc_hashsize *
  1281. sizeof(struct sctp_hashbucket)));
  1282. err_ahash_alloc:
  1283. percpu_counter_destroy(&sctp_sockets_allocated);
  1284. err_percpu_counter_init:
  1285. kmem_cache_destroy(sctp_chunk_cachep);
  1286. err_chunk_cachep:
  1287. kmem_cache_destroy(sctp_bucket_cachep);
  1288. goto out;
  1289. }
  1290. /* Exit handler for the SCTP protocol. */
  1291. SCTP_STATIC __exit void sctp_exit(void)
  1292. {
  1293. /* BUG. This should probably do something useful like clean
  1294. * up all the remaining associations and all that memory.
  1295. */
  1296. /* Unregister with inet6/inet layers. */
  1297. sctp_v6_del_protocol();
  1298. sctp_v4_del_protocol();
  1299. unregister_pernet_subsys(&sctp_net_ops);
  1300. /* Free protosw registrations */
  1301. sctp_v6_protosw_exit();
  1302. sctp_v4_protosw_exit();
  1303. /* Unregister with socket layer. */
  1304. sctp_v6_pf_exit();
  1305. sctp_v4_pf_exit();
  1306. sctp_sysctl_unregister();
  1307. free_pages((unsigned long)sctp_assoc_hashtable,
  1308. get_order(sctp_assoc_hashsize *
  1309. sizeof(struct sctp_hashbucket)));
  1310. kfree(sctp_ep_hashtable);
  1311. free_pages((unsigned long)sctp_port_hashtable,
  1312. get_order(sctp_port_hashsize *
  1313. sizeof(struct sctp_bind_hashbucket)));
  1314. percpu_counter_destroy(&sctp_sockets_allocated);
  1315. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1316. kmem_cache_destroy(sctp_chunk_cachep);
  1317. kmem_cache_destroy(sctp_bucket_cachep);
  1318. }
  1319. module_init(sctp_init);
  1320. module_exit(sctp_exit);
  1321. /*
  1322. * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly.
  1323. */
  1324. MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132");
  1325. MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132");
  1326. MODULE_AUTHOR("Linux Kernel SCTP developers <lksctp-developers@lists.sourceforge.net>");
  1327. MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
  1328. module_param_named(no_checksums, sctp_checksum_disable, bool, 0644);
  1329. MODULE_PARM_DESC(no_checksums, "Disable checksums computing and verification");
  1330. MODULE_LICENSE("GPL");