xfrm_policy.c 61 KB

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