xfrm_policy.c 60 KB

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