xfrm_user.c 70 KB

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