xfrm_policy.c 64 KB

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