xfrm_policy.c 64 KB

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