xfrm_user.c 55 KB

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