xfrm_user.c 73 KB

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