xfrm_policy.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738
  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. list_add_tail(&pol->bytype, &xfrm_policy_bytype[pol->type]);
  923. hlist_add_head(&pol->bydst, chain);
  924. hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
  925. xfrm_policy_count[dir]++;
  926. xfrm_pol_hold(pol);
  927. if (xfrm_bydst_should_resize(dir, NULL))
  928. schedule_work(&xfrm_hash_work);
  929. }
  930. static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
  931. int dir)
  932. {
  933. if (hlist_unhashed(&pol->bydst))
  934. return NULL;
  935. hlist_del(&pol->bydst);
  936. hlist_del(&pol->byidx);
  937. xfrm_policy_count[dir]--;
  938. return pol;
  939. }
  940. int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
  941. {
  942. write_lock_bh(&xfrm_policy_lock);
  943. pol = __xfrm_policy_unlink(pol, dir);
  944. write_unlock_bh(&xfrm_policy_lock);
  945. if (pol) {
  946. if (dir < XFRM_POLICY_MAX)
  947. atomic_inc(&flow_cache_genid);
  948. xfrm_policy_kill(pol);
  949. return 0;
  950. }
  951. return -ENOENT;
  952. }
  953. EXPORT_SYMBOL(xfrm_policy_delete);
  954. int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
  955. {
  956. struct xfrm_policy *old_pol;
  957. #ifdef CONFIG_XFRM_SUB_POLICY
  958. if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
  959. return -EINVAL;
  960. #endif
  961. write_lock_bh(&xfrm_policy_lock);
  962. old_pol = sk->sk_policy[dir];
  963. sk->sk_policy[dir] = pol;
  964. if (pol) {
  965. pol->curlft.add_time = get_seconds();
  966. pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
  967. __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
  968. }
  969. if (old_pol)
  970. __xfrm_policy_unlink(old_pol, XFRM_POLICY_MAX+dir);
  971. write_unlock_bh(&xfrm_policy_lock);
  972. if (old_pol) {
  973. xfrm_policy_kill(old_pol);
  974. }
  975. return 0;
  976. }
  977. static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
  978. {
  979. struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
  980. if (newp) {
  981. newp->selector = old->selector;
  982. if (security_xfrm_policy_clone(old->security,
  983. &newp->security)) {
  984. kfree(newp);
  985. return NULL; /* ENOMEM */
  986. }
  987. newp->lft = old->lft;
  988. newp->curlft = old->curlft;
  989. newp->action = old->action;
  990. newp->flags = old->flags;
  991. newp->xfrm_nr = old->xfrm_nr;
  992. newp->index = old->index;
  993. newp->type = old->type;
  994. memcpy(newp->xfrm_vec, old->xfrm_vec,
  995. newp->xfrm_nr*sizeof(struct xfrm_tmpl));
  996. write_lock_bh(&xfrm_policy_lock);
  997. __xfrm_policy_link(newp, XFRM_POLICY_MAX+dir);
  998. write_unlock_bh(&xfrm_policy_lock);
  999. xfrm_pol_put(newp);
  1000. }
  1001. return newp;
  1002. }
  1003. int __xfrm_sk_clone_policy(struct sock *sk)
  1004. {
  1005. struct xfrm_policy *p0 = sk->sk_policy[0],
  1006. *p1 = sk->sk_policy[1];
  1007. sk->sk_policy[0] = sk->sk_policy[1] = NULL;
  1008. if (p0 && (sk->sk_policy[0] = clone_policy(p0, 0)) == NULL)
  1009. return -ENOMEM;
  1010. if (p1 && (sk->sk_policy[1] = clone_policy(p1, 1)) == NULL)
  1011. return -ENOMEM;
  1012. return 0;
  1013. }
  1014. static int
  1015. xfrm_get_saddr(xfrm_address_t *local, xfrm_address_t *remote,
  1016. unsigned short family)
  1017. {
  1018. int err;
  1019. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1020. if (unlikely(afinfo == NULL))
  1021. return -EINVAL;
  1022. err = afinfo->get_saddr(local, remote);
  1023. xfrm_policy_put_afinfo(afinfo);
  1024. return err;
  1025. }
  1026. /* Resolve list of templates for the flow, given policy. */
  1027. static int
  1028. xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
  1029. struct xfrm_state **xfrm,
  1030. unsigned short family)
  1031. {
  1032. int nx;
  1033. int i, error;
  1034. xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
  1035. xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
  1036. xfrm_address_t tmp;
  1037. for (nx=0, i = 0; i < policy->xfrm_nr; i++) {
  1038. struct xfrm_state *x;
  1039. xfrm_address_t *remote = daddr;
  1040. xfrm_address_t *local = saddr;
  1041. struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
  1042. if (tmpl->mode == XFRM_MODE_TUNNEL ||
  1043. tmpl->mode == XFRM_MODE_BEET) {
  1044. remote = &tmpl->id.daddr;
  1045. local = &tmpl->saddr;
  1046. family = tmpl->encap_family;
  1047. if (xfrm_addr_any(local, family)) {
  1048. error = xfrm_get_saddr(&tmp, remote, family);
  1049. if (error)
  1050. goto fail;
  1051. local = &tmp;
  1052. }
  1053. }
  1054. x = xfrm_state_find(remote, local, fl, tmpl, policy, &error, family);
  1055. if (x && x->km.state == XFRM_STATE_VALID) {
  1056. xfrm[nx++] = x;
  1057. daddr = remote;
  1058. saddr = local;
  1059. continue;
  1060. }
  1061. if (x) {
  1062. error = (x->km.state == XFRM_STATE_ERROR ?
  1063. -EINVAL : -EAGAIN);
  1064. xfrm_state_put(x);
  1065. }
  1066. if (!tmpl->optional)
  1067. goto fail;
  1068. }
  1069. return nx;
  1070. fail:
  1071. for (nx--; nx>=0; nx--)
  1072. xfrm_state_put(xfrm[nx]);
  1073. return error;
  1074. }
  1075. static int
  1076. xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, struct flowi *fl,
  1077. struct xfrm_state **xfrm,
  1078. unsigned short family)
  1079. {
  1080. struct xfrm_state *tp[XFRM_MAX_DEPTH];
  1081. struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
  1082. int cnx = 0;
  1083. int error;
  1084. int ret;
  1085. int i;
  1086. for (i = 0; i < npols; i++) {
  1087. if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
  1088. error = -ENOBUFS;
  1089. goto fail;
  1090. }
  1091. ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
  1092. if (ret < 0) {
  1093. error = ret;
  1094. goto fail;
  1095. } else
  1096. cnx += ret;
  1097. }
  1098. /* found states are sorted for outbound processing */
  1099. if (npols > 1)
  1100. xfrm_state_sort(xfrm, tpp, cnx, family);
  1101. return cnx;
  1102. fail:
  1103. for (cnx--; cnx>=0; cnx--)
  1104. xfrm_state_put(tpp[cnx]);
  1105. return error;
  1106. }
  1107. /* Check that the bundle accepts the flow and its components are
  1108. * still valid.
  1109. */
  1110. static struct dst_entry *
  1111. xfrm_find_bundle(struct flowi *fl, struct xfrm_policy *policy, unsigned short family)
  1112. {
  1113. struct dst_entry *x;
  1114. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1115. if (unlikely(afinfo == NULL))
  1116. return ERR_PTR(-EINVAL);
  1117. x = afinfo->find_bundle(fl, policy);
  1118. xfrm_policy_put_afinfo(afinfo);
  1119. return x;
  1120. }
  1121. static inline int xfrm_get_tos(struct flowi *fl, int family)
  1122. {
  1123. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1124. int tos;
  1125. if (!afinfo)
  1126. return -EINVAL;
  1127. tos = afinfo->get_tos(fl);
  1128. xfrm_policy_put_afinfo(afinfo);
  1129. return tos;
  1130. }
  1131. static inline struct xfrm_dst *xfrm_alloc_dst(int family)
  1132. {
  1133. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1134. struct xfrm_dst *xdst;
  1135. if (!afinfo)
  1136. return ERR_PTR(-EINVAL);
  1137. xdst = dst_alloc(afinfo->dst_ops) ?: ERR_PTR(-ENOBUFS);
  1138. xfrm_policy_put_afinfo(afinfo);
  1139. return xdst;
  1140. }
  1141. static inline int xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  1142. int nfheader_len)
  1143. {
  1144. struct xfrm_policy_afinfo *afinfo =
  1145. xfrm_policy_get_afinfo(dst->ops->family);
  1146. int err;
  1147. if (!afinfo)
  1148. return -EINVAL;
  1149. err = afinfo->init_path(path, dst, nfheader_len);
  1150. xfrm_policy_put_afinfo(afinfo);
  1151. return err;
  1152. }
  1153. static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev)
  1154. {
  1155. struct xfrm_policy_afinfo *afinfo =
  1156. xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
  1157. int err;
  1158. if (!afinfo)
  1159. return -EINVAL;
  1160. err = afinfo->fill_dst(xdst, dev);
  1161. xfrm_policy_put_afinfo(afinfo);
  1162. return err;
  1163. }
  1164. /* Allocate chain of dst_entry's, attach known xfrm's, calculate
  1165. * all the metrics... Shortly, bundle a bundle.
  1166. */
  1167. static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
  1168. struct xfrm_state **xfrm, int nx,
  1169. struct flowi *fl,
  1170. struct dst_entry *dst)
  1171. {
  1172. unsigned long now = jiffies;
  1173. struct net_device *dev;
  1174. struct dst_entry *dst_prev = NULL;
  1175. struct dst_entry *dst0 = NULL;
  1176. int i = 0;
  1177. int err;
  1178. int header_len = 0;
  1179. int nfheader_len = 0;
  1180. int trailer_len = 0;
  1181. int tos;
  1182. int family = policy->selector.family;
  1183. xfrm_address_t saddr, daddr;
  1184. xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
  1185. tos = xfrm_get_tos(fl, family);
  1186. err = tos;
  1187. if (tos < 0)
  1188. goto put_states;
  1189. dst_hold(dst);
  1190. for (; i < nx; i++) {
  1191. struct xfrm_dst *xdst = xfrm_alloc_dst(family);
  1192. struct dst_entry *dst1 = &xdst->u.dst;
  1193. err = PTR_ERR(xdst);
  1194. if (IS_ERR(xdst)) {
  1195. dst_release(dst);
  1196. goto put_states;
  1197. }
  1198. if (!dst_prev)
  1199. dst0 = dst1;
  1200. else {
  1201. dst_prev->child = dst_clone(dst1);
  1202. dst1->flags |= DST_NOHASH;
  1203. }
  1204. xdst->route = dst;
  1205. memcpy(&dst1->metrics, &dst->metrics, sizeof(dst->metrics));
  1206. if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
  1207. family = xfrm[i]->props.family;
  1208. dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
  1209. family);
  1210. err = PTR_ERR(dst);
  1211. if (IS_ERR(dst))
  1212. goto put_states;
  1213. } else
  1214. dst_hold(dst);
  1215. dst1->xfrm = xfrm[i];
  1216. xdst->genid = xfrm[i]->genid;
  1217. dst1->obsolete = -1;
  1218. dst1->flags |= DST_HOST;
  1219. dst1->lastuse = now;
  1220. dst1->input = dst_discard;
  1221. dst1->output = xfrm[i]->outer_mode->afinfo->output;
  1222. dst1->next = dst_prev;
  1223. dst_prev = dst1;
  1224. header_len += xfrm[i]->props.header_len;
  1225. if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
  1226. nfheader_len += xfrm[i]->props.header_len;
  1227. trailer_len += xfrm[i]->props.trailer_len;
  1228. }
  1229. dst_prev->child = dst;
  1230. dst0->path = dst;
  1231. err = -ENODEV;
  1232. dev = dst->dev;
  1233. if (!dev)
  1234. goto free_dst;
  1235. /* Copy neighbout for reachability confirmation */
  1236. dst0->neighbour = neigh_clone(dst->neighbour);
  1237. xfrm_init_path((struct xfrm_dst *)dst0, dst, nfheader_len);
  1238. xfrm_init_pmtu(dst_prev);
  1239. for (dst_prev = dst0; dst_prev != dst; dst_prev = dst_prev->child) {
  1240. struct xfrm_dst *xdst = (struct xfrm_dst *)dst_prev;
  1241. err = xfrm_fill_dst(xdst, dev);
  1242. if (err)
  1243. goto free_dst;
  1244. dst_prev->header_len = header_len;
  1245. dst_prev->trailer_len = trailer_len;
  1246. header_len -= xdst->u.dst.xfrm->props.header_len;
  1247. trailer_len -= xdst->u.dst.xfrm->props.trailer_len;
  1248. }
  1249. out:
  1250. return dst0;
  1251. put_states:
  1252. for (; i < nx; i++)
  1253. xfrm_state_put(xfrm[i]);
  1254. free_dst:
  1255. if (dst0)
  1256. dst_free(dst0);
  1257. dst0 = ERR_PTR(err);
  1258. goto out;
  1259. }
  1260. static int inline
  1261. xfrm_dst_alloc_copy(void **target, void *src, int size)
  1262. {
  1263. if (!*target) {
  1264. *target = kmalloc(size, GFP_ATOMIC);
  1265. if (!*target)
  1266. return -ENOMEM;
  1267. }
  1268. memcpy(*target, src, size);
  1269. return 0;
  1270. }
  1271. static int inline
  1272. xfrm_dst_update_parent(struct dst_entry *dst, struct xfrm_selector *sel)
  1273. {
  1274. #ifdef CONFIG_XFRM_SUB_POLICY
  1275. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1276. return xfrm_dst_alloc_copy((void **)&(xdst->partner),
  1277. sel, sizeof(*sel));
  1278. #else
  1279. return 0;
  1280. #endif
  1281. }
  1282. static int inline
  1283. xfrm_dst_update_origin(struct dst_entry *dst, struct flowi *fl)
  1284. {
  1285. #ifdef CONFIG_XFRM_SUB_POLICY
  1286. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1287. return xfrm_dst_alloc_copy((void **)&(xdst->origin), fl, sizeof(*fl));
  1288. #else
  1289. return 0;
  1290. #endif
  1291. }
  1292. static int stale_bundle(struct dst_entry *dst);
  1293. /* Main function: finds/creates a bundle for given flow.
  1294. *
  1295. * At the moment we eat a raw IP route. Mostly to speed up lookups
  1296. * on interfaces with disabled IPsec.
  1297. */
  1298. int __xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1299. struct sock *sk, int flags)
  1300. {
  1301. struct xfrm_policy *policy;
  1302. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1303. int npols;
  1304. int pol_dead;
  1305. int xfrm_nr;
  1306. int pi;
  1307. struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
  1308. struct dst_entry *dst, *dst_orig = *dst_p;
  1309. int nx = 0;
  1310. int err;
  1311. u32 genid;
  1312. u16 family;
  1313. u8 dir = policy_to_flow_dir(XFRM_POLICY_OUT);
  1314. restart:
  1315. genid = atomic_read(&flow_cache_genid);
  1316. policy = NULL;
  1317. for (pi = 0; pi < ARRAY_SIZE(pols); pi++)
  1318. pols[pi] = NULL;
  1319. npols = 0;
  1320. pol_dead = 0;
  1321. xfrm_nr = 0;
  1322. if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
  1323. policy = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl);
  1324. err = PTR_ERR(policy);
  1325. if (IS_ERR(policy)) {
  1326. XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR);
  1327. goto dropdst;
  1328. }
  1329. }
  1330. if (!policy) {
  1331. /* To accelerate a bit... */
  1332. if ((dst_orig->flags & DST_NOXFRM) ||
  1333. !xfrm_policy_count[XFRM_POLICY_OUT])
  1334. goto nopol;
  1335. policy = flow_cache_lookup(fl, dst_orig->ops->family,
  1336. dir, xfrm_policy_lookup);
  1337. err = PTR_ERR(policy);
  1338. if (IS_ERR(policy)) {
  1339. XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR);
  1340. goto dropdst;
  1341. }
  1342. }
  1343. if (!policy)
  1344. goto nopol;
  1345. family = dst_orig->ops->family;
  1346. pols[0] = policy;
  1347. npols ++;
  1348. xfrm_nr += pols[0]->xfrm_nr;
  1349. err = -ENOENT;
  1350. if ((flags & XFRM_LOOKUP_ICMP) && !(policy->flags & XFRM_POLICY_ICMP))
  1351. goto error;
  1352. policy->curlft.use_time = get_seconds();
  1353. switch (policy->action) {
  1354. default:
  1355. case XFRM_POLICY_BLOCK:
  1356. /* Prohibit the flow */
  1357. XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLBLOCK);
  1358. err = -EPERM;
  1359. goto error;
  1360. case XFRM_POLICY_ALLOW:
  1361. #ifndef CONFIG_XFRM_SUB_POLICY
  1362. if (policy->xfrm_nr == 0) {
  1363. /* Flow passes not transformed. */
  1364. xfrm_pol_put(policy);
  1365. return 0;
  1366. }
  1367. #endif
  1368. /* Try to find matching bundle.
  1369. *
  1370. * LATER: help from flow cache. It is optional, this
  1371. * is required only for output policy.
  1372. */
  1373. dst = xfrm_find_bundle(fl, policy, family);
  1374. if (IS_ERR(dst)) {
  1375. XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
  1376. err = PTR_ERR(dst);
  1377. goto error;
  1378. }
  1379. if (dst)
  1380. break;
  1381. #ifdef CONFIG_XFRM_SUB_POLICY
  1382. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1383. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1384. fl, family,
  1385. XFRM_POLICY_OUT);
  1386. if (pols[1]) {
  1387. if (IS_ERR(pols[1])) {
  1388. XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR);
  1389. err = PTR_ERR(pols[1]);
  1390. goto error;
  1391. }
  1392. if (pols[1]->action == XFRM_POLICY_BLOCK) {
  1393. XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLBLOCK);
  1394. err = -EPERM;
  1395. goto error;
  1396. }
  1397. npols ++;
  1398. xfrm_nr += pols[1]->xfrm_nr;
  1399. }
  1400. }
  1401. /*
  1402. * Because neither flowi nor bundle information knows about
  1403. * transformation template size. On more than one policy usage
  1404. * we can realize whether all of them is bypass or not after
  1405. * they are searched. See above not-transformed bypass
  1406. * is surrounded by non-sub policy configuration, too.
  1407. */
  1408. if (xfrm_nr == 0) {
  1409. /* Flow passes not transformed. */
  1410. xfrm_pols_put(pols, npols);
  1411. return 0;
  1412. }
  1413. #endif
  1414. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1415. if (unlikely(nx<0)) {
  1416. err = nx;
  1417. if (err == -EAGAIN && sysctl_xfrm_larval_drop) {
  1418. /* EREMOTE tells the caller to generate
  1419. * a one-shot blackhole route.
  1420. */
  1421. XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES);
  1422. xfrm_pol_put(policy);
  1423. return -EREMOTE;
  1424. }
  1425. if (err == -EAGAIN && (flags & XFRM_LOOKUP_WAIT)) {
  1426. DECLARE_WAITQUEUE(wait, current);
  1427. add_wait_queue(&km_waitq, &wait);
  1428. set_current_state(TASK_INTERRUPTIBLE);
  1429. schedule();
  1430. set_current_state(TASK_RUNNING);
  1431. remove_wait_queue(&km_waitq, &wait);
  1432. nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
  1433. if (nx == -EAGAIN && signal_pending(current)) {
  1434. XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES);
  1435. err = -ERESTART;
  1436. goto error;
  1437. }
  1438. if (nx == -EAGAIN ||
  1439. genid != atomic_read(&flow_cache_genid)) {
  1440. xfrm_pols_put(pols, npols);
  1441. goto restart;
  1442. }
  1443. err = nx;
  1444. }
  1445. if (err < 0) {
  1446. XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES);
  1447. goto error;
  1448. }
  1449. }
  1450. if (nx == 0) {
  1451. /* Flow passes not transformed. */
  1452. xfrm_pols_put(pols, npols);
  1453. return 0;
  1454. }
  1455. dst = xfrm_bundle_create(policy, xfrm, nx, fl, dst_orig);
  1456. err = PTR_ERR(dst);
  1457. if (IS_ERR(dst)) {
  1458. XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLEGENERROR);
  1459. goto error;
  1460. }
  1461. for (pi = 0; pi < npols; pi++) {
  1462. read_lock_bh(&pols[pi]->lock);
  1463. pol_dead |= pols[pi]->dead;
  1464. read_unlock_bh(&pols[pi]->lock);
  1465. }
  1466. write_lock_bh(&policy->lock);
  1467. if (unlikely(pol_dead || stale_bundle(dst))) {
  1468. /* Wow! While we worked on resolving, this
  1469. * policy has gone. Retry. It is not paranoia,
  1470. * we just cannot enlist new bundle to dead object.
  1471. * We can't enlist stable bundles either.
  1472. */
  1473. write_unlock_bh(&policy->lock);
  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. dst_free(dst);
  1489. XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
  1490. goto error;
  1491. }
  1492. dst->next = policy->bundles;
  1493. policy->bundles = dst;
  1494. dst_hold(dst);
  1495. write_unlock_bh(&policy->lock);
  1496. }
  1497. *dst_p = dst;
  1498. dst_release(dst_orig);
  1499. xfrm_pols_put(pols, npols);
  1500. return 0;
  1501. error:
  1502. xfrm_pols_put(pols, npols);
  1503. dropdst:
  1504. dst_release(dst_orig);
  1505. *dst_p = NULL;
  1506. return err;
  1507. nopol:
  1508. err = -ENOENT;
  1509. if (flags & XFRM_LOOKUP_ICMP)
  1510. goto dropdst;
  1511. return 0;
  1512. }
  1513. EXPORT_SYMBOL(__xfrm_lookup);
  1514. int xfrm_lookup(struct dst_entry **dst_p, struct flowi *fl,
  1515. struct sock *sk, int flags)
  1516. {
  1517. int err = __xfrm_lookup(dst_p, fl, sk, flags);
  1518. if (err == -EREMOTE) {
  1519. dst_release(*dst_p);
  1520. *dst_p = NULL;
  1521. err = -EAGAIN;
  1522. }
  1523. return err;
  1524. }
  1525. EXPORT_SYMBOL(xfrm_lookup);
  1526. static inline int
  1527. xfrm_secpath_reject(int idx, struct sk_buff *skb, struct flowi *fl)
  1528. {
  1529. struct xfrm_state *x;
  1530. if (!skb->sp || idx < 0 || idx >= skb->sp->len)
  1531. return 0;
  1532. x = skb->sp->xvec[idx];
  1533. if (!x->type->reject)
  1534. return 0;
  1535. return x->type->reject(x, skb, fl);
  1536. }
  1537. /* When skb is transformed back to its "native" form, we have to
  1538. * check policy restrictions. At the moment we make this in maximally
  1539. * stupid way. Shame on me. :-) Of course, connected sockets must
  1540. * have policy cached at them.
  1541. */
  1542. static inline int
  1543. xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
  1544. unsigned short family)
  1545. {
  1546. if (xfrm_state_kern(x))
  1547. return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
  1548. return x->id.proto == tmpl->id.proto &&
  1549. (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
  1550. (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
  1551. x->props.mode == tmpl->mode &&
  1552. (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
  1553. !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
  1554. !(x->props.mode != XFRM_MODE_TRANSPORT &&
  1555. xfrm_state_addr_cmp(tmpl, x, family));
  1556. }
  1557. /*
  1558. * 0 or more than 0 is returned when validation is succeeded (either bypass
  1559. * because of optional transport mode, or next index of the mathced secpath
  1560. * state with the template.
  1561. * -1 is returned when no matching template is found.
  1562. * Otherwise "-2 - errored_index" is returned.
  1563. */
  1564. static inline int
  1565. xfrm_policy_ok(struct xfrm_tmpl *tmpl, struct sec_path *sp, int start,
  1566. unsigned short family)
  1567. {
  1568. int idx = start;
  1569. if (tmpl->optional) {
  1570. if (tmpl->mode == XFRM_MODE_TRANSPORT)
  1571. return start;
  1572. } else
  1573. start = -1;
  1574. for (; idx < sp->len; idx++) {
  1575. if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
  1576. return ++idx;
  1577. if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
  1578. if (start == -1)
  1579. start = -2-idx;
  1580. break;
  1581. }
  1582. }
  1583. return start;
  1584. }
  1585. int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
  1586. unsigned int family, int reverse)
  1587. {
  1588. struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
  1589. int err;
  1590. if (unlikely(afinfo == NULL))
  1591. return -EAFNOSUPPORT;
  1592. afinfo->decode_session(skb, fl, reverse);
  1593. err = security_xfrm_decode_session(skb, &fl->secid);
  1594. xfrm_policy_put_afinfo(afinfo);
  1595. return err;
  1596. }
  1597. EXPORT_SYMBOL(__xfrm_decode_session);
  1598. static inline int secpath_has_nontransport(struct sec_path *sp, int k, int *idxp)
  1599. {
  1600. for (; k < sp->len; k++) {
  1601. if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
  1602. *idxp = k;
  1603. return 1;
  1604. }
  1605. }
  1606. return 0;
  1607. }
  1608. int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
  1609. unsigned short family)
  1610. {
  1611. struct xfrm_policy *pol;
  1612. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  1613. int npols = 0;
  1614. int xfrm_nr;
  1615. int pi;
  1616. int reverse;
  1617. struct flowi fl;
  1618. u8 fl_dir;
  1619. int xerr_idx = -1;
  1620. reverse = dir & ~XFRM_POLICY_MASK;
  1621. dir &= XFRM_POLICY_MASK;
  1622. fl_dir = policy_to_flow_dir(dir);
  1623. if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
  1624. XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR);
  1625. return 0;
  1626. }
  1627. nf_nat_decode_session(skb, &fl, family);
  1628. /* First, check used SA against their selectors. */
  1629. if (skb->sp) {
  1630. int i;
  1631. for (i=skb->sp->len-1; i>=0; i--) {
  1632. struct xfrm_state *x = skb->sp->xvec[i];
  1633. if (!xfrm_selector_match(&x->sel, &fl, family)) {
  1634. XFRM_INC_STATS(LINUX_MIB_XFRMINSTATEMISMATCH);
  1635. return 0;
  1636. }
  1637. }
  1638. }
  1639. pol = NULL;
  1640. if (sk && sk->sk_policy[dir]) {
  1641. pol = xfrm_sk_policy_lookup(sk, dir, &fl);
  1642. if (IS_ERR(pol)) {
  1643. XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR);
  1644. return 0;
  1645. }
  1646. }
  1647. if (!pol)
  1648. pol = flow_cache_lookup(&fl, family, fl_dir,
  1649. xfrm_policy_lookup);
  1650. if (IS_ERR(pol)) {
  1651. XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR);
  1652. return 0;
  1653. }
  1654. if (!pol) {
  1655. if (skb->sp && secpath_has_nontransport(skb->sp, 0, &xerr_idx)) {
  1656. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1657. XFRM_INC_STATS(LINUX_MIB_XFRMINNOPOLS);
  1658. return 0;
  1659. }
  1660. return 1;
  1661. }
  1662. pol->curlft.use_time = get_seconds();
  1663. pols[0] = pol;
  1664. npols ++;
  1665. #ifdef CONFIG_XFRM_SUB_POLICY
  1666. if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
  1667. pols[1] = xfrm_policy_lookup_bytype(XFRM_POLICY_TYPE_MAIN,
  1668. &fl, family,
  1669. XFRM_POLICY_IN);
  1670. if (pols[1]) {
  1671. if (IS_ERR(pols[1])) {
  1672. XFRM_INC_STATS(LINUX_MIB_XFRMINPOLERROR);
  1673. return 0;
  1674. }
  1675. pols[1]->curlft.use_time = get_seconds();
  1676. npols ++;
  1677. }
  1678. }
  1679. #endif
  1680. if (pol->action == XFRM_POLICY_ALLOW) {
  1681. struct sec_path *sp;
  1682. static struct sec_path dummy;
  1683. struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
  1684. struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
  1685. struct xfrm_tmpl **tpp = tp;
  1686. int ti = 0;
  1687. int i, k;
  1688. if ((sp = skb->sp) == NULL)
  1689. sp = &dummy;
  1690. for (pi = 0; pi < npols; pi++) {
  1691. if (pols[pi] != pol &&
  1692. pols[pi]->action != XFRM_POLICY_ALLOW) {
  1693. XFRM_INC_STATS(LINUX_MIB_XFRMINPOLBLOCK);
  1694. goto reject;
  1695. }
  1696. if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
  1697. XFRM_INC_STATS(LINUX_MIB_XFRMINBUFFERERROR);
  1698. goto reject_error;
  1699. }
  1700. for (i = 0; i < pols[pi]->xfrm_nr; i++)
  1701. tpp[ti++] = &pols[pi]->xfrm_vec[i];
  1702. }
  1703. xfrm_nr = ti;
  1704. if (npols > 1) {
  1705. xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
  1706. tpp = stp;
  1707. }
  1708. /* For each tunnel xfrm, find the first matching tmpl.
  1709. * For each tmpl before that, find corresponding xfrm.
  1710. * Order is _important_. Later we will implement
  1711. * some barriers, but at the moment barriers
  1712. * are implied between each two transformations.
  1713. */
  1714. for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
  1715. k = xfrm_policy_ok(tpp[i], sp, k, family);
  1716. if (k < 0) {
  1717. if (k < -1)
  1718. /* "-2 - errored_index" returned */
  1719. xerr_idx = -(2+k);
  1720. XFRM_INC_STATS(LINUX_MIB_XFRMINTMPLMISMATCH);
  1721. goto reject;
  1722. }
  1723. }
  1724. if (secpath_has_nontransport(sp, k, &xerr_idx)) {
  1725. XFRM_INC_STATS(LINUX_MIB_XFRMINTMPLMISMATCH);
  1726. goto reject;
  1727. }
  1728. xfrm_pols_put(pols, npols);
  1729. return 1;
  1730. }
  1731. XFRM_INC_STATS(LINUX_MIB_XFRMINPOLBLOCK);
  1732. reject:
  1733. xfrm_secpath_reject(xerr_idx, skb, &fl);
  1734. reject_error:
  1735. xfrm_pols_put(pols, npols);
  1736. return 0;
  1737. }
  1738. EXPORT_SYMBOL(__xfrm_policy_check);
  1739. int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
  1740. {
  1741. struct flowi fl;
  1742. if (xfrm_decode_session(skb, &fl, family) < 0) {
  1743. /* XXX: we should have something like FWDHDRERROR here. */
  1744. XFRM_INC_STATS(LINUX_MIB_XFRMINHDRERROR);
  1745. return 0;
  1746. }
  1747. return xfrm_lookup(&skb->dst, &fl, NULL, 0) == 0;
  1748. }
  1749. EXPORT_SYMBOL(__xfrm_route_forward);
  1750. /* Optimize later using cookies and generation ids. */
  1751. static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
  1752. {
  1753. /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
  1754. * to "-1" to force all XFRM destinations to get validated by
  1755. * dst_ops->check on every use. We do this because when a
  1756. * normal route referenced by an XFRM dst is obsoleted we do
  1757. * not go looking around for all parent referencing XFRM dsts
  1758. * so that we can invalidate them. It is just too much work.
  1759. * Instead we make the checks here on every use. For example:
  1760. *
  1761. * XFRM dst A --> IPv4 dst X
  1762. *
  1763. * X is the "xdst->route" of A (X is also the "dst->path" of A
  1764. * in this example). If X is marked obsolete, "A" will not
  1765. * notice. That's what we are validating here via the
  1766. * stale_bundle() check.
  1767. *
  1768. * When a policy's bundle is pruned, we dst_free() the XFRM
  1769. * dst which causes it's ->obsolete field to be set to a
  1770. * positive non-zero integer. If an XFRM dst has been pruned
  1771. * like this, we want to force a new route lookup.
  1772. */
  1773. if (dst->obsolete < 0 && !stale_bundle(dst))
  1774. return dst;
  1775. return NULL;
  1776. }
  1777. static int stale_bundle(struct dst_entry *dst)
  1778. {
  1779. return !xfrm_bundle_ok(NULL, (struct xfrm_dst *)dst, NULL, AF_UNSPEC, 0);
  1780. }
  1781. void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
  1782. {
  1783. while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
  1784. dst->dev = dev_net(dev)->loopback_dev;
  1785. dev_hold(dst->dev);
  1786. dev_put(dev);
  1787. }
  1788. }
  1789. EXPORT_SYMBOL(xfrm_dst_ifdown);
  1790. static void xfrm_link_failure(struct sk_buff *skb)
  1791. {
  1792. /* Impossible. Such dst must be popped before reaches point of failure. */
  1793. return;
  1794. }
  1795. static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
  1796. {
  1797. if (dst) {
  1798. if (dst->obsolete) {
  1799. dst_release(dst);
  1800. dst = NULL;
  1801. }
  1802. }
  1803. return dst;
  1804. }
  1805. static void prune_one_bundle(struct xfrm_policy *pol, int (*func)(struct dst_entry *), struct dst_entry **gc_list_p)
  1806. {
  1807. struct dst_entry *dst, **dstp;
  1808. write_lock(&pol->lock);
  1809. dstp = &pol->bundles;
  1810. while ((dst=*dstp) != NULL) {
  1811. if (func(dst)) {
  1812. *dstp = dst->next;
  1813. dst->next = *gc_list_p;
  1814. *gc_list_p = dst;
  1815. } else {
  1816. dstp = &dst->next;
  1817. }
  1818. }
  1819. write_unlock(&pol->lock);
  1820. }
  1821. static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
  1822. {
  1823. struct dst_entry *gc_list = NULL;
  1824. int dir;
  1825. read_lock_bh(&xfrm_policy_lock);
  1826. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  1827. struct xfrm_policy *pol;
  1828. struct hlist_node *entry;
  1829. struct hlist_head *table;
  1830. int i;
  1831. hlist_for_each_entry(pol, entry,
  1832. &xfrm_policy_inexact[dir], bydst)
  1833. prune_one_bundle(pol, func, &gc_list);
  1834. table = xfrm_policy_bydst[dir].table;
  1835. for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
  1836. hlist_for_each_entry(pol, entry, table + i, bydst)
  1837. prune_one_bundle(pol, func, &gc_list);
  1838. }
  1839. }
  1840. read_unlock_bh(&xfrm_policy_lock);
  1841. while (gc_list) {
  1842. struct dst_entry *dst = gc_list;
  1843. gc_list = dst->next;
  1844. dst_free(dst);
  1845. }
  1846. }
  1847. static int unused_bundle(struct dst_entry *dst)
  1848. {
  1849. return !atomic_read(&dst->__refcnt);
  1850. }
  1851. static void __xfrm_garbage_collect(void)
  1852. {
  1853. xfrm_prune_bundles(unused_bundle);
  1854. }
  1855. static int xfrm_flush_bundles(void)
  1856. {
  1857. xfrm_prune_bundles(stale_bundle);
  1858. return 0;
  1859. }
  1860. static void xfrm_init_pmtu(struct dst_entry *dst)
  1861. {
  1862. do {
  1863. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1864. u32 pmtu, route_mtu_cached;
  1865. pmtu = dst_mtu(dst->child);
  1866. xdst->child_mtu_cached = pmtu;
  1867. pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
  1868. route_mtu_cached = dst_mtu(xdst->route);
  1869. xdst->route_mtu_cached = route_mtu_cached;
  1870. if (pmtu > route_mtu_cached)
  1871. pmtu = route_mtu_cached;
  1872. dst->metrics[RTAX_MTU-1] = pmtu;
  1873. } while ((dst = dst->next));
  1874. }
  1875. /* Check that the bundle accepts the flow and its components are
  1876. * still valid.
  1877. */
  1878. int xfrm_bundle_ok(struct xfrm_policy *pol, struct xfrm_dst *first,
  1879. struct flowi *fl, int family, int strict)
  1880. {
  1881. struct dst_entry *dst = &first->u.dst;
  1882. struct xfrm_dst *last;
  1883. u32 mtu;
  1884. if (!dst_check(dst->path, ((struct xfrm_dst *)dst)->path_cookie) ||
  1885. (dst->dev && !netif_running(dst->dev)))
  1886. return 0;
  1887. #ifdef CONFIG_XFRM_SUB_POLICY
  1888. if (fl) {
  1889. if (first->origin && !flow_cache_uli_match(first->origin, fl))
  1890. return 0;
  1891. if (first->partner &&
  1892. !xfrm_selector_match(first->partner, fl, family))
  1893. return 0;
  1894. }
  1895. #endif
  1896. last = NULL;
  1897. do {
  1898. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  1899. if (fl && !xfrm_selector_match(&dst->xfrm->sel, fl, family))
  1900. return 0;
  1901. if (fl && pol &&
  1902. !security_xfrm_state_pol_flow_match(dst->xfrm, pol, fl))
  1903. return 0;
  1904. if (dst->xfrm->km.state != XFRM_STATE_VALID)
  1905. return 0;
  1906. if (xdst->genid != dst->xfrm->genid)
  1907. return 0;
  1908. if (strict && fl &&
  1909. !(dst->xfrm->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
  1910. !xfrm_state_addr_flow_check(dst->xfrm, fl, family))
  1911. return 0;
  1912. mtu = dst_mtu(dst->child);
  1913. if (xdst->child_mtu_cached != mtu) {
  1914. last = xdst;
  1915. xdst->child_mtu_cached = mtu;
  1916. }
  1917. if (!dst_check(xdst->route, xdst->route_cookie))
  1918. return 0;
  1919. mtu = dst_mtu(xdst->route);
  1920. if (xdst->route_mtu_cached != mtu) {
  1921. last = xdst;
  1922. xdst->route_mtu_cached = mtu;
  1923. }
  1924. dst = dst->child;
  1925. } while (dst->xfrm);
  1926. if (likely(!last))
  1927. return 1;
  1928. mtu = last->child_mtu_cached;
  1929. for (;;) {
  1930. dst = &last->u.dst;
  1931. mtu = xfrm_state_mtu(dst->xfrm, mtu);
  1932. if (mtu > last->route_mtu_cached)
  1933. mtu = last->route_mtu_cached;
  1934. dst->metrics[RTAX_MTU-1] = mtu;
  1935. if (last == first)
  1936. break;
  1937. last = (struct xfrm_dst *)last->u.dst.next;
  1938. last->child_mtu_cached = mtu;
  1939. }
  1940. return 1;
  1941. }
  1942. EXPORT_SYMBOL(xfrm_bundle_ok);
  1943. int xfrm_policy_register_afinfo(struct xfrm_policy_afinfo *afinfo)
  1944. {
  1945. int err = 0;
  1946. if (unlikely(afinfo == NULL))
  1947. return -EINVAL;
  1948. if (unlikely(afinfo->family >= NPROTO))
  1949. return -EAFNOSUPPORT;
  1950. write_lock_bh(&xfrm_policy_afinfo_lock);
  1951. if (unlikely(xfrm_policy_afinfo[afinfo->family] != NULL))
  1952. err = -ENOBUFS;
  1953. else {
  1954. struct dst_ops *dst_ops = afinfo->dst_ops;
  1955. if (likely(dst_ops->kmem_cachep == NULL))
  1956. dst_ops->kmem_cachep = xfrm_dst_cache;
  1957. if (likely(dst_ops->check == NULL))
  1958. dst_ops->check = xfrm_dst_check;
  1959. if (likely(dst_ops->negative_advice == NULL))
  1960. dst_ops->negative_advice = xfrm_negative_advice;
  1961. if (likely(dst_ops->link_failure == NULL))
  1962. dst_ops->link_failure = xfrm_link_failure;
  1963. if (likely(afinfo->garbage_collect == NULL))
  1964. afinfo->garbage_collect = __xfrm_garbage_collect;
  1965. xfrm_policy_afinfo[afinfo->family] = afinfo;
  1966. }
  1967. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1968. return err;
  1969. }
  1970. EXPORT_SYMBOL(xfrm_policy_register_afinfo);
  1971. int xfrm_policy_unregister_afinfo(struct xfrm_policy_afinfo *afinfo)
  1972. {
  1973. int err = 0;
  1974. if (unlikely(afinfo == NULL))
  1975. return -EINVAL;
  1976. if (unlikely(afinfo->family >= NPROTO))
  1977. return -EAFNOSUPPORT;
  1978. write_lock_bh(&xfrm_policy_afinfo_lock);
  1979. if (likely(xfrm_policy_afinfo[afinfo->family] != NULL)) {
  1980. if (unlikely(xfrm_policy_afinfo[afinfo->family] != afinfo))
  1981. err = -EINVAL;
  1982. else {
  1983. struct dst_ops *dst_ops = afinfo->dst_ops;
  1984. xfrm_policy_afinfo[afinfo->family] = NULL;
  1985. dst_ops->kmem_cachep = NULL;
  1986. dst_ops->check = NULL;
  1987. dst_ops->negative_advice = NULL;
  1988. dst_ops->link_failure = NULL;
  1989. afinfo->garbage_collect = NULL;
  1990. }
  1991. }
  1992. write_unlock_bh(&xfrm_policy_afinfo_lock);
  1993. return err;
  1994. }
  1995. EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
  1996. static struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
  1997. {
  1998. struct xfrm_policy_afinfo *afinfo;
  1999. if (unlikely(family >= NPROTO))
  2000. return NULL;
  2001. read_lock(&xfrm_policy_afinfo_lock);
  2002. afinfo = xfrm_policy_afinfo[family];
  2003. if (unlikely(!afinfo))
  2004. read_unlock(&xfrm_policy_afinfo_lock);
  2005. return afinfo;
  2006. }
  2007. static void xfrm_policy_put_afinfo(struct xfrm_policy_afinfo *afinfo)
  2008. {
  2009. read_unlock(&xfrm_policy_afinfo_lock);
  2010. }
  2011. static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void *ptr)
  2012. {
  2013. struct net_device *dev = ptr;
  2014. if (!net_eq(dev_net(dev), &init_net))
  2015. return NOTIFY_DONE;
  2016. switch (event) {
  2017. case NETDEV_DOWN:
  2018. xfrm_flush_bundles();
  2019. }
  2020. return NOTIFY_DONE;
  2021. }
  2022. static struct notifier_block xfrm_dev_notifier = {
  2023. xfrm_dev_event,
  2024. NULL,
  2025. 0
  2026. };
  2027. #ifdef CONFIG_XFRM_STATISTICS
  2028. static int __init xfrm_statistics_init(void)
  2029. {
  2030. if (snmp_mib_init((void **)xfrm_statistics,
  2031. sizeof(struct linux_xfrm_mib)) < 0)
  2032. return -ENOMEM;
  2033. return 0;
  2034. }
  2035. #endif
  2036. static void __init xfrm_policy_init(void)
  2037. {
  2038. unsigned int hmask, sz;
  2039. int dir;
  2040. xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
  2041. sizeof(struct xfrm_dst),
  2042. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
  2043. NULL);
  2044. hmask = 8 - 1;
  2045. sz = (hmask+1) * sizeof(struct hlist_head);
  2046. xfrm_policy_byidx = xfrm_hash_alloc(sz);
  2047. xfrm_idx_hmask = hmask;
  2048. if (!xfrm_policy_byidx)
  2049. panic("XFRM: failed to allocate byidx hash\n");
  2050. for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
  2051. struct xfrm_policy_hash *htab;
  2052. INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
  2053. htab = &xfrm_policy_bydst[dir];
  2054. htab->table = xfrm_hash_alloc(sz);
  2055. htab->hmask = hmask;
  2056. if (!htab->table)
  2057. panic("XFRM: failed to allocate bydst hash\n");
  2058. }
  2059. for (dir = 0; dir < XFRM_POLICY_TYPE_MAX; dir++)
  2060. INIT_LIST_HEAD(&xfrm_policy_bytype[dir]);
  2061. INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
  2062. register_netdevice_notifier(&xfrm_dev_notifier);
  2063. }
  2064. void __init xfrm_init(void)
  2065. {
  2066. #ifdef CONFIG_XFRM_STATISTICS
  2067. xfrm_statistics_init();
  2068. #endif
  2069. xfrm_state_init();
  2070. xfrm_policy_init();
  2071. xfrm_input_init();
  2072. #ifdef CONFIG_XFRM_STATISTICS
  2073. xfrm_proc_init();
  2074. #endif
  2075. }
  2076. #ifdef CONFIG_AUDITSYSCALL
  2077. static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
  2078. struct audit_buffer *audit_buf)
  2079. {
  2080. struct xfrm_sec_ctx *ctx = xp->security;
  2081. struct xfrm_selector *sel = &xp->selector;
  2082. if (ctx)
  2083. audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
  2084. ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
  2085. switch(sel->family) {
  2086. case AF_INET:
  2087. audit_log_format(audit_buf, " src=" NIPQUAD_FMT,
  2088. NIPQUAD(sel->saddr.a4));
  2089. if (sel->prefixlen_s != 32)
  2090. audit_log_format(audit_buf, " src_prefixlen=%d",
  2091. sel->prefixlen_s);
  2092. audit_log_format(audit_buf, " dst=" NIPQUAD_FMT,
  2093. NIPQUAD(sel->daddr.a4));
  2094. if (sel->prefixlen_d != 32)
  2095. audit_log_format(audit_buf, " dst_prefixlen=%d",
  2096. sel->prefixlen_d);
  2097. break;
  2098. case AF_INET6:
  2099. audit_log_format(audit_buf, " src=" NIP6_FMT,
  2100. NIP6(*(struct in6_addr *)sel->saddr.a6));
  2101. if (sel->prefixlen_s != 128)
  2102. audit_log_format(audit_buf, " src_prefixlen=%d",
  2103. sel->prefixlen_s);
  2104. audit_log_format(audit_buf, " dst=" NIP6_FMT,
  2105. NIP6(*(struct in6_addr *)sel->daddr.a6));
  2106. if (sel->prefixlen_d != 128)
  2107. audit_log_format(audit_buf, " dst_prefixlen=%d",
  2108. sel->prefixlen_d);
  2109. break;
  2110. }
  2111. }
  2112. void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
  2113. uid_t auid, u32 sessionid, u32 secid)
  2114. {
  2115. struct audit_buffer *audit_buf;
  2116. audit_buf = xfrm_audit_start("SPD-add");
  2117. if (audit_buf == NULL)
  2118. return;
  2119. xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
  2120. audit_log_format(audit_buf, " res=%u", result);
  2121. xfrm_audit_common_policyinfo(xp, audit_buf);
  2122. audit_log_end(audit_buf);
  2123. }
  2124. EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
  2125. void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
  2126. uid_t auid, u32 sessionid, u32 secid)
  2127. {
  2128. struct audit_buffer *audit_buf;
  2129. audit_buf = xfrm_audit_start("SPD-delete");
  2130. if (audit_buf == NULL)
  2131. return;
  2132. xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
  2133. audit_log_format(audit_buf, " res=%u", result);
  2134. xfrm_audit_common_policyinfo(xp, audit_buf);
  2135. audit_log_end(audit_buf);
  2136. }
  2137. EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
  2138. #endif
  2139. #ifdef CONFIG_XFRM_MIGRATE
  2140. static int xfrm_migrate_selector_match(struct xfrm_selector *sel_cmp,
  2141. struct xfrm_selector *sel_tgt)
  2142. {
  2143. if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
  2144. if (sel_tgt->family == sel_cmp->family &&
  2145. xfrm_addr_cmp(&sel_tgt->daddr, &sel_cmp->daddr,
  2146. sel_cmp->family) == 0 &&
  2147. xfrm_addr_cmp(&sel_tgt->saddr, &sel_cmp->saddr,
  2148. sel_cmp->family) == 0 &&
  2149. sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
  2150. sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
  2151. return 1;
  2152. }
  2153. } else {
  2154. if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
  2155. return 1;
  2156. }
  2157. }
  2158. return 0;
  2159. }
  2160. static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
  2161. u8 dir, u8 type)
  2162. {
  2163. struct xfrm_policy *pol, *ret = NULL;
  2164. struct hlist_node *entry;
  2165. struct hlist_head *chain;
  2166. u32 priority = ~0U;
  2167. read_lock_bh(&xfrm_policy_lock);
  2168. chain = policy_hash_direct(&sel->daddr, &sel->saddr, sel->family, dir);
  2169. hlist_for_each_entry(pol, entry, chain, bydst) {
  2170. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  2171. pol->type == type) {
  2172. ret = pol;
  2173. priority = ret->priority;
  2174. break;
  2175. }
  2176. }
  2177. chain = &xfrm_policy_inexact[dir];
  2178. hlist_for_each_entry(pol, entry, chain, bydst) {
  2179. if (xfrm_migrate_selector_match(sel, &pol->selector) &&
  2180. pol->type == type &&
  2181. pol->priority < priority) {
  2182. ret = pol;
  2183. break;
  2184. }
  2185. }
  2186. if (ret)
  2187. xfrm_pol_hold(ret);
  2188. read_unlock_bh(&xfrm_policy_lock);
  2189. return ret;
  2190. }
  2191. static int migrate_tmpl_match(struct xfrm_migrate *m, struct xfrm_tmpl *t)
  2192. {
  2193. int match = 0;
  2194. if (t->mode == m->mode && t->id.proto == m->proto &&
  2195. (m->reqid == 0 || t->reqid == m->reqid)) {
  2196. switch (t->mode) {
  2197. case XFRM_MODE_TUNNEL:
  2198. case XFRM_MODE_BEET:
  2199. if (xfrm_addr_cmp(&t->id.daddr, &m->old_daddr,
  2200. m->old_family) == 0 &&
  2201. xfrm_addr_cmp(&t->saddr, &m->old_saddr,
  2202. m->old_family) == 0) {
  2203. match = 1;
  2204. }
  2205. break;
  2206. case XFRM_MODE_TRANSPORT:
  2207. /* in case of transport mode, template does not store
  2208. any IP addresses, hence we just compare mode and
  2209. protocol */
  2210. match = 1;
  2211. break;
  2212. default:
  2213. break;
  2214. }
  2215. }
  2216. return match;
  2217. }
  2218. /* update endpoint address(es) of template(s) */
  2219. static int xfrm_policy_migrate(struct xfrm_policy *pol,
  2220. struct xfrm_migrate *m, int num_migrate)
  2221. {
  2222. struct xfrm_migrate *mp;
  2223. struct dst_entry *dst;
  2224. int i, j, n = 0;
  2225. write_lock_bh(&pol->lock);
  2226. if (unlikely(pol->dead)) {
  2227. /* target policy has been deleted */
  2228. write_unlock_bh(&pol->lock);
  2229. return -ENOENT;
  2230. }
  2231. for (i = 0; i < pol->xfrm_nr; i++) {
  2232. for (j = 0, mp = m; j < num_migrate; j++, mp++) {
  2233. if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
  2234. continue;
  2235. n++;
  2236. if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
  2237. pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
  2238. continue;
  2239. /* update endpoints */
  2240. memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
  2241. sizeof(pol->xfrm_vec[i].id.daddr));
  2242. memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
  2243. sizeof(pol->xfrm_vec[i].saddr));
  2244. pol->xfrm_vec[i].encap_family = mp->new_family;
  2245. /* flush bundles */
  2246. while ((dst = pol->bundles) != NULL) {
  2247. pol->bundles = dst->next;
  2248. dst_free(dst);
  2249. }
  2250. }
  2251. }
  2252. write_unlock_bh(&pol->lock);
  2253. if (!n)
  2254. return -ENODATA;
  2255. return 0;
  2256. }
  2257. static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
  2258. {
  2259. int i, j;
  2260. if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
  2261. return -EINVAL;
  2262. for (i = 0; i < num_migrate; i++) {
  2263. if ((xfrm_addr_cmp(&m[i].old_daddr, &m[i].new_daddr,
  2264. m[i].old_family) == 0) &&
  2265. (xfrm_addr_cmp(&m[i].old_saddr, &m[i].new_saddr,
  2266. m[i].old_family) == 0))
  2267. return -EINVAL;
  2268. if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
  2269. xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
  2270. return -EINVAL;
  2271. /* check if there is any duplicated entry */
  2272. for (j = i + 1; j < num_migrate; j++) {
  2273. if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
  2274. sizeof(m[i].old_daddr)) &&
  2275. !memcmp(&m[i].old_saddr, &m[j].old_saddr,
  2276. sizeof(m[i].old_saddr)) &&
  2277. m[i].proto == m[j].proto &&
  2278. m[i].mode == m[j].mode &&
  2279. m[i].reqid == m[j].reqid &&
  2280. m[i].old_family == m[j].old_family)
  2281. return -EINVAL;
  2282. }
  2283. }
  2284. return 0;
  2285. }
  2286. int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  2287. struct xfrm_migrate *m, int num_migrate)
  2288. {
  2289. int i, err, nx_cur = 0, nx_new = 0;
  2290. struct xfrm_policy *pol = NULL;
  2291. struct xfrm_state *x, *xc;
  2292. struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
  2293. struct xfrm_state *x_new[XFRM_MAX_DEPTH];
  2294. struct xfrm_migrate *mp;
  2295. if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
  2296. goto out;
  2297. /* Stage 1 - find policy */
  2298. if ((pol = xfrm_migrate_policy_find(sel, dir, type)) == NULL) {
  2299. err = -ENOENT;
  2300. goto out;
  2301. }
  2302. /* Stage 2 - find and update state(s) */
  2303. for (i = 0, mp = m; i < num_migrate; i++, mp++) {
  2304. if ((x = xfrm_migrate_state_find(mp))) {
  2305. x_cur[nx_cur] = x;
  2306. nx_cur++;
  2307. if ((xc = xfrm_state_migrate(x, mp))) {
  2308. x_new[nx_new] = xc;
  2309. nx_new++;
  2310. } else {
  2311. err = -ENODATA;
  2312. goto restore_state;
  2313. }
  2314. }
  2315. }
  2316. /* Stage 3 - update policy */
  2317. if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
  2318. goto restore_state;
  2319. /* Stage 4 - delete old state(s) */
  2320. if (nx_cur) {
  2321. xfrm_states_put(x_cur, nx_cur);
  2322. xfrm_states_delete(x_cur, nx_cur);
  2323. }
  2324. /* Stage 5 - announce */
  2325. km_migrate(sel, dir, type, m, num_migrate);
  2326. xfrm_pol_put(pol);
  2327. return 0;
  2328. out:
  2329. return err;
  2330. restore_state:
  2331. if (pol)
  2332. xfrm_pol_put(pol);
  2333. if (nx_cur)
  2334. xfrm_states_put(x_cur, nx_cur);
  2335. if (nx_new)
  2336. xfrm_states_delete(x_new, nx_new);
  2337. return err;
  2338. }
  2339. EXPORT_SYMBOL(xfrm_migrate);
  2340. #endif