xfrm_user.c 61 KB

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