xfrm_user.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. /* xfrm_user.c: User interface to configure xfrm engine.
  2. *
  3. * Copyright (C) 2002 David S. Miller (davem@redhat.com)
  4. *
  5. * Changes:
  6. * Mitsuru KANDA @USAGI
  7. * Kazunori MIYAZAWA @USAGI
  8. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  9. * IPv6 support
  10. *
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/slab.h>
  16. #include <linux/socket.h>
  17. #include <linux/string.h>
  18. #include <linux/net.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/rtnetlink.h>
  21. #include <linux/pfkeyv2.h>
  22. #include <linux/ipsec.h>
  23. #include <linux/init.h>
  24. #include <linux/security.h>
  25. #include <net/sock.h>
  26. #include <net/xfrm.h>
  27. #include <net/netlink.h>
  28. #include <asm/uaccess.h>
  29. struct sock *xfrm_nl;
  30. static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
  31. {
  32. struct rtattr *rt = xfrma[type - 1];
  33. struct xfrm_algo *algp;
  34. int len;
  35. if (!rt)
  36. return 0;
  37. len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
  38. if (len < 0)
  39. return -EINVAL;
  40. algp = RTA_DATA(rt);
  41. len -= (algp->alg_key_len + 7U) / 8;
  42. if (len < 0)
  43. return -EINVAL;
  44. switch (type) {
  45. case XFRMA_ALG_AUTH:
  46. if (!algp->alg_key_len &&
  47. strcmp(algp->alg_name, "digest_null") != 0)
  48. return -EINVAL;
  49. break;
  50. case XFRMA_ALG_CRYPT:
  51. if (!algp->alg_key_len &&
  52. strcmp(algp->alg_name, "cipher_null") != 0)
  53. return -EINVAL;
  54. break;
  55. case XFRMA_ALG_COMP:
  56. /* Zero length keys are legal. */
  57. break;
  58. default:
  59. return -EINVAL;
  60. };
  61. algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
  62. return 0;
  63. }
  64. static int verify_encap_tmpl(struct rtattr **xfrma)
  65. {
  66. struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
  67. struct xfrm_encap_tmpl *encap;
  68. if (!rt)
  69. return 0;
  70. if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
  71. return -EINVAL;
  72. return 0;
  73. }
  74. static inline int verify_sec_ctx_len(struct rtattr **xfrma)
  75. {
  76. struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
  77. struct xfrm_user_sec_ctx *uctx;
  78. int len = 0;
  79. if (!rt)
  80. return 0;
  81. if (rt->rta_len < sizeof(*uctx))
  82. return -EINVAL;
  83. uctx = RTA_DATA(rt);
  84. if (uctx->ctx_len > PAGE_SIZE)
  85. return -EINVAL;
  86. len += sizeof(struct xfrm_user_sec_ctx);
  87. len += uctx->ctx_len;
  88. if (uctx->len != len)
  89. return -EINVAL;
  90. return 0;
  91. }
  92. static int verify_newsa_info(struct xfrm_usersa_info *p,
  93. struct rtattr **xfrma)
  94. {
  95. int err;
  96. err = -EINVAL;
  97. switch (p->family) {
  98. case AF_INET:
  99. break;
  100. case AF_INET6:
  101. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  102. break;
  103. #else
  104. err = -EAFNOSUPPORT;
  105. goto out;
  106. #endif
  107. default:
  108. goto out;
  109. };
  110. err = -EINVAL;
  111. switch (p->id.proto) {
  112. case IPPROTO_AH:
  113. if (!xfrma[XFRMA_ALG_AUTH-1] ||
  114. xfrma[XFRMA_ALG_CRYPT-1] ||
  115. xfrma[XFRMA_ALG_COMP-1])
  116. goto out;
  117. break;
  118. case IPPROTO_ESP:
  119. if ((!xfrma[XFRMA_ALG_AUTH-1] &&
  120. !xfrma[XFRMA_ALG_CRYPT-1]) ||
  121. xfrma[XFRMA_ALG_COMP-1])
  122. goto out;
  123. break;
  124. case IPPROTO_COMP:
  125. if (!xfrma[XFRMA_ALG_COMP-1] ||
  126. xfrma[XFRMA_ALG_AUTH-1] ||
  127. xfrma[XFRMA_ALG_CRYPT-1])
  128. goto out;
  129. break;
  130. default:
  131. goto out;
  132. };
  133. if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
  134. goto out;
  135. if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
  136. goto out;
  137. if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
  138. goto out;
  139. if ((err = verify_encap_tmpl(xfrma)))
  140. goto out;
  141. if ((err = verify_sec_ctx_len(xfrma)))
  142. goto out;
  143. err = -EINVAL;
  144. switch (p->mode) {
  145. case 0:
  146. case 1:
  147. break;
  148. default:
  149. goto out;
  150. };
  151. err = 0;
  152. out:
  153. return err;
  154. }
  155. static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
  156. struct xfrm_algo_desc *(*get_byname)(char *, int),
  157. struct rtattr *u_arg)
  158. {
  159. struct rtattr *rta = u_arg;
  160. struct xfrm_algo *p, *ualg;
  161. struct xfrm_algo_desc *algo;
  162. int len;
  163. if (!rta)
  164. return 0;
  165. ualg = RTA_DATA(rta);
  166. algo = get_byname(ualg->alg_name, 1);
  167. if (!algo)
  168. return -ENOSYS;
  169. *props = algo->desc.sadb_alg_id;
  170. len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
  171. p = kmalloc(len, GFP_KERNEL);
  172. if (!p)
  173. return -ENOMEM;
  174. memcpy(p, ualg, len);
  175. *algpp = p;
  176. return 0;
  177. }
  178. static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
  179. {
  180. struct rtattr *rta = u_arg;
  181. struct xfrm_encap_tmpl *p, *uencap;
  182. if (!rta)
  183. return 0;
  184. uencap = RTA_DATA(rta);
  185. p = kmalloc(sizeof(*p), GFP_KERNEL);
  186. if (!p)
  187. return -ENOMEM;
  188. memcpy(p, uencap, sizeof(*p));
  189. *encapp = p;
  190. return 0;
  191. }
  192. static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
  193. {
  194. struct xfrm_sec_ctx *xfrm_ctx = xp->security;
  195. int len = 0;
  196. if (xfrm_ctx) {
  197. len += sizeof(struct xfrm_user_sec_ctx);
  198. len += xfrm_ctx->ctx_len;
  199. }
  200. return len;
  201. }
  202. static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
  203. {
  204. struct xfrm_user_sec_ctx *uctx;
  205. if (!u_arg)
  206. return 0;
  207. uctx = RTA_DATA(u_arg);
  208. return security_xfrm_state_alloc(x, uctx);
  209. }
  210. static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  211. {
  212. memcpy(&x->id, &p->id, sizeof(x->id));
  213. memcpy(&x->sel, &p->sel, sizeof(x->sel));
  214. memcpy(&x->lft, &p->lft, sizeof(x->lft));
  215. x->props.mode = p->mode;
  216. x->props.replay_window = p->replay_window;
  217. x->props.reqid = p->reqid;
  218. x->props.family = p->family;
  219. x->props.saddr = p->saddr;
  220. x->props.flags = p->flags;
  221. }
  222. /*
  223. * someday when pfkey also has support, we could have the code
  224. * somehow made shareable and move it to xfrm_state.c - JHS
  225. *
  226. */
  227. static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
  228. {
  229. int err = - EINVAL;
  230. struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
  231. struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
  232. struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
  233. struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
  234. if (rp) {
  235. struct xfrm_replay_state *replay;
  236. if (RTA_PAYLOAD(rp) < sizeof(*replay))
  237. goto error;
  238. replay = RTA_DATA(rp);
  239. memcpy(&x->replay, replay, sizeof(*replay));
  240. memcpy(&x->preplay, replay, sizeof(*replay));
  241. }
  242. if (lt) {
  243. struct xfrm_lifetime_cur *ltime;
  244. if (RTA_PAYLOAD(lt) < sizeof(*ltime))
  245. goto error;
  246. ltime = RTA_DATA(lt);
  247. x->curlft.bytes = ltime->bytes;
  248. x->curlft.packets = ltime->packets;
  249. x->curlft.add_time = ltime->add_time;
  250. x->curlft.use_time = ltime->use_time;
  251. }
  252. if (et) {
  253. if (RTA_PAYLOAD(et) < sizeof(u32))
  254. goto error;
  255. x->replay_maxage = *(u32*)RTA_DATA(et);
  256. }
  257. if (rt) {
  258. if (RTA_PAYLOAD(rt) < sizeof(u32))
  259. goto error;
  260. x->replay_maxdiff = *(u32*)RTA_DATA(rt);
  261. }
  262. return 0;
  263. error:
  264. return err;
  265. }
  266. static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
  267. struct rtattr **xfrma,
  268. int *errp)
  269. {
  270. struct xfrm_state *x = xfrm_state_alloc();
  271. int err = -ENOMEM;
  272. if (!x)
  273. goto error_no_put;
  274. copy_from_user_state(x, p);
  275. if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
  276. xfrm_aalg_get_byname,
  277. xfrma[XFRMA_ALG_AUTH-1])))
  278. goto error;
  279. if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
  280. xfrm_ealg_get_byname,
  281. xfrma[XFRMA_ALG_CRYPT-1])))
  282. goto error;
  283. if ((err = attach_one_algo(&x->calg, &x->props.calgo,
  284. xfrm_calg_get_byname,
  285. xfrma[XFRMA_ALG_COMP-1])))
  286. goto error;
  287. if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
  288. goto error;
  289. err = xfrm_init_state(x);
  290. if (err)
  291. goto error;
  292. if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
  293. goto error;
  294. x->km.seq = p->seq;
  295. x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
  296. /* sysctl_xfrm_aevent_etime is in 100ms units */
  297. x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
  298. x->preplay.bitmap = 0;
  299. x->preplay.seq = x->replay.seq+x->replay_maxdiff;
  300. x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
  301. /* override default values from above */
  302. err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
  303. if (err < 0)
  304. goto error;
  305. return x;
  306. error:
  307. x->km.state = XFRM_STATE_DEAD;
  308. xfrm_state_put(x);
  309. error_no_put:
  310. *errp = err;
  311. return NULL;
  312. }
  313. static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  314. {
  315. struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
  316. struct xfrm_state *x;
  317. int err;
  318. struct km_event c;
  319. err = verify_newsa_info(p, (struct rtattr **)xfrma);
  320. if (err)
  321. return err;
  322. x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
  323. if (!x)
  324. return err;
  325. xfrm_state_hold(x);
  326. if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
  327. err = xfrm_state_add(x);
  328. else
  329. err = xfrm_state_update(x);
  330. if (err < 0) {
  331. x->km.state = XFRM_STATE_DEAD;
  332. __xfrm_state_put(x);
  333. goto out;
  334. }
  335. c.seq = nlh->nlmsg_seq;
  336. c.pid = nlh->nlmsg_pid;
  337. c.event = nlh->nlmsg_type;
  338. km_state_notify(x, &c);
  339. out:
  340. xfrm_state_put(x);
  341. return err;
  342. }
  343. static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  344. {
  345. struct xfrm_state *x;
  346. int err;
  347. struct km_event c;
  348. struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
  349. x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
  350. if (x == NULL)
  351. return -ESRCH;
  352. if (xfrm_state_kern(x)) {
  353. xfrm_state_put(x);
  354. return -EPERM;
  355. }
  356. err = xfrm_state_delete(x);
  357. if (err < 0) {
  358. xfrm_state_put(x);
  359. return err;
  360. }
  361. c.seq = nlh->nlmsg_seq;
  362. c.pid = nlh->nlmsg_pid;
  363. c.event = nlh->nlmsg_type;
  364. km_state_notify(x, &c);
  365. xfrm_state_put(x);
  366. return err;
  367. }
  368. static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  369. {
  370. memcpy(&p->id, &x->id, sizeof(p->id));
  371. memcpy(&p->sel, &x->sel, sizeof(p->sel));
  372. memcpy(&p->lft, &x->lft, sizeof(p->lft));
  373. memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
  374. memcpy(&p->stats, &x->stats, sizeof(p->stats));
  375. p->saddr = x->props.saddr;
  376. p->mode = x->props.mode;
  377. p->replay_window = x->props.replay_window;
  378. p->reqid = x->props.reqid;
  379. p->family = x->props.family;
  380. p->flags = x->props.flags;
  381. p->seq = x->km.seq;
  382. }
  383. struct xfrm_dump_info {
  384. struct sk_buff *in_skb;
  385. struct sk_buff *out_skb;
  386. u32 nlmsg_seq;
  387. u16 nlmsg_flags;
  388. int start_idx;
  389. int this_idx;
  390. };
  391. static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
  392. {
  393. struct xfrm_dump_info *sp = ptr;
  394. struct sk_buff *in_skb = sp->in_skb;
  395. struct sk_buff *skb = sp->out_skb;
  396. struct xfrm_usersa_info *p;
  397. struct nlmsghdr *nlh;
  398. unsigned char *b = skb->tail;
  399. if (sp->this_idx < sp->start_idx)
  400. goto out;
  401. nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
  402. sp->nlmsg_seq,
  403. XFRM_MSG_NEWSA, sizeof(*p));
  404. nlh->nlmsg_flags = sp->nlmsg_flags;
  405. p = NLMSG_DATA(nlh);
  406. copy_to_user_state(x, p);
  407. if (x->aalg)
  408. RTA_PUT(skb, XFRMA_ALG_AUTH,
  409. sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
  410. if (x->ealg)
  411. RTA_PUT(skb, XFRMA_ALG_CRYPT,
  412. sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
  413. if (x->calg)
  414. RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  415. if (x->encap)
  416. RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  417. if (x->security) {
  418. int ctx_size = sizeof(struct xfrm_sec_ctx) +
  419. x->security->ctx_len;
  420. struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
  421. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  422. uctx->exttype = XFRMA_SEC_CTX;
  423. uctx->len = ctx_size;
  424. uctx->ctx_doi = x->security->ctx_doi;
  425. uctx->ctx_alg = x->security->ctx_alg;
  426. uctx->ctx_len = x->security->ctx_len;
  427. memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
  428. }
  429. nlh->nlmsg_len = skb->tail - b;
  430. out:
  431. sp->this_idx++;
  432. return 0;
  433. nlmsg_failure:
  434. rtattr_failure:
  435. skb_trim(skb, b - skb->data);
  436. return -1;
  437. }
  438. static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
  439. {
  440. struct xfrm_dump_info info;
  441. info.in_skb = cb->skb;
  442. info.out_skb = skb;
  443. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  444. info.nlmsg_flags = NLM_F_MULTI;
  445. info.this_idx = 0;
  446. info.start_idx = cb->args[0];
  447. (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
  448. cb->args[0] = info.this_idx;
  449. return skb->len;
  450. }
  451. static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
  452. struct xfrm_state *x, u32 seq)
  453. {
  454. struct xfrm_dump_info info;
  455. struct sk_buff *skb;
  456. skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
  457. if (!skb)
  458. return ERR_PTR(-ENOMEM);
  459. NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
  460. info.in_skb = in_skb;
  461. info.out_skb = skb;
  462. info.nlmsg_seq = seq;
  463. info.nlmsg_flags = 0;
  464. info.this_idx = info.start_idx = 0;
  465. if (dump_one_state(x, 0, &info)) {
  466. kfree_skb(skb);
  467. return NULL;
  468. }
  469. return skb;
  470. }
  471. static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  472. {
  473. struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
  474. struct xfrm_state *x;
  475. struct sk_buff *resp_skb;
  476. int err;
  477. x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
  478. err = -ESRCH;
  479. if (x == NULL)
  480. goto out_noput;
  481. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  482. if (IS_ERR(resp_skb)) {
  483. err = PTR_ERR(resp_skb);
  484. } else {
  485. err = netlink_unicast(xfrm_nl, resp_skb,
  486. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  487. }
  488. xfrm_state_put(x);
  489. out_noput:
  490. return err;
  491. }
  492. static int verify_userspi_info(struct xfrm_userspi_info *p)
  493. {
  494. switch (p->info.id.proto) {
  495. case IPPROTO_AH:
  496. case IPPROTO_ESP:
  497. break;
  498. case IPPROTO_COMP:
  499. /* IPCOMP spi is 16-bits. */
  500. if (p->max >= 0x10000)
  501. return -EINVAL;
  502. break;
  503. default:
  504. return -EINVAL;
  505. };
  506. if (p->min > p->max)
  507. return -EINVAL;
  508. return 0;
  509. }
  510. static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  511. {
  512. struct xfrm_state *x;
  513. struct xfrm_userspi_info *p;
  514. struct sk_buff *resp_skb;
  515. xfrm_address_t *daddr;
  516. int family;
  517. int err;
  518. p = NLMSG_DATA(nlh);
  519. err = verify_userspi_info(p);
  520. if (err)
  521. goto out_noput;
  522. family = p->info.family;
  523. daddr = &p->info.id.daddr;
  524. x = NULL;
  525. if (p->info.seq) {
  526. x = xfrm_find_acq_byseq(p->info.seq);
  527. if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
  528. xfrm_state_put(x);
  529. x = NULL;
  530. }
  531. }
  532. if (!x)
  533. x = xfrm_find_acq(p->info.mode, p->info.reqid,
  534. p->info.id.proto, daddr,
  535. &p->info.saddr, 1,
  536. family);
  537. err = -ENOENT;
  538. if (x == NULL)
  539. goto out_noput;
  540. resp_skb = ERR_PTR(-ENOENT);
  541. spin_lock_bh(&x->lock);
  542. if (x->km.state != XFRM_STATE_DEAD) {
  543. xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
  544. if (x->id.spi)
  545. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  546. }
  547. spin_unlock_bh(&x->lock);
  548. if (IS_ERR(resp_skb)) {
  549. err = PTR_ERR(resp_skb);
  550. goto out;
  551. }
  552. err = netlink_unicast(xfrm_nl, resp_skb,
  553. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  554. out:
  555. xfrm_state_put(x);
  556. out_noput:
  557. return err;
  558. }
  559. static int verify_policy_dir(__u8 dir)
  560. {
  561. switch (dir) {
  562. case XFRM_POLICY_IN:
  563. case XFRM_POLICY_OUT:
  564. case XFRM_POLICY_FWD:
  565. break;
  566. default:
  567. return -EINVAL;
  568. };
  569. return 0;
  570. }
  571. static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
  572. {
  573. switch (p->share) {
  574. case XFRM_SHARE_ANY:
  575. case XFRM_SHARE_SESSION:
  576. case XFRM_SHARE_USER:
  577. case XFRM_SHARE_UNIQUE:
  578. break;
  579. default:
  580. return -EINVAL;
  581. };
  582. switch (p->action) {
  583. case XFRM_POLICY_ALLOW:
  584. case XFRM_POLICY_BLOCK:
  585. break;
  586. default:
  587. return -EINVAL;
  588. };
  589. switch (p->sel.family) {
  590. case AF_INET:
  591. break;
  592. case AF_INET6:
  593. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  594. break;
  595. #else
  596. return -EAFNOSUPPORT;
  597. #endif
  598. default:
  599. return -EINVAL;
  600. };
  601. return verify_policy_dir(p->dir);
  602. }
  603. static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
  604. {
  605. struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
  606. struct xfrm_user_sec_ctx *uctx;
  607. if (!rt)
  608. return 0;
  609. uctx = RTA_DATA(rt);
  610. return security_xfrm_policy_alloc(pol, uctx);
  611. }
  612. static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
  613. int nr)
  614. {
  615. int i;
  616. xp->xfrm_nr = nr;
  617. for (i = 0; i < nr; i++, ut++) {
  618. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  619. memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
  620. memcpy(&t->saddr, &ut->saddr,
  621. sizeof(xfrm_address_t));
  622. t->reqid = ut->reqid;
  623. t->mode = ut->mode;
  624. t->share = ut->share;
  625. t->optional = ut->optional;
  626. t->aalgos = ut->aalgos;
  627. t->ealgos = ut->ealgos;
  628. t->calgos = ut->calgos;
  629. }
  630. }
  631. static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
  632. {
  633. struct rtattr *rt = xfrma[XFRMA_TMPL-1];
  634. struct xfrm_user_tmpl *utmpl;
  635. int nr;
  636. if (!rt) {
  637. pol->xfrm_nr = 0;
  638. } else {
  639. nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
  640. if (nr > XFRM_MAX_DEPTH)
  641. return -EINVAL;
  642. copy_templates(pol, RTA_DATA(rt), nr);
  643. }
  644. return 0;
  645. }
  646. static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
  647. {
  648. xp->priority = p->priority;
  649. xp->index = p->index;
  650. memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
  651. memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
  652. xp->action = p->action;
  653. xp->flags = p->flags;
  654. xp->family = p->sel.family;
  655. /* XXX xp->share = p->share; */
  656. }
  657. static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
  658. {
  659. memcpy(&p->sel, &xp->selector, sizeof(p->sel));
  660. memcpy(&p->lft, &xp->lft, sizeof(p->lft));
  661. memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
  662. p->priority = xp->priority;
  663. p->index = xp->index;
  664. p->sel.family = xp->family;
  665. p->dir = dir;
  666. p->action = xp->action;
  667. p->flags = xp->flags;
  668. p->share = XFRM_SHARE_ANY; /* XXX xp->share */
  669. }
  670. static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
  671. {
  672. struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
  673. int err;
  674. if (!xp) {
  675. *errp = -ENOMEM;
  676. return NULL;
  677. }
  678. copy_from_user_policy(xp, p);
  679. if (!(err = copy_from_user_tmpl(xp, xfrma)))
  680. err = copy_from_user_sec_ctx(xp, xfrma);
  681. if (err) {
  682. *errp = err;
  683. kfree(xp);
  684. xp = NULL;
  685. }
  686. return xp;
  687. }
  688. static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  689. {
  690. struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
  691. struct xfrm_policy *xp;
  692. struct km_event c;
  693. int err;
  694. int excl;
  695. err = verify_newpolicy_info(p);
  696. if (err)
  697. return err;
  698. err = verify_sec_ctx_len((struct rtattr **)xfrma);
  699. if (err)
  700. return err;
  701. xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
  702. if (!xp)
  703. return err;
  704. /* shouldnt excl be based on nlh flags??
  705. * Aha! this is anti-netlink really i.e more pfkey derived
  706. * in netlink excl is a flag and you wouldnt need
  707. * a type XFRM_MSG_UPDPOLICY - JHS */
  708. excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
  709. err = xfrm_policy_insert(p->dir, xp, excl);
  710. if (err) {
  711. security_xfrm_policy_free(xp);
  712. kfree(xp);
  713. return err;
  714. }
  715. c.event = nlh->nlmsg_type;
  716. c.seq = nlh->nlmsg_seq;
  717. c.pid = nlh->nlmsg_pid;
  718. km_policy_notify(xp, p->dir, &c);
  719. xfrm_pol_put(xp);
  720. return 0;
  721. }
  722. static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
  723. {
  724. struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
  725. int i;
  726. if (xp->xfrm_nr == 0)
  727. return 0;
  728. for (i = 0; i < xp->xfrm_nr; i++) {
  729. struct xfrm_user_tmpl *up = &vec[i];
  730. struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
  731. memcpy(&up->id, &kp->id, sizeof(up->id));
  732. up->family = xp->family;
  733. memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
  734. up->reqid = kp->reqid;
  735. up->mode = kp->mode;
  736. up->share = kp->share;
  737. up->optional = kp->optional;
  738. up->aalgos = kp->aalgos;
  739. up->ealgos = kp->ealgos;
  740. up->calgos = kp->calgos;
  741. }
  742. RTA_PUT(skb, XFRMA_TMPL,
  743. (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
  744. vec);
  745. return 0;
  746. rtattr_failure:
  747. return -1;
  748. }
  749. static int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
  750. {
  751. if (xp->security) {
  752. int ctx_size = sizeof(struct xfrm_sec_ctx) +
  753. xp->security->ctx_len;
  754. struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
  755. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  756. uctx->exttype = XFRMA_SEC_CTX;
  757. uctx->len = ctx_size;
  758. uctx->ctx_doi = xp->security->ctx_doi;
  759. uctx->ctx_alg = xp->security->ctx_alg;
  760. uctx->ctx_len = xp->security->ctx_len;
  761. memcpy(uctx + 1, xp->security->ctx_str, xp->security->ctx_len);
  762. }
  763. return 0;
  764. rtattr_failure:
  765. return -1;
  766. }
  767. static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
  768. {
  769. struct xfrm_dump_info *sp = ptr;
  770. struct xfrm_userpolicy_info *p;
  771. struct sk_buff *in_skb = sp->in_skb;
  772. struct sk_buff *skb = sp->out_skb;
  773. struct nlmsghdr *nlh;
  774. unsigned char *b = skb->tail;
  775. if (sp->this_idx < sp->start_idx)
  776. goto out;
  777. nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
  778. sp->nlmsg_seq,
  779. XFRM_MSG_NEWPOLICY, sizeof(*p));
  780. p = NLMSG_DATA(nlh);
  781. nlh->nlmsg_flags = sp->nlmsg_flags;
  782. copy_to_user_policy(xp, p, dir);
  783. if (copy_to_user_tmpl(xp, skb) < 0)
  784. goto nlmsg_failure;
  785. if (copy_to_user_sec_ctx(xp, skb))
  786. goto nlmsg_failure;
  787. nlh->nlmsg_len = skb->tail - b;
  788. out:
  789. sp->this_idx++;
  790. return 0;
  791. nlmsg_failure:
  792. skb_trim(skb, b - skb->data);
  793. return -1;
  794. }
  795. static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
  796. {
  797. struct xfrm_dump_info info;
  798. info.in_skb = cb->skb;
  799. info.out_skb = skb;
  800. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  801. info.nlmsg_flags = NLM_F_MULTI;
  802. info.this_idx = 0;
  803. info.start_idx = cb->args[0];
  804. (void) xfrm_policy_walk(dump_one_policy, &info);
  805. cb->args[0] = info.this_idx;
  806. return skb->len;
  807. }
  808. static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
  809. struct xfrm_policy *xp,
  810. int dir, u32 seq)
  811. {
  812. struct xfrm_dump_info info;
  813. struct sk_buff *skb;
  814. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  815. if (!skb)
  816. return ERR_PTR(-ENOMEM);
  817. NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
  818. info.in_skb = in_skb;
  819. info.out_skb = skb;
  820. info.nlmsg_seq = seq;
  821. info.nlmsg_flags = 0;
  822. info.this_idx = info.start_idx = 0;
  823. if (dump_one_policy(xp, dir, 0, &info) < 0) {
  824. kfree_skb(skb);
  825. return NULL;
  826. }
  827. return skb;
  828. }
  829. static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  830. {
  831. struct xfrm_policy *xp;
  832. struct xfrm_userpolicy_id *p;
  833. int err;
  834. struct km_event c;
  835. int delete;
  836. p = NLMSG_DATA(nlh);
  837. delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
  838. err = verify_policy_dir(p->dir);
  839. if (err)
  840. return err;
  841. if (p->index)
  842. xp = xfrm_policy_byid(p->dir, p->index, delete);
  843. else {
  844. struct rtattr **rtattrs = (struct rtattr **)xfrma;
  845. struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
  846. struct xfrm_policy tmp;
  847. err = verify_sec_ctx_len(rtattrs);
  848. if (err)
  849. return err;
  850. memset(&tmp, 0, sizeof(struct xfrm_policy));
  851. if (rt) {
  852. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  853. if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
  854. return err;
  855. }
  856. xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
  857. security_xfrm_policy_free(&tmp);
  858. }
  859. if (xp == NULL)
  860. return -ENOENT;
  861. if (!delete) {
  862. struct sk_buff *resp_skb;
  863. resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
  864. if (IS_ERR(resp_skb)) {
  865. err = PTR_ERR(resp_skb);
  866. } else {
  867. err = netlink_unicast(xfrm_nl, resp_skb,
  868. NETLINK_CB(skb).pid,
  869. MSG_DONTWAIT);
  870. }
  871. } else {
  872. c.data.byid = p->index;
  873. c.event = nlh->nlmsg_type;
  874. c.seq = nlh->nlmsg_seq;
  875. c.pid = nlh->nlmsg_pid;
  876. km_policy_notify(xp, p->dir, &c);
  877. }
  878. xfrm_pol_put(xp);
  879. return err;
  880. }
  881. static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  882. {
  883. struct km_event c;
  884. struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
  885. xfrm_state_flush(p->proto);
  886. c.data.proto = p->proto;
  887. c.event = nlh->nlmsg_type;
  888. c.seq = nlh->nlmsg_seq;
  889. c.pid = nlh->nlmsg_pid;
  890. km_state_notify(NULL, &c);
  891. return 0;
  892. }
  893. static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
  894. {
  895. struct xfrm_aevent_id *id;
  896. struct nlmsghdr *nlh;
  897. struct xfrm_lifetime_cur ltime;
  898. unsigned char *b = skb->tail;
  899. nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
  900. id = NLMSG_DATA(nlh);
  901. nlh->nlmsg_flags = 0;
  902. id->sa_id.daddr = x->id.daddr;
  903. id->sa_id.spi = x->id.spi;
  904. id->sa_id.family = x->props.family;
  905. id->sa_id.proto = x->id.proto;
  906. id->flags = c->data.aevent;
  907. RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
  908. ltime.bytes = x->curlft.bytes;
  909. ltime.packets = x->curlft.packets;
  910. ltime.add_time = x->curlft.add_time;
  911. ltime.use_time = x->curlft.use_time;
  912. RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
  913. if (id->flags&XFRM_AE_RTHR) {
  914. RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
  915. }
  916. if (id->flags&XFRM_AE_ETHR) {
  917. u32 etimer = x->replay_maxage*10/HZ;
  918. RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
  919. }
  920. nlh->nlmsg_len = skb->tail - b;
  921. return skb->len;
  922. rtattr_failure:
  923. nlmsg_failure:
  924. skb_trim(skb, b - skb->data);
  925. return -1;
  926. }
  927. static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  928. {
  929. struct xfrm_state *x;
  930. struct sk_buff *r_skb;
  931. int err;
  932. struct km_event c;
  933. struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
  934. int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
  935. struct xfrm_usersa_id *id = &p->sa_id;
  936. len += RTA_SPACE(sizeof(struct xfrm_replay_state));
  937. len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
  938. if (p->flags&XFRM_AE_RTHR)
  939. len+=RTA_SPACE(sizeof(u32));
  940. if (p->flags&XFRM_AE_ETHR)
  941. len+=RTA_SPACE(sizeof(u32));
  942. r_skb = alloc_skb(len, GFP_ATOMIC);
  943. if (r_skb == NULL)
  944. return -ENOMEM;
  945. x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
  946. if (x == NULL) {
  947. kfree(r_skb);
  948. return -ESRCH;
  949. }
  950. /*
  951. * XXX: is this lock really needed - none of the other
  952. * gets lock (the concern is things getting updated
  953. * while we are still reading) - jhs
  954. */
  955. spin_lock_bh(&x->lock);
  956. c.data.aevent = p->flags;
  957. c.seq = nlh->nlmsg_seq;
  958. c.pid = nlh->nlmsg_pid;
  959. if (build_aevent(r_skb, x, &c) < 0)
  960. BUG();
  961. err = netlink_unicast(xfrm_nl, r_skb,
  962. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  963. spin_unlock_bh(&x->lock);
  964. xfrm_state_put(x);
  965. return err;
  966. }
  967. static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  968. {
  969. struct xfrm_state *x;
  970. struct km_event c;
  971. int err = - EINVAL;
  972. struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
  973. struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
  974. struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
  975. if (!lt && !rp)
  976. return err;
  977. /* pedantic mode - thou shalt sayeth replaceth */
  978. if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
  979. return err;
  980. x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
  981. if (x == NULL)
  982. return -ESRCH;
  983. if (x->km.state != XFRM_STATE_VALID)
  984. goto out;
  985. spin_lock_bh(&x->lock);
  986. err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
  987. spin_unlock_bh(&x->lock);
  988. if (err < 0)
  989. goto out;
  990. c.event = nlh->nlmsg_type;
  991. c.seq = nlh->nlmsg_seq;
  992. c.pid = nlh->nlmsg_pid;
  993. c.data.aevent = XFRM_AE_CU;
  994. km_state_notify(x, &c);
  995. err = 0;
  996. out:
  997. xfrm_state_put(x);
  998. return err;
  999. }
  1000. static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  1001. {
  1002. struct km_event c;
  1003. xfrm_policy_flush();
  1004. c.event = nlh->nlmsg_type;
  1005. c.seq = nlh->nlmsg_seq;
  1006. c.pid = nlh->nlmsg_pid;
  1007. km_policy_notify(NULL, 0, &c);
  1008. return 0;
  1009. }
  1010. #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
  1011. static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
  1012. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1013. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1014. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1015. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1016. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1017. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1018. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
  1019. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1020. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1021. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  1022. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
  1023. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1024. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1025. };
  1026. #undef XMSGSIZE
  1027. static struct xfrm_link {
  1028. int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
  1029. int (*dump)(struct sk_buff *, struct netlink_callback *);
  1030. } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
  1031. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1032. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
  1033. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
  1034. .dump = xfrm_dump_sa },
  1035. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1036. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
  1037. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
  1038. .dump = xfrm_dump_policy },
  1039. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
  1040. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1041. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1042. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
  1043. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
  1044. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
  1045. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
  1046. };
  1047. static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
  1048. {
  1049. struct rtattr *xfrma[XFRMA_MAX];
  1050. struct xfrm_link *link;
  1051. int type, min_len;
  1052. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  1053. return 0;
  1054. type = nlh->nlmsg_type;
  1055. /* A control message: ignore them */
  1056. if (type < XFRM_MSG_BASE)
  1057. return 0;
  1058. /* Unknown message: reply with EINVAL */
  1059. if (type > XFRM_MSG_MAX)
  1060. goto err_einval;
  1061. type -= XFRM_MSG_BASE;
  1062. link = &xfrm_dispatch[type];
  1063. /* All operations require privileges, even GET */
  1064. if (security_netlink_recv(skb)) {
  1065. *errp = -EPERM;
  1066. return -1;
  1067. }
  1068. if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
  1069. type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
  1070. (nlh->nlmsg_flags & NLM_F_DUMP)) {
  1071. if (link->dump == NULL)
  1072. goto err_einval;
  1073. if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
  1074. link->dump, NULL)) != 0) {
  1075. return -1;
  1076. }
  1077. netlink_queue_skip(nlh, skb);
  1078. return -1;
  1079. }
  1080. memset(xfrma, 0, sizeof(xfrma));
  1081. if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
  1082. goto err_einval;
  1083. if (nlh->nlmsg_len > min_len) {
  1084. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  1085. struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
  1086. while (RTA_OK(attr, attrlen)) {
  1087. unsigned short flavor = attr->rta_type;
  1088. if (flavor) {
  1089. if (flavor > XFRMA_MAX)
  1090. goto err_einval;
  1091. xfrma[flavor - 1] = attr;
  1092. }
  1093. attr = RTA_NEXT(attr, attrlen);
  1094. }
  1095. }
  1096. if (link->doit == NULL)
  1097. goto err_einval;
  1098. *errp = link->doit(skb, nlh, (void **) &xfrma);
  1099. return *errp;
  1100. err_einval:
  1101. *errp = -EINVAL;
  1102. return -1;
  1103. }
  1104. static void xfrm_netlink_rcv(struct sock *sk, int len)
  1105. {
  1106. unsigned int qlen = 0;
  1107. do {
  1108. down(&xfrm_cfg_sem);
  1109. netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
  1110. up(&xfrm_cfg_sem);
  1111. } while (qlen);
  1112. }
  1113. static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
  1114. {
  1115. struct xfrm_user_expire *ue;
  1116. struct nlmsghdr *nlh;
  1117. unsigned char *b = skb->tail;
  1118. nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
  1119. sizeof(*ue));
  1120. ue = NLMSG_DATA(nlh);
  1121. nlh->nlmsg_flags = 0;
  1122. copy_to_user_state(x, &ue->state);
  1123. ue->hard = (c->data.hard != 0) ? 1 : 0;
  1124. nlh->nlmsg_len = skb->tail - b;
  1125. return skb->len;
  1126. nlmsg_failure:
  1127. skb_trim(skb, b - skb->data);
  1128. return -1;
  1129. }
  1130. static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
  1131. {
  1132. struct sk_buff *skb;
  1133. int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
  1134. skb = alloc_skb(len, GFP_ATOMIC);
  1135. if (skb == NULL)
  1136. return -ENOMEM;
  1137. if (build_expire(skb, x, c) < 0)
  1138. BUG();
  1139. NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
  1140. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  1141. }
  1142. static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
  1143. {
  1144. struct sk_buff *skb;
  1145. int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
  1146. len += RTA_SPACE(sizeof(struct xfrm_replay_state));
  1147. len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
  1148. skb = alloc_skb(len, GFP_ATOMIC);
  1149. if (skb == NULL)
  1150. return -ENOMEM;
  1151. if (build_aevent(skb, x, c) < 0)
  1152. BUG();
  1153. NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
  1154. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
  1155. }
  1156. static int xfrm_notify_sa_flush(struct km_event *c)
  1157. {
  1158. struct xfrm_usersa_flush *p;
  1159. struct nlmsghdr *nlh;
  1160. struct sk_buff *skb;
  1161. unsigned char *b;
  1162. int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
  1163. skb = alloc_skb(len, GFP_ATOMIC);
  1164. if (skb == NULL)
  1165. return -ENOMEM;
  1166. b = skb->tail;
  1167. nlh = NLMSG_PUT(skb, c->pid, c->seq,
  1168. XFRM_MSG_FLUSHSA, sizeof(*p));
  1169. nlh->nlmsg_flags = 0;
  1170. p = NLMSG_DATA(nlh);
  1171. p->proto = c->data.proto;
  1172. nlh->nlmsg_len = skb->tail - b;
  1173. NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
  1174. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  1175. nlmsg_failure:
  1176. kfree_skb(skb);
  1177. return -1;
  1178. }
  1179. static int inline xfrm_sa_len(struct xfrm_state *x)
  1180. {
  1181. int l = 0;
  1182. if (x->aalg)
  1183. l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
  1184. if (x->ealg)
  1185. l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
  1186. if (x->calg)
  1187. l += RTA_SPACE(sizeof(*x->calg));
  1188. if (x->encap)
  1189. l += RTA_SPACE(sizeof(*x->encap));
  1190. return l;
  1191. }
  1192. static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
  1193. {
  1194. struct xfrm_usersa_info *p;
  1195. struct xfrm_usersa_id *id;
  1196. struct nlmsghdr *nlh;
  1197. struct sk_buff *skb;
  1198. unsigned char *b;
  1199. int len = xfrm_sa_len(x);
  1200. int headlen;
  1201. headlen = sizeof(*p);
  1202. if (c->event == XFRM_MSG_DELSA) {
  1203. len += RTA_SPACE(headlen);
  1204. headlen = sizeof(*id);
  1205. }
  1206. len += NLMSG_SPACE(headlen);
  1207. skb = alloc_skb(len, GFP_ATOMIC);
  1208. if (skb == NULL)
  1209. return -ENOMEM;
  1210. b = skb->tail;
  1211. nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
  1212. nlh->nlmsg_flags = 0;
  1213. p = NLMSG_DATA(nlh);
  1214. if (c->event == XFRM_MSG_DELSA) {
  1215. id = NLMSG_DATA(nlh);
  1216. memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
  1217. id->spi = x->id.spi;
  1218. id->family = x->props.family;
  1219. id->proto = x->id.proto;
  1220. p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
  1221. }
  1222. copy_to_user_state(x, p);
  1223. if (x->aalg)
  1224. RTA_PUT(skb, XFRMA_ALG_AUTH,
  1225. sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
  1226. if (x->ealg)
  1227. RTA_PUT(skb, XFRMA_ALG_CRYPT,
  1228. sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
  1229. if (x->calg)
  1230. RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  1231. if (x->encap)
  1232. RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  1233. nlh->nlmsg_len = skb->tail - b;
  1234. NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
  1235. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  1236. nlmsg_failure:
  1237. rtattr_failure:
  1238. kfree_skb(skb);
  1239. return -1;
  1240. }
  1241. static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
  1242. {
  1243. switch (c->event) {
  1244. case XFRM_MSG_EXPIRE:
  1245. return xfrm_exp_state_notify(x, c);
  1246. case XFRM_MSG_NEWAE:
  1247. return xfrm_aevent_state_notify(x, c);
  1248. case XFRM_MSG_DELSA:
  1249. case XFRM_MSG_UPDSA:
  1250. case XFRM_MSG_NEWSA:
  1251. return xfrm_notify_sa(x, c);
  1252. case XFRM_MSG_FLUSHSA:
  1253. return xfrm_notify_sa_flush(c);
  1254. default:
  1255. printk("xfrm_user: Unknown SA event %d\n", c->event);
  1256. break;
  1257. }
  1258. return 0;
  1259. }
  1260. static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
  1261. struct xfrm_tmpl *xt, struct xfrm_policy *xp,
  1262. int dir)
  1263. {
  1264. struct xfrm_user_acquire *ua;
  1265. struct nlmsghdr *nlh;
  1266. unsigned char *b = skb->tail;
  1267. __u32 seq = xfrm_get_acqseq();
  1268. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
  1269. sizeof(*ua));
  1270. ua = NLMSG_DATA(nlh);
  1271. nlh->nlmsg_flags = 0;
  1272. memcpy(&ua->id, &x->id, sizeof(ua->id));
  1273. memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
  1274. memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
  1275. copy_to_user_policy(xp, &ua->policy, dir);
  1276. ua->aalgos = xt->aalgos;
  1277. ua->ealgos = xt->ealgos;
  1278. ua->calgos = xt->calgos;
  1279. ua->seq = x->km.seq = seq;
  1280. if (copy_to_user_tmpl(xp, skb) < 0)
  1281. goto nlmsg_failure;
  1282. if (copy_to_user_sec_ctx(xp, skb))
  1283. goto nlmsg_failure;
  1284. nlh->nlmsg_len = skb->tail - b;
  1285. return skb->len;
  1286. nlmsg_failure:
  1287. skb_trim(skb, b - skb->data);
  1288. return -1;
  1289. }
  1290. static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
  1291. struct xfrm_policy *xp, int dir)
  1292. {
  1293. struct sk_buff *skb;
  1294. size_t len;
  1295. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1296. len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
  1297. len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
  1298. skb = alloc_skb(len, GFP_ATOMIC);
  1299. if (skb == NULL)
  1300. return -ENOMEM;
  1301. if (build_acquire(skb, x, xt, xp, dir) < 0)
  1302. BUG();
  1303. NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
  1304. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
  1305. }
  1306. /* User gives us xfrm_user_policy_info followed by an array of 0
  1307. * or more templates.
  1308. */
  1309. static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
  1310. u8 *data, int len, int *dir)
  1311. {
  1312. struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
  1313. struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
  1314. struct xfrm_policy *xp;
  1315. int nr;
  1316. switch (family) {
  1317. case AF_INET:
  1318. if (opt != IP_XFRM_POLICY) {
  1319. *dir = -EOPNOTSUPP;
  1320. return NULL;
  1321. }
  1322. break;
  1323. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1324. case AF_INET6:
  1325. if (opt != IPV6_XFRM_POLICY) {
  1326. *dir = -EOPNOTSUPP;
  1327. return NULL;
  1328. }
  1329. break;
  1330. #endif
  1331. default:
  1332. *dir = -EINVAL;
  1333. return NULL;
  1334. }
  1335. *dir = -EINVAL;
  1336. if (len < sizeof(*p) ||
  1337. verify_newpolicy_info(p))
  1338. return NULL;
  1339. nr = ((len - sizeof(*p)) / sizeof(*ut));
  1340. if (nr > XFRM_MAX_DEPTH)
  1341. return NULL;
  1342. if (p->dir > XFRM_POLICY_OUT)
  1343. return NULL;
  1344. xp = xfrm_policy_alloc(GFP_KERNEL);
  1345. if (xp == NULL) {
  1346. *dir = -ENOBUFS;
  1347. return NULL;
  1348. }
  1349. copy_from_user_policy(xp, p);
  1350. copy_templates(xp, ut, nr);
  1351. *dir = p->dir;
  1352. return xp;
  1353. }
  1354. static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
  1355. int dir, struct km_event *c)
  1356. {
  1357. struct xfrm_user_polexpire *upe;
  1358. struct nlmsghdr *nlh;
  1359. int hard = c->data.hard;
  1360. unsigned char *b = skb->tail;
  1361. nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
  1362. upe = NLMSG_DATA(nlh);
  1363. nlh->nlmsg_flags = 0;
  1364. copy_to_user_policy(xp, &upe->pol, dir);
  1365. if (copy_to_user_tmpl(xp, skb) < 0)
  1366. goto nlmsg_failure;
  1367. if (copy_to_user_sec_ctx(xp, skb))
  1368. goto nlmsg_failure;
  1369. upe->hard = !!hard;
  1370. nlh->nlmsg_len = skb->tail - b;
  1371. return skb->len;
  1372. nlmsg_failure:
  1373. skb_trim(skb, b - skb->data);
  1374. return -1;
  1375. }
  1376. static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  1377. {
  1378. struct sk_buff *skb;
  1379. size_t len;
  1380. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1381. len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
  1382. len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
  1383. skb = alloc_skb(len, GFP_ATOMIC);
  1384. if (skb == NULL)
  1385. return -ENOMEM;
  1386. if (build_polexpire(skb, xp, dir, c) < 0)
  1387. BUG();
  1388. NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
  1389. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  1390. }
  1391. static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
  1392. {
  1393. struct xfrm_userpolicy_info *p;
  1394. struct xfrm_userpolicy_id *id;
  1395. struct nlmsghdr *nlh;
  1396. struct sk_buff *skb;
  1397. unsigned char *b;
  1398. int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1399. int headlen;
  1400. headlen = sizeof(*p);
  1401. if (c->event == XFRM_MSG_DELPOLICY) {
  1402. len += RTA_SPACE(headlen);
  1403. headlen = sizeof(*id);
  1404. }
  1405. len += NLMSG_SPACE(headlen);
  1406. skb = alloc_skb(len, GFP_ATOMIC);
  1407. if (skb == NULL)
  1408. return -ENOMEM;
  1409. b = skb->tail;
  1410. nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
  1411. p = NLMSG_DATA(nlh);
  1412. if (c->event == XFRM_MSG_DELPOLICY) {
  1413. id = NLMSG_DATA(nlh);
  1414. memset(id, 0, sizeof(*id));
  1415. id->dir = dir;
  1416. if (c->data.byid)
  1417. id->index = xp->index;
  1418. else
  1419. memcpy(&id->sel, &xp->selector, sizeof(id->sel));
  1420. p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
  1421. }
  1422. nlh->nlmsg_flags = 0;
  1423. copy_to_user_policy(xp, p, dir);
  1424. if (copy_to_user_tmpl(xp, skb) < 0)
  1425. goto nlmsg_failure;
  1426. nlh->nlmsg_len = skb->tail - b;
  1427. NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
  1428. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  1429. nlmsg_failure:
  1430. rtattr_failure:
  1431. kfree_skb(skb);
  1432. return -1;
  1433. }
  1434. static int xfrm_notify_policy_flush(struct km_event *c)
  1435. {
  1436. struct nlmsghdr *nlh;
  1437. struct sk_buff *skb;
  1438. unsigned char *b;
  1439. int len = NLMSG_LENGTH(0);
  1440. skb = alloc_skb(len, GFP_ATOMIC);
  1441. if (skb == NULL)
  1442. return -ENOMEM;
  1443. b = skb->tail;
  1444. nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
  1445. nlh->nlmsg_len = skb->tail - b;
  1446. NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
  1447. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  1448. nlmsg_failure:
  1449. kfree_skb(skb);
  1450. return -1;
  1451. }
  1452. static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  1453. {
  1454. switch (c->event) {
  1455. case XFRM_MSG_NEWPOLICY:
  1456. case XFRM_MSG_UPDPOLICY:
  1457. case XFRM_MSG_DELPOLICY:
  1458. return xfrm_notify_policy(xp, dir, c);
  1459. case XFRM_MSG_FLUSHPOLICY:
  1460. return xfrm_notify_policy_flush(c);
  1461. case XFRM_MSG_POLEXPIRE:
  1462. return xfrm_exp_policy_notify(xp, dir, c);
  1463. default:
  1464. printk("xfrm_user: Unknown Policy event %d\n", c->event);
  1465. }
  1466. return 0;
  1467. }
  1468. static struct xfrm_mgr netlink_mgr = {
  1469. .id = "netlink",
  1470. .notify = xfrm_send_state_notify,
  1471. .acquire = xfrm_send_acquire,
  1472. .compile_policy = xfrm_compile_policy,
  1473. .notify_policy = xfrm_send_policy_notify,
  1474. };
  1475. static int __init xfrm_user_init(void)
  1476. {
  1477. printk(KERN_INFO "Initializing IPsec netlink socket\n");
  1478. xfrm_nl = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
  1479. xfrm_netlink_rcv, THIS_MODULE);
  1480. if (xfrm_nl == NULL)
  1481. return -ENOMEM;
  1482. xfrm_register_km(&netlink_mgr);
  1483. return 0;
  1484. }
  1485. static void __exit xfrm_user_exit(void)
  1486. {
  1487. xfrm_unregister_km(&netlink_mgr);
  1488. sock_release(xfrm_nl->sk_socket);
  1489. }
  1490. module_init(xfrm_user_init);
  1491. module_exit(xfrm_user_exit);
  1492. MODULE_LICENSE("GPL");
  1493. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
  1494. EXPORT_SYMBOL(xfrm_nl);