xfrm_policy.c 57 KB

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