xfrm_user.c 55 KB

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