xfrm_user.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553
  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_sec_ctx *xfrm_ctx)
  222. {
  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. * Set inner address family if the KM left it as zero.
  264. * See comment in validate_tmpl.
  265. */
  266. if (!x->sel.family)
  267. x->sel.family = p->family;
  268. }
  269. /*
  270. * someday when pfkey also has support, we could have the code
  271. * somehow made shareable and move it to xfrm_state.c - JHS
  272. *
  273. */
  274. static int xfrm_update_ae_params(struct xfrm_state *x, struct rtattr **xfrma)
  275. {
  276. int err = - EINVAL;
  277. struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
  278. struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
  279. struct rtattr *et = xfrma[XFRMA_ETIMER_THRESH-1];
  280. struct rtattr *rt = xfrma[XFRMA_REPLAY_THRESH-1];
  281. if (rp) {
  282. struct xfrm_replay_state *replay;
  283. if (RTA_PAYLOAD(rp) < sizeof(*replay))
  284. goto error;
  285. replay = RTA_DATA(rp);
  286. memcpy(&x->replay, replay, sizeof(*replay));
  287. memcpy(&x->preplay, replay, sizeof(*replay));
  288. }
  289. if (lt) {
  290. struct xfrm_lifetime_cur *ltime;
  291. if (RTA_PAYLOAD(lt) < sizeof(*ltime))
  292. goto error;
  293. ltime = RTA_DATA(lt);
  294. x->curlft.bytes = ltime->bytes;
  295. x->curlft.packets = ltime->packets;
  296. x->curlft.add_time = ltime->add_time;
  297. x->curlft.use_time = ltime->use_time;
  298. }
  299. if (et) {
  300. if (RTA_PAYLOAD(et) < sizeof(u32))
  301. goto error;
  302. x->replay_maxage = *(u32*)RTA_DATA(et);
  303. }
  304. if (rt) {
  305. if (RTA_PAYLOAD(rt) < sizeof(u32))
  306. goto error;
  307. x->replay_maxdiff = *(u32*)RTA_DATA(rt);
  308. }
  309. return 0;
  310. error:
  311. return err;
  312. }
  313. static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
  314. struct rtattr **xfrma,
  315. int *errp)
  316. {
  317. struct xfrm_state *x = xfrm_state_alloc();
  318. int err = -ENOMEM;
  319. if (!x)
  320. goto error_no_put;
  321. copy_from_user_state(x, p);
  322. if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
  323. xfrm_aalg_get_byname,
  324. xfrma[XFRMA_ALG_AUTH-1])))
  325. goto error;
  326. if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
  327. xfrm_ealg_get_byname,
  328. xfrma[XFRMA_ALG_CRYPT-1])))
  329. goto error;
  330. if ((err = attach_one_algo(&x->calg, &x->props.calgo,
  331. xfrm_calg_get_byname,
  332. xfrma[XFRMA_ALG_COMP-1])))
  333. goto error;
  334. if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
  335. goto error;
  336. if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
  337. goto error;
  338. err = xfrm_init_state(x);
  339. if (err)
  340. goto error;
  341. if ((err = attach_sec_ctx(x, xfrma[XFRMA_SEC_CTX-1])))
  342. goto error;
  343. x->km.seq = p->seq;
  344. x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
  345. /* sysctl_xfrm_aevent_etime is in 100ms units */
  346. x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
  347. x->preplay.bitmap = 0;
  348. x->preplay.seq = x->replay.seq+x->replay_maxdiff;
  349. x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
  350. /* override default values from above */
  351. err = xfrm_update_ae_params(x, (struct rtattr **)xfrma);
  352. if (err < 0)
  353. goto error;
  354. return x;
  355. error:
  356. x->km.state = XFRM_STATE_DEAD;
  357. xfrm_state_put(x);
  358. error_no_put:
  359. *errp = err;
  360. return NULL;
  361. }
  362. static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  363. struct rtattr **xfrma)
  364. {
  365. struct xfrm_usersa_info *p = nlmsg_data(nlh);
  366. struct xfrm_state *x;
  367. int err;
  368. struct km_event c;
  369. err = verify_newsa_info(p, xfrma);
  370. if (err)
  371. return err;
  372. x = xfrm_state_construct(p, xfrma, &err);
  373. if (!x)
  374. return err;
  375. xfrm_state_hold(x);
  376. if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
  377. err = xfrm_state_add(x);
  378. else
  379. err = xfrm_state_update(x);
  380. xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
  381. AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
  382. if (err < 0) {
  383. x->km.state = XFRM_STATE_DEAD;
  384. __xfrm_state_put(x);
  385. goto out;
  386. }
  387. c.seq = nlh->nlmsg_seq;
  388. c.pid = nlh->nlmsg_pid;
  389. c.event = nlh->nlmsg_type;
  390. km_state_notify(x, &c);
  391. out:
  392. xfrm_state_put(x);
  393. return err;
  394. }
  395. static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
  396. struct rtattr **xfrma,
  397. int *errp)
  398. {
  399. struct xfrm_state *x = NULL;
  400. int err;
  401. if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
  402. err = -ESRCH;
  403. x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
  404. } else {
  405. xfrm_address_t *saddr = NULL;
  406. err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
  407. if (err)
  408. goto out;
  409. if (!saddr) {
  410. err = -EINVAL;
  411. goto out;
  412. }
  413. err = -ESRCH;
  414. x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
  415. p->family);
  416. }
  417. out:
  418. if (!x && errp)
  419. *errp = err;
  420. return x;
  421. }
  422. static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  423. struct rtattr **xfrma)
  424. {
  425. struct xfrm_state *x;
  426. int err = -ESRCH;
  427. struct km_event c;
  428. struct xfrm_usersa_id *p = nlmsg_data(nlh);
  429. x = xfrm_user_state_lookup(p, xfrma, &err);
  430. if (x == NULL)
  431. return err;
  432. if ((err = security_xfrm_state_delete(x)) != 0)
  433. goto out;
  434. if (xfrm_state_kern(x)) {
  435. err = -EPERM;
  436. goto out;
  437. }
  438. err = xfrm_state_delete(x);
  439. if (err < 0)
  440. goto out;
  441. c.seq = nlh->nlmsg_seq;
  442. c.pid = nlh->nlmsg_pid;
  443. c.event = nlh->nlmsg_type;
  444. km_state_notify(x, &c);
  445. out:
  446. xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
  447. AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
  448. xfrm_state_put(x);
  449. return err;
  450. }
  451. static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  452. {
  453. memcpy(&p->id, &x->id, sizeof(p->id));
  454. memcpy(&p->sel, &x->sel, sizeof(p->sel));
  455. memcpy(&p->lft, &x->lft, sizeof(p->lft));
  456. memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
  457. memcpy(&p->stats, &x->stats, sizeof(p->stats));
  458. memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
  459. p->mode = x->props.mode;
  460. p->replay_window = x->props.replay_window;
  461. p->reqid = x->props.reqid;
  462. p->family = x->props.family;
  463. p->flags = x->props.flags;
  464. p->seq = x->km.seq;
  465. }
  466. struct xfrm_dump_info {
  467. struct sk_buff *in_skb;
  468. struct sk_buff *out_skb;
  469. u32 nlmsg_seq;
  470. u16 nlmsg_flags;
  471. int start_idx;
  472. int this_idx;
  473. };
  474. static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
  475. {
  476. struct xfrm_dump_info *sp = ptr;
  477. struct sk_buff *in_skb = sp->in_skb;
  478. struct sk_buff *skb = sp->out_skb;
  479. struct xfrm_usersa_info *p;
  480. struct nlmsghdr *nlh;
  481. if (sp->this_idx < sp->start_idx)
  482. goto out;
  483. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
  484. XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
  485. if (nlh == NULL)
  486. return -EMSGSIZE;
  487. p = nlmsg_data(nlh);
  488. copy_to_user_state(x, p);
  489. if (x->aalg)
  490. RTA_PUT(skb, XFRMA_ALG_AUTH,
  491. sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
  492. if (x->ealg)
  493. RTA_PUT(skb, XFRMA_ALG_CRYPT,
  494. sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
  495. if (x->calg)
  496. RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  497. if (x->encap)
  498. RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  499. if (x->security) {
  500. int ctx_size = sizeof(struct xfrm_sec_ctx) +
  501. x->security->ctx_len;
  502. struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
  503. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  504. uctx->exttype = XFRMA_SEC_CTX;
  505. uctx->len = ctx_size;
  506. uctx->ctx_doi = x->security->ctx_doi;
  507. uctx->ctx_alg = x->security->ctx_alg;
  508. uctx->ctx_len = x->security->ctx_len;
  509. memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
  510. }
  511. if (x->coaddr)
  512. RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
  513. if (x->lastused)
  514. RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
  515. nlmsg_end(skb, nlh);
  516. out:
  517. sp->this_idx++;
  518. return 0;
  519. rtattr_failure:
  520. nlmsg_cancel(skb, nlh);
  521. return -EMSGSIZE;
  522. }
  523. static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
  524. {
  525. struct xfrm_dump_info info;
  526. info.in_skb = cb->skb;
  527. info.out_skb = skb;
  528. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  529. info.nlmsg_flags = NLM_F_MULTI;
  530. info.this_idx = 0;
  531. info.start_idx = cb->args[0];
  532. (void) xfrm_state_walk(0, dump_one_state, &info);
  533. cb->args[0] = info.this_idx;
  534. return skb->len;
  535. }
  536. static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
  537. struct xfrm_state *x, u32 seq)
  538. {
  539. struct xfrm_dump_info info;
  540. struct sk_buff *skb;
  541. skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
  542. if (!skb)
  543. return ERR_PTR(-ENOMEM);
  544. info.in_skb = in_skb;
  545. info.out_skb = skb;
  546. info.nlmsg_seq = seq;
  547. info.nlmsg_flags = 0;
  548. info.this_idx = info.start_idx = 0;
  549. if (dump_one_state(x, 0, &info)) {
  550. kfree_skb(skb);
  551. return NULL;
  552. }
  553. return skb;
  554. }
  555. static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
  556. {
  557. struct xfrmk_spdinfo si;
  558. struct xfrmu_spdinfo spc;
  559. struct xfrmu_spdhinfo sph;
  560. struct nlmsghdr *nlh;
  561. u32 *f;
  562. nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
  563. if (nlh == NULL) /* shouldnt really happen ... */
  564. return -EMSGSIZE;
  565. f = nlmsg_data(nlh);
  566. *f = flags;
  567. xfrm_spd_getinfo(&si);
  568. spc.incnt = si.incnt;
  569. spc.outcnt = si.outcnt;
  570. spc.fwdcnt = si.fwdcnt;
  571. spc.inscnt = si.inscnt;
  572. spc.outscnt = si.outscnt;
  573. spc.fwdscnt = si.fwdscnt;
  574. sph.spdhcnt = si.spdhcnt;
  575. sph.spdhmcnt = si.spdhmcnt;
  576. NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
  577. NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
  578. return nlmsg_end(skb, nlh);
  579. nla_put_failure:
  580. nlmsg_cancel(skb, nlh);
  581. return -EMSGSIZE;
  582. }
  583. static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  584. struct rtattr **xfrma)
  585. {
  586. struct sk_buff *r_skb;
  587. u32 *flags = nlmsg_data(nlh);
  588. u32 spid = NETLINK_CB(skb).pid;
  589. u32 seq = nlh->nlmsg_seq;
  590. int len = NLMSG_LENGTH(sizeof(u32));
  591. len += RTA_SPACE(sizeof(struct xfrmu_spdinfo));
  592. len += RTA_SPACE(sizeof(struct xfrmu_spdhinfo));
  593. r_skb = alloc_skb(len, GFP_ATOMIC);
  594. if (r_skb == NULL)
  595. return -ENOMEM;
  596. if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
  597. BUG();
  598. return nlmsg_unicast(xfrm_nl, r_skb, spid);
  599. }
  600. static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
  601. {
  602. struct xfrmk_sadinfo si;
  603. struct xfrmu_sadhinfo sh;
  604. struct nlmsghdr *nlh;
  605. u32 *f;
  606. nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
  607. if (nlh == NULL) /* shouldnt really happen ... */
  608. return -EMSGSIZE;
  609. f = nlmsg_data(nlh);
  610. *f = flags;
  611. xfrm_sad_getinfo(&si);
  612. sh.sadhmcnt = si.sadhmcnt;
  613. sh.sadhcnt = si.sadhcnt;
  614. NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
  615. NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
  616. return nlmsg_end(skb, nlh);
  617. nla_put_failure:
  618. nlmsg_cancel(skb, nlh);
  619. return -EMSGSIZE;
  620. }
  621. static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  622. struct rtattr **xfrma)
  623. {
  624. struct sk_buff *r_skb;
  625. u32 *flags = nlmsg_data(nlh);
  626. u32 spid = NETLINK_CB(skb).pid;
  627. u32 seq = nlh->nlmsg_seq;
  628. int len = NLMSG_LENGTH(sizeof(u32));
  629. len += RTA_SPACE(sizeof(struct xfrmu_sadhinfo));
  630. len += RTA_SPACE(sizeof(u32));
  631. r_skb = alloc_skb(len, GFP_ATOMIC);
  632. if (r_skb == NULL)
  633. return -ENOMEM;
  634. if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
  635. BUG();
  636. return nlmsg_unicast(xfrm_nl, r_skb, spid);
  637. }
  638. static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  639. struct rtattr **xfrma)
  640. {
  641. struct xfrm_usersa_id *p = nlmsg_data(nlh);
  642. struct xfrm_state *x;
  643. struct sk_buff *resp_skb;
  644. int err = -ESRCH;
  645. x = xfrm_user_state_lookup(p, xfrma, &err);
  646. if (x == NULL)
  647. goto out_noput;
  648. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  649. if (IS_ERR(resp_skb)) {
  650. err = PTR_ERR(resp_skb);
  651. } else {
  652. err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
  653. }
  654. xfrm_state_put(x);
  655. out_noput:
  656. return err;
  657. }
  658. static int verify_userspi_info(struct xfrm_userspi_info *p)
  659. {
  660. switch (p->info.id.proto) {
  661. case IPPROTO_AH:
  662. case IPPROTO_ESP:
  663. break;
  664. case IPPROTO_COMP:
  665. /* IPCOMP spi is 16-bits. */
  666. if (p->max >= 0x10000)
  667. return -EINVAL;
  668. break;
  669. default:
  670. return -EINVAL;
  671. }
  672. if (p->min > p->max)
  673. return -EINVAL;
  674. return 0;
  675. }
  676. static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
  677. struct rtattr **xfrma)
  678. {
  679. struct xfrm_state *x;
  680. struct xfrm_userspi_info *p;
  681. struct sk_buff *resp_skb;
  682. xfrm_address_t *daddr;
  683. int family;
  684. int err;
  685. p = nlmsg_data(nlh);
  686. err = verify_userspi_info(p);
  687. if (err)
  688. goto out_noput;
  689. family = p->info.family;
  690. daddr = &p->info.id.daddr;
  691. x = NULL;
  692. if (p->info.seq) {
  693. x = xfrm_find_acq_byseq(p->info.seq);
  694. if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
  695. xfrm_state_put(x);
  696. x = NULL;
  697. }
  698. }
  699. if (!x)
  700. x = xfrm_find_acq(p->info.mode, p->info.reqid,
  701. p->info.id.proto, daddr,
  702. &p->info.saddr, 1,
  703. family);
  704. err = -ENOENT;
  705. if (x == NULL)
  706. goto out_noput;
  707. resp_skb = ERR_PTR(-ENOENT);
  708. spin_lock_bh(&x->lock);
  709. if (x->km.state != XFRM_STATE_DEAD) {
  710. xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
  711. if (x->id.spi)
  712. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  713. }
  714. spin_unlock_bh(&x->lock);
  715. if (IS_ERR(resp_skb)) {
  716. err = PTR_ERR(resp_skb);
  717. goto out;
  718. }
  719. err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
  720. out:
  721. xfrm_state_put(x);
  722. out_noput:
  723. return err;
  724. }
  725. static int verify_policy_dir(u8 dir)
  726. {
  727. switch (dir) {
  728. case XFRM_POLICY_IN:
  729. case XFRM_POLICY_OUT:
  730. case XFRM_POLICY_FWD:
  731. break;
  732. default:
  733. return -EINVAL;
  734. }
  735. return 0;
  736. }
  737. static int verify_policy_type(u8 type)
  738. {
  739. switch (type) {
  740. case XFRM_POLICY_TYPE_MAIN:
  741. #ifdef CONFIG_XFRM_SUB_POLICY
  742. case XFRM_POLICY_TYPE_SUB:
  743. #endif
  744. break;
  745. default:
  746. return -EINVAL;
  747. }
  748. return 0;
  749. }
  750. static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
  751. {
  752. switch (p->share) {
  753. case XFRM_SHARE_ANY:
  754. case XFRM_SHARE_SESSION:
  755. case XFRM_SHARE_USER:
  756. case XFRM_SHARE_UNIQUE:
  757. break;
  758. default:
  759. return -EINVAL;
  760. }
  761. switch (p->action) {
  762. case XFRM_POLICY_ALLOW:
  763. case XFRM_POLICY_BLOCK:
  764. break;
  765. default:
  766. return -EINVAL;
  767. }
  768. switch (p->sel.family) {
  769. case AF_INET:
  770. break;
  771. case AF_INET6:
  772. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  773. break;
  774. #else
  775. return -EAFNOSUPPORT;
  776. #endif
  777. default:
  778. return -EINVAL;
  779. }
  780. return verify_policy_dir(p->dir);
  781. }
  782. static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
  783. {
  784. struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
  785. struct xfrm_user_sec_ctx *uctx;
  786. if (!rt)
  787. return 0;
  788. uctx = RTA_DATA(rt);
  789. return security_xfrm_policy_alloc(pol, uctx);
  790. }
  791. static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
  792. int nr)
  793. {
  794. int i;
  795. xp->xfrm_nr = nr;
  796. for (i = 0; i < nr; i++, ut++) {
  797. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  798. memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
  799. memcpy(&t->saddr, &ut->saddr,
  800. sizeof(xfrm_address_t));
  801. t->reqid = ut->reqid;
  802. t->mode = ut->mode;
  803. t->share = ut->share;
  804. t->optional = ut->optional;
  805. t->aalgos = ut->aalgos;
  806. t->ealgos = ut->ealgos;
  807. t->calgos = ut->calgos;
  808. t->encap_family = ut->family;
  809. }
  810. }
  811. static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
  812. {
  813. int i;
  814. if (nr > XFRM_MAX_DEPTH)
  815. return -EINVAL;
  816. for (i = 0; i < nr; i++) {
  817. /* We never validated the ut->family value, so many
  818. * applications simply leave it at zero. The check was
  819. * never made and ut->family was ignored because all
  820. * templates could be assumed to have the same family as
  821. * the policy itself. Now that we will have ipv4-in-ipv6
  822. * and ipv6-in-ipv4 tunnels, this is no longer true.
  823. */
  824. if (!ut[i].family)
  825. ut[i].family = family;
  826. switch (ut[i].family) {
  827. case AF_INET:
  828. break;
  829. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  830. case AF_INET6:
  831. break;
  832. #endif
  833. default:
  834. return -EINVAL;
  835. }
  836. }
  837. return 0;
  838. }
  839. static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
  840. {
  841. struct rtattr *rt = xfrma[XFRMA_TMPL-1];
  842. if (!rt) {
  843. pol->xfrm_nr = 0;
  844. } else {
  845. struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
  846. int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
  847. int err;
  848. err = validate_tmpl(nr, utmpl, pol->family);
  849. if (err)
  850. return err;
  851. copy_templates(pol, RTA_DATA(rt), nr);
  852. }
  853. return 0;
  854. }
  855. static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
  856. {
  857. struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
  858. struct xfrm_userpolicy_type *upt;
  859. u8 type = XFRM_POLICY_TYPE_MAIN;
  860. int err;
  861. if (rt) {
  862. if (rt->rta_len < sizeof(*upt))
  863. return -EINVAL;
  864. upt = RTA_DATA(rt);
  865. type = upt->type;
  866. }
  867. err = verify_policy_type(type);
  868. if (err)
  869. return err;
  870. *tp = type;
  871. return 0;
  872. }
  873. static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
  874. {
  875. xp->priority = p->priority;
  876. xp->index = p->index;
  877. memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
  878. memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
  879. xp->action = p->action;
  880. xp->flags = p->flags;
  881. xp->family = p->sel.family;
  882. /* XXX xp->share = p->share; */
  883. }
  884. static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
  885. {
  886. memcpy(&p->sel, &xp->selector, sizeof(p->sel));
  887. memcpy(&p->lft, &xp->lft, sizeof(p->lft));
  888. memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
  889. p->priority = xp->priority;
  890. p->index = xp->index;
  891. p->sel.family = xp->family;
  892. p->dir = dir;
  893. p->action = xp->action;
  894. p->flags = xp->flags;
  895. p->share = XFRM_SHARE_ANY; /* XXX xp->share */
  896. }
  897. static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
  898. {
  899. struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
  900. int err;
  901. if (!xp) {
  902. *errp = -ENOMEM;
  903. return NULL;
  904. }
  905. copy_from_user_policy(xp, p);
  906. err = copy_from_user_policy_type(&xp->type, xfrma);
  907. if (err)
  908. goto error;
  909. if (!(err = copy_from_user_tmpl(xp, xfrma)))
  910. err = copy_from_user_sec_ctx(xp, xfrma);
  911. if (err)
  912. goto error;
  913. return xp;
  914. error:
  915. *errp = err;
  916. kfree(xp);
  917. return NULL;
  918. }
  919. static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  920. struct rtattr **xfrma)
  921. {
  922. struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
  923. struct xfrm_policy *xp;
  924. struct km_event c;
  925. int err;
  926. int excl;
  927. err = verify_newpolicy_info(p);
  928. if (err)
  929. return err;
  930. err = verify_sec_ctx_len(xfrma);
  931. if (err)
  932. return err;
  933. xp = xfrm_policy_construct(p, xfrma, &err);
  934. if (!xp)
  935. return err;
  936. /* shouldnt excl be based on nlh flags??
  937. * Aha! this is anti-netlink really i.e more pfkey derived
  938. * in netlink excl is a flag and you wouldnt need
  939. * a type XFRM_MSG_UPDPOLICY - JHS */
  940. excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
  941. err = xfrm_policy_insert(p->dir, xp, excl);
  942. xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
  943. AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
  944. if (err) {
  945. security_xfrm_policy_free(xp);
  946. kfree(xp);
  947. return err;
  948. }
  949. c.event = nlh->nlmsg_type;
  950. c.seq = nlh->nlmsg_seq;
  951. c.pid = nlh->nlmsg_pid;
  952. km_policy_notify(xp, p->dir, &c);
  953. xfrm_pol_put(xp);
  954. return 0;
  955. }
  956. static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
  957. {
  958. struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
  959. int i;
  960. if (xp->xfrm_nr == 0)
  961. return 0;
  962. for (i = 0; i < xp->xfrm_nr; i++) {
  963. struct xfrm_user_tmpl *up = &vec[i];
  964. struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
  965. memcpy(&up->id, &kp->id, sizeof(up->id));
  966. up->family = kp->encap_family;
  967. memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
  968. up->reqid = kp->reqid;
  969. up->mode = kp->mode;
  970. up->share = kp->share;
  971. up->optional = kp->optional;
  972. up->aalgos = kp->aalgos;
  973. up->ealgos = kp->ealgos;
  974. up->calgos = kp->calgos;
  975. }
  976. RTA_PUT(skb, XFRMA_TMPL,
  977. (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
  978. vec);
  979. return 0;
  980. rtattr_failure:
  981. return -1;
  982. }
  983. static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
  984. {
  985. int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
  986. struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
  987. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  988. uctx->exttype = XFRMA_SEC_CTX;
  989. uctx->len = ctx_size;
  990. uctx->ctx_doi = s->ctx_doi;
  991. uctx->ctx_alg = s->ctx_alg;
  992. uctx->ctx_len = s->ctx_len;
  993. memcpy(uctx + 1, s->ctx_str, s->ctx_len);
  994. return 0;
  995. rtattr_failure:
  996. return -1;
  997. }
  998. static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
  999. {
  1000. if (x->security) {
  1001. return copy_sec_ctx(x->security, skb);
  1002. }
  1003. return 0;
  1004. }
  1005. static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
  1006. {
  1007. if (xp->security) {
  1008. return copy_sec_ctx(xp->security, skb);
  1009. }
  1010. return 0;
  1011. }
  1012. #ifdef CONFIG_XFRM_SUB_POLICY
  1013. static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1014. {
  1015. struct xfrm_userpolicy_type upt;
  1016. memset(&upt, 0, sizeof(upt));
  1017. upt.type = type;
  1018. RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
  1019. return 0;
  1020. rtattr_failure:
  1021. return -1;
  1022. }
  1023. #else
  1024. static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1025. {
  1026. return 0;
  1027. }
  1028. #endif
  1029. static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
  1030. {
  1031. struct xfrm_dump_info *sp = ptr;
  1032. struct xfrm_userpolicy_info *p;
  1033. struct sk_buff *in_skb = sp->in_skb;
  1034. struct sk_buff *skb = sp->out_skb;
  1035. struct nlmsghdr *nlh;
  1036. if (sp->this_idx < sp->start_idx)
  1037. goto out;
  1038. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
  1039. XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
  1040. if (nlh == NULL)
  1041. return -EMSGSIZE;
  1042. p = nlmsg_data(nlh);
  1043. copy_to_user_policy(xp, p, dir);
  1044. if (copy_to_user_tmpl(xp, skb) < 0)
  1045. goto nlmsg_failure;
  1046. if (copy_to_user_sec_ctx(xp, skb))
  1047. goto nlmsg_failure;
  1048. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1049. goto nlmsg_failure;
  1050. nlmsg_end(skb, nlh);
  1051. out:
  1052. sp->this_idx++;
  1053. return 0;
  1054. nlmsg_failure:
  1055. nlmsg_cancel(skb, nlh);
  1056. return -EMSGSIZE;
  1057. }
  1058. static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
  1059. {
  1060. struct xfrm_dump_info info;
  1061. info.in_skb = cb->skb;
  1062. info.out_skb = skb;
  1063. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  1064. info.nlmsg_flags = NLM_F_MULTI;
  1065. info.this_idx = 0;
  1066. info.start_idx = cb->args[0];
  1067. (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
  1068. #ifdef CONFIG_XFRM_SUB_POLICY
  1069. (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
  1070. #endif
  1071. cb->args[0] = info.this_idx;
  1072. return skb->len;
  1073. }
  1074. static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
  1075. struct xfrm_policy *xp,
  1076. int dir, u32 seq)
  1077. {
  1078. struct xfrm_dump_info info;
  1079. struct sk_buff *skb;
  1080. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1081. if (!skb)
  1082. return ERR_PTR(-ENOMEM);
  1083. info.in_skb = in_skb;
  1084. info.out_skb = skb;
  1085. info.nlmsg_seq = seq;
  1086. info.nlmsg_flags = 0;
  1087. info.this_idx = info.start_idx = 0;
  1088. if (dump_one_policy(xp, dir, 0, &info) < 0) {
  1089. kfree_skb(skb);
  1090. return NULL;
  1091. }
  1092. return skb;
  1093. }
  1094. static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1095. struct rtattr **xfrma)
  1096. {
  1097. struct xfrm_policy *xp;
  1098. struct xfrm_userpolicy_id *p;
  1099. u8 type = XFRM_POLICY_TYPE_MAIN;
  1100. int err;
  1101. struct km_event c;
  1102. int delete;
  1103. p = nlmsg_data(nlh);
  1104. delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
  1105. err = copy_from_user_policy_type(&type, xfrma);
  1106. if (err)
  1107. return err;
  1108. err = verify_policy_dir(p->dir);
  1109. if (err)
  1110. return err;
  1111. if (p->index)
  1112. xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
  1113. else {
  1114. struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
  1115. struct xfrm_policy tmp;
  1116. err = verify_sec_ctx_len(xfrma);
  1117. if (err)
  1118. return err;
  1119. memset(&tmp, 0, sizeof(struct xfrm_policy));
  1120. if (rt) {
  1121. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  1122. if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
  1123. return err;
  1124. }
  1125. xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
  1126. delete, &err);
  1127. security_xfrm_policy_free(&tmp);
  1128. }
  1129. if (xp == NULL)
  1130. return -ENOENT;
  1131. if (!delete) {
  1132. struct sk_buff *resp_skb;
  1133. resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
  1134. if (IS_ERR(resp_skb)) {
  1135. err = PTR_ERR(resp_skb);
  1136. } else {
  1137. err = nlmsg_unicast(xfrm_nl, resp_skb,
  1138. NETLINK_CB(skb).pid);
  1139. }
  1140. } else {
  1141. xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
  1142. AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
  1143. if (err != 0)
  1144. goto out;
  1145. c.data.byid = p->index;
  1146. c.event = nlh->nlmsg_type;
  1147. c.seq = nlh->nlmsg_seq;
  1148. c.pid = nlh->nlmsg_pid;
  1149. km_policy_notify(xp, p->dir, &c);
  1150. }
  1151. out:
  1152. xfrm_pol_put(xp);
  1153. return err;
  1154. }
  1155. static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  1156. struct rtattr **xfrma)
  1157. {
  1158. struct km_event c;
  1159. struct xfrm_usersa_flush *p = nlmsg_data(nlh);
  1160. struct xfrm_audit audit_info;
  1161. int err;
  1162. audit_info.loginuid = NETLINK_CB(skb).loginuid;
  1163. audit_info.secid = NETLINK_CB(skb).sid;
  1164. err = xfrm_state_flush(p->proto, &audit_info);
  1165. if (err)
  1166. return err;
  1167. c.data.proto = p->proto;
  1168. c.event = nlh->nlmsg_type;
  1169. c.seq = nlh->nlmsg_seq;
  1170. c.pid = nlh->nlmsg_pid;
  1171. km_state_notify(NULL, &c);
  1172. return 0;
  1173. }
  1174. static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
  1175. {
  1176. struct xfrm_aevent_id *id;
  1177. struct nlmsghdr *nlh;
  1178. struct xfrm_lifetime_cur ltime;
  1179. nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
  1180. if (nlh == NULL)
  1181. return -EMSGSIZE;
  1182. id = nlmsg_data(nlh);
  1183. memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
  1184. id->sa_id.spi = x->id.spi;
  1185. id->sa_id.family = x->props.family;
  1186. id->sa_id.proto = x->id.proto;
  1187. memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
  1188. id->reqid = x->props.reqid;
  1189. id->flags = c->data.aevent;
  1190. RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
  1191. ltime.bytes = x->curlft.bytes;
  1192. ltime.packets = x->curlft.packets;
  1193. ltime.add_time = x->curlft.add_time;
  1194. ltime.use_time = x->curlft.use_time;
  1195. RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
  1196. if (id->flags&XFRM_AE_RTHR) {
  1197. RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
  1198. }
  1199. if (id->flags&XFRM_AE_ETHR) {
  1200. u32 etimer = x->replay_maxage*10/HZ;
  1201. RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
  1202. }
  1203. return nlmsg_end(skb, nlh);
  1204. rtattr_failure:
  1205. nlmsg_cancel(skb, nlh);
  1206. return -EMSGSIZE;
  1207. }
  1208. static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1209. struct rtattr **xfrma)
  1210. {
  1211. struct xfrm_state *x;
  1212. struct sk_buff *r_skb;
  1213. int err;
  1214. struct km_event c;
  1215. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  1216. int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
  1217. struct xfrm_usersa_id *id = &p->sa_id;
  1218. len += RTA_SPACE(sizeof(struct xfrm_replay_state));
  1219. len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
  1220. if (p->flags&XFRM_AE_RTHR)
  1221. len+=RTA_SPACE(sizeof(u32));
  1222. if (p->flags&XFRM_AE_ETHR)
  1223. len+=RTA_SPACE(sizeof(u32));
  1224. r_skb = alloc_skb(len, GFP_ATOMIC);
  1225. if (r_skb == NULL)
  1226. return -ENOMEM;
  1227. x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
  1228. if (x == NULL) {
  1229. kfree_skb(r_skb);
  1230. return -ESRCH;
  1231. }
  1232. /*
  1233. * XXX: is this lock really needed - none of the other
  1234. * gets lock (the concern is things getting updated
  1235. * while we are still reading) - jhs
  1236. */
  1237. spin_lock_bh(&x->lock);
  1238. c.data.aevent = p->flags;
  1239. c.seq = nlh->nlmsg_seq;
  1240. c.pid = nlh->nlmsg_pid;
  1241. if (build_aevent(r_skb, x, &c) < 0)
  1242. BUG();
  1243. err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
  1244. spin_unlock_bh(&x->lock);
  1245. xfrm_state_put(x);
  1246. return err;
  1247. }
  1248. static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1249. struct rtattr **xfrma)
  1250. {
  1251. struct xfrm_state *x;
  1252. struct km_event c;
  1253. int err = - EINVAL;
  1254. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  1255. struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
  1256. struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
  1257. if (!lt && !rp)
  1258. return err;
  1259. /* pedantic mode - thou shalt sayeth replaceth */
  1260. if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
  1261. return err;
  1262. x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
  1263. if (x == NULL)
  1264. return -ESRCH;
  1265. if (x->km.state != XFRM_STATE_VALID)
  1266. goto out;
  1267. spin_lock_bh(&x->lock);
  1268. err = xfrm_update_ae_params(x, xfrma);
  1269. spin_unlock_bh(&x->lock);
  1270. if (err < 0)
  1271. goto out;
  1272. c.event = nlh->nlmsg_type;
  1273. c.seq = nlh->nlmsg_seq;
  1274. c.pid = nlh->nlmsg_pid;
  1275. c.data.aevent = XFRM_AE_CU;
  1276. km_state_notify(x, &c);
  1277. err = 0;
  1278. out:
  1279. xfrm_state_put(x);
  1280. return err;
  1281. }
  1282. static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1283. struct rtattr **xfrma)
  1284. {
  1285. struct km_event c;
  1286. u8 type = XFRM_POLICY_TYPE_MAIN;
  1287. int err;
  1288. struct xfrm_audit audit_info;
  1289. err = copy_from_user_policy_type(&type, xfrma);
  1290. if (err)
  1291. return err;
  1292. audit_info.loginuid = NETLINK_CB(skb).loginuid;
  1293. audit_info.secid = NETLINK_CB(skb).sid;
  1294. err = xfrm_policy_flush(type, &audit_info);
  1295. if (err)
  1296. return err;
  1297. c.data.type = type;
  1298. c.event = nlh->nlmsg_type;
  1299. c.seq = nlh->nlmsg_seq;
  1300. c.pid = nlh->nlmsg_pid;
  1301. km_policy_notify(NULL, 0, &c);
  1302. return 0;
  1303. }
  1304. static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1305. struct rtattr **xfrma)
  1306. {
  1307. struct xfrm_policy *xp;
  1308. struct xfrm_user_polexpire *up = nlmsg_data(nlh);
  1309. struct xfrm_userpolicy_info *p = &up->pol;
  1310. u8 type = XFRM_POLICY_TYPE_MAIN;
  1311. int err = -ENOENT;
  1312. err = copy_from_user_policy_type(&type, xfrma);
  1313. if (err)
  1314. return err;
  1315. if (p->index)
  1316. xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
  1317. else {
  1318. struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
  1319. struct xfrm_policy tmp;
  1320. err = verify_sec_ctx_len(xfrma);
  1321. if (err)
  1322. return err;
  1323. memset(&tmp, 0, sizeof(struct xfrm_policy));
  1324. if (rt) {
  1325. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  1326. if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
  1327. return err;
  1328. }
  1329. xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
  1330. 0, &err);
  1331. security_xfrm_policy_free(&tmp);
  1332. }
  1333. if (xp == NULL)
  1334. return -ENOENT;
  1335. read_lock(&xp->lock);
  1336. if (xp->dead) {
  1337. read_unlock(&xp->lock);
  1338. goto out;
  1339. }
  1340. read_unlock(&xp->lock);
  1341. err = 0;
  1342. if (up->hard) {
  1343. xfrm_policy_delete(xp, p->dir);
  1344. xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
  1345. AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
  1346. } else {
  1347. // reset the timers here?
  1348. printk("Dont know what to do with soft policy expire\n");
  1349. }
  1350. km_policy_expired(xp, p->dir, up->hard, current->pid);
  1351. out:
  1352. xfrm_pol_put(xp);
  1353. return err;
  1354. }
  1355. static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1356. struct rtattr **xfrma)
  1357. {
  1358. struct xfrm_state *x;
  1359. int err;
  1360. struct xfrm_user_expire *ue = nlmsg_data(nlh);
  1361. struct xfrm_usersa_info *p = &ue->state;
  1362. x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
  1363. err = -ENOENT;
  1364. if (x == NULL)
  1365. return err;
  1366. spin_lock_bh(&x->lock);
  1367. err = -EINVAL;
  1368. if (x->km.state != XFRM_STATE_VALID)
  1369. goto out;
  1370. km_state_expired(x, ue->hard, current->pid);
  1371. if (ue->hard) {
  1372. __xfrm_state_delete(x);
  1373. xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
  1374. AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
  1375. }
  1376. err = 0;
  1377. out:
  1378. spin_unlock_bh(&x->lock);
  1379. xfrm_state_put(x);
  1380. return err;
  1381. }
  1382. static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1383. struct rtattr **xfrma)
  1384. {
  1385. struct xfrm_policy *xp;
  1386. struct xfrm_user_tmpl *ut;
  1387. int i;
  1388. struct rtattr *rt = xfrma[XFRMA_TMPL-1];
  1389. struct xfrm_user_acquire *ua = nlmsg_data(nlh);
  1390. struct xfrm_state *x = xfrm_state_alloc();
  1391. int err = -ENOMEM;
  1392. if (!x)
  1393. return err;
  1394. err = verify_newpolicy_info(&ua->policy);
  1395. if (err) {
  1396. printk("BAD policy passed\n");
  1397. kfree(x);
  1398. return err;
  1399. }
  1400. /* build an XP */
  1401. xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
  1402. if (!xp) {
  1403. kfree(x);
  1404. return err;
  1405. }
  1406. memcpy(&x->id, &ua->id, sizeof(ua->id));
  1407. memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
  1408. memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
  1409. ut = RTA_DATA(rt);
  1410. /* extract the templates and for each call km_key */
  1411. for (i = 0; i < xp->xfrm_nr; i++, ut++) {
  1412. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  1413. memcpy(&x->id, &t->id, sizeof(x->id));
  1414. x->props.mode = t->mode;
  1415. x->props.reqid = t->reqid;
  1416. x->props.family = ut->family;
  1417. t->aalgos = ua->aalgos;
  1418. t->ealgos = ua->ealgos;
  1419. t->calgos = ua->calgos;
  1420. err = km_query(x, t, xp);
  1421. }
  1422. kfree(x);
  1423. kfree(xp);
  1424. return 0;
  1425. }
  1426. #ifdef CONFIG_XFRM_MIGRATE
  1427. static int verify_user_migrate(struct rtattr **xfrma)
  1428. {
  1429. struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
  1430. struct xfrm_user_migrate *um;
  1431. if (!rt)
  1432. return -EINVAL;
  1433. if ((rt->rta_len - sizeof(*rt)) < sizeof(*um))
  1434. return -EINVAL;
  1435. return 0;
  1436. }
  1437. static int copy_from_user_migrate(struct xfrm_migrate *ma,
  1438. struct rtattr **xfrma, int *num)
  1439. {
  1440. struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
  1441. struct xfrm_user_migrate *um;
  1442. int i, num_migrate;
  1443. um = RTA_DATA(rt);
  1444. num_migrate = (rt->rta_len - sizeof(*rt)) / sizeof(*um);
  1445. if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
  1446. return -EINVAL;
  1447. for (i = 0; i < num_migrate; i++, um++, ma++) {
  1448. memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
  1449. memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
  1450. memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
  1451. memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
  1452. ma->proto = um->proto;
  1453. ma->mode = um->mode;
  1454. ma->reqid = um->reqid;
  1455. ma->old_family = um->old_family;
  1456. ma->new_family = um->new_family;
  1457. }
  1458. *num = i;
  1459. return 0;
  1460. }
  1461. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1462. struct rtattr **xfrma)
  1463. {
  1464. struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
  1465. struct xfrm_migrate m[XFRM_MAX_DEPTH];
  1466. u8 type;
  1467. int err;
  1468. int n = 0;
  1469. err = verify_user_migrate((struct rtattr **)xfrma);
  1470. if (err)
  1471. return err;
  1472. err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
  1473. if (err)
  1474. return err;
  1475. err = copy_from_user_migrate((struct xfrm_migrate *)m,
  1476. (struct rtattr **)xfrma, &n);
  1477. if (err)
  1478. return err;
  1479. if (!n)
  1480. return 0;
  1481. xfrm_migrate(&pi->sel, pi->dir, type, m, n);
  1482. return 0;
  1483. }
  1484. #else
  1485. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1486. struct rtattr **xfrma)
  1487. {
  1488. return -ENOPROTOOPT;
  1489. }
  1490. #endif
  1491. #ifdef CONFIG_XFRM_MIGRATE
  1492. static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
  1493. {
  1494. struct xfrm_user_migrate um;
  1495. memset(&um, 0, sizeof(um));
  1496. um.proto = m->proto;
  1497. um.mode = m->mode;
  1498. um.reqid = m->reqid;
  1499. um.old_family = m->old_family;
  1500. memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
  1501. memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
  1502. um.new_family = m->new_family;
  1503. memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
  1504. memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
  1505. RTA_PUT(skb, XFRMA_MIGRATE, sizeof(um), &um);
  1506. return 0;
  1507. rtattr_failure:
  1508. return -1;
  1509. }
  1510. static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
  1511. int num_migrate, struct xfrm_selector *sel,
  1512. u8 dir, u8 type)
  1513. {
  1514. struct xfrm_migrate *mp;
  1515. struct xfrm_userpolicy_id *pol_id;
  1516. struct nlmsghdr *nlh;
  1517. int i;
  1518. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
  1519. if (nlh == NULL)
  1520. return -EMSGSIZE;
  1521. pol_id = nlmsg_data(nlh);
  1522. /* copy data from selector, dir, and type to the pol_id */
  1523. memset(pol_id, 0, sizeof(*pol_id));
  1524. memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
  1525. pol_id->dir = dir;
  1526. if (copy_to_user_policy_type(type, skb) < 0)
  1527. goto nlmsg_failure;
  1528. for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
  1529. if (copy_to_user_migrate(mp, skb) < 0)
  1530. goto nlmsg_failure;
  1531. }
  1532. return nlmsg_end(skb, nlh);
  1533. nlmsg_failure:
  1534. nlmsg_cancel(skb, nlh);
  1535. return -EMSGSIZE;
  1536. }
  1537. static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  1538. struct xfrm_migrate *m, int num_migrate)
  1539. {
  1540. struct sk_buff *skb;
  1541. size_t len;
  1542. len = RTA_SPACE(sizeof(struct xfrm_user_migrate) * num_migrate);
  1543. len += NLMSG_SPACE(sizeof(struct xfrm_userpolicy_id));
  1544. #ifdef CONFIG_XFRM_SUB_POLICY
  1545. len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
  1546. #endif
  1547. skb = alloc_skb(len, GFP_ATOMIC);
  1548. if (skb == NULL)
  1549. return -ENOMEM;
  1550. /* build migrate */
  1551. if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
  1552. BUG();
  1553. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
  1554. }
  1555. #else
  1556. static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  1557. struct xfrm_migrate *m, int num_migrate)
  1558. {
  1559. return -ENOPROTOOPT;
  1560. }
  1561. #endif
  1562. #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
  1563. static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
  1564. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1565. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1566. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1567. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1568. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1569. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1570. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
  1571. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
  1572. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
  1573. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1574. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1575. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
  1576. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  1577. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
  1578. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1579. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1580. [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
  1581. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1582. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
  1583. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
  1584. };
  1585. #undef XMSGSIZE
  1586. static struct xfrm_link {
  1587. int (*doit)(struct sk_buff *, struct nlmsghdr *, struct rtattr **);
  1588. int (*dump)(struct sk_buff *, struct netlink_callback *);
  1589. } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
  1590. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1591. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
  1592. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
  1593. .dump = xfrm_dump_sa },
  1594. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1595. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
  1596. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
  1597. .dump = xfrm_dump_policy },
  1598. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
  1599. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
  1600. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
  1601. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1602. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1603. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
  1604. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
  1605. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
  1606. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
  1607. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
  1608. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
  1609. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
  1610. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
  1611. };
  1612. static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  1613. {
  1614. struct rtattr *xfrma[XFRMA_MAX];
  1615. struct xfrm_link *link;
  1616. int type, min_len;
  1617. type = nlh->nlmsg_type;
  1618. if (type > XFRM_MSG_MAX)
  1619. return -EINVAL;
  1620. type -= XFRM_MSG_BASE;
  1621. link = &xfrm_dispatch[type];
  1622. /* All operations require privileges, even GET */
  1623. if (security_netlink_recv(skb, CAP_NET_ADMIN))
  1624. return -EPERM;
  1625. if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
  1626. type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
  1627. (nlh->nlmsg_flags & NLM_F_DUMP)) {
  1628. if (link->dump == NULL)
  1629. return -EINVAL;
  1630. return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
  1631. }
  1632. memset(xfrma, 0, sizeof(xfrma));
  1633. if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
  1634. return -EINVAL;
  1635. if (nlh->nlmsg_len > min_len) {
  1636. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  1637. struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
  1638. while (RTA_OK(attr, attrlen)) {
  1639. unsigned short flavor = attr->rta_type;
  1640. if (flavor) {
  1641. if (flavor > XFRMA_MAX)
  1642. return -EINVAL;
  1643. xfrma[flavor - 1] = attr;
  1644. }
  1645. attr = RTA_NEXT(attr, attrlen);
  1646. }
  1647. }
  1648. if (link->doit == NULL)
  1649. return -EINVAL;
  1650. return link->doit(skb, nlh, xfrma);
  1651. }
  1652. static void xfrm_netlink_rcv(struct sock *sk, int len)
  1653. {
  1654. unsigned int qlen = 0;
  1655. do {
  1656. mutex_lock(&xfrm_cfg_mutex);
  1657. netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
  1658. mutex_unlock(&xfrm_cfg_mutex);
  1659. } while (qlen);
  1660. }
  1661. static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
  1662. {
  1663. struct xfrm_user_expire *ue;
  1664. struct nlmsghdr *nlh;
  1665. nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
  1666. if (nlh == NULL)
  1667. return -EMSGSIZE;
  1668. ue = nlmsg_data(nlh);
  1669. copy_to_user_state(x, &ue->state);
  1670. ue->hard = (c->data.hard != 0) ? 1 : 0;
  1671. return nlmsg_end(skb, nlh);
  1672. }
  1673. static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
  1674. {
  1675. struct sk_buff *skb;
  1676. int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
  1677. skb = alloc_skb(len, GFP_ATOMIC);
  1678. if (skb == NULL)
  1679. return -ENOMEM;
  1680. if (build_expire(skb, x, c) < 0)
  1681. BUG();
  1682. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  1683. }
  1684. static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
  1685. {
  1686. struct sk_buff *skb;
  1687. int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
  1688. len += RTA_SPACE(sizeof(struct xfrm_replay_state));
  1689. len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
  1690. skb = alloc_skb(len, GFP_ATOMIC);
  1691. if (skb == NULL)
  1692. return -ENOMEM;
  1693. if (build_aevent(skb, x, c) < 0)
  1694. BUG();
  1695. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
  1696. }
  1697. static int xfrm_notify_sa_flush(struct km_event *c)
  1698. {
  1699. struct xfrm_usersa_flush *p;
  1700. struct nlmsghdr *nlh;
  1701. struct sk_buff *skb;
  1702. int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
  1703. skb = alloc_skb(len, GFP_ATOMIC);
  1704. if (skb == NULL)
  1705. return -ENOMEM;
  1706. nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
  1707. if (nlh == NULL) {
  1708. kfree_skb(skb);
  1709. return -EMSGSIZE;
  1710. }
  1711. p = nlmsg_data(nlh);
  1712. p->proto = c->data.proto;
  1713. nlmsg_end(skb, nlh);
  1714. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  1715. }
  1716. static inline int xfrm_sa_len(struct xfrm_state *x)
  1717. {
  1718. int l = 0;
  1719. if (x->aalg)
  1720. l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
  1721. if (x->ealg)
  1722. l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
  1723. if (x->calg)
  1724. l += RTA_SPACE(sizeof(*x->calg));
  1725. if (x->encap)
  1726. l += RTA_SPACE(sizeof(*x->encap));
  1727. return l;
  1728. }
  1729. static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
  1730. {
  1731. struct xfrm_usersa_info *p;
  1732. struct xfrm_usersa_id *id;
  1733. struct nlmsghdr *nlh;
  1734. struct sk_buff *skb;
  1735. int len = xfrm_sa_len(x);
  1736. int headlen;
  1737. headlen = sizeof(*p);
  1738. if (c->event == XFRM_MSG_DELSA) {
  1739. len += RTA_SPACE(headlen);
  1740. headlen = sizeof(*id);
  1741. }
  1742. len += NLMSG_SPACE(headlen);
  1743. skb = alloc_skb(len, GFP_ATOMIC);
  1744. if (skb == NULL)
  1745. return -ENOMEM;
  1746. nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
  1747. if (nlh == NULL)
  1748. goto nlmsg_failure;
  1749. p = nlmsg_data(nlh);
  1750. if (c->event == XFRM_MSG_DELSA) {
  1751. id = nlmsg_data(nlh);
  1752. memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
  1753. id->spi = x->id.spi;
  1754. id->family = x->props.family;
  1755. id->proto = x->id.proto;
  1756. p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
  1757. }
  1758. copy_to_user_state(x, p);
  1759. if (x->aalg)
  1760. RTA_PUT(skb, XFRMA_ALG_AUTH,
  1761. sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
  1762. if (x->ealg)
  1763. RTA_PUT(skb, XFRMA_ALG_CRYPT,
  1764. sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
  1765. if (x->calg)
  1766. RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  1767. if (x->encap)
  1768. RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  1769. nlmsg_end(skb, nlh);
  1770. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  1771. nlmsg_failure:
  1772. rtattr_failure:
  1773. kfree_skb(skb);
  1774. return -1;
  1775. }
  1776. static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
  1777. {
  1778. switch (c->event) {
  1779. case XFRM_MSG_EXPIRE:
  1780. return xfrm_exp_state_notify(x, c);
  1781. case XFRM_MSG_NEWAE:
  1782. return xfrm_aevent_state_notify(x, c);
  1783. case XFRM_MSG_DELSA:
  1784. case XFRM_MSG_UPDSA:
  1785. case XFRM_MSG_NEWSA:
  1786. return xfrm_notify_sa(x, c);
  1787. case XFRM_MSG_FLUSHSA:
  1788. return xfrm_notify_sa_flush(c);
  1789. default:
  1790. printk("xfrm_user: Unknown SA event %d\n", c->event);
  1791. break;
  1792. }
  1793. return 0;
  1794. }
  1795. static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
  1796. struct xfrm_tmpl *xt, struct xfrm_policy *xp,
  1797. int dir)
  1798. {
  1799. struct xfrm_user_acquire *ua;
  1800. struct nlmsghdr *nlh;
  1801. __u32 seq = xfrm_get_acqseq();
  1802. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
  1803. if (nlh == NULL)
  1804. return -EMSGSIZE;
  1805. ua = nlmsg_data(nlh);
  1806. memcpy(&ua->id, &x->id, sizeof(ua->id));
  1807. memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
  1808. memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
  1809. copy_to_user_policy(xp, &ua->policy, dir);
  1810. ua->aalgos = xt->aalgos;
  1811. ua->ealgos = xt->ealgos;
  1812. ua->calgos = xt->calgos;
  1813. ua->seq = x->km.seq = seq;
  1814. if (copy_to_user_tmpl(xp, skb) < 0)
  1815. goto nlmsg_failure;
  1816. if (copy_to_user_state_sec_ctx(x, skb))
  1817. goto nlmsg_failure;
  1818. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1819. goto nlmsg_failure;
  1820. return nlmsg_end(skb, nlh);
  1821. nlmsg_failure:
  1822. nlmsg_cancel(skb, nlh);
  1823. return -EMSGSIZE;
  1824. }
  1825. static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
  1826. struct xfrm_policy *xp, int dir)
  1827. {
  1828. struct sk_buff *skb;
  1829. size_t len;
  1830. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1831. len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
  1832. len += RTA_SPACE(xfrm_user_sec_ctx_size(x->security));
  1833. #ifdef CONFIG_XFRM_SUB_POLICY
  1834. len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
  1835. #endif
  1836. skb = alloc_skb(len, GFP_ATOMIC);
  1837. if (skb == NULL)
  1838. return -ENOMEM;
  1839. if (build_acquire(skb, x, xt, xp, dir) < 0)
  1840. BUG();
  1841. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
  1842. }
  1843. /* User gives us xfrm_user_policy_info followed by an array of 0
  1844. * or more templates.
  1845. */
  1846. static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
  1847. u8 *data, int len, int *dir)
  1848. {
  1849. struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
  1850. struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
  1851. struct xfrm_policy *xp;
  1852. int nr;
  1853. switch (sk->sk_family) {
  1854. case AF_INET:
  1855. if (opt != IP_XFRM_POLICY) {
  1856. *dir = -EOPNOTSUPP;
  1857. return NULL;
  1858. }
  1859. break;
  1860. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1861. case AF_INET6:
  1862. if (opt != IPV6_XFRM_POLICY) {
  1863. *dir = -EOPNOTSUPP;
  1864. return NULL;
  1865. }
  1866. break;
  1867. #endif
  1868. default:
  1869. *dir = -EINVAL;
  1870. return NULL;
  1871. }
  1872. *dir = -EINVAL;
  1873. if (len < sizeof(*p) ||
  1874. verify_newpolicy_info(p))
  1875. return NULL;
  1876. nr = ((len - sizeof(*p)) / sizeof(*ut));
  1877. if (validate_tmpl(nr, ut, p->sel.family))
  1878. return NULL;
  1879. if (p->dir > XFRM_POLICY_OUT)
  1880. return NULL;
  1881. xp = xfrm_policy_alloc(GFP_KERNEL);
  1882. if (xp == NULL) {
  1883. *dir = -ENOBUFS;
  1884. return NULL;
  1885. }
  1886. copy_from_user_policy(xp, p);
  1887. xp->type = XFRM_POLICY_TYPE_MAIN;
  1888. copy_templates(xp, ut, nr);
  1889. *dir = p->dir;
  1890. return xp;
  1891. }
  1892. static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
  1893. int dir, struct km_event *c)
  1894. {
  1895. struct xfrm_user_polexpire *upe;
  1896. struct nlmsghdr *nlh;
  1897. int hard = c->data.hard;
  1898. nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
  1899. if (nlh == NULL)
  1900. return -EMSGSIZE;
  1901. upe = nlmsg_data(nlh);
  1902. copy_to_user_policy(xp, &upe->pol, dir);
  1903. if (copy_to_user_tmpl(xp, skb) < 0)
  1904. goto nlmsg_failure;
  1905. if (copy_to_user_sec_ctx(xp, skb))
  1906. goto nlmsg_failure;
  1907. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1908. goto nlmsg_failure;
  1909. upe->hard = !!hard;
  1910. return nlmsg_end(skb, nlh);
  1911. nlmsg_failure:
  1912. nlmsg_cancel(skb, nlh);
  1913. return -EMSGSIZE;
  1914. }
  1915. static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  1916. {
  1917. struct sk_buff *skb;
  1918. size_t len;
  1919. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1920. len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
  1921. len += RTA_SPACE(xfrm_user_sec_ctx_size(xp->security));
  1922. #ifdef CONFIG_XFRM_SUB_POLICY
  1923. len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
  1924. #endif
  1925. skb = alloc_skb(len, GFP_ATOMIC);
  1926. if (skb == NULL)
  1927. return -ENOMEM;
  1928. if (build_polexpire(skb, xp, dir, c) < 0)
  1929. BUG();
  1930. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  1931. }
  1932. static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
  1933. {
  1934. struct xfrm_userpolicy_info *p;
  1935. struct xfrm_userpolicy_id *id;
  1936. struct nlmsghdr *nlh;
  1937. struct sk_buff *skb;
  1938. int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1939. int headlen;
  1940. headlen = sizeof(*p);
  1941. if (c->event == XFRM_MSG_DELPOLICY) {
  1942. len += RTA_SPACE(headlen);
  1943. headlen = sizeof(*id);
  1944. }
  1945. #ifdef CONFIG_XFRM_SUB_POLICY
  1946. len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
  1947. #endif
  1948. len += NLMSG_SPACE(headlen);
  1949. skb = alloc_skb(len, GFP_ATOMIC);
  1950. if (skb == NULL)
  1951. return -ENOMEM;
  1952. nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
  1953. if (nlh == NULL)
  1954. goto nlmsg_failure;
  1955. p = nlmsg_data(nlh);
  1956. if (c->event == XFRM_MSG_DELPOLICY) {
  1957. id = nlmsg_data(nlh);
  1958. memset(id, 0, sizeof(*id));
  1959. id->dir = dir;
  1960. if (c->data.byid)
  1961. id->index = xp->index;
  1962. else
  1963. memcpy(&id->sel, &xp->selector, sizeof(id->sel));
  1964. p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
  1965. }
  1966. copy_to_user_policy(xp, p, dir);
  1967. if (copy_to_user_tmpl(xp, skb) < 0)
  1968. goto nlmsg_failure;
  1969. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1970. goto nlmsg_failure;
  1971. nlmsg_end(skb, nlh);
  1972. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  1973. nlmsg_failure:
  1974. rtattr_failure:
  1975. kfree_skb(skb);
  1976. return -1;
  1977. }
  1978. static int xfrm_notify_policy_flush(struct km_event *c)
  1979. {
  1980. struct nlmsghdr *nlh;
  1981. struct sk_buff *skb;
  1982. int len = 0;
  1983. #ifdef CONFIG_XFRM_SUB_POLICY
  1984. len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
  1985. #endif
  1986. len += NLMSG_LENGTH(0);
  1987. skb = alloc_skb(len, GFP_ATOMIC);
  1988. if (skb == NULL)
  1989. return -ENOMEM;
  1990. nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
  1991. if (nlh == NULL)
  1992. goto nlmsg_failure;
  1993. if (copy_to_user_policy_type(c->data.type, skb) < 0)
  1994. goto nlmsg_failure;
  1995. nlmsg_end(skb, nlh);
  1996. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  1997. nlmsg_failure:
  1998. kfree_skb(skb);
  1999. return -1;
  2000. }
  2001. static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  2002. {
  2003. switch (c->event) {
  2004. case XFRM_MSG_NEWPOLICY:
  2005. case XFRM_MSG_UPDPOLICY:
  2006. case XFRM_MSG_DELPOLICY:
  2007. return xfrm_notify_policy(xp, dir, c);
  2008. case XFRM_MSG_FLUSHPOLICY:
  2009. return xfrm_notify_policy_flush(c);
  2010. case XFRM_MSG_POLEXPIRE:
  2011. return xfrm_exp_policy_notify(xp, dir, c);
  2012. default:
  2013. printk("xfrm_user: Unknown Policy event %d\n", c->event);
  2014. }
  2015. return 0;
  2016. }
  2017. static int build_report(struct sk_buff *skb, u8 proto,
  2018. struct xfrm_selector *sel, xfrm_address_t *addr)
  2019. {
  2020. struct xfrm_user_report *ur;
  2021. struct nlmsghdr *nlh;
  2022. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
  2023. if (nlh == NULL)
  2024. return -EMSGSIZE;
  2025. ur = nlmsg_data(nlh);
  2026. ur->proto = proto;
  2027. memcpy(&ur->sel, sel, sizeof(ur->sel));
  2028. if (addr)
  2029. RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
  2030. return nlmsg_end(skb, nlh);
  2031. rtattr_failure:
  2032. nlmsg_cancel(skb, nlh);
  2033. return -EMSGSIZE;
  2034. }
  2035. static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
  2036. xfrm_address_t *addr)
  2037. {
  2038. struct sk_buff *skb;
  2039. size_t len;
  2040. len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
  2041. skb = alloc_skb(len, GFP_ATOMIC);
  2042. if (skb == NULL)
  2043. return -ENOMEM;
  2044. if (build_report(skb, proto, sel, addr) < 0)
  2045. BUG();
  2046. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
  2047. }
  2048. static struct xfrm_mgr netlink_mgr = {
  2049. .id = "netlink",
  2050. .notify = xfrm_send_state_notify,
  2051. .acquire = xfrm_send_acquire,
  2052. .compile_policy = xfrm_compile_policy,
  2053. .notify_policy = xfrm_send_policy_notify,
  2054. .report = xfrm_send_report,
  2055. .migrate = xfrm_send_migrate,
  2056. };
  2057. static int __init xfrm_user_init(void)
  2058. {
  2059. struct sock *nlsk;
  2060. printk(KERN_INFO "Initializing XFRM netlink socket\n");
  2061. nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
  2062. xfrm_netlink_rcv, NULL, THIS_MODULE);
  2063. if (nlsk == NULL)
  2064. return -ENOMEM;
  2065. rcu_assign_pointer(xfrm_nl, nlsk);
  2066. xfrm_register_km(&netlink_mgr);
  2067. return 0;
  2068. }
  2069. static void __exit xfrm_user_exit(void)
  2070. {
  2071. struct sock *nlsk = xfrm_nl;
  2072. xfrm_unregister_km(&netlink_mgr);
  2073. rcu_assign_pointer(xfrm_nl, NULL);
  2074. synchronize_rcu();
  2075. sock_release(nlsk->sk_socket);
  2076. }
  2077. module_init(xfrm_user_init);
  2078. module_exit(xfrm_user_exit);
  2079. MODULE_LICENSE("GPL");
  2080. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);