xfrm_user.c 57 KB

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