xfrm_policy.c 66 KB

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