xfrm_user.c 73 KB

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