xfrm_policy.c 70 KB

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