xfrm_user.c 50 KB

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