protocol.c 43 KB

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