xfrm_user.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046
  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. if (p->id.proto != IPPROTO_ESP)
  113. return -EINVAL;
  114. if (p->replay_window != 0)
  115. return -EINVAL;
  116. return 0;
  117. }
  118. static int verify_newsa_info(struct xfrm_usersa_info *p,
  119. struct nlattr **attrs)
  120. {
  121. int err;
  122. err = -EINVAL;
  123. switch (p->family) {
  124. case AF_INET:
  125. break;
  126. case AF_INET6:
  127. #if IS_ENABLED(CONFIG_IPV6)
  128. break;
  129. #else
  130. err = -EAFNOSUPPORT;
  131. goto out;
  132. #endif
  133. default:
  134. goto out;
  135. }
  136. err = -EINVAL;
  137. switch (p->id.proto) {
  138. case IPPROTO_AH:
  139. if ((!attrs[XFRMA_ALG_AUTH] &&
  140. !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
  141. attrs[XFRMA_ALG_AEAD] ||
  142. attrs[XFRMA_ALG_CRYPT] ||
  143. attrs[XFRMA_ALG_COMP] ||
  144. attrs[XFRMA_TFCPAD] ||
  145. (ntohl(p->id.spi) >= 0x10000))
  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. goto out;
  173. break;
  174. #if IS_ENABLED(CONFIG_IPV6)
  175. case IPPROTO_DSTOPTS:
  176. case IPPROTO_ROUTING:
  177. if (attrs[XFRMA_ALG_COMP] ||
  178. attrs[XFRMA_ALG_AUTH] ||
  179. attrs[XFRMA_ALG_AUTH_TRUNC] ||
  180. attrs[XFRMA_ALG_AEAD] ||
  181. attrs[XFRMA_ALG_CRYPT] ||
  182. attrs[XFRMA_ENCAP] ||
  183. attrs[XFRMA_SEC_CTX] ||
  184. attrs[XFRMA_TFCPAD] ||
  185. !attrs[XFRMA_COADDR])
  186. goto out;
  187. break;
  188. #endif
  189. default:
  190. goto out;
  191. }
  192. if ((err = verify_aead(attrs)))
  193. goto out;
  194. if ((err = verify_auth_trunc(attrs)))
  195. goto out;
  196. if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
  197. goto out;
  198. if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
  199. goto out;
  200. if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
  201. goto out;
  202. if ((err = verify_sec_ctx_len(attrs)))
  203. goto out;
  204. if ((err = verify_replay(p, attrs)))
  205. goto out;
  206. err = -EINVAL;
  207. switch (p->mode) {
  208. case XFRM_MODE_TRANSPORT:
  209. case XFRM_MODE_TUNNEL:
  210. case XFRM_MODE_ROUTEOPTIMIZATION:
  211. case XFRM_MODE_BEET:
  212. break;
  213. default:
  214. goto out;
  215. }
  216. err = 0;
  217. out:
  218. return err;
  219. }
  220. static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
  221. struct xfrm_algo_desc *(*get_byname)(const char *, int),
  222. struct nlattr *rta)
  223. {
  224. struct xfrm_algo *p, *ualg;
  225. struct xfrm_algo_desc *algo;
  226. if (!rta)
  227. return 0;
  228. ualg = nla_data(rta);
  229. algo = get_byname(ualg->alg_name, 1);
  230. if (!algo)
  231. return -ENOSYS;
  232. *props = algo->desc.sadb_alg_id;
  233. p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
  234. if (!p)
  235. return -ENOMEM;
  236. strcpy(p->alg_name, algo->name);
  237. *algpp = p;
  238. return 0;
  239. }
  240. static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
  241. struct nlattr *rta)
  242. {
  243. struct xfrm_algo *ualg;
  244. struct xfrm_algo_auth *p;
  245. struct xfrm_algo_desc *algo;
  246. if (!rta)
  247. return 0;
  248. ualg = nla_data(rta);
  249. algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
  250. if (!algo)
  251. return -ENOSYS;
  252. *props = algo->desc.sadb_alg_id;
  253. p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
  254. if (!p)
  255. return -ENOMEM;
  256. strcpy(p->alg_name, algo->name);
  257. p->alg_key_len = ualg->alg_key_len;
  258. p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
  259. memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
  260. *algpp = p;
  261. return 0;
  262. }
  263. static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
  264. struct nlattr *rta)
  265. {
  266. struct xfrm_algo_auth *p, *ualg;
  267. struct xfrm_algo_desc *algo;
  268. if (!rta)
  269. return 0;
  270. ualg = nla_data(rta);
  271. algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
  272. if (!algo)
  273. return -ENOSYS;
  274. if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
  275. ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
  276. return -EINVAL;
  277. *props = algo->desc.sadb_alg_id;
  278. p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
  279. if (!p)
  280. return -ENOMEM;
  281. strcpy(p->alg_name, algo->name);
  282. if (!p->alg_trunc_len)
  283. p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
  284. *algpp = p;
  285. return 0;
  286. }
  287. static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
  288. struct nlattr *rta)
  289. {
  290. struct xfrm_algo_aead *p, *ualg;
  291. struct xfrm_algo_desc *algo;
  292. if (!rta)
  293. return 0;
  294. ualg = nla_data(rta);
  295. algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
  296. if (!algo)
  297. return -ENOSYS;
  298. *props = algo->desc.sadb_alg_id;
  299. p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
  300. if (!p)
  301. return -ENOMEM;
  302. strcpy(p->alg_name, algo->name);
  303. *algpp = p;
  304. return 0;
  305. }
  306. static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
  307. struct nlattr *rp)
  308. {
  309. struct xfrm_replay_state_esn *up;
  310. int ulen;
  311. if (!replay_esn || !rp)
  312. return 0;
  313. up = nla_data(rp);
  314. ulen = xfrm_replay_state_esn_len(up);
  315. if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
  316. return -EINVAL;
  317. return 0;
  318. }
  319. static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
  320. struct xfrm_replay_state_esn **preplay_esn,
  321. struct nlattr *rta)
  322. {
  323. struct xfrm_replay_state_esn *p, *pp, *up;
  324. int klen, ulen;
  325. if (!rta)
  326. return 0;
  327. up = nla_data(rta);
  328. klen = xfrm_replay_state_esn_len(up);
  329. ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
  330. p = kzalloc(klen, GFP_KERNEL);
  331. if (!p)
  332. return -ENOMEM;
  333. pp = kzalloc(klen, GFP_KERNEL);
  334. if (!pp) {
  335. kfree(p);
  336. return -ENOMEM;
  337. }
  338. memcpy(p, up, ulen);
  339. memcpy(pp, up, ulen);
  340. *replay_esn = p;
  341. *preplay_esn = pp;
  342. return 0;
  343. }
  344. static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
  345. {
  346. int len = 0;
  347. if (xfrm_ctx) {
  348. len += sizeof(struct xfrm_user_sec_ctx);
  349. len += xfrm_ctx->ctx_len;
  350. }
  351. return len;
  352. }
  353. static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  354. {
  355. memcpy(&x->id, &p->id, sizeof(x->id));
  356. memcpy(&x->sel, &p->sel, sizeof(x->sel));
  357. memcpy(&x->lft, &p->lft, sizeof(x->lft));
  358. x->props.mode = p->mode;
  359. x->props.replay_window = min_t(unsigned int, p->replay_window,
  360. sizeof(x->replay.bitmap) * 8);
  361. x->props.reqid = p->reqid;
  362. x->props.family = p->family;
  363. memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
  364. x->props.flags = p->flags;
  365. if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
  366. x->sel.family = p->family;
  367. }
  368. /*
  369. * someday when pfkey also has support, we could have the code
  370. * somehow made shareable and move it to xfrm_state.c - JHS
  371. *
  372. */
  373. static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
  374. int update_esn)
  375. {
  376. struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
  377. struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
  378. struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
  379. struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
  380. struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
  381. if (re) {
  382. struct xfrm_replay_state_esn *replay_esn;
  383. replay_esn = nla_data(re);
  384. memcpy(x->replay_esn, replay_esn,
  385. xfrm_replay_state_esn_len(replay_esn));
  386. memcpy(x->preplay_esn, replay_esn,
  387. xfrm_replay_state_esn_len(replay_esn));
  388. }
  389. if (rp) {
  390. struct xfrm_replay_state *replay;
  391. replay = nla_data(rp);
  392. memcpy(&x->replay, replay, sizeof(*replay));
  393. memcpy(&x->preplay, replay, sizeof(*replay));
  394. }
  395. if (lt) {
  396. struct xfrm_lifetime_cur *ltime;
  397. ltime = nla_data(lt);
  398. x->curlft.bytes = ltime->bytes;
  399. x->curlft.packets = ltime->packets;
  400. x->curlft.add_time = ltime->add_time;
  401. x->curlft.use_time = ltime->use_time;
  402. }
  403. if (et)
  404. x->replay_maxage = nla_get_u32(et);
  405. if (rt)
  406. x->replay_maxdiff = nla_get_u32(rt);
  407. }
  408. static struct xfrm_state *xfrm_state_construct(struct net *net,
  409. struct xfrm_usersa_info *p,
  410. struct nlattr **attrs,
  411. int *errp)
  412. {
  413. struct xfrm_state *x = xfrm_state_alloc(net);
  414. int err = -ENOMEM;
  415. if (!x)
  416. goto error_no_put;
  417. copy_from_user_state(x, p);
  418. if (attrs[XFRMA_SA_EXTRA_FLAGS])
  419. x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
  420. if ((err = attach_aead(&x->aead, &x->props.ealgo,
  421. attrs[XFRMA_ALG_AEAD])))
  422. goto error;
  423. if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
  424. attrs[XFRMA_ALG_AUTH_TRUNC])))
  425. goto error;
  426. if (!x->props.aalgo) {
  427. if ((err = attach_auth(&x->aalg, &x->props.aalgo,
  428. attrs[XFRMA_ALG_AUTH])))
  429. goto error;
  430. }
  431. if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
  432. xfrm_ealg_get_byname,
  433. attrs[XFRMA_ALG_CRYPT])))
  434. goto error;
  435. if ((err = attach_one_algo(&x->calg, &x->props.calgo,
  436. xfrm_calg_get_byname,
  437. attrs[XFRMA_ALG_COMP])))
  438. goto error;
  439. if (attrs[XFRMA_ENCAP]) {
  440. x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
  441. sizeof(*x->encap), GFP_KERNEL);
  442. if (x->encap == NULL)
  443. goto error;
  444. }
  445. if (attrs[XFRMA_TFCPAD])
  446. x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
  447. if (attrs[XFRMA_COADDR]) {
  448. x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
  449. sizeof(*x->coaddr), GFP_KERNEL);
  450. if (x->coaddr == NULL)
  451. goto error;
  452. }
  453. xfrm_mark_get(attrs, &x->mark);
  454. err = __xfrm_init_state(x, false);
  455. if (err)
  456. goto error;
  457. if (attrs[XFRMA_SEC_CTX] &&
  458. security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
  459. goto error;
  460. if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
  461. attrs[XFRMA_REPLAY_ESN_VAL])))
  462. goto error;
  463. x->km.seq = p->seq;
  464. x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
  465. /* sysctl_xfrm_aevent_etime is in 100ms units */
  466. x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
  467. if ((err = xfrm_init_replay(x)))
  468. goto error;
  469. /* override default values from above */
  470. xfrm_update_ae_params(x, attrs, 0);
  471. return x;
  472. error:
  473. x->km.state = XFRM_STATE_DEAD;
  474. xfrm_state_put(x);
  475. error_no_put:
  476. *errp = err;
  477. return NULL;
  478. }
  479. static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  480. struct nlattr **attrs)
  481. {
  482. struct net *net = sock_net(skb->sk);
  483. struct xfrm_usersa_info *p = nlmsg_data(nlh);
  484. struct xfrm_state *x;
  485. int err;
  486. struct km_event c;
  487. kuid_t loginuid = audit_get_loginuid(current);
  488. unsigned int sessionid = audit_get_sessionid(current);
  489. u32 sid;
  490. err = verify_newsa_info(p, attrs);
  491. if (err)
  492. return err;
  493. x = xfrm_state_construct(net, p, attrs, &err);
  494. if (!x)
  495. return err;
  496. xfrm_state_hold(x);
  497. if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
  498. err = xfrm_state_add(x);
  499. else
  500. err = xfrm_state_update(x);
  501. security_task_getsecid(current, &sid);
  502. xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
  503. if (err < 0) {
  504. x->km.state = XFRM_STATE_DEAD;
  505. __xfrm_state_put(x);
  506. goto out;
  507. }
  508. c.seq = nlh->nlmsg_seq;
  509. c.portid = nlh->nlmsg_pid;
  510. c.event = nlh->nlmsg_type;
  511. km_state_notify(x, &c);
  512. out:
  513. xfrm_state_put(x);
  514. return err;
  515. }
  516. static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
  517. struct xfrm_usersa_id *p,
  518. struct nlattr **attrs,
  519. int *errp)
  520. {
  521. struct xfrm_state *x = NULL;
  522. struct xfrm_mark m;
  523. int err;
  524. u32 mark = xfrm_mark_get(attrs, &m);
  525. if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
  526. err = -ESRCH;
  527. x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
  528. } else {
  529. xfrm_address_t *saddr = NULL;
  530. verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
  531. if (!saddr) {
  532. err = -EINVAL;
  533. goto out;
  534. }
  535. err = -ESRCH;
  536. x = xfrm_state_lookup_byaddr(net, mark,
  537. &p->daddr, saddr,
  538. p->proto, p->family);
  539. }
  540. out:
  541. if (!x && errp)
  542. *errp = err;
  543. return x;
  544. }
  545. static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  546. struct nlattr **attrs)
  547. {
  548. struct net *net = sock_net(skb->sk);
  549. struct xfrm_state *x;
  550. int err = -ESRCH;
  551. struct km_event c;
  552. struct xfrm_usersa_id *p = nlmsg_data(nlh);
  553. kuid_t loginuid = audit_get_loginuid(current);
  554. unsigned int sessionid = audit_get_sessionid(current);
  555. u32 sid;
  556. x = xfrm_user_state_lookup(net, p, attrs, &err);
  557. if (x == NULL)
  558. return err;
  559. if ((err = security_xfrm_state_delete(x)) != 0)
  560. goto out;
  561. if (xfrm_state_kern(x)) {
  562. err = -EPERM;
  563. goto out;
  564. }
  565. err = xfrm_state_delete(x);
  566. if (err < 0)
  567. goto out;
  568. c.seq = nlh->nlmsg_seq;
  569. c.portid = nlh->nlmsg_pid;
  570. c.event = nlh->nlmsg_type;
  571. km_state_notify(x, &c);
  572. out:
  573. security_task_getsecid(current, &sid);
  574. xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
  575. xfrm_state_put(x);
  576. return err;
  577. }
  578. static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  579. {
  580. memset(p, 0, sizeof(*p));
  581. memcpy(&p->id, &x->id, sizeof(p->id));
  582. memcpy(&p->sel, &x->sel, sizeof(p->sel));
  583. memcpy(&p->lft, &x->lft, sizeof(p->lft));
  584. memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
  585. memcpy(&p->stats, &x->stats, sizeof(p->stats));
  586. memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
  587. p->mode = x->props.mode;
  588. p->replay_window = x->props.replay_window;
  589. p->reqid = x->props.reqid;
  590. p->family = x->props.family;
  591. p->flags = x->props.flags;
  592. p->seq = x->km.seq;
  593. }
  594. struct xfrm_dump_info {
  595. struct sk_buff *in_skb;
  596. struct sk_buff *out_skb;
  597. u32 nlmsg_seq;
  598. u16 nlmsg_flags;
  599. };
  600. static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
  601. {
  602. struct xfrm_user_sec_ctx *uctx;
  603. struct nlattr *attr;
  604. int ctx_size = sizeof(*uctx) + s->ctx_len;
  605. attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
  606. if (attr == NULL)
  607. return -EMSGSIZE;
  608. uctx = nla_data(attr);
  609. uctx->exttype = XFRMA_SEC_CTX;
  610. uctx->len = ctx_size;
  611. uctx->ctx_doi = s->ctx_doi;
  612. uctx->ctx_alg = s->ctx_alg;
  613. uctx->ctx_len = s->ctx_len;
  614. memcpy(uctx + 1, s->ctx_str, s->ctx_len);
  615. return 0;
  616. }
  617. static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
  618. {
  619. struct xfrm_algo *algo;
  620. struct nlattr *nla;
  621. nla = nla_reserve(skb, XFRMA_ALG_AUTH,
  622. sizeof(*algo) + (auth->alg_key_len + 7) / 8);
  623. if (!nla)
  624. return -EMSGSIZE;
  625. algo = nla_data(nla);
  626. strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
  627. memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
  628. algo->alg_key_len = auth->alg_key_len;
  629. return 0;
  630. }
  631. /* Don't change this without updating xfrm_sa_len! */
  632. static int copy_to_user_state_extra(struct xfrm_state *x,
  633. struct xfrm_usersa_info *p,
  634. struct sk_buff *skb)
  635. {
  636. int ret = 0;
  637. copy_to_user_state(x, p);
  638. if (x->props.extra_flags) {
  639. ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
  640. x->props.extra_flags);
  641. if (ret)
  642. goto out;
  643. }
  644. if (x->coaddr) {
  645. ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
  646. if (ret)
  647. goto out;
  648. }
  649. if (x->lastused) {
  650. ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
  651. if (ret)
  652. goto out;
  653. }
  654. if (x->aead) {
  655. ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
  656. if (ret)
  657. goto out;
  658. }
  659. if (x->aalg) {
  660. ret = copy_to_user_auth(x->aalg, skb);
  661. if (!ret)
  662. ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
  663. xfrm_alg_auth_len(x->aalg), x->aalg);
  664. if (ret)
  665. goto out;
  666. }
  667. if (x->ealg) {
  668. ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
  669. if (ret)
  670. goto out;
  671. }
  672. if (x->calg) {
  673. ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  674. if (ret)
  675. goto out;
  676. }
  677. if (x->encap) {
  678. ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  679. if (ret)
  680. goto out;
  681. }
  682. if (x->tfcpad) {
  683. ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
  684. if (ret)
  685. goto out;
  686. }
  687. ret = xfrm_mark_put(skb, &x->mark);
  688. if (ret)
  689. goto out;
  690. if (x->replay_esn) {
  691. ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
  692. xfrm_replay_state_esn_len(x->replay_esn),
  693. x->replay_esn);
  694. if (ret)
  695. goto out;
  696. }
  697. if (x->security)
  698. ret = copy_sec_ctx(x->security, skb);
  699. out:
  700. return ret;
  701. }
  702. static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
  703. {
  704. struct xfrm_dump_info *sp = ptr;
  705. struct sk_buff *in_skb = sp->in_skb;
  706. struct sk_buff *skb = sp->out_skb;
  707. struct xfrm_usersa_info *p;
  708. struct nlmsghdr *nlh;
  709. int err;
  710. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
  711. XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
  712. if (nlh == NULL)
  713. return -EMSGSIZE;
  714. p = nlmsg_data(nlh);
  715. err = copy_to_user_state_extra(x, p, skb);
  716. if (err) {
  717. nlmsg_cancel(skb, nlh);
  718. return err;
  719. }
  720. nlmsg_end(skb, nlh);
  721. return 0;
  722. }
  723. static int xfrm_dump_sa_done(struct netlink_callback *cb)
  724. {
  725. struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
  726. struct sock *sk = cb->skb->sk;
  727. struct net *net = sock_net(sk);
  728. xfrm_state_walk_done(walk, net);
  729. return 0;
  730. }
  731. static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
  732. {
  733. struct net *net = sock_net(skb->sk);
  734. struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
  735. struct xfrm_dump_info info;
  736. BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
  737. sizeof(cb->args) - sizeof(cb->args[0]));
  738. info.in_skb = cb->skb;
  739. info.out_skb = skb;
  740. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  741. info.nlmsg_flags = NLM_F_MULTI;
  742. if (!cb->args[0]) {
  743. cb->args[0] = 1;
  744. xfrm_state_walk_init(walk, 0);
  745. }
  746. (void) xfrm_state_walk(net, walk, dump_one_state, &info);
  747. return skb->len;
  748. }
  749. static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
  750. struct xfrm_state *x, u32 seq)
  751. {
  752. struct xfrm_dump_info info;
  753. struct sk_buff *skb;
  754. int err;
  755. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  756. if (!skb)
  757. return ERR_PTR(-ENOMEM);
  758. info.in_skb = in_skb;
  759. info.out_skb = skb;
  760. info.nlmsg_seq = seq;
  761. info.nlmsg_flags = 0;
  762. err = dump_one_state(x, 0, &info);
  763. if (err) {
  764. kfree_skb(skb);
  765. return ERR_PTR(err);
  766. }
  767. return skb;
  768. }
  769. static inline size_t xfrm_spdinfo_msgsize(void)
  770. {
  771. return NLMSG_ALIGN(4)
  772. + nla_total_size(sizeof(struct xfrmu_spdinfo))
  773. + nla_total_size(sizeof(struct xfrmu_spdhinfo));
  774. }
  775. static int build_spdinfo(struct sk_buff *skb, struct net *net,
  776. u32 portid, u32 seq, u32 flags)
  777. {
  778. struct xfrmk_spdinfo si;
  779. struct xfrmu_spdinfo spc;
  780. struct xfrmu_spdhinfo sph;
  781. struct nlmsghdr *nlh;
  782. int err;
  783. u32 *f;
  784. nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
  785. if (nlh == NULL) /* shouldn't really happen ... */
  786. return -EMSGSIZE;
  787. f = nlmsg_data(nlh);
  788. *f = flags;
  789. xfrm_spd_getinfo(net, &si);
  790. spc.incnt = si.incnt;
  791. spc.outcnt = si.outcnt;
  792. spc.fwdcnt = si.fwdcnt;
  793. spc.inscnt = si.inscnt;
  794. spc.outscnt = si.outscnt;
  795. spc.fwdscnt = si.fwdscnt;
  796. sph.spdhcnt = si.spdhcnt;
  797. sph.spdhmcnt = si.spdhmcnt;
  798. err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
  799. if (!err)
  800. err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
  801. if (err) {
  802. nlmsg_cancel(skb, nlh);
  803. return err;
  804. }
  805. return nlmsg_end(skb, nlh);
  806. }
  807. static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  808. struct nlattr **attrs)
  809. {
  810. struct net *net = sock_net(skb->sk);
  811. struct sk_buff *r_skb;
  812. u32 *flags = nlmsg_data(nlh);
  813. u32 sportid = NETLINK_CB(skb).portid;
  814. u32 seq = nlh->nlmsg_seq;
  815. r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
  816. if (r_skb == NULL)
  817. return -ENOMEM;
  818. if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
  819. BUG();
  820. return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
  821. }
  822. static inline size_t xfrm_sadinfo_msgsize(void)
  823. {
  824. return NLMSG_ALIGN(4)
  825. + nla_total_size(sizeof(struct xfrmu_sadhinfo))
  826. + nla_total_size(4); /* XFRMA_SAD_CNT */
  827. }
  828. static int build_sadinfo(struct sk_buff *skb, struct net *net,
  829. u32 portid, u32 seq, u32 flags)
  830. {
  831. struct xfrmk_sadinfo si;
  832. struct xfrmu_sadhinfo sh;
  833. struct nlmsghdr *nlh;
  834. int err;
  835. u32 *f;
  836. nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
  837. if (nlh == NULL) /* shouldn't really happen ... */
  838. return -EMSGSIZE;
  839. f = nlmsg_data(nlh);
  840. *f = flags;
  841. xfrm_sad_getinfo(net, &si);
  842. sh.sadhmcnt = si.sadhmcnt;
  843. sh.sadhcnt = si.sadhcnt;
  844. err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
  845. if (!err)
  846. err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
  847. if (err) {
  848. nlmsg_cancel(skb, nlh);
  849. return err;
  850. }
  851. return nlmsg_end(skb, nlh);
  852. }
  853. static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  854. struct nlattr **attrs)
  855. {
  856. struct net *net = sock_net(skb->sk);
  857. struct sk_buff *r_skb;
  858. u32 *flags = nlmsg_data(nlh);
  859. u32 sportid = NETLINK_CB(skb).portid;
  860. u32 seq = nlh->nlmsg_seq;
  861. r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
  862. if (r_skb == NULL)
  863. return -ENOMEM;
  864. if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
  865. BUG();
  866. return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
  867. }
  868. static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  869. struct nlattr **attrs)
  870. {
  871. struct net *net = sock_net(skb->sk);
  872. struct xfrm_usersa_id *p = nlmsg_data(nlh);
  873. struct xfrm_state *x;
  874. struct sk_buff *resp_skb;
  875. int err = -ESRCH;
  876. x = xfrm_user_state_lookup(net, p, attrs, &err);
  877. if (x == NULL)
  878. goto out_noput;
  879. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  880. if (IS_ERR(resp_skb)) {
  881. err = PTR_ERR(resp_skb);
  882. } else {
  883. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
  884. }
  885. xfrm_state_put(x);
  886. out_noput:
  887. return err;
  888. }
  889. static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
  890. struct nlattr **attrs)
  891. {
  892. struct net *net = sock_net(skb->sk);
  893. struct xfrm_state *x;
  894. struct xfrm_userspi_info *p;
  895. struct sk_buff *resp_skb;
  896. xfrm_address_t *daddr;
  897. int family;
  898. int err;
  899. u32 mark;
  900. struct xfrm_mark m;
  901. p = nlmsg_data(nlh);
  902. err = verify_spi_info(p->info.id.proto, p->min, p->max);
  903. if (err)
  904. goto out_noput;
  905. family = p->info.family;
  906. daddr = &p->info.id.daddr;
  907. x = NULL;
  908. mark = xfrm_mark_get(attrs, &m);
  909. if (p->info.seq) {
  910. x = xfrm_find_acq_byseq(net, mark, p->info.seq);
  911. if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
  912. xfrm_state_put(x);
  913. x = NULL;
  914. }
  915. }
  916. if (!x)
  917. x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
  918. p->info.id.proto, daddr,
  919. &p->info.saddr, 1,
  920. family);
  921. err = -ENOENT;
  922. if (x == NULL)
  923. goto out_noput;
  924. err = xfrm_alloc_spi(x, p->min, p->max);
  925. if (err)
  926. goto out;
  927. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  928. if (IS_ERR(resp_skb)) {
  929. err = PTR_ERR(resp_skb);
  930. goto out;
  931. }
  932. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
  933. out:
  934. xfrm_state_put(x);
  935. out_noput:
  936. return err;
  937. }
  938. static int verify_policy_dir(u8 dir)
  939. {
  940. switch (dir) {
  941. case XFRM_POLICY_IN:
  942. case XFRM_POLICY_OUT:
  943. case XFRM_POLICY_FWD:
  944. break;
  945. default:
  946. return -EINVAL;
  947. }
  948. return 0;
  949. }
  950. static int verify_policy_type(u8 type)
  951. {
  952. switch (type) {
  953. case XFRM_POLICY_TYPE_MAIN:
  954. #ifdef CONFIG_XFRM_SUB_POLICY
  955. case XFRM_POLICY_TYPE_SUB:
  956. #endif
  957. break;
  958. default:
  959. return -EINVAL;
  960. }
  961. return 0;
  962. }
  963. static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
  964. {
  965. int ret;
  966. switch (p->share) {
  967. case XFRM_SHARE_ANY:
  968. case XFRM_SHARE_SESSION:
  969. case XFRM_SHARE_USER:
  970. case XFRM_SHARE_UNIQUE:
  971. break;
  972. default:
  973. return -EINVAL;
  974. }
  975. switch (p->action) {
  976. case XFRM_POLICY_ALLOW:
  977. case XFRM_POLICY_BLOCK:
  978. break;
  979. default:
  980. return -EINVAL;
  981. }
  982. switch (p->sel.family) {
  983. case AF_INET:
  984. break;
  985. case AF_INET6:
  986. #if IS_ENABLED(CONFIG_IPV6)
  987. break;
  988. #else
  989. return -EAFNOSUPPORT;
  990. #endif
  991. default:
  992. return -EINVAL;
  993. }
  994. ret = verify_policy_dir(p->dir);
  995. if (ret)
  996. return ret;
  997. if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
  998. return -EINVAL;
  999. return 0;
  1000. }
  1001. static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
  1002. {
  1003. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1004. struct xfrm_user_sec_ctx *uctx;
  1005. if (!rt)
  1006. return 0;
  1007. uctx = nla_data(rt);
  1008. return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
  1009. }
  1010. static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
  1011. int nr)
  1012. {
  1013. int i;
  1014. xp->xfrm_nr = nr;
  1015. for (i = 0; i < nr; i++, ut++) {
  1016. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  1017. memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
  1018. memcpy(&t->saddr, &ut->saddr,
  1019. sizeof(xfrm_address_t));
  1020. t->reqid = ut->reqid;
  1021. t->mode = ut->mode;
  1022. t->share = ut->share;
  1023. t->optional = ut->optional;
  1024. t->aalgos = ut->aalgos;
  1025. t->ealgos = ut->ealgos;
  1026. t->calgos = ut->calgos;
  1027. /* If all masks are ~0, then we allow all algorithms. */
  1028. t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
  1029. t->encap_family = ut->family;
  1030. }
  1031. }
  1032. static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
  1033. {
  1034. int i;
  1035. if (nr > XFRM_MAX_DEPTH)
  1036. return -EINVAL;
  1037. for (i = 0; i < nr; i++) {
  1038. /* We never validated the ut->family value, so many
  1039. * applications simply leave it at zero. The check was
  1040. * never made and ut->family was ignored because all
  1041. * templates could be assumed to have the same family as
  1042. * the policy itself. Now that we will have ipv4-in-ipv6
  1043. * and ipv6-in-ipv4 tunnels, this is no longer true.
  1044. */
  1045. if (!ut[i].family)
  1046. ut[i].family = family;
  1047. switch (ut[i].family) {
  1048. case AF_INET:
  1049. break;
  1050. #if IS_ENABLED(CONFIG_IPV6)
  1051. case AF_INET6:
  1052. break;
  1053. #endif
  1054. default:
  1055. return -EINVAL;
  1056. }
  1057. }
  1058. return 0;
  1059. }
  1060. static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
  1061. {
  1062. struct nlattr *rt = attrs[XFRMA_TMPL];
  1063. if (!rt) {
  1064. pol->xfrm_nr = 0;
  1065. } else {
  1066. struct xfrm_user_tmpl *utmpl = nla_data(rt);
  1067. int nr = nla_len(rt) / sizeof(*utmpl);
  1068. int err;
  1069. err = validate_tmpl(nr, utmpl, pol->family);
  1070. if (err)
  1071. return err;
  1072. copy_templates(pol, utmpl, nr);
  1073. }
  1074. return 0;
  1075. }
  1076. static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
  1077. {
  1078. struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
  1079. struct xfrm_userpolicy_type *upt;
  1080. u8 type = XFRM_POLICY_TYPE_MAIN;
  1081. int err;
  1082. if (rt) {
  1083. upt = nla_data(rt);
  1084. type = upt->type;
  1085. }
  1086. err = verify_policy_type(type);
  1087. if (err)
  1088. return err;
  1089. *tp = type;
  1090. return 0;
  1091. }
  1092. static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
  1093. {
  1094. xp->priority = p->priority;
  1095. xp->index = p->index;
  1096. memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
  1097. memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
  1098. xp->action = p->action;
  1099. xp->flags = p->flags;
  1100. xp->family = p->sel.family;
  1101. /* XXX xp->share = p->share; */
  1102. }
  1103. static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
  1104. {
  1105. memset(p, 0, sizeof(*p));
  1106. memcpy(&p->sel, &xp->selector, sizeof(p->sel));
  1107. memcpy(&p->lft, &xp->lft, sizeof(p->lft));
  1108. memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
  1109. p->priority = xp->priority;
  1110. p->index = xp->index;
  1111. p->sel.family = xp->family;
  1112. p->dir = dir;
  1113. p->action = xp->action;
  1114. p->flags = xp->flags;
  1115. p->share = XFRM_SHARE_ANY; /* XXX xp->share */
  1116. }
  1117. static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
  1118. {
  1119. struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
  1120. int err;
  1121. if (!xp) {
  1122. *errp = -ENOMEM;
  1123. return NULL;
  1124. }
  1125. copy_from_user_policy(xp, p);
  1126. err = copy_from_user_policy_type(&xp->type, attrs);
  1127. if (err)
  1128. goto error;
  1129. if (!(err = copy_from_user_tmpl(xp, attrs)))
  1130. err = copy_from_user_sec_ctx(xp, attrs);
  1131. if (err)
  1132. goto error;
  1133. xfrm_mark_get(attrs, &xp->mark);
  1134. return xp;
  1135. error:
  1136. *errp = err;
  1137. xp->walk.dead = 1;
  1138. xfrm_policy_destroy(xp);
  1139. return NULL;
  1140. }
  1141. static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1142. struct nlattr **attrs)
  1143. {
  1144. struct net *net = sock_net(skb->sk);
  1145. struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
  1146. struct xfrm_policy *xp;
  1147. struct km_event c;
  1148. int err;
  1149. int excl;
  1150. kuid_t loginuid = audit_get_loginuid(current);
  1151. unsigned int sessionid = audit_get_sessionid(current);
  1152. u32 sid;
  1153. err = verify_newpolicy_info(p);
  1154. if (err)
  1155. return err;
  1156. err = verify_sec_ctx_len(attrs);
  1157. if (err)
  1158. return err;
  1159. xp = xfrm_policy_construct(net, p, attrs, &err);
  1160. if (!xp)
  1161. return err;
  1162. /* shouldn't excl be based on nlh flags??
  1163. * Aha! this is anti-netlink really i.e more pfkey derived
  1164. * in netlink excl is a flag and you wouldnt need
  1165. * a type XFRM_MSG_UPDPOLICY - JHS */
  1166. excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
  1167. err = xfrm_policy_insert(p->dir, xp, excl);
  1168. security_task_getsecid(current, &sid);
  1169. xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
  1170. if (err) {
  1171. security_xfrm_policy_free(xp->security);
  1172. kfree(xp);
  1173. return err;
  1174. }
  1175. c.event = nlh->nlmsg_type;
  1176. c.seq = nlh->nlmsg_seq;
  1177. c.portid = nlh->nlmsg_pid;
  1178. km_policy_notify(xp, p->dir, &c);
  1179. xfrm_pol_put(xp);
  1180. return 0;
  1181. }
  1182. static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
  1183. {
  1184. struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
  1185. int i;
  1186. if (xp->xfrm_nr == 0)
  1187. return 0;
  1188. for (i = 0; i < xp->xfrm_nr; i++) {
  1189. struct xfrm_user_tmpl *up = &vec[i];
  1190. struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
  1191. memset(up, 0, sizeof(*up));
  1192. memcpy(&up->id, &kp->id, sizeof(up->id));
  1193. up->family = kp->encap_family;
  1194. memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
  1195. up->reqid = kp->reqid;
  1196. up->mode = kp->mode;
  1197. up->share = kp->share;
  1198. up->optional = kp->optional;
  1199. up->aalgos = kp->aalgos;
  1200. up->ealgos = kp->ealgos;
  1201. up->calgos = kp->calgos;
  1202. }
  1203. return nla_put(skb, XFRMA_TMPL,
  1204. sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
  1205. }
  1206. static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
  1207. {
  1208. if (x->security) {
  1209. return copy_sec_ctx(x->security, skb);
  1210. }
  1211. return 0;
  1212. }
  1213. static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
  1214. {
  1215. if (xp->security)
  1216. return copy_sec_ctx(xp->security, skb);
  1217. return 0;
  1218. }
  1219. static inline size_t userpolicy_type_attrsize(void)
  1220. {
  1221. #ifdef CONFIG_XFRM_SUB_POLICY
  1222. return nla_total_size(sizeof(struct xfrm_userpolicy_type));
  1223. #else
  1224. return 0;
  1225. #endif
  1226. }
  1227. #ifdef CONFIG_XFRM_SUB_POLICY
  1228. static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1229. {
  1230. struct xfrm_userpolicy_type upt = {
  1231. .type = type,
  1232. };
  1233. return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
  1234. }
  1235. #else
  1236. static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1237. {
  1238. return 0;
  1239. }
  1240. #endif
  1241. static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
  1242. {
  1243. struct xfrm_dump_info *sp = ptr;
  1244. struct xfrm_userpolicy_info *p;
  1245. struct sk_buff *in_skb = sp->in_skb;
  1246. struct sk_buff *skb = sp->out_skb;
  1247. struct nlmsghdr *nlh;
  1248. int err;
  1249. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
  1250. XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
  1251. if (nlh == NULL)
  1252. return -EMSGSIZE;
  1253. p = nlmsg_data(nlh);
  1254. copy_to_user_policy(xp, p, dir);
  1255. err = copy_to_user_tmpl(xp, skb);
  1256. if (!err)
  1257. err = copy_to_user_sec_ctx(xp, skb);
  1258. if (!err)
  1259. err = copy_to_user_policy_type(xp->type, skb);
  1260. if (!err)
  1261. err = xfrm_mark_put(skb, &xp->mark);
  1262. if (err) {
  1263. nlmsg_cancel(skb, nlh);
  1264. return err;
  1265. }
  1266. nlmsg_end(skb, nlh);
  1267. return 0;
  1268. }
  1269. static int xfrm_dump_policy_done(struct netlink_callback *cb)
  1270. {
  1271. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
  1272. struct net *net = sock_net(cb->skb->sk);
  1273. xfrm_policy_walk_done(walk, net);
  1274. return 0;
  1275. }
  1276. static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
  1277. {
  1278. struct net *net = sock_net(skb->sk);
  1279. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
  1280. struct xfrm_dump_info info;
  1281. BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
  1282. sizeof(cb->args) - sizeof(cb->args[0]));
  1283. info.in_skb = cb->skb;
  1284. info.out_skb = skb;
  1285. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  1286. info.nlmsg_flags = NLM_F_MULTI;
  1287. if (!cb->args[0]) {
  1288. cb->args[0] = 1;
  1289. xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
  1290. }
  1291. (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
  1292. return skb->len;
  1293. }
  1294. static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
  1295. struct xfrm_policy *xp,
  1296. int dir, u32 seq)
  1297. {
  1298. struct xfrm_dump_info info;
  1299. struct sk_buff *skb;
  1300. int err;
  1301. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1302. if (!skb)
  1303. return ERR_PTR(-ENOMEM);
  1304. info.in_skb = in_skb;
  1305. info.out_skb = skb;
  1306. info.nlmsg_seq = seq;
  1307. info.nlmsg_flags = 0;
  1308. err = dump_one_policy(xp, dir, 0, &info);
  1309. if (err) {
  1310. kfree_skb(skb);
  1311. return ERR_PTR(err);
  1312. }
  1313. return skb;
  1314. }
  1315. static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1316. struct nlattr **attrs)
  1317. {
  1318. struct net *net = sock_net(skb->sk);
  1319. struct xfrm_policy *xp;
  1320. struct xfrm_userpolicy_id *p;
  1321. u8 type = XFRM_POLICY_TYPE_MAIN;
  1322. int err;
  1323. struct km_event c;
  1324. int delete;
  1325. struct xfrm_mark m;
  1326. u32 mark = xfrm_mark_get(attrs, &m);
  1327. p = nlmsg_data(nlh);
  1328. delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
  1329. err = copy_from_user_policy_type(&type, attrs);
  1330. if (err)
  1331. return err;
  1332. err = verify_policy_dir(p->dir);
  1333. if (err)
  1334. return err;
  1335. if (p->index)
  1336. xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
  1337. else {
  1338. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1339. struct xfrm_sec_ctx *ctx;
  1340. err = verify_sec_ctx_len(attrs);
  1341. if (err)
  1342. return err;
  1343. ctx = NULL;
  1344. if (rt) {
  1345. struct xfrm_user_sec_ctx *uctx = nla_data(rt);
  1346. err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
  1347. if (err)
  1348. return err;
  1349. }
  1350. xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
  1351. ctx, delete, &err);
  1352. security_xfrm_policy_free(ctx);
  1353. }
  1354. if (xp == NULL)
  1355. return -ENOENT;
  1356. if (!delete) {
  1357. struct sk_buff *resp_skb;
  1358. resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
  1359. if (IS_ERR(resp_skb)) {
  1360. err = PTR_ERR(resp_skb);
  1361. } else {
  1362. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
  1363. NETLINK_CB(skb).portid);
  1364. }
  1365. } else {
  1366. kuid_t loginuid = audit_get_loginuid(current);
  1367. unsigned int sessionid = audit_get_sessionid(current);
  1368. u32 sid;
  1369. security_task_getsecid(current, &sid);
  1370. xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
  1371. sid);
  1372. if (err != 0)
  1373. goto out;
  1374. c.data.byid = p->index;
  1375. c.event = nlh->nlmsg_type;
  1376. c.seq = nlh->nlmsg_seq;
  1377. c.portid = nlh->nlmsg_pid;
  1378. km_policy_notify(xp, p->dir, &c);
  1379. }
  1380. out:
  1381. xfrm_pol_put(xp);
  1382. if (delete && err == 0)
  1383. xfrm_garbage_collect(net);
  1384. return err;
  1385. }
  1386. static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  1387. struct nlattr **attrs)
  1388. {
  1389. struct net *net = sock_net(skb->sk);
  1390. struct km_event c;
  1391. struct xfrm_usersa_flush *p = nlmsg_data(nlh);
  1392. struct xfrm_audit audit_info;
  1393. int err;
  1394. audit_info.loginuid = audit_get_loginuid(current);
  1395. audit_info.sessionid = audit_get_sessionid(current);
  1396. security_task_getsecid(current, &audit_info.secid);
  1397. err = xfrm_state_flush(net, p->proto, &audit_info);
  1398. if (err) {
  1399. if (err == -ESRCH) /* empty table */
  1400. return 0;
  1401. return err;
  1402. }
  1403. c.data.proto = p->proto;
  1404. c.event = nlh->nlmsg_type;
  1405. c.seq = nlh->nlmsg_seq;
  1406. c.portid = nlh->nlmsg_pid;
  1407. c.net = net;
  1408. km_state_notify(NULL, &c);
  1409. return 0;
  1410. }
  1411. static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
  1412. {
  1413. size_t replay_size = x->replay_esn ?
  1414. xfrm_replay_state_esn_len(x->replay_esn) :
  1415. sizeof(struct xfrm_replay_state);
  1416. return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
  1417. + nla_total_size(replay_size)
  1418. + nla_total_size(sizeof(struct xfrm_lifetime_cur))
  1419. + nla_total_size(sizeof(struct xfrm_mark))
  1420. + nla_total_size(4) /* XFRM_AE_RTHR */
  1421. + nla_total_size(4); /* XFRM_AE_ETHR */
  1422. }
  1423. static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
  1424. {
  1425. struct xfrm_aevent_id *id;
  1426. struct nlmsghdr *nlh;
  1427. int err;
  1428. nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
  1429. if (nlh == NULL)
  1430. return -EMSGSIZE;
  1431. id = nlmsg_data(nlh);
  1432. memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
  1433. id->sa_id.spi = x->id.spi;
  1434. id->sa_id.family = x->props.family;
  1435. id->sa_id.proto = x->id.proto;
  1436. memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
  1437. id->reqid = x->props.reqid;
  1438. id->flags = c->data.aevent;
  1439. if (x->replay_esn) {
  1440. err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
  1441. xfrm_replay_state_esn_len(x->replay_esn),
  1442. x->replay_esn);
  1443. } else {
  1444. err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
  1445. &x->replay);
  1446. }
  1447. if (err)
  1448. goto out_cancel;
  1449. err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
  1450. if (err)
  1451. goto out_cancel;
  1452. if (id->flags & XFRM_AE_RTHR) {
  1453. err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
  1454. if (err)
  1455. goto out_cancel;
  1456. }
  1457. if (id->flags & XFRM_AE_ETHR) {
  1458. err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
  1459. x->replay_maxage * 10 / HZ);
  1460. if (err)
  1461. goto out_cancel;
  1462. }
  1463. err = xfrm_mark_put(skb, &x->mark);
  1464. if (err)
  1465. goto out_cancel;
  1466. return nlmsg_end(skb, nlh);
  1467. out_cancel:
  1468. nlmsg_cancel(skb, nlh);
  1469. return err;
  1470. }
  1471. static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1472. struct nlattr **attrs)
  1473. {
  1474. struct net *net = sock_net(skb->sk);
  1475. struct xfrm_state *x;
  1476. struct sk_buff *r_skb;
  1477. int err;
  1478. struct km_event c;
  1479. u32 mark;
  1480. struct xfrm_mark m;
  1481. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  1482. struct xfrm_usersa_id *id = &p->sa_id;
  1483. mark = xfrm_mark_get(attrs, &m);
  1484. x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
  1485. if (x == NULL)
  1486. return -ESRCH;
  1487. r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
  1488. if (r_skb == NULL) {
  1489. xfrm_state_put(x);
  1490. return -ENOMEM;
  1491. }
  1492. /*
  1493. * XXX: is this lock really needed - none of the other
  1494. * gets lock (the concern is things getting updated
  1495. * while we are still reading) - jhs
  1496. */
  1497. spin_lock_bh(&x->lock);
  1498. c.data.aevent = p->flags;
  1499. c.seq = nlh->nlmsg_seq;
  1500. c.portid = nlh->nlmsg_pid;
  1501. if (build_aevent(r_skb, x, &c) < 0)
  1502. BUG();
  1503. err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
  1504. spin_unlock_bh(&x->lock);
  1505. xfrm_state_put(x);
  1506. return err;
  1507. }
  1508. static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1509. struct nlattr **attrs)
  1510. {
  1511. struct net *net = sock_net(skb->sk);
  1512. struct xfrm_state *x;
  1513. struct km_event c;
  1514. int err = -EINVAL;
  1515. u32 mark = 0;
  1516. struct xfrm_mark m;
  1517. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  1518. struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
  1519. struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
  1520. struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
  1521. if (!lt && !rp && !re)
  1522. return err;
  1523. /* pedantic mode - thou shalt sayeth replaceth */
  1524. if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
  1525. return err;
  1526. mark = xfrm_mark_get(attrs, &m);
  1527. x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
  1528. if (x == NULL)
  1529. return -ESRCH;
  1530. if (x->km.state != XFRM_STATE_VALID)
  1531. goto out;
  1532. err = xfrm_replay_verify_len(x->replay_esn, re);
  1533. if (err)
  1534. goto out;
  1535. spin_lock_bh(&x->lock);
  1536. xfrm_update_ae_params(x, attrs, 1);
  1537. spin_unlock_bh(&x->lock);
  1538. c.event = nlh->nlmsg_type;
  1539. c.seq = nlh->nlmsg_seq;
  1540. c.portid = nlh->nlmsg_pid;
  1541. c.data.aevent = XFRM_AE_CU;
  1542. km_state_notify(x, &c);
  1543. err = 0;
  1544. out:
  1545. xfrm_state_put(x);
  1546. return err;
  1547. }
  1548. static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1549. struct nlattr **attrs)
  1550. {
  1551. struct net *net = sock_net(skb->sk);
  1552. struct km_event c;
  1553. u8 type = XFRM_POLICY_TYPE_MAIN;
  1554. int err;
  1555. struct xfrm_audit audit_info;
  1556. err = copy_from_user_policy_type(&type, attrs);
  1557. if (err)
  1558. return err;
  1559. audit_info.loginuid = audit_get_loginuid(current);
  1560. audit_info.sessionid = audit_get_sessionid(current);
  1561. security_task_getsecid(current, &audit_info.secid);
  1562. err = xfrm_policy_flush(net, type, &audit_info);
  1563. if (err) {
  1564. if (err == -ESRCH) /* empty table */
  1565. return 0;
  1566. return err;
  1567. }
  1568. c.data.type = type;
  1569. c.event = nlh->nlmsg_type;
  1570. c.seq = nlh->nlmsg_seq;
  1571. c.portid = nlh->nlmsg_pid;
  1572. c.net = net;
  1573. km_policy_notify(NULL, 0, &c);
  1574. return 0;
  1575. }
  1576. static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1577. struct nlattr **attrs)
  1578. {
  1579. struct net *net = sock_net(skb->sk);
  1580. struct xfrm_policy *xp;
  1581. struct xfrm_user_polexpire *up = nlmsg_data(nlh);
  1582. struct xfrm_userpolicy_info *p = &up->pol;
  1583. u8 type = XFRM_POLICY_TYPE_MAIN;
  1584. int err = -ENOENT;
  1585. struct xfrm_mark m;
  1586. u32 mark = xfrm_mark_get(attrs, &m);
  1587. err = copy_from_user_policy_type(&type, attrs);
  1588. if (err)
  1589. return err;
  1590. err = verify_policy_dir(p->dir);
  1591. if (err)
  1592. return err;
  1593. if (p->index)
  1594. xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
  1595. else {
  1596. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1597. struct xfrm_sec_ctx *ctx;
  1598. err = verify_sec_ctx_len(attrs);
  1599. if (err)
  1600. return err;
  1601. ctx = NULL;
  1602. if (rt) {
  1603. struct xfrm_user_sec_ctx *uctx = nla_data(rt);
  1604. err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
  1605. if (err)
  1606. return err;
  1607. }
  1608. xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
  1609. &p->sel, ctx, 0, &err);
  1610. security_xfrm_policy_free(ctx);
  1611. }
  1612. if (xp == NULL)
  1613. return -ENOENT;
  1614. if (unlikely(xp->walk.dead))
  1615. goto out;
  1616. err = 0;
  1617. if (up->hard) {
  1618. kuid_t loginuid = audit_get_loginuid(current);
  1619. unsigned int sessionid = audit_get_sessionid(current);
  1620. u32 sid;
  1621. security_task_getsecid(current, &sid);
  1622. xfrm_policy_delete(xp, p->dir);
  1623. xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
  1624. } else {
  1625. // reset the timers here?
  1626. WARN(1, "Dont know what to do with soft policy expire\n");
  1627. }
  1628. km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
  1629. out:
  1630. xfrm_pol_put(xp);
  1631. return err;
  1632. }
  1633. static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1634. struct nlattr **attrs)
  1635. {
  1636. struct net *net = sock_net(skb->sk);
  1637. struct xfrm_state *x;
  1638. int err;
  1639. struct xfrm_user_expire *ue = nlmsg_data(nlh);
  1640. struct xfrm_usersa_info *p = &ue->state;
  1641. struct xfrm_mark m;
  1642. u32 mark = xfrm_mark_get(attrs, &m);
  1643. x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
  1644. err = -ENOENT;
  1645. if (x == NULL)
  1646. return err;
  1647. spin_lock_bh(&x->lock);
  1648. err = -EINVAL;
  1649. if (x->km.state != XFRM_STATE_VALID)
  1650. goto out;
  1651. km_state_expired(x, ue->hard, nlh->nlmsg_pid);
  1652. if (ue->hard) {
  1653. kuid_t loginuid = audit_get_loginuid(current);
  1654. unsigned int sessionid = audit_get_sessionid(current);
  1655. u32 sid;
  1656. security_task_getsecid(current, &sid);
  1657. __xfrm_state_delete(x);
  1658. xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
  1659. }
  1660. err = 0;
  1661. out:
  1662. spin_unlock_bh(&x->lock);
  1663. xfrm_state_put(x);
  1664. return err;
  1665. }
  1666. static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1667. struct nlattr **attrs)
  1668. {
  1669. struct net *net = sock_net(skb->sk);
  1670. struct xfrm_policy *xp;
  1671. struct xfrm_user_tmpl *ut;
  1672. int i;
  1673. struct nlattr *rt = attrs[XFRMA_TMPL];
  1674. struct xfrm_mark mark;
  1675. struct xfrm_user_acquire *ua = nlmsg_data(nlh);
  1676. struct xfrm_state *x = xfrm_state_alloc(net);
  1677. int err = -ENOMEM;
  1678. if (!x)
  1679. goto nomem;
  1680. xfrm_mark_get(attrs, &mark);
  1681. err = verify_newpolicy_info(&ua->policy);
  1682. if (err)
  1683. goto bad_policy;
  1684. /* build an XP */
  1685. xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
  1686. if (!xp)
  1687. goto free_state;
  1688. memcpy(&x->id, &ua->id, sizeof(ua->id));
  1689. memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
  1690. memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
  1691. xp->mark.m = x->mark.m = mark.m;
  1692. xp->mark.v = x->mark.v = mark.v;
  1693. ut = nla_data(rt);
  1694. /* extract the templates and for each call km_key */
  1695. for (i = 0; i < xp->xfrm_nr; i++, ut++) {
  1696. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  1697. memcpy(&x->id, &t->id, sizeof(x->id));
  1698. x->props.mode = t->mode;
  1699. x->props.reqid = t->reqid;
  1700. x->props.family = ut->family;
  1701. t->aalgos = ua->aalgos;
  1702. t->ealgos = ua->ealgos;
  1703. t->calgos = ua->calgos;
  1704. err = km_query(x, t, xp);
  1705. }
  1706. kfree(x);
  1707. kfree(xp);
  1708. return 0;
  1709. bad_policy:
  1710. WARN(1, "BAD policy passed\n");
  1711. free_state:
  1712. kfree(x);
  1713. nomem:
  1714. return err;
  1715. }
  1716. #ifdef CONFIG_XFRM_MIGRATE
  1717. static int copy_from_user_migrate(struct xfrm_migrate *ma,
  1718. struct xfrm_kmaddress *k,
  1719. struct nlattr **attrs, int *num)
  1720. {
  1721. struct nlattr *rt = attrs[XFRMA_MIGRATE];
  1722. struct xfrm_user_migrate *um;
  1723. int i, num_migrate;
  1724. if (k != NULL) {
  1725. struct xfrm_user_kmaddress *uk;
  1726. uk = nla_data(attrs[XFRMA_KMADDRESS]);
  1727. memcpy(&k->local, &uk->local, sizeof(k->local));
  1728. memcpy(&k->remote, &uk->remote, sizeof(k->remote));
  1729. k->family = uk->family;
  1730. k->reserved = uk->reserved;
  1731. }
  1732. um = nla_data(rt);
  1733. num_migrate = nla_len(rt) / sizeof(*um);
  1734. if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
  1735. return -EINVAL;
  1736. for (i = 0; i < num_migrate; i++, um++, ma++) {
  1737. memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
  1738. memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
  1739. memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
  1740. memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
  1741. ma->proto = um->proto;
  1742. ma->mode = um->mode;
  1743. ma->reqid = um->reqid;
  1744. ma->old_family = um->old_family;
  1745. ma->new_family = um->new_family;
  1746. }
  1747. *num = i;
  1748. return 0;
  1749. }
  1750. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1751. struct nlattr **attrs)
  1752. {
  1753. struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
  1754. struct xfrm_migrate m[XFRM_MAX_DEPTH];
  1755. struct xfrm_kmaddress km, *kmp;
  1756. u8 type;
  1757. int err;
  1758. int n = 0;
  1759. struct net *net = sock_net(skb->sk);
  1760. if (attrs[XFRMA_MIGRATE] == NULL)
  1761. return -EINVAL;
  1762. kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
  1763. err = copy_from_user_policy_type(&type, attrs);
  1764. if (err)
  1765. return err;
  1766. err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
  1767. if (err)
  1768. return err;
  1769. if (!n)
  1770. return 0;
  1771. xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net);
  1772. return 0;
  1773. }
  1774. #else
  1775. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1776. struct nlattr **attrs)
  1777. {
  1778. return -ENOPROTOOPT;
  1779. }
  1780. #endif
  1781. #ifdef CONFIG_XFRM_MIGRATE
  1782. static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
  1783. {
  1784. struct xfrm_user_migrate um;
  1785. memset(&um, 0, sizeof(um));
  1786. um.proto = m->proto;
  1787. um.mode = m->mode;
  1788. um.reqid = m->reqid;
  1789. um.old_family = m->old_family;
  1790. memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
  1791. memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
  1792. um.new_family = m->new_family;
  1793. memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
  1794. memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
  1795. return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
  1796. }
  1797. static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
  1798. {
  1799. struct xfrm_user_kmaddress uk;
  1800. memset(&uk, 0, sizeof(uk));
  1801. uk.family = k->family;
  1802. uk.reserved = k->reserved;
  1803. memcpy(&uk.local, &k->local, sizeof(uk.local));
  1804. memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
  1805. return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
  1806. }
  1807. static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
  1808. {
  1809. return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
  1810. + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
  1811. + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
  1812. + userpolicy_type_attrsize();
  1813. }
  1814. static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
  1815. int num_migrate, const struct xfrm_kmaddress *k,
  1816. const struct xfrm_selector *sel, u8 dir, u8 type)
  1817. {
  1818. const struct xfrm_migrate *mp;
  1819. struct xfrm_userpolicy_id *pol_id;
  1820. struct nlmsghdr *nlh;
  1821. int i, err;
  1822. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
  1823. if (nlh == NULL)
  1824. return -EMSGSIZE;
  1825. pol_id = nlmsg_data(nlh);
  1826. /* copy data from selector, dir, and type to the pol_id */
  1827. memset(pol_id, 0, sizeof(*pol_id));
  1828. memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
  1829. pol_id->dir = dir;
  1830. if (k != NULL) {
  1831. err = copy_to_user_kmaddress(k, skb);
  1832. if (err)
  1833. goto out_cancel;
  1834. }
  1835. err = copy_to_user_policy_type(type, skb);
  1836. if (err)
  1837. goto out_cancel;
  1838. for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
  1839. err = copy_to_user_migrate(mp, skb);
  1840. if (err)
  1841. goto out_cancel;
  1842. }
  1843. return nlmsg_end(skb, nlh);
  1844. out_cancel:
  1845. nlmsg_cancel(skb, nlh);
  1846. return err;
  1847. }
  1848. static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  1849. const struct xfrm_migrate *m, int num_migrate,
  1850. const struct xfrm_kmaddress *k)
  1851. {
  1852. struct net *net = &init_net;
  1853. struct sk_buff *skb;
  1854. skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
  1855. if (skb == NULL)
  1856. return -ENOMEM;
  1857. /* build migrate */
  1858. if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
  1859. BUG();
  1860. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
  1861. }
  1862. #else
  1863. static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  1864. const struct xfrm_migrate *m, int num_migrate,
  1865. const struct xfrm_kmaddress *k)
  1866. {
  1867. return -ENOPROTOOPT;
  1868. }
  1869. #endif
  1870. #define XMSGSIZE(type) sizeof(struct type)
  1871. static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
  1872. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1873. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1874. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1875. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1876. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1877. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1878. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
  1879. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
  1880. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
  1881. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1882. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1883. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
  1884. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  1885. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
  1886. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1887. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1888. [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
  1889. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1890. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
  1891. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
  1892. };
  1893. #undef XMSGSIZE
  1894. static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
  1895. [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
  1896. [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
  1897. [XFRMA_LASTUSED] = { .type = NLA_U64},
  1898. [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
  1899. [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
  1900. [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
  1901. [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
  1902. [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
  1903. [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
  1904. [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
  1905. [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
  1906. [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
  1907. [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
  1908. [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
  1909. [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
  1910. [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
  1911. [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
  1912. [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
  1913. [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
  1914. [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
  1915. [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
  1916. [XFRMA_TFCPAD] = { .type = NLA_U32 },
  1917. [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
  1918. [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
  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 (!ns_capable(net->user_ns, 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 nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  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 nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
  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 nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  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 nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  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 nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
  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 nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  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 nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  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 nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  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 nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
  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 nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
  2468. }
  2469. static struct xfrm_mgr netlink_mgr = {
  2470. .id = "netlink",
  2471. .notify = xfrm_send_state_notify,
  2472. .acquire = xfrm_send_acquire,
  2473. .compile_policy = xfrm_compile_policy,
  2474. .notify_policy = xfrm_send_policy_notify,
  2475. .report = xfrm_send_report,
  2476. .migrate = xfrm_send_migrate,
  2477. .new_mapping = xfrm_send_mapping,
  2478. };
  2479. static int __net_init xfrm_user_net_init(struct net *net)
  2480. {
  2481. struct sock *nlsk;
  2482. struct netlink_kernel_cfg cfg = {
  2483. .groups = XFRMNLGRP_MAX,
  2484. .input = xfrm_netlink_rcv,
  2485. };
  2486. nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
  2487. if (nlsk == NULL)
  2488. return -ENOMEM;
  2489. net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
  2490. rcu_assign_pointer(net->xfrm.nlsk, nlsk);
  2491. return 0;
  2492. }
  2493. static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
  2494. {
  2495. struct net *net;
  2496. list_for_each_entry(net, net_exit_list, exit_list)
  2497. RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
  2498. synchronize_net();
  2499. list_for_each_entry(net, net_exit_list, exit_list)
  2500. netlink_kernel_release(net->xfrm.nlsk_stash);
  2501. }
  2502. static struct pernet_operations xfrm_user_net_ops = {
  2503. .init = xfrm_user_net_init,
  2504. .exit_batch = xfrm_user_net_exit,
  2505. };
  2506. static int __init xfrm_user_init(void)
  2507. {
  2508. int rv;
  2509. printk(KERN_INFO "Initializing XFRM netlink socket\n");
  2510. rv = register_pernet_subsys(&xfrm_user_net_ops);
  2511. if (rv < 0)
  2512. return rv;
  2513. rv = xfrm_register_km(&netlink_mgr);
  2514. if (rv < 0)
  2515. unregister_pernet_subsys(&xfrm_user_net_ops);
  2516. return rv;
  2517. }
  2518. static void __exit xfrm_user_exit(void)
  2519. {
  2520. xfrm_unregister_km(&netlink_mgr);
  2521. unregister_pernet_subsys(&xfrm_user_net_ops);
  2522. }
  2523. module_init(xfrm_user_init);
  2524. module_exit(xfrm_user_exit);
  2525. MODULE_LICENSE("GPL");
  2526. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);