protocol.c 41 KB

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