xfrm_user.c 69 KB

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