xfrm_user.c 69 KB

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