protocol.c 43 KB

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