xfrm_user.c 55 KB

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