xfrm_policy.c 77 KB

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