xfrm_user.c 50 KB

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