ssnmap.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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, see
  22. * <http://www.gnu.org/licenses/>.
  23. *
  24. * Please send any bug reports or fixes you make to the
  25. * email address(es):
  26. * lksctp developers <linux-sctp@vger.kernel.org>
  27. *
  28. * Written or modified by:
  29. * Jon Grimm <jgrimm@us.ibm.com>
  30. */
  31. #include <linux/types.h>
  32. #include <linux/slab.h>
  33. #include <net/sctp/sctp.h>
  34. #include <net/sctp/sm.h>
  35. static struct sctp_ssnmap *sctp_ssnmap_init(struct sctp_ssnmap *map, __u16 in,
  36. __u16 out);
  37. /* Storage size needed for map includes 2 headers and then the
  38. * specific needs of in or out streams.
  39. */
  40. static inline size_t sctp_ssnmap_size(__u16 in, __u16 out)
  41. {
  42. return sizeof(struct sctp_ssnmap) + (in + out) * sizeof(__u16);
  43. }
  44. /* Create a new sctp_ssnmap.
  45. * Allocate room to store at least 'len' contiguous TSNs.
  46. */
  47. struct sctp_ssnmap *sctp_ssnmap_new(__u16 in, __u16 out,
  48. gfp_t gfp)
  49. {
  50. struct sctp_ssnmap *retval;
  51. int size;
  52. size = sctp_ssnmap_size(in, out);
  53. if (size <= KMALLOC_MAX_SIZE)
  54. retval = kmalloc(size, gfp);
  55. else
  56. retval = (struct sctp_ssnmap *)
  57. __get_free_pages(gfp, get_order(size));
  58. if (!retval)
  59. goto fail;
  60. if (!sctp_ssnmap_init(retval, in, out))
  61. goto fail_map;
  62. SCTP_DBG_OBJCNT_INC(ssnmap);
  63. return retval;
  64. fail_map:
  65. if (size <= KMALLOC_MAX_SIZE)
  66. kfree(retval);
  67. else
  68. free_pages((unsigned long)retval, get_order(size));
  69. fail:
  70. return NULL;
  71. }
  72. /* Initialize a block of memory as a ssnmap. */
  73. static struct sctp_ssnmap *sctp_ssnmap_init(struct sctp_ssnmap *map, __u16 in,
  74. __u16 out)
  75. {
  76. memset(map, 0x00, sctp_ssnmap_size(in, out));
  77. /* Start 'in' stream just after the map header. */
  78. map->in.ssn = (__u16 *)&map[1];
  79. map->in.len = in;
  80. /* Start 'out' stream just after 'in'. */
  81. map->out.ssn = &map->in.ssn[in];
  82. map->out.len = out;
  83. return map;
  84. }
  85. /* Clear out the ssnmap streams. */
  86. void sctp_ssnmap_clear(struct sctp_ssnmap *map)
  87. {
  88. size_t size;
  89. size = (map->in.len + map->out.len) * sizeof(__u16);
  90. memset(map->in.ssn, 0x00, size);
  91. }
  92. /* Dispose of a ssnmap. */
  93. void sctp_ssnmap_free(struct sctp_ssnmap *map)
  94. {
  95. int size;
  96. if (unlikely(!map))
  97. return;
  98. size = sctp_ssnmap_size(map->in.len, map->out.len);
  99. if (size <= KMALLOC_MAX_SIZE)
  100. kfree(map);
  101. else
  102. free_pages((unsigned long)map, get_order(size));
  103. SCTP_DBG_OBJCNT_DEC(ssnmap);
  104. }