xfrm_user.c 78 KB

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