protocol.c 43 KB

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