xfrm_user.c 77 KB

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