xfrm_policy.c 68 KB

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