xfrm_user.c 55 KB

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