xfrm_policy.c 61 KB

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