xfrm_user.c 66 KB

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