ssnmap.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* SCTP kernel implementation
  2. * Copyright (c) 2003 International Business Machines, Corp.
  3. *
  4. * This file is part of the SCTP kernel implementation
  5. *
  6. * These functions manipulate sctp SSN tracker.
  7. *
  8. * This SCTP implementation is free software;
  9. * you can redistribute it and/or modify it under the terms of
  10. * the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2, or (at your option)
  12. * any later version.
  13. *
  14. * This SCTP implementation is distributed in the hope that it
  15. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  16. * ************************
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18. * See the GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GNU CC; see the file COPYING. If not, write to
  22. * the Free Software Foundation, 59 Temple Place - Suite 330,
  23. * Boston, MA 02111-1307, USA.
  24. *
  25. * Please send any bug reports or fixes you make to the
  26. * email address(es):
  27. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  28. *
  29. * Or submit a bug report through the following website:
  30. * http://www.sf.net/projects/lksctp
  31. *
  32. * Written or modified by:
  33. * Jon Grimm <jgrimm@us.ibm.com>
  34. *
  35. * Any bugs reported given to us we will try to fix... any fixes shared will
  36. * be incorporated into the next SCTP release.
  37. */
  38. #include <linux/types.h>
  39. #include <net/sctp/sctp.h>
  40. #include <net/sctp/sm.h>
  41. #define MAX_KMALLOC_SIZE 131072
  42. static struct sctp_ssnmap *sctp_ssnmap_init(struct sctp_ssnmap *map, __u16 in,
  43. __u16 out);
  44. /* Storage size needed for map includes 2 headers and then the
  45. * specific needs of in or out streams.
  46. */
  47. static inline size_t sctp_ssnmap_size(__u16 in, __u16 out)
  48. {
  49. return sizeof(struct sctp_ssnmap) + (in + out) * sizeof(__u16);
  50. }
  51. /* Create a new sctp_ssnmap.
  52. * Allocate room to store at least 'len' contiguous TSNs.
  53. */
  54. struct sctp_ssnmap *sctp_ssnmap_new(__u16 in, __u16 out,
  55. gfp_t gfp)
  56. {
  57. struct sctp_ssnmap *retval;
  58. int size;
  59. size = sctp_ssnmap_size(in, out);
  60. if (size <= MAX_KMALLOC_SIZE)
  61. retval = kmalloc(size, gfp);
  62. else
  63. retval = (struct sctp_ssnmap *)
  64. __get_free_pages(gfp, get_order(size));
  65. if (!retval)
  66. goto fail;
  67. if (!sctp_ssnmap_init(retval, in, out))
  68. goto fail_map;
  69. retval->malloced = 1;
  70. SCTP_DBG_OBJCNT_INC(ssnmap);
  71. return retval;
  72. fail_map:
  73. if (size <= MAX_KMALLOC_SIZE)
  74. kfree(retval);
  75. else
  76. free_pages((unsigned long)retval, get_order(size));
  77. fail:
  78. return NULL;
  79. }
  80. /* Initialize a block of memory as a ssnmap. */
  81. static struct sctp_ssnmap *sctp_ssnmap_init(struct sctp_ssnmap *map, __u16 in,
  82. __u16 out)
  83. {
  84. memset(map, 0x00, sctp_ssnmap_size(in, out));
  85. /* Start 'in' stream just after the map header. */
  86. map->in.ssn = (__u16 *)&map[1];
  87. map->in.len = in;
  88. /* Start 'out' stream just after 'in'. */
  89. map->out.ssn = &map->in.ssn[in];
  90. map->out.len = out;
  91. return map;
  92. }
  93. /* Clear out the ssnmap streams. */
  94. void sctp_ssnmap_clear(struct sctp_ssnmap *map)
  95. {
  96. size_t size;
  97. size = (map->in.len + map->out.len) * sizeof(__u16);
  98. memset(map->in.ssn, 0x00, size);
  99. }
  100. /* Dispose of a ssnmap. */
  101. void sctp_ssnmap_free(struct sctp_ssnmap *map)
  102. {
  103. if (map && map->malloced) {
  104. int size;
  105. size = sctp_ssnmap_size(map->in.len, map->out.len);
  106. if (size <= MAX_KMALLOC_SIZE)
  107. kfree(map);
  108. else
  109. free_pages((unsigned long)map, get_order(size));
  110. SCTP_DBG_OBJCNT_DEC(ssnmap);
  111. }
  112. }