xfrm_policy.c 64 KB

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