xfrm_user.c 73 KB

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