xfrm_policy.c 76 KB

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