xfrm_user.c 57 KB

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