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