xfrm_policy.c 66 KB

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