xfrm_user.c 71 KB

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