xfrm_user.c 73 KB

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