xfrm_policy.c 71 KB

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