protocol.c 37 KB

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