xfrm_user.c 51 KB

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