xfrm_user.c 27 KB

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