xfrm_policy.c 70 KB

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