protocol.c 37 KB

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