Whamcloud - gitweb
14004147063ffeaa09fc8128a012f54323c4aa9f
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_rawobj.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/ptlrpc/gss/gss_rawobj.c
32  *
33  * Author: Eric Mei <ericm@clusterfs.com>
34  */
35
36 #define DEBUG_SUBSYSTEM S_SEC
37
38 #include <linux/mutex.h>
39
40 #include <obd.h>
41 #include <obd_class.h>
42 #include <obd_support.h>
43 #include <lustre_sec.h>
44
45 #include "gss_internal.h"
46
47 int rawobj_empty(rawobj_t *obj)
48 {
49         LASSERT(equi(obj->len, obj->data));
50         return (obj->len == 0);
51 }
52
53 int rawobj_alloc(rawobj_t *obj, char *buf, int len)
54 {
55         LASSERT(obj);
56         LASSERT(len >= 0);
57
58         obj->len = len;
59         if (len) {
60                 OBD_ALLOC_LARGE(obj->data, len);
61                 if (!obj->data) {
62                         obj->len = 0;
63                         RETURN(-ENOMEM);
64                 }
65                 memcpy(obj->data, buf, len);
66         } else
67                 obj->data = NULL;
68         return 0;
69 }
70
71 void rawobj_free(rawobj_t *obj)
72 {
73         LASSERT(obj);
74
75         if (obj->len) {
76                 LASSERT(obj->data);
77                 OBD_FREE_LARGE(obj->data, obj->len);
78                 obj->len = 0;
79                 obj->data = NULL;
80         } else
81                 LASSERT(!obj->data);
82 }
83
84 int rawobj_equal(rawobj_t *a, rawobj_t *b)
85 {
86         LASSERT(a && b);
87
88         return (a->len == b->len &&
89                 (!a->len || !memcmp(a->data, b->data, a->len)));
90 }
91
92 int rawobj_dup(rawobj_t *dest, rawobj_t *src)
93 {
94         LASSERT(src && dest);
95
96         dest->len = src->len;
97         if (dest->len) {
98                 OBD_ALLOC_LARGE(dest->data, dest->len);
99                 if (!dest->data) {
100                         dest->len = 0;
101                         return -ENOMEM;
102                 }
103                 memcpy(dest->data, src->data, dest->len);
104         } else
105                 dest->data = NULL;
106         return 0;
107 }
108
109 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen)
110 {
111         __u32 len;
112
113         LASSERT(obj);
114         LASSERT(buf);
115         LASSERT(buflen);
116
117         len = cfs_size_round4(obj->len);
118
119         if (*buflen < 4 + len) {
120                 CERROR("shorter buflen than needed: %u < %u\n",
121                         *buflen, 4 + len);
122                 return -EINVAL;
123         }
124
125         *(*buf)++ = cpu_to_le32(obj->len);
126         memcpy(*buf, obj->data, obj->len);
127         *buf += (len >> 2);
128         *buflen -= (4 + len);
129
130         return 0;
131 }
132
133 static int __rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen,
134                             int alloc, int local)
135 {
136         __u32 len;
137
138         if (*buflen < sizeof(__u32)) {
139                 CERROR("too short buflen: %u\n", *buflen);
140                 return -EINVAL;
141         }
142
143         obj->len = *(*buf)++;
144         if (!local)
145                 obj->len = le32_to_cpu(obj->len);
146         *buflen -= sizeof(__u32);
147
148         if (!obj->len) {
149                 obj->data = NULL;
150                 return 0;
151         }
152
153         len = local ? obj->len : cfs_size_round4(obj->len);
154         if (*buflen < len) {
155                 CERROR("shorter buflen than object size: %u < %u\n",
156                         *buflen, len);
157                 obj->len = 0;
158                 return -EINVAL;
159         }
160
161         if (!alloc)
162                 obj->data = (__u8 *) *buf;
163         else {
164                 OBD_ALLOC_LARGE(obj->data, obj->len);
165                 if (!obj->data) {
166                         CERROR("fail to alloc %u bytes\n", obj->len);
167                         obj->len = 0;
168                         return -ENOMEM;
169                 }
170                 memcpy(obj->data, *buf, obj->len);
171         }
172
173         *((char **)buf) += len;
174         *buflen -= len;
175
176         return 0;
177 }
178
179 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen)
180 {
181         return __rawobj_extract(obj, buf, buflen, 0, 0);
182 }
183
184 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
185 {
186         return __rawobj_extract(obj, buf, buflen, 1, 0);
187 }
188
189 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen)
190 {
191         return __rawobj_extract(obj, buf, buflen, 0, 1);
192 }
193
194 int rawobj_extract_local_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
195 {
196         return __rawobj_extract(obj, buf, buflen, 1, 1);
197 }
198
199 int rawobj_from_netobj(rawobj_t *rawobj, netobj_t *netobj)
200 {
201         rawobj->len = netobj->len;
202         rawobj->data = netobj->data;
203         return 0;
204 }
205
206 int rawobj_from_netobj_alloc(rawobj_t *rawobj, netobj_t *netobj)
207 {
208         rawobj->len = 0;
209         rawobj->data = NULL;
210
211         if (netobj->len == 0)
212                 return 0;
213
214         OBD_ALLOC_LARGE(rawobj->data, netobj->len);
215         if (rawobj->data == NULL)
216                 return -ENOMEM;
217
218         rawobj->len = netobj->len;
219         memcpy(rawobj->data, netobj->data, netobj->len);
220         return 0;
221 }
222
223 /****************************************
224  * misc more                            *
225  ****************************************/
226
227 int buffer_extract_bytes(const void **buf, __u32 *buflen,
228                          void *res, __u32 reslen)
229 {
230         if (*buflen < reslen) {
231                 CERROR("shorter buflen than expected: %u < %u\n",
232                         *buflen, reslen);
233                 return -EINVAL;
234         }
235
236         memcpy(res, *buf, reslen);
237         *buf += reslen;
238         *buflen -= reslen;
239         return 0;
240 }