xfrm_user.c 76 KB

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