xfrm_user.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019
  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. int ret = 0;
  620. copy_to_user_state(x, p);
  621. if (x->coaddr) {
  622. ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
  623. if (ret)
  624. goto out;
  625. }
  626. if (x->lastused) {
  627. ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
  628. if (ret)
  629. goto out;
  630. }
  631. if (x->aead) {
  632. ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
  633. if (ret)
  634. goto out;
  635. }
  636. if (x->aalg) {
  637. ret = copy_to_user_auth(x->aalg, skb);
  638. if (!ret)
  639. ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
  640. xfrm_alg_auth_len(x->aalg), x->aalg);
  641. if (ret)
  642. goto out;
  643. }
  644. if (x->ealg) {
  645. ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
  646. if (ret)
  647. goto out;
  648. }
  649. if (x->calg) {
  650. ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  651. if (ret)
  652. goto out;
  653. }
  654. if (x->encap) {
  655. ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  656. if (ret)
  657. goto out;
  658. }
  659. if (x->tfcpad) {
  660. ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
  661. if (ret)
  662. goto out;
  663. }
  664. ret = xfrm_mark_put(skb, &x->mark);
  665. if (ret)
  666. goto out;
  667. if (x->replay_esn) {
  668. ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
  669. xfrm_replay_state_esn_len(x->replay_esn),
  670. x->replay_esn);
  671. if (ret)
  672. goto out;
  673. }
  674. if (x->security)
  675. ret = copy_sec_ctx(x->security, skb);
  676. out:
  677. return ret;
  678. }
  679. static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
  680. {
  681. struct xfrm_dump_info *sp = ptr;
  682. struct sk_buff *in_skb = sp->in_skb;
  683. struct sk_buff *skb = sp->out_skb;
  684. struct xfrm_usersa_info *p;
  685. struct nlmsghdr *nlh;
  686. int err;
  687. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
  688. XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
  689. if (nlh == NULL)
  690. return -EMSGSIZE;
  691. p = nlmsg_data(nlh);
  692. err = copy_to_user_state_extra(x, p, skb);
  693. if (err) {
  694. nlmsg_cancel(skb, nlh);
  695. return err;
  696. }
  697. nlmsg_end(skb, nlh);
  698. return 0;
  699. }
  700. static int xfrm_dump_sa_done(struct netlink_callback *cb)
  701. {
  702. struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
  703. xfrm_state_walk_done(walk);
  704. return 0;
  705. }
  706. static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
  707. {
  708. struct net *net = sock_net(skb->sk);
  709. struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
  710. struct xfrm_dump_info info;
  711. BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
  712. sizeof(cb->args) - sizeof(cb->args[0]));
  713. info.in_skb = cb->skb;
  714. info.out_skb = skb;
  715. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  716. info.nlmsg_flags = NLM_F_MULTI;
  717. if (!cb->args[0]) {
  718. cb->args[0] = 1;
  719. xfrm_state_walk_init(walk, 0);
  720. }
  721. (void) xfrm_state_walk(net, walk, dump_one_state, &info);
  722. return skb->len;
  723. }
  724. static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
  725. struct xfrm_state *x, u32 seq)
  726. {
  727. struct xfrm_dump_info info;
  728. struct sk_buff *skb;
  729. int err;
  730. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  731. if (!skb)
  732. return ERR_PTR(-ENOMEM);
  733. info.in_skb = in_skb;
  734. info.out_skb = skb;
  735. info.nlmsg_seq = seq;
  736. info.nlmsg_flags = 0;
  737. err = dump_one_state(x, 0, &info);
  738. if (err) {
  739. kfree_skb(skb);
  740. return ERR_PTR(err);
  741. }
  742. return skb;
  743. }
  744. static inline size_t xfrm_spdinfo_msgsize(void)
  745. {
  746. return NLMSG_ALIGN(4)
  747. + nla_total_size(sizeof(struct xfrmu_spdinfo))
  748. + nla_total_size(sizeof(struct xfrmu_spdhinfo));
  749. }
  750. static int build_spdinfo(struct sk_buff *skb, struct net *net,
  751. u32 pid, u32 seq, u32 flags)
  752. {
  753. struct xfrmk_spdinfo si;
  754. struct xfrmu_spdinfo spc;
  755. struct xfrmu_spdhinfo sph;
  756. struct nlmsghdr *nlh;
  757. int err;
  758. u32 *f;
  759. nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
  760. if (nlh == NULL) /* shouldn't really happen ... */
  761. return -EMSGSIZE;
  762. f = nlmsg_data(nlh);
  763. *f = flags;
  764. xfrm_spd_getinfo(net, &si);
  765. spc.incnt = si.incnt;
  766. spc.outcnt = si.outcnt;
  767. spc.fwdcnt = si.fwdcnt;
  768. spc.inscnt = si.inscnt;
  769. spc.outscnt = si.outscnt;
  770. spc.fwdscnt = si.fwdscnt;
  771. sph.spdhcnt = si.spdhcnt;
  772. sph.spdhmcnt = si.spdhmcnt;
  773. err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
  774. if (!err)
  775. err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
  776. if (err) {
  777. nlmsg_cancel(skb, nlh);
  778. return err;
  779. }
  780. return nlmsg_end(skb, nlh);
  781. }
  782. static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  783. struct nlattr **attrs)
  784. {
  785. struct net *net = sock_net(skb->sk);
  786. struct sk_buff *r_skb;
  787. u32 *flags = nlmsg_data(nlh);
  788. u32 spid = NETLINK_CB(skb).pid;
  789. u32 seq = nlh->nlmsg_seq;
  790. r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
  791. if (r_skb == NULL)
  792. return -ENOMEM;
  793. if (build_spdinfo(r_skb, net, spid, seq, *flags) < 0)
  794. BUG();
  795. return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
  796. }
  797. static inline size_t xfrm_sadinfo_msgsize(void)
  798. {
  799. return NLMSG_ALIGN(4)
  800. + nla_total_size(sizeof(struct xfrmu_sadhinfo))
  801. + nla_total_size(4); /* XFRMA_SAD_CNT */
  802. }
  803. static int build_sadinfo(struct sk_buff *skb, struct net *net,
  804. u32 pid, u32 seq, u32 flags)
  805. {
  806. struct xfrmk_sadinfo si;
  807. struct xfrmu_sadhinfo sh;
  808. struct nlmsghdr *nlh;
  809. int err;
  810. u32 *f;
  811. nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
  812. if (nlh == NULL) /* shouldn't really happen ... */
  813. return -EMSGSIZE;
  814. f = nlmsg_data(nlh);
  815. *f = flags;
  816. xfrm_sad_getinfo(net, &si);
  817. sh.sadhmcnt = si.sadhmcnt;
  818. sh.sadhcnt = si.sadhcnt;
  819. err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
  820. if (!err)
  821. err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
  822. if (err) {
  823. nlmsg_cancel(skb, nlh);
  824. return err;
  825. }
  826. return nlmsg_end(skb, nlh);
  827. }
  828. static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  829. struct nlattr **attrs)
  830. {
  831. struct net *net = sock_net(skb->sk);
  832. struct sk_buff *r_skb;
  833. u32 *flags = nlmsg_data(nlh);
  834. u32 spid = NETLINK_CB(skb).pid;
  835. u32 seq = nlh->nlmsg_seq;
  836. r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
  837. if (r_skb == NULL)
  838. return -ENOMEM;
  839. if (build_sadinfo(r_skb, net, spid, seq, *flags) < 0)
  840. BUG();
  841. return nlmsg_unicast(net->xfrm.nlsk, r_skb, spid);
  842. }
  843. static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  844. struct nlattr **attrs)
  845. {
  846. struct net *net = sock_net(skb->sk);
  847. struct xfrm_usersa_id *p = nlmsg_data(nlh);
  848. struct xfrm_state *x;
  849. struct sk_buff *resp_skb;
  850. int err = -ESRCH;
  851. x = xfrm_user_state_lookup(net, p, attrs, &err);
  852. if (x == NULL)
  853. goto out_noput;
  854. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  855. if (IS_ERR(resp_skb)) {
  856. err = PTR_ERR(resp_skb);
  857. } else {
  858. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
  859. }
  860. xfrm_state_put(x);
  861. out_noput:
  862. return err;
  863. }
  864. static int verify_userspi_info(struct xfrm_userspi_info *p)
  865. {
  866. switch (p->info.id.proto) {
  867. case IPPROTO_AH:
  868. case IPPROTO_ESP:
  869. break;
  870. case IPPROTO_COMP:
  871. /* IPCOMP spi is 16-bits. */
  872. if (p->max >= 0x10000)
  873. return -EINVAL;
  874. break;
  875. default:
  876. return -EINVAL;
  877. }
  878. if (p->min > p->max)
  879. return -EINVAL;
  880. return 0;
  881. }
  882. static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
  883. struct nlattr **attrs)
  884. {
  885. struct net *net = sock_net(skb->sk);
  886. struct xfrm_state *x;
  887. struct xfrm_userspi_info *p;
  888. struct sk_buff *resp_skb;
  889. xfrm_address_t *daddr;
  890. int family;
  891. int err;
  892. u32 mark;
  893. struct xfrm_mark m;
  894. p = nlmsg_data(nlh);
  895. err = verify_userspi_info(p);
  896. if (err)
  897. goto out_noput;
  898. family = p->info.family;
  899. daddr = &p->info.id.daddr;
  900. x = NULL;
  901. mark = xfrm_mark_get(attrs, &m);
  902. if (p->info.seq) {
  903. x = xfrm_find_acq_byseq(net, mark, p->info.seq);
  904. if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
  905. xfrm_state_put(x);
  906. x = NULL;
  907. }
  908. }
  909. if (!x)
  910. x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
  911. p->info.id.proto, daddr,
  912. &p->info.saddr, 1,
  913. family);
  914. err = -ENOENT;
  915. if (x == NULL)
  916. goto out_noput;
  917. err = xfrm_alloc_spi(x, p->min, p->max);
  918. if (err)
  919. goto out;
  920. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  921. if (IS_ERR(resp_skb)) {
  922. err = PTR_ERR(resp_skb);
  923. goto out;
  924. }
  925. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).pid);
  926. out:
  927. xfrm_state_put(x);
  928. out_noput:
  929. return err;
  930. }
  931. static int verify_policy_dir(u8 dir)
  932. {
  933. switch (dir) {
  934. case XFRM_POLICY_IN:
  935. case XFRM_POLICY_OUT:
  936. case XFRM_POLICY_FWD:
  937. break;
  938. default:
  939. return -EINVAL;
  940. }
  941. return 0;
  942. }
  943. static int verify_policy_type(u8 type)
  944. {
  945. switch (type) {
  946. case XFRM_POLICY_TYPE_MAIN:
  947. #ifdef CONFIG_XFRM_SUB_POLICY
  948. case XFRM_POLICY_TYPE_SUB:
  949. #endif
  950. break;
  951. default:
  952. return -EINVAL;
  953. }
  954. return 0;
  955. }
  956. static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
  957. {
  958. switch (p->share) {
  959. case XFRM_SHARE_ANY:
  960. case XFRM_SHARE_SESSION:
  961. case XFRM_SHARE_USER:
  962. case XFRM_SHARE_UNIQUE:
  963. break;
  964. default:
  965. return -EINVAL;
  966. }
  967. switch (p->action) {
  968. case XFRM_POLICY_ALLOW:
  969. case XFRM_POLICY_BLOCK:
  970. break;
  971. default:
  972. return -EINVAL;
  973. }
  974. switch (p->sel.family) {
  975. case AF_INET:
  976. break;
  977. case AF_INET6:
  978. #if IS_ENABLED(CONFIG_IPV6)
  979. break;
  980. #else
  981. return -EAFNOSUPPORT;
  982. #endif
  983. default:
  984. return -EINVAL;
  985. }
  986. return verify_policy_dir(p->dir);
  987. }
  988. static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
  989. {
  990. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  991. struct xfrm_user_sec_ctx *uctx;
  992. if (!rt)
  993. return 0;
  994. uctx = nla_data(rt);
  995. return security_xfrm_policy_alloc(&pol->security, uctx);
  996. }
  997. static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
  998. int nr)
  999. {
  1000. int i;
  1001. xp->xfrm_nr = nr;
  1002. for (i = 0; i < nr; i++, ut++) {
  1003. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  1004. memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
  1005. memcpy(&t->saddr, &ut->saddr,
  1006. sizeof(xfrm_address_t));
  1007. t->reqid = ut->reqid;
  1008. t->mode = ut->mode;
  1009. t->share = ut->share;
  1010. t->optional = ut->optional;
  1011. t->aalgos = ut->aalgos;
  1012. t->ealgos = ut->ealgos;
  1013. t->calgos = ut->calgos;
  1014. /* If all masks are ~0, then we allow all algorithms. */
  1015. t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
  1016. t->encap_family = ut->family;
  1017. }
  1018. }
  1019. static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
  1020. {
  1021. int i;
  1022. if (nr > XFRM_MAX_DEPTH)
  1023. return -EINVAL;
  1024. for (i = 0; i < nr; i++) {
  1025. /* We never validated the ut->family value, so many
  1026. * applications simply leave it at zero. The check was
  1027. * never made and ut->family was ignored because all
  1028. * templates could be assumed to have the same family as
  1029. * the policy itself. Now that we will have ipv4-in-ipv6
  1030. * and ipv6-in-ipv4 tunnels, this is no longer true.
  1031. */
  1032. if (!ut[i].family)
  1033. ut[i].family = family;
  1034. switch (ut[i].family) {
  1035. case AF_INET:
  1036. break;
  1037. #if IS_ENABLED(CONFIG_IPV6)
  1038. case AF_INET6:
  1039. break;
  1040. #endif
  1041. default:
  1042. return -EINVAL;
  1043. }
  1044. }
  1045. return 0;
  1046. }
  1047. static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
  1048. {
  1049. struct nlattr *rt = attrs[XFRMA_TMPL];
  1050. if (!rt) {
  1051. pol->xfrm_nr = 0;
  1052. } else {
  1053. struct xfrm_user_tmpl *utmpl = nla_data(rt);
  1054. int nr = nla_len(rt) / sizeof(*utmpl);
  1055. int err;
  1056. err = validate_tmpl(nr, utmpl, pol->family);
  1057. if (err)
  1058. return err;
  1059. copy_templates(pol, utmpl, nr);
  1060. }
  1061. return 0;
  1062. }
  1063. static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
  1064. {
  1065. struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
  1066. struct xfrm_userpolicy_type *upt;
  1067. u8 type = XFRM_POLICY_TYPE_MAIN;
  1068. int err;
  1069. if (rt) {
  1070. upt = nla_data(rt);
  1071. type = upt->type;
  1072. }
  1073. err = verify_policy_type(type);
  1074. if (err)
  1075. return err;
  1076. *tp = type;
  1077. return 0;
  1078. }
  1079. static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
  1080. {
  1081. xp->priority = p->priority;
  1082. xp->index = p->index;
  1083. memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
  1084. memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
  1085. xp->action = p->action;
  1086. xp->flags = p->flags;
  1087. xp->family = p->sel.family;
  1088. /* XXX xp->share = p->share; */
  1089. }
  1090. static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
  1091. {
  1092. memcpy(&p->sel, &xp->selector, sizeof(p->sel));
  1093. memcpy(&p->lft, &xp->lft, sizeof(p->lft));
  1094. memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
  1095. p->priority = xp->priority;
  1096. p->index = xp->index;
  1097. p->sel.family = xp->family;
  1098. p->dir = dir;
  1099. p->action = xp->action;
  1100. p->flags = xp->flags;
  1101. p->share = XFRM_SHARE_ANY; /* XXX xp->share */
  1102. }
  1103. static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
  1104. {
  1105. struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
  1106. int err;
  1107. if (!xp) {
  1108. *errp = -ENOMEM;
  1109. return NULL;
  1110. }
  1111. copy_from_user_policy(xp, p);
  1112. err = copy_from_user_policy_type(&xp->type, attrs);
  1113. if (err)
  1114. goto error;
  1115. if (!(err = copy_from_user_tmpl(xp, attrs)))
  1116. err = copy_from_user_sec_ctx(xp, attrs);
  1117. if (err)
  1118. goto error;
  1119. xfrm_mark_get(attrs, &xp->mark);
  1120. return xp;
  1121. error:
  1122. *errp = err;
  1123. xp->walk.dead = 1;
  1124. xfrm_policy_destroy(xp);
  1125. return NULL;
  1126. }
  1127. static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1128. struct nlattr **attrs)
  1129. {
  1130. struct net *net = sock_net(skb->sk);
  1131. struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
  1132. struct xfrm_policy *xp;
  1133. struct km_event c;
  1134. int err;
  1135. int excl;
  1136. uid_t loginuid = audit_get_loginuid(current);
  1137. u32 sessionid = audit_get_sessionid(current);
  1138. u32 sid;
  1139. err = verify_newpolicy_info(p);
  1140. if (err)
  1141. return err;
  1142. err = verify_sec_ctx_len(attrs);
  1143. if (err)
  1144. return err;
  1145. xp = xfrm_policy_construct(net, p, attrs, &err);
  1146. if (!xp)
  1147. return err;
  1148. /* shouldn't excl be based on nlh flags??
  1149. * Aha! this is anti-netlink really i.e more pfkey derived
  1150. * in netlink excl is a flag and you wouldnt need
  1151. * a type XFRM_MSG_UPDPOLICY - JHS */
  1152. excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
  1153. err = xfrm_policy_insert(p->dir, xp, excl);
  1154. security_task_getsecid(current, &sid);
  1155. xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
  1156. if (err) {
  1157. security_xfrm_policy_free(xp->security);
  1158. kfree(xp);
  1159. return err;
  1160. }
  1161. c.event = nlh->nlmsg_type;
  1162. c.seq = nlh->nlmsg_seq;
  1163. c.pid = nlh->nlmsg_pid;
  1164. km_policy_notify(xp, p->dir, &c);
  1165. xfrm_pol_put(xp);
  1166. return 0;
  1167. }
  1168. static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
  1169. {
  1170. struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
  1171. int i;
  1172. if (xp->xfrm_nr == 0)
  1173. return 0;
  1174. for (i = 0; i < xp->xfrm_nr; i++) {
  1175. struct xfrm_user_tmpl *up = &vec[i];
  1176. struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
  1177. memcpy(&up->id, &kp->id, sizeof(up->id));
  1178. up->family = kp->encap_family;
  1179. memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
  1180. up->reqid = kp->reqid;
  1181. up->mode = kp->mode;
  1182. up->share = kp->share;
  1183. up->optional = kp->optional;
  1184. up->aalgos = kp->aalgos;
  1185. up->ealgos = kp->ealgos;
  1186. up->calgos = kp->calgos;
  1187. }
  1188. return nla_put(skb, XFRMA_TMPL,
  1189. sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
  1190. }
  1191. static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
  1192. {
  1193. if (x->security) {
  1194. return copy_sec_ctx(x->security, skb);
  1195. }
  1196. return 0;
  1197. }
  1198. static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
  1199. {
  1200. if (xp->security)
  1201. return copy_sec_ctx(xp->security, skb);
  1202. return 0;
  1203. }
  1204. static inline size_t userpolicy_type_attrsize(void)
  1205. {
  1206. #ifdef CONFIG_XFRM_SUB_POLICY
  1207. return nla_total_size(sizeof(struct xfrm_userpolicy_type));
  1208. #else
  1209. return 0;
  1210. #endif
  1211. }
  1212. #ifdef CONFIG_XFRM_SUB_POLICY
  1213. static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1214. {
  1215. struct xfrm_userpolicy_type upt = {
  1216. .type = type,
  1217. };
  1218. return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
  1219. }
  1220. #else
  1221. static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1222. {
  1223. return 0;
  1224. }
  1225. #endif
  1226. static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
  1227. {
  1228. struct xfrm_dump_info *sp = ptr;
  1229. struct xfrm_userpolicy_info *p;
  1230. struct sk_buff *in_skb = sp->in_skb;
  1231. struct sk_buff *skb = sp->out_skb;
  1232. struct nlmsghdr *nlh;
  1233. int err;
  1234. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
  1235. XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
  1236. if (nlh == NULL)
  1237. return -EMSGSIZE;
  1238. p = nlmsg_data(nlh);
  1239. copy_to_user_policy(xp, p, dir);
  1240. err = copy_to_user_tmpl(xp, skb);
  1241. if (!err)
  1242. err = copy_to_user_sec_ctx(xp, skb);
  1243. if (!err)
  1244. err = copy_to_user_policy_type(xp->type, skb);
  1245. if (!err)
  1246. err = xfrm_mark_put(skb, &xp->mark);
  1247. if (err) {
  1248. nlmsg_cancel(skb, nlh);
  1249. return err;
  1250. }
  1251. nlmsg_end(skb, nlh);
  1252. return 0;
  1253. }
  1254. static int xfrm_dump_policy_done(struct netlink_callback *cb)
  1255. {
  1256. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
  1257. xfrm_policy_walk_done(walk);
  1258. return 0;
  1259. }
  1260. static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
  1261. {
  1262. struct net *net = sock_net(skb->sk);
  1263. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
  1264. struct xfrm_dump_info info;
  1265. BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
  1266. sizeof(cb->args) - sizeof(cb->args[0]));
  1267. info.in_skb = cb->skb;
  1268. info.out_skb = skb;
  1269. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  1270. info.nlmsg_flags = NLM_F_MULTI;
  1271. if (!cb->args[0]) {
  1272. cb->args[0] = 1;
  1273. xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
  1274. }
  1275. (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
  1276. return skb->len;
  1277. }
  1278. static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
  1279. struct xfrm_policy *xp,
  1280. int dir, u32 seq)
  1281. {
  1282. struct xfrm_dump_info info;
  1283. struct sk_buff *skb;
  1284. int err;
  1285. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1286. if (!skb)
  1287. return ERR_PTR(-ENOMEM);
  1288. info.in_skb = in_skb;
  1289. info.out_skb = skb;
  1290. info.nlmsg_seq = seq;
  1291. info.nlmsg_flags = 0;
  1292. err = dump_one_policy(xp, dir, 0, &info);
  1293. if (err) {
  1294. kfree_skb(skb);
  1295. return ERR_PTR(err);
  1296. }
  1297. return skb;
  1298. }
  1299. static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1300. struct nlattr **attrs)
  1301. {
  1302. struct net *net = sock_net(skb->sk);
  1303. struct xfrm_policy *xp;
  1304. struct xfrm_userpolicy_id *p;
  1305. u8 type = XFRM_POLICY_TYPE_MAIN;
  1306. int err;
  1307. struct km_event c;
  1308. int delete;
  1309. struct xfrm_mark m;
  1310. u32 mark = xfrm_mark_get(attrs, &m);
  1311. p = nlmsg_data(nlh);
  1312. delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
  1313. err = copy_from_user_policy_type(&type, attrs);
  1314. if (err)
  1315. return err;
  1316. err = verify_policy_dir(p->dir);
  1317. if (err)
  1318. return err;
  1319. if (p->index)
  1320. xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
  1321. else {
  1322. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1323. struct xfrm_sec_ctx *ctx;
  1324. err = verify_sec_ctx_len(attrs);
  1325. if (err)
  1326. return err;
  1327. ctx = NULL;
  1328. if (rt) {
  1329. struct xfrm_user_sec_ctx *uctx = nla_data(rt);
  1330. err = security_xfrm_policy_alloc(&ctx, uctx);
  1331. if (err)
  1332. return err;
  1333. }
  1334. xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
  1335. ctx, delete, &err);
  1336. security_xfrm_policy_free(ctx);
  1337. }
  1338. if (xp == NULL)
  1339. return -ENOENT;
  1340. if (!delete) {
  1341. struct sk_buff *resp_skb;
  1342. resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
  1343. if (IS_ERR(resp_skb)) {
  1344. err = PTR_ERR(resp_skb);
  1345. } else {
  1346. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
  1347. NETLINK_CB(skb).pid);
  1348. }
  1349. } else {
  1350. uid_t loginuid = audit_get_loginuid(current);
  1351. u32 sessionid = audit_get_sessionid(current);
  1352. u32 sid;
  1353. security_task_getsecid(current, &sid);
  1354. xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
  1355. sid);
  1356. if (err != 0)
  1357. goto out;
  1358. c.data.byid = p->index;
  1359. c.event = nlh->nlmsg_type;
  1360. c.seq = nlh->nlmsg_seq;
  1361. c.pid = nlh->nlmsg_pid;
  1362. km_policy_notify(xp, p->dir, &c);
  1363. }
  1364. out:
  1365. xfrm_pol_put(xp);
  1366. return err;
  1367. }
  1368. static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  1369. struct nlattr **attrs)
  1370. {
  1371. struct net *net = sock_net(skb->sk);
  1372. struct km_event c;
  1373. struct xfrm_usersa_flush *p = nlmsg_data(nlh);
  1374. struct xfrm_audit audit_info;
  1375. int err;
  1376. audit_info.loginuid = audit_get_loginuid(current);
  1377. audit_info.sessionid = audit_get_sessionid(current);
  1378. security_task_getsecid(current, &audit_info.secid);
  1379. err = xfrm_state_flush(net, p->proto, &audit_info);
  1380. if (err) {
  1381. if (err == -ESRCH) /* empty table */
  1382. return 0;
  1383. return err;
  1384. }
  1385. c.data.proto = p->proto;
  1386. c.event = nlh->nlmsg_type;
  1387. c.seq = nlh->nlmsg_seq;
  1388. c.pid = nlh->nlmsg_pid;
  1389. c.net = net;
  1390. km_state_notify(NULL, &c);
  1391. return 0;
  1392. }
  1393. static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
  1394. {
  1395. size_t replay_size = x->replay_esn ?
  1396. xfrm_replay_state_esn_len(x->replay_esn) :
  1397. sizeof(struct xfrm_replay_state);
  1398. return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
  1399. + nla_total_size(replay_size)
  1400. + nla_total_size(sizeof(struct xfrm_lifetime_cur))
  1401. + nla_total_size(sizeof(struct xfrm_mark))
  1402. + nla_total_size(4) /* XFRM_AE_RTHR */
  1403. + nla_total_size(4); /* XFRM_AE_ETHR */
  1404. }
  1405. static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
  1406. {
  1407. struct xfrm_aevent_id *id;
  1408. struct nlmsghdr *nlh;
  1409. int err;
  1410. nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
  1411. if (nlh == NULL)
  1412. return -EMSGSIZE;
  1413. id = nlmsg_data(nlh);
  1414. memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
  1415. id->sa_id.spi = x->id.spi;
  1416. id->sa_id.family = x->props.family;
  1417. id->sa_id.proto = x->id.proto;
  1418. memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
  1419. id->reqid = x->props.reqid;
  1420. id->flags = c->data.aevent;
  1421. if (x->replay_esn) {
  1422. err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
  1423. xfrm_replay_state_esn_len(x->replay_esn),
  1424. x->replay_esn);
  1425. } else {
  1426. err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
  1427. &x->replay);
  1428. }
  1429. if (err)
  1430. goto out_cancel;
  1431. err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
  1432. if (err)
  1433. goto out_cancel;
  1434. if (id->flags & XFRM_AE_RTHR) {
  1435. err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
  1436. if (err)
  1437. goto out_cancel;
  1438. }
  1439. if (id->flags & XFRM_AE_ETHR) {
  1440. err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
  1441. x->replay_maxage * 10 / HZ);
  1442. if (err)
  1443. goto out_cancel;
  1444. }
  1445. err = xfrm_mark_put(skb, &x->mark);
  1446. if (err)
  1447. goto out_cancel;
  1448. return nlmsg_end(skb, nlh);
  1449. out_cancel:
  1450. nlmsg_cancel(skb, nlh);
  1451. return err;
  1452. }
  1453. static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1454. struct nlattr **attrs)
  1455. {
  1456. struct net *net = sock_net(skb->sk);
  1457. struct xfrm_state *x;
  1458. struct sk_buff *r_skb;
  1459. int err;
  1460. struct km_event c;
  1461. u32 mark;
  1462. struct xfrm_mark m;
  1463. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  1464. struct xfrm_usersa_id *id = &p->sa_id;
  1465. mark = xfrm_mark_get(attrs, &m);
  1466. x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
  1467. if (x == NULL)
  1468. return -ESRCH;
  1469. r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
  1470. if (r_skb == NULL) {
  1471. xfrm_state_put(x);
  1472. return -ENOMEM;
  1473. }
  1474. /*
  1475. * XXX: is this lock really needed - none of the other
  1476. * gets lock (the concern is things getting updated
  1477. * while we are still reading) - jhs
  1478. */
  1479. spin_lock_bh(&x->lock);
  1480. c.data.aevent = p->flags;
  1481. c.seq = nlh->nlmsg_seq;
  1482. c.pid = nlh->nlmsg_pid;
  1483. if (build_aevent(r_skb, x, &c) < 0)
  1484. BUG();
  1485. err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).pid);
  1486. spin_unlock_bh(&x->lock);
  1487. xfrm_state_put(x);
  1488. return err;
  1489. }
  1490. static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1491. struct nlattr **attrs)
  1492. {
  1493. struct net *net = sock_net(skb->sk);
  1494. struct xfrm_state *x;
  1495. struct km_event c;
  1496. int err = - EINVAL;
  1497. u32 mark = 0;
  1498. struct xfrm_mark m;
  1499. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  1500. struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
  1501. struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
  1502. struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
  1503. if (!lt && !rp && !re)
  1504. return err;
  1505. /* pedantic mode - thou shalt sayeth replaceth */
  1506. if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
  1507. return err;
  1508. mark = xfrm_mark_get(attrs, &m);
  1509. x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
  1510. if (x == NULL)
  1511. return -ESRCH;
  1512. if (x->km.state != XFRM_STATE_VALID)
  1513. goto out;
  1514. err = xfrm_replay_verify_len(x->replay_esn, rp);
  1515. if (err)
  1516. goto out;
  1517. spin_lock_bh(&x->lock);
  1518. xfrm_update_ae_params(x, attrs);
  1519. spin_unlock_bh(&x->lock);
  1520. c.event = nlh->nlmsg_type;
  1521. c.seq = nlh->nlmsg_seq;
  1522. c.pid = nlh->nlmsg_pid;
  1523. c.data.aevent = XFRM_AE_CU;
  1524. km_state_notify(x, &c);
  1525. err = 0;
  1526. out:
  1527. xfrm_state_put(x);
  1528. return err;
  1529. }
  1530. static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1531. struct nlattr **attrs)
  1532. {
  1533. struct net *net = sock_net(skb->sk);
  1534. struct km_event c;
  1535. u8 type = XFRM_POLICY_TYPE_MAIN;
  1536. int err;
  1537. struct xfrm_audit audit_info;
  1538. err = copy_from_user_policy_type(&type, attrs);
  1539. if (err)
  1540. return err;
  1541. audit_info.loginuid = audit_get_loginuid(current);
  1542. audit_info.sessionid = audit_get_sessionid(current);
  1543. security_task_getsecid(current, &audit_info.secid);
  1544. err = xfrm_policy_flush(net, type, &audit_info);
  1545. if (err) {
  1546. if (err == -ESRCH) /* empty table */
  1547. return 0;
  1548. return err;
  1549. }
  1550. c.data.type = type;
  1551. c.event = nlh->nlmsg_type;
  1552. c.seq = nlh->nlmsg_seq;
  1553. c.pid = nlh->nlmsg_pid;
  1554. c.net = net;
  1555. km_policy_notify(NULL, 0, &c);
  1556. return 0;
  1557. }
  1558. static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1559. struct nlattr **attrs)
  1560. {
  1561. struct net *net = sock_net(skb->sk);
  1562. struct xfrm_policy *xp;
  1563. struct xfrm_user_polexpire *up = nlmsg_data(nlh);
  1564. struct xfrm_userpolicy_info *p = &up->pol;
  1565. u8 type = XFRM_POLICY_TYPE_MAIN;
  1566. int err = -ENOENT;
  1567. struct xfrm_mark m;
  1568. u32 mark = xfrm_mark_get(attrs, &m);
  1569. err = copy_from_user_policy_type(&type, attrs);
  1570. if (err)
  1571. return err;
  1572. err = verify_policy_dir(p->dir);
  1573. if (err)
  1574. return err;
  1575. if (p->index)
  1576. xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
  1577. else {
  1578. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1579. struct xfrm_sec_ctx *ctx;
  1580. err = verify_sec_ctx_len(attrs);
  1581. if (err)
  1582. return err;
  1583. ctx = NULL;
  1584. if (rt) {
  1585. struct xfrm_user_sec_ctx *uctx = nla_data(rt);
  1586. err = security_xfrm_policy_alloc(&ctx, uctx);
  1587. if (err)
  1588. return err;
  1589. }
  1590. xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
  1591. &p->sel, ctx, 0, &err);
  1592. security_xfrm_policy_free(ctx);
  1593. }
  1594. if (xp == NULL)
  1595. return -ENOENT;
  1596. if (unlikely(xp->walk.dead))
  1597. goto out;
  1598. err = 0;
  1599. if (up->hard) {
  1600. uid_t loginuid = audit_get_loginuid(current);
  1601. u32 sessionid = audit_get_sessionid(current);
  1602. u32 sid;
  1603. security_task_getsecid(current, &sid);
  1604. xfrm_policy_delete(xp, p->dir);
  1605. xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
  1606. } else {
  1607. // reset the timers here?
  1608. WARN(1, "Dont know what to do with soft policy expire\n");
  1609. }
  1610. km_policy_expired(xp, p->dir, up->hard, current->pid);
  1611. out:
  1612. xfrm_pol_put(xp);
  1613. return err;
  1614. }
  1615. static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1616. struct nlattr **attrs)
  1617. {
  1618. struct net *net = sock_net(skb->sk);
  1619. struct xfrm_state *x;
  1620. int err;
  1621. struct xfrm_user_expire *ue = nlmsg_data(nlh);
  1622. struct xfrm_usersa_info *p = &ue->state;
  1623. struct xfrm_mark m;
  1624. u32 mark = xfrm_mark_get(attrs, &m);
  1625. x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
  1626. err = -ENOENT;
  1627. if (x == NULL)
  1628. return err;
  1629. spin_lock_bh(&x->lock);
  1630. err = -EINVAL;
  1631. if (x->km.state != XFRM_STATE_VALID)
  1632. goto out;
  1633. km_state_expired(x, ue->hard, current->pid);
  1634. if (ue->hard) {
  1635. uid_t loginuid = audit_get_loginuid(current);
  1636. u32 sessionid = audit_get_sessionid(current);
  1637. u32 sid;
  1638. security_task_getsecid(current, &sid);
  1639. __xfrm_state_delete(x);
  1640. xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
  1641. }
  1642. err = 0;
  1643. out:
  1644. spin_unlock_bh(&x->lock);
  1645. xfrm_state_put(x);
  1646. return err;
  1647. }
  1648. static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1649. struct nlattr **attrs)
  1650. {
  1651. struct net *net = sock_net(skb->sk);
  1652. struct xfrm_policy *xp;
  1653. struct xfrm_user_tmpl *ut;
  1654. int i;
  1655. struct nlattr *rt = attrs[XFRMA_TMPL];
  1656. struct xfrm_mark mark;
  1657. struct xfrm_user_acquire *ua = nlmsg_data(nlh);
  1658. struct xfrm_state *x = xfrm_state_alloc(net);
  1659. int err = -ENOMEM;
  1660. if (!x)
  1661. goto nomem;
  1662. xfrm_mark_get(attrs, &mark);
  1663. err = verify_newpolicy_info(&ua->policy);
  1664. if (err)
  1665. goto bad_policy;
  1666. /* build an XP */
  1667. xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
  1668. if (!xp)
  1669. goto free_state;
  1670. memcpy(&x->id, &ua->id, sizeof(ua->id));
  1671. memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
  1672. memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
  1673. xp->mark.m = x->mark.m = mark.m;
  1674. xp->mark.v = x->mark.v = mark.v;
  1675. ut = nla_data(rt);
  1676. /* extract the templates and for each call km_key */
  1677. for (i = 0; i < xp->xfrm_nr; i++, ut++) {
  1678. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  1679. memcpy(&x->id, &t->id, sizeof(x->id));
  1680. x->props.mode = t->mode;
  1681. x->props.reqid = t->reqid;
  1682. x->props.family = ut->family;
  1683. t->aalgos = ua->aalgos;
  1684. t->ealgos = ua->ealgos;
  1685. t->calgos = ua->calgos;
  1686. err = km_query(x, t, xp);
  1687. }
  1688. kfree(x);
  1689. kfree(xp);
  1690. return 0;
  1691. bad_policy:
  1692. WARN(1, "BAD policy passed\n");
  1693. free_state:
  1694. kfree(x);
  1695. nomem:
  1696. return err;
  1697. }
  1698. #ifdef CONFIG_XFRM_MIGRATE
  1699. static int copy_from_user_migrate(struct xfrm_migrate *ma,
  1700. struct xfrm_kmaddress *k,
  1701. struct nlattr **attrs, int *num)
  1702. {
  1703. struct nlattr *rt = attrs[XFRMA_MIGRATE];
  1704. struct xfrm_user_migrate *um;
  1705. int i, num_migrate;
  1706. if (k != NULL) {
  1707. struct xfrm_user_kmaddress *uk;
  1708. uk = nla_data(attrs[XFRMA_KMADDRESS]);
  1709. memcpy(&k->local, &uk->local, sizeof(k->local));
  1710. memcpy(&k->remote, &uk->remote, sizeof(k->remote));
  1711. k->family = uk->family;
  1712. k->reserved = uk->reserved;
  1713. }
  1714. um = nla_data(rt);
  1715. num_migrate = nla_len(rt) / sizeof(*um);
  1716. if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
  1717. return -EINVAL;
  1718. for (i = 0; i < num_migrate; i++, um++, ma++) {
  1719. memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
  1720. memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
  1721. memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
  1722. memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
  1723. ma->proto = um->proto;
  1724. ma->mode = um->mode;
  1725. ma->reqid = um->reqid;
  1726. ma->old_family = um->old_family;
  1727. ma->new_family = um->new_family;
  1728. }
  1729. *num = i;
  1730. return 0;
  1731. }
  1732. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1733. struct nlattr **attrs)
  1734. {
  1735. struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
  1736. struct xfrm_migrate m[XFRM_MAX_DEPTH];
  1737. struct xfrm_kmaddress km, *kmp;
  1738. u8 type;
  1739. int err;
  1740. int n = 0;
  1741. if (attrs[XFRMA_MIGRATE] == NULL)
  1742. return -EINVAL;
  1743. kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
  1744. err = copy_from_user_policy_type(&type, attrs);
  1745. if (err)
  1746. return err;
  1747. err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
  1748. if (err)
  1749. return err;
  1750. if (!n)
  1751. return 0;
  1752. xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
  1753. return 0;
  1754. }
  1755. #else
  1756. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1757. struct nlattr **attrs)
  1758. {
  1759. return -ENOPROTOOPT;
  1760. }
  1761. #endif
  1762. #ifdef CONFIG_XFRM_MIGRATE
  1763. static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
  1764. {
  1765. struct xfrm_user_migrate um;
  1766. memset(&um, 0, sizeof(um));
  1767. um.proto = m->proto;
  1768. um.mode = m->mode;
  1769. um.reqid = m->reqid;
  1770. um.old_family = m->old_family;
  1771. memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
  1772. memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
  1773. um.new_family = m->new_family;
  1774. memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
  1775. memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
  1776. return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
  1777. }
  1778. static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
  1779. {
  1780. struct xfrm_user_kmaddress uk;
  1781. memset(&uk, 0, sizeof(uk));
  1782. uk.family = k->family;
  1783. uk.reserved = k->reserved;
  1784. memcpy(&uk.local, &k->local, sizeof(uk.local));
  1785. memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
  1786. return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
  1787. }
  1788. static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
  1789. {
  1790. return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
  1791. + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
  1792. + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
  1793. + userpolicy_type_attrsize();
  1794. }
  1795. static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
  1796. int num_migrate, const struct xfrm_kmaddress *k,
  1797. const struct xfrm_selector *sel, u8 dir, u8 type)
  1798. {
  1799. const struct xfrm_migrate *mp;
  1800. struct xfrm_userpolicy_id *pol_id;
  1801. struct nlmsghdr *nlh;
  1802. int i, err;
  1803. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
  1804. if (nlh == NULL)
  1805. return -EMSGSIZE;
  1806. pol_id = nlmsg_data(nlh);
  1807. /* copy data from selector, dir, and type to the pol_id */
  1808. memset(pol_id, 0, sizeof(*pol_id));
  1809. memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
  1810. pol_id->dir = dir;
  1811. if (k != NULL) {
  1812. err = copy_to_user_kmaddress(k, skb);
  1813. if (err)
  1814. goto out_cancel;
  1815. }
  1816. err = copy_to_user_policy_type(type, skb);
  1817. if (err)
  1818. goto out_cancel;
  1819. for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
  1820. err = copy_to_user_migrate(mp, skb);
  1821. if (err)
  1822. goto out_cancel;
  1823. }
  1824. return nlmsg_end(skb, nlh);
  1825. out_cancel:
  1826. nlmsg_cancel(skb, nlh);
  1827. return err;
  1828. }
  1829. static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  1830. const struct xfrm_migrate *m, int num_migrate,
  1831. const struct xfrm_kmaddress *k)
  1832. {
  1833. struct net *net = &init_net;
  1834. struct sk_buff *skb;
  1835. skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
  1836. if (skb == NULL)
  1837. return -ENOMEM;
  1838. /* build migrate */
  1839. if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
  1840. BUG();
  1841. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
  1842. }
  1843. #else
  1844. static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  1845. const struct xfrm_migrate *m, int num_migrate,
  1846. const struct xfrm_kmaddress *k)
  1847. {
  1848. return -ENOPROTOOPT;
  1849. }
  1850. #endif
  1851. #define XMSGSIZE(type) sizeof(struct type)
  1852. static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
  1853. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1854. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1855. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1856. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1857. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1858. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1859. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
  1860. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
  1861. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
  1862. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1863. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1864. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
  1865. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  1866. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
  1867. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1868. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1869. [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
  1870. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1871. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
  1872. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
  1873. };
  1874. #undef XMSGSIZE
  1875. static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
  1876. [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
  1877. [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
  1878. [XFRMA_LASTUSED] = { .type = NLA_U64},
  1879. [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
  1880. [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
  1881. [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
  1882. [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
  1883. [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
  1884. [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
  1885. [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
  1886. [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
  1887. [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
  1888. [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
  1889. [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
  1890. [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
  1891. [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
  1892. [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
  1893. [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
  1894. [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
  1895. [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
  1896. [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
  1897. [XFRMA_TFCPAD] = { .type = NLA_U32 },
  1898. [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
  1899. };
  1900. static struct xfrm_link {
  1901. int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
  1902. int (*dump)(struct sk_buff *, struct netlink_callback *);
  1903. int (*done)(struct netlink_callback *);
  1904. } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
  1905. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1906. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
  1907. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
  1908. .dump = xfrm_dump_sa,
  1909. .done = xfrm_dump_sa_done },
  1910. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1911. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
  1912. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
  1913. .dump = xfrm_dump_policy,
  1914. .done = xfrm_dump_policy_done },
  1915. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
  1916. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
  1917. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
  1918. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1919. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1920. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
  1921. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
  1922. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
  1923. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
  1924. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
  1925. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
  1926. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
  1927. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
  1928. };
  1929. static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  1930. {
  1931. struct net *net = sock_net(skb->sk);
  1932. struct nlattr *attrs[XFRMA_MAX+1];
  1933. struct xfrm_link *link;
  1934. int type, err;
  1935. type = nlh->nlmsg_type;
  1936. if (type > XFRM_MSG_MAX)
  1937. return -EINVAL;
  1938. type -= XFRM_MSG_BASE;
  1939. link = &xfrm_dispatch[type];
  1940. /* All operations require privileges, even GET */
  1941. if (!capable(CAP_NET_ADMIN))
  1942. return -EPERM;
  1943. if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
  1944. type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
  1945. (nlh->nlmsg_flags & NLM_F_DUMP)) {
  1946. if (link->dump == NULL)
  1947. return -EINVAL;
  1948. {
  1949. struct netlink_dump_control c = {
  1950. .dump = link->dump,
  1951. .done = link->done,
  1952. };
  1953. return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
  1954. }
  1955. }
  1956. err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
  1957. xfrma_policy);
  1958. if (err < 0)
  1959. return err;
  1960. if (link->doit == NULL)
  1961. return -EINVAL;
  1962. return link->doit(skb, nlh, attrs);
  1963. }
  1964. static void xfrm_netlink_rcv(struct sk_buff *skb)
  1965. {
  1966. mutex_lock(&xfrm_cfg_mutex);
  1967. netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
  1968. mutex_unlock(&xfrm_cfg_mutex);
  1969. }
  1970. static inline size_t xfrm_expire_msgsize(void)
  1971. {
  1972. return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
  1973. + nla_total_size(sizeof(struct xfrm_mark));
  1974. }
  1975. static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
  1976. {
  1977. struct xfrm_user_expire *ue;
  1978. struct nlmsghdr *nlh;
  1979. int err;
  1980. nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
  1981. if (nlh == NULL)
  1982. return -EMSGSIZE;
  1983. ue = nlmsg_data(nlh);
  1984. copy_to_user_state(x, &ue->state);
  1985. ue->hard = (c->data.hard != 0) ? 1 : 0;
  1986. err = xfrm_mark_put(skb, &x->mark);
  1987. if (err)
  1988. return err;
  1989. return nlmsg_end(skb, nlh);
  1990. }
  1991. static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
  1992. {
  1993. struct net *net = xs_net(x);
  1994. struct sk_buff *skb;
  1995. skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
  1996. if (skb == NULL)
  1997. return -ENOMEM;
  1998. if (build_expire(skb, x, c) < 0) {
  1999. kfree_skb(skb);
  2000. return -EMSGSIZE;
  2001. }
  2002. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  2003. }
  2004. static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
  2005. {
  2006. struct net *net = xs_net(x);
  2007. struct sk_buff *skb;
  2008. skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
  2009. if (skb == NULL)
  2010. return -ENOMEM;
  2011. if (build_aevent(skb, x, c) < 0)
  2012. BUG();
  2013. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
  2014. }
  2015. static int xfrm_notify_sa_flush(const struct km_event *c)
  2016. {
  2017. struct net *net = c->net;
  2018. struct xfrm_usersa_flush *p;
  2019. struct nlmsghdr *nlh;
  2020. struct sk_buff *skb;
  2021. int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
  2022. skb = nlmsg_new(len, GFP_ATOMIC);
  2023. if (skb == NULL)
  2024. return -ENOMEM;
  2025. nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
  2026. if (nlh == NULL) {
  2027. kfree_skb(skb);
  2028. return -EMSGSIZE;
  2029. }
  2030. p = nlmsg_data(nlh);
  2031. p->proto = c->data.proto;
  2032. nlmsg_end(skb, nlh);
  2033. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  2034. }
  2035. static inline size_t xfrm_sa_len(struct xfrm_state *x)
  2036. {
  2037. size_t l = 0;
  2038. if (x->aead)
  2039. l += nla_total_size(aead_len(x->aead));
  2040. if (x->aalg) {
  2041. l += nla_total_size(sizeof(struct xfrm_algo) +
  2042. (x->aalg->alg_key_len + 7) / 8);
  2043. l += nla_total_size(xfrm_alg_auth_len(x->aalg));
  2044. }
  2045. if (x->ealg)
  2046. l += nla_total_size(xfrm_alg_len(x->ealg));
  2047. if (x->calg)
  2048. l += nla_total_size(sizeof(*x->calg));
  2049. if (x->encap)
  2050. l += nla_total_size(sizeof(*x->encap));
  2051. if (x->tfcpad)
  2052. l += nla_total_size(sizeof(x->tfcpad));
  2053. if (x->replay_esn)
  2054. l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
  2055. if (x->security)
  2056. l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
  2057. x->security->ctx_len);
  2058. if (x->coaddr)
  2059. l += nla_total_size(sizeof(*x->coaddr));
  2060. /* Must count x->lastused as it may become non-zero behind our back. */
  2061. l += nla_total_size(sizeof(u64));
  2062. return l;
  2063. }
  2064. static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
  2065. {
  2066. struct net *net = xs_net(x);
  2067. struct xfrm_usersa_info *p;
  2068. struct xfrm_usersa_id *id;
  2069. struct nlmsghdr *nlh;
  2070. struct sk_buff *skb;
  2071. int len = xfrm_sa_len(x);
  2072. int headlen, err;
  2073. headlen = sizeof(*p);
  2074. if (c->event == XFRM_MSG_DELSA) {
  2075. len += nla_total_size(headlen);
  2076. headlen = sizeof(*id);
  2077. len += nla_total_size(sizeof(struct xfrm_mark));
  2078. }
  2079. len += NLMSG_ALIGN(headlen);
  2080. skb = nlmsg_new(len, GFP_ATOMIC);
  2081. if (skb == NULL)
  2082. return -ENOMEM;
  2083. nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
  2084. err = -EMSGSIZE;
  2085. if (nlh == NULL)
  2086. goto out_free_skb;
  2087. p = nlmsg_data(nlh);
  2088. if (c->event == XFRM_MSG_DELSA) {
  2089. struct nlattr *attr;
  2090. id = nlmsg_data(nlh);
  2091. memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
  2092. id->spi = x->id.spi;
  2093. id->family = x->props.family;
  2094. id->proto = x->id.proto;
  2095. attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
  2096. err = -EMSGSIZE;
  2097. if (attr == NULL)
  2098. goto out_free_skb;
  2099. p = nla_data(attr);
  2100. }
  2101. err = copy_to_user_state_extra(x, p, skb);
  2102. if (err)
  2103. goto out_free_skb;
  2104. nlmsg_end(skb, nlh);
  2105. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  2106. out_free_skb:
  2107. kfree_skb(skb);
  2108. return err;
  2109. }
  2110. static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
  2111. {
  2112. switch (c->event) {
  2113. case XFRM_MSG_EXPIRE:
  2114. return xfrm_exp_state_notify(x, c);
  2115. case XFRM_MSG_NEWAE:
  2116. return xfrm_aevent_state_notify(x, c);
  2117. case XFRM_MSG_DELSA:
  2118. case XFRM_MSG_UPDSA:
  2119. case XFRM_MSG_NEWSA:
  2120. return xfrm_notify_sa(x, c);
  2121. case XFRM_MSG_FLUSHSA:
  2122. return xfrm_notify_sa_flush(c);
  2123. default:
  2124. printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
  2125. c->event);
  2126. break;
  2127. }
  2128. return 0;
  2129. }
  2130. static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
  2131. struct xfrm_policy *xp)
  2132. {
  2133. return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
  2134. + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
  2135. + nla_total_size(sizeof(struct xfrm_mark))
  2136. + nla_total_size(xfrm_user_sec_ctx_size(x->security))
  2137. + userpolicy_type_attrsize();
  2138. }
  2139. static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
  2140. struct xfrm_tmpl *xt, struct xfrm_policy *xp,
  2141. int dir)
  2142. {
  2143. __u32 seq = xfrm_get_acqseq();
  2144. struct xfrm_user_acquire *ua;
  2145. struct nlmsghdr *nlh;
  2146. int err;
  2147. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
  2148. if (nlh == NULL)
  2149. return -EMSGSIZE;
  2150. ua = nlmsg_data(nlh);
  2151. memcpy(&ua->id, &x->id, sizeof(ua->id));
  2152. memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
  2153. memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
  2154. copy_to_user_policy(xp, &ua->policy, dir);
  2155. ua->aalgos = xt->aalgos;
  2156. ua->ealgos = xt->ealgos;
  2157. ua->calgos = xt->calgos;
  2158. ua->seq = x->km.seq = seq;
  2159. err = copy_to_user_tmpl(xp, skb);
  2160. if (!err)
  2161. err = copy_to_user_state_sec_ctx(x, skb);
  2162. if (!err)
  2163. err = copy_to_user_policy_type(xp->type, skb);
  2164. if (!err)
  2165. err = xfrm_mark_put(skb, &xp->mark);
  2166. if (err) {
  2167. nlmsg_cancel(skb, nlh);
  2168. return err;
  2169. }
  2170. return nlmsg_end(skb, nlh);
  2171. }
  2172. static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
  2173. struct xfrm_policy *xp, int dir)
  2174. {
  2175. struct net *net = xs_net(x);
  2176. struct sk_buff *skb;
  2177. skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
  2178. if (skb == NULL)
  2179. return -ENOMEM;
  2180. if (build_acquire(skb, x, xt, xp, dir) < 0)
  2181. BUG();
  2182. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
  2183. }
  2184. /* User gives us xfrm_user_policy_info followed by an array of 0
  2185. * or more templates.
  2186. */
  2187. static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
  2188. u8 *data, int len, int *dir)
  2189. {
  2190. struct net *net = sock_net(sk);
  2191. struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
  2192. struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
  2193. struct xfrm_policy *xp;
  2194. int nr;
  2195. switch (sk->sk_family) {
  2196. case AF_INET:
  2197. if (opt != IP_XFRM_POLICY) {
  2198. *dir = -EOPNOTSUPP;
  2199. return NULL;
  2200. }
  2201. break;
  2202. #if IS_ENABLED(CONFIG_IPV6)
  2203. case AF_INET6:
  2204. if (opt != IPV6_XFRM_POLICY) {
  2205. *dir = -EOPNOTSUPP;
  2206. return NULL;
  2207. }
  2208. break;
  2209. #endif
  2210. default:
  2211. *dir = -EINVAL;
  2212. return NULL;
  2213. }
  2214. *dir = -EINVAL;
  2215. if (len < sizeof(*p) ||
  2216. verify_newpolicy_info(p))
  2217. return NULL;
  2218. nr = ((len - sizeof(*p)) / sizeof(*ut));
  2219. if (validate_tmpl(nr, ut, p->sel.family))
  2220. return NULL;
  2221. if (p->dir > XFRM_POLICY_OUT)
  2222. return NULL;
  2223. xp = xfrm_policy_alloc(net, GFP_ATOMIC);
  2224. if (xp == NULL) {
  2225. *dir = -ENOBUFS;
  2226. return NULL;
  2227. }
  2228. copy_from_user_policy(xp, p);
  2229. xp->type = XFRM_POLICY_TYPE_MAIN;
  2230. copy_templates(xp, ut, nr);
  2231. *dir = p->dir;
  2232. return xp;
  2233. }
  2234. static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
  2235. {
  2236. return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
  2237. + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
  2238. + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
  2239. + nla_total_size(sizeof(struct xfrm_mark))
  2240. + userpolicy_type_attrsize();
  2241. }
  2242. static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
  2243. int dir, const struct km_event *c)
  2244. {
  2245. struct xfrm_user_polexpire *upe;
  2246. int hard = c->data.hard;
  2247. struct nlmsghdr *nlh;
  2248. int err;
  2249. nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
  2250. if (nlh == NULL)
  2251. return -EMSGSIZE;
  2252. upe = nlmsg_data(nlh);
  2253. copy_to_user_policy(xp, &upe->pol, dir);
  2254. err = copy_to_user_tmpl(xp, skb);
  2255. if (!err)
  2256. err = copy_to_user_sec_ctx(xp, skb);
  2257. if (!err)
  2258. err = copy_to_user_policy_type(xp->type, skb);
  2259. if (!err)
  2260. err = xfrm_mark_put(skb, &xp->mark);
  2261. if (err) {
  2262. nlmsg_cancel(skb, nlh);
  2263. return err;
  2264. }
  2265. upe->hard = !!hard;
  2266. return nlmsg_end(skb, nlh);
  2267. }
  2268. static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
  2269. {
  2270. struct net *net = xp_net(xp);
  2271. struct sk_buff *skb;
  2272. skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
  2273. if (skb == NULL)
  2274. return -ENOMEM;
  2275. if (build_polexpire(skb, xp, dir, c) < 0)
  2276. BUG();
  2277. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  2278. }
  2279. static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
  2280. {
  2281. int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  2282. struct net *net = xp_net(xp);
  2283. struct xfrm_userpolicy_info *p;
  2284. struct xfrm_userpolicy_id *id;
  2285. struct nlmsghdr *nlh;
  2286. struct sk_buff *skb;
  2287. int headlen, err;
  2288. headlen = sizeof(*p);
  2289. if (c->event == XFRM_MSG_DELPOLICY) {
  2290. len += nla_total_size(headlen);
  2291. headlen = sizeof(*id);
  2292. }
  2293. len += userpolicy_type_attrsize();
  2294. len += nla_total_size(sizeof(struct xfrm_mark));
  2295. len += NLMSG_ALIGN(headlen);
  2296. skb = nlmsg_new(len, GFP_ATOMIC);
  2297. if (skb == NULL)
  2298. return -ENOMEM;
  2299. nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
  2300. err = -EMSGSIZE;
  2301. if (nlh == NULL)
  2302. goto out_free_skb;
  2303. p = nlmsg_data(nlh);
  2304. if (c->event == XFRM_MSG_DELPOLICY) {
  2305. struct nlattr *attr;
  2306. id = nlmsg_data(nlh);
  2307. memset(id, 0, sizeof(*id));
  2308. id->dir = dir;
  2309. if (c->data.byid)
  2310. id->index = xp->index;
  2311. else
  2312. memcpy(&id->sel, &xp->selector, sizeof(id->sel));
  2313. attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
  2314. err = -EMSGSIZE;
  2315. if (attr == NULL)
  2316. goto out_free_skb;
  2317. p = nla_data(attr);
  2318. }
  2319. copy_to_user_policy(xp, p, dir);
  2320. err = copy_to_user_tmpl(xp, skb);
  2321. if (!err)
  2322. err = copy_to_user_policy_type(xp->type, skb);
  2323. if (!err)
  2324. err = xfrm_mark_put(skb, &xp->mark);
  2325. if (err)
  2326. goto out_free_skb;
  2327. nlmsg_end(skb, nlh);
  2328. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  2329. out_free_skb:
  2330. kfree_skb(skb);
  2331. return err;
  2332. }
  2333. static int xfrm_notify_policy_flush(const struct km_event *c)
  2334. {
  2335. struct net *net = c->net;
  2336. struct nlmsghdr *nlh;
  2337. struct sk_buff *skb;
  2338. int err;
  2339. skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
  2340. if (skb == NULL)
  2341. return -ENOMEM;
  2342. nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
  2343. err = -EMSGSIZE;
  2344. if (nlh == NULL)
  2345. goto out_free_skb;
  2346. err = copy_to_user_policy_type(c->data.type, skb);
  2347. if (err)
  2348. goto out_free_skb;
  2349. nlmsg_end(skb, nlh);
  2350. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  2351. out_free_skb:
  2352. kfree_skb(skb);
  2353. return err;
  2354. }
  2355. static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
  2356. {
  2357. switch (c->event) {
  2358. case XFRM_MSG_NEWPOLICY:
  2359. case XFRM_MSG_UPDPOLICY:
  2360. case XFRM_MSG_DELPOLICY:
  2361. return xfrm_notify_policy(xp, dir, c);
  2362. case XFRM_MSG_FLUSHPOLICY:
  2363. return xfrm_notify_policy_flush(c);
  2364. case XFRM_MSG_POLEXPIRE:
  2365. return xfrm_exp_policy_notify(xp, dir, c);
  2366. default:
  2367. printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
  2368. c->event);
  2369. }
  2370. return 0;
  2371. }
  2372. static inline size_t xfrm_report_msgsize(void)
  2373. {
  2374. return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
  2375. }
  2376. static int build_report(struct sk_buff *skb, u8 proto,
  2377. struct xfrm_selector *sel, xfrm_address_t *addr)
  2378. {
  2379. struct xfrm_user_report *ur;
  2380. struct nlmsghdr *nlh;
  2381. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
  2382. if (nlh == NULL)
  2383. return -EMSGSIZE;
  2384. ur = nlmsg_data(nlh);
  2385. ur->proto = proto;
  2386. memcpy(&ur->sel, sel, sizeof(ur->sel));
  2387. if (addr) {
  2388. int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
  2389. if (err) {
  2390. nlmsg_cancel(skb, nlh);
  2391. return err;
  2392. }
  2393. }
  2394. return nlmsg_end(skb, nlh);
  2395. }
  2396. static int xfrm_send_report(struct net *net, u8 proto,
  2397. struct xfrm_selector *sel, xfrm_address_t *addr)
  2398. {
  2399. struct sk_buff *skb;
  2400. skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
  2401. if (skb == NULL)
  2402. return -ENOMEM;
  2403. if (build_report(skb, proto, sel, addr) < 0)
  2404. BUG();
  2405. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
  2406. }
  2407. static inline size_t xfrm_mapping_msgsize(void)
  2408. {
  2409. return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
  2410. }
  2411. static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
  2412. xfrm_address_t *new_saddr, __be16 new_sport)
  2413. {
  2414. struct xfrm_user_mapping *um;
  2415. struct nlmsghdr *nlh;
  2416. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
  2417. if (nlh == NULL)
  2418. return -EMSGSIZE;
  2419. um = nlmsg_data(nlh);
  2420. memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
  2421. um->id.spi = x->id.spi;
  2422. um->id.family = x->props.family;
  2423. um->id.proto = x->id.proto;
  2424. memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
  2425. memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
  2426. um->new_sport = new_sport;
  2427. um->old_sport = x->encap->encap_sport;
  2428. um->reqid = x->props.reqid;
  2429. return nlmsg_end(skb, nlh);
  2430. }
  2431. static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
  2432. __be16 sport)
  2433. {
  2434. struct net *net = xs_net(x);
  2435. struct sk_buff *skb;
  2436. if (x->id.proto != IPPROTO_ESP)
  2437. return -EINVAL;
  2438. if (!x->encap)
  2439. return -EINVAL;
  2440. skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
  2441. if (skb == NULL)
  2442. return -ENOMEM;
  2443. if (build_mapping(skb, x, ipaddr, sport) < 0)
  2444. BUG();
  2445. return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
  2446. }
  2447. static struct xfrm_mgr netlink_mgr = {
  2448. .id = "netlink",
  2449. .notify = xfrm_send_state_notify,
  2450. .acquire = xfrm_send_acquire,
  2451. .compile_policy = xfrm_compile_policy,
  2452. .notify_policy = xfrm_send_policy_notify,
  2453. .report = xfrm_send_report,
  2454. .migrate = xfrm_send_migrate,
  2455. .new_mapping = xfrm_send_mapping,
  2456. };
  2457. static int __net_init xfrm_user_net_init(struct net *net)
  2458. {
  2459. struct sock *nlsk;
  2460. struct netlink_kernel_cfg cfg = {
  2461. .groups = XFRMNLGRP_MAX,
  2462. .input = xfrm_netlink_rcv,
  2463. };
  2464. nlsk = netlink_kernel_create(net, NETLINK_XFRM, THIS_MODULE, &cfg);
  2465. if (nlsk == NULL)
  2466. return -ENOMEM;
  2467. net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
  2468. rcu_assign_pointer(net->xfrm.nlsk, nlsk);
  2469. return 0;
  2470. }
  2471. static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
  2472. {
  2473. struct net *net;
  2474. list_for_each_entry(net, net_exit_list, exit_list)
  2475. RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
  2476. synchronize_net();
  2477. list_for_each_entry(net, net_exit_list, exit_list)
  2478. netlink_kernel_release(net->xfrm.nlsk_stash);
  2479. }
  2480. static struct pernet_operations xfrm_user_net_ops = {
  2481. .init = xfrm_user_net_init,
  2482. .exit_batch = xfrm_user_net_exit,
  2483. };
  2484. static int __init xfrm_user_init(void)
  2485. {
  2486. int rv;
  2487. printk(KERN_INFO "Initializing XFRM netlink socket\n");
  2488. rv = register_pernet_subsys(&xfrm_user_net_ops);
  2489. if (rv < 0)
  2490. return rv;
  2491. rv = xfrm_register_km(&netlink_mgr);
  2492. if (rv < 0)
  2493. unregister_pernet_subsys(&xfrm_user_net_ops);
  2494. return rv;
  2495. }
  2496. static void __exit xfrm_user_exit(void)
  2497. {
  2498. xfrm_unregister_km(&netlink_mgr);
  2499. unregister_pernet_subsys(&xfrm_user_net_ops);
  2500. }
  2501. module_init(xfrm_user_init);
  2502. module_exit(xfrm_user_exit);
  2503. MODULE_LICENSE("GPL");
  2504. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);