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