xfrm_policy.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586
  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. void xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
  698. {
  699. int dir;
  700. write_lock_bh(&xfrm_policy_lock);
  701. for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
  702. struct xfrm_policy *pol;
  703. struct hlist_node *entry;
  704. int i, killed;
  705. killed = 0;
  706. again1:
  707. hlist_for_each_entry(pol, entry,
  708. &xfrm_policy_inexact[dir], bydst) {
  709. if (pol->type != type)
  710. continue;
  711. hlist_del(&pol->bydst);
  712. hlist_del(&pol->byidx);
  713. write_unlock_bh(&xfrm_policy_lock);
  714. xfrm_audit_log(audit_info->loginuid, audit_info->secid,
  715. AUDIT_MAC_IPSEC_DELSPD, 1, pol, NULL);
  716. xfrm_policy_kill(pol);
  717. killed++;
  718. write_lock_bh(&xfrm_policy_lock);
  719. goto again1;
  720. }
  721. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  722. again2:
  723. hlist_for_each_entry(pol, entry,
  724. xfrm_policy_bydst[dir].table + i,
  725. bydst) {
  726. if (pol->type != type)
  727. continue;
  728. hlist_del(&pol->bydst);
  729. hlist_del(&pol->byidx);
  730. write_unlock_bh(&xfrm_policy_lock);
  731. xfrm_audit_log(audit_info->loginuid,
  732. audit_info->secid,
  733. AUDIT_MAC_IPSEC_DELSPD, 1,
  734. pol, NULL);
  735. xfrm_policy_kill(pol);
  736. killed++;
  737. write_lock_bh(&xfrm_policy_lock);
  738. goto again2;
  739. }
  740. }
  741. xfrm_policy_count[dir] -= killed;
  742. }
  743. atomic_inc(&flow_cache_genid);
  744. write_unlock_bh(&xfrm_policy_lock);
  745. }
  746. EXPORT_SYMBOL(xfrm_policy_flush);
  747. int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
  748. void *data)
  749. {
  750. struct xfrm_policy *pol, *last = NULL;
  751. struct hlist_node *entry;
  752. int dir, last_dir = 0, count, error;
  753. read_lock_bh(&xfrm_policy_lock);
  754. count = 0;
  755. for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
  756. struct hlist_head *table = xfrm_policy_bydst[dir].table;
  757. int i;
  758. hlist_for_each_entry(pol, entry,
  759. &xfrm_policy_inexact[dir], bydst) {
  760. if (pol->type != type)
  761. continue;
  762. if (last) {
  763. error = func(last, last_dir % XFRM_POLICY_MAX,
  764. count, data);
  765. if (error)
  766. goto out;
  767. }
  768. last = pol;
  769. last_dir = dir;
  770. count++;
  771. }
  772. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  773. hlist_for_each_entry(pol, entry, table + i, bydst) {
  774. if (pol->type != type)
  775. continue;
  776. if (last) {
  777. error = func(last, last_dir % XFRM_POLICY_MAX,
  778. count, data);
  779. if (error)
  780. goto out;
  781. }
  782. last = pol;
  783. last_dir = dir;
  784. count++;
  785. }
  786. }
  787. }
  788. if (count == 0) {
  789. error = -ENOENT;
  790. goto out;
  791. }
  792. error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
  793. out:
  794. read_unlock_bh(&xfrm_policy_lock);
  795. return error;
  796. }
  797. EXPORT_SYMBOL(xfrm_policy_walk);
  798. /*
  799. * Find policy to apply to this flow.
  800. *
  801. * Returns 0 if policy found, else an -errno.
  802. */
  803. static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
  804. u8 type, u16 family, int dir)
  805. {
  806. struct xfrm_selector *sel = &pol->selector;
  807. int match, ret = -ESRCH;
  808. if (pol->family != family ||
  809. pol->type != type)
  810. return ret;
  811. match = xfrm_selector_match(sel, fl, family);
  812. if (match)
  813. ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
  814. return ret;
  815. }
  816. static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
  817. u16 family, u8 dir)
  818. {
  819. int err;
  820. struct xfrm_policy *pol, *ret;
  821. xfrm_address_t *daddr, *saddr;
  822. struct hlist_node *entry;
  823. struct hlist_head *chain;
  824. u32 priority = ~0U;
  825. daddr = xfrm_flowi_daddr(fl, family);
  826. saddr = xfrm_flowi_saddr(fl, family);
  827. if (unlikely(!daddr || !saddr))
  828. return NULL;
  829. read_lock_bh(&xfrm_policy_lock);
  830. chain = policy_hash_direct(daddr, saddr, family, dir);
  831. ret = NULL;
  832. hlist_for_each_entry(pol, entry, chain, bydst) {
  833. err = xfrm_policy_match(pol, fl, type, family, dir);
  834. if (err) {
  835. if (err == -ESRCH)
  836. continue;
  837. else {
  838. ret = ERR_PTR(err);
  839. goto fail;
  840. }
  841. } else {
  842. ret = pol;
  843. priority = ret->priority;
  844. break;
  845. }
  846. }
  847. chain = &xfrm_policy_inexact[dir];
  848. hlist_for_each_entry(pol, entry, chain, bydst) {
  849. err = xfrm_policy_match(pol, fl, type, family, dir);
  850. if (err) {
  851. if (err == -ESRCH)
  852. continue;
  853. else {
  854. ret = ERR_PTR(err);
  855. goto fail;
  856. }
  857. } else if (pol->priority < priority) {
  858. ret = pol;
  859. break;
  860. }
  861. }
  862. if (ret)
  863. xfrm_pol_hold(ret);
  864. fail:
  865. read_unlock_bh(&xfrm_policy_lock);
  866. return ret;
  867. }
  868. static int xfrm_policy_lookup(struct flowi *fl, u16 family, u8 dir,
  869. void **objp, atomic_t **obj_refp)
  870. {
  871. struct xfrm_policy *pol;
  872. int err = 0;
  873. #ifdef CONFIG_XFRM_SUB_POLICY
  874. pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_SUB, fl, family, dir);
  875. if (IS_ERR(pol)) {
  876. err = PTR_ERR(pol);
  877. pol = NULL;
  878. }
  879. if (pol || err)
  880. goto end;
  881. #endif
  882. pol = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN, fl, family, dir);
  883. if (IS_ERR(pol)) {
  884. err = PTR_ERR(pol);
  885. pol = NULL;
  886. }
  887. #ifdef CONFIG_XFRM_SUB_POLICY
  888. end:
  889. #endif
  890. if ((*objp = (void *) pol) != NULL)
  891. *obj_refp = &pol->refcnt;
  892. return err;
  893. }
  894. static inline int policy_to_flow_dir(int dir)
  895. {
  896. if (XFRM_POLICY_IN == FLOW_DIR_IN &&
  897. XFRM_POLICY_OUT == FLOW_DIR_OUT &&
  898. XFRM_POLICY_FWD == FLOW_DIR_FWD)
  899. return dir;
  900. switch (dir) {
  901. default:
  902. case XFRM_POLICY_IN:
  903. return FLOW_DIR_IN;
  904. case XFRM_POLICY_OUT:
  905. return FLOW_DIR_OUT;
  906. case XFRM_POLICY_FWD:
  907. return FLOW_DIR_FWD;
  908. }
  909. }
  910. static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struct flowi *fl)
  911. {
  912. struct xfrm_policy *pol;
  913. read_lock_bh(&xfrm_policy_lock);
  914. if ((pol = sk->sk_policy[dir]) != NULL) {
  915. int match = xfrm_selector_match(&pol->selector, fl,
  916. sk->sk_family);
  917. int err = 0;
  918. if (match) {
  919. err = security_xfrm_policy_lookup(pol, fl->secid,
  920. policy_to_flow_dir(dir));
  921. if (!err)
  922. xfrm_pol_hold(pol);
  923. else if (err == -ESRCH)
  924. pol = NULL;
  925. else
  926. pol = ERR_PTR(err);
  927. } else
  928. pol = NULL;
  929. }
  930. read_unlock_bh(&xfrm_policy_lock);
  931. return pol;
  932. }
  933. static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
  934. {
  935. struct hlist_head *chain = policy_hash_bysel(&pol->selector,
  936. pol->family, dir);
  937. hlist_add_head(&pol->bydst, chain);
  938. hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
  939. xfrm_policy_count[dir]++;
  940. xfrm_pol_hold(pol);
  941. if (xfrm_bydst_should_resize(dir, NULL))
  942. schedule_work(&xfrm_hash_work);
  943. }
  944. static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
  945. int dir)
  946. {
  947. if (hlist_unhashed(&pol->bydst))
  948. return NULL;
  949. hlist_del(&pol->bydst);
  950. hlist_del(&pol->byidx);
  951. xfrm_policy_count[dir]--;
  952. return pol;
  953. }
  954. int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
  955. {
  956. write_lock_bh(&xfrm_policy_lock);
  957. pol = __xfrm_policy_unlink(pol, dir);
  958. write_unlock_bh(&xfrm_policy_lock);
  959. if (pol) {
  960. if (dir < XFRM_POLICY_MAX)
  961. atomic_inc(&flow_cache_genid);
  962. xfrm_policy_kill(pol);
  963. return 0;
  964. }
  965. return -ENOENT;
  966. }
  967. EXPORT_SYMBOL(xfrm_policy_delete);
  968. int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
  969. {
  970. struct xfrm_policy *old_pol;
  971. #ifdef CONFIG_XFRM_SUB_POLICY
  972. if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
  973. return -EINVAL;
  974. #endif
  975. write_lock_bh(&xfrm_policy_lock);
  976. old_pol = sk->sk_policy[dir];
  977. sk->sk_policy[dir] = pol;
  978. if (pol) {
  979. pol->curlft.add_time = get_seconds();
  980. pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
  981. __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
  982. }
  983. if (old_pol)
  984. __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
  985. write_unlock_bh(&xfrm_policy_lock);
  986. if (old_pol) {
  987. xfrm_policy_kill(old_pol);
  988. }
  989. return 0;
  990. }
  991. static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
  992. {
  993. struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
  994. if (newp) {
  995. newp->selector = old->selector;
  996. if (security_xfrm_policy_clone(old, newp)) {
  997. kfree(newp);
  998. return NULL; /* ENOMEM */
  999. }
  1000. newp->lft = old->lft;
  1001. newp->curlft = old->curlft;
  1002. newp->action = old->action;
  1003. newp->flags = old->flags;
  1004. newp->xfrm_nr = old->xfrm_nr;
  1005. newp->index = old->index;
  1006. newp->type = old->type;
  1007. memcpy(newp->xfrm_vec, old->xfrm_vec,
  1008. newp->xfrm_nr*sizeof(struct xfrm_tmpl));
  1009. write_lock_bh(&xfrm_policy_lock);
  1010. __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
  1011. write_unlock_bh(&xfrm_policy_lock);
  1012. xfrm_pol_put(newp);
  1013. }
  1014. return newp;
  1015. }
  1016. int __xfrm_sk_clone_policy(struct sock *sk)
  1017. {
  1018. struct xfrm_policy *p0 = sk->sk_policy[0],
  1019. *p1 = sk->sk_policy[1];
  1020. sk->sk_policy[0] = sk->sk_policy[1] = NULL;
  1021. if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
  1022. return -ENOMEM;
  1023. if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
  1024. return -ENOMEM;
  1025. return 0;
  1026. }
  1027. static int
  1028. xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
  1029. unsigned short family)
  1030. {
  1031. int err;
  1032. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1033. if (unlikely(afinfo == NULL))
  1034. return -EINVAL;
  1035. err = afinfo->get_saddr(local, remote);
  1036. xfrm_policy_put_afinfo(afinfo);
  1037. return err;
  1038. }
  1039. /* Resolve list of templates for the flow, given policy. */
  1040. static int
  1041. xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
  1042. struct xfrm_state **xfrm,
  1043. unsigned short family)
  1044. {
  1045. int nx;
  1046. int i, error;
  1047. xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
  1048. xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
  1049. xfrm_address_t tmp;
  1050. for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
  1051. struct xfrm_state *x;
  1052. xfrm_address_t *remote = daddr;
  1053. xfrm_address_t *local = saddr;
  1054. struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
  1055. if (tmpl->mode == XFRM_MODE_TUNNEL) {
  1056. remote = &tmpl->id.daddr;
  1057. local = &tmpl->saddr;
  1058. family = tmpl->encap_family;
  1059. if (xfrm_addr_any(local, family)) {
  1060. error = xfrm_get_saddr(&tmp, remote, family);
  1061. if (error)
  1062. goto fail;
  1063. local = &tmp;
  1064. }
  1065. }
  1066. x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
  1067. if (x && x->km.state == XFRM_STATE_VALID) {
  1068. xfrm[nx++] = x;
  1069. daddr = remote;
  1070. saddr = local;
  1071. continue;
  1072. }
  1073. if (x) {
  1074. error = (x->km.state == XFRM_STATE_ERROR ?
  1075. -EINVAL : -EAGAIN);
  1076. xfrm_state_put(x);
  1077. }
  1078. if (!tmpl->optional)
  1079. goto fail;
  1080. }
  1081. return nx;
  1082. fail:
  1083. for (nx--; nx>=0; nx--)
  1084. xfrm_state_put(xfrm[nx]);
  1085. return error;
  1086. }
  1087. static int
  1088. xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
  1089. struct xfrm_state **xfrm,
  1090. unsigned short family)
  1091. {
  1092. struct xfrm_state *tp[XFRM_MAX_DEPTH];
  1093. struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
  1094. int cnx = 0;
  1095. int error;
  1096. int ret;
  1097. int i;
  1098. for (i = 0; i < npols; i++) {
  1099. if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
  1100. error = -ENOBUFS;
  1101. goto fail;
  1102. }
  1103. ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
  1104. if (ret < 0) {
  1105. error = ret;
  1106. goto fail;
  1107. } else
  1108. cnx += ret;
  1109. }
  1110. /* found states are sorted for outbound processing */
  1111. if (npols > 1)
  1112. xfrm_state_sort(xfrm, tpp, cnx, family);
  1113. return cnx;
  1114. fail:
  1115. for (cnx--; cnx>=0; cnx--)
  1116. xfrm_state_put(tpp[cnx]);
  1117. return error;
  1118. }
  1119. /* Check that the bundle accepts the flow and its components are
  1120. * still valid.
  1121. */
  1122. static struct dst_entry *
  1123. xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
  1124. {
  1125. struct dst_entry *x;
  1126. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1127. if (unlikely(afinfo == NULL))
  1128. return ERR_PTR(-EINVAL);
  1129. x = afinfo->find_bundle(fl, policy);
  1130. xfrm_policy_put_afinfo(afinfo);
  1131. return x;
  1132. }
  1133. /* Allocate chain of dst_entry's, attach known xfrm's, calculate
  1134. * all the metrics... Shortly, bundle a bundle.
  1135. */
  1136. static int
  1137. xfrm_bundle_create(struct xfrm_policy *policy, struct xfrm_state **xfrm, int nx,
  1138. struct flowi *fl, struct dst_entry **dst_p,
  1139. unsigned short family)
  1140. {
  1141. int err;
  1142. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1143. if (unlikely(afinfo == NULL))
  1144. return -EINVAL;
  1145. err = afinfo->bundle_create(policy, xfrm, nx, fl, dst_p);
  1146. xfrm_policy_put_afinfo(afinfo);
  1147. return err;
  1148. }
  1149. static int inline
  1150. xfrm_dst_alloc_copy(void **target, void *src, int size)
  1151. {
  1152. if (!*target) {
  1153. *target = kmalloc(size, GFP_ATOMIC);
  1154. if (!*target)
  1155. return -ENOMEM;
  1156. }
  1157. memcpy(*target, src, size);
  1158. return 0;
  1159. }
  1160. static int inline
  1161. xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
  1162. {
  1163. #ifdef CONFIG_XFRM_SUB_POLICY
  1164. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1165. return xfrm_dst_alloc_copy((void **)&(xdst->partner),
  1166. sel, sizeof(*sel));
  1167. #else
  1168. return 0;
  1169. #endif
  1170. }
  1171. static int inline
  1172. xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
  1173. {
  1174. #ifdef CONFIG_XFRM_SUB_POLICY
  1175. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1176. return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
  1177. #else
  1178. return 0;
  1179. #endif
  1180. }
  1181. static int stale_bundle(struct dst_entry *dst);
  1182. /* Main function: finds/creates a bundle for given flow.
  1183. *
  1184. * At the moment we eat a raw IP route. Mostly to speed up lookups
  1185. * on interfaces with disabled IPsec.
  1186. */
  1187. int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1188. struct sock *sk, int flags)
  1189. {
  1190. struct xfrm_policy *policy;
  1191. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1192. int npols;
  1193. int pol_dead;
  1194. int xfrm_nr;
  1195. int pi;
  1196. struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
  1197. struct dst_entry *dst, *dst_orig = *dst_p;
  1198. int nx = 0;
  1199. int err;
  1200. u32 genid;
  1201. u16 family;
  1202. u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
  1203. restart:
  1204. genid = atomic_read(&flow_cache_genid);
  1205. policy = NULL;
  1206. for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
  1207. pols[pi] = NULL;
  1208. npols = 0;
  1209. pol_dead = 0;
  1210. xfrm_nr = 0;
  1211. if (sk && sk->sk_policy[1]) {
  1212. policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
  1213. if (IS_ERR(policy))
  1214. return PTR_ERR(policy);
  1215. }
  1216. if (!policy) {
  1217. /* To accelerate a bit... */
  1218. if ((dst_orig->flags & DST_NOXFRM) ||
  1219. !xfrm_policy_count[XFRM_POLICY_OUT])
  1220. return 0;
  1221. policy = flow_cache_lookup(fl, dst_orig->ops->family,
  1222. dir, xfrm_policy_lookup);
  1223. if (IS_ERR(policy))
  1224. return PTR_ERR(policy);
  1225. }
  1226. if (!policy)
  1227. return 0;
  1228. family = dst_orig->ops->family;
  1229. policy->curlft.use_time = get_seconds();
  1230. pols[0] = policy;
  1231. npols ++;
  1232. xfrm_nr += pols[0]->xfrm_nr;
  1233. switch (policy->action) {
  1234. case XFRM_POLICY_BLOCK:
  1235. /* Prohibit the flow */
  1236. err = -EPERM;
  1237. goto error;
  1238. case XFRM_POLICY_ALLOW:
  1239. #ifndef CONFIG_XFRM_SUB_POLICY
  1240. if (policy->xfrm_nr == 0) {
  1241. /* Flow passes not transformed. */
  1242. xfrm_pol_put(policy);
  1243. return 0;
  1244. }
  1245. #endif
  1246. /* Try to find matching bundle.
  1247. *
  1248. * LATER: help from flow cache. It is optional, this
  1249. * is required only for output policy.
  1250. */
  1251. dst = xfrm_find_bundle(fl, policy, family);
  1252. if (IS_ERR(dst)) {
  1253. err = PTR_ERR(dst);
  1254. goto error;
  1255. }
  1256. if (dst)
  1257. break;
  1258. #ifdef CONFIG_XFRM_SUB_POLICY
  1259. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1260. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1261. fl, family,
  1262. XFRM_POLICY_OUT);
  1263. if (pols[1]) {
  1264. if (IS_ERR(pols[1])) {
  1265. err = PTR_ERR(pols[1]);
  1266. goto error;
  1267. }
  1268. if (pols[1]->action == XFRM_POLICY_BLOCK) {
  1269. err = -EPERM;
  1270. goto error;
  1271. }
  1272. npols ++;
  1273. xfrm_nr += pols[1]->xfrm_nr;
  1274. }
  1275. }
  1276. /*
  1277. * Because neither flowi nor bundle information knows about
  1278. * transformation template size. On more than one policy usage
  1279. * we can realize whether all of them is bypass or not after
  1280. * they are searched. See above not-transformed bypass
  1281. * is surrounded by non-sub policy configuration, too.
  1282. */
  1283. if (xfrm_nr == 0) {
  1284. /* Flow passes not transformed. */
  1285. xfrm_pols_put(pols, npols);
  1286. return 0;
  1287. }
  1288. #endif
  1289. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1290. if (unlikely(nx<0)) {
  1291. err = nx;
  1292. if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
  1293. /* EREMOTE tells the caller to generate
  1294. * a one-shot blackhole route.
  1295. */
  1296. xfrm_pol_put(policy);
  1297. return -EREMOTE;
  1298. }
  1299. if (err == -EAGAIN && flags) {
  1300. DECLARE_WAITQUEUE(wait, current);
  1301. add_wait_queue(&km_waitq, &wait);
  1302. set_current_state(TASK_INTERRUPTIBLE);
  1303. schedule();
  1304. set_current_state(TASK_RUNNING);
  1305. remove_wait_queue(&km_waitq, &wait);
  1306. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1307. if (nx == -EAGAIN && signal_pending(current)) {
  1308. err = -ERESTART;
  1309. goto error;
  1310. }
  1311. if (nx == -EAGAIN ||
  1312. genid != atomic_read(&flow_cache_genid)) {
  1313. xfrm_pols_put(pols, npols);
  1314. goto restart;
  1315. }
  1316. err = nx;
  1317. }
  1318. if (err < 0)
  1319. goto error;
  1320. }
  1321. if (nx == 0) {
  1322. /* Flow passes not transformed. */
  1323. xfrm_pols_put(pols, npols);
  1324. return 0;
  1325. }
  1326. dst = dst_orig;
  1327. err = xfrm_bundle_create(policy, xfrm, nx, fl, &dst, family);
  1328. if (unlikely(err)) {
  1329. int i;
  1330. for (i=0; i<nx; i++)
  1331. xfrm_state_put(xfrm[i]);
  1332. goto error;
  1333. }
  1334. for (pi = 0; pi < npols; pi++) {
  1335. read_lock_bh(&pols[pi]->lock);
  1336. pol_dead |= pols[pi]->dead;
  1337. read_unlock_bh(&pols[pi]->lock);
  1338. }
  1339. write_lock_bh(&policy->lock);
  1340. if (unlikely(pol_dead || stale_bundle(dst))) {
  1341. /* Wow! While we worked on resolving, this
  1342. * policy has gone. Retry. It is not paranoia,
  1343. * we just cannot enlist new bundle to dead object.
  1344. * We can't enlist stable bundles either.
  1345. */
  1346. write_unlock_bh(&policy->lock);
  1347. if (dst)
  1348. dst_free(dst);
  1349. err = -EHOSTUNREACH;
  1350. goto error;
  1351. }
  1352. if (npols > 1)
  1353. err = xfrm_dst_update_parent(dst, &pols[1]->selector);
  1354. else
  1355. err = xfrm_dst_update_origin(dst, fl);
  1356. if (unlikely(err)) {
  1357. write_unlock_bh(&policy->lock);
  1358. if (dst)
  1359. dst_free(dst);
  1360. goto error;
  1361. }
  1362. dst->next = policy->bundles;
  1363. policy->bundles = dst;
  1364. dst_hold(dst);
  1365. write_unlock_bh(&policy->lock);
  1366. }
  1367. *dst_p = dst;
  1368. dst_release(dst_orig);
  1369. xfrm_pols_put(pols, npols);
  1370. return 0;
  1371. error:
  1372. dst_release(dst_orig);
  1373. xfrm_pols_put(pols, npols);
  1374. *dst_p = NULL;
  1375. return err;
  1376. }
  1377. EXPORT_SYMBOL(__xfrm_lookup);
  1378. int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1379. struct sock *sk, int flags)
  1380. {
  1381. int err = __xfrm_lookup(dst_p, fl, sk, flags);
  1382. if (err == -EREMOTE) {
  1383. dst_release(*dst_p);
  1384. *dst_p = NULL;
  1385. err = -EAGAIN;
  1386. }
  1387. return err;
  1388. }
  1389. EXPORT_SYMBOL(xfrm_lookup);
  1390. static inline int
  1391. xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
  1392. {
  1393. struct xfrm_state *x;
  1394. int err;
  1395. if (!skb->sp || idx < 0 || idx >= skb->sp->len)
  1396. return 0;
  1397. x = skb->sp->xvec[idx];
  1398. if (!x->type->reject)
  1399. return 0;
  1400. xfrm_state_hold(x);
  1401. err = x->type->reject(x, skb, fl);
  1402. xfrm_state_put(x);
  1403. return err;
  1404. }
  1405. /* When skb is transformed back to its "native" form, we have to
  1406. * check policy restrictions. At the moment we make this in maximally
  1407. * stupid way. Shame on me. :-) Of course, connected sockets must
  1408. * have policy cached at them.
  1409. */
  1410. static inline int
  1411. xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
  1412. unsigned short family)
  1413. {
  1414. if (xfrm_state_kern(x))
  1415. return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
  1416. return x->id.proto == tmpl->id.proto &&
  1417. (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
  1418. (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
  1419. x->props.mode == tmpl->mode &&
  1420. ((tmpl->aalgos & (1<<x->props.aalgo)) ||
  1421. !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
  1422. !(x->props.mode != XFRM_MODE_TRANSPORT &&
  1423. xfrm_state_addr_cmp(tmpl, x, family));
  1424. }
  1425. /*
  1426. * 0 or more than 0 is returned when validation is succeeded (either bypass
  1427. * because of optional transport mode, or next index of the mathced secpath
  1428. * state with the template.
  1429. * -1 is returned when no matching template is found.
  1430. * Otherwise "-2 - errored_index" is returned.
  1431. */
  1432. static inline int
  1433. xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
  1434. unsigned short family)
  1435. {
  1436. int idx = start;
  1437. if (tmpl->optional) {
  1438. if (tmpl->mode == XFRM_MODE_TRANSPORT)
  1439. return start;
  1440. } else
  1441. start = -1;
  1442. for (; idx < sp->len; idx++) {
  1443. if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
  1444. return ++idx;
  1445. if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
  1446. if (start == -1)
  1447. start = -2-idx;
  1448. break;
  1449. }
  1450. }
  1451. return start;
  1452. }
  1453. int
  1454. xfrm_decode_session(struct sk_buff *skb, struct flowi *fl, unsigned short family)
  1455. {
  1456. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1457. int err;
  1458. if (unlikely(afinfo == NULL))
  1459. return -EAFNOSUPPORT;
  1460. afinfo->decode_session(skb, fl);
  1461. err = security_xfrm_decode_session(skb, &fl->secid);
  1462. xfrm_policy_put_afinfo(afinfo);
  1463. return err;
  1464. }
  1465. EXPORT_SYMBOL(xfrm_decode_session);
  1466. static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
  1467. {
  1468. for (; k < sp->len; k++) {
  1469. if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
  1470. *idxp = k;
  1471. return 1;
  1472. }
  1473. }
  1474. return 0;
  1475. }
  1476. int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
  1477. unsigned short family)
  1478. {
  1479. struct xfrm_policy *pol;
  1480. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1481. int npols = 0;
  1482. int xfrm_nr;
  1483. int pi;
  1484. struct flowi fl;
  1485. u8 fl_dir = policy_to_flow_dir(dir);
  1486. int xerr_idx = -1;
  1487. if (xfrm_decode_session(skb, &fl, family) < 0)
  1488. return 0;
  1489. nf_nat_decode_session(skb, &fl, family);
  1490. /* First, check used SA against their selectors. */
  1491. if (skb->sp) {
  1492. int i;
  1493. for (i=skb->sp->len-1; i>=0; i--) {
  1494. struct xfrm_state *x = skb->sp->xvec[i];
  1495. if (!xfrm_selector_match(&x->sel, &fl, family))
  1496. return 0;
  1497. }
  1498. }
  1499. pol = NULL;
  1500. if (sk && sk->sk_policy[dir]) {
  1501. pol = xfrm_sk_policy_lookup(sk, dir, &fl);
  1502. if (IS_ERR(pol))
  1503. return 0;
  1504. }
  1505. if (!pol)
  1506. pol = flow_cache_lookup(&fl, family, fl_dir,
  1507. xfrm_policy_lookup);
  1508. if (IS_ERR(pol))
  1509. return 0;
  1510. if (!pol) {
  1511. if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
  1512. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1513. return 0;
  1514. }
  1515. return 1;
  1516. }
  1517. pol->curlft.use_time = get_seconds();
  1518. pols[0] = pol;
  1519. npols ++;
  1520. #ifdef CONFIG_XFRM_SUB_POLICY
  1521. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1522. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1523. &fl, family,
  1524. XFRM_POLICY_IN);
  1525. if (pols[1]) {
  1526. if (IS_ERR(pols[1]))
  1527. return 0;
  1528. pols[1]->curlft.use_time = get_seconds();
  1529. npols ++;
  1530. }
  1531. }
  1532. #endif
  1533. if (pol->action == XFRM_POLICY_ALLOW) {
  1534. struct sec_path *sp;
  1535. static struct sec_path dummy;
  1536. struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
  1537. struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
  1538. struct xfrm_tmpl **tpp = tp;
  1539. int ti = 0;
  1540. int i, k;
  1541. if ((sp = skb->sp) == NULL)
  1542. sp = &dummy;
  1543. for (pi = 0; pi < npols; pi++) {
  1544. if (pols[pi] != pol &&
  1545. pols[pi]->action != XFRM_POLICY_ALLOW)
  1546. goto reject;
  1547. if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH)
  1548. goto reject_error;
  1549. for (i = 0; i < pols[pi]->xfrm_nr; i++)
  1550. tpp[ti++] = &pols[pi]->xfrm_vec[i];
  1551. }
  1552. xfrm_nr = ti;
  1553. if (npols > 1) {
  1554. xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
  1555. tpp = stp;
  1556. }
  1557. /* For each tunnel xfrm, find the first matching tmpl.
  1558. * For each tmpl before that, find corresponding xfrm.
  1559. * Order is _important_. Later we will implement
  1560. * some barriers, but at the moment barriers
  1561. * are implied between each two transformations.
  1562. */
  1563. for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
  1564. k = xfrm_policy_ok(tpp[i], sp, k, family);
  1565. if (k < 0) {
  1566. if (k < -1)
  1567. /* "-2 - errored_index" returned */
  1568. xerr_idx = -(2+k);
  1569. goto reject;
  1570. }
  1571. }
  1572. if (secpath_has_nontransport(sp, k, &xerr_idx))
  1573. goto reject;
  1574. xfrm_pols_put(pols, npols);
  1575. return 1;
  1576. }
  1577. reject:
  1578. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1579. reject_error:
  1580. xfrm_pols_put(pols, npols);
  1581. return 0;
  1582. }
  1583. EXPORT_SYMBOL(__xfrm_policy_check);
  1584. int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
  1585. {
  1586. struct flowi fl;
  1587. if (xfrm_decode_session(skb, &fl, family) < 0)
  1588. return 0;
  1589. return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
  1590. }
  1591. EXPORT_SYMBOL(__xfrm_route_forward);
  1592. /* Optimize later using cookies and generation ids. */
  1593. static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
  1594. {
  1595. /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
  1596. * to "-1" to force all XFRM destinations to get validated by
  1597. * dst_ops->check on every use. We do this because when a
  1598. * normal route referenced by an XFRM dst is obsoleted we do
  1599. * not go looking around for all parent referencing XFRM dsts
  1600. * so that we can invalidate them. It is just too much work.
  1601. * Instead we make the checks here on every use. For example:
  1602. *
  1603. * XFRM dst A --> IPv4 dst X
  1604. *
  1605. * X is the "xdst->route" of A (X is also the "dst->path" of A
  1606. * in this example). If X is marked obsolete, "A" will not
  1607. * notice. That's what we are validating here via the
  1608. * stale_bundle() check.
  1609. *
  1610. * When a policy's bundle is pruned, we dst_free() the XFRM
  1611. * dst which causes it's ->obsolete field to be set to a
  1612. * positive non-zero integer. If an XFRM dst has been pruned
  1613. * like this, we want to force a new route lookup.
  1614. */
  1615. if (dst->obsolete < 0 && !stale_bundle(dst))
  1616. return dst;
  1617. return NULL;
  1618. }
  1619. static int stale_bundle(struct dst_entry *dst)
  1620. {
  1621. return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
  1622. }
  1623. void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
  1624. {
  1625. while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
  1626. dst->dev = &loopback_dev;
  1627. dev_hold(&loopback_dev);
  1628. dev_put(dev);
  1629. }
  1630. }
  1631. EXPORT_SYMBOL(xfrm_dst_ifdown);
  1632. static void xfrm_link_failure(struct sk_buff *skb)
  1633. {
  1634. /* Impossible. Such dst must be popped before reaches point of failure. */
  1635. return;
  1636. }
  1637. static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
  1638. {
  1639. if (dst) {
  1640. if (dst->obsolete) {
  1641. dst_release(dst);
  1642. dst = NULL;
  1643. }
  1644. }
  1645. return dst;
  1646. }
  1647. static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
  1648. {
  1649. struct dst_entry *dst, **dstp;
  1650. write_lock(&pol->lock);
  1651. dstp = &pol->bundles;
  1652. while ((dst=*dstp) != NULL) {
  1653. if (func(dst)) {
  1654. *dstp = dst->next;
  1655. dst->next = *gc_list_p;
  1656. *gc_list_p = dst;
  1657. } else {
  1658. dstp = &dst->next;
  1659. }
  1660. }
  1661. write_unlock(&pol->lock);
  1662. }
  1663. static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
  1664. {
  1665. struct dst_entry *gc_list = NULL;
  1666. int dir;
  1667. read_lock_bh(&xfrm_policy_lock);
  1668. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1669. struct xfrm_policy *pol;
  1670. struct hlist_node *entry;
  1671. struct hlist_head *table;
  1672. int i;
  1673. hlist_for_each_entry(pol, entry,
  1674. &xfrm_policy_inexact[dir], bydst)
  1675. prune_one_bundle(pol, func, &gc_list);
  1676. table = xfrm_policy_bydst[dir].table;
  1677. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  1678. hlist_for_each_entry(pol, entry, table + i, bydst)
  1679. prune_one_bundle(pol, func, &gc_list);
  1680. }
  1681. }
  1682. read_unlock_bh(&xfrm_policy_lock);
  1683. while (gc_list) {
  1684. struct dst_entry *dst = gc_list;
  1685. gc_list = dst->next;
  1686. dst_free(dst);
  1687. }
  1688. }
  1689. static int unused_bundle(struct dst_entry *dst)
  1690. {
  1691. return !atomic_read(&dst->__refcnt);
  1692. }
  1693. static void __xfrm_garbage_collect(void)
  1694. {
  1695. xfrm_prune_bundles(unused_bundle);
  1696. }
  1697. static int xfrm_flush_bundles(void)
  1698. {
  1699. xfrm_prune_bundles(stale_bundle);
  1700. return 0;
  1701. }
  1702. void xfrm_init_pmtu(struct dst_entry *dst)
  1703. {
  1704. do {
  1705. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1706. u32 pmtu, route_mtu_cached;
  1707. pmtu = dst_mtu(dst->child);
  1708. xdst->child_mtu_cached = pmtu;
  1709. pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
  1710. route_mtu_cached = dst_mtu(xdst->route);
  1711. xdst->route_mtu_cached = route_mtu_cached;
  1712. if (pmtu > route_mtu_cached)
  1713. pmtu = route_mtu_cached;
  1714. dst->metrics[RTAX_MTU-1] = pmtu;
  1715. } while ((dst = dst->next));
  1716. }
  1717. EXPORT_SYMBOL(xfrm_init_pmtu);
  1718. /* Check that the bundle accepts the flow and its components are
  1719. * still valid.
  1720. */
  1721. int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
  1722. struct flowi *fl, int family, int strict)
  1723. {
  1724. struct dst_entry *dst = &first->u.dst;
  1725. struct xfrm_dst *last;
  1726. u32 mtu;
  1727. if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
  1728. (dst->dev && !netif_running(dst->dev)))
  1729. return 0;
  1730. #ifdef CONFIG_XFRM_SUB_POLICY
  1731. if (fl) {
  1732. if (first->origin && !flow_cache_uli_match(first->origin, fl))
  1733. return 0;
  1734. if (first->partner &&
  1735. !xfrm_selector_match(first->partner, fl, family))
  1736. return 0;
  1737. }
  1738. #endif
  1739. last = NULL;
  1740. do {
  1741. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1742. if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
  1743. return 0;
  1744. if (fl && pol &&
  1745. !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
  1746. return 0;
  1747. if (dst->xfrm->km.state != XFRM_STATE_VALID)
  1748. return 0;
  1749. if (xdst->genid != dst->xfrm->genid)
  1750. return 0;
  1751. if (strict && fl && dst->xfrm->props.mode != XFRM_MODE_TUNNEL &&
  1752. !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
  1753. return 0;
  1754. mtu = dst_mtu(dst->child);
  1755. if (xdst->child_mtu_cached != mtu) {
  1756. last = xdst;
  1757. xdst->child_mtu_cached = mtu;
  1758. }
  1759. if (!dst_check(xdst->route, xdst->route_cookie))
  1760. return 0;
  1761. mtu = dst_mtu(xdst->route);
  1762. if (xdst->route_mtu_cached != mtu) {
  1763. last = xdst;
  1764. xdst->route_mtu_cached = mtu;
  1765. }
  1766. dst = dst->child;
  1767. } while (dst->xfrm);
  1768. if (likely(!last))
  1769. return 1;
  1770. mtu = last->child_mtu_cached;
  1771. for (;;) {
  1772. dst = &last->u.dst;
  1773. mtu = xfrm_state_mtu(dst->xfrm, mtu);
  1774. if (mtu > last->route_mtu_cached)
  1775. mtu = last->route_mtu_cached;
  1776. dst->metrics[RTAX_MTU-1] = mtu;
  1777. if (last == first)
  1778. break;
  1779. last = last->u.next;
  1780. last->child_mtu_cached = mtu;
  1781. }
  1782. return 1;
  1783. }
  1784. EXPORT_SYMBOL(xfrm_bundle_ok);
  1785. #ifdef CONFIG_AUDITSYSCALL
  1786. /* Audit addition and deletion of SAs and ipsec policy */
  1787. void xfrm_audit_log(uid_t auid, u32 sid, int type, int result,
  1788. struct xfrm_policy *xp, struct xfrm_state *x)
  1789. {
  1790. char *secctx;
  1791. u32 secctx_len;
  1792. struct xfrm_sec_ctx *sctx = NULL;
  1793. struct audit_buffer *audit_buf;
  1794. int family;
  1795. extern int audit_enabled;
  1796. if (audit_enabled == 0)
  1797. return;
  1798. BUG_ON((type == AUDIT_MAC_IPSEC_ADDSA ||
  1799. type == AUDIT_MAC_IPSEC_DELSA) && !x);
  1800. BUG_ON((type == AUDIT_MAC_IPSEC_ADDSPD ||
  1801. type == AUDIT_MAC_IPSEC_DELSPD) && !xp);
  1802. audit_buf = audit_log_start(current->audit_context, GFP_ATOMIC, type);
  1803. if (audit_buf == NULL)
  1804. return;
  1805. switch(type) {
  1806. case AUDIT_MAC_IPSEC_ADDSA:
  1807. audit_log_format(audit_buf, "SAD add: auid=%u", auid);
  1808. break;
  1809. case AUDIT_MAC_IPSEC_DELSA:
  1810. audit_log_format(audit_buf, "SAD delete: auid=%u", auid);
  1811. break;
  1812. case AUDIT_MAC_IPSEC_ADDSPD:
  1813. audit_log_format(audit_buf, "SPD add: auid=%u", auid);
  1814. break;
  1815. case AUDIT_MAC_IPSEC_DELSPD:
  1816. audit_log_format(audit_buf, "SPD delete: auid=%u", auid);
  1817. break;
  1818. default:
  1819. return;
  1820. }
  1821. if (sid != 0 &&
  1822. security_secid_to_secctx(sid, &secctx, &secctx_len) == 0)
  1823. audit_log_format(audit_buf, " subj=%s", secctx);
  1824. else
  1825. audit_log_task_context(audit_buf);
  1826. if (xp) {
  1827. family = xp->selector.family;
  1828. if (xp->security)
  1829. sctx = xp->security;
  1830. } else {
  1831. family = x->props.family;
  1832. if (x->security)
  1833. sctx = x->security;
  1834. }
  1835. if (sctx)
  1836. audit_log_format(audit_buf,
  1837. " sec_alg=%u sec_doi=%u sec_obj=%s",
  1838. sctx->ctx_alg, sctx->ctx_doi, sctx->ctx_str);
  1839. switch(family) {
  1840. case AF_INET:
  1841. {
  1842. struct in_addr saddr, daddr;
  1843. if (xp) {
  1844. saddr.s_addr = xp->selector.saddr.a4;
  1845. daddr.s_addr = xp->selector.daddr.a4;
  1846. } else {
  1847. saddr.s_addr = x->props.saddr.a4;
  1848. daddr.s_addr = x->id.daddr.a4;
  1849. }
  1850. audit_log_format(audit_buf,
  1851. " src=%u.%u.%u.%u dst=%u.%u.%u.%u",
  1852. NIPQUAD(saddr), NIPQUAD(daddr));
  1853. }
  1854. break;
  1855. case AF_INET6:
  1856. {
  1857. struct in6_addr saddr6, daddr6;
  1858. if (xp) {
  1859. memcpy(&saddr6, xp->selector.saddr.a6,
  1860. sizeof(struct in6_addr));
  1861. memcpy(&daddr6, xp->selector.daddr.a6,
  1862. sizeof(struct in6_addr));
  1863. } else {
  1864. memcpy(&saddr6, x->props.saddr.a6,
  1865. sizeof(struct in6_addr));
  1866. memcpy(&daddr6, x->id.daddr.a6,
  1867. sizeof(struct in6_addr));
  1868. }
  1869. audit_log_format(audit_buf,
  1870. " src=" NIP6_FMT " dst=" NIP6_FMT,
  1871. NIP6(saddr6), NIP6(daddr6));
  1872. }
  1873. break;
  1874. }
  1875. if (x)
  1876. audit_log_format(audit_buf, " spi=%lu(0x%lx) protocol=%s",
  1877. (unsigned long)ntohl(x->id.spi),
  1878. (unsigned long)ntohl(x->id.spi),
  1879. x->id.proto == IPPROTO_AH ? "AH" :
  1880. (x->id.proto == IPPROTO_ESP ?
  1881. "ESP" : "IPCOMP"));
  1882. audit_log_format(audit_buf, " res=%u", result);
  1883. audit_log_end(audit_buf);
  1884. }
  1885. EXPORT_SYMBOL(xfrm_audit_log);
  1886. #endif /* CONFIG_AUDITSYSCALL */
  1887. int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
  1888. {
  1889. int err = 0;
  1890. if (unlikely(afinfo == NULL))
  1891. return -EINVAL;
  1892. if (unlikely(afinfo->family >= NPROTO))
  1893. return -EAFNOSUPPORT;
  1894. write_lock_bh(&xfrm_policy_afinfo_lock);
  1895. if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
  1896. err = -ENOBUFS;
  1897. else {
  1898. struct dst_ops *dst_ops = afinfo->dst_ops;
  1899. if (likely(dst_ops->kmem_cachep == NULL))
  1900. dst_ops->kmem_cachep = xfrm_dst_cache;
  1901. if (likely(dst_ops->check == NULL))
  1902. dst_ops->check = xfrm_dst_check;
  1903. if (likely(dst_ops->negative_advice == NULL))
  1904. dst_ops->negative_advice = xfrm_negative_advice;
  1905. if (likely(dst_ops->link_failure == NULL))
  1906. dst_ops->link_failure = xfrm_link_failure;
  1907. if (likely(afinfo->garbage_collect == NULL))
  1908. afinfo->garbage_collect = __xfrm_garbage_collect;
  1909. xfrm_policy_afinfo[afinfo->family] = afinfo;
  1910. }
  1911. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1912. return err;
  1913. }
  1914. EXPORT_SYMBOL(xfrm_policy_register_afinfo);
  1915. int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
  1916. {
  1917. int err = 0;
  1918. if (unlikely(afinfo == NULL))
  1919. return -EINVAL;
  1920. if (unlikely(afinfo->family >= NPROTO))
  1921. return -EAFNOSUPPORT;
  1922. write_lock_bh(&xfrm_policy_afinfo_lock);
  1923. if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
  1924. if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
  1925. err = -EINVAL;
  1926. else {
  1927. struct dst_ops *dst_ops = afinfo->dst_ops;
  1928. xfrm_policy_afinfo[afinfo->family] = NULL;
  1929. dst_ops->kmem_cachep = NULL;
  1930. dst_ops->check = NULL;
  1931. dst_ops->negative_advice = NULL;
  1932. dst_ops->link_failure = NULL;
  1933. afinfo->garbage_collect = NULL;
  1934. }
  1935. }
  1936. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1937. return err;
  1938. }
  1939. EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
  1940. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
  1941. {
  1942. struct xfrm_policy_afinfo *afinfo;
  1943. if (unlikely(family >= NPROTO))
  1944. return NULL;
  1945. read_lock(&xfrm_policy_afinfo_lock);
  1946. afinfo = xfrm_policy_afinfo[family];
  1947. if (unlikely(!afinfo))
  1948. read_unlock(&xfrm_policy_afinfo_lock);
  1949. return afinfo;
  1950. }
  1951. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
  1952. {
  1953. read_unlock(&xfrm_policy_afinfo_lock);
  1954. }
  1955. static struct xfrm_policy_afinfo *xfrm_policy_lock_afinfo(unsigned int family)
  1956. {
  1957. struct xfrm_policy_afinfo *afinfo;
  1958. if (unlikely(family >= NPROTO))
  1959. return NULL;
  1960. write_lock_bh(&xfrm_policy_afinfo_lock);
  1961. afinfo = xfrm_policy_afinfo[family];
  1962. if (unlikely(!afinfo))
  1963. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1964. return afinfo;
  1965. }
  1966. static void xfrm_policy_unlock_afinfo(struct xfrm_policy_afinfo *afinfo)
  1967. {
  1968. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1969. }
  1970. static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  1971. {
  1972. switch (event) {
  1973. case NETDEV_DOWN:
  1974. xfrm_flush_bundles();
  1975. }
  1976. return NOTIFY_DONE;
  1977. }
  1978. static struct notifier_block xfrm_dev_notifier = {
  1979. xfrm_dev_event,
  1980. NULL,
  1981. 0
  1982. };
  1983. static void __init xfrm_policy_init(void)
  1984. {
  1985. unsigned int hmask, sz;
  1986. int dir;
  1987. xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
  1988. sizeof(struct xfrm_dst),
  1989. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  1990. NULL, NULL);
  1991. hmask = 8 - 1;
  1992. sz = (hmask+1) * sizeof(struct hlist_head);
  1993. xfrm_policy_byidx = xfrm_hash_alloc(sz);
  1994. xfrm_idx_hmask = hmask;
  1995. if (!xfrm_policy_byidx)
  1996. panic("XFRM: failed to allocate byidx hash\n");
  1997. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1998. struct xfrm_policy_hash *htab;
  1999. INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
  2000. htab = &xfrm_policy_bydst[dir];
  2001. htab->table = xfrm_hash_alloc(sz);
  2002. htab->hmask = hmask;
  2003. if (!htab->table)
  2004. panic("XFRM: failed to allocate bydst hash\n");
  2005. }
  2006. INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
  2007. register_netdevice_notifier(&xfrm_dev_notifier);
  2008. }
  2009. void __init xfrm_init(void)
  2010. {
  2011. xfrm_state_init();
  2012. xfrm_policy_init();
  2013. xfrm_input_init();
  2014. }
  2015. #ifdef CONFIG_XFRM_MIGRATE
  2016. static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
  2017. struct xfrm_selector *sel_tgt)
  2018. {
  2019. if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
  2020. if (sel_tgt->family == sel_cmp->family &&
  2021. xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
  2022. sel_cmp->family) == 0 &&
  2023. xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
  2024. sel_cmp->family) == 0 &&
  2025. sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
  2026. sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
  2027. return 1;
  2028. }
  2029. } else {
  2030. if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
  2031. return 1;
  2032. }
  2033. }
  2034. return 0;
  2035. }
  2036. static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
  2037. u8 dir, u8 type)
  2038. {
  2039. struct xfrm_policy *pol, *ret = NULL;
  2040. struct hlist_node *entry;
  2041. struct hlist_head *chain;
  2042. u32 priority = ~0U;
  2043. read_lock_bh(&xfrm_policy_lock);
  2044. chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
  2045. hlist_for_each_entry(pol, entry, chain, bydst) {
  2046. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  2047. pol->type == type) {
  2048. ret = pol;
  2049. priority = ret->priority;
  2050. break;
  2051. }
  2052. }
  2053. chain = &xfrm_policy_inexact[dir];
  2054. hlist_for_each_entry(pol, entry, chain, bydst) {
  2055. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  2056. pol->type == type &&
  2057. pol->priority < priority) {
  2058. ret = pol;
  2059. break;
  2060. }
  2061. }
  2062. if (ret)
  2063. xfrm_pol_hold(ret);
  2064. read_unlock_bh(&xfrm_policy_lock);
  2065. return ret;
  2066. }
  2067. static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
  2068. {
  2069. int match = 0;
  2070. if (t->mode == m->mode && t->id.proto == m->proto &&
  2071. (m->reqid == 0 || t->reqid == m->reqid)) {
  2072. switch (t->mode) {
  2073. case XFRM_MODE_TUNNEL:
  2074. case XFRM_MODE_BEET:
  2075. if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
  2076. m->old_family) == 0 &&
  2077. xfrm_addr_cmp(&t->saddr, &m->old_saddr,
  2078. m->old_family) == 0) {
  2079. match = 1;
  2080. }
  2081. break;
  2082. case XFRM_MODE_TRANSPORT:
  2083. /* in case of transport mode, template does not store
  2084. any IP addresses, hence we just compare mode and
  2085. protocol */
  2086. match = 1;
  2087. break;
  2088. default:
  2089. break;
  2090. }
  2091. }
  2092. return match;
  2093. }
  2094. /* update endpoint address(es) of template(s) */
  2095. static int xfrm_policy_migrate(struct xfrm_policy *pol,
  2096. struct xfrm_migrate *m, int num_migrate)
  2097. {
  2098. struct xfrm_migrate *mp;
  2099. struct dst_entry *dst;
  2100. int i, j, n = 0;
  2101. write_lock_bh(&pol->lock);
  2102. if (unlikely(pol->dead)) {
  2103. /* target policy has been deleted */
  2104. write_unlock_bh(&pol->lock);
  2105. return -ENOENT;
  2106. }
  2107. for (i = 0; i < pol->xfrm_nr; i++) {
  2108. for (j = 0, mp = m; j < num_migrate; j++, mp++) {
  2109. if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
  2110. continue;
  2111. n++;
  2112. if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL)
  2113. continue;
  2114. /* update endpoints */
  2115. memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
  2116. sizeof(pol->xfrm_vec[i].id.daddr));
  2117. memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
  2118. sizeof(pol->xfrm_vec[i].saddr));
  2119. pol->xfrm_vec[i].encap_family = mp->new_family;
  2120. /* flush bundles */
  2121. while ((dst = pol->bundles) != NULL) {
  2122. pol->bundles = dst->next;
  2123. dst_free(dst);
  2124. }
  2125. }
  2126. }
  2127. write_unlock_bh(&pol->lock);
  2128. if (!n)
  2129. return -ENODATA;
  2130. return 0;
  2131. }
  2132. static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
  2133. {
  2134. int i, j;
  2135. if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
  2136. return -EINVAL;
  2137. for (i = 0; i < num_migrate; i++) {
  2138. if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
  2139. m[i].old_family) == 0) &&
  2140. (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
  2141. m[i].old_family) == 0))
  2142. return -EINVAL;
  2143. if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
  2144. xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
  2145. return -EINVAL;
  2146. /* check if there is any duplicated entry */
  2147. for (j = i + 1; j < num_migrate; j++) {
  2148. if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
  2149. sizeof(m[i].old_daddr)) &&
  2150. !memcmp(&m[i].old_saddr, &m[j].old_saddr,
  2151. sizeof(m[i].old_saddr)) &&
  2152. m[i].proto == m[j].proto &&
  2153. m[i].mode == m[j].mode &&
  2154. m[i].reqid == m[j].reqid &&
  2155. m[i].old_family == m[j].old_family)
  2156. return -EINVAL;
  2157. }
  2158. }
  2159. return 0;
  2160. }
  2161. int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  2162. struct xfrm_migrate *m, int num_migrate)
  2163. {
  2164. int i, err, nx_cur = 0, nx_new = 0;
  2165. struct xfrm_policy *pol = NULL;
  2166. struct xfrm_state *x, *xc;
  2167. struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
  2168. struct xfrm_state *x_new[XFRM_MAX_DEPTH];
  2169. struct xfrm_migrate *mp;
  2170. if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
  2171. goto out;
  2172. /* Stage 1 - find policy */
  2173. if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
  2174. err = -ENOENT;
  2175. goto out;
  2176. }
  2177. /* Stage 2 - find and update state(s) */
  2178. for (i = 0, mp = m; i < num_migrate; i++, mp++) {
  2179. if ((x = xfrm_migrate_state_find(mp))) {
  2180. x_cur[nx_cur] = x;
  2181. nx_cur++;
  2182. if ((xc = xfrm_state_migrate(x, mp))) {
  2183. x_new[nx_new] = xc;
  2184. nx_new++;
  2185. } else {
  2186. err = -ENODATA;
  2187. goto restore_state;
  2188. }
  2189. }
  2190. }
  2191. /* Stage 3 - update policy */
  2192. if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
  2193. goto restore_state;
  2194. /* Stage 4 - delete old state(s) */
  2195. if (nx_cur) {
  2196. xfrm_states_put(x_cur, nx_cur);
  2197. xfrm_states_delete(x_cur, nx_cur);
  2198. }
  2199. /* Stage 5 - announce */
  2200. km_migrate(sel, dir, type, m, num_migrate);
  2201. xfrm_pol_put(pol);
  2202. return 0;
  2203. out:
  2204. return err;
  2205. restore_state:
  2206. if (pol)
  2207. xfrm_pol_put(pol);
  2208. if (nx_cur)
  2209. xfrm_states_put(x_cur, nx_cur);
  2210. if (nx_new)
  2211. xfrm_states_delete(x_new, nx_new);
  2212. return err;
  2213. }
  2214. EXPORT_SYMBOL(xfrm_migrate);
  2215. #endif