xfrm_user.c 75 KB

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