xfrm_user.c 78 KB

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