/net/sctp/protocol.c

http://github.com/mirrors/linux · C · 1630 lines · 1141 code · 244 blank · 245 comment · 146 complexity · 1fbb44a1530c708b5d397933adaad4d6 MD5 · raw file

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