blob: 442ad4ed6315fab99ec5b78d012e65a8236e2fae [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (c) 2003 International Business Machines, Corp.
3 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05004 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * These functions manipulate sctp SSN tracker.
7 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05008 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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 Yasevich60c778b2008-01-11 09:57:09 -050014 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * 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 Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <net/sctp/sctp.h>
42#include <net/sctp/sm.h>
43
44#define MAX_KMALLOC_SIZE 131072
45
46static struct sctp_ssnmap *sctp_ssnmap_init(struct sctp_ssnmap *map, __u16 in,
47 __u16 out);
48
49/* Storage size needed for map includes 2 headers and then the
50 * specific needs of in or out streams.
51 */
52static inline size_t sctp_ssnmap_size(__u16 in, __u16 out)
53{
54 return sizeof(struct sctp_ssnmap) + (in + out) * sizeof(__u16);
55}
56
57
58/* Create a new sctp_ssnmap.
59 * Allocate room to store at least 'len' contiguous TSNs.
60 */
Alexey Dobriyan3182cd842005-07-11 20:57:47 -070061struct sctp_ssnmap *sctp_ssnmap_new(__u16 in, __u16 out,
Al Virodd0fc662005-10-07 07:46:04 +010062 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 struct sctp_ssnmap *retval;
65 int size;
66
67 size = sctp_ssnmap_size(in, out);
68 if (size <= MAX_KMALLOC_SIZE)
69 retval = kmalloc(size, gfp);
70 else
71 retval = (struct sctp_ssnmap *)
72 __get_free_pages(gfp, get_order(size));
73 if (!retval)
74 goto fail;
75
76 if (!sctp_ssnmap_init(retval, in, out))
77 goto fail_map;
78
79 retval->malloced = 1;
80 SCTP_DBG_OBJCNT_INC(ssnmap);
81
82 return retval;
83
84fail_map:
85 if (size <= MAX_KMALLOC_SIZE)
86 kfree(retval);
87 else
88 free_pages((unsigned long)retval, get_order(size));
89fail:
90 return NULL;
91}
92
93
94/* Initialize a block of memory as a ssnmap. */
95static struct sctp_ssnmap *sctp_ssnmap_init(struct sctp_ssnmap *map, __u16 in,
96 __u16 out)
97{
98 memset(map, 0x00, sctp_ssnmap_size(in, out));
99
100 /* Start 'in' stream just after the map header. */
101 map->in.ssn = (__u16 *)&map[1];
102 map->in.len = in;
103
104 /* Start 'out' stream just after 'in'. */
105 map->out.ssn = &map->in.ssn[in];
106 map->out.len = out;
107
108 return map;
109}
110
111/* Clear out the ssnmap streams. */
112void sctp_ssnmap_clear(struct sctp_ssnmap *map)
113{
114 size_t size;
115
116 size = (map->in.len + map->out.len) * sizeof(__u16);
117 memset(map->in.ssn, 0x00, size);
118}
119
120/* Dispose of a ssnmap. */
121void sctp_ssnmap_free(struct sctp_ssnmap *map)
122{
123 if (map && map->malloced) {
124 int size;
125
126 size = sctp_ssnmap_size(map->in.len, map->out.len);
127 if (size <= MAX_KMALLOC_SIZE)
128 kfree(map);
129 else
130 free_pages((unsigned long)map, get_order(size));
131 SCTP_DBG_OBJCNT_DEC(ssnmap);
132 }
133}