xfrm_user.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568
  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 = netlink_unicast(xfrm_nl, resp_skb,
  653. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  654. }
  655. xfrm_state_put(x);
  656. out_noput:
  657. return err;
  658. }
  659. static int verify_userspi_info(struct xfrm_userspi_info *p)
  660. {
  661. switch (p->info.id.proto) {
  662. case IPPROTO_AH:
  663. case IPPROTO_ESP:
  664. break;
  665. case IPPROTO_COMP:
  666. /* IPCOMP spi is 16-bits. */
  667. if (p->max >= 0x10000)
  668. return -EINVAL;
  669. break;
  670. default:
  671. return -EINVAL;
  672. }
  673. if (p->min > p->max)
  674. return -EINVAL;
  675. return 0;
  676. }
  677. static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
  678. struct rtattr **xfrma)
  679. {
  680. struct xfrm_state *x;
  681. struct xfrm_userspi_info *p;
  682. struct sk_buff *resp_skb;
  683. xfrm_address_t *daddr;
  684. int family;
  685. int err;
  686. p = nlmsg_data(nlh);
  687. err = verify_userspi_info(p);
  688. if (err)
  689. goto out_noput;
  690. family = p->info.family;
  691. daddr = &p->info.id.daddr;
  692. x = NULL;
  693. if (p->info.seq) {
  694. x = xfrm_find_acq_byseq(p->info.seq);
  695. if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
  696. xfrm_state_put(x);
  697. x = NULL;
  698. }
  699. }
  700. if (!x)
  701. x = xfrm_find_acq(p->info.mode, p->info.reqid,
  702. p->info.id.proto, daddr,
  703. &p->info.saddr, 1,
  704. family);
  705. err = -ENOENT;
  706. if (x == NULL)
  707. goto out_noput;
  708. resp_skb = ERR_PTR(-ENOENT);
  709. spin_lock_bh(&x->lock);
  710. if (x->km.state != XFRM_STATE_DEAD) {
  711. xfrm_alloc_spi(x, htonl(p->min), htonl(p->max));
  712. if (x->id.spi)
  713. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  714. }
  715. spin_unlock_bh(&x->lock);
  716. if (IS_ERR(resp_skb)) {
  717. err = PTR_ERR(resp_skb);
  718. goto out;
  719. }
  720. err = netlink_unicast(xfrm_nl, resp_skb,
  721. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  722. out:
  723. xfrm_state_put(x);
  724. out_noput:
  725. return err;
  726. }
  727. static int verify_policy_dir(u8 dir)
  728. {
  729. switch (dir) {
  730. case XFRM_POLICY_IN:
  731. case XFRM_POLICY_OUT:
  732. case XFRM_POLICY_FWD:
  733. break;
  734. default:
  735. return -EINVAL;
  736. }
  737. return 0;
  738. }
  739. static int verify_policy_type(u8 type)
  740. {
  741. switch (type) {
  742. case XFRM_POLICY_TYPE_MAIN:
  743. #ifdef CONFIG_XFRM_SUB_POLICY
  744. case XFRM_POLICY_TYPE_SUB:
  745. #endif
  746. break;
  747. default:
  748. return -EINVAL;
  749. }
  750. return 0;
  751. }
  752. static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
  753. {
  754. switch (p->share) {
  755. case XFRM_SHARE_ANY:
  756. case XFRM_SHARE_SESSION:
  757. case XFRM_SHARE_USER:
  758. case XFRM_SHARE_UNIQUE:
  759. break;
  760. default:
  761. return -EINVAL;
  762. }
  763. switch (p->action) {
  764. case XFRM_POLICY_ALLOW:
  765. case XFRM_POLICY_BLOCK:
  766. break;
  767. default:
  768. return -EINVAL;
  769. }
  770. switch (p->sel.family) {
  771. case AF_INET:
  772. break;
  773. case AF_INET6:
  774. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  775. break;
  776. #else
  777. return -EAFNOSUPPORT;
  778. #endif
  779. default:
  780. return -EINVAL;
  781. }
  782. return verify_policy_dir(p->dir);
  783. }
  784. static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct rtattr **xfrma)
  785. {
  786. struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
  787. struct xfrm_user_sec_ctx *uctx;
  788. if (!rt)
  789. return 0;
  790. uctx = RTA_DATA(rt);
  791. return security_xfrm_policy_alloc(pol, uctx);
  792. }
  793. static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
  794. int nr)
  795. {
  796. int i;
  797. xp->xfrm_nr = nr;
  798. for (i = 0; i < nr; i++, ut++) {
  799. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  800. memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
  801. memcpy(&t->saddr, &ut->saddr,
  802. sizeof(xfrm_address_t));
  803. t->reqid = ut->reqid;
  804. t->mode = ut->mode;
  805. t->share = ut->share;
  806. t->optional = ut->optional;
  807. t->aalgos = ut->aalgos;
  808. t->ealgos = ut->ealgos;
  809. t->calgos = ut->calgos;
  810. t->encap_family = ut->family;
  811. }
  812. }
  813. static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
  814. {
  815. int i;
  816. if (nr > XFRM_MAX_DEPTH)
  817. return -EINVAL;
  818. for (i = 0; i < nr; i++) {
  819. /* We never validated the ut->family value, so many
  820. * applications simply leave it at zero. The check was
  821. * never made and ut->family was ignored because all
  822. * templates could be assumed to have the same family as
  823. * the policy itself. Now that we will have ipv4-in-ipv6
  824. * and ipv6-in-ipv4 tunnels, this is no longer true.
  825. */
  826. if (!ut[i].family)
  827. ut[i].family = family;
  828. switch (ut[i].family) {
  829. case AF_INET:
  830. break;
  831. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  832. case AF_INET6:
  833. break;
  834. #endif
  835. default:
  836. return -EINVAL;
  837. }
  838. }
  839. return 0;
  840. }
  841. static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
  842. {
  843. struct rtattr *rt = xfrma[XFRMA_TMPL-1];
  844. if (!rt) {
  845. pol->xfrm_nr = 0;
  846. } else {
  847. struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
  848. int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
  849. int err;
  850. err = validate_tmpl(nr, utmpl, pol->family);
  851. if (err)
  852. return err;
  853. copy_templates(pol, RTA_DATA(rt), nr);
  854. }
  855. return 0;
  856. }
  857. static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
  858. {
  859. struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
  860. struct xfrm_userpolicy_type *upt;
  861. u8 type = XFRM_POLICY_TYPE_MAIN;
  862. int err;
  863. if (rt) {
  864. if (rt->rta_len < sizeof(*upt))
  865. return -EINVAL;
  866. upt = RTA_DATA(rt);
  867. type = upt->type;
  868. }
  869. err = verify_policy_type(type);
  870. if (err)
  871. return err;
  872. *tp = type;
  873. return 0;
  874. }
  875. static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
  876. {
  877. xp->priority = p->priority;
  878. xp->index = p->index;
  879. memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
  880. memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
  881. xp->action = p->action;
  882. xp->flags = p->flags;
  883. xp->family = p->sel.family;
  884. /* XXX xp->share = p->share; */
  885. }
  886. static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
  887. {
  888. memcpy(&p->sel, &xp->selector, sizeof(p->sel));
  889. memcpy(&p->lft, &xp->lft, sizeof(p->lft));
  890. memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
  891. p->priority = xp->priority;
  892. p->index = xp->index;
  893. p->sel.family = xp->family;
  894. p->dir = dir;
  895. p->action = xp->action;
  896. p->flags = xp->flags;
  897. p->share = XFRM_SHARE_ANY; /* XXX xp->share */
  898. }
  899. static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct rtattr **xfrma, int *errp)
  900. {
  901. struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
  902. int err;
  903. if (!xp) {
  904. *errp = -ENOMEM;
  905. return NULL;
  906. }
  907. copy_from_user_policy(xp, p);
  908. err = copy_from_user_policy_type(&xp->type, xfrma);
  909. if (err)
  910. goto error;
  911. if (!(err = copy_from_user_tmpl(xp, xfrma)))
  912. err = copy_from_user_sec_ctx(xp, xfrma);
  913. if (err)
  914. goto error;
  915. return xp;
  916. error:
  917. *errp = err;
  918. kfree(xp);
  919. return NULL;
  920. }
  921. static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  922. struct rtattr **xfrma)
  923. {
  924. struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
  925. struct xfrm_policy *xp;
  926. struct km_event c;
  927. int err;
  928. int excl;
  929. err = verify_newpolicy_info(p);
  930. if (err)
  931. return err;
  932. err = verify_sec_ctx_len(xfrma);
  933. if (err)
  934. return err;
  935. xp = xfrm_policy_construct(p, xfrma, &err);
  936. if (!xp)
  937. return err;
  938. /* shouldnt excl be based on nlh flags??
  939. * Aha! this is anti-netlink really i.e more pfkey derived
  940. * in netlink excl is a flag and you wouldnt need
  941. * a type XFRM_MSG_UPDPOLICY - JHS */
  942. excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
  943. err = xfrm_policy_insert(p->dir, xp, excl);
  944. xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
  945. AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
  946. if (err) {
  947. security_xfrm_policy_free(xp);
  948. kfree(xp);
  949. return err;
  950. }
  951. c.event = nlh->nlmsg_type;
  952. c.seq = nlh->nlmsg_seq;
  953. c.pid = nlh->nlmsg_pid;
  954. km_policy_notify(xp, p->dir, &c);
  955. xfrm_pol_put(xp);
  956. return 0;
  957. }
  958. static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
  959. {
  960. struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
  961. int i;
  962. if (xp->xfrm_nr == 0)
  963. return 0;
  964. for (i = 0; i < xp->xfrm_nr; i++) {
  965. struct xfrm_user_tmpl *up = &vec[i];
  966. struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
  967. memcpy(&up->id, &kp->id, sizeof(up->id));
  968. up->family = kp->encap_family;
  969. memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
  970. up->reqid = kp->reqid;
  971. up->mode = kp->mode;
  972. up->share = kp->share;
  973. up->optional = kp->optional;
  974. up->aalgos = kp->aalgos;
  975. up->ealgos = kp->ealgos;
  976. up->calgos = kp->calgos;
  977. }
  978. RTA_PUT(skb, XFRMA_TMPL,
  979. (sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr),
  980. vec);
  981. return 0;
  982. rtattr_failure:
  983. return -1;
  984. }
  985. static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
  986. {
  987. int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
  988. struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
  989. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  990. uctx->exttype = XFRMA_SEC_CTX;
  991. uctx->len = ctx_size;
  992. uctx->ctx_doi = s->ctx_doi;
  993. uctx->ctx_alg = s->ctx_alg;
  994. uctx->ctx_len = s->ctx_len;
  995. memcpy(uctx + 1, s->ctx_str, s->ctx_len);
  996. return 0;
  997. rtattr_failure:
  998. return -1;
  999. }
  1000. static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
  1001. {
  1002. if (x->security) {
  1003. return copy_sec_ctx(x->security, skb);
  1004. }
  1005. return 0;
  1006. }
  1007. static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
  1008. {
  1009. if (xp->security) {
  1010. return copy_sec_ctx(xp->security, skb);
  1011. }
  1012. return 0;
  1013. }
  1014. #ifdef CONFIG_XFRM_SUB_POLICY
  1015. static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1016. {
  1017. struct xfrm_userpolicy_type upt;
  1018. memset(&upt, 0, sizeof(upt));
  1019. upt.type = type;
  1020. RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
  1021. return 0;
  1022. rtattr_failure:
  1023. return -1;
  1024. }
  1025. #else
  1026. static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1027. {
  1028. return 0;
  1029. }
  1030. #endif
  1031. static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
  1032. {
  1033. struct xfrm_dump_info *sp = ptr;
  1034. struct xfrm_userpolicy_info *p;
  1035. struct sk_buff *in_skb = sp->in_skb;
  1036. struct sk_buff *skb = sp->out_skb;
  1037. struct nlmsghdr *nlh;
  1038. if (sp->this_idx < sp->start_idx)
  1039. goto out;
  1040. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
  1041. XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
  1042. if (nlh == NULL)
  1043. return -EMSGSIZE;
  1044. p = nlmsg_data(nlh);
  1045. copy_to_user_policy(xp, p, dir);
  1046. if (copy_to_user_tmpl(xp, skb) < 0)
  1047. goto nlmsg_failure;
  1048. if (copy_to_user_sec_ctx(xp, skb))
  1049. goto nlmsg_failure;
  1050. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1051. goto nlmsg_failure;
  1052. nlmsg_end(skb, nlh);
  1053. out:
  1054. sp->this_idx++;
  1055. return 0;
  1056. nlmsg_failure:
  1057. nlmsg_cancel(skb, nlh);
  1058. return -EMSGSIZE;
  1059. }
  1060. static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
  1061. {
  1062. struct xfrm_dump_info info;
  1063. info.in_skb = cb->skb;
  1064. info.out_skb = skb;
  1065. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  1066. info.nlmsg_flags = NLM_F_MULTI;
  1067. info.this_idx = 0;
  1068. info.start_idx = cb->args[0];
  1069. (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
  1070. #ifdef CONFIG_XFRM_SUB_POLICY
  1071. (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
  1072. #endif
  1073. cb->args[0] = info.this_idx;
  1074. return skb->len;
  1075. }
  1076. static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
  1077. struct xfrm_policy *xp,
  1078. int dir, u32 seq)
  1079. {
  1080. struct xfrm_dump_info info;
  1081. struct sk_buff *skb;
  1082. skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
  1083. if (!skb)
  1084. return ERR_PTR(-ENOMEM);
  1085. info.in_skb = in_skb;
  1086. info.out_skb = skb;
  1087. info.nlmsg_seq = seq;
  1088. info.nlmsg_flags = 0;
  1089. info.this_idx = info.start_idx = 0;
  1090. if (dump_one_policy(xp, dir, 0, &info) < 0) {
  1091. kfree_skb(skb);
  1092. return NULL;
  1093. }
  1094. return skb;
  1095. }
  1096. static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1097. struct rtattr **xfrma)
  1098. {
  1099. struct xfrm_policy *xp;
  1100. struct xfrm_userpolicy_id *p;
  1101. u8 type = XFRM_POLICY_TYPE_MAIN;
  1102. int err;
  1103. struct km_event c;
  1104. int delete;
  1105. p = nlmsg_data(nlh);
  1106. delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
  1107. err = copy_from_user_policy_type(&type, xfrma);
  1108. if (err)
  1109. return err;
  1110. err = verify_policy_dir(p->dir);
  1111. if (err)
  1112. return err;
  1113. if (p->index)
  1114. xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
  1115. else {
  1116. struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
  1117. struct xfrm_policy tmp;
  1118. err = verify_sec_ctx_len(xfrma);
  1119. if (err)
  1120. return err;
  1121. memset(&tmp, 0, sizeof(struct xfrm_policy));
  1122. if (rt) {
  1123. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  1124. if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
  1125. return err;
  1126. }
  1127. xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
  1128. delete, &err);
  1129. security_xfrm_policy_free(&tmp);
  1130. }
  1131. if (xp == NULL)
  1132. return -ENOENT;
  1133. if (!delete) {
  1134. struct sk_buff *resp_skb;
  1135. resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
  1136. if (IS_ERR(resp_skb)) {
  1137. err = PTR_ERR(resp_skb);
  1138. } else {
  1139. err = netlink_unicast(xfrm_nl, resp_skb,
  1140. NETLINK_CB(skb).pid,
  1141. MSG_DONTWAIT);
  1142. }
  1143. } else {
  1144. xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
  1145. AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
  1146. if (err != 0)
  1147. goto out;
  1148. c.data.byid = p->index;
  1149. c.event = nlh->nlmsg_type;
  1150. c.seq = nlh->nlmsg_seq;
  1151. c.pid = nlh->nlmsg_pid;
  1152. km_policy_notify(xp, p->dir, &c);
  1153. }
  1154. out:
  1155. xfrm_pol_put(xp);
  1156. return err;
  1157. }
  1158. static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  1159. struct rtattr **xfrma)
  1160. {
  1161. struct km_event c;
  1162. struct xfrm_usersa_flush *p = nlmsg_data(nlh);
  1163. struct xfrm_audit audit_info;
  1164. int err;
  1165. audit_info.loginuid = NETLINK_CB(skb).loginuid;
  1166. audit_info.secid = NETLINK_CB(skb).sid;
  1167. err = xfrm_state_flush(p->proto, &audit_info);
  1168. if (err)
  1169. return err;
  1170. c.data.proto = p->proto;
  1171. c.event = nlh->nlmsg_type;
  1172. c.seq = nlh->nlmsg_seq;
  1173. c.pid = nlh->nlmsg_pid;
  1174. km_state_notify(NULL, &c);
  1175. return 0;
  1176. }
  1177. static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
  1178. {
  1179. struct xfrm_aevent_id *id;
  1180. struct nlmsghdr *nlh;
  1181. struct xfrm_lifetime_cur ltime;
  1182. nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
  1183. if (nlh == NULL)
  1184. return -EMSGSIZE;
  1185. id = nlmsg_data(nlh);
  1186. memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
  1187. id->sa_id.spi = x->id.spi;
  1188. id->sa_id.family = x->props.family;
  1189. id->sa_id.proto = x->id.proto;
  1190. memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
  1191. id->reqid = x->props.reqid;
  1192. id->flags = c->data.aevent;
  1193. RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
  1194. ltime.bytes = x->curlft.bytes;
  1195. ltime.packets = x->curlft.packets;
  1196. ltime.add_time = x->curlft.add_time;
  1197. ltime.use_time = x->curlft.use_time;
  1198. RTA_PUT(skb, XFRMA_LTIME_VAL, sizeof(struct xfrm_lifetime_cur), &ltime);
  1199. if (id->flags&XFRM_AE_RTHR) {
  1200. RTA_PUT(skb,XFRMA_REPLAY_THRESH,sizeof(u32),&x->replay_maxdiff);
  1201. }
  1202. if (id->flags&XFRM_AE_ETHR) {
  1203. u32 etimer = x->replay_maxage*10/HZ;
  1204. RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
  1205. }
  1206. return nlmsg_end(skb, nlh);
  1207. rtattr_failure:
  1208. nlmsg_cancel(skb, nlh);
  1209. return -EMSGSIZE;
  1210. }
  1211. static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1212. struct rtattr **xfrma)
  1213. {
  1214. struct xfrm_state *x;
  1215. struct sk_buff *r_skb;
  1216. int err;
  1217. struct km_event c;
  1218. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  1219. int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
  1220. struct xfrm_usersa_id *id = &p->sa_id;
  1221. len += RTA_SPACE(sizeof(struct xfrm_replay_state));
  1222. len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
  1223. if (p->flags&XFRM_AE_RTHR)
  1224. len+=RTA_SPACE(sizeof(u32));
  1225. if (p->flags&XFRM_AE_ETHR)
  1226. len+=RTA_SPACE(sizeof(u32));
  1227. r_skb = alloc_skb(len, GFP_ATOMIC);
  1228. if (r_skb == NULL)
  1229. return -ENOMEM;
  1230. x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
  1231. if (x == NULL) {
  1232. kfree_skb(r_skb);
  1233. return -ESRCH;
  1234. }
  1235. /*
  1236. * XXX: is this lock really needed - none of the other
  1237. * gets lock (the concern is things getting updated
  1238. * while we are still reading) - jhs
  1239. */
  1240. spin_lock_bh(&x->lock);
  1241. c.data.aevent = p->flags;
  1242. c.seq = nlh->nlmsg_seq;
  1243. c.pid = nlh->nlmsg_pid;
  1244. if (build_aevent(r_skb, x, &c) < 0)
  1245. BUG();
  1246. err = netlink_unicast(xfrm_nl, r_skb,
  1247. NETLINK_CB(skb).pid, MSG_DONTWAIT);
  1248. spin_unlock_bh(&x->lock);
  1249. xfrm_state_put(x);
  1250. return err;
  1251. }
  1252. static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1253. struct rtattr **xfrma)
  1254. {
  1255. struct xfrm_state *x;
  1256. struct km_event c;
  1257. int err = - EINVAL;
  1258. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  1259. struct rtattr *rp = xfrma[XFRMA_REPLAY_VAL-1];
  1260. struct rtattr *lt = xfrma[XFRMA_LTIME_VAL-1];
  1261. if (!lt && !rp)
  1262. return err;
  1263. /* pedantic mode - thou shalt sayeth replaceth */
  1264. if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
  1265. return err;
  1266. x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
  1267. if (x == NULL)
  1268. return -ESRCH;
  1269. if (x->km.state != XFRM_STATE_VALID)
  1270. goto out;
  1271. spin_lock_bh(&x->lock);
  1272. err = xfrm_update_ae_params(x, xfrma);
  1273. spin_unlock_bh(&x->lock);
  1274. if (err < 0)
  1275. goto out;
  1276. c.event = nlh->nlmsg_type;
  1277. c.seq = nlh->nlmsg_seq;
  1278. c.pid = nlh->nlmsg_pid;
  1279. c.data.aevent = XFRM_AE_CU;
  1280. km_state_notify(x, &c);
  1281. err = 0;
  1282. out:
  1283. xfrm_state_put(x);
  1284. return err;
  1285. }
  1286. static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1287. struct rtattr **xfrma)
  1288. {
  1289. struct km_event c;
  1290. u8 type = XFRM_POLICY_TYPE_MAIN;
  1291. int err;
  1292. struct xfrm_audit audit_info;
  1293. err = copy_from_user_policy_type(&type, xfrma);
  1294. if (err)
  1295. return err;
  1296. audit_info.loginuid = NETLINK_CB(skb).loginuid;
  1297. audit_info.secid = NETLINK_CB(skb).sid;
  1298. err = xfrm_policy_flush(type, &audit_info);
  1299. if (err)
  1300. return err;
  1301. c.data.type = type;
  1302. c.event = nlh->nlmsg_type;
  1303. c.seq = nlh->nlmsg_seq;
  1304. c.pid = nlh->nlmsg_pid;
  1305. km_policy_notify(NULL, 0, &c);
  1306. return 0;
  1307. }
  1308. static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1309. struct rtattr **xfrma)
  1310. {
  1311. struct xfrm_policy *xp;
  1312. struct xfrm_user_polexpire *up = nlmsg_data(nlh);
  1313. struct xfrm_userpolicy_info *p = &up->pol;
  1314. u8 type = XFRM_POLICY_TYPE_MAIN;
  1315. int err = -ENOENT;
  1316. err = copy_from_user_policy_type(&type, xfrma);
  1317. if (err)
  1318. return err;
  1319. if (p->index)
  1320. xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
  1321. else {
  1322. struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
  1323. struct xfrm_policy tmp;
  1324. err = verify_sec_ctx_len(xfrma);
  1325. if (err)
  1326. return err;
  1327. memset(&tmp, 0, sizeof(struct xfrm_policy));
  1328. if (rt) {
  1329. struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
  1330. if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
  1331. return err;
  1332. }
  1333. xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
  1334. 0, &err);
  1335. security_xfrm_policy_free(&tmp);
  1336. }
  1337. if (xp == NULL)
  1338. return -ENOENT;
  1339. read_lock(&xp->lock);
  1340. if (xp->dead) {
  1341. read_unlock(&xp->lock);
  1342. goto out;
  1343. }
  1344. read_unlock(&xp->lock);
  1345. err = 0;
  1346. if (up->hard) {
  1347. xfrm_policy_delete(xp, p->dir);
  1348. xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
  1349. AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
  1350. } else {
  1351. // reset the timers here?
  1352. printk("Dont know what to do with soft policy expire\n");
  1353. }
  1354. km_policy_expired(xp, p->dir, up->hard, current->pid);
  1355. out:
  1356. xfrm_pol_put(xp);
  1357. return err;
  1358. }
  1359. static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1360. struct rtattr **xfrma)
  1361. {
  1362. struct xfrm_state *x;
  1363. int err;
  1364. struct xfrm_user_expire *ue = nlmsg_data(nlh);
  1365. struct xfrm_usersa_info *p = &ue->state;
  1366. x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
  1367. err = -ENOENT;
  1368. if (x == NULL)
  1369. return err;
  1370. spin_lock_bh(&x->lock);
  1371. err = -EINVAL;
  1372. if (x->km.state != XFRM_STATE_VALID)
  1373. goto out;
  1374. km_state_expired(x, ue->hard, current->pid);
  1375. if (ue->hard) {
  1376. __xfrm_state_delete(x);
  1377. xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
  1378. AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
  1379. }
  1380. err = 0;
  1381. out:
  1382. spin_unlock_bh(&x->lock);
  1383. xfrm_state_put(x);
  1384. return err;
  1385. }
  1386. static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1387. struct rtattr **xfrma)
  1388. {
  1389. struct xfrm_policy *xp;
  1390. struct xfrm_user_tmpl *ut;
  1391. int i;
  1392. struct rtattr *rt = xfrma[XFRMA_TMPL-1];
  1393. struct xfrm_user_acquire *ua = nlmsg_data(nlh);
  1394. struct xfrm_state *x = xfrm_state_alloc();
  1395. int err = -ENOMEM;
  1396. if (!x)
  1397. return err;
  1398. err = verify_newpolicy_info(&ua->policy);
  1399. if (err) {
  1400. printk("BAD policy passed\n");
  1401. kfree(x);
  1402. return err;
  1403. }
  1404. /* build an XP */
  1405. xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
  1406. if (!xp) {
  1407. kfree(x);
  1408. return err;
  1409. }
  1410. memcpy(&x->id, &ua->id, sizeof(ua->id));
  1411. memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
  1412. memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
  1413. ut = RTA_DATA(rt);
  1414. /* extract the templates and for each call km_key */
  1415. for (i = 0; i < xp->xfrm_nr; i++, ut++) {
  1416. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  1417. memcpy(&x->id, &t->id, sizeof(x->id));
  1418. x->props.mode = t->mode;
  1419. x->props.reqid = t->reqid;
  1420. x->props.family = ut->family;
  1421. t->aalgos = ua->aalgos;
  1422. t->ealgos = ua->ealgos;
  1423. t->calgos = ua->calgos;
  1424. err = km_query(x, t, xp);
  1425. }
  1426. kfree(x);
  1427. kfree(xp);
  1428. return 0;
  1429. }
  1430. #ifdef CONFIG_XFRM_MIGRATE
  1431. static int verify_user_migrate(struct rtattr **xfrma)
  1432. {
  1433. struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
  1434. struct xfrm_user_migrate *um;
  1435. if (!rt)
  1436. return -EINVAL;
  1437. if ((rt->rta_len - sizeof(*rt)) < sizeof(*um))
  1438. return -EINVAL;
  1439. return 0;
  1440. }
  1441. static int copy_from_user_migrate(struct xfrm_migrate *ma,
  1442. struct rtattr **xfrma, int *num)
  1443. {
  1444. struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
  1445. struct xfrm_user_migrate *um;
  1446. int i, num_migrate;
  1447. um = RTA_DATA(rt);
  1448. num_migrate = (rt->rta_len - sizeof(*rt)) / sizeof(*um);
  1449. if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
  1450. return -EINVAL;
  1451. for (i = 0; i < num_migrate; i++, um++, ma++) {
  1452. memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
  1453. memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
  1454. memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
  1455. memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
  1456. ma->proto = um->proto;
  1457. ma->mode = um->mode;
  1458. ma->reqid = um->reqid;
  1459. ma->old_family = um->old_family;
  1460. ma->new_family = um->new_family;
  1461. }
  1462. *num = i;
  1463. return 0;
  1464. }
  1465. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1466. struct rtattr **xfrma)
  1467. {
  1468. struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
  1469. struct xfrm_migrate m[XFRM_MAX_DEPTH];
  1470. u8 type;
  1471. int err;
  1472. int n = 0;
  1473. err = verify_user_migrate((struct rtattr **)xfrma);
  1474. if (err)
  1475. return err;
  1476. err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
  1477. if (err)
  1478. return err;
  1479. err = copy_from_user_migrate((struct xfrm_migrate *)m,
  1480. (struct rtattr **)xfrma, &n);
  1481. if (err)
  1482. return err;
  1483. if (!n)
  1484. return 0;
  1485. xfrm_migrate(&pi->sel, pi->dir, type, m, n);
  1486. return 0;
  1487. }
  1488. #else
  1489. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1490. struct rtattr **xfrma)
  1491. {
  1492. return -ENOPROTOOPT;
  1493. }
  1494. #endif
  1495. #ifdef CONFIG_XFRM_MIGRATE
  1496. static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
  1497. {
  1498. struct xfrm_user_migrate um;
  1499. memset(&um, 0, sizeof(um));
  1500. um.proto = m->proto;
  1501. um.mode = m->mode;
  1502. um.reqid = m->reqid;
  1503. um.old_family = m->old_family;
  1504. memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
  1505. memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
  1506. um.new_family = m->new_family;
  1507. memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
  1508. memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
  1509. RTA_PUT(skb, XFRMA_MIGRATE, sizeof(um), &um);
  1510. return 0;
  1511. rtattr_failure:
  1512. return -1;
  1513. }
  1514. static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
  1515. int num_migrate, struct xfrm_selector *sel,
  1516. u8 dir, u8 type)
  1517. {
  1518. struct xfrm_migrate *mp;
  1519. struct xfrm_userpolicy_id *pol_id;
  1520. struct nlmsghdr *nlh;
  1521. int i;
  1522. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
  1523. if (nlh == NULL)
  1524. return -EMSGSIZE;
  1525. pol_id = nlmsg_data(nlh);
  1526. /* copy data from selector, dir, and type to the pol_id */
  1527. memset(pol_id, 0, sizeof(*pol_id));
  1528. memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
  1529. pol_id->dir = dir;
  1530. if (copy_to_user_policy_type(type, skb) < 0)
  1531. goto nlmsg_failure;
  1532. for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
  1533. if (copy_to_user_migrate(mp, skb) < 0)
  1534. goto nlmsg_failure;
  1535. }
  1536. return nlmsg_end(skb, nlh);
  1537. nlmsg_failure:
  1538. nlmsg_cancel(skb, nlh);
  1539. return -EMSGSIZE;
  1540. }
  1541. static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  1542. struct xfrm_migrate *m, int num_migrate)
  1543. {
  1544. struct sk_buff *skb;
  1545. size_t len;
  1546. len = RTA_SPACE(sizeof(struct xfrm_user_migrate) * num_migrate);
  1547. len += NLMSG_SPACE(sizeof(struct xfrm_userpolicy_id));
  1548. #ifdef CONFIG_XFRM_SUB_POLICY
  1549. len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
  1550. #endif
  1551. skb = alloc_skb(len, GFP_ATOMIC);
  1552. if (skb == NULL)
  1553. return -ENOMEM;
  1554. /* build migrate */
  1555. if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
  1556. BUG();
  1557. NETLINK_CB(skb).dst_group = XFRMNLGRP_MIGRATE;
  1558. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE,
  1559. GFP_ATOMIC);
  1560. }
  1561. #else
  1562. static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  1563. struct xfrm_migrate *m, int num_migrate)
  1564. {
  1565. return -ENOPROTOOPT;
  1566. }
  1567. #endif
  1568. #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
  1569. static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
  1570. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1571. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1572. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1573. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1574. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1575. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1576. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
  1577. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
  1578. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
  1579. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1580. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1581. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
  1582. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  1583. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
  1584. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1585. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1586. [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
  1587. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1588. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
  1589. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
  1590. };
  1591. #undef XMSGSIZE
  1592. static struct xfrm_link {
  1593. int (*doit)(struct sk_buff *, struct nlmsghdr *, struct rtattr **);
  1594. int (*dump)(struct sk_buff *, struct netlink_callback *);
  1595. } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
  1596. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1597. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
  1598. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
  1599. .dump = xfrm_dump_sa },
  1600. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1601. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
  1602. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
  1603. .dump = xfrm_dump_policy },
  1604. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
  1605. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
  1606. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
  1607. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1608. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1609. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
  1610. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
  1611. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
  1612. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
  1613. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
  1614. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
  1615. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
  1616. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
  1617. };
  1618. static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  1619. {
  1620. struct rtattr *xfrma[XFRMA_MAX];
  1621. struct xfrm_link *link;
  1622. int type, min_len;
  1623. type = nlh->nlmsg_type;
  1624. if (type > XFRM_MSG_MAX)
  1625. return -EINVAL;
  1626. type -= XFRM_MSG_BASE;
  1627. link = &xfrm_dispatch[type];
  1628. /* All operations require privileges, even GET */
  1629. if (security_netlink_recv(skb, CAP_NET_ADMIN))
  1630. return -EPERM;
  1631. if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
  1632. type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
  1633. (nlh->nlmsg_flags & NLM_F_DUMP)) {
  1634. if (link->dump == NULL)
  1635. return -EINVAL;
  1636. return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
  1637. }
  1638. memset(xfrma, 0, sizeof(xfrma));
  1639. if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
  1640. return -EINVAL;
  1641. if (nlh->nlmsg_len > min_len) {
  1642. int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
  1643. struct rtattr *attr = (void *) nlh + NLMSG_ALIGN(min_len);
  1644. while (RTA_OK(attr, attrlen)) {
  1645. unsigned short flavor = attr->rta_type;
  1646. if (flavor) {
  1647. if (flavor > XFRMA_MAX)
  1648. return -EINVAL;
  1649. xfrma[flavor - 1] = attr;
  1650. }
  1651. attr = RTA_NEXT(attr, attrlen);
  1652. }
  1653. }
  1654. if (link->doit == NULL)
  1655. return -EINVAL;
  1656. return link->doit(skb, nlh, xfrma);
  1657. }
  1658. static void xfrm_netlink_rcv(struct sock *sk, int len)
  1659. {
  1660. unsigned int qlen = 0;
  1661. do {
  1662. mutex_lock(&xfrm_cfg_mutex);
  1663. netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
  1664. mutex_unlock(&xfrm_cfg_mutex);
  1665. } while (qlen);
  1666. }
  1667. static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
  1668. {
  1669. struct xfrm_user_expire *ue;
  1670. struct nlmsghdr *nlh;
  1671. nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
  1672. if (nlh == NULL)
  1673. return -EMSGSIZE;
  1674. ue = nlmsg_data(nlh);
  1675. copy_to_user_state(x, &ue->state);
  1676. ue->hard = (c->data.hard != 0) ? 1 : 0;
  1677. return nlmsg_end(skb, nlh);
  1678. }
  1679. static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
  1680. {
  1681. struct sk_buff *skb;
  1682. int len = NLMSG_LENGTH(sizeof(struct xfrm_user_expire));
  1683. skb = alloc_skb(len, GFP_ATOMIC);
  1684. if (skb == NULL)
  1685. return -ENOMEM;
  1686. if (build_expire(skb, x, c) < 0)
  1687. BUG();
  1688. NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
  1689. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  1690. }
  1691. static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
  1692. {
  1693. struct sk_buff *skb;
  1694. int len = NLMSG_LENGTH(sizeof(struct xfrm_aevent_id));
  1695. len += RTA_SPACE(sizeof(struct xfrm_replay_state));
  1696. len += RTA_SPACE(sizeof(struct xfrm_lifetime_cur));
  1697. skb = alloc_skb(len, GFP_ATOMIC);
  1698. if (skb == NULL)
  1699. return -ENOMEM;
  1700. if (build_aevent(skb, x, c) < 0)
  1701. BUG();
  1702. NETLINK_CB(skb).dst_group = XFRMNLGRP_AEVENTS;
  1703. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
  1704. }
  1705. static int xfrm_notify_sa_flush(struct km_event *c)
  1706. {
  1707. struct xfrm_usersa_flush *p;
  1708. struct nlmsghdr *nlh;
  1709. struct sk_buff *skb;
  1710. int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
  1711. skb = alloc_skb(len, GFP_ATOMIC);
  1712. if (skb == NULL)
  1713. return -ENOMEM;
  1714. nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
  1715. if (nlh == NULL) {
  1716. kfree_skb(skb);
  1717. return -EMSGSIZE;
  1718. }
  1719. p = nlmsg_data(nlh);
  1720. p->proto = c->data.proto;
  1721. nlmsg_end(skb, nlh);
  1722. NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
  1723. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  1724. }
  1725. static inline int xfrm_sa_len(struct xfrm_state *x)
  1726. {
  1727. int l = 0;
  1728. if (x->aalg)
  1729. l += RTA_SPACE(sizeof(*x->aalg) + (x->aalg->alg_key_len+7)/8);
  1730. if (x->ealg)
  1731. l += RTA_SPACE(sizeof(*x->ealg) + (x->ealg->alg_key_len+7)/8);
  1732. if (x->calg)
  1733. l += RTA_SPACE(sizeof(*x->calg));
  1734. if (x->encap)
  1735. l += RTA_SPACE(sizeof(*x->encap));
  1736. return l;
  1737. }
  1738. static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
  1739. {
  1740. struct xfrm_usersa_info *p;
  1741. struct xfrm_usersa_id *id;
  1742. struct nlmsghdr *nlh;
  1743. struct sk_buff *skb;
  1744. int len = xfrm_sa_len(x);
  1745. int headlen;
  1746. headlen = sizeof(*p);
  1747. if (c->event == XFRM_MSG_DELSA) {
  1748. len += RTA_SPACE(headlen);
  1749. headlen = sizeof(*id);
  1750. }
  1751. len += NLMSG_SPACE(headlen);
  1752. skb = alloc_skb(len, GFP_ATOMIC);
  1753. if (skb == NULL)
  1754. return -ENOMEM;
  1755. nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
  1756. if (nlh == NULL)
  1757. goto nlmsg_failure;
  1758. p = nlmsg_data(nlh);
  1759. if (c->event == XFRM_MSG_DELSA) {
  1760. id = nlmsg_data(nlh);
  1761. memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
  1762. id->spi = x->id.spi;
  1763. id->family = x->props.family;
  1764. id->proto = x->id.proto;
  1765. p = RTA_DATA(__RTA_PUT(skb, XFRMA_SA, sizeof(*p)));
  1766. }
  1767. copy_to_user_state(x, p);
  1768. if (x->aalg)
  1769. RTA_PUT(skb, XFRMA_ALG_AUTH,
  1770. sizeof(*(x->aalg))+(x->aalg->alg_key_len+7)/8, x->aalg);
  1771. if (x->ealg)
  1772. RTA_PUT(skb, XFRMA_ALG_CRYPT,
  1773. sizeof(*(x->ealg))+(x->ealg->alg_key_len+7)/8, x->ealg);
  1774. if (x->calg)
  1775. RTA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  1776. if (x->encap)
  1777. RTA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  1778. nlmsg_end(skb, nlh);
  1779. NETLINK_CB(skb).dst_group = XFRMNLGRP_SA;
  1780. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  1781. nlmsg_failure:
  1782. rtattr_failure:
  1783. kfree_skb(skb);
  1784. return -1;
  1785. }
  1786. static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
  1787. {
  1788. switch (c->event) {
  1789. case XFRM_MSG_EXPIRE:
  1790. return xfrm_exp_state_notify(x, c);
  1791. case XFRM_MSG_NEWAE:
  1792. return xfrm_aevent_state_notify(x, c);
  1793. case XFRM_MSG_DELSA:
  1794. case XFRM_MSG_UPDSA:
  1795. case XFRM_MSG_NEWSA:
  1796. return xfrm_notify_sa(x, c);
  1797. case XFRM_MSG_FLUSHSA:
  1798. return xfrm_notify_sa_flush(c);
  1799. default:
  1800. printk("xfrm_user: Unknown SA event %d\n", c->event);
  1801. break;
  1802. }
  1803. return 0;
  1804. }
  1805. static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
  1806. struct xfrm_tmpl *xt, struct xfrm_policy *xp,
  1807. int dir)
  1808. {
  1809. struct xfrm_user_acquire *ua;
  1810. struct nlmsghdr *nlh;
  1811. __u32 seq = xfrm_get_acqseq();
  1812. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
  1813. if (nlh == NULL)
  1814. return -EMSGSIZE;
  1815. ua = nlmsg_data(nlh);
  1816. memcpy(&ua->id, &x->id, sizeof(ua->id));
  1817. memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
  1818. memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
  1819. copy_to_user_policy(xp, &ua->policy, dir);
  1820. ua->aalgos = xt->aalgos;
  1821. ua->ealgos = xt->ealgos;
  1822. ua->calgos = xt->calgos;
  1823. ua->seq = x->km.seq = seq;
  1824. if (copy_to_user_tmpl(xp, skb) < 0)
  1825. goto nlmsg_failure;
  1826. if (copy_to_user_state_sec_ctx(x, skb))
  1827. goto nlmsg_failure;
  1828. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1829. goto nlmsg_failure;
  1830. return nlmsg_end(skb, nlh);
  1831. nlmsg_failure:
  1832. nlmsg_cancel(skb, nlh);
  1833. return -EMSGSIZE;
  1834. }
  1835. static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
  1836. struct xfrm_policy *xp, int dir)
  1837. {
  1838. struct sk_buff *skb;
  1839. size_t len;
  1840. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1841. len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
  1842. len += RTA_SPACE(xfrm_user_sec_ctx_size(x->security));
  1843. #ifdef CONFIG_XFRM_SUB_POLICY
  1844. len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
  1845. #endif
  1846. skb = alloc_skb(len, GFP_ATOMIC);
  1847. if (skb == NULL)
  1848. return -ENOMEM;
  1849. if (build_acquire(skb, x, xt, xp, dir) < 0)
  1850. BUG();
  1851. NETLINK_CB(skb).dst_group = XFRMNLGRP_ACQUIRE;
  1852. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
  1853. }
  1854. /* User gives us xfrm_user_policy_info followed by an array of 0
  1855. * or more templates.
  1856. */
  1857. static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
  1858. u8 *data, int len, int *dir)
  1859. {
  1860. struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
  1861. struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
  1862. struct xfrm_policy *xp;
  1863. int nr;
  1864. switch (sk->sk_family) {
  1865. case AF_INET:
  1866. if (opt != IP_XFRM_POLICY) {
  1867. *dir = -EOPNOTSUPP;
  1868. return NULL;
  1869. }
  1870. break;
  1871. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1872. case AF_INET6:
  1873. if (opt != IPV6_XFRM_POLICY) {
  1874. *dir = -EOPNOTSUPP;
  1875. return NULL;
  1876. }
  1877. break;
  1878. #endif
  1879. default:
  1880. *dir = -EINVAL;
  1881. return NULL;
  1882. }
  1883. *dir = -EINVAL;
  1884. if (len < sizeof(*p) ||
  1885. verify_newpolicy_info(p))
  1886. return NULL;
  1887. nr = ((len - sizeof(*p)) / sizeof(*ut));
  1888. if (validate_tmpl(nr, ut, p->sel.family))
  1889. return NULL;
  1890. if (p->dir > XFRM_POLICY_OUT)
  1891. return NULL;
  1892. xp = xfrm_policy_alloc(GFP_KERNEL);
  1893. if (xp == NULL) {
  1894. *dir = -ENOBUFS;
  1895. return NULL;
  1896. }
  1897. copy_from_user_policy(xp, p);
  1898. xp->type = XFRM_POLICY_TYPE_MAIN;
  1899. copy_templates(xp, ut, nr);
  1900. *dir = p->dir;
  1901. return xp;
  1902. }
  1903. static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
  1904. int dir, struct km_event *c)
  1905. {
  1906. struct xfrm_user_polexpire *upe;
  1907. struct nlmsghdr *nlh;
  1908. int hard = c->data.hard;
  1909. nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
  1910. if (nlh == NULL)
  1911. return -EMSGSIZE;
  1912. upe = nlmsg_data(nlh);
  1913. copy_to_user_policy(xp, &upe->pol, dir);
  1914. if (copy_to_user_tmpl(xp, skb) < 0)
  1915. goto nlmsg_failure;
  1916. if (copy_to_user_sec_ctx(xp, skb))
  1917. goto nlmsg_failure;
  1918. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1919. goto nlmsg_failure;
  1920. upe->hard = !!hard;
  1921. return nlmsg_end(skb, nlh);
  1922. nlmsg_failure:
  1923. nlmsg_cancel(skb, nlh);
  1924. return -EMSGSIZE;
  1925. }
  1926. static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  1927. {
  1928. struct sk_buff *skb;
  1929. size_t len;
  1930. len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1931. len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
  1932. len += RTA_SPACE(xfrm_user_sec_ctx_size(xp->security));
  1933. #ifdef CONFIG_XFRM_SUB_POLICY
  1934. len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
  1935. #endif
  1936. skb = alloc_skb(len, GFP_ATOMIC);
  1937. if (skb == NULL)
  1938. return -ENOMEM;
  1939. if (build_polexpire(skb, xp, dir, c) < 0)
  1940. BUG();
  1941. NETLINK_CB(skb).dst_group = XFRMNLGRP_EXPIRE;
  1942. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  1943. }
  1944. static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
  1945. {
  1946. struct xfrm_userpolicy_info *p;
  1947. struct xfrm_userpolicy_id *id;
  1948. struct nlmsghdr *nlh;
  1949. struct sk_buff *skb;
  1950. int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1951. int headlen;
  1952. headlen = sizeof(*p);
  1953. if (c->event == XFRM_MSG_DELPOLICY) {
  1954. len += RTA_SPACE(headlen);
  1955. headlen = sizeof(*id);
  1956. }
  1957. #ifdef CONFIG_XFRM_SUB_POLICY
  1958. len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
  1959. #endif
  1960. len += NLMSG_SPACE(headlen);
  1961. skb = alloc_skb(len, GFP_ATOMIC);
  1962. if (skb == NULL)
  1963. return -ENOMEM;
  1964. nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
  1965. if (nlh == NULL)
  1966. goto nlmsg_failure;
  1967. p = nlmsg_data(nlh);
  1968. if (c->event == XFRM_MSG_DELPOLICY) {
  1969. id = nlmsg_data(nlh);
  1970. memset(id, 0, sizeof(*id));
  1971. id->dir = dir;
  1972. if (c->data.byid)
  1973. id->index = xp->index;
  1974. else
  1975. memcpy(&id->sel, &xp->selector, sizeof(id->sel));
  1976. p = RTA_DATA(__RTA_PUT(skb, XFRMA_POLICY, sizeof(*p)));
  1977. }
  1978. copy_to_user_policy(xp, p, dir);
  1979. if (copy_to_user_tmpl(xp, skb) < 0)
  1980. goto nlmsg_failure;
  1981. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1982. goto nlmsg_failure;
  1983. nlmsg_end(skb, nlh);
  1984. NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
  1985. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  1986. nlmsg_failure:
  1987. rtattr_failure:
  1988. kfree_skb(skb);
  1989. return -1;
  1990. }
  1991. static int xfrm_notify_policy_flush(struct km_event *c)
  1992. {
  1993. struct nlmsghdr *nlh;
  1994. struct sk_buff *skb;
  1995. int len = 0;
  1996. #ifdef CONFIG_XFRM_SUB_POLICY
  1997. len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
  1998. #endif
  1999. len += NLMSG_LENGTH(0);
  2000. skb = alloc_skb(len, GFP_ATOMIC);
  2001. if (skb == NULL)
  2002. return -ENOMEM;
  2003. nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
  2004. if (nlh == NULL)
  2005. goto nlmsg_failure;
  2006. if (copy_to_user_policy_type(c->data.type, skb) < 0)
  2007. goto nlmsg_failure;
  2008. nlmsg_end(skb, nlh);
  2009. NETLINK_CB(skb).dst_group = XFRMNLGRP_POLICY;
  2010. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  2011. nlmsg_failure:
  2012. kfree_skb(skb);
  2013. return -1;
  2014. }
  2015. static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  2016. {
  2017. switch (c->event) {
  2018. case XFRM_MSG_NEWPOLICY:
  2019. case XFRM_MSG_UPDPOLICY:
  2020. case XFRM_MSG_DELPOLICY:
  2021. return xfrm_notify_policy(xp, dir, c);
  2022. case XFRM_MSG_FLUSHPOLICY:
  2023. return xfrm_notify_policy_flush(c);
  2024. case XFRM_MSG_POLEXPIRE:
  2025. return xfrm_exp_policy_notify(xp, dir, c);
  2026. default:
  2027. printk("xfrm_user: Unknown Policy event %d\n", c->event);
  2028. }
  2029. return 0;
  2030. }
  2031. static int build_report(struct sk_buff *skb, u8 proto,
  2032. struct xfrm_selector *sel, xfrm_address_t *addr)
  2033. {
  2034. struct xfrm_user_report *ur;
  2035. struct nlmsghdr *nlh;
  2036. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
  2037. if (nlh == NULL)
  2038. return -EMSGSIZE;
  2039. ur = nlmsg_data(nlh);
  2040. ur->proto = proto;
  2041. memcpy(&ur->sel, sel, sizeof(ur->sel));
  2042. if (addr)
  2043. RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
  2044. return nlmsg_end(skb, nlh);
  2045. rtattr_failure:
  2046. nlmsg_cancel(skb, nlh);
  2047. return -EMSGSIZE;
  2048. }
  2049. static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
  2050. xfrm_address_t *addr)
  2051. {
  2052. struct sk_buff *skb;
  2053. size_t len;
  2054. len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
  2055. skb = alloc_skb(len, GFP_ATOMIC);
  2056. if (skb == NULL)
  2057. return -ENOMEM;
  2058. if (build_report(skb, proto, sel, addr) < 0)
  2059. BUG();
  2060. NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
  2061. return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
  2062. }
  2063. static struct xfrm_mgr netlink_mgr = {
  2064. .id = "netlink",
  2065. .notify = xfrm_send_state_notify,
  2066. .acquire = xfrm_send_acquire,
  2067. .compile_policy = xfrm_compile_policy,
  2068. .notify_policy = xfrm_send_policy_notify,
  2069. .report = xfrm_send_report,
  2070. .migrate = xfrm_send_migrate,
  2071. };
  2072. static int __init xfrm_user_init(void)
  2073. {
  2074. struct sock *nlsk;
  2075. printk(KERN_INFO "Initializing XFRM netlink socket\n");
  2076. nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
  2077. xfrm_netlink_rcv, NULL, THIS_MODULE);
  2078. if (nlsk == NULL)
  2079. return -ENOMEM;
  2080. rcu_assign_pointer(xfrm_nl, nlsk);
  2081. xfrm_register_km(&netlink_mgr);
  2082. return 0;
  2083. }
  2084. static void __exit xfrm_user_exit(void)
  2085. {
  2086. struct sock *nlsk = xfrm_nl;
  2087. xfrm_unregister_km(&netlink_mgr);
  2088. rcu_assign_pointer(xfrm_nl, NULL);
  2089. synchronize_rcu();
  2090. sock_release(nlsk->sk_socket);
  2091. }
  2092. module_init(xfrm_user_init);
  2093. module_exit(xfrm_user_exit);
  2094. MODULE_LICENSE("GPL");
  2095. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);