xfrm_user.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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. unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
  830. do {
  831. struct sk_buff *skb;
  832. down(&xfrm_cfg_sem);
  833. if (qlen > skb_queue_len(&sk->sk_receive_queue))
  834. qlen = skb_queue_len(&sk->sk_receive_queue);
  835. for (; qlen; qlen--) {
  836. skb = skb_dequeue(&sk->sk_receive_queue);
  837. if (xfrm_user_rcv_skb(skb)) {
  838. if (skb->len)
  839. skb_queue_head(&sk->sk_receive_queue,
  840. skb);
  841. else {
  842. kfree_skb(skb);
  843. qlen--;
  844. }
  845. break;
  846. }
  847. kfree_skb(skb);
  848. }
  849. up(&xfrm_cfg_sem);
  850. } while (qlen);
  851. }
  852. static int build_expire(struct sk_buff *skb, struct xfrm_state *x, int hard)
  853. {
  854. struct xfrm_user_expire *ue;
  855. struct nlmsghdr *nlh;
  856. unsigned char *b = skb->tail;
  857. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_EXPIRE,
  858. sizeof(*ue));
  859. ue = NLMSG_DATA(nlh);
  860. nlh->nlmsg_flags = 0;
  861. copy_to_user_state(x, &ue->state);
  862. ue->hard = (hard != 0) ? 1 : 0;
  863. nlh->nlmsg_len = skb->tail - b;
  864. return skb->len;
  865. nlmsg_failure:
  866. skb_trim(skb, b - skb->data);
  867. return -1;
  868. }
  869. static int xfrm_send_state_notify(struct xfrm_state *x, int hard)
  870. {
  871. struct sk_buff *skb;
  872. skb = alloc_skb(sizeof(struct xfrm_user_expire) + 16, GFP_ATOMIC);
  873. if (skb == NULL)
  874. return -ENOMEM;
  875. if (build_expire(skb, x, hard) < 0)
  876. BUG();
  877. NETLINK_CB(skb).dst_groups = XFRMGRP_EXPIRE;
  878. return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_EXPIRE, GFP_ATOMIC);
  879. }
  880. static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
  881. struct xfrm_tmpl *xt, struct xfrm_policy *xp,
  882. int dir)
  883. {
  884. struct xfrm_user_acquire *ua;
  885. struct nlmsghdr *nlh;
  886. unsigned char *b = skb->tail;
  887. __u32 seq = xfrm_get_acqseq();
  888. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
  889. sizeof(*ua));
  890. ua = NLMSG_DATA(nlh);
  891. nlh->nlmsg_flags = 0;
  892. memcpy(&ua->id, &x->id, sizeof(ua->id));
  893. memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
  894. memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
  895. copy_to_user_policy(xp, &ua->policy, dir);
  896. ua->aalgos = xt->aalgos;
  897. ua->ealgos = xt->ealgos;
  898. ua->calgos = xt->calgos;
  899. ua->seq = x->km.seq = seq;
  900. if (copy_to_user_tmpl(xp, skb) < 0)
  901. goto nlmsg_failure;
  902. nlh->nlmsg_len = skb->tail - b;
  903. return skb->len;
  904. nlmsg_failure:
  905. skb_trim(skb, b - skb->data);
  906. return -1;
  907. }
  908. static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
  909. struct xfrm_policy *xp, int dir)
  910. {
  911. struct sk_buff *skb;
  912. size_t len;
  913. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  914. len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
  915. skb = alloc_skb(len, GFP_ATOMIC);
  916. if (skb == NULL)
  917. return -ENOMEM;
  918. if (build_acquire(skb, x, xt, xp, dir) < 0)
  919. BUG();
  920. NETLINK_CB(skb).dst_groups = XFRMGRP_ACQUIRE;
  921. return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_ACQUIRE, GFP_ATOMIC);
  922. }
  923. /* User gives us xfrm_user_policy_info followed by an array of 0
  924. * or more templates.
  925. */
  926. static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
  927. u8 *data, int len, int *dir)
  928. {
  929. struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
  930. struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
  931. struct xfrm_policy *xp;
  932. int nr;
  933. switch (family) {
  934. case AF_INET:
  935. if (opt != IP_XFRM_POLICY) {
  936. *dir = -EOPNOTSUPP;
  937. return NULL;
  938. }
  939. break;
  940. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  941. case AF_INET6:
  942. if (opt != IPV6_XFRM_POLICY) {
  943. *dir = -EOPNOTSUPP;
  944. return NULL;
  945. }
  946. break;
  947. #endif
  948. default:
  949. *dir = -EINVAL;
  950. return NULL;
  951. }
  952. *dir = -EINVAL;
  953. if (len < sizeof(*p) ||
  954. verify_newpolicy_info(p))
  955. return NULL;
  956. nr = ((len - sizeof(*p)) / sizeof(*ut));
  957. if (nr > XFRM_MAX_DEPTH)
  958. return NULL;
  959. xp = xfrm_policy_alloc(GFP_KERNEL);
  960. if (xp == NULL) {
  961. *dir = -ENOBUFS;
  962. return NULL;
  963. }
  964. copy_from_user_policy(xp, p);
  965. copy_templates(xp, ut, nr);
  966. *dir = p->dir;
  967. return xp;
  968. }
  969. static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
  970. int dir, int hard)
  971. {
  972. struct xfrm_user_polexpire *upe;
  973. struct nlmsghdr *nlh;
  974. unsigned char *b = skb->tail;
  975. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
  976. upe = NLMSG_DATA(nlh);
  977. nlh->nlmsg_flags = 0;
  978. copy_to_user_policy(xp, &upe->pol, dir);
  979. if (copy_to_user_tmpl(xp, skb) < 0)
  980. goto nlmsg_failure;
  981. upe->hard = !!hard;
  982. nlh->nlmsg_len = skb->tail - b;
  983. return skb->len;
  984. nlmsg_failure:
  985. skb_trim(skb, b - skb->data);
  986. return -1;
  987. }
  988. static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, int hard)
  989. {
  990. struct sk_buff *skb;
  991. size_t len;
  992. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  993. len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
  994. skb = alloc_skb(len, GFP_ATOMIC);
  995. if (skb == NULL)
  996. return -ENOMEM;
  997. if (build_polexpire(skb, xp, dir, hard) < 0)
  998. BUG();
  999. NETLINK_CB(skb).dst_groups = XFRMGRP_EXPIRE;
  1000. return netlink_broadcast(xfrm_nl, skb, 0, XFRMGRP_EXPIRE, GFP_ATOMIC);
  1001. }
  1002. static struct xfrm_mgr netlink_mgr = {
  1003. .id = "netlink",
  1004. .notify = xfrm_send_state_notify,
  1005. .acquire = xfrm_send_acquire,
  1006. .compile_policy = xfrm_compile_policy,
  1007. .notify_policy = xfrm_send_policy_notify,
  1008. };
  1009. static int __init xfrm_user_init(void)
  1010. {
  1011. printk(KERN_INFO "Initializing IPsec netlink socket\n");
  1012. xfrm_nl = netlink_kernel_create(NETLINK_XFRM, xfrm_netlink_rcv);
  1013. if (xfrm_nl == NULL)
  1014. return -ENOMEM;
  1015. xfrm_register_km(&netlink_mgr);
  1016. return 0;
  1017. }
  1018. static void __exit xfrm_user_exit(void)
  1019. {
  1020. xfrm_unregister_km(&netlink_mgr);
  1021. sock_release(xfrm_nl->sk_socket);
  1022. }
  1023. module_init(xfrm_user_init);
  1024. module_exit(xfrm_user_exit);
  1025. MODULE_LICENSE("GPL");