xfrm_user.c 44 KB

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