Merge branch 'for-3.11' of git://linux-nfs.org/~bfields/linux
[linux-2.6.git] / net / sctp / ssnmap.c
blobda860352380872ad1964af228906178bf8b56531
1 /* SCTP kernel implementation
2 * Copyright (c) 2003 International Business Machines, Corp.
4 * This file is part of the SCTP kernel implementation
6 * These functions manipulate sctp SSN tracker.
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.
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.
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.
25 * Please send any bug reports or fixes you make to the
26 * email address(es):
27 * lksctp developers <lksctp-developers@lists.sourceforge.net>
29 * Or submit a bug report through the following website:
30 * http://www.sf.net/projects/lksctp
32 * Written or modified by:
33 * Jon Grimm <jgrimm@us.ibm.com>
35 * Any bugs reported given to us we will try to fix... any fixes shared will
36 * be incorporated into the next SCTP release.
39 #include <linux/types.h>
40 #include <linux/slab.h>
41 #include <net/sctp/sctp.h>
42 #include <net/sctp/sm.h>
44 static struct sctp_ssnmap *sctp_ssnmap_init(struct sctp_ssnmap *map, __u16 in,
45 __u16 out);
47 /* Storage size needed for map includes 2 headers and then the
48 * specific needs of in or out streams.
50 static inline size_t sctp_ssnmap_size(__u16 in, __u16 out)
52 return sizeof(struct sctp_ssnmap) + (in + out) * sizeof(__u16);
56 /* Create a new sctp_ssnmap.
57 * Allocate room to store at least 'len' contiguous TSNs.
59 struct sctp_ssnmap *sctp_ssnmap_new(__u16 in, __u16 out,
60 gfp_t gfp)
62 struct sctp_ssnmap *retval;
63 int size;
65 size = sctp_ssnmap_size(in, out);
66 if (size <= KMALLOC_MAX_SIZE)
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;
74 if (!sctp_ssnmap_init(retval, in, out))
75 goto fail_map;
77 SCTP_DBG_OBJCNT_INC(ssnmap);
79 return retval;
81 fail_map:
82 if (size <= KMALLOC_MAX_SIZE)
83 kfree(retval);
84 else
85 free_pages((unsigned long)retval, get_order(size));
86 fail:
87 return NULL;
91 /* Initialize a block of memory as a ssnmap. */
92 static struct sctp_ssnmap *sctp_ssnmap_init(struct sctp_ssnmap *map, __u16 in,
93 __u16 out)
95 memset(map, 0x00, sctp_ssnmap_size(in, out));
97 /* Start 'in' stream just after the map header. */
98 map->in.ssn = (__u16 *)&map[1];
99 map->in.len = in;
101 /* Start 'out' stream just after 'in'. */
102 map->out.ssn = &map->in.ssn[in];
103 map->out.len = out;
105 return map;
108 /* Clear out the ssnmap streams. */
109 void sctp_ssnmap_clear(struct sctp_ssnmap *map)
111 size_t size;
113 size = (map->in.len + map->out.len) * sizeof(__u16);
114 memset(map->in.ssn, 0x00, size);
117 /* Dispose of a ssnmap. */
118 void sctp_ssnmap_free(struct sctp_ssnmap *map)
120 int size;
122 if (unlikely(!map))
123 return;
125 size = sctp_ssnmap_size(map->in.len, map->out.len);
126 if (size <= KMALLOC_MAX_SIZE)
127 kfree(map);
128 else
129 free_pages((unsigned long)map, get_order(size));
131 SCTP_DBG_OBJCNT_DEC(ssnmap);