svcauth_gss.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  1. /*
  2. * Neil Brown <neilb@cse.unsw.edu.au>
  3. * J. Bruce Fields <bfields@umich.edu>
  4. * Andy Adamson <andros@umich.edu>
  5. * Dug Song <dugsong@monkey.org>
  6. *
  7. * RPCSEC_GSS server authentication.
  8. * This implements RPCSEC_GSS as defined in rfc2203 (rpcsec_gss) and rfc2078
  9. * (gssapi)
  10. *
  11. * The RPCSEC_GSS involves three stages:
  12. * 1/ context creation
  13. * 2/ data exchange
  14. * 3/ context destruction
  15. *
  16. * Context creation is handled largely by upcalls to user-space.
  17. * In particular, GSS_Accept_sec_context is handled by an upcall
  18. * Data exchange is handled entirely within the kernel
  19. * In particular, GSS_GetMIC, GSS_VerifyMIC, GSS_Seal, GSS_Unseal are in-kernel.
  20. * Context destruction is handled in-kernel
  21. * GSS_Delete_sec_context is in-kernel
  22. *
  23. * Context creation is initiated by a RPCSEC_GSS_INIT request arriving.
  24. * The context handle and gss_token are used as a key into the rpcsec_init cache.
  25. * The content of this cache includes some of the outputs of GSS_Accept_sec_context,
  26. * being major_status, minor_status, context_handle, reply_token.
  27. * These are sent back to the client.
  28. * Sequence window management is handled by the kernel. The window size if currently
  29. * a compile time constant.
  30. *
  31. * When user-space is happy that a context is established, it places an entry
  32. * in the rpcsec_context cache. The key for this cache is the context_handle.
  33. * The content includes:
  34. * uid/gidlist - for determining access rights
  35. * mechanism type
  36. * mechanism specific information, such as a key
  37. *
  38. */
  39. #include <linux/slab.h>
  40. #include <linux/types.h>
  41. #include <linux/module.h>
  42. #include <linux/pagemap.h>
  43. #include <linux/user_namespace.h>
  44. #include <linux/sunrpc/auth_gss.h>
  45. #include <linux/sunrpc/gss_err.h>
  46. #include <linux/sunrpc/svcauth.h>
  47. #include <linux/sunrpc/svcauth_gss.h>
  48. #include <linux/sunrpc/cache.h>
  49. #include "gss_rpc_upcall.h"
  50. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  51. # define RPCDBG_FACILITY RPCDBG_AUTH
  52. #endif
  53. /* The rpcsec_init cache is used for mapping RPCSEC_GSS_{,CONT_}INIT requests
  54. * into replies.
  55. *
  56. * Key is context handle (\x if empty) and gss_token.
  57. * Content is major_status minor_status (integers) context_handle, reply_token.
  58. *
  59. */
  60. static int netobj_equal(struct xdr_netobj *a, struct xdr_netobj *b)
  61. {
  62. return a->len == b->len && 0 == memcmp(a->data, b->data, a->len);
  63. }
  64. #define RSI_HASHBITS 6
  65. #define RSI_HASHMAX (1<<RSI_HASHBITS)
  66. struct rsi {
  67. struct cache_head h;
  68. struct xdr_netobj in_handle, in_token;
  69. struct xdr_netobj out_handle, out_token;
  70. int major_status, minor_status;
  71. };
  72. static struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old);
  73. static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item);
  74. static void rsi_free(struct rsi *rsii)
  75. {
  76. kfree(rsii->in_handle.data);
  77. kfree(rsii->in_token.data);
  78. kfree(rsii->out_handle.data);
  79. kfree(rsii->out_token.data);
  80. }
  81. static void rsi_put(struct kref *ref)
  82. {
  83. struct rsi *rsii = container_of(ref, struct rsi, h.ref);
  84. rsi_free(rsii);
  85. kfree(rsii);
  86. }
  87. static inline int rsi_hash(struct rsi *item)
  88. {
  89. return hash_mem(item->in_handle.data, item->in_handle.len, RSI_HASHBITS)
  90. ^ hash_mem(item->in_token.data, item->in_token.len, RSI_HASHBITS);
  91. }
  92. static int rsi_match(struct cache_head *a, struct cache_head *b)
  93. {
  94. struct rsi *item = container_of(a, struct rsi, h);
  95. struct rsi *tmp = container_of(b, struct rsi, h);
  96. return netobj_equal(&item->in_handle, &tmp->in_handle) &&
  97. netobj_equal(&item->in_token, &tmp->in_token);
  98. }
  99. static int dup_to_netobj(struct xdr_netobj *dst, char *src, int len)
  100. {
  101. dst->len = len;
  102. dst->data = (len ? kmemdup(src, len, GFP_KERNEL) : NULL);
  103. if (len && !dst->data)
  104. return -ENOMEM;
  105. return 0;
  106. }
  107. static inline int dup_netobj(struct xdr_netobj *dst, struct xdr_netobj *src)
  108. {
  109. return dup_to_netobj(dst, src->data, src->len);
  110. }
  111. static void rsi_init(struct cache_head *cnew, struct cache_head *citem)
  112. {
  113. struct rsi *new = container_of(cnew, struct rsi, h);
  114. struct rsi *item = container_of(citem, struct rsi, h);
  115. new->out_handle.data = NULL;
  116. new->out_handle.len = 0;
  117. new->out_token.data = NULL;
  118. new->out_token.len = 0;
  119. new->in_handle.len = item->in_handle.len;
  120. item->in_handle.len = 0;
  121. new->in_token.len = item->in_token.len;
  122. item->in_token.len = 0;
  123. new->in_handle.data = item->in_handle.data;
  124. item->in_handle.data = NULL;
  125. new->in_token.data = item->in_token.data;
  126. item->in_token.data = NULL;
  127. }
  128. static void update_rsi(struct cache_head *cnew, struct cache_head *citem)
  129. {
  130. struct rsi *new = container_of(cnew, struct rsi, h);
  131. struct rsi *item = container_of(citem, struct rsi, h);
  132. BUG_ON(new->out_handle.data || new->out_token.data);
  133. new->out_handle.len = item->out_handle.len;
  134. item->out_handle.len = 0;
  135. new->out_token.len = item->out_token.len;
  136. item->out_token.len = 0;
  137. new->out_handle.data = item->out_handle.data;
  138. item->out_handle.data = NULL;
  139. new->out_token.data = item->out_token.data;
  140. item->out_token.data = NULL;
  141. new->major_status = item->major_status;
  142. new->minor_status = item->minor_status;
  143. }
  144. static struct cache_head *rsi_alloc(void)
  145. {
  146. struct rsi *rsii = kmalloc(sizeof(*rsii), GFP_KERNEL);
  147. if (rsii)
  148. return &rsii->h;
  149. else
  150. return NULL;
  151. }
  152. static void rsi_request(struct cache_detail *cd,
  153. struct cache_head *h,
  154. char **bpp, int *blen)
  155. {
  156. struct rsi *rsii = container_of(h, struct rsi, h);
  157. qword_addhex(bpp, blen, rsii->in_handle.data, rsii->in_handle.len);
  158. qword_addhex(bpp, blen, rsii->in_token.data, rsii->in_token.len);
  159. (*bpp)[-1] = '\n';
  160. }
  161. static int rsi_parse(struct cache_detail *cd,
  162. char *mesg, int mlen)
  163. {
  164. /* context token expiry major minor context token */
  165. char *buf = mesg;
  166. char *ep;
  167. int len;
  168. struct rsi rsii, *rsip = NULL;
  169. time_t expiry;
  170. int status = -EINVAL;
  171. memset(&rsii, 0, sizeof(rsii));
  172. /* handle */
  173. len = qword_get(&mesg, buf, mlen);
  174. if (len < 0)
  175. goto out;
  176. status = -ENOMEM;
  177. if (dup_to_netobj(&rsii.in_handle, buf, len))
  178. goto out;
  179. /* token */
  180. len = qword_get(&mesg, buf, mlen);
  181. status = -EINVAL;
  182. if (len < 0)
  183. goto out;
  184. status = -ENOMEM;
  185. if (dup_to_netobj(&rsii.in_token, buf, len))
  186. goto out;
  187. rsip = rsi_lookup(cd, &rsii);
  188. if (!rsip)
  189. goto out;
  190. rsii.h.flags = 0;
  191. /* expiry */
  192. expiry = get_expiry(&mesg);
  193. status = -EINVAL;
  194. if (expiry == 0)
  195. goto out;
  196. /* major/minor */
  197. len = qword_get(&mesg, buf, mlen);
  198. if (len <= 0)
  199. goto out;
  200. rsii.major_status = simple_strtoul(buf, &ep, 10);
  201. if (*ep)
  202. goto out;
  203. len = qword_get(&mesg, buf, mlen);
  204. if (len <= 0)
  205. goto out;
  206. rsii.minor_status = simple_strtoul(buf, &ep, 10);
  207. if (*ep)
  208. goto out;
  209. /* out_handle */
  210. len = qword_get(&mesg, buf, mlen);
  211. if (len < 0)
  212. goto out;
  213. status = -ENOMEM;
  214. if (dup_to_netobj(&rsii.out_handle, buf, len))
  215. goto out;
  216. /* out_token */
  217. len = qword_get(&mesg, buf, mlen);
  218. status = -EINVAL;
  219. if (len < 0)
  220. goto out;
  221. status = -ENOMEM;
  222. if (dup_to_netobj(&rsii.out_token, buf, len))
  223. goto out;
  224. rsii.h.expiry_time = expiry;
  225. rsip = rsi_update(cd, &rsii, rsip);
  226. status = 0;
  227. out:
  228. rsi_free(&rsii);
  229. if (rsip)
  230. cache_put(&rsip->h, cd);
  231. else
  232. status = -ENOMEM;
  233. return status;
  234. }
  235. static struct cache_detail rsi_cache_template = {
  236. .owner = THIS_MODULE,
  237. .hash_size = RSI_HASHMAX,
  238. .name = "auth.rpcsec.init",
  239. .cache_put = rsi_put,
  240. .cache_request = rsi_request,
  241. .cache_parse = rsi_parse,
  242. .match = rsi_match,
  243. .init = rsi_init,
  244. .update = update_rsi,
  245. .alloc = rsi_alloc,
  246. };
  247. static struct rsi *rsi_lookup(struct cache_detail *cd, struct rsi *item)
  248. {
  249. struct cache_head *ch;
  250. int hash = rsi_hash(item);
  251. ch = sunrpc_cache_lookup(cd, &item->h, hash);
  252. if (ch)
  253. return container_of(ch, struct rsi, h);
  254. else
  255. return NULL;
  256. }
  257. static struct rsi *rsi_update(struct cache_detail *cd, struct rsi *new, struct rsi *old)
  258. {
  259. struct cache_head *ch;
  260. int hash = rsi_hash(new);
  261. ch = sunrpc_cache_update(cd, &new->h,
  262. &old->h, hash);
  263. if (ch)
  264. return container_of(ch, struct rsi, h);
  265. else
  266. return NULL;
  267. }
  268. /*
  269. * The rpcsec_context cache is used to store a context that is
  270. * used in data exchange.
  271. * The key is a context handle. The content is:
  272. * uid, gidlist, mechanism, service-set, mech-specific-data
  273. */
  274. #define RSC_HASHBITS 10
  275. #define RSC_HASHMAX (1<<RSC_HASHBITS)
  276. #define GSS_SEQ_WIN 128
  277. struct gss_svc_seq_data {
  278. /* highest seq number seen so far: */
  279. int sd_max;
  280. /* for i such that sd_max-GSS_SEQ_WIN < i <= sd_max, the i-th bit of
  281. * sd_win is nonzero iff sequence number i has been seen already: */
  282. unsigned long sd_win[GSS_SEQ_WIN/BITS_PER_LONG];
  283. spinlock_t sd_lock;
  284. };
  285. struct rsc {
  286. struct cache_head h;
  287. struct xdr_netobj handle;
  288. struct svc_cred cred;
  289. struct gss_svc_seq_data seqdata;
  290. struct gss_ctx *mechctx;
  291. };
  292. static struct rsc *rsc_update(struct cache_detail *cd, struct rsc *new, struct rsc *old);
  293. static struct rsc *rsc_lookup(struct cache_detail *cd, struct rsc *item);
  294. static void rsc_free(struct rsc *rsci)
  295. {
  296. kfree(rsci->handle.data);
  297. if (rsci->mechctx)
  298. gss_delete_sec_context(&rsci->mechctx);
  299. free_svc_cred(&rsci->cred);
  300. }
  301. static void rsc_put(struct kref *ref)
  302. {
  303. struct rsc *rsci = container_of(ref, struct rsc, h.ref);
  304. rsc_free(rsci);
  305. kfree(rsci);
  306. }
  307. static inline int
  308. rsc_hash(struct rsc *rsci)
  309. {
  310. return hash_mem(rsci->handle.data, rsci->handle.len, RSC_HASHBITS);
  311. }
  312. static int
  313. rsc_match(struct cache_head *a, struct cache_head *b)
  314. {
  315. struct rsc *new = container_of(a, struct rsc, h);
  316. struct rsc *tmp = container_of(b, struct rsc, h);
  317. return netobj_equal(&new->handle, &tmp->handle);
  318. }
  319. static void
  320. rsc_init(struct cache_head *cnew, struct cache_head *ctmp)
  321. {
  322. struct rsc *new = container_of(cnew, struct rsc, h);
  323. struct rsc *tmp = container_of(ctmp, struct rsc, h);
  324. new->handle.len = tmp->handle.len;
  325. tmp->handle.len = 0;
  326. new->handle.data = tmp->handle.data;
  327. tmp->handle.data = NULL;
  328. new->mechctx = NULL;
  329. init_svc_cred(&new->cred);
  330. }
  331. static void
  332. update_rsc(struct cache_head *cnew, struct cache_head *ctmp)
  333. {
  334. struct rsc *new = container_of(cnew, struct rsc, h);
  335. struct rsc *tmp = container_of(ctmp, struct rsc, h);
  336. new->mechctx = tmp->mechctx;
  337. tmp->mechctx = NULL;
  338. memset(&new->seqdata, 0, sizeof(new->seqdata));
  339. spin_lock_init(&new->seqdata.sd_lock);
  340. new->cred = tmp->cred;
  341. init_svc_cred(&tmp->cred);
  342. }
  343. static struct cache_head *
  344. rsc_alloc(void)
  345. {
  346. struct rsc *rsci = kmalloc(sizeof(*rsci), GFP_KERNEL);
  347. if (rsci)
  348. return &rsci->h;
  349. else
  350. return NULL;
  351. }
  352. static int rsc_parse(struct cache_detail *cd,
  353. char *mesg, int mlen)
  354. {
  355. /* contexthandle expiry [ uid gid N <n gids> mechname ...mechdata... ] */
  356. char *buf = mesg;
  357. int id;
  358. int len, rv;
  359. struct rsc rsci, *rscp = NULL;
  360. time_t expiry;
  361. int status = -EINVAL;
  362. struct gss_api_mech *gm = NULL;
  363. memset(&rsci, 0, sizeof(rsci));
  364. /* context handle */
  365. len = qword_get(&mesg, buf, mlen);
  366. if (len < 0) goto out;
  367. status = -ENOMEM;
  368. if (dup_to_netobj(&rsci.handle, buf, len))
  369. goto out;
  370. rsci.h.flags = 0;
  371. /* expiry */
  372. expiry = get_expiry(&mesg);
  373. status = -EINVAL;
  374. if (expiry == 0)
  375. goto out;
  376. rscp = rsc_lookup(cd, &rsci);
  377. if (!rscp)
  378. goto out;
  379. /* uid, or NEGATIVE */
  380. rv = get_int(&mesg, &id);
  381. if (rv == -EINVAL)
  382. goto out;
  383. if (rv == -ENOENT)
  384. set_bit(CACHE_NEGATIVE, &rsci.h.flags);
  385. else {
  386. int N, i;
  387. /*
  388. * NOTE: we skip uid_valid()/gid_valid() checks here:
  389. * instead, * -1 id's are later mapped to the
  390. * (export-specific) anonymous id by nfsd_setuser.
  391. *
  392. * (But supplementary gid's get no such special
  393. * treatment so are checked for validity here.)
  394. */
  395. /* uid */
  396. rsci.cred.cr_uid = make_kuid(&init_user_ns, id);
  397. /* gid */
  398. if (get_int(&mesg, &id))
  399. goto out;
  400. rsci.cred.cr_gid = make_kgid(&init_user_ns, id);
  401. /* number of additional gid's */
  402. if (get_int(&mesg, &N))
  403. goto out;
  404. if (N < 0 || N > NGROUPS_MAX)
  405. goto out;
  406. status = -ENOMEM;
  407. rsci.cred.cr_group_info = groups_alloc(N);
  408. if (rsci.cred.cr_group_info == NULL)
  409. goto out;
  410. /* gid's */
  411. status = -EINVAL;
  412. for (i=0; i<N; i++) {
  413. kgid_t kgid;
  414. if (get_int(&mesg, &id))
  415. goto out;
  416. kgid = make_kgid(&init_user_ns, id);
  417. if (!gid_valid(kgid))
  418. goto out;
  419. rsci.cred.cr_group_info->gid[i] = kgid;
  420. }
  421. /* mech name */
  422. len = qword_get(&mesg, buf, mlen);
  423. if (len < 0)
  424. goto out;
  425. gm = rsci.cred.cr_gss_mech = gss_mech_get_by_name(buf);
  426. status = -EOPNOTSUPP;
  427. if (!gm)
  428. goto out;
  429. status = -EINVAL;
  430. /* mech-specific data: */
  431. len = qword_get(&mesg, buf, mlen);
  432. if (len < 0)
  433. goto out;
  434. status = gss_import_sec_context(buf, len, gm, &rsci.mechctx,
  435. NULL, GFP_KERNEL);
  436. if (status)
  437. goto out;
  438. /* get client name */
  439. len = qword_get(&mesg, buf, mlen);
  440. if (len > 0) {
  441. rsci.cred.cr_principal = kstrdup(buf, GFP_KERNEL);
  442. if (!rsci.cred.cr_principal) {
  443. status = -ENOMEM;
  444. goto out;
  445. }
  446. }
  447. }
  448. rsci.h.expiry_time = expiry;
  449. rscp = rsc_update(cd, &rsci, rscp);
  450. status = 0;
  451. out:
  452. rsc_free(&rsci);
  453. if (rscp)
  454. cache_put(&rscp->h, cd);
  455. else
  456. status = -ENOMEM;
  457. return status;
  458. }
  459. static struct cache_detail rsc_cache_template = {
  460. .owner = THIS_MODULE,
  461. .hash_size = RSC_HASHMAX,
  462. .name = "auth.rpcsec.context",
  463. .cache_put = rsc_put,
  464. .cache_parse = rsc_parse,
  465. .match = rsc_match,
  466. .init = rsc_init,
  467. .update = update_rsc,
  468. .alloc = rsc_alloc,
  469. };
  470. static struct rsc *rsc_lookup(struct cache_detail *cd, struct rsc *item)
  471. {
  472. struct cache_head *ch;
  473. int hash = rsc_hash(item);
  474. ch = sunrpc_cache_lookup(cd, &item->h, hash);
  475. if (ch)
  476. return container_of(ch, struct rsc, h);
  477. else
  478. return NULL;
  479. }
  480. static struct rsc *rsc_update(struct cache_detail *cd, struct rsc *new, struct rsc *old)
  481. {
  482. struct cache_head *ch;
  483. int hash = rsc_hash(new);
  484. ch = sunrpc_cache_update(cd, &new->h,
  485. &old->h, hash);
  486. if (ch)
  487. return container_of(ch, struct rsc, h);
  488. else
  489. return NULL;
  490. }
  491. static struct rsc *
  492. gss_svc_searchbyctx(struct cache_detail *cd, struct xdr_netobj *handle)
  493. {
  494. struct rsc rsci;
  495. struct rsc *found;
  496. memset(&rsci, 0, sizeof(rsci));
  497. if (dup_to_netobj(&rsci.handle, handle->data, handle->len))
  498. return NULL;
  499. found = rsc_lookup(cd, &rsci);
  500. rsc_free(&rsci);
  501. if (!found)
  502. return NULL;
  503. if (cache_check(cd, &found->h, NULL))
  504. return NULL;
  505. return found;
  506. }
  507. /* Implements sequence number algorithm as specified in RFC 2203. */
  508. static int
  509. gss_check_seq_num(struct rsc *rsci, int seq_num)
  510. {
  511. struct gss_svc_seq_data *sd = &rsci->seqdata;
  512. spin_lock(&sd->sd_lock);
  513. if (seq_num > sd->sd_max) {
  514. if (seq_num >= sd->sd_max + GSS_SEQ_WIN) {
  515. memset(sd->sd_win,0,sizeof(sd->sd_win));
  516. sd->sd_max = seq_num;
  517. } else while (sd->sd_max < seq_num) {
  518. sd->sd_max++;
  519. __clear_bit(sd->sd_max % GSS_SEQ_WIN, sd->sd_win);
  520. }
  521. __set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win);
  522. goto ok;
  523. } else if (seq_num <= sd->sd_max - GSS_SEQ_WIN) {
  524. goto drop;
  525. }
  526. /* sd_max - GSS_SEQ_WIN < seq_num <= sd_max */
  527. if (__test_and_set_bit(seq_num % GSS_SEQ_WIN, sd->sd_win))
  528. goto drop;
  529. ok:
  530. spin_unlock(&sd->sd_lock);
  531. return 1;
  532. drop:
  533. spin_unlock(&sd->sd_lock);
  534. return 0;
  535. }
  536. static inline u32 round_up_to_quad(u32 i)
  537. {
  538. return (i + 3 ) & ~3;
  539. }
  540. static inline int
  541. svc_safe_getnetobj(struct kvec *argv, struct xdr_netobj *o)
  542. {
  543. int l;
  544. if (argv->iov_len < 4)
  545. return -1;
  546. o->len = svc_getnl(argv);
  547. l = round_up_to_quad(o->len);
  548. if (argv->iov_len < l)
  549. return -1;
  550. o->data = argv->iov_base;
  551. argv->iov_base += l;
  552. argv->iov_len -= l;
  553. return 0;
  554. }
  555. static inline int
  556. svc_safe_putnetobj(struct kvec *resv, struct xdr_netobj *o)
  557. {
  558. u8 *p;
  559. if (resv->iov_len + 4 > PAGE_SIZE)
  560. return -1;
  561. svc_putnl(resv, o->len);
  562. p = resv->iov_base + resv->iov_len;
  563. resv->iov_len += round_up_to_quad(o->len);
  564. if (resv->iov_len > PAGE_SIZE)
  565. return -1;
  566. memcpy(p, o->data, o->len);
  567. memset(p + o->len, 0, round_up_to_quad(o->len) - o->len);
  568. return 0;
  569. }
  570. /*
  571. * Verify the checksum on the header and return SVC_OK on success.
  572. * Otherwise, return SVC_DROP (in the case of a bad sequence number)
  573. * or return SVC_DENIED and indicate error in authp.
  574. */
  575. static int
  576. gss_verify_header(struct svc_rqst *rqstp, struct rsc *rsci,
  577. __be32 *rpcstart, struct rpc_gss_wire_cred *gc, __be32 *authp)
  578. {
  579. struct gss_ctx *ctx_id = rsci->mechctx;
  580. struct xdr_buf rpchdr;
  581. struct xdr_netobj checksum;
  582. u32 flavor = 0;
  583. struct kvec *argv = &rqstp->rq_arg.head[0];
  584. struct kvec iov;
  585. /* data to compute the checksum over: */
  586. iov.iov_base = rpcstart;
  587. iov.iov_len = (u8 *)argv->iov_base - (u8 *)rpcstart;
  588. xdr_buf_from_iov(&iov, &rpchdr);
  589. *authp = rpc_autherr_badverf;
  590. if (argv->iov_len < 4)
  591. return SVC_DENIED;
  592. flavor = svc_getnl(argv);
  593. if (flavor != RPC_AUTH_GSS)
  594. return SVC_DENIED;
  595. if (svc_safe_getnetobj(argv, &checksum))
  596. return SVC_DENIED;
  597. if (rqstp->rq_deferred) /* skip verification of revisited request */
  598. return SVC_OK;
  599. if (gss_verify_mic(ctx_id, &rpchdr, &checksum) != GSS_S_COMPLETE) {
  600. *authp = rpcsec_gsserr_credproblem;
  601. return SVC_DENIED;
  602. }
  603. if (gc->gc_seq > MAXSEQ) {
  604. dprintk("RPC: svcauth_gss: discarding request with "
  605. "large sequence number %d\n", gc->gc_seq);
  606. *authp = rpcsec_gsserr_ctxproblem;
  607. return SVC_DENIED;
  608. }
  609. if (!gss_check_seq_num(rsci, gc->gc_seq)) {
  610. dprintk("RPC: svcauth_gss: discarding request with "
  611. "old sequence number %d\n", gc->gc_seq);
  612. return SVC_DROP;
  613. }
  614. return SVC_OK;
  615. }
  616. static int
  617. gss_write_null_verf(struct svc_rqst *rqstp)
  618. {
  619. __be32 *p;
  620. svc_putnl(rqstp->rq_res.head, RPC_AUTH_NULL);
  621. p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
  622. /* don't really need to check if head->iov_len > PAGE_SIZE ... */
  623. *p++ = 0;
  624. if (!xdr_ressize_check(rqstp, p))
  625. return -1;
  626. return 0;
  627. }
  628. static int
  629. gss_write_verf(struct svc_rqst *rqstp, struct gss_ctx *ctx_id, u32 seq)
  630. {
  631. __be32 *xdr_seq;
  632. u32 maj_stat;
  633. struct xdr_buf verf_data;
  634. struct xdr_netobj mic;
  635. __be32 *p;
  636. struct kvec iov;
  637. int err = -1;
  638. svc_putnl(rqstp->rq_res.head, RPC_AUTH_GSS);
  639. xdr_seq = kmalloc(4, GFP_KERNEL);
  640. if (!xdr_seq)
  641. return -1;
  642. *xdr_seq = htonl(seq);
  643. iov.iov_base = xdr_seq;
  644. iov.iov_len = 4;
  645. xdr_buf_from_iov(&iov, &verf_data);
  646. p = rqstp->rq_res.head->iov_base + rqstp->rq_res.head->iov_len;
  647. mic.data = (u8 *)(p + 1);
  648. maj_stat = gss_get_mic(ctx_id, &verf_data, &mic);
  649. if (maj_stat != GSS_S_COMPLETE)
  650. goto out;
  651. *p++ = htonl(mic.len);
  652. memset((u8 *)p + mic.len, 0, round_up_to_quad(mic.len) - mic.len);
  653. p += XDR_QUADLEN(mic.len);
  654. if (!xdr_ressize_check(rqstp, p))
  655. goto out;
  656. err = 0;
  657. out:
  658. kfree(xdr_seq);
  659. return err;
  660. }
  661. struct gss_domain {
  662. struct auth_domain h;
  663. u32 pseudoflavor;
  664. };
  665. static struct auth_domain *
  666. find_gss_auth_domain(struct gss_ctx *ctx, u32 svc)
  667. {
  668. char *name;
  669. name = gss_service_to_auth_domain_name(ctx->mech_type, svc);
  670. if (!name)
  671. return NULL;
  672. return auth_domain_find(name);
  673. }
  674. static struct auth_ops svcauthops_gss;
  675. u32 svcauth_gss_flavor(struct auth_domain *dom)
  676. {
  677. struct gss_domain *gd = container_of(dom, struct gss_domain, h);
  678. return gd->pseudoflavor;
  679. }
  680. EXPORT_SYMBOL_GPL(svcauth_gss_flavor);
  681. int
  682. svcauth_gss_register_pseudoflavor(u32 pseudoflavor, char * name)
  683. {
  684. struct gss_domain *new;
  685. struct auth_domain *test;
  686. int stat = -ENOMEM;
  687. new = kmalloc(sizeof(*new), GFP_KERNEL);
  688. if (!new)
  689. goto out;
  690. kref_init(&new->h.ref);
  691. new->h.name = kstrdup(name, GFP_KERNEL);
  692. if (!new->h.name)
  693. goto out_free_dom;
  694. new->h.flavour = &svcauthops_gss;
  695. new->pseudoflavor = pseudoflavor;
  696. stat = 0;
  697. test = auth_domain_lookup(name, &new->h);
  698. if (test != &new->h) { /* Duplicate registration */
  699. auth_domain_put(test);
  700. kfree(new->h.name);
  701. goto out_free_dom;
  702. }
  703. return 0;
  704. out_free_dom:
  705. kfree(new);
  706. out:
  707. return stat;
  708. }
  709. EXPORT_SYMBOL_GPL(svcauth_gss_register_pseudoflavor);
  710. static inline int
  711. read_u32_from_xdr_buf(struct xdr_buf *buf, int base, u32 *obj)
  712. {
  713. __be32 raw;
  714. int status;
  715. status = read_bytes_from_xdr_buf(buf, base, &raw, sizeof(*obj));
  716. if (status)
  717. return status;
  718. *obj = ntohl(raw);
  719. return 0;
  720. }
  721. /* It would be nice if this bit of code could be shared with the client.
  722. * Obstacles:
  723. * The client shouldn't malloc(), would have to pass in own memory.
  724. * The server uses base of head iovec as read pointer, while the
  725. * client uses separate pointer. */
  726. static int
  727. unwrap_integ_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)
  728. {
  729. int stat = -EINVAL;
  730. u32 integ_len, maj_stat;
  731. struct xdr_netobj mic;
  732. struct xdr_buf integ_buf;
  733. /* Did we already verify the signature on the original pass through? */
  734. if (rqstp->rq_deferred)
  735. return 0;
  736. integ_len = svc_getnl(&buf->head[0]);
  737. if (integ_len & 3)
  738. return stat;
  739. if (integ_len > buf->len)
  740. return stat;
  741. if (xdr_buf_subsegment(buf, &integ_buf, 0, integ_len))
  742. BUG();
  743. /* copy out mic... */
  744. if (read_u32_from_xdr_buf(buf, integ_len, &mic.len))
  745. BUG();
  746. if (mic.len > RPC_MAX_AUTH_SIZE)
  747. return stat;
  748. mic.data = kmalloc(mic.len, GFP_KERNEL);
  749. if (!mic.data)
  750. return stat;
  751. if (read_bytes_from_xdr_buf(buf, integ_len + 4, mic.data, mic.len))
  752. goto out;
  753. maj_stat = gss_verify_mic(ctx, &integ_buf, &mic);
  754. if (maj_stat != GSS_S_COMPLETE)
  755. goto out;
  756. if (svc_getnl(&buf->head[0]) != seq)
  757. goto out;
  758. /* trim off the mic and padding at the end before returning */
  759. xdr_buf_trim(buf, round_up_to_quad(mic.len) + 4);
  760. stat = 0;
  761. out:
  762. kfree(mic.data);
  763. return stat;
  764. }
  765. static inline int
  766. total_buf_len(struct xdr_buf *buf)
  767. {
  768. return buf->head[0].iov_len + buf->page_len + buf->tail[0].iov_len;
  769. }
  770. static void
  771. fix_priv_head(struct xdr_buf *buf, int pad)
  772. {
  773. if (buf->page_len == 0) {
  774. /* We need to adjust head and buf->len in tandem in this
  775. * case to make svc_defer() work--it finds the original
  776. * buffer start using buf->len - buf->head[0].iov_len. */
  777. buf->head[0].iov_len -= pad;
  778. }
  779. }
  780. static int
  781. unwrap_priv_data(struct svc_rqst *rqstp, struct xdr_buf *buf, u32 seq, struct gss_ctx *ctx)
  782. {
  783. u32 priv_len, maj_stat;
  784. int pad, saved_len, remaining_len, offset;
  785. clear_bit(RQ_SPLICE_OK, &rqstp->rq_flags);
  786. priv_len = svc_getnl(&buf->head[0]);
  787. if (rqstp->rq_deferred) {
  788. /* Already decrypted last time through! The sequence number
  789. * check at out_seq is unnecessary but harmless: */
  790. goto out_seq;
  791. }
  792. /* buf->len is the number of bytes from the original start of the
  793. * request to the end, where head[0].iov_len is just the bytes
  794. * not yet read from the head, so these two values are different: */
  795. remaining_len = total_buf_len(buf);
  796. if (priv_len > remaining_len)
  797. return -EINVAL;
  798. pad = remaining_len - priv_len;
  799. buf->len -= pad;
  800. fix_priv_head(buf, pad);
  801. /* Maybe it would be better to give gss_unwrap a length parameter: */
  802. saved_len = buf->len;
  803. buf->len = priv_len;
  804. maj_stat = gss_unwrap(ctx, 0, buf);
  805. pad = priv_len - buf->len;
  806. buf->len = saved_len;
  807. buf->len -= pad;
  808. /* The upper layers assume the buffer is aligned on 4-byte boundaries.
  809. * In the krb5p case, at least, the data ends up offset, so we need to
  810. * move it around. */
  811. /* XXX: This is very inefficient. It would be better to either do
  812. * this while we encrypt, or maybe in the receive code, if we can peak
  813. * ahead and work out the service and mechanism there. */
  814. offset = buf->head[0].iov_len % 4;
  815. if (offset) {
  816. buf->buflen = RPCSVC_MAXPAYLOAD;
  817. xdr_shift_buf(buf, offset);
  818. fix_priv_head(buf, pad);
  819. }
  820. if (maj_stat != GSS_S_COMPLETE)
  821. return -EINVAL;
  822. out_seq:
  823. if (svc_getnl(&buf->head[0]) != seq)
  824. return -EINVAL;
  825. return 0;
  826. }
  827. struct gss_svc_data {
  828. /* decoded gss client cred: */
  829. struct rpc_gss_wire_cred clcred;
  830. /* save a pointer to the beginning of the encoded verifier,
  831. * for use in encryption/checksumming in svcauth_gss_release: */
  832. __be32 *verf_start;
  833. struct rsc *rsci;
  834. };
  835. static int
  836. svcauth_gss_set_client(struct svc_rqst *rqstp)
  837. {
  838. struct gss_svc_data *svcdata = rqstp->rq_auth_data;
  839. struct rsc *rsci = svcdata->rsci;
  840. struct rpc_gss_wire_cred *gc = &svcdata->clcred;
  841. int stat;
  842. /*
  843. * A gss export can be specified either by:
  844. * export *(sec=krb5,rw)
  845. * or by
  846. * export gss/krb5(rw)
  847. * The latter is deprecated; but for backwards compatibility reasons
  848. * the nfsd code will still fall back on trying it if the former
  849. * doesn't work; so we try to make both available to nfsd, below.
  850. */
  851. rqstp->rq_gssclient = find_gss_auth_domain(rsci->mechctx, gc->gc_svc);
  852. if (rqstp->rq_gssclient == NULL)
  853. return SVC_DENIED;
  854. stat = svcauth_unix_set_client(rqstp);
  855. if (stat == SVC_DROP || stat == SVC_CLOSE)
  856. return stat;
  857. return SVC_OK;
  858. }
  859. static inline int
  860. gss_write_init_verf(struct cache_detail *cd, struct svc_rqst *rqstp,
  861. struct xdr_netobj *out_handle, int *major_status)
  862. {
  863. struct rsc *rsci;
  864. int rc;
  865. if (*major_status != GSS_S_COMPLETE)
  866. return gss_write_null_verf(rqstp);
  867. rsci = gss_svc_searchbyctx(cd, out_handle);
  868. if (rsci == NULL) {
  869. *major_status = GSS_S_NO_CONTEXT;
  870. return gss_write_null_verf(rqstp);
  871. }
  872. rc = gss_write_verf(rqstp, rsci->mechctx, GSS_SEQ_WIN);
  873. cache_put(&rsci->h, cd);
  874. return rc;
  875. }
  876. static inline int
  877. gss_read_common_verf(struct rpc_gss_wire_cred *gc,
  878. struct kvec *argv, __be32 *authp,
  879. struct xdr_netobj *in_handle)
  880. {
  881. /* Read the verifier; should be NULL: */
  882. *authp = rpc_autherr_badverf;
  883. if (argv->iov_len < 2 * 4)
  884. return SVC_DENIED;
  885. if (svc_getnl(argv) != RPC_AUTH_NULL)
  886. return SVC_DENIED;
  887. if (svc_getnl(argv) != 0)
  888. return SVC_DENIED;
  889. /* Martial context handle and token for upcall: */
  890. *authp = rpc_autherr_badcred;
  891. if (gc->gc_proc == RPC_GSS_PROC_INIT && gc->gc_ctx.len != 0)
  892. return SVC_DENIED;
  893. if (dup_netobj(in_handle, &gc->gc_ctx))
  894. return SVC_CLOSE;
  895. *authp = rpc_autherr_badverf;
  896. return 0;
  897. }
  898. static inline int
  899. gss_read_verf(struct rpc_gss_wire_cred *gc,
  900. struct kvec *argv, __be32 *authp,
  901. struct xdr_netobj *in_handle,
  902. struct xdr_netobj *in_token)
  903. {
  904. struct xdr_netobj tmpobj;
  905. int res;
  906. res = gss_read_common_verf(gc, argv, authp, in_handle);
  907. if (res)
  908. return res;
  909. if (svc_safe_getnetobj(argv, &tmpobj)) {
  910. kfree(in_handle->data);
  911. return SVC_DENIED;
  912. }
  913. if (dup_netobj(in_token, &tmpobj)) {
  914. kfree(in_handle->data);
  915. return SVC_CLOSE;
  916. }
  917. return 0;
  918. }
  919. /* Ok this is really heavily depending on a set of semantics in
  920. * how rqstp is set up by svc_recv and pages laid down by the
  921. * server when reading a request. We are basically guaranteed that
  922. * the token lays all down linearly across a set of pages, starting
  923. * at iov_base in rq_arg.head[0] which happens to be the first of a
  924. * set of pages stored in rq_pages[].
  925. * rq_arg.head[0].iov_base will provide us the page_base to pass
  926. * to the upcall.
  927. */
  928. static inline int
  929. gss_read_proxy_verf(struct svc_rqst *rqstp,
  930. struct rpc_gss_wire_cred *gc, __be32 *authp,
  931. struct xdr_netobj *in_handle,
  932. struct gssp_in_token *in_token)
  933. {
  934. struct kvec *argv = &rqstp->rq_arg.head[0];
  935. u32 inlen;
  936. int res;
  937. res = gss_read_common_verf(gc, argv, authp, in_handle);
  938. if (res)
  939. return res;
  940. inlen = svc_getnl(argv);
  941. if (inlen > (argv->iov_len + rqstp->rq_arg.page_len))
  942. return SVC_DENIED;
  943. in_token->pages = rqstp->rq_pages;
  944. in_token->page_base = (ulong)argv->iov_base & ~PAGE_MASK;
  945. in_token->page_len = inlen;
  946. return 0;
  947. }
  948. static inline int
  949. gss_write_resv(struct kvec *resv, size_t size_limit,
  950. struct xdr_netobj *out_handle, struct xdr_netobj *out_token,
  951. int major_status, int minor_status)
  952. {
  953. if (resv->iov_len + 4 > size_limit)
  954. return -1;
  955. svc_putnl(resv, RPC_SUCCESS);
  956. if (svc_safe_putnetobj(resv, out_handle))
  957. return -1;
  958. if (resv->iov_len + 3 * 4 > size_limit)
  959. return -1;
  960. svc_putnl(resv, major_status);
  961. svc_putnl(resv, minor_status);
  962. svc_putnl(resv, GSS_SEQ_WIN);
  963. if (svc_safe_putnetobj(resv, out_token))
  964. return -1;
  965. return 0;
  966. }
  967. /*
  968. * Having read the cred already and found we're in the context
  969. * initiation case, read the verifier and initiate (or check the results
  970. * of) upcalls to userspace for help with context initiation. If
  971. * the upcall results are available, write the verifier and result.
  972. * Otherwise, drop the request pending an answer to the upcall.
  973. */
  974. static int svcauth_gss_legacy_init(struct svc_rqst *rqstp,
  975. struct rpc_gss_wire_cred *gc, __be32 *authp)
  976. {
  977. struct kvec *argv = &rqstp->rq_arg.head[0];
  978. struct kvec *resv = &rqstp->rq_res.head[0];
  979. struct rsi *rsip, rsikey;
  980. int ret;
  981. struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net, sunrpc_net_id);
  982. memset(&rsikey, 0, sizeof(rsikey));
  983. ret = gss_read_verf(gc, argv, authp,
  984. &rsikey.in_handle, &rsikey.in_token);
  985. if (ret)
  986. return ret;
  987. /* Perform upcall, or find upcall result: */
  988. rsip = rsi_lookup(sn->rsi_cache, &rsikey);
  989. rsi_free(&rsikey);
  990. if (!rsip)
  991. return SVC_CLOSE;
  992. if (cache_check(sn->rsi_cache, &rsip->h, &rqstp->rq_chandle) < 0)
  993. /* No upcall result: */
  994. return SVC_CLOSE;
  995. ret = SVC_CLOSE;
  996. /* Got an answer to the upcall; use it: */
  997. if (gss_write_init_verf(sn->rsc_cache, rqstp,
  998. &rsip->out_handle, &rsip->major_status))
  999. goto out;
  1000. if (gss_write_resv(resv, PAGE_SIZE,
  1001. &rsip->out_handle, &rsip->out_token,
  1002. rsip->major_status, rsip->minor_status))
  1003. goto out;
  1004. ret = SVC_COMPLETE;
  1005. out:
  1006. cache_put(&rsip->h, sn->rsi_cache);
  1007. return ret;
  1008. }
  1009. static int gss_proxy_save_rsc(struct cache_detail *cd,
  1010. struct gssp_upcall_data *ud,
  1011. uint64_t *handle)
  1012. {
  1013. struct rsc rsci, *rscp = NULL;
  1014. static atomic64_t ctxhctr;
  1015. long long ctxh;
  1016. struct gss_api_mech *gm = NULL;
  1017. time_t expiry;
  1018. int status = -EINVAL;
  1019. memset(&rsci, 0, sizeof(rsci));
  1020. /* context handle */
  1021. status = -ENOMEM;
  1022. /* the handle needs to be just a unique id,
  1023. * use a static counter */
  1024. ctxh = atomic64_inc_return(&ctxhctr);
  1025. /* make a copy for the caller */
  1026. *handle = ctxh;
  1027. /* make a copy for the rsc cache */
  1028. if (dup_to_netobj(&rsci.handle, (char *)handle, sizeof(uint64_t)))
  1029. goto out;
  1030. rscp = rsc_lookup(cd, &rsci);
  1031. if (!rscp)
  1032. goto out;
  1033. /* creds */
  1034. if (!ud->found_creds) {
  1035. /* userspace seem buggy, we should always get at least a
  1036. * mapping to nobody */
  1037. dprintk("RPC: No creds found!\n");
  1038. goto out;
  1039. } else {
  1040. /* steal creds */
  1041. rsci.cred = ud->creds;
  1042. memset(&ud->creds, 0, sizeof(struct svc_cred));
  1043. status = -EOPNOTSUPP;
  1044. /* get mech handle from OID */
  1045. gm = gss_mech_get_by_OID(&ud->mech_oid);
  1046. if (!gm)
  1047. goto out;
  1048. rsci.cred.cr_gss_mech = gm;
  1049. status = -EINVAL;
  1050. /* mech-specific data: */
  1051. status = gss_import_sec_context(ud->out_handle.data,
  1052. ud->out_handle.len,
  1053. gm, &rsci.mechctx,
  1054. &expiry, GFP_KERNEL);
  1055. if (status)
  1056. goto out;
  1057. }
  1058. rsci.h.expiry_time = expiry;
  1059. rscp = rsc_update(cd, &rsci, rscp);
  1060. status = 0;
  1061. out:
  1062. rsc_free(&rsci);
  1063. if (rscp)
  1064. cache_put(&rscp->h, cd);
  1065. else
  1066. status = -ENOMEM;
  1067. return status;
  1068. }
  1069. static int svcauth_gss_proxy_init(struct svc_rqst *rqstp,
  1070. struct rpc_gss_wire_cred *gc, __be32 *authp)
  1071. {
  1072. struct kvec *resv = &rqstp->rq_res.head[0];
  1073. struct xdr_netobj cli_handle;
  1074. struct gssp_upcall_data ud;
  1075. uint64_t handle;
  1076. int status;
  1077. int ret;
  1078. struct net *net = rqstp->rq_xprt->xpt_net;
  1079. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1080. memset(&ud, 0, sizeof(ud));
  1081. ret = gss_read_proxy_verf(rqstp, gc, authp,
  1082. &ud.in_handle, &ud.in_token);
  1083. if (ret)
  1084. return ret;
  1085. ret = SVC_CLOSE;
  1086. /* Perform synchronous upcall to gss-proxy */
  1087. status = gssp_accept_sec_context_upcall(net, &ud);
  1088. if (status)
  1089. goto out;
  1090. dprintk("RPC: svcauth_gss: gss major status = %d "
  1091. "minor status = %d\n",
  1092. ud.major_status, ud.minor_status);
  1093. switch (ud.major_status) {
  1094. case GSS_S_CONTINUE_NEEDED:
  1095. cli_handle = ud.out_handle;
  1096. break;
  1097. case GSS_S_COMPLETE:
  1098. status = gss_proxy_save_rsc(sn->rsc_cache, &ud, &handle);
  1099. if (status)
  1100. goto out;
  1101. cli_handle.data = (u8 *)&handle;
  1102. cli_handle.len = sizeof(handle);
  1103. break;
  1104. default:
  1105. ret = SVC_CLOSE;
  1106. goto out;
  1107. }
  1108. /* Got an answer to the upcall; use it: */
  1109. if (gss_write_init_verf(sn->rsc_cache, rqstp,
  1110. &cli_handle, &ud.major_status))
  1111. goto out;
  1112. if (gss_write_resv(resv, PAGE_SIZE,
  1113. &cli_handle, &ud.out_token,
  1114. ud.major_status, ud.minor_status))
  1115. goto out;
  1116. ret = SVC_COMPLETE;
  1117. out:
  1118. gssp_free_upcall_data(&ud);
  1119. return ret;
  1120. }
  1121. /*
  1122. * Try to set the sn->use_gss_proxy variable to a new value. We only allow
  1123. * it to be changed if it's currently undefined (-1). If it's any other value
  1124. * then return -EBUSY unless the type wouldn't have changed anyway.
  1125. */
  1126. static int set_gss_proxy(struct net *net, int type)
  1127. {
  1128. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1129. int ret;
  1130. WARN_ON_ONCE(type != 0 && type != 1);
  1131. ret = cmpxchg(&sn->use_gss_proxy, -1, type);
  1132. if (ret != -1 && ret != type)
  1133. return -EBUSY;
  1134. return 0;
  1135. }
  1136. static bool use_gss_proxy(struct net *net)
  1137. {
  1138. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1139. /* If use_gss_proxy is still undefined, then try to disable it */
  1140. if (sn->use_gss_proxy == -1)
  1141. set_gss_proxy(net, 0);
  1142. return sn->use_gss_proxy;
  1143. }
  1144. #ifdef CONFIG_PROC_FS
  1145. static ssize_t write_gssp(struct file *file, const char __user *buf,
  1146. size_t count, loff_t *ppos)
  1147. {
  1148. struct net *net = PDE_DATA(file_inode(file));
  1149. char tbuf[20];
  1150. unsigned long i;
  1151. int res;
  1152. if (*ppos || count > sizeof(tbuf)-1)
  1153. return -EINVAL;
  1154. if (copy_from_user(tbuf, buf, count))
  1155. return -EFAULT;
  1156. tbuf[count] = 0;
  1157. res = kstrtoul(tbuf, 0, &i);
  1158. if (res)
  1159. return res;
  1160. if (i != 1)
  1161. return -EINVAL;
  1162. res = set_gssp_clnt(net);
  1163. if (res)
  1164. return res;
  1165. res = set_gss_proxy(net, 1);
  1166. if (res)
  1167. return res;
  1168. return count;
  1169. }
  1170. static ssize_t read_gssp(struct file *file, char __user *buf,
  1171. size_t count, loff_t *ppos)
  1172. {
  1173. struct net *net = PDE_DATA(file_inode(file));
  1174. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1175. unsigned long p = *ppos;
  1176. char tbuf[10];
  1177. size_t len;
  1178. snprintf(tbuf, sizeof(tbuf), "%d\n", sn->use_gss_proxy);
  1179. len = strlen(tbuf);
  1180. if (p >= len)
  1181. return 0;
  1182. len -= p;
  1183. if (len > count)
  1184. len = count;
  1185. if (copy_to_user(buf, (void *)(tbuf+p), len))
  1186. return -EFAULT;
  1187. *ppos += len;
  1188. return len;
  1189. }
  1190. static const struct file_operations use_gss_proxy_ops = {
  1191. .open = nonseekable_open,
  1192. .write = write_gssp,
  1193. .read = read_gssp,
  1194. };
  1195. static int create_use_gss_proxy_proc_entry(struct net *net)
  1196. {
  1197. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1198. struct proc_dir_entry **p = &sn->use_gssp_proc;
  1199. sn->use_gss_proxy = -1;
  1200. *p = proc_create_data("use-gss-proxy", S_IFREG|S_IRUSR|S_IWUSR,
  1201. sn->proc_net_rpc,
  1202. &use_gss_proxy_ops, net);
  1203. if (!*p)
  1204. return -ENOMEM;
  1205. init_gssp_clnt(sn);
  1206. return 0;
  1207. }
  1208. static void destroy_use_gss_proxy_proc_entry(struct net *net)
  1209. {
  1210. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1211. if (sn->use_gssp_proc) {
  1212. remove_proc_entry("use-gss-proxy", sn->proc_net_rpc);
  1213. clear_gssp_clnt(sn);
  1214. }
  1215. }
  1216. #else /* CONFIG_PROC_FS */
  1217. static int create_use_gss_proxy_proc_entry(struct net *net)
  1218. {
  1219. return 0;
  1220. }
  1221. static void destroy_use_gss_proxy_proc_entry(struct net *net) {}
  1222. #endif /* CONFIG_PROC_FS */
  1223. /*
  1224. * Accept an rpcsec packet.
  1225. * If context establishment, punt to user space
  1226. * If data exchange, verify/decrypt
  1227. * If context destruction, handle here
  1228. * In the context establishment and destruction case we encode
  1229. * response here and return SVC_COMPLETE.
  1230. */
  1231. static int
  1232. svcauth_gss_accept(struct svc_rqst *rqstp, __be32 *authp)
  1233. {
  1234. struct kvec *argv = &rqstp->rq_arg.head[0];
  1235. struct kvec *resv = &rqstp->rq_res.head[0];
  1236. u32 crlen;
  1237. struct gss_svc_data *svcdata = rqstp->rq_auth_data;
  1238. struct rpc_gss_wire_cred *gc;
  1239. struct rsc *rsci = NULL;
  1240. __be32 *rpcstart;
  1241. __be32 *reject_stat = resv->iov_base + resv->iov_len;
  1242. int ret;
  1243. struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net, sunrpc_net_id);
  1244. dprintk("RPC: svcauth_gss: argv->iov_len = %zd\n",
  1245. argv->iov_len);
  1246. *authp = rpc_autherr_badcred;
  1247. if (!svcdata)
  1248. svcdata = kmalloc(sizeof(*svcdata), GFP_KERNEL);
  1249. if (!svcdata)
  1250. goto auth_err;
  1251. rqstp->rq_auth_data = svcdata;
  1252. svcdata->verf_start = NULL;
  1253. svcdata->rsci = NULL;
  1254. gc = &svcdata->clcred;
  1255. /* start of rpc packet is 7 u32's back from here:
  1256. * xid direction rpcversion prog vers proc flavour
  1257. */
  1258. rpcstart = argv->iov_base;
  1259. rpcstart -= 7;
  1260. /* credential is:
  1261. * version(==1), proc(0,1,2,3), seq, service (1,2,3), handle
  1262. * at least 5 u32s, and is preceded by length, so that makes 6.
  1263. */
  1264. if (argv->iov_len < 5 * 4)
  1265. goto auth_err;
  1266. crlen = svc_getnl(argv);
  1267. if (svc_getnl(argv) != RPC_GSS_VERSION)
  1268. goto auth_err;
  1269. gc->gc_proc = svc_getnl(argv);
  1270. gc->gc_seq = svc_getnl(argv);
  1271. gc->gc_svc = svc_getnl(argv);
  1272. if (svc_safe_getnetobj(argv, &gc->gc_ctx))
  1273. goto auth_err;
  1274. if (crlen != round_up_to_quad(gc->gc_ctx.len) + 5 * 4)
  1275. goto auth_err;
  1276. if ((gc->gc_proc != RPC_GSS_PROC_DATA) && (rqstp->rq_proc != 0))
  1277. goto auth_err;
  1278. *authp = rpc_autherr_badverf;
  1279. switch (gc->gc_proc) {
  1280. case RPC_GSS_PROC_INIT:
  1281. case RPC_GSS_PROC_CONTINUE_INIT:
  1282. if (use_gss_proxy(SVC_NET(rqstp)))
  1283. return svcauth_gss_proxy_init(rqstp, gc, authp);
  1284. else
  1285. return svcauth_gss_legacy_init(rqstp, gc, authp);
  1286. case RPC_GSS_PROC_DATA:
  1287. case RPC_GSS_PROC_DESTROY:
  1288. /* Look up the context, and check the verifier: */
  1289. *authp = rpcsec_gsserr_credproblem;
  1290. rsci = gss_svc_searchbyctx(sn->rsc_cache, &gc->gc_ctx);
  1291. if (!rsci)
  1292. goto auth_err;
  1293. switch (gss_verify_header(rqstp, rsci, rpcstart, gc, authp)) {
  1294. case SVC_OK:
  1295. break;
  1296. case SVC_DENIED:
  1297. goto auth_err;
  1298. case SVC_DROP:
  1299. goto drop;
  1300. }
  1301. break;
  1302. default:
  1303. *authp = rpc_autherr_rejectedcred;
  1304. goto auth_err;
  1305. }
  1306. /* now act upon the command: */
  1307. switch (gc->gc_proc) {
  1308. case RPC_GSS_PROC_DESTROY:
  1309. if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
  1310. goto auth_err;
  1311. rsci->h.expiry_time = seconds_since_boot();
  1312. set_bit(CACHE_NEGATIVE, &rsci->h.flags);
  1313. if (resv->iov_len + 4 > PAGE_SIZE)
  1314. goto drop;
  1315. svc_putnl(resv, RPC_SUCCESS);
  1316. goto complete;
  1317. case RPC_GSS_PROC_DATA:
  1318. *authp = rpcsec_gsserr_ctxproblem;
  1319. svcdata->verf_start = resv->iov_base + resv->iov_len;
  1320. if (gss_write_verf(rqstp, rsci->mechctx, gc->gc_seq))
  1321. goto auth_err;
  1322. rqstp->rq_cred = rsci->cred;
  1323. get_group_info(rsci->cred.cr_group_info);
  1324. *authp = rpc_autherr_badcred;
  1325. switch (gc->gc_svc) {
  1326. case RPC_GSS_SVC_NONE:
  1327. break;
  1328. case RPC_GSS_SVC_INTEGRITY:
  1329. /* placeholders for length and seq. number: */
  1330. svc_putnl(resv, 0);
  1331. svc_putnl(resv, 0);
  1332. if (unwrap_integ_data(rqstp, &rqstp->rq_arg,
  1333. gc->gc_seq, rsci->mechctx))
  1334. goto garbage_args;
  1335. rqstp->rq_auth_slack = RPC_MAX_AUTH_SIZE;
  1336. break;
  1337. case RPC_GSS_SVC_PRIVACY:
  1338. /* placeholders for length and seq. number: */
  1339. svc_putnl(resv, 0);
  1340. svc_putnl(resv, 0);
  1341. if (unwrap_priv_data(rqstp, &rqstp->rq_arg,
  1342. gc->gc_seq, rsci->mechctx))
  1343. goto garbage_args;
  1344. rqstp->rq_auth_slack = RPC_MAX_AUTH_SIZE * 2;
  1345. break;
  1346. default:
  1347. goto auth_err;
  1348. }
  1349. svcdata->rsci = rsci;
  1350. cache_get(&rsci->h);
  1351. rqstp->rq_cred.cr_flavor = gss_svc_to_pseudoflavor(
  1352. rsci->mechctx->mech_type,
  1353. GSS_C_QOP_DEFAULT,
  1354. gc->gc_svc);
  1355. ret = SVC_OK;
  1356. goto out;
  1357. }
  1358. garbage_args:
  1359. ret = SVC_GARBAGE;
  1360. goto out;
  1361. auth_err:
  1362. /* Restore write pointer to its original value: */
  1363. xdr_ressize_check(rqstp, reject_stat);
  1364. ret = SVC_DENIED;
  1365. goto out;
  1366. complete:
  1367. ret = SVC_COMPLETE;
  1368. goto out;
  1369. drop:
  1370. ret = SVC_CLOSE;
  1371. out:
  1372. if (rsci)
  1373. cache_put(&rsci->h, sn->rsc_cache);
  1374. return ret;
  1375. }
  1376. static __be32 *
  1377. svcauth_gss_prepare_to_wrap(struct xdr_buf *resbuf, struct gss_svc_data *gsd)
  1378. {
  1379. __be32 *p;
  1380. u32 verf_len;
  1381. p = gsd->verf_start;
  1382. gsd->verf_start = NULL;
  1383. /* If the reply stat is nonzero, don't wrap: */
  1384. if (*(p-1) != rpc_success)
  1385. return NULL;
  1386. /* Skip the verifier: */
  1387. p += 1;
  1388. verf_len = ntohl(*p++);
  1389. p += XDR_QUADLEN(verf_len);
  1390. /* move accept_stat to right place: */
  1391. memcpy(p, p + 2, 4);
  1392. /* Also don't wrap if the accept stat is nonzero: */
  1393. if (*p != rpc_success) {
  1394. resbuf->head[0].iov_len -= 2 * 4;
  1395. return NULL;
  1396. }
  1397. p++;
  1398. return p;
  1399. }
  1400. static inline int
  1401. svcauth_gss_wrap_resp_integ(struct svc_rqst *rqstp)
  1402. {
  1403. struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
  1404. struct rpc_gss_wire_cred *gc = &gsd->clcred;
  1405. struct xdr_buf *resbuf = &rqstp->rq_res;
  1406. struct xdr_buf integ_buf;
  1407. struct xdr_netobj mic;
  1408. struct kvec *resv;
  1409. __be32 *p;
  1410. int integ_offset, integ_len;
  1411. int stat = -EINVAL;
  1412. p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
  1413. if (p == NULL)
  1414. goto out;
  1415. integ_offset = (u8 *)(p + 1) - (u8 *)resbuf->head[0].iov_base;
  1416. integ_len = resbuf->len - integ_offset;
  1417. BUG_ON(integ_len % 4);
  1418. *p++ = htonl(integ_len);
  1419. *p++ = htonl(gc->gc_seq);
  1420. if (xdr_buf_subsegment(resbuf, &integ_buf, integ_offset, integ_len))
  1421. BUG();
  1422. if (resbuf->tail[0].iov_base == NULL) {
  1423. if (resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE > PAGE_SIZE)
  1424. goto out_err;
  1425. resbuf->tail[0].iov_base = resbuf->head[0].iov_base
  1426. + resbuf->head[0].iov_len;
  1427. resbuf->tail[0].iov_len = 0;
  1428. }
  1429. resv = &resbuf->tail[0];
  1430. mic.data = (u8 *)resv->iov_base + resv->iov_len + 4;
  1431. if (gss_get_mic(gsd->rsci->mechctx, &integ_buf, &mic))
  1432. goto out_err;
  1433. svc_putnl(resv, mic.len);
  1434. memset(mic.data + mic.len, 0,
  1435. round_up_to_quad(mic.len) - mic.len);
  1436. resv->iov_len += XDR_QUADLEN(mic.len) << 2;
  1437. /* not strictly required: */
  1438. resbuf->len += XDR_QUADLEN(mic.len) << 2;
  1439. BUG_ON(resv->iov_len > PAGE_SIZE);
  1440. out:
  1441. stat = 0;
  1442. out_err:
  1443. return stat;
  1444. }
  1445. static inline int
  1446. svcauth_gss_wrap_resp_priv(struct svc_rqst *rqstp)
  1447. {
  1448. struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
  1449. struct rpc_gss_wire_cred *gc = &gsd->clcred;
  1450. struct xdr_buf *resbuf = &rqstp->rq_res;
  1451. struct page **inpages = NULL;
  1452. __be32 *p, *len;
  1453. int offset;
  1454. int pad;
  1455. p = svcauth_gss_prepare_to_wrap(resbuf, gsd);
  1456. if (p == NULL)
  1457. return 0;
  1458. len = p++;
  1459. offset = (u8 *)p - (u8 *)resbuf->head[0].iov_base;
  1460. *p++ = htonl(gc->gc_seq);
  1461. inpages = resbuf->pages;
  1462. /* XXX: Would be better to write some xdr helper functions for
  1463. * nfs{2,3,4}xdr.c that place the data right, instead of copying: */
  1464. /*
  1465. * If there is currently tail data, make sure there is
  1466. * room for the head, tail, and 2 * RPC_MAX_AUTH_SIZE in
  1467. * the page, and move the current tail data such that
  1468. * there is RPC_MAX_AUTH_SIZE slack space available in
  1469. * both the head and tail.
  1470. */
  1471. if (resbuf->tail[0].iov_base) {
  1472. BUG_ON(resbuf->tail[0].iov_base >= resbuf->head[0].iov_base
  1473. + PAGE_SIZE);
  1474. BUG_ON(resbuf->tail[0].iov_base < resbuf->head[0].iov_base);
  1475. if (resbuf->tail[0].iov_len + resbuf->head[0].iov_len
  1476. + 2 * RPC_MAX_AUTH_SIZE > PAGE_SIZE)
  1477. return -ENOMEM;
  1478. memmove(resbuf->tail[0].iov_base + RPC_MAX_AUTH_SIZE,
  1479. resbuf->tail[0].iov_base,
  1480. resbuf->tail[0].iov_len);
  1481. resbuf->tail[0].iov_base += RPC_MAX_AUTH_SIZE;
  1482. }
  1483. /*
  1484. * If there is no current tail data, make sure there is
  1485. * room for the head data, and 2 * RPC_MAX_AUTH_SIZE in the
  1486. * allotted page, and set up tail information such that there
  1487. * is RPC_MAX_AUTH_SIZE slack space available in both the
  1488. * head and tail.
  1489. */
  1490. if (resbuf->tail[0].iov_base == NULL) {
  1491. if (resbuf->head[0].iov_len + 2*RPC_MAX_AUTH_SIZE > PAGE_SIZE)
  1492. return -ENOMEM;
  1493. resbuf->tail[0].iov_base = resbuf->head[0].iov_base
  1494. + resbuf->head[0].iov_len + RPC_MAX_AUTH_SIZE;
  1495. resbuf->tail[0].iov_len = 0;
  1496. }
  1497. if (gss_wrap(gsd->rsci->mechctx, offset, resbuf, inpages))
  1498. return -ENOMEM;
  1499. *len = htonl(resbuf->len - offset);
  1500. pad = 3 - ((resbuf->len - offset - 1)&3);
  1501. p = (__be32 *)(resbuf->tail[0].iov_base + resbuf->tail[0].iov_len);
  1502. memset(p, 0, pad);
  1503. resbuf->tail[0].iov_len += pad;
  1504. resbuf->len += pad;
  1505. return 0;
  1506. }
  1507. static int
  1508. svcauth_gss_release(struct svc_rqst *rqstp)
  1509. {
  1510. struct gss_svc_data *gsd = (struct gss_svc_data *)rqstp->rq_auth_data;
  1511. struct rpc_gss_wire_cred *gc = &gsd->clcred;
  1512. struct xdr_buf *resbuf = &rqstp->rq_res;
  1513. int stat = -EINVAL;
  1514. struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net, sunrpc_net_id);
  1515. if (gc->gc_proc != RPC_GSS_PROC_DATA)
  1516. goto out;
  1517. /* Release can be called twice, but we only wrap once. */
  1518. if (gsd->verf_start == NULL)
  1519. goto out;
  1520. /* normally not set till svc_send, but we need it here: */
  1521. /* XXX: what for? Do we mess it up the moment we call svc_putu32
  1522. * or whatever? */
  1523. resbuf->len = total_buf_len(resbuf);
  1524. switch (gc->gc_svc) {
  1525. case RPC_GSS_SVC_NONE:
  1526. break;
  1527. case RPC_GSS_SVC_INTEGRITY:
  1528. stat = svcauth_gss_wrap_resp_integ(rqstp);
  1529. if (stat)
  1530. goto out_err;
  1531. break;
  1532. case RPC_GSS_SVC_PRIVACY:
  1533. stat = svcauth_gss_wrap_resp_priv(rqstp);
  1534. if (stat)
  1535. goto out_err;
  1536. break;
  1537. /*
  1538. * For any other gc_svc value, svcauth_gss_accept() already set
  1539. * the auth_error appropriately; just fall through:
  1540. */
  1541. }
  1542. out:
  1543. stat = 0;
  1544. out_err:
  1545. if (rqstp->rq_client)
  1546. auth_domain_put(rqstp->rq_client);
  1547. rqstp->rq_client = NULL;
  1548. if (rqstp->rq_gssclient)
  1549. auth_domain_put(rqstp->rq_gssclient);
  1550. rqstp->rq_gssclient = NULL;
  1551. if (rqstp->rq_cred.cr_group_info)
  1552. put_group_info(rqstp->rq_cred.cr_group_info);
  1553. rqstp->rq_cred.cr_group_info = NULL;
  1554. if (gsd->rsci)
  1555. cache_put(&gsd->rsci->h, sn->rsc_cache);
  1556. gsd->rsci = NULL;
  1557. return stat;
  1558. }
  1559. static void
  1560. svcauth_gss_domain_release(struct auth_domain *dom)
  1561. {
  1562. struct gss_domain *gd = container_of(dom, struct gss_domain, h);
  1563. kfree(dom->name);
  1564. kfree(gd);
  1565. }
  1566. static struct auth_ops svcauthops_gss = {
  1567. .name = "rpcsec_gss",
  1568. .owner = THIS_MODULE,
  1569. .flavour = RPC_AUTH_GSS,
  1570. .accept = svcauth_gss_accept,
  1571. .release = svcauth_gss_release,
  1572. .domain_release = svcauth_gss_domain_release,
  1573. .set_client = svcauth_gss_set_client,
  1574. };
  1575. static int rsi_cache_create_net(struct net *net)
  1576. {
  1577. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1578. struct cache_detail *cd;
  1579. int err;
  1580. cd = cache_create_net(&rsi_cache_template, net);
  1581. if (IS_ERR(cd))
  1582. return PTR_ERR(cd);
  1583. err = cache_register_net(cd, net);
  1584. if (err) {
  1585. cache_destroy_net(cd, net);
  1586. return err;
  1587. }
  1588. sn->rsi_cache = cd;
  1589. return 0;
  1590. }
  1591. static void rsi_cache_destroy_net(struct net *net)
  1592. {
  1593. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1594. struct cache_detail *cd = sn->rsi_cache;
  1595. sn->rsi_cache = NULL;
  1596. cache_purge(cd);
  1597. cache_unregister_net(cd, net);
  1598. cache_destroy_net(cd, net);
  1599. }
  1600. static int rsc_cache_create_net(struct net *net)
  1601. {
  1602. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1603. struct cache_detail *cd;
  1604. int err;
  1605. cd = cache_create_net(&rsc_cache_template, net);
  1606. if (IS_ERR(cd))
  1607. return PTR_ERR(cd);
  1608. err = cache_register_net(cd, net);
  1609. if (err) {
  1610. cache_destroy_net(cd, net);
  1611. return err;
  1612. }
  1613. sn->rsc_cache = cd;
  1614. return 0;
  1615. }
  1616. static void rsc_cache_destroy_net(struct net *net)
  1617. {
  1618. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  1619. struct cache_detail *cd = sn->rsc_cache;
  1620. sn->rsc_cache = NULL;
  1621. cache_purge(cd);
  1622. cache_unregister_net(cd, net);
  1623. cache_destroy_net(cd, net);
  1624. }
  1625. int
  1626. gss_svc_init_net(struct net *net)
  1627. {
  1628. int rv;
  1629. rv = rsc_cache_create_net(net);
  1630. if (rv)
  1631. return rv;
  1632. rv = rsi_cache_create_net(net);
  1633. if (rv)
  1634. goto out1;
  1635. rv = create_use_gss_proxy_proc_entry(net);
  1636. if (rv)
  1637. goto out2;
  1638. return 0;
  1639. out2:
  1640. destroy_use_gss_proxy_proc_entry(net);
  1641. out1:
  1642. rsc_cache_destroy_net(net);
  1643. return rv;
  1644. }
  1645. void
  1646. gss_svc_shutdown_net(struct net *net)
  1647. {
  1648. destroy_use_gss_proxy_proc_entry(net);
  1649. rsi_cache_destroy_net(net);
  1650. rsc_cache_destroy_net(net);
  1651. }
  1652. int
  1653. gss_svc_init(void)
  1654. {
  1655. return svc_auth_register(RPC_AUTH_GSS, &svcauthops_gss);
  1656. }
  1657. void
  1658. gss_svc_shutdown(void)
  1659. {
  1660. svc_auth_unregister(RPC_AUTH_GSS);
  1661. }