1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#include <linux/types.h>
33#include <linux/slab.h>
34#include <net/sctp/sctp.h>
35#include <net/sctp/sm.h>
36
37static struct sctp_ssnmap *sctp_ssnmap_init(struct sctp_ssnmap *map, __u16 in,
38 __u16 out);
39
40
41
42
43static inline size_t sctp_ssnmap_size(__u16 in, __u16 out)
44{
45 return sizeof(struct sctp_ssnmap) + (in + out) * sizeof(__u16);
46}
47
48
49
50
51
52struct sctp_ssnmap *sctp_ssnmap_new(__u16 in, __u16 out,
53 gfp_t gfp)
54{
55 struct sctp_ssnmap *retval;
56 int size;
57
58 size = sctp_ssnmap_size(in, out);
59 if (size <= KMALLOC_MAX_SIZE)
60 retval = kmalloc(size, gfp);
61 else
62 retval = (struct sctp_ssnmap *)
63 __get_free_pages(gfp, get_order(size));
64 if (!retval)
65 goto fail;
66
67 if (!sctp_ssnmap_init(retval, in, out))
68 goto fail_map;
69
70 SCTP_DBG_OBJCNT_INC(ssnmap);
71
72 return retval;
73
74fail_map:
75 if (size <= KMALLOC_MAX_SIZE)
76 kfree(retval);
77 else
78 free_pages((unsigned long)retval, get_order(size));
79fail:
80 return NULL;
81}
82
83
84
85static struct sctp_ssnmap *sctp_ssnmap_init(struct sctp_ssnmap *map, __u16 in,
86 __u16 out)
87{
88 memset(map, 0x00, sctp_ssnmap_size(in, out));
89
90
91 map->in.ssn = (__u16 *)&map[1];
92 map->in.len = in;
93
94
95 map->out.ssn = &map->in.ssn[in];
96 map->out.len = out;
97
98 return map;
99}
100
101
102void sctp_ssnmap_clear(struct sctp_ssnmap *map)
103{
104 size_t size;
105
106 size = (map->in.len + map->out.len) * sizeof(__u16);
107 memset(map->in.ssn, 0x00, size);
108}
109
110
111void sctp_ssnmap_free(struct sctp_ssnmap *map)
112{
113 int size;
114
115 if (unlikely(!map))
116 return;
117
118 size = sctp_ssnmap_size(map->in.len, map->out.len);
119 if (size <= KMALLOC_MAX_SIZE)
120 kfree(map);
121 else
122 free_pages((unsigned long)map, get_order(size));
123
124 SCTP_DBG_OBJCNT_DEC(ssnmap);
125}
126