xfrm_policy.c 59 KB

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