protocol.c 43 KB

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