protocol.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  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 <[email protected]>
  17. *
  18. * Written or modified by:
  19. * La Monte H.P. Yarroll <[email protected]>
  20. * Karl Knutson <[email protected]>
  21. * Jon Grimm <[email protected]>
  22. * Sridhar Samudrala <[email protected]>
  23. * Daisy Chang <[email protected]>
  24. * Ardelle Fan <[email protected]>
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/inetdevice.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/memblock.h>
  33. #include <linux/highmem.h>
  34. #include <linux/slab.h>
  35. #include <net/net_namespace.h>
  36. #include <net/protocol.h>
  37. #include <net/ip.h>
  38. #include <net/ipv6.h>
  39. #include <net/route.h>
  40. #include <net/sctp/sctp.h>
  41. #include <net/addrconf.h>
  42. #include <net/inet_common.h>
  43. #include <net/inet_ecn.h>
  44. #include <net/udp_tunnel.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. !READ_ONCE(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. static int sctp_udp_rcv(struct sock *sk, struct sk_buff *skb)
  732. {
  733. SCTP_INPUT_CB(skb)->encap_port = udp_hdr(skb)->source;
  734. skb_set_transport_header(skb, sizeof(struct udphdr));
  735. sctp_rcv(skb);
  736. return 0;
  737. }
  738. int sctp_udp_sock_start(struct net *net)
  739. {
  740. struct udp_tunnel_sock_cfg tuncfg = {NULL};
  741. struct udp_port_cfg udp_conf = {0};
  742. struct socket *sock;
  743. int err;
  744. udp_conf.family = AF_INET;
  745. udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
  746. udp_conf.local_udp_port = htons(net->sctp.udp_port);
  747. err = udp_sock_create(net, &udp_conf, &sock);
  748. if (err) {
  749. pr_err("Failed to create the SCTP UDP tunneling v4 sock\n");
  750. return err;
  751. }
  752. tuncfg.encap_type = 1;
  753. tuncfg.encap_rcv = sctp_udp_rcv;
  754. tuncfg.encap_err_lookup = sctp_udp_v4_err;
  755. setup_udp_tunnel_sock(net, sock, &tuncfg);
  756. net->sctp.udp4_sock = sock->sk;
  757. #if IS_ENABLED(CONFIG_IPV6)
  758. memset(&udp_conf, 0, sizeof(udp_conf));
  759. udp_conf.family = AF_INET6;
  760. udp_conf.local_ip6 = in6addr_any;
  761. udp_conf.local_udp_port = htons(net->sctp.udp_port);
  762. udp_conf.use_udp6_rx_checksums = true;
  763. udp_conf.ipv6_v6only = true;
  764. err = udp_sock_create(net, &udp_conf, &sock);
  765. if (err) {
  766. pr_err("Failed to create the SCTP UDP tunneling v6 sock\n");
  767. udp_tunnel_sock_release(net->sctp.udp4_sock->sk_socket);
  768. net->sctp.udp4_sock = NULL;
  769. return err;
  770. }
  771. tuncfg.encap_type = 1;
  772. tuncfg.encap_rcv = sctp_udp_rcv;
  773. tuncfg.encap_err_lookup = sctp_udp_v6_err;
  774. setup_udp_tunnel_sock(net, sock, &tuncfg);
  775. net->sctp.udp6_sock = sock->sk;
  776. #endif
  777. return 0;
  778. }
  779. void sctp_udp_sock_stop(struct net *net)
  780. {
  781. if (net->sctp.udp4_sock) {
  782. udp_tunnel_sock_release(net->sctp.udp4_sock->sk_socket);
  783. net->sctp.udp4_sock = NULL;
  784. }
  785. if (net->sctp.udp6_sock) {
  786. udp_tunnel_sock_release(net->sctp.udp6_sock->sk_socket);
  787. net->sctp.udp6_sock = NULL;
  788. }
  789. }
  790. /* Register address family specific functions. */
  791. int sctp_register_af(struct sctp_af *af)
  792. {
  793. switch (af->sa_family) {
  794. case AF_INET:
  795. if (sctp_af_v4_specific)
  796. return 0;
  797. sctp_af_v4_specific = af;
  798. break;
  799. case AF_INET6:
  800. if (sctp_af_v6_specific)
  801. return 0;
  802. sctp_af_v6_specific = af;
  803. break;
  804. default:
  805. return 0;
  806. }
  807. INIT_LIST_HEAD(&af->list);
  808. list_add_tail(&af->list, &sctp_address_families);
  809. return 1;
  810. }
  811. /* Get the table of functions for manipulating a particular address
  812. * family.
  813. */
  814. struct sctp_af *sctp_get_af_specific(sa_family_t family)
  815. {
  816. switch (family) {
  817. case AF_INET:
  818. return sctp_af_v4_specific;
  819. case AF_INET6:
  820. return sctp_af_v6_specific;
  821. default:
  822. return NULL;
  823. }
  824. }
  825. /* Common code to initialize a AF_INET msg_name. */
  826. static void sctp_inet_msgname(char *msgname, int *addr_len)
  827. {
  828. struct sockaddr_in *sin;
  829. sin = (struct sockaddr_in *)msgname;
  830. *addr_len = sizeof(struct sockaddr_in);
  831. sin->sin_family = AF_INET;
  832. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  833. }
  834. /* Copy the primary address of the peer primary address as the msg_name. */
  835. static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname,
  836. int *addr_len)
  837. {
  838. struct sockaddr_in *sin, *sinfrom;
  839. if (msgname) {
  840. struct sctp_association *asoc;
  841. asoc = event->asoc;
  842. sctp_inet_msgname(msgname, addr_len);
  843. sin = (struct sockaddr_in *)msgname;
  844. sinfrom = &asoc->peer.primary_addr.v4;
  845. sin->sin_port = htons(asoc->peer.port);
  846. sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr;
  847. }
  848. }
  849. /* Initialize and copy out a msgname from an inbound skb. */
  850. static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len)
  851. {
  852. if (msgname) {
  853. struct sctphdr *sh = sctp_hdr(skb);
  854. struct sockaddr_in *sin = (struct sockaddr_in *)msgname;
  855. sctp_inet_msgname(msgname, len);
  856. sin->sin_port = sh->source;
  857. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  858. }
  859. }
  860. /* Do we support this AF? */
  861. static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp)
  862. {
  863. /* PF_INET only supports AF_INET addresses. */
  864. return AF_INET == family;
  865. }
  866. /* Address matching with wildcards allowed. */
  867. static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
  868. const union sctp_addr *addr2,
  869. struct sctp_sock *opt)
  870. {
  871. /* PF_INET only supports AF_INET addresses. */
  872. if (addr1->sa.sa_family != addr2->sa.sa_family)
  873. return 0;
  874. if (htonl(INADDR_ANY) == addr1->v4.sin_addr.s_addr ||
  875. htonl(INADDR_ANY) == addr2->v4.sin_addr.s_addr)
  876. return 1;
  877. if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
  878. return 1;
  879. return 0;
  880. }
  881. /* Verify that provided sockaddr looks bindable. Common verification has
  882. * already been taken care of.
  883. */
  884. static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
  885. {
  886. return sctp_v4_available(addr, opt);
  887. }
  888. /* Verify that sockaddr looks sendable. Common verification has already
  889. * been taken care of.
  890. */
  891. static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
  892. {
  893. return 1;
  894. }
  895. /* Fill in Supported Address Type information for INIT and INIT-ACK
  896. * chunks. Returns number of addresses supported.
  897. */
  898. static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
  899. __be16 *types)
  900. {
  901. types[0] = SCTP_PARAM_IPV4_ADDRESS;
  902. return 1;
  903. }
  904. /* Wrapper routine that calls the ip transmit routine. */
  905. static inline int sctp_v4_xmit(struct sk_buff *skb, struct sctp_transport *t)
  906. {
  907. struct dst_entry *dst = dst_clone(t->dst);
  908. struct flowi4 *fl4 = &t->fl.u.ip4;
  909. struct sock *sk = skb->sk;
  910. struct inet_sock *inet = inet_sk(sk);
  911. __u8 dscp = inet->tos;
  912. __be16 df = 0;
  913. pr_debug("%s: skb:%p, len:%d, src:%pI4, dst:%pI4\n", __func__, skb,
  914. skb->len, &fl4->saddr, &fl4->daddr);
  915. if (t->dscp & SCTP_DSCP_SET_MASK)
  916. dscp = t->dscp & SCTP_DSCP_VAL_MASK;
  917. inet->pmtudisc = t->param_flags & SPP_PMTUD_ENABLE ? IP_PMTUDISC_DO
  918. : IP_PMTUDISC_DONT;
  919. SCTP_INC_STATS(sock_net(sk), SCTP_MIB_OUTSCTPPACKS);
  920. if (!t->encap_port || !sctp_sk(sk)->udp_port) {
  921. skb_dst_set(skb, dst);
  922. return __ip_queue_xmit(sk, skb, &t->fl, dscp);
  923. }
  924. if (skb_is_gso(skb))
  925. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
  926. if (ip_dont_fragment(sk, dst) && !skb->ignore_df)
  927. df = htons(IP_DF);
  928. skb->encapsulation = 1;
  929. skb_reset_inner_mac_header(skb);
  930. skb_reset_inner_transport_header(skb);
  931. skb_set_inner_ipproto(skb, IPPROTO_SCTP);
  932. udp_tunnel_xmit_skb((struct rtable *)dst, sk, skb, fl4->saddr,
  933. fl4->daddr, dscp, ip4_dst_hoplimit(dst), df,
  934. sctp_sk(sk)->udp_port, t->encap_port, false, false);
  935. return 0;
  936. }
  937. static struct sctp_af sctp_af_inet;
  938. static struct sctp_pf sctp_pf_inet = {
  939. .event_msgname = sctp_inet_event_msgname,
  940. .skb_msgname = sctp_inet_skb_msgname,
  941. .af_supported = sctp_inet_af_supported,
  942. .cmp_addr = sctp_inet_cmp_addr,
  943. .bind_verify = sctp_inet_bind_verify,
  944. .send_verify = sctp_inet_send_verify,
  945. .supported_addrs = sctp_inet_supported_addrs,
  946. .create_accept_sk = sctp_v4_create_accept_sk,
  947. .addr_to_user = sctp_v4_addr_to_user,
  948. .to_sk_saddr = sctp_v4_to_sk_saddr,
  949. .to_sk_daddr = sctp_v4_to_sk_daddr,
  950. .copy_ip_options = sctp_v4_copy_ip_options,
  951. .af = &sctp_af_inet
  952. };
  953. /* Notifier for inetaddr addition/deletion events. */
  954. static struct notifier_block sctp_inetaddr_notifier = {
  955. .notifier_call = sctp_inetaddr_event,
  956. };
  957. /* Socket operations. */
  958. static const struct proto_ops inet_seqpacket_ops = {
  959. .family = PF_INET,
  960. .owner = THIS_MODULE,
  961. .release = inet_release, /* Needs to be wrapped... */
  962. .bind = inet_bind,
  963. .connect = sctp_inet_connect,
  964. .socketpair = sock_no_socketpair,
  965. .accept = inet_accept,
  966. .getname = inet_getname, /* Semantics are different. */
  967. .poll = sctp_poll,
  968. .ioctl = inet_ioctl,
  969. .gettstamp = sock_gettstamp,
  970. .listen = sctp_inet_listen,
  971. .shutdown = inet_shutdown, /* Looks harmless. */
  972. .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */
  973. .getsockopt = sock_common_getsockopt,
  974. .sendmsg = inet_sendmsg,
  975. .recvmsg = inet_recvmsg,
  976. .mmap = sock_no_mmap,
  977. .sendpage = sock_no_sendpage,
  978. };
  979. /* Registration with AF_INET family. */
  980. static struct inet_protosw sctp_seqpacket_protosw = {
  981. .type = SOCK_SEQPACKET,
  982. .protocol = IPPROTO_SCTP,
  983. .prot = &sctp_prot,
  984. .ops = &inet_seqpacket_ops,
  985. .flags = SCTP_PROTOSW_FLAG
  986. };
  987. static struct inet_protosw sctp_stream_protosw = {
  988. .type = SOCK_STREAM,
  989. .protocol = IPPROTO_SCTP,
  990. .prot = &sctp_prot,
  991. .ops = &inet_seqpacket_ops,
  992. .flags = SCTP_PROTOSW_FLAG
  993. };
  994. static int sctp4_rcv(struct sk_buff *skb)
  995. {
  996. SCTP_INPUT_CB(skb)->encap_port = 0;
  997. return sctp_rcv(skb);
  998. }
  999. /* Register with IP layer. */
  1000. static const struct net_protocol sctp_protocol = {
  1001. .handler = sctp4_rcv,
  1002. .err_handler = sctp_v4_err,
  1003. .no_policy = 1,
  1004. .icmp_strict_tag_validation = 1,
  1005. };
  1006. /* IPv4 address related functions. */
  1007. static struct sctp_af sctp_af_inet = {
  1008. .sa_family = AF_INET,
  1009. .sctp_xmit = sctp_v4_xmit,
  1010. .setsockopt = ip_setsockopt,
  1011. .getsockopt = ip_getsockopt,
  1012. .get_dst = sctp_v4_get_dst,
  1013. .get_saddr = sctp_v4_get_saddr,
  1014. .copy_addrlist = sctp_v4_copy_addrlist,
  1015. .from_skb = sctp_v4_from_skb,
  1016. .from_sk = sctp_v4_from_sk,
  1017. .from_addr_param = sctp_v4_from_addr_param,
  1018. .to_addr_param = sctp_v4_to_addr_param,
  1019. .cmp_addr = sctp_v4_cmp_addr,
  1020. .addr_valid = sctp_v4_addr_valid,
  1021. .inaddr_any = sctp_v4_inaddr_any,
  1022. .is_any = sctp_v4_is_any,
  1023. .available = sctp_v4_available,
  1024. .scope = sctp_v4_scope,
  1025. .skb_iif = sctp_v4_skb_iif,
  1026. .is_ce = sctp_v4_is_ce,
  1027. .seq_dump_addr = sctp_v4_seq_dump_addr,
  1028. .ecn_capable = sctp_v4_ecn_capable,
  1029. .net_header_len = sizeof(struct iphdr),
  1030. .sockaddr_len = sizeof(struct sockaddr_in),
  1031. .ip_options_len = sctp_v4_ip_options_len,
  1032. };
  1033. struct sctp_pf *sctp_get_pf_specific(sa_family_t family)
  1034. {
  1035. switch (family) {
  1036. case PF_INET:
  1037. return sctp_pf_inet_specific;
  1038. case PF_INET6:
  1039. return sctp_pf_inet6_specific;
  1040. default:
  1041. return NULL;
  1042. }
  1043. }
  1044. /* Register the PF specific function table. */
  1045. int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
  1046. {
  1047. switch (family) {
  1048. case PF_INET:
  1049. if (sctp_pf_inet_specific)
  1050. return 0;
  1051. sctp_pf_inet_specific = pf;
  1052. break;
  1053. case PF_INET6:
  1054. if (sctp_pf_inet6_specific)
  1055. return 0;
  1056. sctp_pf_inet6_specific = pf;
  1057. break;
  1058. default:
  1059. return 0;
  1060. }
  1061. return 1;
  1062. }
  1063. static inline int init_sctp_mibs(struct net *net)
  1064. {
  1065. net->sctp.sctp_statistics = alloc_percpu(struct sctp_mib);
  1066. if (!net->sctp.sctp_statistics)
  1067. return -ENOMEM;
  1068. return 0;
  1069. }
  1070. static inline void cleanup_sctp_mibs(struct net *net)
  1071. {
  1072. free_percpu(net->sctp.sctp_statistics);
  1073. }
  1074. static void sctp_v4_pf_init(void)
  1075. {
  1076. /* Initialize the SCTP specific PF functions. */
  1077. sctp_register_pf(&sctp_pf_inet, PF_INET);
  1078. sctp_register_af(&sctp_af_inet);
  1079. }
  1080. static void sctp_v4_pf_exit(void)
  1081. {
  1082. list_del(&sctp_af_inet.list);
  1083. }
  1084. static int sctp_v4_protosw_init(void)
  1085. {
  1086. int rc;
  1087. rc = proto_register(&sctp_prot, 1);
  1088. if (rc)
  1089. return rc;
  1090. /* Register SCTP(UDP and TCP style) with socket layer. */
  1091. inet_register_protosw(&sctp_seqpacket_protosw);
  1092. inet_register_protosw(&sctp_stream_protosw);
  1093. return 0;
  1094. }
  1095. static void sctp_v4_protosw_exit(void)
  1096. {
  1097. inet_unregister_protosw(&sctp_stream_protosw);
  1098. inet_unregister_protosw(&sctp_seqpacket_protosw);
  1099. proto_unregister(&sctp_prot);
  1100. }
  1101. static int sctp_v4_add_protocol(void)
  1102. {
  1103. /* Register notifier for inet address additions/deletions. */
  1104. register_inetaddr_notifier(&sctp_inetaddr_notifier);
  1105. /* Register SCTP with inet layer. */
  1106. if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
  1107. return -EAGAIN;
  1108. return 0;
  1109. }
  1110. static void sctp_v4_del_protocol(void)
  1111. {
  1112. inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
  1113. unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
  1114. }
  1115. static int __net_init sctp_defaults_init(struct net *net)
  1116. {
  1117. int status;
  1118. /*
  1119. * 14. Suggested SCTP Protocol Parameter Values
  1120. */
  1121. /* The following protocol parameters are RECOMMENDED: */
  1122. /* RTO.Initial - 3 seconds */
  1123. net->sctp.rto_initial = SCTP_RTO_INITIAL;
  1124. /* RTO.Min - 1 second */
  1125. net->sctp.rto_min = SCTP_RTO_MIN;
  1126. /* RTO.Max - 60 seconds */
  1127. net->sctp.rto_max = SCTP_RTO_MAX;
  1128. /* RTO.Alpha - 1/8 */
  1129. net->sctp.rto_alpha = SCTP_RTO_ALPHA;
  1130. /* RTO.Beta - 1/4 */
  1131. net->sctp.rto_beta = SCTP_RTO_BETA;
  1132. /* Valid.Cookie.Life - 60 seconds */
  1133. net->sctp.valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE;
  1134. /* Whether Cookie Preservative is enabled(1) or not(0) */
  1135. net->sctp.cookie_preserve_enable = 1;
  1136. /* Default sctp sockets to use md5 as their hmac alg */
  1137. #if defined (CONFIG_SCTP_DEFAULT_COOKIE_HMAC_MD5)
  1138. net->sctp.sctp_hmac_alg = "md5";
  1139. #elif defined (CONFIG_SCTP_DEFAULT_COOKIE_HMAC_SHA1)
  1140. net->sctp.sctp_hmac_alg = "sha1";
  1141. #else
  1142. net->sctp.sctp_hmac_alg = NULL;
  1143. #endif
  1144. /* Max.Burst - 4 */
  1145. net->sctp.max_burst = SCTP_DEFAULT_MAX_BURST;
  1146. /* Disable of Primary Path Switchover by default */
  1147. net->sctp.ps_retrans = SCTP_PS_RETRANS_MAX;
  1148. /* Enable pf state by default */
  1149. net->sctp.pf_enable = 1;
  1150. /* Ignore pf exposure feature by default */
  1151. net->sctp.pf_expose = SCTP_PF_EXPOSE_UNSET;
  1152. /* Association.Max.Retrans - 10 attempts
  1153. * Path.Max.Retrans - 5 attempts (per destination address)
  1154. * Max.Init.Retransmits - 8 attempts
  1155. */
  1156. net->sctp.max_retrans_association = 10;
  1157. net->sctp.max_retrans_path = 5;
  1158. net->sctp.max_retrans_init = 8;
  1159. /* Sendbuffer growth - do per-socket accounting */
  1160. net->sctp.sndbuf_policy = 0;
  1161. /* Rcvbuffer growth - do per-socket accounting */
  1162. net->sctp.rcvbuf_policy = 0;
  1163. /* HB.interval - 30 seconds */
  1164. net->sctp.hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
  1165. /* delayed SACK timeout */
  1166. net->sctp.sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK;
  1167. /* Disable ADDIP by default. */
  1168. net->sctp.addip_enable = 0;
  1169. net->sctp.addip_noauth = 0;
  1170. net->sctp.default_auto_asconf = 0;
  1171. /* Enable PR-SCTP by default. */
  1172. net->sctp.prsctp_enable = 1;
  1173. /* Disable RECONF by default. */
  1174. net->sctp.reconf_enable = 0;
  1175. /* Disable AUTH by default. */
  1176. net->sctp.auth_enable = 0;
  1177. /* Enable ECN by default. */
  1178. net->sctp.ecn_enable = 1;
  1179. /* Set UDP tunneling listening port to 0 by default */
  1180. net->sctp.udp_port = 0;
  1181. /* Set remote encap port to 0 by default */
  1182. net->sctp.encap_port = 0;
  1183. /* Set SCOPE policy to enabled */
  1184. net->sctp.scope_policy = SCTP_SCOPE_POLICY_ENABLE;
  1185. /* Set the default rwnd update threshold */
  1186. net->sctp.rwnd_upd_shift = SCTP_DEFAULT_RWND_SHIFT;
  1187. /* Initialize maximum autoclose timeout. */
  1188. net->sctp.max_autoclose = INT_MAX / HZ;
  1189. status = sctp_sysctl_net_register(net);
  1190. if (status)
  1191. goto err_sysctl_register;
  1192. /* Allocate and initialise sctp mibs. */
  1193. status = init_sctp_mibs(net);
  1194. if (status)
  1195. goto err_init_mibs;
  1196. #ifdef CONFIG_PROC_FS
  1197. /* Initialize proc fs directory. */
  1198. status = sctp_proc_init(net);
  1199. if (status)
  1200. goto err_init_proc;
  1201. #endif
  1202. sctp_dbg_objcnt_init(net);
  1203. /* Initialize the local address list. */
  1204. INIT_LIST_HEAD(&net->sctp.local_addr_list);
  1205. spin_lock_init(&net->sctp.local_addr_lock);
  1206. sctp_get_local_addr_list(net);
  1207. /* Initialize the address event list */
  1208. INIT_LIST_HEAD(&net->sctp.addr_waitq);
  1209. INIT_LIST_HEAD(&net->sctp.auto_asconf_splist);
  1210. spin_lock_init(&net->sctp.addr_wq_lock);
  1211. net->sctp.addr_wq_timer.expires = 0;
  1212. timer_setup(&net->sctp.addr_wq_timer, sctp_addr_wq_timeout_handler, 0);
  1213. return 0;
  1214. #ifdef CONFIG_PROC_FS
  1215. err_init_proc:
  1216. cleanup_sctp_mibs(net);
  1217. #endif
  1218. err_init_mibs:
  1219. sctp_sysctl_net_unregister(net);
  1220. err_sysctl_register:
  1221. return status;
  1222. }
  1223. static void __net_exit sctp_defaults_exit(struct net *net)
  1224. {
  1225. /* Free the local address list */
  1226. sctp_free_addr_wq(net);
  1227. sctp_free_local_addr_list(net);
  1228. #ifdef CONFIG_PROC_FS
  1229. remove_proc_subtree("sctp", net->proc_net);
  1230. net->sctp.proc_net_sctp = NULL;
  1231. #endif
  1232. cleanup_sctp_mibs(net);
  1233. sctp_sysctl_net_unregister(net);
  1234. }
  1235. static struct pernet_operations sctp_defaults_ops = {
  1236. .init = sctp_defaults_init,
  1237. .exit = sctp_defaults_exit,
  1238. };
  1239. static int __net_init sctp_ctrlsock_init(struct net *net)
  1240. {
  1241. int status;
  1242. /* Initialize the control inode/socket for handling OOTB packets. */
  1243. status = sctp_ctl_sock_init(net);
  1244. if (status)
  1245. pr_err("Failed to initialize the SCTP control sock\n");
  1246. return status;
  1247. }
  1248. static void __net_exit sctp_ctrlsock_exit(struct net *net)
  1249. {
  1250. /* Free the control endpoint. */
  1251. inet_ctl_sock_destroy(net->sctp.ctl_sock);
  1252. }
  1253. static struct pernet_operations sctp_ctrlsock_ops = {
  1254. .init = sctp_ctrlsock_init,
  1255. .exit = sctp_ctrlsock_exit,
  1256. };
  1257. /* Initialize the universe into something sensible. */
  1258. static __init int sctp_init(void)
  1259. {
  1260. unsigned long nr_pages = totalram_pages();
  1261. unsigned long limit;
  1262. unsigned long goal;
  1263. int max_entry_order;
  1264. int num_entries;
  1265. int max_share;
  1266. int status;
  1267. int order;
  1268. int i;
  1269. sock_skb_cb_check_size(sizeof(struct sctp_ulpevent));
  1270. /* Allocate bind_bucket and chunk caches. */
  1271. status = -ENOBUFS;
  1272. sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
  1273. sizeof(struct sctp_bind_bucket),
  1274. 0, SLAB_HWCACHE_ALIGN,
  1275. NULL);
  1276. if (!sctp_bucket_cachep)
  1277. goto out;
  1278. sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
  1279. sizeof(struct sctp_chunk),
  1280. 0, SLAB_HWCACHE_ALIGN,
  1281. NULL);
  1282. if (!sctp_chunk_cachep)
  1283. goto err_chunk_cachep;
  1284. status = percpu_counter_init(&sctp_sockets_allocated, 0, GFP_KERNEL);
  1285. if (status)
  1286. goto err_percpu_counter_init;
  1287. /* Implementation specific variables. */
  1288. /* Initialize default stream count setup information. */
  1289. sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
  1290. sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
  1291. /* Initialize handle used for association ids. */
  1292. idr_init(&sctp_assocs_id);
  1293. limit = nr_free_buffer_pages() / 8;
  1294. limit = max(limit, 128UL);
  1295. sysctl_sctp_mem[0] = limit / 4 * 3;
  1296. sysctl_sctp_mem[1] = limit;
  1297. sysctl_sctp_mem[2] = sysctl_sctp_mem[0] * 2;
  1298. /* Set per-socket limits to no more than 1/128 the pressure threshold*/
  1299. limit = (sysctl_sctp_mem[1]) << (PAGE_SHIFT - 7);
  1300. max_share = min(4UL*1024*1024, limit);
  1301. sysctl_sctp_rmem[0] = PAGE_SIZE; /* give each asoc 1 page min */
  1302. sysctl_sctp_rmem[1] = 1500 * SKB_TRUESIZE(1);
  1303. sysctl_sctp_rmem[2] = max(sysctl_sctp_rmem[1], max_share);
  1304. sysctl_sctp_wmem[0] = PAGE_SIZE;
  1305. sysctl_sctp_wmem[1] = 16*1024;
  1306. sysctl_sctp_wmem[2] = max(64*1024, max_share);
  1307. /* Size and allocate the association hash table.
  1308. * The methodology is similar to that of the tcp hash tables.
  1309. * Though not identical. Start by getting a goal size
  1310. */
  1311. if (nr_pages >= (128 * 1024))
  1312. goal = nr_pages >> (22 - PAGE_SHIFT);
  1313. else
  1314. goal = nr_pages >> (24 - PAGE_SHIFT);
  1315. /* Then compute the page order for said goal */
  1316. order = get_order(goal);
  1317. /* Now compute the required page order for the maximum sized table we
  1318. * want to create
  1319. */
  1320. max_entry_order = get_order(MAX_SCTP_PORT_HASH_ENTRIES *
  1321. sizeof(struct sctp_bind_hashbucket));
  1322. /* Limit the page order by that maximum hash table size */
  1323. order = min(order, max_entry_order);
  1324. /* Allocate and initialize the endpoint hash table. */
  1325. sctp_ep_hashsize = 64;
  1326. sctp_ep_hashtable =
  1327. kmalloc_array(64, sizeof(struct sctp_hashbucket), GFP_KERNEL);
  1328. if (!sctp_ep_hashtable) {
  1329. pr_err("Failed endpoint_hash alloc\n");
  1330. status = -ENOMEM;
  1331. goto err_ehash_alloc;
  1332. }
  1333. for (i = 0; i < sctp_ep_hashsize; i++) {
  1334. rwlock_init(&sctp_ep_hashtable[i].lock);
  1335. INIT_HLIST_HEAD(&sctp_ep_hashtable[i].chain);
  1336. }
  1337. /* Allocate and initialize the SCTP port hash table.
  1338. * Note that order is initalized to start at the max sized
  1339. * table we want to support. If we can't get that many pages
  1340. * reduce the order and try again
  1341. */
  1342. do {
  1343. sctp_port_hashtable = (struct sctp_bind_hashbucket *)
  1344. __get_free_pages(GFP_KERNEL | __GFP_NOWARN, order);
  1345. } while (!sctp_port_hashtable && --order > 0);
  1346. if (!sctp_port_hashtable) {
  1347. pr_err("Failed bind hash alloc\n");
  1348. status = -ENOMEM;
  1349. goto err_bhash_alloc;
  1350. }
  1351. /* Now compute the number of entries that will fit in the
  1352. * port hash space we allocated
  1353. */
  1354. num_entries = (1UL << order) * PAGE_SIZE /
  1355. sizeof(struct sctp_bind_hashbucket);
  1356. /* And finish by rounding it down to the nearest power of two.
  1357. * This wastes some memory of course, but it's needed because
  1358. * the hash function operates based on the assumption that
  1359. * the number of entries is a power of two.
  1360. */
  1361. sctp_port_hashsize = rounddown_pow_of_two(num_entries);
  1362. for (i = 0; i < sctp_port_hashsize; i++) {
  1363. spin_lock_init(&sctp_port_hashtable[i].lock);
  1364. INIT_HLIST_HEAD(&sctp_port_hashtable[i].chain);
  1365. }
  1366. status = sctp_transport_hashtable_init();
  1367. if (status)
  1368. goto err_thash_alloc;
  1369. pr_info("Hash tables configured (bind %d/%d)\n", sctp_port_hashsize,
  1370. num_entries);
  1371. sctp_sysctl_register();
  1372. INIT_LIST_HEAD(&sctp_address_families);
  1373. sctp_v4_pf_init();
  1374. sctp_v6_pf_init();
  1375. sctp_sched_ops_init();
  1376. status = register_pernet_subsys(&sctp_defaults_ops);
  1377. if (status)
  1378. goto err_register_defaults;
  1379. status = sctp_v4_protosw_init();
  1380. if (status)
  1381. goto err_protosw_init;
  1382. status = sctp_v6_protosw_init();
  1383. if (status)
  1384. goto err_v6_protosw_init;
  1385. status = register_pernet_subsys(&sctp_ctrlsock_ops);
  1386. if (status)
  1387. goto err_register_ctrlsock;
  1388. status = sctp_v4_add_protocol();
  1389. if (status)
  1390. goto err_add_protocol;
  1391. /* Register SCTP with inet6 layer. */
  1392. status = sctp_v6_add_protocol();
  1393. if (status)
  1394. goto err_v6_add_protocol;
  1395. if (sctp_offload_init() < 0)
  1396. pr_crit("%s: Cannot add SCTP protocol offload\n", __func__);
  1397. out:
  1398. return status;
  1399. err_v6_add_protocol:
  1400. sctp_v4_del_protocol();
  1401. err_add_protocol:
  1402. unregister_pernet_subsys(&sctp_ctrlsock_ops);
  1403. err_register_ctrlsock:
  1404. sctp_v6_protosw_exit();
  1405. err_v6_protosw_init:
  1406. sctp_v4_protosw_exit();
  1407. err_protosw_init:
  1408. unregister_pernet_subsys(&sctp_defaults_ops);
  1409. err_register_defaults:
  1410. sctp_v4_pf_exit();
  1411. sctp_v6_pf_exit();
  1412. sctp_sysctl_unregister();
  1413. free_pages((unsigned long)sctp_port_hashtable,
  1414. get_order(sctp_port_hashsize *
  1415. sizeof(struct sctp_bind_hashbucket)));
  1416. err_bhash_alloc:
  1417. sctp_transport_hashtable_destroy();
  1418. err_thash_alloc:
  1419. kfree(sctp_ep_hashtable);
  1420. err_ehash_alloc:
  1421. percpu_counter_destroy(&sctp_sockets_allocated);
  1422. err_percpu_counter_init:
  1423. kmem_cache_destroy(sctp_chunk_cachep);
  1424. err_chunk_cachep:
  1425. kmem_cache_destroy(sctp_bucket_cachep);
  1426. goto out;
  1427. }
  1428. /* Exit handler for the SCTP protocol. */
  1429. static __exit void sctp_exit(void)
  1430. {
  1431. /* BUG. This should probably do something useful like clean
  1432. * up all the remaining associations and all that memory.
  1433. */
  1434. /* Unregister with inet6/inet layers. */
  1435. sctp_v6_del_protocol();
  1436. sctp_v4_del_protocol();
  1437. unregister_pernet_subsys(&sctp_ctrlsock_ops);
  1438. /* Free protosw registrations */
  1439. sctp_v6_protosw_exit();
  1440. sctp_v4_protosw_exit();
  1441. unregister_pernet_subsys(&sctp_defaults_ops);
  1442. /* Unregister with socket layer. */
  1443. sctp_v6_pf_exit();
  1444. sctp_v4_pf_exit();
  1445. sctp_sysctl_unregister();
  1446. free_pages((unsigned long)sctp_port_hashtable,
  1447. get_order(sctp_port_hashsize *
  1448. sizeof(struct sctp_bind_hashbucket)));
  1449. kfree(sctp_ep_hashtable);
  1450. sctp_transport_hashtable_destroy();
  1451. percpu_counter_destroy(&sctp_sockets_allocated);
  1452. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1453. kmem_cache_destroy(sctp_chunk_cachep);
  1454. kmem_cache_destroy(sctp_bucket_cachep);
  1455. }
  1456. module_init(sctp_init);
  1457. module_exit(sctp_exit);
  1458. /*
  1459. * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly.
  1460. */
  1461. MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132");
  1462. MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132");
  1463. MODULE_AUTHOR("Linux Kernel SCTP developers <[email protected]>");
  1464. MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
  1465. module_param_named(no_checksums, sctp_checksum_disable, bool, 0644);
  1466. MODULE_PARM_DESC(no_checksums, "Disable checksums computing and verification");
  1467. MODULE_LICENSE("GPL");