xfrm_user.c 69 KB

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