xfrm_user.c 64 KB

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