ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / sctp / ssnmap.c
1 /* SCTP kernel reference Implementation
2  * Copyright (c) 2003 International Business Machines, Corp.
3  *
4  * This file is part of the SCTP kernel reference Implementation
5  *
6  * These functions manipulate sctp SSN tracker.
7  *
8  * The SCTP reference 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  * The SCTP reference 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
39 #include <linux/types.h>
40 #include <net/sctp/sctp.h>
41 #include <net/sctp/sm.h>
42
43 #define MAX_KMALLOC_SIZE        131072
44
45 /* Storage size needed for map includes 2 headers and then the
46  * specific needs of in or out streams.
47  */
48 static inline size_t sctp_ssnmap_size(__u16 in, __u16 out)
49 {
50         return sizeof(struct sctp_ssnmap) + (in + out) * sizeof(__u16);
51 }
52
53
54 /* Create a new sctp_ssnmap.
55  * Allocate room to store at least 'len' contiguous TSNs.
56  */
57 struct sctp_ssnmap *sctp_ssnmap_new(__u16 in, __u16 out, int gfp)
58 {
59         struct sctp_ssnmap *retval;
60         int size;
61
62         size = sctp_ssnmap_size(in, out);
63         if (size <= MAX_KMALLOC_SIZE)
64                 retval = kmalloc(size, gfp);
65         else
66                 retval = (struct sctp_ssnmap *)
67                           __get_free_pages(gfp, get_order(size));
68         if (!retval)
69                 goto fail;
70
71         if (!sctp_ssnmap_init(retval, in, out))
72                 goto fail_map;
73
74         retval->malloced = 1;
75         SCTP_DBG_OBJCNT_INC(ssnmap);
76
77         return retval;
78
79 fail_map:
80         if (size <= MAX_KMALLOC_SIZE)
81                 kfree(retval);
82         else
83                 free_pages((unsigned long)retval, get_order(size));
84 fail:
85         return NULL;
86 }
87
88
89 /* Initialize a block of memory as a ssnmap.  */
90 struct sctp_ssnmap *sctp_ssnmap_init(struct sctp_ssnmap *map, __u16 in,
91                                      __u16 out)
92 {
93         memset(map, 0x00, sctp_ssnmap_size(in, out));
94
95         /* Start 'in' stream just after the map header. */
96         map->in.ssn = (__u16 *)&map[1];
97         map->in.len = in;
98
99         /* Start 'out' stream just after 'in'. */
100         map->out.ssn = &map->in.ssn[in];
101         map->out.len = out;
102
103         return map;
104 }
105
106 /* Clear out the ssnmap streams.  */
107 void sctp_ssnmap_clear(struct sctp_ssnmap *map)
108 {
109         size_t size;
110
111         size = (map->in.len + map->out.len) * sizeof(__u16);
112         memset(map->in.ssn, 0x00, size);
113 }
114
115 /* Dispose of a ssnmap.  */
116 void sctp_ssnmap_free(struct sctp_ssnmap *map)
117 {
118         if (map && map->malloced) {
119                 int size;
120
121                 size = sctp_ssnmap_size(map->in.len, map->out.len);
122                 if (size <= MAX_KMALLOC_SIZE)
123                         kfree(map);
124                 else
125                         free_pages((unsigned long)map, get_order(size));
126                 SCTP_DBG_OBJCNT_DEC(ssnmap);
127         }
128 }