blob: 6007124aefa018fd958b9481e012f3db2df5a5da [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):
Daniel Borkmann91705c62013-07-23 14:51:47 +020027 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * Written or modified by:
30 * Jon Grimm <jgrimm@us.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
32
33#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <net/sctp/sctp.h>
36#include <net/sctp/sm.h>
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static struct sctp_ssnmap *sctp_ssnmap_init(struct sctp_ssnmap *map, __u16 in,
39 __u16 out);
40
41/* Storage size needed for map includes 2 headers and then the
42 * specific needs of in or out streams.
43 */
44static inline size_t sctp_ssnmap_size(__u16 in, __u16 out)
45{
46 return sizeof(struct sctp_ssnmap) + (in + out) * sizeof(__u16);
47}
48
49
50/* Create a new sctp_ssnmap.
51 * Allocate room to store at least 'len' contiguous TSNs.
52 */
Alexey Dobriyan3182cd82005-07-11 20:57:47 -070053struct sctp_ssnmap *sctp_ssnmap_new(__u16 in, __u16 out,
Al Virodd0fc662005-10-07 07:46:04 +010054 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 struct sctp_ssnmap *retval;
57 int size;
58
59 size = sctp_ssnmap_size(in, out);
Cong Wang3f736862013-03-03 16:28:27 +000060 if (size <= KMALLOC_MAX_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 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
68 if (!sctp_ssnmap_init(retval, in, out))
69 goto fail_map;
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 SCTP_DBG_OBJCNT_INC(ssnmap);
72
73 return retval;
74
75fail_map:
Cong Wang3f736862013-03-03 16:28:27 +000076 if (size <= KMALLOC_MAX_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 kfree(retval);
78 else
79 free_pages((unsigned long)retval, get_order(size));
80fail:
81 return NULL;
82}
83
84
85/* Initialize a block of memory as a ssnmap. */
86static struct sctp_ssnmap *sctp_ssnmap_init(struct sctp_ssnmap *map, __u16 in,
87 __u16 out)
88{
89 memset(map, 0x00, sctp_ssnmap_size(in, out));
90
91 /* Start 'in' stream just after the map header. */
92 map->in.ssn = (__u16 *)&map[1];
93 map->in.len = in;
94
95 /* Start 'out' stream just after 'in'. */
96 map->out.ssn = &map->in.ssn[in];
97 map->out.len = out;
98
99 return map;
100}
101
102/* Clear out the ssnmap streams. */
103void sctp_ssnmap_clear(struct sctp_ssnmap *map)
104{
105 size_t size;
106
107 size = (map->in.len + map->out.len) * sizeof(__u16);
108 memset(map->in.ssn, 0x00, size);
109}
110
111/* Dispose of a ssnmap. */
112void sctp_ssnmap_free(struct sctp_ssnmap *map)
113{
Daniel Borkmann542c2d832013-04-16 11:07:10 +0000114 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Daniel Borkmann542c2d832013-04-16 11:07:10 +0000116 if (unlikely(!map))
117 return;
118
119 size = sctp_ssnmap_size(map->in.len, map->out.len);
120 if (size <= KMALLOC_MAX_SIZE)
121 kfree(map);
122 else
123 free_pages((unsigned long)map, get_order(size));
124
125 SCTP_DBG_OBJCNT_DEC(ssnmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}