xfrm_user.c 44 KB

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