protocol.c 43 KB

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