xfrm_user.c 72 KB

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