xfrm_policy.c 80 KB

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