~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/sched/act_tunnel_key.c

Version: ~ [ linux-5.15-rc7 ] ~ [ linux-5.14.14 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.75 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.155 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.213 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.252 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.287 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.289 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Copyright (c) 2016, Amir Vadai <amir@vadai.me>
  3  * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
  4  *
  5  * This program is free software; you can redistribute it and/or modify
  6  * it under the terms of the GNU General Public License as published by
  7  * the Free Software Foundation; either version 2 of the License, or
  8  * (at your option) any later version.
  9  */
 10 
 11 #include <linux/module.h>
 12 #include <linux/init.h>
 13 #include <linux/kernel.h>
 14 #include <linux/skbuff.h>
 15 #include <linux/rtnetlink.h>
 16 #include <net/netlink.h>
 17 #include <net/pkt_sched.h>
 18 #include <net/dst.h>
 19 
 20 #include <linux/tc_act/tc_tunnel_key.h>
 21 #include <net/tc_act/tc_tunnel_key.h>
 22 
 23 static unsigned int tunnel_key_net_id;
 24 static struct tc_action_ops act_tunnel_key_ops;
 25 
 26 static int tunnel_key_act(struct sk_buff *skb, const struct tc_action *a,
 27                           struct tcf_result *res)
 28 {
 29         struct tcf_tunnel_key *t = to_tunnel_key(a);
 30         struct tcf_tunnel_key_params *params;
 31         int action;
 32 
 33         rcu_read_lock();
 34 
 35         params = rcu_dereference(t->params);
 36 
 37         tcf_lastuse_update(&t->tcf_tm);
 38         bstats_cpu_update(this_cpu_ptr(t->common.cpu_bstats), skb);
 39         action = READ_ONCE(t->tcf_action);
 40 
 41         switch (params->tcft_action) {
 42         case TCA_TUNNEL_KEY_ACT_RELEASE:
 43                 skb_dst_drop(skb);
 44                 break;
 45         case TCA_TUNNEL_KEY_ACT_SET:
 46                 skb_dst_drop(skb);
 47                 skb_dst_set(skb, dst_clone(&params->tcft_enc_metadata->dst));
 48                 break;
 49         default:
 50                 WARN_ONCE(1, "Bad tunnel_key action %d.\n",
 51                           params->tcft_action);
 52                 break;
 53         }
 54 
 55         rcu_read_unlock();
 56 
 57         return action;
 58 }
 59 
 60 static const struct nla_policy tunnel_key_policy[TCA_TUNNEL_KEY_MAX + 1] = {
 61         [TCA_TUNNEL_KEY_PARMS]      = { .len = sizeof(struct tc_tunnel_key) },
 62         [TCA_TUNNEL_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
 63         [TCA_TUNNEL_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
 64         [TCA_TUNNEL_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
 65         [TCA_TUNNEL_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
 66         [TCA_TUNNEL_KEY_ENC_KEY_ID]   = { .type = NLA_U32 },
 67         [TCA_TUNNEL_KEY_ENC_DST_PORT] = {.type = NLA_U16},
 68         [TCA_TUNNEL_KEY_NO_CSUM]      = { .type = NLA_U8 },
 69 };
 70 
 71 static int tunnel_key_init(struct net *net, struct nlattr *nla,
 72                            struct nlattr *est, struct tc_action **a,
 73                            int ovr, int bind, struct netlink_ext_ack *extack)
 74 {
 75         struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
 76         struct nlattr *tb[TCA_TUNNEL_KEY_MAX + 1];
 77         struct tcf_tunnel_key_params *params_old;
 78         struct tcf_tunnel_key_params *params_new;
 79         struct metadata_dst *metadata = NULL;
 80         struct tc_tunnel_key *parm;
 81         struct tcf_tunnel_key *t;
 82         bool exists = false;
 83         __be16 dst_port = 0;
 84         __be64 key_id;
 85         __be16 flags;
 86         int ret = 0;
 87         int err;
 88 
 89         if (!nla)
 90                 return -EINVAL;
 91 
 92         err = nla_parse_nested(tb, TCA_TUNNEL_KEY_MAX, nla, tunnel_key_policy,
 93                                NULL);
 94         if (err < 0)
 95                 return err;
 96 
 97         if (!tb[TCA_TUNNEL_KEY_PARMS])
 98                 return -EINVAL;
 99 
100         parm = nla_data(tb[TCA_TUNNEL_KEY_PARMS]);
101         exists = tcf_idr_check(tn, parm->index, a, bind);
102         if (exists && bind)
103                 return 0;
104 
105         switch (parm->t_action) {
106         case TCA_TUNNEL_KEY_ACT_RELEASE:
107                 break;
108         case TCA_TUNNEL_KEY_ACT_SET:
109                 if (!tb[TCA_TUNNEL_KEY_ENC_KEY_ID]) {
110                         ret = -EINVAL;
111                         goto err_out;
112                 }
113 
114                 key_id = key32_to_tunnel_id(nla_get_be32(tb[TCA_TUNNEL_KEY_ENC_KEY_ID]));
115 
116                 flags = TUNNEL_KEY | TUNNEL_CSUM;
117                 if (tb[TCA_TUNNEL_KEY_NO_CSUM] &&
118                     nla_get_u8(tb[TCA_TUNNEL_KEY_NO_CSUM]))
119                         flags &= ~TUNNEL_CSUM;
120 
121                 if (tb[TCA_TUNNEL_KEY_ENC_DST_PORT])
122                         dst_port = nla_get_be16(tb[TCA_TUNNEL_KEY_ENC_DST_PORT]);
123 
124                 if (tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC] &&
125                     tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]) {
126                         __be32 saddr;
127                         __be32 daddr;
128 
129                         saddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_SRC]);
130                         daddr = nla_get_in_addr(tb[TCA_TUNNEL_KEY_ENC_IPV4_DST]);
131 
132                         metadata = __ip_tun_set_dst(saddr, daddr, 0, 0,
133                                                     dst_port, flags,
134                                                     key_id, 0);
135                 } else if (tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC] &&
136                            tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]) {
137                         struct in6_addr saddr;
138                         struct in6_addr daddr;
139 
140                         saddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_SRC]);
141                         daddr = nla_get_in6_addr(tb[TCA_TUNNEL_KEY_ENC_IPV6_DST]);
142 
143                         metadata = __ipv6_tun_set_dst(&saddr, &daddr, 0, 0, dst_port,
144                                                       0, flags,
145                                                       key_id, 0);
146                 }
147 
148                 if (!metadata) {
149                         ret = -EINVAL;
150                         goto err_out;
151                 }
152 
153                 metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX;
154                 break;
155         default:
156                 ret = -EINVAL;
157                 goto err_out;
158         }
159 
160         if (!exists) {
161                 ret = tcf_idr_create(tn, parm->index, est, a,
162                                      &act_tunnel_key_ops, bind, true);
163                 if (ret)
164                         return ret;
165 
166                 ret = ACT_P_CREATED;
167         } else {
168                 tcf_idr_release(*a, bind);
169                 if (!ovr)
170                         return -EEXIST;
171         }
172 
173         t = to_tunnel_key(*a);
174 
175         ASSERT_RTNL();
176         params_new = kzalloc(sizeof(*params_new), GFP_KERNEL);
177         if (unlikely(!params_new)) {
178                 if (ret == ACT_P_CREATED)
179                         tcf_idr_release(*a, bind);
180                 return -ENOMEM;
181         }
182 
183         params_old = rtnl_dereference(t->params);
184 
185         t->tcf_action = parm->action;
186         params_new->tcft_action = parm->t_action;
187         params_new->tcft_enc_metadata = metadata;
188 
189         rcu_assign_pointer(t->params, params_new);
190 
191         if (params_old)
192                 kfree_rcu(params_old, rcu);
193 
194         if (ret == ACT_P_CREATED)
195                 tcf_idr_insert(tn, *a);
196 
197         return ret;
198 
199 err_out:
200         if (exists)
201                 tcf_idr_release(*a, bind);
202         return ret;
203 }
204 
205 static void tunnel_key_release(struct tc_action *a)
206 {
207         struct tcf_tunnel_key *t = to_tunnel_key(a);
208         struct tcf_tunnel_key_params *params;
209 
210         params = rcu_dereference_protected(t->params, 1);
211         if (params) {
212                 if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET)
213                         dst_release(&params->tcft_enc_metadata->dst);
214 
215                 kfree_rcu(params, rcu);
216         }
217 }
218 
219 static int tunnel_key_dump_addresses(struct sk_buff *skb,
220                                      const struct ip_tunnel_info *info)
221 {
222         unsigned short family = ip_tunnel_info_af(info);
223 
224         if (family == AF_INET) {
225                 __be32 saddr = info->key.u.ipv4.src;
226                 __be32 daddr = info->key.u.ipv4.dst;
227 
228                 if (!nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_SRC, saddr) &&
229                     !nla_put_in_addr(skb, TCA_TUNNEL_KEY_ENC_IPV4_DST, daddr))
230                         return 0;
231         }
232 
233         if (family == AF_INET6) {
234                 const struct in6_addr *saddr6 = &info->key.u.ipv6.src;
235                 const struct in6_addr *daddr6 = &info->key.u.ipv6.dst;
236 
237                 if (!nla_put_in6_addr(skb,
238                                       TCA_TUNNEL_KEY_ENC_IPV6_SRC, saddr6) &&
239                     !nla_put_in6_addr(skb,
240                                       TCA_TUNNEL_KEY_ENC_IPV6_DST, daddr6))
241                         return 0;
242         }
243 
244         return -EINVAL;
245 }
246 
247 static int tunnel_key_dump(struct sk_buff *skb, struct tc_action *a,
248                            int bind, int ref)
249 {
250         unsigned char *b = skb_tail_pointer(skb);
251         struct tcf_tunnel_key *t = to_tunnel_key(a);
252         struct tcf_tunnel_key_params *params;
253         struct tc_tunnel_key opt = {
254                 .index    = t->tcf_index,
255                 .refcnt   = t->tcf_refcnt - ref,
256                 .bindcnt  = t->tcf_bindcnt - bind,
257                 .action   = t->tcf_action,
258         };
259         struct tcf_t tm;
260 
261         params = rtnl_dereference(t->params);
262 
263         opt.t_action = params->tcft_action;
264 
265         if (nla_put(skb, TCA_TUNNEL_KEY_PARMS, sizeof(opt), &opt))
266                 goto nla_put_failure;
267 
268         if (params->tcft_action == TCA_TUNNEL_KEY_ACT_SET) {
269                 struct ip_tunnel_key *key =
270                         &params->tcft_enc_metadata->u.tun_info.key;
271                 __be32 key_id = tunnel_id_to_key32(key->tun_id);
272 
273                 if (nla_put_be32(skb, TCA_TUNNEL_KEY_ENC_KEY_ID, key_id) ||
274                     tunnel_key_dump_addresses(skb,
275                                               &params->tcft_enc_metadata->u.tun_info) ||
276                     nla_put_be16(skb, TCA_TUNNEL_KEY_ENC_DST_PORT, key->tp_dst) ||
277                     nla_put_u8(skb, TCA_TUNNEL_KEY_NO_CSUM,
278                                !(key->tun_flags & TUNNEL_CSUM)))
279                         goto nla_put_failure;
280         }
281 
282         tcf_tm_dump(&tm, &t->tcf_tm);
283         if (nla_put_64bit(skb, TCA_TUNNEL_KEY_TM, sizeof(tm),
284                           &tm, TCA_TUNNEL_KEY_PAD))
285                 goto nla_put_failure;
286 
287         return skb->len;
288 
289 nla_put_failure:
290         nlmsg_trim(skb, b);
291         return -1;
292 }
293 
294 static int tunnel_key_walker(struct net *net, struct sk_buff *skb,
295                              struct netlink_callback *cb, int type,
296                              const struct tc_action_ops *ops,
297                              struct netlink_ext_ack *extack)
298 {
299         struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
300 
301         return tcf_generic_walker(tn, skb, cb, type, ops, extack);
302 }
303 
304 static int tunnel_key_search(struct net *net, struct tc_action **a, u32 index,
305                              struct netlink_ext_ack *extack)
306 {
307         struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
308 
309         return tcf_idr_search(tn, a, index);
310 }
311 
312 static struct tc_action_ops act_tunnel_key_ops = {
313         .kind           =       "tunnel_key",
314         .type           =       TCA_ACT_TUNNEL_KEY,
315         .owner          =       THIS_MODULE,
316         .act            =       tunnel_key_act,
317         .dump           =       tunnel_key_dump,
318         .init           =       tunnel_key_init,
319         .cleanup        =       tunnel_key_release,
320         .walk           =       tunnel_key_walker,
321         .lookup         =       tunnel_key_search,
322         .size           =       sizeof(struct tcf_tunnel_key),
323 };
324 
325 static __net_init int tunnel_key_init_net(struct net *net)
326 {
327         struct tc_action_net *tn = net_generic(net, tunnel_key_net_id);
328 
329         return tc_action_net_init(tn, &act_tunnel_key_ops);
330 }
331 
332 static void __net_exit tunnel_key_exit_net(struct list_head *net_list)
333 {
334         tc_action_net_exit(net_list, tunnel_key_net_id);
335 }
336 
337 static struct pernet_operations tunnel_key_net_ops = {
338         .init = tunnel_key_init_net,
339         .exit_batch = tunnel_key_exit_net,
340         .id   = &tunnel_key_net_id,
341         .size = sizeof(struct tc_action_net),
342 };
343 
344 static int __init tunnel_key_init_module(void)
345 {
346         return tcf_register_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
347 }
348 
349 static void __exit tunnel_key_cleanup_module(void)
350 {
351         tcf_unregister_action(&act_tunnel_key_ops, &tunnel_key_net_ops);
352 }
353 
354 module_init(tunnel_key_init_module);
355 module_exit(tunnel_key_cleanup_module);
356 
357 MODULE_AUTHOR("Amir Vadai <amir@vadai.me>");
358 MODULE_DESCRIPTION("ip tunnel manipulation actions");
359 MODULE_LICENSE("GPL v2");
360 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp