xfrm_user.c 69 KB

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