xfrm_policy.c 68 KB

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