xfrm_user.c 49 KB

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