Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]

Diff markup

Differences between /linux/net/xfrm/xfrm_user.c (Version 2.6.25) and /linux/net/xfrm/xfrm_user.c (Version 2.6.31.13)


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