xfrm_user.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* xfrm_user.c: User interface to configure xfrm engine.
  3. *
  4. * Copyright (C) 2002 David S. Miller ([email protected])
  5. *
  6. * Changes:
  7. * Mitsuru KANDA @USAGI
  8. * Kazunori MIYAZAWA @USAGI
  9. * Kunihiro Ishiguro <[email protected]>
  10. * IPv6 support
  11. *
  12. */
  13. #include <linux/compat.h>
  14. #include <linux/crypto.h>
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/slab.h>
  19. #include <linux/socket.h>
  20. #include <linux/string.h>
  21. #include <linux/net.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/pfkeyv2.h>
  24. #include <linux/ipsec.h>
  25. #include <linux/init.h>
  26. #include <linux/security.h>
  27. #include <net/sock.h>
  28. #include <net/xfrm.h>
  29. #include <net/netlink.h>
  30. #include <net/ah.h>
  31. #include <linux/uaccess.h>
  32. #if IS_ENABLED(CONFIG_IPV6)
  33. #include <linux/in6.h>
  34. #endif
  35. #include <asm/unaligned.h>
  36. static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type,
  37. struct netlink_ext_ack *extack)
  38. {
  39. struct nlattr *rt = attrs[type];
  40. struct xfrm_algo *algp;
  41. if (!rt)
  42. return 0;
  43. algp = nla_data(rt);
  44. if (nla_len(rt) < (int)xfrm_alg_len(algp)) {
  45. NL_SET_ERR_MSG(extack, "Invalid AUTH/CRYPT/COMP attribute length");
  46. return -EINVAL;
  47. }
  48. switch (type) {
  49. case XFRMA_ALG_AUTH:
  50. case XFRMA_ALG_CRYPT:
  51. case XFRMA_ALG_COMP:
  52. break;
  53. default:
  54. NL_SET_ERR_MSG(extack, "Invalid algorithm attribute type");
  55. return -EINVAL;
  56. }
  57. algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
  58. return 0;
  59. }
  60. static int verify_auth_trunc(struct nlattr **attrs,
  61. struct netlink_ext_ack *extack)
  62. {
  63. struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
  64. struct xfrm_algo_auth *algp;
  65. if (!rt)
  66. return 0;
  67. algp = nla_data(rt);
  68. if (nla_len(rt) < (int)xfrm_alg_auth_len(algp)) {
  69. NL_SET_ERR_MSG(extack, "Invalid AUTH_TRUNC attribute length");
  70. return -EINVAL;
  71. }
  72. algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
  73. return 0;
  74. }
  75. static int verify_aead(struct nlattr **attrs, struct netlink_ext_ack *extack)
  76. {
  77. struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
  78. struct xfrm_algo_aead *algp;
  79. if (!rt)
  80. return 0;
  81. algp = nla_data(rt);
  82. if (nla_len(rt) < (int)aead_len(algp)) {
  83. NL_SET_ERR_MSG(extack, "Invalid AEAD attribute length");
  84. return -EINVAL;
  85. }
  86. algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
  87. return 0;
  88. }
  89. static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
  90. xfrm_address_t **addrp)
  91. {
  92. struct nlattr *rt = attrs[type];
  93. if (rt && addrp)
  94. *addrp = nla_data(rt);
  95. }
  96. static inline int verify_sec_ctx_len(struct nlattr **attrs, struct netlink_ext_ack *extack)
  97. {
  98. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  99. struct xfrm_user_sec_ctx *uctx;
  100. if (!rt)
  101. return 0;
  102. uctx = nla_data(rt);
  103. if (uctx->len > nla_len(rt) ||
  104. uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len)) {
  105. NL_SET_ERR_MSG(extack, "Invalid security context length");
  106. return -EINVAL;
  107. }
  108. return 0;
  109. }
  110. static inline int verify_replay(struct xfrm_usersa_info *p,
  111. struct nlattr **attrs,
  112. struct netlink_ext_ack *extack)
  113. {
  114. struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
  115. struct xfrm_replay_state_esn *rs;
  116. if (!rt) {
  117. if (p->flags & XFRM_STATE_ESN) {
  118. NL_SET_ERR_MSG(extack, "Missing required attribute for ESN");
  119. return -EINVAL;
  120. }
  121. return 0;
  122. }
  123. rs = nla_data(rt);
  124. if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8) {
  125. NL_SET_ERR_MSG(extack, "ESN bitmap length must be <= 128");
  126. return -EINVAL;
  127. }
  128. if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
  129. nla_len(rt) != sizeof(*rs)) {
  130. NL_SET_ERR_MSG(extack, "ESN attribute is too short to fit the full bitmap length");
  131. return -EINVAL;
  132. }
  133. /* As only ESP and AH support ESN feature. */
  134. if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH)) {
  135. NL_SET_ERR_MSG(extack, "ESN only supported for ESP and AH");
  136. return -EINVAL;
  137. }
  138. if (p->replay_window != 0) {
  139. NL_SET_ERR_MSG(extack, "ESN not compatible with legacy replay_window");
  140. return -EINVAL;
  141. }
  142. return 0;
  143. }
  144. static int verify_newsa_info(struct xfrm_usersa_info *p,
  145. struct nlattr **attrs,
  146. struct netlink_ext_ack *extack)
  147. {
  148. int err;
  149. err = -EINVAL;
  150. switch (p->family) {
  151. case AF_INET:
  152. break;
  153. case AF_INET6:
  154. #if IS_ENABLED(CONFIG_IPV6)
  155. break;
  156. #else
  157. err = -EAFNOSUPPORT;
  158. NL_SET_ERR_MSG(extack, "IPv6 support disabled");
  159. goto out;
  160. #endif
  161. default:
  162. NL_SET_ERR_MSG(extack, "Invalid address family");
  163. goto out;
  164. }
  165. switch (p->sel.family) {
  166. case AF_UNSPEC:
  167. break;
  168. case AF_INET:
  169. if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) {
  170. NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)");
  171. goto out;
  172. }
  173. break;
  174. case AF_INET6:
  175. #if IS_ENABLED(CONFIG_IPV6)
  176. if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) {
  177. NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)");
  178. goto out;
  179. }
  180. break;
  181. #else
  182. NL_SET_ERR_MSG(extack, "IPv6 support disabled");
  183. err = -EAFNOSUPPORT;
  184. goto out;
  185. #endif
  186. default:
  187. NL_SET_ERR_MSG(extack, "Invalid address family in selector");
  188. goto out;
  189. }
  190. err = -EINVAL;
  191. switch (p->id.proto) {
  192. case IPPROTO_AH:
  193. if (!attrs[XFRMA_ALG_AUTH] &&
  194. !attrs[XFRMA_ALG_AUTH_TRUNC]) {
  195. NL_SET_ERR_MSG(extack, "Missing required attribute for AH: AUTH_TRUNC or AUTH");
  196. goto out;
  197. }
  198. if (attrs[XFRMA_ALG_AEAD] ||
  199. attrs[XFRMA_ALG_CRYPT] ||
  200. attrs[XFRMA_ALG_COMP] ||
  201. attrs[XFRMA_TFCPAD]) {
  202. NL_SET_ERR_MSG(extack, "Invalid attributes for AH: AEAD, CRYPT, COMP, TFCPAD");
  203. goto out;
  204. }
  205. break;
  206. case IPPROTO_ESP:
  207. if (attrs[XFRMA_ALG_COMP]) {
  208. NL_SET_ERR_MSG(extack, "Invalid attribute for ESP: COMP");
  209. goto out;
  210. }
  211. if (!attrs[XFRMA_ALG_AUTH] &&
  212. !attrs[XFRMA_ALG_AUTH_TRUNC] &&
  213. !attrs[XFRMA_ALG_CRYPT] &&
  214. !attrs[XFRMA_ALG_AEAD]) {
  215. NL_SET_ERR_MSG(extack, "Missing required attribute for ESP: at least one of AUTH, AUTH_TRUNC, CRYPT, AEAD");
  216. goto out;
  217. }
  218. if ((attrs[XFRMA_ALG_AUTH] ||
  219. attrs[XFRMA_ALG_AUTH_TRUNC] ||
  220. attrs[XFRMA_ALG_CRYPT]) &&
  221. attrs[XFRMA_ALG_AEAD]) {
  222. NL_SET_ERR_MSG(extack, "Invalid attribute combination for ESP: AEAD can't be used with AUTH, AUTH_TRUNC, CRYPT");
  223. goto out;
  224. }
  225. if (attrs[XFRMA_TFCPAD] &&
  226. p->mode != XFRM_MODE_TUNNEL) {
  227. NL_SET_ERR_MSG(extack, "TFC padding can only be used in tunnel mode");
  228. goto out;
  229. }
  230. break;
  231. case IPPROTO_COMP:
  232. if (!attrs[XFRMA_ALG_COMP]) {
  233. NL_SET_ERR_MSG(extack, "Missing required attribute for COMP: COMP");
  234. goto out;
  235. }
  236. if (attrs[XFRMA_ALG_AEAD] ||
  237. attrs[XFRMA_ALG_AUTH] ||
  238. attrs[XFRMA_ALG_AUTH_TRUNC] ||
  239. attrs[XFRMA_ALG_CRYPT] ||
  240. attrs[XFRMA_TFCPAD]) {
  241. NL_SET_ERR_MSG(extack, "Invalid attributes for COMP: AEAD, AUTH, AUTH_TRUNC, CRYPT, TFCPAD");
  242. goto out;
  243. }
  244. if (ntohl(p->id.spi) >= 0x10000) {
  245. NL_SET_ERR_MSG(extack, "SPI is too large for COMP (must be < 0x10000)");
  246. goto out;
  247. }
  248. break;
  249. #if IS_ENABLED(CONFIG_IPV6)
  250. case IPPROTO_DSTOPTS:
  251. case IPPROTO_ROUTING:
  252. if (attrs[XFRMA_ALG_COMP] ||
  253. attrs[XFRMA_ALG_AUTH] ||
  254. attrs[XFRMA_ALG_AUTH_TRUNC] ||
  255. attrs[XFRMA_ALG_AEAD] ||
  256. attrs[XFRMA_ALG_CRYPT] ||
  257. attrs[XFRMA_ENCAP] ||
  258. attrs[XFRMA_SEC_CTX] ||
  259. attrs[XFRMA_TFCPAD]) {
  260. NL_SET_ERR_MSG(extack, "Invalid attributes for DSTOPTS/ROUTING");
  261. goto out;
  262. }
  263. if (!attrs[XFRMA_COADDR]) {
  264. NL_SET_ERR_MSG(extack, "Missing required COADDR attribute for DSTOPTS/ROUTING");
  265. goto out;
  266. }
  267. break;
  268. #endif
  269. default:
  270. NL_SET_ERR_MSG(extack, "Unsupported protocol");
  271. goto out;
  272. }
  273. if ((err = verify_aead(attrs, extack)))
  274. goto out;
  275. if ((err = verify_auth_trunc(attrs, extack)))
  276. goto out;
  277. if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH, extack)))
  278. goto out;
  279. if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT, extack)))
  280. goto out;
  281. if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP, extack)))
  282. goto out;
  283. if ((err = verify_sec_ctx_len(attrs, extack)))
  284. goto out;
  285. if ((err = verify_replay(p, attrs, extack)))
  286. goto out;
  287. err = -EINVAL;
  288. switch (p->mode) {
  289. case XFRM_MODE_TRANSPORT:
  290. case XFRM_MODE_TUNNEL:
  291. case XFRM_MODE_ROUTEOPTIMIZATION:
  292. case XFRM_MODE_BEET:
  293. break;
  294. default:
  295. NL_SET_ERR_MSG(extack, "Unsupported mode");
  296. goto out;
  297. }
  298. err = 0;
  299. if (attrs[XFRMA_MTIMER_THRESH]) {
  300. if (!attrs[XFRMA_ENCAP]) {
  301. NL_SET_ERR_MSG(extack, "MTIMER_THRESH attribute can only be set on ENCAP states");
  302. err = -EINVAL;
  303. goto out;
  304. }
  305. }
  306. out:
  307. return err;
  308. }
  309. static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
  310. struct xfrm_algo_desc *(*get_byname)(const char *, int),
  311. struct nlattr *rta, struct netlink_ext_ack *extack)
  312. {
  313. struct xfrm_algo *p, *ualg;
  314. struct xfrm_algo_desc *algo;
  315. if (!rta)
  316. return 0;
  317. ualg = nla_data(rta);
  318. algo = get_byname(ualg->alg_name, 1);
  319. if (!algo) {
  320. NL_SET_ERR_MSG(extack, "Requested COMP algorithm not found");
  321. return -ENOSYS;
  322. }
  323. *props = algo->desc.sadb_alg_id;
  324. p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
  325. if (!p)
  326. return -ENOMEM;
  327. strcpy(p->alg_name, algo->name);
  328. *algpp = p;
  329. return 0;
  330. }
  331. static int attach_crypt(struct xfrm_state *x, struct nlattr *rta,
  332. struct netlink_ext_ack *extack)
  333. {
  334. struct xfrm_algo *p, *ualg;
  335. struct xfrm_algo_desc *algo;
  336. if (!rta)
  337. return 0;
  338. ualg = nla_data(rta);
  339. algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
  340. if (!algo) {
  341. NL_SET_ERR_MSG(extack, "Requested CRYPT algorithm not found");
  342. return -ENOSYS;
  343. }
  344. x->props.ealgo = algo->desc.sadb_alg_id;
  345. p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
  346. if (!p)
  347. return -ENOMEM;
  348. strcpy(p->alg_name, algo->name);
  349. x->ealg = p;
  350. x->geniv = algo->uinfo.encr.geniv;
  351. return 0;
  352. }
  353. static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
  354. struct nlattr *rta, struct netlink_ext_ack *extack)
  355. {
  356. struct xfrm_algo *ualg;
  357. struct xfrm_algo_auth *p;
  358. struct xfrm_algo_desc *algo;
  359. if (!rta)
  360. return 0;
  361. ualg = nla_data(rta);
  362. algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
  363. if (!algo) {
  364. NL_SET_ERR_MSG(extack, "Requested AUTH algorithm not found");
  365. return -ENOSYS;
  366. }
  367. *props = algo->desc.sadb_alg_id;
  368. p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
  369. if (!p)
  370. return -ENOMEM;
  371. strcpy(p->alg_name, algo->name);
  372. p->alg_key_len = ualg->alg_key_len;
  373. p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
  374. memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
  375. *algpp = p;
  376. return 0;
  377. }
  378. static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
  379. struct nlattr *rta, struct netlink_ext_ack *extack)
  380. {
  381. struct xfrm_algo_auth *p, *ualg;
  382. struct xfrm_algo_desc *algo;
  383. if (!rta)
  384. return 0;
  385. ualg = nla_data(rta);
  386. algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
  387. if (!algo) {
  388. NL_SET_ERR_MSG(extack, "Requested AUTH_TRUNC algorithm not found");
  389. return -ENOSYS;
  390. }
  391. if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits) {
  392. NL_SET_ERR_MSG(extack, "Invalid length requested for truncated ICV");
  393. return -EINVAL;
  394. }
  395. *props = algo->desc.sadb_alg_id;
  396. p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
  397. if (!p)
  398. return -ENOMEM;
  399. strcpy(p->alg_name, algo->name);
  400. if (!p->alg_trunc_len)
  401. p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
  402. *algpp = p;
  403. return 0;
  404. }
  405. static int attach_aead(struct xfrm_state *x, struct nlattr *rta,
  406. struct netlink_ext_ack *extack)
  407. {
  408. struct xfrm_algo_aead *p, *ualg;
  409. struct xfrm_algo_desc *algo;
  410. if (!rta)
  411. return 0;
  412. ualg = nla_data(rta);
  413. algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
  414. if (!algo) {
  415. NL_SET_ERR_MSG(extack, "Requested AEAD algorithm not found");
  416. return -ENOSYS;
  417. }
  418. x->props.ealgo = algo->desc.sadb_alg_id;
  419. p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
  420. if (!p)
  421. return -ENOMEM;
  422. strcpy(p->alg_name, algo->name);
  423. x->aead = p;
  424. x->geniv = algo->uinfo.aead.geniv;
  425. return 0;
  426. }
  427. static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
  428. struct nlattr *rp)
  429. {
  430. struct xfrm_replay_state_esn *up;
  431. unsigned int ulen;
  432. if (!replay_esn || !rp)
  433. return 0;
  434. up = nla_data(rp);
  435. ulen = xfrm_replay_state_esn_len(up);
  436. /* Check the overall length and the internal bitmap length to avoid
  437. * potential overflow. */
  438. if (nla_len(rp) < (int)ulen ||
  439. xfrm_replay_state_esn_len(replay_esn) != ulen ||
  440. replay_esn->bmp_len != up->bmp_len)
  441. return -EINVAL;
  442. if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
  443. return -EINVAL;
  444. return 0;
  445. }
  446. static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
  447. struct xfrm_replay_state_esn **preplay_esn,
  448. struct nlattr *rta)
  449. {
  450. struct xfrm_replay_state_esn *p, *pp, *up;
  451. unsigned int klen, ulen;
  452. if (!rta)
  453. return 0;
  454. up = nla_data(rta);
  455. klen = xfrm_replay_state_esn_len(up);
  456. ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
  457. p = kzalloc(klen, GFP_KERNEL);
  458. if (!p)
  459. return -ENOMEM;
  460. pp = kzalloc(klen, GFP_KERNEL);
  461. if (!pp) {
  462. kfree(p);
  463. return -ENOMEM;
  464. }
  465. memcpy(p, up, ulen);
  466. memcpy(pp, up, ulen);
  467. *replay_esn = p;
  468. *preplay_esn = pp;
  469. return 0;
  470. }
  471. static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
  472. {
  473. unsigned int len = 0;
  474. if (xfrm_ctx) {
  475. len += sizeof(struct xfrm_user_sec_ctx);
  476. len += xfrm_ctx->ctx_len;
  477. }
  478. return len;
  479. }
  480. static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  481. {
  482. memcpy(&x->id, &p->id, sizeof(x->id));
  483. memcpy(&x->sel, &p->sel, sizeof(x->sel));
  484. memcpy(&x->lft, &p->lft, sizeof(x->lft));
  485. x->props.mode = p->mode;
  486. x->props.replay_window = min_t(unsigned int, p->replay_window,
  487. sizeof(x->replay.bitmap) * 8);
  488. x->props.reqid = p->reqid;
  489. x->props.family = p->family;
  490. memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
  491. x->props.flags = p->flags;
  492. if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
  493. x->sel.family = p->family;
  494. }
  495. /*
  496. * someday when pfkey also has support, we could have the code
  497. * somehow made shareable and move it to xfrm_state.c - JHS
  498. *
  499. */
  500. static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
  501. int update_esn)
  502. {
  503. struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
  504. struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
  505. struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
  506. struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
  507. struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
  508. struct nlattr *mt = attrs[XFRMA_MTIMER_THRESH];
  509. if (re && x->replay_esn && x->preplay_esn) {
  510. struct xfrm_replay_state_esn *replay_esn;
  511. replay_esn = nla_data(re);
  512. memcpy(x->replay_esn, replay_esn,
  513. xfrm_replay_state_esn_len(replay_esn));
  514. memcpy(x->preplay_esn, replay_esn,
  515. xfrm_replay_state_esn_len(replay_esn));
  516. }
  517. if (rp) {
  518. struct xfrm_replay_state *replay;
  519. replay = nla_data(rp);
  520. memcpy(&x->replay, replay, sizeof(*replay));
  521. memcpy(&x->preplay, replay, sizeof(*replay));
  522. }
  523. if (lt) {
  524. struct xfrm_lifetime_cur *ltime;
  525. ltime = nla_data(lt);
  526. x->curlft.bytes = ltime->bytes;
  527. x->curlft.packets = ltime->packets;
  528. x->curlft.add_time = ltime->add_time;
  529. x->curlft.use_time = ltime->use_time;
  530. }
  531. if (et)
  532. x->replay_maxage = nla_get_u32(et);
  533. if (rt)
  534. x->replay_maxdiff = nla_get_u32(rt);
  535. if (mt)
  536. x->mapping_maxage = nla_get_u32(mt);
  537. }
  538. static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
  539. {
  540. if (attrs[XFRMA_SET_MARK]) {
  541. m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
  542. if (attrs[XFRMA_SET_MARK_MASK])
  543. m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
  544. else
  545. m->m = 0xffffffff;
  546. } else {
  547. m->v = m->m = 0;
  548. }
  549. }
  550. static struct xfrm_state *xfrm_state_construct(struct net *net,
  551. struct xfrm_usersa_info *p,
  552. struct nlattr **attrs,
  553. int *errp,
  554. struct netlink_ext_ack *extack)
  555. {
  556. struct xfrm_state *x = xfrm_state_alloc(net);
  557. int err = -ENOMEM;
  558. if (!x)
  559. goto error_no_put;
  560. copy_from_user_state(x, p);
  561. if (attrs[XFRMA_ENCAP]) {
  562. x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
  563. sizeof(*x->encap), GFP_KERNEL);
  564. if (x->encap == NULL)
  565. goto error;
  566. }
  567. if (attrs[XFRMA_COADDR]) {
  568. x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
  569. sizeof(*x->coaddr), GFP_KERNEL);
  570. if (x->coaddr == NULL)
  571. goto error;
  572. }
  573. if (attrs[XFRMA_SA_EXTRA_FLAGS])
  574. x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
  575. if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD], extack)))
  576. goto error;
  577. if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
  578. attrs[XFRMA_ALG_AUTH_TRUNC], extack)))
  579. goto error;
  580. if (!x->props.aalgo) {
  581. if ((err = attach_auth(&x->aalg, &x->props.aalgo,
  582. attrs[XFRMA_ALG_AUTH], extack)))
  583. goto error;
  584. }
  585. if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT], extack)))
  586. goto error;
  587. if ((err = attach_one_algo(&x->calg, &x->props.calgo,
  588. xfrm_calg_get_byname,
  589. attrs[XFRMA_ALG_COMP], extack)))
  590. goto error;
  591. if (attrs[XFRMA_TFCPAD])
  592. x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
  593. xfrm_mark_get(attrs, &x->mark);
  594. xfrm_smark_init(attrs, &x->props.smark);
  595. if (attrs[XFRMA_IF_ID])
  596. x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
  597. err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV], extack);
  598. if (err)
  599. goto error;
  600. if (attrs[XFRMA_SEC_CTX]) {
  601. err = security_xfrm_state_alloc(x,
  602. nla_data(attrs[XFRMA_SEC_CTX]));
  603. if (err)
  604. goto error;
  605. }
  606. if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
  607. attrs[XFRMA_REPLAY_ESN_VAL])))
  608. goto error;
  609. x->km.seq = p->seq;
  610. x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
  611. /* sysctl_xfrm_aevent_etime is in 100ms units */
  612. x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
  613. if ((err = xfrm_init_replay(x, extack)))
  614. goto error;
  615. /* override default values from above */
  616. xfrm_update_ae_params(x, attrs, 0);
  617. /* configure the hardware if offload is requested */
  618. if (attrs[XFRMA_OFFLOAD_DEV]) {
  619. err = xfrm_dev_state_add(net, x,
  620. nla_data(attrs[XFRMA_OFFLOAD_DEV]),
  621. extack);
  622. if (err)
  623. goto error;
  624. }
  625. return x;
  626. error:
  627. x->km.state = XFRM_STATE_DEAD;
  628. xfrm_state_put(x);
  629. error_no_put:
  630. *errp = err;
  631. return NULL;
  632. }
  633. static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  634. struct nlattr **attrs, struct netlink_ext_ack *extack)
  635. {
  636. struct net *net = sock_net(skb->sk);
  637. struct xfrm_usersa_info *p = nlmsg_data(nlh);
  638. struct xfrm_state *x;
  639. int err;
  640. struct km_event c;
  641. err = verify_newsa_info(p, attrs, extack);
  642. if (err)
  643. return err;
  644. x = xfrm_state_construct(net, p, attrs, &err, extack);
  645. if (!x)
  646. return err;
  647. xfrm_state_hold(x);
  648. if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
  649. err = xfrm_state_add(x);
  650. else
  651. err = xfrm_state_update(x);
  652. xfrm_audit_state_add(x, err ? 0 : 1, true);
  653. if (err < 0) {
  654. x->km.state = XFRM_STATE_DEAD;
  655. xfrm_dev_state_delete(x);
  656. __xfrm_state_put(x);
  657. goto out;
  658. }
  659. if (x->km.state == XFRM_STATE_VOID)
  660. x->km.state = XFRM_STATE_VALID;
  661. c.seq = nlh->nlmsg_seq;
  662. c.portid = nlh->nlmsg_pid;
  663. c.event = nlh->nlmsg_type;
  664. km_state_notify(x, &c);
  665. out:
  666. xfrm_state_put(x);
  667. return err;
  668. }
  669. static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
  670. struct xfrm_usersa_id *p,
  671. struct nlattr **attrs,
  672. int *errp)
  673. {
  674. struct xfrm_state *x = NULL;
  675. struct xfrm_mark m;
  676. int err;
  677. u32 mark = xfrm_mark_get(attrs, &m);
  678. if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
  679. err = -ESRCH;
  680. x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
  681. } else {
  682. xfrm_address_t *saddr = NULL;
  683. verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
  684. if (!saddr) {
  685. err = -EINVAL;
  686. goto out;
  687. }
  688. err = -ESRCH;
  689. x = xfrm_state_lookup_byaddr(net, mark,
  690. &p->daddr, saddr,
  691. p->proto, p->family);
  692. }
  693. out:
  694. if (!x && errp)
  695. *errp = err;
  696. return x;
  697. }
  698. static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  699. struct nlattr **attrs, struct netlink_ext_ack *extack)
  700. {
  701. struct net *net = sock_net(skb->sk);
  702. struct xfrm_state *x;
  703. int err = -ESRCH;
  704. struct km_event c;
  705. struct xfrm_usersa_id *p = nlmsg_data(nlh);
  706. x = xfrm_user_state_lookup(net, p, attrs, &err);
  707. if (x == NULL)
  708. return err;
  709. if ((err = security_xfrm_state_delete(x)) != 0)
  710. goto out;
  711. if (xfrm_state_kern(x)) {
  712. err = -EPERM;
  713. goto out;
  714. }
  715. err = xfrm_state_delete(x);
  716. if (err < 0)
  717. goto out;
  718. c.seq = nlh->nlmsg_seq;
  719. c.portid = nlh->nlmsg_pid;
  720. c.event = nlh->nlmsg_type;
  721. km_state_notify(x, &c);
  722. out:
  723. xfrm_audit_state_delete(x, err ? 0 : 1, true);
  724. xfrm_state_put(x);
  725. return err;
  726. }
  727. static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  728. {
  729. memset(p, 0, sizeof(*p));
  730. memcpy(&p->id, &x->id, sizeof(p->id));
  731. memcpy(&p->sel, &x->sel, sizeof(p->sel));
  732. memcpy(&p->lft, &x->lft, sizeof(p->lft));
  733. memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
  734. put_unaligned(x->stats.replay_window, &p->stats.replay_window);
  735. put_unaligned(x->stats.replay, &p->stats.replay);
  736. put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
  737. memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
  738. p->mode = x->props.mode;
  739. p->replay_window = x->props.replay_window;
  740. p->reqid = x->props.reqid;
  741. p->family = x->props.family;
  742. p->flags = x->props.flags;
  743. p->seq = x->km.seq;
  744. }
  745. struct xfrm_dump_info {
  746. struct sk_buff *in_skb;
  747. struct sk_buff *out_skb;
  748. u32 nlmsg_seq;
  749. u16 nlmsg_flags;
  750. };
  751. static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
  752. {
  753. struct xfrm_user_sec_ctx *uctx;
  754. struct nlattr *attr;
  755. int ctx_size = sizeof(*uctx) + s->ctx_len;
  756. attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
  757. if (attr == NULL)
  758. return -EMSGSIZE;
  759. uctx = nla_data(attr);
  760. uctx->exttype = XFRMA_SEC_CTX;
  761. uctx->len = ctx_size;
  762. uctx->ctx_doi = s->ctx_doi;
  763. uctx->ctx_alg = s->ctx_alg;
  764. uctx->ctx_len = s->ctx_len;
  765. memcpy(uctx + 1, s->ctx_str, s->ctx_len);
  766. return 0;
  767. }
  768. static int copy_user_offload(struct xfrm_dev_offload *xso, struct sk_buff *skb)
  769. {
  770. struct xfrm_user_offload *xuo;
  771. struct nlattr *attr;
  772. attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
  773. if (attr == NULL)
  774. return -EMSGSIZE;
  775. xuo = nla_data(attr);
  776. memset(xuo, 0, sizeof(*xuo));
  777. xuo->ifindex = xso->dev->ifindex;
  778. if (xso->dir == XFRM_DEV_OFFLOAD_IN)
  779. xuo->flags = XFRM_OFFLOAD_INBOUND;
  780. return 0;
  781. }
  782. static bool xfrm_redact(void)
  783. {
  784. return IS_ENABLED(CONFIG_SECURITY) &&
  785. security_locked_down(LOCKDOWN_XFRM_SECRET);
  786. }
  787. static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
  788. {
  789. struct xfrm_algo *algo;
  790. struct xfrm_algo_auth *ap;
  791. struct nlattr *nla;
  792. bool redact_secret = xfrm_redact();
  793. nla = nla_reserve(skb, XFRMA_ALG_AUTH,
  794. sizeof(*algo) + (auth->alg_key_len + 7) / 8);
  795. if (!nla)
  796. return -EMSGSIZE;
  797. algo = nla_data(nla);
  798. strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
  799. if (redact_secret && auth->alg_key_len)
  800. memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8);
  801. else
  802. memcpy(algo->alg_key, auth->alg_key,
  803. (auth->alg_key_len + 7) / 8);
  804. algo->alg_key_len = auth->alg_key_len;
  805. nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth));
  806. if (!nla)
  807. return -EMSGSIZE;
  808. ap = nla_data(nla);
  809. memcpy(ap, auth, sizeof(struct xfrm_algo_auth));
  810. if (redact_secret && auth->alg_key_len)
  811. memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8);
  812. else
  813. memcpy(ap->alg_key, auth->alg_key,
  814. (auth->alg_key_len + 7) / 8);
  815. return 0;
  816. }
  817. static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
  818. {
  819. struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead));
  820. struct xfrm_algo_aead *ap;
  821. bool redact_secret = xfrm_redact();
  822. if (!nla)
  823. return -EMSGSIZE;
  824. ap = nla_data(nla);
  825. strscpy_pad(ap->alg_name, aead->alg_name, sizeof(ap->alg_name));
  826. ap->alg_key_len = aead->alg_key_len;
  827. ap->alg_icv_len = aead->alg_icv_len;
  828. if (redact_secret && aead->alg_key_len)
  829. memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
  830. else
  831. memcpy(ap->alg_key, aead->alg_key,
  832. (aead->alg_key_len + 7) / 8);
  833. return 0;
  834. }
  835. static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
  836. {
  837. struct xfrm_algo *ap;
  838. bool redact_secret = xfrm_redact();
  839. struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT,
  840. xfrm_alg_len(ealg));
  841. if (!nla)
  842. return -EMSGSIZE;
  843. ap = nla_data(nla);
  844. strscpy_pad(ap->alg_name, ealg->alg_name, sizeof(ap->alg_name));
  845. ap->alg_key_len = ealg->alg_key_len;
  846. if (redact_secret && ealg->alg_key_len)
  847. memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
  848. else
  849. memcpy(ap->alg_key, ealg->alg_key,
  850. (ealg->alg_key_len + 7) / 8);
  851. return 0;
  852. }
  853. static int copy_to_user_calg(struct xfrm_algo *calg, struct sk_buff *skb)
  854. {
  855. struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_COMP, sizeof(*calg));
  856. struct xfrm_algo *ap;
  857. if (!nla)
  858. return -EMSGSIZE;
  859. ap = nla_data(nla);
  860. strscpy_pad(ap->alg_name, calg->alg_name, sizeof(ap->alg_name));
  861. ap->alg_key_len = 0;
  862. return 0;
  863. }
  864. static int copy_to_user_encap(struct xfrm_encap_tmpl *ep, struct sk_buff *skb)
  865. {
  866. struct nlattr *nla = nla_reserve(skb, XFRMA_ENCAP, sizeof(*ep));
  867. struct xfrm_encap_tmpl *uep;
  868. if (!nla)
  869. return -EMSGSIZE;
  870. uep = nla_data(nla);
  871. memset(uep, 0, sizeof(*uep));
  872. uep->encap_type = ep->encap_type;
  873. uep->encap_sport = ep->encap_sport;
  874. uep->encap_dport = ep->encap_dport;
  875. uep->encap_oa = ep->encap_oa;
  876. return 0;
  877. }
  878. static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
  879. {
  880. int ret = 0;
  881. if (m->v | m->m) {
  882. ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
  883. if (!ret)
  884. ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
  885. }
  886. return ret;
  887. }
  888. /* Don't change this without updating xfrm_sa_len! */
  889. static int copy_to_user_state_extra(struct xfrm_state *x,
  890. struct xfrm_usersa_info *p,
  891. struct sk_buff *skb)
  892. {
  893. int ret = 0;
  894. copy_to_user_state(x, p);
  895. if (x->props.extra_flags) {
  896. ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
  897. x->props.extra_flags);
  898. if (ret)
  899. goto out;
  900. }
  901. if (x->coaddr) {
  902. ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
  903. if (ret)
  904. goto out;
  905. }
  906. if (x->lastused) {
  907. ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
  908. XFRMA_PAD);
  909. if (ret)
  910. goto out;
  911. }
  912. if (x->aead) {
  913. ret = copy_to_user_aead(x->aead, skb);
  914. if (ret)
  915. goto out;
  916. }
  917. if (x->aalg) {
  918. ret = copy_to_user_auth(x->aalg, skb);
  919. if (ret)
  920. goto out;
  921. }
  922. if (x->ealg) {
  923. ret = copy_to_user_ealg(x->ealg, skb);
  924. if (ret)
  925. goto out;
  926. }
  927. if (x->calg) {
  928. ret = copy_to_user_calg(x->calg, skb);
  929. if (ret)
  930. goto out;
  931. }
  932. if (x->encap) {
  933. ret = copy_to_user_encap(x->encap, skb);
  934. if (ret)
  935. goto out;
  936. }
  937. if (x->tfcpad) {
  938. ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
  939. if (ret)
  940. goto out;
  941. }
  942. ret = xfrm_mark_put(skb, &x->mark);
  943. if (ret)
  944. goto out;
  945. ret = xfrm_smark_put(skb, &x->props.smark);
  946. if (ret)
  947. goto out;
  948. if (x->replay_esn)
  949. ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
  950. xfrm_replay_state_esn_len(x->replay_esn),
  951. x->replay_esn);
  952. else
  953. ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
  954. &x->replay);
  955. if (ret)
  956. goto out;
  957. if(x->xso.dev)
  958. ret = copy_user_offload(&x->xso, skb);
  959. if (ret)
  960. goto out;
  961. if (x->if_id) {
  962. ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
  963. if (ret)
  964. goto out;
  965. }
  966. if (x->security) {
  967. ret = copy_sec_ctx(x->security, skb);
  968. if (ret)
  969. goto out;
  970. }
  971. if (x->mapping_maxage)
  972. ret = nla_put_u32(skb, XFRMA_MTIMER_THRESH, x->mapping_maxage);
  973. out:
  974. return ret;
  975. }
  976. static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
  977. {
  978. struct xfrm_dump_info *sp = ptr;
  979. struct sk_buff *in_skb = sp->in_skb;
  980. struct sk_buff *skb = sp->out_skb;
  981. struct xfrm_translator *xtr;
  982. struct xfrm_usersa_info *p;
  983. struct nlmsghdr *nlh;
  984. int err;
  985. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
  986. XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
  987. if (nlh == NULL)
  988. return -EMSGSIZE;
  989. p = nlmsg_data(nlh);
  990. err = copy_to_user_state_extra(x, p, skb);
  991. if (err) {
  992. nlmsg_cancel(skb, nlh);
  993. return err;
  994. }
  995. nlmsg_end(skb, nlh);
  996. xtr = xfrm_get_translator();
  997. if (xtr) {
  998. err = xtr->alloc_compat(skb, nlh);
  999. xfrm_put_translator(xtr);
  1000. if (err) {
  1001. nlmsg_cancel(skb, nlh);
  1002. return err;
  1003. }
  1004. }
  1005. return 0;
  1006. }
  1007. static int xfrm_dump_sa_done(struct netlink_callback *cb)
  1008. {
  1009. struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
  1010. struct sock *sk = cb->skb->sk;
  1011. struct net *net = sock_net(sk);
  1012. if (cb->args[0])
  1013. xfrm_state_walk_done(walk, net);
  1014. return 0;
  1015. }
  1016. static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
  1017. {
  1018. struct net *net = sock_net(skb->sk);
  1019. struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
  1020. struct xfrm_dump_info info;
  1021. BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
  1022. sizeof(cb->args) - sizeof(cb->args[0]));
  1023. info.in_skb = cb->skb;
  1024. info.out_skb = skb;
  1025. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  1026. info.nlmsg_flags = NLM_F_MULTI;
  1027. if (!cb->args[0]) {
  1028. struct nlattr *attrs[XFRMA_MAX+1];
  1029. struct xfrm_address_filter *filter = NULL;
  1030. u8 proto = 0;
  1031. int err;
  1032. err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
  1033. xfrma_policy, cb->extack);
  1034. if (err < 0)
  1035. return err;
  1036. if (attrs[XFRMA_ADDRESS_FILTER]) {
  1037. filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
  1038. sizeof(*filter), GFP_KERNEL);
  1039. if (filter == NULL)
  1040. return -ENOMEM;
  1041. /* see addr_match(), (prefix length >> 5) << 2
  1042. * will be used to compare xfrm_address_t
  1043. */
  1044. if (filter->splen > (sizeof(xfrm_address_t) << 3) ||
  1045. filter->dplen > (sizeof(xfrm_address_t) << 3)) {
  1046. kfree(filter);
  1047. return -EINVAL;
  1048. }
  1049. }
  1050. if (attrs[XFRMA_PROTO])
  1051. proto = nla_get_u8(attrs[XFRMA_PROTO]);
  1052. xfrm_state_walk_init(walk, proto, filter);
  1053. cb->args[0] = 1;
  1054. }
  1055. (void) xfrm_state_walk(net, walk, dump_one_state, &info);
  1056. return skb->len;
  1057. }
  1058. static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
  1059. struct xfrm_state *x, u32 seq)
  1060. {
  1061. struct xfrm_dump_info info;
  1062. struct sk_buff *skb;
  1063. int err;
  1064. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1065. if (!skb)
  1066. return ERR_PTR(-ENOMEM);
  1067. info.in_skb = in_skb;
  1068. info.out_skb = skb;
  1069. info.nlmsg_seq = seq;
  1070. info.nlmsg_flags = 0;
  1071. err = dump_one_state(x, 0, &info);
  1072. if (err) {
  1073. kfree_skb(skb);
  1074. return ERR_PTR(err);
  1075. }
  1076. return skb;
  1077. }
  1078. /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
  1079. * Must be called with RCU read lock.
  1080. */
  1081. static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
  1082. u32 pid, unsigned int group)
  1083. {
  1084. struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
  1085. struct xfrm_translator *xtr;
  1086. if (!nlsk) {
  1087. kfree_skb(skb);
  1088. return -EPIPE;
  1089. }
  1090. xtr = xfrm_get_translator();
  1091. if (xtr) {
  1092. int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
  1093. xfrm_put_translator(xtr);
  1094. if (err) {
  1095. kfree_skb(skb);
  1096. return err;
  1097. }
  1098. }
  1099. return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
  1100. }
  1101. static inline unsigned int xfrm_spdinfo_msgsize(void)
  1102. {
  1103. return NLMSG_ALIGN(4)
  1104. + nla_total_size(sizeof(struct xfrmu_spdinfo))
  1105. + nla_total_size(sizeof(struct xfrmu_spdhinfo))
  1106. + nla_total_size(sizeof(struct xfrmu_spdhthresh))
  1107. + nla_total_size(sizeof(struct xfrmu_spdhthresh));
  1108. }
  1109. static int build_spdinfo(struct sk_buff *skb, struct net *net,
  1110. u32 portid, u32 seq, u32 flags)
  1111. {
  1112. struct xfrmk_spdinfo si;
  1113. struct xfrmu_spdinfo spc;
  1114. struct xfrmu_spdhinfo sph;
  1115. struct xfrmu_spdhthresh spt4, spt6;
  1116. struct nlmsghdr *nlh;
  1117. int err;
  1118. u32 *f;
  1119. unsigned lseq;
  1120. nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
  1121. if (nlh == NULL) /* shouldn't really happen ... */
  1122. return -EMSGSIZE;
  1123. f = nlmsg_data(nlh);
  1124. *f = flags;
  1125. xfrm_spd_getinfo(net, &si);
  1126. spc.incnt = si.incnt;
  1127. spc.outcnt = si.outcnt;
  1128. spc.fwdcnt = si.fwdcnt;
  1129. spc.inscnt = si.inscnt;
  1130. spc.outscnt = si.outscnt;
  1131. spc.fwdscnt = si.fwdscnt;
  1132. sph.spdhcnt = si.spdhcnt;
  1133. sph.spdhmcnt = si.spdhmcnt;
  1134. do {
  1135. lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
  1136. spt4.lbits = net->xfrm.policy_hthresh.lbits4;
  1137. spt4.rbits = net->xfrm.policy_hthresh.rbits4;
  1138. spt6.lbits = net->xfrm.policy_hthresh.lbits6;
  1139. spt6.rbits = net->xfrm.policy_hthresh.rbits6;
  1140. } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
  1141. err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
  1142. if (!err)
  1143. err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
  1144. if (!err)
  1145. err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
  1146. if (!err)
  1147. err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
  1148. if (err) {
  1149. nlmsg_cancel(skb, nlh);
  1150. return err;
  1151. }
  1152. nlmsg_end(skb, nlh);
  1153. return 0;
  1154. }
  1155. static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  1156. struct nlattr **attrs,
  1157. struct netlink_ext_ack *extack)
  1158. {
  1159. struct net *net = sock_net(skb->sk);
  1160. struct xfrmu_spdhthresh *thresh4 = NULL;
  1161. struct xfrmu_spdhthresh *thresh6 = NULL;
  1162. /* selector prefixlen thresholds to hash policies */
  1163. if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
  1164. struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
  1165. if (nla_len(rta) < sizeof(*thresh4))
  1166. return -EINVAL;
  1167. thresh4 = nla_data(rta);
  1168. if (thresh4->lbits > 32 || thresh4->rbits > 32)
  1169. return -EINVAL;
  1170. }
  1171. if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
  1172. struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
  1173. if (nla_len(rta) < sizeof(*thresh6))
  1174. return -EINVAL;
  1175. thresh6 = nla_data(rta);
  1176. if (thresh6->lbits > 128 || thresh6->rbits > 128)
  1177. return -EINVAL;
  1178. }
  1179. if (thresh4 || thresh6) {
  1180. write_seqlock(&net->xfrm.policy_hthresh.lock);
  1181. if (thresh4) {
  1182. net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
  1183. net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
  1184. }
  1185. if (thresh6) {
  1186. net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
  1187. net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
  1188. }
  1189. write_sequnlock(&net->xfrm.policy_hthresh.lock);
  1190. xfrm_policy_hash_rebuild(net);
  1191. }
  1192. return 0;
  1193. }
  1194. static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  1195. struct nlattr **attrs,
  1196. struct netlink_ext_ack *extack)
  1197. {
  1198. struct net *net = sock_net(skb->sk);
  1199. struct sk_buff *r_skb;
  1200. u32 *flags = nlmsg_data(nlh);
  1201. u32 sportid = NETLINK_CB(skb).portid;
  1202. u32 seq = nlh->nlmsg_seq;
  1203. int err;
  1204. r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
  1205. if (r_skb == NULL)
  1206. return -ENOMEM;
  1207. err = build_spdinfo(r_skb, net, sportid, seq, *flags);
  1208. BUG_ON(err < 0);
  1209. return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
  1210. }
  1211. static inline unsigned int xfrm_sadinfo_msgsize(void)
  1212. {
  1213. return NLMSG_ALIGN(4)
  1214. + nla_total_size(sizeof(struct xfrmu_sadhinfo))
  1215. + nla_total_size(4); /* XFRMA_SAD_CNT */
  1216. }
  1217. static int build_sadinfo(struct sk_buff *skb, struct net *net,
  1218. u32 portid, u32 seq, u32 flags)
  1219. {
  1220. struct xfrmk_sadinfo si;
  1221. struct xfrmu_sadhinfo sh;
  1222. struct nlmsghdr *nlh;
  1223. int err;
  1224. u32 *f;
  1225. nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
  1226. if (nlh == NULL) /* shouldn't really happen ... */
  1227. return -EMSGSIZE;
  1228. f = nlmsg_data(nlh);
  1229. *f = flags;
  1230. xfrm_sad_getinfo(net, &si);
  1231. sh.sadhmcnt = si.sadhmcnt;
  1232. sh.sadhcnt = si.sadhcnt;
  1233. err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
  1234. if (!err)
  1235. err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
  1236. if (err) {
  1237. nlmsg_cancel(skb, nlh);
  1238. return err;
  1239. }
  1240. nlmsg_end(skb, nlh);
  1241. return 0;
  1242. }
  1243. static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  1244. struct nlattr **attrs,
  1245. struct netlink_ext_ack *extack)
  1246. {
  1247. struct net *net = sock_net(skb->sk);
  1248. struct sk_buff *r_skb;
  1249. u32 *flags = nlmsg_data(nlh);
  1250. u32 sportid = NETLINK_CB(skb).portid;
  1251. u32 seq = nlh->nlmsg_seq;
  1252. int err;
  1253. r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
  1254. if (r_skb == NULL)
  1255. return -ENOMEM;
  1256. err = build_sadinfo(r_skb, net, sportid, seq, *flags);
  1257. BUG_ON(err < 0);
  1258. return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
  1259. }
  1260. static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  1261. struct nlattr **attrs, struct netlink_ext_ack *extack)
  1262. {
  1263. struct net *net = sock_net(skb->sk);
  1264. struct xfrm_usersa_id *p = nlmsg_data(nlh);
  1265. struct xfrm_state *x;
  1266. struct sk_buff *resp_skb;
  1267. int err = -ESRCH;
  1268. x = xfrm_user_state_lookup(net, p, attrs, &err);
  1269. if (x == NULL)
  1270. goto out_noput;
  1271. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  1272. if (IS_ERR(resp_skb)) {
  1273. err = PTR_ERR(resp_skb);
  1274. } else {
  1275. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
  1276. }
  1277. xfrm_state_put(x);
  1278. out_noput:
  1279. return err;
  1280. }
  1281. static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
  1282. struct nlattr **attrs,
  1283. struct netlink_ext_ack *extack)
  1284. {
  1285. struct net *net = sock_net(skb->sk);
  1286. struct xfrm_state *x;
  1287. struct xfrm_userspi_info *p;
  1288. struct xfrm_translator *xtr;
  1289. struct sk_buff *resp_skb;
  1290. xfrm_address_t *daddr;
  1291. int family;
  1292. int err;
  1293. u32 mark;
  1294. struct xfrm_mark m;
  1295. u32 if_id = 0;
  1296. p = nlmsg_data(nlh);
  1297. err = verify_spi_info(p->info.id.proto, p->min, p->max);
  1298. if (err)
  1299. goto out_noput;
  1300. family = p->info.family;
  1301. daddr = &p->info.id.daddr;
  1302. x = NULL;
  1303. mark = xfrm_mark_get(attrs, &m);
  1304. if (attrs[XFRMA_IF_ID])
  1305. if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
  1306. if (p->info.seq) {
  1307. x = xfrm_find_acq_byseq(net, mark, p->info.seq);
  1308. if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
  1309. xfrm_state_put(x);
  1310. x = NULL;
  1311. }
  1312. }
  1313. if (!x)
  1314. x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
  1315. if_id, p->info.id.proto, daddr,
  1316. &p->info.saddr, 1,
  1317. family);
  1318. err = -ENOENT;
  1319. if (x == NULL)
  1320. goto out_noput;
  1321. err = xfrm_alloc_spi(x, p->min, p->max);
  1322. if (err)
  1323. goto out;
  1324. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  1325. if (IS_ERR(resp_skb)) {
  1326. err = PTR_ERR(resp_skb);
  1327. goto out;
  1328. }
  1329. xtr = xfrm_get_translator();
  1330. if (xtr) {
  1331. err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
  1332. xfrm_put_translator(xtr);
  1333. if (err) {
  1334. kfree_skb(resp_skb);
  1335. goto out;
  1336. }
  1337. }
  1338. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
  1339. out:
  1340. xfrm_state_put(x);
  1341. out_noput:
  1342. return err;
  1343. }
  1344. static int verify_policy_dir(u8 dir, struct netlink_ext_ack *extack)
  1345. {
  1346. switch (dir) {
  1347. case XFRM_POLICY_IN:
  1348. case XFRM_POLICY_OUT:
  1349. case XFRM_POLICY_FWD:
  1350. break;
  1351. default:
  1352. NL_SET_ERR_MSG(extack, "Invalid policy direction");
  1353. return -EINVAL;
  1354. }
  1355. return 0;
  1356. }
  1357. static int verify_policy_type(u8 type, struct netlink_ext_ack *extack)
  1358. {
  1359. switch (type) {
  1360. case XFRM_POLICY_TYPE_MAIN:
  1361. #ifdef CONFIG_XFRM_SUB_POLICY
  1362. case XFRM_POLICY_TYPE_SUB:
  1363. #endif
  1364. break;
  1365. default:
  1366. NL_SET_ERR_MSG(extack, "Invalid policy type");
  1367. return -EINVAL;
  1368. }
  1369. return 0;
  1370. }
  1371. static int verify_newpolicy_info(struct xfrm_userpolicy_info *p,
  1372. struct netlink_ext_ack *extack)
  1373. {
  1374. int ret;
  1375. switch (p->share) {
  1376. case XFRM_SHARE_ANY:
  1377. case XFRM_SHARE_SESSION:
  1378. case XFRM_SHARE_USER:
  1379. case XFRM_SHARE_UNIQUE:
  1380. break;
  1381. default:
  1382. NL_SET_ERR_MSG(extack, "Invalid policy share");
  1383. return -EINVAL;
  1384. }
  1385. switch (p->action) {
  1386. case XFRM_POLICY_ALLOW:
  1387. case XFRM_POLICY_BLOCK:
  1388. break;
  1389. default:
  1390. NL_SET_ERR_MSG(extack, "Invalid policy action");
  1391. return -EINVAL;
  1392. }
  1393. switch (p->sel.family) {
  1394. case AF_INET:
  1395. if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) {
  1396. NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)");
  1397. return -EINVAL;
  1398. }
  1399. break;
  1400. case AF_INET6:
  1401. #if IS_ENABLED(CONFIG_IPV6)
  1402. if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) {
  1403. NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)");
  1404. return -EINVAL;
  1405. }
  1406. break;
  1407. #else
  1408. NL_SET_ERR_MSG(extack, "IPv6 support disabled");
  1409. return -EAFNOSUPPORT;
  1410. #endif
  1411. default:
  1412. NL_SET_ERR_MSG(extack, "Invalid selector family");
  1413. return -EINVAL;
  1414. }
  1415. ret = verify_policy_dir(p->dir, extack);
  1416. if (ret)
  1417. return ret;
  1418. if (p->index && (xfrm_policy_id2dir(p->index) != p->dir)) {
  1419. NL_SET_ERR_MSG(extack, "Policy index doesn't match direction");
  1420. return -EINVAL;
  1421. }
  1422. return 0;
  1423. }
  1424. static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
  1425. {
  1426. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1427. struct xfrm_user_sec_ctx *uctx;
  1428. if (!rt)
  1429. return 0;
  1430. uctx = nla_data(rt);
  1431. return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
  1432. }
  1433. static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
  1434. int nr)
  1435. {
  1436. int i;
  1437. xp->xfrm_nr = nr;
  1438. for (i = 0; i < nr; i++, ut++) {
  1439. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  1440. memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
  1441. memcpy(&t->saddr, &ut->saddr,
  1442. sizeof(xfrm_address_t));
  1443. t->reqid = ut->reqid;
  1444. t->mode = ut->mode;
  1445. t->share = ut->share;
  1446. t->optional = ut->optional;
  1447. t->aalgos = ut->aalgos;
  1448. t->ealgos = ut->ealgos;
  1449. t->calgos = ut->calgos;
  1450. /* If all masks are ~0, then we allow all algorithms. */
  1451. t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
  1452. t->encap_family = ut->family;
  1453. }
  1454. }
  1455. static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family,
  1456. int dir, struct netlink_ext_ack *extack)
  1457. {
  1458. u16 prev_family;
  1459. int i;
  1460. if (nr > XFRM_MAX_DEPTH) {
  1461. NL_SET_ERR_MSG(extack, "Template count must be <= XFRM_MAX_DEPTH (" __stringify(XFRM_MAX_DEPTH) ")");
  1462. return -EINVAL;
  1463. }
  1464. prev_family = family;
  1465. for (i = 0; i < nr; i++) {
  1466. /* We never validated the ut->family value, so many
  1467. * applications simply leave it at zero. The check was
  1468. * never made and ut->family was ignored because all
  1469. * templates could be assumed to have the same family as
  1470. * the policy itself. Now that we will have ipv4-in-ipv6
  1471. * and ipv6-in-ipv4 tunnels, this is no longer true.
  1472. */
  1473. if (!ut[i].family)
  1474. ut[i].family = family;
  1475. switch (ut[i].mode) {
  1476. case XFRM_MODE_TUNNEL:
  1477. case XFRM_MODE_BEET:
  1478. if (ut[i].optional && dir == XFRM_POLICY_OUT) {
  1479. NL_SET_ERR_MSG(extack, "Mode in optional template not allowed in outbound policy");
  1480. return -EINVAL;
  1481. }
  1482. break;
  1483. default:
  1484. if (ut[i].family != prev_family) {
  1485. NL_SET_ERR_MSG(extack, "Mode in template doesn't support a family change");
  1486. return -EINVAL;
  1487. }
  1488. break;
  1489. }
  1490. if (ut[i].mode >= XFRM_MODE_MAX) {
  1491. NL_SET_ERR_MSG(extack, "Mode in template must be < XFRM_MODE_MAX (" __stringify(XFRM_MODE_MAX) ")");
  1492. return -EINVAL;
  1493. }
  1494. prev_family = ut[i].family;
  1495. switch (ut[i].family) {
  1496. case AF_INET:
  1497. break;
  1498. #if IS_ENABLED(CONFIG_IPV6)
  1499. case AF_INET6:
  1500. break;
  1501. #endif
  1502. default:
  1503. NL_SET_ERR_MSG(extack, "Invalid family in template");
  1504. return -EINVAL;
  1505. }
  1506. if (!xfrm_id_proto_valid(ut[i].id.proto)) {
  1507. NL_SET_ERR_MSG(extack, "Invalid XFRM protocol in template");
  1508. return -EINVAL;
  1509. }
  1510. }
  1511. return 0;
  1512. }
  1513. static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs,
  1514. int dir, struct netlink_ext_ack *extack)
  1515. {
  1516. struct nlattr *rt = attrs[XFRMA_TMPL];
  1517. if (!rt) {
  1518. pol->xfrm_nr = 0;
  1519. } else {
  1520. struct xfrm_user_tmpl *utmpl = nla_data(rt);
  1521. int nr = nla_len(rt) / sizeof(*utmpl);
  1522. int err;
  1523. err = validate_tmpl(nr, utmpl, pol->family, dir, extack);
  1524. if (err)
  1525. return err;
  1526. copy_templates(pol, utmpl, nr);
  1527. }
  1528. return 0;
  1529. }
  1530. static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs,
  1531. struct netlink_ext_ack *extack)
  1532. {
  1533. struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
  1534. struct xfrm_userpolicy_type *upt;
  1535. u8 type = XFRM_POLICY_TYPE_MAIN;
  1536. int err;
  1537. if (rt) {
  1538. upt = nla_data(rt);
  1539. type = upt->type;
  1540. }
  1541. err = verify_policy_type(type, extack);
  1542. if (err)
  1543. return err;
  1544. *tp = type;
  1545. return 0;
  1546. }
  1547. static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
  1548. {
  1549. xp->priority = p->priority;
  1550. xp->index = p->index;
  1551. memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
  1552. memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
  1553. xp->action = p->action;
  1554. xp->flags = p->flags;
  1555. xp->family = p->sel.family;
  1556. /* XXX xp->share = p->share; */
  1557. }
  1558. static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
  1559. {
  1560. memset(p, 0, sizeof(*p));
  1561. memcpy(&p->sel, &xp->selector, sizeof(p->sel));
  1562. memcpy(&p->lft, &xp->lft, sizeof(p->lft));
  1563. memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
  1564. p->priority = xp->priority;
  1565. p->index = xp->index;
  1566. p->sel.family = xp->family;
  1567. p->dir = dir;
  1568. p->action = xp->action;
  1569. p->flags = xp->flags;
  1570. p->share = XFRM_SHARE_ANY; /* XXX xp->share */
  1571. }
  1572. static struct xfrm_policy *xfrm_policy_construct(struct net *net,
  1573. struct xfrm_userpolicy_info *p,
  1574. struct nlattr **attrs,
  1575. int *errp,
  1576. struct netlink_ext_ack *extack)
  1577. {
  1578. struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
  1579. int err;
  1580. if (!xp) {
  1581. *errp = -ENOMEM;
  1582. return NULL;
  1583. }
  1584. copy_from_user_policy(xp, p);
  1585. err = copy_from_user_policy_type(&xp->type, attrs, extack);
  1586. if (err)
  1587. goto error;
  1588. if (!(err = copy_from_user_tmpl(xp, attrs, p->dir, extack)))
  1589. err = copy_from_user_sec_ctx(xp, attrs);
  1590. if (err)
  1591. goto error;
  1592. xfrm_mark_get(attrs, &xp->mark);
  1593. if (attrs[XFRMA_IF_ID])
  1594. xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
  1595. return xp;
  1596. error:
  1597. *errp = err;
  1598. xp->walk.dead = 1;
  1599. xfrm_policy_destroy(xp);
  1600. return NULL;
  1601. }
  1602. static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1603. struct nlattr **attrs,
  1604. struct netlink_ext_ack *extack)
  1605. {
  1606. struct net *net = sock_net(skb->sk);
  1607. struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
  1608. struct xfrm_policy *xp;
  1609. struct km_event c;
  1610. int err;
  1611. int excl;
  1612. err = verify_newpolicy_info(p, extack);
  1613. if (err)
  1614. return err;
  1615. err = verify_sec_ctx_len(attrs, extack);
  1616. if (err)
  1617. return err;
  1618. xp = xfrm_policy_construct(net, p, attrs, &err, extack);
  1619. if (!xp)
  1620. return err;
  1621. /* shouldn't excl be based on nlh flags??
  1622. * Aha! this is anti-netlink really i.e more pfkey derived
  1623. * in netlink excl is a flag and you wouldn't need
  1624. * a type XFRM_MSG_UPDPOLICY - JHS */
  1625. excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
  1626. err = xfrm_policy_insert(p->dir, xp, excl);
  1627. xfrm_audit_policy_add(xp, err ? 0 : 1, true);
  1628. if (err) {
  1629. security_xfrm_policy_free(xp->security);
  1630. kfree(xp);
  1631. return err;
  1632. }
  1633. c.event = nlh->nlmsg_type;
  1634. c.seq = nlh->nlmsg_seq;
  1635. c.portid = nlh->nlmsg_pid;
  1636. km_policy_notify(xp, p->dir, &c);
  1637. xfrm_pol_put(xp);
  1638. return 0;
  1639. }
  1640. static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
  1641. {
  1642. struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
  1643. int i;
  1644. if (xp->xfrm_nr == 0)
  1645. return 0;
  1646. for (i = 0; i < xp->xfrm_nr; i++) {
  1647. struct xfrm_user_tmpl *up = &vec[i];
  1648. struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
  1649. memset(up, 0, sizeof(*up));
  1650. memcpy(&up->id, &kp->id, sizeof(up->id));
  1651. up->family = kp->encap_family;
  1652. memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
  1653. up->reqid = kp->reqid;
  1654. up->mode = kp->mode;
  1655. up->share = kp->share;
  1656. up->optional = kp->optional;
  1657. up->aalgos = kp->aalgos;
  1658. up->ealgos = kp->ealgos;
  1659. up->calgos = kp->calgos;
  1660. }
  1661. return nla_put(skb, XFRMA_TMPL,
  1662. sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
  1663. }
  1664. static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
  1665. {
  1666. if (x->security) {
  1667. return copy_sec_ctx(x->security, skb);
  1668. }
  1669. return 0;
  1670. }
  1671. static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
  1672. {
  1673. if (xp->security)
  1674. return copy_sec_ctx(xp->security, skb);
  1675. return 0;
  1676. }
  1677. static inline unsigned int userpolicy_type_attrsize(void)
  1678. {
  1679. #ifdef CONFIG_XFRM_SUB_POLICY
  1680. return nla_total_size(sizeof(struct xfrm_userpolicy_type));
  1681. #else
  1682. return 0;
  1683. #endif
  1684. }
  1685. #ifdef CONFIG_XFRM_SUB_POLICY
  1686. static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1687. {
  1688. struct xfrm_userpolicy_type upt;
  1689. /* Sadly there are two holes in struct xfrm_userpolicy_type */
  1690. memset(&upt, 0, sizeof(upt));
  1691. upt.type = type;
  1692. return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
  1693. }
  1694. #else
  1695. static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1696. {
  1697. return 0;
  1698. }
  1699. #endif
  1700. static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
  1701. {
  1702. struct xfrm_dump_info *sp = ptr;
  1703. struct xfrm_userpolicy_info *p;
  1704. struct sk_buff *in_skb = sp->in_skb;
  1705. struct sk_buff *skb = sp->out_skb;
  1706. struct xfrm_translator *xtr;
  1707. struct nlmsghdr *nlh;
  1708. int err;
  1709. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
  1710. XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
  1711. if (nlh == NULL)
  1712. return -EMSGSIZE;
  1713. p = nlmsg_data(nlh);
  1714. copy_to_user_policy(xp, p, dir);
  1715. err = copy_to_user_tmpl(xp, skb);
  1716. if (!err)
  1717. err = copy_to_user_sec_ctx(xp, skb);
  1718. if (!err)
  1719. err = copy_to_user_policy_type(xp->type, skb);
  1720. if (!err)
  1721. err = xfrm_mark_put(skb, &xp->mark);
  1722. if (!err)
  1723. err = xfrm_if_id_put(skb, xp->if_id);
  1724. if (err) {
  1725. nlmsg_cancel(skb, nlh);
  1726. return err;
  1727. }
  1728. nlmsg_end(skb, nlh);
  1729. xtr = xfrm_get_translator();
  1730. if (xtr) {
  1731. err = xtr->alloc_compat(skb, nlh);
  1732. xfrm_put_translator(xtr);
  1733. if (err) {
  1734. nlmsg_cancel(skb, nlh);
  1735. return err;
  1736. }
  1737. }
  1738. return 0;
  1739. }
  1740. static int xfrm_dump_policy_done(struct netlink_callback *cb)
  1741. {
  1742. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
  1743. struct net *net = sock_net(cb->skb->sk);
  1744. xfrm_policy_walk_done(walk, net);
  1745. return 0;
  1746. }
  1747. static int xfrm_dump_policy_start(struct netlink_callback *cb)
  1748. {
  1749. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
  1750. BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
  1751. xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
  1752. return 0;
  1753. }
  1754. static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
  1755. {
  1756. struct net *net = sock_net(skb->sk);
  1757. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
  1758. struct xfrm_dump_info info;
  1759. info.in_skb = cb->skb;
  1760. info.out_skb = skb;
  1761. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  1762. info.nlmsg_flags = NLM_F_MULTI;
  1763. (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
  1764. return skb->len;
  1765. }
  1766. static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
  1767. struct xfrm_policy *xp,
  1768. int dir, u32 seq)
  1769. {
  1770. struct xfrm_dump_info info;
  1771. struct sk_buff *skb;
  1772. int err;
  1773. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1774. if (!skb)
  1775. return ERR_PTR(-ENOMEM);
  1776. info.in_skb = in_skb;
  1777. info.out_skb = skb;
  1778. info.nlmsg_seq = seq;
  1779. info.nlmsg_flags = 0;
  1780. err = dump_one_policy(xp, dir, 0, &info);
  1781. if (err) {
  1782. kfree_skb(skb);
  1783. return ERR_PTR(err);
  1784. }
  1785. return skb;
  1786. }
  1787. static int xfrm_notify_userpolicy(struct net *net)
  1788. {
  1789. struct xfrm_userpolicy_default *up;
  1790. int len = NLMSG_ALIGN(sizeof(*up));
  1791. struct nlmsghdr *nlh;
  1792. struct sk_buff *skb;
  1793. int err;
  1794. skb = nlmsg_new(len, GFP_ATOMIC);
  1795. if (skb == NULL)
  1796. return -ENOMEM;
  1797. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0);
  1798. if (nlh == NULL) {
  1799. kfree_skb(skb);
  1800. return -EMSGSIZE;
  1801. }
  1802. up = nlmsg_data(nlh);
  1803. up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
  1804. up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
  1805. up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
  1806. nlmsg_end(skb, nlh);
  1807. rcu_read_lock();
  1808. err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
  1809. rcu_read_unlock();
  1810. return err;
  1811. }
  1812. static bool xfrm_userpolicy_is_valid(__u8 policy)
  1813. {
  1814. return policy == XFRM_USERPOLICY_BLOCK ||
  1815. policy == XFRM_USERPOLICY_ACCEPT;
  1816. }
  1817. static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
  1818. struct nlattr **attrs, struct netlink_ext_ack *extack)
  1819. {
  1820. struct net *net = sock_net(skb->sk);
  1821. struct xfrm_userpolicy_default *up = nlmsg_data(nlh);
  1822. if (xfrm_userpolicy_is_valid(up->in))
  1823. net->xfrm.policy_default[XFRM_POLICY_IN] = up->in;
  1824. if (xfrm_userpolicy_is_valid(up->fwd))
  1825. net->xfrm.policy_default[XFRM_POLICY_FWD] = up->fwd;
  1826. if (xfrm_userpolicy_is_valid(up->out))
  1827. net->xfrm.policy_default[XFRM_POLICY_OUT] = up->out;
  1828. rt_genid_bump_all(net);
  1829. xfrm_notify_userpolicy(net);
  1830. return 0;
  1831. }
  1832. static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
  1833. struct nlattr **attrs, struct netlink_ext_ack *extack)
  1834. {
  1835. struct sk_buff *r_skb;
  1836. struct nlmsghdr *r_nlh;
  1837. struct net *net = sock_net(skb->sk);
  1838. struct xfrm_userpolicy_default *r_up;
  1839. int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default));
  1840. u32 portid = NETLINK_CB(skb).portid;
  1841. u32 seq = nlh->nlmsg_seq;
  1842. r_skb = nlmsg_new(len, GFP_ATOMIC);
  1843. if (!r_skb)
  1844. return -ENOMEM;
  1845. r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0);
  1846. if (!r_nlh) {
  1847. kfree_skb(r_skb);
  1848. return -EMSGSIZE;
  1849. }
  1850. r_up = nlmsg_data(r_nlh);
  1851. r_up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
  1852. r_up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
  1853. r_up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
  1854. nlmsg_end(r_skb, r_nlh);
  1855. return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid);
  1856. }
  1857. static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1858. struct nlattr **attrs,
  1859. struct netlink_ext_ack *extack)
  1860. {
  1861. struct net *net = sock_net(skb->sk);
  1862. struct xfrm_policy *xp;
  1863. struct xfrm_userpolicy_id *p;
  1864. u8 type = XFRM_POLICY_TYPE_MAIN;
  1865. int err;
  1866. struct km_event c;
  1867. int delete;
  1868. struct xfrm_mark m;
  1869. u32 if_id = 0;
  1870. p = nlmsg_data(nlh);
  1871. delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
  1872. err = copy_from_user_policy_type(&type, attrs, extack);
  1873. if (err)
  1874. return err;
  1875. err = verify_policy_dir(p->dir, extack);
  1876. if (err)
  1877. return err;
  1878. if (attrs[XFRMA_IF_ID])
  1879. if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
  1880. xfrm_mark_get(attrs, &m);
  1881. if (p->index)
  1882. xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
  1883. p->index, delete, &err);
  1884. else {
  1885. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1886. struct xfrm_sec_ctx *ctx;
  1887. err = verify_sec_ctx_len(attrs, extack);
  1888. if (err)
  1889. return err;
  1890. ctx = NULL;
  1891. if (rt) {
  1892. struct xfrm_user_sec_ctx *uctx = nla_data(rt);
  1893. err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
  1894. if (err)
  1895. return err;
  1896. }
  1897. xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
  1898. &p->sel, ctx, delete, &err);
  1899. security_xfrm_policy_free(ctx);
  1900. }
  1901. if (xp == NULL)
  1902. return -ENOENT;
  1903. if (!delete) {
  1904. struct sk_buff *resp_skb;
  1905. resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
  1906. if (IS_ERR(resp_skb)) {
  1907. err = PTR_ERR(resp_skb);
  1908. } else {
  1909. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
  1910. NETLINK_CB(skb).portid);
  1911. }
  1912. } else {
  1913. xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
  1914. if (err != 0)
  1915. goto out;
  1916. c.data.byid = p->index;
  1917. c.event = nlh->nlmsg_type;
  1918. c.seq = nlh->nlmsg_seq;
  1919. c.portid = nlh->nlmsg_pid;
  1920. km_policy_notify(xp, p->dir, &c);
  1921. }
  1922. out:
  1923. xfrm_pol_put(xp);
  1924. return err;
  1925. }
  1926. static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  1927. struct nlattr **attrs,
  1928. struct netlink_ext_ack *extack)
  1929. {
  1930. struct net *net = sock_net(skb->sk);
  1931. struct km_event c;
  1932. struct xfrm_usersa_flush *p = nlmsg_data(nlh);
  1933. int err;
  1934. err = xfrm_state_flush(net, p->proto, true, false);
  1935. if (err) {
  1936. if (err == -ESRCH) /* empty table */
  1937. return 0;
  1938. return err;
  1939. }
  1940. c.data.proto = p->proto;
  1941. c.event = nlh->nlmsg_type;
  1942. c.seq = nlh->nlmsg_seq;
  1943. c.portid = nlh->nlmsg_pid;
  1944. c.net = net;
  1945. km_state_notify(NULL, &c);
  1946. return 0;
  1947. }
  1948. static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
  1949. {
  1950. unsigned int replay_size = x->replay_esn ?
  1951. xfrm_replay_state_esn_len(x->replay_esn) :
  1952. sizeof(struct xfrm_replay_state);
  1953. return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
  1954. + nla_total_size(replay_size)
  1955. + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
  1956. + nla_total_size(sizeof(struct xfrm_mark))
  1957. + nla_total_size(4) /* XFRM_AE_RTHR */
  1958. + nla_total_size(4); /* XFRM_AE_ETHR */
  1959. }
  1960. static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
  1961. {
  1962. struct xfrm_aevent_id *id;
  1963. struct nlmsghdr *nlh;
  1964. int err;
  1965. nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
  1966. if (nlh == NULL)
  1967. return -EMSGSIZE;
  1968. id = nlmsg_data(nlh);
  1969. memset(&id->sa_id, 0, sizeof(id->sa_id));
  1970. memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
  1971. id->sa_id.spi = x->id.spi;
  1972. id->sa_id.family = x->props.family;
  1973. id->sa_id.proto = x->id.proto;
  1974. memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
  1975. id->reqid = x->props.reqid;
  1976. id->flags = c->data.aevent;
  1977. if (x->replay_esn) {
  1978. err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
  1979. xfrm_replay_state_esn_len(x->replay_esn),
  1980. x->replay_esn);
  1981. } else {
  1982. err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
  1983. &x->replay);
  1984. }
  1985. if (err)
  1986. goto out_cancel;
  1987. err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
  1988. XFRMA_PAD);
  1989. if (err)
  1990. goto out_cancel;
  1991. if (id->flags & XFRM_AE_RTHR) {
  1992. err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
  1993. if (err)
  1994. goto out_cancel;
  1995. }
  1996. if (id->flags & XFRM_AE_ETHR) {
  1997. err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
  1998. x->replay_maxage * 10 / HZ);
  1999. if (err)
  2000. goto out_cancel;
  2001. }
  2002. err = xfrm_mark_put(skb, &x->mark);
  2003. if (err)
  2004. goto out_cancel;
  2005. err = xfrm_if_id_put(skb, x->if_id);
  2006. if (err)
  2007. goto out_cancel;
  2008. nlmsg_end(skb, nlh);
  2009. return 0;
  2010. out_cancel:
  2011. nlmsg_cancel(skb, nlh);
  2012. return err;
  2013. }
  2014. static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  2015. struct nlattr **attrs, struct netlink_ext_ack *extack)
  2016. {
  2017. struct net *net = sock_net(skb->sk);
  2018. struct xfrm_state *x;
  2019. struct sk_buff *r_skb;
  2020. int err;
  2021. struct km_event c;
  2022. u32 mark;
  2023. struct xfrm_mark m;
  2024. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  2025. struct xfrm_usersa_id *id = &p->sa_id;
  2026. mark = xfrm_mark_get(attrs, &m);
  2027. x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
  2028. if (x == NULL)
  2029. return -ESRCH;
  2030. r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
  2031. if (r_skb == NULL) {
  2032. xfrm_state_put(x);
  2033. return -ENOMEM;
  2034. }
  2035. /*
  2036. * XXX: is this lock really needed - none of the other
  2037. * gets lock (the concern is things getting updated
  2038. * while we are still reading) - jhs
  2039. */
  2040. spin_lock_bh(&x->lock);
  2041. c.data.aevent = p->flags;
  2042. c.seq = nlh->nlmsg_seq;
  2043. c.portid = nlh->nlmsg_pid;
  2044. err = build_aevent(r_skb, x, &c);
  2045. BUG_ON(err < 0);
  2046. err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
  2047. spin_unlock_bh(&x->lock);
  2048. xfrm_state_put(x);
  2049. return err;
  2050. }
  2051. static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  2052. struct nlattr **attrs, struct netlink_ext_ack *extack)
  2053. {
  2054. struct net *net = sock_net(skb->sk);
  2055. struct xfrm_state *x;
  2056. struct km_event c;
  2057. int err = -EINVAL;
  2058. u32 mark = 0;
  2059. struct xfrm_mark m;
  2060. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  2061. struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
  2062. struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
  2063. struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
  2064. struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
  2065. struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
  2066. if (!lt && !rp && !re && !et && !rt)
  2067. return err;
  2068. /* pedantic mode - thou shalt sayeth replaceth */
  2069. if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
  2070. return err;
  2071. mark = xfrm_mark_get(attrs, &m);
  2072. x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
  2073. if (x == NULL)
  2074. return -ESRCH;
  2075. if (x->km.state != XFRM_STATE_VALID)
  2076. goto out;
  2077. err = xfrm_replay_verify_len(x->replay_esn, re);
  2078. if (err)
  2079. goto out;
  2080. spin_lock_bh(&x->lock);
  2081. xfrm_update_ae_params(x, attrs, 1);
  2082. spin_unlock_bh(&x->lock);
  2083. c.event = nlh->nlmsg_type;
  2084. c.seq = nlh->nlmsg_seq;
  2085. c.portid = nlh->nlmsg_pid;
  2086. c.data.aevent = XFRM_AE_CU;
  2087. km_state_notify(x, &c);
  2088. err = 0;
  2089. out:
  2090. xfrm_state_put(x);
  2091. return err;
  2092. }
  2093. static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  2094. struct nlattr **attrs,
  2095. struct netlink_ext_ack *extack)
  2096. {
  2097. struct net *net = sock_net(skb->sk);
  2098. struct km_event c;
  2099. u8 type = XFRM_POLICY_TYPE_MAIN;
  2100. int err;
  2101. err = copy_from_user_policy_type(&type, attrs, extack);
  2102. if (err)
  2103. return err;
  2104. err = xfrm_policy_flush(net, type, true);
  2105. if (err) {
  2106. if (err == -ESRCH) /* empty table */
  2107. return 0;
  2108. return err;
  2109. }
  2110. c.data.type = type;
  2111. c.event = nlh->nlmsg_type;
  2112. c.seq = nlh->nlmsg_seq;
  2113. c.portid = nlh->nlmsg_pid;
  2114. c.net = net;
  2115. km_policy_notify(NULL, 0, &c);
  2116. return 0;
  2117. }
  2118. static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  2119. struct nlattr **attrs,
  2120. struct netlink_ext_ack *extack)
  2121. {
  2122. struct net *net = sock_net(skb->sk);
  2123. struct xfrm_policy *xp;
  2124. struct xfrm_user_polexpire *up = nlmsg_data(nlh);
  2125. struct xfrm_userpolicy_info *p = &up->pol;
  2126. u8 type = XFRM_POLICY_TYPE_MAIN;
  2127. int err = -ENOENT;
  2128. struct xfrm_mark m;
  2129. u32 if_id = 0;
  2130. err = copy_from_user_policy_type(&type, attrs, extack);
  2131. if (err)
  2132. return err;
  2133. err = verify_policy_dir(p->dir, extack);
  2134. if (err)
  2135. return err;
  2136. if (attrs[XFRMA_IF_ID])
  2137. if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
  2138. xfrm_mark_get(attrs, &m);
  2139. if (p->index)
  2140. xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
  2141. 0, &err);
  2142. else {
  2143. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  2144. struct xfrm_sec_ctx *ctx;
  2145. err = verify_sec_ctx_len(attrs, extack);
  2146. if (err)
  2147. return err;
  2148. ctx = NULL;
  2149. if (rt) {
  2150. struct xfrm_user_sec_ctx *uctx = nla_data(rt);
  2151. err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
  2152. if (err)
  2153. return err;
  2154. }
  2155. xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
  2156. &p->sel, ctx, 0, &err);
  2157. security_xfrm_policy_free(ctx);
  2158. }
  2159. if (xp == NULL)
  2160. return -ENOENT;
  2161. if (unlikely(xp->walk.dead))
  2162. goto out;
  2163. err = 0;
  2164. if (up->hard) {
  2165. xfrm_policy_delete(xp, p->dir);
  2166. xfrm_audit_policy_delete(xp, 1, true);
  2167. }
  2168. km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
  2169. out:
  2170. xfrm_pol_put(xp);
  2171. return err;
  2172. }
  2173. static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  2174. struct nlattr **attrs,
  2175. struct netlink_ext_ack *extack)
  2176. {
  2177. struct net *net = sock_net(skb->sk);
  2178. struct xfrm_state *x;
  2179. int err;
  2180. struct xfrm_user_expire *ue = nlmsg_data(nlh);
  2181. struct xfrm_usersa_info *p = &ue->state;
  2182. struct xfrm_mark m;
  2183. u32 mark = xfrm_mark_get(attrs, &m);
  2184. x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
  2185. err = -ENOENT;
  2186. if (x == NULL)
  2187. return err;
  2188. spin_lock_bh(&x->lock);
  2189. err = -EINVAL;
  2190. if (x->km.state != XFRM_STATE_VALID)
  2191. goto out;
  2192. km_state_expired(x, ue->hard, nlh->nlmsg_pid);
  2193. if (ue->hard) {
  2194. __xfrm_state_delete(x);
  2195. xfrm_audit_state_delete(x, 1, true);
  2196. }
  2197. err = 0;
  2198. out:
  2199. spin_unlock_bh(&x->lock);
  2200. xfrm_state_put(x);
  2201. return err;
  2202. }
  2203. static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
  2204. struct nlattr **attrs,
  2205. struct netlink_ext_ack *extack)
  2206. {
  2207. struct net *net = sock_net(skb->sk);
  2208. struct xfrm_policy *xp;
  2209. struct xfrm_user_tmpl *ut;
  2210. int i;
  2211. struct nlattr *rt = attrs[XFRMA_TMPL];
  2212. struct xfrm_mark mark;
  2213. struct xfrm_user_acquire *ua = nlmsg_data(nlh);
  2214. struct xfrm_state *x = xfrm_state_alloc(net);
  2215. int err = -ENOMEM;
  2216. if (!x)
  2217. goto nomem;
  2218. xfrm_mark_get(attrs, &mark);
  2219. err = verify_newpolicy_info(&ua->policy, extack);
  2220. if (err)
  2221. goto free_state;
  2222. err = verify_sec_ctx_len(attrs, extack);
  2223. if (err)
  2224. goto free_state;
  2225. /* build an XP */
  2226. xp = xfrm_policy_construct(net, &ua->policy, attrs, &err, extack);
  2227. if (!xp)
  2228. goto free_state;
  2229. memcpy(&x->id, &ua->id, sizeof(ua->id));
  2230. memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
  2231. memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
  2232. xp->mark.m = x->mark.m = mark.m;
  2233. xp->mark.v = x->mark.v = mark.v;
  2234. ut = nla_data(rt);
  2235. /* extract the templates and for each call km_key */
  2236. for (i = 0; i < xp->xfrm_nr; i++, ut++) {
  2237. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  2238. memcpy(&x->id, &t->id, sizeof(x->id));
  2239. x->props.mode = t->mode;
  2240. x->props.reqid = t->reqid;
  2241. x->props.family = ut->family;
  2242. t->aalgos = ua->aalgos;
  2243. t->ealgos = ua->ealgos;
  2244. t->calgos = ua->calgos;
  2245. err = km_query(x, t, xp);
  2246. }
  2247. xfrm_state_free(x);
  2248. kfree(xp);
  2249. return 0;
  2250. free_state:
  2251. xfrm_state_free(x);
  2252. nomem:
  2253. return err;
  2254. }
  2255. #ifdef CONFIG_XFRM_MIGRATE
  2256. static int copy_from_user_migrate(struct xfrm_migrate *ma,
  2257. struct xfrm_kmaddress *k,
  2258. struct nlattr **attrs, int *num)
  2259. {
  2260. struct nlattr *rt = attrs[XFRMA_MIGRATE];
  2261. struct xfrm_user_migrate *um;
  2262. int i, num_migrate;
  2263. if (k != NULL) {
  2264. struct xfrm_user_kmaddress *uk;
  2265. uk = nla_data(attrs[XFRMA_KMADDRESS]);
  2266. memcpy(&k->local, &uk->local, sizeof(k->local));
  2267. memcpy(&k->remote, &uk->remote, sizeof(k->remote));
  2268. k->family = uk->family;
  2269. k->reserved = uk->reserved;
  2270. }
  2271. um = nla_data(rt);
  2272. num_migrate = nla_len(rt) / sizeof(*um);
  2273. if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
  2274. return -EINVAL;
  2275. for (i = 0; i < num_migrate; i++, um++, ma++) {
  2276. memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
  2277. memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
  2278. memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
  2279. memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
  2280. ma->proto = um->proto;
  2281. ma->mode = um->mode;
  2282. ma->reqid = um->reqid;
  2283. ma->old_family = um->old_family;
  2284. ma->new_family = um->new_family;
  2285. }
  2286. *num = i;
  2287. return 0;
  2288. }
  2289. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  2290. struct nlattr **attrs, struct netlink_ext_ack *extack)
  2291. {
  2292. struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
  2293. struct xfrm_migrate m[XFRM_MAX_DEPTH];
  2294. struct xfrm_kmaddress km, *kmp;
  2295. u8 type;
  2296. int err;
  2297. int n = 0;
  2298. struct net *net = sock_net(skb->sk);
  2299. struct xfrm_encap_tmpl *encap = NULL;
  2300. u32 if_id = 0;
  2301. if (attrs[XFRMA_MIGRATE] == NULL)
  2302. return -EINVAL;
  2303. kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
  2304. err = copy_from_user_policy_type(&type, attrs, extack);
  2305. if (err)
  2306. return err;
  2307. err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
  2308. if (err)
  2309. return err;
  2310. if (!n)
  2311. return 0;
  2312. if (attrs[XFRMA_ENCAP]) {
  2313. encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
  2314. sizeof(*encap), GFP_KERNEL);
  2315. if (!encap)
  2316. return -ENOMEM;
  2317. }
  2318. if (attrs[XFRMA_IF_ID])
  2319. if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
  2320. err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap, if_id);
  2321. kfree(encap);
  2322. return err;
  2323. }
  2324. #else
  2325. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  2326. struct nlattr **attrs, struct netlink_ext_ack *extack)
  2327. {
  2328. return -ENOPROTOOPT;
  2329. }
  2330. #endif
  2331. #ifdef CONFIG_XFRM_MIGRATE
  2332. static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
  2333. {
  2334. struct xfrm_user_migrate um;
  2335. memset(&um, 0, sizeof(um));
  2336. um.proto = m->proto;
  2337. um.mode = m->mode;
  2338. um.reqid = m->reqid;
  2339. um.old_family = m->old_family;
  2340. memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
  2341. memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
  2342. um.new_family = m->new_family;
  2343. memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
  2344. memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
  2345. return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
  2346. }
  2347. static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
  2348. {
  2349. struct xfrm_user_kmaddress uk;
  2350. memset(&uk, 0, sizeof(uk));
  2351. uk.family = k->family;
  2352. uk.reserved = k->reserved;
  2353. memcpy(&uk.local, &k->local, sizeof(uk.local));
  2354. memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
  2355. return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
  2356. }
  2357. static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
  2358. int with_encp)
  2359. {
  2360. return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
  2361. + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
  2362. + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
  2363. + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
  2364. + userpolicy_type_attrsize();
  2365. }
  2366. static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
  2367. int num_migrate, const struct xfrm_kmaddress *k,
  2368. const struct xfrm_selector *sel,
  2369. const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
  2370. {
  2371. const struct xfrm_migrate *mp;
  2372. struct xfrm_userpolicy_id *pol_id;
  2373. struct nlmsghdr *nlh;
  2374. int i, err;
  2375. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
  2376. if (nlh == NULL)
  2377. return -EMSGSIZE;
  2378. pol_id = nlmsg_data(nlh);
  2379. /* copy data from selector, dir, and type to the pol_id */
  2380. memset(pol_id, 0, sizeof(*pol_id));
  2381. memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
  2382. pol_id->dir = dir;
  2383. if (k != NULL) {
  2384. err = copy_to_user_kmaddress(k, skb);
  2385. if (err)
  2386. goto out_cancel;
  2387. }
  2388. if (encap) {
  2389. err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
  2390. if (err)
  2391. goto out_cancel;
  2392. }
  2393. err = copy_to_user_policy_type(type, skb);
  2394. if (err)
  2395. goto out_cancel;
  2396. for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
  2397. err = copy_to_user_migrate(mp, skb);
  2398. if (err)
  2399. goto out_cancel;
  2400. }
  2401. nlmsg_end(skb, nlh);
  2402. return 0;
  2403. out_cancel:
  2404. nlmsg_cancel(skb, nlh);
  2405. return err;
  2406. }
  2407. static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  2408. const struct xfrm_migrate *m, int num_migrate,
  2409. const struct xfrm_kmaddress *k,
  2410. const struct xfrm_encap_tmpl *encap)
  2411. {
  2412. struct net *net = &init_net;
  2413. struct sk_buff *skb;
  2414. int err;
  2415. skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
  2416. GFP_ATOMIC);
  2417. if (skb == NULL)
  2418. return -ENOMEM;
  2419. /* build migrate */
  2420. err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
  2421. BUG_ON(err < 0);
  2422. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
  2423. }
  2424. #else
  2425. static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  2426. const struct xfrm_migrate *m, int num_migrate,
  2427. const struct xfrm_kmaddress *k,
  2428. const struct xfrm_encap_tmpl *encap)
  2429. {
  2430. return -ENOPROTOOPT;
  2431. }
  2432. #endif
  2433. #define XMSGSIZE(type) sizeof(struct type)
  2434. const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
  2435. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  2436. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  2437. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  2438. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  2439. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  2440. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  2441. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
  2442. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
  2443. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
  2444. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  2445. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  2446. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
  2447. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  2448. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
  2449. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  2450. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  2451. [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
  2452. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  2453. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
  2454. [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
  2455. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
  2456. [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
  2457. [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
  2458. };
  2459. EXPORT_SYMBOL_GPL(xfrm_msg_min);
  2460. #undef XMSGSIZE
  2461. const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
  2462. [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
  2463. [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
  2464. [XFRMA_LASTUSED] = { .type = NLA_U64},
  2465. [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
  2466. [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
  2467. [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
  2468. [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
  2469. [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
  2470. [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
  2471. [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
  2472. [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_user_sec_ctx) },
  2473. [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
  2474. [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
  2475. [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
  2476. [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
  2477. [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
  2478. [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
  2479. [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
  2480. [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
  2481. [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
  2482. [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
  2483. [XFRMA_TFCPAD] = { .type = NLA_U32 },
  2484. [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
  2485. [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
  2486. [XFRMA_PROTO] = { .type = NLA_U8 },
  2487. [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
  2488. [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
  2489. [XFRMA_SET_MARK] = { .type = NLA_U32 },
  2490. [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
  2491. [XFRMA_IF_ID] = { .type = NLA_U32 },
  2492. [XFRMA_MTIMER_THRESH] = { .type = NLA_U32 },
  2493. };
  2494. EXPORT_SYMBOL_GPL(xfrma_policy);
  2495. static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
  2496. [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
  2497. [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
  2498. };
  2499. static const struct xfrm_link {
  2500. int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **,
  2501. struct netlink_ext_ack *);
  2502. int (*start)(struct netlink_callback *);
  2503. int (*dump)(struct sk_buff *, struct netlink_callback *);
  2504. int (*done)(struct netlink_callback *);
  2505. const struct nla_policy *nla_pol;
  2506. int nla_max;
  2507. } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
  2508. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  2509. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
  2510. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
  2511. .dump = xfrm_dump_sa,
  2512. .done = xfrm_dump_sa_done },
  2513. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  2514. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
  2515. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
  2516. .start = xfrm_dump_policy_start,
  2517. .dump = xfrm_dump_policy,
  2518. .done = xfrm_dump_policy_done },
  2519. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
  2520. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
  2521. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
  2522. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  2523. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  2524. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
  2525. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
  2526. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
  2527. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
  2528. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
  2529. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
  2530. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
  2531. [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
  2532. .nla_pol = xfrma_spd_policy,
  2533. .nla_max = XFRMA_SPD_MAX },
  2534. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
  2535. [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_set_default },
  2536. [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_get_default },
  2537. };
  2538. static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
  2539. struct netlink_ext_ack *extack)
  2540. {
  2541. struct net *net = sock_net(skb->sk);
  2542. struct nlattr *attrs[XFRMA_MAX+1];
  2543. const struct xfrm_link *link;
  2544. struct nlmsghdr *nlh64 = NULL;
  2545. int type, err;
  2546. type = nlh->nlmsg_type;
  2547. if (type > XFRM_MSG_MAX)
  2548. return -EINVAL;
  2549. type -= XFRM_MSG_BASE;
  2550. link = &xfrm_dispatch[type];
  2551. /* All operations require privileges, even GET */
  2552. if (!netlink_net_capable(skb, CAP_NET_ADMIN))
  2553. return -EPERM;
  2554. /* Use the 64-bit / untranslated format on Android, even for compat */
  2555. if (!IS_ENABLED(CONFIG_GKI_NET_XFRM_HACKS) || IS_ENABLED(CONFIG_XFRM_USER_COMPAT)) {
  2556. if (in_compat_syscall()) {
  2557. struct xfrm_translator *xtr = xfrm_get_translator();
  2558. if (!xtr)
  2559. return -EOPNOTSUPP;
  2560. nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
  2561. link->nla_pol, extack);
  2562. xfrm_put_translator(xtr);
  2563. if (IS_ERR(nlh64))
  2564. return PTR_ERR(nlh64);
  2565. if (nlh64)
  2566. nlh = nlh64;
  2567. }
  2568. }
  2569. if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
  2570. type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
  2571. (nlh->nlmsg_flags & NLM_F_DUMP)) {
  2572. struct netlink_dump_control c = {
  2573. .start = link->start,
  2574. .dump = link->dump,
  2575. .done = link->done,
  2576. };
  2577. if (link->dump == NULL) {
  2578. err = -EINVAL;
  2579. goto err;
  2580. }
  2581. err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
  2582. goto err;
  2583. }
  2584. err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
  2585. link->nla_max ? : XFRMA_MAX,
  2586. link->nla_pol ? : xfrma_policy, extack);
  2587. if (err < 0)
  2588. goto err;
  2589. if (link->doit == NULL) {
  2590. err = -EINVAL;
  2591. goto err;
  2592. }
  2593. err = link->doit(skb, nlh, attrs, extack);
  2594. /* We need to free skb allocated in xfrm_alloc_compat() before
  2595. * returning from this function, because consume_skb() won't take
  2596. * care of frag_list since netlink destructor sets
  2597. * sbk->head to NULL. (see netlink_skb_destructor())
  2598. */
  2599. if (skb_has_frag_list(skb)) {
  2600. kfree_skb(skb_shinfo(skb)->frag_list);
  2601. skb_shinfo(skb)->frag_list = NULL;
  2602. }
  2603. err:
  2604. kvfree(nlh64);
  2605. return err;
  2606. }
  2607. static void xfrm_netlink_rcv(struct sk_buff *skb)
  2608. {
  2609. struct net *net = sock_net(skb->sk);
  2610. mutex_lock(&net->xfrm.xfrm_cfg_mutex);
  2611. netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
  2612. mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
  2613. }
  2614. static inline unsigned int xfrm_expire_msgsize(void)
  2615. {
  2616. return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
  2617. + nla_total_size(sizeof(struct xfrm_mark));
  2618. }
  2619. static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
  2620. {
  2621. struct xfrm_user_expire *ue;
  2622. struct nlmsghdr *nlh;
  2623. int err;
  2624. nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
  2625. if (nlh == NULL)
  2626. return -EMSGSIZE;
  2627. ue = nlmsg_data(nlh);
  2628. copy_to_user_state(x, &ue->state);
  2629. ue->hard = (c->data.hard != 0) ? 1 : 0;
  2630. /* clear the padding bytes */
  2631. memset_after(ue, 0, hard);
  2632. err = xfrm_mark_put(skb, &x->mark);
  2633. if (err)
  2634. return err;
  2635. err = xfrm_if_id_put(skb, x->if_id);
  2636. if (err)
  2637. return err;
  2638. nlmsg_end(skb, nlh);
  2639. return 0;
  2640. }
  2641. static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
  2642. {
  2643. struct net *net = xs_net(x);
  2644. struct sk_buff *skb;
  2645. skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
  2646. if (skb == NULL)
  2647. return -ENOMEM;
  2648. if (build_expire(skb, x, c) < 0) {
  2649. kfree_skb(skb);
  2650. return -EMSGSIZE;
  2651. }
  2652. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
  2653. }
  2654. static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
  2655. {
  2656. struct net *net = xs_net(x);
  2657. struct sk_buff *skb;
  2658. int err;
  2659. skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
  2660. if (skb == NULL)
  2661. return -ENOMEM;
  2662. err = build_aevent(skb, x, c);
  2663. BUG_ON(err < 0);
  2664. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
  2665. }
  2666. static int xfrm_notify_sa_flush(const struct km_event *c)
  2667. {
  2668. struct net *net = c->net;
  2669. struct xfrm_usersa_flush *p;
  2670. struct nlmsghdr *nlh;
  2671. struct sk_buff *skb;
  2672. int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
  2673. skb = nlmsg_new(len, GFP_ATOMIC);
  2674. if (skb == NULL)
  2675. return -ENOMEM;
  2676. nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
  2677. if (nlh == NULL) {
  2678. kfree_skb(skb);
  2679. return -EMSGSIZE;
  2680. }
  2681. p = nlmsg_data(nlh);
  2682. p->proto = c->data.proto;
  2683. nlmsg_end(skb, nlh);
  2684. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
  2685. }
  2686. static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
  2687. {
  2688. unsigned int l = 0;
  2689. if (x->aead)
  2690. l += nla_total_size(aead_len(x->aead));
  2691. if (x->aalg) {
  2692. l += nla_total_size(sizeof(struct xfrm_algo) +
  2693. (x->aalg->alg_key_len + 7) / 8);
  2694. l += nla_total_size(xfrm_alg_auth_len(x->aalg));
  2695. }
  2696. if (x->ealg)
  2697. l += nla_total_size(xfrm_alg_len(x->ealg));
  2698. if (x->calg)
  2699. l += nla_total_size(sizeof(*x->calg));
  2700. if (x->encap)
  2701. l += nla_total_size(sizeof(*x->encap));
  2702. if (x->tfcpad)
  2703. l += nla_total_size(sizeof(x->tfcpad));
  2704. if (x->replay_esn)
  2705. l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
  2706. else
  2707. l += nla_total_size(sizeof(struct xfrm_replay_state));
  2708. if (x->security)
  2709. l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
  2710. x->security->ctx_len);
  2711. if (x->coaddr)
  2712. l += nla_total_size(sizeof(*x->coaddr));
  2713. if (x->props.extra_flags)
  2714. l += nla_total_size(sizeof(x->props.extra_flags));
  2715. if (x->xso.dev)
  2716. l += nla_total_size(sizeof(struct xfrm_user_offload));
  2717. if (x->props.smark.v | x->props.smark.m) {
  2718. l += nla_total_size(sizeof(x->props.smark.v));
  2719. l += nla_total_size(sizeof(x->props.smark.m));
  2720. }
  2721. if (x->if_id)
  2722. l += nla_total_size(sizeof(x->if_id));
  2723. /* Must count x->lastused as it may become non-zero behind our back. */
  2724. l += nla_total_size_64bit(sizeof(u64));
  2725. if (x->mapping_maxage)
  2726. l += nla_total_size(sizeof(x->mapping_maxage));
  2727. return l;
  2728. }
  2729. static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
  2730. {
  2731. struct net *net = xs_net(x);
  2732. struct xfrm_usersa_info *p;
  2733. struct xfrm_usersa_id *id;
  2734. struct nlmsghdr *nlh;
  2735. struct sk_buff *skb;
  2736. unsigned int len = xfrm_sa_len(x);
  2737. unsigned int headlen;
  2738. int err;
  2739. headlen = sizeof(*p);
  2740. if (c->event == XFRM_MSG_DELSA) {
  2741. len += nla_total_size(headlen);
  2742. headlen = sizeof(*id);
  2743. len += nla_total_size(sizeof(struct xfrm_mark));
  2744. }
  2745. len += NLMSG_ALIGN(headlen);
  2746. skb = nlmsg_new(len, GFP_ATOMIC);
  2747. if (skb == NULL)
  2748. return -ENOMEM;
  2749. nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
  2750. err = -EMSGSIZE;
  2751. if (nlh == NULL)
  2752. goto out_free_skb;
  2753. p = nlmsg_data(nlh);
  2754. if (c->event == XFRM_MSG_DELSA) {
  2755. struct nlattr *attr;
  2756. id = nlmsg_data(nlh);
  2757. memset(id, 0, sizeof(*id));
  2758. memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
  2759. id->spi = x->id.spi;
  2760. id->family = x->props.family;
  2761. id->proto = x->id.proto;
  2762. attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
  2763. err = -EMSGSIZE;
  2764. if (attr == NULL)
  2765. goto out_free_skb;
  2766. p = nla_data(attr);
  2767. }
  2768. err = copy_to_user_state_extra(x, p, skb);
  2769. if (err)
  2770. goto out_free_skb;
  2771. nlmsg_end(skb, nlh);
  2772. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
  2773. out_free_skb:
  2774. kfree_skb(skb);
  2775. return err;
  2776. }
  2777. static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
  2778. {
  2779. switch (c->event) {
  2780. case XFRM_MSG_EXPIRE:
  2781. return xfrm_exp_state_notify(x, c);
  2782. case XFRM_MSG_NEWAE:
  2783. return xfrm_aevent_state_notify(x, c);
  2784. case XFRM_MSG_DELSA:
  2785. case XFRM_MSG_UPDSA:
  2786. case XFRM_MSG_NEWSA:
  2787. return xfrm_notify_sa(x, c);
  2788. case XFRM_MSG_FLUSHSA:
  2789. return xfrm_notify_sa_flush(c);
  2790. default:
  2791. printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
  2792. c->event);
  2793. break;
  2794. }
  2795. return 0;
  2796. }
  2797. static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
  2798. struct xfrm_policy *xp)
  2799. {
  2800. return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
  2801. + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
  2802. + nla_total_size(sizeof(struct xfrm_mark))
  2803. + nla_total_size(xfrm_user_sec_ctx_size(x->security))
  2804. + userpolicy_type_attrsize();
  2805. }
  2806. static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
  2807. struct xfrm_tmpl *xt, struct xfrm_policy *xp)
  2808. {
  2809. __u32 seq = xfrm_get_acqseq();
  2810. struct xfrm_user_acquire *ua;
  2811. struct nlmsghdr *nlh;
  2812. int err;
  2813. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
  2814. if (nlh == NULL)
  2815. return -EMSGSIZE;
  2816. ua = nlmsg_data(nlh);
  2817. memcpy(&ua->id, &x->id, sizeof(ua->id));
  2818. memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
  2819. memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
  2820. copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
  2821. ua->aalgos = xt->aalgos;
  2822. ua->ealgos = xt->ealgos;
  2823. ua->calgos = xt->calgos;
  2824. ua->seq = x->km.seq = seq;
  2825. err = copy_to_user_tmpl(xp, skb);
  2826. if (!err)
  2827. err = copy_to_user_state_sec_ctx(x, skb);
  2828. if (!err)
  2829. err = copy_to_user_policy_type(xp->type, skb);
  2830. if (!err)
  2831. err = xfrm_mark_put(skb, &xp->mark);
  2832. if (!err)
  2833. err = xfrm_if_id_put(skb, xp->if_id);
  2834. if (err) {
  2835. nlmsg_cancel(skb, nlh);
  2836. return err;
  2837. }
  2838. nlmsg_end(skb, nlh);
  2839. return 0;
  2840. }
  2841. static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
  2842. struct xfrm_policy *xp)
  2843. {
  2844. struct net *net = xs_net(x);
  2845. struct sk_buff *skb;
  2846. int err;
  2847. skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
  2848. if (skb == NULL)
  2849. return -ENOMEM;
  2850. err = build_acquire(skb, x, xt, xp);
  2851. BUG_ON(err < 0);
  2852. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
  2853. }
  2854. /* User gives us xfrm_user_policy_info followed by an array of 0
  2855. * or more templates.
  2856. */
  2857. static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
  2858. u8 *data, int len, int *dir)
  2859. {
  2860. struct net *net = sock_net(sk);
  2861. struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
  2862. struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
  2863. struct xfrm_policy *xp;
  2864. int nr;
  2865. switch (sk->sk_family) {
  2866. case AF_INET:
  2867. if (opt != IP_XFRM_POLICY) {
  2868. *dir = -EOPNOTSUPP;
  2869. return NULL;
  2870. }
  2871. break;
  2872. #if IS_ENABLED(CONFIG_IPV6)
  2873. case AF_INET6:
  2874. if (opt != IPV6_XFRM_POLICY) {
  2875. *dir = -EOPNOTSUPP;
  2876. return NULL;
  2877. }
  2878. break;
  2879. #endif
  2880. default:
  2881. *dir = -EINVAL;
  2882. return NULL;
  2883. }
  2884. *dir = -EINVAL;
  2885. if (len < sizeof(*p) ||
  2886. verify_newpolicy_info(p, NULL))
  2887. return NULL;
  2888. nr = ((len - sizeof(*p)) / sizeof(*ut));
  2889. if (validate_tmpl(nr, ut, p->sel.family, p->dir, NULL))
  2890. return NULL;
  2891. if (p->dir > XFRM_POLICY_OUT)
  2892. return NULL;
  2893. xp = xfrm_policy_alloc(net, GFP_ATOMIC);
  2894. if (xp == NULL) {
  2895. *dir = -ENOBUFS;
  2896. return NULL;
  2897. }
  2898. copy_from_user_policy(xp, p);
  2899. xp->type = XFRM_POLICY_TYPE_MAIN;
  2900. copy_templates(xp, ut, nr);
  2901. *dir = p->dir;
  2902. return xp;
  2903. }
  2904. static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
  2905. {
  2906. return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
  2907. + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
  2908. + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
  2909. + nla_total_size(sizeof(struct xfrm_mark))
  2910. + userpolicy_type_attrsize();
  2911. }
  2912. static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
  2913. int dir, const struct km_event *c)
  2914. {
  2915. struct xfrm_user_polexpire *upe;
  2916. int hard = c->data.hard;
  2917. struct nlmsghdr *nlh;
  2918. int err;
  2919. nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
  2920. if (nlh == NULL)
  2921. return -EMSGSIZE;
  2922. upe = nlmsg_data(nlh);
  2923. copy_to_user_policy(xp, &upe->pol, dir);
  2924. err = copy_to_user_tmpl(xp, skb);
  2925. if (!err)
  2926. err = copy_to_user_sec_ctx(xp, skb);
  2927. if (!err)
  2928. err = copy_to_user_policy_type(xp->type, skb);
  2929. if (!err)
  2930. err = xfrm_mark_put(skb, &xp->mark);
  2931. if (!err)
  2932. err = xfrm_if_id_put(skb, xp->if_id);
  2933. if (err) {
  2934. nlmsg_cancel(skb, nlh);
  2935. return err;
  2936. }
  2937. upe->hard = !!hard;
  2938. nlmsg_end(skb, nlh);
  2939. return 0;
  2940. }
  2941. static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
  2942. {
  2943. struct net *net = xp_net(xp);
  2944. struct sk_buff *skb;
  2945. int err;
  2946. skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
  2947. if (skb == NULL)
  2948. return -ENOMEM;
  2949. err = build_polexpire(skb, xp, dir, c);
  2950. BUG_ON(err < 0);
  2951. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
  2952. }
  2953. static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
  2954. {
  2955. unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  2956. struct net *net = xp_net(xp);
  2957. struct xfrm_userpolicy_info *p;
  2958. struct xfrm_userpolicy_id *id;
  2959. struct nlmsghdr *nlh;
  2960. struct sk_buff *skb;
  2961. unsigned int headlen;
  2962. int err;
  2963. headlen = sizeof(*p);
  2964. if (c->event == XFRM_MSG_DELPOLICY) {
  2965. len += nla_total_size(headlen);
  2966. headlen = sizeof(*id);
  2967. }
  2968. len += userpolicy_type_attrsize();
  2969. len += nla_total_size(sizeof(struct xfrm_mark));
  2970. len += NLMSG_ALIGN(headlen);
  2971. skb = nlmsg_new(len, GFP_ATOMIC);
  2972. if (skb == NULL)
  2973. return -ENOMEM;
  2974. nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
  2975. err = -EMSGSIZE;
  2976. if (nlh == NULL)
  2977. goto out_free_skb;
  2978. p = nlmsg_data(nlh);
  2979. if (c->event == XFRM_MSG_DELPOLICY) {
  2980. struct nlattr *attr;
  2981. id = nlmsg_data(nlh);
  2982. memset(id, 0, sizeof(*id));
  2983. id->dir = dir;
  2984. if (c->data.byid)
  2985. id->index = xp->index;
  2986. else
  2987. memcpy(&id->sel, &xp->selector, sizeof(id->sel));
  2988. attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
  2989. err = -EMSGSIZE;
  2990. if (attr == NULL)
  2991. goto out_free_skb;
  2992. p = nla_data(attr);
  2993. }
  2994. copy_to_user_policy(xp, p, dir);
  2995. err = copy_to_user_tmpl(xp, skb);
  2996. if (!err)
  2997. err = copy_to_user_policy_type(xp->type, skb);
  2998. if (!err)
  2999. err = xfrm_mark_put(skb, &xp->mark);
  3000. if (!err)
  3001. err = xfrm_if_id_put(skb, xp->if_id);
  3002. if (err)
  3003. goto out_free_skb;
  3004. nlmsg_end(skb, nlh);
  3005. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
  3006. out_free_skb:
  3007. kfree_skb(skb);
  3008. return err;
  3009. }
  3010. static int xfrm_notify_policy_flush(const struct km_event *c)
  3011. {
  3012. struct net *net = c->net;
  3013. struct nlmsghdr *nlh;
  3014. struct sk_buff *skb;
  3015. int err;
  3016. skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
  3017. if (skb == NULL)
  3018. return -ENOMEM;
  3019. nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
  3020. err = -EMSGSIZE;
  3021. if (nlh == NULL)
  3022. goto out_free_skb;
  3023. err = copy_to_user_policy_type(c->data.type, skb);
  3024. if (err)
  3025. goto out_free_skb;
  3026. nlmsg_end(skb, nlh);
  3027. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
  3028. out_free_skb:
  3029. kfree_skb(skb);
  3030. return err;
  3031. }
  3032. static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
  3033. {
  3034. switch (c->event) {
  3035. case XFRM_MSG_NEWPOLICY:
  3036. case XFRM_MSG_UPDPOLICY:
  3037. case XFRM_MSG_DELPOLICY:
  3038. return xfrm_notify_policy(xp, dir, c);
  3039. case XFRM_MSG_FLUSHPOLICY:
  3040. return xfrm_notify_policy_flush(c);
  3041. case XFRM_MSG_POLEXPIRE:
  3042. return xfrm_exp_policy_notify(xp, dir, c);
  3043. default:
  3044. printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
  3045. c->event);
  3046. }
  3047. return 0;
  3048. }
  3049. static inline unsigned int xfrm_report_msgsize(void)
  3050. {
  3051. return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
  3052. }
  3053. static int build_report(struct sk_buff *skb, u8 proto,
  3054. struct xfrm_selector *sel, xfrm_address_t *addr)
  3055. {
  3056. struct xfrm_user_report *ur;
  3057. struct nlmsghdr *nlh;
  3058. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
  3059. if (nlh == NULL)
  3060. return -EMSGSIZE;
  3061. ur = nlmsg_data(nlh);
  3062. ur->proto = proto;
  3063. memcpy(&ur->sel, sel, sizeof(ur->sel));
  3064. if (addr) {
  3065. int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
  3066. if (err) {
  3067. nlmsg_cancel(skb, nlh);
  3068. return err;
  3069. }
  3070. }
  3071. nlmsg_end(skb, nlh);
  3072. return 0;
  3073. }
  3074. static int xfrm_send_report(struct net *net, u8 proto,
  3075. struct xfrm_selector *sel, xfrm_address_t *addr)
  3076. {
  3077. struct sk_buff *skb;
  3078. int err;
  3079. skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
  3080. if (skb == NULL)
  3081. return -ENOMEM;
  3082. err = build_report(skb, proto, sel, addr);
  3083. BUG_ON(err < 0);
  3084. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
  3085. }
  3086. static inline unsigned int xfrm_mapping_msgsize(void)
  3087. {
  3088. return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
  3089. }
  3090. static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
  3091. xfrm_address_t *new_saddr, __be16 new_sport)
  3092. {
  3093. struct xfrm_user_mapping *um;
  3094. struct nlmsghdr *nlh;
  3095. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
  3096. if (nlh == NULL)
  3097. return -EMSGSIZE;
  3098. um = nlmsg_data(nlh);
  3099. memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
  3100. um->id.spi = x->id.spi;
  3101. um->id.family = x->props.family;
  3102. um->id.proto = x->id.proto;
  3103. memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
  3104. memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
  3105. um->new_sport = new_sport;
  3106. um->old_sport = x->encap->encap_sport;
  3107. um->reqid = x->props.reqid;
  3108. nlmsg_end(skb, nlh);
  3109. return 0;
  3110. }
  3111. static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
  3112. __be16 sport)
  3113. {
  3114. struct net *net = xs_net(x);
  3115. struct sk_buff *skb;
  3116. int err;
  3117. if (x->id.proto != IPPROTO_ESP)
  3118. return -EINVAL;
  3119. if (!x->encap)
  3120. return -EINVAL;
  3121. skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
  3122. if (skb == NULL)
  3123. return -ENOMEM;
  3124. err = build_mapping(skb, x, ipaddr, sport);
  3125. BUG_ON(err < 0);
  3126. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
  3127. }
  3128. static bool xfrm_is_alive(const struct km_event *c)
  3129. {
  3130. return (bool)xfrm_acquire_is_on(c->net);
  3131. }
  3132. static struct xfrm_mgr netlink_mgr = {
  3133. .notify = xfrm_send_state_notify,
  3134. .acquire = xfrm_send_acquire,
  3135. .compile_policy = xfrm_compile_policy,
  3136. .notify_policy = xfrm_send_policy_notify,
  3137. .report = xfrm_send_report,
  3138. .migrate = xfrm_send_migrate,
  3139. .new_mapping = xfrm_send_mapping,
  3140. .is_alive = xfrm_is_alive,
  3141. };
  3142. static int __net_init xfrm_user_net_init(struct net *net)
  3143. {
  3144. struct sock *nlsk;
  3145. struct netlink_kernel_cfg cfg = {
  3146. .groups = XFRMNLGRP_MAX,
  3147. .input = xfrm_netlink_rcv,
  3148. };
  3149. nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
  3150. if (nlsk == NULL)
  3151. return -ENOMEM;
  3152. net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
  3153. rcu_assign_pointer(net->xfrm.nlsk, nlsk);
  3154. return 0;
  3155. }
  3156. static void __net_exit xfrm_user_net_pre_exit(struct net *net)
  3157. {
  3158. RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
  3159. }
  3160. static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
  3161. {
  3162. struct net *net;
  3163. list_for_each_entry(net, net_exit_list, exit_list)
  3164. netlink_kernel_release(net->xfrm.nlsk_stash);
  3165. }
  3166. static struct pernet_operations xfrm_user_net_ops = {
  3167. .init = xfrm_user_net_init,
  3168. .pre_exit = xfrm_user_net_pre_exit,
  3169. .exit_batch = xfrm_user_net_exit,
  3170. };
  3171. static int __init xfrm_user_init(void)
  3172. {
  3173. int rv;
  3174. printk(KERN_INFO "Initializing XFRM netlink socket\n");
  3175. rv = register_pernet_subsys(&xfrm_user_net_ops);
  3176. if (rv < 0)
  3177. return rv;
  3178. xfrm_register_km(&netlink_mgr);
  3179. return 0;
  3180. }
  3181. static void __exit xfrm_user_exit(void)
  3182. {
  3183. xfrm_unregister_km(&netlink_mgr);
  3184. unregister_pernet_subsys(&xfrm_user_net_ops);
  3185. }
  3186. module_init(xfrm_user_init);
  3187. module_exit(xfrm_user_exit);
  3188. MODULE_LICENSE("GPL");
  3189. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);