xfrm_policy.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962
  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 dst_entry *dst_prev = NULL;
  1194. struct dst_entry *dst0 = NULL;
  1195. int i = 0;
  1196. int err;
  1197. int header_len = 0;
  1198. int nfheader_len = 0;
  1199. int trailer_len = 0;
  1200. int tos;
  1201. int family = policy->selector.family;
  1202. xfrm_address_t saddr, daddr;
  1203. xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
  1204. tos = xfrm_get_tos(fl, family);
  1205. err = tos;
  1206. if (tos < 0)
  1207. goto put_states;
  1208. dst_hold(dst);
  1209. for (; i < nx; i++) {
  1210. struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
  1211. struct dst_entry *dst1 = &xdst->u.dst;
  1212. err = PTR_ERR(xdst);
  1213. if (IS_ERR(xdst)) {
  1214. dst_release(dst);
  1215. goto put_states;
  1216. }
  1217. if (!dst_prev)
  1218. dst0 = dst1;
  1219. else {
  1220. dst_prev->child = dst_clone(dst1);
  1221. dst1->flags |= DST_NOHASH;
  1222. }
  1223. xdst->route = dst;
  1224. dst_copy_metrics(dst1, dst);
  1225. if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
  1226. family = xfrm[i]->props.family;
  1227. dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
  1228. family);
  1229. err = PTR_ERR(dst);
  1230. if (IS_ERR(dst))
  1231. goto put_states;
  1232. } else
  1233. dst_hold(dst);
  1234. dst1->xfrm = xfrm[i];
  1235. xdst->xfrm_genid = xfrm[i]->genid;
  1236. dst1->obsolete = -1;
  1237. dst1->flags |= DST_HOST;
  1238. dst1->lastuse = now;
  1239. dst1->input = dst_discard;
  1240. dst1->output = xfrm[i]->outer_mode->afinfo->output;
  1241. dst1->next = dst_prev;
  1242. dst_prev = dst1;
  1243. header_len += xfrm[i]->props.header_len;
  1244. if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
  1245. nfheader_len += xfrm[i]->props.header_len;
  1246. trailer_len += xfrm[i]->props.trailer_len;
  1247. }
  1248. dst_prev->child = dst;
  1249. dst0->path = dst;
  1250. err = -ENODEV;
  1251. dev = dst->dev;
  1252. if (!dev)
  1253. goto free_dst;
  1254. /* Copy neighbour for reachability confirmation */
  1255. dst0->neighbour = neigh_clone(dst->neighbour);
  1256. xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
  1257. xfrm_init_pmtu(dst_prev);
  1258. for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
  1259. struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
  1260. err = xfrm_fill_dst(xdst, dev, fl);
  1261. if (err)
  1262. goto free_dst;
  1263. dst_prev->header_len = header_len;
  1264. dst_prev->trailer_len = trailer_len;
  1265. header_len -= xdst->u.dst.xfrm->props.header_len;
  1266. trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
  1267. }
  1268. out:
  1269. return dst0;
  1270. put_states:
  1271. for (; i < nx; i++)
  1272. xfrm_state_put(xfrm[i]);
  1273. free_dst:
  1274. if (dst0)
  1275. dst_free(dst0);
  1276. dst0 = ERR_PTR(err);
  1277. goto out;
  1278. }
  1279. static int inline
  1280. xfrm_dst_alloc_copy(void **target, const void *src, int size)
  1281. {
  1282. if (!*target) {
  1283. *target = kmalloc(size, GFP_ATOMIC);
  1284. if (!*target)
  1285. return -ENOMEM;
  1286. }
  1287. memcpy(*target, src, size);
  1288. return 0;
  1289. }
  1290. static int inline
  1291. xfrm_dst_update_parent(struct dst_entry *dst, const struct xfrm_selector *sel)
  1292. {
  1293. #ifdef CONFIG_XFRM_SUB_POLICY
  1294. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1295. return xfrm_dst_alloc_copy((void **)&(xdst->partner),
  1296. sel, sizeof(*sel));
  1297. #else
  1298. return 0;
  1299. #endif
  1300. }
  1301. static int inline
  1302. xfrm_dst_update_origin(struct dst_entry *dst, const struct flowi *fl)
  1303. {
  1304. #ifdef CONFIG_XFRM_SUB_POLICY
  1305. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1306. return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
  1307. #else
  1308. return 0;
  1309. #endif
  1310. }
  1311. static int xfrm_expand_policies(const struct flowi *fl, u16 family,
  1312. struct xfrm_policy **pols,
  1313. int *num_pols, int *num_xfrms)
  1314. {
  1315. int i;
  1316. if (*num_pols == 0 || !pols[0]) {
  1317. *num_pols = 0;
  1318. *num_xfrms = 0;
  1319. return 0;
  1320. }
  1321. if (IS_ERR(pols[0]))
  1322. return PTR_ERR(pols[0]);
  1323. *num_xfrms = pols[0]->xfrm_nr;
  1324. #ifdef CONFIG_XFRM_SUB_POLICY
  1325. if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
  1326. pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1327. pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
  1328. XFRM_POLICY_TYPE_MAIN,
  1329. fl, family,
  1330. XFRM_POLICY_OUT);
  1331. if (pols[1]) {
  1332. if (IS_ERR(pols[1])) {
  1333. xfrm_pols_put(pols, *num_pols);
  1334. return PTR_ERR(pols[1]);
  1335. }
  1336. (*num_pols) ++;
  1337. (*num_xfrms) += pols[1]->xfrm_nr;
  1338. }
  1339. }
  1340. #endif
  1341. for (i = 0; i < *num_pols; i++) {
  1342. if (pols[i]->action != XFRM_POLICY_ALLOW) {
  1343. *num_xfrms = -1;
  1344. break;
  1345. }
  1346. }
  1347. return 0;
  1348. }
  1349. static struct xfrm_dst *
  1350. xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
  1351. const struct flowi *fl, u16 family,
  1352. struct dst_entry *dst_orig)
  1353. {
  1354. struct net *net = xp_net(pols[0]);
  1355. struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
  1356. struct dst_entry *dst;
  1357. struct xfrm_dst *xdst;
  1358. int err;
  1359. /* Try to instantiate a bundle */
  1360. err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
  1361. if (err <= 0) {
  1362. if (err != 0 && err != -EAGAIN)
  1363. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
  1364. return ERR_PTR(err);
  1365. }
  1366. dst = xfrm_bundle_create(pols[0], xfrm, err, fl, dst_orig);
  1367. if (IS_ERR(dst)) {
  1368. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
  1369. return ERR_CAST(dst);
  1370. }
  1371. xdst = (struct xfrm_dst *)dst;
  1372. xdst->num_xfrms = err;
  1373. if (num_pols > 1)
  1374. err = xfrm_dst_update_parent(dst, &pols[1]->selector);
  1375. else
  1376. err = xfrm_dst_update_origin(dst, fl);
  1377. if (unlikely(err)) {
  1378. dst_free(dst);
  1379. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
  1380. return ERR_PTR(err);
  1381. }
  1382. xdst->num_pols = num_pols;
  1383. memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
  1384. xdst->policy_genid = atomic_read(&pols[0]->genid);
  1385. return xdst;
  1386. }
  1387. static struct flow_cache_object *
  1388. xfrm_bundle_lookup(struct net *net, const struct flowi *fl, u16 family, u8 dir,
  1389. struct flow_cache_object *oldflo, void *ctx)
  1390. {
  1391. struct dst_entry *dst_orig = (struct dst_entry *)ctx;
  1392. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1393. struct xfrm_dst *xdst, *new_xdst;
  1394. int num_pols = 0, num_xfrms = 0, i, err, pol_dead;
  1395. /* Check if the policies from old bundle are usable */
  1396. xdst = NULL;
  1397. if (oldflo) {
  1398. xdst = container_of(oldflo, struct xfrm_dst, flo);
  1399. num_pols = xdst->num_pols;
  1400. num_xfrms = xdst->num_xfrms;
  1401. pol_dead = 0;
  1402. for (i = 0; i < num_pols; i++) {
  1403. pols[i] = xdst->pols[i];
  1404. pol_dead |= pols[i]->walk.dead;
  1405. }
  1406. if (pol_dead) {
  1407. dst_free(&xdst->u.dst);
  1408. xdst = NULL;
  1409. num_pols = 0;
  1410. num_xfrms = 0;
  1411. oldflo = NULL;
  1412. }
  1413. }
  1414. /* Resolve policies to use if we couldn't get them from
  1415. * previous cache entry */
  1416. if (xdst == NULL) {
  1417. num_pols = 1;
  1418. pols[0] = __xfrm_policy_lookup(net, fl, family, dir);
  1419. err = xfrm_expand_policies(fl, family, pols,
  1420. &num_pols, &num_xfrms);
  1421. if (err < 0)
  1422. goto inc_error;
  1423. if (num_pols == 0)
  1424. return NULL;
  1425. if (num_xfrms <= 0)
  1426. goto make_dummy_bundle;
  1427. }
  1428. new_xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family, dst_orig);
  1429. if (IS_ERR(new_xdst)) {
  1430. err = PTR_ERR(new_xdst);
  1431. if (err != -EAGAIN)
  1432. goto error;
  1433. if (oldflo == NULL)
  1434. goto make_dummy_bundle;
  1435. dst_hold(&xdst->u.dst);
  1436. return oldflo;
  1437. } else if (new_xdst == NULL) {
  1438. num_xfrms = 0;
  1439. if (oldflo == NULL)
  1440. goto make_dummy_bundle;
  1441. xdst->num_xfrms = 0;
  1442. dst_hold(&xdst->u.dst);
  1443. return oldflo;
  1444. }
  1445. /* Kill the previous bundle */
  1446. if (xdst) {
  1447. /* The policies were stolen for newly generated bundle */
  1448. xdst->num_pols = 0;
  1449. dst_free(&xdst->u.dst);
  1450. }
  1451. /* Flow cache does not have reference, it dst_free()'s,
  1452. * but we do need to return one reference for original caller */
  1453. dst_hold(&new_xdst->u.dst);
  1454. return &new_xdst->flo;
  1455. make_dummy_bundle:
  1456. /* We found policies, but there's no bundles to instantiate:
  1457. * either because the policy blocks, has no transformations or
  1458. * we could not build template (no xfrm_states).*/
  1459. xdst = xfrm_alloc_dst(net, family);
  1460. if (IS_ERR(xdst)) {
  1461. xfrm_pols_put(pols, num_pols);
  1462. return ERR_CAST(xdst);
  1463. }
  1464. xdst->num_pols = num_pols;
  1465. xdst->num_xfrms = num_xfrms;
  1466. memcpy(xdst->pols, pols, sizeof(struct xfrm_policy*) * num_pols);
  1467. dst_hold(&xdst->u.dst);
  1468. return &xdst->flo;
  1469. inc_error:
  1470. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
  1471. error:
  1472. if (xdst != NULL)
  1473. dst_free(&xdst->u.dst);
  1474. else
  1475. xfrm_pols_put(pols, num_pols);
  1476. return ERR_PTR(err);
  1477. }
  1478. static struct dst_entry *make_blackhole(struct net *net, u16 family,
  1479. struct dst_entry *dst_orig)
  1480. {
  1481. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1482. struct dst_entry *ret;
  1483. if (!afinfo) {
  1484. dst_release(dst_orig);
  1485. ret = ERR_PTR(-EINVAL);
  1486. } else {
  1487. ret = afinfo->blackhole_route(net, dst_orig);
  1488. }
  1489. xfrm_policy_put_afinfo(afinfo);
  1490. return ret;
  1491. }
  1492. /* Main function: finds/creates a bundle for given flow.
  1493. *
  1494. * At the moment we eat a raw IP route. Mostly to speed up lookups
  1495. * on interfaces with disabled IPsec.
  1496. */
  1497. struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
  1498. const struct flowi *fl,
  1499. struct sock *sk, int flags)
  1500. {
  1501. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1502. struct flow_cache_object *flo;
  1503. struct xfrm_dst *xdst;
  1504. struct dst_entry *dst, *route;
  1505. u16 family = dst_orig->ops->family;
  1506. u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
  1507. int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
  1508. restart:
  1509. dst = NULL;
  1510. xdst = NULL;
  1511. route = NULL;
  1512. if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
  1513. num_pols = 1;
  1514. pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
  1515. err = xfrm_expand_policies(fl, family, pols,
  1516. &num_pols, &num_xfrms);
  1517. if (err < 0)
  1518. goto dropdst;
  1519. if (num_pols) {
  1520. if (num_xfrms <= 0) {
  1521. drop_pols = num_pols;
  1522. goto no_transform;
  1523. }
  1524. xdst = xfrm_resolve_and_create_bundle(
  1525. pols, num_pols, fl,
  1526. family, dst_orig);
  1527. if (IS_ERR(xdst)) {
  1528. xfrm_pols_put(pols, num_pols);
  1529. err = PTR_ERR(xdst);
  1530. goto dropdst;
  1531. } else if (xdst == NULL) {
  1532. num_xfrms = 0;
  1533. drop_pols = num_pols;
  1534. goto no_transform;
  1535. }
  1536. dst_hold(&xdst->u.dst);
  1537. spin_lock_bh(&xfrm_policy_sk_bundle_lock);
  1538. xdst->u.dst.next = xfrm_policy_sk_bundles;
  1539. xfrm_policy_sk_bundles = &xdst->u.dst;
  1540. spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
  1541. route = xdst->route;
  1542. }
  1543. }
  1544. if (xdst == NULL) {
  1545. /* To accelerate a bit... */
  1546. if ((dst_orig->flags & DST_NOXFRM) ||
  1547. !net->xfrm.policy_count[XFRM_POLICY_OUT])
  1548. goto nopol;
  1549. flo = flow_cache_lookup(net, fl, family, dir,
  1550. xfrm_bundle_lookup, dst_orig);
  1551. if (flo == NULL)
  1552. goto nopol;
  1553. if (IS_ERR(flo)) {
  1554. err = PTR_ERR(flo);
  1555. goto dropdst;
  1556. }
  1557. xdst = container_of(flo, struct xfrm_dst, flo);
  1558. num_pols = xdst->num_pols;
  1559. num_xfrms = xdst->num_xfrms;
  1560. memcpy(pols, xdst->pols, sizeof(struct xfrm_policy*) * num_pols);
  1561. route = xdst->route;
  1562. }
  1563. dst = &xdst->u.dst;
  1564. if (route == NULL && num_xfrms > 0) {
  1565. /* The only case when xfrm_bundle_lookup() returns a
  1566. * bundle with null route, is when the template could
  1567. * not be resolved. It means policies are there, but
  1568. * bundle could not be created, since we don't yet
  1569. * have the xfrm_state's. We need to wait for KM to
  1570. * negotiate new SA's or bail out with error.*/
  1571. if (net->xfrm.sysctl_larval_drop) {
  1572. /* EREMOTE tells the caller to generate
  1573. * a one-shot blackhole route. */
  1574. dst_release(dst);
  1575. xfrm_pols_put(pols, drop_pols);
  1576. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
  1577. return make_blackhole(net, family, dst_orig);
  1578. }
  1579. if (fl->flowi_flags & FLOWI_FLAG_CAN_SLEEP) {
  1580. DECLARE_WAITQUEUE(wait, current);
  1581. add_wait_queue(&net->xfrm.km_waitq, &wait);
  1582. set_current_state(TASK_INTERRUPTIBLE);
  1583. schedule();
  1584. set_current_state(TASK_RUNNING);
  1585. remove_wait_queue(&net->xfrm.km_waitq, &wait);
  1586. if (!signal_pending(current)) {
  1587. dst_release(dst);
  1588. goto restart;
  1589. }
  1590. err = -ERESTART;
  1591. } else
  1592. err = -EAGAIN;
  1593. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
  1594. goto error;
  1595. }
  1596. no_transform:
  1597. if (num_pols == 0)
  1598. goto nopol;
  1599. if ((flags & XFRM_LOOKUP_ICMP) &&
  1600. !(pols[0]->flags & XFRM_POLICY_ICMP)) {
  1601. err = -ENOENT;
  1602. goto error;
  1603. }
  1604. for (i = 0; i < num_pols; i++)
  1605. pols[i]->curlft.use_time = get_seconds();
  1606. if (num_xfrms < 0) {
  1607. /* Prohibit the flow */
  1608. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
  1609. err = -EPERM;
  1610. goto error;
  1611. } else if (num_xfrms > 0) {
  1612. /* Flow transformed */
  1613. dst_release(dst_orig);
  1614. } else {
  1615. /* Flow passes untransformed */
  1616. dst_release(dst);
  1617. dst = dst_orig;
  1618. }
  1619. ok:
  1620. xfrm_pols_put(pols, drop_pols);
  1621. return dst;
  1622. nopol:
  1623. if (!(flags & XFRM_LOOKUP_ICMP)) {
  1624. dst = dst_orig;
  1625. goto ok;
  1626. }
  1627. err = -ENOENT;
  1628. error:
  1629. dst_release(dst);
  1630. dropdst:
  1631. dst_release(dst_orig);
  1632. xfrm_pols_put(pols, drop_pols);
  1633. return ERR_PTR(err);
  1634. }
  1635. EXPORT_SYMBOL(xfrm_lookup);
  1636. static inline int
  1637. xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
  1638. {
  1639. struct xfrm_state *x;
  1640. if (!skb->sp || idx < 0 || idx >= skb->sp->len)
  1641. return 0;
  1642. x = skb->sp->xvec[idx];
  1643. if (!x->type->reject)
  1644. return 0;
  1645. return x->type->reject(x, skb, fl);
  1646. }
  1647. /* When skb is transformed back to its "native" form, we have to
  1648. * check policy restrictions. At the moment we make this in maximally
  1649. * stupid way. Shame on me. :-) Of course, connected sockets must
  1650. * have policy cached at them.
  1651. */
  1652. static inline int
  1653. xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
  1654. unsigned short family)
  1655. {
  1656. if (xfrm_state_kern(x))
  1657. return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
  1658. return x->id.proto == tmpl->id.proto &&
  1659. (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
  1660. (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
  1661. x->props.mode == tmpl->mode &&
  1662. (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
  1663. !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
  1664. !(x->props.mode != XFRM_MODE_TRANSPORT &&
  1665. xfrm_state_addr_cmp(tmpl, x, family));
  1666. }
  1667. /*
  1668. * 0 or more than 0 is returned when validation is succeeded (either bypass
  1669. * because of optional transport mode, or next index of the mathced secpath
  1670. * state with the template.
  1671. * -1 is returned when no matching template is found.
  1672. * Otherwise "-2 - errored_index" is returned.
  1673. */
  1674. static inline int
  1675. xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
  1676. unsigned short family)
  1677. {
  1678. int idx = start;
  1679. if (tmpl->optional) {
  1680. if (tmpl->mode == XFRM_MODE_TRANSPORT)
  1681. return start;
  1682. } else
  1683. start = -1;
  1684. for (; idx < sp->len; idx++) {
  1685. if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
  1686. return ++idx;
  1687. if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
  1688. if (start == -1)
  1689. start = -2-idx;
  1690. break;
  1691. }
  1692. }
  1693. return start;
  1694. }
  1695. int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
  1696. unsigned int family, int reverse)
  1697. {
  1698. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1699. int err;
  1700. if (unlikely(afinfo == NULL))
  1701. return -EAFNOSUPPORT;
  1702. afinfo->decode_session(skb, fl, reverse);
  1703. err = security_xfrm_decode_session(skb, &fl->flowi_secid);
  1704. xfrm_policy_put_afinfo(afinfo);
  1705. return err;
  1706. }
  1707. EXPORT_SYMBOL(__xfrm_decode_session);
  1708. static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
  1709. {
  1710. for (; k < sp->len; k++) {
  1711. if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
  1712. *idxp = k;
  1713. return 1;
  1714. }
  1715. }
  1716. return 0;
  1717. }
  1718. int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
  1719. unsigned short family)
  1720. {
  1721. struct net *net = dev_net(skb->dev);
  1722. struct xfrm_policy *pol;
  1723. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1724. int npols = 0;
  1725. int xfrm_nr;
  1726. int pi;
  1727. int reverse;
  1728. struct flowi fl;
  1729. u8 fl_dir;
  1730. int xerr_idx = -1;
  1731. reverse = dir & ~XFRM_POLICY_MASK;
  1732. dir &= XFRM_POLICY_MASK;
  1733. fl_dir = policy_to_flow_dir(dir);
  1734. if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
  1735. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  1736. return 0;
  1737. }
  1738. nf_nat_decode_session(skb, &fl, family);
  1739. /* First, check used SA against their selectors. */
  1740. if (skb->sp) {
  1741. int i;
  1742. for (i=skb->sp->len-1; i>=0; i--) {
  1743. struct xfrm_state *x = skb->sp->xvec[i];
  1744. if (!xfrm_selector_match(&x->sel, &fl, family)) {
  1745. XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
  1746. return 0;
  1747. }
  1748. }
  1749. }
  1750. pol = NULL;
  1751. if (sk && sk->sk_policy[dir]) {
  1752. pol = xfrm_sk_policy_lookup(sk, dir, &fl);
  1753. if (IS_ERR(pol)) {
  1754. XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
  1755. return 0;
  1756. }
  1757. }
  1758. if (!pol) {
  1759. struct flow_cache_object *flo;
  1760. flo = flow_cache_lookup(net, &fl, family, fl_dir,
  1761. xfrm_policy_lookup, NULL);
  1762. if (IS_ERR_OR_NULL(flo))
  1763. pol = ERR_CAST(flo);
  1764. else
  1765. pol = container_of(flo, struct xfrm_policy, flo);
  1766. }
  1767. if (IS_ERR(pol)) {
  1768. XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
  1769. return 0;
  1770. }
  1771. if (!pol) {
  1772. if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
  1773. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1774. XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
  1775. return 0;
  1776. }
  1777. return 1;
  1778. }
  1779. pol->curlft.use_time = get_seconds();
  1780. pols[0] = pol;
  1781. npols ++;
  1782. #ifdef CONFIG_XFRM_SUB_POLICY
  1783. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1784. pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
  1785. &fl, family,
  1786. XFRM_POLICY_IN);
  1787. if (pols[1]) {
  1788. if (IS_ERR(pols[1])) {
  1789. XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
  1790. return 0;
  1791. }
  1792. pols[1]->curlft.use_time = get_seconds();
  1793. npols ++;
  1794. }
  1795. }
  1796. #endif
  1797. if (pol->action == XFRM_POLICY_ALLOW) {
  1798. struct sec_path *sp;
  1799. static struct sec_path dummy;
  1800. struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
  1801. struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
  1802. struct xfrm_tmpl **tpp = tp;
  1803. int ti = 0;
  1804. int i, k;
  1805. if ((sp = skb->sp) == NULL)
  1806. sp = &dummy;
  1807. for (pi = 0; pi < npols; pi++) {
  1808. if (pols[pi] != pol &&
  1809. pols[pi]->action != XFRM_POLICY_ALLOW) {
  1810. XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
  1811. goto reject;
  1812. }
  1813. if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
  1814. XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
  1815. goto reject_error;
  1816. }
  1817. for (i = 0; i < pols[pi]->xfrm_nr; i++)
  1818. tpp[ti++] = &pols[pi]->xfrm_vec[i];
  1819. }
  1820. xfrm_nr = ti;
  1821. if (npols > 1) {
  1822. xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
  1823. tpp = stp;
  1824. }
  1825. /* For each tunnel xfrm, find the first matching tmpl.
  1826. * For each tmpl before that, find corresponding xfrm.
  1827. * Order is _important_. Later we will implement
  1828. * some barriers, but at the moment barriers
  1829. * are implied between each two transformations.
  1830. */
  1831. for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
  1832. k = xfrm_policy_ok(tpp[i], sp, k, family);
  1833. if (k < 0) {
  1834. if (k < -1)
  1835. /* "-2 - errored_index" returned */
  1836. xerr_idx = -(2+k);
  1837. XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
  1838. goto reject;
  1839. }
  1840. }
  1841. if (secpath_has_nontransport(sp, k, &xerr_idx)) {
  1842. XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
  1843. goto reject;
  1844. }
  1845. xfrm_pols_put(pols, npols);
  1846. return 1;
  1847. }
  1848. XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
  1849. reject:
  1850. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1851. reject_error:
  1852. xfrm_pols_put(pols, npols);
  1853. return 0;
  1854. }
  1855. EXPORT_SYMBOL(__xfrm_policy_check);
  1856. int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
  1857. {
  1858. struct net *net = dev_net(skb->dev);
  1859. struct flowi fl;
  1860. struct dst_entry *dst;
  1861. int res = 1;
  1862. if (xfrm_decode_session(skb, &fl, family) < 0) {
  1863. XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
  1864. return 0;
  1865. }
  1866. skb_dst_force(skb);
  1867. dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, 0);
  1868. if (IS_ERR(dst)) {
  1869. res = 0;
  1870. dst = NULL;
  1871. }
  1872. skb_dst_set(skb, dst);
  1873. return res;
  1874. }
  1875. EXPORT_SYMBOL(__xfrm_route_forward);
  1876. /* Optimize later using cookies and generation ids. */
  1877. static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
  1878. {
  1879. /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
  1880. * to "-1" to force all XFRM destinations to get validated by
  1881. * dst_ops->check on every use. We do this because when a
  1882. * normal route referenced by an XFRM dst is obsoleted we do
  1883. * not go looking around for all parent referencing XFRM dsts
  1884. * so that we can invalidate them. It is just too much work.
  1885. * Instead we make the checks here on every use. For example:
  1886. *
  1887. * XFRM dst A --> IPv4 dst X
  1888. *
  1889. * X is the "xdst->route" of A (X is also the "dst->path" of A
  1890. * in this example). If X is marked obsolete, "A" will not
  1891. * notice. That's what we are validating here via the
  1892. * stale_bundle() check.
  1893. *
  1894. * When a policy's bundle is pruned, we dst_free() the XFRM
  1895. * dst which causes it's ->obsolete field to be set to a
  1896. * positive non-zero integer. If an XFRM dst has been pruned
  1897. * like this, we want to force a new route lookup.
  1898. */
  1899. if (dst->obsolete < 0 && !stale_bundle(dst))
  1900. return dst;
  1901. return NULL;
  1902. }
  1903. static int stale_bundle(struct dst_entry *dst)
  1904. {
  1905. return !xfrm_bundle_ok((struct xfrm_dst *)dst, AF_UNSPEC);
  1906. }
  1907. void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
  1908. {
  1909. while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
  1910. dst->dev = dev_net(dev)->loopback_dev;
  1911. dev_hold(dst->dev);
  1912. dev_put(dev);
  1913. }
  1914. }
  1915. EXPORT_SYMBOL(xfrm_dst_ifdown);
  1916. static void xfrm_link_failure(struct sk_buff *skb)
  1917. {
  1918. /* Impossible. Such dst must be popped before reaches point of failure. */
  1919. }
  1920. static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
  1921. {
  1922. if (dst) {
  1923. if (dst->obsolete) {
  1924. dst_release(dst);
  1925. dst = NULL;
  1926. }
  1927. }
  1928. return dst;
  1929. }
  1930. static void __xfrm_garbage_collect(struct net *net)
  1931. {
  1932. struct dst_entry *head, *next;
  1933. flow_cache_flush();
  1934. spin_lock_bh(&xfrm_policy_sk_bundle_lock);
  1935. head = xfrm_policy_sk_bundles;
  1936. xfrm_policy_sk_bundles = NULL;
  1937. spin_unlock_bh(&xfrm_policy_sk_bundle_lock);
  1938. while (head) {
  1939. next = head->next;
  1940. dst_free(head);
  1941. head = next;
  1942. }
  1943. }
  1944. static void xfrm_init_pmtu(struct dst_entry *dst)
  1945. {
  1946. do {
  1947. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1948. u32 pmtu, route_mtu_cached;
  1949. pmtu = dst_mtu(dst->child);
  1950. xdst->child_mtu_cached = pmtu;
  1951. pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
  1952. route_mtu_cached = dst_mtu(xdst->route);
  1953. xdst->route_mtu_cached = route_mtu_cached;
  1954. if (pmtu > route_mtu_cached)
  1955. pmtu = route_mtu_cached;
  1956. dst_metric_set(dst, RTAX_MTU, pmtu);
  1957. } while ((dst = dst->next));
  1958. }
  1959. /* Check that the bundle accepts the flow and its components are
  1960. * still valid.
  1961. */
  1962. static int xfrm_bundle_ok(struct xfrm_dst *first, int family)
  1963. {
  1964. struct dst_entry *dst = &first->u.dst;
  1965. struct xfrm_dst *last;
  1966. u32 mtu;
  1967. if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
  1968. (dst->dev && !netif_running(dst->dev)))
  1969. return 0;
  1970. last = NULL;
  1971. do {
  1972. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1973. if (dst->xfrm->km.state != XFRM_STATE_VALID)
  1974. return 0;
  1975. if (xdst->xfrm_genid != dst->xfrm->genid)
  1976. return 0;
  1977. if (xdst->num_pols > 0 &&
  1978. xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
  1979. return 0;
  1980. mtu = dst_mtu(dst->child);
  1981. if (xdst->child_mtu_cached != mtu) {
  1982. last = xdst;
  1983. xdst->child_mtu_cached = mtu;
  1984. }
  1985. if (!dst_check(xdst->route, xdst->route_cookie))
  1986. return 0;
  1987. mtu = dst_mtu(xdst->route);
  1988. if (xdst->route_mtu_cached != mtu) {
  1989. last = xdst;
  1990. xdst->route_mtu_cached = mtu;
  1991. }
  1992. dst = dst->child;
  1993. } while (dst->xfrm);
  1994. if (likely(!last))
  1995. return 1;
  1996. mtu = last->child_mtu_cached;
  1997. for (;;) {
  1998. dst = &last->u.dst;
  1999. mtu = xfrm_state_mtu(dst->xfrm, mtu);
  2000. if (mtu > last->route_mtu_cached)
  2001. mtu = last->route_mtu_cached;
  2002. dst_metric_set(dst, RTAX_MTU, mtu);
  2003. if (last == first)
  2004. break;
  2005. last = (struct xfrm_dst *)last->u.dst.next;
  2006. last->child_mtu_cached = mtu;
  2007. }
  2008. return 1;
  2009. }
  2010. static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
  2011. {
  2012. return dst_metric_advmss(dst->path);
  2013. }
  2014. static unsigned int xfrm_default_mtu(const struct dst_entry *dst)
  2015. {
  2016. return dst_mtu(dst->path);
  2017. }
  2018. int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
  2019. {
  2020. struct net *net;
  2021. int err = 0;
  2022. if (unlikely(afinfo == NULL))
  2023. return -EINVAL;
  2024. if (unlikely(afinfo->family >= NPROTO))
  2025. return -EAFNOSUPPORT;
  2026. write_lock_bh(&xfrm_policy_afinfo_lock);
  2027. if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
  2028. err = -ENOBUFS;
  2029. else {
  2030. struct dst_ops *dst_ops = afinfo->dst_ops;
  2031. if (likely(dst_ops->kmem_cachep == NULL))
  2032. dst_ops->kmem_cachep = xfrm_dst_cache;
  2033. if (likely(dst_ops->check == NULL))
  2034. dst_ops->check = xfrm_dst_check;
  2035. if (likely(dst_ops->default_advmss == NULL))
  2036. dst_ops->default_advmss = xfrm_default_advmss;
  2037. if (likely(dst_ops->default_mtu == NULL))
  2038. dst_ops->default_mtu = xfrm_default_mtu;
  2039. if (likely(dst_ops->negative_advice == NULL))
  2040. dst_ops->negative_advice = xfrm_negative_advice;
  2041. if (likely(dst_ops->link_failure == NULL))
  2042. dst_ops->link_failure = xfrm_link_failure;
  2043. if (likely(afinfo->garbage_collect == NULL))
  2044. afinfo->garbage_collect = __xfrm_garbage_collect;
  2045. xfrm_policy_afinfo[afinfo->family] = afinfo;
  2046. }
  2047. write_unlock_bh(&xfrm_policy_afinfo_lock);
  2048. rtnl_lock();
  2049. for_each_net(net) {
  2050. struct dst_ops *xfrm_dst_ops;
  2051. switch (afinfo->family) {
  2052. case AF_INET:
  2053. xfrm_dst_ops = &net->xfrm.xfrm4_dst_ops;
  2054. break;
  2055. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  2056. case AF_INET6:
  2057. xfrm_dst_ops = &net->xfrm.xfrm6_dst_ops;
  2058. break;
  2059. #endif
  2060. default:
  2061. BUG();
  2062. }
  2063. *xfrm_dst_ops = *afinfo->dst_ops;
  2064. }
  2065. rtnl_unlock();
  2066. return err;
  2067. }
  2068. EXPORT_SYMBOL(xfrm_policy_register_afinfo);
  2069. int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
  2070. {
  2071. int err = 0;
  2072. if (unlikely(afinfo == NULL))
  2073. return -EINVAL;
  2074. if (unlikely(afinfo->family >= NPROTO))
  2075. return -EAFNOSUPPORT;
  2076. write_lock_bh(&xfrm_policy_afinfo_lock);
  2077. if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
  2078. if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
  2079. err = -EINVAL;
  2080. else {
  2081. struct dst_ops *dst_ops = afinfo->dst_ops;
  2082. xfrm_policy_afinfo[afinfo->family] = NULL;
  2083. dst_ops->kmem_cachep = NULL;
  2084. dst_ops->check = NULL;
  2085. dst_ops->negative_advice = NULL;
  2086. dst_ops->link_failure = NULL;
  2087. afinfo->garbage_collect = NULL;
  2088. }
  2089. }
  2090. write_unlock_bh(&xfrm_policy_afinfo_lock);
  2091. return err;
  2092. }
  2093. EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
  2094. static void __net_init xfrm_dst_ops_init(struct net *net)
  2095. {
  2096. struct xfrm_policy_afinfo *afinfo;
  2097. read_lock_bh(&xfrm_policy_afinfo_lock);
  2098. afinfo = xfrm_policy_afinfo[AF_INET];
  2099. if (afinfo)
  2100. net->xfrm.xfrm4_dst_ops = *afinfo->dst_ops;
  2101. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  2102. afinfo = xfrm_policy_afinfo[AF_INET6];
  2103. if (afinfo)
  2104. net->xfrm.xfrm6_dst_ops = *afinfo->dst_ops;
  2105. #endif
  2106. read_unlock_bh(&xfrm_policy_afinfo_lock);
  2107. }
  2108. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
  2109. {
  2110. struct xfrm_policy_afinfo *afinfo;
  2111. if (unlikely(family >= NPROTO))
  2112. return NULL;
  2113. read_lock(&xfrm_policy_afinfo_lock);
  2114. afinfo = xfrm_policy_afinfo[family];
  2115. if (unlikely(!afinfo))
  2116. read_unlock(&xfrm_policy_afinfo_lock);
  2117. return afinfo;
  2118. }
  2119. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
  2120. {
  2121. read_unlock(&xfrm_policy_afinfo_lock);
  2122. }
  2123. static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  2124. {
  2125. struct net_device *dev = ptr;
  2126. switch (event) {
  2127. case NETDEV_DOWN:
  2128. __xfrm_garbage_collect(dev_net(dev));
  2129. }
  2130. return NOTIFY_DONE;
  2131. }
  2132. static struct notifier_block xfrm_dev_notifier = {
  2133. .notifier_call = xfrm_dev_event,
  2134. };
  2135. #ifdef CONFIG_XFRM_STATISTICS
  2136. static int __net_init xfrm_statistics_init(struct net *net)
  2137. {
  2138. int rv;
  2139. if (snmp_mib_init((void __percpu **)net->mib.xfrm_statistics,
  2140. sizeof(struct linux_xfrm_mib),
  2141. __alignof__(struct linux_xfrm_mib)) < 0)
  2142. return -ENOMEM;
  2143. rv = xfrm_proc_init(net);
  2144. if (rv < 0)
  2145. snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
  2146. return rv;
  2147. }
  2148. static void xfrm_statistics_fini(struct net *net)
  2149. {
  2150. xfrm_proc_fini(net);
  2151. snmp_mib_free((void __percpu **)net->mib.xfrm_statistics);
  2152. }
  2153. #else
  2154. static int __net_init xfrm_statistics_init(struct net *net)
  2155. {
  2156. return 0;
  2157. }
  2158. static void xfrm_statistics_fini(struct net *net)
  2159. {
  2160. }
  2161. #endif
  2162. static int __net_init xfrm_policy_init(struct net *net)
  2163. {
  2164. unsigned int hmask, sz;
  2165. int dir;
  2166. if (net_eq(net, &init_net))
  2167. xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
  2168. sizeof(struct xfrm_dst),
  2169. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2170. NULL);
  2171. hmask = 8 - 1;
  2172. sz = (hmask+1) * sizeof(struct hlist_head);
  2173. net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
  2174. if (!net->xfrm.policy_byidx)
  2175. goto out_byidx;
  2176. net->xfrm.policy_idx_hmask = hmask;
  2177. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  2178. struct xfrm_policy_hash *htab;
  2179. net->xfrm.policy_count[dir] = 0;
  2180. INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
  2181. htab = &net->xfrm.policy_bydst[dir];
  2182. htab->table = xfrm_hash_alloc(sz);
  2183. if (!htab->table)
  2184. goto out_bydst;
  2185. htab->hmask = hmask;
  2186. }
  2187. INIT_LIST_HEAD(&net->xfrm.policy_all);
  2188. INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
  2189. if (net_eq(net, &init_net))
  2190. register_netdevice_notifier(&xfrm_dev_notifier);
  2191. return 0;
  2192. out_bydst:
  2193. for (dir--; dir >= 0; dir--) {
  2194. struct xfrm_policy_hash *htab;
  2195. htab = &net->xfrm.policy_bydst[dir];
  2196. xfrm_hash_free(htab->table, sz);
  2197. }
  2198. xfrm_hash_free(net->xfrm.policy_byidx, sz);
  2199. out_byidx:
  2200. return -ENOMEM;
  2201. }
  2202. static void xfrm_policy_fini(struct net *net)
  2203. {
  2204. struct xfrm_audit audit_info;
  2205. unsigned int sz;
  2206. int dir;
  2207. flush_work(&net->xfrm.policy_hash_work);
  2208. #ifdef CONFIG_XFRM_SUB_POLICY
  2209. audit_info.loginuid = -1;
  2210. audit_info.sessionid = -1;
  2211. audit_info.secid = 0;
  2212. xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, &audit_info);
  2213. #endif
  2214. audit_info.loginuid = -1;
  2215. audit_info.sessionid = -1;
  2216. audit_info.secid = 0;
  2217. xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, &audit_info);
  2218. WARN_ON(!list_empty(&net->xfrm.policy_all));
  2219. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  2220. struct xfrm_policy_hash *htab;
  2221. WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
  2222. htab = &net->xfrm.policy_bydst[dir];
  2223. sz = (htab->hmask + 1);
  2224. WARN_ON(!hlist_empty(htab->table));
  2225. xfrm_hash_free(htab->table, sz);
  2226. }
  2227. sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
  2228. WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
  2229. xfrm_hash_free(net->xfrm.policy_byidx, sz);
  2230. }
  2231. static int __net_init xfrm_net_init(struct net *net)
  2232. {
  2233. int rv;
  2234. rv = xfrm_statistics_init(net);
  2235. if (rv < 0)
  2236. goto out_statistics;
  2237. rv = xfrm_state_init(net);
  2238. if (rv < 0)
  2239. goto out_state;
  2240. rv = xfrm_policy_init(net);
  2241. if (rv < 0)
  2242. goto out_policy;
  2243. xfrm_dst_ops_init(net);
  2244. rv = xfrm_sysctl_init(net);
  2245. if (rv < 0)
  2246. goto out_sysctl;
  2247. return 0;
  2248. out_sysctl:
  2249. xfrm_policy_fini(net);
  2250. out_policy:
  2251. xfrm_state_fini(net);
  2252. out_state:
  2253. xfrm_statistics_fini(net);
  2254. out_statistics:
  2255. return rv;
  2256. }
  2257. static void __net_exit xfrm_net_exit(struct net *net)
  2258. {
  2259. xfrm_sysctl_fini(net);
  2260. xfrm_policy_fini(net);
  2261. xfrm_state_fini(net);
  2262. xfrm_statistics_fini(net);
  2263. }
  2264. static struct pernet_operations __net_initdata xfrm_net_ops = {
  2265. .init = xfrm_net_init,
  2266. .exit = xfrm_net_exit,
  2267. };
  2268. void __init xfrm_init(void)
  2269. {
  2270. register_pernet_subsys(&xfrm_net_ops);
  2271. xfrm_input_init();
  2272. }
  2273. #ifdef CONFIG_AUDITSYSCALL
  2274. static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
  2275. struct audit_buffer *audit_buf)
  2276. {
  2277. struct xfrm_sec_ctx *ctx = xp->security;
  2278. struct xfrm_selector *sel = &xp->selector;
  2279. if (ctx)
  2280. audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
  2281. ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
  2282. switch(sel->family) {
  2283. case AF_INET:
  2284. audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
  2285. if (sel->prefixlen_s != 32)
  2286. audit_log_format(audit_buf, " src_prefixlen=%d",
  2287. sel->prefixlen_s);
  2288. audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
  2289. if (sel->prefixlen_d != 32)
  2290. audit_log_format(audit_buf, " dst_prefixlen=%d",
  2291. sel->prefixlen_d);
  2292. break;
  2293. case AF_INET6:
  2294. audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
  2295. if (sel->prefixlen_s != 128)
  2296. audit_log_format(audit_buf, " src_prefixlen=%d",
  2297. sel->prefixlen_s);
  2298. audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
  2299. if (sel->prefixlen_d != 128)
  2300. audit_log_format(audit_buf, " dst_prefixlen=%d",
  2301. sel->prefixlen_d);
  2302. break;
  2303. }
  2304. }
  2305. void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
  2306. uid_t auid, u32 sessionid, u32 secid)
  2307. {
  2308. struct audit_buffer *audit_buf;
  2309. audit_buf = xfrm_audit_start("SPD-add");
  2310. if (audit_buf == NULL)
  2311. return;
  2312. xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
  2313. audit_log_format(audit_buf, " res=%u", result);
  2314. xfrm_audit_common_policyinfo(xp, audit_buf);
  2315. audit_log_end(audit_buf);
  2316. }
  2317. EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
  2318. void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
  2319. uid_t auid, u32 sessionid, u32 secid)
  2320. {
  2321. struct audit_buffer *audit_buf;
  2322. audit_buf = xfrm_audit_start("SPD-delete");
  2323. if (audit_buf == NULL)
  2324. return;
  2325. xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
  2326. audit_log_format(audit_buf, " res=%u", result);
  2327. xfrm_audit_common_policyinfo(xp, audit_buf);
  2328. audit_log_end(audit_buf);
  2329. }
  2330. EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
  2331. #endif
  2332. #ifdef CONFIG_XFRM_MIGRATE
  2333. static int xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
  2334. const struct xfrm_selector *sel_tgt)
  2335. {
  2336. if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
  2337. if (sel_tgt->family == sel_cmp->family &&
  2338. xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
  2339. sel_cmp->family) == 0 &&
  2340. xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
  2341. sel_cmp->family) == 0 &&
  2342. sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
  2343. sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
  2344. return 1;
  2345. }
  2346. } else {
  2347. if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
  2348. return 1;
  2349. }
  2350. }
  2351. return 0;
  2352. }
  2353. static struct xfrm_policy * xfrm_migrate_policy_find(const struct xfrm_selector *sel,
  2354. u8 dir, u8 type)
  2355. {
  2356. struct xfrm_policy *pol, *ret = NULL;
  2357. struct hlist_node *entry;
  2358. struct hlist_head *chain;
  2359. u32 priority = ~0U;
  2360. read_lock_bh(&xfrm_policy_lock);
  2361. chain = policy_hash_direct(&init_net, &sel->daddr, &sel->saddr, sel->family, dir);
  2362. hlist_for_each_entry(pol, entry, chain, bydst) {
  2363. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  2364. pol->type == type) {
  2365. ret = pol;
  2366. priority = ret->priority;
  2367. break;
  2368. }
  2369. }
  2370. chain = &init_net.xfrm.policy_inexact[dir];
  2371. hlist_for_each_entry(pol, entry, chain, bydst) {
  2372. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  2373. pol->type == type &&
  2374. pol->priority < priority) {
  2375. ret = pol;
  2376. break;
  2377. }
  2378. }
  2379. if (ret)
  2380. xfrm_pol_hold(ret);
  2381. read_unlock_bh(&xfrm_policy_lock);
  2382. return ret;
  2383. }
  2384. static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
  2385. {
  2386. int match = 0;
  2387. if (t->mode == m->mode && t->id.proto == m->proto &&
  2388. (m->reqid == 0 || t->reqid == m->reqid)) {
  2389. switch (t->mode) {
  2390. case XFRM_MODE_TUNNEL:
  2391. case XFRM_MODE_BEET:
  2392. if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
  2393. m->old_family) == 0 &&
  2394. xfrm_addr_cmp(&t->saddr, &m->old_saddr,
  2395. m->old_family) == 0) {
  2396. match = 1;
  2397. }
  2398. break;
  2399. case XFRM_MODE_TRANSPORT:
  2400. /* in case of transport mode, template does not store
  2401. any IP addresses, hence we just compare mode and
  2402. protocol */
  2403. match = 1;
  2404. break;
  2405. default:
  2406. break;
  2407. }
  2408. }
  2409. return match;
  2410. }
  2411. /* update endpoint address(es) of template(s) */
  2412. static int xfrm_policy_migrate(struct xfrm_policy *pol,
  2413. struct xfrm_migrate *m, int num_migrate)
  2414. {
  2415. struct xfrm_migrate *mp;
  2416. int i, j, n = 0;
  2417. write_lock_bh(&pol->lock);
  2418. if (unlikely(pol->walk.dead)) {
  2419. /* target policy has been deleted */
  2420. write_unlock_bh(&pol->lock);
  2421. return -ENOENT;
  2422. }
  2423. for (i = 0; i < pol->xfrm_nr; i++) {
  2424. for (j = 0, mp = m; j < num_migrate; j++, mp++) {
  2425. if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
  2426. continue;
  2427. n++;
  2428. if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
  2429. pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
  2430. continue;
  2431. /* update endpoints */
  2432. memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
  2433. sizeof(pol->xfrm_vec[i].id.daddr));
  2434. memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
  2435. sizeof(pol->xfrm_vec[i].saddr));
  2436. pol->xfrm_vec[i].encap_family = mp->new_family;
  2437. /* flush bundles */
  2438. atomic_inc(&pol->genid);
  2439. }
  2440. }
  2441. write_unlock_bh(&pol->lock);
  2442. if (!n)
  2443. return -ENODATA;
  2444. return 0;
  2445. }
  2446. static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
  2447. {
  2448. int i, j;
  2449. if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
  2450. return -EINVAL;
  2451. for (i = 0; i < num_migrate; i++) {
  2452. if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
  2453. m[i].old_family) == 0) &&
  2454. (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
  2455. m[i].old_family) == 0))
  2456. return -EINVAL;
  2457. if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
  2458. xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
  2459. return -EINVAL;
  2460. /* check if there is any duplicated entry */
  2461. for (j = i + 1; j < num_migrate; j++) {
  2462. if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
  2463. sizeof(m[i].old_daddr)) &&
  2464. !memcmp(&m[i].old_saddr, &m[j].old_saddr,
  2465. sizeof(m[i].old_saddr)) &&
  2466. m[i].proto == m[j].proto &&
  2467. m[i].mode == m[j].mode &&
  2468. m[i].reqid == m[j].reqid &&
  2469. m[i].old_family == m[j].old_family)
  2470. return -EINVAL;
  2471. }
  2472. }
  2473. return 0;
  2474. }
  2475. int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  2476. struct xfrm_migrate *m, int num_migrate,
  2477. struct xfrm_kmaddress *k)
  2478. {
  2479. int i, err, nx_cur = 0, nx_new = 0;
  2480. struct xfrm_policy *pol = NULL;
  2481. struct xfrm_state *x, *xc;
  2482. struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
  2483. struct xfrm_state *x_new[XFRM_MAX_DEPTH];
  2484. struct xfrm_migrate *mp;
  2485. if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
  2486. goto out;
  2487. /* Stage 1 - find policy */
  2488. if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
  2489. err = -ENOENT;
  2490. goto out;
  2491. }
  2492. /* Stage 2 - find and update state(s) */
  2493. for (i = 0, mp = m; i < num_migrate; i++, mp++) {
  2494. if ((x = xfrm_migrate_state_find(mp))) {
  2495. x_cur[nx_cur] = x;
  2496. nx_cur++;
  2497. if ((xc = xfrm_state_migrate(x, mp))) {
  2498. x_new[nx_new] = xc;
  2499. nx_new++;
  2500. } else {
  2501. err = -ENODATA;
  2502. goto restore_state;
  2503. }
  2504. }
  2505. }
  2506. /* Stage 3 - update policy */
  2507. if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
  2508. goto restore_state;
  2509. /* Stage 4 - delete old state(s) */
  2510. if (nx_cur) {
  2511. xfrm_states_put(x_cur, nx_cur);
  2512. xfrm_states_delete(x_cur, nx_cur);
  2513. }
  2514. /* Stage 5 - announce */
  2515. km_migrate(sel, dir, type, m, num_migrate, k);
  2516. xfrm_pol_put(pol);
  2517. return 0;
  2518. out:
  2519. return err;
  2520. restore_state:
  2521. if (pol)
  2522. xfrm_pol_put(pol);
  2523. if (nx_cur)
  2524. xfrm_states_put(x_cur, nx_cur);
  2525. if (nx_new)
  2526. xfrm_states_delete(x_new, nx_new);
  2527. return err;
  2528. }
  2529. EXPORT_SYMBOL(xfrm_migrate);
  2530. #endif