xfrm_user.c 73 KB

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