xfrm_user.c 71 KB

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