xfrm_user.c 55 KB

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