xfrm_user.c 58 KB

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