xfrm_user.c 57 KB

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