xfrm_user.c 55 KB

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