xfrm_user.c 44 KB

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