xfrm_policy.c 80 KB

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