xfrm_policy.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776
  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 HLIST_HEAD(xfrm_policy_gc_list);
  49. static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
  50. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family);
  51. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo);
  52. static void xfrm_init_pmtu(struct dst_entry *dst);
  53. static inline int
  54. __xfrm4_selector_match(struct xfrm_selector *sel, struct flowi *fl)
  55. {
  56. return addr_match(&fl->fl4_dst, &sel->daddr, sel->prefixlen_d) &&
  57. addr_match(&fl->fl4_src, &sel->saddr, sel->prefixlen_s) &&
  58. !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
  59. !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
  60. (fl->proto == sel->proto || !sel->proto) &&
  61. (fl->oif == sel->ifindex || !sel->ifindex);
  62. }
  63. static inline int
  64. __xfrm6_selector_match(struct xfrm_selector *sel, struct flowi *fl)
  65. {
  66. return addr_match(&fl->fl6_dst, &sel->daddr, sel->prefixlen_d) &&
  67. addr_match(&fl->fl6_src, &sel->saddr, sel->prefixlen_s) &&
  68. !((xfrm_flowi_dport(fl) ^ sel->dport) & sel->dport_mask) &&
  69. !((xfrm_flowi_sport(fl) ^ sel->sport) & sel->sport_mask) &&
  70. (fl->proto == sel->proto || !sel->proto) &&
  71. (fl->oif == sel->ifindex || !sel->ifindex);
  72. }
  73. int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
  74. unsigned short family)
  75. {
  76. switch (family) {
  77. case AF_INET:
  78. return __xfrm4_selector_match(sel, fl);
  79. case AF_INET6:
  80. return __xfrm6_selector_match(sel, fl);
  81. }
  82. return 0;
  83. }
  84. static inline struct dst_entry *__xfrm_dst_lookup(int tos,
  85. xfrm_address_t *saddr,
  86. xfrm_address_t *daddr,
  87. int family)
  88. {
  89. struct xfrm_policy_afinfo *afinfo;
  90. struct dst_entry *dst;
  91. afinfo = xfrm_policy_get_afinfo(family);
  92. if (unlikely(afinfo == NULL))
  93. return ERR_PTR(-EAFNOSUPPORT);
  94. dst = afinfo->dst_lookup(tos, saddr, daddr);
  95. xfrm_policy_put_afinfo(afinfo);
  96. return dst;
  97. }
  98. static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
  99. xfrm_address_t *prev_saddr,
  100. xfrm_address_t *prev_daddr,
  101. int family)
  102. {
  103. xfrm_address_t *saddr = &x->props.saddr;
  104. xfrm_address_t *daddr = &x->id.daddr;
  105. struct dst_entry *dst;
  106. if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
  107. saddr = x->coaddr;
  108. daddr = prev_daddr;
  109. }
  110. if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
  111. saddr = prev_saddr;
  112. daddr = x->coaddr;
  113. }
  114. dst = __xfrm_dst_lookup(tos, saddr, daddr, family);
  115. if (!IS_ERR(dst)) {
  116. if (prev_saddr != saddr)
  117. memcpy(prev_saddr, saddr, sizeof(*prev_saddr));
  118. if (prev_daddr != daddr)
  119. memcpy(prev_daddr, daddr, sizeof(*prev_daddr));
  120. }
  121. return dst;
  122. }
  123. static inline unsigned long make_jiffies(long secs)
  124. {
  125. if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
  126. return MAX_SCHEDULE_TIMEOUT-1;
  127. else
  128. return secs*HZ;
  129. }
  130. static void xfrm_policy_timer(unsigned long data)
  131. {
  132. struct xfrm_policy *xp = (struct xfrm_policy*)data;
  133. unsigned long now = get_seconds();
  134. long next = LONG_MAX;
  135. int warn = 0;
  136. int dir;
  137. read_lock(&xp->lock);
  138. if (xp->walk.dead)
  139. goto out;
  140. dir = xfrm_policy_id2dir(xp->index);
  141. if (xp->lft.hard_add_expires_seconds) {
  142. long tmo = xp->lft.hard_add_expires_seconds +
  143. xp->curlft.add_time - now;
  144. if (tmo <= 0)
  145. goto expired;
  146. if (tmo < next)
  147. next = tmo;
  148. }
  149. if (xp->lft.hard_use_expires_seconds) {
  150. long tmo = xp->lft.hard_use_expires_seconds +
  151. (xp->curlft.use_time ? : xp->curlft.add_time) - now;
  152. if (tmo <= 0)
  153. goto expired;
  154. if (tmo < next)
  155. next = tmo;
  156. }
  157. if (xp->lft.soft_add_expires_seconds) {
  158. long tmo = xp->lft.soft_add_expires_seconds +
  159. xp->curlft.add_time - now;
  160. if (tmo <= 0) {
  161. warn = 1;
  162. tmo = XFRM_KM_TIMEOUT;
  163. }
  164. if (tmo < next)
  165. next = tmo;
  166. }
  167. if (xp->lft.soft_use_expires_seconds) {
  168. long tmo = xp->lft.soft_use_expires_seconds +
  169. (xp->curlft.use_time ? : xp->curlft.add_time) - now;
  170. if (tmo <= 0) {
  171. warn = 1;
  172. tmo = XFRM_KM_TIMEOUT;
  173. }
  174. if (tmo < next)
  175. next = tmo;
  176. }
  177. if (warn)
  178. km_policy_expired(xp, dir, 0, 0);
  179. if (next != LONG_MAX &&
  180. !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
  181. xfrm_pol_hold(xp);
  182. out:
  183. read_unlock(&xp->lock);
  184. xfrm_pol_put(xp);
  185. return;
  186. expired:
  187. read_unlock(&xp->lock);
  188. if (!xfrm_policy_delete(xp, dir))
  189. km_policy_expired(xp, dir, 1, 0);
  190. xfrm_pol_put(xp);
  191. }
  192. /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
  193. * SPD calls.
  194. */
  195. struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
  196. {
  197. struct xfrm_policy *policy;
  198. policy = kzalloc(sizeof(struct xfrm_policy), gfp);
  199. if (policy) {
  200. INIT_LIST_HEAD(&policy->walk.all);
  201. INIT_HLIST_NODE(&policy->bydst);
  202. INIT_HLIST_NODE(&policy->byidx);
  203. rwlock_init(&policy->lock);
  204. atomic_set(&policy->refcnt, 1);
  205. setup_timer(&policy->timer, xfrm_policy_timer,
  206. (unsigned long)policy);
  207. }
  208. return policy;
  209. }
  210. EXPORT_SYMBOL(xfrm_policy_alloc);
  211. /* Destroy xfrm_policy: descendant resources must be released to this moment. */
  212. void xfrm_policy_destroy(struct xfrm_policy *policy)
  213. {
  214. BUG_ON(!policy->walk.dead);
  215. BUG_ON(policy->bundles);
  216. if (del_timer(&policy->timer))
  217. BUG();
  218. security_xfrm_policy_free(policy->security);
  219. kfree(policy);
  220. }
  221. EXPORT_SYMBOL(xfrm_policy_destroy);
  222. static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
  223. {
  224. struct dst_entry *dst;
  225. while ((dst = policy->bundles) != NULL) {
  226. policy->bundles = dst->next;
  227. dst_free(dst);
  228. }
  229. if (del_timer(&policy->timer))
  230. atomic_dec(&policy->refcnt);
  231. if (atomic_read(&policy->refcnt) > 1)
  232. flow_cache_flush();
  233. xfrm_pol_put(policy);
  234. }
  235. static void xfrm_policy_gc_task(struct work_struct *work)
  236. {
  237. struct xfrm_policy *policy;
  238. struct hlist_node *entry, *tmp;
  239. struct hlist_head gc_list;
  240. spin_lock_bh(&xfrm_policy_gc_lock);
  241. gc_list.first = xfrm_policy_gc_list.first;
  242. INIT_HLIST_HEAD(&xfrm_policy_gc_list);
  243. spin_unlock_bh(&xfrm_policy_gc_lock);
  244. hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
  245. xfrm_policy_gc_kill(policy);
  246. }
  247. static DECLARE_WORK(xfrm_policy_gc_work, xfrm_policy_gc_task);
  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(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(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(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. else if (error == -ESRCH)
  1076. error = -EAGAIN;
  1077. if (!tmpl->optional)
  1078. goto fail;
  1079. }
  1080. return nx;
  1081. fail:
  1082. for (nx--; nx>=0; nx--)
  1083. xfrm_state_put(xfrm[nx]);
  1084. return error;
  1085. }
  1086. static int
  1087. xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
  1088. struct xfrm_state **xfrm,
  1089. unsigned short family)
  1090. {
  1091. struct xfrm_state *tp[XFRM_MAX_DEPTH];
  1092. struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
  1093. int cnx = 0;
  1094. int error;
  1095. int ret;
  1096. int i;
  1097. for (i = 0; i < npols; i++) {
  1098. if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
  1099. error = -ENOBUFS;
  1100. goto fail;
  1101. }
  1102. ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
  1103. if (ret < 0) {
  1104. error = ret;
  1105. goto fail;
  1106. } else
  1107. cnx += ret;
  1108. }
  1109. /* found states are sorted for outbound processing */
  1110. if (npols > 1)
  1111. xfrm_state_sort(xfrm, tpp, cnx, family);
  1112. return cnx;
  1113. fail:
  1114. for (cnx--; cnx>=0; cnx--)
  1115. xfrm_state_put(tpp[cnx]);
  1116. return error;
  1117. }
  1118. /* Check that the bundle accepts the flow and its components are
  1119. * still valid.
  1120. */
  1121. static struct dst_entry *
  1122. xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
  1123. {
  1124. struct dst_entry *x;
  1125. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1126. if (unlikely(afinfo == NULL))
  1127. return ERR_PTR(-EINVAL);
  1128. x = afinfo->find_bundle(fl, policy);
  1129. xfrm_policy_put_afinfo(afinfo);
  1130. return x;
  1131. }
  1132. static inline int xfrm_get_tos(struct flowi *fl, int family)
  1133. {
  1134. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1135. int tos;
  1136. if (!afinfo)
  1137. return -EINVAL;
  1138. tos = afinfo->get_tos(fl);
  1139. xfrm_policy_put_afinfo(afinfo);
  1140. return tos;
  1141. }
  1142. static inline struct xfrm_dst *xfrm_alloc_dst(int family)
  1143. {
  1144. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1145. struct xfrm_dst *xdst;
  1146. if (!afinfo)
  1147. return ERR_PTR(-EINVAL);
  1148. xdst = dst_alloc(afinfo->dst_ops) ?: ERR_PTR(-ENOBUFS);
  1149. xfrm_policy_put_afinfo(afinfo);
  1150. return xdst;
  1151. }
  1152. static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  1153. int nfheader_len)
  1154. {
  1155. struct xfrm_policy_afinfo *afinfo =
  1156. xfrm_policy_get_afinfo(dst->ops->family);
  1157. int err;
  1158. if (!afinfo)
  1159. return -EINVAL;
  1160. err = afinfo->init_path(path, dst, nfheader_len);
  1161. xfrm_policy_put_afinfo(afinfo);
  1162. return err;
  1163. }
  1164. static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
  1165. {
  1166. struct xfrm_policy_afinfo *afinfo =
  1167. xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
  1168. int err;
  1169. if (!afinfo)
  1170. return -EINVAL;
  1171. err = afinfo->fill_dst(xdst, dev);
  1172. xfrm_policy_put_afinfo(afinfo);
  1173. return err;
  1174. }
  1175. /* Allocate chain of dst_entry's, attach known xfrm's, calculate
  1176. * all the metrics... Shortly, bundle a bundle.
  1177. */
  1178. static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
  1179. struct xfrm_state **xfrm, int nx,
  1180. struct flowi *fl,
  1181. struct dst_entry *dst)
  1182. {
  1183. unsigned long now = jiffies;
  1184. struct net_device *dev;
  1185. struct dst_entry *dst_prev = NULL;
  1186. struct dst_entry *dst0 = NULL;
  1187. int i = 0;
  1188. int err;
  1189. int header_len = 0;
  1190. int nfheader_len = 0;
  1191. int trailer_len = 0;
  1192. int tos;
  1193. int family = policy->selector.family;
  1194. xfrm_address_t saddr, daddr;
  1195. xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
  1196. tos = xfrm_get_tos(fl, family);
  1197. err = tos;
  1198. if (tos < 0)
  1199. goto put_states;
  1200. dst_hold(dst);
  1201. for (; i < nx; i++) {
  1202. struct xfrm_dst *xdst = xfrm_alloc_dst(family);
  1203. struct dst_entry *dst1 = &xdst->u.dst;
  1204. err = PTR_ERR(xdst);
  1205. if (IS_ERR(xdst)) {
  1206. dst_release(dst);
  1207. goto put_states;
  1208. }
  1209. if (!dst_prev)
  1210. dst0 = dst1;
  1211. else {
  1212. dst_prev->child = dst_clone(dst1);
  1213. dst1->flags |= DST_NOHASH;
  1214. }
  1215. xdst->route = dst;
  1216. memcpy(&dst1->metrics, &dst->metrics, sizeof(dst->metrics));
  1217. if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
  1218. family = xfrm[i]->props.family;
  1219. dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
  1220. family);
  1221. err = PTR_ERR(dst);
  1222. if (IS_ERR(dst))
  1223. goto put_states;
  1224. } else
  1225. dst_hold(dst);
  1226. dst1->xfrm = xfrm[i];
  1227. xdst->genid = xfrm[i]->genid;
  1228. dst1->obsolete = -1;
  1229. dst1->flags |= DST_HOST;
  1230. dst1->lastuse = now;
  1231. dst1->input = dst_discard;
  1232. dst1->output = xfrm[i]->outer_mode->afinfo->output;
  1233. dst1->next = dst_prev;
  1234. dst_prev = dst1;
  1235. header_len += xfrm[i]->props.header_len;
  1236. if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
  1237. nfheader_len += xfrm[i]->props.header_len;
  1238. trailer_len += xfrm[i]->props.trailer_len;
  1239. }
  1240. dst_prev->child = dst;
  1241. dst0->path = dst;
  1242. err = -ENODEV;
  1243. dev = dst->dev;
  1244. if (!dev)
  1245. goto free_dst;
  1246. /* Copy neighbout for reachability confirmation */
  1247. dst0->neighbour = neigh_clone(dst->neighbour);
  1248. xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
  1249. xfrm_init_pmtu(dst_prev);
  1250. for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
  1251. struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
  1252. err = xfrm_fill_dst(xdst, dev);
  1253. if (err)
  1254. goto free_dst;
  1255. dst_prev->header_len = header_len;
  1256. dst_prev->trailer_len = trailer_len;
  1257. header_len -= xdst->u.dst.xfrm->props.header_len;
  1258. trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
  1259. }
  1260. out:
  1261. return dst0;
  1262. put_states:
  1263. for (; i < nx; i++)
  1264. xfrm_state_put(xfrm[i]);
  1265. free_dst:
  1266. if (dst0)
  1267. dst_free(dst0);
  1268. dst0 = ERR_PTR(err);
  1269. goto out;
  1270. }
  1271. static int inline
  1272. xfrm_dst_alloc_copy(void **target, void *src, int size)
  1273. {
  1274. if (!*target) {
  1275. *target = kmalloc(size, GFP_ATOMIC);
  1276. if (!*target)
  1277. return -ENOMEM;
  1278. }
  1279. memcpy(*target, src, size);
  1280. return 0;
  1281. }
  1282. static int inline
  1283. xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
  1284. {
  1285. #ifdef CONFIG_XFRM_SUB_POLICY
  1286. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1287. return xfrm_dst_alloc_copy((void **)&(xdst->partner),
  1288. sel, sizeof(*sel));
  1289. #else
  1290. return 0;
  1291. #endif
  1292. }
  1293. static int inline
  1294. xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
  1295. {
  1296. #ifdef CONFIG_XFRM_SUB_POLICY
  1297. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1298. return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
  1299. #else
  1300. return 0;
  1301. #endif
  1302. }
  1303. static int stale_bundle(struct dst_entry *dst);
  1304. /* Main function: finds/creates a bundle for given flow.
  1305. *
  1306. * At the moment we eat a raw IP route. Mostly to speed up lookups
  1307. * on interfaces with disabled IPsec.
  1308. */
  1309. int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1310. struct sock *sk, int flags)
  1311. {
  1312. struct xfrm_policy *policy;
  1313. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1314. int npols;
  1315. int pol_dead;
  1316. int xfrm_nr;
  1317. int pi;
  1318. struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
  1319. struct dst_entry *dst, *dst_orig = *dst_p;
  1320. int nx = 0;
  1321. int err;
  1322. u32 genid;
  1323. u16 family;
  1324. u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
  1325. restart:
  1326. genid = atomic_read(&flow_cache_genid);
  1327. policy = NULL;
  1328. for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
  1329. pols[pi] = NULL;
  1330. npols = 0;
  1331. pol_dead = 0;
  1332. xfrm_nr = 0;
  1333. if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
  1334. policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
  1335. err = PTR_ERR(policy);
  1336. if (IS_ERR(policy)) {
  1337. XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR);
  1338. goto dropdst;
  1339. }
  1340. }
  1341. if (!policy) {
  1342. /* To accelerate a bit... */
  1343. if ((dst_orig->flags & DST_NOXFRM) ||
  1344. !xfrm_policy_count[XFRM_POLICY_OUT])
  1345. goto nopol;
  1346. policy = flow_cache_lookup(fl, dst_orig->ops->family,
  1347. dir, xfrm_policy_lookup);
  1348. err = PTR_ERR(policy);
  1349. if (IS_ERR(policy)) {
  1350. XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR);
  1351. goto dropdst;
  1352. }
  1353. }
  1354. if (!policy)
  1355. goto nopol;
  1356. family = dst_orig->ops->family;
  1357. pols[0] = policy;
  1358. npols ++;
  1359. xfrm_nr += pols[0]->xfrm_nr;
  1360. err = -ENOENT;
  1361. if ((flags & XFRM_LOOKUP_ICMP) && !(policy->flags & XFRM_POLICY_ICMP))
  1362. goto error;
  1363. policy->curlft.use_time = get_seconds();
  1364. switch (policy->action) {
  1365. default:
  1366. case XFRM_POLICY_BLOCK:
  1367. /* Prohibit the flow */
  1368. XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLBLOCK);
  1369. err = -EPERM;
  1370. goto error;
  1371. case XFRM_POLICY_ALLOW:
  1372. #ifndef CONFIG_XFRM_SUB_POLICY
  1373. if (policy->xfrm_nr == 0) {
  1374. /* Flow passes not transformed. */
  1375. xfrm_pol_put(policy);
  1376. return 0;
  1377. }
  1378. #endif
  1379. /* Try to find matching bundle.
  1380. *
  1381. * LATER: help from flow cache. It is optional, this
  1382. * is required only for output policy.
  1383. */
  1384. dst = xfrm_find_bundle(fl, policy, family);
  1385. if (IS_ERR(dst)) {
  1386. XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
  1387. err = PTR_ERR(dst);
  1388. goto error;
  1389. }
  1390. if (dst)
  1391. break;
  1392. #ifdef CONFIG_XFRM_SUB_POLICY
  1393. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1394. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1395. fl, family,
  1396. XFRM_POLICY_OUT);
  1397. if (pols[1]) {
  1398. if (IS_ERR(pols[1])) {
  1399. XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR);
  1400. err = PTR_ERR(pols[1]);
  1401. goto error;
  1402. }
  1403. if (pols[1]->action == XFRM_POLICY_BLOCK) {
  1404. XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLBLOCK);
  1405. err = -EPERM;
  1406. goto error;
  1407. }
  1408. npols ++;
  1409. xfrm_nr += pols[1]->xfrm_nr;
  1410. }
  1411. }
  1412. /*
  1413. * Because neither flowi nor bundle information knows about
  1414. * transformation template size. On more than one policy usage
  1415. * we can realize whether all of them is bypass or not after
  1416. * they are searched. See above not-transformed bypass
  1417. * is surrounded by non-sub policy configuration, too.
  1418. */
  1419. if (xfrm_nr == 0) {
  1420. /* Flow passes not transformed. */
  1421. xfrm_pols_put(pols, npols);
  1422. return 0;
  1423. }
  1424. #endif
  1425. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1426. if (unlikely(nx<0)) {
  1427. err = nx;
  1428. if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
  1429. /* EREMOTE tells the caller to generate
  1430. * a one-shot blackhole route.
  1431. */
  1432. XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES);
  1433. xfrm_pol_put(policy);
  1434. return -EREMOTE;
  1435. }
  1436. if (err == -EAGAIN && (flags & XFRM_LOOKUP_WAIT)) {
  1437. DECLARE_WAITQUEUE(wait, current);
  1438. add_wait_queue(&km_waitq, &wait);
  1439. set_current_state(TASK_INTERRUPTIBLE);
  1440. schedule();
  1441. set_current_state(TASK_RUNNING);
  1442. remove_wait_queue(&km_waitq, &wait);
  1443. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1444. if (nx == -EAGAIN && signal_pending(current)) {
  1445. XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES);
  1446. err = -ERESTART;
  1447. goto error;
  1448. }
  1449. if (nx == -EAGAIN ||
  1450. genid != atomic_read(&flow_cache_genid)) {
  1451. xfrm_pols_put(pols, npols);
  1452. goto restart;
  1453. }
  1454. err = nx;
  1455. }
  1456. if (err < 0) {
  1457. XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES);
  1458. goto error;
  1459. }
  1460. }
  1461. if (nx == 0) {
  1462. /* Flow passes not transformed. */
  1463. xfrm_pols_put(pols, npols);
  1464. return 0;
  1465. }
  1466. dst = xfrm_bundle_create(policy, xfrm, nx, fl, dst_orig);
  1467. err = PTR_ERR(dst);
  1468. if (IS_ERR(dst)) {
  1469. XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLEGENERROR);
  1470. goto error;
  1471. }
  1472. for (pi = 0; pi < npols; pi++) {
  1473. read_lock_bh(&pols[pi]->lock);
  1474. pol_dead |= pols[pi]->walk.dead;
  1475. read_unlock_bh(&pols[pi]->lock);
  1476. }
  1477. write_lock_bh(&policy->lock);
  1478. if (unlikely(pol_dead || stale_bundle(dst))) {
  1479. /* Wow! While we worked on resolving, this
  1480. * policy has gone. Retry. It is not paranoia,
  1481. * we just cannot enlist new bundle to dead object.
  1482. * We can't enlist stable bundles either.
  1483. */
  1484. write_unlock_bh(&policy->lock);
  1485. dst_free(dst);
  1486. if (pol_dead)
  1487. XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLDEAD);
  1488. else
  1489. XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
  1490. err = -EHOSTUNREACH;
  1491. goto error;
  1492. }
  1493. if (npols > 1)
  1494. err = xfrm_dst_update_parent(dst, &pols[1]->selector);
  1495. else
  1496. err = xfrm_dst_update_origin(dst, fl);
  1497. if (unlikely(err)) {
  1498. write_unlock_bh(&policy->lock);
  1499. dst_free(dst);
  1500. XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
  1501. goto error;
  1502. }
  1503. dst->next = policy->bundles;
  1504. policy->bundles = dst;
  1505. dst_hold(dst);
  1506. write_unlock_bh(&policy->lock);
  1507. }
  1508. *dst_p = dst;
  1509. dst_release(dst_orig);
  1510. xfrm_pols_put(pols, npols);
  1511. return 0;
  1512. error:
  1513. xfrm_pols_put(pols, npols);
  1514. dropdst:
  1515. dst_release(dst_orig);
  1516. *dst_p = NULL;
  1517. return err;
  1518. nopol:
  1519. err = -ENOENT;
  1520. if (flags & XFRM_LOOKUP_ICMP)
  1521. goto dropdst;
  1522. return 0;
  1523. }
  1524. EXPORT_SYMBOL(__xfrm_lookup);
  1525. int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1526. struct sock *sk, int flags)
  1527. {
  1528. int err = __xfrm_lookup(dst_p, fl, sk, flags);
  1529. if (err == -EREMOTE) {
  1530. dst_release(*dst_p);
  1531. *dst_p = NULL;
  1532. err = -EAGAIN;
  1533. }
  1534. return err;
  1535. }
  1536. EXPORT_SYMBOL(xfrm_lookup);
  1537. static inline int
  1538. xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
  1539. {
  1540. struct xfrm_state *x;
  1541. if (!skb->sp || idx < 0 || idx >= skb->sp->len)
  1542. return 0;
  1543. x = skb->sp->xvec[idx];
  1544. if (!x->type->reject)
  1545. return 0;
  1546. return x->type->reject(x, skb, fl);
  1547. }
  1548. /* When skb is transformed back to its "native" form, we have to
  1549. * check policy restrictions. At the moment we make this in maximally
  1550. * stupid way. Shame on me. :-) Of course, connected sockets must
  1551. * have policy cached at them.
  1552. */
  1553. static inline int
  1554. xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
  1555. unsigned short family)
  1556. {
  1557. if (xfrm_state_kern(x))
  1558. return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
  1559. return x->id.proto == tmpl->id.proto &&
  1560. (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
  1561. (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
  1562. x->props.mode == tmpl->mode &&
  1563. (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
  1564. !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
  1565. !(x->props.mode != XFRM_MODE_TRANSPORT &&
  1566. xfrm_state_addr_cmp(tmpl, x, family));
  1567. }
  1568. /*
  1569. * 0 or more than 0 is returned when validation is succeeded (either bypass
  1570. * because of optional transport mode, or next index of the mathced secpath
  1571. * state with the template.
  1572. * -1 is returned when no matching template is found.
  1573. * Otherwise "-2 - errored_index" is returned.
  1574. */
  1575. static inline int
  1576. xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
  1577. unsigned short family)
  1578. {
  1579. int idx = start;
  1580. if (tmpl->optional) {
  1581. if (tmpl->mode == XFRM_MODE_TRANSPORT)
  1582. return start;
  1583. } else
  1584. start = -1;
  1585. for (; idx < sp->len; idx++) {
  1586. if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
  1587. return ++idx;
  1588. if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
  1589. if (start == -1)
  1590. start = -2-idx;
  1591. break;
  1592. }
  1593. }
  1594. return start;
  1595. }
  1596. int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
  1597. unsigned int family, int reverse)
  1598. {
  1599. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1600. int err;
  1601. if (unlikely(afinfo == NULL))
  1602. return -EAFNOSUPPORT;
  1603. afinfo->decode_session(skb, fl, reverse);
  1604. err = security_xfrm_decode_session(skb, &fl->secid);
  1605. xfrm_policy_put_afinfo(afinfo);
  1606. return err;
  1607. }
  1608. EXPORT_SYMBOL(__xfrm_decode_session);
  1609. static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
  1610. {
  1611. for (; k < sp->len; k++) {
  1612. if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
  1613. *idxp = k;
  1614. return 1;
  1615. }
  1616. }
  1617. return 0;
  1618. }
  1619. int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
  1620. unsigned short family)
  1621. {
  1622. struct xfrm_policy *pol;
  1623. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1624. int npols = 0;
  1625. int xfrm_nr;
  1626. int pi;
  1627. int reverse;
  1628. struct flowi fl;
  1629. u8 fl_dir;
  1630. int xerr_idx = -1;
  1631. reverse = dir & ~XFRM_POLICY_MASK;
  1632. dir &= XFRM_POLICY_MASK;
  1633. fl_dir = policy_to_flow_dir(dir);
  1634. if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
  1635. XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR);
  1636. return 0;
  1637. }
  1638. nf_nat_decode_session(skb, &fl, family);
  1639. /* First, check used SA against their selectors. */
  1640. if (skb->sp) {
  1641. int i;
  1642. for (i=skb->sp->len-1; i>=0; i--) {
  1643. struct xfrm_state *x = skb->sp->xvec[i];
  1644. if (!xfrm_selector_match(&x->sel, &fl, family)) {
  1645. XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEMISMATCH);
  1646. return 0;
  1647. }
  1648. }
  1649. }
  1650. pol = NULL;
  1651. if (sk && sk->sk_policy[dir]) {
  1652. pol = xfrm_sk_policy_lookup(sk, dir, &fl);
  1653. if (IS_ERR(pol)) {
  1654. XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR);
  1655. return 0;
  1656. }
  1657. }
  1658. if (!pol)
  1659. pol = flow_cache_lookup(&fl, family, fl_dir,
  1660. xfrm_policy_lookup);
  1661. if (IS_ERR(pol)) {
  1662. XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR);
  1663. return 0;
  1664. }
  1665. if (!pol) {
  1666. if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
  1667. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1668. XFRM_INC_STATS(LINUX_MIB_XFRMINNOPOLS);
  1669. return 0;
  1670. }
  1671. return 1;
  1672. }
  1673. pol->curlft.use_time = get_seconds();
  1674. pols[0] = pol;
  1675. npols ++;
  1676. #ifdef CONFIG_XFRM_SUB_POLICY
  1677. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1678. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1679. &fl, family,
  1680. XFRM_POLICY_IN);
  1681. if (pols[1]) {
  1682. if (IS_ERR(pols[1])) {
  1683. XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR);
  1684. return 0;
  1685. }
  1686. pols[1]->curlft.use_time = get_seconds();
  1687. npols ++;
  1688. }
  1689. }
  1690. #endif
  1691. if (pol->action == XFRM_POLICY_ALLOW) {
  1692. struct sec_path *sp;
  1693. static struct sec_path dummy;
  1694. struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
  1695. struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
  1696. struct xfrm_tmpl **tpp = tp;
  1697. int ti = 0;
  1698. int i, k;
  1699. if ((sp = skb->sp) == NULL)
  1700. sp = &dummy;
  1701. for (pi = 0; pi < npols; pi++) {
  1702. if (pols[pi] != pol &&
  1703. pols[pi]->action != XFRM_POLICY_ALLOW) {
  1704. XFRM_INC_STATS(LINUX_MIB_XFRMINPOLBLOCK);
  1705. goto reject;
  1706. }
  1707. if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
  1708. XFRM_INC_STATS(LINUX_MIB_XFRMINBUFFERERROR);
  1709. goto reject_error;
  1710. }
  1711. for (i = 0; i < pols[pi]->xfrm_nr; i++)
  1712. tpp[ti++] = &pols[pi]->xfrm_vec[i];
  1713. }
  1714. xfrm_nr = ti;
  1715. if (npols > 1) {
  1716. xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
  1717. tpp = stp;
  1718. }
  1719. /* For each tunnel xfrm, find the first matching tmpl.
  1720. * For each tmpl before that, find corresponding xfrm.
  1721. * Order is _important_. Later we will implement
  1722. * some barriers, but at the moment barriers
  1723. * are implied between each two transformations.
  1724. */
  1725. for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
  1726. k = xfrm_policy_ok(tpp[i], sp, k, family);
  1727. if (k < 0) {
  1728. if (k < -1)
  1729. /* "-2 - errored_index" returned */
  1730. xerr_idx = -(2+k);
  1731. XFRM_INC_STATS(LINUX_MIB_XFRMINTMPLMISMATCH);
  1732. goto reject;
  1733. }
  1734. }
  1735. if (secpath_has_nontransport(sp, k, &xerr_idx)) {
  1736. XFRM_INC_STATS(LINUX_MIB_XFRMINTMPLMISMATCH);
  1737. goto reject;
  1738. }
  1739. xfrm_pols_put(pols, npols);
  1740. return 1;
  1741. }
  1742. XFRM_INC_STATS(LINUX_MIB_XFRMINPOLBLOCK);
  1743. reject:
  1744. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1745. reject_error:
  1746. xfrm_pols_put(pols, npols);
  1747. return 0;
  1748. }
  1749. EXPORT_SYMBOL(__xfrm_policy_check);
  1750. int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
  1751. {
  1752. struct flowi fl;
  1753. if (xfrm_decode_session(skb, &fl, family) < 0) {
  1754. /* XXX: we should have something like FWDHDRERROR here. */
  1755. XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR);
  1756. return 0;
  1757. }
  1758. return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
  1759. }
  1760. EXPORT_SYMBOL(__xfrm_route_forward);
  1761. /* Optimize later using cookies and generation ids. */
  1762. static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
  1763. {
  1764. /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
  1765. * to "-1" to force all XFRM destinations to get validated by
  1766. * dst_ops->check on every use. We do this because when a
  1767. * normal route referenced by an XFRM dst is obsoleted we do
  1768. * not go looking around for all parent referencing XFRM dsts
  1769. * so that we can invalidate them. It is just too much work.
  1770. * Instead we make the checks here on every use. For example:
  1771. *
  1772. * XFRM dst A --> IPv4 dst X
  1773. *
  1774. * X is the "xdst->route" of A (X is also the "dst->path" of A
  1775. * in this example). If X is marked obsolete, "A" will not
  1776. * notice. That's what we are validating here via the
  1777. * stale_bundle() check.
  1778. *
  1779. * When a policy's bundle is pruned, we dst_free() the XFRM
  1780. * dst which causes it's ->obsolete field to be set to a
  1781. * positive non-zero integer. If an XFRM dst has been pruned
  1782. * like this, we want to force a new route lookup.
  1783. */
  1784. if (dst->obsolete < 0 && !stale_bundle(dst))
  1785. return dst;
  1786. return NULL;
  1787. }
  1788. static int stale_bundle(struct dst_entry *dst)
  1789. {
  1790. return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
  1791. }
  1792. void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
  1793. {
  1794. while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
  1795. dst->dev = dev_net(dev)->loopback_dev;
  1796. dev_hold(dst->dev);
  1797. dev_put(dev);
  1798. }
  1799. }
  1800. EXPORT_SYMBOL(xfrm_dst_ifdown);
  1801. static void xfrm_link_failure(struct sk_buff *skb)
  1802. {
  1803. /* Impossible. Such dst must be popped before reaches point of failure. */
  1804. return;
  1805. }
  1806. static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
  1807. {
  1808. if (dst) {
  1809. if (dst->obsolete) {
  1810. dst_release(dst);
  1811. dst = NULL;
  1812. }
  1813. }
  1814. return dst;
  1815. }
  1816. static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
  1817. {
  1818. struct dst_entry *dst, **dstp;
  1819. write_lock(&pol->lock);
  1820. dstp = &pol->bundles;
  1821. while ((dst=*dstp) != NULL) {
  1822. if (func(dst)) {
  1823. *dstp = dst->next;
  1824. dst->next = *gc_list_p;
  1825. *gc_list_p = dst;
  1826. } else {
  1827. dstp = &dst->next;
  1828. }
  1829. }
  1830. write_unlock(&pol->lock);
  1831. }
  1832. static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
  1833. {
  1834. struct dst_entry *gc_list = NULL;
  1835. int dir;
  1836. read_lock_bh(&xfrm_policy_lock);
  1837. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1838. struct xfrm_policy *pol;
  1839. struct hlist_node *entry;
  1840. struct hlist_head *table;
  1841. int i;
  1842. hlist_for_each_entry(pol, entry,
  1843. &xfrm_policy_inexact[dir], bydst)
  1844. prune_one_bundle(pol, func, &gc_list);
  1845. table = xfrm_policy_bydst[dir].table;
  1846. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  1847. hlist_for_each_entry(pol, entry, table + i, bydst)
  1848. prune_one_bundle(pol, func, &gc_list);
  1849. }
  1850. }
  1851. read_unlock_bh(&xfrm_policy_lock);
  1852. while (gc_list) {
  1853. struct dst_entry *dst = gc_list;
  1854. gc_list = dst->next;
  1855. dst_free(dst);
  1856. }
  1857. }
  1858. static int unused_bundle(struct dst_entry *dst)
  1859. {
  1860. return !atomic_read(&dst->__refcnt);
  1861. }
  1862. static void __xfrm_garbage_collect(void)
  1863. {
  1864. xfrm_prune_bundles(unused_bundle);
  1865. }
  1866. static int xfrm_flush_bundles(void)
  1867. {
  1868. xfrm_prune_bundles(stale_bundle);
  1869. return 0;
  1870. }
  1871. static void xfrm_init_pmtu(struct dst_entry *dst)
  1872. {
  1873. do {
  1874. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1875. u32 pmtu, route_mtu_cached;
  1876. pmtu = dst_mtu(dst->child);
  1877. xdst->child_mtu_cached = pmtu;
  1878. pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
  1879. route_mtu_cached = dst_mtu(xdst->route);
  1880. xdst->route_mtu_cached = route_mtu_cached;
  1881. if (pmtu > route_mtu_cached)
  1882. pmtu = route_mtu_cached;
  1883. dst->metrics[RTAX_MTU-1] = pmtu;
  1884. } while ((dst = dst->next));
  1885. }
  1886. /* Check that the bundle accepts the flow and its components are
  1887. * still valid.
  1888. */
  1889. int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
  1890. struct flowi *fl, int family, int strict)
  1891. {
  1892. struct dst_entry *dst = &first->u.dst;
  1893. struct xfrm_dst *last;
  1894. u32 mtu;
  1895. if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
  1896. (dst->dev && !netif_running(dst->dev)))
  1897. return 0;
  1898. #ifdef CONFIG_XFRM_SUB_POLICY
  1899. if (fl) {
  1900. if (first->origin && !flow_cache_uli_match(first->origin, fl))
  1901. return 0;
  1902. if (first->partner &&
  1903. !xfrm_selector_match(first->partner, fl, family))
  1904. return 0;
  1905. }
  1906. #endif
  1907. last = NULL;
  1908. do {
  1909. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1910. if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
  1911. return 0;
  1912. if (fl && pol &&
  1913. !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
  1914. return 0;
  1915. if (dst->xfrm->km.state != XFRM_STATE_VALID)
  1916. return 0;
  1917. if (xdst->genid != dst->xfrm->genid)
  1918. return 0;
  1919. if (strict && fl &&
  1920. !(dst->xfrm->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
  1921. !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
  1922. return 0;
  1923. mtu = dst_mtu(dst->child);
  1924. if (xdst->child_mtu_cached != mtu) {
  1925. last = xdst;
  1926. xdst->child_mtu_cached = mtu;
  1927. }
  1928. if (!dst_check(xdst->route, xdst->route_cookie))
  1929. return 0;
  1930. mtu = dst_mtu(xdst->route);
  1931. if (xdst->route_mtu_cached != mtu) {
  1932. last = xdst;
  1933. xdst->route_mtu_cached = mtu;
  1934. }
  1935. dst = dst->child;
  1936. } while (dst->xfrm);
  1937. if (likely(!last))
  1938. return 1;
  1939. mtu = last->child_mtu_cached;
  1940. for (;;) {
  1941. dst = &last->u.dst;
  1942. mtu = xfrm_state_mtu(dst->xfrm, mtu);
  1943. if (mtu > last->route_mtu_cached)
  1944. mtu = last->route_mtu_cached;
  1945. dst->metrics[RTAX_MTU-1] = mtu;
  1946. if (last == first)
  1947. break;
  1948. last = (struct xfrm_dst *)last->u.dst.next;
  1949. last->child_mtu_cached = mtu;
  1950. }
  1951. return 1;
  1952. }
  1953. EXPORT_SYMBOL(xfrm_bundle_ok);
  1954. int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
  1955. {
  1956. int err = 0;
  1957. if (unlikely(afinfo == NULL))
  1958. return -EINVAL;
  1959. if (unlikely(afinfo->family >= NPROTO))
  1960. return -EAFNOSUPPORT;
  1961. write_lock_bh(&xfrm_policy_afinfo_lock);
  1962. if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
  1963. err = -ENOBUFS;
  1964. else {
  1965. struct dst_ops *dst_ops = afinfo->dst_ops;
  1966. if (likely(dst_ops->kmem_cachep == NULL))
  1967. dst_ops->kmem_cachep = xfrm_dst_cache;
  1968. if (likely(dst_ops->check == NULL))
  1969. dst_ops->check = xfrm_dst_check;
  1970. if (likely(dst_ops->negative_advice == NULL))
  1971. dst_ops->negative_advice = xfrm_negative_advice;
  1972. if (likely(dst_ops->link_failure == NULL))
  1973. dst_ops->link_failure = xfrm_link_failure;
  1974. if (likely(afinfo->garbage_collect == NULL))
  1975. afinfo->garbage_collect = __xfrm_garbage_collect;
  1976. xfrm_policy_afinfo[afinfo->family] = afinfo;
  1977. }
  1978. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1979. return err;
  1980. }
  1981. EXPORT_SYMBOL(xfrm_policy_register_afinfo);
  1982. int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
  1983. {
  1984. int err = 0;
  1985. if (unlikely(afinfo == NULL))
  1986. return -EINVAL;
  1987. if (unlikely(afinfo->family >= NPROTO))
  1988. return -EAFNOSUPPORT;
  1989. write_lock_bh(&xfrm_policy_afinfo_lock);
  1990. if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
  1991. if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
  1992. err = -EINVAL;
  1993. else {
  1994. struct dst_ops *dst_ops = afinfo->dst_ops;
  1995. xfrm_policy_afinfo[afinfo->family] = NULL;
  1996. dst_ops->kmem_cachep = NULL;
  1997. dst_ops->check = NULL;
  1998. dst_ops->negative_advice = NULL;
  1999. dst_ops->link_failure = NULL;
  2000. afinfo->garbage_collect = NULL;
  2001. }
  2002. }
  2003. write_unlock_bh(&xfrm_policy_afinfo_lock);
  2004. return err;
  2005. }
  2006. EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
  2007. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
  2008. {
  2009. struct xfrm_policy_afinfo *afinfo;
  2010. if (unlikely(family >= NPROTO))
  2011. return NULL;
  2012. read_lock(&xfrm_policy_afinfo_lock);
  2013. afinfo = xfrm_policy_afinfo[family];
  2014. if (unlikely(!afinfo))
  2015. read_unlock(&xfrm_policy_afinfo_lock);
  2016. return afinfo;
  2017. }
  2018. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
  2019. {
  2020. read_unlock(&xfrm_policy_afinfo_lock);
  2021. }
  2022. static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  2023. {
  2024. struct net_device *dev = ptr;
  2025. if (!net_eq(dev_net(dev), &init_net))
  2026. return NOTIFY_DONE;
  2027. switch (event) {
  2028. case NETDEV_DOWN:
  2029. xfrm_flush_bundles();
  2030. }
  2031. return NOTIFY_DONE;
  2032. }
  2033. static struct notifier_block xfrm_dev_notifier = {
  2034. .notifier_call = xfrm_dev_event,
  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 int __net_init xfrm_policy_init(struct net *net)
  2046. {
  2047. unsigned int hmask, sz;
  2048. int dir;
  2049. if (net_eq(net, &init_net))
  2050. xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
  2051. sizeof(struct xfrm_dst),
  2052. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2053. NULL);
  2054. hmask = 8 - 1;
  2055. sz = (hmask+1) * sizeof(struct hlist_head);
  2056. xfrm_policy_byidx = xfrm_hash_alloc(sz);
  2057. xfrm_idx_hmask = hmask;
  2058. if (!xfrm_policy_byidx)
  2059. panic("XFRM: failed to allocate byidx hash\n");
  2060. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  2061. struct xfrm_policy_hash *htab;
  2062. INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
  2063. htab = &xfrm_policy_bydst[dir];
  2064. htab->table = xfrm_hash_alloc(sz);
  2065. htab->hmask = hmask;
  2066. if (!htab->table)
  2067. panic("XFRM: failed to allocate bydst hash\n");
  2068. }
  2069. INIT_LIST_HEAD(&xfrm_policy_all);
  2070. if (net_eq(net, &init_net))
  2071. register_netdevice_notifier(&xfrm_dev_notifier);
  2072. return 0;
  2073. }
  2074. static void xfrm_policy_fini(struct net *net)
  2075. {
  2076. }
  2077. static int __net_init xfrm_net_init(struct net *net)
  2078. {
  2079. int rv;
  2080. rv = xfrm_state_init(net);
  2081. if (rv < 0)
  2082. goto out_state;
  2083. rv = xfrm_policy_init(net);
  2084. if (rv < 0)
  2085. goto out_policy;
  2086. return 0;
  2087. out_policy:
  2088. xfrm_state_fini(net);
  2089. out_state:
  2090. return rv;
  2091. }
  2092. static void __net_exit xfrm_net_exit(struct net *net)
  2093. {
  2094. xfrm_policy_fini(net);
  2095. xfrm_state_fini(net);
  2096. }
  2097. static struct pernet_operations __net_initdata xfrm_net_ops = {
  2098. .init = xfrm_net_init,
  2099. .exit = xfrm_net_exit,
  2100. };
  2101. void __init xfrm_init(void)
  2102. {
  2103. register_pernet_subsys(&xfrm_net_ops);
  2104. #ifdef CONFIG_XFRM_STATISTICS
  2105. xfrm_statistics_init();
  2106. #endif
  2107. xfrm_input_init();
  2108. #ifdef CONFIG_XFRM_STATISTICS
  2109. xfrm_proc_init();
  2110. #endif
  2111. }
  2112. #ifdef CONFIG_AUDITSYSCALL
  2113. static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
  2114. struct audit_buffer *audit_buf)
  2115. {
  2116. struct xfrm_sec_ctx *ctx = xp->security;
  2117. struct xfrm_selector *sel = &xp->selector;
  2118. if (ctx)
  2119. audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
  2120. ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
  2121. switch(sel->family) {
  2122. case AF_INET:
  2123. audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
  2124. if (sel->prefixlen_s != 32)
  2125. audit_log_format(audit_buf, " src_prefixlen=%d",
  2126. sel->prefixlen_s);
  2127. audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
  2128. if (sel->prefixlen_d != 32)
  2129. audit_log_format(audit_buf, " dst_prefixlen=%d",
  2130. sel->prefixlen_d);
  2131. break;
  2132. case AF_INET6:
  2133. audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
  2134. if (sel->prefixlen_s != 128)
  2135. audit_log_format(audit_buf, " src_prefixlen=%d",
  2136. sel->prefixlen_s);
  2137. audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
  2138. if (sel->prefixlen_d != 128)
  2139. audit_log_format(audit_buf, " dst_prefixlen=%d",
  2140. sel->prefixlen_d);
  2141. break;
  2142. }
  2143. }
  2144. void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
  2145. uid_t auid, u32 sessionid, u32 secid)
  2146. {
  2147. struct audit_buffer *audit_buf;
  2148. audit_buf = xfrm_audit_start("SPD-add");
  2149. if (audit_buf == NULL)
  2150. return;
  2151. xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
  2152. audit_log_format(audit_buf, " res=%u", result);
  2153. xfrm_audit_common_policyinfo(xp, audit_buf);
  2154. audit_log_end(audit_buf);
  2155. }
  2156. EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
  2157. void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
  2158. uid_t auid, u32 sessionid, u32 secid)
  2159. {
  2160. struct audit_buffer *audit_buf;
  2161. audit_buf = xfrm_audit_start("SPD-delete");
  2162. if (audit_buf == NULL)
  2163. return;
  2164. xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
  2165. audit_log_format(audit_buf, " res=%u", result);
  2166. xfrm_audit_common_policyinfo(xp, audit_buf);
  2167. audit_log_end(audit_buf);
  2168. }
  2169. EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
  2170. #endif
  2171. #ifdef CONFIG_XFRM_MIGRATE
  2172. static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
  2173. struct xfrm_selector *sel_tgt)
  2174. {
  2175. if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
  2176. if (sel_tgt->family == sel_cmp->family &&
  2177. xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
  2178. sel_cmp->family) == 0 &&
  2179. xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
  2180. sel_cmp->family) == 0 &&
  2181. sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
  2182. sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
  2183. return 1;
  2184. }
  2185. } else {
  2186. if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
  2187. return 1;
  2188. }
  2189. }
  2190. return 0;
  2191. }
  2192. static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
  2193. u8 dir, u8 type)
  2194. {
  2195. struct xfrm_policy *pol, *ret = NULL;
  2196. struct hlist_node *entry;
  2197. struct hlist_head *chain;
  2198. u32 priority = ~0U;
  2199. read_lock_bh(&xfrm_policy_lock);
  2200. chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
  2201. hlist_for_each_entry(pol, entry, chain, bydst) {
  2202. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  2203. pol->type == type) {
  2204. ret = pol;
  2205. priority = ret->priority;
  2206. break;
  2207. }
  2208. }
  2209. chain = &xfrm_policy_inexact[dir];
  2210. hlist_for_each_entry(pol, entry, chain, bydst) {
  2211. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  2212. pol->type == type &&
  2213. pol->priority < priority) {
  2214. ret = pol;
  2215. break;
  2216. }
  2217. }
  2218. if (ret)
  2219. xfrm_pol_hold(ret);
  2220. read_unlock_bh(&xfrm_policy_lock);
  2221. return ret;
  2222. }
  2223. static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
  2224. {
  2225. int match = 0;
  2226. if (t->mode == m->mode && t->id.proto == m->proto &&
  2227. (m->reqid == 0 || t->reqid == m->reqid)) {
  2228. switch (t->mode) {
  2229. case XFRM_MODE_TUNNEL:
  2230. case XFRM_MODE_BEET:
  2231. if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
  2232. m->old_family) == 0 &&
  2233. xfrm_addr_cmp(&t->saddr, &m->old_saddr,
  2234. m->old_family) == 0) {
  2235. match = 1;
  2236. }
  2237. break;
  2238. case XFRM_MODE_TRANSPORT:
  2239. /* in case of transport mode, template does not store
  2240. any IP addresses, hence we just compare mode and
  2241. protocol */
  2242. match = 1;
  2243. break;
  2244. default:
  2245. break;
  2246. }
  2247. }
  2248. return match;
  2249. }
  2250. /* update endpoint address(es) of template(s) */
  2251. static int xfrm_policy_migrate(struct xfrm_policy *pol,
  2252. struct xfrm_migrate *m, int num_migrate)
  2253. {
  2254. struct xfrm_migrate *mp;
  2255. struct dst_entry *dst;
  2256. int i, j, n = 0;
  2257. write_lock_bh(&pol->lock);
  2258. if (unlikely(pol->walk.dead)) {
  2259. /* target policy has been deleted */
  2260. write_unlock_bh(&pol->lock);
  2261. return -ENOENT;
  2262. }
  2263. for (i = 0; i < pol->xfrm_nr; i++) {
  2264. for (j = 0, mp = m; j < num_migrate; j++, mp++) {
  2265. if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
  2266. continue;
  2267. n++;
  2268. if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
  2269. pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
  2270. continue;
  2271. /* update endpoints */
  2272. memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
  2273. sizeof(pol->xfrm_vec[i].id.daddr));
  2274. memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
  2275. sizeof(pol->xfrm_vec[i].saddr));
  2276. pol->xfrm_vec[i].encap_family = mp->new_family;
  2277. /* flush bundles */
  2278. while ((dst = pol->bundles) != NULL) {
  2279. pol->bundles = dst->next;
  2280. dst_free(dst);
  2281. }
  2282. }
  2283. }
  2284. write_unlock_bh(&pol->lock);
  2285. if (!n)
  2286. return -ENODATA;
  2287. return 0;
  2288. }
  2289. static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
  2290. {
  2291. int i, j;
  2292. if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
  2293. return -EINVAL;
  2294. for (i = 0; i < num_migrate; i++) {
  2295. if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
  2296. m[i].old_family) == 0) &&
  2297. (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
  2298. m[i].old_family) == 0))
  2299. return -EINVAL;
  2300. if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
  2301. xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
  2302. return -EINVAL;
  2303. /* check if there is any duplicated entry */
  2304. for (j = i + 1; j < num_migrate; j++) {
  2305. if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
  2306. sizeof(m[i].old_daddr)) &&
  2307. !memcmp(&m[i].old_saddr, &m[j].old_saddr,
  2308. sizeof(m[i].old_saddr)) &&
  2309. m[i].proto == m[j].proto &&
  2310. m[i].mode == m[j].mode &&
  2311. m[i].reqid == m[j].reqid &&
  2312. m[i].old_family == m[j].old_family)
  2313. return -EINVAL;
  2314. }
  2315. }
  2316. return 0;
  2317. }
  2318. int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  2319. struct xfrm_migrate *m, int num_migrate,
  2320. struct xfrm_kmaddress *k)
  2321. {
  2322. int i, err, nx_cur = 0, nx_new = 0;
  2323. struct xfrm_policy *pol = NULL;
  2324. struct xfrm_state *x, *xc;
  2325. struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
  2326. struct xfrm_state *x_new[XFRM_MAX_DEPTH];
  2327. struct xfrm_migrate *mp;
  2328. if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
  2329. goto out;
  2330. /* Stage 1 - find policy */
  2331. if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
  2332. err = -ENOENT;
  2333. goto out;
  2334. }
  2335. /* Stage 2 - find and update state(s) */
  2336. for (i = 0, mp = m; i < num_migrate; i++, mp++) {
  2337. if ((x = xfrm_migrate_state_find(mp))) {
  2338. x_cur[nx_cur] = x;
  2339. nx_cur++;
  2340. if ((xc = xfrm_state_migrate(x, mp))) {
  2341. x_new[nx_new] = xc;
  2342. nx_new++;
  2343. } else {
  2344. err = -ENODATA;
  2345. goto restore_state;
  2346. }
  2347. }
  2348. }
  2349. /* Stage 3 - update policy */
  2350. if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
  2351. goto restore_state;
  2352. /* Stage 4 - delete old state(s) */
  2353. if (nx_cur) {
  2354. xfrm_states_put(x_cur, nx_cur);
  2355. xfrm_states_delete(x_cur, nx_cur);
  2356. }
  2357. /* Stage 5 - announce */
  2358. km_migrate(sel, dir, type, m, num_migrate, k);
  2359. xfrm_pol_put(pol);
  2360. return 0;
  2361. out:
  2362. return err;
  2363. restore_state:
  2364. if (pol)
  2365. xfrm_pol_put(pol);
  2366. if (nx_cur)
  2367. xfrm_states_put(x_cur, nx_cur);
  2368. if (nx_new)
  2369. xfrm_states_delete(x_new, nx_new);
  2370. return err;
  2371. }
  2372. EXPORT_SYMBOL(xfrm_migrate);
  2373. #endif