xfrm_user.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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/netlink.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 <asm/uaccess.h>
  29. static struct sock *xfrm_nl;
  30. static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
  31. {
  32. struct rtattr *rt = xfrma[type - 1];
  33. struct xfrm_algo *algp;
  34. if (!rt)
  35. return 0;
  36. if ((rt->rta_len - sizeof(*rt)) < sizeof(*algp))
  37. return -EINVAL;
  38. algp = RTA_DATA(rt);
  39. switch (type) {
  40. case XFRMA_ALG_AUTH:
  41. if (!algp->alg_key_len &&
  42. strcmp(algp->alg_name, "digest_null") != 0)
  43. return -EINVAL;
  44. break;
  45. case XFRMA_ALG_CRYPT:
  46. if (!algp->alg_key_len &&
  47. strcmp(algp->alg_name, "cipher_null") != 0)
  48. return -EINVAL;
  49. break;
  50. case XFRMA_ALG_COMP:
  51. /* Zero length keys are legal. */
  52. break;
  53. default:
  54. return -EINVAL;
  55. };
  56. algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
  57. return 0;
  58. }
  59. static int verify_encap_tmpl(struct rtattr **xfrma)
  60. {
  61. struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
  62. struct xfrm_encap_tmpl *encap;
  63. if (!rt)
  64. return 0;
  65. if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
  66. return -EINVAL;
  67. return 0;
  68. }
  69. static int verify_newsa_info(struct xfrm_usersa_info *p,
  70. struct rtattr **xfrma)
  71. {
  72. int err;
  73. err = -EINVAL;
  74. switch (p->family) {
  75. case AF_INET:
  76. break;
  77. case AF_INET6:
  78. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  79. break;
  80. #else
  81. err = -EAFNOSUPPORT;
  82. goto out;
  83. #endif
  84. default:
  85. goto out;
  86. };
  87. err = -EINVAL;
  88. switch (p->id.proto) {
  89. case IPPROTO_AH:
  90. if (!xfrma[XFRMA_ALG_AUTH-1] ||
  91. xfrma[XFRMA_ALG_CRYPT-1] ||
  92. xfrma[XFRMA_ALG_COMP-1])
  93. goto out;
  94. break;
  95. case IPPROTO_ESP:
  96. if ((!xfrma[XFRMA_ALG_AUTH-1] &&
  97. !xfrma[XFRMA_ALG_CRYPT-1]) ||
  98. xfrma[XFRMA_ALG_COMP-1])
  99. goto out;
  100. break;
  101. case IPPROTO_COMP:
  102. if (!xfrma[XFRMA_ALG_COMP-1] ||
  103. xfrma[XFRMA_ALG_AUTH-1] ||
  104. xfrma[XFRMA_ALG_CRYPT-1])
  105. goto out;
  106. break;
  107. default:
  108. goto out;
  109. };
  110. if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
  111. goto out;
  112. if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
  113. goto out;
  114. if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
  115. goto out;
  116. if ((err = verify_encap_tmpl(xfrma)))
  117. goto out;
  118. err = -EINVAL;
  119. switch (p->mode) {
  120. case 0:
  121. case 1:
  122. break;
  123. default:
  124. goto out;
  125. };
  126. err = 0;
  127. out:
  128. return err;
  129. }
  130. static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
  131. struct xfrm_algo_desc *(*get_byname)(char *, int),
  132. struct rtattr *u_arg)
  133. {
  134. struct rtattr *rta = u_arg;
  135. struct xfrm_algo *p, *ualg;
  136. struct xfrm_algo_desc *algo;
  137. if (!rta)
  138. return 0;
  139. ualg = RTA_DATA(rta);
  140. algo = get_byname(ualg->alg_name, 1);
  141. if (!algo)
  142. return -ENOSYS;
  143. *props = algo->desc.sadb_alg_id;
  144. p = kmalloc(sizeof(*ualg) + ualg->alg_key_len, GFP_KERNEL);
  145. if (!p)
  146. return -ENOMEM;
  147. memcpy(p, ualg, sizeof(*ualg) + ualg->alg_key_len);
  148. *algpp = p;
  149. return 0;
  150. }
  151. static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
  152. {
  153. struct rtattr *rta = u_arg;
  154. struct xfrm_encap_tmpl *p, *uencap;
  155. if (!rta)
  156. return 0;
  157. uencap = RTA_DATA(rta);
  158. p = kmalloc(sizeof(*p), GFP_KERNEL);
  159. if (!p)
  160. return -ENOMEM;
  161. memcpy(p, uencap, sizeof(*p));
  162. *encapp = p;
  163. return 0;
  164. }
  165. static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  166. {
  167. memcpy(&x->id, &p->id, sizeof(x->id));
  168. memcpy(&x->sel, &p->sel, sizeof(x->sel));
  169. memcpy(&x->lft, &p->lft, sizeof(x->lft));
  170. x->props.mode = p->mode;
  171. x->props.replay_window = p->replay_window;
  172. x->props.reqid = p->reqid;
  173. x->props.family = p->family;
  174. x->props.saddr = p->saddr;
  175. x->props.flags = p->flags;
  176. }
  177. static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
  178. struct rtattr **xfrma,
  179. int *errp)
  180. {
  181. struct xfrm_state *x = xfrm_state_alloc();
  182. int err = -ENOMEM;
  183. if (!x)
  184. goto error_no_put;
  185. copy_from_user_state(x, p);
  186. if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
  187. xfrm_aalg_get_byname,
  188. xfrma[XFRMA_ALG_AUTH-1])))
  189. goto error;
  190. if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
  191. xfrm_ealg_get_byname,
  192. xfrma[XFRMA_ALG_CRYPT-1])))
  193. goto error;
  194. if ((err = attach_one_algo(&x->calg, &x->props.calgo,
  195. xfrm_calg_get_byname,
  196. xfrma[XFRMA_ALG_COMP-1])))
  197. goto error;
  198. if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
  199. goto error;
  200. err = -ENOENT;
  201. x->type = xfrm_get_type(x->id.proto, x->props.family);
  202. if (x->type == NULL)
  203. goto error;
  204. err = x->type->init_state(x, NULL);
  205. if (err)
  206. goto error;
  207. x->curlft.add_time = (unsigned long) xtime.tv_sec;
  208. x->km.state = XFRM_STATE_VALID;
  209. x->km.seq = p->seq;
  210. return x;
  211. error:
  212. x->km.state = XFRM_STATE_DEAD;
  213. xfrm_state_put(x);
  214. error_no_put:
  215. *errp = err;
  216. return NULL;
  217. }
  218. static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  219. {
  220. struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
  221. struct xfrm_state *x;
  222. int err;
  223. err = verify_newsa_info(p, (struct rtattr **) xfrma);
  224. if (err)
  225. return err;
  226. x = xfrm_state_construct(p, (struct rtattr **) xfrma, &err);
  227. if (!x)
  228. return err;
  229. if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
  230. err = xfrm_state_add(x);
  231. else
  232. err = xfrm_state_update(x);
  233. if (err < 0) {
  234. x->km.state = XFRM_STATE_DEAD;
  235. xfrm_state_put(x);
  236. }
  237. return err;
  238. }
  239. static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  240. {
  241. struct xfrm_state *x;
  242. struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
  243. x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
  244. if (x == NULL)
  245. return -ESRCH;
  246. if (xfrm_state_kern(x)) {
  247. xfrm_state_put(x);
  248. return -EPERM;
  249. }
  250. xfrm_state_delete(x);
  251. xfrm_state_put(x);
  252. return 0;
  253. }
  254. static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  255. {
  256. memcpy(&p->id, &x->id, sizeof(p->id));
  257. memcpy(&p->sel, &x->sel, sizeof(p->sel));
  258. memcpy(&p->lft, &x->lft, sizeof(p->lft));
  259. memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
  260. memcpy(&p->stats, &x->stats, sizeof(p->stats));
  261. p->saddr = x->props.saddr;
  262. p->mode = x->props.mode;
  263. p->replay_window = x->props.replay_window;
  264. p->reqid = x->props.reqid;
  265. p->family = x->props.family;
  266. p->flags = x->props.flags;
  267. p->seq = x->km.seq;
  268. }
  269. struct xfrm_dump_info {
  270. struct sk_buff *in_skb;
  271. struct sk_buff *out_skb;
  272. u32 nlmsg_seq;
  273. u16 nlmsg_flags;
  274. int start_idx;
  275. int this_idx;
  276. };
  277. static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
  278. {
  279. struct xfrm_dump_info *sp = ptr;
  280. struct sk_buff *in_skb = sp->in_skb;
  281. struct sk_buff *skb = sp->out_skb;
  282. struct xfrm_usersa_info *p;
  283. struct nlmsghdr *nlh;
  284. unsigned char *b = skb->tail;
  285. if (sp->this_idx < sp->start_idx)
  286. goto out;
  287. nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
  288. sp->nlmsg_seq,
  289. XFRM_MSG_NEWSA, sizeof(*p));
  290. nlh->nlmsg_flags = sp->nlmsg_flags;
  291. p = NLMSG_DATA(nlh);
  292. copy_to_user_state(x, p);
  293. if (x->aalg)
  294. RTA_PUT(skb, XFRMA_ALG_AUTH,
  295. sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
  296. if (x->ealg)
  297. RTA_PUT(skb, XFRMA_ALG_CRYPT,
  298. sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
  299. if (x->calg)
  300. RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  301. if (x->encap)
  302. RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  303. nlh->nlmsg_len = skb->tail - b;
  304. out:
  305. sp->this_idx++;
  306. return 0;
  307. nlmsg_failure:
  308. rtattr_failure:
  309. skb_trim(skb, b - skb->data);
  310. return -1;
  311. }
  312. static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
  313. {
  314. struct xfrm_dump_info info;
  315. info.in_skb = cb->skb;
  316. info.out_skb = skb;
  317. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  318. info.nlmsg_flags = NLM_F_MULTI;
  319. info.this_idx = 0;
  320. info.start_idx = cb->args[0];
  321. (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
  322. cb->args[0] = info.this_idx;
  323. return skb->len;
  324. }
  325. static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
  326. struct xfrm_state *x, u32 seq)
  327. {
  328. struct xfrm_dump_info info;
  329. struct sk_buff *skb;
  330. skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
  331. if (!skb)
  332. return ERR_PTR(-ENOMEM);
  333. NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
  334. info.in_skb = in_skb;
  335. info.out_skb = skb;
  336. info.nlmsg_seq = seq;
  337. info.nlmsg_flags = 0;
  338. info.this_idx = info.start_idx = 0;
  339. if (dump_one_state(x, 0, &info)) {
  340. kfree_skb(skb);
  341. return NULL;
  342. }
  343. return skb;
  344. }
  345. static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  346. {
  347. struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
  348. struct xfrm_state *x;
  349. struct sk_buff *resp_skb;
  350. int err;
  351. x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
  352. err = -ESRCH;
  353. if (x == NULL)
  354. goto out_noput;
  355. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  356. if (IS_ERR(resp_skb)) {
  357. err = PTR_ERR(resp_skb);
  358. } else {
  359. err = netlink_unicast(xfrm_nl, resp_skb,
  360. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  361. }
  362. xfrm_state_put(x);
  363. out_noput:
  364. return err;
  365. }
  366. static int verify_userspi_info(struct xfrm_userspi_info *p)
  367. {
  368. switch (p->info.id.proto) {
  369. case IPPROTO_AH:
  370. case IPPROTO_ESP:
  371. break;
  372. case IPPROTO_COMP:
  373. /* IPCOMP spi is 16-bits. */
  374. if (p->max >= 0x10000)
  375. return -EINVAL;
  376. break;
  377. default:
  378. return -EINVAL;
  379. };
  380. if (p->min > p->max)
  381. return -EINVAL;
  382. return 0;
  383. }
  384. static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  385. {
  386. struct xfrm_state *x;
  387. struct xfrm_userspi_info *p;
  388. struct sk_buff *resp_skb;
  389. xfrm_address_t *daddr;
  390. int family;
  391. int err;
  392. p = NLMSG_DATA(nlh);
  393. err = verify_userspi_info(p);
  394. if (err)
  395. goto out_noput;
  396. family = p->info.family;
  397. daddr = &p->info.id.daddr;
  398. x = NULL;
  399. if (p->info.seq) {
  400. x = xfrm_find_acq_byseq(p->info.seq);
  401. if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
  402. xfrm_state_put(x);
  403. x = NULL;
  404. }
  405. }
  406. if (!x)
  407. x = xfrm_find_acq(p->info.mode, p->info.reqid,
  408. p->info.id.proto, daddr,
  409. &p->info.saddr, 1,
  410. family);
  411. err = -ENOENT;
  412. if (x == NULL)
  413. goto out_noput;
  414. resp_skb = ERR_PTR(-ENOENT);
  415. spin_lock_bh(&x->lock);
  416. if (x->km.state != XFRM_STATE_DEAD) {
  417. xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
  418. if (x->id.spi)
  419. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  420. }
  421. spin_unlock_bh(&x->lock);
  422. if (IS_ERR(resp_skb)) {
  423. err = PTR_ERR(resp_skb);
  424. goto out;
  425. }
  426. err = netlink_unicast(xfrm_nl, resp_skb,
  427. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  428. out:
  429. xfrm_state_put(x);
  430. out_noput:
  431. return err;
  432. }
  433. static int verify_policy_dir(__u8 dir)
  434. {
  435. switch (dir) {
  436. case XFRM_POLICY_IN:
  437. case XFRM_POLICY_OUT:
  438. case XFRM_POLICY_FWD:
  439. break;
  440. default:
  441. return -EINVAL;
  442. };
  443. return 0;
  444. }
  445. static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
  446. {
  447. switch (p->share) {
  448. case XFRM_SHARE_ANY:
  449. case XFRM_SHARE_SESSION:
  450. case XFRM_SHARE_USER:
  451. case XFRM_SHARE_UNIQUE:
  452. break;
  453. default:
  454. return -EINVAL;
  455. };
  456. switch (p->action) {
  457. case XFRM_POLICY_ALLOW:
  458. case XFRM_POLICY_BLOCK:
  459. break;
  460. default:
  461. return -EINVAL;
  462. };
  463. switch (p->sel.family) {
  464. case AF_INET:
  465. break;
  466. case AF_INET6:
  467. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  468. break;
  469. #else
  470. return -EAFNOSUPPORT;
  471. #endif
  472. default:
  473. return -EINVAL;
  474. };
  475. return verify_policy_dir(p->dir);
  476. }
  477. static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
  478. int nr)
  479. {
  480. int i;
  481. xp->xfrm_nr = nr;
  482. for (i = 0; i < nr; i++, ut++) {
  483. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  484. memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
  485. memcpy(&t->saddr, &ut->saddr,
  486. sizeof(xfrm_address_t));
  487. t->reqid = ut->reqid;
  488. t->mode = ut->mode;
  489. t->share = ut->share;
  490. t->optional = ut->optional;
  491. t->aalgos = ut->aalgos;
  492. t->ealgos = ut->ealgos;
  493. t->calgos = ut->calgos;
  494. }
  495. }
  496. static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
  497. {
  498. struct rtattr *rt = xfrma[XFRMA_TMPL-1];
  499. struct xfrm_user_tmpl *utmpl;
  500. int nr;
  501. if (!rt) {
  502. pol->xfrm_nr = 0;
  503. } else {
  504. nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
  505. if (nr > XFRM_MAX_DEPTH)
  506. return -EINVAL;
  507. copy_templates(pol, RTA_DATA(rt), nr);
  508. }
  509. return 0;
  510. }
  511. static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
  512. {
  513. xp->priority = p->priority;
  514. xp->index = p->index;
  515. memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
  516. memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
  517. xp->action = p->action;
  518. xp->flags = p->flags;
  519. xp->family = p->sel.family;
  520. /* XXX xp->share = p->share; */
  521. }
  522. static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
  523. {
  524. memcpy(&p->sel, &xp->selector, sizeof(p->sel));
  525. memcpy(&p->lft, &xp->lft, sizeof(p->lft));
  526. memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
  527. p->priority = xp->priority;
  528. p->index = xp->index;
  529. p->sel.family = xp->family;
  530. p->dir = dir;
  531. p->action = xp->action;
  532. p->flags = xp->flags;
  533. p->share = XFRM_SHARE_ANY; /* XXX xp->share */
  534. }
  535. static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
  536. {
  537. struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
  538. int err;
  539. if (!xp) {
  540. *errp = -ENOMEM;
  541. return NULL;
  542. }
  543. copy_from_user_policy(xp, p);
  544. err = copy_from_user_tmpl(xp, xfrma);
  545. if (err) {
  546. *errp = err;
  547. kfree(xp);
  548. xp = NULL;
  549. }
  550. return xp;
  551. }
  552. static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  553. {
  554. struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
  555. struct xfrm_policy *xp;
  556. int err;
  557. int excl;
  558. err = verify_newpolicy_info(p);
  559. if (err)
  560. return err;
  561. xp = xfrm_policy_construct(p, (struct rtattr **) xfrma, &err);
  562. if (!xp)
  563. return err;
  564. excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
  565. err = xfrm_policy_insert(p->dir, xp, excl);
  566. if (err) {
  567. kfree(xp);
  568. return err;
  569. }
  570. xfrm_pol_put(xp);
  571. return 0;
  572. }
  573. static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
  574. {
  575. struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
  576. int i;
  577. if (xp->xfrm_nr == 0)
  578. return 0;
  579. for (i = 0; i < xp->xfrm_nr; i++) {
  580. struct xfrm_user_tmpl *up = &vec[i];
  581. struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
  582. memcpy(&up->id, &kp->id, sizeof(up->id));
  583. up->family = xp->family;
  584. memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
  585. up->reqid = kp->reqid;
  586. up->mode = kp->mode;
  587. up->share = kp->share;
  588. up->optional = kp->optional;
  589. up->aalgos = kp->aalgos;
  590. up->ealgos = kp->ealgos;
  591. up->calgos = kp->calgos;
  592. }
  593. RTA_PUT(skb, XFRMA_TMPL,
  594. (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
  595. vec);
  596. return 0;
  597. rtattr_failure:
  598. return -1;
  599. }
  600. static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
  601. {
  602. struct xfrm_dump_info *sp = ptr;
  603. struct xfrm_userpolicy_info *p;
  604. struct sk_buff *in_skb = sp->in_skb;
  605. struct sk_buff *skb = sp->out_skb;
  606. struct nlmsghdr *nlh;
  607. unsigned char *b = skb->tail;
  608. if (sp->this_idx < sp->start_idx)
  609. goto out;
  610. nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
  611. sp->nlmsg_seq,
  612. XFRM_MSG_NEWPOLICY, sizeof(*p));
  613. p = NLMSG_DATA(nlh);
  614. nlh->nlmsg_flags = sp->nlmsg_flags;
  615. copy_to_user_policy(xp, p, dir);
  616. if (copy_to_user_tmpl(xp, skb) < 0)
  617. goto nlmsg_failure;
  618. nlh->nlmsg_len = skb->tail - b;
  619. out:
  620. sp->this_idx++;
  621. return 0;
  622. nlmsg_failure:
  623. skb_trim(skb, b - skb->data);
  624. return -1;
  625. }
  626. static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
  627. {
  628. struct xfrm_dump_info info;
  629. info.in_skb = cb->skb;
  630. info.out_skb = skb;
  631. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  632. info.nlmsg_flags = NLM_F_MULTI;
  633. info.this_idx = 0;
  634. info.start_idx = cb->args[0];
  635. (void) xfrm_policy_walk(dump_one_policy, &info);
  636. cb->args[0] = info.this_idx;
  637. return skb->len;
  638. }
  639. static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
  640. struct xfrm_policy *xp,
  641. int dir, u32 seq)
  642. {
  643. struct xfrm_dump_info info;
  644. struct sk_buff *skb;
  645. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  646. if (!skb)
  647. return ERR_PTR(-ENOMEM);
  648. NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
  649. info.in_skb = in_skb;
  650. info.out_skb = skb;
  651. info.nlmsg_seq = seq;
  652. info.nlmsg_flags = 0;
  653. info.this_idx = info.start_idx = 0;
  654. if (dump_one_policy(xp, dir, 0, &info) < 0) {
  655. kfree_skb(skb);
  656. return NULL;
  657. }
  658. return skb;
  659. }
  660. static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  661. {
  662. struct xfrm_policy *xp;
  663. struct xfrm_userpolicy_id *p;
  664. int err;
  665. int delete;
  666. p = NLMSG_DATA(nlh);
  667. delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
  668. err = verify_policy_dir(p->dir);
  669. if (err)
  670. return err;
  671. if (p->index)
  672. xp = xfrm_policy_byid(p->dir, p->index, delete);
  673. else
  674. xp = xfrm_policy_bysel(p->dir, &p->sel, delete);
  675. if (xp == NULL)
  676. return -ENOENT;
  677. if (!delete) {
  678. struct sk_buff *resp_skb;
  679. resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
  680. if (IS_ERR(resp_skb)) {
  681. err = PTR_ERR(resp_skb);
  682. } else {
  683. err = netlink_unicast(xfrm_nl, resp_skb,
  684. NETLINK_CB(skb).pid,
  685. MSG_DONTWAIT);
  686. }
  687. }
  688. xfrm_pol_put(xp);
  689. return err;
  690. }
  691. static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  692. {
  693. struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
  694. xfrm_state_flush(p->proto);
  695. return 0;
  696. }
  697. static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  698. {
  699. xfrm_policy_flush();
  700. return 0;
  701. }
  702. #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
  703. static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
  704. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  705. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  706. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  707. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  708. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  709. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  710. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
  711. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
  712. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
  713. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  714. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  715. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
  716. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  717. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
  718. };
  719. #undef XMSGSIZE
  720. static struct xfrm_link {
  721. int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
  722. int (*dump)(struct sk_buff *, struct netlink_callback *);
  723. } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
  724. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  725. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
  726. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
  727. .dump = xfrm_dump_sa },
  728. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  729. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
  730. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
  731. .dump = xfrm_dump_policy },
  732. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
  733. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  734. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  735. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
  736. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
  737. };
  738. static int xfrm_done(struct netlink_callback *cb)
  739. {
  740. return 0;
  741. }
  742. static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
  743. {
  744. struct rtattr *xfrma[XFRMA_MAX];
  745. struct xfrm_link *link;
  746. int type, min_len;
  747. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  748. return 0;
  749. type = nlh->nlmsg_type;
  750. /* A control message: ignore them */
  751. if (type < XFRM_MSG_BASE)
  752. return 0;
  753. /* Unknown message: reply with EINVAL */
  754. if (type > XFRM_MSG_MAX)
  755. goto err_einval;
  756. type -= XFRM_MSG_BASE;
  757. link = &xfrm_dispatch[type];
  758. /* All operations require privileges, even GET */
  759. if (security_netlink_recv(skb)) {
  760. *errp = -EPERM;
  761. return -1;
  762. }
  763. if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
  764. type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
  765. (nlh->nlmsg_flags & NLM_F_DUMP)) {
  766. u32 rlen;
  767. if (link->dump == NULL)
  768. goto err_einval;
  769. if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
  770. link->dump,
  771. xfrm_done)) != 0) {
  772. return -1;
  773. }
  774. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  775. if (rlen > skb->len)
  776. rlen = skb->len;
  777. skb_pull(skb, rlen);
  778. return -1;
  779. }
  780. memset(xfrma, 0, sizeof(xfrma));
  781. if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
  782. goto err_einval;
  783. if (nlh->nlmsg_len > min_len) {
  784. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  785. struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
  786. while (RTA_OK(attr, attrlen)) {
  787. unsigned short flavor = attr->rta_type;
  788. if (flavor) {
  789. if (flavor > XFRMA_MAX)
  790. goto err_einval;
  791. xfrma[flavor - 1] = attr;
  792. }
  793. attr = RTA_NEXT(attr, attrlen);
  794. }
  795. }
  796. if (link->doit == NULL)
  797. goto err_einval;
  798. *errp = link->doit(skb, nlh, (void **) &xfrma);
  799. return *errp;
  800. err_einval:
  801. *errp = -EINVAL;
  802. return -1;
  803. }
  804. static int xfrm_user_rcv_skb(struct sk_buff *skb)
  805. {
  806. int err;
  807. struct nlmsghdr *nlh;
  808. while (skb->len >= NLMSG_SPACE(0)) {
  809. u32 rlen;
  810. nlh = (struct nlmsghdr *) skb->data;
  811. if (nlh->nlmsg_len < sizeof(*nlh) ||
  812. skb->len < nlh->nlmsg_len)
  813. return 0;
  814. rlen = NLMSG_ALIGN(nlh->nlmsg_len);
  815. if (rlen > skb->len)
  816. rlen = skb->len;
  817. if (xfrm_user_rcv_msg(skb, nlh, &err) < 0) {
  818. if (err == 0)
  819. return -1;
  820. netlink_ack(skb, nlh, err);
  821. } else if (nlh->nlmsg_flags & NLM_F_ACK)
  822. netlink_ack(skb, nlh, 0);
  823. skb_pull(skb, rlen);
  824. }
  825. return 0;
  826. }
  827. static void xfrm_netlink_rcv(struct sock *sk, int len)
  828. {
  829. do {
  830. struct sk_buff *skb;
  831. down(&xfrm_cfg_sem);
  832. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  833. if (xfrm_user_rcv_skb(skb)) {
  834. if (skb->len)
  835. skb_queue_head(&sk->sk_receive_queue,
  836. skb);
  837. else
  838. kfree_skb(skb);
  839. break;
  840. }
  841. kfree_skb(skb);
  842. }
  843. up(&xfrm_cfg_sem);
  844. } while (xfrm_nl && xfrm_nl->sk_receive_queue.qlen);
  845. }
  846. static int build_expire(struct sk_buff *skb, struct xfrm_state *x, int hard)
  847. {
  848. struct xfrm_user_expire *ue;
  849. struct nlmsghdr *nlh;
  850. unsigned char *b = skb->tail;
  851. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_EXPIRE,
  852. sizeof(*ue));
  853. ue = NLMSG_DATA(nlh);
  854. nlh->nlmsg_flags = 0;
  855. copy_to_user_state(x, &ue->state);
  856. ue->hard = (hard != 0) ? 1 : 0;
  857. nlh->nlmsg_len = skb->tail - b;
  858. return skb->len;
  859. nlmsg_failure:
  860. skb_trim(skb, b - skb->data);
  861. return -1;
  862. }
  863. static int xfrm_send_state_notify(struct xfrm_state *x, int hard)
  864. {
  865. struct sk_buff *skb;
  866. skb = alloc_skb(sizeof(struct xfrm_user_expire) + 16, GFP_ATOMIC);
  867. if (skb == NULL)
  868. return -ENOMEM;
  869. if (build_expire(skb, x, hard) < 0)
  870. BUG();
  871. NETLINK_CB(skb).dst_groups = XFRMGRP_EXPIRE;
  872. return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_EXPIRE, GFP_ATOMIC);
  873. }
  874. static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
  875. struct xfrm_tmpl *xt, struct xfrm_policy *xp,
  876. int dir)
  877. {
  878. struct xfrm_user_acquire *ua;
  879. struct nlmsghdr *nlh;
  880. unsigned char *b = skb->tail;
  881. __u32 seq = xfrm_get_acqseq();
  882. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
  883. sizeof(*ua));
  884. ua = NLMSG_DATA(nlh);
  885. nlh->nlmsg_flags = 0;
  886. memcpy(&ua->id, &x->id, sizeof(ua->id));
  887. memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
  888. memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
  889. copy_to_user_policy(xp, &ua->policy, dir);
  890. ua->aalgos = xt->aalgos;
  891. ua->ealgos = xt->ealgos;
  892. ua->calgos = xt->calgos;
  893. ua->seq = x->km.seq = seq;
  894. if (copy_to_user_tmpl(xp, skb) < 0)
  895. goto nlmsg_failure;
  896. nlh->nlmsg_len = skb->tail - b;
  897. return skb->len;
  898. nlmsg_failure:
  899. skb_trim(skb, b - skb->data);
  900. return -1;
  901. }
  902. static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
  903. struct xfrm_policy *xp, int dir)
  904. {
  905. struct sk_buff *skb;
  906. size_t len;
  907. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  908. len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
  909. skb = alloc_skb(len, GFP_ATOMIC);
  910. if (skb == NULL)
  911. return -ENOMEM;
  912. if (build_acquire(skb, x, xt, xp, dir) < 0)
  913. BUG();
  914. NETLINK_CB(skb).dst_groups = XFRMGRP_ACQUIRE;
  915. return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_ACQUIRE, GFP_ATOMIC);
  916. }
  917. /* User gives us xfrm_user_policy_info followed by an array of 0
  918. * or more templates.
  919. */
  920. static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
  921. u8 *data, int len, int *dir)
  922. {
  923. struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
  924. struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
  925. struct xfrm_policy *xp;
  926. int nr;
  927. switch (family) {
  928. case AF_INET:
  929. if (opt != IP_XFRM_POLICY) {
  930. *dir = -EOPNOTSUPP;
  931. return NULL;
  932. }
  933. break;
  934. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  935. case AF_INET6:
  936. if (opt != IPV6_XFRM_POLICY) {
  937. *dir = -EOPNOTSUPP;
  938. return NULL;
  939. }
  940. break;
  941. #endif
  942. default:
  943. *dir = -EINVAL;
  944. return NULL;
  945. }
  946. *dir = -EINVAL;
  947. if (len < sizeof(*p) ||
  948. verify_newpolicy_info(p))
  949. return NULL;
  950. nr = ((len - sizeof(*p)) / sizeof(*ut));
  951. if (nr > XFRM_MAX_DEPTH)
  952. return NULL;
  953. xp = xfrm_policy_alloc(GFP_KERNEL);
  954. if (xp == NULL) {
  955. *dir = -ENOBUFS;
  956. return NULL;
  957. }
  958. copy_from_user_policy(xp, p);
  959. copy_templates(xp, ut, nr);
  960. *dir = p->dir;
  961. return xp;
  962. }
  963. static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
  964. int dir, int hard)
  965. {
  966. struct xfrm_user_polexpire *upe;
  967. struct nlmsghdr *nlh;
  968. unsigned char *b = skb->tail;
  969. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
  970. upe = NLMSG_DATA(nlh);
  971. nlh->nlmsg_flags = 0;
  972. copy_to_user_policy(xp, &upe->pol, dir);
  973. if (copy_to_user_tmpl(xp, skb) < 0)
  974. goto nlmsg_failure;
  975. upe->hard = !!hard;
  976. nlh->nlmsg_len = skb->tail - b;
  977. return skb->len;
  978. nlmsg_failure:
  979. skb_trim(skb, b - skb->data);
  980. return -1;
  981. }
  982. static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, int hard)
  983. {
  984. struct sk_buff *skb;
  985. size_t len;
  986. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  987. len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
  988. skb = alloc_skb(len, GFP_ATOMIC);
  989. if (skb == NULL)
  990. return -ENOMEM;
  991. if (build_polexpire(skb, xp, dir, hard) < 0)
  992. BUG();
  993. NETLINK_CB(skb).dst_groups = XFRMGRP_EXPIRE;
  994. return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_EXPIRE, GFP_ATOMIC);
  995. }
  996. static struct xfrm_mgr netlink_mgr = {
  997. .id = "netlink",
  998. .notify = xfrm_send_state_notify,
  999. .acquire = xfrm_send_acquire,
  1000. .compile_policy = xfrm_compile_policy,
  1001. .notify_policy = xfrm_send_policy_notify,
  1002. };
  1003. static int __init xfrm_user_init(void)
  1004. {
  1005. printk(KERN_INFO "Initializing IPsec netlink socket\n");
  1006. xfrm_nl = netlink_kernel_create(NETLINK_XFRM, xfrm_netlink_rcv);
  1007. if (xfrm_nl == NULL)
  1008. return -ENOMEM;
  1009. xfrm_register_km(&netlink_mgr);
  1010. return 0;
  1011. }
  1012. static void __exit xfrm_user_exit(void)
  1013. {
  1014. xfrm_unregister_km(&netlink_mgr);
  1015. sock_release(xfrm_nl->sk_socket);
  1016. }
  1017. module_init(xfrm_user_init);
  1018. module_exit(xfrm_user_exit);
  1019. MODULE_LICENSE("GPL");