xfrm_user.c 103 KB

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