xfrm_user.c 67 KB

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