protocol.c 41 KB

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