xfrm_user.c 76 KB

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