protocol.c 35 KB

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