xfrm_policy.c 63 KB

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