xfrm_policy.c 71 KB

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