xfrm_policy.c 64 KB

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