xfrm_policy.c 63 KB

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