xfrm_user.c 46 KB

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