protocol.c 42 KB

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