xfrm_user.c 70 KB

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