xfrm_policy.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557
  1. /*
  2. * xfrm_policy.c
  3. *
  4. * Changes:
  5. * Mitsuru KANDA @USAGI
  6. * Kazunori MIYAZAWA @USAGI
  7. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  8. * IPv6 support
  9. * Kazunori MIYAZAWA @USAGI
  10. * YOSHIFUJI Hideaki
  11. * Split up af-specific portion
  12. * Derek Atkins <derek@ihtfp.com> Add the post_input processor
  13. *
  14. */
  15. #include <linux/slab.h>
  16. #include <linux/kmod.h>
  17. #include <linux/list.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/notifier.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/netfilter.h>
  23. #include <linux/module.h>
  24. #include <linux/cache.h>
  25. #include <net/xfrm.h>
  26. #include <net/ip.h>
  27. #include <linux/audit.h>
  28. #include "xfrm_hash.h"
  29. DEFINE_MUTEX(xfrm_cfg_mutex);
  30. EXPORT_SYMBOL(xfrm_cfg_mutex);
  31. static DEFINE_RWLOCK(xfrm_policy_lock);
  32. unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
  33. EXPORT_SYMBOL(xfrm_policy_count);
  34. static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
  35. static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
  36. static struct kmem_cache *xfrm_dst_cache __read_mostly;
  37. static struct work_struct xfrm_policy_gc_work;
  38. static HLIST_HEAD(xfrm_policy_gc_list);
  39. static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
  40. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
  41. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
  42. static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family);
  43. static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo);
  44. static inline int
  45. __xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
  46. {
  47. return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
  48. addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
  49. !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
  50. !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
  51. (fl->proto == sel->proto || !sel->proto) &&
  52. (fl->oif == sel->ifindex || !sel->ifindex);
  53. }
  54. static inline int
  55. __xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
  56. {
  57. return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
  58. addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
  59. !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
  60. !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
  61. (fl->proto == sel->proto || !sel->proto) &&
  62. (fl->oif == sel->ifindex || !sel->ifindex);
  63. }
  64. int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
  65. unsigned short family)
  66. {
  67. switch (family) {
  68. case AF_INET:
  69. return __xfrm4_selector_match(sel, fl);
  70. case AF_INET6:
  71. return __xfrm6_selector_match(sel, fl);
  72. }
  73. return 0;
  74. }
  75. int xfrm_register_type(struct xfrm_type *type, unsigned short family)
  76. {
  77. struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
  78. struct xfrm_type **typemap;
  79. int err = 0;
  80. if (unlikely(afinfo == NULL))
  81. return -EAFNOSUPPORT;
  82. typemap = afinfo->type_map;
  83. if (likely(typemap[type->proto] == NULL))
  84. typemap[type->proto] = type;
  85. else
  86. err = -EEXIST;
  87. xfrm_policy_unlock_afinfo(afinfo);
  88. return err;
  89. }
  90. EXPORT_SYMBOL(xfrm_register_type);
  91. int xfrm_unregister_type(struct xfrm_type *type, unsigned short family)
  92. {
  93. struct xfrm_policy_afinfo *afinfo = xfrm_policy_lock_afinfo(family);
  94. struct xfrm_type **typemap;
  95. int err = 0;
  96. if (unlikely(afinfo == NULL))
  97. return -EAFNOSUPPORT;
  98. typemap = afinfo->type_map;
  99. if (unlikely(typemap[type->proto] != type))
  100. err = -ENOENT;
  101. else
  102. typemap[type->proto] = NULL;
  103. xfrm_policy_unlock_afinfo(afinfo);
  104. return err;
  105. }
  106. EXPORT_SYMBOL(xfrm_unregister_type);
  107. struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
  108. {
  109. struct xfrm_policy_afinfo *afinfo;
  110. struct xfrm_type **typemap;
  111. struct xfrm_type *type;
  112. int modload_attempted = 0;
  113. retry:
  114. afinfo = xfrm_policy_get_afinfo(family);
  115. if (unlikely(afinfo == NULL))
  116. return NULL;
  117. typemap = afinfo->type_map;
  118. type = typemap[proto];
  119. if (unlikely(type && !try_module_get(type->owner)))
  120. type = NULL;
  121. if (!type && !modload_attempted) {
  122. xfrm_policy_put_afinfo(afinfo);
  123. request_module("xfrm-type-%d-%d",
  124. (int) family, (int) proto);
  125. modload_attempted = 1;
  126. goto retry;
  127. }
  128. xfrm_policy_put_afinfo(afinfo);
  129. return type;
  130. }
  131. int xfrm_dst_lookup(struct xfrm_dst **dst, struct flowi *fl,
  132. unsigned short family)
  133. {
  134. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  135. int err = 0;
  136. if (unlikely(afinfo == NULL))
  137. return -EAFNOSUPPORT;
  138. if (likely(afinfo->dst_lookup != NULL))
  139. err = afinfo->dst_lookup(dst, fl);
  140. else
  141. err = -EINVAL;
  142. xfrm_policy_put_afinfo(afinfo);
  143. return err;
  144. }
  145. EXPORT_SYMBOL(xfrm_dst_lookup);
  146. void xfrm_put_type(struct xfrm_type *type)
  147. {
  148. module_put(type->owner);
  149. }
  150. int xfrm_register_mode(struct xfrm_mode *mode, int family)
  151. {
  152. struct xfrm_policy_afinfo *afinfo;
  153. struct xfrm_mode **modemap;
  154. int err;
  155. if (unlikely(mode->encap >= XFRM_MODE_MAX))
  156. return -EINVAL;
  157. afinfo = xfrm_policy_lock_afinfo(family);
  158. if (unlikely(afinfo == NULL))
  159. return -EAFNOSUPPORT;
  160. err = -EEXIST;
  161. modemap = afinfo->mode_map;
  162. if (likely(modemap[mode->encap] == NULL)) {
  163. modemap[mode->encap] = mode;
  164. err = 0;
  165. }
  166. xfrm_policy_unlock_afinfo(afinfo);
  167. return err;
  168. }
  169. EXPORT_SYMBOL(xfrm_register_mode);
  170. int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
  171. {
  172. struct xfrm_policy_afinfo *afinfo;
  173. struct xfrm_mode **modemap;
  174. int err;
  175. if (unlikely(mode->encap >= XFRM_MODE_MAX))
  176. return -EINVAL;
  177. afinfo = xfrm_policy_lock_afinfo(family);
  178. if (unlikely(afinfo == NULL))
  179. return -EAFNOSUPPORT;
  180. err = -ENOENT;
  181. modemap = afinfo->mode_map;
  182. if (likely(modemap[mode->encap] == mode)) {
  183. modemap[mode->encap] = NULL;
  184. err = 0;
  185. }
  186. xfrm_policy_unlock_afinfo(afinfo);
  187. return err;
  188. }
  189. EXPORT_SYMBOL(xfrm_unregister_mode);
  190. struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
  191. {
  192. struct xfrm_policy_afinfo *afinfo;
  193. struct xfrm_mode *mode;
  194. int modload_attempted = 0;
  195. if (unlikely(encap >= XFRM_MODE_MAX))
  196. return NULL;
  197. retry:
  198. afinfo = xfrm_policy_get_afinfo(family);
  199. if (unlikely(afinfo == NULL))
  200. return NULL;
  201. mode = afinfo->mode_map[encap];
  202. if (unlikely(mode && !try_module_get(mode->owner)))
  203. mode = NULL;
  204. if (!mode && !modload_attempted) {
  205. xfrm_policy_put_afinfo(afinfo);
  206. request_module("xfrm-mode-%d-%d", family, encap);
  207. modload_attempted = 1;
  208. goto retry;
  209. }
  210. xfrm_policy_put_afinfo(afinfo);
  211. return mode;
  212. }
  213. void xfrm_put_mode(struct xfrm_mode *mode)
  214. {
  215. module_put(mode->owner);
  216. }
  217. static inline unsigned long make_jiffies(long secs)
  218. {
  219. if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
  220. return MAX_SCHEDULE_TIMEOUT-1;
  221. else
  222. return secs*HZ;
  223. }
  224. static void xfrm_policy_timer(unsigned long data)
  225. {
  226. struct xfrm_policy *xp = (struct xfrm_policy*)data;
  227. unsigned long now = get_seconds();
  228. long next = LONG_MAX;
  229. int warn = 0;
  230. int dir;
  231. read_lock(&xp->lock);
  232. if (xp->dead)
  233. goto out;
  234. dir = xfrm_policy_id2dir(xp->index);
  235. if (xp->lft.hard_add_expires_seconds) {
  236. long tmo = xp->lft.hard_add_expires_seconds +
  237. xp->curlft.add_time - now;
  238. if (tmo <= 0)
  239. goto expired;
  240. if (tmo < next)
  241. next = tmo;
  242. }
  243. if (xp->lft.hard_use_expires_seconds) {
  244. long tmo = xp->lft.hard_use_expires_seconds +
  245. (xp->curlft.use_time ? : xp->curlft.add_time) - now;
  246. if (tmo <= 0)
  247. goto expired;
  248. if (tmo < next)
  249. next = tmo;
  250. }
  251. if (xp->lft.soft_add_expires_seconds) {
  252. long tmo = xp->lft.soft_add_expires_seconds +
  253. xp->curlft.add_time - now;
  254. if (tmo <= 0) {
  255. warn = 1;
  256. tmo = XFRM_KM_TIMEOUT;
  257. }
  258. if (tmo < next)
  259. next = tmo;
  260. }
  261. if (xp->lft.soft_use_expires_seconds) {
  262. long tmo = xp->lft.soft_use_expires_seconds +
  263. (xp->curlft.use_time ? : xp->curlft.add_time) - now;
  264. if (tmo <= 0) {
  265. warn = 1;
  266. tmo = XFRM_KM_TIMEOUT;
  267. }
  268. if (tmo < next)
  269. next = tmo;
  270. }
  271. if (warn)
  272. km_policy_expired(xp, dir, 0, 0);
  273. if (next != LONG_MAX &&
  274. !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
  275. xfrm_pol_hold(xp);
  276. out:
  277. read_unlock(&xp->lock);
  278. xfrm_pol_put(xp);
  279. return;
  280. expired:
  281. read_unlock(&xp->lock);
  282. if (!xfrm_policy_delete(xp, dir))
  283. km_policy_expired(xp, dir, 1, 0);
  284. xfrm_pol_put(xp);
  285. }
  286. /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
  287. * SPD calls.
  288. */
  289. struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
  290. {
  291. struct xfrm_policy *policy;
  292. policy = kzalloc(sizeof(struct xfrm_policy), gfp);
  293. if (policy) {
  294. INIT_HLIST_NODE(&policy->bydst);
  295. INIT_HLIST_NODE(&policy->byidx);
  296. rwlock_init(&policy->lock);
  297. atomic_set(&policy->refcnt, 1);
  298. init_timer(&policy->timer);
  299. policy->timer.data = (unsigned long)policy;
  300. policy->timer.function = xfrm_policy_timer;
  301. }
  302. return policy;
  303. }
  304. EXPORT_SYMBOL(xfrm_policy_alloc);
  305. /* Destroy xfrm_policy: descendant resources must be released to this moment. */
  306. void __xfrm_policy_destroy(struct xfrm_policy *policy)
  307. {
  308. BUG_ON(!policy->dead);
  309. BUG_ON(policy->bundles);
  310. if (del_timer(&policy->timer))
  311. BUG();
  312. security_xfrm_policy_free(policy);
  313. kfree(policy);
  314. }
  315. EXPORT_SYMBOL(__xfrm_policy_destroy);
  316. static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
  317. {
  318. struct dst_entry *dst;
  319. while ((dst = policy->bundles) != NULL) {
  320. policy->bundles = dst->next;
  321. dst_free(dst);
  322. }
  323. if (del_timer(&policy->timer))
  324. atomic_dec(&policy->refcnt);
  325. if (atomic_read(&policy->refcnt) > 1)
  326. flow_cache_flush();
  327. xfrm_pol_put(policy);
  328. }
  329. static void xfrm_policy_gc_task(struct work_struct *work)
  330. {
  331. struct xfrm_policy *policy;
  332. struct hlist_node *entry, *tmp;
  333. struct hlist_head gc_list;
  334. spin_lock_bh(&xfrm_policy_gc_lock);
  335. gc_list.first = xfrm_policy_gc_list.first;
  336. INIT_HLIST_HEAD(&xfrm_policy_gc_list);
  337. spin_unlock_bh(&xfrm_policy_gc_lock);
  338. hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
  339. xfrm_policy_gc_kill(policy);
  340. }
  341. /* Rule must be locked. Release descentant resources, announce
  342. * entry dead. The rule must be unlinked from lists to the moment.
  343. */
  344. static void xfrm_policy_kill(struct xfrm_policy *policy)
  345. {
  346. int dead;
  347. write_lock_bh(&policy->lock);
  348. dead = policy->dead;
  349. policy->dead = 1;
  350. write_unlock_bh(&policy->lock);
  351. if (unlikely(dead)) {
  352. WARN_ON(1);
  353. return;
  354. }
  355. spin_lock(&xfrm_policy_gc_lock);
  356. hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
  357. spin_unlock(&xfrm_policy_gc_lock);
  358. schedule_work(&xfrm_policy_gc_work);
  359. }
  360. struct xfrm_policy_hash {
  361. struct hlist_head *table;
  362. unsigned int hmask;
  363. };
  364. static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
  365. static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
  366. static struct hlist_head *xfrm_policy_byidx __read_mostly;
  367. static unsigned int xfrm_idx_hmask __read_mostly;
  368. static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
  369. static inline unsigned int idx_hash(u32 index)
  370. {
  371. return __idx_hash(index, xfrm_idx_hmask);
  372. }
  373. static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
  374. {
  375. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  376. unsigned int hash = __sel_hash(sel, family, hmask);
  377. return (hash == hmask + 1 ?
  378. &xfrm_policy_inexact[dir] :
  379. xfrm_policy_bydst[dir].table + hash);
  380. }
  381. static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
  382. {
  383. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  384. unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
  385. return xfrm_policy_bydst[dir].table + hash;
  386. }
  387. static void xfrm_dst_hash_transfer(struct hlist_head *list,
  388. struct hlist_head *ndsttable,
  389. unsigned int nhashmask)
  390. {
  391. struct hlist_node *entry, *tmp;
  392. struct xfrm_policy *pol;
  393. hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
  394. unsigned int h;
  395. h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
  396. pol->family, nhashmask);
  397. hlist_add_head(&pol->bydst, ndsttable+h);
  398. }
  399. }
  400. static void xfrm_idx_hash_transfer(struct hlist_head *list,
  401. struct hlist_head *nidxtable,
  402. unsigned int nhashmask)
  403. {
  404. struct hlist_node *entry, *tmp;
  405. struct xfrm_policy *pol;
  406. hlist_for_each_entry_safe(pol, entry, tmp, list, byidx) {
  407. unsigned int h;
  408. h = __idx_hash(pol->index, nhashmask);
  409. hlist_add_head(&pol->byidx, nidxtable+h);
  410. }
  411. }
  412. static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
  413. {
  414. return ((old_hmask + 1) << 1) - 1;
  415. }
  416. static void xfrm_bydst_resize(int dir)
  417. {
  418. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  419. unsigned int nhashmask = xfrm_new_hash_mask(hmask);
  420. unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
  421. struct hlist_head *odst = xfrm_policy_bydst[dir].table;
  422. struct hlist_head *ndst = xfrm_hash_alloc(nsize);
  423. int i;
  424. if (!ndst)
  425. return;
  426. write_lock_bh(&xfrm_policy_lock);
  427. for (i = hmask; i >= 0; i--)
  428. xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
  429. xfrm_policy_bydst[dir].table = ndst;
  430. xfrm_policy_bydst[dir].hmask = nhashmask;
  431. write_unlock_bh(&xfrm_policy_lock);
  432. xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
  433. }
  434. static void xfrm_byidx_resize(int total)
  435. {
  436. unsigned int hmask = xfrm_idx_hmask;
  437. unsigned int nhashmask = xfrm_new_hash_mask(hmask);
  438. unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
  439. struct hlist_head *oidx = xfrm_policy_byidx;
  440. struct hlist_head *nidx = xfrm_hash_alloc(nsize);
  441. int i;
  442. if (!nidx)
  443. return;
  444. write_lock_bh(&xfrm_policy_lock);
  445. for (i = hmask; i >= 0; i--)
  446. xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
  447. xfrm_policy_byidx = nidx;
  448. xfrm_idx_hmask = nhashmask;
  449. write_unlock_bh(&xfrm_policy_lock);
  450. xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
  451. }
  452. static inline int xfrm_bydst_should_resize(int dir, int *total)
  453. {
  454. unsigned int cnt = xfrm_policy_count[dir];
  455. unsigned int hmask = xfrm_policy_bydst[dir].hmask;
  456. if (total)
  457. *total += cnt;
  458. if ((hmask + 1) < xfrm_policy_hashmax &&
  459. cnt > hmask)
  460. return 1;
  461. return 0;
  462. }
  463. static inline int xfrm_byidx_should_resize(int total)
  464. {
  465. unsigned int hmask = xfrm_idx_hmask;
  466. if ((hmask + 1) < xfrm_policy_hashmax &&
  467. total > hmask)
  468. return 1;
  469. return 0;
  470. }
  471. void xfrm_spd_getinfo(struct xfrm_spdinfo *si)
  472. {
  473. read_lock_bh(&xfrm_policy_lock);
  474. si->incnt = xfrm_policy_count[XFRM_POLICY_IN];
  475. si->outcnt = xfrm_policy_count[XFRM_POLICY_OUT];
  476. si->fwdcnt = xfrm_policy_count[XFRM_POLICY_FWD];
  477. si->inscnt = xfrm_policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
  478. si->outscnt = xfrm_policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
  479. si->fwdscnt = xfrm_policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
  480. si->spdhcnt = xfrm_idx_hmask;
  481. si->spdhmcnt = xfrm_policy_hashmax;
  482. read_unlock_bh(&xfrm_policy_lock);
  483. }
  484. EXPORT_SYMBOL(xfrm_spd_getinfo);
  485. static DEFINE_MUTEX(hash_resize_mutex);
  486. static void xfrm_hash_resize(struct work_struct *__unused)
  487. {
  488. int dir, total;
  489. mutex_lock(&hash_resize_mutex);
  490. total = 0;
  491. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  492. if (xfrm_bydst_should_resize(dir, &total))
  493. xfrm_bydst_resize(dir);
  494. }
  495. if (xfrm_byidx_should_resize(total))
  496. xfrm_byidx_resize(total);
  497. mutex_unlock(&hash_resize_mutex);
  498. }
  499. static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
  500. /* Generate new index... KAME seems to generate them ordered by cost
  501. * of an absolute inpredictability of ordering of rules. This will not pass. */
  502. static u32 xfrm_gen_index(u8 type, int dir)
  503. {
  504. static u32 idx_generator;
  505. for (;;) {
  506. struct hlist_node *entry;
  507. struct hlist_head *list;
  508. struct xfrm_policy *p;
  509. u32 idx;
  510. int found;
  511. idx = (idx_generator | dir);
  512. idx_generator += 8;
  513. if (idx == 0)
  514. idx = 8;
  515. list = xfrm_policy_byidx + idx_hash(idx);
  516. found = 0;
  517. hlist_for_each_entry(p, entry, list, byidx) {
  518. if (p->index == idx) {
  519. found = 1;
  520. break;
  521. }
  522. }
  523. if (!found)
  524. return idx;
  525. }
  526. }
  527. static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
  528. {
  529. u32 *p1 = (u32 *) s1;
  530. u32 *p2 = (u32 *) s2;
  531. int len = sizeof(struct xfrm_selector) / sizeof(u32);
  532. int i;
  533. for (i = 0; i < len; i++) {
  534. if (p1[i] != p2[i])
  535. return 1;
  536. }
  537. return 0;
  538. }
  539. int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
  540. {
  541. struct xfrm_policy *pol;
  542. struct xfrm_policy *delpol;
  543. struct hlist_head *chain;
  544. struct hlist_node *entry, *newpos;
  545. struct dst_entry *gc_list;
  546. write_lock_bh(&xfrm_policy_lock);
  547. chain = policy_hash_bysel(&policy->selector, policy->family, dir);
  548. delpol = NULL;
  549. newpos = NULL;
  550. hlist_for_each_entry(pol, entry, chain, bydst) {
  551. if (pol->type == policy->type &&
  552. !selector_cmp(&pol->selector, &policy->selector) &&
  553. xfrm_sec_ctx_match(pol->security, policy->security) &&
  554. !WARN_ON(delpol)) {
  555. if (excl) {
  556. write_unlock_bh(&xfrm_policy_lock);
  557. return -EEXIST;
  558. }
  559. delpol = pol;
  560. if (policy->priority > pol->priority)
  561. continue;
  562. } else if (policy->priority >= pol->priority) {
  563. newpos = &pol->bydst;
  564. continue;
  565. }
  566. if (delpol)
  567. break;
  568. }
  569. if (newpos)
  570. hlist_add_after(newpos, &policy->bydst);
  571. else
  572. hlist_add_head(&policy->bydst, chain);
  573. xfrm_pol_hold(policy);
  574. xfrm_policy_count[dir]++;
  575. atomic_inc(&flow_cache_genid);
  576. if (delpol) {
  577. hlist_del(&delpol->bydst);
  578. hlist_del(&delpol->byidx);
  579. xfrm_policy_count[dir]--;
  580. }
  581. policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
  582. hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
  583. policy->curlft.add_time = get_seconds();
  584. policy->curlft.use_time = 0;
  585. if (!mod_timer(&policy->timer, jiffies + HZ))
  586. xfrm_pol_hold(policy);
  587. write_unlock_bh(&xfrm_policy_lock);
  588. if (delpol)
  589. xfrm_policy_kill(delpol);
  590. else if (xfrm_bydst_should_resize(dir, NULL))
  591. schedule_work(&xfrm_hash_work);
  592. read_lock_bh(&xfrm_policy_lock);
  593. gc_list = NULL;
  594. entry = &policy->bydst;
  595. hlist_for_each_entry_continue(policy, entry, bydst) {
  596. struct dst_entry *dst;
  597. write_lock(&policy->lock);
  598. dst = policy->bundles;
  599. if (dst) {
  600. struct dst_entry *tail = dst;
  601. while (tail->next)
  602. tail = tail->next;
  603. tail->next = gc_list;
  604. gc_list = dst;
  605. policy->bundles = NULL;
  606. }
  607. write_unlock(&policy->lock);
  608. }
  609. read_unlock_bh(&xfrm_policy_lock);
  610. while (gc_list) {
  611. struct dst_entry *dst = gc_list;
  612. gc_list = dst->next;
  613. dst_free(dst);
  614. }
  615. return 0;
  616. }
  617. EXPORT_SYMBOL(xfrm_policy_insert);
  618. struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
  619. struct xfrm_selector *sel,
  620. struct xfrm_sec_ctx *ctx, int delete,
  621. int *err)
  622. {
  623. struct xfrm_policy *pol, *ret;
  624. struct hlist_head *chain;
  625. struct hlist_node *entry;
  626. *err = 0;
  627. write_lock_bh(&xfrm_policy_lock);
  628. chain = policy_hash_bysel(sel, sel->family, dir);
  629. ret = NULL;
  630. hlist_for_each_entry(pol, entry, chain, bydst) {
  631. if (pol->type == type &&
  632. !selector_cmp(sel, &pol->selector) &&
  633. xfrm_sec_ctx_match(ctx, pol->security)) {
  634. xfrm_pol_hold(pol);
  635. if (delete) {
  636. *err = security_xfrm_policy_delete(pol);
  637. if (*err) {
  638. write_unlock_bh(&xfrm_policy_lock);
  639. return pol;
  640. }
  641. hlist_del(&pol->bydst);
  642. hlist_del(&pol->byidx);
  643. xfrm_policy_count[dir]--;
  644. }
  645. ret = pol;
  646. break;
  647. }
  648. }
  649. write_unlock_bh(&xfrm_policy_lock);
  650. if (ret && delete) {
  651. atomic_inc(&flow_cache_genid);
  652. xfrm_policy_kill(ret);
  653. }
  654. return ret;
  655. }
  656. EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
  657. struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete,
  658. int *err)
  659. {
  660. struct xfrm_policy *pol, *ret;
  661. struct hlist_head *chain;
  662. struct hlist_node *entry;
  663. *err = 0;
  664. write_lock_bh(&xfrm_policy_lock);
  665. chain = xfrm_policy_byidx + idx_hash(id);
  666. ret = NULL;
  667. hlist_for_each_entry(pol, entry, chain, byidx) {
  668. if (pol->type == type && pol->index == id) {
  669. xfrm_pol_hold(pol);
  670. if (delete) {
  671. *err = security_xfrm_policy_delete(pol);
  672. if (*err) {
  673. write_unlock_bh(&xfrm_policy_lock);
  674. return pol;
  675. }
  676. hlist_del(&pol->bydst);
  677. hlist_del(&pol->byidx);
  678. xfrm_policy_count[dir]--;
  679. }
  680. ret = pol;
  681. break;
  682. }
  683. }
  684. write_unlock_bh(&xfrm_policy_lock);
  685. if (ret && delete) {
  686. atomic_inc(&flow_cache_genid);
  687. xfrm_policy_kill(ret);
  688. }
  689. return ret;
  690. }
  691. EXPORT_SYMBOL(xfrm_policy_byid);
  692. void xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
  693. {
  694. int dir;
  695. write_lock_bh(&xfrm_policy_lock);
  696. for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
  697. struct xfrm_policy *pol;
  698. struct hlist_node *entry;
  699. int i, killed;
  700. killed = 0;
  701. again1:
  702. hlist_for_each_entry(pol, entry,
  703. &xfrm_policy_inexact[dir], bydst) {
  704. if (pol->type != type)
  705. continue;
  706. hlist_del(&pol->bydst);
  707. hlist_del(&pol->byidx);
  708. write_unlock_bh(&xfrm_policy_lock);
  709. xfrm_audit_log(audit_info->loginuid, audit_info->secid,
  710. AUDIT_MAC_IPSEC_DELSPD, 1, pol, NULL);
  711. xfrm_policy_kill(pol);
  712. killed++;
  713. write_lock_bh(&xfrm_policy_lock);
  714. goto again1;
  715. }
  716. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  717. again2:
  718. hlist_for_each_entry(pol, entry,
  719. xfrm_policy_bydst[dir].table + i,
  720. bydst) {
  721. if (pol->type != type)
  722. continue;
  723. hlist_del(&pol->bydst);
  724. hlist_del(&pol->byidx);
  725. write_unlock_bh(&xfrm_policy_lock);
  726. xfrm_audit_log(audit_info->loginuid,
  727. audit_info->secid,
  728. AUDIT_MAC_IPSEC_DELSPD, 1,
  729. pol, NULL);
  730. xfrm_policy_kill(pol);
  731. killed++;
  732. write_lock_bh(&xfrm_policy_lock);
  733. goto again2;
  734. }
  735. }
  736. xfrm_policy_count[dir] -= killed;
  737. }
  738. atomic_inc(&flow_cache_genid);
  739. write_unlock_bh(&xfrm_policy_lock);
  740. }
  741. EXPORT_SYMBOL(xfrm_policy_flush);
  742. int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
  743. void *data)
  744. {
  745. struct xfrm_policy *pol, *last = NULL;
  746. struct hlist_node *entry;
  747. int dir, last_dir = 0, count, error;
  748. read_lock_bh(&xfrm_policy_lock);
  749. count = 0;
  750. for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
  751. struct hlist_head *table = xfrm_policy_bydst[dir].table;
  752. int i;
  753. hlist_for_each_entry(pol, entry,
  754. &xfrm_policy_inexact[dir], bydst) {
  755. if (pol->type != type)
  756. continue;
  757. if (last) {
  758. error = func(last, last_dir % XFRM_POLICY_MAX,
  759. count, data);
  760. if (error)
  761. goto out;
  762. }
  763. last = pol;
  764. last_dir = dir;
  765. count++;
  766. }
  767. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  768. hlist_for_each_entry(pol, entry, table + i, bydst) {
  769. if (pol->type != type)
  770. continue;
  771. if (last) {
  772. error = func(last, last_dir % XFRM_POLICY_MAX,
  773. count, data);
  774. if (error)
  775. goto out;
  776. }
  777. last = pol;
  778. last_dir = dir;
  779. count++;
  780. }
  781. }
  782. }
  783. if (count == 0) {
  784. error = -ENOENT;
  785. goto out;
  786. }
  787. error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
  788. out:
  789. read_unlock_bh(&xfrm_policy_lock);
  790. return error;
  791. }
  792. EXPORT_SYMBOL(xfrm_policy_walk);
  793. /*
  794. * Find policy to apply to this flow.
  795. *
  796. * Returns 0 if policy found, else an -errno.
  797. */
  798. static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
  799. u8 type, u16 family, int dir)
  800. {
  801. struct xfrm_selector *sel = &pol->selector;
  802. int match, ret = -ESRCH;
  803. if (pol->family != family ||
  804. pol->type != type)
  805. return ret;
  806. match = xfrm_selector_match(sel, fl, family);
  807. if (match)
  808. ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
  809. return ret;
  810. }
  811. static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
  812. u16 family, u8 dir)
  813. {
  814. int err;
  815. struct xfrm_policy *pol, *ret;
  816. xfrm_address_t *daddr, *saddr;
  817. struct hlist_node *entry;
  818. struct hlist_head *chain;
  819. u32 priority = ~0U;
  820. daddr = xfrm_flowi_daddr(fl, family);
  821. saddr = xfrm_flowi_saddr(fl, family);
  822. if (unlikely(!daddr || !saddr))
  823. return NULL;
  824. read_lock_bh(&xfrm_policy_lock);
  825. chain = policy_hash_direct(daddr, saddr, family, dir);
  826. ret = NULL;
  827. hlist_for_each_entry(pol, entry, chain, bydst) {
  828. err = xfrm_policy_match(pol, fl, type, family, dir);
  829. if (err) {
  830. if (err == -ESRCH)
  831. continue;
  832. else {
  833. ret = ERR_PTR(err);
  834. goto fail;
  835. }
  836. } else {
  837. ret = pol;
  838. priority = ret->priority;
  839. break;
  840. }
  841. }
  842. chain = &xfrm_policy_inexact[dir];
  843. hlist_for_each_entry(pol, entry, chain, bydst) {
  844. err = xfrm_policy_match(pol, fl, type, family, dir);
  845. if (err) {
  846. if (err == -ESRCH)
  847. continue;
  848. else {
  849. ret = ERR_PTR(err);
  850. goto fail;
  851. }
  852. } else if (pol->priority < priority) {
  853. ret = pol;
  854. break;
  855. }
  856. }
  857. if (ret)
  858. xfrm_pol_hold(ret);
  859. fail:
  860. read_unlock_bh(&xfrm_policy_lock);
  861. return ret;
  862. }
  863. static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
  864. void **objp, atomic_t **obj_refp)
  865. {
  866. struct xfrm_policy *pol;
  867. int err = 0;
  868. #ifdef CONFIG_XFRM_SUB_POLICY
  869. pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
  870. if (IS_ERR(pol)) {
  871. err = PTR_ERR(pol);
  872. pol = NULL;
  873. }
  874. if (pol || err)
  875. goto end;
  876. #endif
  877. pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
  878. if (IS_ERR(pol)) {
  879. err = PTR_ERR(pol);
  880. pol = NULL;
  881. }
  882. #ifdef CONFIG_XFRM_SUB_POLICY
  883. end:
  884. #endif
  885. if ((*objp = (void *) pol) != NULL)
  886. *obj_refp = &pol->refcnt;
  887. return err;
  888. }
  889. static inline int policy_to_flow_dir(int dir)
  890. {
  891. if (XFRM_POLICY_IN == FLOW_DIR_IN &&
  892. XFRM_POLICY_OUT == FLOW_DIR_OUT &&
  893. XFRM_POLICY_FWD == FLOW_DIR_FWD)
  894. return dir;
  895. switch (dir) {
  896. default:
  897. case XFRM_POLICY_IN:
  898. return FLOW_DIR_IN;
  899. case XFRM_POLICY_OUT:
  900. return FLOW_DIR_OUT;
  901. case XFRM_POLICY_FWD:
  902. return FLOW_DIR_FWD;
  903. }
  904. }
  905. static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
  906. {
  907. struct xfrm_policy *pol;
  908. read_lock_bh(&xfrm_policy_lock);
  909. if ((pol = sk->sk_policy[dir]) != NULL) {
  910. int match = xfrm_selector_match(&pol->selector, fl,
  911. sk->sk_family);
  912. int err = 0;
  913. if (match) {
  914. err = security_xfrm_policy_lookup(pol, fl->secid,
  915. policy_to_flow_dir(dir));
  916. if (!err)
  917. xfrm_pol_hold(pol);
  918. else if (err == -ESRCH)
  919. pol = NULL;
  920. else
  921. pol = ERR_PTR(err);
  922. } else
  923. pol = NULL;
  924. }
  925. read_unlock_bh(&xfrm_policy_lock);
  926. return pol;
  927. }
  928. static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
  929. {
  930. struct hlist_head *chain = policy_hash_bysel(&pol->selector,
  931. pol->family, dir);
  932. hlist_add_head(&pol->bydst, chain);
  933. hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
  934. xfrm_policy_count[dir]++;
  935. xfrm_pol_hold(pol);
  936. if (xfrm_bydst_should_resize(dir, NULL))
  937. schedule_work(&xfrm_hash_work);
  938. }
  939. static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
  940. int dir)
  941. {
  942. if (hlist_unhashed(&pol->bydst))
  943. return NULL;
  944. hlist_del(&pol->bydst);
  945. hlist_del(&pol->byidx);
  946. xfrm_policy_count[dir]--;
  947. return pol;
  948. }
  949. int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
  950. {
  951. write_lock_bh(&xfrm_policy_lock);
  952. pol = __xfrm_policy_unlink(pol, dir);
  953. write_unlock_bh(&xfrm_policy_lock);
  954. if (pol) {
  955. if (dir < XFRM_POLICY_MAX)
  956. atomic_inc(&flow_cache_genid);
  957. xfrm_policy_kill(pol);
  958. return 0;
  959. }
  960. return -ENOENT;
  961. }
  962. EXPORT_SYMBOL(xfrm_policy_delete);
  963. int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
  964. {
  965. struct xfrm_policy *old_pol;
  966. #ifdef CONFIG_XFRM_SUB_POLICY
  967. if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
  968. return -EINVAL;
  969. #endif
  970. write_lock_bh(&xfrm_policy_lock);
  971. old_pol = sk->sk_policy[dir];
  972. sk->sk_policy[dir] = pol;
  973. if (pol) {
  974. pol->curlft.add_time = get_seconds();
  975. pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
  976. __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
  977. }
  978. if (old_pol)
  979. __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
  980. write_unlock_bh(&xfrm_policy_lock);
  981. if (old_pol) {
  982. xfrm_policy_kill(old_pol);
  983. }
  984. return 0;
  985. }
  986. static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
  987. {
  988. struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
  989. if (newp) {
  990. newp->selector = old->selector;
  991. if (security_xfrm_policy_clone(old, newp)) {
  992. kfree(newp);
  993. return NULL; /* ENOMEM */
  994. }
  995. newp->lft = old->lft;
  996. newp->curlft = old->curlft;
  997. newp->action = old->action;
  998. newp->flags = old->flags;
  999. newp->xfrm_nr = old->xfrm_nr;
  1000. newp->index = old->index;
  1001. newp->type = old->type;
  1002. memcpy(newp->xfrm_vec, old->xfrm_vec,
  1003. newp->xfrm_nr*sizeof(struct xfrm_tmpl));
  1004. write_lock_bh(&xfrm_policy_lock);
  1005. __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
  1006. write_unlock_bh(&xfrm_policy_lock);
  1007. xfrm_pol_put(newp);
  1008. }
  1009. return newp;
  1010. }
  1011. int __xfrm_sk_clone_policy(struct sock *sk)
  1012. {
  1013. struct xfrm_policy *p0 = sk->sk_policy[0],
  1014. *p1 = sk->sk_policy[1];
  1015. sk->sk_policy[0] = sk->sk_policy[1] = NULL;
  1016. if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
  1017. return -ENOMEM;
  1018. if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
  1019. return -ENOMEM;
  1020. return 0;
  1021. }
  1022. static int
  1023. xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
  1024. unsigned short family)
  1025. {
  1026. int err;
  1027. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1028. if (unlikely(afinfo == NULL))
  1029. return -EINVAL;
  1030. err = afinfo->get_saddr(local, remote);
  1031. xfrm_policy_put_afinfo(afinfo);
  1032. return err;
  1033. }
  1034. /* Resolve list of templates for the flow, given policy. */
  1035. static int
  1036. xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
  1037. struct xfrm_state **xfrm,
  1038. unsigned short family)
  1039. {
  1040. int nx;
  1041. int i, error;
  1042. xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
  1043. xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
  1044. xfrm_address_t tmp;
  1045. for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
  1046. struct xfrm_state *x;
  1047. xfrm_address_t *remote = daddr;
  1048. xfrm_address_t *local = saddr;
  1049. struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
  1050. if (tmpl->mode == XFRM_MODE_TUNNEL) {
  1051. remote = &tmpl->id.daddr;
  1052. local = &tmpl->saddr;
  1053. family = tmpl->encap_family;
  1054. if (xfrm_addr_any(local, family)) {
  1055. error = xfrm_get_saddr(&tmp, remote, family);
  1056. if (error)
  1057. goto fail;
  1058. local = &tmp;
  1059. }
  1060. }
  1061. x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
  1062. if (x && x->km.state == XFRM_STATE_VALID) {
  1063. xfrm[nx++] = x;
  1064. daddr = remote;
  1065. saddr = local;
  1066. continue;
  1067. }
  1068. if (x) {
  1069. error = (x->km.state == XFRM_STATE_ERROR ?
  1070. -EINVAL : -EAGAIN);
  1071. xfrm_state_put(x);
  1072. }
  1073. if (!tmpl->optional)
  1074. goto fail;
  1075. }
  1076. return nx;
  1077. fail:
  1078. for (nx--; nx>=0; nx--)
  1079. xfrm_state_put(xfrm[nx]);
  1080. return error;
  1081. }
  1082. static int
  1083. xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
  1084. struct xfrm_state **xfrm,
  1085. unsigned short family)
  1086. {
  1087. struct xfrm_state *tp[XFRM_MAX_DEPTH];
  1088. struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
  1089. int cnx = 0;
  1090. int error;
  1091. int ret;
  1092. int i;
  1093. for (i = 0; i < npols; i++) {
  1094. if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
  1095. error = -ENOBUFS;
  1096. goto fail;
  1097. }
  1098. ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
  1099. if (ret < 0) {
  1100. error = ret;
  1101. goto fail;
  1102. } else
  1103. cnx += ret;
  1104. }
  1105. /* found states are sorted for outbound processing */
  1106. if (npols > 1)
  1107. xfrm_state_sort(xfrm, tpp, cnx, family);
  1108. return cnx;
  1109. fail:
  1110. for (cnx--; cnx>=0; cnx--)
  1111. xfrm_state_put(tpp[cnx]);
  1112. return error;
  1113. }
  1114. /* Check that the bundle accepts the flow and its components are
  1115. * still valid.
  1116. */
  1117. static struct dst_entry *
  1118. xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
  1119. {
  1120. struct dst_entry *x;
  1121. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1122. if (unlikely(afinfo == NULL))
  1123. return ERR_PTR(-EINVAL);
  1124. x = afinfo->find_bundle(fl, policy);
  1125. xfrm_policy_put_afinfo(afinfo);
  1126. return x;
  1127. }
  1128. /* Allocate chain of dst_entry's, attach known xfrm's, calculate
  1129. * all the metrics... Shortly, bundle a bundle.
  1130. */
  1131. static int
  1132. xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
  1133. struct flowi *fl, struct dst_entry **dst_p,
  1134. unsigned short family)
  1135. {
  1136. int err;
  1137. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1138. if (unlikely(afinfo == NULL))
  1139. return -EINVAL;
  1140. err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
  1141. xfrm_policy_put_afinfo(afinfo);
  1142. return err;
  1143. }
  1144. static int inline
  1145. xfrm_dst_alloc_copy(void **target, void *src, int size)
  1146. {
  1147. if (!*target) {
  1148. *target = kmalloc(size, GFP_ATOMIC);
  1149. if (!*target)
  1150. return -ENOMEM;
  1151. }
  1152. memcpy(*target, src, size);
  1153. return 0;
  1154. }
  1155. static int inline
  1156. xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
  1157. {
  1158. #ifdef CONFIG_XFRM_SUB_POLICY
  1159. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1160. return xfrm_dst_alloc_copy((void **)&(xdst->partner),
  1161. sel, sizeof(*sel));
  1162. #else
  1163. return 0;
  1164. #endif
  1165. }
  1166. static int inline
  1167. xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
  1168. {
  1169. #ifdef CONFIG_XFRM_SUB_POLICY
  1170. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1171. return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
  1172. #else
  1173. return 0;
  1174. #endif
  1175. }
  1176. static int stale_bundle(struct dst_entry *dst);
  1177. /* Main function: finds/creates a bundle for given flow.
  1178. *
  1179. * At the moment we eat a raw IP route. Mostly to speed up lookups
  1180. * on interfaces with disabled IPsec.
  1181. */
  1182. int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1183. struct sock *sk, int flags)
  1184. {
  1185. struct xfrm_policy *policy;
  1186. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1187. int npols;
  1188. int pol_dead;
  1189. int xfrm_nr;
  1190. int pi;
  1191. struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
  1192. struct dst_entry *dst, *dst_orig = *dst_p;
  1193. int nx = 0;
  1194. int err;
  1195. u32 genid;
  1196. u16 family;
  1197. u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
  1198. restart:
  1199. genid = atomic_read(&flow_cache_genid);
  1200. policy = NULL;
  1201. for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
  1202. pols[pi] = NULL;
  1203. npols = 0;
  1204. pol_dead = 0;
  1205. xfrm_nr = 0;
  1206. if (sk && sk->sk_policy[1]) {
  1207. policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
  1208. if (IS_ERR(policy))
  1209. return PTR_ERR(policy);
  1210. }
  1211. if (!policy) {
  1212. /* To accelerate a bit... */
  1213. if ((dst_orig->flags & DST_NOXFRM) ||
  1214. !xfrm_policy_count[XFRM_POLICY_OUT])
  1215. return 0;
  1216. policy = flow_cache_lookup(fl, dst_orig->ops->family,
  1217. dir, xfrm_policy_lookup);
  1218. if (IS_ERR(policy))
  1219. return PTR_ERR(policy);
  1220. }
  1221. if (!policy)
  1222. return 0;
  1223. family = dst_orig->ops->family;
  1224. policy->curlft.use_time = get_seconds();
  1225. pols[0] = policy;
  1226. npols ++;
  1227. xfrm_nr += pols[0]->xfrm_nr;
  1228. switch (policy->action) {
  1229. case XFRM_POLICY_BLOCK:
  1230. /* Prohibit the flow */
  1231. err = -EPERM;
  1232. goto error;
  1233. case XFRM_POLICY_ALLOW:
  1234. #ifndef CONFIG_XFRM_SUB_POLICY
  1235. if (policy->xfrm_nr == 0) {
  1236. /* Flow passes not transformed. */
  1237. xfrm_pol_put(policy);
  1238. return 0;
  1239. }
  1240. #endif
  1241. /* Try to find matching bundle.
  1242. *
  1243. * LATER: help from flow cache. It is optional, this
  1244. * is required only for output policy.
  1245. */
  1246. dst = xfrm_find_bundle(fl, policy, family);
  1247. if (IS_ERR(dst)) {
  1248. err = PTR_ERR(dst);
  1249. goto error;
  1250. }
  1251. if (dst)
  1252. break;
  1253. #ifdef CONFIG_XFRM_SUB_POLICY
  1254. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1255. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1256. fl, family,
  1257. XFRM_POLICY_OUT);
  1258. if (pols[1]) {
  1259. if (IS_ERR(pols[1])) {
  1260. err = PTR_ERR(pols[1]);
  1261. goto error;
  1262. }
  1263. if (pols[1]->action == XFRM_POLICY_BLOCK) {
  1264. err = -EPERM;
  1265. goto error;
  1266. }
  1267. npols ++;
  1268. xfrm_nr += pols[1]->xfrm_nr;
  1269. }
  1270. }
  1271. /*
  1272. * Because neither flowi nor bundle information knows about
  1273. * transformation template size. On more than one policy usage
  1274. * we can realize whether all of them is bypass or not after
  1275. * they are searched. See above not-transformed bypass
  1276. * is surrounded by non-sub policy configuration, too.
  1277. */
  1278. if (xfrm_nr == 0) {
  1279. /* Flow passes not transformed. */
  1280. xfrm_pols_put(pols, npols);
  1281. return 0;
  1282. }
  1283. #endif
  1284. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1285. if (unlikely(nx<0)) {
  1286. err = nx;
  1287. if (err == -EAGAIN && flags) {
  1288. DECLARE_WAITQUEUE(wait, current);
  1289. add_wait_queue(&km_waitq, &wait);
  1290. set_current_state(TASK_INTERRUPTIBLE);
  1291. schedule();
  1292. set_current_state(TASK_RUNNING);
  1293. remove_wait_queue(&km_waitq, &wait);
  1294. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1295. if (nx == -EAGAIN && signal_pending(current)) {
  1296. err = -ERESTART;
  1297. goto error;
  1298. }
  1299. if (nx == -EAGAIN ||
  1300. genid != atomic_read(&flow_cache_genid)) {
  1301. xfrm_pols_put(pols, npols);
  1302. goto restart;
  1303. }
  1304. err = nx;
  1305. }
  1306. if (err < 0)
  1307. goto error;
  1308. }
  1309. if (nx == 0) {
  1310. /* Flow passes not transformed. */
  1311. xfrm_pols_put(pols, npols);
  1312. return 0;
  1313. }
  1314. dst = dst_orig;
  1315. err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
  1316. if (unlikely(err)) {
  1317. int i;
  1318. for (i=0; i<nx; i++)
  1319. xfrm_state_put(xfrm[i]);
  1320. goto error;
  1321. }
  1322. for (pi = 0; pi < npols; pi++) {
  1323. read_lock_bh(&pols[pi]->lock);
  1324. pol_dead |= pols[pi]->dead;
  1325. read_unlock_bh(&pols[pi]->lock);
  1326. }
  1327. write_lock_bh(&policy->lock);
  1328. if (unlikely(pol_dead || stale_bundle(dst))) {
  1329. /* Wow! While we worked on resolving, this
  1330. * policy has gone. Retry. It is not paranoia,
  1331. * we just cannot enlist new bundle to dead object.
  1332. * We can't enlist stable bundles either.
  1333. */
  1334. write_unlock_bh(&policy->lock);
  1335. if (dst)
  1336. dst_free(dst);
  1337. err = -EHOSTUNREACH;
  1338. goto error;
  1339. }
  1340. if (npols > 1)
  1341. err = xfrm_dst_update_parent(dst, &pols[1]->selector);
  1342. else
  1343. err = xfrm_dst_update_origin(dst, fl);
  1344. if (unlikely(err)) {
  1345. write_unlock_bh(&policy->lock);
  1346. if (dst)
  1347. dst_free(dst);
  1348. goto error;
  1349. }
  1350. dst->next = policy->bundles;
  1351. policy->bundles = dst;
  1352. dst_hold(dst);
  1353. write_unlock_bh(&policy->lock);
  1354. }
  1355. *dst_p = dst;
  1356. dst_release(dst_orig);
  1357. xfrm_pols_put(pols, npols);
  1358. return 0;
  1359. error:
  1360. dst_release(dst_orig);
  1361. xfrm_pols_put(pols, npols);
  1362. *dst_p = NULL;
  1363. return err;
  1364. }
  1365. EXPORT_SYMBOL(xfrm_lookup);
  1366. static inline int
  1367. xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
  1368. {
  1369. struct xfrm_state *x;
  1370. int err;
  1371. if (!skb->sp || idx < 0 || idx >= skb->sp->len)
  1372. return 0;
  1373. x = skb->sp->xvec[idx];
  1374. if (!x->type->reject)
  1375. return 0;
  1376. xfrm_state_hold(x);
  1377. err = x->type->reject(x, skb, fl);
  1378. xfrm_state_put(x);
  1379. return err;
  1380. }
  1381. /* When skb is transformed back to its "native" form, we have to
  1382. * check policy restrictions. At the moment we make this in maximally
  1383. * stupid way. Shame on me. :-) Of course, connected sockets must
  1384. * have policy cached at them.
  1385. */
  1386. static inline int
  1387. xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
  1388. unsigned short family)
  1389. {
  1390. if (xfrm_state_kern(x))
  1391. return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
  1392. return x->id.proto == tmpl->id.proto &&
  1393. (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
  1394. (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
  1395. x->props.mode == tmpl->mode &&
  1396. ((tmpl->aalgos & (1<<x->props.aalgo)) ||
  1397. !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
  1398. !(x->props.mode != XFRM_MODE_TRANSPORT &&
  1399. xfrm_state_addr_cmp(tmpl, x, family));
  1400. }
  1401. /*
  1402. * 0 or more than 0 is returned when validation is succeeded (either bypass
  1403. * because of optional transport mode, or next index of the mathced secpath
  1404. * state with the template.
  1405. * -1 is returned when no matching template is found.
  1406. * Otherwise "-2 - errored_index" is returned.
  1407. */
  1408. static inline int
  1409. xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
  1410. unsigned short family)
  1411. {
  1412. int idx = start;
  1413. if (tmpl->optional) {
  1414. if (tmpl->mode == XFRM_MODE_TRANSPORT)
  1415. return start;
  1416. } else
  1417. start = -1;
  1418. for (; idx < sp->len; idx++) {
  1419. if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
  1420. return ++idx;
  1421. if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
  1422. if (start == -1)
  1423. start = -2-idx;
  1424. break;
  1425. }
  1426. }
  1427. return start;
  1428. }
  1429. int
  1430. xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
  1431. {
  1432. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1433. int err;
  1434. if (unlikely(afinfo == NULL))
  1435. return -EAFNOSUPPORT;
  1436. afinfo->decode_session(skb, fl);
  1437. err = security_xfrm_decode_session(skb, &fl->secid);
  1438. xfrm_policy_put_afinfo(afinfo);
  1439. return err;
  1440. }
  1441. EXPORT_SYMBOL(xfrm_decode_session);
  1442. static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
  1443. {
  1444. for (; k < sp->len; k++) {
  1445. if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
  1446. *idxp = k;
  1447. return 1;
  1448. }
  1449. }
  1450. return 0;
  1451. }
  1452. int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
  1453. unsigned short family)
  1454. {
  1455. struct xfrm_policy *pol;
  1456. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1457. int npols = 0;
  1458. int xfrm_nr;
  1459. int pi;
  1460. struct flowi fl;
  1461. u8 fl_dir = policy_to_flow_dir(dir);
  1462. int xerr_idx = -1;
  1463. if (xfrm_decode_session(skb, &fl, family) < 0)
  1464. return 0;
  1465. nf_nat_decode_session(skb, &fl, family);
  1466. /* First, check used SA against their selectors. */
  1467. if (skb->sp) {
  1468. int i;
  1469. for (i=skb->sp->len-1; i>=0; i--) {
  1470. struct xfrm_state *x = skb->sp->xvec[i];
  1471. if (!xfrm_selector_match(&x->sel, &fl, family))
  1472. return 0;
  1473. }
  1474. }
  1475. pol = NULL;
  1476. if (sk && sk->sk_policy[dir]) {
  1477. pol = xfrm_sk_policy_lookup(sk, dir, &fl);
  1478. if (IS_ERR(pol))
  1479. return 0;
  1480. }
  1481. if (!pol)
  1482. pol = flow_cache_lookup(&fl, family, fl_dir,
  1483. xfrm_policy_lookup);
  1484. if (IS_ERR(pol))
  1485. return 0;
  1486. if (!pol) {
  1487. if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
  1488. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1489. return 0;
  1490. }
  1491. return 1;
  1492. }
  1493. pol->curlft.use_time = get_seconds();
  1494. pols[0] = pol;
  1495. npols ++;
  1496. #ifdef CONFIG_XFRM_SUB_POLICY
  1497. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1498. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1499. &fl, family,
  1500. XFRM_POLICY_IN);
  1501. if (pols[1]) {
  1502. if (IS_ERR(pols[1]))
  1503. return 0;
  1504. pols[1]->curlft.use_time = get_seconds();
  1505. npols ++;
  1506. }
  1507. }
  1508. #endif
  1509. if (pol->action == XFRM_POLICY_ALLOW) {
  1510. struct sec_path *sp;
  1511. static struct sec_path dummy;
  1512. struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
  1513. struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
  1514. struct xfrm_tmpl **tpp = tp;
  1515. int ti = 0;
  1516. int i, k;
  1517. if ((sp = skb->sp) == NULL)
  1518. sp = &dummy;
  1519. for (pi = 0; pi < npols; pi++) {
  1520. if (pols[pi] != pol &&
  1521. pols[pi]->action != XFRM_POLICY_ALLOW)
  1522. goto reject;
  1523. if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
  1524. goto reject_error;
  1525. for (i = 0; i < pols[pi]->xfrm_nr; i++)
  1526. tpp[ti++] = &pols[pi]->xfrm_vec[i];
  1527. }
  1528. xfrm_nr = ti;
  1529. if (npols > 1) {
  1530. xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
  1531. tpp = stp;
  1532. }
  1533. /* For each tunnel xfrm, find the first matching tmpl.
  1534. * For each tmpl before that, find corresponding xfrm.
  1535. * Order is _important_. Later we will implement
  1536. * some barriers, but at the moment barriers
  1537. * are implied between each two transformations.
  1538. */
  1539. for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
  1540. k = xfrm_policy_ok(tpp[i], sp, k, family);
  1541. if (k < 0) {
  1542. if (k < -1)
  1543. /* "-2 - errored_index" returned */
  1544. xerr_idx = -(2+k);
  1545. goto reject;
  1546. }
  1547. }
  1548. if (secpath_has_nontransport(sp, k, &xerr_idx))
  1549. goto reject;
  1550. xfrm_pols_put(pols, npols);
  1551. return 1;
  1552. }
  1553. reject:
  1554. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1555. reject_error:
  1556. xfrm_pols_put(pols, npols);
  1557. return 0;
  1558. }
  1559. EXPORT_SYMBOL(__xfrm_policy_check);
  1560. int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
  1561. {
  1562. struct flowi fl;
  1563. if (xfrm_decode_session(skb, &fl, family) < 0)
  1564. return 0;
  1565. return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
  1566. }
  1567. EXPORT_SYMBOL(__xfrm_route_forward);
  1568. /* Optimize later using cookies and generation ids. */
  1569. static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
  1570. {
  1571. /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
  1572. * to "-1" to force all XFRM destinations to get validated by
  1573. * dst_ops->check on every use. We do this because when a
  1574. * normal route referenced by an XFRM dst is obsoleted we do
  1575. * not go looking around for all parent referencing XFRM dsts
  1576. * so that we can invalidate them. It is just too much work.
  1577. * Instead we make the checks here on every use. For example:
  1578. *
  1579. * XFRM dst A --> IPv4 dst X
  1580. *
  1581. * X is the "xdst->route" of A (X is also the "dst->path" of A
  1582. * in this example). If X is marked obsolete, "A" will not
  1583. * notice. That's what we are validating here via the
  1584. * stale_bundle() check.
  1585. *
  1586. * When a policy's bundle is pruned, we dst_free() the XFRM
  1587. * dst which causes it's ->obsolete field to be set to a
  1588. * positive non-zero integer. If an XFRM dst has been pruned
  1589. * like this, we want to force a new route lookup.
  1590. */
  1591. if (dst->obsolete < 0 && !stale_bundle(dst))
  1592. return dst;
  1593. return NULL;
  1594. }
  1595. static int stale_bundle(struct dst_entry *dst)
  1596. {
  1597. return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
  1598. }
  1599. void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
  1600. {
  1601. while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
  1602. dst->dev = &loopback_dev;
  1603. dev_hold(&loopback_dev);
  1604. dev_put(dev);
  1605. }
  1606. }
  1607. EXPORT_SYMBOL(xfrm_dst_ifdown);
  1608. static void xfrm_link_failure(struct sk_buff *skb)
  1609. {
  1610. /* Impossible. Such dst must be popped before reaches point of failure. */
  1611. return;
  1612. }
  1613. static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
  1614. {
  1615. if (dst) {
  1616. if (dst->obsolete) {
  1617. dst_release(dst);
  1618. dst = NULL;
  1619. }
  1620. }
  1621. return dst;
  1622. }
  1623. static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
  1624. {
  1625. struct dst_entry *dst, **dstp;
  1626. write_lock(&pol->lock);
  1627. dstp = &pol->bundles;
  1628. while ((dst=*dstp) != NULL) {
  1629. if (func(dst)) {
  1630. *dstp = dst->next;
  1631. dst->next = *gc_list_p;
  1632. *gc_list_p = dst;
  1633. } else {
  1634. dstp = &dst->next;
  1635. }
  1636. }
  1637. write_unlock(&pol->lock);
  1638. }
  1639. static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
  1640. {
  1641. struct dst_entry *gc_list = NULL;
  1642. int dir;
  1643. read_lock_bh(&xfrm_policy_lock);
  1644. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1645. struct xfrm_policy *pol;
  1646. struct hlist_node *entry;
  1647. struct hlist_head *table;
  1648. int i;
  1649. hlist_for_each_entry(pol, entry,
  1650. &xfrm_policy_inexact[dir], bydst)
  1651. prune_one_bundle(pol, func, &gc_list);
  1652. table = xfrm_policy_bydst[dir].table;
  1653. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  1654. hlist_for_each_entry(pol, entry, table + i, bydst)
  1655. prune_one_bundle(pol, func, &gc_list);
  1656. }
  1657. }
  1658. read_unlock_bh(&xfrm_policy_lock);
  1659. while (gc_list) {
  1660. struct dst_entry *dst = gc_list;
  1661. gc_list = dst->next;
  1662. dst_free(dst);
  1663. }
  1664. }
  1665. static int unused_bundle(struct dst_entry *dst)
  1666. {
  1667. return !atomic_read(&dst->__refcnt);
  1668. }
  1669. static void __xfrm_garbage_collect(void)
  1670. {
  1671. xfrm_prune_bundles(unused_bundle);
  1672. }
  1673. static int xfrm_flush_bundles(void)
  1674. {
  1675. xfrm_prune_bundles(stale_bundle);
  1676. return 0;
  1677. }
  1678. void xfrm_init_pmtu(struct dst_entry *dst)
  1679. {
  1680. do {
  1681. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1682. u32 pmtu, route_mtu_cached;
  1683. pmtu = dst_mtu(dst->child);
  1684. xdst->child_mtu_cached = pmtu;
  1685. pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
  1686. route_mtu_cached = dst_mtu(xdst->route);
  1687. xdst->route_mtu_cached = route_mtu_cached;
  1688. if (pmtu > route_mtu_cached)
  1689. pmtu = route_mtu_cached;
  1690. dst->metrics[RTAX_MTU-1] = pmtu;
  1691. } while ((dst = dst->next));
  1692. }
  1693. EXPORT_SYMBOL(xfrm_init_pmtu);
  1694. /* Check that the bundle accepts the flow and its components are
  1695. * still valid.
  1696. */
  1697. int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
  1698. struct flowi *fl, int family, int strict)
  1699. {
  1700. struct dst_entry *dst = &first->u.dst;
  1701. struct xfrm_dst *last;
  1702. u32 mtu;
  1703. if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
  1704. (dst->dev && !netif_running(dst->dev)))
  1705. return 0;
  1706. #ifdef CONFIG_XFRM_SUB_POLICY
  1707. if (fl) {
  1708. if (first->origin && !flow_cache_uli_match(first->origin, fl))
  1709. return 0;
  1710. if (first->partner &&
  1711. !xfrm_selector_match(first->partner, fl, family))
  1712. return 0;
  1713. }
  1714. #endif
  1715. last = NULL;
  1716. do {
  1717. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1718. if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
  1719. return 0;
  1720. if (fl && pol &&
  1721. !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
  1722. return 0;
  1723. if (dst->xfrm->km.state != XFRM_STATE_VALID)
  1724. return 0;
  1725. if (xdst->genid != dst->xfrm->genid)
  1726. return 0;
  1727. if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
  1728. !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
  1729. return 0;
  1730. mtu = dst_mtu(dst->child);
  1731. if (xdst->child_mtu_cached != mtu) {
  1732. last = xdst;
  1733. xdst->child_mtu_cached = mtu;
  1734. }
  1735. if (!dst_check(xdst->route, xdst->route_cookie))
  1736. return 0;
  1737. mtu = dst_mtu(xdst->route);
  1738. if (xdst->route_mtu_cached != mtu) {
  1739. last = xdst;
  1740. xdst->route_mtu_cached = mtu;
  1741. }
  1742. dst = dst->child;
  1743. } while (dst->xfrm);
  1744. if (likely(!last))
  1745. return 1;
  1746. mtu = last->child_mtu_cached;
  1747. for (;;) {
  1748. dst = &last->u.dst;
  1749. mtu = xfrm_state_mtu(dst->xfrm, mtu);
  1750. if (mtu > last->route_mtu_cached)
  1751. mtu = last->route_mtu_cached;
  1752. dst->metrics[RTAX_MTU-1] = mtu;
  1753. if (last == first)
  1754. break;
  1755. last = last->u.next;
  1756. last->child_mtu_cached = mtu;
  1757. }
  1758. return 1;
  1759. }
  1760. EXPORT_SYMBOL(xfrm_bundle_ok);
  1761. #ifdef CONFIG_AUDITSYSCALL
  1762. /* Audit addition and deletion of SAs and ipsec policy */
  1763. void xfrm_audit_log(uid_t auid, u32 sid, int type, int result,
  1764. struct xfrm_policy *xp, struct xfrm_state *x)
  1765. {
  1766. char *secctx;
  1767. u32 secctx_len;
  1768. struct xfrm_sec_ctx *sctx = NULL;
  1769. struct audit_buffer *audit_buf;
  1770. int family;
  1771. extern int audit_enabled;
  1772. if (audit_enabled == 0)
  1773. return;
  1774. BUG_ON((type == AUDIT_MAC_IPSEC_ADDSA ||
  1775. type == AUDIT_MAC_IPSEC_DELSA) && !x);
  1776. BUG_ON((type == AUDIT_MAC_IPSEC_ADDSPD ||
  1777. type == AUDIT_MAC_IPSEC_DELSPD) && !xp);
  1778. audit_buf = audit_log_start(current->audit_context, GFP_ATOMIC, type);
  1779. if (audit_buf == NULL)
  1780. return;
  1781. switch(type) {
  1782. case AUDIT_MAC_IPSEC_ADDSA:
  1783. audit_log_format(audit_buf, "SAD add: auid=%u", auid);
  1784. break;
  1785. case AUDIT_MAC_IPSEC_DELSA:
  1786. audit_log_format(audit_buf, "SAD delete: auid=%u", auid);
  1787. break;
  1788. case AUDIT_MAC_IPSEC_ADDSPD:
  1789. audit_log_format(audit_buf, "SPD add: auid=%u", auid);
  1790. break;
  1791. case AUDIT_MAC_IPSEC_DELSPD:
  1792. audit_log_format(audit_buf, "SPD delete: auid=%u", auid);
  1793. break;
  1794. default:
  1795. return;
  1796. }
  1797. if (sid != 0 &&
  1798. security_secid_to_secctx(sid, &secctx, &secctx_len) == 0)
  1799. audit_log_format(audit_buf, " subj=%s", secctx);
  1800. else
  1801. audit_log_task_context(audit_buf);
  1802. if (xp) {
  1803. family = xp->selector.family;
  1804. if (xp->security)
  1805. sctx = xp->security;
  1806. } else {
  1807. family = x->props.family;
  1808. if (x->security)
  1809. sctx = x->security;
  1810. }
  1811. if (sctx)
  1812. audit_log_format(audit_buf,
  1813. " sec_alg=%u sec_doi=%u sec_obj=%s",
  1814. sctx->ctx_alg, sctx->ctx_doi, sctx->ctx_str);
  1815. switch(family) {
  1816. case AF_INET:
  1817. {
  1818. struct in_addr saddr, daddr;
  1819. if (xp) {
  1820. saddr.s_addr = xp->selector.saddr.a4;
  1821. daddr.s_addr = xp->selector.daddr.a4;
  1822. } else {
  1823. saddr.s_addr = x->props.saddr.a4;
  1824. daddr.s_addr = x->id.daddr.a4;
  1825. }
  1826. audit_log_format(audit_buf,
  1827. " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
  1828. NIPQUAD(saddr), NIPQUAD(daddr));
  1829. }
  1830. break;
  1831. case AF_INET6:
  1832. {
  1833. struct in6_addr saddr6, daddr6;
  1834. if (xp) {
  1835. memcpy(&saddr6, xp->selector.saddr.a6,
  1836. sizeof(struct in6_addr));
  1837. memcpy(&daddr6, xp->selector.daddr.a6,
  1838. sizeof(struct in6_addr));
  1839. } else {
  1840. memcpy(&saddr6, x->props.saddr.a6,
  1841. sizeof(struct in6_addr));
  1842. memcpy(&daddr6, x->id.daddr.a6,
  1843. sizeof(struct in6_addr));
  1844. }
  1845. audit_log_format(audit_buf,
  1846. " src=" NIP6_FMT " dst=" NIP6_FMT,
  1847. NIP6(saddr6), NIP6(daddr6));
  1848. }
  1849. break;
  1850. }
  1851. if (x)
  1852. audit_log_format(audit_buf, " spi=%lu(0x%lx) protocol=%s",
  1853. (unsigned long)ntohl(x->id.spi),
  1854. (unsigned long)ntohl(x->id.spi),
  1855. x->id.proto == IPPROTO_AH ? "AH" :
  1856. (x->id.proto == IPPROTO_ESP ?
  1857. "ESP" : "IPCOMP"));
  1858. audit_log_format(audit_buf, " res=%u", result);
  1859. audit_log_end(audit_buf);
  1860. }
  1861. EXPORT_SYMBOL(xfrm_audit_log);
  1862. #endif /* CONFIG_AUDITSYSCALL */
  1863. int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
  1864. {
  1865. int err = 0;
  1866. if (unlikely(afinfo == NULL))
  1867. return -EINVAL;
  1868. if (unlikely(afinfo->family >= NPROTO))
  1869. return -EAFNOSUPPORT;
  1870. write_lock_bh(&xfrm_policy_afinfo_lock);
  1871. if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
  1872. err = -ENOBUFS;
  1873. else {
  1874. struct dst_ops *dst_ops = afinfo->dst_ops;
  1875. if (likely(dst_ops->kmem_cachep == NULL))
  1876. dst_ops->kmem_cachep = xfrm_dst_cache;
  1877. if (likely(dst_ops->check == NULL))
  1878. dst_ops->check = xfrm_dst_check;
  1879. if (likely(dst_ops->negative_advice == NULL))
  1880. dst_ops->negative_advice = xfrm_negative_advice;
  1881. if (likely(dst_ops->link_failure == NULL))
  1882. dst_ops->link_failure = xfrm_link_failure;
  1883. if (likely(afinfo->garbage_collect == NULL))
  1884. afinfo->garbage_collect = __xfrm_garbage_collect;
  1885. xfrm_policy_afinfo[afinfo->family] = afinfo;
  1886. }
  1887. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1888. return err;
  1889. }
  1890. EXPORT_SYMBOL(xfrm_policy_register_afinfo);
  1891. int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
  1892. {
  1893. int err = 0;
  1894. if (unlikely(afinfo == NULL))
  1895. return -EINVAL;
  1896. if (unlikely(afinfo->family >= NPROTO))
  1897. return -EAFNOSUPPORT;
  1898. write_lock_bh(&xfrm_policy_afinfo_lock);
  1899. if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
  1900. if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
  1901. err = -EINVAL;
  1902. else {
  1903. struct dst_ops *dst_ops = afinfo->dst_ops;
  1904. xfrm_policy_afinfo[afinfo->family] = NULL;
  1905. dst_ops->kmem_cachep = NULL;
  1906. dst_ops->check = NULL;
  1907. dst_ops->negative_advice = NULL;
  1908. dst_ops->link_failure = NULL;
  1909. afinfo->garbage_collect = NULL;
  1910. }
  1911. }
  1912. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1913. return err;
  1914. }
  1915. EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
  1916. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
  1917. {
  1918. struct xfrm_policy_afinfo *afinfo;
  1919. if (unlikely(family >= NPROTO))
  1920. return NULL;
  1921. read_lock(&xfrm_policy_afinfo_lock);
  1922. afinfo = xfrm_policy_afinfo[family];
  1923. if (unlikely(!afinfo))
  1924. read_unlock(&xfrm_policy_afinfo_lock);
  1925. return afinfo;
  1926. }
  1927. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
  1928. {
  1929. read_unlock(&xfrm_policy_afinfo_lock);
  1930. }
  1931. static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
  1932. {
  1933. struct xfrm_policy_afinfo *afinfo;
  1934. if (unlikely(family >= NPROTO))
  1935. return NULL;
  1936. write_lock_bh(&xfrm_policy_afinfo_lock);
  1937. afinfo = xfrm_policy_afinfo[family];
  1938. if (unlikely(!afinfo))
  1939. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1940. return afinfo;
  1941. }
  1942. static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
  1943. {
  1944. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1945. }
  1946. static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1947. {
  1948. switch (event) {
  1949. case NETDEV_DOWN:
  1950. xfrm_flush_bundles();
  1951. }
  1952. return NOTIFY_DONE;
  1953. }
  1954. static struct notifier_block xfrm_dev_notifier = {
  1955. xfrm_dev_event,
  1956. NULL,
  1957. 0
  1958. };
  1959. static void __init xfrm_policy_init(void)
  1960. {
  1961. unsigned int hmask, sz;
  1962. int dir;
  1963. xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
  1964. sizeof(struct xfrm_dst),
  1965. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1966. NULL, NULL);
  1967. hmask = 8 - 1;
  1968. sz = (hmask+1) * sizeof(struct hlist_head);
  1969. xfrm_policy_byidx = xfrm_hash_alloc(sz);
  1970. xfrm_idx_hmask = hmask;
  1971. if (!xfrm_policy_byidx)
  1972. panic("XFRM: failed to allocate byidx hash\n");
  1973. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1974. struct xfrm_policy_hash *htab;
  1975. INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
  1976. htab = &xfrm_policy_bydst[dir];
  1977. htab->table = xfrm_hash_alloc(sz);
  1978. htab->hmask = hmask;
  1979. if (!htab->table)
  1980. panic("XFRM: failed to allocate bydst hash\n");
  1981. }
  1982. INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
  1983. register_netdevice_notifier(&xfrm_dev_notifier);
  1984. }
  1985. void __init xfrm_init(void)
  1986. {
  1987. xfrm_state_init();
  1988. xfrm_policy_init();
  1989. xfrm_input_init();
  1990. }
  1991. #ifdef CONFIG_XFRM_MIGRATE
  1992. static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
  1993. struct xfrm_selector *sel_tgt)
  1994. {
  1995. if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
  1996. if (sel_tgt->family == sel_cmp->family &&
  1997. xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
  1998. sel_cmp->family) == 0 &&
  1999. xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
  2000. sel_cmp->family) == 0 &&
  2001. sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
  2002. sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
  2003. return 1;
  2004. }
  2005. } else {
  2006. if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
  2007. return 1;
  2008. }
  2009. }
  2010. return 0;
  2011. }
  2012. static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
  2013. u8 dir, u8 type)
  2014. {
  2015. struct xfrm_policy *pol, *ret = NULL;
  2016. struct hlist_node *entry;
  2017. struct hlist_head *chain;
  2018. u32 priority = ~0U;
  2019. read_lock_bh(&xfrm_policy_lock);
  2020. chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
  2021. hlist_for_each_entry(pol, entry, chain, bydst) {
  2022. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  2023. pol->type == type) {
  2024. ret = pol;
  2025. priority = ret->priority;
  2026. break;
  2027. }
  2028. }
  2029. chain = &xfrm_policy_inexact[dir];
  2030. hlist_for_each_entry(pol, entry, chain, bydst) {
  2031. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  2032. pol->type == type &&
  2033. pol->priority < priority) {
  2034. ret = pol;
  2035. break;
  2036. }
  2037. }
  2038. if (ret)
  2039. xfrm_pol_hold(ret);
  2040. read_unlock_bh(&xfrm_policy_lock);
  2041. return ret;
  2042. }
  2043. static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
  2044. {
  2045. int match = 0;
  2046. if (t->mode == m->mode && t->id.proto == m->proto &&
  2047. (m->reqid == 0 || t->reqid == m->reqid)) {
  2048. switch (t->mode) {
  2049. case XFRM_MODE_TUNNEL:
  2050. case XFRM_MODE_BEET:
  2051. if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
  2052. m->old_family) == 0 &&
  2053. xfrm_addr_cmp(&t->saddr, &m->old_saddr,
  2054. m->old_family) == 0) {
  2055. match = 1;
  2056. }
  2057. break;
  2058. case XFRM_MODE_TRANSPORT:
  2059. /* in case of transport mode, template does not store
  2060. any IP addresses, hence we just compare mode and
  2061. protocol */
  2062. match = 1;
  2063. break;
  2064. default:
  2065. break;
  2066. }
  2067. }
  2068. return match;
  2069. }
  2070. /* update endpoint address(es) of template(s) */
  2071. static int xfrm_policy_migrate(struct xfrm_policy *pol,
  2072. struct xfrm_migrate *m, int num_migrate)
  2073. {
  2074. struct xfrm_migrate *mp;
  2075. struct dst_entry *dst;
  2076. int i, j, n = 0;
  2077. write_lock_bh(&pol->lock);
  2078. if (unlikely(pol->dead)) {
  2079. /* target policy has been deleted */
  2080. write_unlock_bh(&pol->lock);
  2081. return -ENOENT;
  2082. }
  2083. for (i = 0; i < pol->xfrm_nr; i++) {
  2084. for (j = 0, mp = m; j < num_migrate; j++, mp++) {
  2085. if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
  2086. continue;
  2087. n++;
  2088. if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL)
  2089. continue;
  2090. /* update endpoints */
  2091. memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
  2092. sizeof(pol->xfrm_vec[i].id.daddr));
  2093. memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
  2094. sizeof(pol->xfrm_vec[i].saddr));
  2095. pol->xfrm_vec[i].encap_family = mp->new_family;
  2096. /* flush bundles */
  2097. while ((dst = pol->bundles) != NULL) {
  2098. pol->bundles = dst->next;
  2099. dst_free(dst);
  2100. }
  2101. }
  2102. }
  2103. write_unlock_bh(&pol->lock);
  2104. if (!n)
  2105. return -ENODATA;
  2106. return 0;
  2107. }
  2108. static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
  2109. {
  2110. int i, j;
  2111. if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
  2112. return -EINVAL;
  2113. for (i = 0; i < num_migrate; i++) {
  2114. if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
  2115. m[i].old_family) == 0) &&
  2116. (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
  2117. m[i].old_family) == 0))
  2118. return -EINVAL;
  2119. if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
  2120. xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
  2121. return -EINVAL;
  2122. /* check if there is any duplicated entry */
  2123. for (j = i + 1; j < num_migrate; j++) {
  2124. if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
  2125. sizeof(m[i].old_daddr)) &&
  2126. !memcmp(&m[i].old_saddr, &m[j].old_saddr,
  2127. sizeof(m[i].old_saddr)) &&
  2128. m[i].proto == m[j].proto &&
  2129. m[i].mode == m[j].mode &&
  2130. m[i].reqid == m[j].reqid &&
  2131. m[i].old_family == m[j].old_family)
  2132. return -EINVAL;
  2133. }
  2134. }
  2135. return 0;
  2136. }
  2137. int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  2138. struct xfrm_migrate *m, int num_migrate)
  2139. {
  2140. int i, err, nx_cur = 0, nx_new = 0;
  2141. struct xfrm_policy *pol = NULL;
  2142. struct xfrm_state *x, *xc;
  2143. struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
  2144. struct xfrm_state *x_new[XFRM_MAX_DEPTH];
  2145. struct xfrm_migrate *mp;
  2146. if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
  2147. goto out;
  2148. /* Stage 1 - find policy */
  2149. if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
  2150. err = -ENOENT;
  2151. goto out;
  2152. }
  2153. /* Stage 2 - find and update state(s) */
  2154. for (i = 0, mp = m; i < num_migrate; i++, mp++) {
  2155. if ((x = xfrm_migrate_state_find(mp))) {
  2156. x_cur[nx_cur] = x;
  2157. nx_cur++;
  2158. if ((xc = xfrm_state_migrate(x, mp))) {
  2159. x_new[nx_new] = xc;
  2160. nx_new++;
  2161. } else {
  2162. err = -ENODATA;
  2163. goto restore_state;
  2164. }
  2165. }
  2166. }
  2167. /* Stage 3 - update policy */
  2168. if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
  2169. goto restore_state;
  2170. /* Stage 4 - delete old state(s) */
  2171. if (nx_cur) {
  2172. xfrm_states_put(x_cur, nx_cur);
  2173. xfrm_states_delete(x_cur, nx_cur);
  2174. }
  2175. /* Stage 5 - announce */
  2176. km_migrate(sel, dir, type, m, num_migrate);
  2177. xfrm_pol_put(pol);
  2178. return 0;
  2179. out:
  2180. return err;
  2181. restore_state:
  2182. if (pol)
  2183. xfrm_pol_put(pol);
  2184. if (nx_cur)
  2185. xfrm_states_put(x_cur, nx_cur);
  2186. if (nx_new)
  2187. xfrm_states_delete(x_new, nx_new);
  2188. return err;
  2189. }
  2190. EXPORT_SYMBOL(xfrm_migrate);
  2191. #endif