protocol.c 43 KB

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