protocol.c 44 KB

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