xfrm_user.c 69 KB

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