xfrm_user.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587
  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 xfrmk_spdinfo si;
  554. struct xfrmu_spdinfo spc;
  555. struct xfrmu_spdhinfo sph;
  556. struct nlmsghdr *nlh;
  557. u32 *f;
  558. nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
  559. if (nlh == NULL) /* shouldnt really happen ... */
  560. return -EMSGSIZE;
  561. f = nlmsg_data(nlh);
  562. *f = flags;
  563. xfrm_spd_getinfo(&si);
  564. spc.incnt = si.incnt;
  565. spc.outcnt = si.outcnt;
  566. spc.fwdcnt = si.fwdcnt;
  567. spc.inscnt = si.inscnt;
  568. spc.outscnt = si.outscnt;
  569. spc.fwdscnt = si.fwdscnt;
  570. sph.spdhcnt = si.spdhcnt;
  571. sph.spdhmcnt = si.spdhmcnt;
  572. NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
  573. NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
  574. return nlmsg_end(skb, nlh);
  575. nla_put_failure:
  576. nlmsg_cancel(skb, nlh);
  577. return -EMSGSIZE;
  578. }
  579. static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  580. struct rtattr **xfrma)
  581. {
  582. struct sk_buff *r_skb;
  583. u32 *flags = NLMSG_DATA(nlh);
  584. u32 spid = NETLINK_CB(skb).pid;
  585. u32 seq = nlh->nlmsg_seq;
  586. int len = NLMSG_LENGTH(sizeof(u32));
  587. len += RTA_SPACE(sizeof(struct xfrmu_spdinfo));
  588. len += RTA_SPACE(sizeof(struct xfrmu_spdhinfo));
  589. r_skb = alloc_skb(len, GFP_ATOMIC);
  590. if (r_skb == NULL)
  591. return -ENOMEM;
  592. if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
  593. BUG();
  594. return nlmsg_unicast(xfrm_nl, r_skb, spid);
  595. }
  596. static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
  597. {
  598. struct xfrmk_sadinfo si;
  599. struct xfrmu_sadhinfo sh;
  600. struct nlmsghdr *nlh;
  601. u32 *f;
  602. nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
  603. if (nlh == NULL) /* shouldnt really happen ... */
  604. return -EMSGSIZE;
  605. f = nlmsg_data(nlh);
  606. *f = flags;
  607. xfrm_sad_getinfo(&si);
  608. sh.sadhmcnt = si.sadhmcnt;
  609. sh.sadhcnt = si.sadhcnt;
  610. NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
  611. NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
  612. return nlmsg_end(skb, nlh);
  613. nla_put_failure:
  614. nlmsg_cancel(skb, nlh);
  615. return -EMSGSIZE;
  616. }
  617. static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  618. struct rtattr **xfrma)
  619. {
  620. struct sk_buff *r_skb;
  621. u32 *flags = NLMSG_DATA(nlh);
  622. u32 spid = NETLINK_CB(skb).pid;
  623. u32 seq = nlh->nlmsg_seq;
  624. int len = NLMSG_LENGTH(sizeof(u32));
  625. len += RTA_SPACE(sizeof(struct xfrmu_sadhinfo));
  626. len += RTA_SPACE(sizeof(u32));
  627. r_skb = alloc_skb(len, GFP_ATOMIC);
  628. if (r_skb == NULL)
  629. return -ENOMEM;
  630. if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
  631. BUG();
  632. return nlmsg_unicast(xfrm_nl, r_skb, spid);
  633. }
  634. static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  635. struct rtattr **xfrma)
  636. {
  637. struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
  638. struct xfrm_state *x;
  639. struct sk_buff *resp_skb;
  640. int err = -ESRCH;
  641. x = xfrm_user_state_lookup(p, xfrma, &err);
  642. if (x == NULL)
  643. goto out_noput;
  644. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  645. if (IS_ERR(resp_skb)) {
  646. err = PTR_ERR(resp_skb);
  647. } else {
  648. err = netlink_unicast(xfrm_nl, resp_skb,
  649. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  650. }
  651. xfrm_state_put(x);
  652. out_noput:
  653. return err;
  654. }
  655. static int verify_userspi_info(struct xfrm_userspi_info *p)
  656. {
  657. switch (p->info.id.proto) {
  658. case IPPROTO_AH:
  659. case IPPROTO_ESP:
  660. break;
  661. case IPPROTO_COMP:
  662. /* IPCOMP spi is 16-bits. */
  663. if (p->max >= 0x10000)
  664. return -EINVAL;
  665. break;
  666. default:
  667. return -EINVAL;
  668. }
  669. if (p->min > p->max)
  670. return -EINVAL;
  671. return 0;
  672. }
  673. static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
  674. struct rtattr **xfrma)
  675. {
  676. struct xfrm_state *x;
  677. struct xfrm_userspi_info *p;
  678. struct sk_buff *resp_skb;
  679. xfrm_address_t *daddr;
  680. int family;
  681. int err;
  682. p = NLMSG_DATA(nlh);
  683. err = verify_userspi_info(p);
  684. if (err)
  685. goto out_noput;
  686. family = p->info.family;
  687. daddr = &p->info.id.daddr;
  688. x = NULL;
  689. if (p->info.seq) {
  690. x = xfrm_find_acq_byseq(p->info.seq);
  691. if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
  692. xfrm_state_put(x);
  693. x = NULL;
  694. }
  695. }
  696. if (!x)
  697. x = xfrm_find_acq(p->info.mode, p->info.reqid,
  698. p->info.id.proto, daddr,
  699. &p->info.saddr, 1,
  700. family);
  701. err = -ENOENT;
  702. if (x == NULL)
  703. goto out_noput;
  704. resp_skb = ERR_PTR(-ENOENT);
  705. spin_lock_bh(&x->lock);
  706. if (x->km.state != XFRM_STATE_DEAD) {
  707. xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
  708. if (x->id.spi)
  709. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  710. }
  711. spin_unlock_bh(&x->lock);
  712. if (IS_ERR(resp_skb)) {
  713. err = PTR_ERR(resp_skb);
  714. goto out;
  715. }
  716. err = netlink_unicast(xfrm_nl, resp_skb,
  717. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  718. out:
  719. xfrm_state_put(x);
  720. out_noput:
  721. return err;
  722. }
  723. static int verify_policy_dir(u8 dir)
  724. {
  725. switch (dir) {
  726. case XFRM_POLICY_IN:
  727. case XFRM_POLICY_OUT:
  728. case XFRM_POLICY_FWD:
  729. break;
  730. default:
  731. return -EINVAL;
  732. }
  733. return 0;
  734. }
  735. static int verify_policy_type(u8 type)
  736. {
  737. switch (type) {
  738. case XFRM_POLICY_TYPE_MAIN:
  739. #ifdef CONFIG_XFRM_SUB_POLICY
  740. case XFRM_POLICY_TYPE_SUB:
  741. #endif
  742. break;
  743. default:
  744. return -EINVAL;
  745. }
  746. return 0;
  747. }
  748. static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
  749. {
  750. switch (p->share) {
  751. case XFRM_SHARE_ANY:
  752. case XFRM_SHARE_SESSION:
  753. case XFRM_SHARE_USER:
  754. case XFRM_SHARE_UNIQUE:
  755. break;
  756. default:
  757. return -EINVAL;
  758. }
  759. switch (p->action) {
  760. case XFRM_POLICY_ALLOW:
  761. case XFRM_POLICY_BLOCK:
  762. break;
  763. default:
  764. return -EINVAL;
  765. }
  766. switch (p->sel.family) {
  767. case AF_INET:
  768. break;
  769. case AF_INET6:
  770. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  771. break;
  772. #else
  773. return -EAFNOSUPPORT;
  774. #endif
  775. default:
  776. return -EINVAL;
  777. }
  778. return verify_policy_dir(p->dir);
  779. }
  780. static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
  781. {
  782. struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
  783. struct xfrm_user_sec_ctx *uctx;
  784. if (!rt)
  785. return 0;
  786. uctx = RTA_DATA(rt);
  787. return security_xfrm_policy_alloc(pol, uctx);
  788. }
  789. static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
  790. int nr)
  791. {
  792. int i;
  793. xp->xfrm_nr = nr;
  794. for (i = 0; i < nr; i++, ut++) {
  795. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  796. memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
  797. memcpy(&t->saddr, &ut->saddr,
  798. sizeof(xfrm_address_t));
  799. t->reqid = ut->reqid;
  800. t->mode = ut->mode;
  801. t->share = ut->share;
  802. t->optional = ut->optional;
  803. t->aalgos = ut->aalgos;
  804. t->ealgos = ut->ealgos;
  805. t->calgos = ut->calgos;
  806. t->encap_family = ut->family;
  807. }
  808. }
  809. static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
  810. {
  811. int i;
  812. if (nr > XFRM_MAX_DEPTH)
  813. return -EINVAL;
  814. for (i = 0; i < nr; i++) {
  815. /* We never validated the ut->family value, so many
  816. * applications simply leave it at zero. The check was
  817. * never made and ut->family was ignored because all
  818. * templates could be assumed to have the same family as
  819. * the policy itself. Now that we will have ipv4-in-ipv6
  820. * and ipv6-in-ipv4 tunnels, this is no longer true.
  821. */
  822. if (!ut[i].family)
  823. ut[i].family = family;
  824. switch (ut[i].family) {
  825. case AF_INET:
  826. break;
  827. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  828. case AF_INET6:
  829. break;
  830. #endif
  831. default:
  832. return -EINVAL;
  833. }
  834. }
  835. return 0;
  836. }
  837. static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
  838. {
  839. struct rtattr *rt = xfrma[XFRMA_TMPL-1];
  840. if (!rt) {
  841. pol->xfrm_nr = 0;
  842. } else {
  843. struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
  844. int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
  845. int err;
  846. err = validate_tmpl(nr, utmpl, pol->family);
  847. if (err)
  848. return err;
  849. copy_templates(pol, RTA_DATA(rt), nr);
  850. }
  851. return 0;
  852. }
  853. static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
  854. {
  855. struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
  856. struct xfrm_userpolicy_type *upt;
  857. u8 type = XFRM_POLICY_TYPE_MAIN;
  858. int err;
  859. if (rt) {
  860. if (rt->rta_len < sizeof(*upt))
  861. return -EINVAL;
  862. upt = RTA_DATA(rt);
  863. type = upt->type;
  864. }
  865. err = verify_policy_type(type);
  866. if (err)
  867. return err;
  868. *tp = type;
  869. return 0;
  870. }
  871. static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
  872. {
  873. xp->priority = p->priority;
  874. xp->index = p->index;
  875. memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
  876. memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
  877. xp->action = p->action;
  878. xp->flags = p->flags;
  879. xp->family = p->sel.family;
  880. /* XXX xp->share = p->share; */
  881. }
  882. static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
  883. {
  884. memcpy(&p->sel, &xp->selector, sizeof(p->sel));
  885. memcpy(&p->lft, &xp->lft, sizeof(p->lft));
  886. memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
  887. p->priority = xp->priority;
  888. p->index = xp->index;
  889. p->sel.family = xp->family;
  890. p->dir = dir;
  891. p->action = xp->action;
  892. p->flags = xp->flags;
  893. p->share = XFRM_SHARE_ANY; /* XXX xp->share */
  894. }
  895. static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
  896. {
  897. struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
  898. int err;
  899. if (!xp) {
  900. *errp = -ENOMEM;
  901. return NULL;
  902. }
  903. copy_from_user_policy(xp, p);
  904. err = copy_from_user_policy_type(&xp->type, xfrma);
  905. if (err)
  906. goto error;
  907. if (!(err = copy_from_user_tmpl(xp, xfrma)))
  908. err = copy_from_user_sec_ctx(xp, xfrma);
  909. if (err)
  910. goto error;
  911. return xp;
  912. error:
  913. *errp = err;
  914. kfree(xp);
  915. return NULL;
  916. }
  917. static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  918. struct rtattr **xfrma)
  919. {
  920. struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
  921. struct xfrm_policy *xp;
  922. struct km_event c;
  923. int err;
  924. int excl;
  925. err = verify_newpolicy_info(p);
  926. if (err)
  927. return err;
  928. err = verify_sec_ctx_len(xfrma);
  929. if (err)
  930. return err;
  931. xp = xfrm_policy_construct(p, xfrma, &err);
  932. if (!xp)
  933. return err;
  934. /* shouldnt excl be based on nlh flags??
  935. * Aha! this is anti-netlink really i.e more pfkey derived
  936. * in netlink excl is a flag and you wouldnt need
  937. * a type XFRM_MSG_UPDPOLICY - JHS */
  938. excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
  939. err = xfrm_policy_insert(p->dir, xp, excl);
  940. xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
  941. AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
  942. if (err) {
  943. security_xfrm_policy_free(xp);
  944. kfree(xp);
  945. return err;
  946. }
  947. c.event = nlh->nlmsg_type;
  948. c.seq = nlh->nlmsg_seq;
  949. c.pid = nlh->nlmsg_pid;
  950. km_policy_notify(xp, p->dir, &c);
  951. xfrm_pol_put(xp);
  952. return 0;
  953. }
  954. static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
  955. {
  956. struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
  957. int i;
  958. if (xp->xfrm_nr == 0)
  959. return 0;
  960. for (i = 0; i < xp->xfrm_nr; i++) {
  961. struct xfrm_user_tmpl *up = &vec[i];
  962. struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
  963. memcpy(&up->id, &kp->id, sizeof(up->id));
  964. up->family = kp->encap_family;
  965. memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
  966. up->reqid = kp->reqid;
  967. up->mode = kp->mode;
  968. up->share = kp->share;
  969. up->optional = kp->optional;
  970. up->aalgos = kp->aalgos;
  971. up->ealgos = kp->ealgos;
  972. up->calgos = kp->calgos;
  973. }
  974. RTA_PUT(skb, XFRMA_TMPL,
  975. (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
  976. vec);
  977. return 0;
  978. rtattr_failure:
  979. return -1;
  980. }
  981. static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
  982. {
  983. int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
  984. struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
  985. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  986. uctx->exttype = XFRMA_SEC_CTX;
  987. uctx->len = ctx_size;
  988. uctx->ctx_doi = s->ctx_doi;
  989. uctx->ctx_alg = s->ctx_alg;
  990. uctx->ctx_len = s->ctx_len;
  991. memcpy(uctx + 1, s->ctx_str, s->ctx_len);
  992. return 0;
  993. rtattr_failure:
  994. return -1;
  995. }
  996. static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
  997. {
  998. if (x->security) {
  999. return copy_sec_ctx(x->security, skb);
  1000. }
  1001. return 0;
  1002. }
  1003. static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
  1004. {
  1005. if (xp->security) {
  1006. return copy_sec_ctx(xp->security, skb);
  1007. }
  1008. return 0;
  1009. }
  1010. #ifdef CONFIG_XFRM_SUB_POLICY
  1011. static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1012. {
  1013. struct xfrm_userpolicy_type upt;
  1014. memset(&upt, 0, sizeof(upt));
  1015. upt.type = type;
  1016. RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
  1017. return 0;
  1018. rtattr_failure:
  1019. return -1;
  1020. }
  1021. #else
  1022. static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1023. {
  1024. return 0;
  1025. }
  1026. #endif
  1027. static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
  1028. {
  1029. struct xfrm_dump_info *sp = ptr;
  1030. struct xfrm_userpolicy_info *p;
  1031. struct sk_buff *in_skb = sp->in_skb;
  1032. struct sk_buff *skb = sp->out_skb;
  1033. struct nlmsghdr *nlh;
  1034. unsigned char *b = skb_tail_pointer(skb);
  1035. if (sp->this_idx < sp->start_idx)
  1036. goto out;
  1037. nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
  1038. sp->nlmsg_seq,
  1039. XFRM_MSG_NEWPOLICY, sizeof(*p));
  1040. p = NLMSG_DATA(nlh);
  1041. nlh->nlmsg_flags = sp->nlmsg_flags;
  1042. copy_to_user_policy(xp, p, dir);
  1043. if (copy_to_user_tmpl(xp, skb) < 0)
  1044. goto nlmsg_failure;
  1045. if (copy_to_user_sec_ctx(xp, skb))
  1046. goto nlmsg_failure;
  1047. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1048. goto nlmsg_failure;
  1049. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1050. out:
  1051. sp->this_idx++;
  1052. return 0;
  1053. nlmsg_failure:
  1054. nlmsg_trim(skb, b);
  1055. return -1;
  1056. }
  1057. static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
  1058. {
  1059. struct xfrm_dump_info info;
  1060. info.in_skb = cb->skb;
  1061. info.out_skb = skb;
  1062. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  1063. info.nlmsg_flags = NLM_F_MULTI;
  1064. info.this_idx = 0;
  1065. info.start_idx = cb->args[0];
  1066. (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
  1067. #ifdef CONFIG_XFRM_SUB_POLICY
  1068. (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
  1069. #endif
  1070. cb->args[0] = info.this_idx;
  1071. return skb->len;
  1072. }
  1073. static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
  1074. struct xfrm_policy *xp,
  1075. int dir, u32 seq)
  1076. {
  1077. struct xfrm_dump_info info;
  1078. struct sk_buff *skb;
  1079. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1080. if (!skb)
  1081. return ERR_PTR(-ENOMEM);
  1082. info.in_skb = in_skb;
  1083. info.out_skb = skb;
  1084. info.nlmsg_seq = seq;
  1085. info.nlmsg_flags = 0;
  1086. info.this_idx = info.start_idx = 0;
  1087. if (dump_one_policy(xp, dir, 0, &info) < 0) {
  1088. kfree_skb(skb);
  1089. return NULL;
  1090. }
  1091. return skb;
  1092. }
  1093. static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1094. struct rtattr **xfrma)
  1095. {
  1096. struct xfrm_policy *xp;
  1097. struct xfrm_userpolicy_id *p;
  1098. u8 type = XFRM_POLICY_TYPE_MAIN;
  1099. int err;
  1100. struct km_event c;
  1101. int delete;
  1102. p = NLMSG_DATA(nlh);
  1103. delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
  1104. err = copy_from_user_policy_type(&type, xfrma);
  1105. if (err)
  1106. return err;
  1107. err = verify_policy_dir(p->dir);
  1108. if (err)
  1109. return err;
  1110. if (p->index)
  1111. xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
  1112. else {
  1113. struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
  1114. struct xfrm_policy tmp;
  1115. err = verify_sec_ctx_len(xfrma);
  1116. if (err)
  1117. return err;
  1118. memset(&tmp, 0, sizeof(struct xfrm_policy));
  1119. if (rt) {
  1120. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  1121. if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
  1122. return err;
  1123. }
  1124. xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
  1125. delete, &err);
  1126. security_xfrm_policy_free(&tmp);
  1127. }
  1128. if (xp == NULL)
  1129. return -ENOENT;
  1130. if (!delete) {
  1131. struct sk_buff *resp_skb;
  1132. resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
  1133. if (IS_ERR(resp_skb)) {
  1134. err = PTR_ERR(resp_skb);
  1135. } else {
  1136. err = netlink_unicast(xfrm_nl, resp_skb,
  1137. NETLINK_CB(skb).pid,
  1138. MSG_DONTWAIT);
  1139. }
  1140. } else {
  1141. xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
  1142. AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
  1143. if (err != 0)
  1144. goto out;
  1145. c.data.byid = p->index;
  1146. c.event = nlh->nlmsg_type;
  1147. c.seq = nlh->nlmsg_seq;
  1148. c.pid = nlh->nlmsg_pid;
  1149. km_policy_notify(xp, p->dir, &c);
  1150. }
  1151. out:
  1152. xfrm_pol_put(xp);
  1153. return err;
  1154. }
  1155. static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  1156. struct rtattr **xfrma)
  1157. {
  1158. struct km_event c;
  1159. struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
  1160. struct xfrm_audit audit_info;
  1161. int err;
  1162. audit_info.loginuid = NETLINK_CB(skb).loginuid;
  1163. audit_info.secid = NETLINK_CB(skb).sid;
  1164. err = xfrm_state_flush(p->proto, &audit_info);
  1165. if (err)
  1166. return err;
  1167. c.data.proto = p->proto;
  1168. c.event = nlh->nlmsg_type;
  1169. c.seq = nlh->nlmsg_seq;
  1170. c.pid = nlh->nlmsg_pid;
  1171. km_state_notify(NULL, &c);
  1172. return 0;
  1173. }
  1174. static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
  1175. {
  1176. struct xfrm_aevent_id *id;
  1177. struct nlmsghdr *nlh;
  1178. struct xfrm_lifetime_cur ltime;
  1179. unsigned char *b = skb_tail_pointer(skb);
  1180. nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
  1181. id = NLMSG_DATA(nlh);
  1182. nlh->nlmsg_flags = 0;
  1183. memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
  1184. id->sa_id.spi = x->id.spi;
  1185. id->sa_id.family = x->props.family;
  1186. id->sa_id.proto = x->id.proto;
  1187. memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
  1188. id->reqid = x->props.reqid;
  1189. id->flags = c->data.aevent;
  1190. RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
  1191. ltime.bytes = x->curlft.bytes;
  1192. ltime.packets = x->curlft.packets;
  1193. ltime.add_time = x->curlft.add_time;
  1194. ltime.use_time = x->curlft.use_time;
  1195. RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
  1196. if (id->flags&XFRM_AE_RTHR) {
  1197. RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
  1198. }
  1199. if (id->flags&XFRM_AE_ETHR) {
  1200. u32 etimer = x->replay_maxage*10/HZ;
  1201. RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
  1202. }
  1203. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1204. return skb->len;
  1205. rtattr_failure:
  1206. nlmsg_failure:
  1207. nlmsg_trim(skb, b);
  1208. return -1;
  1209. }
  1210. static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1211. struct rtattr **xfrma)
  1212. {
  1213. struct xfrm_state *x;
  1214. struct sk_buff *r_skb;
  1215. int err;
  1216. struct km_event c;
  1217. struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
  1218. int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
  1219. struct xfrm_usersa_id *id = &p->sa_id;
  1220. len += RTA_SPACE(sizeof(struct xfrm_replay_state));
  1221. len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
  1222. if (p->flags&XFRM_AE_RTHR)
  1223. len+=RTA_SPACE(sizeof(u32));
  1224. if (p->flags&XFRM_AE_ETHR)
  1225. len+=RTA_SPACE(sizeof(u32));
  1226. r_skb = alloc_skb(len, GFP_ATOMIC);
  1227. if (r_skb == NULL)
  1228. return -ENOMEM;
  1229. x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
  1230. if (x == NULL) {
  1231. kfree_skb(r_skb);
  1232. return -ESRCH;
  1233. }
  1234. /*
  1235. * XXX: is this lock really needed - none of the other
  1236. * gets lock (the concern is things getting updated
  1237. * while we are still reading) - jhs
  1238. */
  1239. spin_lock_bh(&x->lock);
  1240. c.data.aevent = p->flags;
  1241. c.seq = nlh->nlmsg_seq;
  1242. c.pid = nlh->nlmsg_pid;
  1243. if (build_aevent(r_skb, x, &c) < 0)
  1244. BUG();
  1245. err = netlink_unicast(xfrm_nl, r_skb,
  1246. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  1247. spin_unlock_bh(&x->lock);
  1248. xfrm_state_put(x);
  1249. return err;
  1250. }
  1251. static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1252. struct rtattr **xfrma)
  1253. {
  1254. struct xfrm_state *x;
  1255. struct km_event c;
  1256. int err = - EINVAL;
  1257. struct xfrm_aevent_id *p = NLMSG_DATA(nlh);
  1258. struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
  1259. struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
  1260. if (!lt && !rp)
  1261. return err;
  1262. /* pedantic mode - thou shalt sayeth replaceth */
  1263. if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
  1264. return err;
  1265. x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
  1266. if (x == NULL)
  1267. return -ESRCH;
  1268. if (x->km.state != XFRM_STATE_VALID)
  1269. goto out;
  1270. spin_lock_bh(&x->lock);
  1271. err = xfrm_update_ae_params(x, xfrma);
  1272. spin_unlock_bh(&x->lock);
  1273. if (err < 0)
  1274. goto out;
  1275. c.event = nlh->nlmsg_type;
  1276. c.seq = nlh->nlmsg_seq;
  1277. c.pid = nlh->nlmsg_pid;
  1278. c.data.aevent = XFRM_AE_CU;
  1279. km_state_notify(x, &c);
  1280. err = 0;
  1281. out:
  1282. xfrm_state_put(x);
  1283. return err;
  1284. }
  1285. static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1286. struct rtattr **xfrma)
  1287. {
  1288. struct km_event c;
  1289. u8 type = XFRM_POLICY_TYPE_MAIN;
  1290. int err;
  1291. struct xfrm_audit audit_info;
  1292. err = copy_from_user_policy_type(&type, xfrma);
  1293. if (err)
  1294. return err;
  1295. audit_info.loginuid = NETLINK_CB(skb).loginuid;
  1296. audit_info.secid = NETLINK_CB(skb).sid;
  1297. err = xfrm_policy_flush(type, &audit_info);
  1298. if (err)
  1299. return err;
  1300. c.data.type = type;
  1301. c.event = nlh->nlmsg_type;
  1302. c.seq = nlh->nlmsg_seq;
  1303. c.pid = nlh->nlmsg_pid;
  1304. km_policy_notify(NULL, 0, &c);
  1305. return 0;
  1306. }
  1307. static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1308. struct rtattr **xfrma)
  1309. {
  1310. struct xfrm_policy *xp;
  1311. struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
  1312. struct xfrm_userpolicy_info *p = &up->pol;
  1313. u8 type = XFRM_POLICY_TYPE_MAIN;
  1314. int err = -ENOENT;
  1315. err = copy_from_user_policy_type(&type, xfrma);
  1316. if (err)
  1317. return err;
  1318. if (p->index)
  1319. xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
  1320. else {
  1321. struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
  1322. struct xfrm_policy tmp;
  1323. err = verify_sec_ctx_len(xfrma);
  1324. if (err)
  1325. return err;
  1326. memset(&tmp, 0, sizeof(struct xfrm_policy));
  1327. if (rt) {
  1328. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  1329. if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
  1330. return err;
  1331. }
  1332. xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
  1333. 0, &err);
  1334. security_xfrm_policy_free(&tmp);
  1335. }
  1336. if (xp == NULL)
  1337. return -ENOENT;
  1338. read_lock(&xp->lock);
  1339. if (xp->dead) {
  1340. read_unlock(&xp->lock);
  1341. goto out;
  1342. }
  1343. read_unlock(&xp->lock);
  1344. err = 0;
  1345. if (up->hard) {
  1346. xfrm_policy_delete(xp, p->dir);
  1347. xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
  1348. AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
  1349. } else {
  1350. // reset the timers here?
  1351. printk("Dont know what to do with soft policy expire\n");
  1352. }
  1353. km_policy_expired(xp, p->dir, up->hard, current->pid);
  1354. out:
  1355. xfrm_pol_put(xp);
  1356. return err;
  1357. }
  1358. static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1359. struct rtattr **xfrma)
  1360. {
  1361. struct xfrm_state *x;
  1362. int err;
  1363. struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
  1364. struct xfrm_usersa_info *p = &ue->state;
  1365. x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
  1366. err = -ENOENT;
  1367. if (x == NULL)
  1368. return err;
  1369. spin_lock_bh(&x->lock);
  1370. err = -EINVAL;
  1371. if (x->km.state != XFRM_STATE_VALID)
  1372. goto out;
  1373. km_state_expired(x, ue->hard, current->pid);
  1374. if (ue->hard) {
  1375. __xfrm_state_delete(x);
  1376. xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
  1377. AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
  1378. }
  1379. err = 0;
  1380. out:
  1381. spin_unlock_bh(&x->lock);
  1382. xfrm_state_put(x);
  1383. return err;
  1384. }
  1385. static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1386. struct rtattr **xfrma)
  1387. {
  1388. struct xfrm_policy *xp;
  1389. struct xfrm_user_tmpl *ut;
  1390. int i;
  1391. struct rtattr *rt = xfrma[XFRMA_TMPL-1];
  1392. struct xfrm_user_acquire *ua = NLMSG_DATA(nlh);
  1393. struct xfrm_state *x = xfrm_state_alloc();
  1394. int err = -ENOMEM;
  1395. if (!x)
  1396. return err;
  1397. err = verify_newpolicy_info(&ua->policy);
  1398. if (err) {
  1399. printk("BAD policy passed\n");
  1400. kfree(x);
  1401. return err;
  1402. }
  1403. /* build an XP */
  1404. xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
  1405. if (!xp) {
  1406. kfree(x);
  1407. return err;
  1408. }
  1409. memcpy(&x->id, &ua->id, sizeof(ua->id));
  1410. memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
  1411. memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
  1412. ut = RTA_DATA(rt);
  1413. /* extract the templates and for each call km_key */
  1414. for (i = 0; i < xp->xfrm_nr; i++, ut++) {
  1415. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  1416. memcpy(&x->id, &t->id, sizeof(x->id));
  1417. x->props.mode = t->mode;
  1418. x->props.reqid = t->reqid;
  1419. x->props.family = ut->family;
  1420. t->aalgos = ua->aalgos;
  1421. t->ealgos = ua->ealgos;
  1422. t->calgos = ua->calgos;
  1423. err = km_query(x, t, xp);
  1424. }
  1425. kfree(x);
  1426. kfree(xp);
  1427. return 0;
  1428. }
  1429. #ifdef CONFIG_XFRM_MIGRATE
  1430. static int verify_user_migrate(struct rtattr **xfrma)
  1431. {
  1432. struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
  1433. struct xfrm_user_migrate *um;
  1434. if (!rt)
  1435. return -EINVAL;
  1436. if ((rt->rta_len - sizeof(*rt)) < sizeof(*um))
  1437. return -EINVAL;
  1438. return 0;
  1439. }
  1440. static int copy_from_user_migrate(struct xfrm_migrate *ma,
  1441. struct rtattr **xfrma, int *num)
  1442. {
  1443. struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
  1444. struct xfrm_user_migrate *um;
  1445. int i, num_migrate;
  1446. um = RTA_DATA(rt);
  1447. num_migrate = (rt->rta_len - sizeof(*rt)) / sizeof(*um);
  1448. if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
  1449. return -EINVAL;
  1450. for (i = 0; i < num_migrate; i++, um++, ma++) {
  1451. memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
  1452. memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
  1453. memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
  1454. memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
  1455. ma->proto = um->proto;
  1456. ma->mode = um->mode;
  1457. ma->reqid = um->reqid;
  1458. ma->old_family = um->old_family;
  1459. ma->new_family = um->new_family;
  1460. }
  1461. *num = i;
  1462. return 0;
  1463. }
  1464. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1465. struct rtattr **xfrma)
  1466. {
  1467. struct xfrm_userpolicy_id *pi = NLMSG_DATA(nlh);
  1468. struct xfrm_migrate m[XFRM_MAX_DEPTH];
  1469. u8 type;
  1470. int err;
  1471. int n = 0;
  1472. err = verify_user_migrate((struct rtattr **)xfrma);
  1473. if (err)
  1474. return err;
  1475. err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
  1476. if (err)
  1477. return err;
  1478. err = copy_from_user_migrate((struct xfrm_migrate *)m,
  1479. (struct rtattr **)xfrma, &n);
  1480. if (err)
  1481. return err;
  1482. if (!n)
  1483. return 0;
  1484. xfrm_migrate(&pi->sel, pi->dir, type, m, n);
  1485. return 0;
  1486. }
  1487. #else
  1488. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1489. struct rtattr **xfrma)
  1490. {
  1491. return -ENOPROTOOPT;
  1492. }
  1493. #endif
  1494. #ifdef CONFIG_XFRM_MIGRATE
  1495. static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
  1496. {
  1497. struct xfrm_user_migrate um;
  1498. memset(&um, 0, sizeof(um));
  1499. um.proto = m->proto;
  1500. um.mode = m->mode;
  1501. um.reqid = m->reqid;
  1502. um.old_family = m->old_family;
  1503. memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
  1504. memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
  1505. um.new_family = m->new_family;
  1506. memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
  1507. memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
  1508. RTA_PUT(skb, XFRMA_MIGRATE, sizeof(um), &um);
  1509. return 0;
  1510. rtattr_failure:
  1511. return -1;
  1512. }
  1513. static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
  1514. int num_migrate, struct xfrm_selector *sel,
  1515. u8 dir, u8 type)
  1516. {
  1517. struct xfrm_migrate *mp;
  1518. struct xfrm_userpolicy_id *pol_id;
  1519. struct nlmsghdr *nlh;
  1520. unsigned char *b = skb_tail_pointer(skb);
  1521. int i;
  1522. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id));
  1523. pol_id = NLMSG_DATA(nlh);
  1524. nlh->nlmsg_flags = 0;
  1525. /* copy data from selector, dir, and type to the pol_id */
  1526. memset(pol_id, 0, sizeof(*pol_id));
  1527. memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
  1528. pol_id->dir = dir;
  1529. if (copy_to_user_policy_type(type, skb) < 0)
  1530. goto nlmsg_failure;
  1531. for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
  1532. if (copy_to_user_migrate(mp, skb) < 0)
  1533. goto nlmsg_failure;
  1534. }
  1535. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1536. return skb->len;
  1537. nlmsg_failure:
  1538. nlmsg_trim(skb, b);
  1539. return -1;
  1540. }
  1541. static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  1542. struct xfrm_migrate *m, int num_migrate)
  1543. {
  1544. struct sk_buff *skb;
  1545. size_t len;
  1546. len = RTA_SPACE(sizeof(struct xfrm_user_migrate) * num_migrate);
  1547. len += NLMSG_SPACE(sizeof(struct xfrm_userpolicy_id));
  1548. #ifdef CONFIG_XFRM_SUB_POLICY
  1549. len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
  1550. #endif
  1551. skb = alloc_skb(len, GFP_ATOMIC);
  1552. if (skb == NULL)
  1553. return -ENOMEM;
  1554. /* build migrate */
  1555. if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
  1556. BUG();
  1557. NETLINK_CB(skb).dst_group = XFRMNLGRP_MIGRATE;
  1558. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE,
  1559. GFP_ATOMIC);
  1560. }
  1561. #else
  1562. static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  1563. struct xfrm_migrate *m, int num_migrate)
  1564. {
  1565. return -ENOPROTOOPT;
  1566. }
  1567. #endif
  1568. #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
  1569. static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
  1570. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1571. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1572. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1573. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1574. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1575. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1576. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
  1577. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
  1578. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
  1579. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1580. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1581. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
  1582. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  1583. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
  1584. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1585. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1586. [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
  1587. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1588. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
  1589. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
  1590. };
  1591. #undef XMSGSIZE
  1592. static struct xfrm_link {
  1593. int (*doit)(struct sk_buff *, struct nlmsghdr *, struct rtattr **);
  1594. int (*dump)(struct sk_buff *, struct netlink_callback *);
  1595. } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
  1596. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1597. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
  1598. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
  1599. .dump = xfrm_dump_sa },
  1600. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1601. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
  1602. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
  1603. .dump = xfrm_dump_policy },
  1604. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
  1605. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
  1606. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
  1607. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1608. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1609. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
  1610. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
  1611. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
  1612. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
  1613. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
  1614. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
  1615. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
  1616. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
  1617. };
  1618. static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  1619. {
  1620. struct rtattr *xfrma[XFRMA_MAX];
  1621. struct xfrm_link *link;
  1622. int type, min_len;
  1623. type = nlh->nlmsg_type;
  1624. if (type > XFRM_MSG_MAX)
  1625. return -EINVAL;
  1626. type -= XFRM_MSG_BASE;
  1627. link = &xfrm_dispatch[type];
  1628. /* All operations require privileges, even GET */
  1629. if (security_netlink_recv(skb, CAP_NET_ADMIN))
  1630. return -EPERM;
  1631. if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
  1632. type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
  1633. (nlh->nlmsg_flags & NLM_F_DUMP)) {
  1634. if (link->dump == NULL)
  1635. return -EINVAL;
  1636. return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
  1637. }
  1638. memset(xfrma, 0, sizeof(xfrma));
  1639. if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
  1640. return -EINVAL;
  1641. if (nlh->nlmsg_len > min_len) {
  1642. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  1643. struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
  1644. while (RTA_OK(attr, attrlen)) {
  1645. unsigned short flavor = attr->rta_type;
  1646. if (flavor) {
  1647. if (flavor > XFRMA_MAX)
  1648. return -EINVAL;
  1649. xfrma[flavor - 1] = attr;
  1650. }
  1651. attr = RTA_NEXT(attr, attrlen);
  1652. }
  1653. }
  1654. if (link->doit == NULL)
  1655. return -EINVAL;
  1656. return link->doit(skb, nlh, xfrma);
  1657. }
  1658. static void xfrm_netlink_rcv(struct sock *sk, int len)
  1659. {
  1660. unsigned int qlen = 0;
  1661. do {
  1662. mutex_lock(&xfrm_cfg_mutex);
  1663. netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
  1664. mutex_unlock(&xfrm_cfg_mutex);
  1665. } while (qlen);
  1666. }
  1667. static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
  1668. {
  1669. struct xfrm_user_expire *ue;
  1670. struct nlmsghdr *nlh;
  1671. unsigned char *b = skb_tail_pointer(skb);
  1672. nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
  1673. sizeof(*ue));
  1674. ue = NLMSG_DATA(nlh);
  1675. nlh->nlmsg_flags = 0;
  1676. copy_to_user_state(x, &ue->state);
  1677. ue->hard = (c->data.hard != 0) ? 1 : 0;
  1678. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1679. return skb->len;
  1680. nlmsg_failure:
  1681. nlmsg_trim(skb, b);
  1682. return -1;
  1683. }
  1684. static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
  1685. {
  1686. struct sk_buff *skb;
  1687. int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
  1688. skb = alloc_skb(len, GFP_ATOMIC);
  1689. if (skb == NULL)
  1690. return -ENOMEM;
  1691. if (build_expire(skb, x, c) < 0)
  1692. BUG();
  1693. NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
  1694. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  1695. }
  1696. static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
  1697. {
  1698. struct sk_buff *skb;
  1699. int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
  1700. len += RTA_SPACE(sizeof(struct xfrm_replay_state));
  1701. len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
  1702. skb = alloc_skb(len, GFP_ATOMIC);
  1703. if (skb == NULL)
  1704. return -ENOMEM;
  1705. if (build_aevent(skb, x, c) < 0)
  1706. BUG();
  1707. NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
  1708. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
  1709. }
  1710. static int xfrm_notify_sa_flush(struct km_event *c)
  1711. {
  1712. struct xfrm_usersa_flush *p;
  1713. struct nlmsghdr *nlh;
  1714. struct sk_buff *skb;
  1715. sk_buff_data_t b;
  1716. int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
  1717. skb = alloc_skb(len, GFP_ATOMIC);
  1718. if (skb == NULL)
  1719. return -ENOMEM;
  1720. b = skb->tail;
  1721. nlh = NLMSG_PUT(skb, c->pid, c->seq,
  1722. XFRM_MSG_FLUSHSA, sizeof(*p));
  1723. nlh->nlmsg_flags = 0;
  1724. p = NLMSG_DATA(nlh);
  1725. p->proto = c->data.proto;
  1726. nlh->nlmsg_len = skb->tail - b;
  1727. NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
  1728. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  1729. nlmsg_failure:
  1730. kfree_skb(skb);
  1731. return -1;
  1732. }
  1733. static inline int xfrm_sa_len(struct xfrm_state *x)
  1734. {
  1735. int l = 0;
  1736. if (x->aalg)
  1737. l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
  1738. if (x->ealg)
  1739. l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
  1740. if (x->calg)
  1741. l += RTA_SPACE(sizeof(*x->calg));
  1742. if (x->encap)
  1743. l += RTA_SPACE(sizeof(*x->encap));
  1744. return l;
  1745. }
  1746. static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
  1747. {
  1748. struct xfrm_usersa_info *p;
  1749. struct xfrm_usersa_id *id;
  1750. struct nlmsghdr *nlh;
  1751. struct sk_buff *skb;
  1752. sk_buff_data_t b;
  1753. int len = xfrm_sa_len(x);
  1754. int headlen;
  1755. headlen = sizeof(*p);
  1756. if (c->event == XFRM_MSG_DELSA) {
  1757. len += RTA_SPACE(headlen);
  1758. headlen = sizeof(*id);
  1759. }
  1760. len += NLMSG_SPACE(headlen);
  1761. skb = alloc_skb(len, GFP_ATOMIC);
  1762. if (skb == NULL)
  1763. return -ENOMEM;
  1764. b = skb->tail;
  1765. nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
  1766. nlh->nlmsg_flags = 0;
  1767. p = NLMSG_DATA(nlh);
  1768. if (c->event == XFRM_MSG_DELSA) {
  1769. id = NLMSG_DATA(nlh);
  1770. memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
  1771. id->spi = x->id.spi;
  1772. id->family = x->props.family;
  1773. id->proto = x->id.proto;
  1774. p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
  1775. }
  1776. copy_to_user_state(x, p);
  1777. if (x->aalg)
  1778. RTA_PUT(skb, XFRMA_ALG_AUTH,
  1779. sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
  1780. if (x->ealg)
  1781. RTA_PUT(skb, XFRMA_ALG_CRYPT,
  1782. sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
  1783. if (x->calg)
  1784. RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  1785. if (x->encap)
  1786. RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  1787. nlh->nlmsg_len = skb->tail - b;
  1788. NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
  1789. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  1790. nlmsg_failure:
  1791. rtattr_failure:
  1792. kfree_skb(skb);
  1793. return -1;
  1794. }
  1795. static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
  1796. {
  1797. switch (c->event) {
  1798. case XFRM_MSG_EXPIRE:
  1799. return xfrm_exp_state_notify(x, c);
  1800. case XFRM_MSG_NEWAE:
  1801. return xfrm_aevent_state_notify(x, c);
  1802. case XFRM_MSG_DELSA:
  1803. case XFRM_MSG_UPDSA:
  1804. case XFRM_MSG_NEWSA:
  1805. return xfrm_notify_sa(x, c);
  1806. case XFRM_MSG_FLUSHSA:
  1807. return xfrm_notify_sa_flush(c);
  1808. default:
  1809. printk("xfrm_user: Unknown SA event %d\n", c->event);
  1810. break;
  1811. }
  1812. return 0;
  1813. }
  1814. static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
  1815. struct xfrm_tmpl *xt, struct xfrm_policy *xp,
  1816. int dir)
  1817. {
  1818. struct xfrm_user_acquire *ua;
  1819. struct nlmsghdr *nlh;
  1820. unsigned char *b = skb_tail_pointer(skb);
  1821. __u32 seq = xfrm_get_acqseq();
  1822. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
  1823. sizeof(*ua));
  1824. ua = NLMSG_DATA(nlh);
  1825. nlh->nlmsg_flags = 0;
  1826. memcpy(&ua->id, &x->id, sizeof(ua->id));
  1827. memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
  1828. memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
  1829. copy_to_user_policy(xp, &ua->policy, dir);
  1830. ua->aalgos = xt->aalgos;
  1831. ua->ealgos = xt->ealgos;
  1832. ua->calgos = xt->calgos;
  1833. ua->seq = x->km.seq = seq;
  1834. if (copy_to_user_tmpl(xp, skb) < 0)
  1835. goto nlmsg_failure;
  1836. if (copy_to_user_state_sec_ctx(x, skb))
  1837. goto nlmsg_failure;
  1838. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1839. goto nlmsg_failure;
  1840. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1841. return skb->len;
  1842. nlmsg_failure:
  1843. nlmsg_trim(skb, b);
  1844. return -1;
  1845. }
  1846. static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
  1847. struct xfrm_policy *xp, int dir)
  1848. {
  1849. struct sk_buff *skb;
  1850. size_t len;
  1851. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1852. len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
  1853. len += RTA_SPACE(xfrm_user_sec_ctx_size(x->security));
  1854. #ifdef CONFIG_XFRM_SUB_POLICY
  1855. len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
  1856. #endif
  1857. skb = alloc_skb(len, GFP_ATOMIC);
  1858. if (skb == NULL)
  1859. return -ENOMEM;
  1860. if (build_acquire(skb, x, xt, xp, dir) < 0)
  1861. BUG();
  1862. NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
  1863. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
  1864. }
  1865. /* User gives us xfrm_user_policy_info followed by an array of 0
  1866. * or more templates.
  1867. */
  1868. static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
  1869. u8 *data, int len, int *dir)
  1870. {
  1871. struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
  1872. struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
  1873. struct xfrm_policy *xp;
  1874. int nr;
  1875. switch (sk->sk_family) {
  1876. case AF_INET:
  1877. if (opt != IP_XFRM_POLICY) {
  1878. *dir = -EOPNOTSUPP;
  1879. return NULL;
  1880. }
  1881. break;
  1882. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1883. case AF_INET6:
  1884. if (opt != IPV6_XFRM_POLICY) {
  1885. *dir = -EOPNOTSUPP;
  1886. return NULL;
  1887. }
  1888. break;
  1889. #endif
  1890. default:
  1891. *dir = -EINVAL;
  1892. return NULL;
  1893. }
  1894. *dir = -EINVAL;
  1895. if (len < sizeof(*p) ||
  1896. verify_newpolicy_info(p))
  1897. return NULL;
  1898. nr = ((len - sizeof(*p)) / sizeof(*ut));
  1899. if (validate_tmpl(nr, ut, p->sel.family))
  1900. return NULL;
  1901. if (p->dir > XFRM_POLICY_OUT)
  1902. return NULL;
  1903. xp = xfrm_policy_alloc(GFP_KERNEL);
  1904. if (xp == NULL) {
  1905. *dir = -ENOBUFS;
  1906. return NULL;
  1907. }
  1908. copy_from_user_policy(xp, p);
  1909. xp->type = XFRM_POLICY_TYPE_MAIN;
  1910. copy_templates(xp, ut, nr);
  1911. *dir = p->dir;
  1912. return xp;
  1913. }
  1914. static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
  1915. int dir, struct km_event *c)
  1916. {
  1917. struct xfrm_user_polexpire *upe;
  1918. struct nlmsghdr *nlh;
  1919. int hard = c->data.hard;
  1920. unsigned char *b = skb_tail_pointer(skb);
  1921. nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
  1922. upe = NLMSG_DATA(nlh);
  1923. nlh->nlmsg_flags = 0;
  1924. copy_to_user_policy(xp, &upe->pol, dir);
  1925. if (copy_to_user_tmpl(xp, skb) < 0)
  1926. goto nlmsg_failure;
  1927. if (copy_to_user_sec_ctx(xp, skb))
  1928. goto nlmsg_failure;
  1929. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1930. goto nlmsg_failure;
  1931. upe->hard = !!hard;
  1932. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  1933. return skb->len;
  1934. nlmsg_failure:
  1935. nlmsg_trim(skb, b);
  1936. return -1;
  1937. }
  1938. static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  1939. {
  1940. struct sk_buff *skb;
  1941. size_t len;
  1942. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1943. len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
  1944. len += RTA_SPACE(xfrm_user_sec_ctx_size(xp->security));
  1945. #ifdef CONFIG_XFRM_SUB_POLICY
  1946. len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
  1947. #endif
  1948. skb = alloc_skb(len, GFP_ATOMIC);
  1949. if (skb == NULL)
  1950. return -ENOMEM;
  1951. if (build_polexpire(skb, xp, dir, c) < 0)
  1952. BUG();
  1953. NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
  1954. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  1955. }
  1956. static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
  1957. {
  1958. struct xfrm_userpolicy_info *p;
  1959. struct xfrm_userpolicy_id *id;
  1960. struct nlmsghdr *nlh;
  1961. struct sk_buff *skb;
  1962. sk_buff_data_t b;
  1963. int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1964. int headlen;
  1965. headlen = sizeof(*p);
  1966. if (c->event == XFRM_MSG_DELPOLICY) {
  1967. len += RTA_SPACE(headlen);
  1968. headlen = sizeof(*id);
  1969. }
  1970. #ifdef CONFIG_XFRM_SUB_POLICY
  1971. len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
  1972. #endif
  1973. len += NLMSG_SPACE(headlen);
  1974. skb = alloc_skb(len, GFP_ATOMIC);
  1975. if (skb == NULL)
  1976. return -ENOMEM;
  1977. b = skb->tail;
  1978. nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
  1979. p = NLMSG_DATA(nlh);
  1980. if (c->event == XFRM_MSG_DELPOLICY) {
  1981. id = NLMSG_DATA(nlh);
  1982. memset(id, 0, sizeof(*id));
  1983. id->dir = dir;
  1984. if (c->data.byid)
  1985. id->index = xp->index;
  1986. else
  1987. memcpy(&id->sel, &xp->selector, sizeof(id->sel));
  1988. p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
  1989. }
  1990. nlh->nlmsg_flags = 0;
  1991. copy_to_user_policy(xp, p, dir);
  1992. if (copy_to_user_tmpl(xp, skb) < 0)
  1993. goto nlmsg_failure;
  1994. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1995. goto nlmsg_failure;
  1996. nlh->nlmsg_len = skb->tail - b;
  1997. NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
  1998. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  1999. nlmsg_failure:
  2000. rtattr_failure:
  2001. kfree_skb(skb);
  2002. return -1;
  2003. }
  2004. static int xfrm_notify_policy_flush(struct km_event *c)
  2005. {
  2006. struct nlmsghdr *nlh;
  2007. struct sk_buff *skb;
  2008. sk_buff_data_t b;
  2009. int len = 0;
  2010. #ifdef CONFIG_XFRM_SUB_POLICY
  2011. len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
  2012. #endif
  2013. len += NLMSG_LENGTH(0);
  2014. skb = alloc_skb(len, GFP_ATOMIC);
  2015. if (skb == NULL)
  2016. return -ENOMEM;
  2017. b = skb->tail;
  2018. nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
  2019. nlh->nlmsg_flags = 0;
  2020. if (copy_to_user_policy_type(c->data.type, skb) < 0)
  2021. goto nlmsg_failure;
  2022. nlh->nlmsg_len = skb->tail - b;
  2023. NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
  2024. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  2025. nlmsg_failure:
  2026. kfree_skb(skb);
  2027. return -1;
  2028. }
  2029. static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  2030. {
  2031. switch (c->event) {
  2032. case XFRM_MSG_NEWPOLICY:
  2033. case XFRM_MSG_UPDPOLICY:
  2034. case XFRM_MSG_DELPOLICY:
  2035. return xfrm_notify_policy(xp, dir, c);
  2036. case XFRM_MSG_FLUSHPOLICY:
  2037. return xfrm_notify_policy_flush(c);
  2038. case XFRM_MSG_POLEXPIRE:
  2039. return xfrm_exp_policy_notify(xp, dir, c);
  2040. default:
  2041. printk("xfrm_user: Unknown Policy event %d\n", c->event);
  2042. }
  2043. return 0;
  2044. }
  2045. static int build_report(struct sk_buff *skb, u8 proto,
  2046. struct xfrm_selector *sel, xfrm_address_t *addr)
  2047. {
  2048. struct xfrm_user_report *ur;
  2049. struct nlmsghdr *nlh;
  2050. unsigned char *b = skb_tail_pointer(skb);
  2051. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
  2052. ur = NLMSG_DATA(nlh);
  2053. nlh->nlmsg_flags = 0;
  2054. ur->proto = proto;
  2055. memcpy(&ur->sel, sel, sizeof(ur->sel));
  2056. if (addr)
  2057. RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
  2058. nlh->nlmsg_len = skb_tail_pointer(skb) - b;
  2059. return skb->len;
  2060. nlmsg_failure:
  2061. rtattr_failure:
  2062. nlmsg_trim(skb, b);
  2063. return -1;
  2064. }
  2065. static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
  2066. xfrm_address_t *addr)
  2067. {
  2068. struct sk_buff *skb;
  2069. size_t len;
  2070. len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
  2071. skb = alloc_skb(len, GFP_ATOMIC);
  2072. if (skb == NULL)
  2073. return -ENOMEM;
  2074. if (build_report(skb, proto, sel, addr) < 0)
  2075. BUG();
  2076. NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
  2077. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
  2078. }
  2079. static struct xfrm_mgr netlink_mgr = {
  2080. .id = "netlink",
  2081. .notify = xfrm_send_state_notify,
  2082. .acquire = xfrm_send_acquire,
  2083. .compile_policy = xfrm_compile_policy,
  2084. .notify_policy = xfrm_send_policy_notify,
  2085. .report = xfrm_send_report,
  2086. .migrate = xfrm_send_migrate,
  2087. };
  2088. static int __init xfrm_user_init(void)
  2089. {
  2090. struct sock *nlsk;
  2091. printk(KERN_INFO "Initializing XFRM netlink socket\n");
  2092. nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
  2093. xfrm_netlink_rcv, NULL, THIS_MODULE);
  2094. if (nlsk == NULL)
  2095. return -ENOMEM;
  2096. rcu_assign_pointer(xfrm_nl, nlsk);
  2097. xfrm_register_km(&netlink_mgr);
  2098. return 0;
  2099. }
  2100. static void __exit xfrm_user_exit(void)
  2101. {
  2102. struct sock *nlsk = xfrm_nl;
  2103. xfrm_unregister_km(&netlink_mgr);
  2104. rcu_assign_pointer(xfrm_nl, NULL);
  2105. synchronize_rcu();
  2106. sock_release(nlsk->sk_socket);
  2107. }
  2108. module_init(xfrm_user_init);
  2109. module_exit(xfrm_user_exit);
  2110. MODULE_LICENSE("GPL");
  2111. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);