xfrm_policy.c 81 KB

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