xfrm_user.c 55 KB

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