xfrm_user.c 69 KB

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