xfrm_user.c 45 KB

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