xfrm_user.c 70 KB

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