blob: 8b378f91f255f34a14b51b73407d9a022cbc0591 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/types.h>
2#include <linux/sched.h>
3#include <linux/module.h>
4#include <linux/sunrpc/types.h>
5#include <linux/sunrpc/xdr.h>
6#include <linux/sunrpc/svcsock.h>
7#include <linux/sunrpc/svcauth.h>
Andy Adamsonc4170583f2007-07-17 04:04:42 -07008#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/err.h>
10#include <linux/seq_file.h>
11#include <linux/hash.h>
Paulo Marques543537b2005-06-23 00:09:02 -070012#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Greg Banks7b2b1fe2006-10-04 02:15:50 -070014#include <net/sock.h>
Aurélien Charbonf15364b2008-01-18 15:50:56 +010015#include <net/ipv6.h>
16#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#define RPCDBG_FACILITY RPCDBG_AUTH
18
Chuck Lever07396052010-01-26 14:03:47 -050019#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Pavel Emelyanov90d51b02010-09-27 14:02:29 +040021#include "netns.h"
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023/*
24 * AUTHUNIX and AUTHNULL credentials are both handled here.
25 * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
26 * are always nobody (-2). i.e. we do the same IP address checks for
27 * AUTHNULL as for AUTHUNIX, and that is done here.
28 */
29
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031struct unix_domain {
32 struct auth_domain h;
33 int addr_changes;
34 /* other stuff later */
35};
36
NeilBrownefc36aa2006-03-27 01:14:59 -080037extern struct auth_ops svcauth_unix;
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039struct auth_domain *unix_domain_find(char *name)
40{
NeilBrownefc36aa2006-03-27 01:14:59 -080041 struct auth_domain *rv;
42 struct unix_domain *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
NeilBrownefc36aa2006-03-27 01:14:59 -080044 rv = auth_domain_lookup(name, NULL);
45 while(1) {
NeilBrownad1b5222006-03-27 01:15:11 -080046 if (rv) {
47 if (new && rv != &new->h)
48 auth_domain_put(&new->h);
49
50 if (rv->flavour != &svcauth_unix) {
51 auth_domain_put(rv);
52 return NULL;
53 }
NeilBrownefc36aa2006-03-27 01:14:59 -080054 return rv;
55 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
NeilBrownefc36aa2006-03-27 01:14:59 -080057 new = kmalloc(sizeof(*new), GFP_KERNEL);
58 if (new == NULL)
59 return NULL;
60 kref_init(&new->h.ref);
61 new->h.name = kstrdup(name, GFP_KERNEL);
NeilBrowndd08d6e2006-12-13 00:35:44 -080062 if (new->h.name == NULL) {
63 kfree(new);
64 return NULL;
65 }
NeilBrownefc36aa2006-03-27 01:14:59 -080066 new->h.flavour = &svcauth_unix;
67 new->addr_changes = 0;
68 rv = auth_domain_lookup(name, &new->h);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
Trond Myklebust24c37672008-12-23 16:30:12 -050071EXPORT_SYMBOL_GPL(unix_domain_find);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73static void svcauth_unix_domain_release(struct auth_domain *dom)
74{
75 struct unix_domain *ud = container_of(dom, struct unix_domain, h);
76
77 kfree(dom->name);
78 kfree(ud);
79}
80
81
82/**************************************************
83 * cache for IP address to unix_domain
84 * as needed by AUTH_UNIX
85 */
86#define IP_HASHBITS 8
87#define IP_HASHMAX (1<<IP_HASHBITS)
88#define IP_HASHMASK (IP_HASHMAX-1)
89
90struct ip_map {
91 struct cache_head h;
92 char m_class[8]; /* e.g. "nfsd" */
Aurélien Charbonf15364b2008-01-18 15:50:56 +010093 struct in6_addr m_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct unix_domain *m_client;
95 int m_add_change;
96};
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
NeilBrownbaab9352006-03-27 01:15:09 -080098static void ip_map_put(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
NeilBrownbaab9352006-03-27 01:15:09 -0800100 struct cache_head *item = container_of(kref, struct cache_head, ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct ip_map *im = container_of(item, struct ip_map,h);
NeilBrownbaab9352006-03-27 01:15:09 -0800102
103 if (test_bit(CACHE_VALID, &item->flags) &&
104 !test_bit(CACHE_NEGATIVE, &item->flags))
105 auth_domain_put(&im->m_client->h);
106 kfree(im);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
NeilBrown1f1e0302006-01-06 00:09:49 -0800109#if IP_HASHBITS == 8
110/* hash_long on a 64 bit machine is currently REALLY BAD for
111 * IP addresses in reverse-endian (i.e. on a little-endian machine).
112 * So use a trivial but reliable hash instead
113 */
Al Viro48061262006-11-08 00:22:34 -0800114static inline int hash_ip(__be32 ip)
NeilBrown1f1e0302006-01-06 00:09:49 -0800115{
Al Viro48061262006-11-08 00:22:34 -0800116 int hash = (__force u32)ip ^ ((__force u32)ip>>16);
NeilBrown1f1e0302006-01-06 00:09:49 -0800117 return (hash ^ (hash>>8)) & 0xff;
118}
119#endif
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100120static inline int hash_ip6(struct in6_addr ip)
121{
122 return (hash_ip(ip.s6_addr32[0]) ^
123 hash_ip(ip.s6_addr32[1]) ^
124 hash_ip(ip.s6_addr32[2]) ^
125 hash_ip(ip.s6_addr32[3]));
126}
NeilBrown1a9917c2006-03-27 01:15:02 -0800127static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
128{
129 struct ip_map *orig = container_of(corig, struct ip_map, h);
130 struct ip_map *new = container_of(cnew, struct ip_map, h);
Joe Perchesf64f9e72009-11-29 16:55:45 -0800131 return strcmp(orig->m_class, new->m_class) == 0 &&
132 ipv6_addr_equal(&orig->m_addr, &new->m_addr);
NeilBrown1a9917c2006-03-27 01:15:02 -0800133}
134static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
135{
136 struct ip_map *new = container_of(cnew, struct ip_map, h);
137 struct ip_map *item = container_of(citem, struct ip_map, h);
NeilBrown1f1e0302006-01-06 00:09:49 -0800138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 strcpy(new->m_class, item->m_class);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100140 ipv6_addr_copy(&new->m_addr, &item->m_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
NeilBrown1a9917c2006-03-27 01:15:02 -0800142static void update(struct cache_head *cnew, struct cache_head *citem)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
NeilBrown1a9917c2006-03-27 01:15:02 -0800144 struct ip_map *new = container_of(cnew, struct ip_map, h);
145 struct ip_map *item = container_of(citem, struct ip_map, h);
146
NeilBrownefc36aa2006-03-27 01:14:59 -0800147 kref_get(&item->m_client->h.ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 new->m_client = item->m_client;
149 new->m_add_change = item->m_add_change;
150}
NeilBrown1a9917c2006-03-27 01:15:02 -0800151static struct cache_head *ip_map_alloc(void)
152{
153 struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
154 if (i)
155 return &i->h;
156 else
157 return NULL;
158}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160static void ip_map_request(struct cache_detail *cd,
161 struct cache_head *h,
162 char **bpp, int *blen)
163{
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100164 char text_addr[40];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 struct ip_map *im = container_of(h, struct ip_map, h);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800166
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100167 if (ipv6_addr_v4mapped(&(im->m_addr))) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700168 snprintf(text_addr, 20, "%pI4", &im->m_addr.s6_addr32[3]);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100169 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700170 snprintf(text_addr, 40, "%pI6", &im->m_addr);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 qword_add(bpp, blen, im->m_class);
173 qword_add(bpp, blen, text_addr);
174 (*bpp)[-1] = '\n';
175}
176
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400177static int ip_map_upcall(struct cache_detail *cd, struct cache_head *h)
178{
179 return sunrpc_cache_pipe_upcall(cd, h, ip_map_request);
180}
181
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400182static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class, struct in6_addr *addr);
183static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, struct unix_domain *udom, time_t expiry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185static int ip_map_parse(struct cache_detail *cd,
186 char *mesg, int mlen)
187{
188 /* class ipaddress [domainname] */
189 /* should be safe just to use the start of the input buffer
190 * for scratch: */
191 char *buf = mesg;
192 int len;
NeilBrown1a9917c2006-03-27 01:15:02 -0800193 char class[8];
Chuck Lever07396052010-01-26 14:03:47 -0500194 union {
195 struct sockaddr sa;
196 struct sockaddr_in s4;
197 struct sockaddr_in6 s6;
198 } address;
199 struct sockaddr_in6 sin6;
NeilBrown1a9917c2006-03-27 01:15:02 -0800200 int err;
201
202 struct ip_map *ipmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct auth_domain *dom;
204 time_t expiry;
205
206 if (mesg[mlen-1] != '\n')
207 return -EINVAL;
208 mesg[mlen-1] = 0;
209
210 /* class */
NeilBrown1a9917c2006-03-27 01:15:02 -0800211 len = qword_get(&mesg, class, sizeof(class));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (len <= 0) return -EINVAL;
213
214 /* ip address */
215 len = qword_get(&mesg, buf, mlen);
216 if (len <= 0) return -EINVAL;
217
Chuck Lever07396052010-01-26 14:03:47 -0500218 if (rpc_pton(buf, len, &address.sa, sizeof(address)) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return -EINVAL;
Chuck Lever07396052010-01-26 14:03:47 -0500220 switch (address.sa.sa_family) {
221 case AF_INET:
222 /* Form a mapped IPv4 address in sin6 */
223 memset(&sin6, 0, sizeof(sin6));
224 sin6.sin6_family = AF_INET6;
225 sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
226 sin6.sin6_addr.s6_addr32[3] = address.s4.sin_addr.s_addr;
227 break;
228#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
229 case AF_INET6:
230 memcpy(&sin6, &address.s6, sizeof(sin6));
231 break;
232#endif
233 default:
234 return -EINVAL;
235 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 expiry = get_expiry(&mesg);
238 if (expiry ==0)
239 return -EINVAL;
240
241 /* domainname, or empty for NEGATIVE */
242 len = qword_get(&mesg, buf, mlen);
243 if (len < 0) return -EINVAL;
244
245 if (len) {
246 dom = unix_domain_find(buf);
247 if (dom == NULL)
248 return -ENOENT;
249 } else
250 dom = NULL;
251
Chuck Lever07396052010-01-26 14:03:47 -0500252 /* IPv6 scope IDs are ignored for now */
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400253 ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr);
NeilBrown1a9917c2006-03-27 01:15:02 -0800254 if (ipmp) {
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400255 err = __ip_map_update(cd, ipmp,
NeilBrown1a9917c2006-03-27 01:15:02 -0800256 container_of(dom, struct unix_domain, h),
257 expiry);
258 } else
259 err = -ENOMEM;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (dom)
262 auth_domain_put(dom);
NeilBrown1a9917c2006-03-27 01:15:02 -0800263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 cache_flush();
NeilBrown1a9917c2006-03-27 01:15:02 -0800265 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268static int ip_map_show(struct seq_file *m,
269 struct cache_detail *cd,
270 struct cache_head *h)
271{
272 struct ip_map *im;
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100273 struct in6_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 char *dom = "-no-domain-";
275
276 if (h == NULL) {
277 seq_puts(m, "#class IP domain\n");
278 return 0;
279 }
280 im = container_of(h, struct ip_map, h);
281 /* class addr domain */
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100282 ipv6_addr_copy(&addr, &im->m_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800284 if (test_bit(CACHE_VALID, &h->flags) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 !test_bit(CACHE_NEGATIVE, &h->flags))
286 dom = im->m_client->h.name;
287
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100288 if (ipv6_addr_v4mapped(&addr)) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700289 seq_printf(m, "%s %pI4 %s\n",
290 im->m_class, &addr.s6_addr32[3], dom);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100291 } else {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700292 seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return 0;
295}
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400298static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class,
299 struct in6_addr *addr)
NeilBrown1a9917c2006-03-27 01:15:02 -0800300{
301 struct ip_map ip;
302 struct cache_head *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
NeilBrown1a9917c2006-03-27 01:15:02 -0800304 strcpy(ip.m_class, class);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100305 ipv6_addr_copy(&ip.m_addr, addr);
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400306 ch = sunrpc_cache_lookup(cd, &ip.h,
NeilBrown1a9917c2006-03-27 01:15:02 -0800307 hash_str(class, IP_HASHBITS) ^
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100308 hash_ip6(*addr));
NeilBrown1a9917c2006-03-27 01:15:02 -0800309
310 if (ch)
311 return container_of(ch, struct ip_map, h);
312 else
313 return NULL;
314}
315
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400316static inline struct ip_map *ip_map_lookup(struct net *net, char *class,
317 struct in6_addr *addr)
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400318{
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400319 struct sunrpc_net *sn;
320
321 sn = net_generic(net, sunrpc_net_id);
322 return __ip_map_lookup(sn->ip_map_cache, class, addr);
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400323}
324
325static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm,
326 struct unix_domain *udom, time_t expiry)
NeilBrown1a9917c2006-03-27 01:15:02 -0800327{
328 struct ip_map ip;
329 struct cache_head *ch;
330
331 ip.m_client = udom;
332 ip.h.flags = 0;
333 if (!udom)
334 set_bit(CACHE_NEGATIVE, &ip.h.flags);
335 else {
336 ip.m_add_change = udom->addr_changes;
337 /* if this is from the legacy set_client system call,
338 * we need m_add_change to be one higher
339 */
340 if (expiry == NEVER)
341 ip.m_add_change++;
342 }
343 ip.h.expiry_time = expiry;
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400344 ch = sunrpc_cache_update(cd, &ip.h, &ipm->h,
NeilBrown1a9917c2006-03-27 01:15:02 -0800345 hash_str(ipm->m_class, IP_HASHBITS) ^
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100346 hash_ip6(ipm->m_addr));
NeilBrown1a9917c2006-03-27 01:15:02 -0800347 if (!ch)
348 return -ENOMEM;
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400349 cache_put(ch, cd);
NeilBrown1a9917c2006-03-27 01:15:02 -0800350 return 0;
351}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400353static inline int ip_map_update(struct net *net, struct ip_map *ipm,
354 struct unix_domain *udom, time_t expiry)
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400355{
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400356 struct sunrpc_net *sn;
357
358 sn = net_generic(net, sunrpc_net_id);
359 return __ip_map_update(sn->ip_map_cache, ipm, udom, expiry);
Pavel Emelyanovbf18ab32010-09-27 13:57:36 +0400360}
361
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400362int auth_unix_add_addr(struct net *net, struct in6_addr *addr, struct auth_domain *dom)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 struct unix_domain *udom;
NeilBrown1a9917c2006-03-27 01:15:02 -0800365 struct ip_map *ipmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
NeilBrownefc36aa2006-03-27 01:14:59 -0800367 if (dom->flavour != &svcauth_unix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return -EINVAL;
369 udom = container_of(dom, struct unix_domain, h);
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400370 ipmp = ip_map_lookup(net, "nfsd", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
NeilBrown1a9917c2006-03-27 01:15:02 -0800372 if (ipmp)
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400373 return ip_map_update(net, ipmp, udom, NEVER);
NeilBrown1a9917c2006-03-27 01:15:02 -0800374 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return -ENOMEM;
376}
Trond Myklebust24c37672008-12-23 16:30:12 -0500377EXPORT_SYMBOL_GPL(auth_unix_add_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379int auth_unix_forget_old(struct auth_domain *dom)
380{
381 struct unix_domain *udom;
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800382
NeilBrownefc36aa2006-03-27 01:14:59 -0800383 if (dom->flavour != &svcauth_unix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return -EINVAL;
385 udom = container_of(dom, struct unix_domain, h);
386 udom->addr_changes++;
387 return 0;
388}
Trond Myklebust24c37672008-12-23 16:30:12 -0500389EXPORT_SYMBOL_GPL(auth_unix_forget_old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400391struct auth_domain *auth_unix_lookup(struct net *net, struct in6_addr *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
Greg Banks40f10522006-10-02 02:17:43 -0700393 struct ip_map *ipm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct auth_domain *rv;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400395 struct sunrpc_net *sn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400397 sn = net_generic(net, sunrpc_net_id);
Pavel Emelyanov352114f2010-09-27 13:59:48 +0400398 ipm = ip_map_lookup(net, "nfsd", addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (!ipm)
401 return NULL;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400402 if (cache_check(sn->ip_map_cache, &ipm->h, NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 return NULL;
404
405 if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
406 if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
407 auth_domain_put(&ipm->m_client->h);
408 rv = NULL;
409 } else {
410 rv = &ipm->m_client->h;
NeilBrownefc36aa2006-03-27 01:14:59 -0800411 kref_get(&rv->ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400413 cache_put(&ipm->h, sn->ip_map_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return rv;
415}
Trond Myklebust24c37672008-12-23 16:30:12 -0500416EXPORT_SYMBOL_GPL(auth_unix_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418void svcauth_unix_purge(void)
419{
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400420 struct net *net;
421
422 for_each_net(net) {
423 struct sunrpc_net *sn;
424
425 sn = net_generic(net, sunrpc_net_id);
426 cache_purge(sn->ip_map_cache);
427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
Trond Myklebust24c37672008-12-23 16:30:12 -0500429EXPORT_SYMBOL_GPL(svcauth_unix_purge);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700431static inline struct ip_map *
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400432ip_map_cached_get(struct svc_xprt *xprt)
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700433{
Tom Tuckerdef13d72007-12-30 21:08:08 -0600434 struct ip_map *ipm = NULL;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400435 struct sunrpc_net *sn;
Tom Tuckerdef13d72007-12-30 21:08:08 -0600436
437 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
438 spin_lock(&xprt->xpt_lock);
439 ipm = xprt->xpt_auth_cache;
440 if (ipm != NULL) {
441 if (!cache_valid(&ipm->h)) {
442 /*
443 * The entry has been invalidated since it was
444 * remembered, e.g. by a second mount from the
445 * same IP address.
446 */
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400447 sn = net_generic(xprt->xpt_net, sunrpc_net_id);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600448 xprt->xpt_auth_cache = NULL;
449 spin_unlock(&xprt->xpt_lock);
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400450 cache_put(&ipm->h, sn->ip_map_cache);
Tom Tuckerdef13d72007-12-30 21:08:08 -0600451 return NULL;
452 }
453 cache_get(&ipm->h);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700454 }
Tom Tuckerdef13d72007-12-30 21:08:08 -0600455 spin_unlock(&xprt->xpt_lock);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700456 }
457 return ipm;
458}
459
460static inline void
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400461ip_map_cached_put(struct svc_xprt *xprt, struct ip_map *ipm)
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700462{
Tom Tuckerdef13d72007-12-30 21:08:08 -0600463 if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
464 spin_lock(&xprt->xpt_lock);
465 if (xprt->xpt_auth_cache == NULL) {
466 /* newly cached, keep the reference */
467 xprt->xpt_auth_cache = ipm;
468 ipm = NULL;
469 }
470 spin_unlock(&xprt->xpt_lock);
NeilBrown30f3dee2007-04-16 22:53:25 -0700471 }
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400472 if (ipm) {
473 struct sunrpc_net *sn;
474
475 sn = net_generic(xprt->xpt_net, sunrpc_net_id);
476 cache_put(&ipm->h, sn->ip_map_cache);
477 }
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700478}
479
480void
Pavel Emelyanove3bfca02010-09-27 13:58:42 +0400481svcauth_unix_info_release(struct svc_xprt *xpt)
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700482{
Pavel Emelyanove3bfca02010-09-27 13:58:42 +0400483 struct ip_map *ipm;
484
485 ipm = xpt->xpt_auth_cache;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400486 if (ipm != NULL) {
487 struct sunrpc_net *sn;
488
489 sn = net_generic(xpt->xpt_net, sunrpc_net_id);
490 cache_put(&ipm->h, sn->ip_map_cache);
491 }
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700492}
493
NeilBrown3fc605a2007-02-14 00:33:13 -0800494/****************************************************************************
495 * auth.unix.gid cache
496 * simple cache to map a UID to a list of GIDs
497 * because AUTH_UNIX aka AUTH_SYS has a max of 16
498 */
499#define GID_HASHBITS 8
500#define GID_HASHMAX (1<<GID_HASHBITS)
501#define GID_HASHMASK (GID_HASHMAX - 1)
502
503struct unix_gid {
504 struct cache_head h;
505 uid_t uid;
506 struct group_info *gi;
507};
508static struct cache_head *gid_table[GID_HASHMAX];
509
510static void unix_gid_put(struct kref *kref)
511{
512 struct cache_head *item = container_of(kref, struct cache_head, ref);
513 struct unix_gid *ug = container_of(item, struct unix_gid, h);
514 if (test_bit(CACHE_VALID, &item->flags) &&
515 !test_bit(CACHE_NEGATIVE, &item->flags))
516 put_group_info(ug->gi);
517 kfree(ug);
518}
519
520static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
521{
522 struct unix_gid *orig = container_of(corig, struct unix_gid, h);
523 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
524 return orig->uid == new->uid;
525}
526static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
527{
528 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
529 struct unix_gid *item = container_of(citem, struct unix_gid, h);
530 new->uid = item->uid;
531}
532static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
533{
534 struct unix_gid *new = container_of(cnew, struct unix_gid, h);
535 struct unix_gid *item = container_of(citem, struct unix_gid, h);
536
537 get_group_info(item->gi);
538 new->gi = item->gi;
539}
540static struct cache_head *unix_gid_alloc(void)
541{
542 struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
543 if (g)
544 return &g->h;
545 else
546 return NULL;
547}
548
549static void unix_gid_request(struct cache_detail *cd,
550 struct cache_head *h,
551 char **bpp, int *blen)
552{
553 char tuid[20];
554 struct unix_gid *ug = container_of(h, struct unix_gid, h);
555
556 snprintf(tuid, 20, "%u", ug->uid);
557 qword_add(bpp, blen, tuid);
558 (*bpp)[-1] = '\n';
559}
560
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400561static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h)
562{
563 return sunrpc_cache_pipe_upcall(cd, h, unix_gid_request);
564}
565
NeilBrown3fc605a2007-02-14 00:33:13 -0800566static struct unix_gid *unix_gid_lookup(uid_t uid);
567extern struct cache_detail unix_gid_cache;
568
569static int unix_gid_parse(struct cache_detail *cd,
570 char *mesg, int mlen)
571{
572 /* uid expiry Ngid gid0 gid1 ... gidN-1 */
573 int uid;
574 int gids;
575 int rv;
576 int i;
577 int err;
578 time_t expiry;
579 struct unix_gid ug, *ugp;
580
581 if (mlen <= 0 || mesg[mlen-1] != '\n')
582 return -EINVAL;
583 mesg[mlen-1] = 0;
584
585 rv = get_int(&mesg, &uid);
586 if (rv)
587 return -EINVAL;
588 ug.uid = uid;
589
590 expiry = get_expiry(&mesg);
591 if (expiry == 0)
592 return -EINVAL;
593
594 rv = get_int(&mesg, &gids);
595 if (rv || gids < 0 || gids > 8192)
596 return -EINVAL;
597
598 ug.gi = groups_alloc(gids);
599 if (!ug.gi)
600 return -ENOMEM;
601
602 for (i = 0 ; i < gids ; i++) {
603 int gid;
604 rv = get_int(&mesg, &gid);
605 err = -EINVAL;
606 if (rv)
607 goto out;
608 GROUP_AT(ug.gi, i) = gid;
609 }
610
611 ugp = unix_gid_lookup(uid);
612 if (ugp) {
613 struct cache_head *ch;
614 ug.h.flags = 0;
615 ug.h.expiry_time = expiry;
616 ch = sunrpc_cache_update(&unix_gid_cache,
617 &ug.h, &ugp->h,
618 hash_long(uid, GID_HASHBITS));
619 if (!ch)
620 err = -ENOMEM;
621 else {
622 err = 0;
623 cache_put(ch, &unix_gid_cache);
624 }
625 } else
626 err = -ENOMEM;
627 out:
628 if (ug.gi)
629 put_group_info(ug.gi);
630 return err;
631}
632
633static int unix_gid_show(struct seq_file *m,
634 struct cache_detail *cd,
635 struct cache_head *h)
636{
637 struct unix_gid *ug;
638 int i;
639 int glen;
640
641 if (h == NULL) {
642 seq_puts(m, "#uid cnt: gids...\n");
643 return 0;
644 }
645 ug = container_of(h, struct unix_gid, h);
646 if (test_bit(CACHE_VALID, &h->flags) &&
647 !test_bit(CACHE_NEGATIVE, &h->flags))
648 glen = ug->gi->ngroups;
649 else
650 glen = 0;
651
J. Bruce Fieldsccdb3572010-03-02 15:49:21 -0500652 seq_printf(m, "%u %d:", ug->uid, glen);
NeilBrown3fc605a2007-02-14 00:33:13 -0800653 for (i = 0; i < glen; i++)
654 seq_printf(m, " %d", GROUP_AT(ug->gi, i));
655 seq_printf(m, "\n");
656 return 0;
657}
658
659struct cache_detail unix_gid_cache = {
660 .owner = THIS_MODULE,
661 .hash_size = GID_HASHMAX,
662 .hash_table = gid_table,
663 .name = "auth.unix.gid",
664 .cache_put = unix_gid_put,
Trond Myklebustbc74b4f2009-08-09 15:14:29 -0400665 .cache_upcall = unix_gid_upcall,
NeilBrown3fc605a2007-02-14 00:33:13 -0800666 .cache_parse = unix_gid_parse,
667 .cache_show = unix_gid_show,
668 .match = unix_gid_match,
669 .init = unix_gid_init,
670 .update = unix_gid_update,
671 .alloc = unix_gid_alloc,
672};
673
674static struct unix_gid *unix_gid_lookup(uid_t uid)
675{
676 struct unix_gid ug;
677 struct cache_head *ch;
678
679 ug.uid = uid;
680 ch = sunrpc_cache_lookup(&unix_gid_cache, &ug.h,
681 hash_long(uid, GID_HASHBITS));
682 if (ch)
683 return container_of(ch, struct unix_gid, h);
684 else
685 return NULL;
686}
687
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400688static struct group_info *unix_gid_find(uid_t uid, struct svc_rqst *rqstp)
NeilBrown3fc605a2007-02-14 00:33:13 -0800689{
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400690 struct unix_gid *ug;
691 struct group_info *gi;
692 int ret;
693
694 ug = unix_gid_lookup(uid);
NeilBrown3fc605a2007-02-14 00:33:13 -0800695 if (!ug)
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400696 return ERR_PTR(-EAGAIN);
697 ret = cache_check(&unix_gid_cache, &ug->h, &rqstp->rq_chandle);
698 switch (ret) {
NeilBrown3fc605a2007-02-14 00:33:13 -0800699 case -ENOENT:
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400700 return ERR_PTR(-ENOENT);
NeilBrown1ebede82010-08-12 17:04:07 +1000701 case -ETIMEDOUT:
702 return ERR_PTR(-ESHUTDOWN);
NeilBrown3fc605a2007-02-14 00:33:13 -0800703 case 0:
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400704 gi = get_group_info(ug->gi);
NeilBrown560ab422009-08-04 15:22:39 +1000705 cache_put(&ug->h, &unix_gid_cache);
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400706 return gi;
NeilBrown3fc605a2007-02-14 00:33:13 -0800707 default:
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400708 return ERR_PTR(-EAGAIN);
NeilBrown3fc605a2007-02-14 00:33:13 -0800709 }
710}
711
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700712int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713svcauth_unix_set_client(struct svc_rqst *rqstp)
714{
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100715 struct sockaddr_in *sin;
716 struct sockaddr_in6 *sin6, sin6_storage;
NeilBrown1a9917c2006-03-27 01:15:02 -0800717 struct ip_map *ipm;
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400718 struct group_info *gi;
719 struct svc_cred *cred = &rqstp->rq_cred;
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400720 struct svc_xprt *xprt = rqstp->rq_xprt;
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400721 struct net *net = xprt->xpt_net;
722 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100724 switch (rqstp->rq_addr.ss_family) {
725 case AF_INET:
726 sin = svc_addr_in(rqstp);
727 sin6 = &sin6_storage;
Brian Haleyb301e822009-10-07 13:58:25 -0700728 ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100729 break;
730 case AF_INET6:
731 sin6 = svc_addr_in6(rqstp);
732 break;
733 default:
734 BUG();
735 }
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 rqstp->rq_client = NULL;
738 if (rqstp->rq_proc == 0)
739 return SVC_OK;
740
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400741 ipm = ip_map_cached_get(xprt);
Greg Banks7b2b1fe2006-10-04 02:15:50 -0700742 if (ipm == NULL)
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400743 ipm = __ip_map_lookup(sn->ip_map_cache, rqstp->rq_server->sv_program->pg_class,
Aurélien Charbonf15364b2008-01-18 15:50:56 +0100744 &sin6->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 if (ipm == NULL)
747 return SVC_DENIED;
748
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400749 switch (cache_check(sn->ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 default:
751 BUG();
J.Bruce Fieldse0bb89e2006-12-13 00:35:25 -0800752 case -ETIMEDOUT:
NeilBrown1ebede82010-08-12 17:04:07 +1000753 return SVC_CLOSE;
754 case -EAGAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return SVC_DROP;
756 case -ENOENT:
757 return SVC_DENIED;
758 case 0:
759 rqstp->rq_client = &ipm->m_client->h;
NeilBrownefc36aa2006-03-27 01:14:59 -0800760 kref_get(&rqstp->rq_client->ref);
Pavel Emelyanov3be44792010-09-27 13:59:13 +0400761 ip_map_cached_put(xprt, ipm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 break;
763 }
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400764
765 gi = unix_gid_find(cred->cr_uid, rqstp);
766 switch (PTR_ERR(gi)) {
767 case -EAGAIN:
768 return SVC_DROP;
NeilBrown1ebede82010-08-12 17:04:07 +1000769 case -ESHUTDOWN:
770 return SVC_CLOSE;
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400771 case -ENOENT:
772 break;
773 default:
774 put_group_info(cred->cr_group_info);
775 cred->cr_group_info = gi;
776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return SVC_OK;
778}
779
Trond Myklebust24c37672008-12-23 16:30:12 -0500780EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
J. Bruce Fields3ab4d8b2007-07-17 04:04:46 -0700781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782static int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700783svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 struct kvec *argv = &rqstp->rq_arg.head[0];
786 struct kvec *resv = &rqstp->rq_res.head[0];
787 struct svc_cred *cred = &rqstp->rq_cred;
788
789 cred->cr_group_info = NULL;
790 rqstp->rq_client = NULL;
791
792 if (argv->iov_len < 3*4)
793 return SVC_GARBAGE;
794
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800795 if (svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 dprintk("svc: bad null cred\n");
797 *authp = rpc_autherr_badcred;
798 return SVC_DENIED;
799 }
Alexey Dobriyan76994312006-09-26 22:28:46 -0700800 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 dprintk("svc: bad null verf\n");
802 *authp = rpc_autherr_badverf;
803 return SVC_DENIED;
804 }
805
806 /* Signal that mapping to nobody uid/gid is required */
807 cred->cr_uid = (uid_t) -1;
808 cred->cr_gid = (gid_t) -1;
809 cred->cr_group_info = groups_alloc(0);
810 if (cred->cr_group_info == NULL)
NeilBrown1ebede82010-08-12 17:04:07 +1000811 return SVC_CLOSE; /* kmalloc failure - client must retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 /* Put NULL verifier */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700814 svc_putnl(resv, RPC_AUTH_NULL);
815 svc_putnl(resv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Andy Adamsonc4170583f2007-07-17 04:04:42 -0700817 rqstp->rq_flavor = RPC_AUTH_NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return SVC_OK;
819}
820
821static int
822svcauth_null_release(struct svc_rqst *rqstp)
823{
824 if (rqstp->rq_client)
825 auth_domain_put(rqstp->rq_client);
826 rqstp->rq_client = NULL;
827 if (rqstp->rq_cred.cr_group_info)
828 put_group_info(rqstp->rq_cred.cr_group_info);
829 rqstp->rq_cred.cr_group_info = NULL;
830
831 return 0; /* don't drop */
832}
833
834
835struct auth_ops svcauth_null = {
836 .name = "null",
837 .owner = THIS_MODULE,
838 .flavour = RPC_AUTH_NULL,
839 .accept = svcauth_null_accept,
840 .release = svcauth_null_release,
841 .set_client = svcauth_unix_set_client,
842};
843
844
845static int
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700846svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 struct kvec *argv = &rqstp->rq_arg.head[0];
849 struct kvec *resv = &rqstp->rq_res.head[0];
850 struct svc_cred *cred = &rqstp->rq_cred;
851 u32 slen, i;
852 int len = argv->iov_len;
853
854 cred->cr_group_info = NULL;
855 rqstp->rq_client = NULL;
856
857 if ((len -= 3*4) < 0)
858 return SVC_GARBAGE;
859
860 svc_getu32(argv); /* length */
861 svc_getu32(argv); /* time stamp */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700862 slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (slen > 64 || (len -= (slen + 3)*4) < 0)
864 goto badcred;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700865 argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 argv->iov_len -= slen*4;
867
Alexey Dobriyan76994312006-09-26 22:28:46 -0700868 cred->cr_uid = svc_getnl(argv); /* uid */
869 cred->cr_gid = svc_getnl(argv); /* gid */
870 slen = svc_getnl(argv); /* gids length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (slen > 16 || (len -= (slen + 2)*4) < 0)
872 goto badcred;
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400873 cred->cr_group_info = groups_alloc(slen);
874 if (cred->cr_group_info == NULL)
NeilBrown1ebede82010-08-12 17:04:07 +1000875 return SVC_CLOSE;
J. Bruce Fieldsdc83d6e2009-10-20 18:51:34 -0400876 for (i = 0; i < slen; i++)
877 GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
Alexey Dobriyan76994312006-09-26 22:28:46 -0700878 if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 *authp = rpc_autherr_badverf;
880 return SVC_DENIED;
881 }
882
883 /* Put NULL verifier */
Alexey Dobriyan76994312006-09-26 22:28:46 -0700884 svc_putnl(resv, RPC_AUTH_NULL);
885 svc_putnl(resv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Andy Adamsonc4170583f2007-07-17 04:04:42 -0700887 rqstp->rq_flavor = RPC_AUTH_UNIX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return SVC_OK;
889
890badcred:
891 *authp = rpc_autherr_badcred;
892 return SVC_DENIED;
893}
894
895static int
896svcauth_unix_release(struct svc_rqst *rqstp)
897{
898 /* Verifier (such as it is) is already in place.
899 */
900 if (rqstp->rq_client)
901 auth_domain_put(rqstp->rq_client);
902 rqstp->rq_client = NULL;
903 if (rqstp->rq_cred.cr_group_info)
904 put_group_info(rqstp->rq_cred.cr_group_info);
905 rqstp->rq_cred.cr_group_info = NULL;
906
907 return 0;
908}
909
910
911struct auth_ops svcauth_unix = {
912 .name = "unix",
913 .owner = THIS_MODULE,
914 .flavour = RPC_AUTH_UNIX,
915 .accept = svcauth_unix_accept,
916 .release = svcauth_unix_release,
917 .domain_release = svcauth_unix_domain_release,
918 .set_client = svcauth_unix_set_client,
919};
920
Pavel Emelyanov90d51b02010-09-27 14:02:29 +0400921int ip_map_cache_create(struct net *net)
922{
923 int err = -ENOMEM;
924 struct cache_detail *cd;
925 struct cache_head **tbl;
926 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
927
928 cd = kzalloc(sizeof(struct cache_detail), GFP_KERNEL);
929 if (cd == NULL)
930 goto err_cd;
931
932 tbl = kzalloc(IP_HASHMAX * sizeof(struct cache_head *), GFP_KERNEL);
933 if (tbl == NULL)
934 goto err_tbl;
935
936 cd->owner = THIS_MODULE,
937 cd->hash_size = IP_HASHMAX,
938 cd->hash_table = tbl,
939 cd->name = "auth.unix.ip",
940 cd->cache_put = ip_map_put,
941 cd->cache_upcall = ip_map_upcall,
942 cd->cache_parse = ip_map_parse,
943 cd->cache_show = ip_map_show,
944 cd->match = ip_map_match,
945 cd->init = ip_map_init,
946 cd->update = update,
947 cd->alloc = ip_map_alloc,
948
949 err = cache_register_net(cd, net);
950 if (err)
951 goto err_reg;
952
953 sn->ip_map_cache = cd;
954 return 0;
955
956err_reg:
957 kfree(tbl);
958err_tbl:
959 kfree(cd);
960err_cd:
961 return err;
962}
963
964void ip_map_cache_destroy(struct net *net)
965{
966 struct sunrpc_net *sn;
967
968 sn = net_generic(net, sunrpc_net_id);
969 cache_purge(sn->ip_map_cache);
970 cache_unregister_net(sn->ip_map_cache, net);
971 kfree(sn->ip_map_cache->hash_table);
972 kfree(sn->ip_map_cache);
973}