xfrm_policy.c 68 KB

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