xfrm_user.c 61 KB

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