xfrm_user.c 51 KB

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