xfrm_user.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132
  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. return nlmsg_end(skb, nlh);
  845. }
  846. static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  847. struct nlattr **attrs)
  848. {
  849. struct net *net = sock_net(skb->sk);
  850. struct xfrmu_spdhthresh *thresh4 = NULL;
  851. struct xfrmu_spdhthresh *thresh6 = NULL;
  852. /* selector prefixlen thresholds to hash policies */
  853. if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
  854. struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
  855. if (nla_len(rta) < sizeof(*thresh4))
  856. return -EINVAL;
  857. thresh4 = nla_data(rta);
  858. if (thresh4->lbits > 32 || thresh4->rbits > 32)
  859. return -EINVAL;
  860. }
  861. if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
  862. struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
  863. if (nla_len(rta) < sizeof(*thresh6))
  864. return -EINVAL;
  865. thresh6 = nla_data(rta);
  866. if (thresh6->lbits > 128 || thresh6->rbits > 128)
  867. return -EINVAL;
  868. }
  869. if (thresh4 || thresh6) {
  870. write_seqlock(&net->xfrm.policy_hthresh.lock);
  871. if (thresh4) {
  872. net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
  873. net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
  874. }
  875. if (thresh6) {
  876. net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
  877. net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
  878. }
  879. write_sequnlock(&net->xfrm.policy_hthresh.lock);
  880. xfrm_policy_hash_rebuild(net);
  881. }
  882. return 0;
  883. }
  884. static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  885. struct nlattr **attrs)
  886. {
  887. struct net *net = sock_net(skb->sk);
  888. struct sk_buff *r_skb;
  889. u32 *flags = nlmsg_data(nlh);
  890. u32 sportid = NETLINK_CB(skb).portid;
  891. u32 seq = nlh->nlmsg_seq;
  892. r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
  893. if (r_skb == NULL)
  894. return -ENOMEM;
  895. if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
  896. BUG();
  897. return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
  898. }
  899. static inline size_t xfrm_sadinfo_msgsize(void)
  900. {
  901. return NLMSG_ALIGN(4)
  902. + nla_total_size(sizeof(struct xfrmu_sadhinfo))
  903. + nla_total_size(4); /* XFRMA_SAD_CNT */
  904. }
  905. static int build_sadinfo(struct sk_buff *skb, struct net *net,
  906. u32 portid, u32 seq, u32 flags)
  907. {
  908. struct xfrmk_sadinfo si;
  909. struct xfrmu_sadhinfo sh;
  910. struct nlmsghdr *nlh;
  911. int err;
  912. u32 *f;
  913. nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
  914. if (nlh == NULL) /* shouldn't really happen ... */
  915. return -EMSGSIZE;
  916. f = nlmsg_data(nlh);
  917. *f = flags;
  918. xfrm_sad_getinfo(net, &si);
  919. sh.sadhmcnt = si.sadhmcnt;
  920. sh.sadhcnt = si.sadhcnt;
  921. err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
  922. if (!err)
  923. err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
  924. if (err) {
  925. nlmsg_cancel(skb, nlh);
  926. return err;
  927. }
  928. return nlmsg_end(skb, nlh);
  929. }
  930. static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  931. struct nlattr **attrs)
  932. {
  933. struct net *net = sock_net(skb->sk);
  934. struct sk_buff *r_skb;
  935. u32 *flags = nlmsg_data(nlh);
  936. u32 sportid = NETLINK_CB(skb).portid;
  937. u32 seq = nlh->nlmsg_seq;
  938. r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
  939. if (r_skb == NULL)
  940. return -ENOMEM;
  941. if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
  942. BUG();
  943. return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
  944. }
  945. static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  946. struct nlattr **attrs)
  947. {
  948. struct net *net = sock_net(skb->sk);
  949. struct xfrm_usersa_id *p = nlmsg_data(nlh);
  950. struct xfrm_state *x;
  951. struct sk_buff *resp_skb;
  952. int err = -ESRCH;
  953. x = xfrm_user_state_lookup(net, p, attrs, &err);
  954. if (x == NULL)
  955. goto out_noput;
  956. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  957. if (IS_ERR(resp_skb)) {
  958. err = PTR_ERR(resp_skb);
  959. } else {
  960. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
  961. }
  962. xfrm_state_put(x);
  963. out_noput:
  964. return err;
  965. }
  966. static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
  967. struct nlattr **attrs)
  968. {
  969. struct net *net = sock_net(skb->sk);
  970. struct xfrm_state *x;
  971. struct xfrm_userspi_info *p;
  972. struct sk_buff *resp_skb;
  973. xfrm_address_t *daddr;
  974. int family;
  975. int err;
  976. u32 mark;
  977. struct xfrm_mark m;
  978. p = nlmsg_data(nlh);
  979. err = verify_spi_info(p->info.id.proto, p->min, p->max);
  980. if (err)
  981. goto out_noput;
  982. family = p->info.family;
  983. daddr = &p->info.id.daddr;
  984. x = NULL;
  985. mark = xfrm_mark_get(attrs, &m);
  986. if (p->info.seq) {
  987. x = xfrm_find_acq_byseq(net, mark, p->info.seq);
  988. if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
  989. xfrm_state_put(x);
  990. x = NULL;
  991. }
  992. }
  993. if (!x)
  994. x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
  995. p->info.id.proto, daddr,
  996. &p->info.saddr, 1,
  997. family);
  998. err = -ENOENT;
  999. if (x == NULL)
  1000. goto out_noput;
  1001. err = xfrm_alloc_spi(x, p->min, p->max);
  1002. if (err)
  1003. goto out;
  1004. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  1005. if (IS_ERR(resp_skb)) {
  1006. err = PTR_ERR(resp_skb);
  1007. goto out;
  1008. }
  1009. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
  1010. out:
  1011. xfrm_state_put(x);
  1012. out_noput:
  1013. return err;
  1014. }
  1015. static int verify_policy_dir(u8 dir)
  1016. {
  1017. switch (dir) {
  1018. case XFRM_POLICY_IN:
  1019. case XFRM_POLICY_OUT:
  1020. case XFRM_POLICY_FWD:
  1021. break;
  1022. default:
  1023. return -EINVAL;
  1024. }
  1025. return 0;
  1026. }
  1027. static int verify_policy_type(u8 type)
  1028. {
  1029. switch (type) {
  1030. case XFRM_POLICY_TYPE_MAIN:
  1031. #ifdef CONFIG_XFRM_SUB_POLICY
  1032. case XFRM_POLICY_TYPE_SUB:
  1033. #endif
  1034. break;
  1035. default:
  1036. return -EINVAL;
  1037. }
  1038. return 0;
  1039. }
  1040. static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
  1041. {
  1042. int ret;
  1043. switch (p->share) {
  1044. case XFRM_SHARE_ANY:
  1045. case XFRM_SHARE_SESSION:
  1046. case XFRM_SHARE_USER:
  1047. case XFRM_SHARE_UNIQUE:
  1048. break;
  1049. default:
  1050. return -EINVAL;
  1051. }
  1052. switch (p->action) {
  1053. case XFRM_POLICY_ALLOW:
  1054. case XFRM_POLICY_BLOCK:
  1055. break;
  1056. default:
  1057. return -EINVAL;
  1058. }
  1059. switch (p->sel.family) {
  1060. case AF_INET:
  1061. break;
  1062. case AF_INET6:
  1063. #if IS_ENABLED(CONFIG_IPV6)
  1064. break;
  1065. #else
  1066. return -EAFNOSUPPORT;
  1067. #endif
  1068. default:
  1069. return -EINVAL;
  1070. }
  1071. ret = verify_policy_dir(p->dir);
  1072. if (ret)
  1073. return ret;
  1074. if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
  1075. return -EINVAL;
  1076. return 0;
  1077. }
  1078. static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
  1079. {
  1080. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1081. struct xfrm_user_sec_ctx *uctx;
  1082. if (!rt)
  1083. return 0;
  1084. uctx = nla_data(rt);
  1085. return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
  1086. }
  1087. static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
  1088. int nr)
  1089. {
  1090. int i;
  1091. xp->xfrm_nr = nr;
  1092. for (i = 0; i < nr; i++, ut++) {
  1093. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  1094. memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
  1095. memcpy(&t->saddr, &ut->saddr,
  1096. sizeof(xfrm_address_t));
  1097. t->reqid = ut->reqid;
  1098. t->mode = ut->mode;
  1099. t->share = ut->share;
  1100. t->optional = ut->optional;
  1101. t->aalgos = ut->aalgos;
  1102. t->ealgos = ut->ealgos;
  1103. t->calgos = ut->calgos;
  1104. /* If all masks are ~0, then we allow all algorithms. */
  1105. t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
  1106. t->encap_family = ut->family;
  1107. }
  1108. }
  1109. static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
  1110. {
  1111. int i;
  1112. if (nr > XFRM_MAX_DEPTH)
  1113. return -EINVAL;
  1114. for (i = 0; i < nr; i++) {
  1115. /* We never validated the ut->family value, so many
  1116. * applications simply leave it at zero. The check was
  1117. * never made and ut->family was ignored because all
  1118. * templates could be assumed to have the same family as
  1119. * the policy itself. Now that we will have ipv4-in-ipv6
  1120. * and ipv6-in-ipv4 tunnels, this is no longer true.
  1121. */
  1122. if (!ut[i].family)
  1123. ut[i].family = family;
  1124. switch (ut[i].family) {
  1125. case AF_INET:
  1126. break;
  1127. #if IS_ENABLED(CONFIG_IPV6)
  1128. case AF_INET6:
  1129. break;
  1130. #endif
  1131. default:
  1132. return -EINVAL;
  1133. }
  1134. }
  1135. return 0;
  1136. }
  1137. static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
  1138. {
  1139. struct nlattr *rt = attrs[XFRMA_TMPL];
  1140. if (!rt) {
  1141. pol->xfrm_nr = 0;
  1142. } else {
  1143. struct xfrm_user_tmpl *utmpl = nla_data(rt);
  1144. int nr = nla_len(rt) / sizeof(*utmpl);
  1145. int err;
  1146. err = validate_tmpl(nr, utmpl, pol->family);
  1147. if (err)
  1148. return err;
  1149. copy_templates(pol, utmpl, nr);
  1150. }
  1151. return 0;
  1152. }
  1153. static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
  1154. {
  1155. struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
  1156. struct xfrm_userpolicy_type *upt;
  1157. u8 type = XFRM_POLICY_TYPE_MAIN;
  1158. int err;
  1159. if (rt) {
  1160. upt = nla_data(rt);
  1161. type = upt->type;
  1162. }
  1163. err = verify_policy_type(type);
  1164. if (err)
  1165. return err;
  1166. *tp = type;
  1167. return 0;
  1168. }
  1169. static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
  1170. {
  1171. xp->priority = p->priority;
  1172. xp->index = p->index;
  1173. memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
  1174. memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
  1175. xp->action = p->action;
  1176. xp->flags = p->flags;
  1177. xp->family = p->sel.family;
  1178. /* XXX xp->share = p->share; */
  1179. }
  1180. static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
  1181. {
  1182. memset(p, 0, sizeof(*p));
  1183. memcpy(&p->sel, &xp->selector, sizeof(p->sel));
  1184. memcpy(&p->lft, &xp->lft, sizeof(p->lft));
  1185. memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
  1186. p->priority = xp->priority;
  1187. p->index = xp->index;
  1188. p->sel.family = xp->family;
  1189. p->dir = dir;
  1190. p->action = xp->action;
  1191. p->flags = xp->flags;
  1192. p->share = XFRM_SHARE_ANY; /* XXX xp->share */
  1193. }
  1194. static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
  1195. {
  1196. struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
  1197. int err;
  1198. if (!xp) {
  1199. *errp = -ENOMEM;
  1200. return NULL;
  1201. }
  1202. copy_from_user_policy(xp, p);
  1203. err = copy_from_user_policy_type(&xp->type, attrs);
  1204. if (err)
  1205. goto error;
  1206. if (!(err = copy_from_user_tmpl(xp, attrs)))
  1207. err = copy_from_user_sec_ctx(xp, attrs);
  1208. if (err)
  1209. goto error;
  1210. xfrm_mark_get(attrs, &xp->mark);
  1211. return xp;
  1212. error:
  1213. *errp = err;
  1214. xp->walk.dead = 1;
  1215. xfrm_policy_destroy(xp);
  1216. return NULL;
  1217. }
  1218. static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1219. struct nlattr **attrs)
  1220. {
  1221. struct net *net = sock_net(skb->sk);
  1222. struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
  1223. struct xfrm_policy *xp;
  1224. struct km_event c;
  1225. int err;
  1226. int excl;
  1227. err = verify_newpolicy_info(p);
  1228. if (err)
  1229. return err;
  1230. err = verify_sec_ctx_len(attrs);
  1231. if (err)
  1232. return err;
  1233. xp = xfrm_policy_construct(net, p, attrs, &err);
  1234. if (!xp)
  1235. return err;
  1236. /* shouldn't excl be based on nlh flags??
  1237. * Aha! this is anti-netlink really i.e more pfkey derived
  1238. * in netlink excl is a flag and you wouldnt need
  1239. * a type XFRM_MSG_UPDPOLICY - JHS */
  1240. excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
  1241. err = xfrm_policy_insert(p->dir, xp, excl);
  1242. xfrm_audit_policy_add(xp, err ? 0 : 1, true);
  1243. if (err) {
  1244. security_xfrm_policy_free(xp->security);
  1245. kfree(xp);
  1246. return err;
  1247. }
  1248. c.event = nlh->nlmsg_type;
  1249. c.seq = nlh->nlmsg_seq;
  1250. c.portid = nlh->nlmsg_pid;
  1251. km_policy_notify(xp, p->dir, &c);
  1252. xfrm_pol_put(xp);
  1253. return 0;
  1254. }
  1255. static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
  1256. {
  1257. struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
  1258. int i;
  1259. if (xp->xfrm_nr == 0)
  1260. return 0;
  1261. for (i = 0; i < xp->xfrm_nr; i++) {
  1262. struct xfrm_user_tmpl *up = &vec[i];
  1263. struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
  1264. memset(up, 0, sizeof(*up));
  1265. memcpy(&up->id, &kp->id, sizeof(up->id));
  1266. up->family = kp->encap_family;
  1267. memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
  1268. up->reqid = kp->reqid;
  1269. up->mode = kp->mode;
  1270. up->share = kp->share;
  1271. up->optional = kp->optional;
  1272. up->aalgos = kp->aalgos;
  1273. up->ealgos = kp->ealgos;
  1274. up->calgos = kp->calgos;
  1275. }
  1276. return nla_put(skb, XFRMA_TMPL,
  1277. sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
  1278. }
  1279. static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
  1280. {
  1281. if (x->security) {
  1282. return copy_sec_ctx(x->security, skb);
  1283. }
  1284. return 0;
  1285. }
  1286. static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
  1287. {
  1288. if (xp->security)
  1289. return copy_sec_ctx(xp->security, skb);
  1290. return 0;
  1291. }
  1292. static inline size_t userpolicy_type_attrsize(void)
  1293. {
  1294. #ifdef CONFIG_XFRM_SUB_POLICY
  1295. return nla_total_size(sizeof(struct xfrm_userpolicy_type));
  1296. #else
  1297. return 0;
  1298. #endif
  1299. }
  1300. #ifdef CONFIG_XFRM_SUB_POLICY
  1301. static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1302. {
  1303. struct xfrm_userpolicy_type upt = {
  1304. .type = type,
  1305. };
  1306. return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
  1307. }
  1308. #else
  1309. static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1310. {
  1311. return 0;
  1312. }
  1313. #endif
  1314. static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
  1315. {
  1316. struct xfrm_dump_info *sp = ptr;
  1317. struct xfrm_userpolicy_info *p;
  1318. struct sk_buff *in_skb = sp->in_skb;
  1319. struct sk_buff *skb = sp->out_skb;
  1320. struct nlmsghdr *nlh;
  1321. int err;
  1322. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
  1323. XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
  1324. if (nlh == NULL)
  1325. return -EMSGSIZE;
  1326. p = nlmsg_data(nlh);
  1327. copy_to_user_policy(xp, p, dir);
  1328. err = copy_to_user_tmpl(xp, skb);
  1329. if (!err)
  1330. err = copy_to_user_sec_ctx(xp, skb);
  1331. if (!err)
  1332. err = copy_to_user_policy_type(xp->type, skb);
  1333. if (!err)
  1334. err = xfrm_mark_put(skb, &xp->mark);
  1335. if (err) {
  1336. nlmsg_cancel(skb, nlh);
  1337. return err;
  1338. }
  1339. nlmsg_end(skb, nlh);
  1340. return 0;
  1341. }
  1342. static int xfrm_dump_policy_done(struct netlink_callback *cb)
  1343. {
  1344. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
  1345. struct net *net = sock_net(cb->skb->sk);
  1346. xfrm_policy_walk_done(walk, net);
  1347. return 0;
  1348. }
  1349. static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
  1350. {
  1351. struct net *net = sock_net(skb->sk);
  1352. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
  1353. struct xfrm_dump_info info;
  1354. BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
  1355. sizeof(cb->args) - sizeof(cb->args[0]));
  1356. info.in_skb = cb->skb;
  1357. info.out_skb = skb;
  1358. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  1359. info.nlmsg_flags = NLM_F_MULTI;
  1360. if (!cb->args[0]) {
  1361. cb->args[0] = 1;
  1362. xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
  1363. }
  1364. (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
  1365. return skb->len;
  1366. }
  1367. static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
  1368. struct xfrm_policy *xp,
  1369. int dir, u32 seq)
  1370. {
  1371. struct xfrm_dump_info info;
  1372. struct sk_buff *skb;
  1373. int err;
  1374. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1375. if (!skb)
  1376. return ERR_PTR(-ENOMEM);
  1377. info.in_skb = in_skb;
  1378. info.out_skb = skb;
  1379. info.nlmsg_seq = seq;
  1380. info.nlmsg_flags = 0;
  1381. err = dump_one_policy(xp, dir, 0, &info);
  1382. if (err) {
  1383. kfree_skb(skb);
  1384. return ERR_PTR(err);
  1385. }
  1386. return skb;
  1387. }
  1388. static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1389. struct nlattr **attrs)
  1390. {
  1391. struct net *net = sock_net(skb->sk);
  1392. struct xfrm_policy *xp;
  1393. struct xfrm_userpolicy_id *p;
  1394. u8 type = XFRM_POLICY_TYPE_MAIN;
  1395. int err;
  1396. struct km_event c;
  1397. int delete;
  1398. struct xfrm_mark m;
  1399. u32 mark = xfrm_mark_get(attrs, &m);
  1400. p = nlmsg_data(nlh);
  1401. delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
  1402. err = copy_from_user_policy_type(&type, attrs);
  1403. if (err)
  1404. return err;
  1405. err = verify_policy_dir(p->dir);
  1406. if (err)
  1407. return err;
  1408. if (p->index)
  1409. xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
  1410. else {
  1411. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1412. struct xfrm_sec_ctx *ctx;
  1413. err = verify_sec_ctx_len(attrs);
  1414. if (err)
  1415. return err;
  1416. ctx = NULL;
  1417. if (rt) {
  1418. struct xfrm_user_sec_ctx *uctx = nla_data(rt);
  1419. err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
  1420. if (err)
  1421. return err;
  1422. }
  1423. xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
  1424. ctx, delete, &err);
  1425. security_xfrm_policy_free(ctx);
  1426. }
  1427. if (xp == NULL)
  1428. return -ENOENT;
  1429. if (!delete) {
  1430. struct sk_buff *resp_skb;
  1431. resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
  1432. if (IS_ERR(resp_skb)) {
  1433. err = PTR_ERR(resp_skb);
  1434. } else {
  1435. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
  1436. NETLINK_CB(skb).portid);
  1437. }
  1438. } else {
  1439. xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
  1440. if (err != 0)
  1441. goto out;
  1442. c.data.byid = p->index;
  1443. c.event = nlh->nlmsg_type;
  1444. c.seq = nlh->nlmsg_seq;
  1445. c.portid = nlh->nlmsg_pid;
  1446. km_policy_notify(xp, p->dir, &c);
  1447. }
  1448. out:
  1449. xfrm_pol_put(xp);
  1450. if (delete && err == 0)
  1451. xfrm_garbage_collect(net);
  1452. return err;
  1453. }
  1454. static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  1455. struct nlattr **attrs)
  1456. {
  1457. struct net *net = sock_net(skb->sk);
  1458. struct km_event c;
  1459. struct xfrm_usersa_flush *p = nlmsg_data(nlh);
  1460. int err;
  1461. err = xfrm_state_flush(net, p->proto, true);
  1462. if (err) {
  1463. if (err == -ESRCH) /* empty table */
  1464. return 0;
  1465. return err;
  1466. }
  1467. c.data.proto = p->proto;
  1468. c.event = nlh->nlmsg_type;
  1469. c.seq = nlh->nlmsg_seq;
  1470. c.portid = nlh->nlmsg_pid;
  1471. c.net = net;
  1472. km_state_notify(NULL, &c);
  1473. return 0;
  1474. }
  1475. static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
  1476. {
  1477. size_t replay_size = x->replay_esn ?
  1478. xfrm_replay_state_esn_len(x->replay_esn) :
  1479. sizeof(struct xfrm_replay_state);
  1480. return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
  1481. + nla_total_size(replay_size)
  1482. + nla_total_size(sizeof(struct xfrm_lifetime_cur))
  1483. + nla_total_size(sizeof(struct xfrm_mark))
  1484. + nla_total_size(4) /* XFRM_AE_RTHR */
  1485. + nla_total_size(4); /* XFRM_AE_ETHR */
  1486. }
  1487. static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
  1488. {
  1489. struct xfrm_aevent_id *id;
  1490. struct nlmsghdr *nlh;
  1491. int err;
  1492. nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
  1493. if (nlh == NULL)
  1494. return -EMSGSIZE;
  1495. id = nlmsg_data(nlh);
  1496. memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
  1497. id->sa_id.spi = x->id.spi;
  1498. id->sa_id.family = x->props.family;
  1499. id->sa_id.proto = x->id.proto;
  1500. memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
  1501. id->reqid = x->props.reqid;
  1502. id->flags = c->data.aevent;
  1503. if (x->replay_esn) {
  1504. err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
  1505. xfrm_replay_state_esn_len(x->replay_esn),
  1506. x->replay_esn);
  1507. } else {
  1508. err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
  1509. &x->replay);
  1510. }
  1511. if (err)
  1512. goto out_cancel;
  1513. err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
  1514. if (err)
  1515. goto out_cancel;
  1516. if (id->flags & XFRM_AE_RTHR) {
  1517. err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
  1518. if (err)
  1519. goto out_cancel;
  1520. }
  1521. if (id->flags & XFRM_AE_ETHR) {
  1522. err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
  1523. x->replay_maxage * 10 / HZ);
  1524. if (err)
  1525. goto out_cancel;
  1526. }
  1527. err = xfrm_mark_put(skb, &x->mark);
  1528. if (err)
  1529. goto out_cancel;
  1530. return nlmsg_end(skb, nlh);
  1531. out_cancel:
  1532. nlmsg_cancel(skb, nlh);
  1533. return err;
  1534. }
  1535. static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1536. struct nlattr **attrs)
  1537. {
  1538. struct net *net = sock_net(skb->sk);
  1539. struct xfrm_state *x;
  1540. struct sk_buff *r_skb;
  1541. int err;
  1542. struct km_event c;
  1543. u32 mark;
  1544. struct xfrm_mark m;
  1545. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  1546. struct xfrm_usersa_id *id = &p->sa_id;
  1547. mark = xfrm_mark_get(attrs, &m);
  1548. x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
  1549. if (x == NULL)
  1550. return -ESRCH;
  1551. r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
  1552. if (r_skb == NULL) {
  1553. xfrm_state_put(x);
  1554. return -ENOMEM;
  1555. }
  1556. /*
  1557. * XXX: is this lock really needed - none of the other
  1558. * gets lock (the concern is things getting updated
  1559. * while we are still reading) - jhs
  1560. */
  1561. spin_lock_bh(&x->lock);
  1562. c.data.aevent = p->flags;
  1563. c.seq = nlh->nlmsg_seq;
  1564. c.portid = nlh->nlmsg_pid;
  1565. if (build_aevent(r_skb, x, &c) < 0)
  1566. BUG();
  1567. err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
  1568. spin_unlock_bh(&x->lock);
  1569. xfrm_state_put(x);
  1570. return err;
  1571. }
  1572. static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1573. struct nlattr **attrs)
  1574. {
  1575. struct net *net = sock_net(skb->sk);
  1576. struct xfrm_state *x;
  1577. struct km_event c;
  1578. int err = -EINVAL;
  1579. u32 mark = 0;
  1580. struct xfrm_mark m;
  1581. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  1582. struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
  1583. struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
  1584. struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
  1585. if (!lt && !rp && !re)
  1586. return err;
  1587. /* pedantic mode - thou shalt sayeth replaceth */
  1588. if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
  1589. return err;
  1590. mark = xfrm_mark_get(attrs, &m);
  1591. x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
  1592. if (x == NULL)
  1593. return -ESRCH;
  1594. if (x->km.state != XFRM_STATE_VALID)
  1595. goto out;
  1596. err = xfrm_replay_verify_len(x->replay_esn, re);
  1597. if (err)
  1598. goto out;
  1599. spin_lock_bh(&x->lock);
  1600. xfrm_update_ae_params(x, attrs, 1);
  1601. spin_unlock_bh(&x->lock);
  1602. c.event = nlh->nlmsg_type;
  1603. c.seq = nlh->nlmsg_seq;
  1604. c.portid = nlh->nlmsg_pid;
  1605. c.data.aevent = XFRM_AE_CU;
  1606. km_state_notify(x, &c);
  1607. err = 0;
  1608. out:
  1609. xfrm_state_put(x);
  1610. return err;
  1611. }
  1612. static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1613. struct nlattr **attrs)
  1614. {
  1615. struct net *net = sock_net(skb->sk);
  1616. struct km_event c;
  1617. u8 type = XFRM_POLICY_TYPE_MAIN;
  1618. int err;
  1619. err = copy_from_user_policy_type(&type, attrs);
  1620. if (err)
  1621. return err;
  1622. err = xfrm_policy_flush(net, type, true);
  1623. if (err) {
  1624. if (err == -ESRCH) /* empty table */
  1625. return 0;
  1626. return err;
  1627. }
  1628. c.data.type = type;
  1629. c.event = nlh->nlmsg_type;
  1630. c.seq = nlh->nlmsg_seq;
  1631. c.portid = nlh->nlmsg_pid;
  1632. c.net = net;
  1633. km_policy_notify(NULL, 0, &c);
  1634. return 0;
  1635. }
  1636. static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1637. struct nlattr **attrs)
  1638. {
  1639. struct net *net = sock_net(skb->sk);
  1640. struct xfrm_policy *xp;
  1641. struct xfrm_user_polexpire *up = nlmsg_data(nlh);
  1642. struct xfrm_userpolicy_info *p = &up->pol;
  1643. u8 type = XFRM_POLICY_TYPE_MAIN;
  1644. int err = -ENOENT;
  1645. struct xfrm_mark m;
  1646. u32 mark = xfrm_mark_get(attrs, &m);
  1647. err = copy_from_user_policy_type(&type, attrs);
  1648. if (err)
  1649. return err;
  1650. err = verify_policy_dir(p->dir);
  1651. if (err)
  1652. return err;
  1653. if (p->index)
  1654. xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
  1655. else {
  1656. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1657. struct xfrm_sec_ctx *ctx;
  1658. err = verify_sec_ctx_len(attrs);
  1659. if (err)
  1660. return err;
  1661. ctx = NULL;
  1662. if (rt) {
  1663. struct xfrm_user_sec_ctx *uctx = nla_data(rt);
  1664. err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
  1665. if (err)
  1666. return err;
  1667. }
  1668. xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
  1669. &p->sel, ctx, 0, &err);
  1670. security_xfrm_policy_free(ctx);
  1671. }
  1672. if (xp == NULL)
  1673. return -ENOENT;
  1674. if (unlikely(xp->walk.dead))
  1675. goto out;
  1676. err = 0;
  1677. if (up->hard) {
  1678. xfrm_policy_delete(xp, p->dir);
  1679. xfrm_audit_policy_delete(xp, 1, true);
  1680. } else {
  1681. // reset the timers here?
  1682. WARN(1, "Dont know what to do with soft policy expire\n");
  1683. }
  1684. km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
  1685. out:
  1686. xfrm_pol_put(xp);
  1687. return err;
  1688. }
  1689. static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1690. struct nlattr **attrs)
  1691. {
  1692. struct net *net = sock_net(skb->sk);
  1693. struct xfrm_state *x;
  1694. int err;
  1695. struct xfrm_user_expire *ue = nlmsg_data(nlh);
  1696. struct xfrm_usersa_info *p = &ue->state;
  1697. struct xfrm_mark m;
  1698. u32 mark = xfrm_mark_get(attrs, &m);
  1699. x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
  1700. err = -ENOENT;
  1701. if (x == NULL)
  1702. return err;
  1703. spin_lock_bh(&x->lock);
  1704. err = -EINVAL;
  1705. if (x->km.state != XFRM_STATE_VALID)
  1706. goto out;
  1707. km_state_expired(x, ue->hard, nlh->nlmsg_pid);
  1708. if (ue->hard) {
  1709. __xfrm_state_delete(x);
  1710. xfrm_audit_state_delete(x, 1, true);
  1711. }
  1712. err = 0;
  1713. out:
  1714. spin_unlock_bh(&x->lock);
  1715. xfrm_state_put(x);
  1716. return err;
  1717. }
  1718. static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1719. struct nlattr **attrs)
  1720. {
  1721. struct net *net = sock_net(skb->sk);
  1722. struct xfrm_policy *xp;
  1723. struct xfrm_user_tmpl *ut;
  1724. int i;
  1725. struct nlattr *rt = attrs[XFRMA_TMPL];
  1726. struct xfrm_mark mark;
  1727. struct xfrm_user_acquire *ua = nlmsg_data(nlh);
  1728. struct xfrm_state *x = xfrm_state_alloc(net);
  1729. int err = -ENOMEM;
  1730. if (!x)
  1731. goto nomem;
  1732. xfrm_mark_get(attrs, &mark);
  1733. err = verify_newpolicy_info(&ua->policy);
  1734. if (err)
  1735. goto bad_policy;
  1736. /* build an XP */
  1737. xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
  1738. if (!xp)
  1739. goto free_state;
  1740. memcpy(&x->id, &ua->id, sizeof(ua->id));
  1741. memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
  1742. memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
  1743. xp->mark.m = x->mark.m = mark.m;
  1744. xp->mark.v = x->mark.v = mark.v;
  1745. ut = nla_data(rt);
  1746. /* extract the templates and for each call km_key */
  1747. for (i = 0; i < xp->xfrm_nr; i++, ut++) {
  1748. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  1749. memcpy(&x->id, &t->id, sizeof(x->id));
  1750. x->props.mode = t->mode;
  1751. x->props.reqid = t->reqid;
  1752. x->props.family = ut->family;
  1753. t->aalgos = ua->aalgos;
  1754. t->ealgos = ua->ealgos;
  1755. t->calgos = ua->calgos;
  1756. err = km_query(x, t, xp);
  1757. }
  1758. kfree(x);
  1759. kfree(xp);
  1760. return 0;
  1761. bad_policy:
  1762. WARN(1, "BAD policy passed\n");
  1763. free_state:
  1764. kfree(x);
  1765. nomem:
  1766. return err;
  1767. }
  1768. #ifdef CONFIG_XFRM_MIGRATE
  1769. static int copy_from_user_migrate(struct xfrm_migrate *ma,
  1770. struct xfrm_kmaddress *k,
  1771. struct nlattr **attrs, int *num)
  1772. {
  1773. struct nlattr *rt = attrs[XFRMA_MIGRATE];
  1774. struct xfrm_user_migrate *um;
  1775. int i, num_migrate;
  1776. if (k != NULL) {
  1777. struct xfrm_user_kmaddress *uk;
  1778. uk = nla_data(attrs[XFRMA_KMADDRESS]);
  1779. memcpy(&k->local, &uk->local, sizeof(k->local));
  1780. memcpy(&k->remote, &uk->remote, sizeof(k->remote));
  1781. k->family = uk->family;
  1782. k->reserved = uk->reserved;
  1783. }
  1784. um = nla_data(rt);
  1785. num_migrate = nla_len(rt) / sizeof(*um);
  1786. if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
  1787. return -EINVAL;
  1788. for (i = 0; i < num_migrate; i++, um++, ma++) {
  1789. memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
  1790. memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
  1791. memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
  1792. memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
  1793. ma->proto = um->proto;
  1794. ma->mode = um->mode;
  1795. ma->reqid = um->reqid;
  1796. ma->old_family = um->old_family;
  1797. ma->new_family = um->new_family;
  1798. }
  1799. *num = i;
  1800. return 0;
  1801. }
  1802. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1803. struct nlattr **attrs)
  1804. {
  1805. struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
  1806. struct xfrm_migrate m[XFRM_MAX_DEPTH];
  1807. struct xfrm_kmaddress km, *kmp;
  1808. u8 type;
  1809. int err;
  1810. int n = 0;
  1811. struct net *net = sock_net(skb->sk);
  1812. if (attrs[XFRMA_MIGRATE] == NULL)
  1813. return -EINVAL;
  1814. kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
  1815. err = copy_from_user_policy_type(&type, attrs);
  1816. if (err)
  1817. return err;
  1818. err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
  1819. if (err)
  1820. return err;
  1821. if (!n)
  1822. return 0;
  1823. xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
  1824. return 0;
  1825. }
  1826. #else
  1827. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1828. struct nlattr **attrs)
  1829. {
  1830. return -ENOPROTOOPT;
  1831. }
  1832. #endif
  1833. #ifdef CONFIG_XFRM_MIGRATE
  1834. static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
  1835. {
  1836. struct xfrm_user_migrate um;
  1837. memset(&um, 0, sizeof(um));
  1838. um.proto = m->proto;
  1839. um.mode = m->mode;
  1840. um.reqid = m->reqid;
  1841. um.old_family = m->old_family;
  1842. memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
  1843. memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
  1844. um.new_family = m->new_family;
  1845. memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
  1846. memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
  1847. return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
  1848. }
  1849. static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
  1850. {
  1851. struct xfrm_user_kmaddress uk;
  1852. memset(&uk, 0, sizeof(uk));
  1853. uk.family = k->family;
  1854. uk.reserved = k->reserved;
  1855. memcpy(&uk.local, &k->local, sizeof(uk.local));
  1856. memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
  1857. return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
  1858. }
  1859. static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
  1860. {
  1861. return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
  1862. + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
  1863. + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
  1864. + userpolicy_type_attrsize();
  1865. }
  1866. static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
  1867. int num_migrate, const struct xfrm_kmaddress *k,
  1868. const struct xfrm_selector *sel, u8 dir, u8 type)
  1869. {
  1870. const struct xfrm_migrate *mp;
  1871. struct xfrm_userpolicy_id *pol_id;
  1872. struct nlmsghdr *nlh;
  1873. int i, err;
  1874. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
  1875. if (nlh == NULL)
  1876. return -EMSGSIZE;
  1877. pol_id = nlmsg_data(nlh);
  1878. /* copy data from selector, dir, and type to the pol_id */
  1879. memset(pol_id, 0, sizeof(*pol_id));
  1880. memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
  1881. pol_id->dir = dir;
  1882. if (k != NULL) {
  1883. err = copy_to_user_kmaddress(k, skb);
  1884. if (err)
  1885. goto out_cancel;
  1886. }
  1887. err = copy_to_user_policy_type(type, skb);
  1888. if (err)
  1889. goto out_cancel;
  1890. for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
  1891. err = copy_to_user_migrate(mp, skb);
  1892. if (err)
  1893. goto out_cancel;
  1894. }
  1895. return nlmsg_end(skb, nlh);
  1896. out_cancel:
  1897. nlmsg_cancel(skb, nlh);
  1898. return err;
  1899. }
  1900. static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  1901. const struct xfrm_migrate *m, int num_migrate,
  1902. const struct xfrm_kmaddress *k)
  1903. {
  1904. struct net *net = &init_net;
  1905. struct sk_buff *skb;
  1906. skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
  1907. if (skb == NULL)
  1908. return -ENOMEM;
  1909. /* build migrate */
  1910. if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
  1911. BUG();
  1912. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
  1913. }
  1914. #else
  1915. static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  1916. const struct xfrm_migrate *m, int num_migrate,
  1917. const struct xfrm_kmaddress *k)
  1918. {
  1919. return -ENOPROTOOPT;
  1920. }
  1921. #endif
  1922. #define XMSGSIZE(type) sizeof(struct type)
  1923. static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
  1924. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1925. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1926. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1927. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1928. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1929. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1930. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
  1931. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
  1932. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
  1933. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1934. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1935. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
  1936. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  1937. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
  1938. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1939. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1940. [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
  1941. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1942. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
  1943. [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
  1944. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
  1945. };
  1946. #undef XMSGSIZE
  1947. static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
  1948. [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
  1949. [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
  1950. [XFRMA_LASTUSED] = { .type = NLA_U64},
  1951. [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
  1952. [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
  1953. [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
  1954. [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
  1955. [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
  1956. [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
  1957. [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
  1958. [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
  1959. [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
  1960. [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
  1961. [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
  1962. [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
  1963. [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
  1964. [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
  1965. [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
  1966. [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
  1967. [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
  1968. [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
  1969. [XFRMA_TFCPAD] = { .type = NLA_U32 },
  1970. [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
  1971. [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
  1972. [XFRMA_PROTO] = { .type = NLA_U8 },
  1973. [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
  1974. };
  1975. static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
  1976. [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
  1977. [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
  1978. };
  1979. static const struct xfrm_link {
  1980. int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
  1981. int (*dump)(struct sk_buff *, struct netlink_callback *);
  1982. int (*done)(struct netlink_callback *);
  1983. const struct nla_policy *nla_pol;
  1984. int nla_max;
  1985. } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
  1986. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1987. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
  1988. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
  1989. .dump = xfrm_dump_sa,
  1990. .done = xfrm_dump_sa_done },
  1991. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1992. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
  1993. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
  1994. .dump = xfrm_dump_policy,
  1995. .done = xfrm_dump_policy_done },
  1996. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
  1997. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
  1998. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
  1999. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  2000. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  2001. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
  2002. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
  2003. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
  2004. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
  2005. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
  2006. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
  2007. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
  2008. [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
  2009. .nla_pol = xfrma_spd_policy,
  2010. .nla_max = XFRMA_SPD_MAX },
  2011. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
  2012. };
  2013. static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  2014. {
  2015. struct net *net = sock_net(skb->sk);
  2016. struct nlattr *attrs[XFRMA_MAX+1];
  2017. const struct xfrm_link *link;
  2018. int type, err;
  2019. type = nlh->nlmsg_type;
  2020. if (type > XFRM_MSG_MAX)
  2021. return -EINVAL;
  2022. type -= XFRM_MSG_BASE;
  2023. link = &xfrm_dispatch[type];
  2024. /* All operations require privileges, even GET */
  2025. if (!netlink_net_capable(skb, CAP_NET_ADMIN))
  2026. return -EPERM;
  2027. if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
  2028. type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
  2029. (nlh->nlmsg_flags & NLM_F_DUMP)) {
  2030. if (link->dump == NULL)
  2031. return -EINVAL;
  2032. {
  2033. struct netlink_dump_control c = {
  2034. .dump = link->dump,
  2035. .done = link->done,
  2036. };
  2037. return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
  2038. }
  2039. }
  2040. err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
  2041. link->nla_max ? : XFRMA_MAX,
  2042. link->nla_pol ? : xfrma_policy);
  2043. if (err < 0)
  2044. return err;
  2045. if (link->doit == NULL)
  2046. return -EINVAL;
  2047. return link->doit(skb, nlh, attrs);
  2048. }
  2049. static void xfrm_netlink_rcv(struct sk_buff *skb)
  2050. {
  2051. struct net *net = sock_net(skb->sk);
  2052. mutex_lock(&net->xfrm.xfrm_cfg_mutex);
  2053. netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
  2054. mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
  2055. }
  2056. static inline size_t xfrm_expire_msgsize(void)
  2057. {
  2058. return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
  2059. + nla_total_size(sizeof(struct xfrm_mark));
  2060. }
  2061. static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
  2062. {
  2063. struct xfrm_user_expire *ue;
  2064. struct nlmsghdr *nlh;
  2065. int err;
  2066. nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
  2067. if (nlh == NULL)
  2068. return -EMSGSIZE;
  2069. ue = nlmsg_data(nlh);
  2070. copy_to_user_state(x, &ue->state);
  2071. ue->hard = (c->data.hard != 0) ? 1 : 0;
  2072. err = xfrm_mark_put(skb, &x->mark);
  2073. if (err)
  2074. return err;
  2075. return nlmsg_end(skb, nlh);
  2076. }
  2077. static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
  2078. {
  2079. struct net *net = xs_net(x);
  2080. struct sk_buff *skb;
  2081. skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
  2082. if (skb == NULL)
  2083. return -ENOMEM;
  2084. if (build_expire(skb, x, c) < 0) {
  2085. kfree_skb(skb);
  2086. return -EMSGSIZE;
  2087. }
  2088. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
  2089. }
  2090. static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
  2091. {
  2092. struct net *net = xs_net(x);
  2093. struct sk_buff *skb;
  2094. skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
  2095. if (skb == NULL)
  2096. return -ENOMEM;
  2097. if (build_aevent(skb, x, c) < 0)
  2098. BUG();
  2099. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
  2100. }
  2101. static int xfrm_notify_sa_flush(const struct km_event *c)
  2102. {
  2103. struct net *net = c->net;
  2104. struct xfrm_usersa_flush *p;
  2105. struct nlmsghdr *nlh;
  2106. struct sk_buff *skb;
  2107. int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
  2108. skb = nlmsg_new(len, GFP_ATOMIC);
  2109. if (skb == NULL)
  2110. return -ENOMEM;
  2111. nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
  2112. if (nlh == NULL) {
  2113. kfree_skb(skb);
  2114. return -EMSGSIZE;
  2115. }
  2116. p = nlmsg_data(nlh);
  2117. p->proto = c->data.proto;
  2118. nlmsg_end(skb, nlh);
  2119. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
  2120. }
  2121. static inline size_t xfrm_sa_len(struct xfrm_state *x)
  2122. {
  2123. size_t l = 0;
  2124. if (x->aead)
  2125. l += nla_total_size(aead_len(x->aead));
  2126. if (x->aalg) {
  2127. l += nla_total_size(sizeof(struct xfrm_algo) +
  2128. (x->aalg->alg_key_len + 7) / 8);
  2129. l += nla_total_size(xfrm_alg_auth_len(x->aalg));
  2130. }
  2131. if (x->ealg)
  2132. l += nla_total_size(xfrm_alg_len(x->ealg));
  2133. if (x->calg)
  2134. l += nla_total_size(sizeof(*x->calg));
  2135. if (x->encap)
  2136. l += nla_total_size(sizeof(*x->encap));
  2137. if (x->tfcpad)
  2138. l += nla_total_size(sizeof(x->tfcpad));
  2139. if (x->replay_esn)
  2140. l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
  2141. else
  2142. l += nla_total_size(sizeof(struct xfrm_replay_state));
  2143. if (x->security)
  2144. l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
  2145. x->security->ctx_len);
  2146. if (x->coaddr)
  2147. l += nla_total_size(sizeof(*x->coaddr));
  2148. if (x->props.extra_flags)
  2149. l += nla_total_size(sizeof(x->props.extra_flags));
  2150. /* Must count x->lastused as it may become non-zero behind our back. */
  2151. l += nla_total_size(sizeof(u64));
  2152. return l;
  2153. }
  2154. static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
  2155. {
  2156. struct net *net = xs_net(x);
  2157. struct xfrm_usersa_info *p;
  2158. struct xfrm_usersa_id *id;
  2159. struct nlmsghdr *nlh;
  2160. struct sk_buff *skb;
  2161. int len = xfrm_sa_len(x);
  2162. int headlen, err;
  2163. headlen = sizeof(*p);
  2164. if (c->event == XFRM_MSG_DELSA) {
  2165. len += nla_total_size(headlen);
  2166. headlen = sizeof(*id);
  2167. len += nla_total_size(sizeof(struct xfrm_mark));
  2168. }
  2169. len += NLMSG_ALIGN(headlen);
  2170. skb = nlmsg_new(len, GFP_ATOMIC);
  2171. if (skb == NULL)
  2172. return -ENOMEM;
  2173. nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
  2174. err = -EMSGSIZE;
  2175. if (nlh == NULL)
  2176. goto out_free_skb;
  2177. p = nlmsg_data(nlh);
  2178. if (c->event == XFRM_MSG_DELSA) {
  2179. struct nlattr *attr;
  2180. id = nlmsg_data(nlh);
  2181. memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
  2182. id->spi = x->id.spi;
  2183. id->family = x->props.family;
  2184. id->proto = x->id.proto;
  2185. attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
  2186. err = -EMSGSIZE;
  2187. if (attr == NULL)
  2188. goto out_free_skb;
  2189. p = nla_data(attr);
  2190. }
  2191. err = copy_to_user_state_extra(x, p, skb);
  2192. if (err)
  2193. goto out_free_skb;
  2194. nlmsg_end(skb, nlh);
  2195. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
  2196. out_free_skb:
  2197. kfree_skb(skb);
  2198. return err;
  2199. }
  2200. static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
  2201. {
  2202. switch (c->event) {
  2203. case XFRM_MSG_EXPIRE:
  2204. return xfrm_exp_state_notify(x, c);
  2205. case XFRM_MSG_NEWAE:
  2206. return xfrm_aevent_state_notify(x, c);
  2207. case XFRM_MSG_DELSA:
  2208. case XFRM_MSG_UPDSA:
  2209. case XFRM_MSG_NEWSA:
  2210. return xfrm_notify_sa(x, c);
  2211. case XFRM_MSG_FLUSHSA:
  2212. return xfrm_notify_sa_flush(c);
  2213. default:
  2214. printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
  2215. c->event);
  2216. break;
  2217. }
  2218. return 0;
  2219. }
  2220. static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
  2221. struct xfrm_policy *xp)
  2222. {
  2223. return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
  2224. + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
  2225. + nla_total_size(sizeof(struct xfrm_mark))
  2226. + nla_total_size(xfrm_user_sec_ctx_size(x->security))
  2227. + userpolicy_type_attrsize();
  2228. }
  2229. static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
  2230. struct xfrm_tmpl *xt, struct xfrm_policy *xp)
  2231. {
  2232. __u32 seq = xfrm_get_acqseq();
  2233. struct xfrm_user_acquire *ua;
  2234. struct nlmsghdr *nlh;
  2235. int err;
  2236. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
  2237. if (nlh == NULL)
  2238. return -EMSGSIZE;
  2239. ua = nlmsg_data(nlh);
  2240. memcpy(&ua->id, &x->id, sizeof(ua->id));
  2241. memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
  2242. memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
  2243. copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
  2244. ua->aalgos = xt->aalgos;
  2245. ua->ealgos = xt->ealgos;
  2246. ua->calgos = xt->calgos;
  2247. ua->seq = x->km.seq = seq;
  2248. err = copy_to_user_tmpl(xp, skb);
  2249. if (!err)
  2250. err = copy_to_user_state_sec_ctx(x, skb);
  2251. if (!err)
  2252. err = copy_to_user_policy_type(xp->type, skb);
  2253. if (!err)
  2254. err = xfrm_mark_put(skb, &xp->mark);
  2255. if (err) {
  2256. nlmsg_cancel(skb, nlh);
  2257. return err;
  2258. }
  2259. return nlmsg_end(skb, nlh);
  2260. }
  2261. static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
  2262. struct xfrm_policy *xp)
  2263. {
  2264. struct net *net = xs_net(x);
  2265. struct sk_buff *skb;
  2266. skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
  2267. if (skb == NULL)
  2268. return -ENOMEM;
  2269. if (build_acquire(skb, x, xt, xp) < 0)
  2270. BUG();
  2271. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
  2272. }
  2273. /* User gives us xfrm_user_policy_info followed by an array of 0
  2274. * or more templates.
  2275. */
  2276. static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
  2277. u8 *data, int len, int *dir)
  2278. {
  2279. struct net *net = sock_net(sk);
  2280. struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
  2281. struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
  2282. struct xfrm_policy *xp;
  2283. int nr;
  2284. switch (sk->sk_family) {
  2285. case AF_INET:
  2286. if (opt != IP_XFRM_POLICY) {
  2287. *dir = -EOPNOTSUPP;
  2288. return NULL;
  2289. }
  2290. break;
  2291. #if IS_ENABLED(CONFIG_IPV6)
  2292. case AF_INET6:
  2293. if (opt != IPV6_XFRM_POLICY) {
  2294. *dir = -EOPNOTSUPP;
  2295. return NULL;
  2296. }
  2297. break;
  2298. #endif
  2299. default:
  2300. *dir = -EINVAL;
  2301. return NULL;
  2302. }
  2303. *dir = -EINVAL;
  2304. if (len < sizeof(*p) ||
  2305. verify_newpolicy_info(p))
  2306. return NULL;
  2307. nr = ((len - sizeof(*p)) / sizeof(*ut));
  2308. if (validate_tmpl(nr, ut, p->sel.family))
  2309. return NULL;
  2310. if (p->dir > XFRM_POLICY_OUT)
  2311. return NULL;
  2312. xp = xfrm_policy_alloc(net, GFP_ATOMIC);
  2313. if (xp == NULL) {
  2314. *dir = -ENOBUFS;
  2315. return NULL;
  2316. }
  2317. copy_from_user_policy(xp, p);
  2318. xp->type = XFRM_POLICY_TYPE_MAIN;
  2319. copy_templates(xp, ut, nr);
  2320. *dir = p->dir;
  2321. return xp;
  2322. }
  2323. static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
  2324. {
  2325. return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
  2326. + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
  2327. + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
  2328. + nla_total_size(sizeof(struct xfrm_mark))
  2329. + userpolicy_type_attrsize();
  2330. }
  2331. static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
  2332. int dir, const struct km_event *c)
  2333. {
  2334. struct xfrm_user_polexpire *upe;
  2335. int hard = c->data.hard;
  2336. struct nlmsghdr *nlh;
  2337. int err;
  2338. nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
  2339. if (nlh == NULL)
  2340. return -EMSGSIZE;
  2341. upe = nlmsg_data(nlh);
  2342. copy_to_user_policy(xp, &upe->pol, dir);
  2343. err = copy_to_user_tmpl(xp, skb);
  2344. if (!err)
  2345. err = copy_to_user_sec_ctx(xp, skb);
  2346. if (!err)
  2347. err = copy_to_user_policy_type(xp->type, skb);
  2348. if (!err)
  2349. err = xfrm_mark_put(skb, &xp->mark);
  2350. if (err) {
  2351. nlmsg_cancel(skb, nlh);
  2352. return err;
  2353. }
  2354. upe->hard = !!hard;
  2355. return nlmsg_end(skb, nlh);
  2356. }
  2357. static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
  2358. {
  2359. struct net *net = xp_net(xp);
  2360. struct sk_buff *skb;
  2361. skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
  2362. if (skb == NULL)
  2363. return -ENOMEM;
  2364. if (build_polexpire(skb, xp, dir, c) < 0)
  2365. BUG();
  2366. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
  2367. }
  2368. static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
  2369. {
  2370. int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  2371. struct net *net = xp_net(xp);
  2372. struct xfrm_userpolicy_info *p;
  2373. struct xfrm_userpolicy_id *id;
  2374. struct nlmsghdr *nlh;
  2375. struct sk_buff *skb;
  2376. int headlen, err;
  2377. headlen = sizeof(*p);
  2378. if (c->event == XFRM_MSG_DELPOLICY) {
  2379. len += nla_total_size(headlen);
  2380. headlen = sizeof(*id);
  2381. }
  2382. len += userpolicy_type_attrsize();
  2383. len += nla_total_size(sizeof(struct xfrm_mark));
  2384. len += NLMSG_ALIGN(headlen);
  2385. skb = nlmsg_new(len, GFP_ATOMIC);
  2386. if (skb == NULL)
  2387. return -ENOMEM;
  2388. nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
  2389. err = -EMSGSIZE;
  2390. if (nlh == NULL)
  2391. goto out_free_skb;
  2392. p = nlmsg_data(nlh);
  2393. if (c->event == XFRM_MSG_DELPOLICY) {
  2394. struct nlattr *attr;
  2395. id = nlmsg_data(nlh);
  2396. memset(id, 0, sizeof(*id));
  2397. id->dir = dir;
  2398. if (c->data.byid)
  2399. id->index = xp->index;
  2400. else
  2401. memcpy(&id->sel, &xp->selector, sizeof(id->sel));
  2402. attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
  2403. err = -EMSGSIZE;
  2404. if (attr == NULL)
  2405. goto out_free_skb;
  2406. p = nla_data(attr);
  2407. }
  2408. copy_to_user_policy(xp, p, dir);
  2409. err = copy_to_user_tmpl(xp, skb);
  2410. if (!err)
  2411. err = copy_to_user_policy_type(xp->type, skb);
  2412. if (!err)
  2413. err = xfrm_mark_put(skb, &xp->mark);
  2414. if (err)
  2415. goto out_free_skb;
  2416. nlmsg_end(skb, nlh);
  2417. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
  2418. out_free_skb:
  2419. kfree_skb(skb);
  2420. return err;
  2421. }
  2422. static int xfrm_notify_policy_flush(const struct km_event *c)
  2423. {
  2424. struct net *net = c->net;
  2425. struct nlmsghdr *nlh;
  2426. struct sk_buff *skb;
  2427. int err;
  2428. skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
  2429. if (skb == NULL)
  2430. return -ENOMEM;
  2431. nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
  2432. err = -EMSGSIZE;
  2433. if (nlh == NULL)
  2434. goto out_free_skb;
  2435. err = copy_to_user_policy_type(c->data.type, skb);
  2436. if (err)
  2437. goto out_free_skb;
  2438. nlmsg_end(skb, nlh);
  2439. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
  2440. out_free_skb:
  2441. kfree_skb(skb);
  2442. return err;
  2443. }
  2444. static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
  2445. {
  2446. switch (c->event) {
  2447. case XFRM_MSG_NEWPOLICY:
  2448. case XFRM_MSG_UPDPOLICY:
  2449. case XFRM_MSG_DELPOLICY:
  2450. return xfrm_notify_policy(xp, dir, c);
  2451. case XFRM_MSG_FLUSHPOLICY:
  2452. return xfrm_notify_policy_flush(c);
  2453. case XFRM_MSG_POLEXPIRE:
  2454. return xfrm_exp_policy_notify(xp, dir, c);
  2455. default:
  2456. printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
  2457. c->event);
  2458. }
  2459. return 0;
  2460. }
  2461. static inline size_t xfrm_report_msgsize(void)
  2462. {
  2463. return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
  2464. }
  2465. static int build_report(struct sk_buff *skb, u8 proto,
  2466. struct xfrm_selector *sel, xfrm_address_t *addr)
  2467. {
  2468. struct xfrm_user_report *ur;
  2469. struct nlmsghdr *nlh;
  2470. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
  2471. if (nlh == NULL)
  2472. return -EMSGSIZE;
  2473. ur = nlmsg_data(nlh);
  2474. ur->proto = proto;
  2475. memcpy(&ur->sel, sel, sizeof(ur->sel));
  2476. if (addr) {
  2477. int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
  2478. if (err) {
  2479. nlmsg_cancel(skb, nlh);
  2480. return err;
  2481. }
  2482. }
  2483. return nlmsg_end(skb, nlh);
  2484. }
  2485. static int xfrm_send_report(struct net *net, u8 proto,
  2486. struct xfrm_selector *sel, xfrm_address_t *addr)
  2487. {
  2488. struct sk_buff *skb;
  2489. skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
  2490. if (skb == NULL)
  2491. return -ENOMEM;
  2492. if (build_report(skb, proto, sel, addr) < 0)
  2493. BUG();
  2494. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
  2495. }
  2496. static inline size_t xfrm_mapping_msgsize(void)
  2497. {
  2498. return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
  2499. }
  2500. static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
  2501. xfrm_address_t *new_saddr, __be16 new_sport)
  2502. {
  2503. struct xfrm_user_mapping *um;
  2504. struct nlmsghdr *nlh;
  2505. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
  2506. if (nlh == NULL)
  2507. return -EMSGSIZE;
  2508. um = nlmsg_data(nlh);
  2509. memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
  2510. um->id.spi = x->id.spi;
  2511. um->id.family = x->props.family;
  2512. um->id.proto = x->id.proto;
  2513. memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
  2514. memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
  2515. um->new_sport = new_sport;
  2516. um->old_sport = x->encap->encap_sport;
  2517. um->reqid = x->props.reqid;
  2518. return nlmsg_end(skb, nlh);
  2519. }
  2520. static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
  2521. __be16 sport)
  2522. {
  2523. struct net *net = xs_net(x);
  2524. struct sk_buff *skb;
  2525. if (x->id.proto != IPPROTO_ESP)
  2526. return -EINVAL;
  2527. if (!x->encap)
  2528. return -EINVAL;
  2529. skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
  2530. if (skb == NULL)
  2531. return -ENOMEM;
  2532. if (build_mapping(skb, x, ipaddr, sport) < 0)
  2533. BUG();
  2534. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
  2535. }
  2536. static bool xfrm_is_alive(const struct km_event *c)
  2537. {
  2538. return (bool)xfrm_acquire_is_on(c->net);
  2539. }
  2540. static struct xfrm_mgr netlink_mgr = {
  2541. .id = "netlink",
  2542. .notify = xfrm_send_state_notify,
  2543. .acquire = xfrm_send_acquire,
  2544. .compile_policy = xfrm_compile_policy,
  2545. .notify_policy = xfrm_send_policy_notify,
  2546. .report = xfrm_send_report,
  2547. .migrate = xfrm_send_migrate,
  2548. .new_mapping = xfrm_send_mapping,
  2549. .is_alive = xfrm_is_alive,
  2550. };
  2551. static int __net_init xfrm_user_net_init(struct net *net)
  2552. {
  2553. struct sock *nlsk;
  2554. struct netlink_kernel_cfg cfg = {
  2555. .groups = XFRMNLGRP_MAX,
  2556. .input = xfrm_netlink_rcv,
  2557. };
  2558. nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
  2559. if (nlsk == NULL)
  2560. return -ENOMEM;
  2561. net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
  2562. rcu_assign_pointer(net->xfrm.nlsk, nlsk);
  2563. return 0;
  2564. }
  2565. static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
  2566. {
  2567. struct net *net;
  2568. list_for_each_entry(net, net_exit_list, exit_list)
  2569. RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
  2570. synchronize_net();
  2571. list_for_each_entry(net, net_exit_list, exit_list)
  2572. netlink_kernel_release(net->xfrm.nlsk_stash);
  2573. }
  2574. static struct pernet_operations xfrm_user_net_ops = {
  2575. .init = xfrm_user_net_init,
  2576. .exit_batch = xfrm_user_net_exit,
  2577. };
  2578. static int __init xfrm_user_init(void)
  2579. {
  2580. int rv;
  2581. printk(KERN_INFO "Initializing XFRM netlink socket\n");
  2582. rv = register_pernet_subsys(&xfrm_user_net_ops);
  2583. if (rv < 0)
  2584. return rv;
  2585. rv = xfrm_register_km(&netlink_mgr);
  2586. if (rv < 0)
  2587. unregister_pernet_subsys(&xfrm_user_net_ops);
  2588. return rv;
  2589. }
  2590. static void __exit xfrm_user_exit(void)
  2591. {
  2592. xfrm_unregister_km(&netlink_mgr);
  2593. unregister_pernet_subsys(&xfrm_user_net_ops);
  2594. }
  2595. module_init(xfrm_user_init);
  2596. module_exit(xfrm_user_exit);
  2597. MODULE_LICENSE("GPL");
  2598. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);