xfrm_user.c 66 KB

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