xfrm_user.c 57 KB

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