xfrm_user.c 57 KB

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