xfrm_policy.c 76 KB

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