xfrm_user.c 65 KB

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