xfrm_user.c 27 KB

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