xfrm_user.c 59 KB

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