xfrm_policy.c 70 KB

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