LCOV - code coverage report
Current view: top level - net/xfrm - xfrm_policy.c (source / functions) Hit Total Coverage
Test: Real Lines: 140 1496 9.4 %
Date: 2020-10-17 15:46:16 Functions: 3 111 2.7 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-only
       2                 :            : /*
       3                 :            :  * xfrm_policy.c
       4                 :            :  *
       5                 :            :  * Changes:
       6                 :            :  *      Mitsuru KANDA @USAGI
       7                 :            :  *      Kazunori MIYAZAWA @USAGI
       8                 :            :  *      Kunihiro Ishiguro <kunihiro@ipinfusion.com>
       9                 :            :  *              IPv6 support
      10                 :            :  *      Kazunori MIYAZAWA @USAGI
      11                 :            :  *      YOSHIFUJI Hideaki
      12                 :            :  *              Split up af-specific portion
      13                 :            :  *      Derek Atkins <derek@ihtfp.com>            Add the post_input processor
      14                 :            :  *
      15                 :            :  */
      16                 :            : 
      17                 :            : #include <linux/err.h>
      18                 :            : #include <linux/slab.h>
      19                 :            : #include <linux/kmod.h>
      20                 :            : #include <linux/list.h>
      21                 :            : #include <linux/spinlock.h>
      22                 :            : #include <linux/workqueue.h>
      23                 :            : #include <linux/notifier.h>
      24                 :            : #include <linux/netdevice.h>
      25                 :            : #include <linux/netfilter.h>
      26                 :            : #include <linux/module.h>
      27                 :            : #include <linux/cache.h>
      28                 :            : #include <linux/cpu.h>
      29                 :            : #include <linux/audit.h>
      30                 :            : #include <linux/rhashtable.h>
      31                 :            : #include <linux/if_tunnel.h>
      32                 :            : #include <net/dst.h>
      33                 :            : #include <net/flow.h>
      34                 :            : #include <net/xfrm.h>
      35                 :            : #include <net/ip.h>
      36                 :            : #if IS_ENABLED(CONFIG_IPV6_MIP6)
      37                 :            : #include <net/mip6.h>
      38                 :            : #endif
      39                 :            : #ifdef CONFIG_XFRM_STATISTICS
      40                 :            : #include <net/snmp.h>
      41                 :            : #endif
      42                 :            : 
      43                 :            : #include "xfrm_hash.h"
      44                 :            : 
      45                 :            : #define XFRM_QUEUE_TMO_MIN ((unsigned)(HZ/10))
      46                 :            : #define XFRM_QUEUE_TMO_MAX ((unsigned)(60*HZ))
      47                 :            : #define XFRM_MAX_QUEUE_LEN      100
      48                 :            : 
      49                 :            : struct xfrm_flo {
      50                 :            :         struct dst_entry *dst_orig;
      51                 :            :         u8 flags;
      52                 :            : };
      53                 :            : 
      54                 :            : /* prefixes smaller than this are stored in lists, not trees. */
      55                 :            : #define INEXACT_PREFIXLEN_IPV4  16
      56                 :            : #define INEXACT_PREFIXLEN_IPV6  48
      57                 :            : 
      58                 :            : struct xfrm_pol_inexact_node {
      59                 :            :         struct rb_node node;
      60                 :            :         union {
      61                 :            :                 xfrm_address_t addr;
      62                 :            :                 struct rcu_head rcu;
      63                 :            :         };
      64                 :            :         u8 prefixlen;
      65                 :            : 
      66                 :            :         struct rb_root root;
      67                 :            : 
      68                 :            :         /* the policies matching this node, can be empty list */
      69                 :            :         struct hlist_head hhead;
      70                 :            : };
      71                 :            : 
      72                 :            : /* xfrm inexact policy search tree:
      73                 :            :  * xfrm_pol_inexact_bin = hash(dir,type,family,if_id);
      74                 :            :  *  |
      75                 :            :  * +---- root_d: sorted by daddr:prefix
      76                 :            :  * |                 |
      77                 :            :  * |        xfrm_pol_inexact_node
      78                 :            :  * |                 |
      79                 :            :  * |                 +- root: sorted by saddr/prefix
      80                 :            :  * |                 |              |
      81                 :            :  * |                 |         xfrm_pol_inexact_node
      82                 :            :  * |                 |              |
      83                 :            :  * |                 |              + root: unused
      84                 :            :  * |                 |              |
      85                 :            :  * |                 |              + hhead: saddr:daddr policies
      86                 :            :  * |                 |
      87                 :            :  * |                 +- coarse policies and all any:daddr policies
      88                 :            :  * |
      89                 :            :  * +---- root_s: sorted by saddr:prefix
      90                 :            :  * |                 |
      91                 :            :  * |        xfrm_pol_inexact_node
      92                 :            :  * |                 |
      93                 :            :  * |                 + root: unused
      94                 :            :  * |                 |
      95                 :            :  * |                 + hhead: saddr:any policies
      96                 :            :  * |
      97                 :            :  * +---- coarse policies and all any:any policies
      98                 :            :  *
      99                 :            :  * Lookups return four candidate lists:
     100                 :            :  * 1. any:any list from top-level xfrm_pol_inexact_bin
     101                 :            :  * 2. any:daddr list from daddr tree
     102                 :            :  * 3. saddr:daddr list from 2nd level daddr tree
     103                 :            :  * 4. saddr:any list from saddr tree
     104                 :            :  *
     105                 :            :  * This result set then needs to be searched for the policy with
     106                 :            :  * the lowest priority.  If two results have same prio, youngest one wins.
     107                 :            :  */
     108                 :            : 
     109                 :            : struct xfrm_pol_inexact_key {
     110                 :            :         possible_net_t net;
     111                 :            :         u32 if_id;
     112                 :            :         u16 family;
     113                 :            :         u8 dir, type;
     114                 :            : };
     115                 :            : 
     116                 :            : struct xfrm_pol_inexact_bin {
     117                 :            :         struct xfrm_pol_inexact_key k;
     118                 :            :         struct rhash_head head;
     119                 :            :         /* list containing '*:*' policies */
     120                 :            :         struct hlist_head hhead;
     121                 :            : 
     122                 :            :         seqcount_t count;
     123                 :            :         /* tree sorted by daddr/prefix */
     124                 :            :         struct rb_root root_d;
     125                 :            : 
     126                 :            :         /* tree sorted by saddr/prefix */
     127                 :            :         struct rb_root root_s;
     128                 :            : 
     129                 :            :         /* slow path below */
     130                 :            :         struct list_head inexact_bins;
     131                 :            :         struct rcu_head rcu;
     132                 :            : };
     133                 :            : 
     134                 :            : enum xfrm_pol_inexact_candidate_type {
     135                 :            :         XFRM_POL_CAND_BOTH,
     136                 :            :         XFRM_POL_CAND_SADDR,
     137                 :            :         XFRM_POL_CAND_DADDR,
     138                 :            :         XFRM_POL_CAND_ANY,
     139                 :            : 
     140                 :            :         XFRM_POL_CAND_MAX,
     141                 :            : };
     142                 :            : 
     143                 :            : struct xfrm_pol_inexact_candidates {
     144                 :            :         struct hlist_head *res[XFRM_POL_CAND_MAX];
     145                 :            : };
     146                 :            : 
     147                 :            : static DEFINE_SPINLOCK(xfrm_if_cb_lock);
     148                 :            : static struct xfrm_if_cb const __rcu *xfrm_if_cb __read_mostly;
     149                 :            : 
     150                 :            : static DEFINE_SPINLOCK(xfrm_policy_afinfo_lock);
     151                 :            : static struct xfrm_policy_afinfo const __rcu *xfrm_policy_afinfo[AF_INET6 + 1]
     152                 :            :                                                 __read_mostly;
     153                 :            : 
     154                 :            : static struct kmem_cache *xfrm_dst_cache __ro_after_init;
     155                 :            : static __read_mostly seqcount_t xfrm_policy_hash_generation;
     156                 :            : 
     157                 :            : static struct rhashtable xfrm_policy_inexact_table;
     158                 :            : static const struct rhashtable_params xfrm_pol_inexact_params;
     159                 :            : 
     160                 :            : static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr);
     161                 :            : static int stale_bundle(struct dst_entry *dst);
     162                 :            : static int xfrm_bundle_ok(struct xfrm_dst *xdst);
     163                 :            : static void xfrm_policy_queue_process(struct timer_list *t);
     164                 :            : 
     165                 :            : static void __xfrm_policy_link(struct xfrm_policy *pol, int dir);
     166                 :            : static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
     167                 :            :                                                 int dir);
     168                 :            : 
     169                 :            : static struct xfrm_pol_inexact_bin *
     170                 :            : xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family, u8 dir,
     171                 :            :                            u32 if_id);
     172                 :            : 
     173                 :            : static struct xfrm_pol_inexact_bin *
     174                 :            : xfrm_policy_inexact_lookup_rcu(struct net *net,
     175                 :            :                                u8 type, u16 family, u8 dir, u32 if_id);
     176                 :            : static struct xfrm_policy *
     177                 :            : xfrm_policy_insert_list(struct hlist_head *chain, struct xfrm_policy *policy,
     178                 :            :                         bool excl);
     179                 :            : static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
     180                 :            :                                             struct xfrm_policy *policy);
     181                 :            : 
     182                 :            : static bool
     183                 :            : xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand,
     184                 :            :                                     struct xfrm_pol_inexact_bin *b,
     185                 :            :                                     const xfrm_address_t *saddr,
     186                 :            :                                     const xfrm_address_t *daddr);
     187                 :            : 
     188                 :            : static inline bool xfrm_pol_hold_rcu(struct xfrm_policy *policy)
     189                 :            : {
     190                 :          0 :         return refcount_inc_not_zero(&policy->refcnt);
     191                 :            : }
     192                 :            : 
     193                 :            : static inline bool
     194                 :          0 : __xfrm4_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
     195                 :            : {
     196                 :            :         const struct flowi4 *fl4 = &fl->u.ip4;
     197                 :            : 
     198                 :          0 :         return  addr4_match(fl4->daddr, sel->daddr.a4, sel->prefixlen_d) &&
     199                 :          0 :                 addr4_match(fl4->saddr, sel->saddr.a4, sel->prefixlen_s) &&
     200                 :          0 :                 !((xfrm_flowi_dport(fl, &fl4->uli) ^ sel->dport) & sel->dport_mask) &&
     201                 :          0 :                 !((xfrm_flowi_sport(fl, &fl4->uli) ^ sel->sport) & sel->sport_mask) &&
     202                 :          0 :                 (fl4->flowi4_proto == sel->proto || !sel->proto) &&
     203                 :          0 :                 (fl4->flowi4_oif == sel->ifindex || !sel->ifindex);
     204                 :            : }
     205                 :            : 
     206                 :            : static inline bool
     207                 :          0 : __xfrm6_selector_match(const struct xfrm_selector *sel, const struct flowi *fl)
     208                 :            : {
     209                 :            :         const struct flowi6 *fl6 = &fl->u.ip6;
     210                 :            : 
     211                 :          0 :         return  addr_match(&fl6->daddr, &sel->daddr, sel->prefixlen_d) &&
     212                 :          0 :                 addr_match(&fl6->saddr, &sel->saddr, sel->prefixlen_s) &&
     213                 :          0 :                 !((xfrm_flowi_dport(fl, &fl6->uli) ^ sel->dport) & sel->dport_mask) &&
     214                 :          0 :                 !((xfrm_flowi_sport(fl, &fl6->uli) ^ sel->sport) & sel->sport_mask) &&
     215                 :          0 :                 (fl6->flowi6_proto == sel->proto || !sel->proto) &&
     216                 :          0 :                 (fl6->flowi6_oif == sel->ifindex || !sel->ifindex);
     217                 :            : }
     218                 :            : 
     219                 :          0 : bool xfrm_selector_match(const struct xfrm_selector *sel, const struct flowi *fl,
     220                 :            :                          unsigned short family)
     221                 :            : {
     222                 :          0 :         switch (family) {
     223                 :            :         case AF_INET:
     224                 :          0 :                 return __xfrm4_selector_match(sel, fl);
     225                 :            :         case AF_INET6:
     226                 :          0 :                 return __xfrm6_selector_match(sel, fl);
     227                 :            :         }
     228                 :            :         return false;
     229                 :            : }
     230                 :            : 
     231                 :            : static const struct xfrm_policy_afinfo *xfrm_policy_get_afinfo(unsigned short family)
     232                 :            : {
     233                 :            :         const struct xfrm_policy_afinfo *afinfo;
     234                 :            : 
     235                 :          0 :         if (unlikely(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
     236                 :            :                 return NULL;
     237                 :            :         rcu_read_lock();
     238                 :          0 :         afinfo = rcu_dereference(xfrm_policy_afinfo[family]);
     239                 :          0 :         if (unlikely(!afinfo))
     240                 :            :                 rcu_read_unlock();
     241                 :            :         return afinfo;
     242                 :            : }
     243                 :            : 
     244                 :            : /* Called with rcu_read_lock(). */
     245                 :            : static const struct xfrm_if_cb *xfrm_if_get_cb(void)
     246                 :            : {
     247                 :          0 :         return rcu_dereference(xfrm_if_cb);
     248                 :            : }
     249                 :            : 
     250                 :          0 : struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
     251                 :            :                                     const xfrm_address_t *saddr,
     252                 :            :                                     const xfrm_address_t *daddr,
     253                 :            :                                     int family, u32 mark)
     254                 :            : {
     255                 :            :         const struct xfrm_policy_afinfo *afinfo;
     256                 :            :         struct dst_entry *dst;
     257                 :            : 
     258                 :          0 :         afinfo = xfrm_policy_get_afinfo(family);
     259                 :          0 :         if (unlikely(afinfo == NULL))
     260                 :            :                 return ERR_PTR(-EAFNOSUPPORT);
     261                 :            : 
     262                 :          0 :         dst = afinfo->dst_lookup(net, tos, oif, saddr, daddr, mark);
     263                 :            : 
     264                 :            :         rcu_read_unlock();
     265                 :            : 
     266                 :          0 :         return dst;
     267                 :            : }
     268                 :            : EXPORT_SYMBOL(__xfrm_dst_lookup);
     269                 :            : 
     270                 :          0 : static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
     271                 :            :                                                 int tos, int oif,
     272                 :            :                                                 xfrm_address_t *prev_saddr,
     273                 :            :                                                 xfrm_address_t *prev_daddr,
     274                 :            :                                                 int family, u32 mark)
     275                 :            : {
     276                 :            :         struct net *net = xs_net(x);
     277                 :          0 :         xfrm_address_t *saddr = &x->props.saddr;
     278                 :          0 :         xfrm_address_t *daddr = &x->id.daddr;
     279                 :            :         struct dst_entry *dst;
     280                 :            : 
     281                 :          0 :         if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
     282                 :          0 :                 saddr = x->coaddr;
     283                 :            :                 daddr = prev_daddr;
     284                 :            :         }
     285                 :          0 :         if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
     286                 :            :                 saddr = prev_saddr;
     287                 :          0 :                 daddr = x->coaddr;
     288                 :            :         }
     289                 :            : 
     290                 :          0 :         dst = __xfrm_dst_lookup(net, tos, oif, saddr, daddr, family, mark);
     291                 :            : 
     292                 :          0 :         if (!IS_ERR(dst)) {
     293                 :          0 :                 if (prev_saddr != saddr)
     294                 :          0 :                         memcpy(prev_saddr, saddr,  sizeof(*prev_saddr));
     295                 :          0 :                 if (prev_daddr != daddr)
     296                 :          0 :                         memcpy(prev_daddr, daddr,  sizeof(*prev_daddr));
     297                 :            :         }
     298                 :            : 
     299                 :          0 :         return dst;
     300                 :            : }
     301                 :            : 
     302                 :            : static inline unsigned long make_jiffies(long secs)
     303                 :            : {
     304                 :          0 :         if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
     305                 :            :                 return MAX_SCHEDULE_TIMEOUT-1;
     306                 :            :         else
     307                 :          0 :                 return secs*HZ;
     308                 :            : }
     309                 :            : 
     310                 :          0 : static void xfrm_policy_timer(struct timer_list *t)
     311                 :            : {
     312                 :          0 :         struct xfrm_policy *xp = from_timer(xp, t, timer);
     313                 :          0 :         time64_t now = ktime_get_real_seconds();
     314                 :            :         time64_t next = TIME64_MAX;
     315                 :            :         int warn = 0;
     316                 :            :         int dir;
     317                 :            : 
     318                 :          0 :         read_lock(&xp->lock);
     319                 :            : 
     320                 :          0 :         if (unlikely(xp->walk.dead))
     321                 :            :                 goto out;
     322                 :            : 
     323                 :          0 :         dir = xfrm_policy_id2dir(xp->index);
     324                 :            : 
     325                 :          0 :         if (xp->lft.hard_add_expires_seconds) {
     326                 :          0 :                 time64_t tmo = xp->lft.hard_add_expires_seconds +
     327                 :          0 :                         xp->curlft.add_time - now;
     328                 :          0 :                 if (tmo <= 0)
     329                 :            :                         goto expired;
     330                 :          0 :                 if (tmo < next)
     331                 :            :                         next = tmo;
     332                 :            :         }
     333                 :          0 :         if (xp->lft.hard_use_expires_seconds) {
     334                 :          0 :                 time64_t tmo = xp->lft.hard_use_expires_seconds +
     335                 :          0 :                         (xp->curlft.use_time ? : xp->curlft.add_time) - now;
     336                 :          0 :                 if (tmo <= 0)
     337                 :            :                         goto expired;
     338                 :          0 :                 if (tmo < next)
     339                 :            :                         next = tmo;
     340                 :            :         }
     341                 :          0 :         if (xp->lft.soft_add_expires_seconds) {
     342                 :          0 :                 time64_t tmo = xp->lft.soft_add_expires_seconds +
     343                 :          0 :                         xp->curlft.add_time - now;
     344                 :          0 :                 if (tmo <= 0) {
     345                 :            :                         warn = 1;
     346                 :            :                         tmo = XFRM_KM_TIMEOUT;
     347                 :            :                 }
     348                 :          0 :                 if (tmo < next)
     349                 :            :                         next = tmo;
     350                 :            :         }
     351                 :          0 :         if (xp->lft.soft_use_expires_seconds) {
     352                 :          0 :                 time64_t tmo = xp->lft.soft_use_expires_seconds +
     353                 :          0 :                         (xp->curlft.use_time ? : xp->curlft.add_time) - now;
     354                 :          0 :                 if (tmo <= 0) {
     355                 :            :                         warn = 1;
     356                 :            :                         tmo = XFRM_KM_TIMEOUT;
     357                 :            :                 }
     358                 :          0 :                 if (tmo < next)
     359                 :            :                         next = tmo;
     360                 :            :         }
     361                 :            : 
     362                 :          0 :         if (warn)
     363                 :          0 :                 km_policy_expired(xp, dir, 0, 0);
     364                 :          0 :         if (next != TIME64_MAX &&
     365                 :          0 :             !mod_timer(&xp->timer, jiffies + make_jiffies(next)))
     366                 :            :                 xfrm_pol_hold(xp);
     367                 :            : 
     368                 :            : out:
     369                 :            :         read_unlock(&xp->lock);
     370                 :          0 :         xfrm_pol_put(xp);
     371                 :          0 :         return;
     372                 :            : 
     373                 :            : expired:
     374                 :            :         read_unlock(&xp->lock);
     375                 :          0 :         if (!xfrm_policy_delete(xp, dir))
     376                 :          0 :                 km_policy_expired(xp, dir, 1, 0);
     377                 :          0 :         xfrm_pol_put(xp);
     378                 :            : }
     379                 :            : 
     380                 :            : /* Allocate xfrm_policy. Not used here, it is supposed to be used by pfkeyv2
     381                 :            :  * SPD calls.
     382                 :            :  */
     383                 :            : 
     384                 :          0 : struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
     385                 :            : {
     386                 :            :         struct xfrm_policy *policy;
     387                 :            : 
     388                 :          0 :         policy = kzalloc(sizeof(struct xfrm_policy), gfp);
     389                 :            : 
     390                 :          0 :         if (policy) {
     391                 :            :                 write_pnet(&policy->xp_net, net);
     392                 :          0 :                 INIT_LIST_HEAD(&policy->walk.all);
     393                 :            :                 INIT_HLIST_NODE(&policy->bydst_inexact_list);
     394                 :            :                 INIT_HLIST_NODE(&policy->bydst);
     395                 :            :                 INIT_HLIST_NODE(&policy->byidx);
     396                 :          0 :                 rwlock_init(&policy->lock);
     397                 :            :                 refcount_set(&policy->refcnt, 1);
     398                 :          0 :                 skb_queue_head_init(&policy->polq.hold_queue);
     399                 :          0 :                 timer_setup(&policy->timer, xfrm_policy_timer, 0);
     400                 :          0 :                 timer_setup(&policy->polq.hold_timer,
     401                 :            :                             xfrm_policy_queue_process, 0);
     402                 :            :         }
     403                 :          0 :         return policy;
     404                 :            : }
     405                 :            : EXPORT_SYMBOL(xfrm_policy_alloc);
     406                 :            : 
     407                 :          0 : static void xfrm_policy_destroy_rcu(struct rcu_head *head)
     408                 :            : {
     409                 :          0 :         struct xfrm_policy *policy = container_of(head, struct xfrm_policy, rcu);
     410                 :            : 
     411                 :            :         security_xfrm_policy_free(policy->security);
     412                 :          0 :         kfree(policy);
     413                 :          0 : }
     414                 :            : 
     415                 :            : /* Destroy xfrm_policy: descendant resources must be released to this moment. */
     416                 :            : 
     417                 :          0 : void xfrm_policy_destroy(struct xfrm_policy *policy)
     418                 :            : {
     419                 :          0 :         BUG_ON(!policy->walk.dead);
     420                 :            : 
     421                 :          0 :         if (del_timer(&policy->timer) || del_timer(&policy->polq.hold_timer))
     422                 :          0 :                 BUG();
     423                 :            : 
     424                 :          0 :         call_rcu(&policy->rcu, xfrm_policy_destroy_rcu);
     425                 :          0 : }
     426                 :            : EXPORT_SYMBOL(xfrm_policy_destroy);
     427                 :            : 
     428                 :            : /* Rule must be locked. Release descendant resources, announce
     429                 :            :  * entry dead. The rule must be unlinked from lists to the moment.
     430                 :            :  */
     431                 :            : 
     432                 :          0 : static void xfrm_policy_kill(struct xfrm_policy *policy)
     433                 :            : {
     434                 :          0 :         write_lock_bh(&policy->lock);
     435                 :          0 :         policy->walk.dead = 1;
     436                 :          0 :         write_unlock_bh(&policy->lock);
     437                 :            : 
     438                 :          0 :         atomic_inc(&policy->genid);
     439                 :            : 
     440                 :          0 :         if (del_timer(&policy->polq.hold_timer))
     441                 :          0 :                 xfrm_pol_put(policy);
     442                 :          0 :         skb_queue_purge(&policy->polq.hold_queue);
     443                 :            : 
     444                 :          0 :         if (del_timer(&policy->timer))
     445                 :          0 :                 xfrm_pol_put(policy);
     446                 :            : 
     447                 :          0 :         xfrm_pol_put(policy);
     448                 :          0 : }
     449                 :            : 
     450                 :            : static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
     451                 :            : 
     452                 :            : static inline unsigned int idx_hash(struct net *net, u32 index)
     453                 :            : {
     454                 :          0 :         return __idx_hash(index, net->xfrm.policy_idx_hmask);
     455                 :            : }
     456                 :            : 
     457                 :            : /* calculate policy hash thresholds */
     458                 :            : static void __get_hash_thresh(struct net *net,
     459                 :            :                               unsigned short family, int dir,
     460                 :            :                               u8 *dbits, u8 *sbits)
     461                 :            : {
     462                 :          0 :         switch (family) {
     463                 :            :         case AF_INET:
     464                 :          0 :                 *dbits = net->xfrm.policy_bydst[dir].dbits4;
     465                 :          0 :                 *sbits = net->xfrm.policy_bydst[dir].sbits4;
     466                 :            :                 break;
     467                 :            : 
     468                 :            :         case AF_INET6:
     469                 :          0 :                 *dbits = net->xfrm.policy_bydst[dir].dbits6;
     470                 :          0 :                 *sbits = net->xfrm.policy_bydst[dir].sbits6;
     471                 :            :                 break;
     472                 :            : 
     473                 :            :         default:
     474                 :            :                 *dbits = 0;
     475                 :            :                 *sbits = 0;
     476                 :            :         }
     477                 :            : }
     478                 :            : 
     479                 :          0 : static struct hlist_head *policy_hash_bysel(struct net *net,
     480                 :            :                                             const struct xfrm_selector *sel,
     481                 :            :                                             unsigned short family, int dir)
     482                 :            : {
     483                 :          0 :         unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
     484                 :            :         unsigned int hash;
     485                 :            :         u8 dbits;
     486                 :            :         u8 sbits;
     487                 :            : 
     488                 :            :         __get_hash_thresh(net, family, dir, &dbits, &sbits);
     489                 :          0 :         hash = __sel_hash(sel, family, hmask, dbits, sbits);
     490                 :            : 
     491                 :          0 :         if (hash == hmask + 1)
     492                 :            :                 return NULL;
     493                 :            : 
     494                 :          0 :         return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
     495                 :          0 :                      lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
     496                 :            : }
     497                 :            : 
     498                 :          0 : static struct hlist_head *policy_hash_direct(struct net *net,
     499                 :            :                                              const xfrm_address_t *daddr,
     500                 :            :                                              const xfrm_address_t *saddr,
     501                 :            :                                              unsigned short family, int dir)
     502                 :            : {
     503                 :          0 :         unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
     504                 :            :         unsigned int hash;
     505                 :            :         u8 dbits;
     506                 :            :         u8 sbits;
     507                 :            : 
     508                 :            :         __get_hash_thresh(net, family, dir, &dbits, &sbits);
     509                 :          0 :         hash = __addr_hash(daddr, saddr, family, hmask, dbits, sbits);
     510                 :            : 
     511                 :          0 :         return rcu_dereference_check(net->xfrm.policy_bydst[dir].table,
     512                 :          0 :                      lockdep_is_held(&net->xfrm.xfrm_policy_lock)) + hash;
     513                 :            : }
     514                 :            : 
     515                 :          0 : static void xfrm_dst_hash_transfer(struct net *net,
     516                 :            :                                    struct hlist_head *list,
     517                 :            :                                    struct hlist_head *ndsttable,
     518                 :            :                                    unsigned int nhashmask,
     519                 :            :                                    int dir)
     520                 :            : {
     521                 :            :         struct hlist_node *tmp, *entry0 = NULL;
     522                 :            :         struct xfrm_policy *pol;
     523                 :            :         unsigned int h0 = 0;
     524                 :            :         u8 dbits;
     525                 :            :         u8 sbits;
     526                 :            : 
     527                 :            : redo:
     528                 :          0 :         hlist_for_each_entry_safe(pol, tmp, list, bydst) {
     529                 :            :                 unsigned int h;
     530                 :            : 
     531                 :          0 :                 __get_hash_thresh(net, pol->family, dir, &dbits, &sbits);
     532                 :          0 :                 h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
     533                 :            :                                 pol->family, nhashmask, dbits, sbits);
     534                 :          0 :                 if (!entry0) {
     535                 :            :                         hlist_del_rcu(&pol->bydst);
     536                 :          0 :                         hlist_add_head_rcu(&pol->bydst, ndsttable + h);
     537                 :            :                         h0 = h;
     538                 :            :                 } else {
     539                 :          0 :                         if (h != h0)
     540                 :          0 :                                 continue;
     541                 :            :                         hlist_del_rcu(&pol->bydst);
     542                 :          0 :                         hlist_add_behind_rcu(&pol->bydst, entry0);
     543                 :            :                 }
     544                 :          0 :                 entry0 = &pol->bydst;
     545                 :            :         }
     546                 :          0 :         if (!hlist_empty(list)) {
     547                 :            :                 entry0 = NULL;
     548                 :            :                 goto redo;
     549                 :            :         }
     550                 :          0 : }
     551                 :            : 
     552                 :          0 : static void xfrm_idx_hash_transfer(struct hlist_head *list,
     553                 :            :                                    struct hlist_head *nidxtable,
     554                 :            :                                    unsigned int nhashmask)
     555                 :            : {
     556                 :            :         struct hlist_node *tmp;
     557                 :            :         struct xfrm_policy *pol;
     558                 :            : 
     559                 :          0 :         hlist_for_each_entry_safe(pol, tmp, list, byidx) {
     560                 :            :                 unsigned int h;
     561                 :            : 
     562                 :          0 :                 h = __idx_hash(pol->index, nhashmask);
     563                 :          0 :                 hlist_add_head(&pol->byidx, nidxtable+h);
     564                 :            :         }
     565                 :          0 : }
     566                 :            : 
     567                 :            : static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
     568                 :            : {
     569                 :          0 :         return ((old_hmask + 1) << 1) - 1;
     570                 :            : }
     571                 :            : 
     572                 :          0 : static void xfrm_bydst_resize(struct net *net, int dir)
     573                 :            : {
     574                 :          0 :         unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
     575                 :            :         unsigned int nhashmask = xfrm_new_hash_mask(hmask);
     576                 :          0 :         unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
     577                 :          0 :         struct hlist_head *ndst = xfrm_hash_alloc(nsize);
     578                 :            :         struct hlist_head *odst;
     579                 :            :         int i;
     580                 :            : 
     581                 :          0 :         if (!ndst)
     582                 :          0 :                 return;
     583                 :            : 
     584                 :            :         spin_lock_bh(&net->xfrm.xfrm_policy_lock);
     585                 :            :         write_seqcount_begin(&xfrm_policy_hash_generation);
     586                 :            : 
     587                 :          0 :         odst = rcu_dereference_protected(net->xfrm.policy_bydst[dir].table,
     588                 :            :                                 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
     589                 :            : 
     590                 :          0 :         for (i = hmask; i >= 0; i--)
     591                 :          0 :                 xfrm_dst_hash_transfer(net, odst + i, ndst, nhashmask, dir);
     592                 :            : 
     593                 :          0 :         rcu_assign_pointer(net->xfrm.policy_bydst[dir].table, ndst);
     594                 :          0 :         net->xfrm.policy_bydst[dir].hmask = nhashmask;
     595                 :            : 
     596                 :            :         write_seqcount_end(&xfrm_policy_hash_generation);
     597                 :            :         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
     598                 :            : 
     599                 :          0 :         synchronize_rcu();
     600                 :            : 
     601                 :          0 :         xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
     602                 :            : }
     603                 :            : 
     604                 :          0 : static void xfrm_byidx_resize(struct net *net, int total)
     605                 :            : {
     606                 :          0 :         unsigned int hmask = net->xfrm.policy_idx_hmask;
     607                 :            :         unsigned int nhashmask = xfrm_new_hash_mask(hmask);
     608                 :          0 :         unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
     609                 :          0 :         struct hlist_head *oidx = net->xfrm.policy_byidx;
     610                 :          0 :         struct hlist_head *nidx = xfrm_hash_alloc(nsize);
     611                 :            :         int i;
     612                 :            : 
     613                 :          0 :         if (!nidx)
     614                 :          0 :                 return;
     615                 :            : 
     616                 :            :         spin_lock_bh(&net->xfrm.xfrm_policy_lock);
     617                 :            : 
     618                 :          0 :         for (i = hmask; i >= 0; i--)
     619                 :          0 :                 xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
     620                 :            : 
     621                 :          0 :         net->xfrm.policy_byidx = nidx;
     622                 :          0 :         net->xfrm.policy_idx_hmask = nhashmask;
     623                 :            : 
     624                 :            :         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
     625                 :            : 
     626                 :          0 :         xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
     627                 :            : }
     628                 :            : 
     629                 :            : static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
     630                 :            : {
     631                 :          0 :         unsigned int cnt = net->xfrm.policy_count[dir];
     632                 :          0 :         unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
     633                 :            : 
     634                 :            :         if (total)
     635                 :          0 :                 *total += cnt;
     636                 :            : 
     637                 :          0 :         if ((hmask + 1) < xfrm_policy_hashmax &&
     638                 :            :             cnt > hmask)
     639                 :            :                 return 1;
     640                 :            : 
     641                 :            :         return 0;
     642                 :            : }
     643                 :            : 
     644                 :            : static inline int xfrm_byidx_should_resize(struct net *net, int total)
     645                 :            : {
     646                 :          0 :         unsigned int hmask = net->xfrm.policy_idx_hmask;
     647                 :            : 
     648                 :          0 :         if ((hmask + 1) < xfrm_policy_hashmax &&
     649                 :          0 :             total > hmask)
     650                 :            :                 return 1;
     651                 :            : 
     652                 :            :         return 0;
     653                 :            : }
     654                 :            : 
     655                 :          0 : void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si)
     656                 :            : {
     657                 :          0 :         si->incnt = net->xfrm.policy_count[XFRM_POLICY_IN];
     658                 :          0 :         si->outcnt = net->xfrm.policy_count[XFRM_POLICY_OUT];
     659                 :          0 :         si->fwdcnt = net->xfrm.policy_count[XFRM_POLICY_FWD];
     660                 :          0 :         si->inscnt = net->xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
     661                 :          0 :         si->outscnt = net->xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
     662                 :          0 :         si->fwdscnt = net->xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
     663                 :          0 :         si->spdhcnt = net->xfrm.policy_idx_hmask;
     664                 :          0 :         si->spdhmcnt = xfrm_policy_hashmax;
     665                 :          0 : }
     666                 :            : EXPORT_SYMBOL(xfrm_spd_getinfo);
     667                 :            : 
     668                 :            : static DEFINE_MUTEX(hash_resize_mutex);
     669                 :          0 : static void xfrm_hash_resize(struct work_struct *work)
     670                 :            : {
     671                 :          0 :         struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
     672                 :            :         int dir, total;
     673                 :            : 
     674                 :          0 :         mutex_lock(&hash_resize_mutex);
     675                 :            : 
     676                 :            :         total = 0;
     677                 :          0 :         for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
     678                 :          0 :                 if (xfrm_bydst_should_resize(net, dir, &total))
     679                 :          0 :                         xfrm_bydst_resize(net, dir);
     680                 :            :         }
     681                 :          0 :         if (xfrm_byidx_should_resize(net, total))
     682                 :          0 :                 xfrm_byidx_resize(net, total);
     683                 :            : 
     684                 :          0 :         mutex_unlock(&hash_resize_mutex);
     685                 :          0 : }
     686                 :            : 
     687                 :            : /* Make sure *pol can be inserted into fastbin.
     688                 :            :  * Useful to check that later insert requests will be sucessful
     689                 :            :  * (provided xfrm_policy_lock is held throughout).
     690                 :            :  */
     691                 :            : static struct xfrm_pol_inexact_bin *
     692                 :          0 : xfrm_policy_inexact_alloc_bin(const struct xfrm_policy *pol, u8 dir)
     693                 :            : {
     694                 :            :         struct xfrm_pol_inexact_bin *bin, *prev;
     695                 :          0 :         struct xfrm_pol_inexact_key k = {
     696                 :          0 :                 .family = pol->family,
     697                 :          0 :                 .type = pol->type,
     698                 :            :                 .dir = dir,
     699                 :          0 :                 .if_id = pol->if_id,
     700                 :            :         };
     701                 :            :         struct net *net = xp_net(pol);
     702                 :            : 
     703                 :            :         lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
     704                 :            : 
     705                 :            :         write_pnet(&k.net, net);
     706                 :          0 :         bin = rhashtable_lookup_fast(&xfrm_policy_inexact_table, &k,
     707                 :            :                                      xfrm_pol_inexact_params);
     708                 :          0 :         if (bin)
     709                 :            :                 return bin;
     710                 :            : 
     711                 :          0 :         bin = kzalloc(sizeof(*bin), GFP_ATOMIC);
     712                 :          0 :         if (!bin)
     713                 :            :                 return NULL;
     714                 :            : 
     715                 :          0 :         bin->k = k;
     716                 :          0 :         INIT_HLIST_HEAD(&bin->hhead);
     717                 :          0 :         bin->root_d = RB_ROOT;
     718                 :          0 :         bin->root_s = RB_ROOT;
     719                 :            :         seqcount_init(&bin->count);
     720                 :            : 
     721                 :          0 :         prev = rhashtable_lookup_get_insert_key(&xfrm_policy_inexact_table,
     722                 :          0 :                                                 &bin->k, &bin->head,
     723                 :            :                                                 xfrm_pol_inexact_params);
     724                 :          0 :         if (!prev) {
     725                 :          0 :                 list_add(&bin->inexact_bins, &net->xfrm.inexact_bins);
     726                 :          0 :                 return bin;
     727                 :            :         }
     728                 :            : 
     729                 :          0 :         kfree(bin);
     730                 :            : 
     731                 :          0 :         return IS_ERR(prev) ? NULL : prev;
     732                 :            : }
     733                 :            : 
     734                 :          0 : static bool xfrm_pol_inexact_addr_use_any_list(const xfrm_address_t *addr,
     735                 :            :                                                int family, u8 prefixlen)
     736                 :            : {
     737                 :          0 :         if (xfrm_addr_any(addr, family))
     738                 :            :                 return true;
     739                 :            : 
     740                 :          0 :         if (family == AF_INET6 && prefixlen < INEXACT_PREFIXLEN_IPV6)
     741                 :            :                 return true;
     742                 :            : 
     743                 :          0 :         if (family == AF_INET && prefixlen < INEXACT_PREFIXLEN_IPV4)
     744                 :            :                 return true;
     745                 :            : 
     746                 :          0 :         return false;
     747                 :            : }
     748                 :            : 
     749                 :            : static bool
     750                 :          0 : xfrm_policy_inexact_insert_use_any_list(const struct xfrm_policy *policy)
     751                 :            : {
     752                 :            :         const xfrm_address_t *addr;
     753                 :            :         bool saddr_any, daddr_any;
     754                 :            :         u8 prefixlen;
     755                 :            : 
     756                 :          0 :         addr = &policy->selector.saddr;
     757                 :          0 :         prefixlen = policy->selector.prefixlen_s;
     758                 :            : 
     759                 :          0 :         saddr_any = xfrm_pol_inexact_addr_use_any_list(addr,
     760                 :          0 :                                                        policy->family,
     761                 :            :                                                        prefixlen);
     762                 :          0 :         addr = &policy->selector.daddr;
     763                 :          0 :         prefixlen = policy->selector.prefixlen_d;
     764                 :          0 :         daddr_any = xfrm_pol_inexact_addr_use_any_list(addr,
     765                 :            :                                                        policy->family,
     766                 :            :                                                        prefixlen);
     767                 :          0 :         return saddr_any && daddr_any;
     768                 :            : }
     769                 :            : 
     770                 :            : static void xfrm_pol_inexact_node_init(struct xfrm_pol_inexact_node *node,
     771                 :            :                                        const xfrm_address_t *addr, u8 prefixlen)
     772                 :            : {
     773                 :          0 :         node->addr = *addr;
     774                 :          0 :         node->prefixlen = prefixlen;
     775                 :            : }
     776                 :            : 
     777                 :            : static struct xfrm_pol_inexact_node *
     778                 :          0 : xfrm_pol_inexact_node_alloc(const xfrm_address_t *addr, u8 prefixlen)
     779                 :            : {
     780                 :            :         struct xfrm_pol_inexact_node *node;
     781                 :            : 
     782                 :          0 :         node = kzalloc(sizeof(*node), GFP_ATOMIC);
     783                 :          0 :         if (node)
     784                 :            :                 xfrm_pol_inexact_node_init(node, addr, prefixlen);
     785                 :            : 
     786                 :          0 :         return node;
     787                 :            : }
     788                 :            : 
     789                 :          0 : static int xfrm_policy_addr_delta(const xfrm_address_t *a,
     790                 :            :                                   const xfrm_address_t *b,
     791                 :            :                                   u8 prefixlen, u16 family)
     792                 :            : {
     793                 :            :         unsigned int pdw, pbi;
     794                 :            :         int delta = 0;
     795                 :            : 
     796                 :          0 :         switch (family) {
     797                 :            :         case AF_INET:
     798                 :          0 :                 if (sizeof(long) == 4 && prefixlen == 0)
     799                 :          0 :                         return ntohl(a->a4) - ntohl(b->a4);
     800                 :          0 :                 return (ntohl(a->a4) & ((~0UL << (32 - prefixlen)))) -
     801                 :          0 :                        (ntohl(b->a4) & ((~0UL << (32 - prefixlen))));
     802                 :            :         case AF_INET6:
     803                 :          0 :                 pdw = prefixlen >> 5;
     804                 :          0 :                 pbi = prefixlen & 0x1f;
     805                 :            : 
     806                 :          0 :                 if (pdw) {
     807                 :          0 :                         delta = memcmp(a->a6, b->a6, pdw << 2);
     808                 :          0 :                         if (delta)
     809                 :            :                                 return delta;
     810                 :            :                 }
     811                 :          0 :                 if (pbi) {
     812                 :          0 :                         u32 mask = ~0u << (32 - pbi);
     813                 :            : 
     814                 :          0 :                         delta = (ntohl(a->a6[pdw]) & mask) -
     815                 :          0 :                                 (ntohl(b->a6[pdw]) & mask);
     816                 :            :                 }
     817                 :            :                 break;
     818                 :            :         default:
     819                 :            :                 break;
     820                 :            :         }
     821                 :            : 
     822                 :          0 :         return delta;
     823                 :            : }
     824                 :            : 
     825                 :          0 : static void xfrm_policy_inexact_list_reinsert(struct net *net,
     826                 :            :                                               struct xfrm_pol_inexact_node *n,
     827                 :            :                                               u16 family)
     828                 :            : {
     829                 :            :         unsigned int matched_s, matched_d;
     830                 :            :         struct xfrm_policy *policy, *p;
     831                 :            : 
     832                 :            :         matched_s = 0;
     833                 :            :         matched_d = 0;
     834                 :            : 
     835                 :          0 :         list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
     836                 :            :                 struct hlist_node *newpos = NULL;
     837                 :            :                 bool matches_s, matches_d;
     838                 :            : 
     839                 :          0 :                 if (!policy->bydst_reinsert)
     840                 :          0 :                         continue;
     841                 :            : 
     842                 :          0 :                 WARN_ON_ONCE(policy->family != family);
     843                 :            : 
     844                 :          0 :                 policy->bydst_reinsert = false;
     845                 :          0 :                 hlist_for_each_entry(p, &n->hhead, bydst) {
     846                 :          0 :                         if (policy->priority > p->priority)
     847                 :          0 :                                 newpos = &p->bydst;
     848                 :          0 :                         else if (policy->priority == p->priority &&
     849                 :          0 :                                  policy->pos > p->pos)
     850                 :          0 :                                 newpos = &p->bydst;
     851                 :            :                         else
     852                 :            :                                 break;
     853                 :            :                 }
     854                 :            : 
     855                 :          0 :                 if (newpos)
     856                 :          0 :                         hlist_add_behind_rcu(&policy->bydst, newpos);
     857                 :            :                 else
     858                 :          0 :                         hlist_add_head_rcu(&policy->bydst, &n->hhead);
     859                 :            : 
     860                 :            :                 /* paranoia checks follow.
     861                 :            :                  * Check that the reinserted policy matches at least
     862                 :            :                  * saddr or daddr for current node prefix.
     863                 :            :                  *
     864                 :            :                  * Matching both is fine, matching saddr in one policy
     865                 :            :                  * (but not daddr) and then matching only daddr in another
     866                 :            :                  * is a bug.
     867                 :            :                  */
     868                 :          0 :                 matches_s = xfrm_policy_addr_delta(&policy->selector.saddr,
     869                 :          0 :                                                    &n->addr,
     870                 :            :                                                    n->prefixlen,
     871                 :            :                                                    family) == 0;
     872                 :          0 :                 matches_d = xfrm_policy_addr_delta(&policy->selector.daddr,
     873                 :            :                                                    &n->addr,
     874                 :            :                                                    n->prefixlen,
     875                 :            :                                                    family) == 0;
     876                 :          0 :                 if (matches_s && matches_d)
     877                 :          0 :                         continue;
     878                 :            : 
     879                 :          0 :                 WARN_ON_ONCE(!matches_s && !matches_d);
     880                 :          0 :                 if (matches_s)
     881                 :          0 :                         matched_s++;
     882                 :          0 :                 if (matches_d)
     883                 :          0 :                         matched_d++;
     884                 :          0 :                 WARN_ON_ONCE(matched_s && matched_d);
     885                 :            :         }
     886                 :          0 : }
     887                 :            : 
     888                 :          0 : static void xfrm_policy_inexact_node_reinsert(struct net *net,
     889                 :            :                                               struct xfrm_pol_inexact_node *n,
     890                 :            :                                               struct rb_root *new,
     891                 :            :                                               u16 family)
     892                 :            : {
     893                 :            :         struct xfrm_pol_inexact_node *node;
     894                 :            :         struct rb_node **p, *parent;
     895                 :            : 
     896                 :            :         /* we should not have another subtree here */
     897                 :          0 :         WARN_ON_ONCE(!RB_EMPTY_ROOT(&n->root));
     898                 :            : restart:
     899                 :            :         parent = NULL;
     900                 :          0 :         p = &new->rb_node;
     901                 :          0 :         while (*p) {
     902                 :            :                 u8 prefixlen;
     903                 :            :                 int delta;
     904                 :            : 
     905                 :            :                 parent = *p;
     906                 :            :                 node = rb_entry(*p, struct xfrm_pol_inexact_node, node);
     907                 :            : 
     908                 :          0 :                 prefixlen = min(node->prefixlen, n->prefixlen);
     909                 :            : 
     910                 :          0 :                 delta = xfrm_policy_addr_delta(&n->addr, &node->addr,
     911                 :            :                                                prefixlen, family);
     912                 :          0 :                 if (delta < 0) {
     913                 :          0 :                         p = &parent->rb_left;
     914                 :          0 :                 } else if (delta > 0) {
     915                 :          0 :                         p = &parent->rb_right;
     916                 :            :                 } else {
     917                 :          0 :                         bool same_prefixlen = node->prefixlen == n->prefixlen;
     918                 :            :                         struct xfrm_policy *tmp;
     919                 :            : 
     920                 :          0 :                         hlist_for_each_entry(tmp, &n->hhead, bydst) {
     921                 :          0 :                                 tmp->bydst_reinsert = true;
     922                 :            :                                 hlist_del_rcu(&tmp->bydst);
     923                 :            :                         }
     924                 :            : 
     925                 :          0 :                         node->prefixlen = prefixlen;
     926                 :            : 
     927                 :          0 :                         xfrm_policy_inexact_list_reinsert(net, node, family);
     928                 :            : 
     929                 :          0 :                         if (same_prefixlen) {
     930                 :          0 :                                 kfree_rcu(n, rcu);
     931                 :          0 :                                 return;
     932                 :            :                         }
     933                 :            : 
     934                 :          0 :                         rb_erase(*p, new);
     935                 :          0 :                         kfree_rcu(n, rcu);
     936                 :          0 :                         n = node;
     937                 :          0 :                         goto restart;
     938                 :            :                 }
     939                 :            :         }
     940                 :            : 
     941                 :          0 :         rb_link_node_rcu(&n->node, parent, p);
     942                 :          0 :         rb_insert_color(&n->node, new);
     943                 :            : }
     944                 :            : 
     945                 :            : /* merge nodes v and n */
     946                 :          0 : static void xfrm_policy_inexact_node_merge(struct net *net,
     947                 :            :                                            struct xfrm_pol_inexact_node *v,
     948                 :            :                                            struct xfrm_pol_inexact_node *n,
     949                 :            :                                            u16 family)
     950                 :            : {
     951                 :            :         struct xfrm_pol_inexact_node *node;
     952                 :            :         struct xfrm_policy *tmp;
     953                 :            :         struct rb_node *rnode;
     954                 :            : 
     955                 :            :         /* To-be-merged node v has a subtree.
     956                 :            :          *
     957                 :            :          * Dismantle it and insert its nodes to n->root.
     958                 :            :          */
     959                 :          0 :         while ((rnode = rb_first(&v->root)) != NULL) {
     960                 :            :                 node = rb_entry(rnode, struct xfrm_pol_inexact_node, node);
     961                 :          0 :                 rb_erase(&node->node, &v->root);
     962                 :          0 :                 xfrm_policy_inexact_node_reinsert(net, node, &n->root,
     963                 :            :                                                   family);
     964                 :            :         }
     965                 :            : 
     966                 :          0 :         hlist_for_each_entry(tmp, &v->hhead, bydst) {
     967                 :          0 :                 tmp->bydst_reinsert = true;
     968                 :            :                 hlist_del_rcu(&tmp->bydst);
     969                 :            :         }
     970                 :            : 
     971                 :          0 :         xfrm_policy_inexact_list_reinsert(net, n, family);
     972                 :          0 : }
     973                 :            : 
     974                 :            : static struct xfrm_pol_inexact_node *
     975                 :          0 : xfrm_policy_inexact_insert_node(struct net *net,
     976                 :            :                                 struct rb_root *root,
     977                 :            :                                 xfrm_address_t *addr,
     978                 :            :                                 u16 family, u8 prefixlen, u8 dir)
     979                 :            : {
     980                 :            :         struct xfrm_pol_inexact_node *cached = NULL;
     981                 :            :         struct rb_node **p, *parent = NULL;
     982                 :            :         struct xfrm_pol_inexact_node *node;
     983                 :            : 
     984                 :          0 :         p = &root->rb_node;
     985                 :          0 :         while (*p) {
     986                 :            :                 int delta;
     987                 :            : 
     988                 :            :                 parent = *p;
     989                 :            :                 node = rb_entry(*p, struct xfrm_pol_inexact_node, node);
     990                 :            : 
     991                 :          0 :                 delta = xfrm_policy_addr_delta(addr, &node->addr,
     992                 :            :                                                node->prefixlen,
     993                 :            :                                                family);
     994                 :          0 :                 if (delta == 0 && prefixlen >= node->prefixlen) {
     995                 :          0 :                         WARN_ON_ONCE(cached); /* ipsec policies got lost */
     996                 :          0 :                         return node;
     997                 :            :                 }
     998                 :            : 
     999                 :          0 :                 if (delta < 0)
    1000                 :          0 :                         p = &parent->rb_left;
    1001                 :            :                 else
    1002                 :          0 :                         p = &parent->rb_right;
    1003                 :            : 
    1004                 :          0 :                 if (prefixlen < node->prefixlen) {
    1005                 :          0 :                         delta = xfrm_policy_addr_delta(addr, &node->addr,
    1006                 :            :                                                        prefixlen,
    1007                 :            :                                                        family);
    1008                 :          0 :                         if (delta)
    1009                 :          0 :                                 continue;
    1010                 :            : 
    1011                 :            :                         /* This node is a subnet of the new prefix. It needs
    1012                 :            :                          * to be removed and re-inserted with the smaller
    1013                 :            :                          * prefix and all nodes that are now also covered
    1014                 :            :                          * by the reduced prefixlen.
    1015                 :            :                          */
    1016                 :          0 :                         rb_erase(&node->node, root);
    1017                 :            : 
    1018                 :          0 :                         if (!cached) {
    1019                 :            :                                 xfrm_pol_inexact_node_init(node, addr,
    1020                 :            :                                                            prefixlen);
    1021                 :            :                                 cached = node;
    1022                 :            :                         } else {
    1023                 :            :                                 /* This node also falls within the new
    1024                 :            :                                  * prefixlen. Merge the to-be-reinserted
    1025                 :            :                                  * node and this one.
    1026                 :            :                                  */
    1027                 :          0 :                                 xfrm_policy_inexact_node_merge(net, node,
    1028                 :            :                                                                cached, family);
    1029                 :          0 :                                 kfree_rcu(node, rcu);
    1030                 :            :                         }
    1031                 :            : 
    1032                 :            :                         /* restart */
    1033                 :            :                         p = &root->rb_node;
    1034                 :            :                         parent = NULL;
    1035                 :            :                 }
    1036                 :            :         }
    1037                 :            : 
    1038                 :          0 :         node = cached;
    1039                 :          0 :         if (!node) {
    1040                 :          0 :                 node = xfrm_pol_inexact_node_alloc(addr, prefixlen);
    1041                 :          0 :                 if (!node)
    1042                 :            :                         return NULL;
    1043                 :            :         }
    1044                 :            : 
    1045                 :          0 :         rb_link_node_rcu(&node->node, parent, p);
    1046                 :          0 :         rb_insert_color(&node->node, root);
    1047                 :            : 
    1048                 :          0 :         return node;
    1049                 :            : }
    1050                 :            : 
    1051                 :          0 : static void xfrm_policy_inexact_gc_tree(struct rb_root *r, bool rm)
    1052                 :            : {
    1053                 :            :         struct xfrm_pol_inexact_node *node;
    1054                 :          0 :         struct rb_node *rn = rb_first(r);
    1055                 :            : 
    1056                 :          0 :         while (rn) {
    1057                 :            :                 node = rb_entry(rn, struct xfrm_pol_inexact_node, node);
    1058                 :            : 
    1059                 :          0 :                 xfrm_policy_inexact_gc_tree(&node->root, rm);
    1060                 :          0 :                 rn = rb_next(rn);
    1061                 :            : 
    1062                 :          0 :                 if (!hlist_empty(&node->hhead) || !RB_EMPTY_ROOT(&node->root)) {
    1063                 :          0 :                         WARN_ON_ONCE(rm);
    1064                 :          0 :                         continue;
    1065                 :            :                 }
    1066                 :            : 
    1067                 :          0 :                 rb_erase(&node->node, r);
    1068                 :          0 :                 kfree_rcu(node, rcu);
    1069                 :            :         }
    1070                 :          0 : }
    1071                 :            : 
    1072                 :          0 : static void __xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b, bool net_exit)
    1073                 :            : {
    1074                 :            :         write_seqcount_begin(&b->count);
    1075                 :          0 :         xfrm_policy_inexact_gc_tree(&b->root_d, net_exit);
    1076                 :          0 :         xfrm_policy_inexact_gc_tree(&b->root_s, net_exit);
    1077                 :            :         write_seqcount_end(&b->count);
    1078                 :            : 
    1079                 :          0 :         if (!RB_EMPTY_ROOT(&b->root_d) || !RB_EMPTY_ROOT(&b->root_s) ||
    1080                 :            :             !hlist_empty(&b->hhead)) {
    1081                 :          0 :                 WARN_ON_ONCE(net_exit);
    1082                 :          0 :                 return;
    1083                 :            :         }
    1084                 :            : 
    1085                 :          0 :         if (rhashtable_remove_fast(&xfrm_policy_inexact_table, &b->head,
    1086                 :            :                                    xfrm_pol_inexact_params) == 0) {
    1087                 :            :                 list_del(&b->inexact_bins);
    1088                 :          0 :                 kfree_rcu(b, rcu);
    1089                 :            :         }
    1090                 :            : }
    1091                 :            : 
    1092                 :          0 : static void xfrm_policy_inexact_prune_bin(struct xfrm_pol_inexact_bin *b)
    1093                 :            : {
    1094                 :            :         struct net *net = read_pnet(&b->k.net);
    1095                 :            : 
    1096                 :            :         spin_lock_bh(&net->xfrm.xfrm_policy_lock);
    1097                 :          0 :         __xfrm_policy_inexact_prune_bin(b, false);
    1098                 :            :         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    1099                 :          0 : }
    1100                 :            : 
    1101                 :          0 : static void __xfrm_policy_inexact_flush(struct net *net)
    1102                 :            : {
    1103                 :            :         struct xfrm_pol_inexact_bin *bin, *t;
    1104                 :            : 
    1105                 :            :         lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
    1106                 :            : 
    1107                 :          0 :         list_for_each_entry_safe(bin, t, &net->xfrm.inexact_bins, inexact_bins)
    1108                 :          0 :                 __xfrm_policy_inexact_prune_bin(bin, false);
    1109                 :          0 : }
    1110                 :            : 
    1111                 :            : static struct hlist_head *
    1112                 :          0 : xfrm_policy_inexact_alloc_chain(struct xfrm_pol_inexact_bin *bin,
    1113                 :            :                                 struct xfrm_policy *policy, u8 dir)
    1114                 :            : {
    1115                 :            :         struct xfrm_pol_inexact_node *n;
    1116                 :            :         struct net *net;
    1117                 :            : 
    1118                 :            :         net = xp_net(policy);
    1119                 :            :         lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
    1120                 :            : 
    1121                 :          0 :         if (xfrm_policy_inexact_insert_use_any_list(policy))
    1122                 :          0 :                 return &bin->hhead;
    1123                 :            : 
    1124                 :          0 :         if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.daddr,
    1125                 :          0 :                                                policy->family,
    1126                 :            :                                                policy->selector.prefixlen_d)) {
    1127                 :            :                 write_seqcount_begin(&bin->count);
    1128                 :          0 :                 n = xfrm_policy_inexact_insert_node(net,
    1129                 :            :                                                     &bin->root_s,
    1130                 :            :                                                     &policy->selector.saddr,
    1131                 :            :                                                     policy->family,
    1132                 :            :                                                     policy->selector.prefixlen_s,
    1133                 :            :                                                     dir);
    1134                 :            :                 write_seqcount_end(&bin->count);
    1135                 :          0 :                 if (!n)
    1136                 :            :                         return NULL;
    1137                 :            : 
    1138                 :          0 :                 return &n->hhead;
    1139                 :            :         }
    1140                 :            : 
    1141                 :            :         /* daddr is fixed */
    1142                 :            :         write_seqcount_begin(&bin->count);
    1143                 :          0 :         n = xfrm_policy_inexact_insert_node(net,
    1144                 :            :                                             &bin->root_d,
    1145                 :            :                                             &policy->selector.daddr,
    1146                 :            :                                             policy->family,
    1147                 :            :                                             policy->selector.prefixlen_d, dir);
    1148                 :            :         write_seqcount_end(&bin->count);
    1149                 :          0 :         if (!n)
    1150                 :            :                 return NULL;
    1151                 :            : 
    1152                 :            :         /* saddr is wildcard */
    1153                 :          0 :         if (xfrm_pol_inexact_addr_use_any_list(&policy->selector.saddr,
    1154                 :          0 :                                                policy->family,
    1155                 :            :                                                policy->selector.prefixlen_s))
    1156                 :          0 :                 return &n->hhead;
    1157                 :            : 
    1158                 :            :         write_seqcount_begin(&bin->count);
    1159                 :          0 :         n = xfrm_policy_inexact_insert_node(net,
    1160                 :            :                                             &n->root,
    1161                 :            :                                             &policy->selector.saddr,
    1162                 :            :                                             policy->family,
    1163                 :            :                                             policy->selector.prefixlen_s, dir);
    1164                 :            :         write_seqcount_end(&bin->count);
    1165                 :          0 :         if (!n)
    1166                 :            :                 return NULL;
    1167                 :            : 
    1168                 :          0 :         return &n->hhead;
    1169                 :            : }
    1170                 :            : 
    1171                 :            : static struct xfrm_policy *
    1172                 :          0 : xfrm_policy_inexact_insert(struct xfrm_policy *policy, u8 dir, int excl)
    1173                 :            : {
    1174                 :            :         struct xfrm_pol_inexact_bin *bin;
    1175                 :            :         struct xfrm_policy *delpol;
    1176                 :            :         struct hlist_head *chain;
    1177                 :            :         struct net *net;
    1178                 :            : 
    1179                 :          0 :         bin = xfrm_policy_inexact_alloc_bin(policy, dir);
    1180                 :          0 :         if (!bin)
    1181                 :            :                 return ERR_PTR(-ENOMEM);
    1182                 :            : 
    1183                 :            :         net = xp_net(policy);
    1184                 :            :         lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
    1185                 :            : 
    1186                 :          0 :         chain = xfrm_policy_inexact_alloc_chain(bin, policy, dir);
    1187                 :          0 :         if (!chain) {
    1188                 :          0 :                 __xfrm_policy_inexact_prune_bin(bin, false);
    1189                 :          0 :                 return ERR_PTR(-ENOMEM);
    1190                 :            :         }
    1191                 :            : 
    1192                 :          0 :         delpol = xfrm_policy_insert_list(chain, policy, excl);
    1193                 :          0 :         if (delpol && excl) {
    1194                 :          0 :                 __xfrm_policy_inexact_prune_bin(bin, false);
    1195                 :          0 :                 return ERR_PTR(-EEXIST);
    1196                 :            :         }
    1197                 :            : 
    1198                 :          0 :         chain = &net->xfrm.policy_inexact[dir];
    1199                 :          0 :         xfrm_policy_insert_inexact_list(chain, policy);
    1200                 :            : 
    1201                 :          0 :         if (delpol)
    1202                 :          0 :                 __xfrm_policy_inexact_prune_bin(bin, false);
    1203                 :            : 
    1204                 :          0 :         return delpol;
    1205                 :            : }
    1206                 :            : 
    1207                 :          0 : static void xfrm_hash_rebuild(struct work_struct *work)
    1208                 :            : {
    1209                 :          0 :         struct net *net = container_of(work, struct net,
    1210                 :            :                                        xfrm.policy_hthresh.work);
    1211                 :            :         unsigned int hmask;
    1212                 :            :         struct xfrm_policy *pol;
    1213                 :            :         struct xfrm_policy *policy;
    1214                 :            :         struct hlist_head *chain;
    1215                 :            :         struct hlist_head *odst;
    1216                 :            :         struct hlist_node *newpos;
    1217                 :            :         int i;
    1218                 :            :         int dir;
    1219                 :            :         unsigned seq;
    1220                 :            :         u8 lbits4, rbits4, lbits6, rbits6;
    1221                 :            : 
    1222                 :          0 :         mutex_lock(&hash_resize_mutex);
    1223                 :            : 
    1224                 :            :         /* read selector prefixlen thresholds */
    1225                 :            :         do {
    1226                 :            :                 seq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
    1227                 :            : 
    1228                 :          0 :                 lbits4 = net->xfrm.policy_hthresh.lbits4;
    1229                 :          0 :                 rbits4 = net->xfrm.policy_hthresh.rbits4;
    1230                 :          0 :                 lbits6 = net->xfrm.policy_hthresh.lbits6;
    1231                 :          0 :                 rbits6 = net->xfrm.policy_hthresh.rbits6;
    1232                 :          0 :         } while (read_seqretry(&net->xfrm.policy_hthresh.lock, seq));
    1233                 :            : 
    1234                 :            :         spin_lock_bh(&net->xfrm.xfrm_policy_lock);
    1235                 :            :         write_seqcount_begin(&xfrm_policy_hash_generation);
    1236                 :            : 
    1237                 :            :         /* make sure that we can insert the indirect policies again before
    1238                 :            :          * we start with destructive action.
    1239                 :            :          */
    1240                 :          0 :         list_for_each_entry(policy, &net->xfrm.policy_all, walk.all) {
    1241                 :            :                 struct xfrm_pol_inexact_bin *bin;
    1242                 :            :                 u8 dbits, sbits;
    1243                 :            : 
    1244                 :          0 :                 dir = xfrm_policy_id2dir(policy->index);
    1245                 :          0 :                 if (policy->walk.dead || dir >= XFRM_POLICY_MAX)
    1246                 :          0 :                         continue;
    1247                 :            : 
    1248                 :          0 :                 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
    1249                 :          0 :                         if (policy->family == AF_INET) {
    1250                 :            :                                 dbits = rbits4;
    1251                 :            :                                 sbits = lbits4;
    1252                 :            :                         } else {
    1253                 :            :                                 dbits = rbits6;
    1254                 :            :                                 sbits = lbits6;
    1255                 :            :                         }
    1256                 :            :                 } else {
    1257                 :          0 :                         if (policy->family == AF_INET) {
    1258                 :            :                                 dbits = lbits4;
    1259                 :            :                                 sbits = rbits4;
    1260                 :            :                         } else {
    1261                 :            :                                 dbits = lbits6;
    1262                 :            :                                 sbits = rbits6;
    1263                 :            :                         }
    1264                 :            :                 }
    1265                 :            : 
    1266                 :          0 :                 if (policy->selector.prefixlen_d < dbits ||
    1267                 :          0 :                     policy->selector.prefixlen_s < sbits)
    1268                 :          0 :                         continue;
    1269                 :            : 
    1270                 :          0 :                 bin = xfrm_policy_inexact_alloc_bin(policy, dir);
    1271                 :          0 :                 if (!bin)
    1272                 :            :                         goto out_unlock;
    1273                 :            : 
    1274                 :          0 :                 if (!xfrm_policy_inexact_alloc_chain(bin, policy, dir))
    1275                 :            :                         goto out_unlock;
    1276                 :            :         }
    1277                 :            : 
    1278                 :            :         /* reset the bydst and inexact table in all directions */
    1279                 :          0 :         for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
    1280                 :            :                 struct hlist_node *n;
    1281                 :            : 
    1282                 :          0 :                 hlist_for_each_entry_safe(policy, n,
    1283                 :            :                                           &net->xfrm.policy_inexact[dir],
    1284                 :            :                                           bydst_inexact_list) {
    1285                 :            :                         hlist_del_rcu(&policy->bydst);
    1286                 :            :                         hlist_del_init(&policy->bydst_inexact_list);
    1287                 :            :                 }
    1288                 :            : 
    1289                 :          0 :                 hmask = net->xfrm.policy_bydst[dir].hmask;
    1290                 :          0 :                 odst = net->xfrm.policy_bydst[dir].table;
    1291                 :          0 :                 for (i = hmask; i >= 0; i--) {
    1292                 :          0 :                         hlist_for_each_entry_safe(policy, n, odst + i, bydst)
    1293                 :            :                                 hlist_del_rcu(&policy->bydst);
    1294                 :            :                 }
    1295                 :          0 :                 if ((dir & XFRM_POLICY_MASK) == XFRM_POLICY_OUT) {
    1296                 :            :                         /* dir out => dst = remote, src = local */
    1297                 :          0 :                         net->xfrm.policy_bydst[dir].dbits4 = rbits4;
    1298                 :          0 :                         net->xfrm.policy_bydst[dir].sbits4 = lbits4;
    1299                 :          0 :                         net->xfrm.policy_bydst[dir].dbits6 = rbits6;
    1300                 :          0 :                         net->xfrm.policy_bydst[dir].sbits6 = lbits6;
    1301                 :            :                 } else {
    1302                 :            :                         /* dir in/fwd => dst = local, src = remote */
    1303                 :          0 :                         net->xfrm.policy_bydst[dir].dbits4 = lbits4;
    1304                 :          0 :                         net->xfrm.policy_bydst[dir].sbits4 = rbits4;
    1305                 :          0 :                         net->xfrm.policy_bydst[dir].dbits6 = lbits6;
    1306                 :          0 :                         net->xfrm.policy_bydst[dir].sbits6 = rbits6;
    1307                 :            :                 }
    1308                 :            :         }
    1309                 :            : 
    1310                 :            :         /* re-insert all policies by order of creation */
    1311                 :          0 :         list_for_each_entry_reverse(policy, &net->xfrm.policy_all, walk.all) {
    1312                 :          0 :                 if (policy->walk.dead)
    1313                 :          0 :                         continue;
    1314                 :          0 :                 dir = xfrm_policy_id2dir(policy->index);
    1315                 :          0 :                 if (dir >= XFRM_POLICY_MAX) {
    1316                 :            :                         /* skip socket policies */
    1317                 :          0 :                         continue;
    1318                 :            :                 }
    1319                 :            :                 newpos = NULL;
    1320                 :          0 :                 chain = policy_hash_bysel(net, &policy->selector,
    1321                 :            :                                           policy->family, dir);
    1322                 :            : 
    1323                 :          0 :                 if (!chain) {
    1324                 :          0 :                         void *p = xfrm_policy_inexact_insert(policy, dir, 0);
    1325                 :            : 
    1326                 :          0 :                         WARN_ONCE(IS_ERR(p), "reinsert: %ld\n", PTR_ERR(p));
    1327                 :          0 :                         continue;
    1328                 :            :                 }
    1329                 :            : 
    1330                 :          0 :                 hlist_for_each_entry(pol, chain, bydst) {
    1331                 :          0 :                         if (policy->priority >= pol->priority)
    1332                 :          0 :                                 newpos = &pol->bydst;
    1333                 :            :                         else
    1334                 :            :                                 break;
    1335                 :            :                 }
    1336                 :          0 :                 if (newpos)
    1337                 :          0 :                         hlist_add_behind_rcu(&policy->bydst, newpos);
    1338                 :            :                 else
    1339                 :          0 :                         hlist_add_head_rcu(&policy->bydst, chain);
    1340                 :            :         }
    1341                 :            : 
    1342                 :            : out_unlock:
    1343                 :          0 :         __xfrm_policy_inexact_flush(net);
    1344                 :            :         write_seqcount_end(&xfrm_policy_hash_generation);
    1345                 :            :         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    1346                 :            : 
    1347                 :          0 :         mutex_unlock(&hash_resize_mutex);
    1348                 :          0 : }
    1349                 :            : 
    1350                 :          0 : void xfrm_policy_hash_rebuild(struct net *net)
    1351                 :            : {
    1352                 :          0 :         schedule_work(&net->xfrm.policy_hthresh.work);
    1353                 :          0 : }
    1354                 :            : EXPORT_SYMBOL(xfrm_policy_hash_rebuild);
    1355                 :            : 
    1356                 :            : /* Generate new index... KAME seems to generate them ordered by cost
    1357                 :            :  * of an absolute inpredictability of ordering of rules. This will not pass. */
    1358                 :          0 : static u32 xfrm_gen_index(struct net *net, int dir, u32 index)
    1359                 :            : {
    1360                 :            :         static u32 idx_generator;
    1361                 :            : 
    1362                 :            :         for (;;) {
    1363                 :            :                 struct hlist_head *list;
    1364                 :            :                 struct xfrm_policy *p;
    1365                 :            :                 u32 idx;
    1366                 :            :                 int found;
    1367                 :            : 
    1368                 :          0 :                 if (!index) {
    1369                 :          0 :                         idx = (idx_generator | dir);
    1370                 :          0 :                         idx_generator += 8;
    1371                 :            :                 } else {
    1372                 :            :                         idx = index;
    1373                 :            :                         index = 0;
    1374                 :            :                 }
    1375                 :            : 
    1376                 :          0 :                 if (idx == 0)
    1377                 :            :                         idx = 8;
    1378                 :          0 :                 list = net->xfrm.policy_byidx + idx_hash(net, idx);
    1379                 :            :                 found = 0;
    1380                 :          0 :                 hlist_for_each_entry(p, list, byidx) {
    1381                 :          0 :                         if (p->index == idx) {
    1382                 :            :                                 found = 1;
    1383                 :            :                                 break;
    1384                 :            :                         }
    1385                 :            :                 }
    1386                 :          0 :                 if (!found)
    1387                 :          0 :                         return idx;
    1388                 :            :         }
    1389                 :            : }
    1390                 :            : 
    1391                 :            : static inline int selector_cmp(struct xfrm_selector *s1, struct xfrm_selector *s2)
    1392                 :            : {
    1393                 :            :         u32 *p1 = (u32 *) s1;
    1394                 :            :         u32 *p2 = (u32 *) s2;
    1395                 :            :         int len = sizeof(struct xfrm_selector) / sizeof(u32);
    1396                 :            :         int i;
    1397                 :            : 
    1398                 :          0 :         for (i = 0; i < len; i++) {
    1399                 :          0 :                 if (p1[i] != p2[i])
    1400                 :            :                         return 1;
    1401                 :            :         }
    1402                 :            : 
    1403                 :            :         return 0;
    1404                 :            : }
    1405                 :            : 
    1406                 :          0 : static void xfrm_policy_requeue(struct xfrm_policy *old,
    1407                 :            :                                 struct xfrm_policy *new)
    1408                 :            : {
    1409                 :            :         struct xfrm_policy_queue *pq = &old->polq;
    1410                 :            :         struct sk_buff_head list;
    1411                 :            : 
    1412                 :          0 :         if (skb_queue_empty(&pq->hold_queue))
    1413                 :          0 :                 return;
    1414                 :            : 
    1415                 :            :         __skb_queue_head_init(&list);
    1416                 :            : 
    1417                 :            :         spin_lock_bh(&pq->hold_queue.lock);
    1418                 :            :         skb_queue_splice_init(&pq->hold_queue, &list);
    1419                 :          0 :         if (del_timer(&pq->hold_timer))
    1420                 :          0 :                 xfrm_pol_put(old);
    1421                 :            :         spin_unlock_bh(&pq->hold_queue.lock);
    1422                 :            : 
    1423                 :            :         pq = &new->polq;
    1424                 :            : 
    1425                 :            :         spin_lock_bh(&pq->hold_queue.lock);
    1426                 :          0 :         skb_queue_splice(&list, &pq->hold_queue);
    1427                 :          0 :         pq->timeout = XFRM_QUEUE_TMO_MIN;
    1428                 :          0 :         if (!mod_timer(&pq->hold_timer, jiffies))
    1429                 :            :                 xfrm_pol_hold(new);
    1430                 :            :         spin_unlock_bh(&pq->hold_queue.lock);
    1431                 :            : }
    1432                 :            : 
    1433                 :            : static inline bool xfrm_policy_mark_match(const struct xfrm_mark *mark,
    1434                 :            :                                           struct xfrm_policy *pol)
    1435                 :            : {
    1436                 :          0 :         return mark->v == pol->mark.v && mark->m == pol->mark.m;
    1437                 :            : }
    1438                 :            : 
    1439                 :          0 : static u32 xfrm_pol_bin_key(const void *data, u32 len, u32 seed)
    1440                 :            : {
    1441                 :            :         const struct xfrm_pol_inexact_key *k = data;
    1442                 :          0 :         u32 a = k->type << 24 | k->dir << 16 | k->family;
    1443                 :            : 
    1444                 :          0 :         return jhash_3words(a, k->if_id, net_hash_mix(read_pnet(&k->net)),
    1445                 :            :                             seed);
    1446                 :            : }
    1447                 :            : 
    1448                 :          0 : static u32 xfrm_pol_bin_obj(const void *data, u32 len, u32 seed)
    1449                 :            : {
    1450                 :            :         const struct xfrm_pol_inexact_bin *b = data;
    1451                 :            : 
    1452                 :          0 :         return xfrm_pol_bin_key(&b->k, 0, seed);
    1453                 :            : }
    1454                 :            : 
    1455                 :          0 : static int xfrm_pol_bin_cmp(struct rhashtable_compare_arg *arg,
    1456                 :            :                             const void *ptr)
    1457                 :            : {
    1458                 :          0 :         const struct xfrm_pol_inexact_key *key = arg->key;
    1459                 :            :         const struct xfrm_pol_inexact_bin *b = ptr;
    1460                 :            :         int ret;
    1461                 :            : 
    1462                 :          0 :         if (!net_eq(read_pnet(&b->k.net), read_pnet(&key->net)))
    1463                 :            :                 return -1;
    1464                 :            : 
    1465                 :          0 :         ret = b->k.dir ^ key->dir;
    1466                 :          0 :         if (ret)
    1467                 :            :                 return ret;
    1468                 :            : 
    1469                 :          0 :         ret = b->k.type ^ key->type;
    1470                 :          0 :         if (ret)
    1471                 :            :                 return ret;
    1472                 :            : 
    1473                 :          0 :         ret = b->k.family ^ key->family;
    1474                 :          0 :         if (ret)
    1475                 :            :                 return ret;
    1476                 :            : 
    1477                 :          0 :         return b->k.if_id ^ key->if_id;
    1478                 :            : }
    1479                 :            : 
    1480                 :            : static const struct rhashtable_params xfrm_pol_inexact_params = {
    1481                 :            :         .head_offset            = offsetof(struct xfrm_pol_inexact_bin, head),
    1482                 :            :         .hashfn                 = xfrm_pol_bin_key,
    1483                 :            :         .obj_hashfn             = xfrm_pol_bin_obj,
    1484                 :            :         .obj_cmpfn              = xfrm_pol_bin_cmp,
    1485                 :            :         .automatic_shrinking    = true,
    1486                 :            : };
    1487                 :            : 
    1488                 :          0 : static void xfrm_policy_insert_inexact_list(struct hlist_head *chain,
    1489                 :            :                                             struct xfrm_policy *policy)
    1490                 :            : {
    1491                 :            :         struct xfrm_policy *pol, *delpol = NULL;
    1492                 :            :         struct hlist_node *newpos = NULL;
    1493                 :            :         int i = 0;
    1494                 :            : 
    1495                 :          0 :         hlist_for_each_entry(pol, chain, bydst_inexact_list) {
    1496                 :          0 :                 if (pol->type == policy->type &&
    1497                 :          0 :                     pol->if_id == policy->if_id &&
    1498                 :          0 :                     !selector_cmp(&pol->selector, &policy->selector) &&
    1499                 :            :                     xfrm_policy_mark_match(&policy->mark, pol) &&
    1500                 :          0 :                     xfrm_sec_ctx_match(pol->security, policy->security) &&
    1501                 :          0 :                     !WARN_ON(delpol)) {
    1502                 :            :                         delpol = pol;
    1503                 :          0 :                         if (policy->priority > pol->priority)
    1504                 :          0 :                                 continue;
    1505                 :          0 :                 } else if (policy->priority >= pol->priority) {
    1506                 :          0 :                         newpos = &pol->bydst_inexact_list;
    1507                 :          0 :                         continue;
    1508                 :            :                 }
    1509                 :          0 :                 if (delpol)
    1510                 :            :                         break;
    1511                 :            :         }
    1512                 :            : 
    1513                 :          0 :         if (newpos)
    1514                 :          0 :                 hlist_add_behind_rcu(&policy->bydst_inexact_list, newpos);
    1515                 :            :         else
    1516                 :          0 :                 hlist_add_head_rcu(&policy->bydst_inexact_list, chain);
    1517                 :            : 
    1518                 :          0 :         hlist_for_each_entry(pol, chain, bydst_inexact_list) {
    1519                 :          0 :                 pol->pos = i;
    1520                 :          0 :                 i++;
    1521                 :            :         }
    1522                 :          0 : }
    1523                 :            : 
    1524                 :          0 : static struct xfrm_policy *xfrm_policy_insert_list(struct hlist_head *chain,
    1525                 :            :                                                    struct xfrm_policy *policy,
    1526                 :            :                                                    bool excl)
    1527                 :            : {
    1528                 :            :         struct xfrm_policy *pol, *newpos = NULL, *delpol = NULL;
    1529                 :            : 
    1530                 :          0 :         hlist_for_each_entry(pol, chain, bydst) {
    1531                 :          0 :                 if (pol->type == policy->type &&
    1532                 :          0 :                     pol->if_id == policy->if_id &&
    1533                 :          0 :                     !selector_cmp(&pol->selector, &policy->selector) &&
    1534                 :            :                     xfrm_policy_mark_match(&policy->mark, pol) &&
    1535                 :          0 :                     xfrm_sec_ctx_match(pol->security, policy->security) &&
    1536                 :          0 :                     !WARN_ON(delpol)) {
    1537                 :          0 :                         if (excl)
    1538                 :            :                                 return ERR_PTR(-EEXIST);
    1539                 :            :                         delpol = pol;
    1540                 :          0 :                         if (policy->priority > pol->priority)
    1541                 :          0 :                                 continue;
    1542                 :          0 :                 } else if (policy->priority >= pol->priority) {
    1543                 :            :                         newpos = pol;
    1544                 :          0 :                         continue;
    1545                 :            :                 }
    1546                 :          0 :                 if (delpol)
    1547                 :            :                         break;
    1548                 :            :         }
    1549                 :            : 
    1550                 :          0 :         if (newpos)
    1551                 :          0 :                 hlist_add_behind_rcu(&policy->bydst, &newpos->bydst);
    1552                 :            :         else
    1553                 :          0 :                 hlist_add_head_rcu(&policy->bydst, chain);
    1554                 :            : 
    1555                 :          0 :         return delpol;
    1556                 :            : }
    1557                 :            : 
    1558                 :          0 : int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
    1559                 :            : {
    1560                 :            :         struct net *net = xp_net(policy);
    1561                 :            :         struct xfrm_policy *delpol;
    1562                 :            :         struct hlist_head *chain;
    1563                 :            : 
    1564                 :            :         spin_lock_bh(&net->xfrm.xfrm_policy_lock);
    1565                 :          0 :         chain = policy_hash_bysel(net, &policy->selector, policy->family, dir);
    1566                 :          0 :         if (chain)
    1567                 :          0 :                 delpol = xfrm_policy_insert_list(chain, policy, excl);
    1568                 :            :         else
    1569                 :          0 :                 delpol = xfrm_policy_inexact_insert(policy, dir, excl);
    1570                 :            : 
    1571                 :          0 :         if (IS_ERR(delpol)) {
    1572                 :            :                 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    1573                 :          0 :                 return PTR_ERR(delpol);
    1574                 :            :         }
    1575                 :            : 
    1576                 :          0 :         __xfrm_policy_link(policy, dir);
    1577                 :            : 
    1578                 :            :         /* After previous checking, family can either be AF_INET or AF_INET6 */
    1579                 :          0 :         if (policy->family == AF_INET)
    1580                 :            :                 rt_genid_bump_ipv4(net);
    1581                 :            :         else
    1582                 :            :                 rt_genid_bump_ipv6(net);
    1583                 :            : 
    1584                 :          0 :         if (delpol) {
    1585                 :          0 :                 xfrm_policy_requeue(delpol, policy);
    1586                 :          0 :                 __xfrm_policy_unlink(delpol, dir);
    1587                 :            :         }
    1588                 :          0 :         policy->index = delpol ? delpol->index : xfrm_gen_index(net, dir, policy->index);
    1589                 :          0 :         hlist_add_head(&policy->byidx, net->xfrm.policy_byidx+idx_hash(net, policy->index));
    1590                 :          0 :         policy->curlft.add_time = ktime_get_real_seconds();
    1591                 :          0 :         policy->curlft.use_time = 0;
    1592                 :          0 :         if (!mod_timer(&policy->timer, jiffies + HZ))
    1593                 :            :                 xfrm_pol_hold(policy);
    1594                 :            :         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    1595                 :            : 
    1596                 :          0 :         if (delpol)
    1597                 :          0 :                 xfrm_policy_kill(delpol);
    1598                 :          0 :         else if (xfrm_bydst_should_resize(net, dir, NULL))
    1599                 :          0 :                 schedule_work(&net->xfrm.policy_hash_work);
    1600                 :            : 
    1601                 :            :         return 0;
    1602                 :            : }
    1603                 :            : EXPORT_SYMBOL(xfrm_policy_insert);
    1604                 :            : 
    1605                 :            : static struct xfrm_policy *
    1606                 :          0 : __xfrm_policy_bysel_ctx(struct hlist_head *chain, const struct xfrm_mark *mark,
    1607                 :            :                         u32 if_id, u8 type, int dir, struct xfrm_selector *sel,
    1608                 :            :                         struct xfrm_sec_ctx *ctx)
    1609                 :            : {
    1610                 :            :         struct xfrm_policy *pol;
    1611                 :            : 
    1612                 :          0 :         if (!chain)
    1613                 :            :                 return NULL;
    1614                 :            : 
    1615                 :          0 :         hlist_for_each_entry(pol, chain, bydst) {
    1616                 :          0 :                 if (pol->type == type &&
    1617                 :          0 :                     pol->if_id == if_id &&
    1618                 :          0 :                     xfrm_policy_mark_match(mark, pol) &&
    1619                 :          0 :                     !selector_cmp(sel, &pol->selector) &&
    1620                 :            :                     xfrm_sec_ctx_match(ctx, pol->security))
    1621                 :          0 :                         return pol;
    1622                 :            :         }
    1623                 :            : 
    1624                 :            :         return NULL;
    1625                 :            : }
    1626                 :            : 
    1627                 :            : struct xfrm_policy *
    1628                 :          0 : xfrm_policy_bysel_ctx(struct net *net, const struct xfrm_mark *mark, u32 if_id,
    1629                 :            :                       u8 type, int dir, struct xfrm_selector *sel,
    1630                 :            :                       struct xfrm_sec_ctx *ctx, int delete, int *err)
    1631                 :            : {
    1632                 :            :         struct xfrm_pol_inexact_bin *bin = NULL;
    1633                 :            :         struct xfrm_policy *pol, *ret = NULL;
    1634                 :            :         struct hlist_head *chain;
    1635                 :            : 
    1636                 :          0 :         *err = 0;
    1637                 :            :         spin_lock_bh(&net->xfrm.xfrm_policy_lock);
    1638                 :          0 :         chain = policy_hash_bysel(net, sel, sel->family, dir);
    1639                 :          0 :         if (!chain) {
    1640                 :            :                 struct xfrm_pol_inexact_candidates cand;
    1641                 :            :                 int i;
    1642                 :            : 
    1643                 :          0 :                 bin = xfrm_policy_inexact_lookup(net, type,
    1644                 :            :                                                  sel->family, dir, if_id);
    1645                 :          0 :                 if (!bin) {
    1646                 :            :                         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    1647                 :          0 :                         return NULL;
    1648                 :            :                 }
    1649                 :            : 
    1650                 :          0 :                 if (!xfrm_policy_find_inexact_candidates(&cand, bin,
    1651                 :          0 :                                                          &sel->saddr,
    1652                 :          0 :                                                          &sel->daddr)) {
    1653                 :            :                         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    1654                 :          0 :                         return NULL;
    1655                 :            :                 }
    1656                 :            : 
    1657                 :            :                 pol = NULL;
    1658                 :          0 :                 for (i = 0; i < ARRAY_SIZE(cand.res); i++) {
    1659                 :            :                         struct xfrm_policy *tmp;
    1660                 :            : 
    1661                 :          0 :                         tmp = __xfrm_policy_bysel_ctx(cand.res[i], mark,
    1662                 :            :                                                       if_id, type, dir,
    1663                 :            :                                                       sel, ctx);
    1664                 :          0 :                         if (!tmp)
    1665                 :          0 :                                 continue;
    1666                 :            : 
    1667                 :          0 :                         if (!pol || tmp->pos < pol->pos)
    1668                 :            :                                 pol = tmp;
    1669                 :            :                 }
    1670                 :            :         } else {
    1671                 :          0 :                 pol = __xfrm_policy_bysel_ctx(chain, mark, if_id, type, dir,
    1672                 :            :                                               sel, ctx);
    1673                 :            :         }
    1674                 :            : 
    1675                 :          0 :         if (pol) {
    1676                 :            :                 xfrm_pol_hold(pol);
    1677                 :          0 :                 if (delete) {
    1678                 :          0 :                         *err = security_xfrm_policy_delete(pol->security);
    1679                 :            :                         if (*err) {
    1680                 :            :                                 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    1681                 :            :                                 return pol;
    1682                 :            :                         }
    1683                 :          0 :                         __xfrm_policy_unlink(pol, dir);
    1684                 :            :                 }
    1685                 :            :                 ret = pol;
    1686                 :            :         }
    1687                 :            :         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    1688                 :            : 
    1689                 :          0 :         if (ret && delete)
    1690                 :          0 :                 xfrm_policy_kill(ret);
    1691                 :          0 :         if (bin && delete)
    1692                 :          0 :                 xfrm_policy_inexact_prune_bin(bin);
    1693                 :          0 :         return ret;
    1694                 :            : }
    1695                 :            : EXPORT_SYMBOL(xfrm_policy_bysel_ctx);
    1696                 :            : 
    1697                 :            : struct xfrm_policy *
    1698                 :          0 : xfrm_policy_byid(struct net *net, const struct xfrm_mark *mark, u32 if_id,
    1699                 :            :                  u8 type, int dir, u32 id, int delete, int *err)
    1700                 :            : {
    1701                 :            :         struct xfrm_policy *pol, *ret;
    1702                 :            :         struct hlist_head *chain;
    1703                 :            : 
    1704                 :          0 :         *err = -ENOENT;
    1705                 :          0 :         if (xfrm_policy_id2dir(id) != dir)
    1706                 :            :                 return NULL;
    1707                 :            : 
    1708                 :          0 :         *err = 0;
    1709                 :            :         spin_lock_bh(&net->xfrm.xfrm_policy_lock);
    1710                 :          0 :         chain = net->xfrm.policy_byidx + idx_hash(net, id);
    1711                 :            :         ret = NULL;
    1712                 :          0 :         hlist_for_each_entry(pol, chain, byidx) {
    1713                 :          0 :                 if (pol->type == type && pol->index == id &&
    1714                 :          0 :                     pol->if_id == if_id && xfrm_policy_mark_match(mark, pol)) {
    1715                 :            :                         xfrm_pol_hold(pol);
    1716                 :          0 :                         if (delete) {
    1717                 :          0 :                                 *err = security_xfrm_policy_delete(
    1718                 :            :                                                                 pol->security);
    1719                 :            :                                 if (*err) {
    1720                 :            :                                         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    1721                 :            :                                         return pol;
    1722                 :            :                                 }
    1723                 :          0 :                                 __xfrm_policy_unlink(pol, dir);
    1724                 :            :                         }
    1725                 :          0 :                         ret = pol;
    1726                 :          0 :                         break;
    1727                 :            :                 }
    1728                 :            :         }
    1729                 :            :         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    1730                 :            : 
    1731                 :          0 :         if (ret && delete)
    1732                 :          0 :                 xfrm_policy_kill(ret);
    1733                 :          0 :         return ret;
    1734                 :            : }
    1735                 :            : EXPORT_SYMBOL(xfrm_policy_byid);
    1736                 :            : 
    1737                 :            : #ifdef CONFIG_SECURITY_NETWORK_XFRM
    1738                 :            : static inline int
    1739                 :            : xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
    1740                 :            : {
    1741                 :            :         struct xfrm_policy *pol;
    1742                 :            :         int err = 0;
    1743                 :            : 
    1744                 :            :         list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
    1745                 :            :                 if (pol->walk.dead ||
    1746                 :            :                     xfrm_policy_id2dir(pol->index) >= XFRM_POLICY_MAX ||
    1747                 :            :                     pol->type != type)
    1748                 :            :                         continue;
    1749                 :            : 
    1750                 :            :                 err = security_xfrm_policy_delete(pol->security);
    1751                 :            :                 if (err) {
    1752                 :            :                         xfrm_audit_policy_delete(pol, 0, task_valid);
    1753                 :            :                         return err;
    1754                 :            :                 }
    1755                 :            :         }
    1756                 :            :         return err;
    1757                 :            : }
    1758                 :            : #else
    1759                 :            : static inline int
    1760                 :            : xfrm_policy_flush_secctx_check(struct net *net, u8 type, bool task_valid)
    1761                 :            : {
    1762                 :            :         return 0;
    1763                 :            : }
    1764                 :            : #endif
    1765                 :            : 
    1766                 :          1 : int xfrm_policy_flush(struct net *net, u8 type, bool task_valid)
    1767                 :            : {
    1768                 :            :         int dir, err = 0, cnt = 0;
    1769                 :            :         struct xfrm_policy *pol;
    1770                 :            : 
    1771                 :            :         spin_lock_bh(&net->xfrm.xfrm_policy_lock);
    1772                 :            : 
    1773                 :            :         err = xfrm_policy_flush_secctx_check(net, type, task_valid);
    1774                 :            :         if (err)
    1775                 :            :                 goto out;
    1776                 :            : 
    1777                 :            : again:
    1778                 :          1 :         list_for_each_entry(pol, &net->xfrm.policy_all, walk.all) {
    1779                 :          0 :                 dir = xfrm_policy_id2dir(pol->index);
    1780                 :          0 :                 if (pol->walk.dead ||
    1781                 :          0 :                     dir >= XFRM_POLICY_MAX ||
    1782                 :          0 :                     pol->type != type)
    1783                 :          0 :                         continue;
    1784                 :            : 
    1785                 :          0 :                 __xfrm_policy_unlink(pol, dir);
    1786                 :            :                 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    1787                 :          0 :                 cnt++;
    1788                 :          0 :                 xfrm_audit_policy_delete(pol, 1, task_valid);
    1789                 :          0 :                 xfrm_policy_kill(pol);
    1790                 :            :                 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
    1791                 :            :                 goto again;
    1792                 :            :         }
    1793                 :          1 :         if (cnt)
    1794                 :          0 :                 __xfrm_policy_inexact_flush(net);
    1795                 :            :         else
    1796                 :            :                 err = -ESRCH;
    1797                 :            : out:
    1798                 :            :         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    1799                 :          1 :         return err;
    1800                 :            : }
    1801                 :            : EXPORT_SYMBOL(xfrm_policy_flush);
    1802                 :            : 
    1803                 :          0 : int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
    1804                 :            :                      int (*func)(struct xfrm_policy *, int, int, void*),
    1805                 :            :                      void *data)
    1806                 :            : {
    1807                 :            :         struct xfrm_policy *pol;
    1808                 :            :         struct xfrm_policy_walk_entry *x;
    1809                 :            :         int error = 0;
    1810                 :            : 
    1811                 :          0 :         if (walk->type >= XFRM_POLICY_TYPE_MAX &&
    1812                 :            :             walk->type != XFRM_POLICY_TYPE_ANY)
    1813                 :            :                 return -EINVAL;
    1814                 :            : 
    1815                 :          0 :         if (list_empty(&walk->walk.all) && walk->seq != 0)
    1816                 :            :                 return 0;
    1817                 :            : 
    1818                 :            :         spin_lock_bh(&net->xfrm.xfrm_policy_lock);
    1819                 :          0 :         if (list_empty(&walk->walk.all))
    1820                 :          0 :                 x = list_first_entry(&net->xfrm.policy_all, struct xfrm_policy_walk_entry, all);
    1821                 :            :         else
    1822                 :          0 :                 x = list_first_entry(&walk->walk.all,
    1823                 :            :                                      struct xfrm_policy_walk_entry, all);
    1824                 :            : 
    1825                 :          0 :         list_for_each_entry_from(x, &net->xfrm.policy_all, all) {
    1826                 :          0 :                 if (x->dead)
    1827                 :          0 :                         continue;
    1828                 :          0 :                 pol = container_of(x, struct xfrm_policy, walk);
    1829                 :          0 :                 if (walk->type != XFRM_POLICY_TYPE_ANY &&
    1830                 :          0 :                     walk->type != pol->type)
    1831                 :          0 :                         continue;
    1832                 :          0 :                 error = func(pol, xfrm_policy_id2dir(pol->index),
    1833                 :          0 :                              walk->seq, data);
    1834                 :          0 :                 if (error) {
    1835                 :          0 :                         list_move_tail(&walk->walk.all, &x->all);
    1836                 :            :                         goto out;
    1837                 :            :                 }
    1838                 :          0 :                 walk->seq++;
    1839                 :            :         }
    1840                 :          0 :         if (walk->seq == 0) {
    1841                 :            :                 error = -ENOENT;
    1842                 :            :                 goto out;
    1843                 :            :         }
    1844                 :            :         list_del_init(&walk->walk.all);
    1845                 :            : out:
    1846                 :            :         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    1847                 :          0 :         return error;
    1848                 :            : }
    1849                 :            : EXPORT_SYMBOL(xfrm_policy_walk);
    1850                 :            : 
    1851                 :          0 : void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
    1852                 :            : {
    1853                 :          0 :         INIT_LIST_HEAD(&walk->walk.all);
    1854                 :          0 :         walk->walk.dead = 1;
    1855                 :          0 :         walk->type = type;
    1856                 :          0 :         walk->seq = 0;
    1857                 :          0 : }
    1858                 :            : EXPORT_SYMBOL(xfrm_policy_walk_init);
    1859                 :            : 
    1860                 :          0 : void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net)
    1861                 :            : {
    1862                 :          0 :         if (list_empty(&walk->walk.all))
    1863                 :          0 :                 return;
    1864                 :            : 
    1865                 :            :         spin_lock_bh(&net->xfrm.xfrm_policy_lock); /*FIXME where is net? */
    1866                 :            :         list_del(&walk->walk.all);
    1867                 :            :         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    1868                 :            : }
    1869                 :            : EXPORT_SYMBOL(xfrm_policy_walk_done);
    1870                 :            : 
    1871                 :            : /*
    1872                 :            :  * Find policy to apply to this flow.
    1873                 :            :  *
    1874                 :            :  * Returns 0 if policy found, else an -errno.
    1875                 :            :  */
    1876                 :          0 : static int xfrm_policy_match(const struct xfrm_policy *pol,
    1877                 :            :                              const struct flowi *fl,
    1878                 :            :                              u8 type, u16 family, int dir, u32 if_id)
    1879                 :            : {
    1880                 :          0 :         const struct xfrm_selector *sel = &pol->selector;
    1881                 :            :         int ret = -ESRCH;
    1882                 :            :         bool match;
    1883                 :            : 
    1884                 :          0 :         if (pol->family != family ||
    1885                 :          0 :             pol->if_id != if_id ||
    1886                 :          0 :             (fl->flowi_mark & pol->mark.m) != pol->mark.v ||
    1887                 :          0 :             pol->type != type)
    1888                 :            :                 return ret;
    1889                 :            : 
    1890                 :          0 :         match = xfrm_selector_match(sel, fl, family);
    1891                 :          0 :         if (match)
    1892                 :            :                 ret = security_xfrm_policy_lookup(pol->security, fl->flowi_secid,
    1893                 :            :                                                   dir);
    1894                 :          0 :         return ret;
    1895                 :            : }
    1896                 :            : 
    1897                 :            : static struct xfrm_pol_inexact_node *
    1898                 :          0 : xfrm_policy_lookup_inexact_addr(const struct rb_root *r,
    1899                 :            :                                 seqcount_t *count,
    1900                 :            :                                 const xfrm_address_t *addr, u16 family)
    1901                 :            : {
    1902                 :            :         const struct rb_node *parent;
    1903                 :            :         int seq;
    1904                 :            : 
    1905                 :            : again:
    1906                 :            :         seq = read_seqcount_begin(count);
    1907                 :            : 
    1908                 :          0 :         parent = rcu_dereference_raw(r->rb_node);
    1909                 :          0 :         while (parent) {
    1910                 :            :                 struct xfrm_pol_inexact_node *node;
    1911                 :            :                 int delta;
    1912                 :            : 
    1913                 :            :                 node = rb_entry(parent, struct xfrm_pol_inexact_node, node);
    1914                 :            : 
    1915                 :          0 :                 delta = xfrm_policy_addr_delta(addr, &node->addr,
    1916                 :            :                                                node->prefixlen, family);
    1917                 :          0 :                 if (delta < 0) {
    1918                 :          0 :                         parent = rcu_dereference_raw(parent->rb_left);
    1919                 :          0 :                         continue;
    1920                 :          0 :                 } else if (delta > 0) {
    1921                 :          0 :                         parent = rcu_dereference_raw(parent->rb_right);
    1922                 :          0 :                         continue;
    1923                 :            :                 }
    1924                 :            : 
    1925                 :          0 :                 return node;
    1926                 :            :         }
    1927                 :            : 
    1928                 :          0 :         if (read_seqcount_retry(count, seq))
    1929                 :            :                 goto again;
    1930                 :            : 
    1931                 :            :         return NULL;
    1932                 :            : }
    1933                 :            : 
    1934                 :            : static bool
    1935                 :          0 : xfrm_policy_find_inexact_candidates(struct xfrm_pol_inexact_candidates *cand,
    1936                 :            :                                     struct xfrm_pol_inexact_bin *b,
    1937                 :            :                                     const xfrm_address_t *saddr,
    1938                 :            :                                     const xfrm_address_t *daddr)
    1939                 :            : {
    1940                 :            :         struct xfrm_pol_inexact_node *n;
    1941                 :            :         u16 family;
    1942                 :            : 
    1943                 :          0 :         if (!b)
    1944                 :            :                 return false;
    1945                 :            : 
    1946                 :          0 :         family = b->k.family;
    1947                 :          0 :         memset(cand, 0, sizeof(*cand));
    1948                 :          0 :         cand->res[XFRM_POL_CAND_ANY] = &b->hhead;
    1949                 :            : 
    1950                 :          0 :         n = xfrm_policy_lookup_inexact_addr(&b->root_d, &b->count, daddr,
    1951                 :            :                                             family);
    1952                 :          0 :         if (n) {
    1953                 :          0 :                 cand->res[XFRM_POL_CAND_DADDR] = &n->hhead;
    1954                 :          0 :                 n = xfrm_policy_lookup_inexact_addr(&n->root, &b->count, saddr,
    1955                 :            :                                                     family);
    1956                 :          0 :                 if (n)
    1957                 :          0 :                         cand->res[XFRM_POL_CAND_BOTH] = &n->hhead;
    1958                 :            :         }
    1959                 :            : 
    1960                 :          0 :         n = xfrm_policy_lookup_inexact_addr(&b->root_s, &b->count, saddr,
    1961                 :            :                                             family);
    1962                 :          0 :         if (n)
    1963                 :          0 :                 cand->res[XFRM_POL_CAND_SADDR] = &n->hhead;
    1964                 :            : 
    1965                 :            :         return true;
    1966                 :            : }
    1967                 :            : 
    1968                 :            : static struct xfrm_pol_inexact_bin *
    1969                 :          0 : xfrm_policy_inexact_lookup_rcu(struct net *net, u8 type, u16 family,
    1970                 :            :                                u8 dir, u32 if_id)
    1971                 :            : {
    1972                 :          0 :         struct xfrm_pol_inexact_key k = {
    1973                 :            :                 .family = family,
    1974                 :            :                 .type = type,
    1975                 :            :                 .dir = dir,
    1976                 :            :                 .if_id = if_id,
    1977                 :            :         };
    1978                 :            : 
    1979                 :            :         write_pnet(&k.net, net);
    1980                 :            : 
    1981                 :          0 :         return rhashtable_lookup(&xfrm_policy_inexact_table, &k,
    1982                 :            :                                  xfrm_pol_inexact_params);
    1983                 :            : }
    1984                 :            : 
    1985                 :            : static struct xfrm_pol_inexact_bin *
    1986                 :            : xfrm_policy_inexact_lookup(struct net *net, u8 type, u16 family,
    1987                 :            :                            u8 dir, u32 if_id)
    1988                 :            : {
    1989                 :            :         struct xfrm_pol_inexact_bin *bin;
    1990                 :            : 
    1991                 :            :         lockdep_assert_held(&net->xfrm.xfrm_policy_lock);
    1992                 :            : 
    1993                 :            :         rcu_read_lock();
    1994                 :          0 :         bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
    1995                 :            :         rcu_read_unlock();
    1996                 :            : 
    1997                 :            :         return bin;
    1998                 :            : }
    1999                 :            : 
    2000                 :            : static struct xfrm_policy *
    2001                 :          0 : __xfrm_policy_eval_candidates(struct hlist_head *chain,
    2002                 :            :                               struct xfrm_policy *prefer,
    2003                 :            :                               const struct flowi *fl,
    2004                 :            :                               u8 type, u16 family, int dir, u32 if_id)
    2005                 :            : {
    2006                 :          0 :         u32 priority = prefer ? prefer->priority : ~0u;
    2007                 :            :         struct xfrm_policy *pol;
    2008                 :            : 
    2009                 :          0 :         if (!chain)
    2010                 :            :                 return NULL;
    2011                 :            : 
    2012                 :          0 :         hlist_for_each_entry_rcu(pol, chain, bydst) {
    2013                 :            :                 int err;
    2014                 :            : 
    2015                 :          0 :                 if (pol->priority > priority)
    2016                 :            :                         break;
    2017                 :            : 
    2018                 :          0 :                 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
    2019                 :          0 :                 if (err) {
    2020                 :          0 :                         if (err != -ESRCH)
    2021                 :          0 :                                 return ERR_PTR(err);
    2022                 :            : 
    2023                 :          0 :                         continue;
    2024                 :            :                 }
    2025                 :            : 
    2026                 :          0 :                 if (prefer) {
    2027                 :            :                         /* matches.  Is it older than *prefer? */
    2028                 :          0 :                         if (pol->priority == priority &&
    2029                 :          0 :                             prefer->pos < pol->pos)
    2030                 :            :                                 return prefer;
    2031                 :            :                 }
    2032                 :            : 
    2033                 :          0 :                 return pol;
    2034                 :            :         }
    2035                 :            : 
    2036                 :            :         return NULL;
    2037                 :            : }
    2038                 :            : 
    2039                 :            : static struct xfrm_policy *
    2040                 :          0 : xfrm_policy_eval_candidates(struct xfrm_pol_inexact_candidates *cand,
    2041                 :            :                             struct xfrm_policy *prefer,
    2042                 :            :                             const struct flowi *fl,
    2043                 :            :                             u8 type, u16 family, int dir, u32 if_id)
    2044                 :            : {
    2045                 :            :         struct xfrm_policy *tmp;
    2046                 :            :         int i;
    2047                 :            : 
    2048                 :          0 :         for (i = 0; i < ARRAY_SIZE(cand->res); i++) {
    2049                 :          0 :                 tmp = __xfrm_policy_eval_candidates(cand->res[i],
    2050                 :            :                                                     prefer,
    2051                 :            :                                                     fl, type, family, dir,
    2052                 :            :                                                     if_id);
    2053                 :          0 :                 if (!tmp)
    2054                 :          0 :                         continue;
    2055                 :            : 
    2056                 :          0 :                 if (IS_ERR(tmp))
    2057                 :          0 :                         return tmp;
    2058                 :            :                 prefer = tmp;
    2059                 :            :         }
    2060                 :            : 
    2061                 :          0 :         return prefer;
    2062                 :            : }
    2063                 :            : 
    2064                 :          0 : static struct xfrm_policy *xfrm_policy_lookup_bytype(struct net *net, u8 type,
    2065                 :            :                                                      const struct flowi *fl,
    2066                 :            :                                                      u16 family, u8 dir,
    2067                 :            :                                                      u32 if_id)
    2068                 :            : {
    2069                 :            :         struct xfrm_pol_inexact_candidates cand;
    2070                 :            :         const xfrm_address_t *daddr, *saddr;
    2071                 :            :         struct xfrm_pol_inexact_bin *bin;
    2072                 :            :         struct xfrm_policy *pol, *ret;
    2073                 :            :         struct hlist_head *chain;
    2074                 :            :         unsigned int sequence;
    2075                 :            :         int err;
    2076                 :            : 
    2077                 :            :         daddr = xfrm_flowi_daddr(fl, family);
    2078                 :            :         saddr = xfrm_flowi_saddr(fl, family);
    2079                 :          0 :         if (unlikely(!daddr || !saddr))
    2080                 :            :                 return NULL;
    2081                 :            : 
    2082                 :            :         rcu_read_lock();
    2083                 :            :  retry:
    2084                 :            :         do {
    2085                 :            :                 sequence = read_seqcount_begin(&xfrm_policy_hash_generation);
    2086                 :          0 :                 chain = policy_hash_direct(net, daddr, saddr, family, dir);
    2087                 :          0 :         } while (read_seqcount_retry(&xfrm_policy_hash_generation, sequence));
    2088                 :            : 
    2089                 :            :         ret = NULL;
    2090                 :          0 :         hlist_for_each_entry_rcu(pol, chain, bydst) {
    2091                 :          0 :                 err = xfrm_policy_match(pol, fl, type, family, dir, if_id);
    2092                 :          0 :                 if (err) {
    2093                 :          0 :                         if (err == -ESRCH)
    2094                 :          0 :                                 continue;
    2095                 :            :                         else {
    2096                 :            :                                 ret = ERR_PTR(err);
    2097                 :          0 :                                 goto fail;
    2098                 :            :                         }
    2099                 :            :                 } else {
    2100                 :          0 :                         ret = pol;
    2101                 :          0 :                         break;
    2102                 :            :                 }
    2103                 :            :         }
    2104                 :          0 :         bin = xfrm_policy_inexact_lookup_rcu(net, type, family, dir, if_id);
    2105                 :          0 :         if (!bin || !xfrm_policy_find_inexact_candidates(&cand, bin, saddr,
    2106                 :            :                                                          daddr))
    2107                 :            :                 goto skip_inexact;
    2108                 :            : 
    2109                 :          0 :         pol = xfrm_policy_eval_candidates(&cand, ret, fl, type,
    2110                 :            :                                           family, dir, if_id);
    2111                 :          0 :         if (pol) {
    2112                 :            :                 ret = pol;
    2113                 :          0 :                 if (IS_ERR(pol))
    2114                 :            :                         goto fail;
    2115                 :            :         }
    2116                 :            : 
    2117                 :            : skip_inexact:
    2118                 :          0 :         if (read_seqcount_retry(&xfrm_policy_hash_generation, sequence))
    2119                 :            :                 goto retry;
    2120                 :            : 
    2121                 :          0 :         if (ret && !xfrm_pol_hold_rcu(ret))
    2122                 :            :                 goto retry;
    2123                 :            : fail:
    2124                 :            :         rcu_read_unlock();
    2125                 :            : 
    2126                 :          0 :         return ret;
    2127                 :            : }
    2128                 :            : 
    2129                 :            : static struct xfrm_policy *xfrm_policy_lookup(struct net *net,
    2130                 :            :                                               const struct flowi *fl,
    2131                 :            :                                               u16 family, u8 dir, u32 if_id)
    2132                 :            : {
    2133                 :            : #ifdef CONFIG_XFRM_SUB_POLICY
    2134                 :            :         struct xfrm_policy *pol;
    2135                 :            : 
    2136                 :            :         pol = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_SUB, fl, family,
    2137                 :            :                                         dir, if_id);
    2138                 :            :         if (pol != NULL)
    2139                 :            :                 return pol;
    2140                 :            : #endif
    2141                 :          0 :         return xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN, fl, family,
    2142                 :            :                                          dir, if_id);
    2143                 :            : }
    2144                 :            : 
    2145                 :          0 : static struct xfrm_policy *xfrm_sk_policy_lookup(const struct sock *sk, int dir,
    2146                 :            :                                                  const struct flowi *fl,
    2147                 :            :                                                  u16 family, u32 if_id)
    2148                 :            : {
    2149                 :            :         struct xfrm_policy *pol;
    2150                 :            : 
    2151                 :            :         rcu_read_lock();
    2152                 :            :  again:
    2153                 :          0 :         pol = rcu_dereference(sk->sk_policy[dir]);
    2154                 :          0 :         if (pol != NULL) {
    2155                 :            :                 bool match;
    2156                 :            :                 int err = 0;
    2157                 :            : 
    2158                 :          0 :                 if (pol->family != family) {
    2159                 :            :                         pol = NULL;
    2160                 :            :                         goto out;
    2161                 :            :                 }
    2162                 :            : 
    2163                 :          0 :                 match = xfrm_selector_match(&pol->selector, fl, family);
    2164                 :          0 :                 if (match) {
    2165                 :          0 :                         if ((sk->sk_mark & pol->mark.m) != pol->mark.v ||
    2166                 :          0 :                             pol->if_id != if_id) {
    2167                 :            :                                 pol = NULL;
    2168                 :            :                                 goto out;
    2169                 :            :                         }
    2170                 :            :                         err = security_xfrm_policy_lookup(pol->security,
    2171                 :            :                                                       fl->flowi_secid,
    2172                 :            :                                                       dir);
    2173                 :            :                         if (!err) {
    2174                 :          0 :                                 if (!xfrm_pol_hold_rcu(pol))
    2175                 :            :                                         goto again;
    2176                 :            :                         } else if (err == -ESRCH) {
    2177                 :            :                                 pol = NULL;
    2178                 :            :                         } else {
    2179                 :            :                                 pol = ERR_PTR(err);
    2180                 :            :                         }
    2181                 :            :                 } else
    2182                 :            :                         pol = NULL;
    2183                 :            :         }
    2184                 :            : out:
    2185                 :            :         rcu_read_unlock();
    2186                 :          0 :         return pol;
    2187                 :            : }
    2188                 :            : 
    2189                 :          0 : static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
    2190                 :            : {
    2191                 :            :         struct net *net = xp_net(pol);
    2192                 :            : 
    2193                 :          0 :         list_add(&pol->walk.all, &net->xfrm.policy_all);
    2194                 :          0 :         net->xfrm.policy_count[dir]++;
    2195                 :            :         xfrm_pol_hold(pol);
    2196                 :          0 : }
    2197                 :            : 
    2198                 :          0 : static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
    2199                 :            :                                                 int dir)
    2200                 :            : {
    2201                 :            :         struct net *net = xp_net(pol);
    2202                 :            : 
    2203                 :          0 :         if (list_empty(&pol->walk.all))
    2204                 :            :                 return NULL;
    2205                 :            : 
    2206                 :            :         /* Socket policies are not hashed. */
    2207                 :          0 :         if (!hlist_unhashed(&pol->bydst)) {
    2208                 :            :                 hlist_del_rcu(&pol->bydst);
    2209                 :            :                 hlist_del_init(&pol->bydst_inexact_list);
    2210                 :            :                 hlist_del(&pol->byidx);
    2211                 :            :         }
    2212                 :            : 
    2213                 :            :         list_del_init(&pol->walk.all);
    2214                 :          0 :         net->xfrm.policy_count[dir]--;
    2215                 :            : 
    2216                 :          0 :         return pol;
    2217                 :            : }
    2218                 :            : 
    2219                 :            : static void xfrm_sk_policy_link(struct xfrm_policy *pol, int dir)
    2220                 :            : {
    2221                 :          0 :         __xfrm_policy_link(pol, XFRM_POLICY_MAX + dir);
    2222                 :            : }
    2223                 :            : 
    2224                 :            : static void xfrm_sk_policy_unlink(struct xfrm_policy *pol, int dir)
    2225                 :            : {
    2226                 :          0 :         __xfrm_policy_unlink(pol, XFRM_POLICY_MAX + dir);
    2227                 :            : }
    2228                 :            : 
    2229                 :          0 : int xfrm_policy_delete(struct xfrm_policy *pol, int dir)
    2230                 :            : {
    2231                 :            :         struct net *net = xp_net(pol);
    2232                 :            : 
    2233                 :            :         spin_lock_bh(&net->xfrm.xfrm_policy_lock);
    2234                 :          0 :         pol = __xfrm_policy_unlink(pol, dir);
    2235                 :            :         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    2236                 :          0 :         if (pol) {
    2237                 :          0 :                 xfrm_policy_kill(pol);
    2238                 :          0 :                 return 0;
    2239                 :            :         }
    2240                 :            :         return -ENOENT;
    2241                 :            : }
    2242                 :            : EXPORT_SYMBOL(xfrm_policy_delete);
    2243                 :            : 
    2244                 :          0 : int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
    2245                 :            : {
    2246                 :            :         struct net *net = sock_net(sk);
    2247                 :            :         struct xfrm_policy *old_pol;
    2248                 :            : 
    2249                 :            : #ifdef CONFIG_XFRM_SUB_POLICY
    2250                 :            :         if (pol && pol->type != XFRM_POLICY_TYPE_MAIN)
    2251                 :            :                 return -EINVAL;
    2252                 :            : #endif
    2253                 :            : 
    2254                 :            :         spin_lock_bh(&net->xfrm.xfrm_policy_lock);
    2255                 :          0 :         old_pol = rcu_dereference_protected(sk->sk_policy[dir],
    2256                 :            :                                 lockdep_is_held(&net->xfrm.xfrm_policy_lock));
    2257                 :          0 :         if (pol) {
    2258                 :          0 :                 pol->curlft.add_time = ktime_get_real_seconds();
    2259                 :          0 :                 pol->index = xfrm_gen_index(net, XFRM_POLICY_MAX+dir, 0);
    2260                 :            :                 xfrm_sk_policy_link(pol, dir);
    2261                 :            :         }
    2262                 :          0 :         rcu_assign_pointer(sk->sk_policy[dir], pol);
    2263                 :          0 :         if (old_pol) {
    2264                 :          0 :                 if (pol)
    2265                 :          0 :                         xfrm_policy_requeue(old_pol, pol);
    2266                 :            : 
    2267                 :            :                 /* Unlinking succeeds always. This is the only function
    2268                 :            :                  * allowed to delete or replace socket policy.
    2269                 :            :                  */
    2270                 :            :                 xfrm_sk_policy_unlink(old_pol, dir);
    2271                 :            :         }
    2272                 :            :         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    2273                 :            : 
    2274                 :          0 :         if (old_pol) {
    2275                 :          0 :                 xfrm_policy_kill(old_pol);
    2276                 :            :         }
    2277                 :          0 :         return 0;
    2278                 :            : }
    2279                 :            : 
    2280                 :          0 : static struct xfrm_policy *clone_policy(const struct xfrm_policy *old, int dir)
    2281                 :            : {
    2282                 :          0 :         struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
    2283                 :            :         struct net *net = xp_net(old);
    2284                 :            : 
    2285                 :          0 :         if (newp) {
    2286                 :          0 :                 newp->selector = old->selector;
    2287                 :            :                 if (security_xfrm_policy_clone(old->security,
    2288                 :            :                                                &newp->security)) {
    2289                 :            :                         kfree(newp);
    2290                 :            :                         return NULL;  /* ENOMEM */
    2291                 :            :                 }
    2292                 :          0 :                 newp->lft = old->lft;
    2293                 :          0 :                 newp->curlft = old->curlft;
    2294                 :          0 :                 newp->mark = old->mark;
    2295                 :          0 :                 newp->if_id = old->if_id;
    2296                 :          0 :                 newp->action = old->action;
    2297                 :          0 :                 newp->flags = old->flags;
    2298                 :          0 :                 newp->xfrm_nr = old->xfrm_nr;
    2299                 :          0 :                 newp->index = old->index;
    2300                 :          0 :                 newp->type = old->type;
    2301                 :          0 :                 newp->family = old->family;
    2302                 :          0 :                 memcpy(newp->xfrm_vec, old->xfrm_vec,
    2303                 :            :                        newp->xfrm_nr*sizeof(struct xfrm_tmpl));
    2304                 :            :                 spin_lock_bh(&net->xfrm.xfrm_policy_lock);
    2305                 :            :                 xfrm_sk_policy_link(newp, dir);
    2306                 :            :                 spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    2307                 :          0 :                 xfrm_pol_put(newp);
    2308                 :            :         }
    2309                 :            :         return newp;
    2310                 :            : }
    2311                 :            : 
    2312                 :          0 : int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
    2313                 :            : {
    2314                 :            :         const struct xfrm_policy *p;
    2315                 :            :         struct xfrm_policy *np;
    2316                 :            :         int i, ret = 0;
    2317                 :            : 
    2318                 :            :         rcu_read_lock();
    2319                 :          0 :         for (i = 0; i < 2; i++) {
    2320                 :          0 :                 p = rcu_dereference(osk->sk_policy[i]);
    2321                 :          0 :                 if (p) {
    2322                 :          0 :                         np = clone_policy(p, i);
    2323                 :          0 :                         if (unlikely(!np)) {
    2324                 :            :                                 ret = -ENOMEM;
    2325                 :            :                                 break;
    2326                 :            :                         }
    2327                 :          0 :                         rcu_assign_pointer(sk->sk_policy[i], np);
    2328                 :            :                 }
    2329                 :            :         }
    2330                 :            :         rcu_read_unlock();
    2331                 :          0 :         return ret;
    2332                 :            : }
    2333                 :            : 
    2334                 :            : static int
    2335                 :          0 : xfrm_get_saddr(struct net *net, int oif, xfrm_address_t *local,
    2336                 :            :                xfrm_address_t *remote, unsigned short family, u32 mark)
    2337                 :            : {
    2338                 :            :         int err;
    2339                 :            :         const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
    2340                 :            : 
    2341                 :          0 :         if (unlikely(afinfo == NULL))
    2342                 :            :                 return -EINVAL;
    2343                 :          0 :         err = afinfo->get_saddr(net, oif, local, remote, mark);
    2344                 :            :         rcu_read_unlock();
    2345                 :          0 :         return err;
    2346                 :            : }
    2347                 :            : 
    2348                 :            : /* Resolve list of templates for the flow, given policy. */
    2349                 :            : 
    2350                 :            : static int
    2351                 :          0 : xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl,
    2352                 :            :                       struct xfrm_state **xfrm, unsigned short family)
    2353                 :            : {
    2354                 :            :         struct net *net = xp_net(policy);
    2355                 :            :         int nx;
    2356                 :            :         int i, error;
    2357                 :            :         xfrm_address_t *daddr = xfrm_flowi_daddr(fl, family);
    2358                 :            :         xfrm_address_t *saddr = xfrm_flowi_saddr(fl, family);
    2359                 :            :         xfrm_address_t tmp;
    2360                 :            : 
    2361                 :          0 :         for (nx = 0, i = 0; i < policy->xfrm_nr; i++) {
    2362                 :            :                 struct xfrm_state *x;
    2363                 :            :                 xfrm_address_t *remote = daddr;
    2364                 :            :                 xfrm_address_t *local  = saddr;
    2365                 :          0 :                 struct xfrm_tmpl *tmpl = &policy->xfrm_vec[i];
    2366                 :            : 
    2367                 :          0 :                 if (tmpl->mode == XFRM_MODE_TUNNEL ||
    2368                 :            :                     tmpl->mode == XFRM_MODE_BEET) {
    2369                 :          0 :                         remote = &tmpl->id.daddr;
    2370                 :          0 :                         local = &tmpl->saddr;
    2371                 :          0 :                         if (xfrm_addr_any(local, tmpl->encap_family)) {
    2372                 :          0 :                                 error = xfrm_get_saddr(net, fl->flowi_oif,
    2373                 :            :                                                        &tmp, remote,
    2374                 :            :                                                        tmpl->encap_family, 0);
    2375                 :          0 :                                 if (error)
    2376                 :            :                                         goto fail;
    2377                 :            :                                 local = &tmp;
    2378                 :            :                         }
    2379                 :            :                 }
    2380                 :            : 
    2381                 :          0 :                 x = xfrm_state_find(remote, local, fl, tmpl, policy, &error,
    2382                 :            :                                     family, policy->if_id);
    2383                 :            : 
    2384                 :          0 :                 if (x && x->km.state == XFRM_STATE_VALID) {
    2385                 :          0 :                         xfrm[nx++] = x;
    2386                 :            :                         daddr = remote;
    2387                 :            :                         saddr = local;
    2388                 :          0 :                         continue;
    2389                 :            :                 }
    2390                 :          0 :                 if (x) {
    2391                 :          0 :                         error = (x->km.state == XFRM_STATE_ERROR ?
    2392                 :          0 :                                  -EINVAL : -EAGAIN);
    2393                 :          0 :                         xfrm_state_put(x);
    2394                 :          0 :                 } else if (error == -ESRCH) {
    2395                 :          0 :                         error = -EAGAIN;
    2396                 :            :                 }
    2397                 :            : 
    2398                 :          0 :                 if (!tmpl->optional)
    2399                 :            :                         goto fail;
    2400                 :            :         }
    2401                 :          0 :         return nx;
    2402                 :            : 
    2403                 :            : fail:
    2404                 :          0 :         for (nx--; nx >= 0; nx--)
    2405                 :          0 :                 xfrm_state_put(xfrm[nx]);
    2406                 :          0 :         return error;
    2407                 :            : }
    2408                 :            : 
    2409                 :            : static int
    2410                 :          0 : xfrm_tmpl_resolve(struct xfrm_policy **pols, int npols, const struct flowi *fl,
    2411                 :            :                   struct xfrm_state **xfrm, unsigned short family)
    2412                 :            : {
    2413                 :            :         struct xfrm_state *tp[XFRM_MAX_DEPTH];
    2414                 :          0 :         struct xfrm_state **tpp = (npols > 1) ? tp : xfrm;
    2415                 :            :         int cnx = 0;
    2416                 :            :         int error;
    2417                 :            :         int ret;
    2418                 :            :         int i;
    2419                 :            : 
    2420                 :          0 :         for (i = 0; i < npols; i++) {
    2421                 :          0 :                 if (cnx + pols[i]->xfrm_nr >= XFRM_MAX_DEPTH) {
    2422                 :            :                         error = -ENOBUFS;
    2423                 :            :                         goto fail;
    2424                 :            :                 }
    2425                 :            : 
    2426                 :          0 :                 ret = xfrm_tmpl_resolve_one(pols[i], fl, &tpp[cnx], family);
    2427                 :          0 :                 if (ret < 0) {
    2428                 :          0 :                         error = ret;
    2429                 :          0 :                         goto fail;
    2430                 :            :                 } else
    2431                 :          0 :                         cnx += ret;
    2432                 :            :         }
    2433                 :            : 
    2434                 :            :         /* found states are sorted for outbound processing */
    2435                 :            :         if (npols > 1)
    2436                 :            :                 xfrm_state_sort(xfrm, tpp, cnx, family);
    2437                 :            : 
    2438                 :          0 :         return cnx;
    2439                 :            : 
    2440                 :            :  fail:
    2441                 :          0 :         for (cnx--; cnx >= 0; cnx--)
    2442                 :          0 :                 xfrm_state_put(tpp[cnx]);
    2443                 :            :         return error;
    2444                 :            : 
    2445                 :            : }
    2446                 :            : 
    2447                 :            : static int xfrm_get_tos(const struct flowi *fl, int family)
    2448                 :            : {
    2449                 :          0 :         if (family == AF_INET)
    2450                 :          0 :                 return IPTOS_RT_MASK & fl->u.ip4.flowi4_tos;
    2451                 :            : 
    2452                 :            :         return 0;
    2453                 :            : }
    2454                 :            : 
    2455                 :          0 : static inline struct xfrm_dst *xfrm_alloc_dst(struct net *net, int family)
    2456                 :            : {
    2457                 :          0 :         const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
    2458                 :            :         struct dst_ops *dst_ops;
    2459                 :            :         struct xfrm_dst *xdst;
    2460                 :            : 
    2461                 :          0 :         if (!afinfo)
    2462                 :            :                 return ERR_PTR(-EINVAL);
    2463                 :            : 
    2464                 :          0 :         switch (family) {
    2465                 :            :         case AF_INET:
    2466                 :          0 :                 dst_ops = &net->xfrm.xfrm4_dst_ops;
    2467                 :          0 :                 break;
    2468                 :            : #if IS_ENABLED(CONFIG_IPV6)
    2469                 :            :         case AF_INET6:
    2470                 :          0 :                 dst_ops = &net->xfrm.xfrm6_dst_ops;
    2471                 :          0 :                 break;
    2472                 :            : #endif
    2473                 :            :         default:
    2474                 :          0 :                 BUG();
    2475                 :            :         }
    2476                 :          0 :         xdst = dst_alloc(dst_ops, NULL, 1, DST_OBSOLETE_NONE, 0);
    2477                 :            : 
    2478                 :          0 :         if (likely(xdst)) {
    2479                 :            :                 struct dst_entry *dst = &xdst->u.dst;
    2480                 :            : 
    2481                 :          0 :                 memset(dst + 1, 0, sizeof(*xdst) - sizeof(*dst));
    2482                 :            :         } else
    2483                 :            :                 xdst = ERR_PTR(-ENOBUFS);
    2484                 :            : 
    2485                 :            :         rcu_read_unlock();
    2486                 :            : 
    2487                 :          0 :         return xdst;
    2488                 :            : }
    2489                 :            : 
    2490                 :            : static void xfrm_init_path(struct xfrm_dst *path, struct dst_entry *dst,
    2491                 :            :                            int nfheader_len)
    2492                 :            : {
    2493                 :          0 :         if (dst->ops->family == AF_INET6) {
    2494                 :            :                 struct rt6_info *rt = (struct rt6_info *)dst;
    2495                 :          0 :                 path->path_cookie = rt6_get_cookie(rt);
    2496                 :          0 :                 path->u.rt6.rt6i_nfheader_len = nfheader_len;
    2497                 :            :         }
    2498                 :            : }
    2499                 :            : 
    2500                 :          0 : static inline int xfrm_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
    2501                 :            :                                 const struct flowi *fl)
    2502                 :            : {
    2503                 :            :         const struct xfrm_policy_afinfo *afinfo =
    2504                 :          0 :                 xfrm_policy_get_afinfo(xdst->u.dst.ops->family);
    2505                 :            :         int err;
    2506                 :            : 
    2507                 :          0 :         if (!afinfo)
    2508                 :            :                 return -EINVAL;
    2509                 :            : 
    2510                 :          0 :         err = afinfo->fill_dst(xdst, dev, fl);
    2511                 :            : 
    2512                 :            :         rcu_read_unlock();
    2513                 :            : 
    2514                 :          0 :         return err;
    2515                 :            : }
    2516                 :            : 
    2517                 :            : 
    2518                 :            : /* Allocate chain of dst_entry's, attach known xfrm's, calculate
    2519                 :            :  * all the metrics... Shortly, bundle a bundle.
    2520                 :            :  */
    2521                 :            : 
    2522                 :          0 : static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
    2523                 :            :                                             struct xfrm_state **xfrm,
    2524                 :            :                                             struct xfrm_dst **bundle,
    2525                 :            :                                             int nx,
    2526                 :            :                                             const struct flowi *fl,
    2527                 :            :                                             struct dst_entry *dst)
    2528                 :            : {
    2529                 :            :         const struct xfrm_state_afinfo *afinfo;
    2530                 :            :         const struct xfrm_mode *inner_mode;
    2531                 :            :         struct net *net = xp_net(policy);
    2532                 :          0 :         unsigned long now = jiffies;
    2533                 :            :         struct net_device *dev;
    2534                 :            :         struct xfrm_dst *xdst_prev = NULL;
    2535                 :            :         struct xfrm_dst *xdst0 = NULL;
    2536                 :            :         int i = 0;
    2537                 :            :         int err;
    2538                 :            :         int header_len = 0;
    2539                 :            :         int nfheader_len = 0;
    2540                 :            :         int trailer_len = 0;
    2541                 :            :         int tos;
    2542                 :          0 :         int family = policy->selector.family;
    2543                 :            :         xfrm_address_t saddr, daddr;
    2544                 :            : 
    2545                 :          0 :         xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
    2546                 :            : 
    2547                 :            :         tos = xfrm_get_tos(fl, family);
    2548                 :            : 
    2549                 :          0 :         dst_hold(dst);
    2550                 :            : 
    2551                 :          0 :         for (; i < nx; i++) {
    2552                 :          0 :                 struct xfrm_dst *xdst = xfrm_alloc_dst(net, family);
    2553                 :          0 :                 struct dst_entry *dst1 = &xdst->u.dst;
    2554                 :            : 
    2555                 :            :                 err = PTR_ERR(xdst);
    2556                 :          0 :                 if (IS_ERR(xdst)) {
    2557                 :          0 :                         dst_release(dst);
    2558                 :          0 :                         goto put_states;
    2559                 :            :                 }
    2560                 :            : 
    2561                 :          0 :                 bundle[i] = xdst;
    2562                 :          0 :                 if (!xdst_prev)
    2563                 :            :                         xdst0 = xdst;
    2564                 :            :                 else
    2565                 :            :                         /* Ref count is taken during xfrm_alloc_dst()
    2566                 :            :                          * No need to do dst_clone() on dst1
    2567                 :            :                          */
    2568                 :            :                         xfrm_dst_set_child(xdst_prev, &xdst->u.dst);
    2569                 :            : 
    2570                 :          0 :                 if (xfrm[i]->sel.family == AF_UNSPEC) {
    2571                 :          0 :                         inner_mode = xfrm_ip2inner_mode(xfrm[i],
    2572                 :            :                                                         xfrm_af2proto(family));
    2573                 :          0 :                         if (!inner_mode) {
    2574                 :            :                                 err = -EAFNOSUPPORT;
    2575                 :          0 :                                 dst_release(dst);
    2576                 :          0 :                                 goto put_states;
    2577                 :            :                         }
    2578                 :            :                 } else
    2579                 :          0 :                         inner_mode = &xfrm[i]->inner_mode;
    2580                 :            : 
    2581                 :          0 :                 xdst->route = dst;
    2582                 :          0 :                 dst_copy_metrics(dst1, dst);
    2583                 :            : 
    2584                 :          0 :                 if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
    2585                 :            :                         __u32 mark = 0;
    2586                 :            : 
    2587                 :          0 :                         if (xfrm[i]->props.smark.v || xfrm[i]->props.smark.m)
    2588                 :          0 :                                 mark = xfrm_smark_get(fl->flowi_mark, xfrm[i]);
    2589                 :            : 
    2590                 :          0 :                         family = xfrm[i]->props.family;
    2591                 :          0 :                         dst = xfrm_dst_lookup(xfrm[i], tos, fl->flowi_oif,
    2592                 :            :                                               &saddr, &daddr, family, mark);
    2593                 :            :                         err = PTR_ERR(dst);
    2594                 :          0 :                         if (IS_ERR(dst))
    2595                 :            :                                 goto put_states;
    2596                 :            :                 } else
    2597                 :          0 :                         dst_hold(dst);
    2598                 :            : 
    2599                 :          0 :                 dst1->xfrm = xfrm[i];
    2600                 :          0 :                 xdst->xfrm_genid = xfrm[i]->genid;
    2601                 :            : 
    2602                 :          0 :                 dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
    2603                 :          0 :                 dst1->flags |= DST_HOST;
    2604                 :          0 :                 dst1->lastuse = now;
    2605                 :            : 
    2606                 :          0 :                 dst1->input = dst_discard;
    2607                 :            : 
    2608                 :            :                 rcu_read_lock();
    2609                 :          0 :                 afinfo = xfrm_state_afinfo_get_rcu(inner_mode->family);
    2610                 :          0 :                 if (likely(afinfo))
    2611                 :          0 :                         dst1->output = afinfo->output;
    2612                 :            :                 else
    2613                 :          0 :                         dst1->output = dst_discard_out;
    2614                 :            :                 rcu_read_unlock();
    2615                 :            : 
    2616                 :            :                 xdst_prev = xdst;
    2617                 :            : 
    2618                 :          0 :                 header_len += xfrm[i]->props.header_len;
    2619                 :          0 :                 if (xfrm[i]->type->flags & XFRM_TYPE_NON_FRAGMENT)
    2620                 :          0 :                         nfheader_len += xfrm[i]->props.header_len;
    2621                 :          0 :                 trailer_len += xfrm[i]->props.trailer_len;
    2622                 :            :         }
    2623                 :            : 
    2624                 :            :         xfrm_dst_set_child(xdst_prev, dst);
    2625                 :          0 :         xdst0->path = dst;
    2626                 :            : 
    2627                 :            :         err = -ENODEV;
    2628                 :          0 :         dev = dst->dev;
    2629                 :          0 :         if (!dev)
    2630                 :            :                 goto free_dst;
    2631                 :            : 
    2632                 :            :         xfrm_init_path(xdst0, dst, nfheader_len);
    2633                 :          0 :         xfrm_init_pmtu(bundle, nx);
    2634                 :            : 
    2635                 :          0 :         for (xdst_prev = xdst0; xdst_prev != (struct xfrm_dst *)dst;
    2636                 :            :              xdst_prev = (struct xfrm_dst *) xfrm_dst_child(&xdst_prev->u.dst)) {
    2637                 :          0 :                 err = xfrm_fill_dst(xdst_prev, dev, fl);
    2638                 :          0 :                 if (err)
    2639                 :            :                         goto free_dst;
    2640                 :            : 
    2641                 :          0 :                 xdst_prev->u.dst.header_len = header_len;
    2642                 :          0 :                 xdst_prev->u.dst.trailer_len = trailer_len;
    2643                 :          0 :                 header_len -= xdst_prev->u.dst.xfrm->props.header_len;
    2644                 :          0 :                 trailer_len -= xdst_prev->u.dst.xfrm->props.trailer_len;
    2645                 :            :         }
    2646                 :            : 
    2647                 :          0 :         return &xdst0->u.dst;
    2648                 :            : 
    2649                 :            : put_states:
    2650                 :          0 :         for (; i < nx; i++)
    2651                 :          0 :                 xfrm_state_put(xfrm[i]);
    2652                 :            : free_dst:
    2653                 :          0 :         if (xdst0)
    2654                 :          0 :                 dst_release_immediate(&xdst0->u.dst);
    2655                 :            : 
    2656                 :          0 :         return ERR_PTR(err);
    2657                 :            : }
    2658                 :            : 
    2659                 :          0 : static int xfrm_expand_policies(const struct flowi *fl, u16 family,
    2660                 :            :                                 struct xfrm_policy **pols,
    2661                 :            :                                 int *num_pols, int *num_xfrms)
    2662                 :            : {
    2663                 :            :         int i;
    2664                 :            : 
    2665                 :          0 :         if (*num_pols == 0 || !pols[0]) {
    2666                 :          0 :                 *num_pols = 0;
    2667                 :          0 :                 *num_xfrms = 0;
    2668                 :          0 :                 return 0;
    2669                 :            :         }
    2670                 :          0 :         if (IS_ERR(pols[0]))
    2671                 :          0 :                 return PTR_ERR(pols[0]);
    2672                 :            : 
    2673                 :          0 :         *num_xfrms = pols[0]->xfrm_nr;
    2674                 :            : 
    2675                 :            : #ifdef CONFIG_XFRM_SUB_POLICY
    2676                 :            :         if (pols[0] && pols[0]->action == XFRM_POLICY_ALLOW &&
    2677                 :            :             pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
    2678                 :            :                 pols[1] = xfrm_policy_lookup_bytype(xp_net(pols[0]),
    2679                 :            :                                                     XFRM_POLICY_TYPE_MAIN,
    2680                 :            :                                                     fl, family,
    2681                 :            :                                                     XFRM_POLICY_OUT,
    2682                 :            :                                                     pols[0]->if_id);
    2683                 :            :                 if (pols[1]) {
    2684                 :            :                         if (IS_ERR(pols[1])) {
    2685                 :            :                                 xfrm_pols_put(pols, *num_pols);
    2686                 :            :                                 return PTR_ERR(pols[1]);
    2687                 :            :                         }
    2688                 :            :                         (*num_pols)++;
    2689                 :            :                         (*num_xfrms) += pols[1]->xfrm_nr;
    2690                 :            :                 }
    2691                 :            :         }
    2692                 :            : #endif
    2693                 :          0 :         for (i = 0; i < *num_pols; i++) {
    2694                 :          0 :                 if (pols[i]->action != XFRM_POLICY_ALLOW) {
    2695                 :          0 :                         *num_xfrms = -1;
    2696                 :          0 :                         break;
    2697                 :            :                 }
    2698                 :            :         }
    2699                 :            : 
    2700                 :            :         return 0;
    2701                 :            : 
    2702                 :            : }
    2703                 :            : 
    2704                 :            : static struct xfrm_dst *
    2705                 :          0 : xfrm_resolve_and_create_bundle(struct xfrm_policy **pols, int num_pols,
    2706                 :            :                                const struct flowi *fl, u16 family,
    2707                 :            :                                struct dst_entry *dst_orig)
    2708                 :            : {
    2709                 :            :         struct net *net = xp_net(pols[0]);
    2710                 :            :         struct xfrm_state *xfrm[XFRM_MAX_DEPTH];
    2711                 :            :         struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
    2712                 :            :         struct xfrm_dst *xdst;
    2713                 :            :         struct dst_entry *dst;
    2714                 :            :         int err;
    2715                 :            : 
    2716                 :            :         /* Try to instantiate a bundle */
    2717                 :          0 :         err = xfrm_tmpl_resolve(pols, num_pols, fl, xfrm, family);
    2718                 :          0 :         if (err <= 0) {
    2719                 :          0 :                 if (err == 0)
    2720                 :            :                         return NULL;
    2721                 :            : 
    2722                 :            :                 if (err != -EAGAIN)
    2723                 :            :                         XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
    2724                 :          0 :                 return ERR_PTR(err);
    2725                 :            :         }
    2726                 :            : 
    2727                 :          0 :         dst = xfrm_bundle_create(pols[0], xfrm, bundle, err, fl, dst_orig);
    2728                 :          0 :         if (IS_ERR(dst)) {
    2729                 :            :                 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTBUNDLEGENERROR);
    2730                 :            :                 return ERR_CAST(dst);
    2731                 :            :         }
    2732                 :            : 
    2733                 :            :         xdst = (struct xfrm_dst *)dst;
    2734                 :          0 :         xdst->num_xfrms = err;
    2735                 :          0 :         xdst->num_pols = num_pols;
    2736                 :          0 :         memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
    2737                 :          0 :         xdst->policy_genid = atomic_read(&pols[0]->genid);
    2738                 :            : 
    2739                 :          0 :         return xdst;
    2740                 :            : }
    2741                 :            : 
    2742                 :          0 : static void xfrm_policy_queue_process(struct timer_list *t)
    2743                 :            : {
    2744                 :            :         struct sk_buff *skb;
    2745                 :            :         struct sock *sk;
    2746                 :            :         struct dst_entry *dst;
    2747                 :          0 :         struct xfrm_policy *pol = from_timer(pol, t, polq.hold_timer);
    2748                 :            :         struct net *net = xp_net(pol);
    2749                 :            :         struct xfrm_policy_queue *pq = &pol->polq;
    2750                 :            :         struct flowi fl;
    2751                 :            :         struct sk_buff_head list;
    2752                 :            : 
    2753                 :            :         spin_lock(&pq->hold_queue.lock);
    2754                 :          0 :         skb = skb_peek(&pq->hold_queue);
    2755                 :          0 :         if (!skb) {
    2756                 :            :                 spin_unlock(&pq->hold_queue.lock);
    2757                 :            :                 goto out;
    2758                 :            :         }
    2759                 :            :         dst = skb_dst(skb);
    2760                 :          0 :         sk = skb->sk;
    2761                 :          0 :         xfrm_decode_session(skb, &fl, dst->ops->family);
    2762                 :            :         spin_unlock(&pq->hold_queue.lock);
    2763                 :            : 
    2764                 :          0 :         dst_hold(xfrm_dst_path(dst));
    2765                 :            :         dst = xfrm_lookup(net, xfrm_dst_path(dst), &fl, sk, XFRM_LOOKUP_QUEUE);
    2766                 :          0 :         if (IS_ERR(dst))
    2767                 :            :                 goto purge_queue;
    2768                 :            : 
    2769                 :          0 :         if (dst->flags & DST_XFRM_QUEUE) {
    2770                 :          0 :                 dst_release(dst);
    2771                 :            : 
    2772                 :          0 :                 if (pq->timeout >= XFRM_QUEUE_TMO_MAX)
    2773                 :            :                         goto purge_queue;
    2774                 :            : 
    2775                 :          0 :                 pq->timeout = pq->timeout << 1;
    2776                 :          0 :                 if (!mod_timer(&pq->hold_timer, jiffies + pq->timeout))
    2777                 :            :                         xfrm_pol_hold(pol);
    2778                 :            :                 goto out;
    2779                 :            :         }
    2780                 :            : 
    2781                 :          0 :         dst_release(dst);
    2782                 :            : 
    2783                 :            :         __skb_queue_head_init(&list);
    2784                 :            : 
    2785                 :            :         spin_lock(&pq->hold_queue.lock);
    2786                 :          0 :         pq->timeout = 0;
    2787                 :            :         skb_queue_splice_init(&pq->hold_queue, &list);
    2788                 :            :         spin_unlock(&pq->hold_queue.lock);
    2789                 :            : 
    2790                 :          0 :         while (!skb_queue_empty(&list)) {
    2791                 :            :                 skb = __skb_dequeue(&list);
    2792                 :            : 
    2793                 :          0 :                 xfrm_decode_session(skb, &fl, skb_dst(skb)->ops->family);
    2794                 :          0 :                 dst_hold(xfrm_dst_path(skb_dst(skb)));
    2795                 :          0 :                 dst = xfrm_lookup(net, xfrm_dst_path(skb_dst(skb)), &fl, skb->sk, 0);
    2796                 :          0 :                 if (IS_ERR(dst)) {
    2797                 :          0 :                         kfree_skb(skb);
    2798                 :          0 :                         continue;
    2799                 :            :                 }
    2800                 :            : 
    2801                 :            :                 nf_reset_ct(skb);
    2802                 :          0 :                 skb_dst_drop(skb);
    2803                 :            :                 skb_dst_set(skb, dst);
    2804                 :            : 
    2805                 :          0 :                 dst_output(net, skb->sk, skb);
    2806                 :            :         }
    2807                 :            : 
    2808                 :            : out:
    2809                 :          0 :         xfrm_pol_put(pol);
    2810                 :          0 :         return;
    2811                 :            : 
    2812                 :            : purge_queue:
    2813                 :          0 :         pq->timeout = 0;
    2814                 :          0 :         skb_queue_purge(&pq->hold_queue);
    2815                 :          0 :         xfrm_pol_put(pol);
    2816                 :            : }
    2817                 :            : 
    2818                 :          0 : static int xdst_queue_output(struct net *net, struct sock *sk, struct sk_buff *skb)
    2819                 :            : {
    2820                 :            :         unsigned long sched_next;
    2821                 :            :         struct dst_entry *dst = skb_dst(skb);
    2822                 :            :         struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
    2823                 :          0 :         struct xfrm_policy *pol = xdst->pols[0];
    2824                 :            :         struct xfrm_policy_queue *pq = &pol->polq;
    2825                 :            : 
    2826                 :          0 :         if (unlikely(skb_fclone_busy(sk, skb))) {
    2827                 :          0 :                 kfree_skb(skb);
    2828                 :          0 :                 return 0;
    2829                 :            :         }
    2830                 :            : 
    2831                 :          0 :         if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
    2832                 :          0 :                 kfree_skb(skb);
    2833                 :          0 :                 return -EAGAIN;
    2834                 :            :         }
    2835                 :            : 
    2836                 :          0 :         skb_dst_force(skb);
    2837                 :            : 
    2838                 :            :         spin_lock_bh(&pq->hold_queue.lock);
    2839                 :            : 
    2840                 :          0 :         if (!pq->timeout)
    2841                 :          0 :                 pq->timeout = XFRM_QUEUE_TMO_MIN;
    2842                 :            : 
    2843                 :          0 :         sched_next = jiffies + pq->timeout;
    2844                 :            : 
    2845                 :          0 :         if (del_timer(&pq->hold_timer)) {
    2846                 :          0 :                 if (time_before(pq->hold_timer.expires, sched_next))
    2847                 :            :                         sched_next = pq->hold_timer.expires;
    2848                 :          0 :                 xfrm_pol_put(pol);
    2849                 :            :         }
    2850                 :            : 
    2851                 :          0 :         __skb_queue_tail(&pq->hold_queue, skb);
    2852                 :          0 :         if (!mod_timer(&pq->hold_timer, sched_next))
    2853                 :            :                 xfrm_pol_hold(pol);
    2854                 :            : 
    2855                 :            :         spin_unlock_bh(&pq->hold_queue.lock);
    2856                 :            : 
    2857                 :          0 :         return 0;
    2858                 :            : }
    2859                 :            : 
    2860                 :          0 : static struct xfrm_dst *xfrm_create_dummy_bundle(struct net *net,
    2861                 :            :                                                  struct xfrm_flo *xflo,
    2862                 :            :                                                  const struct flowi *fl,
    2863                 :            :                                                  int num_xfrms,
    2864                 :            :                                                  u16 family)
    2865                 :            : {
    2866                 :            :         int err;
    2867                 :            :         struct net_device *dev;
    2868                 :            :         struct dst_entry *dst;
    2869                 :            :         struct dst_entry *dst1;
    2870                 :            :         struct xfrm_dst *xdst;
    2871                 :            : 
    2872                 :          0 :         xdst = xfrm_alloc_dst(net, family);
    2873                 :          0 :         if (IS_ERR(xdst))
    2874                 :            :                 return xdst;
    2875                 :            : 
    2876                 :          0 :         if (!(xflo->flags & XFRM_LOOKUP_QUEUE) ||
    2877                 :          0 :             net->xfrm.sysctl_larval_drop ||
    2878                 :            :             num_xfrms <= 0)
    2879                 :            :                 return xdst;
    2880                 :            : 
    2881                 :          0 :         dst = xflo->dst_orig;
    2882                 :          0 :         dst1 = &xdst->u.dst;
    2883                 :          0 :         dst_hold(dst);
    2884                 :          0 :         xdst->route = dst;
    2885                 :            : 
    2886                 :          0 :         dst_copy_metrics(dst1, dst);
    2887                 :            : 
    2888                 :          0 :         dst1->obsolete = DST_OBSOLETE_FORCE_CHK;
    2889                 :          0 :         dst1->flags |= DST_HOST | DST_XFRM_QUEUE;
    2890                 :          0 :         dst1->lastuse = jiffies;
    2891                 :            : 
    2892                 :          0 :         dst1->input = dst_discard;
    2893                 :          0 :         dst1->output = xdst_queue_output;
    2894                 :            : 
    2895                 :          0 :         dst_hold(dst);
    2896                 :            :         xfrm_dst_set_child(xdst, dst);
    2897                 :          0 :         xdst->path = dst;
    2898                 :            : 
    2899                 :            :         xfrm_init_path((struct xfrm_dst *)dst1, dst, 0);
    2900                 :            : 
    2901                 :            :         err = -ENODEV;
    2902                 :          0 :         dev = dst->dev;
    2903                 :          0 :         if (!dev)
    2904                 :            :                 goto free_dst;
    2905                 :            : 
    2906                 :          0 :         err = xfrm_fill_dst(xdst, dev, fl);
    2907                 :          0 :         if (err)
    2908                 :            :                 goto free_dst;
    2909                 :            : 
    2910                 :            : out:
    2911                 :          0 :         return xdst;
    2912                 :            : 
    2913                 :            : free_dst:
    2914                 :          0 :         dst_release(dst1);
    2915                 :            :         xdst = ERR_PTR(err);
    2916                 :          0 :         goto out;
    2917                 :            : }
    2918                 :            : 
    2919                 :          0 : static struct xfrm_dst *xfrm_bundle_lookup(struct net *net,
    2920                 :            :                                            const struct flowi *fl,
    2921                 :            :                                            u16 family, u8 dir,
    2922                 :            :                                            struct xfrm_flo *xflo, u32 if_id)
    2923                 :            : {
    2924                 :            :         struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
    2925                 :          0 :         int num_pols = 0, num_xfrms = 0, err;
    2926                 :            :         struct xfrm_dst *xdst;
    2927                 :            : 
    2928                 :            :         /* Resolve policies to use if we couldn't get them from
    2929                 :            :          * previous cache entry */
    2930                 :          0 :         num_pols = 1;
    2931                 :          0 :         pols[0] = xfrm_policy_lookup(net, fl, family, dir, if_id);
    2932                 :          0 :         err = xfrm_expand_policies(fl, family, pols,
    2933                 :            :                                            &num_pols, &num_xfrms);
    2934                 :          0 :         if (err < 0)
    2935                 :            :                 goto inc_error;
    2936                 :          0 :         if (num_pols == 0)
    2937                 :            :                 return NULL;
    2938                 :          0 :         if (num_xfrms <= 0)
    2939                 :            :                 goto make_dummy_bundle;
    2940                 :            : 
    2941                 :          0 :         xdst = xfrm_resolve_and_create_bundle(pols, num_pols, fl, family,
    2942                 :            :                                               xflo->dst_orig);
    2943                 :          0 :         if (IS_ERR(xdst)) {
    2944                 :            :                 err = PTR_ERR(xdst);
    2945                 :          0 :                 if (err == -EREMOTE) {
    2946                 :          0 :                         xfrm_pols_put(pols, num_pols);
    2947                 :          0 :                         return NULL;
    2948                 :            :                 }
    2949                 :            : 
    2950                 :          0 :                 if (err != -EAGAIN)
    2951                 :            :                         goto error;
    2952                 :            :                 goto make_dummy_bundle;
    2953                 :          0 :         } else if (xdst == NULL) {
    2954                 :          0 :                 num_xfrms = 0;
    2955                 :          0 :                 goto make_dummy_bundle;
    2956                 :            :         }
    2957                 :            : 
    2958                 :            :         return xdst;
    2959                 :            : 
    2960                 :            : make_dummy_bundle:
    2961                 :            :         /* We found policies, but there's no bundles to instantiate:
    2962                 :            :          * either because the policy blocks, has no transformations or
    2963                 :            :          * we could not build template (no xfrm_states).*/
    2964                 :          0 :         xdst = xfrm_create_dummy_bundle(net, xflo, fl, num_xfrms, family);
    2965                 :          0 :         if (IS_ERR(xdst)) {
    2966                 :          0 :                 xfrm_pols_put(pols, num_pols);
    2967                 :          0 :                 return ERR_CAST(xdst);
    2968                 :            :         }
    2969                 :          0 :         xdst->num_pols = num_pols;
    2970                 :          0 :         xdst->num_xfrms = num_xfrms;
    2971                 :          0 :         memcpy(xdst->pols, pols, sizeof(struct xfrm_policy *) * num_pols);
    2972                 :            : 
    2973                 :          0 :         return xdst;
    2974                 :            : 
    2975                 :            : inc_error:
    2976                 :            :         XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLERROR);
    2977                 :            : error:
    2978                 :          0 :         xfrm_pols_put(pols, num_pols);
    2979                 :          0 :         return ERR_PTR(err);
    2980                 :            : }
    2981                 :            : 
    2982                 :          0 : static struct dst_entry *make_blackhole(struct net *net, u16 family,
    2983                 :            :                                         struct dst_entry *dst_orig)
    2984                 :            : {
    2985                 :            :         const struct xfrm_policy_afinfo *afinfo = xfrm_policy_get_afinfo(family);
    2986                 :            :         struct dst_entry *ret;
    2987                 :            : 
    2988                 :          0 :         if (!afinfo) {
    2989                 :          0 :                 dst_release(dst_orig);
    2990                 :          0 :                 return ERR_PTR(-EINVAL);
    2991                 :            :         } else {
    2992                 :          0 :                 ret = afinfo->blackhole_route(net, dst_orig);
    2993                 :            :         }
    2994                 :            :         rcu_read_unlock();
    2995                 :            : 
    2996                 :          0 :         return ret;
    2997                 :            : }
    2998                 :            : 
    2999                 :            : /* Finds/creates a bundle for given flow and if_id
    3000                 :            :  *
    3001                 :            :  * At the moment we eat a raw IP route. Mostly to speed up lookups
    3002                 :            :  * on interfaces with disabled IPsec.
    3003                 :            :  *
    3004                 :            :  * xfrm_lookup uses an if_id of 0 by default, and is provided for
    3005                 :            :  * compatibility
    3006                 :            :  */
    3007                 :          3 : struct dst_entry *xfrm_lookup_with_ifid(struct net *net,
    3008                 :            :                                         struct dst_entry *dst_orig,
    3009                 :            :                                         const struct flowi *fl,
    3010                 :            :                                         const struct sock *sk,
    3011                 :            :                                         int flags, u32 if_id)
    3012                 :            : {
    3013                 :            :         struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
    3014                 :            :         struct xfrm_dst *xdst;
    3015                 :            :         struct dst_entry *dst, *route;
    3016                 :          3 :         u16 family = dst_orig->ops->family;
    3017                 :            :         u8 dir = XFRM_POLICY_OUT;
    3018                 :          3 :         int i, err, num_pols, num_xfrms = 0, drop_pols = 0;
    3019                 :            : 
    3020                 :            :         dst = NULL;
    3021                 :            :         xdst = NULL;
    3022                 :            :         route = NULL;
    3023                 :            : 
    3024                 :            :         sk = sk_const_to_full_sk(sk);
    3025                 :          3 :         if (sk && sk->sk_policy[XFRM_POLICY_OUT]) {
    3026                 :          0 :                 num_pols = 1;
    3027                 :          0 :                 pols[0] = xfrm_sk_policy_lookup(sk, XFRM_POLICY_OUT, fl, family,
    3028                 :            :                                                 if_id);
    3029                 :          0 :                 err = xfrm_expand_policies(fl, family, pols,
    3030                 :            :                                            &num_pols, &num_xfrms);
    3031                 :          0 :                 if (err < 0)
    3032                 :            :                         goto dropdst;
    3033                 :            : 
    3034                 :          0 :                 if (num_pols) {
    3035                 :          0 :                         if (num_xfrms <= 0) {
    3036                 :            :                                 drop_pols = num_pols;
    3037                 :            :                                 goto no_transform;
    3038                 :            :                         }
    3039                 :            : 
    3040                 :          0 :                         xdst = xfrm_resolve_and_create_bundle(
    3041                 :            :                                         pols, num_pols, fl,
    3042                 :            :                                         family, dst_orig);
    3043                 :            : 
    3044                 :          0 :                         if (IS_ERR(xdst)) {
    3045                 :          0 :                                 xfrm_pols_put(pols, num_pols);
    3046                 :            :                                 err = PTR_ERR(xdst);
    3047                 :          0 :                                 if (err == -EREMOTE)
    3048                 :            :                                         goto nopol;
    3049                 :            : 
    3050                 :            :                                 goto dropdst;
    3051                 :          0 :                         } else if (xdst == NULL) {
    3052                 :          0 :                                 num_xfrms = 0;
    3053                 :          0 :                                 drop_pols = num_pols;
    3054                 :          0 :                                 goto no_transform;
    3055                 :            :                         }
    3056                 :            : 
    3057                 :          0 :                         route = xdst->route;
    3058                 :            :                 }
    3059                 :            :         }
    3060                 :            : 
    3061                 :          3 :         if (xdst == NULL) {
    3062                 :            :                 struct xfrm_flo xflo;
    3063                 :            : 
    3064                 :          3 :                 xflo.dst_orig = dst_orig;
    3065                 :          3 :                 xflo.flags = flags;
    3066                 :            : 
    3067                 :            :                 /* To accelerate a bit...  */
    3068                 :          3 :                 if ((dst_orig->flags & DST_NOXFRM) ||
    3069                 :          3 :                     !net->xfrm.policy_count[XFRM_POLICY_OUT])
    3070                 :            :                         goto nopol;
    3071                 :            : 
    3072                 :          0 :                 xdst = xfrm_bundle_lookup(net, fl, family, dir, &xflo, if_id);
    3073                 :          0 :                 if (xdst == NULL)
    3074                 :            :                         goto nopol;
    3075                 :          0 :                 if (IS_ERR(xdst)) {
    3076                 :            :                         err = PTR_ERR(xdst);
    3077                 :          0 :                         goto dropdst;
    3078                 :            :                 }
    3079                 :            : 
    3080                 :          0 :                 num_pols = xdst->num_pols;
    3081                 :          0 :                 num_xfrms = xdst->num_xfrms;
    3082                 :          0 :                 memcpy(pols, xdst->pols, sizeof(struct xfrm_policy *) * num_pols);
    3083                 :          0 :                 route = xdst->route;
    3084                 :            :         }
    3085                 :            : 
    3086                 :          0 :         dst = &xdst->u.dst;
    3087                 :          0 :         if (route == NULL && num_xfrms > 0) {
    3088                 :            :                 /* The only case when xfrm_bundle_lookup() returns a
    3089                 :            :                  * bundle with null route, is when the template could
    3090                 :            :                  * not be resolved. It means policies are there, but
    3091                 :            :                  * bundle could not be created, since we don't yet
    3092                 :            :                  * have the xfrm_state's. We need to wait for KM to
    3093                 :            :                  * negotiate new SA's or bail out with error.*/
    3094                 :          0 :                 if (net->xfrm.sysctl_larval_drop) {
    3095                 :            :                         XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
    3096                 :            :                         err = -EREMOTE;
    3097                 :            :                         goto error;
    3098                 :            :                 }
    3099                 :            : 
    3100                 :            :                 err = -EAGAIN;
    3101                 :            : 
    3102                 :            :                 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOSTATES);
    3103                 :          0 :                 goto error;
    3104                 :            :         }
    3105                 :            : 
    3106                 :            : no_transform:
    3107                 :          0 :         if (num_pols == 0)
    3108                 :            :                 goto nopol;
    3109                 :            : 
    3110                 :          0 :         if ((flags & XFRM_LOOKUP_ICMP) &&
    3111                 :          0 :             !(pols[0]->flags & XFRM_POLICY_ICMP)) {
    3112                 :            :                 err = -ENOENT;
    3113                 :            :                 goto error;
    3114                 :            :         }
    3115                 :            : 
    3116                 :          0 :         for (i = 0; i < num_pols; i++)
    3117                 :          0 :                 pols[i]->curlft.use_time = ktime_get_real_seconds();
    3118                 :            : 
    3119                 :          0 :         if (num_xfrms < 0) {
    3120                 :            :                 /* Prohibit the flow */
    3121                 :            :                 XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTPOLBLOCK);
    3122                 :            :                 err = -EPERM;
    3123                 :            :                 goto error;
    3124                 :          0 :         } else if (num_xfrms > 0) {
    3125                 :            :                 /* Flow transformed */
    3126                 :          0 :                 dst_release(dst_orig);
    3127                 :            :         } else {
    3128                 :            :                 /* Flow passes untransformed */
    3129                 :          0 :                 dst_release(dst);
    3130                 :            :                 dst = dst_orig;
    3131                 :            :         }
    3132                 :            : ok:
    3133                 :          3 :         xfrm_pols_put(pols, drop_pols);
    3134                 :          3 :         if (dst && dst->xfrm &&
    3135                 :          0 :             dst->xfrm->props.mode == XFRM_MODE_TUNNEL)
    3136                 :          0 :                 dst->flags |= DST_XFRM_TUNNEL;
    3137                 :          3 :         return dst;
    3138                 :            : 
    3139                 :            : nopol:
    3140                 :          3 :         if (!(flags & XFRM_LOOKUP_ICMP)) {
    3141                 :            :                 dst = dst_orig;
    3142                 :            :                 goto ok;
    3143                 :            :         }
    3144                 :            :         err = -ENOENT;
    3145                 :            : error:
    3146                 :          3 :         dst_release(dst);
    3147                 :            : dropdst:
    3148                 :          3 :         if (!(flags & XFRM_LOOKUP_KEEP_DST_REF))
    3149                 :          3 :                 dst_release(dst_orig);
    3150                 :          3 :         xfrm_pols_put(pols, drop_pols);
    3151                 :          3 :         return ERR_PTR(err);
    3152                 :            : }
    3153                 :            : EXPORT_SYMBOL(xfrm_lookup_with_ifid);
    3154                 :            : 
    3155                 :            : /* Main function: finds/creates a bundle for given flow.
    3156                 :            :  *
    3157                 :            :  * At the moment we eat a raw IP route. Mostly to speed up lookups
    3158                 :            :  * on interfaces with disabled IPsec.
    3159                 :            :  */
    3160                 :          3 : struct dst_entry *xfrm_lookup(struct net *net, struct dst_entry *dst_orig,
    3161                 :            :                               const struct flowi *fl, const struct sock *sk,
    3162                 :            :                               int flags)
    3163                 :            : {
    3164                 :          3 :         return xfrm_lookup_with_ifid(net, dst_orig, fl, sk, flags, 0);
    3165                 :            : }
    3166                 :            : EXPORT_SYMBOL(xfrm_lookup);
    3167                 :            : 
    3168                 :            : /* Callers of xfrm_lookup_route() must ensure a call to dst_output().
    3169                 :            :  * Otherwise we may send out blackholed packets.
    3170                 :            :  */
    3171                 :          3 : struct dst_entry *xfrm_lookup_route(struct net *net, struct dst_entry *dst_orig,
    3172                 :            :                                     const struct flowi *fl,
    3173                 :            :                                     const struct sock *sk, int flags)
    3174                 :            : {
    3175                 :          3 :         struct dst_entry *dst = xfrm_lookup(net, dst_orig, fl, sk,
    3176                 :            :                                             flags | XFRM_LOOKUP_QUEUE |
    3177                 :            :                                             XFRM_LOOKUP_KEEP_DST_REF);
    3178                 :            : 
    3179                 :          3 :         if (IS_ERR(dst) && PTR_ERR(dst) == -EREMOTE)
    3180                 :          0 :                 return make_blackhole(net, dst_orig->ops->family, dst_orig);
    3181                 :            : 
    3182                 :          3 :         if (IS_ERR(dst))
    3183                 :          0 :                 dst_release(dst_orig);
    3184                 :            : 
    3185                 :          3 :         return dst;
    3186                 :            : }
    3187                 :            : EXPORT_SYMBOL(xfrm_lookup_route);
    3188                 :            : 
    3189                 :            : static inline int
    3190                 :          0 : xfrm_secpath_reject(int idx, struct sk_buff *skb, const struct flowi *fl)
    3191                 :            : {
    3192                 :            :         struct sec_path *sp = skb_sec_path(skb);
    3193                 :            :         struct xfrm_state *x;
    3194                 :            : 
    3195                 :          0 :         if (!sp || idx < 0 || idx >= sp->len)
    3196                 :            :                 return 0;
    3197                 :          0 :         x = sp->xvec[idx];
    3198                 :          0 :         if (!x->type->reject)
    3199                 :            :                 return 0;
    3200                 :          0 :         return x->type->reject(x, skb, fl);
    3201                 :            : }
    3202                 :            : 
    3203                 :            : /* When skb is transformed back to its "native" form, we have to
    3204                 :            :  * check policy restrictions. At the moment we make this in maximally
    3205                 :            :  * stupid way. Shame on me. :-) Of course, connected sockets must
    3206                 :            :  * have policy cached at them.
    3207                 :            :  */
    3208                 :            : 
    3209                 :            : static inline int
    3210                 :          0 : xfrm_state_ok(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x,
    3211                 :            :               unsigned short family)
    3212                 :            : {
    3213                 :          0 :         if (xfrm_state_kern(x))
    3214                 :          0 :                 return tmpl->optional && !xfrm_state_addr_cmp(tmpl, x, tmpl->encap_family);
    3215                 :          0 :         return  x->id.proto == tmpl->id.proto &&
    3216                 :          0 :                 (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
    3217                 :          0 :                 (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
    3218                 :          0 :                 x->props.mode == tmpl->mode &&
    3219                 :          0 :                 (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
    3220                 :          0 :                  !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
    3221                 :          0 :                 !(x->props.mode != XFRM_MODE_TRANSPORT &&
    3222                 :          0 :                   xfrm_state_addr_cmp(tmpl, x, family));
    3223                 :            : }
    3224                 :            : 
    3225                 :            : /*
    3226                 :            :  * 0 or more than 0 is returned when validation is succeeded (either bypass
    3227                 :            :  * because of optional transport mode, or next index of the mathced secpath
    3228                 :            :  * state with the template.
    3229                 :            :  * -1 is returned when no matching template is found.
    3230                 :            :  * Otherwise "-2 - errored_index" is returned.
    3231                 :            :  */
    3232                 :            : static inline int
    3233                 :          0 : xfrm_policy_ok(const struct xfrm_tmpl *tmpl, const struct sec_path *sp, int start,
    3234                 :            :                unsigned short family)
    3235                 :            : {
    3236                 :            :         int idx = start;
    3237                 :            : 
    3238                 :          0 :         if (tmpl->optional) {
    3239                 :          0 :                 if (tmpl->mode == XFRM_MODE_TRANSPORT)
    3240                 :            :                         return start;
    3241                 :            :         } else
    3242                 :            :                 start = -1;
    3243                 :          0 :         for (; idx < sp->len; idx++) {
    3244                 :          0 :                 if (xfrm_state_ok(tmpl, sp->xvec[idx], family))
    3245                 :          0 :                         return ++idx;
    3246                 :          0 :                 if (sp->xvec[idx]->props.mode != XFRM_MODE_TRANSPORT) {
    3247                 :          0 :                         if (start == -1)
    3248                 :          0 :                                 start = -2-idx;
    3249                 :            :                         break;
    3250                 :            :                 }
    3251                 :            :         }
    3252                 :          0 :         return start;
    3253                 :            : }
    3254                 :            : 
    3255                 :            : static void
    3256                 :          3 : decode_session4(struct sk_buff *skb, struct flowi *fl, bool reverse)
    3257                 :            : {
    3258                 :            :         const struct iphdr *iph = ip_hdr(skb);
    3259                 :          3 :         int ihl = iph->ihl;
    3260                 :          3 :         u8 *xprth = skb_network_header(skb) + ihl * 4;
    3261                 :          3 :         struct flowi4 *fl4 = &fl->u.ip4;
    3262                 :            :         int oif = 0;
    3263                 :            : 
    3264                 :          3 :         if (skb_dst(skb) && skb_dst(skb)->dev)
    3265                 :          3 :                 oif = skb_dst(skb)->dev->ifindex;
    3266                 :            : 
    3267                 :          3 :         memset(fl4, 0, sizeof(struct flowi4));
    3268                 :          3 :         fl4->flowi4_mark = skb->mark;
    3269                 :          3 :         fl4->flowi4_oif = reverse ? skb->skb_iif : oif;
    3270                 :            : 
    3271                 :          3 :         fl4->flowi4_proto = iph->protocol;
    3272                 :          3 :         fl4->daddr = reverse ? iph->saddr : iph->daddr;
    3273                 :          3 :         fl4->saddr = reverse ? iph->daddr : iph->saddr;
    3274                 :          3 :         fl4->flowi4_tos = iph->tos;
    3275                 :            : 
    3276                 :          3 :         if (!ip_is_fragment(iph)) {
    3277                 :          3 :                 switch (iph->protocol) {
    3278                 :            :                 case IPPROTO_UDP:
    3279                 :            :                 case IPPROTO_UDPLITE:
    3280                 :            :                 case IPPROTO_TCP:
    3281                 :            :                 case IPPROTO_SCTP:
    3282                 :            :                 case IPPROTO_DCCP:
    3283                 :          3 :                         if (xprth + 4 < skb->data ||
    3284                 :          3 :                             pskb_may_pull(skb, xprth + 4 - skb->data)) {
    3285                 :            :                                 __be16 *ports;
    3286                 :            : 
    3287                 :          3 :                                 xprth = skb_network_header(skb) + ihl * 4;
    3288                 :            :                                 ports = (__be16 *)xprth;
    3289                 :            : 
    3290                 :          3 :                                 fl4->fl4_sport = ports[!!reverse];
    3291                 :          3 :                                 fl4->fl4_dport = ports[!reverse];
    3292                 :            :                         }
    3293                 :            :                         break;
    3294                 :            :                 case IPPROTO_ICMP:
    3295                 :          0 :                         if (xprth + 2 < skb->data ||
    3296                 :          0 :                             pskb_may_pull(skb, xprth + 2 - skb->data)) {
    3297                 :            :                                 u8 *icmp;
    3298                 :            : 
    3299                 :          0 :                                 xprth = skb_network_header(skb) + ihl * 4;
    3300                 :            :                                 icmp = xprth;
    3301                 :            : 
    3302                 :          0 :                                 fl4->fl4_icmp_type = icmp[0];
    3303                 :          0 :                                 fl4->fl4_icmp_code = icmp[1];
    3304                 :            :                         }
    3305                 :            :                         break;
    3306                 :            :                 case IPPROTO_ESP:
    3307                 :          0 :                         if (xprth + 4 < skb->data ||
    3308                 :          0 :                             pskb_may_pull(skb, xprth + 4 - skb->data)) {
    3309                 :            :                                 __be32 *ehdr;
    3310                 :            : 
    3311                 :          0 :                                 xprth = skb_network_header(skb) + ihl * 4;
    3312                 :            :                                 ehdr = (__be32 *)xprth;
    3313                 :            : 
    3314                 :          0 :                                 fl4->fl4_ipsec_spi = ehdr[0];
    3315                 :            :                         }
    3316                 :            :                         break;
    3317                 :            :                 case IPPROTO_AH:
    3318                 :          0 :                         if (xprth + 8 < skb->data ||
    3319                 :          0 :                             pskb_may_pull(skb, xprth + 8 - skb->data)) {
    3320                 :            :                                 __be32 *ah_hdr;
    3321                 :            : 
    3322                 :          0 :                                 xprth = skb_network_header(skb) + ihl * 4;
    3323                 :            :                                 ah_hdr = (__be32 *)xprth;
    3324                 :            : 
    3325                 :          0 :                                 fl4->fl4_ipsec_spi = ah_hdr[1];
    3326                 :            :                         }
    3327                 :            :                         break;
    3328                 :            :                 case IPPROTO_COMP:
    3329                 :          0 :                         if (xprth + 4 < skb->data ||
    3330                 :          0 :                             pskb_may_pull(skb, xprth + 4 - skb->data)) {
    3331                 :            :                                 __be16 *ipcomp_hdr;
    3332                 :            : 
    3333                 :          0 :                                 xprth = skb_network_header(skb) + ihl * 4;
    3334                 :            :                                 ipcomp_hdr = (__be16 *)xprth;
    3335                 :            : 
    3336                 :          0 :                                 fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
    3337                 :            :                         }
    3338                 :            :                         break;
    3339                 :            :                 case IPPROTO_GRE:
    3340                 :          0 :                         if (xprth + 12 < skb->data ||
    3341                 :          0 :                             pskb_may_pull(skb, xprth + 12 - skb->data)) {
    3342                 :            :                                 __be16 *greflags;
    3343                 :            :                                 __be32 *gre_hdr;
    3344                 :            : 
    3345                 :          0 :                                 xprth = skb_network_header(skb) + ihl * 4;
    3346                 :            :                                 greflags = (__be16 *)xprth;
    3347                 :            :                                 gre_hdr = (__be32 *)xprth;
    3348                 :            : 
    3349                 :          0 :                                 if (greflags[0] & GRE_KEY) {
    3350                 :          0 :                                         if (greflags[0] & GRE_CSUM)
    3351                 :          0 :                                                 gre_hdr++;
    3352                 :          0 :                                         fl4->fl4_gre_key = gre_hdr[1];
    3353                 :            :                                 }
    3354                 :            :                         }
    3355                 :            :                         break;
    3356                 :            :                 default:
    3357                 :          0 :                         fl4->fl4_ipsec_spi = 0;
    3358                 :          0 :                         break;
    3359                 :            :                 }
    3360                 :            :         }
    3361                 :          3 : }
    3362                 :            : 
    3363                 :            : #if IS_ENABLED(CONFIG_IPV6)
    3364                 :            : static void
    3365                 :          0 : decode_session6(struct sk_buff *skb, struct flowi *fl, bool reverse)
    3366                 :            : {
    3367                 :          0 :         struct flowi6 *fl6 = &fl->u.ip6;
    3368                 :            :         int onlyproto = 0;
    3369                 :            :         const struct ipv6hdr *hdr = ipv6_hdr(skb);
    3370                 :            :         u32 offset = sizeof(*hdr);
    3371                 :            :         struct ipv6_opt_hdr *exthdr;
    3372                 :            :         const unsigned char *nh = skb_network_header(skb);
    3373                 :          0 :         u16 nhoff = IP6CB(skb)->nhoff;
    3374                 :            :         int oif = 0;
    3375                 :            :         u8 nexthdr;
    3376                 :            : 
    3377                 :          0 :         if (!nhoff)
    3378                 :            :                 nhoff = offsetof(struct ipv6hdr, nexthdr);
    3379                 :            : 
    3380                 :          0 :         nexthdr = nh[nhoff];
    3381                 :            : 
    3382                 :          0 :         if (skb_dst(skb) && skb_dst(skb)->dev)
    3383                 :          0 :                 oif = skb_dst(skb)->dev->ifindex;
    3384                 :            : 
    3385                 :          0 :         memset(fl6, 0, sizeof(struct flowi6));
    3386                 :          0 :         fl6->flowi6_mark = skb->mark;
    3387                 :          0 :         fl6->flowi6_oif = reverse ? skb->skb_iif : oif;
    3388                 :            : 
    3389                 :          0 :         fl6->daddr = reverse ? hdr->saddr : hdr->daddr;
    3390                 :          0 :         fl6->saddr = reverse ? hdr->daddr : hdr->saddr;
    3391                 :            : 
    3392                 :          0 :         while (nh + offset + sizeof(*exthdr) < skb->data ||
    3393                 :          0 :                pskb_may_pull(skb, nh + offset + sizeof(*exthdr) - skb->data)) {
    3394                 :            :                 nh = skb_network_header(skb);
    3395                 :          0 :                 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
    3396                 :            : 
    3397                 :          0 :                 switch (nexthdr) {
    3398                 :            :                 case NEXTHDR_FRAGMENT:
    3399                 :            :                         onlyproto = 1;
    3400                 :            :                         /* fall through */
    3401                 :            :                 case NEXTHDR_ROUTING:
    3402                 :            :                 case NEXTHDR_HOP:
    3403                 :            :                 case NEXTHDR_DEST:
    3404                 :          0 :                         offset += ipv6_optlen(exthdr);
    3405                 :          0 :                         nexthdr = exthdr->nexthdr;
    3406                 :            :                         exthdr = (struct ipv6_opt_hdr *)(nh + offset);
    3407                 :          0 :                         break;
    3408                 :            :                 case IPPROTO_UDP:
    3409                 :            :                 case IPPROTO_UDPLITE:
    3410                 :            :                 case IPPROTO_TCP:
    3411                 :            :                 case IPPROTO_SCTP:
    3412                 :            :                 case IPPROTO_DCCP:
    3413                 :          0 :                         if (!onlyproto && (nh + offset + 4 < skb->data ||
    3414                 :          0 :                              pskb_may_pull(skb, nh + offset + 4 - skb->data))) {
    3415                 :            :                                 __be16 *ports;
    3416                 :            : 
    3417                 :            :                                 nh = skb_network_header(skb);
    3418                 :          0 :                                 ports = (__be16 *)(nh + offset);
    3419                 :          0 :                                 fl6->fl6_sport = ports[!!reverse];
    3420                 :          0 :                                 fl6->fl6_dport = ports[!reverse];
    3421                 :            :                         }
    3422                 :          0 :                         fl6->flowi6_proto = nexthdr;
    3423                 :          0 :                         return;
    3424                 :            :                 case IPPROTO_ICMPV6:
    3425                 :          0 :                         if (!onlyproto && (nh + offset + 2 < skb->data ||
    3426                 :          0 :                             pskb_may_pull(skb, nh + offset + 2 - skb->data))) {
    3427                 :            :                                 u8 *icmp;
    3428                 :            : 
    3429                 :            :                                 nh = skb_network_header(skb);
    3430                 :          0 :                                 icmp = (u8 *)(nh + offset);
    3431                 :          0 :                                 fl6->fl6_icmp_type = icmp[0];
    3432                 :          0 :                                 fl6->fl6_icmp_code = icmp[1];
    3433                 :            :                         }
    3434                 :          0 :                         fl6->flowi6_proto = nexthdr;
    3435                 :          0 :                         return;
    3436                 :            : #if IS_ENABLED(CONFIG_IPV6_MIP6)
    3437                 :            :                 case IPPROTO_MH:
    3438                 :            :                         offset += ipv6_optlen(exthdr);
    3439                 :            :                         if (!onlyproto && (nh + offset + 3 < skb->data ||
    3440                 :            :                             pskb_may_pull(skb, nh + offset + 3 - skb->data))) {
    3441                 :            :                                 struct ip6_mh *mh;
    3442                 :            : 
    3443                 :            :                                 nh = skb_network_header(skb);
    3444                 :            :                                 mh = (struct ip6_mh *)(nh + offset);
    3445                 :            :                                 fl6->fl6_mh_type = mh->ip6mh_type;
    3446                 :            :                         }
    3447                 :            :                         fl6->flowi6_proto = nexthdr;
    3448                 :            :                         return;
    3449                 :            : #endif
    3450                 :            :                 /* XXX Why are there these headers? */
    3451                 :            :                 case IPPROTO_AH:
    3452                 :            :                 case IPPROTO_ESP:
    3453                 :            :                 case IPPROTO_COMP:
    3454                 :            :                 default:
    3455                 :          0 :                         fl6->fl6_ipsec_spi = 0;
    3456                 :          0 :                         fl6->flowi6_proto = nexthdr;
    3457                 :          0 :                         return;
    3458                 :            :                 }
    3459                 :            :         }
    3460                 :            : }
    3461                 :            : #endif
    3462                 :            : 
    3463                 :          3 : int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
    3464                 :            :                           unsigned int family, int reverse)
    3465                 :            : {
    3466                 :          3 :         switch (family) {
    3467                 :            :         case AF_INET:
    3468                 :          3 :                 decode_session4(skb, fl, reverse);
    3469                 :          3 :                 break;
    3470                 :            : #if IS_ENABLED(CONFIG_IPV6)
    3471                 :            :         case AF_INET6:
    3472                 :          0 :                 decode_session6(skb, fl, reverse);
    3473                 :          0 :                 break;
    3474                 :            : #endif
    3475                 :            :         default:
    3476                 :            :                 return -EAFNOSUPPORT;
    3477                 :            :         }
    3478                 :            : 
    3479                 :            :         return security_xfrm_decode_session(skb, &fl->flowi_secid);
    3480                 :            : }
    3481                 :            : EXPORT_SYMBOL(__xfrm_decode_session);
    3482                 :            : 
    3483                 :            : static inline int secpath_has_nontransport(const struct sec_path *sp, int k, int *idxp)
    3484                 :            : {
    3485                 :          0 :         for (; k < sp->len; k++) {
    3486                 :          0 :                 if (sp->xvec[k]->props.mode != XFRM_MODE_TRANSPORT) {
    3487                 :          0 :                         *idxp = k;
    3488                 :            :                         return 1;
    3489                 :            :                 }
    3490                 :            :         }
    3491                 :            : 
    3492                 :            :         return 0;
    3493                 :            : }
    3494                 :            : 
    3495                 :          0 : int __xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb,
    3496                 :            :                         unsigned short family)
    3497                 :            : {
    3498                 :          0 :         struct net *net = dev_net(skb->dev);
    3499                 :            :         struct xfrm_policy *pol;
    3500                 :            :         struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
    3501                 :            :         int npols = 0;
    3502                 :            :         int xfrm_nr;
    3503                 :            :         int pi;
    3504                 :            :         int reverse;
    3505                 :            :         struct flowi fl;
    3506                 :            :         int xerr_idx = -1;
    3507                 :            :         const struct xfrm_if_cb *ifcb;
    3508                 :            :         struct sec_path *sp;
    3509                 :            :         struct xfrm_if *xi;
    3510                 :            :         u32 if_id = 0;
    3511                 :            : 
    3512                 :            :         rcu_read_lock();
    3513                 :            :         ifcb = xfrm_if_get_cb();
    3514                 :            : 
    3515                 :          0 :         if (ifcb) {
    3516                 :          0 :                 xi = ifcb->decode_session(skb, family);
    3517                 :          0 :                 if (xi) {
    3518                 :          0 :                         if_id = xi->p.if_id;
    3519                 :          0 :                         net = xi->net;
    3520                 :            :                 }
    3521                 :            :         }
    3522                 :            :         rcu_read_unlock();
    3523                 :            : 
    3524                 :          0 :         reverse = dir & ~XFRM_POLICY_MASK;
    3525                 :          0 :         dir &= XFRM_POLICY_MASK;
    3526                 :            : 
    3527                 :          0 :         if (__xfrm_decode_session(skb, &fl, family, reverse) < 0) {
    3528                 :            :                 XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
    3529                 :            :                 return 0;
    3530                 :            :         }
    3531                 :            : 
    3532                 :          0 :         nf_nat_decode_session(skb, &fl, family);
    3533                 :            : 
    3534                 :            :         /* First, check used SA against their selectors. */
    3535                 :            :         sp = skb_sec_path(skb);
    3536                 :          0 :         if (sp) {
    3537                 :            :                 int i;
    3538                 :            : 
    3539                 :          0 :                 for (i = sp->len - 1; i >= 0; i--) {
    3540                 :          0 :                         struct xfrm_state *x = sp->xvec[i];
    3541                 :          0 :                         if (!xfrm_selector_match(&x->sel, &fl, family)) {
    3542                 :            :                                 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMISMATCH);
    3543                 :            :                                 return 0;
    3544                 :            :                         }
    3545                 :            :                 }
    3546                 :            :         }
    3547                 :            : 
    3548                 :            :         pol = NULL;
    3549                 :            :         sk = sk_to_full_sk(sk);
    3550                 :          0 :         if (sk && sk->sk_policy[dir]) {
    3551                 :          0 :                 pol = xfrm_sk_policy_lookup(sk, dir, &fl, family, if_id);
    3552                 :          0 :                 if (IS_ERR(pol)) {
    3553                 :            :                         XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
    3554                 :            :                         return 0;
    3555                 :            :                 }
    3556                 :            :         }
    3557                 :            : 
    3558                 :          0 :         if (!pol)
    3559                 :          0 :                 pol = xfrm_policy_lookup(net, &fl, family, dir, if_id);
    3560                 :            : 
    3561                 :          0 :         if (IS_ERR(pol)) {
    3562                 :            :                 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
    3563                 :            :                 return 0;
    3564                 :            :         }
    3565                 :            : 
    3566                 :          0 :         if (!pol) {
    3567                 :          0 :                 if (sp && secpath_has_nontransport(sp, 0, &xerr_idx)) {
    3568                 :          0 :                         xfrm_secpath_reject(xerr_idx, skb, &fl);
    3569                 :            :                         XFRM_INC_STATS(net, LINUX_MIB_XFRMINNOPOLS);
    3570                 :          0 :                         return 0;
    3571                 :            :                 }
    3572                 :            :                 return 1;
    3573                 :            :         }
    3574                 :            : 
    3575                 :          0 :         pol->curlft.use_time = ktime_get_real_seconds();
    3576                 :            : 
    3577                 :          0 :         pols[0] = pol;
    3578                 :            :         npols++;
    3579                 :            : #ifdef CONFIG_XFRM_SUB_POLICY
    3580                 :            :         if (pols[0]->type != XFRM_POLICY_TYPE_MAIN) {
    3581                 :            :                 pols[1] = xfrm_policy_lookup_bytype(net, XFRM_POLICY_TYPE_MAIN,
    3582                 :            :                                                     &fl, family,
    3583                 :            :                                                     XFRM_POLICY_IN, if_id);
    3584                 :            :                 if (pols[1]) {
    3585                 :            :                         if (IS_ERR(pols[1])) {
    3586                 :            :                                 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLERROR);
    3587                 :            :                                 return 0;
    3588                 :            :                         }
    3589                 :            :                         pols[1]->curlft.use_time = ktime_get_real_seconds();
    3590                 :            :                         npols++;
    3591                 :            :                 }
    3592                 :            :         }
    3593                 :            : #endif
    3594                 :            : 
    3595                 :          0 :         if (pol->action == XFRM_POLICY_ALLOW) {
    3596                 :            :                 static struct sec_path dummy;
    3597                 :            :                 struct xfrm_tmpl *tp[XFRM_MAX_DEPTH];
    3598                 :            :                 struct xfrm_tmpl *stp[XFRM_MAX_DEPTH];
    3599                 :            :                 struct xfrm_tmpl **tpp = tp;
    3600                 :            :                 int ti = 0;
    3601                 :            :                 int i, k;
    3602                 :            : 
    3603                 :            :                 sp = skb_sec_path(skb);
    3604                 :          0 :                 if (!sp)
    3605                 :            :                         sp = &dummy;
    3606                 :            : 
    3607                 :          0 :                 for (pi = 0; pi < npols; pi++) {
    3608                 :          0 :                         if (pols[pi] != pol &&
    3609                 :          0 :                             pols[pi]->action != XFRM_POLICY_ALLOW) {
    3610                 :            :                                 XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
    3611                 :          0 :                                 goto reject;
    3612                 :            :                         }
    3613                 :          0 :                         if (ti + pols[pi]->xfrm_nr >= XFRM_MAX_DEPTH) {
    3614                 :            :                                 XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
    3615                 :          0 :                                 goto reject_error;
    3616                 :            :                         }
    3617                 :          0 :                         for (i = 0; i < pols[pi]->xfrm_nr; i++)
    3618                 :          0 :                                 tpp[ti++] = &pols[pi]->xfrm_vec[i];
    3619                 :            :                 }
    3620                 :          0 :                 xfrm_nr = ti;
    3621                 :            :                 if (npols > 1) {
    3622                 :            :                         xfrm_tmpl_sort(stp, tpp, xfrm_nr, family);
    3623                 :            :                         tpp = stp;
    3624                 :            :                 }
    3625                 :            : 
    3626                 :            :                 /* For each tunnel xfrm, find the first matching tmpl.
    3627                 :            :                  * For each tmpl before that, find corresponding xfrm.
    3628                 :            :                  * Order is _important_. Later we will implement
    3629                 :            :                  * some barriers, but at the moment barriers
    3630                 :            :                  * are implied between each two transformations.
    3631                 :            :                  */
    3632                 :          0 :                 for (i = xfrm_nr-1, k = 0; i >= 0; i--) {
    3633                 :          0 :                         k = xfrm_policy_ok(tpp[i], sp, k, family);
    3634                 :          0 :                         if (k < 0) {
    3635                 :          0 :                                 if (k < -1)
    3636                 :            :                                         /* "-2 - errored_index" returned */
    3637                 :          0 :                                         xerr_idx = -(2+k);
    3638                 :            :                                 XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
    3639                 :            :                                 goto reject;
    3640                 :            :                         }
    3641                 :            :                 }
    3642                 :            : 
    3643                 :          0 :                 if (secpath_has_nontransport(sp, k, &xerr_idx)) {
    3644                 :            :                         XFRM_INC_STATS(net, LINUX_MIB_XFRMINTMPLMISMATCH);
    3645                 :            :                         goto reject;
    3646                 :            :                 }
    3647                 :            : 
    3648                 :          0 :                 xfrm_pols_put(pols, npols);
    3649                 :          0 :                 return 1;
    3650                 :            :         }
    3651                 :            :         XFRM_INC_STATS(net, LINUX_MIB_XFRMINPOLBLOCK);
    3652                 :            : 
    3653                 :            : reject:
    3654                 :          0 :         xfrm_secpath_reject(xerr_idx, skb, &fl);
    3655                 :            : reject_error:
    3656                 :          0 :         xfrm_pols_put(pols, npols);
    3657                 :          0 :         return 0;
    3658                 :            : }
    3659                 :            : EXPORT_SYMBOL(__xfrm_policy_check);
    3660                 :            : 
    3661                 :          0 : int __xfrm_route_forward(struct sk_buff *skb, unsigned short family)
    3662                 :            : {
    3663                 :          0 :         struct net *net = dev_net(skb->dev);
    3664                 :            :         struct flowi fl;
    3665                 :            :         struct dst_entry *dst;
    3666                 :            :         int res = 1;
    3667                 :            : 
    3668                 :          0 :         if (xfrm_decode_session(skb, &fl, family) < 0) {
    3669                 :            :                 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
    3670                 :            :                 return 0;
    3671                 :            :         }
    3672                 :            : 
    3673                 :          0 :         skb_dst_force(skb);
    3674                 :          0 :         if (!skb_dst(skb)) {
    3675                 :            :                 XFRM_INC_STATS(net, LINUX_MIB_XFRMFWDHDRERROR);
    3676                 :            :                 return 0;
    3677                 :            :         }
    3678                 :            : 
    3679                 :            :         dst = xfrm_lookup(net, skb_dst(skb), &fl, NULL, XFRM_LOOKUP_QUEUE);
    3680                 :          0 :         if (IS_ERR(dst)) {
    3681                 :            :                 res = 0;
    3682                 :            :                 dst = NULL;
    3683                 :            :         }
    3684                 :            :         skb_dst_set(skb, dst);
    3685                 :          0 :         return res;
    3686                 :            : }
    3687                 :            : EXPORT_SYMBOL(__xfrm_route_forward);
    3688                 :            : 
    3689                 :            : /* Optimize later using cookies and generation ids. */
    3690                 :            : 
    3691                 :          0 : static struct dst_entry *xfrm_dst_check(struct dst_entry *dst, u32 cookie)
    3692                 :            : {
    3693                 :            :         /* Code (such as __xfrm4_bundle_create()) sets dst->obsolete
    3694                 :            :          * to DST_OBSOLETE_FORCE_CHK to force all XFRM destinations to
    3695                 :            :          * get validated by dst_ops->check on every use.  We do this
    3696                 :            :          * because when a normal route referenced by an XFRM dst is
    3697                 :            :          * obsoleted we do not go looking around for all parent
    3698                 :            :          * referencing XFRM dsts so that we can invalidate them.  It
    3699                 :            :          * is just too much work.  Instead we make the checks here on
    3700                 :            :          * every use.  For example:
    3701                 :            :          *
    3702                 :            :          *      XFRM dst A --> IPv4 dst X
    3703                 :            :          *
    3704                 :            :          * X is the "xdst->route" of A (X is also the "dst->path" of A
    3705                 :            :          * in this example).  If X is marked obsolete, "A" will not
    3706                 :            :          * notice.  That's what we are validating here via the
    3707                 :            :          * stale_bundle() check.
    3708                 :            :          *
    3709                 :            :          * When a dst is removed from the fib tree, DST_OBSOLETE_DEAD will
    3710                 :            :          * be marked on it.
    3711                 :            :          * This will force stale_bundle() to fail on any xdst bundle with
    3712                 :            :          * this dst linked in it.
    3713                 :            :          */
    3714                 :          0 :         if (dst->obsolete < 0 && !stale_bundle(dst))
    3715                 :          0 :                 return dst;
    3716                 :            : 
    3717                 :            :         return NULL;
    3718                 :            : }
    3719                 :            : 
    3720                 :            : static int stale_bundle(struct dst_entry *dst)
    3721                 :            : {
    3722                 :          0 :         return !xfrm_bundle_ok((struct xfrm_dst *)dst);
    3723                 :            : }
    3724                 :            : 
    3725                 :          0 : void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
    3726                 :            : {
    3727                 :          0 :         while ((dst = xfrm_dst_child(dst)) && dst->xfrm && dst->dev == dev) {
    3728                 :          0 :                 dst->dev = dev_net(dev)->loopback_dev;
    3729                 :          0 :                 dev_hold(dst->dev);
    3730                 :          0 :                 dev_put(dev);
    3731                 :            :         }
    3732                 :          0 : }
    3733                 :            : EXPORT_SYMBOL(xfrm_dst_ifdown);
    3734                 :            : 
    3735                 :          0 : static void xfrm_link_failure(struct sk_buff *skb)
    3736                 :            : {
    3737                 :            :         /* Impossible. Such dst must be popped before reaches point of failure. */
    3738                 :          0 : }
    3739                 :            : 
    3740                 :          0 : static struct dst_entry *xfrm_negative_advice(struct dst_entry *dst)
    3741                 :            : {
    3742                 :          0 :         if (dst) {
    3743                 :          0 :                 if (dst->obsolete) {
    3744                 :          0 :                         dst_release(dst);
    3745                 :            :                         dst = NULL;
    3746                 :            :                 }
    3747                 :            :         }
    3748                 :          0 :         return dst;
    3749                 :            : }
    3750                 :            : 
    3751                 :          0 : static void xfrm_init_pmtu(struct xfrm_dst **bundle, int nr)
    3752                 :            : {
    3753                 :          0 :         while (nr--) {
    3754                 :          0 :                 struct xfrm_dst *xdst = bundle[nr];
    3755                 :            :                 u32 pmtu, route_mtu_cached;
    3756                 :            :                 struct dst_entry *dst;
    3757                 :            : 
    3758                 :          0 :                 dst = &xdst->u.dst;
    3759                 :            :                 pmtu = dst_mtu(xfrm_dst_child(dst));
    3760                 :          0 :                 xdst->child_mtu_cached = pmtu;
    3761                 :            : 
    3762                 :          0 :                 pmtu = xfrm_state_mtu(dst->xfrm, pmtu);
    3763                 :            : 
    3764                 :          0 :                 route_mtu_cached = dst_mtu(xdst->route);
    3765                 :          0 :                 xdst->route_mtu_cached = route_mtu_cached;
    3766                 :            : 
    3767                 :          0 :                 if (pmtu > route_mtu_cached)
    3768                 :            :                         pmtu = route_mtu_cached;
    3769                 :            : 
    3770                 :            :                 dst_metric_set(dst, RTAX_MTU, pmtu);
    3771                 :            :         }
    3772                 :          0 : }
    3773                 :            : 
    3774                 :            : /* Check that the bundle accepts the flow and its components are
    3775                 :            :  * still valid.
    3776                 :            :  */
    3777                 :            : 
    3778                 :          0 : static int xfrm_bundle_ok(struct xfrm_dst *first)
    3779                 :            : {
    3780                 :            :         struct xfrm_dst *bundle[XFRM_MAX_DEPTH];
    3781                 :          0 :         struct dst_entry *dst = &first->u.dst;
    3782                 :            :         struct xfrm_dst *xdst;
    3783                 :            :         int start_from, nr;
    3784                 :            :         u32 mtu;
    3785                 :            : 
    3786                 :          0 :         if (!dst_check(xfrm_dst_path(dst), ((struct xfrm_dst *)dst)->path_cookie) ||
    3787                 :          0 :             (dst->dev && !netif_running(dst->dev)))
    3788                 :            :                 return 0;
    3789                 :            : 
    3790                 :          0 :         if (dst->flags & DST_XFRM_QUEUE)
    3791                 :            :                 return 1;
    3792                 :            : 
    3793                 :            :         start_from = nr = 0;
    3794                 :            :         do {
    3795                 :            :                 struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
    3796                 :            : 
    3797                 :          0 :                 if (dst->xfrm->km.state != XFRM_STATE_VALID)
    3798                 :            :                         return 0;
    3799                 :          0 :                 if (xdst->xfrm_genid != dst->xfrm->genid)
    3800                 :            :                         return 0;
    3801                 :          0 :                 if (xdst->num_pols > 0 &&
    3802                 :          0 :                     xdst->policy_genid != atomic_read(&xdst->pols[0]->genid))
    3803                 :            :                         return 0;
    3804                 :            : 
    3805                 :          0 :                 bundle[nr++] = xdst;
    3806                 :            : 
    3807                 :            :                 mtu = dst_mtu(xfrm_dst_child(dst));
    3808                 :          0 :                 if (xdst->child_mtu_cached != mtu) {
    3809                 :            :                         start_from = nr;
    3810                 :          0 :                         xdst->child_mtu_cached = mtu;
    3811                 :            :                 }
    3812                 :            : 
    3813                 :          0 :                 if (!dst_check(xdst->route, xdst->route_cookie))
    3814                 :            :                         return 0;
    3815                 :          0 :                 mtu = dst_mtu(xdst->route);
    3816                 :          0 :                 if (xdst->route_mtu_cached != mtu) {
    3817                 :            :                         start_from = nr;
    3818                 :          0 :                         xdst->route_mtu_cached = mtu;
    3819                 :            :                 }
    3820                 :            : 
    3821                 :            :                 dst = xfrm_dst_child(dst);
    3822                 :          0 :         } while (dst->xfrm);
    3823                 :            : 
    3824                 :          0 :         if (likely(!start_from))
    3825                 :            :                 return 1;
    3826                 :            : 
    3827                 :          0 :         xdst = bundle[start_from - 1];
    3828                 :          0 :         mtu = xdst->child_mtu_cached;
    3829                 :          0 :         while (start_from--) {
    3830                 :          0 :                 dst = &xdst->u.dst;
    3831                 :            : 
    3832                 :          0 :                 mtu = xfrm_state_mtu(dst->xfrm, mtu);
    3833                 :          0 :                 if (mtu > xdst->route_mtu_cached)
    3834                 :            :                         mtu = xdst->route_mtu_cached;
    3835                 :            :                 dst_metric_set(dst, RTAX_MTU, mtu);
    3836                 :          0 :                 if (!start_from)
    3837                 :            :                         break;
    3838                 :            : 
    3839                 :          0 :                 xdst = bundle[start_from - 1];
    3840                 :          0 :                 xdst->child_mtu_cached = mtu;
    3841                 :            :         }
    3842                 :            : 
    3843                 :            :         return 1;
    3844                 :            : }
    3845                 :            : 
    3846                 :          0 : static unsigned int xfrm_default_advmss(const struct dst_entry *dst)
    3847                 :            : {
    3848                 :          0 :         return dst_metric_advmss(xfrm_dst_path(dst));
    3849                 :            : }
    3850                 :            : 
    3851                 :          0 : static unsigned int xfrm_mtu(const struct dst_entry *dst)
    3852                 :            : {
    3853                 :            :         unsigned int mtu = dst_metric_raw(dst, RTAX_MTU);
    3854                 :            : 
    3855                 :          0 :         return mtu ? : dst_mtu(xfrm_dst_path(dst));
    3856                 :            : }
    3857                 :            : 
    3858                 :          0 : static const void *xfrm_get_dst_nexthop(const struct dst_entry *dst,
    3859                 :            :                                         const void *daddr)
    3860                 :            : {
    3861                 :          0 :         while (dst->xfrm) {
    3862                 :            :                 const struct xfrm_state *xfrm = dst->xfrm;
    3863                 :            : 
    3864                 :            :                 dst = xfrm_dst_child(dst);
    3865                 :            : 
    3866                 :          0 :                 if (xfrm->props.mode == XFRM_MODE_TRANSPORT)
    3867                 :          0 :                         continue;
    3868                 :          0 :                 if (xfrm->type->flags & XFRM_TYPE_REMOTE_COADDR)
    3869                 :          0 :                         daddr = xfrm->coaddr;
    3870                 :          0 :                 else if (!(xfrm->type->flags & XFRM_TYPE_LOCAL_COADDR))
    3871                 :          0 :                         daddr = &xfrm->id.daddr;
    3872                 :            :         }
    3873                 :          0 :         return daddr;
    3874                 :            : }
    3875                 :            : 
    3876                 :          0 : static struct neighbour *xfrm_neigh_lookup(const struct dst_entry *dst,
    3877                 :            :                                            struct sk_buff *skb,
    3878                 :            :                                            const void *daddr)
    3879                 :            : {
    3880                 :            :         const struct dst_entry *path = xfrm_dst_path(dst);
    3881                 :            : 
    3882                 :          0 :         if (!skb)
    3883                 :          0 :                 daddr = xfrm_get_dst_nexthop(dst, daddr);
    3884                 :          0 :         return path->ops->neigh_lookup(path, skb, daddr);
    3885                 :            : }
    3886                 :            : 
    3887                 :          0 : static void xfrm_confirm_neigh(const struct dst_entry *dst, const void *daddr)
    3888                 :            : {
    3889                 :            :         const struct dst_entry *path = xfrm_dst_path(dst);
    3890                 :            : 
    3891                 :          0 :         daddr = xfrm_get_dst_nexthop(dst, daddr);
    3892                 :          0 :         path->ops->confirm_neigh(path, daddr);
    3893                 :          0 : }
    3894                 :            : 
    3895                 :          3 : int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family)
    3896                 :            : {
    3897                 :            :         int err = 0;
    3898                 :            : 
    3899                 :          3 :         if (WARN_ON(family >= ARRAY_SIZE(xfrm_policy_afinfo)))
    3900                 :            :                 return -EAFNOSUPPORT;
    3901                 :            : 
    3902                 :            :         spin_lock(&xfrm_policy_afinfo_lock);
    3903                 :          3 :         if (unlikely(xfrm_policy_afinfo[family] != NULL))
    3904                 :            :                 err = -EEXIST;
    3905                 :            :         else {
    3906                 :          3 :                 struct dst_ops *dst_ops = afinfo->dst_ops;
    3907                 :          3 :                 if (likely(dst_ops->kmem_cachep == NULL))
    3908                 :          3 :                         dst_ops->kmem_cachep = xfrm_dst_cache;
    3909                 :          3 :                 if (likely(dst_ops->check == NULL))
    3910                 :          3 :                         dst_ops->check = xfrm_dst_check;
    3911                 :          3 :                 if (likely(dst_ops->default_advmss == NULL))
    3912                 :          3 :                         dst_ops->default_advmss = xfrm_default_advmss;
    3913                 :          3 :                 if (likely(dst_ops->mtu == NULL))
    3914                 :          3 :                         dst_ops->mtu = xfrm_mtu;
    3915                 :          3 :                 if (likely(dst_ops->negative_advice == NULL))
    3916                 :          3 :                         dst_ops->negative_advice = xfrm_negative_advice;
    3917                 :          3 :                 if (likely(dst_ops->link_failure == NULL))
    3918                 :          3 :                         dst_ops->link_failure = xfrm_link_failure;
    3919                 :          3 :                 if (likely(dst_ops->neigh_lookup == NULL))
    3920                 :          3 :                         dst_ops->neigh_lookup = xfrm_neigh_lookup;
    3921                 :          3 :                 if (likely(!dst_ops->confirm_neigh))
    3922                 :          3 :                         dst_ops->confirm_neigh = xfrm_confirm_neigh;
    3923                 :          3 :                 rcu_assign_pointer(xfrm_policy_afinfo[family], afinfo);
    3924                 :            :         }
    3925                 :            :         spin_unlock(&xfrm_policy_afinfo_lock);
    3926                 :            : 
    3927                 :          3 :         return err;
    3928                 :            : }
    3929                 :            : EXPORT_SYMBOL(xfrm_policy_register_afinfo);
    3930                 :            : 
    3931                 :          0 : void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo)
    3932                 :            : {
    3933                 :          0 :         struct dst_ops *dst_ops = afinfo->dst_ops;
    3934                 :            :         int i;
    3935                 :            : 
    3936                 :          0 :         for (i = 0; i < ARRAY_SIZE(xfrm_policy_afinfo); i++) {
    3937                 :          0 :                 if (xfrm_policy_afinfo[i] != afinfo)
    3938                 :          0 :                         continue;
    3939                 :          0 :                 RCU_INIT_POINTER(xfrm_policy_afinfo[i], NULL);
    3940                 :          0 :                 break;
    3941                 :            :         }
    3942                 :            : 
    3943                 :          0 :         synchronize_rcu();
    3944                 :            : 
    3945                 :          0 :         dst_ops->kmem_cachep = NULL;
    3946                 :          0 :         dst_ops->check = NULL;
    3947                 :          0 :         dst_ops->negative_advice = NULL;
    3948                 :          0 :         dst_ops->link_failure = NULL;
    3949                 :          0 : }
    3950                 :            : EXPORT_SYMBOL(xfrm_policy_unregister_afinfo);
    3951                 :            : 
    3952                 :          0 : void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb)
    3953                 :            : {
    3954                 :            :         spin_lock(&xfrm_if_cb_lock);
    3955                 :          0 :         rcu_assign_pointer(xfrm_if_cb, ifcb);
    3956                 :            :         spin_unlock(&xfrm_if_cb_lock);
    3957                 :          0 : }
    3958                 :            : EXPORT_SYMBOL(xfrm_if_register_cb);
    3959                 :            : 
    3960                 :          0 : void xfrm_if_unregister_cb(void)
    3961                 :            : {
    3962                 :            :         RCU_INIT_POINTER(xfrm_if_cb, NULL);
    3963                 :          0 :         synchronize_rcu();
    3964                 :          0 : }
    3965                 :            : EXPORT_SYMBOL(xfrm_if_unregister_cb);
    3966                 :            : 
    3967                 :            : #ifdef CONFIG_XFRM_STATISTICS
    3968                 :            : static int __net_init xfrm_statistics_init(struct net *net)
    3969                 :            : {
    3970                 :            :         int rv;
    3971                 :            :         net->mib.xfrm_statistics = alloc_percpu(struct linux_xfrm_mib);
    3972                 :            :         if (!net->mib.xfrm_statistics)
    3973                 :            :                 return -ENOMEM;
    3974                 :            :         rv = xfrm_proc_init(net);
    3975                 :            :         if (rv < 0)
    3976                 :            :                 free_percpu(net->mib.xfrm_statistics);
    3977                 :            :         return rv;
    3978                 :            : }
    3979                 :            : 
    3980                 :            : static void xfrm_statistics_fini(struct net *net)
    3981                 :            : {
    3982                 :            :         xfrm_proc_fini(net);
    3983                 :            :         free_percpu(net->mib.xfrm_statistics);
    3984                 :            : }
    3985                 :            : #else
    3986                 :            : static int __net_init xfrm_statistics_init(struct net *net)
    3987                 :            : {
    3988                 :            :         return 0;
    3989                 :            : }
    3990                 :            : 
    3991                 :            : static void xfrm_statistics_fini(struct net *net)
    3992                 :            : {
    3993                 :            : }
    3994                 :            : #endif
    3995                 :            : 
    3996                 :          3 : static int __net_init xfrm_policy_init(struct net *net)
    3997                 :            : {
    3998                 :            :         unsigned int hmask, sz;
    3999                 :            :         int dir, err;
    4000                 :            : 
    4001                 :          3 :         if (net_eq(net, &init_net)) {
    4002                 :          3 :                 xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
    4003                 :            :                                            sizeof(struct xfrm_dst),
    4004                 :            :                                            0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
    4005                 :            :                                            NULL);
    4006                 :          3 :                 err = rhashtable_init(&xfrm_policy_inexact_table,
    4007                 :            :                                       &xfrm_pol_inexact_params);
    4008                 :          3 :                 BUG_ON(err);
    4009                 :            :         }
    4010                 :            : 
    4011                 :            :         hmask = 8 - 1;
    4012                 :            :         sz = (hmask+1) * sizeof(struct hlist_head);
    4013                 :            : 
    4014                 :          3 :         net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
    4015                 :          3 :         if (!net->xfrm.policy_byidx)
    4016                 :            :                 goto out_byidx;
    4017                 :          3 :         net->xfrm.policy_idx_hmask = hmask;
    4018                 :            : 
    4019                 :          3 :         for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
    4020                 :            :                 struct xfrm_policy_hash *htab;
    4021                 :            : 
    4022                 :          3 :                 net->xfrm.policy_count[dir] = 0;
    4023                 :          3 :                 net->xfrm.policy_count[XFRM_POLICY_MAX + dir] = 0;
    4024                 :          3 :                 INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
    4025                 :            : 
    4026                 :            :                 htab = &net->xfrm.policy_bydst[dir];
    4027                 :          3 :                 htab->table = xfrm_hash_alloc(sz);
    4028                 :          3 :                 if (!htab->table)
    4029                 :            :                         goto out_bydst;
    4030                 :          3 :                 htab->hmask = hmask;
    4031                 :          3 :                 htab->dbits4 = 32;
    4032                 :          3 :                 htab->sbits4 = 32;
    4033                 :          3 :                 htab->dbits6 = 128;
    4034                 :          3 :                 htab->sbits6 = 128;
    4035                 :            :         }
    4036                 :          3 :         net->xfrm.policy_hthresh.lbits4 = 32;
    4037                 :          3 :         net->xfrm.policy_hthresh.rbits4 = 32;
    4038                 :          3 :         net->xfrm.policy_hthresh.lbits6 = 128;
    4039                 :          3 :         net->xfrm.policy_hthresh.rbits6 = 128;
    4040                 :            : 
    4041                 :          3 :         seqlock_init(&net->xfrm.policy_hthresh.lock);
    4042                 :            : 
    4043                 :          3 :         INIT_LIST_HEAD(&net->xfrm.policy_all);
    4044                 :          3 :         INIT_LIST_HEAD(&net->xfrm.inexact_bins);
    4045                 :          3 :         INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
    4046                 :          3 :         INIT_WORK(&net->xfrm.policy_hthresh.work, xfrm_hash_rebuild);
    4047                 :          3 :         return 0;
    4048                 :            : 
    4049                 :            : out_bydst:
    4050                 :          0 :         for (dir--; dir >= 0; dir--) {
    4051                 :            :                 struct xfrm_policy_hash *htab;
    4052                 :            : 
    4053                 :            :                 htab = &net->xfrm.policy_bydst[dir];
    4054                 :          0 :                 xfrm_hash_free(htab->table, sz);
    4055                 :            :         }
    4056                 :          0 :         xfrm_hash_free(net->xfrm.policy_byidx, sz);
    4057                 :            : out_byidx:
    4058                 :            :         return -ENOMEM;
    4059                 :            : }
    4060                 :            : 
    4061                 :          1 : static void xfrm_policy_fini(struct net *net)
    4062                 :            : {
    4063                 :            :         struct xfrm_pol_inexact_bin *b, *t;
    4064                 :            :         unsigned int sz;
    4065                 :            :         int dir;
    4066                 :            : 
    4067                 :          1 :         flush_work(&net->xfrm.policy_hash_work);
    4068                 :            : #ifdef CONFIG_XFRM_SUB_POLICY
    4069                 :            :         xfrm_policy_flush(net, XFRM_POLICY_TYPE_SUB, false);
    4070                 :            : #endif
    4071                 :          1 :         xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, false);
    4072                 :            : 
    4073                 :          1 :         WARN_ON(!list_empty(&net->xfrm.policy_all));
    4074                 :            : 
    4075                 :          1 :         for (dir = 0; dir < XFRM_POLICY_MAX; dir++) {
    4076                 :            :                 struct xfrm_policy_hash *htab;
    4077                 :            : 
    4078                 :          1 :                 WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
    4079                 :            : 
    4080                 :            :                 htab = &net->xfrm.policy_bydst[dir];
    4081                 :          1 :                 sz = (htab->hmask + 1) * sizeof(struct hlist_head);
    4082                 :          1 :                 WARN_ON(!hlist_empty(htab->table));
    4083                 :          1 :                 xfrm_hash_free(htab->table, sz);
    4084                 :            :         }
    4085                 :            : 
    4086                 :          1 :         sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
    4087                 :          1 :         WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
    4088                 :          1 :         xfrm_hash_free(net->xfrm.policy_byidx, sz);
    4089                 :            : 
    4090                 :            :         spin_lock_bh(&net->xfrm.xfrm_policy_lock);
    4091                 :          1 :         list_for_each_entry_safe(b, t, &net->xfrm.inexact_bins, inexact_bins)
    4092                 :          0 :                 __xfrm_policy_inexact_prune_bin(b, true);
    4093                 :            :         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    4094                 :          1 : }
    4095                 :            : 
    4096                 :          3 : static int __net_init xfrm_net_init(struct net *net)
    4097                 :            : {
    4098                 :            :         int rv;
    4099                 :            : 
    4100                 :            :         /* Initialize the per-net locks here */
    4101                 :          3 :         spin_lock_init(&net->xfrm.xfrm_state_lock);
    4102                 :          3 :         spin_lock_init(&net->xfrm.xfrm_policy_lock);
    4103                 :          3 :         mutex_init(&net->xfrm.xfrm_cfg_mutex);
    4104                 :            : 
    4105                 :            :         rv = xfrm_statistics_init(net);
    4106                 :            :         if (rv < 0)
    4107                 :            :                 goto out_statistics;
    4108                 :          3 :         rv = xfrm_state_init(net);
    4109                 :          3 :         if (rv < 0)
    4110                 :            :                 goto out_state;
    4111                 :          3 :         rv = xfrm_policy_init(net);
    4112                 :          3 :         if (rv < 0)
    4113                 :            :                 goto out_policy;
    4114                 :          3 :         rv = xfrm_sysctl_init(net);
    4115                 :          3 :         if (rv < 0)
    4116                 :            :                 goto out_sysctl;
    4117                 :            : 
    4118                 :            :         return 0;
    4119                 :            : 
    4120                 :            : out_sysctl:
    4121                 :          0 :         xfrm_policy_fini(net);
    4122                 :            : out_policy:
    4123                 :          0 :         xfrm_state_fini(net);
    4124                 :            : out_state:
    4125                 :            :         xfrm_statistics_fini(net);
    4126                 :            : out_statistics:
    4127                 :          0 :         return rv;
    4128                 :            : }
    4129                 :            : 
    4130                 :          1 : static void __net_exit xfrm_net_exit(struct net *net)
    4131                 :            : {
    4132                 :          1 :         xfrm_sysctl_fini(net);
    4133                 :          1 :         xfrm_policy_fini(net);
    4134                 :          1 :         xfrm_state_fini(net);
    4135                 :            :         xfrm_statistics_fini(net);
    4136                 :          1 : }
    4137                 :            : 
    4138                 :            : static struct pernet_operations __net_initdata xfrm_net_ops = {
    4139                 :            :         .init = xfrm_net_init,
    4140                 :            :         .exit = xfrm_net_exit,
    4141                 :            : };
    4142                 :            : 
    4143                 :          3 : void __init xfrm_init(void)
    4144                 :            : {
    4145                 :          3 :         register_pernet_subsys(&xfrm_net_ops);
    4146                 :          3 :         xfrm_dev_init();
    4147                 :            :         seqcount_init(&xfrm_policy_hash_generation);
    4148                 :          3 :         xfrm_input_init();
    4149                 :            : 
    4150                 :            :         RCU_INIT_POINTER(xfrm_if_cb, NULL);
    4151                 :          3 :         synchronize_rcu();
    4152                 :          3 : }
    4153                 :            : 
    4154                 :            : #ifdef CONFIG_AUDITSYSCALL
    4155                 :          0 : static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
    4156                 :            :                                          struct audit_buffer *audit_buf)
    4157                 :            : {
    4158                 :          0 :         struct xfrm_sec_ctx *ctx = xp->security;
    4159                 :            :         struct xfrm_selector *sel = &xp->selector;
    4160                 :            : 
    4161                 :          0 :         if (ctx)
    4162                 :          0 :                 audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
    4163                 :          0 :                                  ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
    4164                 :            : 
    4165                 :          0 :         switch (sel->family) {
    4166                 :            :         case AF_INET:
    4167                 :          0 :                 audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
    4168                 :          0 :                 if (sel->prefixlen_s != 32)
    4169                 :          0 :                         audit_log_format(audit_buf, " src_prefixlen=%d",
    4170                 :            :                                          sel->prefixlen_s);
    4171                 :          0 :                 audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
    4172                 :          0 :                 if (sel->prefixlen_d != 32)
    4173                 :          0 :                         audit_log_format(audit_buf, " dst_prefixlen=%d",
    4174                 :            :                                          sel->prefixlen_d);
    4175                 :            :                 break;
    4176                 :            :         case AF_INET6:
    4177                 :          0 :                 audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
    4178                 :          0 :                 if (sel->prefixlen_s != 128)
    4179                 :          0 :                         audit_log_format(audit_buf, " src_prefixlen=%d",
    4180                 :            :                                          sel->prefixlen_s);
    4181                 :          0 :                 audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
    4182                 :          0 :                 if (sel->prefixlen_d != 128)
    4183                 :          0 :                         audit_log_format(audit_buf, " dst_prefixlen=%d",
    4184                 :            :                                          sel->prefixlen_d);
    4185                 :            :                 break;
    4186                 :            :         }
    4187                 :          0 : }
    4188                 :            : 
    4189                 :          0 : void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid)
    4190                 :            : {
    4191                 :            :         struct audit_buffer *audit_buf;
    4192                 :            : 
    4193                 :          0 :         audit_buf = xfrm_audit_start("SPD-add");
    4194                 :          0 :         if (audit_buf == NULL)
    4195                 :          0 :                 return;
    4196                 :          0 :         xfrm_audit_helper_usrinfo(task_valid, audit_buf);
    4197                 :          0 :         audit_log_format(audit_buf, " res=%u", result);
    4198                 :          0 :         xfrm_audit_common_policyinfo(xp, audit_buf);
    4199                 :          0 :         audit_log_end(audit_buf);
    4200                 :            : }
    4201                 :            : EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
    4202                 :            : 
    4203                 :          0 : void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
    4204                 :            :                               bool task_valid)
    4205                 :            : {
    4206                 :            :         struct audit_buffer *audit_buf;
    4207                 :            : 
    4208                 :          0 :         audit_buf = xfrm_audit_start("SPD-delete");
    4209                 :          0 :         if (audit_buf == NULL)
    4210                 :          0 :                 return;
    4211                 :          0 :         xfrm_audit_helper_usrinfo(task_valid, audit_buf);
    4212                 :          0 :         audit_log_format(audit_buf, " res=%u", result);
    4213                 :          0 :         xfrm_audit_common_policyinfo(xp, audit_buf);
    4214                 :          0 :         audit_log_end(audit_buf);
    4215                 :            : }
    4216                 :            : EXPORT_SYMBOL_GPL(xfrm_audit_policy_delete);
    4217                 :            : #endif
    4218                 :            : 
    4219                 :            : #ifdef CONFIG_XFRM_MIGRATE
    4220                 :            : static bool xfrm_migrate_selector_match(const struct xfrm_selector *sel_cmp,
    4221                 :            :                                         const struct xfrm_selector *sel_tgt)
    4222                 :            : {
    4223                 :            :         if (sel_cmp->proto == IPSEC_ULPROTO_ANY) {
    4224                 :            :                 if (sel_tgt->family == sel_cmp->family &&
    4225                 :            :                     xfrm_addr_equal(&sel_tgt->daddr, &sel_cmp->daddr,
    4226                 :            :                                     sel_cmp->family) &&
    4227                 :            :                     xfrm_addr_equal(&sel_tgt->saddr, &sel_cmp->saddr,
    4228                 :            :                                     sel_cmp->family) &&
    4229                 :            :                     sel_tgt->prefixlen_d == sel_cmp->prefixlen_d &&
    4230                 :            :                     sel_tgt->prefixlen_s == sel_cmp->prefixlen_s) {
    4231                 :            :                         return true;
    4232                 :            :                 }
    4233                 :            :         } else {
    4234                 :            :                 if (memcmp(sel_tgt, sel_cmp, sizeof(*sel_tgt)) == 0) {
    4235                 :            :                         return true;
    4236                 :            :                 }
    4237                 :            :         }
    4238                 :            :         return false;
    4239                 :            : }
    4240                 :            : 
    4241                 :            : static struct xfrm_policy *xfrm_migrate_policy_find(const struct xfrm_selector *sel,
    4242                 :            :                                                     u8 dir, u8 type, struct net *net)
    4243                 :            : {
    4244                 :            :         struct xfrm_policy *pol, *ret = NULL;
    4245                 :            :         struct hlist_head *chain;
    4246                 :            :         u32 priority = ~0U;
    4247                 :            : 
    4248                 :            :         spin_lock_bh(&net->xfrm.xfrm_policy_lock);
    4249                 :            :         chain = policy_hash_direct(net, &sel->daddr, &sel->saddr, sel->family, dir);
    4250                 :            :         hlist_for_each_entry(pol, chain, bydst) {
    4251                 :            :                 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
    4252                 :            :                     pol->type == type) {
    4253                 :            :                         ret = pol;
    4254                 :            :                         priority = ret->priority;
    4255                 :            :                         break;
    4256                 :            :                 }
    4257                 :            :         }
    4258                 :            :         chain = &net->xfrm.policy_inexact[dir];
    4259                 :            :         hlist_for_each_entry(pol, chain, bydst_inexact_list) {
    4260                 :            :                 if ((pol->priority >= priority) && ret)
    4261                 :            :                         break;
    4262                 :            : 
    4263                 :            :                 if (xfrm_migrate_selector_match(sel, &pol->selector) &&
    4264                 :            :                     pol->type == type) {
    4265                 :            :                         ret = pol;
    4266                 :            :                         break;
    4267                 :            :                 }
    4268                 :            :         }
    4269                 :            : 
    4270                 :            :         xfrm_pol_hold(ret);
    4271                 :            : 
    4272                 :            :         spin_unlock_bh(&net->xfrm.xfrm_policy_lock);
    4273                 :            : 
    4274                 :            :         return ret;
    4275                 :            : }
    4276                 :            : 
    4277                 :            : static int migrate_tmpl_match(const struct xfrm_migrate *m, const struct xfrm_tmpl *t)
    4278                 :            : {
    4279                 :            :         int match = 0;
    4280                 :            : 
    4281                 :            :         if (t->mode == m->mode && t->id.proto == m->proto &&
    4282                 :            :             (m->reqid == 0 || t->reqid == m->reqid)) {
    4283                 :            :                 switch (t->mode) {
    4284                 :            :                 case XFRM_MODE_TUNNEL:
    4285                 :            :                 case XFRM_MODE_BEET:
    4286                 :            :                         if (xfrm_addr_equal(&t->id.daddr, &m->old_daddr,
    4287                 :            :                                             m->old_family) &&
    4288                 :            :                             xfrm_addr_equal(&t->saddr, &m->old_saddr,
    4289                 :            :                                             m->old_family)) {
    4290                 :            :                                 match = 1;
    4291                 :            :                         }
    4292                 :            :                         break;
    4293                 :            :                 case XFRM_MODE_TRANSPORT:
    4294                 :            :                         /* in case of transport mode, template does not store
    4295                 :            :                            any IP addresses, hence we just compare mode and
    4296                 :            :                            protocol */
    4297                 :            :                         match = 1;
    4298                 :            :                         break;
    4299                 :            :                 default:
    4300                 :            :                         break;
    4301                 :            :                 }
    4302                 :            :         }
    4303                 :            :         return match;
    4304                 :            : }
    4305                 :            : 
    4306                 :            : /* update endpoint address(es) of template(s) */
    4307                 :            : static int xfrm_policy_migrate(struct xfrm_policy *pol,
    4308                 :            :                                struct xfrm_migrate *m, int num_migrate)
    4309                 :            : {
    4310                 :            :         struct xfrm_migrate *mp;
    4311                 :            :         int i, j, n = 0;
    4312                 :            : 
    4313                 :            :         write_lock_bh(&pol->lock);
    4314                 :            :         if (unlikely(pol->walk.dead)) {
    4315                 :            :                 /* target policy has been deleted */
    4316                 :            :                 write_unlock_bh(&pol->lock);
    4317                 :            :                 return -ENOENT;
    4318                 :            :         }
    4319                 :            : 
    4320                 :            :         for (i = 0; i < pol->xfrm_nr; i++) {
    4321                 :            :                 for (j = 0, mp = m; j < num_migrate; j++, mp++) {
    4322                 :            :                         if (!migrate_tmpl_match(mp, &pol->xfrm_vec[i]))
    4323                 :            :                                 continue;
    4324                 :            :                         n++;
    4325                 :            :                         if (pol->xfrm_vec[i].mode != XFRM_MODE_TUNNEL &&
    4326                 :            :                             pol->xfrm_vec[i].mode != XFRM_MODE_BEET)
    4327                 :            :                                 continue;
    4328                 :            :                         /* update endpoints */
    4329                 :            :                         memcpy(&pol->xfrm_vec[i].id.daddr, &mp->new_daddr,
    4330                 :            :                                sizeof(pol->xfrm_vec[i].id.daddr));
    4331                 :            :                         memcpy(&pol->xfrm_vec[i].saddr, &mp->new_saddr,
    4332                 :            :                                sizeof(pol->xfrm_vec[i].saddr));
    4333                 :            :                         pol->xfrm_vec[i].encap_family = mp->new_family;
    4334                 :            :                         /* flush bundles */
    4335                 :            :                         atomic_inc(&pol->genid);
    4336                 :            :                 }
    4337                 :            :         }
    4338                 :            : 
    4339                 :            :         write_unlock_bh(&pol->lock);
    4340                 :            : 
    4341                 :            :         if (!n)
    4342                 :            :                 return -ENODATA;
    4343                 :            : 
    4344                 :            :         return 0;
    4345                 :            : }
    4346                 :            : 
    4347                 :            : static int xfrm_migrate_check(const struct xfrm_migrate *m, int num_migrate)
    4348                 :            : {
    4349                 :            :         int i, j;
    4350                 :            : 
    4351                 :            :         if (num_migrate < 1 || num_migrate > XFRM_MAX_DEPTH)
    4352                 :            :                 return -EINVAL;
    4353                 :            : 
    4354                 :            :         for (i = 0; i < num_migrate; i++) {
    4355                 :            :                 if (xfrm_addr_any(&m[i].new_daddr, m[i].new_family) ||
    4356                 :            :                     xfrm_addr_any(&m[i].new_saddr, m[i].new_family))
    4357                 :            :                         return -EINVAL;
    4358                 :            : 
    4359                 :            :                 /* check if there is any duplicated entry */
    4360                 :            :                 for (j = i + 1; j < num_migrate; j++) {
    4361                 :            :                         if (!memcmp(&m[i].old_daddr, &m[j].old_daddr,
    4362                 :            :                                     sizeof(m[i].old_daddr)) &&
    4363                 :            :                             !memcmp(&m[i].old_saddr, &m[j].old_saddr,
    4364                 :            :                                     sizeof(m[i].old_saddr)) &&
    4365                 :            :                             m[i].proto == m[j].proto &&
    4366                 :            :                             m[i].mode == m[j].mode &&
    4367                 :            :                             m[i].reqid == m[j].reqid &&
    4368                 :            :                             m[i].old_family == m[j].old_family)
    4369                 :            :                                 return -EINVAL;
    4370                 :            :                 }
    4371                 :            :         }
    4372                 :            : 
    4373                 :            :         return 0;
    4374                 :            : }
    4375                 :            : 
    4376                 :            : int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
    4377                 :            :                  struct xfrm_migrate *m, int num_migrate,
    4378                 :            :                  struct xfrm_kmaddress *k, struct net *net,
    4379                 :            :                  struct xfrm_encap_tmpl *encap)
    4380                 :            : {
    4381                 :            :         int i, err, nx_cur = 0, nx_new = 0;
    4382                 :            :         struct xfrm_policy *pol = NULL;
    4383                 :            :         struct xfrm_state *x, *xc;
    4384                 :            :         struct xfrm_state *x_cur[XFRM_MAX_DEPTH];
    4385                 :            :         struct xfrm_state *x_new[XFRM_MAX_DEPTH];
    4386                 :            :         struct xfrm_migrate *mp;
    4387                 :            : 
    4388                 :            :         /* Stage 0 - sanity checks */
    4389                 :            :         if ((err = xfrm_migrate_check(m, num_migrate)) < 0)
    4390                 :            :                 goto out;
    4391                 :            : 
    4392                 :            :         if (dir >= XFRM_POLICY_MAX) {
    4393                 :            :                 err = -EINVAL;
    4394                 :            :                 goto out;
    4395                 :            :         }
    4396                 :            : 
    4397                 :            :         /* Stage 1 - find policy */
    4398                 :            :         if ((pol = xfrm_migrate_policy_find(sel, dir, type, net)) == NULL) {
    4399                 :            :                 err = -ENOENT;
    4400                 :            :                 goto out;
    4401                 :            :         }
    4402                 :            : 
    4403                 :            :         /* Stage 2 - find and update state(s) */
    4404                 :            :         for (i = 0, mp = m; i < num_migrate; i++, mp++) {
    4405                 :            :                 if ((x = xfrm_migrate_state_find(mp, net))) {
    4406                 :            :                         x_cur[nx_cur] = x;
    4407                 :            :                         nx_cur++;
    4408                 :            :                         xc = xfrm_state_migrate(x, mp, encap);
    4409                 :            :                         if (xc) {
    4410                 :            :                                 x_new[nx_new] = xc;
    4411                 :            :                                 nx_new++;
    4412                 :            :                         } else {
    4413                 :            :                                 err = -ENODATA;
    4414                 :            :                                 goto restore_state;
    4415                 :            :                         }
    4416                 :            :                 }
    4417                 :            :         }
    4418                 :            : 
    4419                 :            :         /* Stage 3 - update policy */
    4420                 :            :         if ((err = xfrm_policy_migrate(pol, m, num_migrate)) < 0)
    4421                 :            :                 goto restore_state;
    4422                 :            : 
    4423                 :            :         /* Stage 4 - delete old state(s) */
    4424                 :            :         if (nx_cur) {
    4425                 :            :                 xfrm_states_put(x_cur, nx_cur);
    4426                 :            :                 xfrm_states_delete(x_cur, nx_cur);
    4427                 :            :         }
    4428                 :            : 
    4429                 :            :         /* Stage 5 - announce */
    4430                 :            :         km_migrate(sel, dir, type, m, num_migrate, k, encap);
    4431                 :            : 
    4432                 :            :         xfrm_pol_put(pol);
    4433                 :            : 
    4434                 :            :         return 0;
    4435                 :            : out:
    4436                 :            :         return err;
    4437                 :            : 
    4438                 :            : restore_state:
    4439                 :            :         if (pol)
    4440                 :            :                 xfrm_pol_put(pol);
    4441                 :            :         if (nx_cur)
    4442                 :            :                 xfrm_states_put(x_cur, nx_cur);
    4443                 :            :         if (nx_new)
    4444                 :            :                 xfrm_states_delete(x_new, nx_new);
    4445                 :            : 
    4446                 :            :         return err;
    4447                 :            : }
    4448                 :            : EXPORT_SYMBOL(xfrm_migrate);
    4449                 :            : #endif
    

Generated by: LCOV version 1.14