xfrm_policy.c 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593
  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 "xfrm_hash.h"
  28. int sysctl_xfrm_larval_drop __read_mostly;
  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 xfrmk_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 = -ENOENT;
  664. if (xfrm_policy_id2dir(id) != dir)
  665. return NULL;
  666. *err = 0;
  667. write_lock_bh(&xfrm_policy_lock);
  668. chain = xfrm_policy_byidx + idx_hash(id);
  669. ret = NULL;
  670. hlist_for_each_entry(pol, entry, chain, byidx) {
  671. if (pol->type == type && pol->index == id) {
  672. xfrm_pol_hold(pol);
  673. if (delete) {
  674. *err = security_xfrm_policy_delete(pol);
  675. if (*err) {
  676. write_unlock_bh(&xfrm_policy_lock);
  677. return pol;
  678. }
  679. hlist_del(&pol->bydst);
  680. hlist_del(&pol->byidx);
  681. xfrm_policy_count[dir]--;
  682. }
  683. ret = pol;
  684. break;
  685. }
  686. }
  687. write_unlock_bh(&xfrm_policy_lock);
  688. if (ret && delete) {
  689. atomic_inc(&flow_cache_genid);
  690. xfrm_policy_kill(ret);
  691. }
  692. return ret;
  693. }
  694. EXPORT_SYMBOL(xfrm_policy_byid);
  695. #ifdef CONFIG_SECURITY_NETWORK_XFRM
  696. static inline int
  697. xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
  698. {
  699. int dir, err = 0;
  700. for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
  701. struct xfrm_policy *pol;
  702. struct hlist_node *entry;
  703. int i;
  704. hlist_for_each_entry(pol, entry,
  705. &xfrm_policy_inexact[dir], bydst) {
  706. if (pol->type != type)
  707. continue;
  708. err = security_xfrm_policy_delete(pol);
  709. if (err) {
  710. xfrm_audit_policy_delete(pol, 0,
  711. audit_info->loginuid,
  712. audit_info->secid);
  713. return err;
  714. }
  715. }
  716. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  717. hlist_for_each_entry(pol, entry,
  718. xfrm_policy_bydst[dir].table + i,
  719. bydst) {
  720. if (pol->type != type)
  721. continue;
  722. err = security_xfrm_policy_delete(pol);
  723. if (err) {
  724. xfrm_audit_policy_delete(pol, 0,
  725. audit_info->loginuid,
  726. audit_info->secid);
  727. return err;
  728. }
  729. }
  730. }
  731. }
  732. return err;
  733. }
  734. #else
  735. static inline int
  736. xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
  737. {
  738. return 0;
  739. }
  740. #endif
  741. int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
  742. {
  743. int dir, err = 0;
  744. write_lock_bh(&xfrm_policy_lock);
  745. err = xfrm_policy_flush_secctx_check(type, audit_info);
  746. if (err)
  747. goto out;
  748. for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
  749. struct xfrm_policy *pol;
  750. struct hlist_node *entry;
  751. int i, killed;
  752. killed = 0;
  753. again1:
  754. hlist_for_each_entry(pol, entry,
  755. &xfrm_policy_inexact[dir], bydst) {
  756. if (pol->type != type)
  757. continue;
  758. hlist_del(&pol->bydst);
  759. hlist_del(&pol->byidx);
  760. write_unlock_bh(&xfrm_policy_lock);
  761. xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
  762. audit_info->secid);
  763. xfrm_policy_kill(pol);
  764. killed++;
  765. write_lock_bh(&xfrm_policy_lock);
  766. goto again1;
  767. }
  768. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  769. again2:
  770. hlist_for_each_entry(pol, entry,
  771. xfrm_policy_bydst[dir].table + i,
  772. bydst) {
  773. if (pol->type != type)
  774. continue;
  775. hlist_del(&pol->bydst);
  776. hlist_del(&pol->byidx);
  777. write_unlock_bh(&xfrm_policy_lock);
  778. xfrm_audit_policy_delete(pol, 1,
  779. audit_info->loginuid,
  780. audit_info->secid);
  781. xfrm_policy_kill(pol);
  782. killed++;
  783. write_lock_bh(&xfrm_policy_lock);
  784. goto again2;
  785. }
  786. }
  787. xfrm_policy_count[dir] -= killed;
  788. }
  789. atomic_inc(&flow_cache_genid);
  790. out:
  791. write_unlock_bh(&xfrm_policy_lock);
  792. return err;
  793. }
  794. EXPORT_SYMBOL(xfrm_policy_flush);
  795. int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
  796. void *data)
  797. {
  798. struct xfrm_policy *pol, *last = NULL;
  799. struct hlist_node *entry;
  800. int dir, last_dir = 0, count, error;
  801. read_lock_bh(&xfrm_policy_lock);
  802. count = 0;
  803. for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
  804. struct hlist_head *table = xfrm_policy_bydst[dir].table;
  805. int i;
  806. hlist_for_each_entry(pol, entry,
  807. &xfrm_policy_inexact[dir], bydst) {
  808. if (pol->type != type)
  809. continue;
  810. if (last) {
  811. error = func(last, last_dir % XFRM_POLICY_MAX,
  812. count, data);
  813. if (error)
  814. goto out;
  815. }
  816. last = pol;
  817. last_dir = dir;
  818. count++;
  819. }
  820. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  821. hlist_for_each_entry(pol, entry, table + i, bydst) {
  822. if (pol->type != type)
  823. continue;
  824. if (last) {
  825. error = func(last, last_dir % XFRM_POLICY_MAX,
  826. count, data);
  827. if (error)
  828. goto out;
  829. }
  830. last = pol;
  831. last_dir = dir;
  832. count++;
  833. }
  834. }
  835. }
  836. if (count == 0) {
  837. error = -ENOENT;
  838. goto out;
  839. }
  840. error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
  841. out:
  842. read_unlock_bh(&xfrm_policy_lock);
  843. return error;
  844. }
  845. EXPORT_SYMBOL(xfrm_policy_walk);
  846. /*
  847. * Find policy to apply to this flow.
  848. *
  849. * Returns 0 if policy found, else an -errno.
  850. */
  851. static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
  852. u8 type, u16 family, int dir)
  853. {
  854. struct xfrm_selector *sel = &pol->selector;
  855. int match, ret = -ESRCH;
  856. if (pol->family != family ||
  857. pol->type != type)
  858. return ret;
  859. match = xfrm_selector_match(sel, fl, family);
  860. if (match)
  861. ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
  862. return ret;
  863. }
  864. static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
  865. u16 family, u8 dir)
  866. {
  867. int err;
  868. struct xfrm_policy *pol, *ret;
  869. xfrm_address_t *daddr, *saddr;
  870. struct hlist_node *entry;
  871. struct hlist_head *chain;
  872. u32 priority = ~0U;
  873. daddr = xfrm_flowi_daddr(fl, family);
  874. saddr = xfrm_flowi_saddr(fl, family);
  875. if (unlikely(!daddr || !saddr))
  876. return NULL;
  877. read_lock_bh(&xfrm_policy_lock);
  878. chain = policy_hash_direct(daddr, saddr, family, dir);
  879. ret = NULL;
  880. hlist_for_each_entry(pol, entry, chain, bydst) {
  881. err = xfrm_policy_match(pol, fl, type, family, dir);
  882. if (err) {
  883. if (err == -ESRCH)
  884. continue;
  885. else {
  886. ret = ERR_PTR(err);
  887. goto fail;
  888. }
  889. } else {
  890. ret = pol;
  891. priority = ret->priority;
  892. break;
  893. }
  894. }
  895. chain = &xfrm_policy_inexact[dir];
  896. hlist_for_each_entry(pol, entry, chain, bydst) {
  897. err = xfrm_policy_match(pol, fl, type, family, dir);
  898. if (err) {
  899. if (err == -ESRCH)
  900. continue;
  901. else {
  902. ret = ERR_PTR(err);
  903. goto fail;
  904. }
  905. } else if (pol->priority < priority) {
  906. ret = pol;
  907. break;
  908. }
  909. }
  910. if (ret)
  911. xfrm_pol_hold(ret);
  912. fail:
  913. read_unlock_bh(&xfrm_policy_lock);
  914. return ret;
  915. }
  916. static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
  917. void **objp, atomic_t **obj_refp)
  918. {
  919. struct xfrm_policy *pol;
  920. int err = 0;
  921. #ifdef CONFIG_XFRM_SUB_POLICY
  922. pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
  923. if (IS_ERR(pol)) {
  924. err = PTR_ERR(pol);
  925. pol = NULL;
  926. }
  927. if (pol || err)
  928. goto end;
  929. #endif
  930. pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
  931. if (IS_ERR(pol)) {
  932. err = PTR_ERR(pol);
  933. pol = NULL;
  934. }
  935. #ifdef CONFIG_XFRM_SUB_POLICY
  936. end:
  937. #endif
  938. if ((*objp = (void *) pol) != NULL)
  939. *obj_refp = &pol->refcnt;
  940. return err;
  941. }
  942. static inline int policy_to_flow_dir(int dir)
  943. {
  944. if (XFRM_POLICY_IN == FLOW_DIR_IN &&
  945. XFRM_POLICY_OUT == FLOW_DIR_OUT &&
  946. XFRM_POLICY_FWD == FLOW_DIR_FWD)
  947. return dir;
  948. switch (dir) {
  949. default:
  950. case XFRM_POLICY_IN:
  951. return FLOW_DIR_IN;
  952. case XFRM_POLICY_OUT:
  953. return FLOW_DIR_OUT;
  954. case XFRM_POLICY_FWD:
  955. return FLOW_DIR_FWD;
  956. }
  957. }
  958. static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
  959. {
  960. struct xfrm_policy *pol;
  961. read_lock_bh(&xfrm_policy_lock);
  962. if ((pol = sk->sk_policy[dir]) != NULL) {
  963. int match = xfrm_selector_match(&pol->selector, fl,
  964. sk->sk_family);
  965. int err = 0;
  966. if (match) {
  967. err = security_xfrm_policy_lookup(pol, fl->secid,
  968. policy_to_flow_dir(dir));
  969. if (!err)
  970. xfrm_pol_hold(pol);
  971. else if (err == -ESRCH)
  972. pol = NULL;
  973. else
  974. pol = ERR_PTR(err);
  975. } else
  976. pol = NULL;
  977. }
  978. read_unlock_bh(&xfrm_policy_lock);
  979. return pol;
  980. }
  981. static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
  982. {
  983. struct hlist_head *chain = policy_hash_bysel(&pol->selector,
  984. pol->family, dir);
  985. hlist_add_head(&pol->bydst, chain);
  986. hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
  987. xfrm_policy_count[dir]++;
  988. xfrm_pol_hold(pol);
  989. if (xfrm_bydst_should_resize(dir, NULL))
  990. schedule_work(&xfrm_hash_work);
  991. }
  992. static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
  993. int dir)
  994. {
  995. if (hlist_unhashed(&pol->bydst))
  996. return NULL;
  997. hlist_del(&pol->bydst);
  998. hlist_del(&pol->byidx);
  999. xfrm_policy_count[dir]--;
  1000. return pol;
  1001. }
  1002. int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
  1003. {
  1004. write_lock_bh(&xfrm_policy_lock);
  1005. pol = __xfrm_policy_unlink(pol, dir);
  1006. write_unlock_bh(&xfrm_policy_lock);
  1007. if (pol) {
  1008. if (dir < XFRM_POLICY_MAX)
  1009. atomic_inc(&flow_cache_genid);
  1010. xfrm_policy_kill(pol);
  1011. return 0;
  1012. }
  1013. return -ENOENT;
  1014. }
  1015. EXPORT_SYMBOL(xfrm_policy_delete);
  1016. int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
  1017. {
  1018. struct xfrm_policy *old_pol;
  1019. #ifdef CONFIG_XFRM_SUB_POLICY
  1020. if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
  1021. return -EINVAL;
  1022. #endif
  1023. write_lock_bh(&xfrm_policy_lock);
  1024. old_pol = sk->sk_policy[dir];
  1025. sk->sk_policy[dir] = pol;
  1026. if (pol) {
  1027. pol->curlft.add_time = get_seconds();
  1028. pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
  1029. __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
  1030. }
  1031. if (old_pol)
  1032. __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
  1033. write_unlock_bh(&xfrm_policy_lock);
  1034. if (old_pol) {
  1035. xfrm_policy_kill(old_pol);
  1036. }
  1037. return 0;
  1038. }
  1039. static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
  1040. {
  1041. struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
  1042. if (newp) {
  1043. newp->selector = old->selector;
  1044. if (security_xfrm_policy_clone(old, newp)) {
  1045. kfree(newp);
  1046. return NULL; /* ENOMEM */
  1047. }
  1048. newp->lft = old->lft;
  1049. newp->curlft = old->curlft;
  1050. newp->action = old->action;
  1051. newp->flags = old->flags;
  1052. newp->xfrm_nr = old->xfrm_nr;
  1053. newp->index = old->index;
  1054. newp->type = old->type;
  1055. memcpy(newp->xfrm_vec, old->xfrm_vec,
  1056. newp->xfrm_nr*sizeof(struct xfrm_tmpl));
  1057. write_lock_bh(&xfrm_policy_lock);
  1058. __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
  1059. write_unlock_bh(&xfrm_policy_lock);
  1060. xfrm_pol_put(newp);
  1061. }
  1062. return newp;
  1063. }
  1064. int __xfrm_sk_clone_policy(struct sock *sk)
  1065. {
  1066. struct xfrm_policy *p0 = sk->sk_policy[0],
  1067. *p1 = sk->sk_policy[1];
  1068. sk->sk_policy[0] = sk->sk_policy[1] = NULL;
  1069. if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
  1070. return -ENOMEM;
  1071. if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
  1072. return -ENOMEM;
  1073. return 0;
  1074. }
  1075. static int
  1076. xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
  1077. unsigned short family)
  1078. {
  1079. int err;
  1080. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1081. if (unlikely(afinfo == NULL))
  1082. return -EINVAL;
  1083. err = afinfo->get_saddr(local, remote);
  1084. xfrm_policy_put_afinfo(afinfo);
  1085. return err;
  1086. }
  1087. /* Resolve list of templates for the flow, given policy. */
  1088. static int
  1089. xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
  1090. struct xfrm_state **xfrm,
  1091. unsigned short family)
  1092. {
  1093. int nx;
  1094. int i, error;
  1095. xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
  1096. xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
  1097. xfrm_address_t tmp;
  1098. for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
  1099. struct xfrm_state *x;
  1100. xfrm_address_t *remote = daddr;
  1101. xfrm_address_t *local = saddr;
  1102. struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
  1103. if (tmpl->mode == XFRM_MODE_TUNNEL ||
  1104. tmpl->mode == XFRM_MODE_BEET) {
  1105. remote = &tmpl->id.daddr;
  1106. local = &tmpl->saddr;
  1107. family = tmpl->encap_family;
  1108. if (xfrm_addr_any(local, family)) {
  1109. error = xfrm_get_saddr(&tmp, remote, family);
  1110. if (error)
  1111. goto fail;
  1112. local = &tmp;
  1113. }
  1114. }
  1115. x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
  1116. if (x && x->km.state == XFRM_STATE_VALID) {
  1117. xfrm[nx++] = x;
  1118. daddr = remote;
  1119. saddr = local;
  1120. continue;
  1121. }
  1122. if (x) {
  1123. error = (x->km.state == XFRM_STATE_ERROR ?
  1124. -EINVAL : -EAGAIN);
  1125. xfrm_state_put(x);
  1126. }
  1127. if (!tmpl->optional)
  1128. goto fail;
  1129. }
  1130. return nx;
  1131. fail:
  1132. for (nx--; nx>=0; nx--)
  1133. xfrm_state_put(xfrm[nx]);
  1134. return error;
  1135. }
  1136. static int
  1137. xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
  1138. struct xfrm_state **xfrm,
  1139. unsigned short family)
  1140. {
  1141. struct xfrm_state *tp[XFRM_MAX_DEPTH];
  1142. struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
  1143. int cnx = 0;
  1144. int error;
  1145. int ret;
  1146. int i;
  1147. for (i = 0; i < npols; i++) {
  1148. if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
  1149. error = -ENOBUFS;
  1150. goto fail;
  1151. }
  1152. ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
  1153. if (ret < 0) {
  1154. error = ret;
  1155. goto fail;
  1156. } else
  1157. cnx += ret;
  1158. }
  1159. /* found states are sorted for outbound processing */
  1160. if (npols > 1)
  1161. xfrm_state_sort(xfrm, tpp, cnx, family);
  1162. return cnx;
  1163. fail:
  1164. for (cnx--; cnx>=0; cnx--)
  1165. xfrm_state_put(tpp[cnx]);
  1166. return error;
  1167. }
  1168. /* Check that the bundle accepts the flow and its components are
  1169. * still valid.
  1170. */
  1171. static struct dst_entry *
  1172. xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
  1173. {
  1174. struct dst_entry *x;
  1175. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1176. if (unlikely(afinfo == NULL))
  1177. return ERR_PTR(-EINVAL);
  1178. x = afinfo->find_bundle(fl, policy);
  1179. xfrm_policy_put_afinfo(afinfo);
  1180. return x;
  1181. }
  1182. /* Allocate chain of dst_entry's, attach known xfrm's, calculate
  1183. * all the metrics... Shortly, bundle a bundle.
  1184. */
  1185. static int
  1186. xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
  1187. struct flowi *fl, struct dst_entry **dst_p,
  1188. unsigned short family)
  1189. {
  1190. int err;
  1191. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1192. if (unlikely(afinfo == NULL))
  1193. return -EINVAL;
  1194. err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
  1195. xfrm_policy_put_afinfo(afinfo);
  1196. return err;
  1197. }
  1198. static int inline
  1199. xfrm_dst_alloc_copy(void **target, void *src, int size)
  1200. {
  1201. if (!*target) {
  1202. *target = kmalloc(size, GFP_ATOMIC);
  1203. if (!*target)
  1204. return -ENOMEM;
  1205. }
  1206. memcpy(*target, src, size);
  1207. return 0;
  1208. }
  1209. static int inline
  1210. xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
  1211. {
  1212. #ifdef CONFIG_XFRM_SUB_POLICY
  1213. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1214. return xfrm_dst_alloc_copy((void **)&(xdst->partner),
  1215. sel, sizeof(*sel));
  1216. #else
  1217. return 0;
  1218. #endif
  1219. }
  1220. static int inline
  1221. xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
  1222. {
  1223. #ifdef CONFIG_XFRM_SUB_POLICY
  1224. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1225. return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
  1226. #else
  1227. return 0;
  1228. #endif
  1229. }
  1230. static int stale_bundle(struct dst_entry *dst);
  1231. /* Main function: finds/creates a bundle for given flow.
  1232. *
  1233. * At the moment we eat a raw IP route. Mostly to speed up lookups
  1234. * on interfaces with disabled IPsec.
  1235. */
  1236. int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1237. struct sock *sk, int flags)
  1238. {
  1239. struct xfrm_policy *policy;
  1240. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1241. int npols;
  1242. int pol_dead;
  1243. int xfrm_nr;
  1244. int pi;
  1245. struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
  1246. struct dst_entry *dst, *dst_orig = *dst_p;
  1247. int nx = 0;
  1248. int err;
  1249. u32 genid;
  1250. u16 family;
  1251. u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
  1252. restart:
  1253. genid = atomic_read(&flow_cache_genid);
  1254. policy = NULL;
  1255. for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
  1256. pols[pi] = NULL;
  1257. npols = 0;
  1258. pol_dead = 0;
  1259. xfrm_nr = 0;
  1260. if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
  1261. policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
  1262. if (IS_ERR(policy))
  1263. return PTR_ERR(policy);
  1264. }
  1265. if (!policy) {
  1266. /* To accelerate a bit... */
  1267. if ((dst_orig->flags & DST_NOXFRM) ||
  1268. !xfrm_policy_count[XFRM_POLICY_OUT])
  1269. return 0;
  1270. policy = flow_cache_lookup(fl, dst_orig->ops->family,
  1271. dir, xfrm_policy_lookup);
  1272. if (IS_ERR(policy))
  1273. return PTR_ERR(policy);
  1274. }
  1275. if (!policy)
  1276. return 0;
  1277. family = dst_orig->ops->family;
  1278. policy->curlft.use_time = get_seconds();
  1279. pols[0] = policy;
  1280. npols ++;
  1281. xfrm_nr += pols[0]->xfrm_nr;
  1282. switch (policy->action) {
  1283. case XFRM_POLICY_BLOCK:
  1284. /* Prohibit the flow */
  1285. err = -EPERM;
  1286. goto error;
  1287. case XFRM_POLICY_ALLOW:
  1288. #ifndef CONFIG_XFRM_SUB_POLICY
  1289. if (policy->xfrm_nr == 0) {
  1290. /* Flow passes not transformed. */
  1291. xfrm_pol_put(policy);
  1292. return 0;
  1293. }
  1294. #endif
  1295. /* Try to find matching bundle.
  1296. *
  1297. * LATER: help from flow cache. It is optional, this
  1298. * is required only for output policy.
  1299. */
  1300. dst = xfrm_find_bundle(fl, policy, family);
  1301. if (IS_ERR(dst)) {
  1302. err = PTR_ERR(dst);
  1303. goto error;
  1304. }
  1305. if (dst)
  1306. break;
  1307. #ifdef CONFIG_XFRM_SUB_POLICY
  1308. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1309. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1310. fl, family,
  1311. XFRM_POLICY_OUT);
  1312. if (pols[1]) {
  1313. if (IS_ERR(pols[1])) {
  1314. err = PTR_ERR(pols[1]);
  1315. goto error;
  1316. }
  1317. if (pols[1]->action == XFRM_POLICY_BLOCK) {
  1318. err = -EPERM;
  1319. goto error;
  1320. }
  1321. npols ++;
  1322. xfrm_nr += pols[1]->xfrm_nr;
  1323. }
  1324. }
  1325. /*
  1326. * Because neither flowi nor bundle information knows about
  1327. * transformation template size. On more than one policy usage
  1328. * we can realize whether all of them is bypass or not after
  1329. * they are searched. See above not-transformed bypass
  1330. * is surrounded by non-sub policy configuration, too.
  1331. */
  1332. if (xfrm_nr == 0) {
  1333. /* Flow passes not transformed. */
  1334. xfrm_pols_put(pols, npols);
  1335. return 0;
  1336. }
  1337. #endif
  1338. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1339. if (unlikely(nx<0)) {
  1340. err = nx;
  1341. if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
  1342. /* EREMOTE tells the caller to generate
  1343. * a one-shot blackhole route.
  1344. */
  1345. xfrm_pol_put(policy);
  1346. return -EREMOTE;
  1347. }
  1348. if (err == -EAGAIN && flags) {
  1349. DECLARE_WAITQUEUE(wait, current);
  1350. add_wait_queue(&km_waitq, &wait);
  1351. set_current_state(TASK_INTERRUPTIBLE);
  1352. schedule();
  1353. set_current_state(TASK_RUNNING);
  1354. remove_wait_queue(&km_waitq, &wait);
  1355. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1356. if (nx == -EAGAIN && signal_pending(current)) {
  1357. err = -ERESTART;
  1358. goto error;
  1359. }
  1360. if (nx == -EAGAIN ||
  1361. genid != atomic_read(&flow_cache_genid)) {
  1362. xfrm_pols_put(pols, npols);
  1363. goto restart;
  1364. }
  1365. err = nx;
  1366. }
  1367. if (err < 0)
  1368. goto error;
  1369. }
  1370. if (nx == 0) {
  1371. /* Flow passes not transformed. */
  1372. xfrm_pols_put(pols, npols);
  1373. return 0;
  1374. }
  1375. dst = dst_orig;
  1376. err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
  1377. if (unlikely(err)) {
  1378. int i;
  1379. for (i=0; i<nx; i++)
  1380. xfrm_state_put(xfrm[i]);
  1381. goto error;
  1382. }
  1383. for (pi = 0; pi < npols; pi++) {
  1384. read_lock_bh(&pols[pi]->lock);
  1385. pol_dead |= pols[pi]->dead;
  1386. read_unlock_bh(&pols[pi]->lock);
  1387. }
  1388. write_lock_bh(&policy->lock);
  1389. if (unlikely(pol_dead || stale_bundle(dst))) {
  1390. /* Wow! While we worked on resolving, this
  1391. * policy has gone. Retry. It is not paranoia,
  1392. * we just cannot enlist new bundle to dead object.
  1393. * We can't enlist stable bundles either.
  1394. */
  1395. write_unlock_bh(&policy->lock);
  1396. if (dst)
  1397. dst_free(dst);
  1398. err = -EHOSTUNREACH;
  1399. goto error;
  1400. }
  1401. if (npols > 1)
  1402. err = xfrm_dst_update_parent(dst, &pols[1]->selector);
  1403. else
  1404. err = xfrm_dst_update_origin(dst, fl);
  1405. if (unlikely(err)) {
  1406. write_unlock_bh(&policy->lock);
  1407. if (dst)
  1408. dst_free(dst);
  1409. goto error;
  1410. }
  1411. dst->next = policy->bundles;
  1412. policy->bundles = dst;
  1413. dst_hold(dst);
  1414. write_unlock_bh(&policy->lock);
  1415. }
  1416. *dst_p = dst;
  1417. dst_release(dst_orig);
  1418. xfrm_pols_put(pols, npols);
  1419. return 0;
  1420. error:
  1421. dst_release(dst_orig);
  1422. xfrm_pols_put(pols, npols);
  1423. *dst_p = NULL;
  1424. return err;
  1425. }
  1426. EXPORT_SYMBOL(__xfrm_lookup);
  1427. int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1428. struct sock *sk, int flags)
  1429. {
  1430. int err = __xfrm_lookup(dst_p, fl, sk, flags);
  1431. if (err == -EREMOTE) {
  1432. dst_release(*dst_p);
  1433. *dst_p = NULL;
  1434. err = -EAGAIN;
  1435. }
  1436. return err;
  1437. }
  1438. EXPORT_SYMBOL(xfrm_lookup);
  1439. static inline int
  1440. xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
  1441. {
  1442. struct xfrm_state *x;
  1443. int err;
  1444. if (!skb->sp || idx < 0 || idx >= skb->sp->len)
  1445. return 0;
  1446. x = skb->sp->xvec[idx];
  1447. if (!x->type->reject)
  1448. return 0;
  1449. xfrm_state_hold(x);
  1450. err = x->type->reject(x, skb, fl);
  1451. xfrm_state_put(x);
  1452. return err;
  1453. }
  1454. /* When skb is transformed back to its "native" form, we have to
  1455. * check policy restrictions. At the moment we make this in maximally
  1456. * stupid way. Shame on me. :-) Of course, connected sockets must
  1457. * have policy cached at them.
  1458. */
  1459. static inline int
  1460. xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
  1461. unsigned short family)
  1462. {
  1463. if (xfrm_state_kern(x))
  1464. return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
  1465. return x->id.proto == tmpl->id.proto &&
  1466. (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
  1467. (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
  1468. x->props.mode == tmpl->mode &&
  1469. ((tmpl->aalgos & (1<<x->props.aalgo)) ||
  1470. !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
  1471. !(x->props.mode != XFRM_MODE_TRANSPORT &&
  1472. xfrm_state_addr_cmp(tmpl, x, family));
  1473. }
  1474. /*
  1475. * 0 or more than 0 is returned when validation is succeeded (either bypass
  1476. * because of optional transport mode, or next index of the mathced secpath
  1477. * state with the template.
  1478. * -1 is returned when no matching template is found.
  1479. * Otherwise "-2 - errored_index" is returned.
  1480. */
  1481. static inline int
  1482. xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
  1483. unsigned short family)
  1484. {
  1485. int idx = start;
  1486. if (tmpl->optional) {
  1487. if (tmpl->mode == XFRM_MODE_TRANSPORT)
  1488. return start;
  1489. } else
  1490. start = -1;
  1491. for (; idx < sp->len; idx++) {
  1492. if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
  1493. return ++idx;
  1494. if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
  1495. if (start == -1)
  1496. start = -2-idx;
  1497. break;
  1498. }
  1499. }
  1500. return start;
  1501. }
  1502. int
  1503. xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
  1504. {
  1505. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1506. int err;
  1507. if (unlikely(afinfo == NULL))
  1508. return -EAFNOSUPPORT;
  1509. afinfo->decode_session(skb, fl);
  1510. err = security_xfrm_decode_session(skb, &fl->secid);
  1511. xfrm_policy_put_afinfo(afinfo);
  1512. return err;
  1513. }
  1514. EXPORT_SYMBOL(xfrm_decode_session);
  1515. static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
  1516. {
  1517. for (; k < sp->len; k++) {
  1518. if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
  1519. *idxp = k;
  1520. return 1;
  1521. }
  1522. }
  1523. return 0;
  1524. }
  1525. int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
  1526. unsigned short family)
  1527. {
  1528. struct xfrm_policy *pol;
  1529. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1530. int npols = 0;
  1531. int xfrm_nr;
  1532. int pi;
  1533. struct flowi fl;
  1534. u8 fl_dir = policy_to_flow_dir(dir);
  1535. int xerr_idx = -1;
  1536. if (xfrm_decode_session(skb, &fl, family) < 0)
  1537. return 0;
  1538. nf_nat_decode_session(skb, &fl, family);
  1539. /* First, check used SA against their selectors. */
  1540. if (skb->sp) {
  1541. int i;
  1542. for (i=skb->sp->len-1; i>=0; i--) {
  1543. struct xfrm_state *x = skb->sp->xvec[i];
  1544. if (!xfrm_selector_match(&x->sel, &fl, family))
  1545. return 0;
  1546. }
  1547. }
  1548. pol = NULL;
  1549. if (sk && sk->sk_policy[dir]) {
  1550. pol = xfrm_sk_policy_lookup(sk, dir, &fl);
  1551. if (IS_ERR(pol))
  1552. return 0;
  1553. }
  1554. if (!pol)
  1555. pol = flow_cache_lookup(&fl, family, fl_dir,
  1556. xfrm_policy_lookup);
  1557. if (IS_ERR(pol))
  1558. return 0;
  1559. if (!pol) {
  1560. if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
  1561. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1562. return 0;
  1563. }
  1564. return 1;
  1565. }
  1566. pol->curlft.use_time = get_seconds();
  1567. pols[0] = pol;
  1568. npols ++;
  1569. #ifdef CONFIG_XFRM_SUB_POLICY
  1570. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1571. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1572. &fl, family,
  1573. XFRM_POLICY_IN);
  1574. if (pols[1]) {
  1575. if (IS_ERR(pols[1]))
  1576. return 0;
  1577. pols[1]->curlft.use_time = get_seconds();
  1578. npols ++;
  1579. }
  1580. }
  1581. #endif
  1582. if (pol->action == XFRM_POLICY_ALLOW) {
  1583. struct sec_path *sp;
  1584. static struct sec_path dummy;
  1585. struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
  1586. struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
  1587. struct xfrm_tmpl **tpp = tp;
  1588. int ti = 0;
  1589. int i, k;
  1590. if ((sp = skb->sp) == NULL)
  1591. sp = &dummy;
  1592. for (pi = 0; pi < npols; pi++) {
  1593. if (pols[pi] != pol &&
  1594. pols[pi]->action != XFRM_POLICY_ALLOW)
  1595. goto reject;
  1596. if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
  1597. goto reject_error;
  1598. for (i = 0; i < pols[pi]->xfrm_nr; i++)
  1599. tpp[ti++] = &pols[pi]->xfrm_vec[i];
  1600. }
  1601. xfrm_nr = ti;
  1602. if (npols > 1) {
  1603. xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
  1604. tpp = stp;
  1605. }
  1606. /* For each tunnel xfrm, find the first matching tmpl.
  1607. * For each tmpl before that, find corresponding xfrm.
  1608. * Order is _important_. Later we will implement
  1609. * some barriers, but at the moment barriers
  1610. * are implied between each two transformations.
  1611. */
  1612. for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
  1613. k = xfrm_policy_ok(tpp[i], sp, k, family);
  1614. if (k < 0) {
  1615. if (k < -1)
  1616. /* "-2 - errored_index" returned */
  1617. xerr_idx = -(2+k);
  1618. goto reject;
  1619. }
  1620. }
  1621. if (secpath_has_nontransport(sp, k, &xerr_idx))
  1622. goto reject;
  1623. xfrm_pols_put(pols, npols);
  1624. return 1;
  1625. }
  1626. reject:
  1627. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1628. reject_error:
  1629. xfrm_pols_put(pols, npols);
  1630. return 0;
  1631. }
  1632. EXPORT_SYMBOL(__xfrm_policy_check);
  1633. int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
  1634. {
  1635. struct flowi fl;
  1636. if (xfrm_decode_session(skb, &fl, family) < 0)
  1637. return 0;
  1638. return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
  1639. }
  1640. EXPORT_SYMBOL(__xfrm_route_forward);
  1641. /* Optimize later using cookies and generation ids. */
  1642. static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
  1643. {
  1644. /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
  1645. * to "-1" to force all XFRM destinations to get validated by
  1646. * dst_ops->check on every use. We do this because when a
  1647. * normal route referenced by an XFRM dst is obsoleted we do
  1648. * not go looking around for all parent referencing XFRM dsts
  1649. * so that we can invalidate them. It is just too much work.
  1650. * Instead we make the checks here on every use. For example:
  1651. *
  1652. * XFRM dst A --> IPv4 dst X
  1653. *
  1654. * X is the "xdst->route" of A (X is also the "dst->path" of A
  1655. * in this example). If X is marked obsolete, "A" will not
  1656. * notice. That's what we are validating here via the
  1657. * stale_bundle() check.
  1658. *
  1659. * When a policy's bundle is pruned, we dst_free() the XFRM
  1660. * dst which causes it's ->obsolete field to be set to a
  1661. * positive non-zero integer. If an XFRM dst has been pruned
  1662. * like this, we want to force a new route lookup.
  1663. */
  1664. if (dst->obsolete < 0 && !stale_bundle(dst))
  1665. return dst;
  1666. return NULL;
  1667. }
  1668. static int stale_bundle(struct dst_entry *dst)
  1669. {
  1670. return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
  1671. }
  1672. void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
  1673. {
  1674. while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
  1675. dst->dev = &loopback_dev;
  1676. dev_hold(&loopback_dev);
  1677. dev_put(dev);
  1678. }
  1679. }
  1680. EXPORT_SYMBOL(xfrm_dst_ifdown);
  1681. static void xfrm_link_failure(struct sk_buff *skb)
  1682. {
  1683. /* Impossible. Such dst must be popped before reaches point of failure. */
  1684. return;
  1685. }
  1686. static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
  1687. {
  1688. if (dst) {
  1689. if (dst->obsolete) {
  1690. dst_release(dst);
  1691. dst = NULL;
  1692. }
  1693. }
  1694. return dst;
  1695. }
  1696. static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
  1697. {
  1698. struct dst_entry *dst, **dstp;
  1699. write_lock(&pol->lock);
  1700. dstp = &pol->bundles;
  1701. while ((dst=*dstp) != NULL) {
  1702. if (func(dst)) {
  1703. *dstp = dst->next;
  1704. dst->next = *gc_list_p;
  1705. *gc_list_p = dst;
  1706. } else {
  1707. dstp = &dst->next;
  1708. }
  1709. }
  1710. write_unlock(&pol->lock);
  1711. }
  1712. static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
  1713. {
  1714. struct dst_entry *gc_list = NULL;
  1715. int dir;
  1716. read_lock_bh(&xfrm_policy_lock);
  1717. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1718. struct xfrm_policy *pol;
  1719. struct hlist_node *entry;
  1720. struct hlist_head *table;
  1721. int i;
  1722. hlist_for_each_entry(pol, entry,
  1723. &xfrm_policy_inexact[dir], bydst)
  1724. prune_one_bundle(pol, func, &gc_list);
  1725. table = xfrm_policy_bydst[dir].table;
  1726. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  1727. hlist_for_each_entry(pol, entry, table + i, bydst)
  1728. prune_one_bundle(pol, func, &gc_list);
  1729. }
  1730. }
  1731. read_unlock_bh(&xfrm_policy_lock);
  1732. while (gc_list) {
  1733. struct dst_entry *dst = gc_list;
  1734. gc_list = dst->next;
  1735. dst_free(dst);
  1736. }
  1737. }
  1738. static int unused_bundle(struct dst_entry *dst)
  1739. {
  1740. return !atomic_read(&dst->__refcnt);
  1741. }
  1742. static void __xfrm_garbage_collect(void)
  1743. {
  1744. xfrm_prune_bundles(unused_bundle);
  1745. }
  1746. static int xfrm_flush_bundles(void)
  1747. {
  1748. xfrm_prune_bundles(stale_bundle);
  1749. return 0;
  1750. }
  1751. void xfrm_init_pmtu(struct dst_entry *dst)
  1752. {
  1753. do {
  1754. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1755. u32 pmtu, route_mtu_cached;
  1756. pmtu = dst_mtu(dst->child);
  1757. xdst->child_mtu_cached = pmtu;
  1758. pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
  1759. route_mtu_cached = dst_mtu(xdst->route);
  1760. xdst->route_mtu_cached = route_mtu_cached;
  1761. if (pmtu > route_mtu_cached)
  1762. pmtu = route_mtu_cached;
  1763. dst->metrics[RTAX_MTU-1] = pmtu;
  1764. } while ((dst = dst->next));
  1765. }
  1766. EXPORT_SYMBOL(xfrm_init_pmtu);
  1767. /* Check that the bundle accepts the flow and its components are
  1768. * still valid.
  1769. */
  1770. int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
  1771. struct flowi *fl, int family, int strict)
  1772. {
  1773. struct dst_entry *dst = &first->u.dst;
  1774. struct xfrm_dst *last;
  1775. u32 mtu;
  1776. if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
  1777. (dst->dev && !netif_running(dst->dev)))
  1778. return 0;
  1779. #ifdef CONFIG_XFRM_SUB_POLICY
  1780. if (fl) {
  1781. if (first->origin && !flow_cache_uli_match(first->origin, fl))
  1782. return 0;
  1783. if (first->partner &&
  1784. !xfrm_selector_match(first->partner, fl, family))
  1785. return 0;
  1786. }
  1787. #endif
  1788. last = NULL;
  1789. do {
  1790. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1791. if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
  1792. return 0;
  1793. if (fl && pol &&
  1794. !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
  1795. return 0;
  1796. if (dst->xfrm->km.state != XFRM_STATE_VALID)
  1797. return 0;
  1798. if (xdst->genid != dst->xfrm->genid)
  1799. return 0;
  1800. if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
  1801. !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
  1802. return 0;
  1803. mtu = dst_mtu(dst->child);
  1804. if (xdst->child_mtu_cached != mtu) {
  1805. last = xdst;
  1806. xdst->child_mtu_cached = mtu;
  1807. }
  1808. if (!dst_check(xdst->route, xdst->route_cookie))
  1809. return 0;
  1810. mtu = dst_mtu(xdst->route);
  1811. if (xdst->route_mtu_cached != mtu) {
  1812. last = xdst;
  1813. xdst->route_mtu_cached = mtu;
  1814. }
  1815. dst = dst->child;
  1816. } while (dst->xfrm);
  1817. if (likely(!last))
  1818. return 1;
  1819. mtu = last->child_mtu_cached;
  1820. for (;;) {
  1821. dst = &last->u.dst;
  1822. mtu = xfrm_state_mtu(dst->xfrm, mtu);
  1823. if (mtu > last->route_mtu_cached)
  1824. mtu = last->route_mtu_cached;
  1825. dst->metrics[RTAX_MTU-1] = mtu;
  1826. if (last == first)
  1827. break;
  1828. last = (struct xfrm_dst *)last->u.dst.next;
  1829. last->child_mtu_cached = mtu;
  1830. }
  1831. return 1;
  1832. }
  1833. EXPORT_SYMBOL(xfrm_bundle_ok);
  1834. int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
  1835. {
  1836. int err = 0;
  1837. if (unlikely(afinfo == NULL))
  1838. return -EINVAL;
  1839. if (unlikely(afinfo->family >= NPROTO))
  1840. return -EAFNOSUPPORT;
  1841. write_lock_bh(&xfrm_policy_afinfo_lock);
  1842. if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
  1843. err = -ENOBUFS;
  1844. else {
  1845. struct dst_ops *dst_ops = afinfo->dst_ops;
  1846. if (likely(dst_ops->kmem_cachep == NULL))
  1847. dst_ops->kmem_cachep = xfrm_dst_cache;
  1848. if (likely(dst_ops->check == NULL))
  1849. dst_ops->check = xfrm_dst_check;
  1850. if (likely(dst_ops->negative_advice == NULL))
  1851. dst_ops->negative_advice = xfrm_negative_advice;
  1852. if (likely(dst_ops->link_failure == NULL))
  1853. dst_ops->link_failure = xfrm_link_failure;
  1854. if (likely(afinfo->garbage_collect == NULL))
  1855. afinfo->garbage_collect = __xfrm_garbage_collect;
  1856. xfrm_policy_afinfo[afinfo->family] = afinfo;
  1857. }
  1858. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1859. return err;
  1860. }
  1861. EXPORT_SYMBOL(xfrm_policy_register_afinfo);
  1862. int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
  1863. {
  1864. int err = 0;
  1865. if (unlikely(afinfo == NULL))
  1866. return -EINVAL;
  1867. if (unlikely(afinfo->family >= NPROTO))
  1868. return -EAFNOSUPPORT;
  1869. write_lock_bh(&xfrm_policy_afinfo_lock);
  1870. if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
  1871. if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
  1872. err = -EINVAL;
  1873. else {
  1874. struct dst_ops *dst_ops = afinfo->dst_ops;
  1875. xfrm_policy_afinfo[afinfo->family] = NULL;
  1876. dst_ops->kmem_cachep = NULL;
  1877. dst_ops->check = NULL;
  1878. dst_ops->negative_advice = NULL;
  1879. dst_ops->link_failure = NULL;
  1880. afinfo->garbage_collect = NULL;
  1881. }
  1882. }
  1883. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1884. return err;
  1885. }
  1886. EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
  1887. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
  1888. {
  1889. struct xfrm_policy_afinfo *afinfo;
  1890. if (unlikely(family >= NPROTO))
  1891. return NULL;
  1892. read_lock(&xfrm_policy_afinfo_lock);
  1893. afinfo = xfrm_policy_afinfo[family];
  1894. if (unlikely(!afinfo))
  1895. read_unlock(&xfrm_policy_afinfo_lock);
  1896. return afinfo;
  1897. }
  1898. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
  1899. {
  1900. read_unlock(&xfrm_policy_afinfo_lock);
  1901. }
  1902. static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
  1903. {
  1904. struct xfrm_policy_afinfo *afinfo;
  1905. if (unlikely(family >= NPROTO))
  1906. return NULL;
  1907. write_lock_bh(&xfrm_policy_afinfo_lock);
  1908. afinfo = xfrm_policy_afinfo[family];
  1909. if (unlikely(!afinfo))
  1910. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1911. return afinfo;
  1912. }
  1913. static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
  1914. {
  1915. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1916. }
  1917. static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1918. {
  1919. struct net_device *dev = ptr;
  1920. if (dev->nd_net != &init_net)
  1921. return NOTIFY_DONE;
  1922. switch (event) {
  1923. case NETDEV_DOWN:
  1924. xfrm_flush_bundles();
  1925. }
  1926. return NOTIFY_DONE;
  1927. }
  1928. static struct notifier_block xfrm_dev_notifier = {
  1929. xfrm_dev_event,
  1930. NULL,
  1931. 0
  1932. };
  1933. static void __init xfrm_policy_init(void)
  1934. {
  1935. unsigned int hmask, sz;
  1936. int dir;
  1937. xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
  1938. sizeof(struct xfrm_dst),
  1939. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1940. NULL);
  1941. hmask = 8 - 1;
  1942. sz = (hmask+1) * sizeof(struct hlist_head);
  1943. xfrm_policy_byidx = xfrm_hash_alloc(sz);
  1944. xfrm_idx_hmask = hmask;
  1945. if (!xfrm_policy_byidx)
  1946. panic("XFRM: failed to allocate byidx hash\n");
  1947. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1948. struct xfrm_policy_hash *htab;
  1949. INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
  1950. htab = &xfrm_policy_bydst[dir];
  1951. htab->table = xfrm_hash_alloc(sz);
  1952. htab->hmask = hmask;
  1953. if (!htab->table)
  1954. panic("XFRM: failed to allocate bydst hash\n");
  1955. }
  1956. INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
  1957. register_netdevice_notifier(&xfrm_dev_notifier);
  1958. }
  1959. void __init xfrm_init(void)
  1960. {
  1961. xfrm_state_init();
  1962. xfrm_policy_init();
  1963. xfrm_input_init();
  1964. }
  1965. #ifdef CONFIG_AUDITSYSCALL
  1966. static inline void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
  1967. struct audit_buffer *audit_buf)
  1968. {
  1969. if (xp->security)
  1970. audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
  1971. xp->security->ctx_alg, xp->security->ctx_doi,
  1972. xp->security->ctx_str);
  1973. switch(xp->selector.family) {
  1974. case AF_INET:
  1975. audit_log_format(audit_buf, " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
  1976. NIPQUAD(xp->selector.saddr.a4),
  1977. NIPQUAD(xp->selector.daddr.a4));
  1978. break;
  1979. case AF_INET6:
  1980. {
  1981. struct in6_addr saddr6, daddr6;
  1982. memcpy(&saddr6, xp->selector.saddr.a6,
  1983. sizeof(struct in6_addr));
  1984. memcpy(&daddr6, xp->selector.daddr.a6,
  1985. sizeof(struct in6_addr));
  1986. audit_log_format(audit_buf,
  1987. " src=" NIP6_FMT " dst=" NIP6_FMT,
  1988. NIP6(saddr6), NIP6(daddr6));
  1989. }
  1990. break;
  1991. }
  1992. }
  1993. void
  1994. xfrm_audit_policy_add(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
  1995. {
  1996. struct audit_buffer *audit_buf;
  1997. extern int audit_enabled;
  1998. if (audit_enabled == 0)
  1999. return;
  2000. audit_buf = xfrm_audit_start(sid, auid);
  2001. if (audit_buf == NULL)
  2002. return;
  2003. audit_log_format(audit_buf, " op=SPD-add res=%u", result);
  2004. xfrm_audit_common_policyinfo(xp, audit_buf);
  2005. audit_log_end(audit_buf);
  2006. }
  2007. EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
  2008. void
  2009. xfrm_audit_policy_delete(struct xfrm_policy *xp, int result, u32 auid, u32 sid)
  2010. {
  2011. struct audit_buffer *audit_buf;
  2012. extern int audit_enabled;
  2013. if (audit_enabled == 0)
  2014. return;
  2015. audit_buf = xfrm_audit_start(sid, auid);
  2016. if (audit_buf == NULL)
  2017. return;
  2018. audit_log_format(audit_buf, " op=SPD-delete res=%u", result);
  2019. xfrm_audit_common_policyinfo(xp, audit_buf);
  2020. audit_log_end(audit_buf);
  2021. }
  2022. EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
  2023. #endif
  2024. #ifdef CONFIG_XFRM_MIGRATE
  2025. static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
  2026. struct xfrm_selector *sel_tgt)
  2027. {
  2028. if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
  2029. if (sel_tgt->family == sel_cmp->family &&
  2030. xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
  2031. sel_cmp->family) == 0 &&
  2032. xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
  2033. sel_cmp->family) == 0 &&
  2034. sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
  2035. sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
  2036. return 1;
  2037. }
  2038. } else {
  2039. if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
  2040. return 1;
  2041. }
  2042. }
  2043. return 0;
  2044. }
  2045. static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
  2046. u8 dir, u8 type)
  2047. {
  2048. struct xfrm_policy *pol, *ret = NULL;
  2049. struct hlist_node *entry;
  2050. struct hlist_head *chain;
  2051. u32 priority = ~0U;
  2052. read_lock_bh(&xfrm_policy_lock);
  2053. chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
  2054. hlist_for_each_entry(pol, entry, chain, bydst) {
  2055. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  2056. pol->type == type) {
  2057. ret = pol;
  2058. priority = ret->priority;
  2059. break;
  2060. }
  2061. }
  2062. chain = &xfrm_policy_inexact[dir];
  2063. hlist_for_each_entry(pol, entry, chain, bydst) {
  2064. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  2065. pol->type == type &&
  2066. pol->priority < priority) {
  2067. ret = pol;
  2068. break;
  2069. }
  2070. }
  2071. if (ret)
  2072. xfrm_pol_hold(ret);
  2073. read_unlock_bh(&xfrm_policy_lock);
  2074. return ret;
  2075. }
  2076. static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
  2077. {
  2078. int match = 0;
  2079. if (t->mode == m->mode && t->id.proto == m->proto &&
  2080. (m->reqid == 0 || t->reqid == m->reqid)) {
  2081. switch (t->mode) {
  2082. case XFRM_MODE_TUNNEL:
  2083. case XFRM_MODE_BEET:
  2084. if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
  2085. m->old_family) == 0 &&
  2086. xfrm_addr_cmp(&t->saddr, &m->old_saddr,
  2087. m->old_family) == 0) {
  2088. match = 1;
  2089. }
  2090. break;
  2091. case XFRM_MODE_TRANSPORT:
  2092. /* in case of transport mode, template does not store
  2093. any IP addresses, hence we just compare mode and
  2094. protocol */
  2095. match = 1;
  2096. break;
  2097. default:
  2098. break;
  2099. }
  2100. }
  2101. return match;
  2102. }
  2103. /* update endpoint address(es) of template(s) */
  2104. static int xfrm_policy_migrate(struct xfrm_policy *pol,
  2105. struct xfrm_migrate *m, int num_migrate)
  2106. {
  2107. struct xfrm_migrate *mp;
  2108. struct dst_entry *dst;
  2109. int i, j, n = 0;
  2110. write_lock_bh(&pol->lock);
  2111. if (unlikely(pol->dead)) {
  2112. /* target policy has been deleted */
  2113. write_unlock_bh(&pol->lock);
  2114. return -ENOENT;
  2115. }
  2116. for (i = 0; i < pol->xfrm_nr; i++) {
  2117. for (j = 0, mp = m; j < num_migrate; j++, mp++) {
  2118. if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
  2119. continue;
  2120. n++;
  2121. if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL)
  2122. continue;
  2123. /* update endpoints */
  2124. memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
  2125. sizeof(pol->xfrm_vec[i].id.daddr));
  2126. memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
  2127. sizeof(pol->xfrm_vec[i].saddr));
  2128. pol->xfrm_vec[i].encap_family = mp->new_family;
  2129. /* flush bundles */
  2130. while ((dst = pol->bundles) != NULL) {
  2131. pol->bundles = dst->next;
  2132. dst_free(dst);
  2133. }
  2134. }
  2135. }
  2136. write_unlock_bh(&pol->lock);
  2137. if (!n)
  2138. return -ENODATA;
  2139. return 0;
  2140. }
  2141. static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
  2142. {
  2143. int i, j;
  2144. if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
  2145. return -EINVAL;
  2146. for (i = 0; i < num_migrate; i++) {
  2147. if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
  2148. m[i].old_family) == 0) &&
  2149. (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
  2150. m[i].old_family) == 0))
  2151. return -EINVAL;
  2152. if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
  2153. xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
  2154. return -EINVAL;
  2155. /* check if there is any duplicated entry */
  2156. for (j = i + 1; j < num_migrate; j++) {
  2157. if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
  2158. sizeof(m[i].old_daddr)) &&
  2159. !memcmp(&m[i].old_saddr, &m[j].old_saddr,
  2160. sizeof(m[i].old_saddr)) &&
  2161. m[i].proto == m[j].proto &&
  2162. m[i].mode == m[j].mode &&
  2163. m[i].reqid == m[j].reqid &&
  2164. m[i].old_family == m[j].old_family)
  2165. return -EINVAL;
  2166. }
  2167. }
  2168. return 0;
  2169. }
  2170. int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  2171. struct xfrm_migrate *m, int num_migrate)
  2172. {
  2173. int i, err, nx_cur = 0, nx_new = 0;
  2174. struct xfrm_policy *pol = NULL;
  2175. struct xfrm_state *x, *xc;
  2176. struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
  2177. struct xfrm_state *x_new[XFRM_MAX_DEPTH];
  2178. struct xfrm_migrate *mp;
  2179. if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
  2180. goto out;
  2181. /* Stage 1 - find policy */
  2182. if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
  2183. err = -ENOENT;
  2184. goto out;
  2185. }
  2186. /* Stage 2 - find and update state(s) */
  2187. for (i = 0, mp = m; i < num_migrate; i++, mp++) {
  2188. if ((x = xfrm_migrate_state_find(mp))) {
  2189. x_cur[nx_cur] = x;
  2190. nx_cur++;
  2191. if ((xc = xfrm_state_migrate(x, mp))) {
  2192. x_new[nx_new] = xc;
  2193. nx_new++;
  2194. } else {
  2195. err = -ENODATA;
  2196. goto restore_state;
  2197. }
  2198. }
  2199. }
  2200. /* Stage 3 - update policy */
  2201. if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
  2202. goto restore_state;
  2203. /* Stage 4 - delete old state(s) */
  2204. if (nx_cur) {
  2205. xfrm_states_put(x_cur, nx_cur);
  2206. xfrm_states_delete(x_cur, nx_cur);
  2207. }
  2208. /* Stage 5 - announce */
  2209. km_migrate(sel, dir, type, m, num_migrate);
  2210. xfrm_pol_put(pol);
  2211. return 0;
  2212. out:
  2213. return err;
  2214. restore_state:
  2215. if (pol)
  2216. xfrm_pol_put(pol);
  2217. if (nx_cur)
  2218. xfrm_states_put(x_cur, nx_cur);
  2219. if (nx_new)
  2220. xfrm_states_delete(x_new, nx_new);
  2221. return err;
  2222. }
  2223. EXPORT_SYMBOL(xfrm_migrate);
  2224. #endif