xfrm_user.c 75 KB

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