xfrm_user.c 66 KB

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