xfrm_user.c 44 KB

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