xfrm_policy.c 77 KB

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