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