xfrm_user.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979
  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 int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
  30. {
  31. struct rtattr *rt = xfrma[type - 1];
  32. struct xfrm_algo *algp;
  33. int len;
  34. if (!rt)
  35. return 0;
  36. len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
  37. if (len < 0)
  38. return -EINVAL;
  39. algp = RTA_DATA(rt);
  40. len -= (algp->alg_key_len + 7U) / 8;
  41. if (len < 0)
  42. return -EINVAL;
  43. switch (type) {
  44. case XFRMA_ALG_AUTH:
  45. if (!algp->alg_key_len &&
  46. strcmp(algp->alg_name, "digest_null") != 0)
  47. return -EINVAL;
  48. break;
  49. case XFRMA_ALG_CRYPT:
  50. if (!algp->alg_key_len &&
  51. strcmp(algp->alg_name, "cipher_null") != 0)
  52. return -EINVAL;
  53. break;
  54. case XFRMA_ALG_COMP:
  55. /* Zero length keys are legal. */
  56. break;
  57. default:
  58. return -EINVAL;
  59. };
  60. algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
  61. return 0;
  62. }
  63. static int verify_encap_tmpl(struct rtattr **xfrma)
  64. {
  65. struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
  66. struct xfrm_encap_tmpl *encap;
  67. if (!rt)
  68. return 0;
  69. if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
  70. return -EINVAL;
  71. return 0;
  72. }
  73. static inline int verify_sec_ctx_len(struct rtattr **xfrma)
  74. {
  75. struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
  76. struct xfrm_user_sec_ctx *uctx;
  77. int len = 0;
  78. if (!rt)
  79. return 0;
  80. if (rt->rta_len < sizeof(*uctx))
  81. return -EINVAL;
  82. uctx = RTA_DATA(rt);
  83. len += sizeof(struct xfrm_user_sec_ctx);
  84. len += uctx->ctx_len;
  85. if (uctx->len != len)
  86. return -EINVAL;
  87. return 0;
  88. }
  89. static int verify_newsa_info(struct xfrm_usersa_info *p,
  90. struct rtattr **xfrma)
  91. {
  92. int err;
  93. err = -EINVAL;
  94. switch (p->family) {
  95. case AF_INET:
  96. break;
  97. case AF_INET6:
  98. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  99. break;
  100. #else
  101. err = -EAFNOSUPPORT;
  102. goto out;
  103. #endif
  104. default:
  105. goto out;
  106. };
  107. err = -EINVAL;
  108. switch (p->id.proto) {
  109. case IPPROTO_AH:
  110. if (!xfrma[XFRMA_ALG_AUTH-1] ||
  111. xfrma[XFRMA_ALG_CRYPT-1] ||
  112. xfrma[XFRMA_ALG_COMP-1])
  113. goto out;
  114. break;
  115. case IPPROTO_ESP:
  116. if ((!xfrma[XFRMA_ALG_AUTH-1] &&
  117. !xfrma[XFRMA_ALG_CRYPT-1]) ||
  118. xfrma[XFRMA_ALG_COMP-1])
  119. goto out;
  120. break;
  121. case IPPROTO_COMP:
  122. if (!xfrma[XFRMA_ALG_COMP-1] ||
  123. xfrma[XFRMA_ALG_AUTH-1] ||
  124. xfrma[XFRMA_ALG_CRYPT-1])
  125. goto out;
  126. break;
  127. default:
  128. goto out;
  129. };
  130. if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
  131. goto out;
  132. if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
  133. goto out;
  134. if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
  135. goto out;
  136. if ((err = verify_encap_tmpl(xfrma)))
  137. goto out;
  138. if ((err = verify_sec_ctx_len(xfrma)))
  139. goto out;
  140. err = -EINVAL;
  141. switch (p->mode) {
  142. case 0:
  143. case 1:
  144. break;
  145. default:
  146. goto out;
  147. };
  148. err = 0;
  149. out:
  150. return err;
  151. }
  152. static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
  153. struct xfrm_algo_desc *(*get_byname)(char *, int),
  154. struct rtattr *u_arg)
  155. {
  156. struct rtattr *rta = u_arg;
  157. struct xfrm_algo *p, *ualg;
  158. struct xfrm_algo_desc *algo;
  159. int len;
  160. if (!rta)
  161. return 0;
  162. ualg = RTA_DATA(rta);
  163. algo = get_byname(ualg->alg_name, 1);
  164. if (!algo)
  165. return -ENOSYS;
  166. *props = algo->desc.sadb_alg_id;
  167. len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
  168. p = kmalloc(len, GFP_KERNEL);
  169. if (!p)
  170. return -ENOMEM;
  171. memcpy(p, ualg, len);
  172. *algpp = p;
  173. return 0;
  174. }
  175. static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
  176. {
  177. struct rtattr *rta = u_arg;
  178. struct xfrm_encap_tmpl *p, *uencap;
  179. if (!rta)
  180. return 0;
  181. uencap = RTA_DATA(rta);
  182. p = kmalloc(sizeof(*p), GFP_KERNEL);
  183. if (!p)
  184. return -ENOMEM;
  185. memcpy(p, uencap, sizeof(*p));
  186. *encapp = p;
  187. return 0;
  188. }
  189. static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
  190. {
  191. struct xfrm_sec_ctx *xfrm_ctx = xp->security;
  192. int len = 0;
  193. if (xfrm_ctx) {
  194. len += sizeof(struct xfrm_user_sec_ctx);
  195. len += xfrm_ctx->ctx_len;
  196. }
  197. return len;
  198. }
  199. static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
  200. {
  201. struct xfrm_user_sec_ctx *uctx;
  202. if (!u_arg)
  203. return 0;
  204. uctx = RTA_DATA(u_arg);
  205. return security_xfrm_state_alloc(x, uctx);
  206. }
  207. static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  208. {
  209. memcpy(&x->id, &p->id, sizeof(x->id));
  210. memcpy(&x->sel, &p->sel, sizeof(x->sel));
  211. memcpy(&x->lft, &p->lft, sizeof(x->lft));
  212. x->props.mode = p->mode;
  213. x->props.replay_window = p->replay_window;
  214. x->props.reqid = p->reqid;
  215. x->props.family = p->family;
  216. x->props.saddr = p->saddr;
  217. x->props.flags = p->flags;
  218. }
  219. /*
  220. * someday when pfkey also has support, we could have the code
  221. * somehow made shareable and move it to xfrm_state.c - JHS
  222. *
  223. */
  224. static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
  225. {
  226. int err = - EINVAL;
  227. struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
  228. struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
  229. struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
  230. struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
  231. if (rp) {
  232. struct xfrm_replay_state *replay;
  233. if (RTA_PAYLOAD(rp) < sizeof(*replay))
  234. goto error;
  235. replay = RTA_DATA(rp);
  236. memcpy(&x->replay, replay, sizeof(*replay));
  237. memcpy(&x->preplay, replay, sizeof(*replay));
  238. }
  239. if (lt) {
  240. struct xfrm_lifetime_cur *ltime;
  241. if (RTA_PAYLOAD(lt) < sizeof(*ltime))
  242. goto error;
  243. ltime = RTA_DATA(lt);
  244. x->curlft.bytes = ltime->bytes;
  245. x->curlft.packets = ltime->packets;
  246. x->curlft.add_time = ltime->add_time;
  247. x->curlft.use_time = ltime->use_time;
  248. }
  249. if (et) {
  250. if (RTA_PAYLOAD(et) < sizeof(u32))
  251. goto error;
  252. x->replay_maxage = *(u32*)RTA_DATA(et);
  253. }
  254. if (rt) {
  255. if (RTA_PAYLOAD(rt) < sizeof(u32))
  256. goto error;
  257. x->replay_maxdiff = *(u32*)RTA_DATA(rt);
  258. }
  259. return 0;
  260. error:
  261. return err;
  262. }
  263. static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
  264. struct rtattr **xfrma,
  265. int *errp)
  266. {
  267. struct xfrm_state *x = xfrm_state_alloc();
  268. int err = -ENOMEM;
  269. if (!x)
  270. goto error_no_put;
  271. copy_from_user_state(x, p);
  272. if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
  273. xfrm_aalg_get_byname,
  274. xfrma[XFRMA_ALG_AUTH-1])))
  275. goto error;
  276. if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
  277. xfrm_ealg_get_byname,
  278. xfrma[XFRMA_ALG_CRYPT-1])))
  279. goto error;
  280. if ((err = attach_one_algo(&x->calg, &x->props.calgo,
  281. xfrm_calg_get_byname,
  282. xfrma[XFRMA_ALG_COMP-1])))
  283. goto error;
  284. if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
  285. goto error;
  286. err = xfrm_init_state(x);
  287. if (err)
  288. goto error;
  289. if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
  290. goto error;
  291. x->km.seq = p->seq;
  292. x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
  293. /* sysctl_xfrm_aevent_etime is in 100ms units */
  294. x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
  295. x->preplay.bitmap = 0;
  296. x->preplay.seq = x->replay.seq+x->replay_maxdiff;
  297. x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
  298. /* override default values from above */
  299. err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
  300. if (err < 0)
  301. goto error;
  302. return x;
  303. error:
  304. x->km.state = XFRM_STATE_DEAD;
  305. xfrm_state_put(x);
  306. error_no_put:
  307. *errp = err;
  308. return NULL;
  309. }
  310. static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  311. {
  312. struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
  313. struct xfrm_state *x;
  314. int err;
  315. struct km_event c;
  316. err = verify_newsa_info(p, (struct rtattr **)xfrma);
  317. if (err)
  318. return err;
  319. x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
  320. if (!x)
  321. return err;
  322. xfrm_state_hold(x);
  323. if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
  324. err = xfrm_state_add(x);
  325. else
  326. err = xfrm_state_update(x);
  327. if (err < 0) {
  328. x->km.state = XFRM_STATE_DEAD;
  329. __xfrm_state_put(x);
  330. goto out;
  331. }
  332. c.seq = nlh->nlmsg_seq;
  333. c.pid = nlh->nlmsg_pid;
  334. c.event = nlh->nlmsg_type;
  335. km_state_notify(x, &c);
  336. out:
  337. xfrm_state_put(x);
  338. return err;
  339. }
  340. static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  341. {
  342. struct xfrm_state *x;
  343. int err;
  344. struct km_event c;
  345. struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
  346. x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
  347. if (x == NULL)
  348. return -ESRCH;
  349. if (xfrm_state_kern(x)) {
  350. xfrm_state_put(x);
  351. return -EPERM;
  352. }
  353. err = xfrm_state_delete(x);
  354. if (err < 0) {
  355. xfrm_state_put(x);
  356. return err;
  357. }
  358. c.seq = nlh->nlmsg_seq;
  359. c.pid = nlh->nlmsg_pid;
  360. c.event = nlh->nlmsg_type;
  361. km_state_notify(x, &c);
  362. xfrm_state_put(x);
  363. return err;
  364. }
  365. static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  366. {
  367. memcpy(&p->id, &x->id, sizeof(p->id));
  368. memcpy(&p->sel, &x->sel, sizeof(p->sel));
  369. memcpy(&p->lft, &x->lft, sizeof(p->lft));
  370. memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
  371. memcpy(&p->stats, &x->stats, sizeof(p->stats));
  372. p->saddr = x->props.saddr;
  373. p->mode = x->props.mode;
  374. p->replay_window = x->props.replay_window;
  375. p->reqid = x->props.reqid;
  376. p->family = x->props.family;
  377. p->flags = x->props.flags;
  378. p->seq = x->km.seq;
  379. }
  380. struct xfrm_dump_info {
  381. struct sk_buff *in_skb;
  382. struct sk_buff *out_skb;
  383. u32 nlmsg_seq;
  384. u16 nlmsg_flags;
  385. int start_idx;
  386. int this_idx;
  387. };
  388. static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
  389. {
  390. struct xfrm_dump_info *sp = ptr;
  391. struct sk_buff *in_skb = sp->in_skb;
  392. struct sk_buff *skb = sp->out_skb;
  393. struct xfrm_usersa_info *p;
  394. struct nlmsghdr *nlh;
  395. unsigned char *b = skb->tail;
  396. if (sp->this_idx < sp->start_idx)
  397. goto out;
  398. nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
  399. sp->nlmsg_seq,
  400. XFRM_MSG_NEWSA, sizeof(*p));
  401. nlh->nlmsg_flags = sp->nlmsg_flags;
  402. p = NLMSG_DATA(nlh);
  403. copy_to_user_state(x, p);
  404. if (x->aalg)
  405. RTA_PUT(skb, XFRMA_ALG_AUTH,
  406. sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
  407. if (x->ealg)
  408. RTA_PUT(skb, XFRMA_ALG_CRYPT,
  409. sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
  410. if (x->calg)
  411. RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  412. if (x->encap)
  413. RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  414. if (x->security) {
  415. int ctx_size = sizeof(struct xfrm_sec_ctx) +
  416. x->security->ctx_len;
  417. struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
  418. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  419. uctx->exttype = XFRMA_SEC_CTX;
  420. uctx->len = ctx_size;
  421. uctx->ctx_doi = x->security->ctx_doi;
  422. uctx->ctx_alg = x->security->ctx_alg;
  423. uctx->ctx_len = x->security->ctx_len;
  424. memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
  425. }
  426. nlh->nlmsg_len = skb->tail - b;
  427. out:
  428. sp->this_idx++;
  429. return 0;
  430. nlmsg_failure:
  431. rtattr_failure:
  432. skb_trim(skb, b - skb->data);
  433. return -1;
  434. }
  435. static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
  436. {
  437. struct xfrm_dump_info info;
  438. info.in_skb = cb->skb;
  439. info.out_skb = skb;
  440. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  441. info.nlmsg_flags = NLM_F_MULTI;
  442. info.this_idx = 0;
  443. info.start_idx = cb->args[0];
  444. (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
  445. cb->args[0] = info.this_idx;
  446. return skb->len;
  447. }
  448. static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
  449. struct xfrm_state *x, u32 seq)
  450. {
  451. struct xfrm_dump_info info;
  452. struct sk_buff *skb;
  453. skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
  454. if (!skb)
  455. return ERR_PTR(-ENOMEM);
  456. NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
  457. info.in_skb = in_skb;
  458. info.out_skb = skb;
  459. info.nlmsg_seq = seq;
  460. info.nlmsg_flags = 0;
  461. info.this_idx = info.start_idx = 0;
  462. if (dump_one_state(x, 0, &info)) {
  463. kfree_skb(skb);
  464. return NULL;
  465. }
  466. return skb;
  467. }
  468. static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  469. {
  470. struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
  471. struct xfrm_state *x;
  472. struct sk_buff *resp_skb;
  473. int err;
  474. x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
  475. err = -ESRCH;
  476. if (x == NULL)
  477. goto out_noput;
  478. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  479. if (IS_ERR(resp_skb)) {
  480. err = PTR_ERR(resp_skb);
  481. } else {
  482. err = netlink_unicast(xfrm_nl, resp_skb,
  483. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  484. }
  485. xfrm_state_put(x);
  486. out_noput:
  487. return err;
  488. }
  489. static int verify_userspi_info(struct xfrm_userspi_info *p)
  490. {
  491. switch (p->info.id.proto) {
  492. case IPPROTO_AH:
  493. case IPPROTO_ESP:
  494. break;
  495. case IPPROTO_COMP:
  496. /* IPCOMP spi is 16-bits. */
  497. if (p->max >= 0x10000)
  498. return -EINVAL;
  499. break;
  500. default:
  501. return -EINVAL;
  502. };
  503. if (p->min > p->max)
  504. return -EINVAL;
  505. return 0;
  506. }
  507. static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  508. {
  509. struct xfrm_state *x;
  510. struct xfrm_userspi_info *p;
  511. struct sk_buff *resp_skb;
  512. xfrm_address_t *daddr;
  513. int family;
  514. int err;
  515. p = NLMSG_DATA(nlh);
  516. err = verify_userspi_info(p);
  517. if (err)
  518. goto out_noput;
  519. family = p->info.family;
  520. daddr = &p->info.id.daddr;
  521. x = NULL;
  522. if (p->info.seq) {
  523. x = xfrm_find_acq_byseq(p->info.seq);
  524. if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
  525. xfrm_state_put(x);
  526. x = NULL;
  527. }
  528. }
  529. if (!x)
  530. x = xfrm_find_acq(p->info.mode, p->info.reqid,
  531. p->info.id.proto, daddr,
  532. &p->info.saddr, 1,
  533. family);
  534. err = -ENOENT;
  535. if (x == NULL)
  536. goto out_noput;
  537. resp_skb = ERR_PTR(-ENOENT);
  538. spin_lock_bh(&x->lock);
  539. if (x->km.state != XFRM_STATE_DEAD) {
  540. xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
  541. if (x->id.spi)
  542. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  543. }
  544. spin_unlock_bh(&x->lock);
  545. if (IS_ERR(resp_skb)) {
  546. err = PTR_ERR(resp_skb);
  547. goto out;
  548. }
  549. err = netlink_unicast(xfrm_nl, resp_skb,
  550. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  551. out:
  552. xfrm_state_put(x);
  553. out_noput:
  554. return err;
  555. }
  556. static int verify_policy_dir(__u8 dir)
  557. {
  558. switch (dir) {
  559. case XFRM_POLICY_IN:
  560. case XFRM_POLICY_OUT:
  561. case XFRM_POLICY_FWD:
  562. break;
  563. default:
  564. return -EINVAL;
  565. };
  566. return 0;
  567. }
  568. static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
  569. {
  570. switch (p->share) {
  571. case XFRM_SHARE_ANY:
  572. case XFRM_SHARE_SESSION:
  573. case XFRM_SHARE_USER:
  574. case XFRM_SHARE_UNIQUE:
  575. break;
  576. default:
  577. return -EINVAL;
  578. };
  579. switch (p->action) {
  580. case XFRM_POLICY_ALLOW:
  581. case XFRM_POLICY_BLOCK:
  582. break;
  583. default:
  584. return -EINVAL;
  585. };
  586. switch (p->sel.family) {
  587. case AF_INET:
  588. break;
  589. case AF_INET6:
  590. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  591. break;
  592. #else
  593. return -EAFNOSUPPORT;
  594. #endif
  595. default:
  596. return -EINVAL;
  597. };
  598. return verify_policy_dir(p->dir);
  599. }
  600. static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
  601. {
  602. struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
  603. struct xfrm_user_sec_ctx *uctx;
  604. if (!rt)
  605. return 0;
  606. uctx = RTA_DATA(rt);
  607. return security_xfrm_policy_alloc(pol, uctx);
  608. }
  609. static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
  610. int nr)
  611. {
  612. int i;
  613. xp->xfrm_nr = nr;
  614. for (i = 0; i < nr; i++, ut++) {
  615. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  616. memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
  617. memcpy(&t->saddr, &ut->saddr,
  618. sizeof(xfrm_address_t));
  619. t->reqid = ut->reqid;
  620. t->mode = ut->mode;
  621. t->share = ut->share;
  622. t->optional = ut->optional;
  623. t->aalgos = ut->aalgos;
  624. t->ealgos = ut->ealgos;
  625. t->calgos = ut->calgos;
  626. }
  627. }
  628. static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
  629. {
  630. struct rtattr *rt = xfrma[XFRMA_TMPL-1];
  631. struct xfrm_user_tmpl *utmpl;
  632. int nr;
  633. if (!rt) {
  634. pol->xfrm_nr = 0;
  635. } else {
  636. nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
  637. if (nr > XFRM_MAX_DEPTH)
  638. return -EINVAL;
  639. copy_templates(pol, RTA_DATA(rt), nr);
  640. }
  641. return 0;
  642. }
  643. static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
  644. {
  645. xp->priority = p->priority;
  646. xp->index = p->index;
  647. memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
  648. memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
  649. xp->action = p->action;
  650. xp->flags = p->flags;
  651. xp->family = p->sel.family;
  652. /* XXX xp->share = p->share; */
  653. }
  654. static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
  655. {
  656. memcpy(&p->sel, &xp->selector, sizeof(p->sel));
  657. memcpy(&p->lft, &xp->lft, sizeof(p->lft));
  658. memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
  659. p->priority = xp->priority;
  660. p->index = xp->index;
  661. p->sel.family = xp->family;
  662. p->dir = dir;
  663. p->action = xp->action;
  664. p->flags = xp->flags;
  665. p->share = XFRM_SHARE_ANY; /* XXX xp->share */
  666. }
  667. static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
  668. {
  669. struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
  670. int err;
  671. if (!xp) {
  672. *errp = -ENOMEM;
  673. return NULL;
  674. }
  675. copy_from_user_policy(xp, p);
  676. if (!(err = copy_from_user_tmpl(xp, xfrma)))
  677. err = copy_from_user_sec_ctx(xp, xfrma);
  678. if (err) {
  679. *errp = err;
  680. kfree(xp);
  681. xp = NULL;
  682. }
  683. return xp;
  684. }
  685. static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  686. {
  687. struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
  688. struct xfrm_policy *xp;
  689. struct km_event c;
  690. int err;
  691. int excl;
  692. err = verify_newpolicy_info(p);
  693. if (err)
  694. return err;
  695. err = verify_sec_ctx_len((struct rtattr **)xfrma);
  696. if (err)
  697. return err;
  698. xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
  699. if (!xp)
  700. return err;
  701. /* shouldnt excl be based on nlh flags??
  702. * Aha! this is anti-netlink really i.e more pfkey derived
  703. * in netlink excl is a flag and you wouldnt need
  704. * a type XFRM_MSG_UPDPOLICY - JHS */
  705. excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
  706. err = xfrm_policy_insert(p->dir, xp, excl);
  707. if (err) {
  708. security_xfrm_policy_free(xp);
  709. kfree(xp);
  710. return err;
  711. }
  712. c.event = nlh->nlmsg_type;
  713. c.seq = nlh->nlmsg_seq;
  714. c.pid = nlh->nlmsg_pid;
  715. km_policy_notify(xp, p->dir, &c);
  716. xfrm_pol_put(xp);
  717. return 0;
  718. }
  719. static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
  720. {
  721. struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
  722. int i;
  723. if (xp->xfrm_nr == 0)
  724. return 0;
  725. for (i = 0; i < xp->xfrm_nr; i++) {
  726. struct xfrm_user_tmpl *up = &vec[i];
  727. struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
  728. memcpy(&up->id, &kp->id, sizeof(up->id));
  729. up->family = xp->family;
  730. memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
  731. up->reqid = kp->reqid;
  732. up->mode = kp->mode;
  733. up->share = kp->share;
  734. up->optional = kp->optional;
  735. up->aalgos = kp->aalgos;
  736. up->ealgos = kp->ealgos;
  737. up->calgos = kp->calgos;
  738. }
  739. RTA_PUT(skb, XFRMA_TMPL,
  740. (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
  741. vec);
  742. return 0;
  743. rtattr_failure:
  744. return -1;
  745. }
  746. static int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
  747. {
  748. if (xp->security) {
  749. int ctx_size = sizeof(struct xfrm_sec_ctx) +
  750. xp->security->ctx_len;
  751. struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
  752. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  753. uctx->exttype = XFRMA_SEC_CTX;
  754. uctx->len = ctx_size;
  755. uctx->ctx_doi = xp->security->ctx_doi;
  756. uctx->ctx_alg = xp->security->ctx_alg;
  757. uctx->ctx_len = xp->security->ctx_len;
  758. memcpy(uctx + 1, xp->security->ctx_str, xp->security->ctx_len);
  759. }
  760. return 0;
  761. rtattr_failure:
  762. return -1;
  763. }
  764. static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
  765. {
  766. struct xfrm_dump_info *sp = ptr;
  767. struct xfrm_userpolicy_info *p;
  768. struct sk_buff *in_skb = sp->in_skb;
  769. struct sk_buff *skb = sp->out_skb;
  770. struct nlmsghdr *nlh;
  771. unsigned char *b = skb->tail;
  772. if (sp->this_idx < sp->start_idx)
  773. goto out;
  774. nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
  775. sp->nlmsg_seq,
  776. XFRM_MSG_NEWPOLICY, sizeof(*p));
  777. p = NLMSG_DATA(nlh);
  778. nlh->nlmsg_flags = sp->nlmsg_flags;
  779. copy_to_user_policy(xp, p, dir);
  780. if (copy_to_user_tmpl(xp, skb) < 0)
  781. goto nlmsg_failure;
  782. if (copy_to_user_sec_ctx(xp, skb))
  783. goto nlmsg_failure;
  784. nlh->nlmsg_len = skb->tail - b;
  785. out:
  786. sp->this_idx++;
  787. return 0;
  788. nlmsg_failure:
  789. skb_trim(skb, b - skb->data);
  790. return -1;
  791. }
  792. static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
  793. {
  794. struct xfrm_dump_info info;
  795. info.in_skb = cb->skb;
  796. info.out_skb = skb;
  797. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  798. info.nlmsg_flags = NLM_F_MULTI;
  799. info.this_idx = 0;
  800. info.start_idx = cb->args[0];
  801. (void) xfrm_policy_walk(dump_one_policy, &info);
  802. cb->args[0] = info.this_idx;
  803. return skb->len;
  804. }
  805. static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
  806. struct xfrm_policy *xp,
  807. int dir, u32 seq)
  808. {
  809. struct xfrm_dump_info info;
  810. struct sk_buff *skb;
  811. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  812. if (!skb)
  813. return ERR_PTR(-ENOMEM);
  814. NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
  815. info.in_skb = in_skb;
  816. info.out_skb = skb;
  817. info.nlmsg_seq = seq;
  818. info.nlmsg_flags = 0;
  819. info.this_idx = info.start_idx = 0;
  820. if (dump_one_policy(xp, dir, 0, &info) < 0) {
  821. kfree_skb(skb);
  822. return NULL;
  823. }
  824. return skb;
  825. }
  826. static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  827. {
  828. struct xfrm_policy *xp;
  829. struct xfrm_userpolicy_id *p;
  830. int err;
  831. struct km_event c;
  832. int delete;
  833. p = NLMSG_DATA(nlh);
  834. delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
  835. err = verify_policy_dir(p->dir);
  836. if (err)
  837. return err;
  838. if (p->index)
  839. xp = xfrm_policy_byid(p->dir, p->index, delete);
  840. else {
  841. struct rtattr **rtattrs = (struct rtattr **)xfrma;
  842. struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
  843. struct xfrm_policy tmp;
  844. err = verify_sec_ctx_len(rtattrs);
  845. if (err)
  846. return err;
  847. memset(&tmp, 0, sizeof(struct xfrm_policy));
  848. if (rt) {
  849. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  850. if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
  851. return err;
  852. }
  853. xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
  854. security_xfrm_policy_free(&tmp);
  855. }
  856. if (xp == NULL)
  857. return -ENOENT;
  858. if (!delete) {
  859. struct sk_buff *resp_skb;
  860. resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
  861. if (IS_ERR(resp_skb)) {
  862. err = PTR_ERR(resp_skb);
  863. } else {
  864. err = netlink_unicast(xfrm_nl, resp_skb,
  865. NETLINK_CB(skb).pid,
  866. MSG_DONTWAIT);
  867. }
  868. } else {
  869. c.data.byid = p->index;
  870. c.event = nlh->nlmsg_type;
  871. c.seq = nlh->nlmsg_seq;
  872. c.pid = nlh->nlmsg_pid;
  873. km_policy_notify(xp, p->dir, &c);
  874. }
  875. xfrm_pol_put(xp);
  876. return err;
  877. }
  878. static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  879. {
  880. struct km_event c;
  881. struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
  882. xfrm_state_flush(p->proto);
  883. c.data.proto = p->proto;
  884. c.event = nlh->nlmsg_type;
  885. c.seq = nlh->nlmsg_seq;
  886. c.pid = nlh->nlmsg_pid;
  887. km_state_notify(NULL, &c);
  888. return 0;
  889. }
  890. static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
  891. {
  892. struct xfrm_aevent_id *id;
  893. struct nlmsghdr *nlh;
  894. struct xfrm_lifetime_cur ltime;
  895. unsigned char *b = skb->tail;
  896. nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
  897. id = NLMSG_DATA(nlh);
  898. nlh->nlmsg_flags = 0;
  899. id->sa_id.daddr = x->id.daddr;
  900. id->sa_id.spi = x->id.spi;
  901. id->sa_id.family = x->props.family;
  902. id->sa_id.proto = x->id.proto;
  903. id->flags = c->data.aevent;
  904. RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
  905. ltime.bytes = x->curlft.bytes;
  906. ltime.packets = x->curlft.packets;
  907. ltime.add_time = x->curlft.add_time;
  908. ltime.use_time = x->curlft.use_time;
  909. RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
  910. if (id->flags&XFRM_AE_RTHR) {
  911. RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
  912. }
  913. if (id->flags&XFRM_AE_ETHR) {
  914. u32 etimer = x->replay_maxage*10/HZ;
  915. RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
  916. }
  917. nlh->nlmsg_len = skb->tail - b;
  918. return skb->len;
  919. rtattr_failure:
  920. nlmsg_failure:
  921. skb_trim(skb, b - skb->data);
  922. return -1;
  923. }
  924. static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  925. {
  926. struct xfrm_state *x;
  927. struct sk_buff *r_skb;
  928. int err;
  929. struct km_event c;
  930. struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
  931. int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
  932. struct xfrm_usersa_id *id = &p->sa_id;
  933. len += RTA_SPACE(sizeof(struct xfrm_replay_state));
  934. len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
  935. if (p->flags&XFRM_AE_RTHR)
  936. len+=RTA_SPACE(sizeof(u32));
  937. if (p->flags&XFRM_AE_ETHR)
  938. len+=RTA_SPACE(sizeof(u32));
  939. r_skb = alloc_skb(len, GFP_ATOMIC);
  940. if (r_skb == NULL)
  941. return -ENOMEM;
  942. x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
  943. if (x == NULL) {
  944. kfree(r_skb);
  945. return -ESRCH;
  946. }
  947. /*
  948. * XXX: is this lock really needed - none of the other
  949. * gets lock (the concern is things getting updated
  950. * while we are still reading) - jhs
  951. */
  952. spin_lock_bh(&x->lock);
  953. c.data.aevent = p->flags;
  954. c.seq = nlh->nlmsg_seq;
  955. c.pid = nlh->nlmsg_pid;
  956. if (build_aevent(r_skb, x, &c) < 0)
  957. BUG();
  958. err = netlink_unicast(xfrm_nl, r_skb,
  959. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  960. spin_unlock_bh(&x->lock);
  961. xfrm_state_put(x);
  962. return err;
  963. }
  964. static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  965. {
  966. struct xfrm_state *x;
  967. struct km_event c;
  968. int err = - EINVAL;
  969. struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
  970. struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
  971. struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
  972. if (!lt && !rp)
  973. return err;
  974. /* pedantic mode - thou shalt sayeth replaceth */
  975. if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
  976. return err;
  977. x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
  978. if (x == NULL)
  979. return -ESRCH;
  980. if (x->km.state != XFRM_STATE_VALID)
  981. goto out;
  982. spin_lock_bh(&x->lock);
  983. err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
  984. spin_unlock_bh(&x->lock);
  985. if (err < 0)
  986. goto out;
  987. c.event = nlh->nlmsg_type;
  988. c.seq = nlh->nlmsg_seq;
  989. c.pid = nlh->nlmsg_pid;
  990. c.data.aevent = XFRM_AE_CU;
  991. km_state_notify(x, &c);
  992. err = 0;
  993. out:
  994. xfrm_state_put(x);
  995. return err;
  996. }
  997. static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  998. {
  999. struct km_event c;
  1000. xfrm_policy_flush();
  1001. c.event = nlh->nlmsg_type;
  1002. c.seq = nlh->nlmsg_seq;
  1003. c.pid = nlh->nlmsg_pid;
  1004. km_policy_notify(NULL, 0, &c);
  1005. return 0;
  1006. }
  1007. static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  1008. {
  1009. struct xfrm_policy *xp;
  1010. struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
  1011. struct xfrm_userpolicy_info *p = &up->pol;
  1012. int err = -ENOENT;
  1013. if (p->index)
  1014. xp = xfrm_policy_byid(p->dir, p->index, 0);
  1015. else {
  1016. struct rtattr **rtattrs = (struct rtattr **)xfrma;
  1017. struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
  1018. struct xfrm_policy tmp;
  1019. err = verify_sec_ctx_len(rtattrs);
  1020. if (err)
  1021. return err;
  1022. memset(&tmp, 0, sizeof(struct xfrm_policy));
  1023. if (rt) {
  1024. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  1025. if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
  1026. return err;
  1027. }
  1028. xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, 0);
  1029. security_xfrm_policy_free(&tmp);
  1030. }
  1031. if (xp == NULL)
  1032. return err;
  1033. read_lock(&xp->lock);
  1034. if (xp->dead) {
  1035. read_unlock(&xp->lock);
  1036. goto out;
  1037. }
  1038. read_unlock(&xp->lock);
  1039. err = 0;
  1040. if (up->hard) {
  1041. xfrm_policy_delete(xp, p->dir);
  1042. } else {
  1043. // reset the timers here?
  1044. printk("Dont know what to do with soft policy expire\n");
  1045. }
  1046. km_policy_expired(xp, p->dir, up->hard, current->pid);
  1047. out:
  1048. xfrm_pol_put(xp);
  1049. return err;
  1050. }
  1051. static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  1052. {
  1053. struct xfrm_state *x;
  1054. int err;
  1055. struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
  1056. struct xfrm_usersa_info *p = &ue->state;
  1057. x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
  1058. err = -ENOENT;
  1059. if (x == NULL)
  1060. return err;
  1061. err = -EINVAL;
  1062. spin_lock_bh(&x->lock);
  1063. if (x->km.state != XFRM_STATE_VALID)
  1064. goto out;
  1065. km_state_expired(x, ue->hard, current->pid);
  1066. if (ue->hard)
  1067. __xfrm_state_delete(x);
  1068. out:
  1069. spin_unlock_bh(&x->lock);
  1070. xfrm_state_put(x);
  1071. return err;
  1072. }
  1073. static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  1074. {
  1075. struct xfrm_policy *xp;
  1076. struct xfrm_user_tmpl *ut;
  1077. int i;
  1078. struct rtattr *rt = xfrma[XFRMA_TMPL-1];
  1079. struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
  1080. struct xfrm_state *x = xfrm_state_alloc();
  1081. int err = -ENOMEM;
  1082. if (!x)
  1083. return err;
  1084. err = verify_newpolicy_info(&ua->policy);
  1085. if (err) {
  1086. printk("BAD policy passed\n");
  1087. kfree(x);
  1088. return err;
  1089. }
  1090. /* build an XP */
  1091. xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err); if (!xp) {
  1092. kfree(x);
  1093. return err;
  1094. }
  1095. memcpy(&x->id, &ua->id, sizeof(ua->id));
  1096. memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
  1097. memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
  1098. ut = RTA_DATA(rt);
  1099. /* extract the templates and for each call km_key */
  1100. for (i = 0; i < xp->xfrm_nr; i++, ut++) {
  1101. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  1102. memcpy(&x->id, &t->id, sizeof(x->id));
  1103. x->props.mode = t->mode;
  1104. x->props.reqid = t->reqid;
  1105. x->props.family = ut->family;
  1106. t->aalgos = ua->aalgos;
  1107. t->ealgos = ua->ealgos;
  1108. t->calgos = ua->calgos;
  1109. err = km_query(x, t, xp);
  1110. }
  1111. kfree(x);
  1112. kfree(xp);
  1113. return 0;
  1114. }
  1115. #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
  1116. static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
  1117. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1118. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1119. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1120. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1121. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1122. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1123. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
  1124. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
  1125. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
  1126. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1127. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1128. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
  1129. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  1130. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
  1131. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1132. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1133. };
  1134. #undef XMSGSIZE
  1135. static struct xfrm_link {
  1136. int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
  1137. int (*dump)(struct sk_buff *, struct netlink_callback *);
  1138. } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
  1139. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1140. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
  1141. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
  1142. .dump = xfrm_dump_sa },
  1143. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1144. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
  1145. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
  1146. .dump = xfrm_dump_policy },
  1147. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
  1148. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
  1149. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
  1150. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1151. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1152. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
  1153. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
  1154. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
  1155. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
  1156. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
  1157. };
  1158. static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
  1159. {
  1160. struct rtattr *xfrma[XFRMA_MAX];
  1161. struct xfrm_link *link;
  1162. int type, min_len;
  1163. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  1164. return 0;
  1165. type = nlh->nlmsg_type;
  1166. /* A control message: ignore them */
  1167. if (type < XFRM_MSG_BASE)
  1168. return 0;
  1169. /* Unknown message: reply with EINVAL */
  1170. if (type > XFRM_MSG_MAX)
  1171. goto err_einval;
  1172. type -= XFRM_MSG_BASE;
  1173. link = &xfrm_dispatch[type];
  1174. /* All operations require privileges, even GET */
  1175. if (security_netlink_recv(skb)) {
  1176. *errp = -EPERM;
  1177. return -1;
  1178. }
  1179. if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
  1180. type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
  1181. (nlh->nlmsg_flags & NLM_F_DUMP)) {
  1182. if (link->dump == NULL)
  1183. goto err_einval;
  1184. if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
  1185. link->dump, NULL)) != 0) {
  1186. return -1;
  1187. }
  1188. netlink_queue_skip(nlh, skb);
  1189. return -1;
  1190. }
  1191. memset(xfrma, 0, sizeof(xfrma));
  1192. if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
  1193. goto err_einval;
  1194. if (nlh->nlmsg_len > min_len) {
  1195. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  1196. struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
  1197. while (RTA_OK(attr, attrlen)) {
  1198. unsigned short flavor = attr->rta_type;
  1199. if (flavor) {
  1200. if (flavor > XFRMA_MAX)
  1201. goto err_einval;
  1202. xfrma[flavor - 1] = attr;
  1203. }
  1204. attr = RTA_NEXT(attr, attrlen);
  1205. }
  1206. }
  1207. if (link->doit == NULL)
  1208. goto err_einval;
  1209. *errp = link->doit(skb, nlh, (void **) &xfrma);
  1210. return *errp;
  1211. err_einval:
  1212. *errp = -EINVAL;
  1213. return -1;
  1214. }
  1215. static void xfrm_netlink_rcv(struct sock *sk, int len)
  1216. {
  1217. unsigned int qlen = 0;
  1218. do {
  1219. mutex_lock(&xfrm_cfg_mutex);
  1220. netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
  1221. mutex_unlock(&xfrm_cfg_mutex);
  1222. } while (qlen);
  1223. }
  1224. static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
  1225. {
  1226. struct xfrm_user_expire *ue;
  1227. struct nlmsghdr *nlh;
  1228. unsigned char *b = skb->tail;
  1229. nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
  1230. sizeof(*ue));
  1231. ue = NLMSG_DATA(nlh);
  1232. nlh->nlmsg_flags = 0;
  1233. copy_to_user_state(x, &ue->state);
  1234. ue->hard = (c->data.hard != 0) ? 1 : 0;
  1235. nlh->nlmsg_len = skb->tail - b;
  1236. return skb->len;
  1237. nlmsg_failure:
  1238. skb_trim(skb, b - skb->data);
  1239. return -1;
  1240. }
  1241. static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
  1242. {
  1243. struct sk_buff *skb;
  1244. int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
  1245. skb = alloc_skb(len, GFP_ATOMIC);
  1246. if (skb == NULL)
  1247. return -ENOMEM;
  1248. if (build_expire(skb, x, c) < 0)
  1249. BUG();
  1250. NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
  1251. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  1252. }
  1253. static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
  1254. {
  1255. struct sk_buff *skb;
  1256. int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
  1257. len += RTA_SPACE(sizeof(struct xfrm_replay_state));
  1258. len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
  1259. skb = alloc_skb(len, GFP_ATOMIC);
  1260. if (skb == NULL)
  1261. return -ENOMEM;
  1262. if (build_aevent(skb, x, c) < 0)
  1263. BUG();
  1264. NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
  1265. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
  1266. }
  1267. static int xfrm_notify_sa_flush(struct km_event *c)
  1268. {
  1269. struct xfrm_usersa_flush *p;
  1270. struct nlmsghdr *nlh;
  1271. struct sk_buff *skb;
  1272. unsigned char *b;
  1273. int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
  1274. skb = alloc_skb(len, GFP_ATOMIC);
  1275. if (skb == NULL)
  1276. return -ENOMEM;
  1277. b = skb->tail;
  1278. nlh = NLMSG_PUT(skb, c->pid, c->seq,
  1279. XFRM_MSG_FLUSHSA, sizeof(*p));
  1280. nlh->nlmsg_flags = 0;
  1281. p = NLMSG_DATA(nlh);
  1282. p->proto = c->data.proto;
  1283. nlh->nlmsg_len = skb->tail - b;
  1284. NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
  1285. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  1286. nlmsg_failure:
  1287. kfree_skb(skb);
  1288. return -1;
  1289. }
  1290. static int inline xfrm_sa_len(struct xfrm_state *x)
  1291. {
  1292. int l = 0;
  1293. if (x->aalg)
  1294. l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
  1295. if (x->ealg)
  1296. l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
  1297. if (x->calg)
  1298. l += RTA_SPACE(sizeof(*x->calg));
  1299. if (x->encap)
  1300. l += RTA_SPACE(sizeof(*x->encap));
  1301. return l;
  1302. }
  1303. static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
  1304. {
  1305. struct xfrm_usersa_info *p;
  1306. struct xfrm_usersa_id *id;
  1307. struct nlmsghdr *nlh;
  1308. struct sk_buff *skb;
  1309. unsigned char *b;
  1310. int len = xfrm_sa_len(x);
  1311. int headlen;
  1312. headlen = sizeof(*p);
  1313. if (c->event == XFRM_MSG_DELSA) {
  1314. len += RTA_SPACE(headlen);
  1315. headlen = sizeof(*id);
  1316. }
  1317. len += NLMSG_SPACE(headlen);
  1318. skb = alloc_skb(len, GFP_ATOMIC);
  1319. if (skb == NULL)
  1320. return -ENOMEM;
  1321. b = skb->tail;
  1322. nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
  1323. nlh->nlmsg_flags = 0;
  1324. p = NLMSG_DATA(nlh);
  1325. if (c->event == XFRM_MSG_DELSA) {
  1326. id = NLMSG_DATA(nlh);
  1327. memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
  1328. id->spi = x->id.spi;
  1329. id->family = x->props.family;
  1330. id->proto = x->id.proto;
  1331. p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
  1332. }
  1333. copy_to_user_state(x, p);
  1334. if (x->aalg)
  1335. RTA_PUT(skb, XFRMA_ALG_AUTH,
  1336. sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
  1337. if (x->ealg)
  1338. RTA_PUT(skb, XFRMA_ALG_CRYPT,
  1339. sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
  1340. if (x->calg)
  1341. RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  1342. if (x->encap)
  1343. RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  1344. nlh->nlmsg_len = skb->tail - b;
  1345. NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
  1346. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  1347. nlmsg_failure:
  1348. rtattr_failure:
  1349. kfree_skb(skb);
  1350. return -1;
  1351. }
  1352. static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
  1353. {
  1354. switch (c->event) {
  1355. case XFRM_MSG_EXPIRE:
  1356. return xfrm_exp_state_notify(x, c);
  1357. case XFRM_MSG_NEWAE:
  1358. return xfrm_aevent_state_notify(x, c);
  1359. case XFRM_MSG_DELSA:
  1360. case XFRM_MSG_UPDSA:
  1361. case XFRM_MSG_NEWSA:
  1362. return xfrm_notify_sa(x, c);
  1363. case XFRM_MSG_FLUSHSA:
  1364. return xfrm_notify_sa_flush(c);
  1365. default:
  1366. printk("xfrm_user: Unknown SA event %d\n", c->event);
  1367. break;
  1368. }
  1369. return 0;
  1370. }
  1371. static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
  1372. struct xfrm_tmpl *xt, struct xfrm_policy *xp,
  1373. int dir)
  1374. {
  1375. struct xfrm_user_acquire *ua;
  1376. struct nlmsghdr *nlh;
  1377. unsigned char *b = skb->tail;
  1378. __u32 seq = xfrm_get_acqseq();
  1379. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
  1380. sizeof(*ua));
  1381. ua = NLMSG_DATA(nlh);
  1382. nlh->nlmsg_flags = 0;
  1383. memcpy(&ua->id, &x->id, sizeof(ua->id));
  1384. memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
  1385. memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
  1386. copy_to_user_policy(xp, &ua->policy, dir);
  1387. ua->aalgos = xt->aalgos;
  1388. ua->ealgos = xt->ealgos;
  1389. ua->calgos = xt->calgos;
  1390. ua->seq = x->km.seq = seq;
  1391. if (copy_to_user_tmpl(xp, skb) < 0)
  1392. goto nlmsg_failure;
  1393. if (copy_to_user_sec_ctx(xp, skb))
  1394. goto nlmsg_failure;
  1395. nlh->nlmsg_len = skb->tail - b;
  1396. return skb->len;
  1397. nlmsg_failure:
  1398. skb_trim(skb, b - skb->data);
  1399. return -1;
  1400. }
  1401. static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
  1402. struct xfrm_policy *xp, int dir)
  1403. {
  1404. struct sk_buff *skb;
  1405. size_t len;
  1406. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1407. len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
  1408. len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
  1409. skb = alloc_skb(len, GFP_ATOMIC);
  1410. if (skb == NULL)
  1411. return -ENOMEM;
  1412. if (build_acquire(skb, x, xt, xp, dir) < 0)
  1413. BUG();
  1414. NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
  1415. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
  1416. }
  1417. /* User gives us xfrm_user_policy_info followed by an array of 0
  1418. * or more templates.
  1419. */
  1420. static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
  1421. u8 *data, int len, int *dir)
  1422. {
  1423. struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
  1424. struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
  1425. struct xfrm_policy *xp;
  1426. int nr;
  1427. switch (family) {
  1428. case AF_INET:
  1429. if (opt != IP_XFRM_POLICY) {
  1430. *dir = -EOPNOTSUPP;
  1431. return NULL;
  1432. }
  1433. break;
  1434. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1435. case AF_INET6:
  1436. if (opt != IPV6_XFRM_POLICY) {
  1437. *dir = -EOPNOTSUPP;
  1438. return NULL;
  1439. }
  1440. break;
  1441. #endif
  1442. default:
  1443. *dir = -EINVAL;
  1444. return NULL;
  1445. }
  1446. *dir = -EINVAL;
  1447. if (len < sizeof(*p) ||
  1448. verify_newpolicy_info(p))
  1449. return NULL;
  1450. nr = ((len - sizeof(*p)) / sizeof(*ut));
  1451. if (nr > XFRM_MAX_DEPTH)
  1452. return NULL;
  1453. if (p->dir > XFRM_POLICY_OUT)
  1454. return NULL;
  1455. xp = xfrm_policy_alloc(GFP_KERNEL);
  1456. if (xp == NULL) {
  1457. *dir = -ENOBUFS;
  1458. return NULL;
  1459. }
  1460. copy_from_user_policy(xp, p);
  1461. copy_templates(xp, ut, nr);
  1462. *dir = p->dir;
  1463. return xp;
  1464. }
  1465. static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
  1466. int dir, struct km_event *c)
  1467. {
  1468. struct xfrm_user_polexpire *upe;
  1469. struct nlmsghdr *nlh;
  1470. int hard = c->data.hard;
  1471. unsigned char *b = skb->tail;
  1472. nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
  1473. upe = NLMSG_DATA(nlh);
  1474. nlh->nlmsg_flags = 0;
  1475. copy_to_user_policy(xp, &upe->pol, dir);
  1476. if (copy_to_user_tmpl(xp, skb) < 0)
  1477. goto nlmsg_failure;
  1478. if (copy_to_user_sec_ctx(xp, skb))
  1479. goto nlmsg_failure;
  1480. upe->hard = !!hard;
  1481. nlh->nlmsg_len = skb->tail - b;
  1482. return skb->len;
  1483. nlmsg_failure:
  1484. skb_trim(skb, b - skb->data);
  1485. return -1;
  1486. }
  1487. static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  1488. {
  1489. struct sk_buff *skb;
  1490. size_t len;
  1491. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1492. len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
  1493. len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
  1494. skb = alloc_skb(len, GFP_ATOMIC);
  1495. if (skb == NULL)
  1496. return -ENOMEM;
  1497. if (build_polexpire(skb, xp, dir, c) < 0)
  1498. BUG();
  1499. NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
  1500. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  1501. }
  1502. static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
  1503. {
  1504. struct xfrm_userpolicy_info *p;
  1505. struct xfrm_userpolicy_id *id;
  1506. struct nlmsghdr *nlh;
  1507. struct sk_buff *skb;
  1508. unsigned char *b;
  1509. int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1510. int headlen;
  1511. headlen = sizeof(*p);
  1512. if (c->event == XFRM_MSG_DELPOLICY) {
  1513. len += RTA_SPACE(headlen);
  1514. headlen = sizeof(*id);
  1515. }
  1516. len += NLMSG_SPACE(headlen);
  1517. skb = alloc_skb(len, GFP_ATOMIC);
  1518. if (skb == NULL)
  1519. return -ENOMEM;
  1520. b = skb->tail;
  1521. nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
  1522. p = NLMSG_DATA(nlh);
  1523. if (c->event == XFRM_MSG_DELPOLICY) {
  1524. id = NLMSG_DATA(nlh);
  1525. memset(id, 0, sizeof(*id));
  1526. id->dir = dir;
  1527. if (c->data.byid)
  1528. id->index = xp->index;
  1529. else
  1530. memcpy(&id->sel, &xp->selector, sizeof(id->sel));
  1531. p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
  1532. }
  1533. nlh->nlmsg_flags = 0;
  1534. copy_to_user_policy(xp, p, dir);
  1535. if (copy_to_user_tmpl(xp, skb) < 0)
  1536. goto nlmsg_failure;
  1537. nlh->nlmsg_len = skb->tail - b;
  1538. NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
  1539. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  1540. nlmsg_failure:
  1541. rtattr_failure:
  1542. kfree_skb(skb);
  1543. return -1;
  1544. }
  1545. static int xfrm_notify_policy_flush(struct km_event *c)
  1546. {
  1547. struct nlmsghdr *nlh;
  1548. struct sk_buff *skb;
  1549. unsigned char *b;
  1550. int len = NLMSG_LENGTH(0);
  1551. skb = alloc_skb(len, GFP_ATOMIC);
  1552. if (skb == NULL)
  1553. return -ENOMEM;
  1554. b = skb->tail;
  1555. nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
  1556. nlh->nlmsg_len = skb->tail - b;
  1557. NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
  1558. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  1559. nlmsg_failure:
  1560. kfree_skb(skb);
  1561. return -1;
  1562. }
  1563. static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  1564. {
  1565. switch (c->event) {
  1566. case XFRM_MSG_NEWPOLICY:
  1567. case XFRM_MSG_UPDPOLICY:
  1568. case XFRM_MSG_DELPOLICY:
  1569. return xfrm_notify_policy(xp, dir, c);
  1570. case XFRM_MSG_FLUSHPOLICY:
  1571. return xfrm_notify_policy_flush(c);
  1572. case XFRM_MSG_POLEXPIRE:
  1573. return xfrm_exp_policy_notify(xp, dir, c);
  1574. default:
  1575. printk("xfrm_user: Unknown Policy event %d\n", c->event);
  1576. }
  1577. return 0;
  1578. }
  1579. static struct xfrm_mgr netlink_mgr = {
  1580. .id = "netlink",
  1581. .notify = xfrm_send_state_notify,
  1582. .acquire = xfrm_send_acquire,
  1583. .compile_policy = xfrm_compile_policy,
  1584. .notify_policy = xfrm_send_policy_notify,
  1585. };
  1586. static int __init xfrm_user_init(void)
  1587. {
  1588. struct sock *nlsk;
  1589. printk(KERN_INFO "Initializing IPsec netlink socket\n");
  1590. nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
  1591. xfrm_netlink_rcv, THIS_MODULE);
  1592. if (nlsk == NULL)
  1593. return -ENOMEM;
  1594. rcu_assign_pointer(xfrm_nl, nlsk);
  1595. xfrm_register_km(&netlink_mgr);
  1596. return 0;
  1597. }
  1598. static void __exit xfrm_user_exit(void)
  1599. {
  1600. struct sock *nlsk = xfrm_nl;
  1601. xfrm_unregister_km(&netlink_mgr);
  1602. rcu_assign_pointer(xfrm_nl, NULL);
  1603. synchronize_rcu();
  1604. sock_release(nlsk->sk_socket);
  1605. }
  1606. module_init(xfrm_user_init);
  1607. module_exit(xfrm_user_exit);
  1608. MODULE_LICENSE("GPL");
  1609. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);