xfrm_user.c 74 KB

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