xfrm_policy.c 62 KB

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