xfrm_user.c 64 KB

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