xfrm_user.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  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/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/slab.h>
  16. #include <linux/socket.h>
  17. #include <linux/string.h>
  18. #include <linux/net.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/rtnetlink.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. static struct sock *xfrm_nl;
  30. static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
  31. {
  32. struct rtattr *rt = xfrma[type - 1];
  33. struct xfrm_algo *algp;
  34. int len;
  35. if (!rt)
  36. return 0;
  37. len = (rt->rta_len - sizeof(*rt)) - sizeof(*algp);
  38. if (len < 0)
  39. return -EINVAL;
  40. algp = RTA_DATA(rt);
  41. len -= (algp->alg_key_len + 7U) / 8;
  42. if (len < 0)
  43. return -EINVAL;
  44. switch (type) {
  45. case XFRMA_ALG_AUTH:
  46. if (!algp->alg_key_len &&
  47. strcmp(algp->alg_name, "digest_null") != 0)
  48. return -EINVAL;
  49. break;
  50. case XFRMA_ALG_CRYPT:
  51. if (!algp->alg_key_len &&
  52. strcmp(algp->alg_name, "cipher_null") != 0)
  53. return -EINVAL;
  54. break;
  55. case XFRMA_ALG_COMP:
  56. /* Zero length keys are legal. */
  57. break;
  58. default:
  59. return -EINVAL;
  60. };
  61. algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
  62. return 0;
  63. }
  64. static int verify_encap_tmpl(struct rtattr **xfrma)
  65. {
  66. struct rtattr *rt = xfrma[XFRMA_ENCAP - 1];
  67. struct xfrm_encap_tmpl *encap;
  68. if (!rt)
  69. return 0;
  70. if ((rt->rta_len - sizeof(*rt)) < sizeof(*encap))
  71. return -EINVAL;
  72. return 0;
  73. }
  74. static inline int verify_sec_ctx_len(struct rtattr **xfrma)
  75. {
  76. struct rtattr *rt = xfrma[XFRMA_SEC_CTX - 1];
  77. struct xfrm_user_sec_ctx *uctx;
  78. int len = 0;
  79. if (!rt)
  80. return 0;
  81. if (rt->rta_len < sizeof(*uctx))
  82. return -EINVAL;
  83. uctx = RTA_DATA(rt);
  84. if (uctx->ctx_len > PAGE_SIZE)
  85. return -EINVAL;
  86. len += sizeof(struct xfrm_user_sec_ctx);
  87. len += uctx->ctx_len;
  88. if (uctx->len != len)
  89. return -EINVAL;
  90. return 0;
  91. }
  92. static int verify_newsa_info(struct xfrm_usersa_info *p,
  93. struct rtattr **xfrma)
  94. {
  95. int err;
  96. err = -EINVAL;
  97. switch (p->family) {
  98. case AF_INET:
  99. break;
  100. case AF_INET6:
  101. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  102. break;
  103. #else
  104. err = -EAFNOSUPPORT;
  105. goto out;
  106. #endif
  107. default:
  108. goto out;
  109. };
  110. err = -EINVAL;
  111. switch (p->id.proto) {
  112. case IPPROTO_AH:
  113. if (!xfrma[XFRMA_ALG_AUTH-1] ||
  114. xfrma[XFRMA_ALG_CRYPT-1] ||
  115. xfrma[XFRMA_ALG_COMP-1])
  116. goto out;
  117. break;
  118. case IPPROTO_ESP:
  119. if ((!xfrma[XFRMA_ALG_AUTH-1] &&
  120. !xfrma[XFRMA_ALG_CRYPT-1]) ||
  121. xfrma[XFRMA_ALG_COMP-1])
  122. goto out;
  123. break;
  124. case IPPROTO_COMP:
  125. if (!xfrma[XFRMA_ALG_COMP-1] ||
  126. xfrma[XFRMA_ALG_AUTH-1] ||
  127. xfrma[XFRMA_ALG_CRYPT-1])
  128. goto out;
  129. break;
  130. default:
  131. goto out;
  132. };
  133. if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
  134. goto out;
  135. if ((err = verify_one_alg(xfrma, XFRMA_ALG_CRYPT)))
  136. goto out;
  137. if ((err = verify_one_alg(xfrma, XFRMA_ALG_COMP)))
  138. goto out;
  139. if ((err = verify_encap_tmpl(xfrma)))
  140. goto out;
  141. if ((err = verify_sec_ctx_len(xfrma)))
  142. goto out;
  143. err = -EINVAL;
  144. switch (p->mode) {
  145. case 0:
  146. case 1:
  147. break;
  148. default:
  149. goto out;
  150. };
  151. err = 0;
  152. out:
  153. return err;
  154. }
  155. static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
  156. struct xfrm_algo_desc *(*get_byname)(char *, int),
  157. struct rtattr *u_arg)
  158. {
  159. struct rtattr *rta = u_arg;
  160. struct xfrm_algo *p, *ualg;
  161. struct xfrm_algo_desc *algo;
  162. int len;
  163. if (!rta)
  164. return 0;
  165. ualg = RTA_DATA(rta);
  166. algo = get_byname(ualg->alg_name, 1);
  167. if (!algo)
  168. return -ENOSYS;
  169. *props = algo->desc.sadb_alg_id;
  170. len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
  171. p = kmalloc(len, GFP_KERNEL);
  172. if (!p)
  173. return -ENOMEM;
  174. memcpy(p, ualg, len);
  175. *algpp = p;
  176. return 0;
  177. }
  178. static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_arg)
  179. {
  180. struct rtattr *rta = u_arg;
  181. struct xfrm_encap_tmpl *p, *uencap;
  182. if (!rta)
  183. return 0;
  184. uencap = RTA_DATA(rta);
  185. p = kmalloc(sizeof(*p), GFP_KERNEL);
  186. if (!p)
  187. return -ENOMEM;
  188. memcpy(p, uencap, sizeof(*p));
  189. *encapp = p;
  190. return 0;
  191. }
  192. static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
  193. {
  194. struct xfrm_sec_ctx *xfrm_ctx = xp->security;
  195. int len = 0;
  196. if (xfrm_ctx) {
  197. len += sizeof(struct xfrm_user_sec_ctx);
  198. len += xfrm_ctx->ctx_len;
  199. }
  200. return len;
  201. }
  202. static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
  203. {
  204. struct xfrm_user_sec_ctx *uctx;
  205. if (!u_arg)
  206. return 0;
  207. uctx = RTA_DATA(u_arg);
  208. return security_xfrm_state_alloc(x, uctx);
  209. }
  210. static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  211. {
  212. memcpy(&x->id, &p->id, sizeof(x->id));
  213. memcpy(&x->sel, &p->sel, sizeof(x->sel));
  214. memcpy(&x->lft, &p->lft, sizeof(x->lft));
  215. x->props.mode = p->mode;
  216. x->props.replay_window = p->replay_window;
  217. x->props.reqid = p->reqid;
  218. x->props.family = p->family;
  219. x->props.saddr = p->saddr;
  220. x->props.flags = p->flags;
  221. }
  222. static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
  223. struct rtattr **xfrma,
  224. int *errp)
  225. {
  226. struct xfrm_state *x = xfrm_state_alloc();
  227. int err = -ENOMEM;
  228. if (!x)
  229. goto error_no_put;
  230. copy_from_user_state(x, p);
  231. if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
  232. xfrm_aalg_get_byname,
  233. xfrma[XFRMA_ALG_AUTH-1])))
  234. goto error;
  235. if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
  236. xfrm_ealg_get_byname,
  237. xfrma[XFRMA_ALG_CRYPT-1])))
  238. goto error;
  239. if ((err = attach_one_algo(&x->calg, &x->props.calgo,
  240. xfrm_calg_get_byname,
  241. xfrma[XFRMA_ALG_COMP-1])))
  242. goto error;
  243. if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
  244. goto error;
  245. err = xfrm_init_state(x);
  246. if (err)
  247. goto error;
  248. if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
  249. goto error;
  250. x->km.seq = p->seq;
  251. return x;
  252. error:
  253. x->km.state = XFRM_STATE_DEAD;
  254. xfrm_state_put(x);
  255. error_no_put:
  256. *errp = err;
  257. return NULL;
  258. }
  259. static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  260. {
  261. struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
  262. struct xfrm_state *x;
  263. int err;
  264. struct km_event c;
  265. err = verify_newsa_info(p, (struct rtattr **)xfrma);
  266. if (err)
  267. return err;
  268. x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
  269. if (!x)
  270. return err;
  271. xfrm_state_hold(x);
  272. if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
  273. err = xfrm_state_add(x);
  274. else
  275. err = xfrm_state_update(x);
  276. if (err < 0) {
  277. x->km.state = XFRM_STATE_DEAD;
  278. xfrm_state_put(x);
  279. goto out;
  280. }
  281. c.seq = nlh->nlmsg_seq;
  282. c.pid = nlh->nlmsg_pid;
  283. c.event = nlh->nlmsg_type;
  284. km_state_notify(x, &c);
  285. out:
  286. xfrm_state_put(x);
  287. return err;
  288. }
  289. static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  290. {
  291. struct xfrm_state *x;
  292. int err;
  293. struct km_event c;
  294. struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
  295. x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
  296. if (x == NULL)
  297. return -ESRCH;
  298. if (xfrm_state_kern(x)) {
  299. xfrm_state_put(x);
  300. return -EPERM;
  301. }
  302. err = xfrm_state_delete(x);
  303. if (err < 0) {
  304. xfrm_state_put(x);
  305. return err;
  306. }
  307. c.seq = nlh->nlmsg_seq;
  308. c.pid = nlh->nlmsg_pid;
  309. c.event = nlh->nlmsg_type;
  310. km_state_notify(x, &c);
  311. xfrm_state_put(x);
  312. return err;
  313. }
  314. static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  315. {
  316. memcpy(&p->id, &x->id, sizeof(p->id));
  317. memcpy(&p->sel, &x->sel, sizeof(p->sel));
  318. memcpy(&p->lft, &x->lft, sizeof(p->lft));
  319. memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
  320. memcpy(&p->stats, &x->stats, sizeof(p->stats));
  321. p->saddr = x->props.saddr;
  322. p->mode = x->props.mode;
  323. p->replay_window = x->props.replay_window;
  324. p->reqid = x->props.reqid;
  325. p->family = x->props.family;
  326. p->flags = x->props.flags;
  327. p->seq = x->km.seq;
  328. }
  329. struct xfrm_dump_info {
  330. struct sk_buff *in_skb;
  331. struct sk_buff *out_skb;
  332. u32 nlmsg_seq;
  333. u16 nlmsg_flags;
  334. int start_idx;
  335. int this_idx;
  336. };
  337. static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
  338. {
  339. struct xfrm_dump_info *sp = ptr;
  340. struct sk_buff *in_skb = sp->in_skb;
  341. struct sk_buff *skb = sp->out_skb;
  342. struct xfrm_usersa_info *p;
  343. struct nlmsghdr *nlh;
  344. unsigned char *b = skb->tail;
  345. if (sp->this_idx < sp->start_idx)
  346. goto out;
  347. nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
  348. sp->nlmsg_seq,
  349. XFRM_MSG_NEWSA, sizeof(*p));
  350. nlh->nlmsg_flags = sp->nlmsg_flags;
  351. p = NLMSG_DATA(nlh);
  352. copy_to_user_state(x, p);
  353. if (x->aalg)
  354. RTA_PUT(skb, XFRMA_ALG_AUTH,
  355. sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
  356. if (x->ealg)
  357. RTA_PUT(skb, XFRMA_ALG_CRYPT,
  358. sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
  359. if (x->calg)
  360. RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  361. if (x->encap)
  362. RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  363. if (x->security) {
  364. int ctx_size = sizeof(struct xfrm_sec_ctx) +
  365. x->security->ctx_len;
  366. struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
  367. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  368. uctx->exttype = XFRMA_SEC_CTX;
  369. uctx->len = ctx_size;
  370. uctx->ctx_doi = x->security->ctx_doi;
  371. uctx->ctx_alg = x->security->ctx_alg;
  372. uctx->ctx_len = x->security->ctx_len;
  373. memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
  374. }
  375. nlh->nlmsg_len = skb->tail - b;
  376. out:
  377. sp->this_idx++;
  378. return 0;
  379. nlmsg_failure:
  380. rtattr_failure:
  381. skb_trim(skb, b - skb->data);
  382. return -1;
  383. }
  384. static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
  385. {
  386. struct xfrm_dump_info info;
  387. info.in_skb = cb->skb;
  388. info.out_skb = skb;
  389. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  390. info.nlmsg_flags = NLM_F_MULTI;
  391. info.this_idx = 0;
  392. info.start_idx = cb->args[0];
  393. (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
  394. cb->args[0] = info.this_idx;
  395. return skb->len;
  396. }
  397. static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
  398. struct xfrm_state *x, u32 seq)
  399. {
  400. struct xfrm_dump_info info;
  401. struct sk_buff *skb;
  402. skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
  403. if (!skb)
  404. return ERR_PTR(-ENOMEM);
  405. NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
  406. info.in_skb = in_skb;
  407. info.out_skb = skb;
  408. info.nlmsg_seq = seq;
  409. info.nlmsg_flags = 0;
  410. info.this_idx = info.start_idx = 0;
  411. if (dump_one_state(x, 0, &info)) {
  412. kfree_skb(skb);
  413. return NULL;
  414. }
  415. return skb;
  416. }
  417. static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  418. {
  419. struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
  420. struct xfrm_state *x;
  421. struct sk_buff *resp_skb;
  422. int err;
  423. x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
  424. err = -ESRCH;
  425. if (x == NULL)
  426. goto out_noput;
  427. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  428. if (IS_ERR(resp_skb)) {
  429. err = PTR_ERR(resp_skb);
  430. } else {
  431. err = netlink_unicast(xfrm_nl, resp_skb,
  432. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  433. }
  434. xfrm_state_put(x);
  435. out_noput:
  436. return err;
  437. }
  438. static int verify_userspi_info(struct xfrm_userspi_info *p)
  439. {
  440. switch (p->info.id.proto) {
  441. case IPPROTO_AH:
  442. case IPPROTO_ESP:
  443. break;
  444. case IPPROTO_COMP:
  445. /* IPCOMP spi is 16-bits. */
  446. if (p->max >= 0x10000)
  447. return -EINVAL;
  448. break;
  449. default:
  450. return -EINVAL;
  451. };
  452. if (p->min > p->max)
  453. return -EINVAL;
  454. return 0;
  455. }
  456. static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  457. {
  458. struct xfrm_state *x;
  459. struct xfrm_userspi_info *p;
  460. struct sk_buff *resp_skb;
  461. xfrm_address_t *daddr;
  462. int family;
  463. int err;
  464. p = NLMSG_DATA(nlh);
  465. err = verify_userspi_info(p);
  466. if (err)
  467. goto out_noput;
  468. family = p->info.family;
  469. daddr = &p->info.id.daddr;
  470. x = NULL;
  471. if (p->info.seq) {
  472. x = xfrm_find_acq_byseq(p->info.seq);
  473. if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
  474. xfrm_state_put(x);
  475. x = NULL;
  476. }
  477. }
  478. if (!x)
  479. x = xfrm_find_acq(p->info.mode, p->info.reqid,
  480. p->info.id.proto, daddr,
  481. &p->info.saddr, 1,
  482. family);
  483. err = -ENOENT;
  484. if (x == NULL)
  485. goto out_noput;
  486. resp_skb = ERR_PTR(-ENOENT);
  487. spin_lock_bh(&x->lock);
  488. if (x->km.state != XFRM_STATE_DEAD) {
  489. xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
  490. if (x->id.spi)
  491. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  492. }
  493. spin_unlock_bh(&x->lock);
  494. if (IS_ERR(resp_skb)) {
  495. err = PTR_ERR(resp_skb);
  496. goto out;
  497. }
  498. err = netlink_unicast(xfrm_nl, resp_skb,
  499. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  500. out:
  501. xfrm_state_put(x);
  502. out_noput:
  503. return err;
  504. }
  505. static int verify_policy_dir(__u8 dir)
  506. {
  507. switch (dir) {
  508. case XFRM_POLICY_IN:
  509. case XFRM_POLICY_OUT:
  510. case XFRM_POLICY_FWD:
  511. break;
  512. default:
  513. return -EINVAL;
  514. };
  515. return 0;
  516. }
  517. static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
  518. {
  519. switch (p->share) {
  520. case XFRM_SHARE_ANY:
  521. case XFRM_SHARE_SESSION:
  522. case XFRM_SHARE_USER:
  523. case XFRM_SHARE_UNIQUE:
  524. break;
  525. default:
  526. return -EINVAL;
  527. };
  528. switch (p->action) {
  529. case XFRM_POLICY_ALLOW:
  530. case XFRM_POLICY_BLOCK:
  531. break;
  532. default:
  533. return -EINVAL;
  534. };
  535. switch (p->sel.family) {
  536. case AF_INET:
  537. break;
  538. case AF_INET6:
  539. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  540. break;
  541. #else
  542. return -EAFNOSUPPORT;
  543. #endif
  544. default:
  545. return -EINVAL;
  546. };
  547. return verify_policy_dir(p->dir);
  548. }
  549. static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
  550. {
  551. struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
  552. struct xfrm_user_sec_ctx *uctx;
  553. if (!rt)
  554. return 0;
  555. uctx = RTA_DATA(rt);
  556. return security_xfrm_policy_alloc(pol, uctx);
  557. }
  558. static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
  559. int nr)
  560. {
  561. int i;
  562. xp->xfrm_nr = nr;
  563. for (i = 0; i < nr; i++, ut++) {
  564. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  565. memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
  566. memcpy(&t->saddr, &ut->saddr,
  567. sizeof(xfrm_address_t));
  568. t->reqid = ut->reqid;
  569. t->mode = ut->mode;
  570. t->share = ut->share;
  571. t->optional = ut->optional;
  572. t->aalgos = ut->aalgos;
  573. t->ealgos = ut->ealgos;
  574. t->calgos = ut->calgos;
  575. }
  576. }
  577. static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
  578. {
  579. struct rtattr *rt = xfrma[XFRMA_TMPL-1];
  580. struct xfrm_user_tmpl *utmpl;
  581. int nr;
  582. if (!rt) {
  583. pol->xfrm_nr = 0;
  584. } else {
  585. nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
  586. if (nr > XFRM_MAX_DEPTH)
  587. return -EINVAL;
  588. copy_templates(pol, RTA_DATA(rt), nr);
  589. }
  590. return 0;
  591. }
  592. static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
  593. {
  594. xp->priority = p->priority;
  595. xp->index = p->index;
  596. memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
  597. memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
  598. xp->action = p->action;
  599. xp->flags = p->flags;
  600. xp->family = p->sel.family;
  601. /* XXX xp->share = p->share; */
  602. }
  603. static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
  604. {
  605. memcpy(&p->sel, &xp->selector, sizeof(p->sel));
  606. memcpy(&p->lft, &xp->lft, sizeof(p->lft));
  607. memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
  608. p->priority = xp->priority;
  609. p->index = xp->index;
  610. p->sel.family = xp->family;
  611. p->dir = dir;
  612. p->action = xp->action;
  613. p->flags = xp->flags;
  614. p->share = XFRM_SHARE_ANY; /* XXX xp->share */
  615. }
  616. static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
  617. {
  618. struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
  619. int err;
  620. if (!xp) {
  621. *errp = -ENOMEM;
  622. return NULL;
  623. }
  624. copy_from_user_policy(xp, p);
  625. if (!(err = copy_from_user_tmpl(xp, xfrma)))
  626. err = copy_from_user_sec_ctx(xp, xfrma);
  627. if (err) {
  628. *errp = err;
  629. kfree(xp);
  630. xp = NULL;
  631. }
  632. return xp;
  633. }
  634. static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  635. {
  636. struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
  637. struct xfrm_policy *xp;
  638. struct km_event c;
  639. int err;
  640. int excl;
  641. err = verify_newpolicy_info(p);
  642. if (err)
  643. return err;
  644. err = verify_sec_ctx_len((struct rtattr **)xfrma);
  645. if (err)
  646. return err;
  647. xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
  648. if (!xp)
  649. return err;
  650. /* shouldnt excl be based on nlh flags??
  651. * Aha! this is anti-netlink really i.e more pfkey derived
  652. * in netlink excl is a flag and you wouldnt need
  653. * a type XFRM_MSG_UPDPOLICY - JHS */
  654. excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
  655. err = xfrm_policy_insert(p->dir, xp, excl);
  656. if (err) {
  657. security_xfrm_policy_free(xp);
  658. kfree(xp);
  659. return err;
  660. }
  661. c.event = nlh->nlmsg_type;
  662. c.seq = nlh->nlmsg_seq;
  663. c.pid = nlh->nlmsg_pid;
  664. km_policy_notify(xp, p->dir, &c);
  665. xfrm_pol_put(xp);
  666. return 0;
  667. }
  668. static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
  669. {
  670. struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
  671. int i;
  672. if (xp->xfrm_nr == 0)
  673. return 0;
  674. for (i = 0; i < xp->xfrm_nr; i++) {
  675. struct xfrm_user_tmpl *up = &vec[i];
  676. struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
  677. memcpy(&up->id, &kp->id, sizeof(up->id));
  678. up->family = xp->family;
  679. memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
  680. up->reqid = kp->reqid;
  681. up->mode = kp->mode;
  682. up->share = kp->share;
  683. up->optional = kp->optional;
  684. up->aalgos = kp->aalgos;
  685. up->ealgos = kp->ealgos;
  686. up->calgos = kp->calgos;
  687. }
  688. RTA_PUT(skb, XFRMA_TMPL,
  689. (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
  690. vec);
  691. return 0;
  692. rtattr_failure:
  693. return -1;
  694. }
  695. static int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
  696. {
  697. if (xp->security) {
  698. int ctx_size = sizeof(struct xfrm_sec_ctx) +
  699. xp->security->ctx_len;
  700. struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
  701. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  702. uctx->exttype = XFRMA_SEC_CTX;
  703. uctx->len = ctx_size;
  704. uctx->ctx_doi = xp->security->ctx_doi;
  705. uctx->ctx_alg = xp->security->ctx_alg;
  706. uctx->ctx_len = xp->security->ctx_len;
  707. memcpy(uctx + 1, xp->security->ctx_str, xp->security->ctx_len);
  708. }
  709. return 0;
  710. rtattr_failure:
  711. return -1;
  712. }
  713. static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
  714. {
  715. struct xfrm_dump_info *sp = ptr;
  716. struct xfrm_userpolicy_info *p;
  717. struct sk_buff *in_skb = sp->in_skb;
  718. struct sk_buff *skb = sp->out_skb;
  719. struct nlmsghdr *nlh;
  720. unsigned char *b = skb->tail;
  721. if (sp->this_idx < sp->start_idx)
  722. goto out;
  723. nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid,
  724. sp->nlmsg_seq,
  725. XFRM_MSG_NEWPOLICY, sizeof(*p));
  726. p = NLMSG_DATA(nlh);
  727. nlh->nlmsg_flags = sp->nlmsg_flags;
  728. copy_to_user_policy(xp, p, dir);
  729. if (copy_to_user_tmpl(xp, skb) < 0)
  730. goto nlmsg_failure;
  731. if (copy_to_user_sec_ctx(xp, skb))
  732. goto nlmsg_failure;
  733. nlh->nlmsg_len = skb->tail - b;
  734. out:
  735. sp->this_idx++;
  736. return 0;
  737. nlmsg_failure:
  738. skb_trim(skb, b - skb->data);
  739. return -1;
  740. }
  741. static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
  742. {
  743. struct xfrm_dump_info info;
  744. info.in_skb = cb->skb;
  745. info.out_skb = skb;
  746. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  747. info.nlmsg_flags = NLM_F_MULTI;
  748. info.this_idx = 0;
  749. info.start_idx = cb->args[0];
  750. (void) xfrm_policy_walk(dump_one_policy, &info);
  751. cb->args[0] = info.this_idx;
  752. return skb->len;
  753. }
  754. static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
  755. struct xfrm_policy *xp,
  756. int dir, u32 seq)
  757. {
  758. struct xfrm_dump_info info;
  759. struct sk_buff *skb;
  760. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  761. if (!skb)
  762. return ERR_PTR(-ENOMEM);
  763. NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
  764. info.in_skb = in_skb;
  765. info.out_skb = skb;
  766. info.nlmsg_seq = seq;
  767. info.nlmsg_flags = 0;
  768. info.this_idx = info.start_idx = 0;
  769. if (dump_one_policy(xp, dir, 0, &info) < 0) {
  770. kfree_skb(skb);
  771. return NULL;
  772. }
  773. return skb;
  774. }
  775. static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  776. {
  777. struct xfrm_policy *xp;
  778. struct xfrm_userpolicy_id *p;
  779. int err;
  780. struct km_event c;
  781. int delete;
  782. p = NLMSG_DATA(nlh);
  783. delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
  784. err = verify_policy_dir(p->dir);
  785. if (err)
  786. return err;
  787. if (p->index)
  788. xp = xfrm_policy_byid(p->dir, p->index, delete);
  789. else {
  790. struct rtattr **rtattrs = (struct rtattr **)xfrma;
  791. struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
  792. struct xfrm_policy tmp;
  793. err = verify_sec_ctx_len(rtattrs);
  794. if (err)
  795. return err;
  796. memset(&tmp, 0, sizeof(struct xfrm_policy));
  797. if (rt) {
  798. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  799. if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
  800. return err;
  801. }
  802. xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
  803. security_xfrm_policy_free(&tmp);
  804. }
  805. if (xp == NULL)
  806. return -ENOENT;
  807. if (!delete) {
  808. struct sk_buff *resp_skb;
  809. resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
  810. if (IS_ERR(resp_skb)) {
  811. err = PTR_ERR(resp_skb);
  812. } else {
  813. err = netlink_unicast(xfrm_nl, resp_skb,
  814. NETLINK_CB(skb).pid,
  815. MSG_DONTWAIT);
  816. }
  817. } else {
  818. c.data.byid = p->index;
  819. c.event = nlh->nlmsg_type;
  820. c.seq = nlh->nlmsg_seq;
  821. c.pid = nlh->nlmsg_pid;
  822. km_policy_notify(xp, p->dir, &c);
  823. }
  824. xfrm_pol_put(xp);
  825. return err;
  826. }
  827. static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  828. {
  829. struct km_event c;
  830. struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
  831. xfrm_state_flush(p->proto);
  832. c.data.proto = p->proto;
  833. c.event = nlh->nlmsg_type;
  834. c.seq = nlh->nlmsg_seq;
  835. c.pid = nlh->nlmsg_pid;
  836. km_state_notify(NULL, &c);
  837. return 0;
  838. }
  839. static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
  840. {
  841. struct km_event c;
  842. xfrm_policy_flush();
  843. c.event = nlh->nlmsg_type;
  844. c.seq = nlh->nlmsg_seq;
  845. c.pid = nlh->nlmsg_pid;
  846. km_policy_notify(NULL, 0, &c);
  847. return 0;
  848. }
  849. #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
  850. static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
  851. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  852. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  853. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  854. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  855. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  856. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  857. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
  858. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
  859. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
  860. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  861. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  862. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
  863. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  864. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
  865. };
  866. #undef XMSGSIZE
  867. static struct xfrm_link {
  868. int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
  869. int (*dump)(struct sk_buff *, struct netlink_callback *);
  870. } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
  871. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  872. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
  873. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
  874. .dump = xfrm_dump_sa },
  875. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  876. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
  877. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
  878. .dump = xfrm_dump_policy },
  879. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
  880. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  881. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  882. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
  883. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
  884. };
  885. static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
  886. {
  887. struct rtattr *xfrma[XFRMA_MAX];
  888. struct xfrm_link *link;
  889. int type, min_len;
  890. if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
  891. return 0;
  892. type = nlh->nlmsg_type;
  893. /* A control message: ignore them */
  894. if (type < XFRM_MSG_BASE)
  895. return 0;
  896. /* Unknown message: reply with EINVAL */
  897. if (type > XFRM_MSG_MAX)
  898. goto err_einval;
  899. type -= XFRM_MSG_BASE;
  900. link = &xfrm_dispatch[type];
  901. /* All operations require privileges, even GET */
  902. if (security_netlink_recv(skb)) {
  903. *errp = -EPERM;
  904. return -1;
  905. }
  906. if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
  907. type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
  908. (nlh->nlmsg_flags & NLM_F_DUMP)) {
  909. if (link->dump == NULL)
  910. goto err_einval;
  911. if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
  912. link->dump, NULL)) != 0) {
  913. return -1;
  914. }
  915. netlink_queue_skip(nlh, skb);
  916. return -1;
  917. }
  918. memset(xfrma, 0, sizeof(xfrma));
  919. if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
  920. goto err_einval;
  921. if (nlh->nlmsg_len > min_len) {
  922. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  923. struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
  924. while (RTA_OK(attr, attrlen)) {
  925. unsigned short flavor = attr->rta_type;
  926. if (flavor) {
  927. if (flavor > XFRMA_MAX)
  928. goto err_einval;
  929. xfrma[flavor - 1] = attr;
  930. }
  931. attr = RTA_NEXT(attr, attrlen);
  932. }
  933. }
  934. if (link->doit == NULL)
  935. goto err_einval;
  936. *errp = link->doit(skb, nlh, (void **) &xfrma);
  937. return *errp;
  938. err_einval:
  939. *errp = -EINVAL;
  940. return -1;
  941. }
  942. static void xfrm_netlink_rcv(struct sock *sk, int len)
  943. {
  944. unsigned int qlen = 0;
  945. do {
  946. down(&xfrm_cfg_sem);
  947. netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
  948. up(&xfrm_cfg_sem);
  949. } while (qlen);
  950. }
  951. static int build_expire(struct sk_buff *skb, struct xfrm_state *x, int hard)
  952. {
  953. struct xfrm_user_expire *ue;
  954. struct nlmsghdr *nlh;
  955. unsigned char *b = skb->tail;
  956. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_EXPIRE,
  957. sizeof(*ue));
  958. ue = NLMSG_DATA(nlh);
  959. nlh->nlmsg_flags = 0;
  960. copy_to_user_state(x, &ue->state);
  961. ue->hard = (hard != 0) ? 1 : 0;
  962. nlh->nlmsg_len = skb->tail - b;
  963. return skb->len;
  964. nlmsg_failure:
  965. skb_trim(skb, b - skb->data);
  966. return -1;
  967. }
  968. static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
  969. {
  970. struct sk_buff *skb;
  971. int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
  972. skb = alloc_skb(len, GFP_ATOMIC);
  973. if (skb == NULL)
  974. return -ENOMEM;
  975. if (build_expire(skb, x, c->data.hard) < 0)
  976. BUG();
  977. NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
  978. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  979. }
  980. static int xfrm_notify_sa_flush(struct km_event *c)
  981. {
  982. struct xfrm_usersa_flush *p;
  983. struct nlmsghdr *nlh;
  984. struct sk_buff *skb;
  985. unsigned char *b;
  986. int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
  987. skb = alloc_skb(len, GFP_ATOMIC);
  988. if (skb == NULL)
  989. return -ENOMEM;
  990. b = skb->tail;
  991. nlh = NLMSG_PUT(skb, c->pid, c->seq,
  992. XFRM_MSG_FLUSHSA, sizeof(*p));
  993. nlh->nlmsg_flags = 0;
  994. p = NLMSG_DATA(nlh);
  995. p->proto = c->data.proto;
  996. nlh->nlmsg_len = skb->tail - b;
  997. NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
  998. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  999. nlmsg_failure:
  1000. kfree_skb(skb);
  1001. return -1;
  1002. }
  1003. static int inline xfrm_sa_len(struct xfrm_state *x)
  1004. {
  1005. int l = 0;
  1006. if (x->aalg)
  1007. l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
  1008. if (x->ealg)
  1009. l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
  1010. if (x->calg)
  1011. l += RTA_SPACE(sizeof(*x->calg));
  1012. if (x->encap)
  1013. l += RTA_SPACE(sizeof(*x->encap));
  1014. return l;
  1015. }
  1016. static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
  1017. {
  1018. struct xfrm_usersa_info *p;
  1019. struct xfrm_usersa_id *id;
  1020. struct nlmsghdr *nlh;
  1021. struct sk_buff *skb;
  1022. unsigned char *b;
  1023. int len = xfrm_sa_len(x);
  1024. int headlen;
  1025. headlen = sizeof(*p);
  1026. if (c->event == XFRM_MSG_DELSA) {
  1027. len += RTA_SPACE(headlen);
  1028. headlen = sizeof(*id);
  1029. }
  1030. len += NLMSG_SPACE(headlen);
  1031. skb = alloc_skb(len, GFP_ATOMIC);
  1032. if (skb == NULL)
  1033. return -ENOMEM;
  1034. b = skb->tail;
  1035. nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
  1036. nlh->nlmsg_flags = 0;
  1037. p = NLMSG_DATA(nlh);
  1038. if (c->event == XFRM_MSG_DELSA) {
  1039. id = NLMSG_DATA(nlh);
  1040. memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
  1041. id->spi = x->id.spi;
  1042. id->family = x->props.family;
  1043. id->proto = x->id.proto;
  1044. p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
  1045. }
  1046. copy_to_user_state(x, p);
  1047. if (x->aalg)
  1048. RTA_PUT(skb, XFRMA_ALG_AUTH,
  1049. sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
  1050. if (x->ealg)
  1051. RTA_PUT(skb, XFRMA_ALG_CRYPT,
  1052. sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
  1053. if (x->calg)
  1054. RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  1055. if (x->encap)
  1056. RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  1057. nlh->nlmsg_len = skb->tail - b;
  1058. NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
  1059. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  1060. nlmsg_failure:
  1061. rtattr_failure:
  1062. kfree_skb(skb);
  1063. return -1;
  1064. }
  1065. static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
  1066. {
  1067. switch (c->event) {
  1068. case XFRM_MSG_EXPIRE:
  1069. return xfrm_exp_state_notify(x, c);
  1070. case XFRM_MSG_DELSA:
  1071. case XFRM_MSG_UPDSA:
  1072. case XFRM_MSG_NEWSA:
  1073. return xfrm_notify_sa(x, c);
  1074. case XFRM_MSG_FLUSHSA:
  1075. return xfrm_notify_sa_flush(c);
  1076. default:
  1077. printk("xfrm_user: Unknown SA event %d\n", c->event);
  1078. break;
  1079. }
  1080. return 0;
  1081. }
  1082. static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
  1083. struct xfrm_tmpl *xt, struct xfrm_policy *xp,
  1084. int dir)
  1085. {
  1086. struct xfrm_user_acquire *ua;
  1087. struct nlmsghdr *nlh;
  1088. unsigned char *b = skb->tail;
  1089. __u32 seq = xfrm_get_acqseq();
  1090. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
  1091. sizeof(*ua));
  1092. ua = NLMSG_DATA(nlh);
  1093. nlh->nlmsg_flags = 0;
  1094. memcpy(&ua->id, &x->id, sizeof(ua->id));
  1095. memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
  1096. memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
  1097. copy_to_user_policy(xp, &ua->policy, dir);
  1098. ua->aalgos = xt->aalgos;
  1099. ua->ealgos = xt->ealgos;
  1100. ua->calgos = xt->calgos;
  1101. ua->seq = x->km.seq = seq;
  1102. if (copy_to_user_tmpl(xp, skb) < 0)
  1103. goto nlmsg_failure;
  1104. if (copy_to_user_sec_ctx(xp, skb))
  1105. goto nlmsg_failure;
  1106. nlh->nlmsg_len = skb->tail - b;
  1107. return skb->len;
  1108. nlmsg_failure:
  1109. skb_trim(skb, b - skb->data);
  1110. return -1;
  1111. }
  1112. static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
  1113. struct xfrm_policy *xp, int dir)
  1114. {
  1115. struct sk_buff *skb;
  1116. size_t len;
  1117. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1118. len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
  1119. len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
  1120. skb = alloc_skb(len, GFP_ATOMIC);
  1121. if (skb == NULL)
  1122. return -ENOMEM;
  1123. if (build_acquire(skb, x, xt, xp, dir) < 0)
  1124. BUG();
  1125. NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
  1126. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
  1127. }
  1128. /* User gives us xfrm_user_policy_info followed by an array of 0
  1129. * or more templates.
  1130. */
  1131. static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
  1132. u8 *data, int len, int *dir)
  1133. {
  1134. struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
  1135. struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
  1136. struct xfrm_policy *xp;
  1137. int nr;
  1138. switch (family) {
  1139. case AF_INET:
  1140. if (opt != IP_XFRM_POLICY) {
  1141. *dir = -EOPNOTSUPP;
  1142. return NULL;
  1143. }
  1144. break;
  1145. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1146. case AF_INET6:
  1147. if (opt != IPV6_XFRM_POLICY) {
  1148. *dir = -EOPNOTSUPP;
  1149. return NULL;
  1150. }
  1151. break;
  1152. #endif
  1153. default:
  1154. *dir = -EINVAL;
  1155. return NULL;
  1156. }
  1157. *dir = -EINVAL;
  1158. if (len < sizeof(*p) ||
  1159. verify_newpolicy_info(p))
  1160. return NULL;
  1161. nr = ((len - sizeof(*p)) / sizeof(*ut));
  1162. if (nr > XFRM_MAX_DEPTH)
  1163. return NULL;
  1164. if (p->dir > XFRM_POLICY_OUT)
  1165. return NULL;
  1166. xp = xfrm_policy_alloc(GFP_KERNEL);
  1167. if (xp == NULL) {
  1168. *dir = -ENOBUFS;
  1169. return NULL;
  1170. }
  1171. copy_from_user_policy(xp, p);
  1172. copy_templates(xp, ut, nr);
  1173. *dir = p->dir;
  1174. return xp;
  1175. }
  1176. static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
  1177. int dir, int hard)
  1178. {
  1179. struct xfrm_user_polexpire *upe;
  1180. struct nlmsghdr *nlh;
  1181. unsigned char *b = skb->tail;
  1182. nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
  1183. upe = NLMSG_DATA(nlh);
  1184. nlh->nlmsg_flags = 0;
  1185. copy_to_user_policy(xp, &upe->pol, dir);
  1186. if (copy_to_user_tmpl(xp, skb) < 0)
  1187. goto nlmsg_failure;
  1188. if (copy_to_user_sec_ctx(xp, skb))
  1189. goto nlmsg_failure;
  1190. upe->hard = !!hard;
  1191. nlh->nlmsg_len = skb->tail - b;
  1192. return skb->len;
  1193. nlmsg_failure:
  1194. skb_trim(skb, b - skb->data);
  1195. return -1;
  1196. }
  1197. static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  1198. {
  1199. struct sk_buff *skb;
  1200. size_t len;
  1201. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1202. len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
  1203. len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
  1204. skb = alloc_skb(len, GFP_ATOMIC);
  1205. if (skb == NULL)
  1206. return -ENOMEM;
  1207. if (build_polexpire(skb, xp, dir, c->data.hard) < 0)
  1208. BUG();
  1209. NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
  1210. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  1211. }
  1212. static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
  1213. {
  1214. struct xfrm_userpolicy_info *p;
  1215. struct xfrm_userpolicy_id *id;
  1216. struct nlmsghdr *nlh;
  1217. struct sk_buff *skb;
  1218. unsigned char *b;
  1219. int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1220. int headlen;
  1221. headlen = sizeof(*p);
  1222. if (c->event == XFRM_MSG_DELPOLICY) {
  1223. len += RTA_SPACE(headlen);
  1224. headlen = sizeof(*id);
  1225. }
  1226. len += NLMSG_SPACE(headlen);
  1227. skb = alloc_skb(len, GFP_ATOMIC);
  1228. if (skb == NULL)
  1229. return -ENOMEM;
  1230. b = skb->tail;
  1231. nlh = NLMSG_PUT(skb, c->pid, c->seq, c->event, headlen);
  1232. p = NLMSG_DATA(nlh);
  1233. if (c->event == XFRM_MSG_DELPOLICY) {
  1234. id = NLMSG_DATA(nlh);
  1235. memset(id, 0, sizeof(*id));
  1236. id->dir = dir;
  1237. if (c->data.byid)
  1238. id->index = xp->index;
  1239. else
  1240. memcpy(&id->sel, &xp->selector, sizeof(id->sel));
  1241. p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
  1242. }
  1243. nlh->nlmsg_flags = 0;
  1244. copy_to_user_policy(xp, p, dir);
  1245. if (copy_to_user_tmpl(xp, skb) < 0)
  1246. goto nlmsg_failure;
  1247. nlh->nlmsg_len = skb->tail - b;
  1248. NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
  1249. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  1250. nlmsg_failure:
  1251. rtattr_failure:
  1252. kfree_skb(skb);
  1253. return -1;
  1254. }
  1255. static int xfrm_notify_policy_flush(struct km_event *c)
  1256. {
  1257. struct nlmsghdr *nlh;
  1258. struct sk_buff *skb;
  1259. unsigned char *b;
  1260. int len = NLMSG_LENGTH(0);
  1261. skb = alloc_skb(len, GFP_ATOMIC);
  1262. if (skb == NULL)
  1263. return -ENOMEM;
  1264. b = skb->tail;
  1265. nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
  1266. nlh->nlmsg_len = skb->tail - b;
  1267. NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
  1268. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  1269. nlmsg_failure:
  1270. kfree_skb(skb);
  1271. return -1;
  1272. }
  1273. static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  1274. {
  1275. switch (c->event) {
  1276. case XFRM_MSG_NEWPOLICY:
  1277. case XFRM_MSG_UPDPOLICY:
  1278. case XFRM_MSG_DELPOLICY:
  1279. return xfrm_notify_policy(xp, dir, c);
  1280. case XFRM_MSG_FLUSHPOLICY:
  1281. return xfrm_notify_policy_flush(c);
  1282. case XFRM_MSG_POLEXPIRE:
  1283. return xfrm_exp_policy_notify(xp, dir, c);
  1284. default:
  1285. printk("xfrm_user: Unknown Policy event %d\n", c->event);
  1286. }
  1287. return 0;
  1288. }
  1289. static struct xfrm_mgr netlink_mgr = {
  1290. .id = "netlink",
  1291. .notify = xfrm_send_state_notify,
  1292. .acquire = xfrm_send_acquire,
  1293. .compile_policy = xfrm_compile_policy,
  1294. .notify_policy = xfrm_send_policy_notify,
  1295. };
  1296. static int __init xfrm_user_init(void)
  1297. {
  1298. printk(KERN_INFO "Initializing IPsec netlink socket\n");
  1299. xfrm_nl = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
  1300. xfrm_netlink_rcv, THIS_MODULE);
  1301. if (xfrm_nl == NULL)
  1302. return -ENOMEM;
  1303. xfrm_register_km(&netlink_mgr);
  1304. return 0;
  1305. }
  1306. static void __exit xfrm_user_exit(void)
  1307. {
  1308. xfrm_unregister_km(&netlink_mgr);
  1309. sock_release(xfrm_nl->sk_socket);
  1310. }
  1311. module_init(xfrm_user_init);
  1312. module_exit(xfrm_user_exit);
  1313. MODULE_LICENSE("GPL");
  1314. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);