protocol.c 43 KB

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