xfrm_policy.c 63 KB

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