protocol.c 41 KB

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