xfrm_user.c 56 KB

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