protocol.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  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. /* Global data structures. */
  61. struct sctp_globals sctp_globals __read_mostly;
  62. struct idr sctp_assocs_id;
  63. DEFINE_SPINLOCK(sctp_assocs_id_lock);
  64. static struct sctp_pf *sctp_pf_inet6_specific;
  65. static struct sctp_pf *sctp_pf_inet_specific;
  66. static struct sctp_af *sctp_af_v4_specific;
  67. static struct sctp_af *sctp_af_v6_specific;
  68. struct kmem_cache *sctp_chunk_cachep __read_mostly;
  69. struct kmem_cache *sctp_bucket_cachep __read_mostly;
  70. long sysctl_sctp_mem[3];
  71. int sysctl_sctp_rmem[3];
  72. int sysctl_sctp_wmem[3];
  73. /* Set up the proc fs entry for the SCTP protocol. */
  74. static int __net_init sctp_proc_init(struct net *net)
  75. {
  76. #ifdef CONFIG_PROC_FS
  77. net->sctp.proc_net_sctp = proc_net_mkdir(net, "sctp", net->proc_net);
  78. if (!net->sctp.proc_net_sctp)
  79. goto out_proc_net_sctp;
  80. if (sctp_snmp_proc_init(net))
  81. goto out_snmp_proc_init;
  82. if (sctp_eps_proc_init(net))
  83. goto out_eps_proc_init;
  84. if (sctp_assocs_proc_init(net))
  85. goto out_assocs_proc_init;
  86. if (sctp_remaddr_proc_init(net))
  87. goto out_remaddr_proc_init;
  88. return 0;
  89. out_remaddr_proc_init:
  90. sctp_assocs_proc_exit(net);
  91. out_assocs_proc_init:
  92. sctp_eps_proc_exit(net);
  93. out_eps_proc_init:
  94. sctp_snmp_proc_exit(net);
  95. out_snmp_proc_init:
  96. remove_proc_entry("sctp", net->proc_net);
  97. net->sctp.proc_net_sctp = NULL;
  98. out_proc_net_sctp:
  99. return -ENOMEM;
  100. #endif /* CONFIG_PROC_FS */
  101. return 0;
  102. }
  103. /* Clean up the proc fs entry for the SCTP protocol.
  104. * Note: Do not make this __exit as it is used in the init error
  105. * path.
  106. */
  107. static void sctp_proc_exit(struct net *net)
  108. {
  109. #ifdef CONFIG_PROC_FS
  110. sctp_snmp_proc_exit(net);
  111. sctp_eps_proc_exit(net);
  112. sctp_assocs_proc_exit(net);
  113. sctp_remaddr_proc_exit(net);
  114. remove_proc_entry("sctp", net->proc_net);
  115. net->sctp.proc_net_sctp = NULL;
  116. #endif
  117. }
  118. /* Private helper to extract ipv4 address and stash them in
  119. * the protocol structure.
  120. */
  121. static void sctp_v4_copy_addrlist(struct list_head *addrlist,
  122. struct net_device *dev)
  123. {
  124. struct in_device *in_dev;
  125. struct in_ifaddr *ifa;
  126. struct sctp_sockaddr_entry *addr;
  127. rcu_read_lock();
  128. if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
  129. rcu_read_unlock();
  130. return;
  131. }
  132. for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
  133. /* Add the address to the local list. */
  134. addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
  135. if (addr) {
  136. addr->a.v4.sin_family = AF_INET;
  137. addr->a.v4.sin_port = 0;
  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. !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. if (!laddr->valid)
  432. continue;
  433. if ((laddr->state == SCTP_ADDR_SRC) &&
  434. (AF_INET == laddr->a.sa.sa_family)) {
  435. fl4->saddr = laddr->a.v4.sin_addr.s_addr;
  436. fl4->fl4_sport = laddr->a.v4.sin_port;
  437. rt = ip_route_output_key(sock_net(sk), fl4);
  438. if (!IS_ERR(rt)) {
  439. dst = &rt->dst;
  440. goto out_unlock;
  441. }
  442. }
  443. }
  444. out_unlock:
  445. rcu_read_unlock();
  446. out:
  447. t->dst = dst;
  448. if (dst)
  449. pr_debug("rt_dst:%pI4, rt_src:%pI4\n",
  450. &fl4->daddr, &fl4->saddr);
  451. else
  452. pr_debug("no route\n");
  453. }
  454. /* For v4, the source address is cached in the route entry(dst). So no need
  455. * to cache it separately and hence this is an empty routine.
  456. */
  457. static void sctp_v4_get_saddr(struct sctp_sock *sk,
  458. struct sctp_transport *t,
  459. struct flowi *fl)
  460. {
  461. union sctp_addr *saddr = &t->saddr;
  462. struct rtable *rt = (struct rtable *)t->dst;
  463. if (rt) {
  464. saddr->v4.sin_family = AF_INET;
  465. saddr->v4.sin_addr.s_addr = fl->u.ip4.saddr;
  466. }
  467. }
  468. /* What interface did this skb arrive on? */
  469. static int sctp_v4_skb_iif(const struct sk_buff *skb)
  470. {
  471. return inet_iif(skb);
  472. }
  473. /* Was this packet marked by Explicit Congestion Notification? */
  474. static int sctp_v4_is_ce(const struct sk_buff *skb)
  475. {
  476. return INET_ECN_is_ce(ip_hdr(skb)->tos);
  477. }
  478. /* Create and initialize a new sk for the socket returned by accept(). */
  479. static struct sock *sctp_v4_create_accept_sk(struct sock *sk,
  480. struct sctp_association *asoc)
  481. {
  482. struct sock *newsk = sk_alloc(sock_net(sk), PF_INET, GFP_KERNEL,
  483. sk->sk_prot);
  484. struct inet_sock *newinet;
  485. if (!newsk)
  486. goto out;
  487. sock_init_data(NULL, newsk);
  488. sctp_copy_sock(newsk, sk, asoc);
  489. sock_reset_flag(newsk, SOCK_ZAPPED);
  490. newinet = inet_sk(newsk);
  491. newinet->inet_daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
  492. sk_refcnt_debug_inc(newsk);
  493. if (newsk->sk_prot->init(newsk)) {
  494. sk_common_release(newsk);
  495. newsk = NULL;
  496. }
  497. out:
  498. return newsk;
  499. }
  500. /* Map address, empty for v4 family */
  501. static void sctp_v4_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
  502. {
  503. /* Empty */
  504. }
  505. /* Dump the v4 addr to the seq file. */
  506. static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
  507. {
  508. seq_printf(seq, "%pI4 ", &addr->v4.sin_addr);
  509. }
  510. static void sctp_v4_ecn_capable(struct sock *sk)
  511. {
  512. INET_ECN_xmit(sk);
  513. }
  514. static void sctp_addr_wq_timeout_handler(unsigned long arg)
  515. {
  516. struct net *net = (struct net *)arg;
  517. struct sctp_sockaddr_entry *addrw, *temp;
  518. struct sctp_sock *sp;
  519. spin_lock_bh(&net->sctp.addr_wq_lock);
  520. list_for_each_entry_safe(addrw, temp, &net->sctp.addr_waitq, list) {
  521. pr_debug("%s: the first ent in wq:%p is addr:%pISc for cmd:%d at "
  522. "entry:%p\n", __func__, &net->sctp.addr_waitq, &addrw->a.sa,
  523. addrw->state, addrw);
  524. #if IS_ENABLED(CONFIG_IPV6)
  525. /* Now we send an ASCONF for each association */
  526. /* Note. we currently don't handle link local IPv6 addressees */
  527. if (addrw->a.sa.sa_family == AF_INET6) {
  528. struct in6_addr *in6;
  529. if (ipv6_addr_type(&addrw->a.v6.sin6_addr) &
  530. IPV6_ADDR_LINKLOCAL)
  531. goto free_next;
  532. in6 = (struct in6_addr *)&addrw->a.v6.sin6_addr;
  533. if (ipv6_chk_addr(net, in6, NULL, 0) == 0 &&
  534. addrw->state == SCTP_ADDR_NEW) {
  535. unsigned long timeo_val;
  536. pr_debug("%s: this is on DAD, trying %d sec "
  537. "later\n", __func__,
  538. SCTP_ADDRESS_TICK_DELAY);
  539. timeo_val = jiffies;
  540. timeo_val += msecs_to_jiffies(SCTP_ADDRESS_TICK_DELAY);
  541. mod_timer(&net->sctp.addr_wq_timer, timeo_val);
  542. break;
  543. }
  544. }
  545. #endif
  546. list_for_each_entry(sp, &net->sctp.auto_asconf_splist, auto_asconf_list) {
  547. struct sock *sk;
  548. sk = sctp_opt2sk(sp);
  549. /* ignore bound-specific endpoints */
  550. if (!sctp_is_ep_boundall(sk))
  551. continue;
  552. sctp_bh_lock_sock(sk);
  553. if (sctp_asconf_mgmt(sp, addrw) < 0)
  554. pr_debug("%s: sctp_asconf_mgmt failed\n", __func__);
  555. sctp_bh_unlock_sock(sk);
  556. }
  557. #if IS_ENABLED(CONFIG_IPV6)
  558. free_next:
  559. #endif
  560. list_del(&addrw->list);
  561. kfree(addrw);
  562. }
  563. spin_unlock_bh(&net->sctp.addr_wq_lock);
  564. }
  565. static void sctp_free_addr_wq(struct net *net)
  566. {
  567. struct sctp_sockaddr_entry *addrw;
  568. struct sctp_sockaddr_entry *temp;
  569. spin_lock_bh(&net->sctp.addr_wq_lock);
  570. del_timer(&net->sctp.addr_wq_timer);
  571. list_for_each_entry_safe(addrw, temp, &net->sctp.addr_waitq, list) {
  572. list_del(&addrw->list);
  573. kfree(addrw);
  574. }
  575. spin_unlock_bh(&net->sctp.addr_wq_lock);
  576. }
  577. /* lookup the entry for the same address in the addr_waitq
  578. * sctp_addr_wq MUST be locked
  579. */
  580. static struct sctp_sockaddr_entry *sctp_addr_wq_lookup(struct net *net,
  581. struct sctp_sockaddr_entry *addr)
  582. {
  583. struct sctp_sockaddr_entry *addrw;
  584. list_for_each_entry(addrw, &net->sctp.addr_waitq, list) {
  585. if (addrw->a.sa.sa_family != addr->a.sa.sa_family)
  586. continue;
  587. if (addrw->a.sa.sa_family == AF_INET) {
  588. if (addrw->a.v4.sin_addr.s_addr ==
  589. addr->a.v4.sin_addr.s_addr)
  590. return addrw;
  591. } else if (addrw->a.sa.sa_family == AF_INET6) {
  592. if (ipv6_addr_equal(&addrw->a.v6.sin6_addr,
  593. &addr->a.v6.sin6_addr))
  594. return addrw;
  595. }
  596. }
  597. return NULL;
  598. }
  599. void sctp_addr_wq_mgmt(struct net *net, struct sctp_sockaddr_entry *addr, int cmd)
  600. {
  601. struct sctp_sockaddr_entry *addrw;
  602. unsigned long timeo_val;
  603. /* first, we check if an opposite message already exist in the queue.
  604. * If we found such message, it is removed.
  605. * This operation is a bit stupid, but the DHCP client attaches the
  606. * new address after a couple of addition and deletion of that address
  607. */
  608. spin_lock_bh(&net->sctp.addr_wq_lock);
  609. /* Offsets existing events in addr_wq */
  610. addrw = sctp_addr_wq_lookup(net, addr);
  611. if (addrw) {
  612. if (addrw->state != cmd) {
  613. pr_debug("%s: offsets existing entry for %d, addr:%pISc "
  614. "in wq:%p\n", __func__, addrw->state, &addrw->a.sa,
  615. &net->sctp.addr_waitq);
  616. list_del(&addrw->list);
  617. kfree(addrw);
  618. }
  619. spin_unlock_bh(&net->sctp.addr_wq_lock);
  620. return;
  621. }
  622. /* OK, we have to add the new address to the wait queue */
  623. addrw = kmemdup(addr, sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
  624. if (addrw == NULL) {
  625. spin_unlock_bh(&net->sctp.addr_wq_lock);
  626. return;
  627. }
  628. addrw->state = cmd;
  629. list_add_tail(&addrw->list, &net->sctp.addr_waitq);
  630. pr_debug("%s: add new entry for cmd:%d, addr:%pISc in wq:%p\n",
  631. __func__, addrw->state, &addrw->a.sa, &net->sctp.addr_waitq);
  632. if (!timer_pending(&net->sctp.addr_wq_timer)) {
  633. timeo_val = jiffies;
  634. timeo_val += msecs_to_jiffies(SCTP_ADDRESS_TICK_DELAY);
  635. mod_timer(&net->sctp.addr_wq_timer, timeo_val);
  636. }
  637. spin_unlock_bh(&net->sctp.addr_wq_lock);
  638. }
  639. /* Event handler for inet address addition/deletion events.
  640. * The sctp_local_addr_list needs to be protocted by a spin lock since
  641. * multiple notifiers (say IPv4 and IPv6) may be running at the same
  642. * time and thus corrupt the list.
  643. * The reader side is protected with RCU.
  644. */
  645. static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
  646. void *ptr)
  647. {
  648. struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
  649. struct sctp_sockaddr_entry *addr = NULL;
  650. struct sctp_sockaddr_entry *temp;
  651. struct net *net = dev_net(ifa->ifa_dev->dev);
  652. int found = 0;
  653. switch (ev) {
  654. case NETDEV_UP:
  655. addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
  656. if (addr) {
  657. addr->a.v4.sin_family = AF_INET;
  658. addr->a.v4.sin_port = 0;
  659. addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
  660. addr->valid = 1;
  661. spin_lock_bh(&net->sctp.local_addr_lock);
  662. list_add_tail_rcu(&addr->list, &net->sctp.local_addr_list);
  663. sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_NEW);
  664. spin_unlock_bh(&net->sctp.local_addr_lock);
  665. }
  666. break;
  667. case NETDEV_DOWN:
  668. spin_lock_bh(&net->sctp.local_addr_lock);
  669. list_for_each_entry_safe(addr, temp,
  670. &net->sctp.local_addr_list, list) {
  671. if (addr->a.sa.sa_family == AF_INET &&
  672. addr->a.v4.sin_addr.s_addr ==
  673. ifa->ifa_local) {
  674. sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL);
  675. found = 1;
  676. addr->valid = 0;
  677. list_del_rcu(&addr->list);
  678. break;
  679. }
  680. }
  681. spin_unlock_bh(&net->sctp.local_addr_lock);
  682. if (found)
  683. kfree_rcu(addr, rcu);
  684. break;
  685. }
  686. return NOTIFY_DONE;
  687. }
  688. /*
  689. * Initialize the control inode/socket with a control endpoint data
  690. * structure. This endpoint is reserved exclusively for the OOTB processing.
  691. */
  692. static int sctp_ctl_sock_init(struct net *net)
  693. {
  694. int err;
  695. sa_family_t family = PF_INET;
  696. if (sctp_get_pf_specific(PF_INET6))
  697. family = PF_INET6;
  698. err = inet_ctl_sock_create(&net->sctp.ctl_sock, family,
  699. SOCK_SEQPACKET, IPPROTO_SCTP, net);
  700. /* If IPv6 socket could not be created, try the IPv4 socket */
  701. if (err < 0 && family == PF_INET6)
  702. err = inet_ctl_sock_create(&net->sctp.ctl_sock, AF_INET,
  703. SOCK_SEQPACKET, IPPROTO_SCTP,
  704. net);
  705. if (err < 0) {
  706. pr_err("Failed to create the SCTP control socket\n");
  707. return err;
  708. }
  709. return 0;
  710. }
  711. /* Register address family specific functions. */
  712. int sctp_register_af(struct sctp_af *af)
  713. {
  714. switch (af->sa_family) {
  715. case AF_INET:
  716. if (sctp_af_v4_specific)
  717. return 0;
  718. sctp_af_v4_specific = af;
  719. break;
  720. case AF_INET6:
  721. if (sctp_af_v6_specific)
  722. return 0;
  723. sctp_af_v6_specific = af;
  724. break;
  725. default:
  726. return 0;
  727. }
  728. INIT_LIST_HEAD(&af->list);
  729. list_add_tail(&af->list, &sctp_address_families);
  730. return 1;
  731. }
  732. /* Get the table of functions for manipulating a particular address
  733. * family.
  734. */
  735. struct sctp_af *sctp_get_af_specific(sa_family_t family)
  736. {
  737. switch (family) {
  738. case AF_INET:
  739. return sctp_af_v4_specific;
  740. case AF_INET6:
  741. return sctp_af_v6_specific;
  742. default:
  743. return NULL;
  744. }
  745. }
  746. /* Common code to initialize a AF_INET msg_name. */
  747. static void sctp_inet_msgname(char *msgname, int *addr_len)
  748. {
  749. struct sockaddr_in *sin;
  750. sin = (struct sockaddr_in *)msgname;
  751. *addr_len = sizeof(struct sockaddr_in);
  752. sin->sin_family = AF_INET;
  753. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  754. }
  755. /* Copy the primary address of the peer primary address as the msg_name. */
  756. static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname,
  757. int *addr_len)
  758. {
  759. struct sockaddr_in *sin, *sinfrom;
  760. if (msgname) {
  761. struct sctp_association *asoc;
  762. asoc = event->asoc;
  763. sctp_inet_msgname(msgname, addr_len);
  764. sin = (struct sockaddr_in *)msgname;
  765. sinfrom = &asoc->peer.primary_addr.v4;
  766. sin->sin_port = htons(asoc->peer.port);
  767. sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr;
  768. }
  769. }
  770. /* Initialize and copy out a msgname from an inbound skb. */
  771. static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len)
  772. {
  773. if (msgname) {
  774. struct sctphdr *sh = sctp_hdr(skb);
  775. struct sockaddr_in *sin = (struct sockaddr_in *)msgname;
  776. sctp_inet_msgname(msgname, len);
  777. sin->sin_port = sh->source;
  778. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  779. }
  780. }
  781. /* Do we support this AF? */
  782. static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp)
  783. {
  784. /* PF_INET only supports AF_INET addresses. */
  785. return AF_INET == family;
  786. }
  787. /* Address matching with wildcards allowed. */
  788. static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
  789. const union sctp_addr *addr2,
  790. struct sctp_sock *opt)
  791. {
  792. /* PF_INET only supports AF_INET addresses. */
  793. if (addr1->sa.sa_family != addr2->sa.sa_family)
  794. return 0;
  795. if (htonl(INADDR_ANY) == addr1->v4.sin_addr.s_addr ||
  796. htonl(INADDR_ANY) == addr2->v4.sin_addr.s_addr)
  797. return 1;
  798. if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
  799. return 1;
  800. return 0;
  801. }
  802. /* Verify that provided sockaddr looks bindable. Common verification has
  803. * already been taken care of.
  804. */
  805. static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
  806. {
  807. return sctp_v4_available(addr, opt);
  808. }
  809. /* Verify that sockaddr looks sendable. Common verification has already
  810. * been taken care of.
  811. */
  812. static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
  813. {
  814. return 1;
  815. }
  816. /* Fill in Supported Address Type information for INIT and INIT-ACK
  817. * chunks. Returns number of addresses supported.
  818. */
  819. static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
  820. __be16 *types)
  821. {
  822. types[0] = SCTP_PARAM_IPV4_ADDRESS;
  823. return 1;
  824. }
  825. /* Wrapper routine that calls the ip transmit routine. */
  826. static inline int sctp_v4_xmit(struct sk_buff *skb,
  827. struct sctp_transport *transport)
  828. {
  829. struct inet_sock *inet = inet_sk(skb->sk);
  830. pr_debug("%s: skb:%p, len:%d, src:%pI4, dst:%pI4\n", __func__, skb,
  831. skb->len, &transport->fl.u.ip4.saddr, &transport->fl.u.ip4.daddr);
  832. inet->pmtudisc = transport->param_flags & SPP_PMTUD_ENABLE ?
  833. IP_PMTUDISC_DO : IP_PMTUDISC_DONT;
  834. SCTP_INC_STATS(sock_net(&inet->sk), SCTP_MIB_OUTSCTPPACKS);
  835. return ip_queue_xmit(skb, &transport->fl);
  836. }
  837. static struct sctp_af sctp_af_inet;
  838. static struct sctp_pf sctp_pf_inet = {
  839. .event_msgname = sctp_inet_event_msgname,
  840. .skb_msgname = sctp_inet_skb_msgname,
  841. .af_supported = sctp_inet_af_supported,
  842. .cmp_addr = sctp_inet_cmp_addr,
  843. .bind_verify = sctp_inet_bind_verify,
  844. .send_verify = sctp_inet_send_verify,
  845. .supported_addrs = sctp_inet_supported_addrs,
  846. .create_accept_sk = sctp_v4_create_accept_sk,
  847. .addr_v4map = sctp_v4_addr_v4map,
  848. .af = &sctp_af_inet
  849. };
  850. /* Notifier for inetaddr addition/deletion events. */
  851. static struct notifier_block sctp_inetaddr_notifier = {
  852. .notifier_call = sctp_inetaddr_event,
  853. };
  854. /* Socket operations. */
  855. static const struct proto_ops inet_seqpacket_ops = {
  856. .family = PF_INET,
  857. .owner = THIS_MODULE,
  858. .release = inet_release, /* Needs to be wrapped... */
  859. .bind = inet_bind,
  860. .connect = inet_dgram_connect,
  861. .socketpair = sock_no_socketpair,
  862. .accept = inet_accept,
  863. .getname = inet_getname, /* Semantics are different. */
  864. .poll = sctp_poll,
  865. .ioctl = inet_ioctl,
  866. .listen = sctp_inet_listen,
  867. .shutdown = inet_shutdown, /* Looks harmless. */
  868. .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */
  869. .getsockopt = sock_common_getsockopt,
  870. .sendmsg = inet_sendmsg,
  871. .recvmsg = sock_common_recvmsg,
  872. .mmap = sock_no_mmap,
  873. .sendpage = sock_no_sendpage,
  874. #ifdef CONFIG_COMPAT
  875. .compat_setsockopt = compat_sock_common_setsockopt,
  876. .compat_getsockopt = compat_sock_common_getsockopt,
  877. #endif
  878. };
  879. /* Registration with AF_INET family. */
  880. static struct inet_protosw sctp_seqpacket_protosw = {
  881. .type = SOCK_SEQPACKET,
  882. .protocol = IPPROTO_SCTP,
  883. .prot = &sctp_prot,
  884. .ops = &inet_seqpacket_ops,
  885. .no_check = 0,
  886. .flags = SCTP_PROTOSW_FLAG
  887. };
  888. static struct inet_protosw sctp_stream_protosw = {
  889. .type = SOCK_STREAM,
  890. .protocol = IPPROTO_SCTP,
  891. .prot = &sctp_prot,
  892. .ops = &inet_seqpacket_ops,
  893. .no_check = 0,
  894. .flags = SCTP_PROTOSW_FLAG
  895. };
  896. /* Register with IP layer. */
  897. static const struct net_protocol sctp_protocol = {
  898. .handler = sctp_rcv,
  899. .err_handler = sctp_v4_err,
  900. .no_policy = 1,
  901. .netns_ok = 1,
  902. };
  903. /* IPv4 address related functions. */
  904. static struct sctp_af sctp_af_inet = {
  905. .sa_family = AF_INET,
  906. .sctp_xmit = sctp_v4_xmit,
  907. .setsockopt = ip_setsockopt,
  908. .getsockopt = ip_getsockopt,
  909. .get_dst = sctp_v4_get_dst,
  910. .get_saddr = sctp_v4_get_saddr,
  911. .copy_addrlist = sctp_v4_copy_addrlist,
  912. .from_skb = sctp_v4_from_skb,
  913. .from_sk = sctp_v4_from_sk,
  914. .to_sk_saddr = sctp_v4_to_sk_saddr,
  915. .to_sk_daddr = sctp_v4_to_sk_daddr,
  916. .from_addr_param = sctp_v4_from_addr_param,
  917. .to_addr_param = sctp_v4_to_addr_param,
  918. .cmp_addr = sctp_v4_cmp_addr,
  919. .addr_valid = sctp_v4_addr_valid,
  920. .inaddr_any = sctp_v4_inaddr_any,
  921. .is_any = sctp_v4_is_any,
  922. .available = sctp_v4_available,
  923. .scope = sctp_v4_scope,
  924. .skb_iif = sctp_v4_skb_iif,
  925. .is_ce = sctp_v4_is_ce,
  926. .seq_dump_addr = sctp_v4_seq_dump_addr,
  927. .ecn_capable = sctp_v4_ecn_capable,
  928. .net_header_len = sizeof(struct iphdr),
  929. .sockaddr_len = sizeof(struct sockaddr_in),
  930. #ifdef CONFIG_COMPAT
  931. .compat_setsockopt = compat_ip_setsockopt,
  932. .compat_getsockopt = compat_ip_getsockopt,
  933. #endif
  934. };
  935. struct sctp_pf *sctp_get_pf_specific(sa_family_t family) {
  936. switch (family) {
  937. case PF_INET:
  938. return sctp_pf_inet_specific;
  939. case PF_INET6:
  940. return sctp_pf_inet6_specific;
  941. default:
  942. return NULL;
  943. }
  944. }
  945. /* Register the PF specific function table. */
  946. int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
  947. {
  948. switch (family) {
  949. case PF_INET:
  950. if (sctp_pf_inet_specific)
  951. return 0;
  952. sctp_pf_inet_specific = pf;
  953. break;
  954. case PF_INET6:
  955. if (sctp_pf_inet6_specific)
  956. return 0;
  957. sctp_pf_inet6_specific = pf;
  958. break;
  959. default:
  960. return 0;
  961. }
  962. return 1;
  963. }
  964. static inline int init_sctp_mibs(struct net *net)
  965. {
  966. return snmp_mib_init((void __percpu **)net->sctp.sctp_statistics,
  967. sizeof(struct sctp_mib),
  968. __alignof__(struct sctp_mib));
  969. }
  970. static inline void cleanup_sctp_mibs(struct net *net)
  971. {
  972. snmp_mib_free((void __percpu **)net->sctp.sctp_statistics);
  973. }
  974. static void sctp_v4_pf_init(void)
  975. {
  976. /* Initialize the SCTP specific PF functions. */
  977. sctp_register_pf(&sctp_pf_inet, PF_INET);
  978. sctp_register_af(&sctp_af_inet);
  979. }
  980. static void sctp_v4_pf_exit(void)
  981. {
  982. list_del(&sctp_af_inet.list);
  983. }
  984. static int sctp_v4_protosw_init(void)
  985. {
  986. int rc;
  987. rc = proto_register(&sctp_prot, 1);
  988. if (rc)
  989. return rc;
  990. /* Register SCTP(UDP and TCP style) with socket layer. */
  991. inet_register_protosw(&sctp_seqpacket_protosw);
  992. inet_register_protosw(&sctp_stream_protosw);
  993. return 0;
  994. }
  995. static void sctp_v4_protosw_exit(void)
  996. {
  997. inet_unregister_protosw(&sctp_stream_protosw);
  998. inet_unregister_protosw(&sctp_seqpacket_protosw);
  999. proto_unregister(&sctp_prot);
  1000. }
  1001. static int sctp_v4_add_protocol(void)
  1002. {
  1003. /* Register notifier for inet address additions/deletions. */
  1004. register_inetaddr_notifier(&sctp_inetaddr_notifier);
  1005. /* Register SCTP with inet layer. */
  1006. if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
  1007. return -EAGAIN;
  1008. return 0;
  1009. }
  1010. static void sctp_v4_del_protocol(void)
  1011. {
  1012. inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
  1013. unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
  1014. }
  1015. static int __net_init sctp_net_init(struct net *net)
  1016. {
  1017. int status;
  1018. /*
  1019. * 14. Suggested SCTP Protocol Parameter Values
  1020. */
  1021. /* The following protocol parameters are RECOMMENDED: */
  1022. /* RTO.Initial - 3 seconds */
  1023. net->sctp.rto_initial = SCTP_RTO_INITIAL;
  1024. /* RTO.Min - 1 second */
  1025. net->sctp.rto_min = SCTP_RTO_MIN;
  1026. /* RTO.Max - 60 seconds */
  1027. net->sctp.rto_max = SCTP_RTO_MAX;
  1028. /* RTO.Alpha - 1/8 */
  1029. net->sctp.rto_alpha = SCTP_RTO_ALPHA;
  1030. /* RTO.Beta - 1/4 */
  1031. net->sctp.rto_beta = SCTP_RTO_BETA;
  1032. /* Valid.Cookie.Life - 60 seconds */
  1033. net->sctp.valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE;
  1034. /* Whether Cookie Preservative is enabled(1) or not(0) */
  1035. net->sctp.cookie_preserve_enable = 1;
  1036. /* Default sctp sockets to use md5 as their hmac alg */
  1037. #if defined (CONFIG_SCTP_DEFAULT_COOKIE_HMAC_MD5)
  1038. net->sctp.sctp_hmac_alg = "md5";
  1039. #elif defined (CONFIG_SCTP_DEFAULT_COOKIE_HMAC_SHA1)
  1040. net->sctp.sctp_hmac_alg = "sha1";
  1041. #else
  1042. net->sctp.sctp_hmac_alg = NULL;
  1043. #endif
  1044. /* Max.Burst - 4 */
  1045. net->sctp.max_burst = SCTP_DEFAULT_MAX_BURST;
  1046. /* Association.Max.Retrans - 10 attempts
  1047. * Path.Max.Retrans - 5 attempts (per destination address)
  1048. * Max.Init.Retransmits - 8 attempts
  1049. */
  1050. net->sctp.max_retrans_association = 10;
  1051. net->sctp.max_retrans_path = 5;
  1052. net->sctp.max_retrans_init = 8;
  1053. /* Sendbuffer growth - do per-socket accounting */
  1054. net->sctp.sndbuf_policy = 0;
  1055. /* Rcvbuffer growth - do per-socket accounting */
  1056. net->sctp.rcvbuf_policy = 0;
  1057. /* HB.interval - 30 seconds */
  1058. net->sctp.hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
  1059. /* delayed SACK timeout */
  1060. net->sctp.sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK;
  1061. /* Disable ADDIP by default. */
  1062. net->sctp.addip_enable = 0;
  1063. net->sctp.addip_noauth = 0;
  1064. net->sctp.default_auto_asconf = 0;
  1065. /* Enable PR-SCTP by default. */
  1066. net->sctp.prsctp_enable = 1;
  1067. /* Disable AUTH by default. */
  1068. net->sctp.auth_enable = 0;
  1069. /* Set SCOPE policy to enabled */
  1070. net->sctp.scope_policy = SCTP_SCOPE_POLICY_ENABLE;
  1071. /* Set the default rwnd update threshold */
  1072. net->sctp.rwnd_upd_shift = SCTP_DEFAULT_RWND_SHIFT;
  1073. /* Initialize maximum autoclose timeout. */
  1074. net->sctp.max_autoclose = INT_MAX / HZ;
  1075. status = sctp_sysctl_net_register(net);
  1076. if (status)
  1077. goto err_sysctl_register;
  1078. /* Allocate and initialise sctp mibs. */
  1079. status = init_sctp_mibs(net);
  1080. if (status)
  1081. goto err_init_mibs;
  1082. /* Initialize proc fs directory. */
  1083. status = sctp_proc_init(net);
  1084. if (status)
  1085. goto err_init_proc;
  1086. sctp_dbg_objcnt_init(net);
  1087. /* Initialize the control inode/socket for handling OOTB packets. */
  1088. if ((status = sctp_ctl_sock_init(net))) {
  1089. pr_err("Failed to initialize the SCTP control sock\n");
  1090. goto err_ctl_sock_init;
  1091. }
  1092. /* Initialize the local address list. */
  1093. INIT_LIST_HEAD(&net->sctp.local_addr_list);
  1094. spin_lock_init(&net->sctp.local_addr_lock);
  1095. sctp_get_local_addr_list(net);
  1096. /* Initialize the address event list */
  1097. INIT_LIST_HEAD(&net->sctp.addr_waitq);
  1098. INIT_LIST_HEAD(&net->sctp.auto_asconf_splist);
  1099. spin_lock_init(&net->sctp.addr_wq_lock);
  1100. net->sctp.addr_wq_timer.expires = 0;
  1101. setup_timer(&net->sctp.addr_wq_timer, sctp_addr_wq_timeout_handler,
  1102. (unsigned long)net);
  1103. return 0;
  1104. err_ctl_sock_init:
  1105. sctp_dbg_objcnt_exit(net);
  1106. sctp_proc_exit(net);
  1107. err_init_proc:
  1108. cleanup_sctp_mibs(net);
  1109. err_init_mibs:
  1110. sctp_sysctl_net_unregister(net);
  1111. err_sysctl_register:
  1112. return status;
  1113. }
  1114. static void __net_exit sctp_net_exit(struct net *net)
  1115. {
  1116. /* Free the local address list */
  1117. sctp_free_addr_wq(net);
  1118. sctp_free_local_addr_list(net);
  1119. /* Free the control endpoint. */
  1120. inet_ctl_sock_destroy(net->sctp.ctl_sock);
  1121. sctp_dbg_objcnt_exit(net);
  1122. sctp_proc_exit(net);
  1123. cleanup_sctp_mibs(net);
  1124. sctp_sysctl_net_unregister(net);
  1125. }
  1126. static struct pernet_operations sctp_net_ops = {
  1127. .init = sctp_net_init,
  1128. .exit = sctp_net_exit,
  1129. };
  1130. /* Initialize the universe into something sensible. */
  1131. static __init int sctp_init(void)
  1132. {
  1133. int i;
  1134. int status = -EINVAL;
  1135. unsigned long goal;
  1136. unsigned long limit;
  1137. int max_share;
  1138. int order;
  1139. BUILD_BUG_ON(sizeof(struct sctp_ulpevent) >
  1140. sizeof(((struct sk_buff *) 0)->cb));
  1141. /* Allocate bind_bucket and chunk caches. */
  1142. status = -ENOBUFS;
  1143. sctp_bucket_cachep = kmem_cache_create("sctp_bind_bucket",
  1144. sizeof(struct sctp_bind_bucket),
  1145. 0, SLAB_HWCACHE_ALIGN,
  1146. NULL);
  1147. if (!sctp_bucket_cachep)
  1148. goto out;
  1149. sctp_chunk_cachep = kmem_cache_create("sctp_chunk",
  1150. sizeof(struct sctp_chunk),
  1151. 0, SLAB_HWCACHE_ALIGN,
  1152. NULL);
  1153. if (!sctp_chunk_cachep)
  1154. goto err_chunk_cachep;
  1155. status = percpu_counter_init(&sctp_sockets_allocated, 0);
  1156. if (status)
  1157. goto err_percpu_counter_init;
  1158. /* Implementation specific variables. */
  1159. /* Initialize default stream count setup information. */
  1160. sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
  1161. sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
  1162. /* Initialize handle used for association ids. */
  1163. idr_init(&sctp_assocs_id);
  1164. limit = nr_free_buffer_pages() / 8;
  1165. limit = max(limit, 128UL);
  1166. sysctl_sctp_mem[0] = limit / 4 * 3;
  1167. sysctl_sctp_mem[1] = limit;
  1168. sysctl_sctp_mem[2] = sysctl_sctp_mem[0] * 2;
  1169. /* Set per-socket limits to no more than 1/128 the pressure threshold*/
  1170. limit = (sysctl_sctp_mem[1]) << (PAGE_SHIFT - 7);
  1171. max_share = min(4UL*1024*1024, limit);
  1172. sysctl_sctp_rmem[0] = SK_MEM_QUANTUM; /* give each asoc 1 page min */
  1173. sysctl_sctp_rmem[1] = 1500 * SKB_TRUESIZE(1);
  1174. sysctl_sctp_rmem[2] = max(sysctl_sctp_rmem[1], max_share);
  1175. sysctl_sctp_wmem[0] = SK_MEM_QUANTUM;
  1176. sysctl_sctp_wmem[1] = 16*1024;
  1177. sysctl_sctp_wmem[2] = max(64*1024, max_share);
  1178. /* Size and allocate the association hash table.
  1179. * The methodology is similar to that of the tcp hash tables.
  1180. */
  1181. if (totalram_pages >= (128 * 1024))
  1182. goal = totalram_pages >> (22 - PAGE_SHIFT);
  1183. else
  1184. goal = totalram_pages >> (24 - PAGE_SHIFT);
  1185. for (order = 0; (1UL << order) < goal; order++)
  1186. ;
  1187. do {
  1188. sctp_assoc_hashsize = (1UL << order) * PAGE_SIZE /
  1189. sizeof(struct sctp_hashbucket);
  1190. if ((sctp_assoc_hashsize > (64 * 1024)) && order > 0)
  1191. continue;
  1192. sctp_assoc_hashtable = (struct sctp_hashbucket *)
  1193. __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, order);
  1194. } while (!sctp_assoc_hashtable && --order > 0);
  1195. if (!sctp_assoc_hashtable) {
  1196. pr_err("Failed association hash alloc\n");
  1197. status = -ENOMEM;
  1198. goto err_ahash_alloc;
  1199. }
  1200. for (i = 0; i < sctp_assoc_hashsize; i++) {
  1201. rwlock_init(&sctp_assoc_hashtable[i].lock);
  1202. INIT_HLIST_HEAD(&sctp_assoc_hashtable[i].chain);
  1203. }
  1204. /* Allocate and initialize the endpoint hash table. */
  1205. sctp_ep_hashsize = 64;
  1206. sctp_ep_hashtable =
  1207. kmalloc(64 * sizeof(struct sctp_hashbucket), GFP_KERNEL);
  1208. if (!sctp_ep_hashtable) {
  1209. pr_err("Failed endpoint_hash alloc\n");
  1210. status = -ENOMEM;
  1211. goto err_ehash_alloc;
  1212. }
  1213. for (i = 0; i < sctp_ep_hashsize; i++) {
  1214. rwlock_init(&sctp_ep_hashtable[i].lock);
  1215. INIT_HLIST_HEAD(&sctp_ep_hashtable[i].chain);
  1216. }
  1217. /* Allocate and initialize the SCTP port hash table. */
  1218. do {
  1219. sctp_port_hashsize = (1UL << order) * PAGE_SIZE /
  1220. sizeof(struct sctp_bind_hashbucket);
  1221. if ((sctp_port_hashsize > (64 * 1024)) && order > 0)
  1222. continue;
  1223. sctp_port_hashtable = (struct sctp_bind_hashbucket *)
  1224. __get_free_pages(GFP_ATOMIC|__GFP_NOWARN, order);
  1225. } while (!sctp_port_hashtable && --order > 0);
  1226. if (!sctp_port_hashtable) {
  1227. pr_err("Failed bind hash alloc\n");
  1228. status = -ENOMEM;
  1229. goto err_bhash_alloc;
  1230. }
  1231. for (i = 0; i < sctp_port_hashsize; i++) {
  1232. spin_lock_init(&sctp_port_hashtable[i].lock);
  1233. INIT_HLIST_HEAD(&sctp_port_hashtable[i].chain);
  1234. }
  1235. pr_info("Hash tables configured (established %d bind %d)\n",
  1236. sctp_assoc_hashsize, sctp_port_hashsize);
  1237. sctp_sysctl_register();
  1238. INIT_LIST_HEAD(&sctp_address_families);
  1239. sctp_v4_pf_init();
  1240. sctp_v6_pf_init();
  1241. status = sctp_v4_protosw_init();
  1242. if (status)
  1243. goto err_protosw_init;
  1244. status = sctp_v6_protosw_init();
  1245. if (status)
  1246. goto err_v6_protosw_init;
  1247. status = register_pernet_subsys(&sctp_net_ops);
  1248. if (status)
  1249. goto err_register_pernet_subsys;
  1250. status = sctp_v4_add_protocol();
  1251. if (status)
  1252. goto err_add_protocol;
  1253. /* Register SCTP with inet6 layer. */
  1254. status = sctp_v6_add_protocol();
  1255. if (status)
  1256. goto err_v6_add_protocol;
  1257. status = 0;
  1258. out:
  1259. return status;
  1260. err_v6_add_protocol:
  1261. sctp_v4_del_protocol();
  1262. err_add_protocol:
  1263. unregister_pernet_subsys(&sctp_net_ops);
  1264. err_register_pernet_subsys:
  1265. sctp_v6_protosw_exit();
  1266. err_v6_protosw_init:
  1267. sctp_v4_protosw_exit();
  1268. err_protosw_init:
  1269. sctp_v4_pf_exit();
  1270. sctp_v6_pf_exit();
  1271. sctp_sysctl_unregister();
  1272. free_pages((unsigned long)sctp_port_hashtable,
  1273. get_order(sctp_port_hashsize *
  1274. sizeof(struct sctp_bind_hashbucket)));
  1275. err_bhash_alloc:
  1276. kfree(sctp_ep_hashtable);
  1277. err_ehash_alloc:
  1278. free_pages((unsigned long)sctp_assoc_hashtable,
  1279. get_order(sctp_assoc_hashsize *
  1280. sizeof(struct sctp_hashbucket)));
  1281. err_ahash_alloc:
  1282. percpu_counter_destroy(&sctp_sockets_allocated);
  1283. err_percpu_counter_init:
  1284. kmem_cache_destroy(sctp_chunk_cachep);
  1285. err_chunk_cachep:
  1286. kmem_cache_destroy(sctp_bucket_cachep);
  1287. goto out;
  1288. }
  1289. /* Exit handler for the SCTP protocol. */
  1290. static __exit void sctp_exit(void)
  1291. {
  1292. /* BUG. This should probably do something useful like clean
  1293. * up all the remaining associations and all that memory.
  1294. */
  1295. /* Unregister with inet6/inet layers. */
  1296. sctp_v6_del_protocol();
  1297. sctp_v4_del_protocol();
  1298. unregister_pernet_subsys(&sctp_net_ops);
  1299. /* Free protosw registrations */
  1300. sctp_v6_protosw_exit();
  1301. sctp_v4_protosw_exit();
  1302. /* Unregister with socket layer. */
  1303. sctp_v6_pf_exit();
  1304. sctp_v4_pf_exit();
  1305. sctp_sysctl_unregister();
  1306. free_pages((unsigned long)sctp_assoc_hashtable,
  1307. get_order(sctp_assoc_hashsize *
  1308. sizeof(struct sctp_hashbucket)));
  1309. kfree(sctp_ep_hashtable);
  1310. free_pages((unsigned long)sctp_port_hashtable,
  1311. get_order(sctp_port_hashsize *
  1312. sizeof(struct sctp_bind_hashbucket)));
  1313. percpu_counter_destroy(&sctp_sockets_allocated);
  1314. rcu_barrier(); /* Wait for completion of call_rcu()'s */
  1315. kmem_cache_destroy(sctp_chunk_cachep);
  1316. kmem_cache_destroy(sctp_bucket_cachep);
  1317. }
  1318. module_init(sctp_init);
  1319. module_exit(sctp_exit);
  1320. /*
  1321. * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly.
  1322. */
  1323. MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132");
  1324. MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132");
  1325. MODULE_AUTHOR("Linux Kernel SCTP developers <linux-sctp@vger.kernel.org>");
  1326. MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
  1327. module_param_named(no_checksums, sctp_checksum_disable, bool, 0644);
  1328. MODULE_PARM_DESC(no_checksums, "Disable checksums computing and verification");
  1329. MODULE_LICENSE("GPL");