xfrm_user.c 71 KB

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