xfrm_user.c 99 KB

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