xfrm_user.c 74 KB

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