xfrm_user.c 55 KB

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