xfrm_user.c 50 KB

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