protocol.c 43 KB

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