LCOV - code coverage report
Current view: top level - net/xfrm - xfrm_user.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 1169 0.0 %
Date: 2014-04-07 Functions: 0 79 0.0 %
Branches: 0 760 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* xfrm_user.c: User interface to configure xfrm engine.
       2                 :            :  *
       3                 :            :  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
       4                 :            :  *
       5                 :            :  * Changes:
       6                 :            :  *      Mitsuru KANDA @USAGI
       7                 :            :  *      Kazunori MIYAZAWA @USAGI
       8                 :            :  *      Kunihiro Ishiguro <kunihiro@ipinfusion.com>
       9                 :            :  *              IPv6 support
      10                 :            :  *
      11                 :            :  */
      12                 :            : 
      13                 :            : #include <linux/crypto.h>
      14                 :            : #include <linux/module.h>
      15                 :            : #include <linux/kernel.h>
      16                 :            : #include <linux/types.h>
      17                 :            : #include <linux/slab.h>
      18                 :            : #include <linux/socket.h>
      19                 :            : #include <linux/string.h>
      20                 :            : #include <linux/net.h>
      21                 :            : #include <linux/skbuff.h>
      22                 :            : #include <linux/pfkeyv2.h>
      23                 :            : #include <linux/ipsec.h>
      24                 :            : #include <linux/init.h>
      25                 :            : #include <linux/security.h>
      26                 :            : #include <net/sock.h>
      27                 :            : #include <net/xfrm.h>
      28                 :            : #include <net/netlink.h>
      29                 :            : #include <net/ah.h>
      30                 :            : #include <asm/uaccess.h>
      31                 :            : #if IS_ENABLED(CONFIG_IPV6)
      32                 :            : #include <linux/in6.h>
      33                 :            : #endif
      34                 :            : 
      35                 :            : static inline int aead_len(struct xfrm_algo_aead *alg)
      36                 :            : {
      37                 :          0 :         return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
      38                 :            : }
      39                 :            : 
      40                 :          0 : static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
      41                 :            : {
      42                 :          0 :         struct nlattr *rt = attrs[type];
      43                 :          0 :         struct xfrm_algo *algp;
      44                 :            : 
      45         [ #  # ]:          0 :         if (!rt)
      46                 :            :                 return 0;
      47                 :            : 
      48                 :            :         algp = nla_data(rt);
      49         [ #  # ]:          0 :         if (nla_len(rt) < xfrm_alg_len(algp))
      50                 :            :                 return -EINVAL;
      51                 :            : 
      52         [ #  # ]:          0 :         switch (type) {
      53                 :            :         case XFRMA_ALG_AUTH:
      54                 :            :         case XFRMA_ALG_CRYPT:
      55                 :            :         case XFRMA_ALG_COMP:
      56                 :            :                 break;
      57                 :            : 
      58                 :            :         default:
      59                 :            :                 return -EINVAL;
      60                 :            :         }
      61                 :            : 
      62                 :          0 :         algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
      63                 :          0 :         return 0;
      64                 :            : }
      65                 :            : 
      66                 :            : static int verify_auth_trunc(struct nlattr **attrs)
      67                 :            : {
      68                 :          0 :         struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
      69                 :          0 :         struct xfrm_algo_auth *algp;
      70                 :            : 
      71         [ #  # ]:          0 :         if (!rt)
      72                 :            :                 return 0;
      73                 :            : 
      74                 :            :         algp = nla_data(rt);
      75         [ #  # ]:          0 :         if (nla_len(rt) < xfrm_alg_auth_len(algp))
      76                 :            :                 return -EINVAL;
      77                 :            : 
      78                 :          0 :         algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
      79                 :            :         return 0;
      80                 :            : }
      81                 :            : 
      82                 :            : static int verify_aead(struct nlattr **attrs)
      83                 :            : {
      84                 :          0 :         struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
      85                 :          0 :         struct xfrm_algo_aead *algp;
      86                 :            : 
      87         [ #  # ]:          0 :         if (!rt)
      88                 :            :                 return 0;
      89                 :            : 
      90                 :            :         algp = nla_data(rt);
      91         [ #  # ]:          0 :         if (nla_len(rt) < aead_len(algp))
      92                 :            :                 return -EINVAL;
      93                 :            : 
      94                 :          0 :         algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
      95                 :            :         return 0;
      96                 :            : }
      97                 :            : 
      98                 :            : static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
      99                 :            :                            xfrm_address_t **addrp)
     100                 :            : {
     101                 :          0 :         struct nlattr *rt = attrs[type];
     102                 :            : 
     103         [ #  # ]:          0 :         if (rt && addrp)
     104                 :          0 :                 *addrp = nla_data(rt);
     105                 :            : }
     106                 :            : 
     107                 :            : static inline int verify_sec_ctx_len(struct nlattr **attrs)
     108                 :            : {
     109                 :          0 :         struct nlattr *rt = attrs[XFRMA_SEC_CTX];
     110                 :            :         struct xfrm_user_sec_ctx *uctx;
     111                 :            : 
     112 [ #  # ][ #  # ]:          0 :         if (!rt)
         [ #  # ][ #  # ]
     113                 :            :                 return 0;
     114                 :            : 
     115                 :            :         uctx = nla_data(rt);
     116 [ #  # ][ #  # ]:          0 :         if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
         [ #  # ][ #  # ]
     117                 :            :                 return -EINVAL;
     118                 :            : 
     119                 :            :         return 0;
     120                 :            : }
     121                 :            : 
     122                 :            : static inline int verify_replay(struct xfrm_usersa_info *p,
     123                 :            :                                 struct nlattr **attrs)
     124                 :            : {
     125                 :          0 :         struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
     126                 :            :         struct xfrm_replay_state_esn *rs;
     127                 :            : 
     128         [ #  # ]:          0 :         if (p->flags & XFRM_STATE_ESN) {
     129         [ #  # ]:          0 :                 if (!rt)
     130                 :            :                         return -EINVAL;
     131                 :            : 
     132                 :            :                 rs = nla_data(rt);
     133                 :            : 
     134         [ #  # ]:          0 :                 if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
     135                 :            :                         return -EINVAL;
     136                 :            : 
     137 [ #  # ][ #  # ]:          0 :                 if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
     138                 :            :                     nla_len(rt) != sizeof(*rs))
     139                 :            :                         return -EINVAL;
     140                 :            :         }
     141                 :            : 
     142         [ #  # ]:          0 :         if (!rt)
     143                 :            :                 return 0;
     144                 :            : 
     145         [ #  # ]:          0 :         if (p->id.proto != IPPROTO_ESP)
     146                 :            :                 return -EINVAL;
     147                 :            : 
     148         [ #  # ]:          0 :         if (p->replay_window != 0)
     149                 :            :                 return -EINVAL;
     150                 :            : 
     151                 :            :         return 0;
     152                 :            : }
     153                 :            : 
     154                 :          0 : static int verify_newsa_info(struct xfrm_usersa_info *p,
     155                 :            :                              struct nlattr **attrs)
     156                 :            : {
     157                 :            :         int err;
     158                 :            : 
     159                 :            :         err = -EINVAL;
     160         [ #  # ]:          0 :         switch (p->family) {
     161                 :            :         case AF_INET:
     162                 :            :                 break;
     163                 :            : 
     164                 :            :         case AF_INET6:
     165                 :            : #if IS_ENABLED(CONFIG_IPV6)
     166                 :            :                 break;
     167                 :            : #else
     168                 :            :                 err = -EAFNOSUPPORT;
     169                 :            :                 goto out;
     170                 :            : #endif
     171                 :            : 
     172                 :            :         default:
     173                 :            :                 goto out;
     174                 :            :         }
     175                 :            : 
     176                 :            :         err = -EINVAL;
     177   [ #  #  #  #  :          0 :         switch (p->id.proto) {
                      # ]
     178                 :            :         case IPPROTO_AH:
     179 [ #  # ][ #  # ]:          0 :                 if ((!attrs[XFRMA_ALG_AUTH]     &&
     180         [ #  # ]:          0 :                      !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
     181         [ #  # ]:          0 :                     attrs[XFRMA_ALG_AEAD]       ||
     182         [ #  # ]:          0 :                     attrs[XFRMA_ALG_CRYPT]      ||
     183         [ #  # ]:          0 :                     attrs[XFRMA_ALG_COMP]       ||
     184                 :          0 :                     attrs[XFRMA_TFCPAD])
     185                 :            :                         goto out;
     186                 :            :                 break;
     187                 :            : 
     188                 :            :         case IPPROTO_ESP:
     189         [ #  # ]:          0 :                 if (attrs[XFRMA_ALG_COMP])
     190                 :            :                         goto out;
     191 [ #  # ][ #  # ]:          0 :                 if (!attrs[XFRMA_ALG_AUTH] &&
     192         [ #  # ]:          0 :                     !attrs[XFRMA_ALG_AUTH_TRUNC] &&
     193         [ #  # ]:          0 :                     !attrs[XFRMA_ALG_CRYPT] &&
     194                 :          0 :                     !attrs[XFRMA_ALG_AEAD])
     195                 :            :                         goto out;
     196 [ #  # ][ #  # ]:          0 :                 if ((attrs[XFRMA_ALG_AUTH] ||
     197         [ #  # ]:          0 :                      attrs[XFRMA_ALG_AUTH_TRUNC] ||
     198         [ #  # ]:          0 :                      attrs[XFRMA_ALG_CRYPT]) &&
     199                 :          0 :                     attrs[XFRMA_ALG_AEAD])
     200                 :            :                         goto out;
     201 [ #  # ][ #  # ]:          0 :                 if (attrs[XFRMA_TFCPAD] &&
     202                 :          0 :                     p->mode != XFRM_MODE_TUNNEL)
     203                 :            :                         goto out;
     204                 :            :                 break;
     205                 :            : 
     206                 :            :         case IPPROTO_COMP:
     207 [ #  # ][ #  # ]:          0 :                 if (!attrs[XFRMA_ALG_COMP]      ||
     208         [ #  # ]:          0 :                     attrs[XFRMA_ALG_AEAD]       ||
     209         [ #  # ]:          0 :                     attrs[XFRMA_ALG_AUTH]       ||
     210         [ #  # ]:          0 :                     attrs[XFRMA_ALG_AUTH_TRUNC] ||
     211         [ #  # ]:          0 :                     attrs[XFRMA_ALG_CRYPT]      ||
     212                 :          0 :                     attrs[XFRMA_TFCPAD])
     213                 :            :                         goto out;
     214                 :            :                 break;
     215                 :            : 
     216                 :            : #if IS_ENABLED(CONFIG_IPV6)
     217                 :            :         case IPPROTO_DSTOPTS:
     218                 :            :         case IPPROTO_ROUTING:
     219 [ #  # ][ #  # ]:          0 :                 if (attrs[XFRMA_ALG_COMP]       ||
     220         [ #  # ]:          0 :                     attrs[XFRMA_ALG_AUTH]       ||
     221         [ #  # ]:          0 :                     attrs[XFRMA_ALG_AUTH_TRUNC] ||
     222         [ #  # ]:          0 :                     attrs[XFRMA_ALG_AEAD]       ||
     223         [ #  # ]:          0 :                     attrs[XFRMA_ALG_CRYPT]      ||
     224         [ #  # ]:          0 :                     attrs[XFRMA_ENCAP]          ||
     225         [ #  # ]:          0 :                     attrs[XFRMA_SEC_CTX]        ||
     226         [ #  # ]:          0 :                     attrs[XFRMA_TFCPAD]         ||
     227                 :          0 :                     !attrs[XFRMA_COADDR])
     228                 :            :                         goto out;
     229                 :            :                 break;
     230                 :            : #endif
     231                 :            : 
     232                 :            :         default:
     233                 :            :                 goto out;
     234                 :            :         }
     235                 :            : 
     236         [ #  # ]:          0 :         if ((err = verify_aead(attrs)))
     237                 :            :                 goto out;
     238         [ #  # ]:          0 :         if ((err = verify_auth_trunc(attrs)))
     239                 :            :                 goto out;
     240         [ #  # ]:          0 :         if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
     241                 :            :                 goto out;
     242         [ #  # ]:          0 :         if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
     243                 :            :                 goto out;
     244         [ #  # ]:          0 :         if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
     245                 :            :                 goto out;
     246         [ #  # ]:          0 :         if ((err = verify_sec_ctx_len(attrs)))
     247                 :            :                 goto out;
     248         [ #  # ]:          0 :         if ((err = verify_replay(p, attrs)))
     249                 :            :                 goto out;
     250                 :            : 
     251                 :            :         err = -EINVAL;
     252 [ #  # ][ #  # ]:          0 :         switch (p->mode) {
     253                 :            :         case XFRM_MODE_TRANSPORT:
     254                 :            :         case XFRM_MODE_TUNNEL:
     255                 :            :         case XFRM_MODE_ROUTEOPTIMIZATION:
     256                 :            :         case XFRM_MODE_BEET:
     257                 :            :                 break;
     258                 :            : 
     259                 :            :         default:
     260                 :            :                 goto out;
     261                 :            :         }
     262                 :            : 
     263                 :            :         err = 0;
     264                 :            : 
     265                 :            : out:
     266                 :          0 :         return err;
     267                 :            : }
     268                 :            : 
     269                 :          0 : static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
     270                 :            :                            struct xfrm_algo_desc *(*get_byname)(const char *, int),
     271                 :            :                            struct nlattr *rta)
     272                 :            : {
     273                 :          0 :         struct xfrm_algo *p, *ualg;
     274                 :            :         struct xfrm_algo_desc *algo;
     275                 :            : 
     276         [ #  # ]:          0 :         if (!rta)
     277                 :            :                 return 0;
     278                 :            : 
     279                 :          0 :         ualg = nla_data(rta);
     280                 :            : 
     281                 :          0 :         algo = get_byname(ualg->alg_name, 1);
     282         [ #  # ]:          0 :         if (!algo)
     283                 :            :                 return -ENOSYS;
     284                 :          0 :         *props = algo->desc.sadb_alg_id;
     285                 :            : 
     286                 :          0 :         p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
     287         [ #  # ]:          0 :         if (!p)
     288                 :            :                 return -ENOMEM;
     289                 :            : 
     290                 :          0 :         strcpy(p->alg_name, algo->name);
     291                 :          0 :         *algpp = p;
     292                 :          0 :         return 0;
     293                 :            : }
     294                 :            : 
     295                 :          0 : static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
     296                 :            :                        struct nlattr *rta)
     297                 :            : {
     298                 :            :         struct xfrm_algo *ualg;
     299                 :            :         struct xfrm_algo_auth *p;
     300                 :            :         struct xfrm_algo_desc *algo;
     301                 :            : 
     302         [ #  # ]:          0 :         if (!rta)
     303                 :            :                 return 0;
     304                 :            : 
     305                 :            :         ualg = nla_data(rta);
     306                 :            : 
     307                 :          0 :         algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
     308         [ #  # ]:          0 :         if (!algo)
     309                 :            :                 return -ENOSYS;
     310                 :          0 :         *props = algo->desc.sadb_alg_id;
     311                 :            : 
     312                 :          0 :         p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
     313         [ #  # ]:          0 :         if (!p)
     314                 :            :                 return -ENOMEM;
     315                 :            : 
     316                 :          0 :         strcpy(p->alg_name, algo->name);
     317                 :          0 :         p->alg_key_len = ualg->alg_key_len;
     318                 :          0 :         p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
     319                 :          0 :         memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
     320                 :            : 
     321                 :          0 :         *algpp = p;
     322                 :          0 :         return 0;
     323                 :            : }
     324                 :            : 
     325                 :          0 : static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
     326                 :            :                              struct nlattr *rta)
     327                 :            : {
     328                 :          0 :         struct xfrm_algo_auth *p, *ualg;
     329                 :            :         struct xfrm_algo_desc *algo;
     330                 :            : 
     331         [ #  # ]:          0 :         if (!rta)
     332                 :            :                 return 0;
     333                 :            : 
     334                 :          0 :         ualg = nla_data(rta);
     335                 :            : 
     336                 :          0 :         algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
     337         [ #  # ]:          0 :         if (!algo)
     338                 :            :                 return -ENOSYS;
     339 [ #  # ][ #  # ]:          0 :         if ((ualg->alg_trunc_len / 8) > MAX_AH_AUTH_LEN ||
     340                 :          0 :             ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
     341                 :            :                 return -EINVAL;
     342                 :          0 :         *props = algo->desc.sadb_alg_id;
     343                 :            : 
     344                 :          0 :         p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
     345         [ #  # ]:          0 :         if (!p)
     346                 :            :                 return -ENOMEM;
     347                 :            : 
     348                 :          0 :         strcpy(p->alg_name, algo->name);
     349         [ #  # ]:          0 :         if (!p->alg_trunc_len)
     350                 :          0 :                 p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
     351                 :            : 
     352                 :          0 :         *algpp = p;
     353                 :          0 :         return 0;
     354                 :            : }
     355                 :            : 
     356                 :          0 : static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
     357                 :            :                        struct nlattr *rta)
     358                 :            : {
     359                 :          0 :         struct xfrm_algo_aead *p, *ualg;
     360                 :            :         struct xfrm_algo_desc *algo;
     361                 :            : 
     362         [ #  # ]:          0 :         if (!rta)
     363                 :            :                 return 0;
     364                 :            : 
     365                 :          0 :         ualg = nla_data(rta);
     366                 :            : 
     367                 :          0 :         algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
     368         [ #  # ]:          0 :         if (!algo)
     369                 :            :                 return -ENOSYS;
     370                 :          0 :         *props = algo->desc.sadb_alg_id;
     371                 :            : 
     372                 :          0 :         p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
     373         [ #  # ]:          0 :         if (!p)
     374                 :            :                 return -ENOMEM;
     375                 :            : 
     376                 :          0 :         strcpy(p->alg_name, algo->name);
     377                 :          0 :         *algpp = p;
     378                 :          0 :         return 0;
     379                 :            : }
     380                 :            : 
     381                 :          0 : static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
     382                 :          0 :                                          struct nlattr *rp)
     383                 :            : {
     384                 :          0 :         struct xfrm_replay_state_esn *up;
     385                 :            :         int ulen;
     386                 :            : 
     387         [ #  # ]:          0 :         if (!replay_esn || !rp)
     388                 :            :                 return 0;
     389                 :            : 
     390                 :            :         up = nla_data(rp);
     391                 :            :         ulen = xfrm_replay_state_esn_len(up);
     392                 :            : 
     393 [ #  # ][ #  # ]:          0 :         if (nla_len(rp) < ulen || xfrm_replay_state_esn_len(replay_esn) != ulen)
     394                 :            :                 return -EINVAL;
     395                 :            : 
     396                 :            :         return 0;
     397                 :            : }
     398                 :            : 
     399                 :          0 : static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
     400                 :            :                                        struct xfrm_replay_state_esn **preplay_esn,
     401                 :          0 :                                        struct nlattr *rta)
     402                 :            : {
     403                 :          0 :         struct xfrm_replay_state_esn *p, *pp, *up;
     404                 :            :         int klen, ulen;
     405                 :            : 
     406         [ #  # ]:          0 :         if (!rta)
     407                 :            :                 return 0;
     408                 :            : 
     409                 :          0 :         up = nla_data(rta);
     410                 :            :         klen = xfrm_replay_state_esn_len(up);
     411         [ #  # ]:          0 :         ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
     412                 :            : 
     413                 :            :         p = kzalloc(klen, GFP_KERNEL);
     414         [ #  # ]:          0 :         if (!p)
     415                 :            :                 return -ENOMEM;
     416                 :            : 
     417                 :            :         pp = kzalloc(klen, GFP_KERNEL);
     418         [ #  # ]:          0 :         if (!pp) {
     419                 :          0 :                 kfree(p);
     420                 :          0 :                 return -ENOMEM;
     421                 :            :         }
     422                 :            : 
     423                 :          0 :         memcpy(p, up, ulen);
     424                 :          0 :         memcpy(pp, up, ulen);
     425                 :            : 
     426                 :          0 :         *replay_esn = p;
     427                 :          0 :         *preplay_esn = pp;
     428                 :            : 
     429                 :          0 :         return 0;
     430                 :            : }
     431                 :            : 
     432                 :            : static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
     433                 :            : {
     434                 :            :         int len = 0;
     435                 :            : 
     436 [ #  # ][ #  # ]:          0 :         if (xfrm_ctx) {
     437                 :            :                 len += sizeof(struct xfrm_user_sec_ctx);
     438                 :          0 :                 len += xfrm_ctx->ctx_len;
     439                 :            :         }
     440                 :            :         return len;
     441                 :            : }
     442                 :            : 
     443                 :          0 : static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
     444                 :            : {
     445                 :          0 :         memcpy(&x->id, &p->id, sizeof(x->id));
     446                 :          0 :         memcpy(&x->sel, &p->sel, sizeof(x->sel));
     447                 :          0 :         memcpy(&x->lft, &p->lft, sizeof(x->lft));
     448                 :          0 :         x->props.mode = p->mode;
     449                 :          0 :         x->props.replay_window = min_t(unsigned int, p->replay_window,
     450                 :            :                                         sizeof(x->replay.bitmap) * 8);
     451                 :          0 :         x->props.reqid = p->reqid;
     452                 :          0 :         x->props.family = p->family;
     453                 :          0 :         memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
     454                 :          0 :         x->props.flags = p->flags;
     455                 :            : 
     456 [ #  # ][ #  # ]:          0 :         if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
     457                 :          0 :                 x->sel.family = p->family;
     458                 :          0 : }
     459                 :            : 
     460                 :            : /*
     461                 :            :  * someday when pfkey also has support, we could have the code
     462                 :            :  * somehow made shareable and move it to xfrm_state.c - JHS
     463                 :            :  *
     464                 :            : */
     465                 :          0 : static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
     466                 :            :                                   int update_esn)
     467                 :            : {
     468                 :          0 :         struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
     469         [ #  # ]:          0 :         struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
     470                 :          0 :         struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
     471                 :          0 :         struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
     472                 :          0 :         struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
     473                 :            : 
     474         [ #  # ]:          0 :         if (re) {
     475                 :          0 :                 struct xfrm_replay_state_esn *replay_esn;
     476                 :          0 :                 replay_esn = nla_data(re);
     477                 :          0 :                 memcpy(x->replay_esn, replay_esn,
     478                 :            :                        xfrm_replay_state_esn_len(replay_esn));
     479                 :          0 :                 memcpy(x->preplay_esn, replay_esn,
     480                 :            :                        xfrm_replay_state_esn_len(replay_esn));
     481                 :            :         }
     482                 :            : 
     483         [ #  # ]:          0 :         if (rp) {
     484                 :            :                 struct xfrm_replay_state *replay;
     485                 :          0 :                 replay = nla_data(rp);
     486                 :          0 :                 memcpy(&x->replay, replay, sizeof(*replay));
     487                 :          0 :                 memcpy(&x->preplay, replay, sizeof(*replay));
     488                 :            :         }
     489                 :            : 
     490         [ #  # ]:          0 :         if (lt) {
     491                 :            :                 struct xfrm_lifetime_cur *ltime;
     492                 :            :                 ltime = nla_data(lt);
     493                 :          0 :                 x->curlft.bytes = ltime->bytes;
     494                 :          0 :                 x->curlft.packets = ltime->packets;
     495                 :          0 :                 x->curlft.add_time = ltime->add_time;
     496                 :          0 :                 x->curlft.use_time = ltime->use_time;
     497                 :            :         }
     498                 :            : 
     499         [ #  # ]:          0 :         if (et)
     500                 :          0 :                 x->replay_maxage = nla_get_u32(et);
     501                 :            : 
     502         [ #  # ]:          0 :         if (rt)
     503                 :          0 :                 x->replay_maxdiff = nla_get_u32(rt);
     504                 :          0 : }
     505                 :            : 
     506                 :          0 : static struct xfrm_state *xfrm_state_construct(struct net *net,
     507                 :            :                                                struct xfrm_usersa_info *p,
     508                 :            :                                                struct nlattr **attrs,
     509                 :            :                                                int *errp)
     510                 :            : {
     511                 :          0 :         struct xfrm_state *x = xfrm_state_alloc(net);
     512                 :            :         int err = -ENOMEM;
     513                 :            : 
     514         [ #  # ]:          0 :         if (!x)
     515                 :            :                 goto error_no_put;
     516                 :            : 
     517                 :          0 :         copy_from_user_state(x, p);
     518                 :            : 
     519         [ #  # ]:          0 :         if (attrs[XFRMA_SA_EXTRA_FLAGS])
     520                 :          0 :                 x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
     521                 :            : 
     522         [ #  # ]:          0 :         if ((err = attach_aead(&x->aead, &x->props.ealgo,
     523                 :            :                                attrs[XFRMA_ALG_AEAD])))
     524                 :            :                 goto error;
     525         [ #  # ]:          0 :         if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
     526                 :            :                                      attrs[XFRMA_ALG_AUTH_TRUNC])))
     527                 :            :                 goto error;
     528         [ #  # ]:          0 :         if (!x->props.aalgo) {
     529         [ #  # ]:          0 :                 if ((err = attach_auth(&x->aalg, &x->props.aalgo,
     530                 :            :                                        attrs[XFRMA_ALG_AUTH])))
     531                 :            :                         goto error;
     532                 :            :         }
     533         [ #  # ]:          0 :         if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
     534                 :            :                                    xfrm_ealg_get_byname,
     535                 :            :                                    attrs[XFRMA_ALG_CRYPT])))
     536                 :            :                 goto error;
     537         [ #  # ]:          0 :         if ((err = attach_one_algo(&x->calg, &x->props.calgo,
     538                 :            :                                    xfrm_calg_get_byname,
     539                 :            :                                    attrs[XFRMA_ALG_COMP])))
     540                 :            :                 goto error;
     541                 :            : 
     542         [ #  # ]:          0 :         if (attrs[XFRMA_ENCAP]) {
     543                 :          0 :                 x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
     544                 :            :                                    sizeof(*x->encap), GFP_KERNEL);
     545         [ #  # ]:          0 :                 if (x->encap == NULL)
     546                 :            :                         goto error;
     547                 :            :         }
     548                 :            : 
     549         [ #  # ]:          0 :         if (attrs[XFRMA_TFCPAD])
     550                 :          0 :                 x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
     551                 :            : 
     552         [ #  # ]:          0 :         if (attrs[XFRMA_COADDR]) {
     553                 :          0 :                 x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
     554                 :            :                                     sizeof(*x->coaddr), GFP_KERNEL);
     555         [ #  # ]:          0 :                 if (x->coaddr == NULL)
     556                 :            :                         goto error;
     557                 :            :         }
     558                 :            : 
     559                 :          0 :         xfrm_mark_get(attrs, &x->mark);
     560                 :            : 
     561                 :          0 :         err = __xfrm_init_state(x, false);
     562         [ #  # ]:          0 :         if (err)
     563                 :            :                 goto error;
     564                 :            : 
     565                 :            :         if (attrs[XFRMA_SEC_CTX] &&
     566                 :            :             security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
     567                 :            :                 goto error;
     568                 :            : 
     569         [ #  # ]:          0 :         if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
     570                 :            :                                                attrs[XFRMA_REPLAY_ESN_VAL])))
     571                 :            :                 goto error;
     572                 :            : 
     573                 :          0 :         x->km.seq = p->seq;
     574                 :          0 :         x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
     575                 :            :         /* sysctl_xfrm_aevent_etime is in 100ms units */
     576                 :          0 :         x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
     577                 :            : 
     578         [ #  # ]:          0 :         if ((err = xfrm_init_replay(x)))
     579                 :            :                 goto error;
     580                 :            : 
     581                 :            :         /* override default values from above */
     582                 :          0 :         xfrm_update_ae_params(x, attrs, 0);
     583                 :            : 
     584                 :          0 :         return x;
     585                 :            : 
     586                 :            : error:
     587                 :          0 :         x->km.state = XFRM_STATE_DEAD;
     588                 :            :         xfrm_state_put(x);
     589                 :            : error_no_put:
     590                 :          0 :         *errp = err;
     591                 :          0 :         return NULL;
     592                 :            : }
     593                 :            : 
     594                 :          0 : static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
     595                 :            :                 struct nlattr **attrs)
     596                 :            : {
     597                 :            :         struct net *net = sock_net(skb->sk);
     598                 :          0 :         struct xfrm_usersa_info *p = nlmsg_data(nlh);
     599                 :            :         struct xfrm_state *x;
     600                 :            :         int err;
     601                 :            :         struct km_event c;
     602                 :          0 :         kuid_t loginuid = audit_get_loginuid(current);
     603                 :          0 :         u32 sessionid = audit_get_sessionid(current);
     604                 :            :         u32 sid;
     605                 :            : 
     606                 :          0 :         err = verify_newsa_info(p, attrs);
     607         [ #  # ]:          0 :         if (err)
     608                 :            :                 return err;
     609                 :            : 
     610                 :          0 :         x = xfrm_state_construct(net, p, attrs, &err);
     611         [ #  # ]:          0 :         if (!x)
     612                 :          0 :                 return err;
     613                 :            : 
     614                 :            :         xfrm_state_hold(x);
     615         [ #  # ]:          0 :         if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
     616                 :          0 :                 err = xfrm_state_add(x);
     617                 :            :         else
     618                 :          0 :                 err = xfrm_state_update(x);
     619                 :            : 
     620                 :          0 :         security_task_getsecid(current, &sid);
     621                 :          0 :         xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
     622                 :            : 
     623         [ #  # ]:          0 :         if (err < 0) {
     624                 :          0 :                 x->km.state = XFRM_STATE_DEAD;
     625                 :            :                 __xfrm_state_put(x);
     626                 :            :                 goto out;
     627                 :            :         }
     628                 :            : 
     629                 :          0 :         c.seq = nlh->nlmsg_seq;
     630                 :          0 :         c.portid = nlh->nlmsg_pid;
     631                 :          0 :         c.event = nlh->nlmsg_type;
     632                 :            : 
     633                 :          0 :         km_state_notify(x, &c);
     634                 :            : out:
     635                 :            :         xfrm_state_put(x);
     636                 :          0 :         return err;
     637                 :            : }
     638                 :            : 
     639                 :          0 : static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
     640                 :            :                                                  struct xfrm_usersa_id *p,
     641                 :            :                                                  struct nlattr **attrs,
     642                 :            :                                                  int *errp)
     643                 :            : {
     644                 :            :         struct xfrm_state *x = NULL;
     645                 :            :         struct xfrm_mark m;
     646                 :            :         int err;
     647                 :            :         u32 mark = xfrm_mark_get(attrs, &m);
     648                 :            : 
     649         [ #  # ]:          0 :         if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
     650                 :            :                 err = -ESRCH;
     651                 :          0 :                 x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
     652                 :            :         } else {
     653                 :            :                 xfrm_address_t *saddr = NULL;
     654                 :            : 
     655                 :            :                 verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
     656         [ #  # ]:          0 :                 if (!saddr) {
     657                 :            :                         err = -EINVAL;
     658                 :            :                         goto out;
     659                 :            :                 }
     660                 :            : 
     661                 :            :                 err = -ESRCH;
     662                 :          0 :                 x = xfrm_state_lookup_byaddr(net, mark,
     663                 :          0 :                                              &p->daddr, saddr,
     664                 :            :                                              p->proto, p->family);
     665                 :            :         }
     666                 :            : 
     667                 :            :  out:
     668         [ #  # ]:          0 :         if (!x && errp)
     669                 :          0 :                 *errp = err;
     670                 :          0 :         return x;
     671                 :            : }
     672                 :            : 
     673                 :          0 : static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
     674                 :            :                 struct nlattr **attrs)
     675                 :            : {
     676                 :            :         struct net *net = sock_net(skb->sk);
     677                 :            :         struct xfrm_state *x;
     678                 :          0 :         int err = -ESRCH;
     679                 :            :         struct km_event c;
     680                 :          0 :         struct xfrm_usersa_id *p = nlmsg_data(nlh);
     681                 :          0 :         kuid_t loginuid = audit_get_loginuid(current);
     682                 :          0 :         u32 sessionid = audit_get_sessionid(current);
     683                 :            :         u32 sid;
     684                 :            : 
     685                 :          0 :         x = xfrm_user_state_lookup(net, p, attrs, &err);
     686         [ #  # ]:          0 :         if (x == NULL)
     687                 :          0 :                 return err;
     688                 :            : 
     689                 :          0 :         if ((err = security_xfrm_state_delete(x)) != 0)
     690                 :            :                 goto out;
     691                 :            : 
     692         [ #  # ]:          0 :         if (xfrm_state_kern(x)) {
     693                 :          0 :                 err = -EPERM;
     694                 :          0 :                 goto out;
     695                 :            :         }
     696                 :            : 
     697                 :          0 :         err = xfrm_state_delete(x);
     698                 :            : 
     699         [ #  # ]:          0 :         if (err < 0)
     700                 :            :                 goto out;
     701                 :            : 
     702                 :          0 :         c.seq = nlh->nlmsg_seq;
     703                 :          0 :         c.portid = nlh->nlmsg_pid;
     704                 :          0 :         c.event = nlh->nlmsg_type;
     705                 :          0 :         km_state_notify(x, &c);
     706                 :            : 
     707                 :            : out:
     708                 :          0 :         security_task_getsecid(current, &sid);
     709                 :          0 :         xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
     710                 :            :         xfrm_state_put(x);
     711                 :          0 :         return err;
     712                 :            : }
     713                 :            : 
     714                 :          0 : static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
     715                 :            : {
     716                 :          0 :         memset(p, 0, sizeof(*p));
     717                 :          0 :         memcpy(&p->id, &x->id, sizeof(p->id));
     718                 :          0 :         memcpy(&p->sel, &x->sel, sizeof(p->sel));
     719                 :          0 :         memcpy(&p->lft, &x->lft, sizeof(p->lft));
     720                 :          0 :         memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
     721                 :          0 :         memcpy(&p->stats, &x->stats, sizeof(p->stats));
     722                 :          0 :         memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
     723                 :          0 :         p->mode = x->props.mode;
     724                 :          0 :         p->replay_window = x->props.replay_window;
     725                 :          0 :         p->reqid = x->props.reqid;
     726                 :          0 :         p->family = x->props.family;
     727                 :          0 :         p->flags = x->props.flags;
     728                 :          0 :         p->seq = x->km.seq;
     729                 :          0 : }
     730                 :            : 
     731                 :            : struct xfrm_dump_info {
     732                 :            :         struct sk_buff *in_skb;
     733                 :            :         struct sk_buff *out_skb;
     734                 :            :         u32 nlmsg_seq;
     735                 :            :         u16 nlmsg_flags;
     736                 :            : };
     737                 :            : 
     738                 :          0 : static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
     739                 :            : {
     740                 :            :         struct xfrm_user_sec_ctx *uctx;
     741                 :            :         struct nlattr *attr;
     742                 :          0 :         int ctx_size = sizeof(*uctx) + s->ctx_len;
     743                 :            : 
     744                 :          0 :         attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
     745         [ #  # ]:          0 :         if (attr == NULL)
     746                 :            :                 return -EMSGSIZE;
     747                 :            : 
     748                 :            :         uctx = nla_data(attr);
     749                 :          0 :         uctx->exttype = XFRMA_SEC_CTX;
     750                 :          0 :         uctx->len = ctx_size;
     751                 :          0 :         uctx->ctx_doi = s->ctx_doi;
     752                 :          0 :         uctx->ctx_alg = s->ctx_alg;
     753                 :          0 :         uctx->ctx_len = s->ctx_len;
     754                 :          0 :         memcpy(uctx + 1, s->ctx_str, s->ctx_len);
     755                 :            : 
     756                 :          0 :         return 0;
     757                 :            : }
     758                 :            : 
     759                 :          0 : static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
     760                 :            : {
     761                 :            :         struct xfrm_algo *algo;
     762                 :            :         struct nlattr *nla;
     763                 :            : 
     764                 :          0 :         nla = nla_reserve(skb, XFRMA_ALG_AUTH,
     765                 :          0 :                           sizeof(*algo) + (auth->alg_key_len + 7) / 8);
     766         [ #  # ]:          0 :         if (!nla)
     767                 :            :                 return -EMSGSIZE;
     768                 :            : 
     769                 :            :         algo = nla_data(nla);
     770                 :          0 :         strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
     771                 :          0 :         memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
     772                 :          0 :         algo->alg_key_len = auth->alg_key_len;
     773                 :            : 
     774                 :          0 :         return 0;
     775                 :            : }
     776                 :            : 
     777                 :            : /* Don't change this without updating xfrm_sa_len! */
     778                 :          0 : static int copy_to_user_state_extra(struct xfrm_state *x,
     779                 :            :                                     struct xfrm_usersa_info *p,
     780                 :            :                                     struct sk_buff *skb)
     781                 :            : {
     782                 :            :         int ret = 0;
     783                 :            : 
     784                 :          0 :         copy_to_user_state(x, p);
     785                 :            : 
     786         [ #  # ]:          0 :         if (x->props.extra_flags) {
     787                 :            :                 ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
     788                 :            :                                   x->props.extra_flags);
     789         [ #  # ]:          0 :                 if (ret)
     790                 :            :                         goto out;
     791                 :            :         }
     792                 :            : 
     793         [ #  # ]:          0 :         if (x->coaddr) {
     794                 :          0 :                 ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
     795         [ #  # ]:          0 :                 if (ret)
     796                 :            :                         goto out;
     797                 :            :         }
     798         [ #  # ]:          0 :         if (x->lastused) {
     799                 :          0 :                 ret = nla_put_u64(skb, XFRMA_LASTUSED, x->lastused);
     800         [ #  # ]:          0 :                 if (ret)
     801                 :            :                         goto out;
     802                 :            :         }
     803         [ #  # ]:          0 :         if (x->aead) {
     804                 :          0 :                 ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
     805         [ #  # ]:          0 :                 if (ret)
     806                 :            :                         goto out;
     807                 :            :         }
     808         [ #  # ]:          0 :         if (x->aalg) {
     809                 :          0 :                 ret = copy_to_user_auth(x->aalg, skb);
     810         [ #  # ]:          0 :                 if (!ret)
     811                 :          0 :                         ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
     812                 :          0 :                                       xfrm_alg_auth_len(x->aalg), x->aalg);
     813         [ #  # ]:          0 :                 if (ret)
     814                 :            :                         goto out;
     815                 :            :         }
     816         [ #  # ]:          0 :         if (x->ealg) {
     817                 :          0 :                 ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
     818         [ #  # ]:          0 :                 if (ret)
     819                 :            :                         goto out;
     820                 :            :         }
     821         [ #  # ]:          0 :         if (x->calg) {
     822                 :          0 :                 ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
     823         [ #  # ]:          0 :                 if (ret)
     824                 :            :                         goto out;
     825                 :            :         }
     826         [ #  # ]:          0 :         if (x->encap) {
     827                 :          0 :                 ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
     828         [ #  # ]:          0 :                 if (ret)
     829                 :            :                         goto out;
     830                 :            :         }
     831         [ #  # ]:          0 :         if (x->tfcpad) {
     832                 :            :                 ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
     833         [ #  # ]:          0 :                 if (ret)
     834                 :            :                         goto out;
     835                 :            :         }
     836                 :          0 :         ret = xfrm_mark_put(skb, &x->mark);
     837         [ #  # ]:          0 :         if (ret)
     838                 :            :                 goto out;
     839         [ #  # ]:          0 :         if (x->replay_esn) {
     840                 :          0 :                 ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
     841                 :            :                               xfrm_replay_state_esn_len(x->replay_esn),
     842                 :            :                               x->replay_esn);
     843         [ #  # ]:          0 :                 if (ret)
     844                 :            :                         goto out;
     845                 :            :         }
     846         [ #  # ]:          0 :         if (x->security)
     847                 :          0 :                 ret = copy_sec_ctx(x->security, skb);
     848                 :            : out:
     849                 :          0 :         return ret;
     850                 :            : }
     851                 :            : 
     852                 :          0 : static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
     853                 :            : {
     854                 :            :         struct xfrm_dump_info *sp = ptr;
     855                 :          0 :         struct sk_buff *in_skb = sp->in_skb;
     856                 :          0 :         struct sk_buff *skb = sp->out_skb;
     857                 :            :         struct xfrm_usersa_info *p;
     858                 :            :         struct nlmsghdr *nlh;
     859                 :            :         int err;
     860                 :            : 
     861                 :          0 :         nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
     862                 :          0 :                         XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
     863         [ #  # ]:          0 :         if (nlh == NULL)
     864                 :            :                 return -EMSGSIZE;
     865                 :            : 
     866                 :          0 :         p = nlmsg_data(nlh);
     867                 :            : 
     868                 :          0 :         err = copy_to_user_state_extra(x, p, skb);
     869         [ #  # ]:          0 :         if (err) {
     870                 :            :                 nlmsg_cancel(skb, nlh);
     871                 :          0 :                 return err;
     872                 :            :         }
     873                 :            :         nlmsg_end(skb, nlh);
     874                 :          0 :         return 0;
     875                 :            : }
     876                 :            : 
     877                 :          0 : static int xfrm_dump_sa_done(struct netlink_callback *cb)
     878                 :            : {
     879                 :          0 :         struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
     880                 :          0 :         xfrm_state_walk_done(walk);
     881                 :          0 :         return 0;
     882                 :            : }
     883                 :            : 
     884                 :          0 : static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
     885                 :            : {
     886                 :            :         struct net *net = sock_net(skb->sk);
     887                 :          0 :         struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
     888                 :            :         struct xfrm_dump_info info;
     889                 :            : 
     890                 :            :         BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
     891                 :            :                      sizeof(cb->args) - sizeof(cb->args[0]));
     892                 :            : 
     893                 :          0 :         info.in_skb = cb->skb;
     894                 :          0 :         info.out_skb = skb;
     895                 :          0 :         info.nlmsg_seq = cb->nlh->nlmsg_seq;
     896                 :          0 :         info.nlmsg_flags = NLM_F_MULTI;
     897                 :            : 
     898         [ #  # ]:          0 :         if (!cb->args[0]) {
     899                 :          0 :                 cb->args[0] = 1;
     900                 :          0 :                 xfrm_state_walk_init(walk, 0);
     901                 :            :         }
     902                 :            : 
     903                 :          0 :         (void) xfrm_state_walk(net, walk, dump_one_state, &info);
     904                 :            : 
     905                 :          0 :         return skb->len;
     906                 :            : }
     907                 :            : 
     908                 :          0 : static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
     909                 :            :                                           struct xfrm_state *x, u32 seq)
     910                 :            : {
     911                 :            :         struct xfrm_dump_info info;
     912                 :            :         struct sk_buff *skb;
     913                 :            :         int err;
     914                 :            : 
     915                 :            :         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
     916         [ #  # ]:          0 :         if (!skb)
     917                 :            :                 return ERR_PTR(-ENOMEM);
     918                 :            : 
     919                 :          0 :         info.in_skb = in_skb;
     920                 :          0 :         info.out_skb = skb;
     921                 :          0 :         info.nlmsg_seq = seq;
     922                 :          0 :         info.nlmsg_flags = 0;
     923                 :            : 
     924                 :          0 :         err = dump_one_state(x, 0, &info);
     925         [ #  # ]:          0 :         if (err) {
     926                 :          0 :                 kfree_skb(skb);
     927                 :          0 :                 return ERR_PTR(err);
     928                 :            :         }
     929                 :            : 
     930                 :            :         return skb;
     931                 :            : }
     932                 :            : 
     933                 :            : static inline size_t xfrm_spdinfo_msgsize(void)
     934                 :            : {
     935                 :            :         return NLMSG_ALIGN(4)
     936                 :            :                + nla_total_size(sizeof(struct xfrmu_spdinfo))
     937                 :            :                + nla_total_size(sizeof(struct xfrmu_spdhinfo));
     938                 :            : }
     939                 :            : 
     940                 :          0 : static int build_spdinfo(struct sk_buff *skb, struct net *net,
     941                 :            :                          u32 portid, u32 seq, u32 flags)
     942                 :            : {
     943                 :            :         struct xfrmk_spdinfo si;
     944                 :            :         struct xfrmu_spdinfo spc;
     945                 :            :         struct xfrmu_spdhinfo sph;
     946                 :            :         struct nlmsghdr *nlh;
     947                 :            :         int err;
     948                 :            :         u32 *f;
     949                 :            : 
     950                 :            :         nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
     951         [ #  # ]:          0 :         if (nlh == NULL) /* shouldn't really happen ... */
     952                 :            :                 return -EMSGSIZE;
     953                 :            : 
     954                 :            :         f = nlmsg_data(nlh);
     955                 :          0 :         *f = flags;
     956                 :          0 :         xfrm_spd_getinfo(net, &si);
     957                 :          0 :         spc.incnt = si.incnt;
     958                 :          0 :         spc.outcnt = si.outcnt;
     959                 :          0 :         spc.fwdcnt = si.fwdcnt;
     960                 :          0 :         spc.inscnt = si.inscnt;
     961                 :          0 :         spc.outscnt = si.outscnt;
     962                 :          0 :         spc.fwdscnt = si.fwdscnt;
     963                 :          0 :         sph.spdhcnt = si.spdhcnt;
     964                 :          0 :         sph.spdhmcnt = si.spdhmcnt;
     965                 :            : 
     966                 :          0 :         err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
     967         [ #  # ]:          0 :         if (!err)
     968                 :          0 :                 err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
     969         [ #  # ]:          0 :         if (err) {
     970                 :            :                 nlmsg_cancel(skb, nlh);
     971                 :          0 :                 return err;
     972                 :            :         }
     973                 :            : 
     974                 :          0 :         return nlmsg_end(skb, nlh);
     975                 :            : }
     976                 :            : 
     977                 :          0 : static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
     978                 :            :                 struct nlattr **attrs)
     979                 :            : {
     980                 :            :         struct net *net = sock_net(skb->sk);
     981                 :            :         struct sk_buff *r_skb;
     982                 :            :         u32 *flags = nlmsg_data(nlh);
     983                 :          0 :         u32 sportid = NETLINK_CB(skb).portid;
     984                 :          0 :         u32 seq = nlh->nlmsg_seq;
     985                 :            : 
     986                 :            :         r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
     987         [ #  # ]:          0 :         if (r_skb == NULL)
     988                 :            :                 return -ENOMEM;
     989                 :            : 
     990         [ #  # ]:          0 :         if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
     991                 :          0 :                 BUG();
     992                 :            : 
     993                 :          0 :         return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
     994                 :            : }
     995                 :            : 
     996                 :            : static inline size_t xfrm_sadinfo_msgsize(void)
     997                 :            : {
     998                 :            :         return NLMSG_ALIGN(4)
     999                 :            :                + nla_total_size(sizeof(struct xfrmu_sadhinfo))
    1000                 :            :                + nla_total_size(4); /* XFRMA_SAD_CNT */
    1001                 :            : }
    1002                 :            : 
    1003                 :          0 : static int build_sadinfo(struct sk_buff *skb, struct net *net,
    1004                 :            :                          u32 portid, u32 seq, u32 flags)
    1005                 :            : {
    1006                 :            :         struct xfrmk_sadinfo si;
    1007                 :            :         struct xfrmu_sadhinfo sh;
    1008                 :            :         struct nlmsghdr *nlh;
    1009                 :            :         int err;
    1010                 :            :         u32 *f;
    1011                 :            : 
    1012                 :            :         nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
    1013         [ #  # ]:          0 :         if (nlh == NULL) /* shouldn't really happen ... */
    1014                 :            :                 return -EMSGSIZE;
    1015                 :            : 
    1016                 :            :         f = nlmsg_data(nlh);
    1017                 :          0 :         *f = flags;
    1018                 :          0 :         xfrm_sad_getinfo(net, &si);
    1019                 :            : 
    1020                 :          0 :         sh.sadhmcnt = si.sadhmcnt;
    1021                 :          0 :         sh.sadhcnt = si.sadhcnt;
    1022                 :            : 
    1023                 :          0 :         err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
    1024         [ #  # ]:          0 :         if (!err)
    1025                 :          0 :                 err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
    1026         [ #  # ]:          0 :         if (err) {
    1027                 :            :                 nlmsg_cancel(skb, nlh);
    1028                 :          0 :                 return err;
    1029                 :            :         }
    1030                 :            : 
    1031                 :          0 :         return nlmsg_end(skb, nlh);
    1032                 :            : }
    1033                 :            : 
    1034                 :          0 : static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
    1035                 :            :                 struct nlattr **attrs)
    1036                 :            : {
    1037                 :            :         struct net *net = sock_net(skb->sk);
    1038                 :            :         struct sk_buff *r_skb;
    1039                 :            :         u32 *flags = nlmsg_data(nlh);
    1040                 :          0 :         u32 sportid = NETLINK_CB(skb).portid;
    1041                 :          0 :         u32 seq = nlh->nlmsg_seq;
    1042                 :            : 
    1043                 :            :         r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
    1044         [ #  # ]:          0 :         if (r_skb == NULL)
    1045                 :            :                 return -ENOMEM;
    1046                 :            : 
    1047         [ #  # ]:          0 :         if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
    1048                 :          0 :                 BUG();
    1049                 :            : 
    1050                 :          0 :         return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
    1051                 :            : }
    1052                 :            : 
    1053                 :          0 : static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
    1054                 :            :                 struct nlattr **attrs)
    1055                 :            : {
    1056                 :            :         struct net *net = sock_net(skb->sk);
    1057                 :          0 :         struct xfrm_usersa_id *p = nlmsg_data(nlh);
    1058                 :            :         struct xfrm_state *x;
    1059                 :            :         struct sk_buff *resp_skb;
    1060                 :          0 :         int err = -ESRCH;
    1061                 :            : 
    1062                 :          0 :         x = xfrm_user_state_lookup(net, p, attrs, &err);
    1063         [ #  # ]:          0 :         if (x == NULL)
    1064                 :            :                 goto out_noput;
    1065                 :            : 
    1066                 :          0 :         resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
    1067         [ #  # ]:          0 :         if (IS_ERR(resp_skb)) {
    1068                 :          0 :                 err = PTR_ERR(resp_skb);
    1069                 :            :         } else {
    1070                 :          0 :                 err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
    1071                 :            :         }
    1072                 :            :         xfrm_state_put(x);
    1073                 :            : out_noput:
    1074                 :          0 :         return err;
    1075                 :            : }
    1076                 :            : 
    1077                 :            : static int verify_userspi_info(struct xfrm_userspi_info *p)
    1078                 :            : {
    1079      [ #  #  # ]:          0 :         switch (p->info.id.proto) {
    1080                 :            :         case IPPROTO_AH:
    1081                 :            :         case IPPROTO_ESP:
    1082                 :            :                 break;
    1083                 :            : 
    1084                 :            :         case IPPROTO_COMP:
    1085                 :            :                 /* IPCOMP spi is 16-bits. */
    1086         [ #  # ]:          0 :                 if (p->max >= 0x10000)
    1087                 :            :                         return -EINVAL;
    1088                 :            :                 break;
    1089                 :            : 
    1090                 :            :         default:
    1091                 :            :                 return -EINVAL;
    1092                 :            :         }
    1093                 :            : 
    1094         [ #  # ]:          0 :         if (p->min > p->max)
    1095                 :            :                 return -EINVAL;
    1096                 :            : 
    1097                 :            :         return 0;
    1098                 :            : }
    1099                 :            : 
    1100                 :          0 : static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
    1101                 :            :                 struct nlattr **attrs)
    1102                 :            : {
    1103                 :            :         struct net *net = sock_net(skb->sk);
    1104                 :            :         struct xfrm_state *x;
    1105                 :            :         struct xfrm_userspi_info *p;
    1106                 :            :         struct sk_buff *resp_skb;
    1107                 :            :         xfrm_address_t *daddr;
    1108                 :            :         int family;
    1109                 :            :         int err;
    1110                 :            :         u32 mark;
    1111                 :            :         struct xfrm_mark m;
    1112                 :            : 
    1113                 :            :         p = nlmsg_data(nlh);
    1114                 :            :         err = verify_userspi_info(p);
    1115         [ #  # ]:          0 :         if (err)
    1116                 :            :                 goto out_noput;
    1117                 :            : 
    1118                 :          0 :         family = p->info.family;
    1119                 :          0 :         daddr = &p->info.id.daddr;
    1120                 :            : 
    1121                 :            :         x = NULL;
    1122                 :            : 
    1123                 :            :         mark = xfrm_mark_get(attrs, &m);
    1124         [ #  # ]:          0 :         if (p->info.seq) {
    1125                 :          0 :                 x = xfrm_find_acq_byseq(net, mark, p->info.seq);
    1126 [ #  # ][ #  # ]:          0 :                 if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
    1127                 :            :                         xfrm_state_put(x);
    1128                 :            :                         x = NULL;
    1129                 :            :                 }
    1130                 :            :         }
    1131                 :            : 
    1132         [ #  # ]:          0 :         if (!x)
    1133                 :          0 :                 x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
    1134                 :            :                                   p->info.id.proto, daddr,
    1135                 :          0 :                                   &p->info.saddr, 1,
    1136                 :            :                                   family);
    1137                 :            :         err = -ENOENT;
    1138         [ #  # ]:          0 :         if (x == NULL)
    1139                 :            :                 goto out_noput;
    1140                 :            : 
    1141                 :          0 :         err = xfrm_alloc_spi(x, p->min, p->max);
    1142         [ #  # ]:          0 :         if (err)
    1143                 :            :                 goto out;
    1144                 :            : 
    1145                 :          0 :         resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
    1146         [ #  # ]:          0 :         if (IS_ERR(resp_skb)) {
    1147                 :            :                 err = PTR_ERR(resp_skb);
    1148                 :          0 :                 goto out;
    1149                 :            :         }
    1150                 :            : 
    1151                 :          0 :         err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
    1152                 :            : 
    1153                 :            : out:
    1154                 :            :         xfrm_state_put(x);
    1155                 :            : out_noput:
    1156                 :          0 :         return err;
    1157                 :            : }
    1158                 :            : 
    1159                 :            : static int verify_policy_dir(u8 dir)
    1160                 :            : {
    1161 [ #  # ][ #  # ]:          0 :         switch (dir) {
                 [ #  # ]
    1162                 :            :         case XFRM_POLICY_IN:
    1163                 :            :         case XFRM_POLICY_OUT:
    1164                 :            :         case XFRM_POLICY_FWD:
    1165                 :            :                 break;
    1166                 :            : 
    1167                 :            :         default:
    1168                 :            :                 return -EINVAL;
    1169                 :            :         }
    1170                 :            : 
    1171                 :            :         return 0;
    1172                 :            : }
    1173                 :            : 
    1174                 :            : static int verify_policy_type(u8 type)
    1175                 :            : {
    1176 [ #  # ][ #  # ]:          0 :         switch (type) {
         [ #  # ][ #  # ]
                 [ #  # ]
    1177                 :            :         case XFRM_POLICY_TYPE_MAIN:
    1178                 :            : #ifdef CONFIG_XFRM_SUB_POLICY
    1179                 :            :         case XFRM_POLICY_TYPE_SUB:
    1180                 :            : #endif
    1181                 :            :                 break;
    1182                 :            : 
    1183                 :            :         default:
    1184                 :            :                 return -EINVAL;
    1185                 :            :         }
    1186                 :            : 
    1187                 :            :         return 0;
    1188                 :            : }
    1189                 :            : 
    1190                 :          0 : static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
    1191                 :            : {
    1192         [ #  # ]:          0 :         switch (p->share) {
    1193                 :            :         case XFRM_SHARE_ANY:
    1194                 :            :         case XFRM_SHARE_SESSION:
    1195                 :            :         case XFRM_SHARE_USER:
    1196                 :            :         case XFRM_SHARE_UNIQUE:
    1197                 :            :                 break;
    1198                 :            : 
    1199                 :            :         default:
    1200                 :            :                 return -EINVAL;
    1201                 :            :         }
    1202                 :            : 
    1203         [ #  # ]:          0 :         switch (p->action) {
    1204                 :            :         case XFRM_POLICY_ALLOW:
    1205                 :            :         case XFRM_POLICY_BLOCK:
    1206                 :            :                 break;
    1207                 :            : 
    1208                 :            :         default:
    1209                 :            :                 return -EINVAL;
    1210                 :            :         }
    1211                 :            : 
    1212         [ #  # ]:          0 :         switch (p->sel.family) {
    1213                 :            :         case AF_INET:
    1214                 :            :                 break;
    1215                 :            : 
    1216                 :            :         case AF_INET6:
    1217                 :            : #if IS_ENABLED(CONFIG_IPV6)
    1218                 :            :                 break;
    1219                 :            : #else
    1220                 :            :                 return  -EAFNOSUPPORT;
    1221                 :            : #endif
    1222                 :            : 
    1223                 :            :         default:
    1224                 :            :                 return -EINVAL;
    1225                 :            :         }
    1226                 :            : 
    1227                 :          0 :         return verify_policy_dir(p->dir);
    1228                 :            : }
    1229                 :            : 
    1230                 :            : static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
    1231                 :            : {
    1232                 :            :         struct nlattr *rt = attrs[XFRMA_SEC_CTX];
    1233                 :            :         struct xfrm_user_sec_ctx *uctx;
    1234                 :            : 
    1235                 :            :         if (!rt)
    1236                 :            :                 return 0;
    1237                 :            : 
    1238                 :            :         uctx = nla_data(rt);
    1239                 :            :         return security_xfrm_policy_alloc(&pol->security, uctx);
    1240                 :            : }
    1241                 :            : 
    1242                 :          0 : static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
    1243                 :            :                            int nr)
    1244                 :            : {
    1245                 :            :         int i;
    1246                 :            : 
    1247                 :          0 :         xp->xfrm_nr = nr;
    1248         [ #  # ]:          0 :         for (i = 0; i < nr; i++, ut++) {
    1249                 :          0 :                 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
    1250                 :            : 
    1251                 :          0 :                 memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
    1252                 :          0 :                 memcpy(&t->saddr, &ut->saddr,
    1253                 :            :                        sizeof(xfrm_address_t));
    1254                 :          0 :                 t->reqid = ut->reqid;
    1255                 :          0 :                 t->mode = ut->mode;
    1256                 :          0 :                 t->share = ut->share;
    1257                 :          0 :                 t->optional = ut->optional;
    1258                 :          0 :                 t->aalgos = ut->aalgos;
    1259                 :          0 :                 t->ealgos = ut->ealgos;
    1260                 :          0 :                 t->calgos = ut->calgos;
    1261                 :            :                 /* If all masks are ~0, then we allow all algorithms. */
    1262                 :          0 :                 t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
    1263                 :          0 :                 t->encap_family = ut->family;
    1264                 :            :         }
    1265                 :          0 : }
    1266                 :            : 
    1267                 :          0 : static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
    1268                 :            : {
    1269                 :            :         int i;
    1270                 :            : 
    1271         [ #  # ]:          0 :         if (nr > XFRM_MAX_DEPTH)
    1272                 :            :                 return -EINVAL;
    1273                 :            : 
    1274         [ #  # ]:          0 :         for (i = 0; i < nr; i++) {
    1275                 :            :                 /* We never validated the ut->family value, so many
    1276                 :            :                  * applications simply leave it at zero.  The check was
    1277                 :            :                  * never made and ut->family was ignored because all
    1278                 :            :                  * templates could be assumed to have the same family as
    1279                 :            :                  * the policy itself.  Now that we will have ipv4-in-ipv6
    1280                 :            :                  * and ipv6-in-ipv4 tunnels, this is no longer true.
    1281                 :            :                  */
    1282         [ #  # ]:          0 :                 if (!ut[i].family)
    1283                 :          0 :                         ut[i].family = family;
    1284                 :            : 
    1285         [ #  # ]:          0 :                 switch (ut[i].family) {
    1286                 :            :                 case AF_INET:
    1287                 :            :                         break;
    1288                 :            : #if IS_ENABLED(CONFIG_IPV6)
    1289                 :            :                 case AF_INET6:
    1290                 :            :                         break;
    1291                 :            : #endif
    1292                 :            :                 default:
    1293                 :            :                         return -EINVAL;
    1294                 :            :                 }
    1295                 :            :         }
    1296                 :            : 
    1297                 :            :         return 0;
    1298                 :            : }
    1299                 :            : 
    1300                 :          0 : static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
    1301                 :            : {
    1302                 :          0 :         struct nlattr *rt = attrs[XFRMA_TMPL];
    1303                 :            : 
    1304         [ #  # ]:          0 :         if (!rt) {
    1305                 :          0 :                 pol->xfrm_nr = 0;
    1306                 :            :         } else {
    1307                 :          0 :                 struct xfrm_user_tmpl *utmpl = nla_data(rt);
    1308                 :          0 :                 int nr = nla_len(rt) / sizeof(*utmpl);
    1309                 :            :                 int err;
    1310                 :            : 
    1311                 :          0 :                 err = validate_tmpl(nr, utmpl, pol->family);
    1312         [ #  # ]:          0 :                 if (err)
    1313                 :            :                         return err;
    1314                 :            : 
    1315                 :          0 :                 copy_templates(pol, utmpl, nr);
    1316                 :            :         }
    1317                 :            :         return 0;
    1318                 :            : }
    1319                 :            : 
    1320                 :            : static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
    1321                 :            : {
    1322                 :          0 :         struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
    1323                 :            :         struct xfrm_userpolicy_type *upt;
    1324                 :            :         u8 type = XFRM_POLICY_TYPE_MAIN;
    1325                 :            :         int err;
    1326                 :            : 
    1327 [ #  # ][ #  # ]:          0 :         if (rt) {
                 [ #  # ]
           [ #  #  #  # ]
    1328                 :            :                 upt = nla_data(rt);
    1329                 :          0 :                 type = upt->type;
    1330                 :            :         }
    1331                 :            : 
    1332                 :            :         err = verify_policy_type(type);
    1333 [ #  # ][ #  # ]:          0 :         if (err)
         [ #  # ][ #  # ]
                 [ #  # ]
    1334                 :            :                 return err;
    1335                 :            : 
    1336                 :          0 :         *tp = type;
    1337                 :            :         return 0;
    1338                 :            : }
    1339                 :            : 
    1340                 :          0 : static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
    1341                 :            : {
    1342                 :          0 :         xp->priority = p->priority;
    1343                 :          0 :         xp->index = p->index;
    1344                 :          0 :         memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
    1345                 :          0 :         memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
    1346                 :          0 :         xp->action = p->action;
    1347                 :          0 :         xp->flags = p->flags;
    1348                 :          0 :         xp->family = p->sel.family;
    1349                 :            :         /* XXX xp->share = p->share; */
    1350                 :          0 : }
    1351                 :            : 
    1352                 :          0 : static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
    1353                 :            : {
    1354                 :          0 :         memset(p, 0, sizeof(*p));
    1355                 :          0 :         memcpy(&p->sel, &xp->selector, sizeof(p->sel));
    1356                 :          0 :         memcpy(&p->lft, &xp->lft, sizeof(p->lft));
    1357                 :          0 :         memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
    1358                 :          0 :         p->priority = xp->priority;
    1359                 :          0 :         p->index = xp->index;
    1360                 :          0 :         p->sel.family = xp->family;
    1361                 :          0 :         p->dir = dir;
    1362                 :          0 :         p->action = xp->action;
    1363                 :          0 :         p->flags = xp->flags;
    1364                 :          0 :         p->share = XFRM_SHARE_ANY; /* XXX xp->share */
    1365                 :          0 : }
    1366                 :            : 
    1367                 :          0 : static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
    1368                 :            : {
    1369                 :          0 :         struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
    1370                 :            :         int err;
    1371                 :            : 
    1372         [ #  # ]:          0 :         if (!xp) {
    1373                 :          0 :                 *errp = -ENOMEM;
    1374                 :          0 :                 return NULL;
    1375                 :            :         }
    1376                 :            : 
    1377                 :          0 :         copy_from_user_policy(xp, p);
    1378                 :            : 
    1379                 :            :         err = copy_from_user_policy_type(&xp->type, attrs);
    1380         [ #  # ]:          0 :         if (err)
    1381                 :            :                 goto error;
    1382                 :            : 
    1383         [ #  # ]:          0 :         if (!(err = copy_from_user_tmpl(xp, attrs)))
    1384                 :            :                 err = copy_from_user_sec_ctx(xp, attrs);
    1385         [ #  # ]:          0 :         if (err)
    1386                 :            :                 goto error;
    1387                 :            : 
    1388                 :          0 :         xfrm_mark_get(attrs, &xp->mark);
    1389                 :            : 
    1390                 :          0 :         return xp;
    1391                 :            :  error:
    1392                 :          0 :         *errp = err;
    1393                 :          0 :         xp->walk.dead = 1;
    1394                 :          0 :         xfrm_policy_destroy(xp);
    1395                 :          0 :         return NULL;
    1396                 :            : }
    1397                 :            : 
    1398                 :          0 : static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
    1399                 :            :                 struct nlattr **attrs)
    1400                 :            : {
    1401                 :            :         struct net *net = sock_net(skb->sk);
    1402                 :          0 :         struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
    1403                 :            :         struct xfrm_policy *xp;
    1404                 :            :         struct km_event c;
    1405                 :            :         int err;
    1406                 :            :         int excl;
    1407                 :          0 :         kuid_t loginuid = audit_get_loginuid(current);
    1408                 :          0 :         u32 sessionid = audit_get_sessionid(current);
    1409                 :            :         u32 sid;
    1410                 :            : 
    1411                 :          0 :         err = verify_newpolicy_info(p);
    1412         [ #  # ]:          0 :         if (err)
    1413                 :            :                 return err;
    1414                 :          0 :         err = verify_sec_ctx_len(attrs);
    1415         [ #  # ]:          0 :         if (err)
    1416                 :            :                 return err;
    1417                 :            : 
    1418                 :          0 :         xp = xfrm_policy_construct(net, p, attrs, &err);
    1419         [ #  # ]:          0 :         if (!xp)
    1420                 :          0 :                 return err;
    1421                 :            : 
    1422                 :            :         /* shouldn't excl be based on nlh flags??
    1423                 :            :          * Aha! this is anti-netlink really i.e  more pfkey derived
    1424                 :            :          * in netlink excl is a flag and you wouldnt need
    1425                 :            :          * a type XFRM_MSG_UPDPOLICY - JHS */
    1426                 :          0 :         excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
    1427                 :          0 :         err = xfrm_policy_insert(p->dir, xp, excl);
    1428                 :          0 :         security_task_getsecid(current, &sid);
    1429                 :          0 :         xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
    1430                 :            : 
    1431         [ #  # ]:          0 :         if (err) {
    1432                 :            :                 security_xfrm_policy_free(xp->security);
    1433                 :          0 :                 kfree(xp);
    1434                 :          0 :                 return err;
    1435                 :            :         }
    1436                 :            : 
    1437                 :          0 :         c.event = nlh->nlmsg_type;
    1438                 :          0 :         c.seq = nlh->nlmsg_seq;
    1439                 :          0 :         c.portid = nlh->nlmsg_pid;
    1440                 :          0 :         km_policy_notify(xp, p->dir, &c);
    1441                 :            : 
    1442                 :            :         xfrm_pol_put(xp);
    1443                 :            : 
    1444                 :            :         return 0;
    1445                 :            : }
    1446                 :            : 
    1447                 :          0 : static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
    1448                 :            : {
    1449                 :            :         struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
    1450                 :            :         int i;
    1451                 :            : 
    1452         [ #  # ]:          0 :         if (xp->xfrm_nr == 0)
    1453                 :            :                 return 0;
    1454                 :            : 
    1455         [ #  # ]:          0 :         for (i = 0; i < xp->xfrm_nr; i++) {
    1456                 :          0 :                 struct xfrm_user_tmpl *up = &vec[i];
    1457                 :          0 :                 struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
    1458                 :            : 
    1459                 :          0 :                 memset(up, 0, sizeof(*up));
    1460                 :          0 :                 memcpy(&up->id, &kp->id, sizeof(up->id));
    1461                 :          0 :                 up->family = kp->encap_family;
    1462                 :          0 :                 memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
    1463                 :          0 :                 up->reqid = kp->reqid;
    1464                 :          0 :                 up->mode = kp->mode;
    1465                 :          0 :                 up->share = kp->share;
    1466                 :          0 :                 up->optional = kp->optional;
    1467                 :          0 :                 up->aalgos = kp->aalgos;
    1468                 :          0 :                 up->ealgos = kp->ealgos;
    1469                 :          0 :                 up->calgos = kp->calgos;
    1470                 :            :         }
    1471                 :            : 
    1472                 :          0 :         return nla_put(skb, XFRMA_TMPL,
    1473                 :          0 :                        sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
    1474                 :            : }
    1475                 :            : 
    1476                 :            : static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
    1477                 :            : {
    1478         [ #  # ]:          0 :         if (x->security) {
    1479                 :          0 :                 return copy_sec_ctx(x->security, skb);
    1480                 :            :         }
    1481                 :            :         return 0;
    1482                 :            : }
    1483                 :            : 
    1484                 :            : static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
    1485                 :            : {
    1486 [ #  # ][ #  # ]:          0 :         if (xp->security)
    1487                 :          0 :                 return copy_sec_ctx(xp->security, skb);
    1488                 :            :         return 0;
    1489                 :            : }
    1490                 :            : static inline size_t userpolicy_type_attrsize(void)
    1491                 :            : {
    1492                 :            : #ifdef CONFIG_XFRM_SUB_POLICY
    1493                 :            :         return nla_total_size(sizeof(struct xfrm_userpolicy_type));
    1494                 :            : #else
    1495                 :            :         return 0;
    1496                 :            : #endif
    1497                 :            : }
    1498                 :            : 
    1499                 :            : #ifdef CONFIG_XFRM_SUB_POLICY
    1500                 :            : static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
    1501                 :            : {
    1502                 :            :         struct xfrm_userpolicy_type upt = {
    1503                 :            :                 .type = type,
    1504                 :            :         };
    1505                 :            : 
    1506                 :            :         return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
    1507                 :            : }
    1508                 :            : 
    1509                 :            : #else
    1510                 :            : static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
    1511                 :            : {
    1512                 :            :         return 0;
    1513                 :            : }
    1514                 :            : #endif
    1515                 :            : 
    1516                 :          0 : static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
    1517                 :            : {
    1518                 :            :         struct xfrm_dump_info *sp = ptr;
    1519                 :            :         struct xfrm_userpolicy_info *p;
    1520                 :          0 :         struct sk_buff *in_skb = sp->in_skb;
    1521                 :          0 :         struct sk_buff *skb = sp->out_skb;
    1522                 :            :         struct nlmsghdr *nlh;
    1523                 :            :         int err;
    1524                 :            : 
    1525                 :          0 :         nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
    1526                 :          0 :                         XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
    1527         [ #  # ]:          0 :         if (nlh == NULL)
    1528                 :            :                 return -EMSGSIZE;
    1529                 :            : 
    1530                 :          0 :         p = nlmsg_data(nlh);
    1531                 :          0 :         copy_to_user_policy(xp, p, dir);
    1532                 :          0 :         err = copy_to_user_tmpl(xp, skb);
    1533         [ #  # ]:          0 :         if (!err)
    1534                 :            :                 err = copy_to_user_sec_ctx(xp, skb);
    1535         [ #  # ]:          0 :         if (!err)
    1536                 :            :                 err = copy_to_user_policy_type(xp->type, skb);
    1537         [ #  # ]:          0 :         if (!err)
    1538                 :          0 :                 err = xfrm_mark_put(skb, &xp->mark);
    1539         [ #  # ]:          0 :         if (err) {
    1540                 :            :                 nlmsg_cancel(skb, nlh);
    1541                 :          0 :                 return err;
    1542                 :            :         }
    1543                 :            :         nlmsg_end(skb, nlh);
    1544                 :          0 :         return 0;
    1545                 :            : }
    1546                 :            : 
    1547                 :          0 : static int xfrm_dump_policy_done(struct netlink_callback *cb)
    1548                 :            : {
    1549                 :          0 :         struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
    1550                 :            : 
    1551                 :          0 :         xfrm_policy_walk_done(walk);
    1552                 :          0 :         return 0;
    1553                 :            : }
    1554                 :            : 
    1555                 :          0 : static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
    1556                 :            : {
    1557                 :            :         struct net *net = sock_net(skb->sk);
    1558                 :          0 :         struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
    1559                 :            :         struct xfrm_dump_info info;
    1560                 :            : 
    1561                 :            :         BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
    1562                 :            :                      sizeof(cb->args) - sizeof(cb->args[0]));
    1563                 :            : 
    1564                 :          0 :         info.in_skb = cb->skb;
    1565                 :          0 :         info.out_skb = skb;
    1566                 :          0 :         info.nlmsg_seq = cb->nlh->nlmsg_seq;
    1567                 :          0 :         info.nlmsg_flags = NLM_F_MULTI;
    1568                 :            : 
    1569         [ #  # ]:          0 :         if (!cb->args[0]) {
    1570                 :          0 :                 cb->args[0] = 1;
    1571                 :          0 :                 xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
    1572                 :            :         }
    1573                 :            : 
    1574                 :          0 :         (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
    1575                 :            : 
    1576                 :          0 :         return skb->len;
    1577                 :            : }
    1578                 :            : 
    1579                 :          0 : static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
    1580                 :            :                                           struct xfrm_policy *xp,
    1581                 :            :                                           int dir, u32 seq)
    1582                 :            : {
    1583                 :            :         struct xfrm_dump_info info;
    1584                 :            :         struct sk_buff *skb;
    1585                 :            :         int err;
    1586                 :            : 
    1587                 :            :         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
    1588         [ #  # ]:          0 :         if (!skb)
    1589                 :            :                 return ERR_PTR(-ENOMEM);
    1590                 :            : 
    1591                 :          0 :         info.in_skb = in_skb;
    1592                 :          0 :         info.out_skb = skb;
    1593                 :          0 :         info.nlmsg_seq = seq;
    1594                 :          0 :         info.nlmsg_flags = 0;
    1595                 :            : 
    1596                 :          0 :         err = dump_one_policy(xp, dir, 0, &info);
    1597         [ #  # ]:          0 :         if (err) {
    1598                 :          0 :                 kfree_skb(skb);
    1599                 :          0 :                 return ERR_PTR(err);
    1600                 :            :         }
    1601                 :            : 
    1602                 :            :         return skb;
    1603                 :            : }
    1604                 :            : 
    1605                 :          0 : static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
    1606                 :            :                 struct nlattr **attrs)
    1607                 :            : {
    1608                 :            :         struct net *net = sock_net(skb->sk);
    1609                 :            :         struct xfrm_policy *xp;
    1610                 :            :         struct xfrm_userpolicy_id *p;
    1611                 :            :         u8 type = XFRM_POLICY_TYPE_MAIN;
    1612                 :            :         int err;
    1613                 :            :         struct km_event c;
    1614                 :            :         int delete;
    1615                 :            :         struct xfrm_mark m;
    1616                 :            :         u32 mark = xfrm_mark_get(attrs, &m);
    1617                 :            : 
    1618                 :            :         p = nlmsg_data(nlh);
    1619                 :          0 :         delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
    1620                 :            : 
    1621                 :          0 :         err = copy_from_user_policy_type(&type, attrs);
    1622         [ #  # ]:          0 :         if (err)
    1623                 :            :                 return err;
    1624                 :            : 
    1625                 :          0 :         err = verify_policy_dir(p->dir);
    1626         [ #  # ]:          0 :         if (err)
    1627                 :            :                 return err;
    1628                 :            : 
    1629         [ #  # ]:          0 :         if (p->index)
    1630                 :          0 :                 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
    1631                 :            :         else {
    1632                 :          0 :                 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
    1633                 :            :                 struct xfrm_sec_ctx *ctx;
    1634                 :            : 
    1635                 :          0 :                 err = verify_sec_ctx_len(attrs);
    1636         [ #  # ]:          0 :                 if (err)
    1637                 :            :                         return err;
    1638                 :            : 
    1639                 :            :                 ctx = NULL;
    1640         [ #  # ]:          0 :                 if (rt) {
    1641                 :            :                         struct xfrm_user_sec_ctx *uctx = nla_data(rt);
    1642                 :            : 
    1643                 :          0 :                         err = security_xfrm_policy_alloc(&ctx, uctx);
    1644                 :            :                         if (err)
    1645                 :            :                                 return err;
    1646                 :            :                 }
    1647                 :          0 :                 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
    1648                 :            :                                            ctx, delete, &err);
    1649                 :            :                 security_xfrm_policy_free(ctx);
    1650                 :            :         }
    1651         [ #  # ]:          0 :         if (xp == NULL)
    1652                 :            :                 return -ENOENT;
    1653                 :            : 
    1654         [ #  # ]:          0 :         if (!delete) {
    1655                 :            :                 struct sk_buff *resp_skb;
    1656                 :            : 
    1657                 :          0 :                 resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
    1658         [ #  # ]:          0 :                 if (IS_ERR(resp_skb)) {
    1659                 :          0 :                         err = PTR_ERR(resp_skb);
    1660                 :            :                 } else {
    1661                 :          0 :                         err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
    1662                 :            :                                             NETLINK_CB(skb).portid);
    1663                 :            :                 }
    1664                 :            :         } else {
    1665                 :          0 :                 kuid_t loginuid = audit_get_loginuid(current);
    1666                 :          0 :                 u32 sessionid = audit_get_sessionid(current);
    1667                 :            :                 u32 sid;
    1668                 :            : 
    1669                 :          0 :                 security_task_getsecid(current, &sid);
    1670                 :          0 :                 xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
    1671                 :            :                                          sid);
    1672                 :            : 
    1673         [ #  # ]:          0 :                 if (err != 0)
    1674                 :            :                         goto out;
    1675                 :            : 
    1676                 :          0 :                 c.data.byid = p->index;
    1677                 :          0 :                 c.event = nlh->nlmsg_type;
    1678                 :          0 :                 c.seq = nlh->nlmsg_seq;
    1679                 :          0 :                 c.portid = nlh->nlmsg_pid;
    1680                 :          0 :                 km_policy_notify(xp, p->dir, &c);
    1681                 :            :         }
    1682                 :            : 
    1683                 :            : out:
    1684                 :            :         xfrm_pol_put(xp);
    1685 [ #  # ][ #  # ]:          0 :         if (delete && err == 0)
    1686                 :          0 :                 xfrm_garbage_collect(net);
    1687                 :          0 :         return err;
    1688                 :            : }
    1689                 :            : 
    1690                 :          0 : static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
    1691                 :            :                 struct nlattr **attrs)
    1692                 :            : {
    1693                 :            :         struct net *net = sock_net(skb->sk);
    1694                 :            :         struct km_event c;
    1695                 :            :         struct xfrm_usersa_flush *p = nlmsg_data(nlh);
    1696                 :            :         struct xfrm_audit audit_info;
    1697                 :            :         int err;
    1698                 :            : 
    1699                 :          0 :         audit_info.loginuid = audit_get_loginuid(current);
    1700                 :          0 :         audit_info.sessionid = audit_get_sessionid(current);
    1701                 :          0 :         security_task_getsecid(current, &audit_info.secid);
    1702                 :          0 :         err = xfrm_state_flush(net, p->proto, &audit_info);
    1703         [ #  # ]:          0 :         if (err) {
    1704         [ #  # ]:          0 :                 if (err == -ESRCH) /* empty table */
    1705                 :            :                         return 0;
    1706                 :          0 :                 return err;
    1707                 :            :         }
    1708                 :          0 :         c.data.proto = p->proto;
    1709                 :          0 :         c.event = nlh->nlmsg_type;
    1710                 :          0 :         c.seq = nlh->nlmsg_seq;
    1711                 :          0 :         c.portid = nlh->nlmsg_pid;
    1712                 :          0 :         c.net = net;
    1713                 :          0 :         km_state_notify(NULL, &c);
    1714                 :            : 
    1715                 :          0 :         return 0;
    1716                 :            : }
    1717                 :            : 
    1718                 :            : static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
    1719                 :            : {
    1720                 :          0 :         size_t replay_size = x->replay_esn ?
    1721 [ #  # ][ #  # ]:          0 :                               xfrm_replay_state_esn_len(x->replay_esn) :
    1722                 :            :                               sizeof(struct xfrm_replay_state);
    1723                 :            : 
    1724                 :          0 :         return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
    1725                 :          0 :                + nla_total_size(replay_size)
    1726                 :          0 :                + nla_total_size(sizeof(struct xfrm_lifetime_cur))
    1727                 :            :                + nla_total_size(sizeof(struct xfrm_mark))
    1728                 :            :                + nla_total_size(4) /* XFRM_AE_RTHR */
    1729                 :            :                + nla_total_size(4); /* XFRM_AE_ETHR */
    1730                 :            : }
    1731                 :            : 
    1732                 :          0 : static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
    1733                 :            : {
    1734                 :            :         struct xfrm_aevent_id *id;
    1735                 :            :         struct nlmsghdr *nlh;
    1736                 :            :         int err;
    1737                 :            : 
    1738                 :          0 :         nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
    1739         [ #  # ]:          0 :         if (nlh == NULL)
    1740                 :            :                 return -EMSGSIZE;
    1741                 :            : 
    1742                 :            :         id = nlmsg_data(nlh);
    1743                 :          0 :         memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
    1744                 :          0 :         id->sa_id.spi = x->id.spi;
    1745                 :          0 :         id->sa_id.family = x->props.family;
    1746                 :          0 :         id->sa_id.proto = x->id.proto;
    1747                 :          0 :         memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
    1748                 :          0 :         id->reqid = x->props.reqid;
    1749                 :          0 :         id->flags = c->data.aevent;
    1750                 :            : 
    1751         [ #  # ]:          0 :         if (x->replay_esn) {
    1752                 :          0 :                 err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
    1753                 :            :                               xfrm_replay_state_esn_len(x->replay_esn),
    1754                 :            :                               x->replay_esn);
    1755                 :            :         } else {
    1756                 :          0 :                 err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
    1757                 :          0 :                               &x->replay);
    1758                 :            :         }
    1759         [ #  # ]:          0 :         if (err)
    1760                 :            :                 goto out_cancel;
    1761                 :          0 :         err = nla_put(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
    1762         [ #  # ]:          0 :         if (err)
    1763                 :            :                 goto out_cancel;
    1764                 :            : 
    1765         [ #  # ]:          0 :         if (id->flags & XFRM_AE_RTHR) {
    1766                 :          0 :                 err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
    1767         [ #  # ]:          0 :                 if (err)
    1768                 :            :                         goto out_cancel;
    1769                 :            :         }
    1770         [ #  # ]:          0 :         if (id->flags & XFRM_AE_ETHR) {
    1771                 :          0 :                 err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
    1772                 :          0 :                                   x->replay_maxage * 10 / HZ);
    1773         [ #  # ]:          0 :                 if (err)
    1774                 :            :                         goto out_cancel;
    1775                 :            :         }
    1776                 :          0 :         err = xfrm_mark_put(skb, &x->mark);
    1777         [ #  # ]:          0 :         if (err)
    1778                 :            :                 goto out_cancel;
    1779                 :            : 
    1780                 :          0 :         return nlmsg_end(skb, nlh);
    1781                 :            : 
    1782                 :            : out_cancel:
    1783                 :            :         nlmsg_cancel(skb, nlh);
    1784                 :          0 :         return err;
    1785                 :            : }
    1786                 :            : 
    1787                 :          0 : static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
    1788                 :            :                 struct nlattr **attrs)
    1789                 :            : {
    1790                 :            :         struct net *net = sock_net(skb->sk);
    1791                 :          0 :         struct xfrm_state *x;
    1792                 :            :         struct sk_buff *r_skb;
    1793                 :            :         int err;
    1794                 :            :         struct km_event c;
    1795                 :            :         u32 mark;
    1796                 :            :         struct xfrm_mark m;
    1797                 :            :         struct xfrm_aevent_id *p = nlmsg_data(nlh);
    1798                 :            :         struct xfrm_usersa_id *id = &p->sa_id;
    1799                 :            : 
    1800                 :            :         mark = xfrm_mark_get(attrs, &m);
    1801                 :            : 
    1802                 :          0 :         x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
    1803         [ #  # ]:          0 :         if (x == NULL)
    1804                 :            :                 return -ESRCH;
    1805                 :            : 
    1806                 :            :         r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
    1807         [ #  # ]:          0 :         if (r_skb == NULL) {
    1808                 :            :                 xfrm_state_put(x);
    1809                 :            :                 return -ENOMEM;
    1810                 :            :         }
    1811                 :            : 
    1812                 :            :         /*
    1813                 :            :          * XXX: is this lock really needed - none of the other
    1814                 :            :          * gets lock (the concern is things getting updated
    1815                 :            :          * while we are still reading) - jhs
    1816                 :            :         */
    1817                 :            :         spin_lock_bh(&x->lock);
    1818                 :          0 :         c.data.aevent = p->flags;
    1819                 :          0 :         c.seq = nlh->nlmsg_seq;
    1820                 :          0 :         c.portid = nlh->nlmsg_pid;
    1821                 :            : 
    1822         [ #  # ]:          0 :         if (build_aevent(r_skb, x, &c) < 0)
    1823                 :          0 :                 BUG();
    1824                 :          0 :         err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
    1825                 :            :         spin_unlock_bh(&x->lock);
    1826                 :            :         xfrm_state_put(x);
    1827                 :          0 :         return err;
    1828                 :            : }
    1829                 :            : 
    1830                 :          0 : static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
    1831                 :            :                 struct nlattr **attrs)
    1832                 :            : {
    1833                 :            :         struct net *net = sock_net(skb->sk);
    1834                 :            :         struct xfrm_state *x;
    1835                 :            :         struct km_event c;
    1836                 :            :         int err = - EINVAL;
    1837                 :            :         u32 mark = 0;
    1838                 :            :         struct xfrm_mark m;
    1839                 :            :         struct xfrm_aevent_id *p = nlmsg_data(nlh);
    1840                 :          0 :         struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
    1841                 :          0 :         struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
    1842                 :          0 :         struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
    1843                 :            : 
    1844 [ #  # ][ #  # ]:          0 :         if (!lt && !rp && !re)
    1845                 :            :                 return err;
    1846                 :            : 
    1847                 :            :         /* pedantic mode - thou shalt sayeth replaceth */
    1848         [ #  # ]:          0 :         if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
    1849                 :            :                 return err;
    1850                 :            : 
    1851                 :            :         mark = xfrm_mark_get(attrs, &m);
    1852                 :            : 
    1853                 :          0 :         x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
    1854         [ #  # ]:          0 :         if (x == NULL)
    1855                 :            :                 return -ESRCH;
    1856                 :            : 
    1857         [ #  # ]:          0 :         if (x->km.state != XFRM_STATE_VALID)
    1858                 :            :                 goto out;
    1859                 :            : 
    1860                 :          0 :         err = xfrm_replay_verify_len(x->replay_esn, re);
    1861         [ #  # ]:          0 :         if (err)
    1862                 :            :                 goto out;
    1863                 :            : 
    1864                 :            :         spin_lock_bh(&x->lock);
    1865                 :          0 :         xfrm_update_ae_params(x, attrs, 1);
    1866                 :            :         spin_unlock_bh(&x->lock);
    1867                 :            : 
    1868                 :          0 :         c.event = nlh->nlmsg_type;
    1869                 :          0 :         c.seq = nlh->nlmsg_seq;
    1870                 :          0 :         c.portid = nlh->nlmsg_pid;
    1871                 :          0 :         c.data.aevent = XFRM_AE_CU;
    1872                 :          0 :         km_state_notify(x, &c);
    1873                 :            :         err = 0;
    1874                 :            : out:
    1875                 :            :         xfrm_state_put(x);
    1876                 :          0 :         return err;
    1877                 :            : }
    1878                 :            : 
    1879                 :          0 : static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
    1880                 :            :                 struct nlattr **attrs)
    1881                 :            : {
    1882                 :            :         struct net *net = sock_net(skb->sk);
    1883                 :            :         struct km_event c;
    1884                 :            :         u8 type = XFRM_POLICY_TYPE_MAIN;
    1885                 :            :         int err;
    1886                 :            :         struct xfrm_audit audit_info;
    1887                 :            : 
    1888                 :            :         err = copy_from_user_policy_type(&type, attrs);
    1889         [ #  # ]:          0 :         if (err)
    1890                 :            :                 return err;
    1891                 :            : 
    1892                 :          0 :         audit_info.loginuid = audit_get_loginuid(current);
    1893                 :          0 :         audit_info.sessionid = audit_get_sessionid(current);
    1894                 :          0 :         security_task_getsecid(current, &audit_info.secid);
    1895                 :          0 :         err = xfrm_policy_flush(net, type, &audit_info);
    1896         [ #  # ]:          0 :         if (err) {
    1897         [ #  # ]:          0 :                 if (err == -ESRCH) /* empty table */
    1898                 :            :                         return 0;
    1899                 :          0 :                 return err;
    1900                 :            :         }
    1901                 :            : 
    1902                 :          0 :         c.data.type = type;
    1903                 :          0 :         c.event = nlh->nlmsg_type;
    1904                 :          0 :         c.seq = nlh->nlmsg_seq;
    1905                 :          0 :         c.portid = nlh->nlmsg_pid;
    1906                 :          0 :         c.net = net;
    1907                 :          0 :         km_policy_notify(NULL, 0, &c);
    1908                 :          0 :         return 0;
    1909                 :            : }
    1910                 :            : 
    1911                 :          0 : static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
    1912                 :            :                 struct nlattr **attrs)
    1913                 :            : {
    1914                 :            :         struct net *net = sock_net(skb->sk);
    1915                 :            :         struct xfrm_policy *xp;
    1916                 :            :         struct xfrm_user_polexpire *up = nlmsg_data(nlh);
    1917                 :            :         struct xfrm_userpolicy_info *p = &up->pol;
    1918                 :            :         u8 type = XFRM_POLICY_TYPE_MAIN;
    1919                 :          0 :         int err = -ENOENT;
    1920                 :            :         struct xfrm_mark m;
    1921                 :            :         u32 mark = xfrm_mark_get(attrs, &m);
    1922                 :            : 
    1923                 :          0 :         err = copy_from_user_policy_type(&type, attrs);
    1924         [ #  # ]:          0 :         if (err)
    1925                 :            :                 return err;
    1926                 :            : 
    1927                 :          0 :         err = verify_policy_dir(p->dir);
    1928         [ #  # ]:          0 :         if (err)
    1929                 :            :                 return err;
    1930                 :            : 
    1931         [ #  # ]:          0 :         if (p->index)
    1932                 :          0 :                 xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
    1933                 :            :         else {
    1934                 :          0 :                 struct nlattr *rt = attrs[XFRMA_SEC_CTX];
    1935                 :            :                 struct xfrm_sec_ctx *ctx;
    1936                 :            : 
    1937                 :          0 :                 err = verify_sec_ctx_len(attrs);
    1938         [ #  # ]:          0 :                 if (err)
    1939                 :            :                         return err;
    1940                 :            : 
    1941                 :            :                 ctx = NULL;
    1942         [ #  # ]:          0 :                 if (rt) {
    1943                 :            :                         struct xfrm_user_sec_ctx *uctx = nla_data(rt);
    1944                 :            : 
    1945                 :          0 :                         err = security_xfrm_policy_alloc(&ctx, uctx);
    1946                 :            :                         if (err)
    1947                 :            :                                 return err;
    1948                 :            :                 }
    1949                 :          0 :                 xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
    1950                 :            :                                            &p->sel, ctx, 0, &err);
    1951                 :            :                 security_xfrm_policy_free(ctx);
    1952                 :            :         }
    1953         [ #  # ]:          0 :         if (xp == NULL)
    1954                 :            :                 return -ENOENT;
    1955                 :            : 
    1956         [ #  # ]:          0 :         if (unlikely(xp->walk.dead))
    1957                 :            :                 goto out;
    1958                 :            : 
    1959                 :          0 :         err = 0;
    1960         [ #  # ]:          0 :         if (up->hard) {
    1961                 :          0 :                 kuid_t loginuid = audit_get_loginuid(current);
    1962                 :          0 :                 u32 sessionid = audit_get_sessionid(current);
    1963                 :            :                 u32 sid;
    1964                 :            : 
    1965                 :          0 :                 security_task_getsecid(current, &sid);
    1966                 :          0 :                 xfrm_policy_delete(xp, p->dir);
    1967                 :          0 :                 xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
    1968                 :            : 
    1969                 :            :         } else {
    1970                 :            :                 // reset the timers here?
    1971                 :          0 :                 WARN(1, "Dont know what to do with soft policy expire\n");
    1972                 :            :         }
    1973                 :          0 :         km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
    1974                 :            : 
    1975                 :            : out:
    1976                 :            :         xfrm_pol_put(xp);
    1977                 :          0 :         return err;
    1978                 :            : }
    1979                 :            : 
    1980                 :          0 : static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
    1981                 :            :                 struct nlattr **attrs)
    1982                 :            : {
    1983                 :            :         struct net *net = sock_net(skb->sk);
    1984                 :            :         struct xfrm_state *x;
    1985                 :            :         int err;
    1986                 :            :         struct xfrm_user_expire *ue = nlmsg_data(nlh);
    1987                 :            :         struct xfrm_usersa_info *p = &ue->state;
    1988                 :            :         struct xfrm_mark m;
    1989                 :            :         u32 mark = xfrm_mark_get(attrs, &m);
    1990                 :            : 
    1991                 :          0 :         x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
    1992                 :            : 
    1993                 :            :         err = -ENOENT;
    1994         [ #  # ]:          0 :         if (x == NULL)
    1995                 :            :                 return err;
    1996                 :            : 
    1997                 :            :         spin_lock_bh(&x->lock);
    1998                 :            :         err = -EINVAL;
    1999         [ #  # ]:          0 :         if (x->km.state != XFRM_STATE_VALID)
    2000                 :            :                 goto out;
    2001                 :          0 :         km_state_expired(x, ue->hard, nlh->nlmsg_pid);
    2002                 :            : 
    2003         [ #  # ]:          0 :         if (ue->hard) {
    2004                 :          0 :                 kuid_t loginuid = audit_get_loginuid(current);
    2005                 :          0 :                 u32 sessionid = audit_get_sessionid(current);
    2006                 :            :                 u32 sid;
    2007                 :            : 
    2008                 :          0 :                 security_task_getsecid(current, &sid);
    2009                 :          0 :                 __xfrm_state_delete(x);
    2010                 :          0 :                 xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
    2011                 :            :         }
    2012                 :            :         err = 0;
    2013                 :            : out:
    2014                 :            :         spin_unlock_bh(&x->lock);
    2015                 :            :         xfrm_state_put(x);
    2016                 :          0 :         return err;
    2017                 :            : }
    2018                 :            : 
    2019                 :          0 : static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
    2020                 :            :                 struct nlattr **attrs)
    2021                 :            : {
    2022                 :            :         struct net *net = sock_net(skb->sk);
    2023                 :            :         struct xfrm_policy *xp;
    2024                 :            :         struct xfrm_user_tmpl *ut;
    2025                 :            :         int i;
    2026                 :          0 :         struct nlattr *rt = attrs[XFRMA_TMPL];
    2027                 :            :         struct xfrm_mark mark;
    2028                 :            : 
    2029                 :            :         struct xfrm_user_acquire *ua = nlmsg_data(nlh);
    2030                 :          0 :         struct xfrm_state *x = xfrm_state_alloc(net);
    2031                 :          0 :         int err = -ENOMEM;
    2032                 :            : 
    2033         [ #  # ]:          0 :         if (!x)
    2034                 :            :                 goto nomem;
    2035                 :            : 
    2036                 :            :         xfrm_mark_get(attrs, &mark);
    2037                 :            : 
    2038                 :          0 :         err = verify_newpolicy_info(&ua->policy);
    2039         [ #  # ]:          0 :         if (err)
    2040                 :            :                 goto bad_policy;
    2041                 :            : 
    2042                 :            :         /*   build an XP */
    2043                 :          0 :         xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
    2044         [ #  # ]:          0 :         if (!xp)
    2045                 :            :                 goto free_state;
    2046                 :            : 
    2047                 :          0 :         memcpy(&x->id, &ua->id, sizeof(ua->id));
    2048                 :          0 :         memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
    2049                 :          0 :         memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
    2050                 :          0 :         xp->mark.m = x->mark.m = mark.m;
    2051                 :          0 :         xp->mark.v = x->mark.v = mark.v;
    2052                 :          0 :         ut = nla_data(rt);
    2053                 :            :         /* extract the templates and for each call km_key */
    2054         [ #  # ]:          0 :         for (i = 0; i < xp->xfrm_nr; i++, ut++) {
    2055                 :          0 :                 struct xfrm_tmpl *t = &xp->xfrm_vec[i];
    2056                 :          0 :                 memcpy(&x->id, &t->id, sizeof(x->id));
    2057                 :          0 :                 x->props.mode = t->mode;
    2058                 :          0 :                 x->props.reqid = t->reqid;
    2059                 :          0 :                 x->props.family = ut->family;
    2060                 :          0 :                 t->aalgos = ua->aalgos;
    2061                 :          0 :                 t->ealgos = ua->ealgos;
    2062                 :          0 :                 t->calgos = ua->calgos;
    2063                 :          0 :                 err = km_query(x, t, xp);
    2064                 :            : 
    2065                 :            :         }
    2066                 :            : 
    2067                 :          0 :         kfree(x);
    2068                 :          0 :         kfree(xp);
    2069                 :            : 
    2070                 :          0 :         return 0;
    2071                 :            : 
    2072                 :            : bad_policy:
    2073                 :          0 :         WARN(1, "BAD policy passed\n");
    2074                 :            : free_state:
    2075                 :          0 :         kfree(x);
    2076                 :            : nomem:
    2077                 :          0 :         return err;
    2078                 :            : }
    2079                 :            : 
    2080                 :            : #ifdef CONFIG_XFRM_MIGRATE
    2081                 :          0 : static int copy_from_user_migrate(struct xfrm_migrate *ma,
    2082                 :            :                                   struct xfrm_kmaddress *k,
    2083                 :            :                                   struct nlattr **attrs, int *num)
    2084                 :            : {
    2085                 :          0 :         struct nlattr *rt = attrs[XFRMA_MIGRATE];
    2086                 :            :         struct xfrm_user_migrate *um;
    2087                 :            :         int i, num_migrate;
    2088                 :            : 
    2089         [ #  # ]:          0 :         if (k != NULL) {
    2090                 :            :                 struct xfrm_user_kmaddress *uk;
    2091                 :            : 
    2092                 :          0 :                 uk = nla_data(attrs[XFRMA_KMADDRESS]);
    2093                 :          0 :                 memcpy(&k->local, &uk->local, sizeof(k->local));
    2094                 :          0 :                 memcpy(&k->remote, &uk->remote, sizeof(k->remote));
    2095                 :          0 :                 k->family = uk->family;
    2096                 :          0 :                 k->reserved = uk->reserved;
    2097                 :            :         }
    2098                 :            : 
    2099                 :          0 :         um = nla_data(rt);
    2100                 :          0 :         num_migrate = nla_len(rt) / sizeof(*um);
    2101                 :            : 
    2102         [ #  # ]:          0 :         if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
    2103                 :            :                 return -EINVAL;
    2104                 :            : 
    2105         [ #  # ]:          0 :         for (i = 0; i < num_migrate; i++, um++, ma++) {
    2106                 :          0 :                 memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
    2107                 :          0 :                 memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
    2108                 :          0 :                 memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
    2109                 :          0 :                 memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
    2110                 :            : 
    2111                 :          0 :                 ma->proto = um->proto;
    2112                 :          0 :                 ma->mode = um->mode;
    2113                 :          0 :                 ma->reqid = um->reqid;
    2114                 :            : 
    2115                 :          0 :                 ma->old_family = um->old_family;
    2116                 :          0 :                 ma->new_family = um->new_family;
    2117                 :            :         }
    2118                 :            : 
    2119                 :          0 :         *num = i;
    2120                 :          0 :         return 0;
    2121                 :            : }
    2122                 :            : 
    2123                 :          0 : static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
    2124                 :            :                            struct nlattr **attrs)
    2125                 :            : {
    2126                 :            :         struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
    2127                 :            :         struct xfrm_migrate m[XFRM_MAX_DEPTH];
    2128                 :            :         struct xfrm_kmaddress km, *kmp;
    2129                 :            :         u8 type;
    2130                 :            :         int err;
    2131                 :          0 :         int n = 0;
    2132                 :            : 
    2133         [ #  # ]:          0 :         if (attrs[XFRMA_MIGRATE] == NULL)
    2134                 :            :                 return -EINVAL;
    2135                 :            : 
    2136         [ #  # ]:          0 :         kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
    2137                 :            : 
    2138                 :            :         err = copy_from_user_policy_type(&type, attrs);
    2139         [ #  # ]:          0 :         if (err)
    2140                 :            :                 return err;
    2141                 :            : 
    2142                 :          0 :         err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
    2143         [ #  # ]:          0 :         if (err)
    2144                 :            :                 return err;
    2145                 :            : 
    2146         [ #  # ]:          0 :         if (!n)
    2147                 :            :                 return 0;
    2148                 :            : 
    2149                 :          0 :         xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp);
    2150                 :            : 
    2151                 :          0 :         return 0;
    2152                 :            : }
    2153                 :            : #else
    2154                 :            : static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
    2155                 :            :                            struct nlattr **attrs)
    2156                 :            : {
    2157                 :            :         return -ENOPROTOOPT;
    2158                 :            : }
    2159                 :            : #endif
    2160                 :            : 
    2161                 :            : #ifdef CONFIG_XFRM_MIGRATE
    2162                 :          0 : static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
    2163                 :            : {
    2164                 :            :         struct xfrm_user_migrate um;
    2165                 :            : 
    2166                 :          0 :         memset(&um, 0, sizeof(um));
    2167                 :          0 :         um.proto = m->proto;
    2168                 :          0 :         um.mode = m->mode;
    2169                 :          0 :         um.reqid = m->reqid;
    2170                 :          0 :         um.old_family = m->old_family;
    2171                 :          0 :         memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
    2172                 :          0 :         memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
    2173                 :          0 :         um.new_family = m->new_family;
    2174                 :          0 :         memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
    2175                 :          0 :         memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
    2176                 :            : 
    2177                 :          0 :         return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
    2178                 :            : }
    2179                 :            : 
    2180                 :          0 : static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
    2181                 :            : {
    2182                 :            :         struct xfrm_user_kmaddress uk;
    2183                 :            : 
    2184                 :          0 :         memset(&uk, 0, sizeof(uk));
    2185                 :          0 :         uk.family = k->family;
    2186                 :          0 :         uk.reserved = k->reserved;
    2187                 :          0 :         memcpy(&uk.local, &k->local, sizeof(uk.local));
    2188                 :          0 :         memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
    2189                 :            : 
    2190                 :          0 :         return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
    2191                 :            : }
    2192                 :            : 
    2193                 :            : static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma)
    2194                 :            : {
    2195                 :            :         return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
    2196         [ #  # ]:          0 :               + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
    2197                 :          0 :               + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
    2198                 :            :               + userpolicy_type_attrsize();
    2199                 :            : }
    2200                 :            : 
    2201                 :          0 : static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
    2202                 :            :                          int num_migrate, const struct xfrm_kmaddress *k,
    2203                 :            :                          const struct xfrm_selector *sel, u8 dir, u8 type)
    2204                 :            : {
    2205                 :            :         const struct xfrm_migrate *mp;
    2206                 :            :         struct xfrm_userpolicy_id *pol_id;
    2207                 :            :         struct nlmsghdr *nlh;
    2208                 :            :         int i, err;
    2209                 :            : 
    2210                 :            :         nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
    2211         [ #  # ]:          0 :         if (nlh == NULL)
    2212                 :            :                 return -EMSGSIZE;
    2213                 :            : 
    2214                 :          0 :         pol_id = nlmsg_data(nlh);
    2215                 :            :         /* copy data from selector, dir, and type to the pol_id */
    2216                 :          0 :         memset(pol_id, 0, sizeof(*pol_id));
    2217                 :          0 :         memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
    2218                 :          0 :         pol_id->dir = dir;
    2219                 :            : 
    2220         [ #  # ]:          0 :         if (k != NULL) {
    2221                 :          0 :                 err = copy_to_user_kmaddress(k, skb);
    2222         [ #  # ]:          0 :                 if (err)
    2223                 :            :                         goto out_cancel;
    2224                 :            :         }
    2225                 :            :         err = copy_to_user_policy_type(type, skb);
    2226                 :            :         if (err)
    2227                 :            :                 goto out_cancel;
    2228         [ #  # ]:          0 :         for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
    2229                 :          0 :                 err = copy_to_user_migrate(mp, skb);
    2230         [ #  # ]:          0 :                 if (err)
    2231                 :            :                         goto out_cancel;
    2232                 :            :         }
    2233                 :            : 
    2234                 :            :         return nlmsg_end(skb, nlh);
    2235                 :            : 
    2236                 :            : out_cancel:
    2237                 :            :         nlmsg_cancel(skb, nlh);
    2238                 :            :         return err;
    2239                 :            : }
    2240                 :            : 
    2241                 :          0 : static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
    2242                 :            :                              const struct xfrm_migrate *m, int num_migrate,
    2243                 :            :                              const struct xfrm_kmaddress *k)
    2244                 :            : {
    2245                 :            :         struct net *net = &init_net;
    2246                 :            :         struct sk_buff *skb;
    2247                 :            : 
    2248                 :            :         skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k), GFP_ATOMIC);
    2249         [ #  # ]:          0 :         if (skb == NULL)
    2250                 :            :                 return -ENOMEM;
    2251                 :            : 
    2252                 :            :         /* build migrate */
    2253         [ #  # ]:          0 :         if (build_migrate(skb, m, num_migrate, k, sel, dir, type) < 0)
    2254                 :          0 :                 BUG();
    2255                 :            : 
    2256                 :          0 :         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
    2257                 :            : }
    2258                 :            : #else
    2259                 :            : static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
    2260                 :            :                              const struct xfrm_migrate *m, int num_migrate,
    2261                 :            :                              const struct xfrm_kmaddress *k)
    2262                 :            : {
    2263                 :            :         return -ENOPROTOOPT;
    2264                 :            : }
    2265                 :            : #endif
    2266                 :            : 
    2267                 :            : #define XMSGSIZE(type) sizeof(struct type)
    2268                 :            : 
    2269                 :            : static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
    2270                 :            :         [XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
    2271                 :            :         [XFRM_MSG_DELSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
    2272                 :            :         [XFRM_MSG_GETSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
    2273                 :            :         [XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
    2274                 :            :         [XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
    2275                 :            :         [XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
    2276                 :            :         [XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
    2277                 :            :         [XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
    2278                 :            :         [XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
    2279                 :            :         [XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
    2280                 :            :         [XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
    2281                 :            :         [XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
    2282                 :            :         [XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
    2283                 :            :         [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
    2284                 :            :         [XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
    2285                 :            :         [XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
    2286                 :            :         [XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
    2287                 :            :         [XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
    2288                 :            :         [XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = sizeof(u32),
    2289                 :            :         [XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = sizeof(u32),
    2290                 :            : };
    2291                 :            : 
    2292                 :            : #undef XMSGSIZE
    2293                 :            : 
    2294                 :            : static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
    2295                 :            :         [XFRMA_SA]              = { .len = sizeof(struct xfrm_usersa_info)},
    2296                 :            :         [XFRMA_POLICY]          = { .len = sizeof(struct xfrm_userpolicy_info)},
    2297                 :            :         [XFRMA_LASTUSED]        = { .type = NLA_U64},
    2298                 :            :         [XFRMA_ALG_AUTH_TRUNC]  = { .len = sizeof(struct xfrm_algo_auth)},
    2299                 :            :         [XFRMA_ALG_AEAD]        = { .len = sizeof(struct xfrm_algo_aead) },
    2300                 :            :         [XFRMA_ALG_AUTH]        = { .len = sizeof(struct xfrm_algo) },
    2301                 :            :         [XFRMA_ALG_CRYPT]       = { .len = sizeof(struct xfrm_algo) },
    2302                 :            :         [XFRMA_ALG_COMP]        = { .len = sizeof(struct xfrm_algo) },
    2303                 :            :         [XFRMA_ENCAP]           = { .len = sizeof(struct xfrm_encap_tmpl) },
    2304                 :            :         [XFRMA_TMPL]            = { .len = sizeof(struct xfrm_user_tmpl) },
    2305                 :            :         [XFRMA_SEC_CTX]         = { .len = sizeof(struct xfrm_sec_ctx) },
    2306                 :            :         [XFRMA_LTIME_VAL]       = { .len = sizeof(struct xfrm_lifetime_cur) },
    2307                 :            :         [XFRMA_REPLAY_VAL]      = { .len = sizeof(struct xfrm_replay_state) },
    2308                 :            :         [XFRMA_REPLAY_THRESH]   = { .type = NLA_U32 },
    2309                 :            :         [XFRMA_ETIMER_THRESH]   = { .type = NLA_U32 },
    2310                 :            :         [XFRMA_SRCADDR]         = { .len = sizeof(xfrm_address_t) },
    2311                 :            :         [XFRMA_COADDR]          = { .len = sizeof(xfrm_address_t) },
    2312                 :            :         [XFRMA_POLICY_TYPE]     = { .len = sizeof(struct xfrm_userpolicy_type)},
    2313                 :            :         [XFRMA_MIGRATE]         = { .len = sizeof(struct xfrm_user_migrate) },
    2314                 :            :         [XFRMA_KMADDRESS]       = { .len = sizeof(struct xfrm_user_kmaddress) },
    2315                 :            :         [XFRMA_MARK]            = { .len = sizeof(struct xfrm_mark) },
    2316                 :            :         [XFRMA_TFCPAD]          = { .type = NLA_U32 },
    2317                 :            :         [XFRMA_REPLAY_ESN_VAL]  = { .len = sizeof(struct xfrm_replay_state_esn) },
    2318                 :            :         [XFRMA_SA_EXTRA_FLAGS]  = { .type = NLA_U32 },
    2319                 :            : };
    2320                 :            : 
    2321                 :            : static const struct xfrm_link {
    2322                 :            :         int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
    2323                 :            :         int (*dump)(struct sk_buff *, struct netlink_callback *);
    2324                 :            :         int (*done)(struct netlink_callback *);
    2325                 :            : } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
    2326                 :            :         [XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
    2327                 :            :         [XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
    2328                 :            :         [XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
    2329                 :            :                                                    .dump = xfrm_dump_sa,
    2330                 :            :                                                    .done = xfrm_dump_sa_done  },
    2331                 :            :         [XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
    2332                 :            :         [XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
    2333                 :            :         [XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
    2334                 :            :                                                    .dump = xfrm_dump_policy,
    2335                 :            :                                                    .done = xfrm_dump_policy_done },
    2336                 :            :         [XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
    2337                 :            :         [XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
    2338                 :            :         [XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
    2339                 :            :         [XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
    2340                 :            :         [XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
    2341                 :            :         [XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
    2342                 :            :         [XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
    2343                 :            :         [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
    2344                 :            :         [XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
    2345                 :            :         [XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
    2346                 :            :         [XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
    2347                 :            :         [XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
    2348                 :            :         [XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
    2349                 :            : };
    2350                 :            : 
    2351                 :          0 : static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
    2352                 :            : {
    2353                 :            :         struct net *net = sock_net(skb->sk);
    2354                 :            :         struct nlattr *attrs[XFRMA_MAX+1];
    2355                 :            :         const struct xfrm_link *link;
    2356                 :            :         int type, err;
    2357                 :            : 
    2358                 :          0 :         type = nlh->nlmsg_type;
    2359         [ #  # ]:          0 :         if (type > XFRM_MSG_MAX)
    2360                 :            :                 return -EINVAL;
    2361                 :            : 
    2362                 :          0 :         type -= XFRM_MSG_BASE;
    2363                 :          0 :         link = &xfrm_dispatch[type];
    2364                 :            : 
    2365                 :            :         /* All operations require privileges, even GET */
    2366         [ #  # ]:          0 :         if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
    2367                 :            :                 return -EPERM;
    2368                 :            : 
    2369         [ #  # ]:          0 :         if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
    2370         [ #  # ]:          0 :              type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
    2371                 :          0 :             (nlh->nlmsg_flags & NLM_F_DUMP)) {
    2372         [ #  # ]:          0 :                 if (link->dump == NULL)
    2373                 :            :                         return -EINVAL;
    2374                 :            : 
    2375                 :            :                 {
    2376                 :          0 :                         struct netlink_dump_control c = {
    2377                 :            :                                 .dump = link->dump,
    2378                 :          0 :                                 .done = link->done,
    2379                 :            :                         };
    2380                 :          0 :                         return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
    2381                 :            :                 }
    2382                 :            :         }
    2383                 :            : 
    2384                 :          0 :         err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
    2385                 :            :                           xfrma_policy);
    2386         [ #  # ]:          0 :         if (err < 0)
    2387                 :            :                 return err;
    2388                 :            : 
    2389         [ #  # ]:          0 :         if (link->doit == NULL)
    2390                 :            :                 return -EINVAL;
    2391                 :            : 
    2392                 :          0 :         return link->doit(skb, nlh, attrs);
    2393                 :            : }
    2394                 :            : 
    2395                 :          0 : static void xfrm_netlink_rcv(struct sk_buff *skb)
    2396                 :            : {
    2397                 :          0 :         mutex_lock(&xfrm_cfg_mutex);
    2398                 :          0 :         netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
    2399                 :          0 :         mutex_unlock(&xfrm_cfg_mutex);
    2400                 :          0 : }
    2401                 :            : 
    2402                 :            : static inline size_t xfrm_expire_msgsize(void)
    2403                 :            : {
    2404                 :            :         return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
    2405                 :            :                + nla_total_size(sizeof(struct xfrm_mark));
    2406                 :            : }
    2407                 :            : 
    2408                 :          0 : static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
    2409                 :            : {
    2410                 :            :         struct xfrm_user_expire *ue;
    2411                 :            :         struct nlmsghdr *nlh;
    2412                 :            :         int err;
    2413                 :            : 
    2414                 :          0 :         nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
    2415         [ #  # ]:          0 :         if (nlh == NULL)
    2416                 :            :                 return -EMSGSIZE;
    2417                 :            : 
    2418                 :            :         ue = nlmsg_data(nlh);
    2419                 :          0 :         copy_to_user_state(x, &ue->state);
    2420                 :          0 :         ue->hard = (c->data.hard != 0) ? 1 : 0;
    2421                 :            : 
    2422                 :          0 :         err = xfrm_mark_put(skb, &x->mark);
    2423         [ #  # ]:          0 :         if (err)
    2424                 :            :                 return err;
    2425                 :            : 
    2426                 :            :         return nlmsg_end(skb, nlh);
    2427                 :            : }
    2428                 :            : 
    2429                 :          0 : static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
    2430                 :            : {
    2431                 :            :         struct net *net = xs_net(x);
    2432                 :            :         struct sk_buff *skb;
    2433                 :            : 
    2434                 :            :         skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
    2435         [ #  # ]:          0 :         if (skb == NULL)
    2436                 :            :                 return -ENOMEM;
    2437                 :            : 
    2438         [ #  # ]:          0 :         if (build_expire(skb, x, c) < 0) {
    2439                 :          0 :                 kfree_skb(skb);
    2440                 :          0 :                 return -EMSGSIZE;
    2441                 :            :         }
    2442                 :            : 
    2443                 :          0 :         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
    2444                 :            : }
    2445                 :            : 
    2446                 :          0 : static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
    2447                 :            : {
    2448                 :            :         struct net *net = xs_net(x);
    2449                 :            :         struct sk_buff *skb;
    2450                 :            : 
    2451                 :            :         skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
    2452         [ #  # ]:          0 :         if (skb == NULL)
    2453                 :            :                 return -ENOMEM;
    2454                 :            : 
    2455         [ #  # ]:          0 :         if (build_aevent(skb, x, c) < 0)
    2456                 :          0 :                 BUG();
    2457                 :            : 
    2458                 :          0 :         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
    2459                 :            : }
    2460                 :            : 
    2461                 :          0 : static int xfrm_notify_sa_flush(const struct km_event *c)
    2462                 :            : {
    2463                 :          0 :         struct net *net = c->net;
    2464                 :            :         struct xfrm_usersa_flush *p;
    2465                 :            :         struct nlmsghdr *nlh;
    2466                 :            :         struct sk_buff *skb;
    2467                 :            :         int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
    2468                 :            : 
    2469                 :            :         skb = nlmsg_new(len, GFP_ATOMIC);
    2470         [ #  # ]:          0 :         if (skb == NULL)
    2471                 :            :                 return -ENOMEM;
    2472                 :            : 
    2473                 :          0 :         nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
    2474         [ #  # ]:          0 :         if (nlh == NULL) {
    2475                 :          0 :                 kfree_skb(skb);
    2476                 :          0 :                 return -EMSGSIZE;
    2477                 :            :         }
    2478                 :            : 
    2479                 :            :         p = nlmsg_data(nlh);
    2480                 :          0 :         p->proto = c->data.proto;
    2481                 :            : 
    2482                 :            :         nlmsg_end(skb, nlh);
    2483                 :            : 
    2484                 :          0 :         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
    2485                 :            : }
    2486                 :            : 
    2487                 :            : static inline size_t xfrm_sa_len(struct xfrm_state *x)
    2488                 :            : {
    2489                 :            :         size_t l = 0;
    2490         [ #  # ]:          0 :         if (x->aead)
    2491                 :          0 :                 l += nla_total_size(aead_len(x->aead));
    2492         [ #  # ]:          0 :         if (x->aalg) {
    2493                 :          0 :                 l += nla_total_size(sizeof(struct xfrm_algo) +
    2494                 :          0 :                                     (x->aalg->alg_key_len + 7) / 8);
    2495                 :          0 :                 l += nla_total_size(xfrm_alg_auth_len(x->aalg));
    2496                 :            :         }
    2497         [ #  # ]:          0 :         if (x->ealg)
    2498                 :          0 :                 l += nla_total_size(xfrm_alg_len(x->ealg));
    2499         [ #  # ]:          0 :         if (x->calg)
    2500                 :          0 :                 l += nla_total_size(sizeof(*x->calg));
    2501         [ #  # ]:          0 :         if (x->encap)
    2502                 :          0 :                 l += nla_total_size(sizeof(*x->encap));
    2503         [ #  # ]:          0 :         if (x->tfcpad)
    2504                 :          0 :                 l += nla_total_size(sizeof(x->tfcpad));
    2505         [ #  # ]:          0 :         if (x->replay_esn)
    2506                 :          0 :                 l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
    2507         [ #  # ]:          0 :         if (x->security)
    2508                 :          0 :                 l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
    2509                 :          0 :                                     x->security->ctx_len);
    2510         [ #  # ]:          0 :         if (x->coaddr)
    2511                 :          0 :                 l += nla_total_size(sizeof(*x->coaddr));
    2512         [ #  # ]:          0 :         if (x->props.extra_flags)
    2513                 :          0 :                 l += nla_total_size(sizeof(x->props.extra_flags));
    2514                 :            : 
    2515                 :            :         /* Must count x->lastused as it may become non-zero behind our back. */
    2516                 :          0 :         l += nla_total_size(sizeof(u64));
    2517                 :            : 
    2518                 :            :         return l;
    2519                 :            : }
    2520                 :            : 
    2521                 :          0 : static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
    2522                 :            : {
    2523                 :            :         struct net *net = xs_net(x);
    2524                 :            :         struct xfrm_usersa_info *p;
    2525                 :            :         struct xfrm_usersa_id *id;
    2526                 :            :         struct nlmsghdr *nlh;
    2527                 :            :         struct sk_buff *skb;
    2528                 :          0 :         int len = xfrm_sa_len(x);
    2529                 :            :         int headlen, err;
    2530                 :            : 
    2531                 :            :         headlen = sizeof(*p);
    2532         [ #  # ]:          0 :         if (c->event == XFRM_MSG_DELSA) {
    2533                 :            :                 len += nla_total_size(headlen);
    2534                 :            :                 headlen = sizeof(*id);
    2535                 :          0 :                 len += nla_total_size(sizeof(struct xfrm_mark));
    2536                 :            :         }
    2537                 :          0 :         len += NLMSG_ALIGN(headlen);
    2538                 :            : 
    2539                 :            :         skb = nlmsg_new(len, GFP_ATOMIC);
    2540         [ #  # ]:          0 :         if (skb == NULL)
    2541                 :            :                 return -ENOMEM;
    2542                 :            : 
    2543                 :          0 :         nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
    2544                 :            :         err = -EMSGSIZE;
    2545         [ #  # ]:          0 :         if (nlh == NULL)
    2546                 :            :                 goto out_free_skb;
    2547                 :            : 
    2548                 :          0 :         p = nlmsg_data(nlh);
    2549         [ #  # ]:          0 :         if (c->event == XFRM_MSG_DELSA) {
    2550                 :            :                 struct nlattr *attr;
    2551                 :            : 
    2552                 :            :                 id = nlmsg_data(nlh);
    2553                 :          0 :                 memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
    2554                 :          0 :                 id->spi = x->id.spi;
    2555                 :          0 :                 id->family = x->props.family;
    2556                 :          0 :                 id->proto = x->id.proto;
    2557                 :            : 
    2558                 :          0 :                 attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
    2559                 :            :                 err = -EMSGSIZE;
    2560         [ #  # ]:          0 :                 if (attr == NULL)
    2561                 :            :                         goto out_free_skb;
    2562                 :            : 
    2563                 :          0 :                 p = nla_data(attr);
    2564                 :            :         }
    2565                 :          0 :         err = copy_to_user_state_extra(x, p, skb);
    2566         [ #  # ]:          0 :         if (err)
    2567                 :            :                 goto out_free_skb;
    2568                 :            : 
    2569                 :            :         nlmsg_end(skb, nlh);
    2570                 :            : 
    2571                 :          0 :         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
    2572                 :            : 
    2573                 :            : out_free_skb:
    2574                 :          0 :         kfree_skb(skb);
    2575                 :          0 :         return err;
    2576                 :            : }
    2577                 :            : 
    2578                 :          0 : static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
    2579                 :            : {
    2580                 :            : 
    2581   [ #  #  #  #  :          0 :         switch (c->event) {
                      # ]
    2582                 :            :         case XFRM_MSG_EXPIRE:
    2583                 :          0 :                 return xfrm_exp_state_notify(x, c);
    2584                 :            :         case XFRM_MSG_NEWAE:
    2585                 :          0 :                 return xfrm_aevent_state_notify(x, c);
    2586                 :            :         case XFRM_MSG_DELSA:
    2587                 :            :         case XFRM_MSG_UPDSA:
    2588                 :            :         case XFRM_MSG_NEWSA:
    2589                 :          0 :                 return xfrm_notify_sa(x, c);
    2590                 :            :         case XFRM_MSG_FLUSHSA:
    2591                 :          0 :                 return xfrm_notify_sa_flush(c);
    2592                 :            :         default:
    2593                 :          0 :                 printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
    2594                 :            :                        c->event);
    2595                 :            :                 break;
    2596                 :            :         }
    2597                 :            : 
    2598                 :          0 :         return 0;
    2599                 :            : 
    2600                 :            : }
    2601                 :            : 
    2602                 :            : static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
    2603                 :            :                                           struct xfrm_policy *xp)
    2604                 :            : {
    2605                 :          0 :         return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
    2606                 :          0 :                + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
    2607                 :          0 :                + nla_total_size(sizeof(struct xfrm_mark))
    2608                 :          0 :                + nla_total_size(xfrm_user_sec_ctx_size(x->security))
    2609                 :            :                + userpolicy_type_attrsize();
    2610                 :            : }
    2611                 :            : 
    2612                 :          0 : static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
    2613                 :            :                          struct xfrm_tmpl *xt, struct xfrm_policy *xp)
    2614                 :            : {
    2615                 :          0 :         __u32 seq = xfrm_get_acqseq();
    2616                 :            :         struct xfrm_user_acquire *ua;
    2617                 :            :         struct nlmsghdr *nlh;
    2618                 :            :         int err;
    2619                 :            : 
    2620                 :            :         nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
    2621         [ #  # ]:          0 :         if (nlh == NULL)
    2622                 :            :                 return -EMSGSIZE;
    2623                 :            : 
    2624                 :            :         ua = nlmsg_data(nlh);
    2625                 :          0 :         memcpy(&ua->id, &x->id, sizeof(ua->id));
    2626                 :          0 :         memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
    2627                 :          0 :         memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
    2628                 :          0 :         copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
    2629                 :          0 :         ua->aalgos = xt->aalgos;
    2630                 :          0 :         ua->ealgos = xt->ealgos;
    2631                 :          0 :         ua->calgos = xt->calgos;
    2632                 :          0 :         ua->seq = x->km.seq = seq;
    2633                 :            : 
    2634                 :          0 :         err = copy_to_user_tmpl(xp, skb);
    2635         [ #  # ]:          0 :         if (!err)
    2636                 :            :                 err = copy_to_user_state_sec_ctx(x, skb);
    2637         [ #  # ]:          0 :         if (!err)
    2638                 :            :                 err = copy_to_user_policy_type(xp->type, skb);
    2639         [ #  # ]:          0 :         if (!err)
    2640                 :          0 :                 err = xfrm_mark_put(skb, &xp->mark);
    2641         [ #  # ]:          0 :         if (err) {
    2642                 :            :                 nlmsg_cancel(skb, nlh);
    2643                 :          0 :                 return err;
    2644                 :            :         }
    2645                 :            : 
    2646                 :          0 :         return nlmsg_end(skb, nlh);
    2647                 :            : }
    2648                 :            : 
    2649                 :          0 : static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
    2650                 :          0 :                              struct xfrm_policy *xp)
    2651                 :            : {
    2652                 :            :         struct net *net = xs_net(x);
    2653                 :            :         struct sk_buff *skb;
    2654                 :            : 
    2655                 :            :         skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
    2656         [ #  # ]:          0 :         if (skb == NULL)
    2657                 :            :                 return -ENOMEM;
    2658                 :            : 
    2659         [ #  # ]:          0 :         if (build_acquire(skb, x, xt, xp) < 0)
    2660                 :          0 :                 BUG();
    2661                 :            : 
    2662                 :          0 :         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
    2663                 :            : }
    2664                 :            : 
    2665                 :            : /* User gives us xfrm_user_policy_info followed by an array of 0
    2666                 :            :  * or more templates.
    2667                 :            :  */
    2668                 :          0 : static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
    2669                 :            :                                                u8 *data, int len, int *dir)
    2670                 :            : {
    2671                 :            :         struct net *net = sock_net(sk);
    2672                 :          0 :         struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
    2673                 :          0 :         struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
    2674                 :            :         struct xfrm_policy *xp;
    2675                 :            :         int nr;
    2676                 :            : 
    2677      [ #  #  # ]:          0 :         switch (sk->sk_family) {
    2678                 :            :         case AF_INET:
    2679         [ #  # ]:          0 :                 if (opt != IP_XFRM_POLICY) {
    2680                 :          0 :                         *dir = -EOPNOTSUPP;
    2681                 :          0 :                         return NULL;
    2682                 :            :                 }
    2683                 :            :                 break;
    2684                 :            : #if IS_ENABLED(CONFIG_IPV6)
    2685                 :            :         case AF_INET6:
    2686         [ #  # ]:          0 :                 if (opt != IPV6_XFRM_POLICY) {
    2687                 :          0 :                         *dir = -EOPNOTSUPP;
    2688                 :          0 :                         return NULL;
    2689                 :            :                 }
    2690                 :            :                 break;
    2691                 :            : #endif
    2692                 :            :         default:
    2693                 :          0 :                 *dir = -EINVAL;
    2694                 :          0 :                 return NULL;
    2695                 :            :         }
    2696                 :            : 
    2697                 :          0 :         *dir = -EINVAL;
    2698                 :            : 
    2699   [ #  #  #  # ]:          0 :         if (len < sizeof(*p) ||
    2700                 :          0 :             verify_newpolicy_info(p))
    2701                 :            :                 return NULL;
    2702                 :            : 
    2703                 :          0 :         nr = ((len - sizeof(*p)) / sizeof(*ut));
    2704         [ #  # ]:          0 :         if (validate_tmpl(nr, ut, p->sel.family))
    2705                 :            :                 return NULL;
    2706                 :            : 
    2707         [ #  # ]:          0 :         if (p->dir > XFRM_POLICY_OUT)
    2708                 :            :                 return NULL;
    2709                 :            : 
    2710                 :          0 :         xp = xfrm_policy_alloc(net, GFP_ATOMIC);
    2711         [ #  # ]:          0 :         if (xp == NULL) {
    2712                 :          0 :                 *dir = -ENOBUFS;
    2713                 :          0 :                 return NULL;
    2714                 :            :         }
    2715                 :            : 
    2716                 :          0 :         copy_from_user_policy(xp, p);
    2717                 :          0 :         xp->type = XFRM_POLICY_TYPE_MAIN;
    2718                 :          0 :         copy_templates(xp, ut, nr);
    2719                 :            : 
    2720                 :          0 :         *dir = p->dir;
    2721                 :            : 
    2722                 :          0 :         return xp;
    2723                 :            : }
    2724                 :            : 
    2725                 :            : static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
    2726                 :            : {
    2727                 :          0 :         return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
    2728                 :          0 :                + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
    2729                 :          0 :                + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
    2730                 :            :                + nla_total_size(sizeof(struct xfrm_mark))
    2731                 :            :                + userpolicy_type_attrsize();
    2732                 :            : }
    2733                 :            : 
    2734                 :          0 : static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
    2735                 :            :                            int dir, const struct km_event *c)
    2736                 :            : {
    2737                 :            :         struct xfrm_user_polexpire *upe;
    2738                 :          0 :         int hard = c->data.hard;
    2739                 :            :         struct nlmsghdr *nlh;
    2740                 :            :         int err;
    2741                 :            : 
    2742                 :          0 :         nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
    2743         [ #  # ]:          0 :         if (nlh == NULL)
    2744                 :            :                 return -EMSGSIZE;
    2745                 :            : 
    2746                 :            :         upe = nlmsg_data(nlh);
    2747                 :          0 :         copy_to_user_policy(xp, &upe->pol, dir);
    2748                 :          0 :         err = copy_to_user_tmpl(xp, skb);
    2749         [ #  # ]:          0 :         if (!err)
    2750                 :            :                 err = copy_to_user_sec_ctx(xp, skb);
    2751         [ #  # ]:          0 :         if (!err)
    2752                 :            :                 err = copy_to_user_policy_type(xp->type, skb);
    2753         [ #  # ]:          0 :         if (!err)
    2754                 :          0 :                 err = xfrm_mark_put(skb, &xp->mark);
    2755         [ #  # ]:          0 :         if (err) {
    2756                 :            :                 nlmsg_cancel(skb, nlh);
    2757                 :            :                 return err;
    2758                 :            :         }
    2759                 :          0 :         upe->hard = !!hard;
    2760                 :            : 
    2761                 :            :         return nlmsg_end(skb, nlh);
    2762                 :            : }
    2763                 :            : 
    2764                 :          0 : static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
    2765                 :            : {
    2766                 :            :         struct net *net = xp_net(xp);
    2767                 :            :         struct sk_buff *skb;
    2768                 :            : 
    2769                 :            :         skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
    2770         [ #  # ]:          0 :         if (skb == NULL)
    2771                 :            :                 return -ENOMEM;
    2772                 :            : 
    2773         [ #  # ]:          0 :         if (build_polexpire(skb, xp, dir, c) < 0)
    2774                 :          0 :                 BUG();
    2775                 :            : 
    2776                 :          0 :         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
    2777                 :            : }
    2778                 :            : 
    2779                 :          0 : static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
    2780                 :            : {
    2781                 :          0 :         int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
    2782                 :            :         struct net *net = xp_net(xp);
    2783                 :            :         struct xfrm_userpolicy_info *p;
    2784                 :            :         struct xfrm_userpolicy_id *id;
    2785                 :            :         struct nlmsghdr *nlh;
    2786                 :            :         struct sk_buff *skb;
    2787                 :            :         int headlen, err;
    2788                 :            : 
    2789                 :            :         headlen = sizeof(*p);
    2790         [ #  # ]:          0 :         if (c->event == XFRM_MSG_DELPOLICY) {
    2791                 :          0 :                 len += nla_total_size(headlen);
    2792                 :            :                 headlen = sizeof(*id);
    2793                 :            :         }
    2794                 :            :         len += userpolicy_type_attrsize();
    2795                 :          0 :         len += nla_total_size(sizeof(struct xfrm_mark));
    2796                 :          0 :         len += NLMSG_ALIGN(headlen);
    2797                 :            : 
    2798                 :            :         skb = nlmsg_new(len, GFP_ATOMIC);
    2799         [ #  # ]:          0 :         if (skb == NULL)
    2800                 :            :                 return -ENOMEM;
    2801                 :            : 
    2802                 :          0 :         nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
    2803                 :            :         err = -EMSGSIZE;
    2804         [ #  # ]:          0 :         if (nlh == NULL)
    2805                 :            :                 goto out_free_skb;
    2806                 :            : 
    2807                 :          0 :         p = nlmsg_data(nlh);
    2808         [ #  # ]:          0 :         if (c->event == XFRM_MSG_DELPOLICY) {
    2809                 :            :                 struct nlattr *attr;
    2810                 :            : 
    2811                 :            :                 id = nlmsg_data(nlh);
    2812                 :          0 :                 memset(id, 0, sizeof(*id));
    2813                 :          0 :                 id->dir = dir;
    2814         [ #  # ]:          0 :                 if (c->data.byid)
    2815                 :          0 :                         id->index = xp->index;
    2816                 :            :                 else
    2817                 :          0 :                         memcpy(&id->sel, &xp->selector, sizeof(id->sel));
    2818                 :            : 
    2819                 :          0 :                 attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
    2820                 :            :                 err = -EMSGSIZE;
    2821         [ #  # ]:          0 :                 if (attr == NULL)
    2822                 :            :                         goto out_free_skb;
    2823                 :            : 
    2824                 :          0 :                 p = nla_data(attr);
    2825                 :            :         }
    2826                 :            : 
    2827                 :          0 :         copy_to_user_policy(xp, p, dir);
    2828                 :          0 :         err = copy_to_user_tmpl(xp, skb);
    2829         [ #  # ]:          0 :         if (!err)
    2830                 :            :                 err = copy_to_user_policy_type(xp->type, skb);
    2831         [ #  # ]:          0 :         if (!err)
    2832                 :          0 :                 err = xfrm_mark_put(skb, &xp->mark);
    2833         [ #  # ]:          0 :         if (err)
    2834                 :            :                 goto out_free_skb;
    2835                 :            : 
    2836                 :            :         nlmsg_end(skb, nlh);
    2837                 :            : 
    2838                 :          0 :         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
    2839                 :            : 
    2840                 :            : out_free_skb:
    2841                 :          0 :         kfree_skb(skb);
    2842                 :          0 :         return err;
    2843                 :            : }
    2844                 :            : 
    2845                 :          0 : static int xfrm_notify_policy_flush(const struct km_event *c)
    2846                 :            : {
    2847                 :          0 :         struct net *net = c->net;
    2848                 :            :         struct nlmsghdr *nlh;
    2849                 :            :         struct sk_buff *skb;
    2850                 :            :         int err;
    2851                 :            : 
    2852                 :            :         skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
    2853         [ #  # ]:          0 :         if (skb == NULL)
    2854                 :            :                 return -ENOMEM;
    2855                 :            : 
    2856                 :          0 :         nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
    2857                 :            :         err = -EMSGSIZE;
    2858         [ #  # ]:          0 :         if (nlh == NULL)
    2859                 :            :                 goto out_free_skb;
    2860                 :            :         err = copy_to_user_policy_type(c->data.type, skb);
    2861                 :            :         if (err)
    2862                 :            :                 goto out_free_skb;
    2863                 :            : 
    2864                 :            :         nlmsg_end(skb, nlh);
    2865                 :            : 
    2866                 :          0 :         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
    2867                 :            : 
    2868                 :            : out_free_skb:
    2869                 :          0 :         kfree_skb(skb);
    2870                 :          0 :         return err;
    2871                 :            : }
    2872                 :            : 
    2873                 :          0 : static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
    2874                 :            : {
    2875                 :            : 
    2876   [ #  #  #  # ]:          0 :         switch (c->event) {
    2877                 :            :         case XFRM_MSG_NEWPOLICY:
    2878                 :            :         case XFRM_MSG_UPDPOLICY:
    2879                 :            :         case XFRM_MSG_DELPOLICY:
    2880                 :          0 :                 return xfrm_notify_policy(xp, dir, c);
    2881                 :            :         case XFRM_MSG_FLUSHPOLICY:
    2882                 :          0 :                 return xfrm_notify_policy_flush(c);
    2883                 :            :         case XFRM_MSG_POLEXPIRE:
    2884                 :          0 :                 return xfrm_exp_policy_notify(xp, dir, c);
    2885                 :            :         default:
    2886                 :          0 :                 printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
    2887                 :            :                        c->event);
    2888                 :            :         }
    2889                 :            : 
    2890                 :          0 :         return 0;
    2891                 :            : 
    2892                 :            : }
    2893                 :            : 
    2894                 :            : static inline size_t xfrm_report_msgsize(void)
    2895                 :            : {
    2896                 :            :         return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
    2897                 :            : }
    2898                 :            : 
    2899                 :          0 : static int build_report(struct sk_buff *skb, u8 proto,
    2900                 :            :                         struct xfrm_selector *sel, xfrm_address_t *addr)
    2901                 :            : {
    2902                 :            :         struct xfrm_user_report *ur;
    2903                 :            :         struct nlmsghdr *nlh;
    2904                 :            : 
    2905                 :            :         nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
    2906         [ #  # ]:          0 :         if (nlh == NULL)
    2907                 :            :                 return -EMSGSIZE;
    2908                 :            : 
    2909                 :            :         ur = nlmsg_data(nlh);
    2910                 :          0 :         ur->proto = proto;
    2911                 :          0 :         memcpy(&ur->sel, sel, sizeof(ur->sel));
    2912                 :            : 
    2913         [ #  # ]:          0 :         if (addr) {
    2914                 :          0 :                 int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
    2915         [ #  # ]:          0 :                 if (err) {
    2916                 :            :                         nlmsg_cancel(skb, nlh);
    2917                 :          0 :                         return err;
    2918                 :            :                 }
    2919                 :            :         }
    2920                 :          0 :         return nlmsg_end(skb, nlh);
    2921                 :            : }
    2922                 :            : 
    2923                 :          0 : static int xfrm_send_report(struct net *net, u8 proto,
    2924                 :            :                             struct xfrm_selector *sel, xfrm_address_t *addr)
    2925                 :            : {
    2926                 :            :         struct sk_buff *skb;
    2927                 :            : 
    2928                 :            :         skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
    2929         [ #  # ]:          0 :         if (skb == NULL)
    2930                 :            :                 return -ENOMEM;
    2931                 :            : 
    2932         [ #  # ]:          0 :         if (build_report(skb, proto, sel, addr) < 0)
    2933                 :          0 :                 BUG();
    2934                 :            : 
    2935                 :          0 :         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
    2936                 :            : }
    2937                 :            : 
    2938                 :            : static inline size_t xfrm_mapping_msgsize(void)
    2939                 :            : {
    2940                 :            :         return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
    2941                 :            : }
    2942                 :            : 
    2943                 :          0 : static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
    2944                 :            :                          xfrm_address_t *new_saddr, __be16 new_sport)
    2945                 :            : {
    2946                 :            :         struct xfrm_user_mapping *um;
    2947                 :            :         struct nlmsghdr *nlh;
    2948                 :            : 
    2949                 :            :         nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
    2950         [ #  # ]:          0 :         if (nlh == NULL)
    2951                 :            :                 return -EMSGSIZE;
    2952                 :            : 
    2953                 :            :         um = nlmsg_data(nlh);
    2954                 :            : 
    2955                 :          0 :         memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
    2956                 :          0 :         um->id.spi = x->id.spi;
    2957                 :          0 :         um->id.family = x->props.family;
    2958                 :          0 :         um->id.proto = x->id.proto;
    2959                 :          0 :         memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
    2960                 :          0 :         memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
    2961                 :          0 :         um->new_sport = new_sport;
    2962                 :          0 :         um->old_sport = x->encap->encap_sport;
    2963                 :          0 :         um->reqid = x->props.reqid;
    2964                 :            : 
    2965                 :          0 :         return nlmsg_end(skb, nlh);
    2966                 :            : }
    2967                 :            : 
    2968                 :          0 : static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
    2969                 :            :                              __be16 sport)
    2970                 :            : {
    2971                 :            :         struct net *net = xs_net(x);
    2972                 :            :         struct sk_buff *skb;
    2973                 :            : 
    2974         [ #  # ]:          0 :         if (x->id.proto != IPPROTO_ESP)
    2975                 :            :                 return -EINVAL;
    2976                 :            : 
    2977         [ #  # ]:          0 :         if (!x->encap)
    2978                 :            :                 return -EINVAL;
    2979                 :            : 
    2980                 :            :         skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
    2981         [ #  # ]:          0 :         if (skb == NULL)
    2982                 :            :                 return -ENOMEM;
    2983                 :            : 
    2984         [ #  # ]:          0 :         if (build_mapping(skb, x, ipaddr, sport) < 0)
    2985                 :          0 :                 BUG();
    2986                 :            : 
    2987                 :          0 :         return nlmsg_multicast(net->xfrm.nlsk, skb, 0, XFRMNLGRP_MAPPING, GFP_ATOMIC);
    2988                 :            : }
    2989                 :            : 
    2990                 :            : static struct xfrm_mgr netlink_mgr = {
    2991                 :            :         .id             = "netlink",
    2992                 :            :         .notify         = xfrm_send_state_notify,
    2993                 :            :         .acquire        = xfrm_send_acquire,
    2994                 :            :         .compile_policy = xfrm_compile_policy,
    2995                 :            :         .notify_policy  = xfrm_send_policy_notify,
    2996                 :            :         .report         = xfrm_send_report,
    2997                 :            :         .migrate        = xfrm_send_migrate,
    2998                 :            :         .new_mapping    = xfrm_send_mapping,
    2999                 :            : };
    3000                 :            : 
    3001                 :          0 : static int __net_init xfrm_user_net_init(struct net *net)
    3002                 :            : {
    3003                 :            :         struct sock *nlsk;
    3004                 :          0 :         struct netlink_kernel_cfg cfg = {
    3005                 :            :                 .groups = XFRMNLGRP_MAX,
    3006                 :            :                 .input  = xfrm_netlink_rcv,
    3007                 :            :         };
    3008                 :            : 
    3009                 :            :         nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
    3010         [ #  # ]:          0 :         if (nlsk == NULL)
    3011                 :            :                 return -ENOMEM;
    3012                 :          0 :         net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
    3013                 :          0 :         rcu_assign_pointer(net->xfrm.nlsk, nlsk);
    3014                 :          0 :         return 0;
    3015                 :            : }
    3016                 :            : 
    3017                 :          0 : static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
    3018                 :            : {
    3019                 :            :         struct net *net;
    3020         [ #  # ]:          0 :         list_for_each_entry(net, net_exit_list, exit_list)
    3021                 :          0 :                 RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
    3022                 :          0 :         synchronize_net();
    3023         [ #  # ]:          0 :         list_for_each_entry(net, net_exit_list, exit_list)
    3024                 :          0 :                 netlink_kernel_release(net->xfrm.nlsk_stash);
    3025                 :          0 : }
    3026                 :            : 
    3027                 :            : static struct pernet_operations xfrm_user_net_ops = {
    3028                 :            :         .init       = xfrm_user_net_init,
    3029                 :            :         .exit_batch = xfrm_user_net_exit,
    3030                 :            : };
    3031                 :            : 
    3032                 :          0 : static int __init xfrm_user_init(void)
    3033                 :            : {
    3034                 :            :         int rv;
    3035                 :            : 
    3036                 :          0 :         printk(KERN_INFO "Initializing XFRM netlink socket\n");
    3037                 :            : 
    3038                 :          0 :         rv = register_pernet_subsys(&xfrm_user_net_ops);
    3039         [ #  # ]:          0 :         if (rv < 0)
    3040                 :            :                 return rv;
    3041                 :          0 :         rv = xfrm_register_km(&netlink_mgr);
    3042         [ #  # ]:          0 :         if (rv < 0)
    3043                 :          0 :                 unregister_pernet_subsys(&xfrm_user_net_ops);
    3044                 :          0 :         return rv;
    3045                 :            : }
    3046                 :            : 
    3047                 :          0 : static void __exit xfrm_user_exit(void)
    3048                 :            : {
    3049                 :          0 :         xfrm_unregister_km(&netlink_mgr);
    3050                 :          0 :         unregister_pernet_subsys(&xfrm_user_net_ops);
    3051                 :          0 : }
    3052                 :            : 
    3053                 :            : module_init(xfrm_user_init);
    3054                 :            : module_exit(xfrm_user_exit);
    3055                 :            : MODULE_LICENSE("GPL");
    3056                 :            : MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
    3057                 :            : 

Generated by: LCOV version 1.9