xfrm_user.c 57 KB

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