xfrm_policy.c 81 KB

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