PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/net/sctp/bind_addr.c

http://github.com/leemgs/samsung-s3c6410-android.1.0
C | 465 lines | 272 code | 65 blank | 128 comment | 37 complexity | f4902477ca81023650c6a2d1400bb497 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0, CC-BY-SA-3.0
  1. /* SCTP kernel reference Implementation
  2. * (C) Copyright IBM Corp. 2001, 2003
  3. * Copyright (c) Cisco 1999,2000
  4. * Copyright (c) Motorola 1999,2000,2001
  5. * Copyright (c) La Monte H.P. Yarroll 2001
  6. *
  7. * This file is part of the SCTP kernel reference implementation.
  8. *
  9. * A collection class to handle the storage of transport addresses.
  10. *
  11. * The SCTP reference implementation is free software;
  12. * you can redistribute it and/or modify it under the terms of
  13. * the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * The SCTP reference implementation is distributed in the hope that it
  18. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  19. * ************************
  20. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21. * See the GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with GNU CC; see the file COPYING. If not, write to
  25. * the Free Software Foundation, 59 Temple Place - Suite 330,
  26. * Boston, MA 02111-1307, USA.
  27. *
  28. * Please send any bug reports or fixes you make to the
  29. * email address(es):
  30. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  31. *
  32. * Or submit a bug report through the following website:
  33. * http://www.sf.net/projects/lksctp
  34. *
  35. * Written or modified by:
  36. * La Monte H.P. Yarroll <piggy@acm.org>
  37. * Karl Knutson <karl@athena.chicago.il.us>
  38. * Jon Grimm <jgrimm@us.ibm.com>
  39. * Daisy Chang <daisyc@us.ibm.com>
  40. *
  41. * Any bugs reported given to us we will try to fix... any fixes shared will
  42. * be incorporated into the next SCTP release.
  43. */
  44. #include <linux/types.h>
  45. #include <linux/in.h>
  46. #include <net/sock.h>
  47. #include <net/ipv6.h>
  48. #include <net/if_inet6.h>
  49. #include <net/sctp/sctp.h>
  50. #include <net/sctp/sm.h>
  51. /* Forward declarations for internal helpers. */
  52. static int sctp_copy_one_addr(struct sctp_bind_addr *, union sctp_addr *,
  53. sctp_scope_t scope, gfp_t gfp,
  54. int flags);
  55. static void sctp_bind_addr_clean(struct sctp_bind_addr *);
  56. /* First Level Abstractions. */
  57. /* Copy 'src' to 'dest' taking 'scope' into account. Omit addresses
  58. * in 'src' which have a broader scope than 'scope'.
  59. */
  60. int sctp_bind_addr_copy(struct sctp_bind_addr *dest,
  61. const struct sctp_bind_addr *src,
  62. sctp_scope_t scope, gfp_t gfp,
  63. int flags)
  64. {
  65. struct sctp_sockaddr_entry *addr;
  66. struct list_head *pos;
  67. int error = 0;
  68. /* All addresses share the same port. */
  69. dest->port = src->port;
  70. /* Extract the addresses which are relevant for this scope. */
  71. list_for_each(pos, &src->address_list) {
  72. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  73. error = sctp_copy_one_addr(dest, &addr->a, scope,
  74. gfp, flags);
  75. if (error < 0)
  76. goto out;
  77. }
  78. /* If there are no addresses matching the scope and
  79. * this is global scope, try to get a link scope address, with
  80. * the assumption that we must be sitting behind a NAT.
  81. */
  82. if (list_empty(&dest->address_list) && (SCTP_SCOPE_GLOBAL == scope)) {
  83. list_for_each(pos, &src->address_list) {
  84. addr = list_entry(pos, struct sctp_sockaddr_entry,
  85. list);
  86. error = sctp_copy_one_addr(dest, &addr->a,
  87. SCTP_SCOPE_LINK, gfp,
  88. flags);
  89. if (error < 0)
  90. goto out;
  91. }
  92. }
  93. out:
  94. if (error)
  95. sctp_bind_addr_clean(dest);
  96. return error;
  97. }
  98. /* Exactly duplicate the address lists. This is necessary when doing
  99. * peer-offs and accepts. We don't want to put all the current system
  100. * addresses into the endpoint. That's useless. But we do want duplicat
  101. * the list of bound addresses that the older endpoint used.
  102. */
  103. int sctp_bind_addr_dup(struct sctp_bind_addr *dest,
  104. const struct sctp_bind_addr *src,
  105. gfp_t gfp)
  106. {
  107. struct sctp_sockaddr_entry *addr;
  108. struct list_head *pos;
  109. int error = 0;
  110. /* All addresses share the same port. */
  111. dest->port = src->port;
  112. list_for_each(pos, &src->address_list) {
  113. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  114. error = sctp_add_bind_addr(dest, &addr->a, 1, gfp);
  115. if (error < 0)
  116. break;
  117. }
  118. return error;
  119. }
  120. /* Initialize the SCTP_bind_addr structure for either an endpoint or
  121. * an association.
  122. */
  123. void sctp_bind_addr_init(struct sctp_bind_addr *bp, __u16 port)
  124. {
  125. bp->malloced = 0;
  126. INIT_LIST_HEAD(&bp->address_list);
  127. bp->port = port;
  128. }
  129. /* Dispose of the address list. */
  130. static void sctp_bind_addr_clean(struct sctp_bind_addr *bp)
  131. {
  132. struct sctp_sockaddr_entry *addr;
  133. struct list_head *pos, *temp;
  134. /* Empty the bind address list. */
  135. list_for_each_safe(pos, temp, &bp->address_list) {
  136. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  137. list_del(pos);
  138. kfree(addr);
  139. SCTP_DBG_OBJCNT_DEC(addr);
  140. }
  141. }
  142. /* Dispose of an SCTP_bind_addr structure */
  143. void sctp_bind_addr_free(struct sctp_bind_addr *bp)
  144. {
  145. /* Empty the bind address list. */
  146. sctp_bind_addr_clean(bp);
  147. if (bp->malloced) {
  148. kfree(bp);
  149. SCTP_DBG_OBJCNT_DEC(bind_addr);
  150. }
  151. }
  152. /* Add an address to the bind address list in the SCTP_bind_addr structure. */
  153. int sctp_add_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *new,
  154. __u8 use_as_src, gfp_t gfp)
  155. {
  156. struct sctp_sockaddr_entry *addr;
  157. /* Add the address to the bind address list. */
  158. addr = t_new(struct sctp_sockaddr_entry, gfp);
  159. if (!addr)
  160. return -ENOMEM;
  161. memcpy(&addr->a, new, sizeof(*new));
  162. /* Fix up the port if it has not yet been set.
  163. * Both v4 and v6 have the port at the same offset.
  164. */
  165. if (!addr->a.v4.sin_port)
  166. addr->a.v4.sin_port = htons(bp->port);
  167. addr->use_as_src = use_as_src;
  168. addr->valid = 1;
  169. INIT_LIST_HEAD(&addr->list);
  170. INIT_RCU_HEAD(&addr->rcu);
  171. /* We always hold a socket lock when calling this function,
  172. * and that acts as a writer synchronizing lock.
  173. */
  174. list_add_tail_rcu(&addr->list, &bp->address_list);
  175. SCTP_DBG_OBJCNT_INC(addr);
  176. return 0;
  177. }
  178. /* Delete an address from the bind address list in the SCTP_bind_addr
  179. * structure.
  180. */
  181. int sctp_del_bind_addr(struct sctp_bind_addr *bp, union sctp_addr *del_addr)
  182. {
  183. struct sctp_sockaddr_entry *addr, *temp;
  184. /* We hold the socket lock when calling this function,
  185. * and that acts as a writer synchronizing lock.
  186. */
  187. list_for_each_entry_safe(addr, temp, &bp->address_list, list) {
  188. if (sctp_cmp_addr_exact(&addr->a, del_addr)) {
  189. /* Found the exact match. */
  190. addr->valid = 0;
  191. list_del_rcu(&addr->list);
  192. break;
  193. }
  194. }
  195. if (addr && !addr->valid) {
  196. call_rcu(&addr->rcu, sctp_local_addr_free);
  197. SCTP_DBG_OBJCNT_DEC(addr);
  198. return 0;
  199. }
  200. return -EINVAL;
  201. }
  202. /* Create a network byte-order representation of all the addresses
  203. * formated as SCTP parameters.
  204. *
  205. * The second argument is the return value for the length.
  206. */
  207. union sctp_params sctp_bind_addrs_to_raw(const struct sctp_bind_addr *bp,
  208. int *addrs_len,
  209. gfp_t gfp)
  210. {
  211. union sctp_params addrparms;
  212. union sctp_params retval;
  213. int addrparms_len;
  214. union sctp_addr_param rawaddr;
  215. int len;
  216. struct sctp_sockaddr_entry *addr;
  217. struct list_head *pos;
  218. struct sctp_af *af;
  219. addrparms_len = 0;
  220. len = 0;
  221. /* Allocate enough memory at once. */
  222. list_for_each(pos, &bp->address_list) {
  223. len += sizeof(union sctp_addr_param);
  224. }
  225. /* Don't even bother embedding an address if there
  226. * is only one.
  227. */
  228. if (len == sizeof(union sctp_addr_param)) {
  229. retval.v = NULL;
  230. goto end_raw;
  231. }
  232. retval.v = kmalloc(len, gfp);
  233. if (!retval.v)
  234. goto end_raw;
  235. addrparms = retval;
  236. list_for_each(pos, &bp->address_list) {
  237. addr = list_entry(pos, struct sctp_sockaddr_entry, list);
  238. af = sctp_get_af_specific(addr->a.v4.sin_family);
  239. len = af->to_addr_param(&addr->a, &rawaddr);
  240. memcpy(addrparms.v, &rawaddr, len);
  241. addrparms.v += len;
  242. addrparms_len += len;
  243. }
  244. end_raw:
  245. *addrs_len = addrparms_len;
  246. return retval;
  247. }
  248. /*
  249. * Create an address list out of the raw address list format (IPv4 and IPv6
  250. * address parameters).
  251. */
  252. int sctp_raw_to_bind_addrs(struct sctp_bind_addr *bp, __u8 *raw_addr_list,
  253. int addrs_len, __u16 port, gfp_t gfp)
  254. {
  255. union sctp_addr_param *rawaddr;
  256. struct sctp_paramhdr *param;
  257. union sctp_addr addr;
  258. int retval = 0;
  259. int len;
  260. struct sctp_af *af;
  261. /* Convert the raw address to standard address format */
  262. while (addrs_len) {
  263. param = (struct sctp_paramhdr *)raw_addr_list;
  264. rawaddr = (union sctp_addr_param *)raw_addr_list;
  265. af = sctp_get_af_specific(param_type2af(param->type));
  266. if (unlikely(!af)) {
  267. retval = -EINVAL;
  268. sctp_bind_addr_clean(bp);
  269. break;
  270. }
  271. af->from_addr_param(&addr, rawaddr, htons(port), 0);
  272. retval = sctp_add_bind_addr(bp, &addr, 1, gfp);
  273. if (retval) {
  274. /* Can't finish building the list, clean up. */
  275. sctp_bind_addr_clean(bp);
  276. break;
  277. }
  278. len = ntohs(param->length);
  279. addrs_len -= len;
  280. raw_addr_list += len;
  281. }
  282. return retval;
  283. }
  284. /********************************************************************
  285. * 2nd Level Abstractions
  286. ********************************************************************/
  287. /* Does this contain a specified address? Allow wildcarding. */
  288. int sctp_bind_addr_match(struct sctp_bind_addr *bp,
  289. const union sctp_addr *addr,
  290. struct sctp_sock *opt)
  291. {
  292. struct sctp_sockaddr_entry *laddr;
  293. int match = 0;
  294. rcu_read_lock();
  295. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  296. if (!laddr->valid)
  297. continue;
  298. if (opt->pf->cmp_addr(&laddr->a, addr, opt)) {
  299. match = 1;
  300. break;
  301. }
  302. }
  303. rcu_read_unlock();
  304. return match;
  305. }
  306. /* Find the first address in the bind address list that is not present in
  307. * the addrs packed array.
  308. */
  309. union sctp_addr *sctp_find_unmatch_addr(struct sctp_bind_addr *bp,
  310. const union sctp_addr *addrs,
  311. int addrcnt,
  312. struct sctp_sock *opt)
  313. {
  314. struct sctp_sockaddr_entry *laddr;
  315. union sctp_addr *addr;
  316. void *addr_buf;
  317. struct sctp_af *af;
  318. int i;
  319. /* This is only called sctp_send_asconf_del_ip() and we hold
  320. * the socket lock in that code patch, so that address list
  321. * can't change.
  322. */
  323. list_for_each_entry(laddr, &bp->address_list, list) {
  324. addr_buf = (union sctp_addr *)addrs;
  325. for (i = 0; i < addrcnt; i++) {
  326. addr = (union sctp_addr *)addr_buf;
  327. af = sctp_get_af_specific(addr->v4.sin_family);
  328. if (!af)
  329. break;
  330. if (opt->pf->cmp_addr(&laddr->a, addr, opt))
  331. break;
  332. addr_buf += af->sockaddr_len;
  333. }
  334. if (i == addrcnt)
  335. return &laddr->a;
  336. }
  337. return NULL;
  338. }
  339. /* Copy out addresses from the global local address list. */
  340. static int sctp_copy_one_addr(struct sctp_bind_addr *dest,
  341. union sctp_addr *addr,
  342. sctp_scope_t scope, gfp_t gfp,
  343. int flags)
  344. {
  345. int error = 0;
  346. if (sctp_is_any(addr)) {
  347. error = sctp_copy_local_addr_list(dest, scope, gfp, flags);
  348. } else if (sctp_in_scope(addr, scope)) {
  349. /* Now that the address is in scope, check to see if
  350. * the address type is supported by local sock as
  351. * well as the remote peer.
  352. */
  353. if ((((AF_INET == addr->sa.sa_family) &&
  354. (flags & SCTP_ADDR4_PEERSUPP))) ||
  355. (((AF_INET6 == addr->sa.sa_family) &&
  356. (flags & SCTP_ADDR6_ALLOWED) &&
  357. (flags & SCTP_ADDR6_PEERSUPP))))
  358. error = sctp_add_bind_addr(dest, addr, 1, gfp);
  359. }
  360. return error;
  361. }
  362. /* Is this a wildcard address? */
  363. int sctp_is_any(const union sctp_addr *addr)
  364. {
  365. struct sctp_af *af = sctp_get_af_specific(addr->sa.sa_family);
  366. if (!af)
  367. return 0;
  368. return af->is_any(addr);
  369. }
  370. /* Is 'addr' valid for 'scope'? */
  371. int sctp_in_scope(const union sctp_addr *addr, sctp_scope_t scope)
  372. {
  373. sctp_scope_t addr_scope = sctp_scope(addr);
  374. /* The unusable SCTP addresses will not be considered with
  375. * any defined scopes.
  376. */
  377. if (SCTP_SCOPE_UNUSABLE == addr_scope)
  378. return 0;
  379. /*
  380. * For INIT and INIT-ACK address list, let L be the level of
  381. * of requested destination address, sender and receiver
  382. * SHOULD include all of its addresses with level greater
  383. * than or equal to L.
  384. */
  385. if (addr_scope <= scope)
  386. return 1;
  387. return 0;
  388. }
  389. /********************************************************************
  390. * 3rd Level Abstractions
  391. ********************************************************************/
  392. /* What is the scope of 'addr'? */
  393. sctp_scope_t sctp_scope(const union sctp_addr *addr)
  394. {
  395. struct sctp_af *af;
  396. af = sctp_get_af_specific(addr->sa.sa_family);
  397. if (!af)
  398. return SCTP_SCOPE_UNUSABLE;
  399. return af->scope((union sctp_addr *)addr);
  400. }