xfrm_user.c 44 KB

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