xfrm_user.c 70 KB

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