Vlad Yasevich | 60c778b | 2008-01-11 09:57:09 -0500 | [diff] [blame] | 1 | /* SCTP kernel implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (c) 2003 International Business Machines, Corp. |
| 3 | * |
Vlad Yasevich | 60c778b | 2008-01-11 09:57:09 -0500 | [diff] [blame] | 4 | * This file is part of the SCTP kernel implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * These functions manipulate sctp SSN tracker. |
| 7 | * |
Vlad Yasevich | 60c778b | 2008-01-11 09:57:09 -0500 | [diff] [blame] | 8 | * This SCTP implementation is free software; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
Vlad Yasevich | 60c778b | 2008-01-11 09:57:09 -0500 | [diff] [blame] | 14 | * This SCTP implementation is distributed in the hope that it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 39 | #include <linux/types.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 40 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <net/sctp/sctp.h> |
| 42 | #include <net/sctp/sm.h> |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | static struct sctp_ssnmap *sctp_ssnmap_init(struct sctp_ssnmap *map, __u16 in, |
| 45 | __u16 out); |
| 46 | |
| 47 | /* Storage size needed for map includes 2 headers and then the |
| 48 | * specific needs of in or out streams. |
| 49 | */ |
| 50 | static inline size_t sctp_ssnmap_size(__u16 in, __u16 out) |
| 51 | { |
| 52 | return sizeof(struct sctp_ssnmap) + (in + out) * sizeof(__u16); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /* Create a new sctp_ssnmap. |
| 57 | * Allocate room to store at least 'len' contiguous TSNs. |
| 58 | */ |
Alexey Dobriyan | 3182cd8 | 2005-07-11 20:57:47 -0700 | [diff] [blame] | 59 | struct sctp_ssnmap *sctp_ssnmap_new(__u16 in, __u16 out, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 60 | gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
| 62 | struct sctp_ssnmap *retval; |
| 63 | int size; |
| 64 | |
| 65 | size = sctp_ssnmap_size(in, out); |
Cong Wang | 3f73686 | 2013-03-03 16:28:27 +0000 | [diff] [blame] | 66 | if (size <= KMALLOC_MAX_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | retval = kmalloc(size, gfp); |
| 68 | else |
| 69 | retval = (struct sctp_ssnmap *) |
| 70 | __get_free_pages(gfp, get_order(size)); |
| 71 | if (!retval) |
| 72 | goto fail; |
| 73 | |
| 74 | if (!sctp_ssnmap_init(retval, in, out)) |
| 75 | goto fail_map; |
| 76 | |
| 77 | retval->malloced = 1; |
| 78 | SCTP_DBG_OBJCNT_INC(ssnmap); |
| 79 | |
| 80 | return retval; |
| 81 | |
| 82 | fail_map: |
Cong Wang | 3f73686 | 2013-03-03 16:28:27 +0000 | [diff] [blame] | 83 | if (size <= KMALLOC_MAX_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | kfree(retval); |
| 85 | else |
| 86 | free_pages((unsigned long)retval, get_order(size)); |
| 87 | fail: |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /* Initialize a block of memory as a ssnmap. */ |
| 93 | static struct sctp_ssnmap *sctp_ssnmap_init(struct sctp_ssnmap *map, __u16 in, |
| 94 | __u16 out) |
| 95 | { |
| 96 | memset(map, 0x00, sctp_ssnmap_size(in, out)); |
| 97 | |
| 98 | /* Start 'in' stream just after the map header. */ |
| 99 | map->in.ssn = (__u16 *)&map[1]; |
| 100 | map->in.len = in; |
| 101 | |
| 102 | /* Start 'out' stream just after 'in'. */ |
| 103 | map->out.ssn = &map->in.ssn[in]; |
| 104 | map->out.len = out; |
| 105 | |
| 106 | return map; |
| 107 | } |
| 108 | |
| 109 | /* Clear out the ssnmap streams. */ |
| 110 | void sctp_ssnmap_clear(struct sctp_ssnmap *map) |
| 111 | { |
| 112 | size_t size; |
| 113 | |
| 114 | size = (map->in.len + map->out.len) * sizeof(__u16); |
| 115 | memset(map->in.ssn, 0x00, size); |
| 116 | } |
| 117 | |
| 118 | /* Dispose of a ssnmap. */ |
| 119 | void sctp_ssnmap_free(struct sctp_ssnmap *map) |
| 120 | { |
| 121 | if (map && map->malloced) { |
| 122 | int size; |
| 123 | |
| 124 | size = sctp_ssnmap_size(map->in.len, map->out.len); |
Cong Wang | 3f73686 | 2013-03-03 16:28:27 +0000 | [diff] [blame] | 125 | if (size <= KMALLOC_MAX_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | kfree(map); |
| 127 | else |
| 128 | free_pages((unsigned long)map, get_order(size)); |
| 129 | SCTP_DBG_OBJCNT_DEC(ssnmap); |
| 130 | } |
| 131 | } |