xfrm_policy.c 70 KB

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