xfrm_policy.c 64 KB

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