protocol.c 45 KB

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