protocol.c 41 KB

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