xfrm_user.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* xfrm_user.c: User interface to configure xfrm engine.
  3. *
  4. * Copyright (C) 2002 David S. Miller (davem@redhat.com)
  5. *
  6. * Changes:
  7. * Mitsuru KANDA @USAGI
  8. * Kazunori MIYAZAWA @USAGI
  9. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  10. * IPv6 support
  11. *
  12. */
  13. #include <linux/compat.h>
  14. #include <linux/crypto.h>
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/slab.h>
  19. #include <linux/socket.h>
  20. #include <linux/string.h>
  21. #include <linux/net.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/pfkeyv2.h>
  24. #include <linux/ipsec.h>
  25. #include <linux/init.h>
  26. #include <linux/security.h>
  27. #include <net/sock.h>
  28. #include <net/xfrm.h>
  29. #include <net/netlink.h>
  30. #include <net/ah.h>
  31. #include <linux/uaccess.h>
  32. #if IS_ENABLED(CONFIG_IPV6)
  33. #include <linux/in6.h>
  34. #endif
  35. #include <linux/unaligned.h>
  36. static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type,
  37. struct netlink_ext_ack *extack)
  38. {
  39. struct nlattr *rt = attrs[type];
  40. struct xfrm_algo *algp;
  41. if (!rt)
  42. return 0;
  43. algp = nla_data(rt);
  44. if (nla_len(rt) < (int)xfrm_alg_len(algp)) {
  45. NL_SET_ERR_MSG(extack, "Invalid AUTH/CRYPT/COMP attribute length");
  46. return -EINVAL;
  47. }
  48. switch (type) {
  49. case XFRMA_ALG_AUTH:
  50. case XFRMA_ALG_CRYPT:
  51. case XFRMA_ALG_COMP:
  52. break;
  53. default:
  54. NL_SET_ERR_MSG(extack, "Invalid algorithm attribute type");
  55. return -EINVAL;
  56. }
  57. algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
  58. return 0;
  59. }
  60. static int verify_auth_trunc(struct nlattr **attrs,
  61. struct netlink_ext_ack *extack)
  62. {
  63. struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
  64. struct xfrm_algo_auth *algp;
  65. if (!rt)
  66. return 0;
  67. algp = nla_data(rt);
  68. if (nla_len(rt) < (int)xfrm_alg_auth_len(algp)) {
  69. NL_SET_ERR_MSG(extack, "Invalid AUTH_TRUNC attribute length");
  70. return -EINVAL;
  71. }
  72. algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
  73. return 0;
  74. }
  75. static int verify_aead(struct nlattr **attrs, struct netlink_ext_ack *extack)
  76. {
  77. struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
  78. struct xfrm_algo_aead *algp;
  79. if (!rt)
  80. return 0;
  81. algp = nla_data(rt);
  82. if (nla_len(rt) < (int)aead_len(algp)) {
  83. NL_SET_ERR_MSG(extack, "Invalid AEAD attribute length");
  84. return -EINVAL;
  85. }
  86. algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
  87. return 0;
  88. }
  89. static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
  90. xfrm_address_t **addrp)
  91. {
  92. struct nlattr *rt = attrs[type];
  93. if (rt && addrp)
  94. *addrp = nla_data(rt);
  95. }
  96. static inline int verify_sec_ctx_len(struct nlattr **attrs, struct netlink_ext_ack *extack)
  97. {
  98. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  99. struct xfrm_user_sec_ctx *uctx;
  100. if (!rt)
  101. return 0;
  102. uctx = nla_data(rt);
  103. if (uctx->len > nla_len(rt) ||
  104. uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len)) {
  105. NL_SET_ERR_MSG(extack, "Invalid security context length");
  106. return -EINVAL;
  107. }
  108. return 0;
  109. }
  110. static inline int verify_replay(struct xfrm_usersa_info *p,
  111. struct nlattr **attrs, u8 sa_dir,
  112. struct netlink_ext_ack *extack)
  113. {
  114. struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
  115. struct xfrm_replay_state_esn *rs;
  116. if (!rt) {
  117. if (p->flags & XFRM_STATE_ESN) {
  118. NL_SET_ERR_MSG(extack, "Missing required attribute for ESN");
  119. return -EINVAL;
  120. }
  121. return 0;
  122. }
  123. rs = nla_data(rt);
  124. if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8) {
  125. NL_SET_ERR_MSG(extack, "ESN bitmap length must be <= 128");
  126. return -EINVAL;
  127. }
  128. if (nla_len(rt) < (int)xfrm_replay_state_esn_len(rs) &&
  129. nla_len(rt) != sizeof(*rs)) {
  130. NL_SET_ERR_MSG(extack, "ESN attribute is too short to fit the full bitmap length");
  131. return -EINVAL;
  132. }
  133. /* As only ESP and AH support ESN feature. */
  134. if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH)) {
  135. NL_SET_ERR_MSG(extack, "ESN only supported for ESP and AH");
  136. return -EINVAL;
  137. }
  138. if (p->replay_window != 0) {
  139. NL_SET_ERR_MSG(extack, "ESN not compatible with legacy replay_window");
  140. return -EINVAL;
  141. }
  142. if (sa_dir == XFRM_SA_DIR_OUT) {
  143. if (rs->replay_window) {
  144. NL_SET_ERR_MSG(extack, "Replay window should be 0 for output SA");
  145. return -EINVAL;
  146. }
  147. if (rs->seq || rs->seq_hi) {
  148. NL_SET_ERR_MSG(extack,
  149. "Replay seq and seq_hi should be 0 for output SA");
  150. return -EINVAL;
  151. }
  152. if (!(p->flags & XFRM_STATE_ESN)) {
  153. if (rs->oseq_hi) {
  154. NL_SET_ERR_MSG(
  155. extack,
  156. "Replay oseq_hi should be 0 in non-ESN mode for output SA");
  157. return -EINVAL;
  158. }
  159. if (rs->oseq == U32_MAX) {
  160. NL_SET_ERR_MSG(
  161. extack,
  162. "Replay oseq should be less than 0xFFFFFFFF in non-ESN mode for output SA");
  163. return -EINVAL;
  164. }
  165. } else {
  166. if (rs->oseq == U32_MAX && rs->oseq_hi == U32_MAX) {
  167. NL_SET_ERR_MSG(
  168. extack,
  169. "Replay oseq and oseq_hi should be less than 0xFFFFFFFF for output SA");
  170. return -EINVAL;
  171. }
  172. }
  173. if (rs->bmp_len) {
  174. NL_SET_ERR_MSG(extack, "Replay bmp_len should 0 for output SA");
  175. return -EINVAL;
  176. }
  177. }
  178. if (sa_dir == XFRM_SA_DIR_IN) {
  179. if (rs->oseq || rs->oseq_hi) {
  180. NL_SET_ERR_MSG(extack,
  181. "Replay oseq and oseq_hi should be 0 for input SA");
  182. return -EINVAL;
  183. }
  184. if (!(p->flags & XFRM_STATE_ESN)) {
  185. if (rs->seq_hi) {
  186. NL_SET_ERR_MSG(
  187. extack,
  188. "Replay seq_hi should be 0 in non-ESN mode for input SA");
  189. return -EINVAL;
  190. }
  191. if (rs->seq == U32_MAX) {
  192. NL_SET_ERR_MSG(
  193. extack,
  194. "Replay seq should be less than 0xFFFFFFFF in non-ESN mode for input SA");
  195. return -EINVAL;
  196. }
  197. } else {
  198. if (rs->seq == U32_MAX && rs->seq_hi == U32_MAX) {
  199. NL_SET_ERR_MSG(
  200. extack,
  201. "Replay seq and seq_hi should be less than 0xFFFFFFFF for input SA");
  202. return -EINVAL;
  203. }
  204. }
  205. }
  206. return 0;
  207. }
  208. static int verify_newsa_info(struct xfrm_usersa_info *p,
  209. struct nlattr **attrs,
  210. struct netlink_ext_ack *extack)
  211. {
  212. int err;
  213. u8 sa_dir = attrs[XFRMA_SA_DIR] ? nla_get_u8(attrs[XFRMA_SA_DIR]) : 0;
  214. u16 family = p->sel.family;
  215. err = -EINVAL;
  216. switch (p->family) {
  217. case AF_INET:
  218. break;
  219. case AF_INET6:
  220. #if IS_ENABLED(CONFIG_IPV6)
  221. break;
  222. #else
  223. err = -EAFNOSUPPORT;
  224. NL_SET_ERR_MSG(extack, "IPv6 support disabled");
  225. goto out;
  226. #endif
  227. default:
  228. NL_SET_ERR_MSG(extack, "Invalid address family");
  229. goto out;
  230. }
  231. if (!family && !(p->flags & XFRM_STATE_AF_UNSPEC))
  232. family = p->family;
  233. switch (family) {
  234. case AF_UNSPEC:
  235. break;
  236. case AF_INET:
  237. if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) {
  238. NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)");
  239. goto out;
  240. }
  241. break;
  242. case AF_INET6:
  243. #if IS_ENABLED(CONFIG_IPV6)
  244. if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) {
  245. NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)");
  246. goto out;
  247. }
  248. break;
  249. #else
  250. NL_SET_ERR_MSG(extack, "IPv6 support disabled");
  251. err = -EAFNOSUPPORT;
  252. goto out;
  253. #endif
  254. default:
  255. NL_SET_ERR_MSG(extack, "Invalid address family in selector");
  256. goto out;
  257. }
  258. err = -EINVAL;
  259. switch (p->id.proto) {
  260. case IPPROTO_AH:
  261. if (!attrs[XFRMA_ALG_AUTH] &&
  262. !attrs[XFRMA_ALG_AUTH_TRUNC]) {
  263. NL_SET_ERR_MSG(extack, "Missing required attribute for AH: AUTH_TRUNC or AUTH");
  264. goto out;
  265. }
  266. if (attrs[XFRMA_ALG_AEAD] ||
  267. attrs[XFRMA_ALG_CRYPT] ||
  268. attrs[XFRMA_ALG_COMP] ||
  269. attrs[XFRMA_TFCPAD]) {
  270. NL_SET_ERR_MSG(extack, "Invalid attributes for AH: AEAD, CRYPT, COMP, TFCPAD");
  271. goto out;
  272. }
  273. break;
  274. case IPPROTO_ESP:
  275. if (attrs[XFRMA_ALG_COMP]) {
  276. NL_SET_ERR_MSG(extack, "Invalid attribute for ESP: COMP");
  277. goto out;
  278. }
  279. if (!attrs[XFRMA_ALG_AUTH] &&
  280. !attrs[XFRMA_ALG_AUTH_TRUNC] &&
  281. !attrs[XFRMA_ALG_CRYPT] &&
  282. !attrs[XFRMA_ALG_AEAD]) {
  283. NL_SET_ERR_MSG(extack, "Missing required attribute for ESP: at least one of AUTH, AUTH_TRUNC, CRYPT, AEAD");
  284. goto out;
  285. }
  286. if ((attrs[XFRMA_ALG_AUTH] ||
  287. attrs[XFRMA_ALG_AUTH_TRUNC] ||
  288. attrs[XFRMA_ALG_CRYPT]) &&
  289. attrs[XFRMA_ALG_AEAD]) {
  290. NL_SET_ERR_MSG(extack, "Invalid attribute combination for ESP: AEAD can't be used with AUTH, AUTH_TRUNC, CRYPT");
  291. goto out;
  292. }
  293. if (attrs[XFRMA_TFCPAD] &&
  294. p->mode != XFRM_MODE_TUNNEL) {
  295. NL_SET_ERR_MSG(extack, "TFC padding can only be used in tunnel mode");
  296. goto out;
  297. }
  298. break;
  299. case IPPROTO_COMP:
  300. if (!attrs[XFRMA_ALG_COMP]) {
  301. NL_SET_ERR_MSG(extack, "Missing required attribute for COMP: COMP");
  302. goto out;
  303. }
  304. if (attrs[XFRMA_ALG_AEAD] ||
  305. attrs[XFRMA_ALG_AUTH] ||
  306. attrs[XFRMA_ALG_AUTH_TRUNC] ||
  307. attrs[XFRMA_ALG_CRYPT] ||
  308. attrs[XFRMA_TFCPAD]) {
  309. NL_SET_ERR_MSG(extack, "Invalid attributes for COMP: AEAD, AUTH, AUTH_TRUNC, CRYPT, TFCPAD");
  310. goto out;
  311. }
  312. if (ntohl(p->id.spi) >= 0x10000) {
  313. NL_SET_ERR_MSG(extack, "SPI is too large for COMP (must be < 0x10000)");
  314. goto out;
  315. }
  316. break;
  317. #if IS_ENABLED(CONFIG_IPV6)
  318. case IPPROTO_DSTOPTS:
  319. case IPPROTO_ROUTING:
  320. if (attrs[XFRMA_ALG_COMP] ||
  321. attrs[XFRMA_ALG_AUTH] ||
  322. attrs[XFRMA_ALG_AUTH_TRUNC] ||
  323. attrs[XFRMA_ALG_AEAD] ||
  324. attrs[XFRMA_ALG_CRYPT] ||
  325. attrs[XFRMA_ENCAP] ||
  326. attrs[XFRMA_SEC_CTX] ||
  327. attrs[XFRMA_TFCPAD]) {
  328. NL_SET_ERR_MSG(extack, "Invalid attributes for DSTOPTS/ROUTING");
  329. goto out;
  330. }
  331. if (!attrs[XFRMA_COADDR]) {
  332. NL_SET_ERR_MSG(extack, "Missing required COADDR attribute for DSTOPTS/ROUTING");
  333. goto out;
  334. }
  335. break;
  336. #endif
  337. default:
  338. NL_SET_ERR_MSG(extack, "Unsupported protocol");
  339. goto out;
  340. }
  341. if ((err = verify_aead(attrs, extack)))
  342. goto out;
  343. if ((err = verify_auth_trunc(attrs, extack)))
  344. goto out;
  345. if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH, extack)))
  346. goto out;
  347. if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT, extack)))
  348. goto out;
  349. if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP, extack)))
  350. goto out;
  351. if ((err = verify_sec_ctx_len(attrs, extack)))
  352. goto out;
  353. if ((err = verify_replay(p, attrs, sa_dir, extack)))
  354. goto out;
  355. err = -EINVAL;
  356. switch (p->mode) {
  357. case XFRM_MODE_TRANSPORT:
  358. case XFRM_MODE_TUNNEL:
  359. case XFRM_MODE_ROUTEOPTIMIZATION:
  360. case XFRM_MODE_BEET:
  361. break;
  362. default:
  363. NL_SET_ERR_MSG(extack, "Unsupported mode");
  364. goto out;
  365. }
  366. err = 0;
  367. if (attrs[XFRMA_MTIMER_THRESH]) {
  368. if (!attrs[XFRMA_ENCAP]) {
  369. NL_SET_ERR_MSG(extack, "MTIMER_THRESH attribute can only be set on ENCAP states");
  370. err = -EINVAL;
  371. goto out;
  372. }
  373. if (sa_dir == XFRM_SA_DIR_OUT) {
  374. NL_SET_ERR_MSG(extack,
  375. "MTIMER_THRESH attribute should not be set on output SA");
  376. err = -EINVAL;
  377. goto out;
  378. }
  379. }
  380. if (sa_dir == XFRM_SA_DIR_OUT) {
  381. if (p->flags & XFRM_STATE_DECAP_DSCP) {
  382. NL_SET_ERR_MSG(extack, "Flag DECAP_DSCP should not be set for output SA");
  383. err = -EINVAL;
  384. goto out;
  385. }
  386. if (p->flags & XFRM_STATE_ICMP) {
  387. NL_SET_ERR_MSG(extack, "Flag ICMP should not be set for output SA");
  388. err = -EINVAL;
  389. goto out;
  390. }
  391. if (p->flags & XFRM_STATE_WILDRECV) {
  392. NL_SET_ERR_MSG(extack, "Flag WILDRECV should not be set for output SA");
  393. err = -EINVAL;
  394. goto out;
  395. }
  396. if (p->replay_window) {
  397. NL_SET_ERR_MSG(extack, "Replay window should be 0 for output SA");
  398. err = -EINVAL;
  399. goto out;
  400. }
  401. if (attrs[XFRMA_REPLAY_VAL]) {
  402. struct xfrm_replay_state *replay;
  403. replay = nla_data(attrs[XFRMA_REPLAY_VAL]);
  404. if (replay->seq || replay->bitmap) {
  405. NL_SET_ERR_MSG(extack,
  406. "Replay seq and bitmap should be 0 for output SA");
  407. err = -EINVAL;
  408. goto out;
  409. }
  410. }
  411. }
  412. if (sa_dir == XFRM_SA_DIR_IN) {
  413. if (p->flags & XFRM_STATE_NOPMTUDISC) {
  414. NL_SET_ERR_MSG(extack, "Flag NOPMTUDISC should not be set for input SA");
  415. err = -EINVAL;
  416. goto out;
  417. }
  418. if (attrs[XFRMA_SA_EXTRA_FLAGS]) {
  419. u32 xflags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
  420. if (xflags & XFRM_SA_XFLAG_DONT_ENCAP_DSCP) {
  421. NL_SET_ERR_MSG(extack, "Flag DONT_ENCAP_DSCP should not be set for input SA");
  422. err = -EINVAL;
  423. goto out;
  424. }
  425. if (xflags & XFRM_SA_XFLAG_OSEQ_MAY_WRAP) {
  426. NL_SET_ERR_MSG(extack, "Flag OSEQ_MAY_WRAP should not be set for input SA");
  427. err = -EINVAL;
  428. goto out;
  429. }
  430. }
  431. }
  432. if (!sa_dir && attrs[XFRMA_SA_PCPU]) {
  433. NL_SET_ERR_MSG(extack, "SA_PCPU only supported with SA_DIR");
  434. err = -EINVAL;
  435. goto out;
  436. }
  437. out:
  438. return err;
  439. }
  440. static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
  441. struct xfrm_algo_desc *(*get_byname)(const char *, int),
  442. struct nlattr *rta, struct netlink_ext_ack *extack)
  443. {
  444. struct xfrm_algo *p, *ualg;
  445. struct xfrm_algo_desc *algo;
  446. if (!rta)
  447. return 0;
  448. ualg = nla_data(rta);
  449. algo = get_byname(ualg->alg_name, 1);
  450. if (!algo) {
  451. NL_SET_ERR_MSG(extack, "Requested COMP algorithm not found");
  452. return -ENOSYS;
  453. }
  454. *props = algo->desc.sadb_alg_id;
  455. p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
  456. if (!p)
  457. return -ENOMEM;
  458. strcpy(p->alg_name, algo->name);
  459. *algpp = p;
  460. return 0;
  461. }
  462. static int attach_crypt(struct xfrm_state *x, struct nlattr *rta,
  463. struct netlink_ext_ack *extack)
  464. {
  465. struct xfrm_algo *p, *ualg;
  466. struct xfrm_algo_desc *algo;
  467. if (!rta)
  468. return 0;
  469. ualg = nla_data(rta);
  470. algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
  471. if (!algo) {
  472. NL_SET_ERR_MSG(extack, "Requested CRYPT algorithm not found");
  473. return -ENOSYS;
  474. }
  475. x->props.ealgo = algo->desc.sadb_alg_id;
  476. p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
  477. if (!p)
  478. return -ENOMEM;
  479. strcpy(p->alg_name, algo->name);
  480. x->ealg = p;
  481. x->geniv = algo->uinfo.encr.geniv;
  482. return 0;
  483. }
  484. static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
  485. struct nlattr *rta, struct netlink_ext_ack *extack)
  486. {
  487. struct xfrm_algo *ualg;
  488. struct xfrm_algo_auth *p;
  489. struct xfrm_algo_desc *algo;
  490. if (!rta)
  491. return 0;
  492. ualg = nla_data(rta);
  493. algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
  494. if (!algo) {
  495. NL_SET_ERR_MSG(extack, "Requested AUTH algorithm not found");
  496. return -ENOSYS;
  497. }
  498. *props = algo->desc.sadb_alg_id;
  499. p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
  500. if (!p)
  501. return -ENOMEM;
  502. strcpy(p->alg_name, algo->name);
  503. p->alg_key_len = ualg->alg_key_len;
  504. p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
  505. memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
  506. *algpp = p;
  507. return 0;
  508. }
  509. static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
  510. struct nlattr *rta, struct netlink_ext_ack *extack)
  511. {
  512. struct xfrm_algo_auth *p, *ualg;
  513. struct xfrm_algo_desc *algo;
  514. if (!rta)
  515. return 0;
  516. ualg = nla_data(rta);
  517. algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
  518. if (!algo) {
  519. NL_SET_ERR_MSG(extack, "Requested AUTH_TRUNC algorithm not found");
  520. return -ENOSYS;
  521. }
  522. if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits) {
  523. NL_SET_ERR_MSG(extack, "Invalid length requested for truncated ICV");
  524. return -EINVAL;
  525. }
  526. *props = algo->desc.sadb_alg_id;
  527. p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
  528. if (!p)
  529. return -ENOMEM;
  530. strcpy(p->alg_name, algo->name);
  531. if (!p->alg_trunc_len)
  532. p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
  533. *algpp = p;
  534. return 0;
  535. }
  536. static int attach_aead(struct xfrm_state *x, struct nlattr *rta,
  537. struct netlink_ext_ack *extack)
  538. {
  539. struct xfrm_algo_aead *p, *ualg;
  540. struct xfrm_algo_desc *algo;
  541. if (!rta)
  542. return 0;
  543. ualg = nla_data(rta);
  544. algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
  545. if (!algo) {
  546. NL_SET_ERR_MSG(extack, "Requested AEAD algorithm not found");
  547. return -ENOSYS;
  548. }
  549. x->props.ealgo = algo->desc.sadb_alg_id;
  550. p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
  551. if (!p)
  552. return -ENOMEM;
  553. strcpy(p->alg_name, algo->name);
  554. x->aead = p;
  555. x->geniv = algo->uinfo.aead.geniv;
  556. return 0;
  557. }
  558. static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
  559. struct nlattr *rp,
  560. struct netlink_ext_ack *extack)
  561. {
  562. struct xfrm_replay_state_esn *up;
  563. unsigned int ulen;
  564. if (!replay_esn || !rp)
  565. return 0;
  566. up = nla_data(rp);
  567. ulen = xfrm_replay_state_esn_len(up);
  568. /* Check the overall length and the internal bitmap length to avoid
  569. * potential overflow. */
  570. if (nla_len(rp) < (int)ulen) {
  571. NL_SET_ERR_MSG(extack, "ESN attribute is too short");
  572. return -EINVAL;
  573. }
  574. if (xfrm_replay_state_esn_len(replay_esn) != ulen) {
  575. NL_SET_ERR_MSG(extack, "New ESN size doesn't match the existing SA's ESN size");
  576. return -EINVAL;
  577. }
  578. if (replay_esn->bmp_len != up->bmp_len) {
  579. NL_SET_ERR_MSG(extack, "New ESN bitmap size doesn't match the existing SA's ESN bitmap");
  580. return -EINVAL;
  581. }
  582. if (up->replay_window > up->bmp_len * sizeof(__u32) * 8) {
  583. NL_SET_ERR_MSG(extack, "ESN replay window is longer than the bitmap");
  584. return -EINVAL;
  585. }
  586. return 0;
  587. }
  588. static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
  589. struct xfrm_replay_state_esn **preplay_esn,
  590. struct nlattr *rta)
  591. {
  592. struct xfrm_replay_state_esn *p, *pp, *up;
  593. unsigned int klen, ulen;
  594. if (!rta)
  595. return 0;
  596. up = nla_data(rta);
  597. klen = xfrm_replay_state_esn_len(up);
  598. ulen = nla_len(rta) >= (int)klen ? klen : sizeof(*up);
  599. p = kzalloc(klen, GFP_KERNEL);
  600. if (!p)
  601. return -ENOMEM;
  602. pp = kzalloc(klen, GFP_KERNEL);
  603. if (!pp) {
  604. kfree(p);
  605. return -ENOMEM;
  606. }
  607. memcpy(p, up, ulen);
  608. memcpy(pp, up, ulen);
  609. *replay_esn = p;
  610. *preplay_esn = pp;
  611. return 0;
  612. }
  613. static inline unsigned int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
  614. {
  615. unsigned int len = 0;
  616. if (xfrm_ctx) {
  617. len += sizeof(struct xfrm_user_sec_ctx);
  618. len += xfrm_ctx->ctx_len;
  619. }
  620. return len;
  621. }
  622. static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  623. {
  624. memcpy(&x->id, &p->id, sizeof(x->id));
  625. memcpy(&x->sel, &p->sel, sizeof(x->sel));
  626. memcpy(&x->lft, &p->lft, sizeof(x->lft));
  627. x->props.mode = p->mode;
  628. x->props.replay_window = min_t(unsigned int, p->replay_window,
  629. sizeof(x->replay.bitmap) * 8);
  630. x->props.reqid = p->reqid;
  631. x->props.family = p->family;
  632. memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
  633. x->props.flags = p->flags;
  634. if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
  635. x->sel.family = p->family;
  636. }
  637. /*
  638. * someday when pfkey also has support, we could have the code
  639. * somehow made shareable and move it to xfrm_state.c - JHS
  640. *
  641. */
  642. static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
  643. int update_esn)
  644. {
  645. struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
  646. struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
  647. struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
  648. struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
  649. struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
  650. struct nlattr *mt = attrs[XFRMA_MTIMER_THRESH];
  651. if (re && x->replay_esn && x->preplay_esn) {
  652. struct xfrm_replay_state_esn *replay_esn;
  653. replay_esn = nla_data(re);
  654. memcpy(x->replay_esn, replay_esn,
  655. xfrm_replay_state_esn_len(replay_esn));
  656. memcpy(x->preplay_esn, replay_esn,
  657. xfrm_replay_state_esn_len(replay_esn));
  658. }
  659. if (rp) {
  660. struct xfrm_replay_state *replay;
  661. replay = nla_data(rp);
  662. memcpy(&x->replay, replay, sizeof(*replay));
  663. memcpy(&x->preplay, replay, sizeof(*replay));
  664. }
  665. if (lt) {
  666. struct xfrm_lifetime_cur *ltime;
  667. ltime = nla_data(lt);
  668. x->curlft.bytes = ltime->bytes;
  669. x->curlft.packets = ltime->packets;
  670. x->curlft.add_time = ltime->add_time;
  671. x->curlft.use_time = ltime->use_time;
  672. }
  673. if (et)
  674. x->replay_maxage = nla_get_u32(et);
  675. if (rt)
  676. x->replay_maxdiff = nla_get_u32(rt);
  677. if (mt)
  678. x->mapping_maxage = nla_get_u32(mt);
  679. }
  680. static void xfrm_smark_init(struct nlattr **attrs, struct xfrm_mark *m)
  681. {
  682. if (attrs[XFRMA_SET_MARK]) {
  683. m->v = nla_get_u32(attrs[XFRMA_SET_MARK]);
  684. if (attrs[XFRMA_SET_MARK_MASK])
  685. m->m = nla_get_u32(attrs[XFRMA_SET_MARK_MASK]);
  686. else
  687. m->m = 0xffffffff;
  688. } else {
  689. m->v = m->m = 0;
  690. }
  691. }
  692. static struct xfrm_state *xfrm_state_construct(struct net *net,
  693. struct xfrm_usersa_info *p,
  694. struct nlattr **attrs,
  695. int *errp,
  696. struct netlink_ext_ack *extack)
  697. {
  698. struct xfrm_state *x = xfrm_state_alloc(net);
  699. int err = -ENOMEM;
  700. if (!x)
  701. goto error_no_put;
  702. copy_from_user_state(x, p);
  703. if (attrs[XFRMA_ENCAP]) {
  704. x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
  705. sizeof(*x->encap), GFP_KERNEL);
  706. if (x->encap == NULL)
  707. goto error;
  708. }
  709. if (attrs[XFRMA_COADDR]) {
  710. x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
  711. sizeof(*x->coaddr), GFP_KERNEL);
  712. if (x->coaddr == NULL)
  713. goto error;
  714. }
  715. if (attrs[XFRMA_SA_EXTRA_FLAGS])
  716. x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
  717. if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD], extack)))
  718. goto error;
  719. if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
  720. attrs[XFRMA_ALG_AUTH_TRUNC], extack)))
  721. goto error;
  722. if (!x->props.aalgo) {
  723. if ((err = attach_auth(&x->aalg, &x->props.aalgo,
  724. attrs[XFRMA_ALG_AUTH], extack)))
  725. goto error;
  726. }
  727. if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT], extack)))
  728. goto error;
  729. if ((err = attach_one_algo(&x->calg, &x->props.calgo,
  730. xfrm_calg_get_byname,
  731. attrs[XFRMA_ALG_COMP], extack)))
  732. goto error;
  733. if (attrs[XFRMA_TFCPAD])
  734. x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
  735. xfrm_mark_get(attrs, &x->mark);
  736. xfrm_smark_init(attrs, &x->props.smark);
  737. if (attrs[XFRMA_IF_ID])
  738. x->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
  739. if (attrs[XFRMA_SA_DIR])
  740. x->dir = nla_get_u8(attrs[XFRMA_SA_DIR]);
  741. if (attrs[XFRMA_NAT_KEEPALIVE_INTERVAL])
  742. x->nat_keepalive_interval =
  743. nla_get_u32(attrs[XFRMA_NAT_KEEPALIVE_INTERVAL]);
  744. if (attrs[XFRMA_SA_PCPU]) {
  745. x->pcpu_num = nla_get_u32(attrs[XFRMA_SA_PCPU]);
  746. if (x->pcpu_num >= num_possible_cpus())
  747. goto error;
  748. }
  749. err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV], extack);
  750. if (err)
  751. goto error;
  752. if (attrs[XFRMA_SEC_CTX]) {
  753. err = security_xfrm_state_alloc(x,
  754. nla_data(attrs[XFRMA_SEC_CTX]));
  755. if (err)
  756. goto error;
  757. }
  758. if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
  759. attrs[XFRMA_REPLAY_ESN_VAL])))
  760. goto error;
  761. x->km.seq = p->seq;
  762. x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
  763. /* sysctl_xfrm_aevent_etime is in 100ms units */
  764. x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
  765. if ((err = xfrm_init_replay(x, extack)))
  766. goto error;
  767. /* override default values from above */
  768. xfrm_update_ae_params(x, attrs, 0);
  769. /* configure the hardware if offload is requested */
  770. if (attrs[XFRMA_OFFLOAD_DEV]) {
  771. err = xfrm_dev_state_add(net, x,
  772. nla_data(attrs[XFRMA_OFFLOAD_DEV]),
  773. extack);
  774. if (err)
  775. goto error;
  776. }
  777. return x;
  778. error:
  779. x->km.state = XFRM_STATE_DEAD;
  780. xfrm_state_put(x);
  781. error_no_put:
  782. *errp = err;
  783. return NULL;
  784. }
  785. static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  786. struct nlattr **attrs, struct netlink_ext_ack *extack)
  787. {
  788. struct net *net = sock_net(skb->sk);
  789. struct xfrm_usersa_info *p = nlmsg_data(nlh);
  790. struct xfrm_state *x;
  791. int err;
  792. struct km_event c;
  793. err = verify_newsa_info(p, attrs, extack);
  794. if (err)
  795. return err;
  796. x = xfrm_state_construct(net, p, attrs, &err, extack);
  797. if (!x)
  798. return err;
  799. xfrm_state_hold(x);
  800. if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
  801. err = xfrm_state_add(x);
  802. else
  803. err = xfrm_state_update(x);
  804. xfrm_audit_state_add(x, err ? 0 : 1, true);
  805. if (err < 0) {
  806. x->km.state = XFRM_STATE_DEAD;
  807. xfrm_dev_state_delete(x);
  808. __xfrm_state_put(x);
  809. goto out;
  810. }
  811. if (x->km.state == XFRM_STATE_VOID)
  812. x->km.state = XFRM_STATE_VALID;
  813. c.seq = nlh->nlmsg_seq;
  814. c.portid = nlh->nlmsg_pid;
  815. c.event = nlh->nlmsg_type;
  816. km_state_notify(x, &c);
  817. out:
  818. xfrm_state_put(x);
  819. return err;
  820. }
  821. static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
  822. struct xfrm_usersa_id *p,
  823. struct nlattr **attrs,
  824. int *errp)
  825. {
  826. struct xfrm_state *x = NULL;
  827. struct xfrm_mark m;
  828. int err;
  829. u32 mark = xfrm_mark_get(attrs, &m);
  830. if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
  831. err = -ESRCH;
  832. x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
  833. } else {
  834. xfrm_address_t *saddr = NULL;
  835. verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
  836. if (!saddr) {
  837. err = -EINVAL;
  838. goto out;
  839. }
  840. err = -ESRCH;
  841. x = xfrm_state_lookup_byaddr(net, mark,
  842. &p->daddr, saddr,
  843. p->proto, p->family);
  844. }
  845. out:
  846. if (!x && errp)
  847. *errp = err;
  848. return x;
  849. }
  850. static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  851. struct nlattr **attrs, struct netlink_ext_ack *extack)
  852. {
  853. struct net *net = sock_net(skb->sk);
  854. struct xfrm_state *x;
  855. int err = -ESRCH;
  856. struct km_event c;
  857. struct xfrm_usersa_id *p = nlmsg_data(nlh);
  858. x = xfrm_user_state_lookup(net, p, attrs, &err);
  859. if (x == NULL)
  860. return err;
  861. if ((err = security_xfrm_state_delete(x)) != 0)
  862. goto out;
  863. if (xfrm_state_kern(x)) {
  864. NL_SET_ERR_MSG(extack, "SA is in use by tunnels");
  865. err = -EPERM;
  866. goto out;
  867. }
  868. err = xfrm_state_delete(x);
  869. if (err < 0)
  870. goto out;
  871. c.seq = nlh->nlmsg_seq;
  872. c.portid = nlh->nlmsg_pid;
  873. c.event = nlh->nlmsg_type;
  874. km_state_notify(x, &c);
  875. out:
  876. xfrm_audit_state_delete(x, err ? 0 : 1, true);
  877. xfrm_state_put(x);
  878. return err;
  879. }
  880. static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  881. {
  882. memset(p, 0, sizeof(*p));
  883. memcpy(&p->id, &x->id, sizeof(p->id));
  884. memcpy(&p->sel, &x->sel, sizeof(p->sel));
  885. memcpy(&p->lft, &x->lft, sizeof(p->lft));
  886. if (x->xso.dev)
  887. xfrm_dev_state_update_stats(x);
  888. memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
  889. put_unaligned(x->stats.replay_window, &p->stats.replay_window);
  890. put_unaligned(x->stats.replay, &p->stats.replay);
  891. put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
  892. memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
  893. p->mode = x->props.mode;
  894. p->replay_window = x->props.replay_window;
  895. p->reqid = x->props.reqid;
  896. p->family = x->props.family;
  897. p->flags = x->props.flags;
  898. p->seq = x->km.seq;
  899. }
  900. struct xfrm_dump_info {
  901. struct sk_buff *in_skb;
  902. struct sk_buff *out_skb;
  903. u32 nlmsg_seq;
  904. u16 nlmsg_flags;
  905. };
  906. static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
  907. {
  908. struct xfrm_user_sec_ctx *uctx;
  909. struct nlattr *attr;
  910. int ctx_size = sizeof(*uctx) + s->ctx_len;
  911. attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
  912. if (attr == NULL)
  913. return -EMSGSIZE;
  914. uctx = nla_data(attr);
  915. uctx->exttype = XFRMA_SEC_CTX;
  916. uctx->len = ctx_size;
  917. uctx->ctx_doi = s->ctx_doi;
  918. uctx->ctx_alg = s->ctx_alg;
  919. uctx->ctx_len = s->ctx_len;
  920. memcpy(uctx + 1, s->ctx_str, s->ctx_len);
  921. return 0;
  922. }
  923. static int copy_user_offload(struct xfrm_dev_offload *xso, struct sk_buff *skb)
  924. {
  925. struct xfrm_user_offload *xuo;
  926. struct nlattr *attr;
  927. attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
  928. if (attr == NULL)
  929. return -EMSGSIZE;
  930. xuo = nla_data(attr);
  931. memset(xuo, 0, sizeof(*xuo));
  932. xuo->ifindex = xso->dev->ifindex;
  933. if (xso->dir == XFRM_DEV_OFFLOAD_IN)
  934. xuo->flags = XFRM_OFFLOAD_INBOUND;
  935. if (xso->type == XFRM_DEV_OFFLOAD_PACKET)
  936. xuo->flags |= XFRM_OFFLOAD_PACKET;
  937. return 0;
  938. }
  939. static bool xfrm_redact(void)
  940. {
  941. return IS_ENABLED(CONFIG_SECURITY) &&
  942. security_locked_down(LOCKDOWN_XFRM_SECRET);
  943. }
  944. static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
  945. {
  946. struct xfrm_algo *algo;
  947. struct xfrm_algo_auth *ap;
  948. struct nlattr *nla;
  949. bool redact_secret = xfrm_redact();
  950. nla = nla_reserve(skb, XFRMA_ALG_AUTH,
  951. sizeof(*algo) + (auth->alg_key_len + 7) / 8);
  952. if (!nla)
  953. return -EMSGSIZE;
  954. algo = nla_data(nla);
  955. strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
  956. if (redact_secret && auth->alg_key_len)
  957. memset(algo->alg_key, 0, (auth->alg_key_len + 7) / 8);
  958. else
  959. memcpy(algo->alg_key, auth->alg_key,
  960. (auth->alg_key_len + 7) / 8);
  961. algo->alg_key_len = auth->alg_key_len;
  962. nla = nla_reserve(skb, XFRMA_ALG_AUTH_TRUNC, xfrm_alg_auth_len(auth));
  963. if (!nla)
  964. return -EMSGSIZE;
  965. ap = nla_data(nla);
  966. strscpy_pad(ap->alg_name, auth->alg_name, sizeof(ap->alg_name));
  967. ap->alg_key_len = auth->alg_key_len;
  968. ap->alg_trunc_len = auth->alg_trunc_len;
  969. if (redact_secret && auth->alg_key_len)
  970. memset(ap->alg_key, 0, (auth->alg_key_len + 7) / 8);
  971. else
  972. memcpy(ap->alg_key, auth->alg_key,
  973. (auth->alg_key_len + 7) / 8);
  974. return 0;
  975. }
  976. static int copy_to_user_aead(struct xfrm_algo_aead *aead, struct sk_buff *skb)
  977. {
  978. struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_AEAD, aead_len(aead));
  979. struct xfrm_algo_aead *ap;
  980. bool redact_secret = xfrm_redact();
  981. if (!nla)
  982. return -EMSGSIZE;
  983. ap = nla_data(nla);
  984. strscpy_pad(ap->alg_name, aead->alg_name, sizeof(ap->alg_name));
  985. ap->alg_key_len = aead->alg_key_len;
  986. ap->alg_icv_len = aead->alg_icv_len;
  987. if (redact_secret && aead->alg_key_len)
  988. memset(ap->alg_key, 0, (aead->alg_key_len + 7) / 8);
  989. else
  990. memcpy(ap->alg_key, aead->alg_key,
  991. (aead->alg_key_len + 7) / 8);
  992. return 0;
  993. }
  994. static int copy_to_user_ealg(struct xfrm_algo *ealg, struct sk_buff *skb)
  995. {
  996. struct xfrm_algo *ap;
  997. bool redact_secret = xfrm_redact();
  998. struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_CRYPT,
  999. xfrm_alg_len(ealg));
  1000. if (!nla)
  1001. return -EMSGSIZE;
  1002. ap = nla_data(nla);
  1003. strscpy_pad(ap->alg_name, ealg->alg_name, sizeof(ap->alg_name));
  1004. ap->alg_key_len = ealg->alg_key_len;
  1005. if (redact_secret && ealg->alg_key_len)
  1006. memset(ap->alg_key, 0, (ealg->alg_key_len + 7) / 8);
  1007. else
  1008. memcpy(ap->alg_key, ealg->alg_key,
  1009. (ealg->alg_key_len + 7) / 8);
  1010. return 0;
  1011. }
  1012. static int copy_to_user_calg(struct xfrm_algo *calg, struct sk_buff *skb)
  1013. {
  1014. struct nlattr *nla = nla_reserve(skb, XFRMA_ALG_COMP, sizeof(*calg));
  1015. struct xfrm_algo *ap;
  1016. if (!nla)
  1017. return -EMSGSIZE;
  1018. ap = nla_data(nla);
  1019. strscpy_pad(ap->alg_name, calg->alg_name, sizeof(ap->alg_name));
  1020. ap->alg_key_len = 0;
  1021. return 0;
  1022. }
  1023. static int copy_to_user_encap(struct xfrm_encap_tmpl *ep, struct sk_buff *skb)
  1024. {
  1025. struct nlattr *nla = nla_reserve(skb, XFRMA_ENCAP, sizeof(*ep));
  1026. struct xfrm_encap_tmpl *uep;
  1027. if (!nla)
  1028. return -EMSGSIZE;
  1029. uep = nla_data(nla);
  1030. memset(uep, 0, sizeof(*uep));
  1031. uep->encap_type = ep->encap_type;
  1032. uep->encap_sport = ep->encap_sport;
  1033. uep->encap_dport = ep->encap_dport;
  1034. uep->encap_oa = ep->encap_oa;
  1035. return 0;
  1036. }
  1037. static int xfrm_smark_put(struct sk_buff *skb, struct xfrm_mark *m)
  1038. {
  1039. int ret = 0;
  1040. if (m->v | m->m) {
  1041. ret = nla_put_u32(skb, XFRMA_SET_MARK, m->v);
  1042. if (!ret)
  1043. ret = nla_put_u32(skb, XFRMA_SET_MARK_MASK, m->m);
  1044. }
  1045. return ret;
  1046. }
  1047. /* Don't change this without updating xfrm_sa_len! */
  1048. static int copy_to_user_state_extra(struct xfrm_state *x,
  1049. struct xfrm_usersa_info *p,
  1050. struct sk_buff *skb)
  1051. {
  1052. int ret = 0;
  1053. copy_to_user_state(x, p);
  1054. if (x->props.extra_flags) {
  1055. ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
  1056. x->props.extra_flags);
  1057. if (ret)
  1058. goto out;
  1059. }
  1060. if (x->coaddr) {
  1061. ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
  1062. if (ret)
  1063. goto out;
  1064. }
  1065. if (x->lastused) {
  1066. ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
  1067. XFRMA_PAD);
  1068. if (ret)
  1069. goto out;
  1070. }
  1071. if (x->aead) {
  1072. ret = copy_to_user_aead(x->aead, skb);
  1073. if (ret)
  1074. goto out;
  1075. }
  1076. if (x->aalg) {
  1077. ret = copy_to_user_auth(x->aalg, skb);
  1078. if (ret)
  1079. goto out;
  1080. }
  1081. if (x->ealg) {
  1082. ret = copy_to_user_ealg(x->ealg, skb);
  1083. if (ret)
  1084. goto out;
  1085. }
  1086. if (x->calg) {
  1087. ret = copy_to_user_calg(x->calg, skb);
  1088. if (ret)
  1089. goto out;
  1090. }
  1091. if (x->encap) {
  1092. ret = copy_to_user_encap(x->encap, skb);
  1093. if (ret)
  1094. goto out;
  1095. }
  1096. if (x->tfcpad) {
  1097. ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
  1098. if (ret)
  1099. goto out;
  1100. }
  1101. ret = xfrm_mark_put(skb, &x->mark);
  1102. if (ret)
  1103. goto out;
  1104. ret = xfrm_smark_put(skb, &x->props.smark);
  1105. if (ret)
  1106. goto out;
  1107. if (x->replay_esn)
  1108. ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
  1109. xfrm_replay_state_esn_len(x->replay_esn),
  1110. x->replay_esn);
  1111. else
  1112. ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
  1113. &x->replay);
  1114. if (ret)
  1115. goto out;
  1116. if(x->xso.dev)
  1117. ret = copy_user_offload(&x->xso, skb);
  1118. if (ret)
  1119. goto out;
  1120. if (x->if_id) {
  1121. ret = nla_put_u32(skb, XFRMA_IF_ID, x->if_id);
  1122. if (ret)
  1123. goto out;
  1124. }
  1125. if (x->security) {
  1126. ret = copy_sec_ctx(x->security, skb);
  1127. if (ret)
  1128. goto out;
  1129. }
  1130. if (x->mapping_maxage) {
  1131. ret = nla_put_u32(skb, XFRMA_MTIMER_THRESH, x->mapping_maxage);
  1132. if (ret)
  1133. goto out;
  1134. }
  1135. if (x->pcpu_num != UINT_MAX) {
  1136. ret = nla_put_u32(skb, XFRMA_SA_PCPU, x->pcpu_num);
  1137. if (ret)
  1138. goto out;
  1139. }
  1140. if (x->dir)
  1141. ret = nla_put_u8(skb, XFRMA_SA_DIR, x->dir);
  1142. if (x->nat_keepalive_interval) {
  1143. ret = nla_put_u32(skb, XFRMA_NAT_KEEPALIVE_INTERVAL,
  1144. x->nat_keepalive_interval);
  1145. if (ret)
  1146. goto out;
  1147. }
  1148. out:
  1149. return ret;
  1150. }
  1151. static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
  1152. {
  1153. struct xfrm_dump_info *sp = ptr;
  1154. struct sk_buff *in_skb = sp->in_skb;
  1155. struct sk_buff *skb = sp->out_skb;
  1156. struct xfrm_translator *xtr;
  1157. struct xfrm_usersa_info *p;
  1158. struct nlmsghdr *nlh;
  1159. int err;
  1160. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
  1161. XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
  1162. if (nlh == NULL)
  1163. return -EMSGSIZE;
  1164. p = nlmsg_data(nlh);
  1165. err = copy_to_user_state_extra(x, p, skb);
  1166. if (err) {
  1167. nlmsg_cancel(skb, nlh);
  1168. return err;
  1169. }
  1170. nlmsg_end(skb, nlh);
  1171. xtr = xfrm_get_translator();
  1172. if (xtr) {
  1173. err = xtr->alloc_compat(skb, nlh);
  1174. xfrm_put_translator(xtr);
  1175. if (err) {
  1176. nlmsg_cancel(skb, nlh);
  1177. return err;
  1178. }
  1179. }
  1180. return 0;
  1181. }
  1182. static int xfrm_dump_sa_done(struct netlink_callback *cb)
  1183. {
  1184. struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
  1185. struct sock *sk = cb->skb->sk;
  1186. struct net *net = sock_net(sk);
  1187. if (cb->args[0])
  1188. xfrm_state_walk_done(walk, net);
  1189. return 0;
  1190. }
  1191. static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
  1192. {
  1193. struct net *net = sock_net(skb->sk);
  1194. struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
  1195. struct xfrm_dump_info info;
  1196. BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
  1197. sizeof(cb->args) - sizeof(cb->args[0]));
  1198. info.in_skb = cb->skb;
  1199. info.out_skb = skb;
  1200. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  1201. info.nlmsg_flags = NLM_F_MULTI;
  1202. if (!cb->args[0]) {
  1203. struct nlattr *attrs[XFRMA_MAX+1];
  1204. struct xfrm_address_filter *filter = NULL;
  1205. u8 proto = 0;
  1206. int err;
  1207. err = nlmsg_parse_deprecated(cb->nlh, 0, attrs, XFRMA_MAX,
  1208. xfrma_policy, cb->extack);
  1209. if (err < 0)
  1210. return err;
  1211. if (attrs[XFRMA_ADDRESS_FILTER]) {
  1212. filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
  1213. sizeof(*filter), GFP_KERNEL);
  1214. if (filter == NULL)
  1215. return -ENOMEM;
  1216. /* see addr_match(), (prefix length >> 5) << 2
  1217. * will be used to compare xfrm_address_t
  1218. */
  1219. if (filter->splen > (sizeof(xfrm_address_t) << 3) ||
  1220. filter->dplen > (sizeof(xfrm_address_t) << 3)) {
  1221. kfree(filter);
  1222. return -EINVAL;
  1223. }
  1224. }
  1225. if (attrs[XFRMA_PROTO])
  1226. proto = nla_get_u8(attrs[XFRMA_PROTO]);
  1227. xfrm_state_walk_init(walk, proto, filter);
  1228. cb->args[0] = 1;
  1229. }
  1230. (void) xfrm_state_walk(net, walk, dump_one_state, &info);
  1231. return skb->len;
  1232. }
  1233. static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
  1234. struct xfrm_state *x, u32 seq)
  1235. {
  1236. struct xfrm_dump_info info;
  1237. struct sk_buff *skb;
  1238. int err;
  1239. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1240. if (!skb)
  1241. return ERR_PTR(-ENOMEM);
  1242. info.in_skb = in_skb;
  1243. info.out_skb = skb;
  1244. info.nlmsg_seq = seq;
  1245. info.nlmsg_flags = 0;
  1246. err = dump_one_state(x, 0, &info);
  1247. if (err) {
  1248. kfree_skb(skb);
  1249. return ERR_PTR(err);
  1250. }
  1251. return skb;
  1252. }
  1253. /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
  1254. * Must be called with RCU read lock.
  1255. */
  1256. static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
  1257. u32 pid, unsigned int group)
  1258. {
  1259. struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
  1260. struct xfrm_translator *xtr;
  1261. if (!nlsk) {
  1262. kfree_skb(skb);
  1263. return -EPIPE;
  1264. }
  1265. xtr = xfrm_get_translator();
  1266. if (xtr) {
  1267. int err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
  1268. xfrm_put_translator(xtr);
  1269. if (err) {
  1270. kfree_skb(skb);
  1271. return err;
  1272. }
  1273. }
  1274. return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
  1275. }
  1276. static inline unsigned int xfrm_spdinfo_msgsize(void)
  1277. {
  1278. return NLMSG_ALIGN(4)
  1279. + nla_total_size(sizeof(struct xfrmu_spdinfo))
  1280. + nla_total_size(sizeof(struct xfrmu_spdhinfo))
  1281. + nla_total_size(sizeof(struct xfrmu_spdhthresh))
  1282. + nla_total_size(sizeof(struct xfrmu_spdhthresh));
  1283. }
  1284. static int build_spdinfo(struct sk_buff *skb, struct net *net,
  1285. u32 portid, u32 seq, u32 flags)
  1286. {
  1287. struct xfrmk_spdinfo si;
  1288. struct xfrmu_spdinfo spc;
  1289. struct xfrmu_spdhinfo sph;
  1290. struct xfrmu_spdhthresh spt4, spt6;
  1291. struct nlmsghdr *nlh;
  1292. int err;
  1293. u32 *f;
  1294. unsigned lseq;
  1295. nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
  1296. if (nlh == NULL) /* shouldn't really happen ... */
  1297. return -EMSGSIZE;
  1298. f = nlmsg_data(nlh);
  1299. *f = flags;
  1300. xfrm_spd_getinfo(net, &si);
  1301. spc.incnt = si.incnt;
  1302. spc.outcnt = si.outcnt;
  1303. spc.fwdcnt = si.fwdcnt;
  1304. spc.inscnt = si.inscnt;
  1305. spc.outscnt = si.outscnt;
  1306. spc.fwdscnt = si.fwdscnt;
  1307. sph.spdhcnt = si.spdhcnt;
  1308. sph.spdhmcnt = si.spdhmcnt;
  1309. do {
  1310. lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
  1311. spt4.lbits = net->xfrm.policy_hthresh.lbits4;
  1312. spt4.rbits = net->xfrm.policy_hthresh.rbits4;
  1313. spt6.lbits = net->xfrm.policy_hthresh.lbits6;
  1314. spt6.rbits = net->xfrm.policy_hthresh.rbits6;
  1315. } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
  1316. err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
  1317. if (!err)
  1318. err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
  1319. if (!err)
  1320. err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
  1321. if (!err)
  1322. err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
  1323. if (err) {
  1324. nlmsg_cancel(skb, nlh);
  1325. return err;
  1326. }
  1327. nlmsg_end(skb, nlh);
  1328. return 0;
  1329. }
  1330. static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  1331. struct nlattr **attrs,
  1332. struct netlink_ext_ack *extack)
  1333. {
  1334. struct net *net = sock_net(skb->sk);
  1335. struct xfrmu_spdhthresh *thresh4 = NULL;
  1336. struct xfrmu_spdhthresh *thresh6 = NULL;
  1337. /* selector prefixlen thresholds to hash policies */
  1338. if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
  1339. struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
  1340. if (nla_len(rta) < sizeof(*thresh4)) {
  1341. NL_SET_ERR_MSG(extack, "Invalid SPD_IPV4_HTHRESH attribute length");
  1342. return -EINVAL;
  1343. }
  1344. thresh4 = nla_data(rta);
  1345. if (thresh4->lbits > 32 || thresh4->rbits > 32) {
  1346. NL_SET_ERR_MSG(extack, "Invalid hash threshold (must be <= 32 for IPv4)");
  1347. return -EINVAL;
  1348. }
  1349. }
  1350. if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
  1351. struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
  1352. if (nla_len(rta) < sizeof(*thresh6)) {
  1353. NL_SET_ERR_MSG(extack, "Invalid SPD_IPV6_HTHRESH attribute length");
  1354. return -EINVAL;
  1355. }
  1356. thresh6 = nla_data(rta);
  1357. if (thresh6->lbits > 128 || thresh6->rbits > 128) {
  1358. NL_SET_ERR_MSG(extack, "Invalid hash threshold (must be <= 128 for IPv6)");
  1359. return -EINVAL;
  1360. }
  1361. }
  1362. if (thresh4 || thresh6) {
  1363. write_seqlock(&net->xfrm.policy_hthresh.lock);
  1364. if (thresh4) {
  1365. net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
  1366. net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
  1367. }
  1368. if (thresh6) {
  1369. net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
  1370. net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
  1371. }
  1372. write_sequnlock(&net->xfrm.policy_hthresh.lock);
  1373. xfrm_policy_hash_rebuild(net);
  1374. }
  1375. return 0;
  1376. }
  1377. static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  1378. struct nlattr **attrs,
  1379. struct netlink_ext_ack *extack)
  1380. {
  1381. struct net *net = sock_net(skb->sk);
  1382. struct sk_buff *r_skb;
  1383. u32 *flags = nlmsg_data(nlh);
  1384. u32 sportid = NETLINK_CB(skb).portid;
  1385. u32 seq = nlh->nlmsg_seq;
  1386. int err;
  1387. r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
  1388. if (r_skb == NULL)
  1389. return -ENOMEM;
  1390. err = build_spdinfo(r_skb, net, sportid, seq, *flags);
  1391. BUG_ON(err < 0);
  1392. return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
  1393. }
  1394. static inline unsigned int xfrm_sadinfo_msgsize(void)
  1395. {
  1396. return NLMSG_ALIGN(4)
  1397. + nla_total_size(sizeof(struct xfrmu_sadhinfo))
  1398. + nla_total_size(4); /* XFRMA_SAD_CNT */
  1399. }
  1400. static int build_sadinfo(struct sk_buff *skb, struct net *net,
  1401. u32 portid, u32 seq, u32 flags)
  1402. {
  1403. struct xfrmk_sadinfo si;
  1404. struct xfrmu_sadhinfo sh;
  1405. struct nlmsghdr *nlh;
  1406. int err;
  1407. u32 *f;
  1408. nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
  1409. if (nlh == NULL) /* shouldn't really happen ... */
  1410. return -EMSGSIZE;
  1411. f = nlmsg_data(nlh);
  1412. *f = flags;
  1413. xfrm_sad_getinfo(net, &si);
  1414. sh.sadhmcnt = si.sadhmcnt;
  1415. sh.sadhcnt = si.sadhcnt;
  1416. err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
  1417. if (!err)
  1418. err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
  1419. if (err) {
  1420. nlmsg_cancel(skb, nlh);
  1421. return err;
  1422. }
  1423. nlmsg_end(skb, nlh);
  1424. return 0;
  1425. }
  1426. static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  1427. struct nlattr **attrs,
  1428. struct netlink_ext_ack *extack)
  1429. {
  1430. struct net *net = sock_net(skb->sk);
  1431. struct sk_buff *r_skb;
  1432. u32 *flags = nlmsg_data(nlh);
  1433. u32 sportid = NETLINK_CB(skb).portid;
  1434. u32 seq = nlh->nlmsg_seq;
  1435. int err;
  1436. r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
  1437. if (r_skb == NULL)
  1438. return -ENOMEM;
  1439. err = build_sadinfo(r_skb, net, sportid, seq, *flags);
  1440. BUG_ON(err < 0);
  1441. return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
  1442. }
  1443. static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  1444. struct nlattr **attrs, struct netlink_ext_ack *extack)
  1445. {
  1446. struct net *net = sock_net(skb->sk);
  1447. struct xfrm_usersa_id *p = nlmsg_data(nlh);
  1448. struct xfrm_state *x;
  1449. struct sk_buff *resp_skb;
  1450. int err = -ESRCH;
  1451. x = xfrm_user_state_lookup(net, p, attrs, &err);
  1452. if (x == NULL)
  1453. goto out_noput;
  1454. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  1455. if (IS_ERR(resp_skb)) {
  1456. err = PTR_ERR(resp_skb);
  1457. } else {
  1458. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
  1459. }
  1460. xfrm_state_put(x);
  1461. out_noput:
  1462. return err;
  1463. }
  1464. static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
  1465. struct nlattr **attrs,
  1466. struct netlink_ext_ack *extack)
  1467. {
  1468. struct net *net = sock_net(skb->sk);
  1469. struct xfrm_state *x;
  1470. struct xfrm_userspi_info *p;
  1471. struct xfrm_translator *xtr;
  1472. struct sk_buff *resp_skb;
  1473. xfrm_address_t *daddr;
  1474. int family;
  1475. int err;
  1476. u32 mark;
  1477. struct xfrm_mark m;
  1478. u32 if_id = 0;
  1479. u32 pcpu_num = UINT_MAX;
  1480. p = nlmsg_data(nlh);
  1481. err = verify_spi_info(p->info.id.proto, p->min, p->max, extack);
  1482. if (err)
  1483. goto out_noput;
  1484. family = p->info.family;
  1485. daddr = &p->info.id.daddr;
  1486. x = NULL;
  1487. mark = xfrm_mark_get(attrs, &m);
  1488. if (attrs[XFRMA_IF_ID])
  1489. if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
  1490. if (attrs[XFRMA_SA_PCPU]) {
  1491. pcpu_num = nla_get_u32(attrs[XFRMA_SA_PCPU]);
  1492. if (pcpu_num >= num_possible_cpus()) {
  1493. err = -EINVAL;
  1494. goto out_noput;
  1495. }
  1496. }
  1497. if (p->info.seq) {
  1498. x = xfrm_find_acq_byseq(net, mark, p->info.seq, pcpu_num);
  1499. if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
  1500. xfrm_state_put(x);
  1501. x = NULL;
  1502. }
  1503. }
  1504. if (!x)
  1505. x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
  1506. if_id, pcpu_num, p->info.id.proto, daddr,
  1507. &p->info.saddr, 1,
  1508. family);
  1509. err = -ENOENT;
  1510. if (!x) {
  1511. NL_SET_ERR_MSG(extack, "Target ACQUIRE not found");
  1512. goto out_noput;
  1513. }
  1514. err = xfrm_alloc_spi(x, p->min, p->max, extack);
  1515. if (err)
  1516. goto out;
  1517. if (attrs[XFRMA_SA_DIR])
  1518. x->dir = nla_get_u8(attrs[XFRMA_SA_DIR]);
  1519. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  1520. if (IS_ERR(resp_skb)) {
  1521. err = PTR_ERR(resp_skb);
  1522. goto out;
  1523. }
  1524. xtr = xfrm_get_translator();
  1525. if (xtr) {
  1526. err = xtr->alloc_compat(skb, nlmsg_hdr(skb));
  1527. xfrm_put_translator(xtr);
  1528. if (err) {
  1529. kfree_skb(resp_skb);
  1530. goto out;
  1531. }
  1532. }
  1533. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
  1534. out:
  1535. xfrm_state_put(x);
  1536. out_noput:
  1537. return err;
  1538. }
  1539. static int verify_policy_dir(u8 dir, struct netlink_ext_ack *extack)
  1540. {
  1541. switch (dir) {
  1542. case XFRM_POLICY_IN:
  1543. case XFRM_POLICY_OUT:
  1544. case XFRM_POLICY_FWD:
  1545. break;
  1546. default:
  1547. NL_SET_ERR_MSG(extack, "Invalid policy direction");
  1548. return -EINVAL;
  1549. }
  1550. return 0;
  1551. }
  1552. static int verify_policy_type(u8 type, struct netlink_ext_ack *extack)
  1553. {
  1554. switch (type) {
  1555. case XFRM_POLICY_TYPE_MAIN:
  1556. #ifdef CONFIG_XFRM_SUB_POLICY
  1557. case XFRM_POLICY_TYPE_SUB:
  1558. #endif
  1559. break;
  1560. default:
  1561. NL_SET_ERR_MSG(extack, "Invalid policy type");
  1562. return -EINVAL;
  1563. }
  1564. return 0;
  1565. }
  1566. static int verify_newpolicy_info(struct xfrm_userpolicy_info *p,
  1567. struct netlink_ext_ack *extack)
  1568. {
  1569. int ret;
  1570. switch (p->share) {
  1571. case XFRM_SHARE_ANY:
  1572. case XFRM_SHARE_SESSION:
  1573. case XFRM_SHARE_USER:
  1574. case XFRM_SHARE_UNIQUE:
  1575. break;
  1576. default:
  1577. NL_SET_ERR_MSG(extack, "Invalid policy share");
  1578. return -EINVAL;
  1579. }
  1580. switch (p->action) {
  1581. case XFRM_POLICY_ALLOW:
  1582. case XFRM_POLICY_BLOCK:
  1583. break;
  1584. default:
  1585. NL_SET_ERR_MSG(extack, "Invalid policy action");
  1586. return -EINVAL;
  1587. }
  1588. switch (p->sel.family) {
  1589. case AF_INET:
  1590. if (p->sel.prefixlen_d > 32 || p->sel.prefixlen_s > 32) {
  1591. NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 32 for IPv4)");
  1592. return -EINVAL;
  1593. }
  1594. break;
  1595. case AF_INET6:
  1596. #if IS_ENABLED(CONFIG_IPV6)
  1597. if (p->sel.prefixlen_d > 128 || p->sel.prefixlen_s > 128) {
  1598. NL_SET_ERR_MSG(extack, "Invalid prefix length in selector (must be <= 128 for IPv6)");
  1599. return -EINVAL;
  1600. }
  1601. break;
  1602. #else
  1603. NL_SET_ERR_MSG(extack, "IPv6 support disabled");
  1604. return -EAFNOSUPPORT;
  1605. #endif
  1606. default:
  1607. NL_SET_ERR_MSG(extack, "Invalid selector family");
  1608. return -EINVAL;
  1609. }
  1610. ret = verify_policy_dir(p->dir, extack);
  1611. if (ret)
  1612. return ret;
  1613. if (p->index && (xfrm_policy_id2dir(p->index) != p->dir)) {
  1614. NL_SET_ERR_MSG(extack, "Policy index doesn't match direction");
  1615. return -EINVAL;
  1616. }
  1617. return 0;
  1618. }
  1619. static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
  1620. {
  1621. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1622. struct xfrm_user_sec_ctx *uctx;
  1623. if (!rt)
  1624. return 0;
  1625. uctx = nla_data(rt);
  1626. return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
  1627. }
  1628. static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
  1629. int nr)
  1630. {
  1631. int i;
  1632. xp->xfrm_nr = nr;
  1633. for (i = 0; i < nr; i++, ut++) {
  1634. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  1635. memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
  1636. memcpy(&t->saddr, &ut->saddr,
  1637. sizeof(xfrm_address_t));
  1638. t->reqid = ut->reqid;
  1639. t->mode = ut->mode;
  1640. t->share = ut->share;
  1641. t->optional = ut->optional;
  1642. t->aalgos = ut->aalgos;
  1643. t->ealgos = ut->ealgos;
  1644. t->calgos = ut->calgos;
  1645. /* If all masks are ~0, then we allow all algorithms. */
  1646. t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
  1647. t->encap_family = ut->family;
  1648. }
  1649. }
  1650. static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family,
  1651. int dir, struct netlink_ext_ack *extack)
  1652. {
  1653. u16 prev_family;
  1654. int i;
  1655. if (nr > XFRM_MAX_DEPTH) {
  1656. NL_SET_ERR_MSG(extack, "Template count must be <= XFRM_MAX_DEPTH (" __stringify(XFRM_MAX_DEPTH) ")");
  1657. return -EINVAL;
  1658. }
  1659. prev_family = family;
  1660. for (i = 0; i < nr; i++) {
  1661. /* We never validated the ut->family value, so many
  1662. * applications simply leave it at zero. The check was
  1663. * never made and ut->family was ignored because all
  1664. * templates could be assumed to have the same family as
  1665. * the policy itself. Now that we will have ipv4-in-ipv6
  1666. * and ipv6-in-ipv4 tunnels, this is no longer true.
  1667. */
  1668. if (!ut[i].family)
  1669. ut[i].family = family;
  1670. switch (ut[i].mode) {
  1671. case XFRM_MODE_TUNNEL:
  1672. case XFRM_MODE_BEET:
  1673. if (ut[i].optional && dir == XFRM_POLICY_OUT) {
  1674. NL_SET_ERR_MSG(extack, "Mode in optional template not allowed in outbound policy");
  1675. return -EINVAL;
  1676. }
  1677. break;
  1678. default:
  1679. if (ut[i].family != prev_family) {
  1680. NL_SET_ERR_MSG(extack, "Mode in template doesn't support a family change");
  1681. return -EINVAL;
  1682. }
  1683. break;
  1684. }
  1685. if (ut[i].mode >= XFRM_MODE_MAX) {
  1686. NL_SET_ERR_MSG(extack, "Mode in template must be < XFRM_MODE_MAX (" __stringify(XFRM_MODE_MAX) ")");
  1687. return -EINVAL;
  1688. }
  1689. prev_family = ut[i].family;
  1690. switch (ut[i].family) {
  1691. case AF_INET:
  1692. break;
  1693. #if IS_ENABLED(CONFIG_IPV6)
  1694. case AF_INET6:
  1695. break;
  1696. #endif
  1697. default:
  1698. NL_SET_ERR_MSG(extack, "Invalid family in template");
  1699. return -EINVAL;
  1700. }
  1701. if (!xfrm_id_proto_valid(ut[i].id.proto)) {
  1702. NL_SET_ERR_MSG(extack, "Invalid XFRM protocol in template");
  1703. return -EINVAL;
  1704. }
  1705. }
  1706. return 0;
  1707. }
  1708. static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs,
  1709. int dir, struct netlink_ext_ack *extack)
  1710. {
  1711. struct nlattr *rt = attrs[XFRMA_TMPL];
  1712. if (!rt) {
  1713. pol->xfrm_nr = 0;
  1714. } else {
  1715. struct xfrm_user_tmpl *utmpl = nla_data(rt);
  1716. int nr = nla_len(rt) / sizeof(*utmpl);
  1717. int err;
  1718. err = validate_tmpl(nr, utmpl, pol->family, dir, extack);
  1719. if (err)
  1720. return err;
  1721. copy_templates(pol, utmpl, nr);
  1722. }
  1723. return 0;
  1724. }
  1725. static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs,
  1726. struct netlink_ext_ack *extack)
  1727. {
  1728. struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
  1729. struct xfrm_userpolicy_type *upt;
  1730. u8 type = XFRM_POLICY_TYPE_MAIN;
  1731. int err;
  1732. if (rt) {
  1733. upt = nla_data(rt);
  1734. type = upt->type;
  1735. }
  1736. err = verify_policy_type(type, extack);
  1737. if (err)
  1738. return err;
  1739. *tp = type;
  1740. return 0;
  1741. }
  1742. static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
  1743. {
  1744. xp->priority = p->priority;
  1745. xp->index = p->index;
  1746. memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
  1747. memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
  1748. xp->action = p->action;
  1749. xp->flags = p->flags;
  1750. xp->family = p->sel.family;
  1751. /* XXX xp->share = p->share; */
  1752. }
  1753. static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
  1754. {
  1755. memset(p, 0, sizeof(*p));
  1756. memcpy(&p->sel, &xp->selector, sizeof(p->sel));
  1757. memcpy(&p->lft, &xp->lft, sizeof(p->lft));
  1758. memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
  1759. p->priority = xp->priority;
  1760. p->index = xp->index;
  1761. p->sel.family = xp->family;
  1762. p->dir = dir;
  1763. p->action = xp->action;
  1764. p->flags = xp->flags;
  1765. p->share = XFRM_SHARE_ANY; /* XXX xp->share */
  1766. }
  1767. static struct xfrm_policy *xfrm_policy_construct(struct net *net,
  1768. struct xfrm_userpolicy_info *p,
  1769. struct nlattr **attrs,
  1770. int *errp,
  1771. struct netlink_ext_ack *extack)
  1772. {
  1773. struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
  1774. int err;
  1775. if (!xp) {
  1776. *errp = -ENOMEM;
  1777. return NULL;
  1778. }
  1779. copy_from_user_policy(xp, p);
  1780. err = copy_from_user_policy_type(&xp->type, attrs, extack);
  1781. if (err)
  1782. goto error;
  1783. if (!(err = copy_from_user_tmpl(xp, attrs, p->dir, extack)))
  1784. err = copy_from_user_sec_ctx(xp, attrs);
  1785. if (err)
  1786. goto error;
  1787. xfrm_mark_get(attrs, &xp->mark);
  1788. if (attrs[XFRMA_IF_ID])
  1789. xp->if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
  1790. /* configure the hardware if offload is requested */
  1791. if (attrs[XFRMA_OFFLOAD_DEV]) {
  1792. err = xfrm_dev_policy_add(net, xp,
  1793. nla_data(attrs[XFRMA_OFFLOAD_DEV]),
  1794. p->dir, extack);
  1795. if (err)
  1796. goto error;
  1797. }
  1798. return xp;
  1799. error:
  1800. *errp = err;
  1801. xp->walk.dead = 1;
  1802. xfrm_policy_destroy(xp);
  1803. return NULL;
  1804. }
  1805. static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1806. struct nlattr **attrs,
  1807. struct netlink_ext_ack *extack)
  1808. {
  1809. struct net *net = sock_net(skb->sk);
  1810. struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
  1811. struct xfrm_policy *xp;
  1812. struct km_event c;
  1813. int err;
  1814. int excl;
  1815. err = verify_newpolicy_info(p, extack);
  1816. if (err)
  1817. return err;
  1818. err = verify_sec_ctx_len(attrs, extack);
  1819. if (err)
  1820. return err;
  1821. xp = xfrm_policy_construct(net, p, attrs, &err, extack);
  1822. if (!xp)
  1823. return err;
  1824. /* shouldn't excl be based on nlh flags??
  1825. * Aha! this is anti-netlink really i.e more pfkey derived
  1826. * in netlink excl is a flag and you wouldn't need
  1827. * a type XFRM_MSG_UPDPOLICY - JHS */
  1828. excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
  1829. err = xfrm_policy_insert(p->dir, xp, excl);
  1830. xfrm_audit_policy_add(xp, err ? 0 : 1, true);
  1831. if (err) {
  1832. xfrm_dev_policy_delete(xp);
  1833. xfrm_dev_policy_free(xp);
  1834. security_xfrm_policy_free(xp->security);
  1835. kfree(xp);
  1836. return err;
  1837. }
  1838. c.event = nlh->nlmsg_type;
  1839. c.seq = nlh->nlmsg_seq;
  1840. c.portid = nlh->nlmsg_pid;
  1841. km_policy_notify(xp, p->dir, &c);
  1842. xfrm_pol_put(xp);
  1843. return 0;
  1844. }
  1845. static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
  1846. {
  1847. struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
  1848. int i;
  1849. if (xp->xfrm_nr == 0)
  1850. return 0;
  1851. if (xp->xfrm_nr > XFRM_MAX_DEPTH)
  1852. return -ENOBUFS;
  1853. for (i = 0; i < xp->xfrm_nr; i++) {
  1854. struct xfrm_user_tmpl *up = &vec[i];
  1855. struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
  1856. memset(up, 0, sizeof(*up));
  1857. memcpy(&up->id, &kp->id, sizeof(up->id));
  1858. up->family = kp->encap_family;
  1859. memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
  1860. up->reqid = kp->reqid;
  1861. up->mode = kp->mode;
  1862. up->share = kp->share;
  1863. up->optional = kp->optional;
  1864. up->aalgos = kp->aalgos;
  1865. up->ealgos = kp->ealgos;
  1866. up->calgos = kp->calgos;
  1867. }
  1868. return nla_put(skb, XFRMA_TMPL,
  1869. sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
  1870. }
  1871. static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
  1872. {
  1873. if (x->security) {
  1874. return copy_sec_ctx(x->security, skb);
  1875. }
  1876. return 0;
  1877. }
  1878. static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
  1879. {
  1880. if (xp->security)
  1881. return copy_sec_ctx(xp->security, skb);
  1882. return 0;
  1883. }
  1884. static inline unsigned int userpolicy_type_attrsize(void)
  1885. {
  1886. #ifdef CONFIG_XFRM_SUB_POLICY
  1887. return nla_total_size(sizeof(struct xfrm_userpolicy_type));
  1888. #else
  1889. return 0;
  1890. #endif
  1891. }
  1892. #ifdef CONFIG_XFRM_SUB_POLICY
  1893. static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1894. {
  1895. struct xfrm_userpolicy_type upt;
  1896. /* Sadly there are two holes in struct xfrm_userpolicy_type */
  1897. memset(&upt, 0, sizeof(upt));
  1898. upt.type = type;
  1899. return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
  1900. }
  1901. #else
  1902. static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1903. {
  1904. return 0;
  1905. }
  1906. #endif
  1907. static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
  1908. {
  1909. struct xfrm_dump_info *sp = ptr;
  1910. struct xfrm_userpolicy_info *p;
  1911. struct sk_buff *in_skb = sp->in_skb;
  1912. struct sk_buff *skb = sp->out_skb;
  1913. struct xfrm_translator *xtr;
  1914. struct nlmsghdr *nlh;
  1915. int err;
  1916. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
  1917. XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
  1918. if (nlh == NULL)
  1919. return -EMSGSIZE;
  1920. p = nlmsg_data(nlh);
  1921. copy_to_user_policy(xp, p, dir);
  1922. err = copy_to_user_tmpl(xp, skb);
  1923. if (!err)
  1924. err = copy_to_user_sec_ctx(xp, skb);
  1925. if (!err)
  1926. err = copy_to_user_policy_type(xp->type, skb);
  1927. if (!err)
  1928. err = xfrm_mark_put(skb, &xp->mark);
  1929. if (!err)
  1930. err = xfrm_if_id_put(skb, xp->if_id);
  1931. if (!err && xp->xdo.dev)
  1932. err = copy_user_offload(&xp->xdo, skb);
  1933. if (err) {
  1934. nlmsg_cancel(skb, nlh);
  1935. return err;
  1936. }
  1937. nlmsg_end(skb, nlh);
  1938. xtr = xfrm_get_translator();
  1939. if (xtr) {
  1940. err = xtr->alloc_compat(skb, nlh);
  1941. xfrm_put_translator(xtr);
  1942. if (err) {
  1943. nlmsg_cancel(skb, nlh);
  1944. return err;
  1945. }
  1946. }
  1947. return 0;
  1948. }
  1949. static int xfrm_dump_policy_done(struct netlink_callback *cb)
  1950. {
  1951. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
  1952. struct net *net = sock_net(cb->skb->sk);
  1953. xfrm_policy_walk_done(walk, net);
  1954. return 0;
  1955. }
  1956. static int xfrm_dump_policy_start(struct netlink_callback *cb)
  1957. {
  1958. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
  1959. BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
  1960. xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
  1961. return 0;
  1962. }
  1963. static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
  1964. {
  1965. struct net *net = sock_net(skb->sk);
  1966. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
  1967. struct xfrm_dump_info info;
  1968. info.in_skb = cb->skb;
  1969. info.out_skb = skb;
  1970. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  1971. info.nlmsg_flags = NLM_F_MULTI;
  1972. (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
  1973. return skb->len;
  1974. }
  1975. static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
  1976. struct xfrm_policy *xp,
  1977. int dir, u32 seq)
  1978. {
  1979. struct xfrm_dump_info info;
  1980. struct sk_buff *skb;
  1981. int err;
  1982. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1983. if (!skb)
  1984. return ERR_PTR(-ENOMEM);
  1985. info.in_skb = in_skb;
  1986. info.out_skb = skb;
  1987. info.nlmsg_seq = seq;
  1988. info.nlmsg_flags = 0;
  1989. err = dump_one_policy(xp, dir, 0, &info);
  1990. if (err) {
  1991. kfree_skb(skb);
  1992. return ERR_PTR(err);
  1993. }
  1994. return skb;
  1995. }
  1996. static int xfrm_notify_userpolicy(struct net *net)
  1997. {
  1998. struct xfrm_userpolicy_default *up;
  1999. int len = NLMSG_ALIGN(sizeof(*up));
  2000. struct nlmsghdr *nlh;
  2001. struct sk_buff *skb;
  2002. int err;
  2003. skb = nlmsg_new(len, GFP_ATOMIC);
  2004. if (skb == NULL)
  2005. return -ENOMEM;
  2006. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_GETDEFAULT, sizeof(*up), 0);
  2007. if (nlh == NULL) {
  2008. kfree_skb(skb);
  2009. return -EMSGSIZE;
  2010. }
  2011. up = nlmsg_data(nlh);
  2012. up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
  2013. up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
  2014. up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
  2015. nlmsg_end(skb, nlh);
  2016. rcu_read_lock();
  2017. err = xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
  2018. rcu_read_unlock();
  2019. return err;
  2020. }
  2021. static bool xfrm_userpolicy_is_valid(__u8 policy)
  2022. {
  2023. return policy == XFRM_USERPOLICY_BLOCK ||
  2024. policy == XFRM_USERPOLICY_ACCEPT;
  2025. }
  2026. static int xfrm_set_default(struct sk_buff *skb, struct nlmsghdr *nlh,
  2027. struct nlattr **attrs, struct netlink_ext_ack *extack)
  2028. {
  2029. struct net *net = sock_net(skb->sk);
  2030. struct xfrm_userpolicy_default *up = nlmsg_data(nlh);
  2031. if (xfrm_userpolicy_is_valid(up->in))
  2032. net->xfrm.policy_default[XFRM_POLICY_IN] = up->in;
  2033. if (xfrm_userpolicy_is_valid(up->fwd))
  2034. net->xfrm.policy_default[XFRM_POLICY_FWD] = up->fwd;
  2035. if (xfrm_userpolicy_is_valid(up->out))
  2036. net->xfrm.policy_default[XFRM_POLICY_OUT] = up->out;
  2037. rt_genid_bump_all(net);
  2038. xfrm_notify_userpolicy(net);
  2039. return 0;
  2040. }
  2041. static int xfrm_get_default(struct sk_buff *skb, struct nlmsghdr *nlh,
  2042. struct nlattr **attrs, struct netlink_ext_ack *extack)
  2043. {
  2044. struct sk_buff *r_skb;
  2045. struct nlmsghdr *r_nlh;
  2046. struct net *net = sock_net(skb->sk);
  2047. struct xfrm_userpolicy_default *r_up;
  2048. int len = NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_default));
  2049. u32 portid = NETLINK_CB(skb).portid;
  2050. u32 seq = nlh->nlmsg_seq;
  2051. r_skb = nlmsg_new(len, GFP_ATOMIC);
  2052. if (!r_skb)
  2053. return -ENOMEM;
  2054. r_nlh = nlmsg_put(r_skb, portid, seq, XFRM_MSG_GETDEFAULT, sizeof(*r_up), 0);
  2055. if (!r_nlh) {
  2056. kfree_skb(r_skb);
  2057. return -EMSGSIZE;
  2058. }
  2059. r_up = nlmsg_data(r_nlh);
  2060. r_up->in = net->xfrm.policy_default[XFRM_POLICY_IN];
  2061. r_up->fwd = net->xfrm.policy_default[XFRM_POLICY_FWD];
  2062. r_up->out = net->xfrm.policy_default[XFRM_POLICY_OUT];
  2063. nlmsg_end(r_skb, r_nlh);
  2064. return nlmsg_unicast(net->xfrm.nlsk, r_skb, portid);
  2065. }
  2066. static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  2067. struct nlattr **attrs,
  2068. struct netlink_ext_ack *extack)
  2069. {
  2070. struct net *net = sock_net(skb->sk);
  2071. struct xfrm_policy *xp;
  2072. struct xfrm_userpolicy_id *p;
  2073. u8 type = XFRM_POLICY_TYPE_MAIN;
  2074. int err;
  2075. struct km_event c;
  2076. int delete;
  2077. struct xfrm_mark m;
  2078. u32 if_id = 0;
  2079. p = nlmsg_data(nlh);
  2080. delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
  2081. err = copy_from_user_policy_type(&type, attrs, extack);
  2082. if (err)
  2083. return err;
  2084. err = verify_policy_dir(p->dir, extack);
  2085. if (err)
  2086. return err;
  2087. if (attrs[XFRMA_IF_ID])
  2088. if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
  2089. xfrm_mark_get(attrs, &m);
  2090. if (p->index)
  2091. xp = xfrm_policy_byid(net, &m, if_id, type, p->dir,
  2092. p->index, delete, &err);
  2093. else {
  2094. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  2095. struct xfrm_sec_ctx *ctx;
  2096. err = verify_sec_ctx_len(attrs, extack);
  2097. if (err)
  2098. return err;
  2099. ctx = NULL;
  2100. if (rt) {
  2101. struct xfrm_user_sec_ctx *uctx = nla_data(rt);
  2102. err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
  2103. if (err)
  2104. return err;
  2105. }
  2106. xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
  2107. &p->sel, ctx, delete, &err);
  2108. security_xfrm_policy_free(ctx);
  2109. }
  2110. if (xp == NULL)
  2111. return -ENOENT;
  2112. if (!delete) {
  2113. struct sk_buff *resp_skb;
  2114. resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
  2115. if (IS_ERR(resp_skb)) {
  2116. err = PTR_ERR(resp_skb);
  2117. } else {
  2118. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
  2119. NETLINK_CB(skb).portid);
  2120. }
  2121. } else {
  2122. xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
  2123. if (err != 0)
  2124. goto out;
  2125. c.data.byid = p->index;
  2126. c.event = nlh->nlmsg_type;
  2127. c.seq = nlh->nlmsg_seq;
  2128. c.portid = nlh->nlmsg_pid;
  2129. km_policy_notify(xp, p->dir, &c);
  2130. }
  2131. out:
  2132. xfrm_pol_put(xp);
  2133. return err;
  2134. }
  2135. static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  2136. struct nlattr **attrs,
  2137. struct netlink_ext_ack *extack)
  2138. {
  2139. struct net *net = sock_net(skb->sk);
  2140. struct km_event c;
  2141. struct xfrm_usersa_flush *p = nlmsg_data(nlh);
  2142. int err;
  2143. err = xfrm_state_flush(net, p->proto, true, false);
  2144. if (err) {
  2145. if (err == -ESRCH) /* empty table */
  2146. return 0;
  2147. return err;
  2148. }
  2149. c.data.proto = p->proto;
  2150. c.event = nlh->nlmsg_type;
  2151. c.seq = nlh->nlmsg_seq;
  2152. c.portid = nlh->nlmsg_pid;
  2153. c.net = net;
  2154. km_state_notify(NULL, &c);
  2155. return 0;
  2156. }
  2157. static inline unsigned int xfrm_aevent_msgsize(struct xfrm_state *x)
  2158. {
  2159. unsigned int replay_size = x->replay_esn ?
  2160. xfrm_replay_state_esn_len(x->replay_esn) :
  2161. sizeof(struct xfrm_replay_state);
  2162. return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
  2163. + nla_total_size(replay_size)
  2164. + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
  2165. + nla_total_size(sizeof(struct xfrm_mark))
  2166. + nla_total_size(4) /* XFRM_AE_RTHR */
  2167. + nla_total_size(4) /* XFRM_AE_ETHR */
  2168. + nla_total_size(sizeof(x->dir)) /* XFRMA_SA_DIR */
  2169. + nla_total_size(4); /* XFRMA_SA_PCPU */
  2170. }
  2171. static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
  2172. {
  2173. struct xfrm_aevent_id *id;
  2174. struct nlmsghdr *nlh;
  2175. int err;
  2176. nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
  2177. if (nlh == NULL)
  2178. return -EMSGSIZE;
  2179. id = nlmsg_data(nlh);
  2180. memset(&id->sa_id, 0, sizeof(id->sa_id));
  2181. memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
  2182. id->sa_id.spi = x->id.spi;
  2183. id->sa_id.family = x->props.family;
  2184. id->sa_id.proto = x->id.proto;
  2185. memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
  2186. id->reqid = x->props.reqid;
  2187. id->flags = c->data.aevent;
  2188. if (x->replay_esn) {
  2189. err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
  2190. xfrm_replay_state_esn_len(x->replay_esn),
  2191. x->replay_esn);
  2192. } else {
  2193. err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
  2194. &x->replay);
  2195. }
  2196. if (err)
  2197. goto out_cancel;
  2198. err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
  2199. XFRMA_PAD);
  2200. if (err)
  2201. goto out_cancel;
  2202. if (id->flags & XFRM_AE_RTHR) {
  2203. err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
  2204. if (err)
  2205. goto out_cancel;
  2206. }
  2207. if (id->flags & XFRM_AE_ETHR) {
  2208. err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
  2209. x->replay_maxage * 10 / HZ);
  2210. if (err)
  2211. goto out_cancel;
  2212. }
  2213. err = xfrm_mark_put(skb, &x->mark);
  2214. if (err)
  2215. goto out_cancel;
  2216. err = xfrm_if_id_put(skb, x->if_id);
  2217. if (err)
  2218. goto out_cancel;
  2219. if (x->pcpu_num != UINT_MAX) {
  2220. err = nla_put_u32(skb, XFRMA_SA_PCPU, x->pcpu_num);
  2221. if (err)
  2222. goto out_cancel;
  2223. }
  2224. if (x->dir) {
  2225. err = nla_put_u8(skb, XFRMA_SA_DIR, x->dir);
  2226. if (err)
  2227. goto out_cancel;
  2228. }
  2229. nlmsg_end(skb, nlh);
  2230. return 0;
  2231. out_cancel:
  2232. nlmsg_cancel(skb, nlh);
  2233. return err;
  2234. }
  2235. static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  2236. struct nlattr **attrs, struct netlink_ext_ack *extack)
  2237. {
  2238. struct net *net = sock_net(skb->sk);
  2239. struct xfrm_state *x;
  2240. struct sk_buff *r_skb;
  2241. int err;
  2242. struct km_event c;
  2243. u32 mark;
  2244. struct xfrm_mark m;
  2245. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  2246. struct xfrm_usersa_id *id = &p->sa_id;
  2247. mark = xfrm_mark_get(attrs, &m);
  2248. x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
  2249. if (x == NULL)
  2250. return -ESRCH;
  2251. r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
  2252. if (r_skb == NULL) {
  2253. xfrm_state_put(x);
  2254. return -ENOMEM;
  2255. }
  2256. /*
  2257. * XXX: is this lock really needed - none of the other
  2258. * gets lock (the concern is things getting updated
  2259. * while we are still reading) - jhs
  2260. */
  2261. spin_lock_bh(&x->lock);
  2262. c.data.aevent = p->flags;
  2263. c.seq = nlh->nlmsg_seq;
  2264. c.portid = nlh->nlmsg_pid;
  2265. err = build_aevent(r_skb, x, &c);
  2266. BUG_ON(err < 0);
  2267. err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
  2268. spin_unlock_bh(&x->lock);
  2269. xfrm_state_put(x);
  2270. return err;
  2271. }
  2272. static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  2273. struct nlattr **attrs, struct netlink_ext_ack *extack)
  2274. {
  2275. struct net *net = sock_net(skb->sk);
  2276. struct xfrm_state *x;
  2277. struct km_event c;
  2278. int err = -EINVAL;
  2279. u32 mark = 0;
  2280. struct xfrm_mark m;
  2281. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  2282. struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
  2283. struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
  2284. struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
  2285. struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
  2286. struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
  2287. if (!lt && !rp && !re && !et && !rt) {
  2288. NL_SET_ERR_MSG(extack, "Missing required attribute for AE");
  2289. return err;
  2290. }
  2291. /* pedantic mode - thou shalt sayeth replaceth */
  2292. if (!(nlh->nlmsg_flags & NLM_F_REPLACE)) {
  2293. NL_SET_ERR_MSG(extack, "NLM_F_REPLACE flag is required");
  2294. return err;
  2295. }
  2296. mark = xfrm_mark_get(attrs, &m);
  2297. x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
  2298. if (x == NULL)
  2299. return -ESRCH;
  2300. if (x->km.state != XFRM_STATE_VALID) {
  2301. NL_SET_ERR_MSG(extack, "SA must be in VALID state");
  2302. goto out;
  2303. }
  2304. err = xfrm_replay_verify_len(x->replay_esn, re, extack);
  2305. if (err)
  2306. goto out;
  2307. spin_lock_bh(&x->lock);
  2308. xfrm_update_ae_params(x, attrs, 1);
  2309. spin_unlock_bh(&x->lock);
  2310. c.event = nlh->nlmsg_type;
  2311. c.seq = nlh->nlmsg_seq;
  2312. c.portid = nlh->nlmsg_pid;
  2313. c.data.aevent = XFRM_AE_CU;
  2314. km_state_notify(x, &c);
  2315. err = 0;
  2316. out:
  2317. xfrm_state_put(x);
  2318. return err;
  2319. }
  2320. static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  2321. struct nlattr **attrs,
  2322. struct netlink_ext_ack *extack)
  2323. {
  2324. struct net *net = sock_net(skb->sk);
  2325. struct km_event c;
  2326. u8 type = XFRM_POLICY_TYPE_MAIN;
  2327. int err;
  2328. err = copy_from_user_policy_type(&type, attrs, extack);
  2329. if (err)
  2330. return err;
  2331. err = xfrm_policy_flush(net, type, true);
  2332. if (err) {
  2333. if (err == -ESRCH) /* empty table */
  2334. return 0;
  2335. return err;
  2336. }
  2337. c.data.type = type;
  2338. c.event = nlh->nlmsg_type;
  2339. c.seq = nlh->nlmsg_seq;
  2340. c.portid = nlh->nlmsg_pid;
  2341. c.net = net;
  2342. km_policy_notify(NULL, 0, &c);
  2343. return 0;
  2344. }
  2345. static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  2346. struct nlattr **attrs,
  2347. struct netlink_ext_ack *extack)
  2348. {
  2349. struct net *net = sock_net(skb->sk);
  2350. struct xfrm_policy *xp;
  2351. struct xfrm_user_polexpire *up = nlmsg_data(nlh);
  2352. struct xfrm_userpolicy_info *p = &up->pol;
  2353. u8 type = XFRM_POLICY_TYPE_MAIN;
  2354. int err = -ENOENT;
  2355. struct xfrm_mark m;
  2356. u32 if_id = 0;
  2357. err = copy_from_user_policy_type(&type, attrs, extack);
  2358. if (err)
  2359. return err;
  2360. err = verify_policy_dir(p->dir, extack);
  2361. if (err)
  2362. return err;
  2363. if (attrs[XFRMA_IF_ID])
  2364. if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
  2365. xfrm_mark_get(attrs, &m);
  2366. if (p->index)
  2367. xp = xfrm_policy_byid(net, &m, if_id, type, p->dir, p->index,
  2368. 0, &err);
  2369. else {
  2370. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  2371. struct xfrm_sec_ctx *ctx;
  2372. err = verify_sec_ctx_len(attrs, extack);
  2373. if (err)
  2374. return err;
  2375. ctx = NULL;
  2376. if (rt) {
  2377. struct xfrm_user_sec_ctx *uctx = nla_data(rt);
  2378. err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
  2379. if (err)
  2380. return err;
  2381. }
  2382. xp = xfrm_policy_bysel_ctx(net, &m, if_id, type, p->dir,
  2383. &p->sel, ctx, 0, &err);
  2384. security_xfrm_policy_free(ctx);
  2385. }
  2386. if (xp == NULL)
  2387. return -ENOENT;
  2388. if (unlikely(xp->walk.dead))
  2389. goto out;
  2390. err = 0;
  2391. if (up->hard) {
  2392. xfrm_policy_delete(xp, p->dir);
  2393. xfrm_audit_policy_delete(xp, 1, true);
  2394. }
  2395. km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
  2396. out:
  2397. xfrm_pol_put(xp);
  2398. return err;
  2399. }
  2400. static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  2401. struct nlattr **attrs,
  2402. struct netlink_ext_ack *extack)
  2403. {
  2404. struct net *net = sock_net(skb->sk);
  2405. struct xfrm_state *x;
  2406. int err;
  2407. struct xfrm_user_expire *ue = nlmsg_data(nlh);
  2408. struct xfrm_usersa_info *p = &ue->state;
  2409. struct xfrm_mark m;
  2410. u32 mark = xfrm_mark_get(attrs, &m);
  2411. x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
  2412. err = -ENOENT;
  2413. if (x == NULL)
  2414. return err;
  2415. spin_lock_bh(&x->lock);
  2416. err = -EINVAL;
  2417. if (x->km.state != XFRM_STATE_VALID) {
  2418. NL_SET_ERR_MSG(extack, "SA must be in VALID state");
  2419. goto out;
  2420. }
  2421. km_state_expired(x, ue->hard, nlh->nlmsg_pid);
  2422. if (ue->hard) {
  2423. __xfrm_state_delete(x);
  2424. xfrm_audit_state_delete(x, 1, true);
  2425. }
  2426. err = 0;
  2427. out:
  2428. spin_unlock_bh(&x->lock);
  2429. xfrm_state_put(x);
  2430. return err;
  2431. }
  2432. static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
  2433. struct nlattr **attrs,
  2434. struct netlink_ext_ack *extack)
  2435. {
  2436. struct net *net = sock_net(skb->sk);
  2437. struct xfrm_policy *xp;
  2438. struct xfrm_user_tmpl *ut;
  2439. int i;
  2440. struct nlattr *rt = attrs[XFRMA_TMPL];
  2441. struct xfrm_mark mark;
  2442. struct xfrm_user_acquire *ua = nlmsg_data(nlh);
  2443. struct xfrm_state *x = xfrm_state_alloc(net);
  2444. int err = -ENOMEM;
  2445. if (!x)
  2446. goto nomem;
  2447. xfrm_mark_get(attrs, &mark);
  2448. if (attrs[XFRMA_SA_PCPU]) {
  2449. x->pcpu_num = nla_get_u32(attrs[XFRMA_SA_PCPU]);
  2450. err = -EINVAL;
  2451. if (x->pcpu_num >= num_possible_cpus())
  2452. goto free_state;
  2453. }
  2454. err = verify_newpolicy_info(&ua->policy, extack);
  2455. if (err)
  2456. goto free_state;
  2457. err = verify_sec_ctx_len(attrs, extack);
  2458. if (err)
  2459. goto free_state;
  2460. /* build an XP */
  2461. xp = xfrm_policy_construct(net, &ua->policy, attrs, &err, extack);
  2462. if (!xp)
  2463. goto free_state;
  2464. memcpy(&x->id, &ua->id, sizeof(ua->id));
  2465. memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
  2466. memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
  2467. xp->mark.m = x->mark.m = mark.m;
  2468. xp->mark.v = x->mark.v = mark.v;
  2469. ut = nla_data(rt);
  2470. /* extract the templates and for each call km_key */
  2471. for (i = 0; i < xp->xfrm_nr; i++, ut++) {
  2472. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  2473. memcpy(&x->id, &t->id, sizeof(x->id));
  2474. x->props.mode = t->mode;
  2475. x->props.reqid = t->reqid;
  2476. x->props.family = ut->family;
  2477. t->aalgos = ua->aalgos;
  2478. t->ealgos = ua->ealgos;
  2479. t->calgos = ua->calgos;
  2480. err = km_query(x, t, xp);
  2481. }
  2482. xfrm_state_free(x);
  2483. kfree(xp);
  2484. return 0;
  2485. free_state:
  2486. xfrm_state_free(x);
  2487. nomem:
  2488. return err;
  2489. }
  2490. #ifdef CONFIG_XFRM_MIGRATE
  2491. static int copy_from_user_migrate(struct xfrm_migrate *ma,
  2492. struct xfrm_kmaddress *k,
  2493. struct nlattr **attrs, int *num,
  2494. struct netlink_ext_ack *extack)
  2495. {
  2496. struct nlattr *rt = attrs[XFRMA_MIGRATE];
  2497. struct xfrm_user_migrate *um;
  2498. int i, num_migrate;
  2499. if (k != NULL) {
  2500. struct xfrm_user_kmaddress *uk;
  2501. uk = nla_data(attrs[XFRMA_KMADDRESS]);
  2502. memcpy(&k->local, &uk->local, sizeof(k->local));
  2503. memcpy(&k->remote, &uk->remote, sizeof(k->remote));
  2504. k->family = uk->family;
  2505. k->reserved = uk->reserved;
  2506. }
  2507. um = nla_data(rt);
  2508. num_migrate = nla_len(rt) / sizeof(*um);
  2509. if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH) {
  2510. NL_SET_ERR_MSG(extack, "Invalid number of SAs to migrate, must be 0 < num <= XFRM_MAX_DEPTH (6)");
  2511. return -EINVAL;
  2512. }
  2513. for (i = 0; i < num_migrate; i++, um++, ma++) {
  2514. memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
  2515. memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
  2516. memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
  2517. memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
  2518. ma->proto = um->proto;
  2519. ma->mode = um->mode;
  2520. ma->reqid = um->reqid;
  2521. ma->old_family = um->old_family;
  2522. ma->new_family = um->new_family;
  2523. }
  2524. *num = i;
  2525. return 0;
  2526. }
  2527. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  2528. struct nlattr **attrs, struct netlink_ext_ack *extack)
  2529. {
  2530. struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
  2531. struct xfrm_migrate m[XFRM_MAX_DEPTH];
  2532. struct xfrm_kmaddress km, *kmp;
  2533. u8 type;
  2534. int err;
  2535. int n = 0;
  2536. struct net *net = sock_net(skb->sk);
  2537. struct xfrm_encap_tmpl *encap = NULL;
  2538. u32 if_id = 0;
  2539. if (!attrs[XFRMA_MIGRATE]) {
  2540. NL_SET_ERR_MSG(extack, "Missing required MIGRATE attribute");
  2541. return -EINVAL;
  2542. }
  2543. kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
  2544. err = copy_from_user_policy_type(&type, attrs, extack);
  2545. if (err)
  2546. return err;
  2547. err = copy_from_user_migrate(m, kmp, attrs, &n, extack);
  2548. if (err)
  2549. return err;
  2550. if (!n)
  2551. return 0;
  2552. if (attrs[XFRMA_ENCAP]) {
  2553. encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
  2554. sizeof(*encap), GFP_KERNEL);
  2555. if (!encap)
  2556. return -ENOMEM;
  2557. }
  2558. if (attrs[XFRMA_IF_ID])
  2559. if_id = nla_get_u32(attrs[XFRMA_IF_ID]);
  2560. err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap,
  2561. if_id, extack);
  2562. kfree(encap);
  2563. return err;
  2564. }
  2565. #else
  2566. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  2567. struct nlattr **attrs, struct netlink_ext_ack *extack)
  2568. {
  2569. return -ENOPROTOOPT;
  2570. }
  2571. #endif
  2572. #ifdef CONFIG_XFRM_MIGRATE
  2573. static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
  2574. {
  2575. struct xfrm_user_migrate um;
  2576. memset(&um, 0, sizeof(um));
  2577. um.proto = m->proto;
  2578. um.mode = m->mode;
  2579. um.reqid = m->reqid;
  2580. um.old_family = m->old_family;
  2581. memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
  2582. memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
  2583. um.new_family = m->new_family;
  2584. memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
  2585. memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
  2586. return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
  2587. }
  2588. static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
  2589. {
  2590. struct xfrm_user_kmaddress uk;
  2591. memset(&uk, 0, sizeof(uk));
  2592. uk.family = k->family;
  2593. uk.reserved = k->reserved;
  2594. memcpy(&uk.local, &k->local, sizeof(uk.local));
  2595. memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
  2596. return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
  2597. }
  2598. static inline unsigned int xfrm_migrate_msgsize(int num_migrate, int with_kma,
  2599. int with_encp)
  2600. {
  2601. return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
  2602. + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
  2603. + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
  2604. + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
  2605. + userpolicy_type_attrsize();
  2606. }
  2607. static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
  2608. int num_migrate, const struct xfrm_kmaddress *k,
  2609. const struct xfrm_selector *sel,
  2610. const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
  2611. {
  2612. const struct xfrm_migrate *mp;
  2613. struct xfrm_userpolicy_id *pol_id;
  2614. struct nlmsghdr *nlh;
  2615. int i, err;
  2616. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
  2617. if (nlh == NULL)
  2618. return -EMSGSIZE;
  2619. pol_id = nlmsg_data(nlh);
  2620. /* copy data from selector, dir, and type to the pol_id */
  2621. memset(pol_id, 0, sizeof(*pol_id));
  2622. memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
  2623. pol_id->dir = dir;
  2624. if (k != NULL) {
  2625. err = copy_to_user_kmaddress(k, skb);
  2626. if (err)
  2627. goto out_cancel;
  2628. }
  2629. if (encap) {
  2630. err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
  2631. if (err)
  2632. goto out_cancel;
  2633. }
  2634. err = copy_to_user_policy_type(type, skb);
  2635. if (err)
  2636. goto out_cancel;
  2637. for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
  2638. err = copy_to_user_migrate(mp, skb);
  2639. if (err)
  2640. goto out_cancel;
  2641. }
  2642. nlmsg_end(skb, nlh);
  2643. return 0;
  2644. out_cancel:
  2645. nlmsg_cancel(skb, nlh);
  2646. return err;
  2647. }
  2648. static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  2649. const struct xfrm_migrate *m, int num_migrate,
  2650. const struct xfrm_kmaddress *k,
  2651. const struct xfrm_encap_tmpl *encap)
  2652. {
  2653. struct net *net = &init_net;
  2654. struct sk_buff *skb;
  2655. int err;
  2656. skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
  2657. GFP_ATOMIC);
  2658. if (skb == NULL)
  2659. return -ENOMEM;
  2660. /* build migrate */
  2661. err = build_migrate(skb, m, num_migrate, k, sel, encap, dir, type);
  2662. BUG_ON(err < 0);
  2663. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
  2664. }
  2665. #else
  2666. static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  2667. const struct xfrm_migrate *m, int num_migrate,
  2668. const struct xfrm_kmaddress *k,
  2669. const struct xfrm_encap_tmpl *encap)
  2670. {
  2671. return -ENOPROTOOPT;
  2672. }
  2673. #endif
  2674. #define XMSGSIZE(type) sizeof(struct type)
  2675. const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
  2676. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  2677. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  2678. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  2679. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  2680. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  2681. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  2682. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
  2683. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
  2684. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
  2685. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  2686. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  2687. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
  2688. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  2689. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
  2690. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  2691. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  2692. [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
  2693. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  2694. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
  2695. [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
  2696. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
  2697. [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
  2698. [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_default),
  2699. };
  2700. EXPORT_SYMBOL_GPL(xfrm_msg_min);
  2701. #undef XMSGSIZE
  2702. const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
  2703. [XFRMA_UNSPEC] = { .strict_start_type = XFRMA_SA_DIR },
  2704. [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
  2705. [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
  2706. [XFRMA_LASTUSED] = { .type = NLA_U64},
  2707. [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
  2708. [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
  2709. [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
  2710. [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
  2711. [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
  2712. [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
  2713. [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
  2714. [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_user_sec_ctx) },
  2715. [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
  2716. [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
  2717. [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
  2718. [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
  2719. [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
  2720. [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
  2721. [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
  2722. [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
  2723. [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
  2724. [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
  2725. [XFRMA_TFCPAD] = { .type = NLA_U32 },
  2726. [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
  2727. [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
  2728. [XFRMA_PROTO] = { .type = NLA_U8 },
  2729. [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
  2730. [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
  2731. [XFRMA_SET_MARK] = { .type = NLA_U32 },
  2732. [XFRMA_SET_MARK_MASK] = { .type = NLA_U32 },
  2733. [XFRMA_IF_ID] = { .type = NLA_U32 },
  2734. [XFRMA_MTIMER_THRESH] = { .type = NLA_U32 },
  2735. [XFRMA_SA_DIR] = NLA_POLICY_RANGE(NLA_U8, XFRM_SA_DIR_IN, XFRM_SA_DIR_OUT),
  2736. [XFRMA_NAT_KEEPALIVE_INTERVAL] = { .type = NLA_U32 },
  2737. [XFRMA_SA_PCPU] = { .type = NLA_U32 },
  2738. };
  2739. EXPORT_SYMBOL_GPL(xfrma_policy);
  2740. static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
  2741. [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
  2742. [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
  2743. };
  2744. static const struct xfrm_link {
  2745. int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **,
  2746. struct netlink_ext_ack *);
  2747. int (*start)(struct netlink_callback *);
  2748. int (*dump)(struct sk_buff *, struct netlink_callback *);
  2749. int (*done)(struct netlink_callback *);
  2750. const struct nla_policy *nla_pol;
  2751. int nla_max;
  2752. } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
  2753. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  2754. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
  2755. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
  2756. .dump = xfrm_dump_sa,
  2757. .done = xfrm_dump_sa_done },
  2758. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  2759. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
  2760. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
  2761. .start = xfrm_dump_policy_start,
  2762. .dump = xfrm_dump_policy,
  2763. .done = xfrm_dump_policy_done },
  2764. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
  2765. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
  2766. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
  2767. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  2768. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  2769. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
  2770. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
  2771. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
  2772. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
  2773. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
  2774. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
  2775. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
  2776. [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
  2777. .nla_pol = xfrma_spd_policy,
  2778. .nla_max = XFRMA_SPD_MAX },
  2779. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
  2780. [XFRM_MSG_SETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_set_default },
  2781. [XFRM_MSG_GETDEFAULT - XFRM_MSG_BASE] = { .doit = xfrm_get_default },
  2782. };
  2783. static int xfrm_reject_unused_attr(int type, struct nlattr **attrs,
  2784. struct netlink_ext_ack *extack)
  2785. {
  2786. if (attrs[XFRMA_SA_DIR]) {
  2787. switch (type) {
  2788. case XFRM_MSG_NEWSA:
  2789. case XFRM_MSG_UPDSA:
  2790. case XFRM_MSG_ALLOCSPI:
  2791. break;
  2792. default:
  2793. NL_SET_ERR_MSG(extack, "Invalid attribute SA_DIR");
  2794. return -EINVAL;
  2795. }
  2796. }
  2797. return 0;
  2798. }
  2799. static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
  2800. struct netlink_ext_ack *extack)
  2801. {
  2802. struct net *net = sock_net(skb->sk);
  2803. struct nlattr *attrs[XFRMA_MAX+1];
  2804. const struct xfrm_link *link;
  2805. struct nlmsghdr *nlh64 = NULL;
  2806. int type, err;
  2807. type = nlh->nlmsg_type;
  2808. if (type > XFRM_MSG_MAX)
  2809. return -EINVAL;
  2810. type -= XFRM_MSG_BASE;
  2811. link = &xfrm_dispatch[type];
  2812. /* All operations require privileges, even GET */
  2813. if (!netlink_net_capable(skb, CAP_NET_ADMIN))
  2814. return -EPERM;
  2815. if (in_compat_syscall()) {
  2816. struct xfrm_translator *xtr = xfrm_get_translator();
  2817. if (!xtr)
  2818. return -EOPNOTSUPP;
  2819. nlh64 = xtr->rcv_msg_compat(nlh, link->nla_max,
  2820. link->nla_pol, extack);
  2821. xfrm_put_translator(xtr);
  2822. if (IS_ERR(nlh64))
  2823. return PTR_ERR(nlh64);
  2824. if (nlh64)
  2825. nlh = nlh64;
  2826. }
  2827. if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
  2828. type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
  2829. (nlh->nlmsg_flags & NLM_F_DUMP)) {
  2830. struct netlink_dump_control c = {
  2831. .start = link->start,
  2832. .dump = link->dump,
  2833. .done = link->done,
  2834. };
  2835. if (link->dump == NULL) {
  2836. err = -EINVAL;
  2837. goto err;
  2838. }
  2839. err = netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
  2840. goto err;
  2841. }
  2842. err = nlmsg_parse_deprecated(nlh, xfrm_msg_min[type], attrs,
  2843. link->nla_max ? : XFRMA_MAX,
  2844. link->nla_pol ? : xfrma_policy, extack);
  2845. if (err < 0)
  2846. goto err;
  2847. if (!link->nla_pol || link->nla_pol == xfrma_policy) {
  2848. err = xfrm_reject_unused_attr((type + XFRM_MSG_BASE), attrs, extack);
  2849. if (err < 0)
  2850. goto err;
  2851. }
  2852. if (link->doit == NULL) {
  2853. err = -EINVAL;
  2854. goto err;
  2855. }
  2856. err = link->doit(skb, nlh, attrs, extack);
  2857. /* We need to free skb allocated in xfrm_alloc_compat() before
  2858. * returning from this function, because consume_skb() won't take
  2859. * care of frag_list since netlink destructor sets
  2860. * sbk->head to NULL. (see netlink_skb_destructor())
  2861. */
  2862. if (skb_has_frag_list(skb)) {
  2863. kfree_skb(skb_shinfo(skb)->frag_list);
  2864. skb_shinfo(skb)->frag_list = NULL;
  2865. }
  2866. err:
  2867. kvfree(nlh64);
  2868. return err;
  2869. }
  2870. static void xfrm_netlink_rcv(struct sk_buff *skb)
  2871. {
  2872. struct net *net = sock_net(skb->sk);
  2873. mutex_lock(&net->xfrm.xfrm_cfg_mutex);
  2874. netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
  2875. mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
  2876. }
  2877. static inline unsigned int xfrm_expire_msgsize(void)
  2878. {
  2879. return NLMSG_ALIGN(sizeof(struct xfrm_user_expire)) +
  2880. nla_total_size(sizeof(struct xfrm_mark)) +
  2881. nla_total_size(sizeof_field(struct xfrm_state, dir)) +
  2882. nla_total_size(4); /* XFRMA_SA_PCPU */
  2883. }
  2884. static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
  2885. {
  2886. struct xfrm_user_expire *ue;
  2887. struct nlmsghdr *nlh;
  2888. int err;
  2889. nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
  2890. if (nlh == NULL)
  2891. return -EMSGSIZE;
  2892. ue = nlmsg_data(nlh);
  2893. copy_to_user_state(x, &ue->state);
  2894. ue->hard = (c->data.hard != 0) ? 1 : 0;
  2895. /* clear the padding bytes */
  2896. memset_after(ue, 0, hard);
  2897. err = xfrm_mark_put(skb, &x->mark);
  2898. if (err)
  2899. return err;
  2900. err = xfrm_if_id_put(skb, x->if_id);
  2901. if (err)
  2902. return err;
  2903. if (x->pcpu_num != UINT_MAX) {
  2904. err = nla_put_u32(skb, XFRMA_SA_PCPU, x->pcpu_num);
  2905. if (err)
  2906. return err;
  2907. }
  2908. if (x->dir) {
  2909. err = nla_put_u8(skb, XFRMA_SA_DIR, x->dir);
  2910. if (err)
  2911. return err;
  2912. }
  2913. nlmsg_end(skb, nlh);
  2914. return 0;
  2915. }
  2916. static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
  2917. {
  2918. struct net *net = xs_net(x);
  2919. struct sk_buff *skb;
  2920. skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
  2921. if (skb == NULL)
  2922. return -ENOMEM;
  2923. if (build_expire(skb, x, c) < 0) {
  2924. kfree_skb(skb);
  2925. return -EMSGSIZE;
  2926. }
  2927. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
  2928. }
  2929. static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
  2930. {
  2931. struct net *net = xs_net(x);
  2932. struct sk_buff *skb;
  2933. int err;
  2934. skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
  2935. if (skb == NULL)
  2936. return -ENOMEM;
  2937. err = build_aevent(skb, x, c);
  2938. BUG_ON(err < 0);
  2939. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
  2940. }
  2941. static int xfrm_notify_sa_flush(const struct km_event *c)
  2942. {
  2943. struct net *net = c->net;
  2944. struct xfrm_usersa_flush *p;
  2945. struct nlmsghdr *nlh;
  2946. struct sk_buff *skb;
  2947. int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
  2948. skb = nlmsg_new(len, GFP_ATOMIC);
  2949. if (skb == NULL)
  2950. return -ENOMEM;
  2951. nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
  2952. if (nlh == NULL) {
  2953. kfree_skb(skb);
  2954. return -EMSGSIZE;
  2955. }
  2956. p = nlmsg_data(nlh);
  2957. p->proto = c->data.proto;
  2958. nlmsg_end(skb, nlh);
  2959. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
  2960. }
  2961. static inline unsigned int xfrm_sa_len(struct xfrm_state *x)
  2962. {
  2963. unsigned int l = 0;
  2964. if (x->aead)
  2965. l += nla_total_size(aead_len(x->aead));
  2966. if (x->aalg) {
  2967. l += nla_total_size(sizeof(struct xfrm_algo) +
  2968. (x->aalg->alg_key_len + 7) / 8);
  2969. l += nla_total_size(xfrm_alg_auth_len(x->aalg));
  2970. }
  2971. if (x->ealg)
  2972. l += nla_total_size(xfrm_alg_len(x->ealg));
  2973. if (x->calg)
  2974. l += nla_total_size(sizeof(*x->calg));
  2975. if (x->encap)
  2976. l += nla_total_size(sizeof(*x->encap));
  2977. if (x->tfcpad)
  2978. l += nla_total_size(sizeof(x->tfcpad));
  2979. if (x->replay_esn)
  2980. l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
  2981. else
  2982. l += nla_total_size(sizeof(struct xfrm_replay_state));
  2983. if (x->security)
  2984. l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
  2985. x->security->ctx_len);
  2986. if (x->coaddr)
  2987. l += nla_total_size(sizeof(*x->coaddr));
  2988. if (x->props.extra_flags)
  2989. l += nla_total_size(sizeof(x->props.extra_flags));
  2990. if (x->xso.dev)
  2991. l += nla_total_size(sizeof(struct xfrm_user_offload));
  2992. if (x->props.smark.v | x->props.smark.m) {
  2993. l += nla_total_size(sizeof(x->props.smark.v));
  2994. l += nla_total_size(sizeof(x->props.smark.m));
  2995. }
  2996. if (x->if_id)
  2997. l += nla_total_size(sizeof(x->if_id));
  2998. if (x->pcpu_num)
  2999. l += nla_total_size(sizeof(x->pcpu_num));
  3000. /* Must count x->lastused as it may become non-zero behind our back. */
  3001. l += nla_total_size_64bit(sizeof(u64));
  3002. if (x->mapping_maxage)
  3003. l += nla_total_size(sizeof(x->mapping_maxage));
  3004. if (x->dir)
  3005. l += nla_total_size(sizeof(x->dir));
  3006. if (x->nat_keepalive_interval)
  3007. l += nla_total_size(sizeof(x->nat_keepalive_interval));
  3008. return l;
  3009. }
  3010. static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
  3011. {
  3012. struct net *net = xs_net(x);
  3013. struct xfrm_usersa_info *p;
  3014. struct xfrm_usersa_id *id;
  3015. struct nlmsghdr *nlh;
  3016. struct sk_buff *skb;
  3017. unsigned int len = xfrm_sa_len(x);
  3018. unsigned int headlen;
  3019. int err;
  3020. headlen = sizeof(*p);
  3021. if (c->event == XFRM_MSG_DELSA) {
  3022. len += nla_total_size(headlen);
  3023. headlen = sizeof(*id);
  3024. len += nla_total_size(sizeof(struct xfrm_mark));
  3025. }
  3026. len += NLMSG_ALIGN(headlen);
  3027. skb = nlmsg_new(len, GFP_ATOMIC);
  3028. if (skb == NULL)
  3029. return -ENOMEM;
  3030. nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
  3031. err = -EMSGSIZE;
  3032. if (nlh == NULL)
  3033. goto out_free_skb;
  3034. p = nlmsg_data(nlh);
  3035. if (c->event == XFRM_MSG_DELSA) {
  3036. struct nlattr *attr;
  3037. id = nlmsg_data(nlh);
  3038. memset(id, 0, sizeof(*id));
  3039. memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
  3040. id->spi = x->id.spi;
  3041. id->family = x->props.family;
  3042. id->proto = x->id.proto;
  3043. attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
  3044. err = -EMSGSIZE;
  3045. if (attr == NULL)
  3046. goto out_free_skb;
  3047. p = nla_data(attr);
  3048. }
  3049. err = copy_to_user_state_extra(x, p, skb);
  3050. if (err)
  3051. goto out_free_skb;
  3052. nlmsg_end(skb, nlh);
  3053. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
  3054. out_free_skb:
  3055. kfree_skb(skb);
  3056. return err;
  3057. }
  3058. static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
  3059. {
  3060. switch (c->event) {
  3061. case XFRM_MSG_EXPIRE:
  3062. return xfrm_exp_state_notify(x, c);
  3063. case XFRM_MSG_NEWAE:
  3064. return xfrm_aevent_state_notify(x, c);
  3065. case XFRM_MSG_DELSA:
  3066. case XFRM_MSG_UPDSA:
  3067. case XFRM_MSG_NEWSA:
  3068. return xfrm_notify_sa(x, c);
  3069. case XFRM_MSG_FLUSHSA:
  3070. return xfrm_notify_sa_flush(c);
  3071. default:
  3072. printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
  3073. c->event);
  3074. break;
  3075. }
  3076. return 0;
  3077. }
  3078. static inline unsigned int xfrm_acquire_msgsize(struct xfrm_state *x,
  3079. struct xfrm_policy *xp)
  3080. {
  3081. return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
  3082. + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
  3083. + nla_total_size(sizeof(struct xfrm_mark))
  3084. + nla_total_size(xfrm_user_sec_ctx_size(x->security))
  3085. + nla_total_size(4) /* XFRMA_SA_PCPU */
  3086. + userpolicy_type_attrsize();
  3087. }
  3088. static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
  3089. struct xfrm_tmpl *xt, struct xfrm_policy *xp)
  3090. {
  3091. __u32 seq = xfrm_get_acqseq();
  3092. struct xfrm_user_acquire *ua;
  3093. struct nlmsghdr *nlh;
  3094. int err;
  3095. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
  3096. if (nlh == NULL)
  3097. return -EMSGSIZE;
  3098. ua = nlmsg_data(nlh);
  3099. memcpy(&ua->id, &x->id, sizeof(ua->id));
  3100. memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
  3101. memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
  3102. copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
  3103. ua->aalgos = xt->aalgos;
  3104. ua->ealgos = xt->ealgos;
  3105. ua->calgos = xt->calgos;
  3106. ua->seq = x->km.seq = seq;
  3107. err = copy_to_user_tmpl(xp, skb);
  3108. if (!err)
  3109. err = copy_to_user_state_sec_ctx(x, skb);
  3110. if (!err)
  3111. err = copy_to_user_policy_type(xp->type, skb);
  3112. if (!err)
  3113. err = xfrm_mark_put(skb, &xp->mark);
  3114. if (!err)
  3115. err = xfrm_if_id_put(skb, xp->if_id);
  3116. if (!err && xp->xdo.dev)
  3117. err = copy_user_offload(&xp->xdo, skb);
  3118. if (!err && x->pcpu_num != UINT_MAX)
  3119. err = nla_put_u32(skb, XFRMA_SA_PCPU, x->pcpu_num);
  3120. if (err) {
  3121. nlmsg_cancel(skb, nlh);
  3122. return err;
  3123. }
  3124. nlmsg_end(skb, nlh);
  3125. return 0;
  3126. }
  3127. static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
  3128. struct xfrm_policy *xp)
  3129. {
  3130. struct net *net = xs_net(x);
  3131. struct sk_buff *skb;
  3132. int err;
  3133. skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
  3134. if (skb == NULL)
  3135. return -ENOMEM;
  3136. err = build_acquire(skb, x, xt, xp);
  3137. BUG_ON(err < 0);
  3138. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
  3139. }
  3140. /* User gives us xfrm_user_policy_info followed by an array of 0
  3141. * or more templates.
  3142. */
  3143. static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
  3144. u8 *data, int len, int *dir)
  3145. {
  3146. struct net *net = sock_net(sk);
  3147. struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
  3148. struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
  3149. struct xfrm_policy *xp;
  3150. int nr;
  3151. switch (sk->sk_family) {
  3152. case AF_INET:
  3153. if (opt != IP_XFRM_POLICY) {
  3154. *dir = -EOPNOTSUPP;
  3155. return NULL;
  3156. }
  3157. break;
  3158. #if IS_ENABLED(CONFIG_IPV6)
  3159. case AF_INET6:
  3160. if (opt != IPV6_XFRM_POLICY) {
  3161. *dir = -EOPNOTSUPP;
  3162. return NULL;
  3163. }
  3164. break;
  3165. #endif
  3166. default:
  3167. *dir = -EINVAL;
  3168. return NULL;
  3169. }
  3170. *dir = -EINVAL;
  3171. if (len < sizeof(*p) ||
  3172. verify_newpolicy_info(p, NULL))
  3173. return NULL;
  3174. nr = ((len - sizeof(*p)) / sizeof(*ut));
  3175. if (validate_tmpl(nr, ut, p->sel.family, p->dir, NULL))
  3176. return NULL;
  3177. if (p->dir > XFRM_POLICY_OUT)
  3178. return NULL;
  3179. xp = xfrm_policy_alloc(net, GFP_ATOMIC);
  3180. if (xp == NULL) {
  3181. *dir = -ENOBUFS;
  3182. return NULL;
  3183. }
  3184. copy_from_user_policy(xp, p);
  3185. xp->type = XFRM_POLICY_TYPE_MAIN;
  3186. copy_templates(xp, ut, nr);
  3187. *dir = p->dir;
  3188. return xp;
  3189. }
  3190. static inline unsigned int xfrm_polexpire_msgsize(struct xfrm_policy *xp)
  3191. {
  3192. return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
  3193. + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
  3194. + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
  3195. + nla_total_size(sizeof(struct xfrm_mark))
  3196. + userpolicy_type_attrsize();
  3197. }
  3198. static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
  3199. int dir, const struct km_event *c)
  3200. {
  3201. struct xfrm_user_polexpire *upe;
  3202. int hard = c->data.hard;
  3203. struct nlmsghdr *nlh;
  3204. int err;
  3205. nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
  3206. if (nlh == NULL)
  3207. return -EMSGSIZE;
  3208. upe = nlmsg_data(nlh);
  3209. copy_to_user_policy(xp, &upe->pol, dir);
  3210. err = copy_to_user_tmpl(xp, skb);
  3211. if (!err)
  3212. err = copy_to_user_sec_ctx(xp, skb);
  3213. if (!err)
  3214. err = copy_to_user_policy_type(xp->type, skb);
  3215. if (!err)
  3216. err = xfrm_mark_put(skb, &xp->mark);
  3217. if (!err)
  3218. err = xfrm_if_id_put(skb, xp->if_id);
  3219. if (!err && xp->xdo.dev)
  3220. err = copy_user_offload(&xp->xdo, skb);
  3221. if (err) {
  3222. nlmsg_cancel(skb, nlh);
  3223. return err;
  3224. }
  3225. upe->hard = !!hard;
  3226. nlmsg_end(skb, nlh);
  3227. return 0;
  3228. }
  3229. static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
  3230. {
  3231. struct net *net = xp_net(xp);
  3232. struct sk_buff *skb;
  3233. int err;
  3234. skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
  3235. if (skb == NULL)
  3236. return -ENOMEM;
  3237. err = build_polexpire(skb, xp, dir, c);
  3238. BUG_ON(err < 0);
  3239. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
  3240. }
  3241. static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
  3242. {
  3243. unsigned int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  3244. struct net *net = xp_net(xp);
  3245. struct xfrm_userpolicy_info *p;
  3246. struct xfrm_userpolicy_id *id;
  3247. struct nlmsghdr *nlh;
  3248. struct sk_buff *skb;
  3249. unsigned int headlen;
  3250. int err;
  3251. headlen = sizeof(*p);
  3252. if (c->event == XFRM_MSG_DELPOLICY) {
  3253. len += nla_total_size(headlen);
  3254. headlen = sizeof(*id);
  3255. }
  3256. len += userpolicy_type_attrsize();
  3257. len += nla_total_size(sizeof(struct xfrm_mark));
  3258. len += NLMSG_ALIGN(headlen);
  3259. skb = nlmsg_new(len, GFP_ATOMIC);
  3260. if (skb == NULL)
  3261. return -ENOMEM;
  3262. nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
  3263. err = -EMSGSIZE;
  3264. if (nlh == NULL)
  3265. goto out_free_skb;
  3266. p = nlmsg_data(nlh);
  3267. if (c->event == XFRM_MSG_DELPOLICY) {
  3268. struct nlattr *attr;
  3269. id = nlmsg_data(nlh);
  3270. memset(id, 0, sizeof(*id));
  3271. id->dir = dir;
  3272. if (c->data.byid)
  3273. id->index = xp->index;
  3274. else
  3275. memcpy(&id->sel, &xp->selector, sizeof(id->sel));
  3276. attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
  3277. err = -EMSGSIZE;
  3278. if (attr == NULL)
  3279. goto out_free_skb;
  3280. p = nla_data(attr);
  3281. }
  3282. copy_to_user_policy(xp, p, dir);
  3283. err = copy_to_user_tmpl(xp, skb);
  3284. if (!err)
  3285. err = copy_to_user_policy_type(xp->type, skb);
  3286. if (!err)
  3287. err = xfrm_mark_put(skb, &xp->mark);
  3288. if (!err)
  3289. err = xfrm_if_id_put(skb, xp->if_id);
  3290. if (!err && xp->xdo.dev)
  3291. err = copy_user_offload(&xp->xdo, skb);
  3292. if (err)
  3293. goto out_free_skb;
  3294. nlmsg_end(skb, nlh);
  3295. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
  3296. out_free_skb:
  3297. kfree_skb(skb);
  3298. return err;
  3299. }
  3300. static int xfrm_notify_policy_flush(const struct km_event *c)
  3301. {
  3302. struct net *net = c->net;
  3303. struct nlmsghdr *nlh;
  3304. struct sk_buff *skb;
  3305. int err;
  3306. skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
  3307. if (skb == NULL)
  3308. return -ENOMEM;
  3309. nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
  3310. err = -EMSGSIZE;
  3311. if (nlh == NULL)
  3312. goto out_free_skb;
  3313. err = copy_to_user_policy_type(c->data.type, skb);
  3314. if (err)
  3315. goto out_free_skb;
  3316. nlmsg_end(skb, nlh);
  3317. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
  3318. out_free_skb:
  3319. kfree_skb(skb);
  3320. return err;
  3321. }
  3322. static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
  3323. {
  3324. switch (c->event) {
  3325. case XFRM_MSG_NEWPOLICY:
  3326. case XFRM_MSG_UPDPOLICY:
  3327. case XFRM_MSG_DELPOLICY:
  3328. return xfrm_notify_policy(xp, dir, c);
  3329. case XFRM_MSG_FLUSHPOLICY:
  3330. return xfrm_notify_policy_flush(c);
  3331. case XFRM_MSG_POLEXPIRE:
  3332. return xfrm_exp_policy_notify(xp, dir, c);
  3333. default:
  3334. printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
  3335. c->event);
  3336. }
  3337. return 0;
  3338. }
  3339. static inline unsigned int xfrm_report_msgsize(void)
  3340. {
  3341. return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
  3342. }
  3343. static int build_report(struct sk_buff *skb, u8 proto,
  3344. struct xfrm_selector *sel, xfrm_address_t *addr)
  3345. {
  3346. struct xfrm_user_report *ur;
  3347. struct nlmsghdr *nlh;
  3348. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
  3349. if (nlh == NULL)
  3350. return -EMSGSIZE;
  3351. ur = nlmsg_data(nlh);
  3352. ur->proto = proto;
  3353. memcpy(&ur->sel, sel, sizeof(ur->sel));
  3354. if (addr) {
  3355. int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
  3356. if (err) {
  3357. nlmsg_cancel(skb, nlh);
  3358. return err;
  3359. }
  3360. }
  3361. nlmsg_end(skb, nlh);
  3362. return 0;
  3363. }
  3364. static int xfrm_send_report(struct net *net, u8 proto,
  3365. struct xfrm_selector *sel, xfrm_address_t *addr)
  3366. {
  3367. struct sk_buff *skb;
  3368. int err;
  3369. skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
  3370. if (skb == NULL)
  3371. return -ENOMEM;
  3372. err = build_report(skb, proto, sel, addr);
  3373. BUG_ON(err < 0);
  3374. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
  3375. }
  3376. static inline unsigned int xfrm_mapping_msgsize(void)
  3377. {
  3378. return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
  3379. }
  3380. static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
  3381. xfrm_address_t *new_saddr, __be16 new_sport)
  3382. {
  3383. struct xfrm_user_mapping *um;
  3384. struct nlmsghdr *nlh;
  3385. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
  3386. if (nlh == NULL)
  3387. return -EMSGSIZE;
  3388. um = nlmsg_data(nlh);
  3389. memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
  3390. um->id.spi = x->id.spi;
  3391. um->id.family = x->props.family;
  3392. um->id.proto = x->id.proto;
  3393. memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
  3394. memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
  3395. um->new_sport = new_sport;
  3396. um->old_sport = x->encap->encap_sport;
  3397. um->reqid = x->props.reqid;
  3398. nlmsg_end(skb, nlh);
  3399. return 0;
  3400. }
  3401. static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
  3402. __be16 sport)
  3403. {
  3404. struct net *net = xs_net(x);
  3405. struct sk_buff *skb;
  3406. int err;
  3407. if (x->id.proto != IPPROTO_ESP)
  3408. return -EINVAL;
  3409. if (!x->encap)
  3410. return -EINVAL;
  3411. skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
  3412. if (skb == NULL)
  3413. return -ENOMEM;
  3414. err = build_mapping(skb, x, ipaddr, sport);
  3415. BUG_ON(err < 0);
  3416. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
  3417. }
  3418. static bool xfrm_is_alive(const struct km_event *c)
  3419. {
  3420. return (bool)xfrm_acquire_is_on(c->net);
  3421. }
  3422. static struct xfrm_mgr netlink_mgr = {
  3423. .notify = xfrm_send_state_notify,
  3424. .acquire = xfrm_send_acquire,
  3425. .compile_policy = xfrm_compile_policy,
  3426. .notify_policy = xfrm_send_policy_notify,
  3427. .report = xfrm_send_report,
  3428. .migrate = xfrm_send_migrate,
  3429. .new_mapping = xfrm_send_mapping,
  3430. .is_alive = xfrm_is_alive,
  3431. };
  3432. static int __net_init xfrm_user_net_init(struct net *net)
  3433. {
  3434. struct sock *nlsk;
  3435. struct netlink_kernel_cfg cfg = {
  3436. .groups = XFRMNLGRP_MAX,
  3437. .input = xfrm_netlink_rcv,
  3438. };
  3439. nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
  3440. if (nlsk == NULL)
  3441. return -ENOMEM;
  3442. net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
  3443. rcu_assign_pointer(net->xfrm.nlsk, nlsk);
  3444. return 0;
  3445. }
  3446. static void __net_exit xfrm_user_net_pre_exit(struct net *net)
  3447. {
  3448. RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
  3449. }
  3450. static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
  3451. {
  3452. struct net *net;
  3453. list_for_each_entry(net, net_exit_list, exit_list)
  3454. netlink_kernel_release(net->xfrm.nlsk_stash);
  3455. }
  3456. static struct pernet_operations xfrm_user_net_ops = {
  3457. .init = xfrm_user_net_init,
  3458. .pre_exit = xfrm_user_net_pre_exit,
  3459. .exit_batch = xfrm_user_net_exit,
  3460. };
  3461. static int __init xfrm_user_init(void)
  3462. {
  3463. int rv;
  3464. printk(KERN_INFO "Initializing XFRM netlink socket\n");
  3465. rv = register_pernet_subsys(&xfrm_user_net_ops);
  3466. if (rv < 0)
  3467. return rv;
  3468. xfrm_register_km(&netlink_mgr);
  3469. return 0;
  3470. }
  3471. static void __exit xfrm_user_exit(void)
  3472. {
  3473. xfrm_unregister_km(&netlink_mgr);
  3474. unregister_pernet_subsys(&xfrm_user_net_ops);
  3475. }
  3476. module_init(xfrm_user_init);
  3477. module_exit(xfrm_user_exit);
  3478. MODULE_DESCRIPTION("XFRM User interface");
  3479. MODULE_LICENSE("GPL");
  3480. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);