xfrm_policy.c 80 KB

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