xfrm_user.c 58 KB

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