xfrm_user.c 70 KB

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