xfrm_user.c 71 KB

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