xfrm_user.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769
  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, u32 pid, u32 seq, u32 flags)
  640. {
  641. struct xfrmk_spdinfo si;
  642. struct xfrmu_spdinfo spc;
  643. struct xfrmu_spdhinfo sph;
  644. struct nlmsghdr *nlh;
  645. u32 *f;
  646. nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
  647. if (nlh == NULL) /* shouldnt really happen ... */
  648. return -EMSGSIZE;
  649. f = nlmsg_data(nlh);
  650. *f = flags;
  651. xfrm_spd_getinfo(&si);
  652. spc.incnt = si.incnt;
  653. spc.outcnt = si.outcnt;
  654. spc.fwdcnt = si.fwdcnt;
  655. spc.inscnt = si.inscnt;
  656. spc.outscnt = si.outscnt;
  657. spc.fwdscnt = si.fwdscnt;
  658. sph.spdhcnt = si.spdhcnt;
  659. sph.spdhmcnt = si.spdhmcnt;
  660. NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
  661. NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
  662. return nlmsg_end(skb, nlh);
  663. nla_put_failure:
  664. nlmsg_cancel(skb, nlh);
  665. return -EMSGSIZE;
  666. }
  667. static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  668. struct nlattr **attrs)
  669. {
  670. struct net *net = sock_net(skb->sk);
  671. struct sk_buff *r_skb;
  672. u32 *flags = nlmsg_data(nlh);
  673. u32 spid = NETLINK_CB(skb).pid;
  674. u32 seq = nlh->nlmsg_seq;
  675. r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
  676. if (r_skb == NULL)
  677. return -ENOMEM;
  678. if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
  679. BUG();
  680. return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
  681. }
  682. static inline size_t xfrm_sadinfo_msgsize(void)
  683. {
  684. return NLMSG_ALIGN(4)
  685. + nla_total_size(sizeof(struct xfrmu_sadhinfo))
  686. + nla_total_size(4); /* XFRMA_SAD_CNT */
  687. }
  688. static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
  689. {
  690. struct xfrmk_sadinfo si;
  691. struct xfrmu_sadhinfo sh;
  692. struct nlmsghdr *nlh;
  693. u32 *f;
  694. nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
  695. if (nlh == NULL) /* shouldnt really happen ... */
  696. return -EMSGSIZE;
  697. f = nlmsg_data(nlh);
  698. *f = flags;
  699. xfrm_sad_getinfo(&si);
  700. sh.sadhmcnt = si.sadhmcnt;
  701. sh.sadhcnt = si.sadhcnt;
  702. NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
  703. NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
  704. return nlmsg_end(skb, nlh);
  705. nla_put_failure:
  706. nlmsg_cancel(skb, nlh);
  707. return -EMSGSIZE;
  708. }
  709. static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  710. struct nlattr **attrs)
  711. {
  712. struct net *net = sock_net(skb->sk);
  713. struct sk_buff *r_skb;
  714. u32 *flags = nlmsg_data(nlh);
  715. u32 spid = NETLINK_CB(skb).pid;
  716. u32 seq = nlh->nlmsg_seq;
  717. r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
  718. if (r_skb == NULL)
  719. return -ENOMEM;
  720. if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
  721. BUG();
  722. return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
  723. }
  724. static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  725. struct nlattr **attrs)
  726. {
  727. struct net *net = sock_net(skb->sk);
  728. struct xfrm_usersa_id *p = nlmsg_data(nlh);
  729. struct xfrm_state *x;
  730. struct sk_buff *resp_skb;
  731. int err = -ESRCH;
  732. x = xfrm_user_state_lookup(net, p, attrs, &err);
  733. if (x == NULL)
  734. goto out_noput;
  735. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  736. if (IS_ERR(resp_skb)) {
  737. err = PTR_ERR(resp_skb);
  738. } else {
  739. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
  740. }
  741. xfrm_state_put(x);
  742. out_noput:
  743. return err;
  744. }
  745. static int verify_userspi_info(struct xfrm_userspi_info *p)
  746. {
  747. switch (p->info.id.proto) {
  748. case IPPROTO_AH:
  749. case IPPROTO_ESP:
  750. break;
  751. case IPPROTO_COMP:
  752. /* IPCOMP spi is 16-bits. */
  753. if (p->max >= 0x10000)
  754. return -EINVAL;
  755. break;
  756. default:
  757. return -EINVAL;
  758. }
  759. if (p->min > p->max)
  760. return -EINVAL;
  761. return 0;
  762. }
  763. static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
  764. struct nlattr **attrs)
  765. {
  766. struct net *net = sock_net(skb->sk);
  767. struct xfrm_state *x;
  768. struct xfrm_userspi_info *p;
  769. struct sk_buff *resp_skb;
  770. xfrm_address_t *daddr;
  771. int family;
  772. int err;
  773. p = nlmsg_data(nlh);
  774. err = verify_userspi_info(p);
  775. if (err)
  776. goto out_noput;
  777. family = p->info.family;
  778. daddr = &p->info.id.daddr;
  779. x = NULL;
  780. if (p->info.seq) {
  781. x = xfrm_find_acq_byseq(net, p->info.seq);
  782. if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
  783. xfrm_state_put(x);
  784. x = NULL;
  785. }
  786. }
  787. if (!x)
  788. x = xfrm_find_acq(net, p->info.mode, p->info.reqid,
  789. p->info.id.proto, daddr,
  790. &p->info.saddr, 1,
  791. family);
  792. err = -ENOENT;
  793. if (x == NULL)
  794. goto out_noput;
  795. err = xfrm_alloc_spi(x, p->min, p->max);
  796. if (err)
  797. goto out;
  798. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  799. if (IS_ERR(resp_skb)) {
  800. err = PTR_ERR(resp_skb);
  801. goto out;
  802. }
  803. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
  804. out:
  805. xfrm_state_put(x);
  806. out_noput:
  807. return err;
  808. }
  809. static int verify_policy_dir(u8 dir)
  810. {
  811. switch (dir) {
  812. case XFRM_POLICY_IN:
  813. case XFRM_POLICY_OUT:
  814. case XFRM_POLICY_FWD:
  815. break;
  816. default:
  817. return -EINVAL;
  818. }
  819. return 0;
  820. }
  821. static int verify_policy_type(u8 type)
  822. {
  823. switch (type) {
  824. case XFRM_POLICY_TYPE_MAIN:
  825. #ifdef CONFIG_XFRM_SUB_POLICY
  826. case XFRM_POLICY_TYPE_SUB:
  827. #endif
  828. break;
  829. default:
  830. return -EINVAL;
  831. }
  832. return 0;
  833. }
  834. static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
  835. {
  836. switch (p->share) {
  837. case XFRM_SHARE_ANY:
  838. case XFRM_SHARE_SESSION:
  839. case XFRM_SHARE_USER:
  840. case XFRM_SHARE_UNIQUE:
  841. break;
  842. default:
  843. return -EINVAL;
  844. }
  845. switch (p->action) {
  846. case XFRM_POLICY_ALLOW:
  847. case XFRM_POLICY_BLOCK:
  848. break;
  849. default:
  850. return -EINVAL;
  851. }
  852. switch (p->sel.family) {
  853. case AF_INET:
  854. break;
  855. case AF_INET6:
  856. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  857. break;
  858. #else
  859. return -EAFNOSUPPORT;
  860. #endif
  861. default:
  862. return -EINVAL;
  863. }
  864. return verify_policy_dir(p->dir);
  865. }
  866. static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
  867. {
  868. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  869. struct xfrm_user_sec_ctx *uctx;
  870. if (!rt)
  871. return 0;
  872. uctx = nla_data(rt);
  873. return security_xfrm_policy_alloc(&pol->security, uctx);
  874. }
  875. static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
  876. int nr)
  877. {
  878. int i;
  879. xp->xfrm_nr = nr;
  880. for (i = 0; i < nr; i++, ut++) {
  881. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  882. memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
  883. memcpy(&t->saddr, &ut->saddr,
  884. sizeof(xfrm_address_t));
  885. t->reqid = ut->reqid;
  886. t->mode = ut->mode;
  887. t->share = ut->share;
  888. t->optional = ut->optional;
  889. t->aalgos = ut->aalgos;
  890. t->ealgos = ut->ealgos;
  891. t->calgos = ut->calgos;
  892. /* If all masks are ~0, then we allow all algorithms. */
  893. t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
  894. t->encap_family = ut->family;
  895. }
  896. }
  897. static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
  898. {
  899. int i;
  900. if (nr > XFRM_MAX_DEPTH)
  901. return -EINVAL;
  902. for (i = 0; i < nr; i++) {
  903. /* We never validated the ut->family value, so many
  904. * applications simply leave it at zero. The check was
  905. * never made and ut->family was ignored because all
  906. * templates could be assumed to have the same family as
  907. * the policy itself. Now that we will have ipv4-in-ipv6
  908. * and ipv6-in-ipv4 tunnels, this is no longer true.
  909. */
  910. if (!ut[i].family)
  911. ut[i].family = family;
  912. switch (ut[i].family) {
  913. case AF_INET:
  914. break;
  915. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  916. case AF_INET6:
  917. break;
  918. #endif
  919. default:
  920. return -EINVAL;
  921. }
  922. }
  923. return 0;
  924. }
  925. static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
  926. {
  927. struct nlattr *rt = attrs[XFRMA_TMPL];
  928. if (!rt) {
  929. pol->xfrm_nr = 0;
  930. } else {
  931. struct xfrm_user_tmpl *utmpl = nla_data(rt);
  932. int nr = nla_len(rt) / sizeof(*utmpl);
  933. int err;
  934. err = validate_tmpl(nr, utmpl, pol->family);
  935. if (err)
  936. return err;
  937. copy_templates(pol, utmpl, nr);
  938. }
  939. return 0;
  940. }
  941. static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
  942. {
  943. struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
  944. struct xfrm_userpolicy_type *upt;
  945. u8 type = XFRM_POLICY_TYPE_MAIN;
  946. int err;
  947. if (rt) {
  948. upt = nla_data(rt);
  949. type = upt->type;
  950. }
  951. err = verify_policy_type(type);
  952. if (err)
  953. return err;
  954. *tp = type;
  955. return 0;
  956. }
  957. static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
  958. {
  959. xp->priority = p->priority;
  960. xp->index = p->index;
  961. memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
  962. memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
  963. xp->action = p->action;
  964. xp->flags = p->flags;
  965. xp->family = p->sel.family;
  966. /* XXX xp->share = p->share; */
  967. }
  968. static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
  969. {
  970. memcpy(&p->sel, &xp->selector, sizeof(p->sel));
  971. memcpy(&p->lft, &xp->lft, sizeof(p->lft));
  972. memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
  973. p->priority = xp->priority;
  974. p->index = xp->index;
  975. p->sel.family = xp->family;
  976. p->dir = dir;
  977. p->action = xp->action;
  978. p->flags = xp->flags;
  979. p->share = XFRM_SHARE_ANY; /* XXX xp->share */
  980. }
  981. static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
  982. {
  983. struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
  984. int err;
  985. if (!xp) {
  986. *errp = -ENOMEM;
  987. return NULL;
  988. }
  989. copy_from_user_policy(xp, p);
  990. err = copy_from_user_policy_type(&xp->type, attrs);
  991. if (err)
  992. goto error;
  993. if (!(err = copy_from_user_tmpl(xp, attrs)))
  994. err = copy_from_user_sec_ctx(xp, attrs);
  995. if (err)
  996. goto error;
  997. return xp;
  998. error:
  999. *errp = err;
  1000. xp->walk.dead = 1;
  1001. xfrm_policy_destroy(xp);
  1002. return NULL;
  1003. }
  1004. static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1005. struct nlattr **attrs)
  1006. {
  1007. struct net *net = sock_net(skb->sk);
  1008. struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
  1009. struct xfrm_policy *xp;
  1010. struct km_event c;
  1011. int err;
  1012. int excl;
  1013. uid_t loginuid = NETLINK_CB(skb).loginuid;
  1014. u32 sessionid = NETLINK_CB(skb).sessionid;
  1015. u32 sid = NETLINK_CB(skb).sid;
  1016. err = verify_newpolicy_info(p);
  1017. if (err)
  1018. return err;
  1019. err = verify_sec_ctx_len(attrs);
  1020. if (err)
  1021. return err;
  1022. xp = xfrm_policy_construct(net, p, attrs, &err);
  1023. if (!xp)
  1024. return err;
  1025. /* shouldnt excl be based on nlh flags??
  1026. * Aha! this is anti-netlink really i.e more pfkey derived
  1027. * in netlink excl is a flag and you wouldnt need
  1028. * a type XFRM_MSG_UPDPOLICY - JHS */
  1029. excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
  1030. err = xfrm_policy_insert(p->dir, xp, excl);
  1031. xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
  1032. if (err) {
  1033. security_xfrm_policy_free(xp->security);
  1034. kfree(xp);
  1035. return err;
  1036. }
  1037. c.event = nlh->nlmsg_type;
  1038. c.seq = nlh->nlmsg_seq;
  1039. c.pid = nlh->nlmsg_pid;
  1040. km_policy_notify(xp, p->dir, &c);
  1041. xfrm_pol_put(xp);
  1042. return 0;
  1043. }
  1044. static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
  1045. {
  1046. struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
  1047. int i;
  1048. if (xp->xfrm_nr == 0)
  1049. return 0;
  1050. for (i = 0; i < xp->xfrm_nr; i++) {
  1051. struct xfrm_user_tmpl *up = &vec[i];
  1052. struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
  1053. memcpy(&up->id, &kp->id, sizeof(up->id));
  1054. up->family = kp->encap_family;
  1055. memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
  1056. up->reqid = kp->reqid;
  1057. up->mode = kp->mode;
  1058. up->share = kp->share;
  1059. up->optional = kp->optional;
  1060. up->aalgos = kp->aalgos;
  1061. up->ealgos = kp->ealgos;
  1062. up->calgos = kp->calgos;
  1063. }
  1064. return nla_put(skb, XFRMA_TMPL,
  1065. sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
  1066. }
  1067. static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
  1068. {
  1069. if (x->security) {
  1070. return copy_sec_ctx(x->security, skb);
  1071. }
  1072. return 0;
  1073. }
  1074. static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
  1075. {
  1076. if (xp->security) {
  1077. return copy_sec_ctx(xp->security, skb);
  1078. }
  1079. return 0;
  1080. }
  1081. static inline size_t userpolicy_type_attrsize(void)
  1082. {
  1083. #ifdef CONFIG_XFRM_SUB_POLICY
  1084. return nla_total_size(sizeof(struct xfrm_userpolicy_type));
  1085. #else
  1086. return 0;
  1087. #endif
  1088. }
  1089. #ifdef CONFIG_XFRM_SUB_POLICY
  1090. static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1091. {
  1092. struct xfrm_userpolicy_type upt = {
  1093. .type = type,
  1094. };
  1095. return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
  1096. }
  1097. #else
  1098. static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1099. {
  1100. return 0;
  1101. }
  1102. #endif
  1103. static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
  1104. {
  1105. struct xfrm_dump_info *sp = ptr;
  1106. struct xfrm_userpolicy_info *p;
  1107. struct sk_buff *in_skb = sp->in_skb;
  1108. struct sk_buff *skb = sp->out_skb;
  1109. struct nlmsghdr *nlh;
  1110. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
  1111. XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
  1112. if (nlh == NULL)
  1113. return -EMSGSIZE;
  1114. p = nlmsg_data(nlh);
  1115. copy_to_user_policy(xp, p, dir);
  1116. if (copy_to_user_tmpl(xp, skb) < 0)
  1117. goto nlmsg_failure;
  1118. if (copy_to_user_sec_ctx(xp, skb))
  1119. goto nlmsg_failure;
  1120. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1121. goto nlmsg_failure;
  1122. nlmsg_end(skb, nlh);
  1123. return 0;
  1124. nlmsg_failure:
  1125. nlmsg_cancel(skb, nlh);
  1126. return -EMSGSIZE;
  1127. }
  1128. static int xfrm_dump_policy_done(struct netlink_callback *cb)
  1129. {
  1130. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
  1131. xfrm_policy_walk_done(walk);
  1132. return 0;
  1133. }
  1134. static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
  1135. {
  1136. struct net *net = sock_net(skb->sk);
  1137. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
  1138. struct xfrm_dump_info info;
  1139. BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
  1140. sizeof(cb->args) - sizeof(cb->args[0]));
  1141. info.in_skb = cb->skb;
  1142. info.out_skb = skb;
  1143. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  1144. info.nlmsg_flags = NLM_F_MULTI;
  1145. if (!cb->args[0]) {
  1146. cb->args[0] = 1;
  1147. xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
  1148. }
  1149. (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
  1150. return skb->len;
  1151. }
  1152. static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
  1153. struct xfrm_policy *xp,
  1154. int dir, u32 seq)
  1155. {
  1156. struct xfrm_dump_info info;
  1157. struct sk_buff *skb;
  1158. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1159. if (!skb)
  1160. return ERR_PTR(-ENOMEM);
  1161. info.in_skb = in_skb;
  1162. info.out_skb = skb;
  1163. info.nlmsg_seq = seq;
  1164. info.nlmsg_flags = 0;
  1165. if (dump_one_policy(xp, dir, 0, &info) < 0) {
  1166. kfree_skb(skb);
  1167. return NULL;
  1168. }
  1169. return skb;
  1170. }
  1171. static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1172. struct nlattr **attrs)
  1173. {
  1174. struct net *net = sock_net(skb->sk);
  1175. struct xfrm_policy *xp;
  1176. struct xfrm_userpolicy_id *p;
  1177. u8 type = XFRM_POLICY_TYPE_MAIN;
  1178. int err;
  1179. struct km_event c;
  1180. int delete;
  1181. p = nlmsg_data(nlh);
  1182. delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
  1183. err = copy_from_user_policy_type(&type, attrs);
  1184. if (err)
  1185. return err;
  1186. err = verify_policy_dir(p->dir);
  1187. if (err)
  1188. return err;
  1189. if (p->index)
  1190. xp = xfrm_policy_byid(net, type, p->dir, p->index, delete, &err);
  1191. else {
  1192. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1193. struct xfrm_sec_ctx *ctx;
  1194. err = verify_sec_ctx_len(attrs);
  1195. if (err)
  1196. return err;
  1197. ctx = NULL;
  1198. if (rt) {
  1199. struct xfrm_user_sec_ctx *uctx = nla_data(rt);
  1200. err = security_xfrm_policy_alloc(&ctx, uctx);
  1201. if (err)
  1202. return err;
  1203. }
  1204. xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx,
  1205. delete, &err);
  1206. security_xfrm_policy_free(ctx);
  1207. }
  1208. if (xp == NULL)
  1209. return -ENOENT;
  1210. if (!delete) {
  1211. struct sk_buff *resp_skb;
  1212. resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
  1213. if (IS_ERR(resp_skb)) {
  1214. err = PTR_ERR(resp_skb);
  1215. } else {
  1216. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
  1217. NETLINK_CB(skb).pid);
  1218. }
  1219. } else {
  1220. uid_t loginuid = NETLINK_CB(skb).loginuid;
  1221. u32 sessionid = NETLINK_CB(skb).sessionid;
  1222. u32 sid = NETLINK_CB(skb).sid;
  1223. xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
  1224. sid);
  1225. if (err != 0)
  1226. goto out;
  1227. c.data.byid = p->index;
  1228. c.event = nlh->nlmsg_type;
  1229. c.seq = nlh->nlmsg_seq;
  1230. c.pid = nlh->nlmsg_pid;
  1231. km_policy_notify(xp, p->dir, &c);
  1232. }
  1233. out:
  1234. xfrm_pol_put(xp);
  1235. return err;
  1236. }
  1237. static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  1238. struct nlattr **attrs)
  1239. {
  1240. struct net *net = sock_net(skb->sk);
  1241. struct km_event c;
  1242. struct xfrm_usersa_flush *p = nlmsg_data(nlh);
  1243. struct xfrm_audit audit_info;
  1244. int err;
  1245. audit_info.loginuid = NETLINK_CB(skb).loginuid;
  1246. audit_info.sessionid = NETLINK_CB(skb).sessionid;
  1247. audit_info.secid = NETLINK_CB(skb).sid;
  1248. err = xfrm_state_flush(net, p->proto, &audit_info);
  1249. if (err)
  1250. return err;
  1251. c.data.proto = p->proto;
  1252. c.event = nlh->nlmsg_type;
  1253. c.seq = nlh->nlmsg_seq;
  1254. c.pid = nlh->nlmsg_pid;
  1255. c.net = net;
  1256. km_state_notify(NULL, &c);
  1257. return 0;
  1258. }
  1259. static inline size_t xfrm_aevent_msgsize(void)
  1260. {
  1261. return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
  1262. + nla_total_size(sizeof(struct xfrm_replay_state))
  1263. + nla_total_size(sizeof(struct xfrm_lifetime_cur))
  1264. + nla_total_size(4) /* XFRM_AE_RTHR */
  1265. + nla_total_size(4); /* XFRM_AE_ETHR */
  1266. }
  1267. static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
  1268. {
  1269. struct xfrm_aevent_id *id;
  1270. struct nlmsghdr *nlh;
  1271. nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
  1272. if (nlh == NULL)
  1273. return -EMSGSIZE;
  1274. id = nlmsg_data(nlh);
  1275. memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
  1276. id->sa_id.spi = x->id.spi;
  1277. id->sa_id.family = x->props.family;
  1278. id->sa_id.proto = x->id.proto;
  1279. memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
  1280. id->reqid = x->props.reqid;
  1281. id->flags = c->data.aevent;
  1282. NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
  1283. NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
  1284. if (id->flags & XFRM_AE_RTHR)
  1285. NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
  1286. if (id->flags & XFRM_AE_ETHR)
  1287. NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
  1288. x->replay_maxage * 10 / HZ);
  1289. return nlmsg_end(skb, nlh);
  1290. nla_put_failure:
  1291. nlmsg_cancel(skb, nlh);
  1292. return -EMSGSIZE;
  1293. }
  1294. static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1295. struct nlattr **attrs)
  1296. {
  1297. struct net *net = sock_net(skb->sk);
  1298. struct xfrm_state *x;
  1299. struct sk_buff *r_skb;
  1300. int err;
  1301. struct km_event c;
  1302. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  1303. struct xfrm_usersa_id *id = &p->sa_id;
  1304. r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
  1305. if (r_skb == NULL)
  1306. return -ENOMEM;
  1307. x = xfrm_state_lookup(net, &id->daddr, id->spi, id->proto, id->family);
  1308. if (x == NULL) {
  1309. kfree_skb(r_skb);
  1310. return -ESRCH;
  1311. }
  1312. /*
  1313. * XXX: is this lock really needed - none of the other
  1314. * gets lock (the concern is things getting updated
  1315. * while we are still reading) - jhs
  1316. */
  1317. spin_lock_bh(&x->lock);
  1318. c.data.aevent = p->flags;
  1319. c.seq = nlh->nlmsg_seq;
  1320. c.pid = nlh->nlmsg_pid;
  1321. if (build_aevent(r_skb, x, &c) < 0)
  1322. BUG();
  1323. err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
  1324. spin_unlock_bh(&x->lock);
  1325. xfrm_state_put(x);
  1326. return err;
  1327. }
  1328. static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1329. struct nlattr **attrs)
  1330. {
  1331. struct net *net = sock_net(skb->sk);
  1332. struct xfrm_state *x;
  1333. struct km_event c;
  1334. int err = - EINVAL;
  1335. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  1336. struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
  1337. struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
  1338. if (!lt && !rp)
  1339. return err;
  1340. /* pedantic mode - thou shalt sayeth replaceth */
  1341. if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
  1342. return err;
  1343. x = xfrm_state_lookup(net, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
  1344. if (x == NULL)
  1345. return -ESRCH;
  1346. if (x->km.state != XFRM_STATE_VALID)
  1347. goto out;
  1348. spin_lock_bh(&x->lock);
  1349. xfrm_update_ae_params(x, attrs);
  1350. spin_unlock_bh(&x->lock);
  1351. c.event = nlh->nlmsg_type;
  1352. c.seq = nlh->nlmsg_seq;
  1353. c.pid = nlh->nlmsg_pid;
  1354. c.data.aevent = XFRM_AE_CU;
  1355. km_state_notify(x, &c);
  1356. err = 0;
  1357. out:
  1358. xfrm_state_put(x);
  1359. return err;
  1360. }
  1361. static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1362. struct nlattr **attrs)
  1363. {
  1364. struct net *net = sock_net(skb->sk);
  1365. struct km_event c;
  1366. u8 type = XFRM_POLICY_TYPE_MAIN;
  1367. int err;
  1368. struct xfrm_audit audit_info;
  1369. err = copy_from_user_policy_type(&type, attrs);
  1370. if (err)
  1371. return err;
  1372. audit_info.loginuid = NETLINK_CB(skb).loginuid;
  1373. audit_info.sessionid = NETLINK_CB(skb).sessionid;
  1374. audit_info.secid = NETLINK_CB(skb).sid;
  1375. err = xfrm_policy_flush(net, type, &audit_info);
  1376. if (err)
  1377. return err;
  1378. c.data.type = type;
  1379. c.event = nlh->nlmsg_type;
  1380. c.seq = nlh->nlmsg_seq;
  1381. c.pid = nlh->nlmsg_pid;
  1382. c.net = net;
  1383. km_policy_notify(NULL, 0, &c);
  1384. return 0;
  1385. }
  1386. static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1387. struct nlattr **attrs)
  1388. {
  1389. struct net *net = sock_net(skb->sk);
  1390. struct xfrm_policy *xp;
  1391. struct xfrm_user_polexpire *up = nlmsg_data(nlh);
  1392. struct xfrm_userpolicy_info *p = &up->pol;
  1393. u8 type = XFRM_POLICY_TYPE_MAIN;
  1394. int err = -ENOENT;
  1395. err = copy_from_user_policy_type(&type, attrs);
  1396. if (err)
  1397. return err;
  1398. if (p->index)
  1399. xp = xfrm_policy_byid(net, type, p->dir, p->index, 0, &err);
  1400. else {
  1401. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1402. struct xfrm_sec_ctx *ctx;
  1403. err = verify_sec_ctx_len(attrs);
  1404. if (err)
  1405. return err;
  1406. ctx = NULL;
  1407. if (rt) {
  1408. struct xfrm_user_sec_ctx *uctx = nla_data(rt);
  1409. err = security_xfrm_policy_alloc(&ctx, uctx);
  1410. if (err)
  1411. return err;
  1412. }
  1413. xp = xfrm_policy_bysel_ctx(net, type, p->dir, &p->sel, ctx, 0, &err);
  1414. security_xfrm_policy_free(ctx);
  1415. }
  1416. if (xp == NULL)
  1417. return -ENOENT;
  1418. read_lock(&xp->lock);
  1419. if (xp->walk.dead) {
  1420. read_unlock(&xp->lock);
  1421. goto out;
  1422. }
  1423. read_unlock(&xp->lock);
  1424. err = 0;
  1425. if (up->hard) {
  1426. uid_t loginuid = NETLINK_CB(skb).loginuid;
  1427. uid_t sessionid = NETLINK_CB(skb).sessionid;
  1428. u32 sid = NETLINK_CB(skb).sid;
  1429. xfrm_policy_delete(xp, p->dir);
  1430. xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
  1431. } else {
  1432. // reset the timers here?
  1433. printk("Dont know what to do with soft policy expire\n");
  1434. }
  1435. km_policy_expired(xp, p->dir, up->hard, current->pid);
  1436. out:
  1437. xfrm_pol_put(xp);
  1438. return err;
  1439. }
  1440. static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1441. struct nlattr **attrs)
  1442. {
  1443. struct net *net = sock_net(skb->sk);
  1444. struct xfrm_state *x;
  1445. int err;
  1446. struct xfrm_user_expire *ue = nlmsg_data(nlh);
  1447. struct xfrm_usersa_info *p = &ue->state;
  1448. x = xfrm_state_lookup(net, &p->id.daddr, p->id.spi, p->id.proto, p->family);
  1449. err = -ENOENT;
  1450. if (x == NULL)
  1451. return err;
  1452. spin_lock_bh(&x->lock);
  1453. err = -EINVAL;
  1454. if (x->km.state != XFRM_STATE_VALID)
  1455. goto out;
  1456. km_state_expired(x, ue->hard, current->pid);
  1457. if (ue->hard) {
  1458. uid_t loginuid = NETLINK_CB(skb).loginuid;
  1459. uid_t sessionid = NETLINK_CB(skb).sessionid;
  1460. u32 sid = NETLINK_CB(skb).sid;
  1461. __xfrm_state_delete(x);
  1462. xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
  1463. }
  1464. err = 0;
  1465. out:
  1466. spin_unlock_bh(&x->lock);
  1467. xfrm_state_put(x);
  1468. return err;
  1469. }
  1470. static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1471. struct nlattr **attrs)
  1472. {
  1473. struct net *net = sock_net(skb->sk);
  1474. struct xfrm_policy *xp;
  1475. struct xfrm_user_tmpl *ut;
  1476. int i;
  1477. struct nlattr *rt = attrs[XFRMA_TMPL];
  1478. struct xfrm_user_acquire *ua = nlmsg_data(nlh);
  1479. struct xfrm_state *x = xfrm_state_alloc(net);
  1480. int err = -ENOMEM;
  1481. if (!x)
  1482. goto nomem;
  1483. err = verify_newpolicy_info(&ua->policy);
  1484. if (err)
  1485. goto bad_policy;
  1486. /* build an XP */
  1487. xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
  1488. if (!xp)
  1489. goto free_state;
  1490. memcpy(&x->id, &ua->id, sizeof(ua->id));
  1491. memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
  1492. memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
  1493. ut = nla_data(rt);
  1494. /* extract the templates and for each call km_key */
  1495. for (i = 0; i < xp->xfrm_nr; i++, ut++) {
  1496. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  1497. memcpy(&x->id, &t->id, sizeof(x->id));
  1498. x->props.mode = t->mode;
  1499. x->props.reqid = t->reqid;
  1500. x->props.family = ut->family;
  1501. t->aalgos = ua->aalgos;
  1502. t->ealgos = ua->ealgos;
  1503. t->calgos = ua->calgos;
  1504. err = km_query(x, t, xp);
  1505. }
  1506. kfree(x);
  1507. kfree(xp);
  1508. return 0;
  1509. bad_policy:
  1510. printk("BAD policy passed\n");
  1511. free_state:
  1512. kfree(x);
  1513. nomem:
  1514. return err;
  1515. }
  1516. #ifdef CONFIG_XFRM_MIGRATE
  1517. static int copy_from_user_migrate(struct xfrm_migrate *ma,
  1518. struct xfrm_kmaddress *k,
  1519. struct nlattr **attrs, int *num)
  1520. {
  1521. struct nlattr *rt = attrs[XFRMA_MIGRATE];
  1522. struct xfrm_user_migrate *um;
  1523. int i, num_migrate;
  1524. if (k != NULL) {
  1525. struct xfrm_user_kmaddress *uk;
  1526. uk = nla_data(attrs[XFRMA_KMADDRESS]);
  1527. memcpy(&k->local, &uk->local, sizeof(k->local));
  1528. memcpy(&k->remote, &uk->remote, sizeof(k->remote));
  1529. k->family = uk->family;
  1530. k->reserved = uk->reserved;
  1531. }
  1532. um = nla_data(rt);
  1533. num_migrate = nla_len(rt) / sizeof(*um);
  1534. if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
  1535. return -EINVAL;
  1536. for (i = 0; i < num_migrate; i++, um++, ma++) {
  1537. memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
  1538. memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
  1539. memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
  1540. memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
  1541. ma->proto = um->proto;
  1542. ma->mode = um->mode;
  1543. ma->reqid = um->reqid;
  1544. ma->old_family = um->old_family;
  1545. ma->new_family = um->new_family;
  1546. }
  1547. *num = i;
  1548. return 0;
  1549. }
  1550. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1551. struct nlattr **attrs)
  1552. {
  1553. struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
  1554. struct xfrm_migrate m[XFRM_MAX_DEPTH];
  1555. struct xfrm_kmaddress km, *kmp;
  1556. u8 type;
  1557. int err;
  1558. int n = 0;
  1559. if (attrs[XFRMA_MIGRATE] == NULL)
  1560. return -EINVAL;
  1561. kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
  1562. err = copy_from_user_policy_type(&type, attrs);
  1563. if (err)
  1564. return err;
  1565. err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
  1566. if (err)
  1567. return err;
  1568. if (!n)
  1569. return 0;
  1570. xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
  1571. return 0;
  1572. }
  1573. #else
  1574. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1575. struct nlattr **attrs)
  1576. {
  1577. return -ENOPROTOOPT;
  1578. }
  1579. #endif
  1580. #ifdef CONFIG_XFRM_MIGRATE
  1581. static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
  1582. {
  1583. struct xfrm_user_migrate um;
  1584. memset(&um, 0, sizeof(um));
  1585. um.proto = m->proto;
  1586. um.mode = m->mode;
  1587. um.reqid = m->reqid;
  1588. um.old_family = m->old_family;
  1589. memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
  1590. memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
  1591. um.new_family = m->new_family;
  1592. memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
  1593. memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
  1594. return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
  1595. }
  1596. static int copy_to_user_kmaddress(struct xfrm_kmaddress *k, struct sk_buff *skb)
  1597. {
  1598. struct xfrm_user_kmaddress uk;
  1599. memset(&uk, 0, sizeof(uk));
  1600. uk.family = k->family;
  1601. uk.reserved = k->reserved;
  1602. memcpy(&uk.local, &k->local, sizeof(uk.local));
  1603. memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
  1604. return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
  1605. }
  1606. static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
  1607. {
  1608. return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
  1609. + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
  1610. + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
  1611. + userpolicy_type_attrsize();
  1612. }
  1613. static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
  1614. int num_migrate, struct xfrm_kmaddress *k,
  1615. struct xfrm_selector *sel, u8 dir, u8 type)
  1616. {
  1617. struct xfrm_migrate *mp;
  1618. struct xfrm_userpolicy_id *pol_id;
  1619. struct nlmsghdr *nlh;
  1620. int i;
  1621. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
  1622. if (nlh == NULL)
  1623. return -EMSGSIZE;
  1624. pol_id = nlmsg_data(nlh);
  1625. /* copy data from selector, dir, and type to the pol_id */
  1626. memset(pol_id, 0, sizeof(*pol_id));
  1627. memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
  1628. pol_id->dir = dir;
  1629. if (k != NULL && (copy_to_user_kmaddress(k, skb) < 0))
  1630. goto nlmsg_failure;
  1631. if (copy_to_user_policy_type(type, skb) < 0)
  1632. goto nlmsg_failure;
  1633. for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
  1634. if (copy_to_user_migrate(mp, skb) < 0)
  1635. goto nlmsg_failure;
  1636. }
  1637. return nlmsg_end(skb, nlh);
  1638. nlmsg_failure:
  1639. nlmsg_cancel(skb, nlh);
  1640. return -EMSGSIZE;
  1641. }
  1642. static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  1643. struct xfrm_migrate *m, int num_migrate,
  1644. struct xfrm_kmaddress *k)
  1645. {
  1646. struct net *net = &init_net;
  1647. struct sk_buff *skb;
  1648. skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
  1649. if (skb == NULL)
  1650. return -ENOMEM;
  1651. /* build migrate */
  1652. if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
  1653. BUG();
  1654. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
  1655. }
  1656. #else
  1657. static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  1658. struct xfrm_migrate *m, int num_migrate,
  1659. struct xfrm_kmaddress *k)
  1660. {
  1661. return -ENOPROTOOPT;
  1662. }
  1663. #endif
  1664. #define XMSGSIZE(type) sizeof(struct type)
  1665. static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
  1666. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1667. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1668. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1669. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1670. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1671. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1672. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
  1673. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
  1674. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
  1675. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1676. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1677. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
  1678. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  1679. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
  1680. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1681. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1682. [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
  1683. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1684. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
  1685. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
  1686. };
  1687. #undef XMSGSIZE
  1688. static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
  1689. [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
  1690. [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
  1691. [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
  1692. [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
  1693. [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
  1694. [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
  1695. [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
  1696. [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
  1697. [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
  1698. [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
  1699. [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
  1700. [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
  1701. [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
  1702. [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
  1703. [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
  1704. [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
  1705. };
  1706. static struct xfrm_link {
  1707. int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
  1708. int (*dump)(struct sk_buff *, struct netlink_callback *);
  1709. int (*done)(struct netlink_callback *);
  1710. } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
  1711. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1712. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
  1713. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
  1714. .dump = xfrm_dump_sa,
  1715. .done = xfrm_dump_sa_done },
  1716. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1717. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
  1718. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
  1719. .dump = xfrm_dump_policy,
  1720. .done = xfrm_dump_policy_done },
  1721. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
  1722. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
  1723. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
  1724. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1725. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1726. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
  1727. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
  1728. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
  1729. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
  1730. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
  1731. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
  1732. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
  1733. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
  1734. };
  1735. static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  1736. {
  1737. struct net *net = sock_net(skb->sk);
  1738. struct nlattr *attrs[XFRMA_MAX+1];
  1739. struct xfrm_link *link;
  1740. int type, err;
  1741. type = nlh->nlmsg_type;
  1742. if (type > XFRM_MSG_MAX)
  1743. return -EINVAL;
  1744. type -= XFRM_MSG_BASE;
  1745. link = &xfrm_dispatch[type];
  1746. /* All operations require privileges, even GET */
  1747. if (security_netlink_recv(skb, CAP_NET_ADMIN))
  1748. return -EPERM;
  1749. if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
  1750. type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
  1751. (nlh->nlmsg_flags & NLM_F_DUMP)) {
  1752. if (link->dump == NULL)
  1753. return -EINVAL;
  1754. return netlink_dump_start(net->xfrm.nlsk, skb, nlh, link->dump, link->done);
  1755. }
  1756. err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
  1757. xfrma_policy);
  1758. if (err < 0)
  1759. return err;
  1760. if (link->doit == NULL)
  1761. return -EINVAL;
  1762. return link->doit(skb, nlh, attrs);
  1763. }
  1764. static void xfrm_netlink_rcv(struct sk_buff *skb)
  1765. {
  1766. mutex_lock(&xfrm_cfg_mutex);
  1767. netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
  1768. mutex_unlock(&xfrm_cfg_mutex);
  1769. }
  1770. static inline size_t xfrm_expire_msgsize(void)
  1771. {
  1772. return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
  1773. }
  1774. static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
  1775. {
  1776. struct xfrm_user_expire *ue;
  1777. struct nlmsghdr *nlh;
  1778. nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
  1779. if (nlh == NULL)
  1780. return -EMSGSIZE;
  1781. ue = nlmsg_data(nlh);
  1782. copy_to_user_state(x, &ue->state);
  1783. ue->hard = (c->data.hard != 0) ? 1 : 0;
  1784. return nlmsg_end(skb, nlh);
  1785. }
  1786. static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
  1787. {
  1788. struct net *net = xs_net(x);
  1789. struct sk_buff *skb;
  1790. skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
  1791. if (skb == NULL)
  1792. return -ENOMEM;
  1793. if (build_expire(skb, x, c) < 0)
  1794. BUG();
  1795. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  1796. }
  1797. static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
  1798. {
  1799. struct net *net = xs_net(x);
  1800. struct sk_buff *skb;
  1801. skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
  1802. if (skb == NULL)
  1803. return -ENOMEM;
  1804. if (build_aevent(skb, x, c) < 0)
  1805. BUG();
  1806. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
  1807. }
  1808. static int xfrm_notify_sa_flush(struct km_event *c)
  1809. {
  1810. struct net *net = c->net;
  1811. struct xfrm_usersa_flush *p;
  1812. struct nlmsghdr *nlh;
  1813. struct sk_buff *skb;
  1814. int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
  1815. skb = nlmsg_new(len, GFP_ATOMIC);
  1816. if (skb == NULL)
  1817. return -ENOMEM;
  1818. nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
  1819. if (nlh == NULL) {
  1820. kfree_skb(skb);
  1821. return -EMSGSIZE;
  1822. }
  1823. p = nlmsg_data(nlh);
  1824. p->proto = c->data.proto;
  1825. nlmsg_end(skb, nlh);
  1826. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  1827. }
  1828. static inline size_t xfrm_sa_len(struct xfrm_state *x)
  1829. {
  1830. size_t l = 0;
  1831. if (x->aead)
  1832. l += nla_total_size(aead_len(x->aead));
  1833. if (x->aalg) {
  1834. l += nla_total_size(sizeof(struct xfrm_algo) +
  1835. (x->aalg->alg_key_len + 7) / 8);
  1836. l += nla_total_size(xfrm_alg_auth_len(x->aalg));
  1837. }
  1838. if (x->ealg)
  1839. l += nla_total_size(xfrm_alg_len(x->ealg));
  1840. if (x->calg)
  1841. l += nla_total_size(sizeof(*x->calg));
  1842. if (x->encap)
  1843. l += nla_total_size(sizeof(*x->encap));
  1844. if (x->security)
  1845. l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
  1846. x->security->ctx_len);
  1847. if (x->coaddr)
  1848. l += nla_total_size(sizeof(*x->coaddr));
  1849. /* Must count x->lastused as it may become non-zero behind our back. */
  1850. l += nla_total_size(sizeof(u64));
  1851. return l;
  1852. }
  1853. static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
  1854. {
  1855. struct net *net = xs_net(x);
  1856. struct xfrm_usersa_info *p;
  1857. struct xfrm_usersa_id *id;
  1858. struct nlmsghdr *nlh;
  1859. struct sk_buff *skb;
  1860. int len = xfrm_sa_len(x);
  1861. int headlen;
  1862. headlen = sizeof(*p);
  1863. if (c->event == XFRM_MSG_DELSA) {
  1864. len += nla_total_size(headlen);
  1865. headlen = sizeof(*id);
  1866. }
  1867. len += NLMSG_ALIGN(headlen);
  1868. skb = nlmsg_new(len, GFP_ATOMIC);
  1869. if (skb == NULL)
  1870. return -ENOMEM;
  1871. nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
  1872. if (nlh == NULL)
  1873. goto nla_put_failure;
  1874. p = nlmsg_data(nlh);
  1875. if (c->event == XFRM_MSG_DELSA) {
  1876. struct nlattr *attr;
  1877. id = nlmsg_data(nlh);
  1878. memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
  1879. id->spi = x->id.spi;
  1880. id->family = x->props.family;
  1881. id->proto = x->id.proto;
  1882. attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
  1883. if (attr == NULL)
  1884. goto nla_put_failure;
  1885. p = nla_data(attr);
  1886. }
  1887. if (copy_to_user_state_extra(x, p, skb))
  1888. goto nla_put_failure;
  1889. nlmsg_end(skb, nlh);
  1890. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  1891. nla_put_failure:
  1892. /* Somebody screwed up with xfrm_sa_len! */
  1893. WARN_ON(1);
  1894. kfree_skb(skb);
  1895. return -1;
  1896. }
  1897. static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
  1898. {
  1899. switch (c->event) {
  1900. case XFRM_MSG_EXPIRE:
  1901. return xfrm_exp_state_notify(x, c);
  1902. case XFRM_MSG_NEWAE:
  1903. return xfrm_aevent_state_notify(x, c);
  1904. case XFRM_MSG_DELSA:
  1905. case XFRM_MSG_UPDSA:
  1906. case XFRM_MSG_NEWSA:
  1907. return xfrm_notify_sa(x, c);
  1908. case XFRM_MSG_FLUSHSA:
  1909. return xfrm_notify_sa_flush(c);
  1910. default:
  1911. printk("xfrm_user: Unknown SA event %d\n", c->event);
  1912. break;
  1913. }
  1914. return 0;
  1915. }
  1916. static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
  1917. struct xfrm_policy *xp)
  1918. {
  1919. return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
  1920. + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
  1921. + nla_total_size(xfrm_user_sec_ctx_size(x->security))
  1922. + userpolicy_type_attrsize();
  1923. }
  1924. static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
  1925. struct xfrm_tmpl *xt, struct xfrm_policy *xp,
  1926. int dir)
  1927. {
  1928. struct xfrm_user_acquire *ua;
  1929. struct nlmsghdr *nlh;
  1930. __u32 seq = xfrm_get_acqseq();
  1931. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
  1932. if (nlh == NULL)
  1933. return -EMSGSIZE;
  1934. ua = nlmsg_data(nlh);
  1935. memcpy(&ua->id, &x->id, sizeof(ua->id));
  1936. memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
  1937. memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
  1938. copy_to_user_policy(xp, &ua->policy, dir);
  1939. ua->aalgos = xt->aalgos;
  1940. ua->ealgos = xt->ealgos;
  1941. ua->calgos = xt->calgos;
  1942. ua->seq = x->km.seq = seq;
  1943. if (copy_to_user_tmpl(xp, skb) < 0)
  1944. goto nlmsg_failure;
  1945. if (copy_to_user_state_sec_ctx(x, skb))
  1946. goto nlmsg_failure;
  1947. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1948. goto nlmsg_failure;
  1949. return nlmsg_end(skb, nlh);
  1950. nlmsg_failure:
  1951. nlmsg_cancel(skb, nlh);
  1952. return -EMSGSIZE;
  1953. }
  1954. static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
  1955. struct xfrm_policy *xp, int dir)
  1956. {
  1957. struct net *net = xs_net(x);
  1958. struct sk_buff *skb;
  1959. skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
  1960. if (skb == NULL)
  1961. return -ENOMEM;
  1962. if (build_acquire(skb, x, xt, xp, dir) < 0)
  1963. BUG();
  1964. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
  1965. }
  1966. /* User gives us xfrm_user_policy_info followed by an array of 0
  1967. * or more templates.
  1968. */
  1969. static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
  1970. u8 *data, int len, int *dir)
  1971. {
  1972. struct net *net = sock_net(sk);
  1973. struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
  1974. struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
  1975. struct xfrm_policy *xp;
  1976. int nr;
  1977. switch (sk->sk_family) {
  1978. case AF_INET:
  1979. if (opt != IP_XFRM_POLICY) {
  1980. *dir = -EOPNOTSUPP;
  1981. return NULL;
  1982. }
  1983. break;
  1984. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1985. case AF_INET6:
  1986. if (opt != IPV6_XFRM_POLICY) {
  1987. *dir = -EOPNOTSUPP;
  1988. return NULL;
  1989. }
  1990. break;
  1991. #endif
  1992. default:
  1993. *dir = -EINVAL;
  1994. return NULL;
  1995. }
  1996. *dir = -EINVAL;
  1997. if (len < sizeof(*p) ||
  1998. verify_newpolicy_info(p))
  1999. return NULL;
  2000. nr = ((len - sizeof(*p)) / sizeof(*ut));
  2001. if (validate_tmpl(nr, ut, p->sel.family))
  2002. return NULL;
  2003. if (p->dir > XFRM_POLICY_OUT)
  2004. return NULL;
  2005. xp = xfrm_policy_alloc(net, GFP_KERNEL);
  2006. if (xp == NULL) {
  2007. *dir = -ENOBUFS;
  2008. return NULL;
  2009. }
  2010. copy_from_user_policy(xp, p);
  2011. xp->type = XFRM_POLICY_TYPE_MAIN;
  2012. copy_templates(xp, ut, nr);
  2013. *dir = p->dir;
  2014. return xp;
  2015. }
  2016. static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
  2017. {
  2018. return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
  2019. + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
  2020. + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
  2021. + userpolicy_type_attrsize();
  2022. }
  2023. static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
  2024. int dir, struct km_event *c)
  2025. {
  2026. struct xfrm_user_polexpire *upe;
  2027. struct nlmsghdr *nlh;
  2028. int hard = c->data.hard;
  2029. nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
  2030. if (nlh == NULL)
  2031. return -EMSGSIZE;
  2032. upe = nlmsg_data(nlh);
  2033. copy_to_user_policy(xp, &upe->pol, dir);
  2034. if (copy_to_user_tmpl(xp, skb) < 0)
  2035. goto nlmsg_failure;
  2036. if (copy_to_user_sec_ctx(xp, skb))
  2037. goto nlmsg_failure;
  2038. if (copy_to_user_policy_type(xp->type, skb) < 0)
  2039. goto nlmsg_failure;
  2040. upe->hard = !!hard;
  2041. return nlmsg_end(skb, nlh);
  2042. nlmsg_failure:
  2043. nlmsg_cancel(skb, nlh);
  2044. return -EMSGSIZE;
  2045. }
  2046. static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  2047. {
  2048. struct net *net = xp_net(xp);
  2049. struct sk_buff *skb;
  2050. skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
  2051. if (skb == NULL)
  2052. return -ENOMEM;
  2053. if (build_polexpire(skb, xp, dir, c) < 0)
  2054. BUG();
  2055. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  2056. }
  2057. static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
  2058. {
  2059. struct net *net = xp_net(xp);
  2060. struct xfrm_userpolicy_info *p;
  2061. struct xfrm_userpolicy_id *id;
  2062. struct nlmsghdr *nlh;
  2063. struct sk_buff *skb;
  2064. int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  2065. int headlen;
  2066. headlen = sizeof(*p);
  2067. if (c->event == XFRM_MSG_DELPOLICY) {
  2068. len += nla_total_size(headlen);
  2069. headlen = sizeof(*id);
  2070. }
  2071. len += userpolicy_type_attrsize();
  2072. len += NLMSG_ALIGN(headlen);
  2073. skb = nlmsg_new(len, GFP_ATOMIC);
  2074. if (skb == NULL)
  2075. return -ENOMEM;
  2076. nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
  2077. if (nlh == NULL)
  2078. goto nlmsg_failure;
  2079. p = nlmsg_data(nlh);
  2080. if (c->event == XFRM_MSG_DELPOLICY) {
  2081. struct nlattr *attr;
  2082. id = nlmsg_data(nlh);
  2083. memset(id, 0, sizeof(*id));
  2084. id->dir = dir;
  2085. if (c->data.byid)
  2086. id->index = xp->index;
  2087. else
  2088. memcpy(&id->sel, &xp->selector, sizeof(id->sel));
  2089. attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
  2090. if (attr == NULL)
  2091. goto nlmsg_failure;
  2092. p = nla_data(attr);
  2093. }
  2094. copy_to_user_policy(xp, p, dir);
  2095. if (copy_to_user_tmpl(xp, skb) < 0)
  2096. goto nlmsg_failure;
  2097. if (copy_to_user_policy_type(xp->type, skb) < 0)
  2098. goto nlmsg_failure;
  2099. nlmsg_end(skb, nlh);
  2100. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  2101. nlmsg_failure:
  2102. kfree_skb(skb);
  2103. return -1;
  2104. }
  2105. static int xfrm_notify_policy_flush(struct km_event *c)
  2106. {
  2107. struct net *net = c->net;
  2108. struct nlmsghdr *nlh;
  2109. struct sk_buff *skb;
  2110. skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
  2111. if (skb == NULL)
  2112. return -ENOMEM;
  2113. nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
  2114. if (nlh == NULL)
  2115. goto nlmsg_failure;
  2116. if (copy_to_user_policy_type(c->data.type, skb) < 0)
  2117. goto nlmsg_failure;
  2118. nlmsg_end(skb, nlh);
  2119. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  2120. nlmsg_failure:
  2121. kfree_skb(skb);
  2122. return -1;
  2123. }
  2124. static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  2125. {
  2126. switch (c->event) {
  2127. case XFRM_MSG_NEWPOLICY:
  2128. case XFRM_MSG_UPDPOLICY:
  2129. case XFRM_MSG_DELPOLICY:
  2130. return xfrm_notify_policy(xp, dir, c);
  2131. case XFRM_MSG_FLUSHPOLICY:
  2132. return xfrm_notify_policy_flush(c);
  2133. case XFRM_MSG_POLEXPIRE:
  2134. return xfrm_exp_policy_notify(xp, dir, c);
  2135. default:
  2136. printk("xfrm_user: Unknown Policy event %d\n", c->event);
  2137. }
  2138. return 0;
  2139. }
  2140. static inline size_t xfrm_report_msgsize(void)
  2141. {
  2142. return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
  2143. }
  2144. static int build_report(struct sk_buff *skb, u8 proto,
  2145. struct xfrm_selector *sel, xfrm_address_t *addr)
  2146. {
  2147. struct xfrm_user_report *ur;
  2148. struct nlmsghdr *nlh;
  2149. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
  2150. if (nlh == NULL)
  2151. return -EMSGSIZE;
  2152. ur = nlmsg_data(nlh);
  2153. ur->proto = proto;
  2154. memcpy(&ur->sel, sel, sizeof(ur->sel));
  2155. if (addr)
  2156. NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
  2157. return nlmsg_end(skb, nlh);
  2158. nla_put_failure:
  2159. nlmsg_cancel(skb, nlh);
  2160. return -EMSGSIZE;
  2161. }
  2162. static int xfrm_send_report(struct net *net, u8 proto,
  2163. struct xfrm_selector *sel, xfrm_address_t *addr)
  2164. {
  2165. struct sk_buff *skb;
  2166. skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
  2167. if (skb == NULL)
  2168. return -ENOMEM;
  2169. if (build_report(skb, proto, sel, addr) < 0)
  2170. BUG();
  2171. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
  2172. }
  2173. static inline size_t xfrm_mapping_msgsize(void)
  2174. {
  2175. return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
  2176. }
  2177. static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
  2178. xfrm_address_t *new_saddr, __be16 new_sport)
  2179. {
  2180. struct xfrm_user_mapping *um;
  2181. struct nlmsghdr *nlh;
  2182. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
  2183. if (nlh == NULL)
  2184. return -EMSGSIZE;
  2185. um = nlmsg_data(nlh);
  2186. memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
  2187. um->id.spi = x->id.spi;
  2188. um->id.family = x->props.family;
  2189. um->id.proto = x->id.proto;
  2190. memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
  2191. memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
  2192. um->new_sport = new_sport;
  2193. um->old_sport = x->encap->encap_sport;
  2194. um->reqid = x->props.reqid;
  2195. return nlmsg_end(skb, nlh);
  2196. }
  2197. static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
  2198. __be16 sport)
  2199. {
  2200. struct net *net = xs_net(x);
  2201. struct sk_buff *skb;
  2202. if (x->id.proto != IPPROTO_ESP)
  2203. return -EINVAL;
  2204. if (!x->encap)
  2205. return -EINVAL;
  2206. skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
  2207. if (skb == NULL)
  2208. return -ENOMEM;
  2209. if (build_mapping(skb, x, ipaddr, sport) < 0)
  2210. BUG();
  2211. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
  2212. }
  2213. static struct xfrm_mgr netlink_mgr = {
  2214. .id = "netlink",
  2215. .notify = xfrm_send_state_notify,
  2216. .acquire = xfrm_send_acquire,
  2217. .compile_policy = xfrm_compile_policy,
  2218. .notify_policy = xfrm_send_policy_notify,
  2219. .report = xfrm_send_report,
  2220. .migrate = xfrm_send_migrate,
  2221. .new_mapping = xfrm_send_mapping,
  2222. };
  2223. static int __net_init xfrm_user_net_init(struct net *net)
  2224. {
  2225. struct sock *nlsk;
  2226. nlsk = netlink_kernel_create(net, NETLINK_XFRM, XFRMNLGRP_MAX,
  2227. xfrm_netlink_rcv, NULL, THIS_MODULE);
  2228. if (nlsk == NULL)
  2229. return -ENOMEM;
  2230. net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
  2231. rcu_assign_pointer(net->xfrm.nlsk, nlsk);
  2232. return 0;
  2233. }
  2234. static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
  2235. {
  2236. struct net *net;
  2237. list_for_each_entry(net, net_exit_list, exit_list)
  2238. rcu_assign_pointer(net->xfrm.nlsk, NULL);
  2239. synchronize_net();
  2240. list_for_each_entry(net, net_exit_list, exit_list)
  2241. netlink_kernel_release(net->xfrm.nlsk_stash);
  2242. }
  2243. static struct pernet_operations xfrm_user_net_ops = {
  2244. .init = xfrm_user_net_init,
  2245. .exit_batch = xfrm_user_net_exit,
  2246. };
  2247. static int __init xfrm_user_init(void)
  2248. {
  2249. int rv;
  2250. printk(KERN_INFO "Initializing XFRM netlink socket\n");
  2251. rv = register_pernet_subsys(&xfrm_user_net_ops);
  2252. if (rv < 0)
  2253. return rv;
  2254. rv = xfrm_register_km(&netlink_mgr);
  2255. if (rv < 0)
  2256. unregister_pernet_subsys(&xfrm_user_net_ops);
  2257. return rv;
  2258. }
  2259. static void __exit xfrm_user_exit(void)
  2260. {
  2261. xfrm_unregister_km(&netlink_mgr);
  2262. unregister_pernet_subsys(&xfrm_user_net_ops);
  2263. }
  2264. module_init(xfrm_user_init);
  2265. module_exit(xfrm_user_exit);
  2266. MODULE_LICENSE("GPL");
  2267. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);