xfrm_user.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  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/rtnetlink.h>
  21. #include <linux/pfkeyv2.h>
  22. #include <linux/ipsec.h>
  23. #include <linux/init.h>
  24. #include <linux/security.h>
  25. #include <net/sock.h>
  26. #include <net/xfrm.h>
  27. #include <net/netlink.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 inline int verify_sec_ctx_len(struct rtattr **xfrma)
  75. {
  76. struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
  77. struct xfrm_user_sec_ctx *uctx;
  78. int len = 0;
  79. if (!rt)
  80. return 0;
  81. if (rt->rta_len < sizeof(*uctx))
  82. return -EINVAL;
  83. uctx = RTA_DATA(rt);
  84. if (uctx->ctx_len > PAGE_SIZE)
  85. return -EINVAL;
  86. len += sizeof(struct xfrm_user_sec_ctx);
  87. len += uctx->ctx_len;
  88. if (uctx->len != len)
  89. return -EINVAL;
  90. return 0;
  91. }
  92. static int verify_newsa_info(struct xfrm_usersa_info *p,
  93. struct rtattr **xfrma)
  94. {
  95. int err;
  96. err = -EINVAL;
  97. switch (p->family) {
  98. case AF_INET:
  99. break;
  100. case AF_INET6:
  101. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  102. break;
  103. #else
  104. err = -EAFNOSUPPORT;
  105. goto out;
  106. #endif
  107. default:
  108. goto out;
  109. };
  110. err = -EINVAL;
  111. switch (p->id.proto) {
  112. case IPPROTO_AH:
  113. if (!xfrma[XFRMA_ALG_AUTH-1] ||
  114. xfrma[XFRMA_ALG_CRYPT-1] ||
  115. xfrma[XFRMA_ALG_COMP-1])
  116. goto out;
  117. break;
  118. case IPPROTO_ESP:
  119. if ((!xfrma[XFRMA_ALG_AUTH-1] &&
  120. !xfrma[XFRMA_ALG_CRYPT-1]) ||
  121. xfrma[XFRMA_ALG_COMP-1])
  122. goto out;
  123. break;
  124. case IPPROTO_COMP:
  125. if (!xfrma[XFRMA_ALG_COMP-1] ||
  126. xfrma[XFRMA_ALG_AUTH-1] ||
  127. xfrma[XFRMA_ALG_CRYPT-1])
  128. goto out;
  129. break;
  130. default:
  131. goto out;
  132. };
  133. if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
  134. goto out;
  135. if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
  136. goto out;
  137. if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
  138. goto out;
  139. if ((err = verify_encap_tmpl(xfrma)))
  140. goto out;
  141. if ((err = verify_sec_ctx_len(xfrma)))
  142. goto out;
  143. err = -EINVAL;
  144. switch (p->mode) {
  145. case 0:
  146. case 1:
  147. break;
  148. default:
  149. goto out;
  150. };
  151. err = 0;
  152. out:
  153. return err;
  154. }
  155. static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
  156. struct xfrm_algo_desc *(*get_byname)(char *, int),
  157. struct rtattr *u_arg)
  158. {
  159. struct rtattr *rta = u_arg;
  160. struct xfrm_algo *p, *ualg;
  161. struct xfrm_algo_desc *algo;
  162. int len;
  163. if (!rta)
  164. return 0;
  165. ualg = RTA_DATA(rta);
  166. algo = get_byname(ualg->alg_name, 1);
  167. if (!algo)
  168. return -ENOSYS;
  169. *props = algo->desc.sadb_alg_id;
  170. len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
  171. p = kmalloc(len, GFP_KERNEL);
  172. if (!p)
  173. return -ENOMEM;
  174. memcpy(p, ualg, len);
  175. *algpp = p;
  176. return 0;
  177. }
  178. static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
  179. {
  180. struct rtattr *rta = u_arg;
  181. struct xfrm_encap_tmpl *p, *uencap;
  182. if (!rta)
  183. return 0;
  184. uencap = RTA_DATA(rta);
  185. p = kmalloc(sizeof(*p), GFP_KERNEL);
  186. if (!p)
  187. return -ENOMEM;
  188. memcpy(p, uencap, sizeof(*p));
  189. *encapp = p;
  190. return 0;
  191. }
  192. static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
  193. {
  194. struct xfrm_sec_ctx *xfrm_ctx = xp->security;
  195. int len = 0;
  196. if (xfrm_ctx) {
  197. len += sizeof(struct xfrm_user_sec_ctx);
  198. len += xfrm_ctx->ctx_len;
  199. }
  200. return len;
  201. }
  202. static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
  203. {
  204. struct xfrm_user_sec_ctx *uctx;
  205. if (!u_arg)
  206. return 0;
  207. uctx = RTA_DATA(u_arg);
  208. return security_xfrm_state_alloc(x, uctx);
  209. }
  210. static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  211. {
  212. memcpy(&x->id, &p->id, sizeof(x->id));
  213. memcpy(&x->sel, &p->sel, sizeof(x->sel));
  214. memcpy(&x->lft, &p->lft, sizeof(x->lft));
  215. x->props.mode = p->mode;
  216. x->props.replay_window = p->replay_window;
  217. x->props.reqid = p->reqid;
  218. x->props.family = p->family;
  219. x->props.saddr = p->saddr;
  220. x->props.flags = p->flags;
  221. }
  222. static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
  223. struct rtattr **xfrma,
  224. int *errp)
  225. {
  226. struct xfrm_state *x = xfrm_state_alloc();
  227. int err = -ENOMEM;
  228. if (!x)
  229. goto error_no_put;
  230. copy_from_user_state(x, p);
  231. if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
  232. xfrm_aalg_get_byname,
  233. xfrma[XFRMA_ALG_AUTH-1])))
  234. goto error;
  235. if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
  236. xfrm_ealg_get_byname,
  237. xfrma[XFRMA_ALG_CRYPT-1])))
  238. goto error;
  239. if ((err = attach_one_algo(&x->calg, &x->props.calgo,
  240. xfrm_calg_get_byname,
  241. xfrma[XFRMA_ALG_COMP-1])))
  242. goto error;
  243. if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
  244. goto error;
  245. err = xfrm_init_state(x);
  246. if (err)
  247. goto error;
  248. if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
  249. goto error;
  250. x->km.seq = p->seq;
  251. return x;
  252. error:
  253. x->km.state = XFRM_STATE_DEAD;
  254. xfrm_state_put(x);
  255. error_no_put:
  256. *errp = err;
  257. return NULL;
  258. }
  259. static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  260. {
  261. struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
  262. struct xfrm_state *x;
  263. int err;
  264. struct km_event c;
  265. err = verify_newsa_info(p, (struct rtattr **)xfrma);
  266. if (err)
  267. return err;
  268. x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
  269. if (!x)
  270. return err;
  271. xfrm_state_hold(x);
  272. if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
  273. err = xfrm_state_add(x);
  274. else
  275. err = xfrm_state_update(x);
  276. if (err < 0) {
  277. x->km.state = XFRM_STATE_DEAD;
  278. xfrm_state_put(x);
  279. goto out;
  280. }
  281. c.seq = nlh->nlmsg_seq;
  282. c.pid = nlh->nlmsg_pid;
  283. c.event = nlh->nlmsg_type;
  284. km_state_notify(x, &c);
  285. out:
  286. xfrm_state_put(x);
  287. return err;
  288. }
  289. static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  290. {
  291. struct xfrm_state *x;
  292. int err;
  293. struct km_event c;
  294. struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
  295. x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
  296. if (x == NULL)
  297. return -ESRCH;
  298. if (xfrm_state_kern(x)) {
  299. xfrm_state_put(x);
  300. return -EPERM;
  301. }
  302. err = xfrm_state_delete(x);
  303. if (err < 0) {
  304. xfrm_state_put(x);
  305. return err;
  306. }
  307. c.seq = nlh->nlmsg_seq;
  308. c.pid = nlh->nlmsg_pid;
  309. c.event = nlh->nlmsg_type;
  310. km_state_notify(x, &c);
  311. xfrm_state_put(x);
  312. return err;
  313. }
  314. static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  315. {
  316. memcpy(&p->id, &x->id, sizeof(p->id));
  317. memcpy(&p->sel, &x->sel, sizeof(p->sel));
  318. memcpy(&p->lft, &x->lft, sizeof(p->lft));
  319. memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
  320. memcpy(&p->stats, &x->stats, sizeof(p->stats));
  321. p->saddr = x->props.saddr;
  322. p->mode = x->props.mode;
  323. p->replay_window = x->props.replay_window;
  324. p->reqid = x->props.reqid;
  325. p->family = x->props.family;
  326. p->flags = x->props.flags;
  327. p->seq = x->km.seq;
  328. }
  329. struct xfrm_dump_info {
  330. struct sk_buff *in_skb;
  331. struct sk_buff *out_skb;
  332. u32 nlmsg_seq;
  333. u16 nlmsg_flags;
  334. int start_idx;
  335. int this_idx;
  336. };
  337. static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
  338. {
  339. struct xfrm_dump_info *sp = ptr;
  340. struct sk_buff *in_skb = sp->in_skb;
  341. struct sk_buff *skb = sp->out_skb;
  342. struct xfrm_usersa_info *p;
  343. struct nlmsghdr *nlh;
  344. unsigned char *b = skb->tail;
  345. if (sp->this_idx < sp->start_idx)
  346. goto out;
  347. nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
  348. sp->nlmsg_seq,
  349. XFRM_MSG_NEWSA, sizeof(*p));
  350. nlh->nlmsg_flags = sp->nlmsg_flags;
  351. p = NLMSG_DATA(nlh);
  352. copy_to_user_state(x, p);
  353. if (x->aalg)
  354. RTA_PUT(skb, XFRMA_ALG_AUTH,
  355. sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
  356. if (x->ealg)
  357. RTA_PUT(skb, XFRMA_ALG_CRYPT,
  358. sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
  359. if (x->calg)
  360. RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  361. if (x->encap)
  362. RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  363. if (x->security) {
  364. int ctx_size = sizeof(struct xfrm_sec_ctx) +
  365. x->security->ctx_len;
  366. struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
  367. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  368. uctx->exttype = XFRMA_SEC_CTX;
  369. uctx->len = ctx_size;
  370. uctx->ctx_doi = x->security->ctx_doi;
  371. uctx->ctx_alg = x->security->ctx_alg;
  372. uctx->ctx_len = x->security->ctx_len;
  373. memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
  374. }
  375. nlh->nlmsg_len = skb->tail - b;
  376. out:
  377. sp->this_idx++;
  378. return 0;
  379. nlmsg_failure:
  380. rtattr_failure:
  381. skb_trim(skb, b - skb->data);
  382. return -1;
  383. }
  384. static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
  385. {
  386. struct xfrm_dump_info info;
  387. info.in_skb = cb->skb;
  388. info.out_skb = skb;
  389. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  390. info.nlmsg_flags = NLM_F_MULTI;
  391. info.this_idx = 0;
  392. info.start_idx = cb->args[0];
  393. (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
  394. cb->args[0] = info.this_idx;
  395. return skb->len;
  396. }
  397. static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
  398. struct xfrm_state *x, u32 seq)
  399. {
  400. struct xfrm_dump_info info;
  401. struct sk_buff *skb;
  402. skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
  403. if (!skb)
  404. return ERR_PTR(-ENOMEM);
  405. NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
  406. info.in_skb = in_skb;
  407. info.out_skb = skb;
  408. info.nlmsg_seq = seq;
  409. info.nlmsg_flags = 0;
  410. info.this_idx = info.start_idx = 0;
  411. if (dump_one_state(x, 0, &info)) {
  412. kfree_skb(skb);
  413. return NULL;
  414. }
  415. return skb;
  416. }
  417. static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  418. {
  419. struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
  420. struct xfrm_state *x;
  421. struct sk_buff *resp_skb;
  422. int err;
  423. x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
  424. err = -ESRCH;
  425. if (x == NULL)
  426. goto out_noput;
  427. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  428. if (IS_ERR(resp_skb)) {
  429. err = PTR_ERR(resp_skb);
  430. } else {
  431. err = netlink_unicast(xfrm_nl, resp_skb,
  432. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  433. }
  434. xfrm_state_put(x);
  435. out_noput:
  436. return err;
  437. }
  438. static int verify_userspi_info(struct xfrm_userspi_info *p)
  439. {
  440. switch (p->info.id.proto) {
  441. case IPPROTO_AH:
  442. case IPPROTO_ESP:
  443. break;
  444. case IPPROTO_COMP:
  445. /* IPCOMP spi is 16-bits. */
  446. if (p->max >= 0x10000)
  447. return -EINVAL;
  448. break;
  449. default:
  450. return -EINVAL;
  451. };
  452. if (p->min > p->max)
  453. return -EINVAL;
  454. return 0;
  455. }
  456. static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  457. {
  458. struct xfrm_state *x;
  459. struct xfrm_userspi_info *p;
  460. struct sk_buff *resp_skb;
  461. xfrm_address_t *daddr;
  462. int family;
  463. int err;
  464. p = NLMSG_DATA(nlh);
  465. err = verify_userspi_info(p);
  466. if (err)
  467. goto out_noput;
  468. family = p->info.family;
  469. daddr = &p->info.id.daddr;
  470. x = NULL;
  471. if (p->info.seq) {
  472. x = xfrm_find_acq_byseq(p->info.seq);
  473. if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
  474. xfrm_state_put(x);
  475. x = NULL;
  476. }
  477. }
  478. if (!x)
  479. x = xfrm_find_acq(p->info.mode, p->info.reqid,
  480. p->info.id.proto, daddr,
  481. &p->info.saddr, 1,
  482. family);
  483. err = -ENOENT;
  484. if (x == NULL)
  485. goto out_noput;
  486. resp_skb = ERR_PTR(-ENOENT);
  487. spin_lock_bh(&x->lock);
  488. if (x->km.state != XFRM_STATE_DEAD) {
  489. xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
  490. if (x->id.spi)
  491. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  492. }
  493. spin_unlock_bh(&x->lock);
  494. if (IS_ERR(resp_skb)) {
  495. err = PTR_ERR(resp_skb);
  496. goto out;
  497. }
  498. err = netlink_unicast(xfrm_nl, resp_skb,
  499. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  500. out:
  501. xfrm_state_put(x);
  502. out_noput:
  503. return err;
  504. }
  505. static int verify_policy_dir(__u8 dir)
  506. {
  507. switch (dir) {
  508. case XFRM_POLICY_IN:
  509. case XFRM_POLICY_OUT:
  510. case XFRM_POLICY_FWD:
  511. break;
  512. default:
  513. return -EINVAL;
  514. };
  515. return 0;
  516. }
  517. static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
  518. {
  519. switch (p->share) {
  520. case XFRM_SHARE_ANY:
  521. case XFRM_SHARE_SESSION:
  522. case XFRM_SHARE_USER:
  523. case XFRM_SHARE_UNIQUE:
  524. break;
  525. default:
  526. return -EINVAL;
  527. };
  528. switch (p->action) {
  529. case XFRM_POLICY_ALLOW:
  530. case XFRM_POLICY_BLOCK:
  531. break;
  532. default:
  533. return -EINVAL;
  534. };
  535. switch (p->sel.family) {
  536. case AF_INET:
  537. break;
  538. case AF_INET6:
  539. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  540. break;
  541. #else
  542. return -EAFNOSUPPORT;
  543. #endif
  544. default:
  545. return -EINVAL;
  546. };
  547. return verify_policy_dir(p->dir);
  548. }
  549. static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
  550. {
  551. struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
  552. struct xfrm_user_sec_ctx *uctx;
  553. if (!rt)
  554. return 0;
  555. uctx = RTA_DATA(rt);
  556. return security_xfrm_policy_alloc(pol, uctx);
  557. }
  558. static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
  559. int nr)
  560. {
  561. int i;
  562. xp->xfrm_nr = nr;
  563. for (i = 0; i < nr; i++, ut++) {
  564. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  565. memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
  566. memcpy(&t->saddr, &ut->saddr,
  567. sizeof(xfrm_address_t));
  568. t->reqid = ut->reqid;
  569. t->mode = ut->mode;
  570. t->share = ut->share;
  571. t->optional = ut->optional;
  572. t->aalgos = ut->aalgos;
  573. t->ealgos = ut->ealgos;
  574. t->calgos = ut->calgos;
  575. }
  576. }
  577. static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
  578. {
  579. struct rtattr *rt = xfrma[XFRMA_TMPL-1];
  580. struct xfrm_user_tmpl *utmpl;
  581. int nr;
  582. if (!rt) {
  583. pol->xfrm_nr = 0;
  584. } else {
  585. nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
  586. if (nr > XFRM_MAX_DEPTH)
  587. return -EINVAL;
  588. copy_templates(pol, RTA_DATA(rt), nr);
  589. }
  590. return 0;
  591. }
  592. static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
  593. {
  594. xp->priority = p->priority;
  595. xp->index = p->index;
  596. memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
  597. memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
  598. xp->action = p->action;
  599. xp->flags = p->flags;
  600. xp->family = p->sel.family;
  601. /* XXX xp->share = p->share; */
  602. }
  603. static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
  604. {
  605. memcpy(&p->sel, &xp->selector, sizeof(p->sel));
  606. memcpy(&p->lft, &xp->lft, sizeof(p->lft));
  607. memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
  608. p->priority = xp->priority;
  609. p->index = xp->index;
  610. p->sel.family = xp->family;
  611. p->dir = dir;
  612. p->action = xp->action;
  613. p->flags = xp->flags;
  614. p->share = XFRM_SHARE_ANY; /* XXX xp->share */
  615. }
  616. static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
  617. {
  618. struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
  619. int err;
  620. if (!xp) {
  621. *errp = -ENOMEM;
  622. return NULL;
  623. }
  624. copy_from_user_policy(xp, p);
  625. if (!(err = copy_from_user_tmpl(xp, xfrma)))
  626. err = copy_from_user_sec_ctx(xp, xfrma);
  627. if (err) {
  628. *errp = err;
  629. kfree(xp);
  630. xp = NULL;
  631. }
  632. return xp;
  633. }
  634. static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  635. {
  636. struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
  637. struct xfrm_policy *xp;
  638. struct km_event c;
  639. int err;
  640. int excl;
  641. err = verify_newpolicy_info(p);
  642. if (err)
  643. return err;
  644. err = verify_sec_ctx_len((struct rtattr **)xfrma);
  645. if (err)
  646. return err;
  647. xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
  648. if (!xp)
  649. return err;
  650. /* shouldnt excl be based on nlh flags??
  651. * Aha! this is anti-netlink really i.e more pfkey derived
  652. * in netlink excl is a flag and you wouldnt need
  653. * a type XFRM_MSG_UPDPOLICY - JHS */
  654. excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
  655. err = xfrm_policy_insert(p->dir, xp, excl);
  656. if (err) {
  657. kfree(xp);
  658. return err;
  659. }
  660. c.event = nlh->nlmsg_type;
  661. c.seq = nlh->nlmsg_seq;
  662. c.pid = nlh->nlmsg_pid;
  663. km_policy_notify(xp, p->dir, &c);
  664. xfrm_pol_put(xp);
  665. return 0;
  666. }
  667. static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
  668. {
  669. struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
  670. int i;
  671. if (xp->xfrm_nr == 0)
  672. return 0;
  673. for (i = 0; i < xp->xfrm_nr; i++) {
  674. struct xfrm_user_tmpl *up = &vec[i];
  675. struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
  676. memcpy(&up->id, &kp->id, sizeof(up->id));
  677. up->family = xp->family;
  678. memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
  679. up->reqid = kp->reqid;
  680. up->mode = kp->mode;
  681. up->share = kp->share;
  682. up->optional = kp->optional;
  683. up->aalgos = kp->aalgos;
  684. up->ealgos = kp->ealgos;
  685. up->calgos = kp->calgos;
  686. }
  687. RTA_PUT(skb, XFRMA_TMPL,
  688. (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
  689. vec);
  690. return 0;
  691. rtattr_failure:
  692. return -1;
  693. }
  694. static int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
  695. {
  696. if (xp->security) {
  697. int ctx_size = sizeof(struct xfrm_sec_ctx) +
  698. xp->security->ctx_len;
  699. struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
  700. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  701. uctx->exttype = XFRMA_SEC_CTX;
  702. uctx->len = ctx_size;
  703. uctx->ctx_doi = xp->security->ctx_doi;
  704. uctx->ctx_alg = xp->security->ctx_alg;
  705. uctx->ctx_len = xp->security->ctx_len;
  706. memcpy(uctx + 1, xp->security->ctx_str, xp->security->ctx_len);
  707. }
  708. return 0;
  709. rtattr_failure:
  710. return -1;
  711. }
  712. static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
  713. {
  714. struct xfrm_dump_info *sp = ptr;
  715. struct xfrm_userpolicy_info *p;
  716. struct sk_buff *in_skb = sp->in_skb;
  717. struct sk_buff *skb = sp->out_skb;
  718. struct nlmsghdr *nlh;
  719. unsigned char *b = skb->tail;
  720. if (sp->this_idx < sp->start_idx)
  721. goto out;
  722. nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
  723. sp->nlmsg_seq,
  724. XFRM_MSG_NEWPOLICY, sizeof(*p));
  725. p = NLMSG_DATA(nlh);
  726. nlh->nlmsg_flags = sp->nlmsg_flags;
  727. copy_to_user_policy(xp, p, dir);
  728. if (copy_to_user_tmpl(xp, skb) < 0)
  729. goto nlmsg_failure;
  730. if (copy_to_user_sec_ctx(xp, skb))
  731. goto nlmsg_failure;
  732. nlh->nlmsg_len = skb->tail - b;
  733. out:
  734. sp->this_idx++;
  735. return 0;
  736. nlmsg_failure:
  737. skb_trim(skb, b - skb->data);
  738. return -1;
  739. }
  740. static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
  741. {
  742. struct xfrm_dump_info info;
  743. info.in_skb = cb->skb;
  744. info.out_skb = skb;
  745. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  746. info.nlmsg_flags = NLM_F_MULTI;
  747. info.this_idx = 0;
  748. info.start_idx = cb->args[0];
  749. (void) xfrm_policy_walk(dump_one_policy, &info);
  750. cb->args[0] = info.this_idx;
  751. return skb->len;
  752. }
  753. static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
  754. struct xfrm_policy *xp,
  755. int dir, u32 seq)
  756. {
  757. struct xfrm_dump_info info;
  758. struct sk_buff *skb;
  759. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  760. if (!skb)
  761. return ERR_PTR(-ENOMEM);
  762. NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
  763. info.in_skb = in_skb;
  764. info.out_skb = skb;
  765. info.nlmsg_seq = seq;
  766. info.nlmsg_flags = 0;
  767. info.this_idx = info.start_idx = 0;
  768. if (dump_one_policy(xp, dir, 0, &info) < 0) {
  769. kfree_skb(skb);
  770. return NULL;
  771. }
  772. return skb;
  773. }
  774. static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  775. {
  776. struct xfrm_policy *xp;
  777. struct xfrm_userpolicy_id *p;
  778. int err;
  779. struct km_event c;
  780. int delete;
  781. p = NLMSG_DATA(nlh);
  782. delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
  783. err = verify_policy_dir(p->dir);
  784. if (err)
  785. return err;
  786. if (p->index)
  787. xp = xfrm_policy_byid(p->dir, p->index, delete);
  788. else {
  789. struct rtattr **rtattrs = (struct rtattr **)xfrma;
  790. struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
  791. struct xfrm_policy tmp;
  792. err = verify_sec_ctx_len(rtattrs);
  793. if (err)
  794. return err;
  795. memset(&tmp, 0, sizeof(struct xfrm_policy));
  796. if (rt) {
  797. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  798. if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
  799. return err;
  800. }
  801. xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
  802. security_xfrm_policy_free(&tmp);
  803. }
  804. if (xp == NULL)
  805. return -ENOENT;
  806. if (!delete) {
  807. struct sk_buff *resp_skb;
  808. resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
  809. if (IS_ERR(resp_skb)) {
  810. err = PTR_ERR(resp_skb);
  811. } else {
  812. err = netlink_unicast(xfrm_nl, resp_skb,
  813. NETLINK_CB(skb).pid,
  814. MSG_DONTWAIT);
  815. }
  816. } else {
  817. c.data.byid = p->index;
  818. c.event = nlh->nlmsg_type;
  819. c.seq = nlh->nlmsg_seq;
  820. c.pid = nlh->nlmsg_pid;
  821. km_policy_notify(xp, p->dir, &c);
  822. }
  823. xfrm_pol_put(xp);
  824. return err;
  825. }
  826. static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  827. {
  828. struct km_event c;
  829. struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
  830. xfrm_state_flush(p->proto);
  831. c.data.proto = p->proto;
  832. c.event = nlh->nlmsg_type;
  833. c.seq = nlh->nlmsg_seq;
  834. c.pid = nlh->nlmsg_pid;
  835. km_state_notify(NULL, &c);
  836. return 0;
  837. }
  838. static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  839. {
  840. struct km_event c;
  841. xfrm_policy_flush();
  842. c.event = nlh->nlmsg_type;
  843. c.seq = nlh->nlmsg_seq;
  844. c.pid = nlh->nlmsg_pid;
  845. km_policy_notify(NULL, 0, &c);
  846. return 0;
  847. }
  848. #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
  849. static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
  850. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  851. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  852. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  853. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  854. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  855. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  856. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
  857. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
  858. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
  859. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  860. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  861. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
  862. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  863. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
  864. };
  865. #undef XMSGSIZE
  866. static struct xfrm_link {
  867. int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
  868. int (*dump)(struct sk_buff *, struct netlink_callback *);
  869. } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
  870. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  871. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
  872. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
  873. .dump = xfrm_dump_sa },
  874. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  875. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
  876. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
  877. .dump = xfrm_dump_policy },
  878. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
  879. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  880. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  881. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
  882. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
  883. };
  884. static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
  885. {
  886. struct rtattr *xfrma[XFRMA_MAX];
  887. struct xfrm_link *link;
  888. int type, min_len;
  889. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  890. return 0;
  891. type = nlh->nlmsg_type;
  892. /* A control message: ignore them */
  893. if (type < XFRM_MSG_BASE)
  894. return 0;
  895. /* Unknown message: reply with EINVAL */
  896. if (type > XFRM_MSG_MAX)
  897. goto err_einval;
  898. type -= XFRM_MSG_BASE;
  899. link = &xfrm_dispatch[type];
  900. /* All operations require privileges, even GET */
  901. if (security_netlink_recv(skb)) {
  902. *errp = -EPERM;
  903. return -1;
  904. }
  905. if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
  906. type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
  907. (nlh->nlmsg_flags & NLM_F_DUMP)) {
  908. if (link->dump == NULL)
  909. goto err_einval;
  910. if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
  911. link->dump, NULL)) != 0) {
  912. return -1;
  913. }
  914. netlink_queue_skip(nlh, skb);
  915. return -1;
  916. }
  917. memset(xfrma, 0, sizeof(xfrma));
  918. if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
  919. goto err_einval;
  920. if (nlh->nlmsg_len > min_len) {
  921. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  922. struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
  923. while (RTA_OK(attr, attrlen)) {
  924. unsigned short flavor = attr->rta_type;
  925. if (flavor) {
  926. if (flavor > XFRMA_MAX)
  927. goto err_einval;
  928. xfrma[flavor - 1] = attr;
  929. }
  930. attr = RTA_NEXT(attr, attrlen);
  931. }
  932. }
  933. if (link->doit == NULL)
  934. goto err_einval;
  935. *errp = link->doit(skb, nlh, (void **) &xfrma);
  936. return *errp;
  937. err_einval:
  938. *errp = -EINVAL;
  939. return -1;
  940. }
  941. static void xfrm_netlink_rcv(struct sock *sk, int len)
  942. {
  943. unsigned int qlen = 0;
  944. do {
  945. down(&xfrm_cfg_sem);
  946. netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
  947. up(&xfrm_cfg_sem);
  948. } while (qlen);
  949. }
  950. static int build_expire(struct sk_buff *skb, struct xfrm_state *x, int hard)
  951. {
  952. struct xfrm_user_expire *ue;
  953. struct nlmsghdr *nlh;
  954. unsigned char *b = skb->tail;
  955. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_EXPIRE,
  956. sizeof(*ue));
  957. ue = NLMSG_DATA(nlh);
  958. nlh->nlmsg_flags = 0;
  959. copy_to_user_state(x, &ue->state);
  960. ue->hard = (hard != 0) ? 1 : 0;
  961. nlh->nlmsg_len = skb->tail - b;
  962. return skb->len;
  963. nlmsg_failure:
  964. skb_trim(skb, b - skb->data);
  965. return -1;
  966. }
  967. static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
  968. {
  969. struct sk_buff *skb;
  970. int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
  971. skb = alloc_skb(len, GFP_ATOMIC);
  972. if (skb == NULL)
  973. return -ENOMEM;
  974. if (build_expire(skb, x, c->data.hard) < 0)
  975. BUG();
  976. NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
  977. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  978. }
  979. static int xfrm_notify_sa_flush(struct km_event *c)
  980. {
  981. struct xfrm_usersa_flush *p;
  982. struct nlmsghdr *nlh;
  983. struct sk_buff *skb;
  984. unsigned char *b;
  985. int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
  986. skb = alloc_skb(len, GFP_ATOMIC);
  987. if (skb == NULL)
  988. return -ENOMEM;
  989. b = skb->tail;
  990. nlh = NLMSG_PUT(skb, c->pid, c->seq,
  991. XFRM_MSG_FLUSHSA, sizeof(*p));
  992. nlh->nlmsg_flags = 0;
  993. p = NLMSG_DATA(nlh);
  994. p->proto = c->data.proto;
  995. nlh->nlmsg_len = skb->tail - b;
  996. NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
  997. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  998. nlmsg_failure:
  999. kfree_skb(skb);
  1000. return -1;
  1001. }
  1002. static int inline xfrm_sa_len(struct xfrm_state *x)
  1003. {
  1004. int l = 0;
  1005. if (x->aalg)
  1006. l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
  1007. if (x->ealg)
  1008. l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
  1009. if (x->calg)
  1010. l += RTA_SPACE(sizeof(*x->calg));
  1011. if (x->encap)
  1012. l += RTA_SPACE(sizeof(*x->encap));
  1013. return l;
  1014. }
  1015. static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
  1016. {
  1017. struct xfrm_usersa_info *p;
  1018. struct xfrm_usersa_id *id;
  1019. struct nlmsghdr *nlh;
  1020. struct sk_buff *skb;
  1021. unsigned char *b;
  1022. int len = xfrm_sa_len(x);
  1023. int headlen;
  1024. headlen = sizeof(*p);
  1025. if (c->event == XFRM_MSG_DELSA) {
  1026. len += RTA_SPACE(headlen);
  1027. headlen = sizeof(*id);
  1028. }
  1029. len += NLMSG_SPACE(headlen);
  1030. skb = alloc_skb(len, GFP_ATOMIC);
  1031. if (skb == NULL)
  1032. return -ENOMEM;
  1033. b = skb->tail;
  1034. nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
  1035. nlh->nlmsg_flags = 0;
  1036. p = NLMSG_DATA(nlh);
  1037. if (c->event == XFRM_MSG_DELSA) {
  1038. id = NLMSG_DATA(nlh);
  1039. memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
  1040. id->spi = x->id.spi;
  1041. id->family = x->props.family;
  1042. id->proto = x->id.proto;
  1043. p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
  1044. }
  1045. copy_to_user_state(x, p);
  1046. if (x->aalg)
  1047. RTA_PUT(skb, XFRMA_ALG_AUTH,
  1048. sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
  1049. if (x->ealg)
  1050. RTA_PUT(skb, XFRMA_ALG_CRYPT,
  1051. sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
  1052. if (x->calg)
  1053. RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  1054. if (x->encap)
  1055. RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  1056. nlh->nlmsg_len = skb->tail - b;
  1057. NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
  1058. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  1059. nlmsg_failure:
  1060. rtattr_failure:
  1061. kfree_skb(skb);
  1062. return -1;
  1063. }
  1064. static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
  1065. {
  1066. switch (c->event) {
  1067. case XFRM_MSG_EXPIRE:
  1068. return xfrm_exp_state_notify(x, c);
  1069. case XFRM_MSG_DELSA:
  1070. case XFRM_MSG_UPDSA:
  1071. case XFRM_MSG_NEWSA:
  1072. return xfrm_notify_sa(x, c);
  1073. case XFRM_MSG_FLUSHSA:
  1074. return xfrm_notify_sa_flush(c);
  1075. default:
  1076. printk("xfrm_user: Unknown SA event %d\n", c->event);
  1077. break;
  1078. }
  1079. return 0;
  1080. }
  1081. static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
  1082. struct xfrm_tmpl *xt, struct xfrm_policy *xp,
  1083. int dir)
  1084. {
  1085. struct xfrm_user_acquire *ua;
  1086. struct nlmsghdr *nlh;
  1087. unsigned char *b = skb->tail;
  1088. __u32 seq = xfrm_get_acqseq();
  1089. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
  1090. sizeof(*ua));
  1091. ua = NLMSG_DATA(nlh);
  1092. nlh->nlmsg_flags = 0;
  1093. memcpy(&ua->id, &x->id, sizeof(ua->id));
  1094. memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
  1095. memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
  1096. copy_to_user_policy(xp, &ua->policy, dir);
  1097. ua->aalgos = xt->aalgos;
  1098. ua->ealgos = xt->ealgos;
  1099. ua->calgos = xt->calgos;
  1100. ua->seq = x->km.seq = seq;
  1101. if (copy_to_user_tmpl(xp, skb) < 0)
  1102. goto nlmsg_failure;
  1103. if (copy_to_user_sec_ctx(xp, skb))
  1104. goto nlmsg_failure;
  1105. nlh->nlmsg_len = skb->tail - b;
  1106. return skb->len;
  1107. nlmsg_failure:
  1108. skb_trim(skb, b - skb->data);
  1109. return -1;
  1110. }
  1111. static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
  1112. struct xfrm_policy *xp, int dir)
  1113. {
  1114. struct sk_buff *skb;
  1115. size_t len;
  1116. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1117. len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
  1118. len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
  1119. skb = alloc_skb(len, GFP_ATOMIC);
  1120. if (skb == NULL)
  1121. return -ENOMEM;
  1122. if (build_acquire(skb, x, xt, xp, dir) < 0)
  1123. BUG();
  1124. NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
  1125. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
  1126. }
  1127. /* User gives us xfrm_user_policy_info followed by an array of 0
  1128. * or more templates.
  1129. */
  1130. static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
  1131. u8 *data, int len, int *dir)
  1132. {
  1133. struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
  1134. struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
  1135. struct xfrm_policy *xp;
  1136. int nr;
  1137. switch (family) {
  1138. case AF_INET:
  1139. if (opt != IP_XFRM_POLICY) {
  1140. *dir = -EOPNOTSUPP;
  1141. return NULL;
  1142. }
  1143. break;
  1144. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1145. case AF_INET6:
  1146. if (opt != IPV6_XFRM_POLICY) {
  1147. *dir = -EOPNOTSUPP;
  1148. return NULL;
  1149. }
  1150. break;
  1151. #endif
  1152. default:
  1153. *dir = -EINVAL;
  1154. return NULL;
  1155. }
  1156. *dir = -EINVAL;
  1157. if (len < sizeof(*p) ||
  1158. verify_newpolicy_info(p))
  1159. return NULL;
  1160. nr = ((len - sizeof(*p)) / sizeof(*ut));
  1161. if (nr > XFRM_MAX_DEPTH)
  1162. return NULL;
  1163. if (p->dir > XFRM_POLICY_OUT)
  1164. return NULL;
  1165. xp = xfrm_policy_alloc(GFP_KERNEL);
  1166. if (xp == NULL) {
  1167. *dir = -ENOBUFS;
  1168. return NULL;
  1169. }
  1170. copy_from_user_policy(xp, p);
  1171. copy_templates(xp, ut, nr);
  1172. *dir = p->dir;
  1173. return xp;
  1174. }
  1175. static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
  1176. int dir, int hard)
  1177. {
  1178. struct xfrm_user_polexpire *upe;
  1179. struct nlmsghdr *nlh;
  1180. unsigned char *b = skb->tail;
  1181. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
  1182. upe = NLMSG_DATA(nlh);
  1183. nlh->nlmsg_flags = 0;
  1184. copy_to_user_policy(xp, &upe->pol, dir);
  1185. if (copy_to_user_tmpl(xp, skb) < 0)
  1186. goto nlmsg_failure;
  1187. if (copy_to_user_sec_ctx(xp, skb))
  1188. goto nlmsg_failure;
  1189. upe->hard = !!hard;
  1190. nlh->nlmsg_len = skb->tail - b;
  1191. return skb->len;
  1192. nlmsg_failure:
  1193. skb_trim(skb, b - skb->data);
  1194. return -1;
  1195. }
  1196. static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  1197. {
  1198. struct sk_buff *skb;
  1199. size_t len;
  1200. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1201. len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
  1202. len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
  1203. skb = alloc_skb(len, GFP_ATOMIC);
  1204. if (skb == NULL)
  1205. return -ENOMEM;
  1206. if (build_polexpire(skb, xp, dir, c->data.hard) < 0)
  1207. BUG();
  1208. NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
  1209. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  1210. }
  1211. static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
  1212. {
  1213. struct xfrm_userpolicy_info *p;
  1214. struct xfrm_userpolicy_id *id;
  1215. struct nlmsghdr *nlh;
  1216. struct sk_buff *skb;
  1217. unsigned char *b;
  1218. int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1219. int headlen;
  1220. headlen = sizeof(*p);
  1221. if (c->event == XFRM_MSG_DELPOLICY) {
  1222. len += RTA_SPACE(headlen);
  1223. headlen = sizeof(*id);
  1224. }
  1225. len += NLMSG_SPACE(headlen);
  1226. skb = alloc_skb(len, GFP_ATOMIC);
  1227. if (skb == NULL)
  1228. return -ENOMEM;
  1229. b = skb->tail;
  1230. nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
  1231. p = NLMSG_DATA(nlh);
  1232. if (c->event == XFRM_MSG_DELPOLICY) {
  1233. id = NLMSG_DATA(nlh);
  1234. memset(id, 0, sizeof(*id));
  1235. id->dir = dir;
  1236. if (c->data.byid)
  1237. id->index = xp->index;
  1238. else
  1239. memcpy(&id->sel, &xp->selector, sizeof(id->sel));
  1240. p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
  1241. }
  1242. nlh->nlmsg_flags = 0;
  1243. copy_to_user_policy(xp, p, dir);
  1244. if (copy_to_user_tmpl(xp, skb) < 0)
  1245. goto nlmsg_failure;
  1246. nlh->nlmsg_len = skb->tail - b;
  1247. NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
  1248. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  1249. nlmsg_failure:
  1250. rtattr_failure:
  1251. kfree_skb(skb);
  1252. return -1;
  1253. }
  1254. static int xfrm_notify_policy_flush(struct km_event *c)
  1255. {
  1256. struct nlmsghdr *nlh;
  1257. struct sk_buff *skb;
  1258. unsigned char *b;
  1259. int len = NLMSG_LENGTH(0);
  1260. skb = alloc_skb(len, GFP_ATOMIC);
  1261. if (skb == NULL)
  1262. return -ENOMEM;
  1263. b = skb->tail;
  1264. nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
  1265. nlh->nlmsg_len = skb->tail - b;
  1266. NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
  1267. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  1268. nlmsg_failure:
  1269. kfree_skb(skb);
  1270. return -1;
  1271. }
  1272. static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  1273. {
  1274. switch (c->event) {
  1275. case XFRM_MSG_NEWPOLICY:
  1276. case XFRM_MSG_UPDPOLICY:
  1277. case XFRM_MSG_DELPOLICY:
  1278. return xfrm_notify_policy(xp, dir, c);
  1279. case XFRM_MSG_FLUSHPOLICY:
  1280. return xfrm_notify_policy_flush(c);
  1281. case XFRM_MSG_POLEXPIRE:
  1282. return xfrm_exp_policy_notify(xp, dir, c);
  1283. default:
  1284. printk("xfrm_user: Unknown Policy event %d\n", c->event);
  1285. }
  1286. return 0;
  1287. }
  1288. static struct xfrm_mgr netlink_mgr = {
  1289. .id = "netlink",
  1290. .notify = xfrm_send_state_notify,
  1291. .acquire = xfrm_send_acquire,
  1292. .compile_policy = xfrm_compile_policy,
  1293. .notify_policy = xfrm_send_policy_notify,
  1294. };
  1295. static int __init xfrm_user_init(void)
  1296. {
  1297. printk(KERN_INFO "Initializing IPsec netlink socket\n");
  1298. xfrm_nl = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
  1299. xfrm_netlink_rcv, THIS_MODULE);
  1300. if (xfrm_nl == NULL)
  1301. return -ENOMEM;
  1302. xfrm_register_km(&netlink_mgr);
  1303. return 0;
  1304. }
  1305. static void __exit xfrm_user_exit(void)
  1306. {
  1307. xfrm_unregister_km(&netlink_mgr);
  1308. sock_release(xfrm_nl->sk_socket);
  1309. }
  1310. module_init(xfrm_user_init);
  1311. module_exit(xfrm_user_exit);
  1312. MODULE_LICENSE("GPL");
  1313. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);