Bluetooth: ISO: use hci_sync for setting CIG parameters
[platform/kernel/linux-starfive.git] / net / sched / act_pedit.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * net/sched/act_pedit.c        Generic packet editor
4  *
5  * Authors:     Jamal Hadi Salim (2002-4)
6  */
7
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/string.h>
11 #include <linux/errno.h>
12 #include <linux/skbuff.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/ip.h>
17 #include <linux/ipv6.h>
18 #include <linux/slab.h>
19 #include <net/ipv6.h>
20 #include <net/netlink.h>
21 #include <net/pkt_sched.h>
22 #include <linux/tc_act/tc_pedit.h>
23 #include <net/tc_act/tc_pedit.h>
24 #include <uapi/linux/tc_act/tc_pedit.h>
25 #include <net/pkt_cls.h>
26
27 static struct tc_action_ops act_pedit_ops;
28
29 static const struct nla_policy pedit_policy[TCA_PEDIT_MAX + 1] = {
30         [TCA_PEDIT_PARMS]       = { .len = sizeof(struct tc_pedit) },
31         [TCA_PEDIT_KEYS_EX]   = { .type = NLA_NESTED },
32 };
33
34 static const struct nla_policy pedit_key_ex_policy[TCA_PEDIT_KEY_EX_MAX + 1] = {
35         [TCA_PEDIT_KEY_EX_HTYPE]  = { .type = NLA_U16 },
36         [TCA_PEDIT_KEY_EX_CMD]    = { .type = NLA_U16 },
37 };
38
39 static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla,
40                                                         u8 n)
41 {
42         struct tcf_pedit_key_ex *keys_ex;
43         struct tcf_pedit_key_ex *k;
44         const struct nlattr *ka;
45         int err = -EINVAL;
46         int rem;
47
48         if (!nla)
49                 return NULL;
50
51         keys_ex = kcalloc(n, sizeof(*k), GFP_KERNEL);
52         if (!keys_ex)
53                 return ERR_PTR(-ENOMEM);
54
55         k = keys_ex;
56
57         nla_for_each_nested(ka, nla, rem) {
58                 struct nlattr *tb[TCA_PEDIT_KEY_EX_MAX + 1];
59
60                 if (!n) {
61                         err = -EINVAL;
62                         goto err_out;
63                 }
64                 n--;
65
66                 if (nla_type(ka) != TCA_PEDIT_KEY_EX) {
67                         err = -EINVAL;
68                         goto err_out;
69                 }
70
71                 err = nla_parse_nested_deprecated(tb, TCA_PEDIT_KEY_EX_MAX,
72                                                   ka, pedit_key_ex_policy,
73                                                   NULL);
74                 if (err)
75                         goto err_out;
76
77                 if (!tb[TCA_PEDIT_KEY_EX_HTYPE] ||
78                     !tb[TCA_PEDIT_KEY_EX_CMD]) {
79                         err = -EINVAL;
80                         goto err_out;
81                 }
82
83                 k->htype = nla_get_u16(tb[TCA_PEDIT_KEY_EX_HTYPE]);
84                 k->cmd = nla_get_u16(tb[TCA_PEDIT_KEY_EX_CMD]);
85
86                 if (k->htype > TCA_PEDIT_HDR_TYPE_MAX ||
87                     k->cmd > TCA_PEDIT_CMD_MAX) {
88                         err = -EINVAL;
89                         goto err_out;
90                 }
91
92                 k++;
93         }
94
95         if (n) {
96                 err = -EINVAL;
97                 goto err_out;
98         }
99
100         return keys_ex;
101
102 err_out:
103         kfree(keys_ex);
104         return ERR_PTR(err);
105 }
106
107 static int tcf_pedit_key_ex_dump(struct sk_buff *skb,
108                                  struct tcf_pedit_key_ex *keys_ex, int n)
109 {
110         struct nlattr *keys_start = nla_nest_start_noflag(skb,
111                                                           TCA_PEDIT_KEYS_EX);
112
113         if (!keys_start)
114                 goto nla_failure;
115         for (; n > 0; n--) {
116                 struct nlattr *key_start;
117
118                 key_start = nla_nest_start_noflag(skb, TCA_PEDIT_KEY_EX);
119                 if (!key_start)
120                         goto nla_failure;
121
122                 if (nla_put_u16(skb, TCA_PEDIT_KEY_EX_HTYPE, keys_ex->htype) ||
123                     nla_put_u16(skb, TCA_PEDIT_KEY_EX_CMD, keys_ex->cmd))
124                         goto nla_failure;
125
126                 nla_nest_end(skb, key_start);
127
128                 keys_ex++;
129         }
130
131         nla_nest_end(skb, keys_start);
132
133         return 0;
134 nla_failure:
135         nla_nest_cancel(skb, keys_start);
136         return -EINVAL;
137 }
138
139 static void tcf_pedit_cleanup_rcu(struct rcu_head *head)
140 {
141         struct tcf_pedit_parms *parms =
142                 container_of(head, struct tcf_pedit_parms, rcu);
143
144         kfree(parms->tcfp_keys_ex);
145         kfree(parms->tcfp_keys);
146
147         kfree(parms);
148 }
149
150 static int tcf_pedit_init(struct net *net, struct nlattr *nla,
151                           struct nlattr *est, struct tc_action **a,
152                           struct tcf_proto *tp, u32 flags,
153                           struct netlink_ext_ack *extack)
154 {
155         struct tc_action_net *tn = net_generic(net, act_pedit_ops.net_id);
156         bool bind = flags & TCA_ACT_FLAGS_BIND;
157         struct tcf_chain *goto_ch = NULL;
158         struct tcf_pedit_parms *oparms, *nparms;
159         struct nlattr *tb[TCA_PEDIT_MAX + 1];
160         struct tc_pedit *parm;
161         struct nlattr *pattr;
162         struct tcf_pedit *p;
163         int ret = 0, err;
164         int i, ksize;
165         u32 index;
166
167         if (!nla) {
168                 NL_SET_ERR_MSG_MOD(extack, "Pedit requires attributes to be passed");
169                 return -EINVAL;
170         }
171
172         err = nla_parse_nested_deprecated(tb, TCA_PEDIT_MAX, nla,
173                                           pedit_policy, NULL);
174         if (err < 0)
175                 return err;
176
177         pattr = tb[TCA_PEDIT_PARMS];
178         if (!pattr)
179                 pattr = tb[TCA_PEDIT_PARMS_EX];
180         if (!pattr) {
181                 NL_SET_ERR_MSG_MOD(extack, "Missing required TCA_PEDIT_PARMS or TCA_PEDIT_PARMS_EX pedit attribute");
182                 return -EINVAL;
183         }
184
185         parm = nla_data(pattr);
186
187         index = parm->index;
188         err = tcf_idr_check_alloc(tn, &index, a, bind);
189         if (!err) {
190                 ret = tcf_idr_create_from_flags(tn, index, est, a,
191                                                 &act_pedit_ops, bind, flags);
192                 if (ret) {
193                         tcf_idr_cleanup(tn, index);
194                         return ret;
195                 }
196                 ret = ACT_P_CREATED;
197         } else if (err > 0) {
198                 if (bind)
199                         return 0;
200                 if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
201                         ret = -EEXIST;
202                         goto out_release;
203                 }
204         } else {
205                 return err;
206         }
207
208         if (!parm->nkeys) {
209                 NL_SET_ERR_MSG_MOD(extack, "Pedit requires keys to be passed");
210                 ret = -EINVAL;
211                 goto out_release;
212         }
213         ksize = parm->nkeys * sizeof(struct tc_pedit_key);
214         if (nla_len(pattr) < sizeof(*parm) + ksize) {
215                 NL_SET_ERR_MSG_ATTR(extack, pattr, "Length of TCA_PEDIT_PARMS or TCA_PEDIT_PARMS_EX pedit attribute is invalid");
216                 ret = -EINVAL;
217                 goto out_release;
218         }
219
220         nparms = kzalloc(sizeof(*nparms), GFP_KERNEL);
221         if (!nparms) {
222                 ret = -ENOMEM;
223                 goto out_release;
224         }
225
226         nparms->tcfp_keys_ex =
227                 tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys);
228         if (IS_ERR(nparms->tcfp_keys_ex)) {
229                 ret = PTR_ERR(nparms->tcfp_keys_ex);
230                 goto out_free;
231         }
232
233         err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
234         if (err < 0) {
235                 ret = err;
236                 goto out_free_ex;
237         }
238
239         nparms->tcfp_off_max_hint = 0;
240         nparms->tcfp_flags = parm->flags;
241         nparms->tcfp_nkeys = parm->nkeys;
242
243         nparms->tcfp_keys = kmalloc(ksize, GFP_KERNEL);
244         if (!nparms->tcfp_keys) {
245                 ret = -ENOMEM;
246                 goto put_chain;
247         }
248
249         memcpy(nparms->tcfp_keys, parm->keys, ksize);
250
251         for (i = 0; i < nparms->tcfp_nkeys; ++i) {
252                 u32 cur = nparms->tcfp_keys[i].off;
253
254                 /* sanitize the shift value for any later use */
255                 nparms->tcfp_keys[i].shift = min_t(size_t,
256                                                    BITS_PER_TYPE(int) - 1,
257                                                    nparms->tcfp_keys[i].shift);
258
259                 /* The AT option can read a single byte, we can bound the actual
260                  * value with uchar max.
261                  */
262                 cur += (0xff & nparms->tcfp_keys[i].offmask) >> nparms->tcfp_keys[i].shift;
263
264                 /* Each key touches 4 bytes starting from the computed offset */
265                 nparms->tcfp_off_max_hint =
266                         max(nparms->tcfp_off_max_hint, cur + 4);
267         }
268
269         p = to_pedit(*a);
270
271         spin_lock_bh(&p->tcf_lock);
272         goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
273         oparms = rcu_replace_pointer(p->parms, nparms, 1);
274         spin_unlock_bh(&p->tcf_lock);
275
276         if (oparms)
277                 call_rcu(&oparms->rcu, tcf_pedit_cleanup_rcu);
278
279         if (goto_ch)
280                 tcf_chain_put_by_act(goto_ch);
281
282         return ret;
283
284 put_chain:
285         if (goto_ch)
286                 tcf_chain_put_by_act(goto_ch);
287 out_free_ex:
288         kfree(nparms->tcfp_keys_ex);
289 out_free:
290         kfree(nparms);
291 out_release:
292         tcf_idr_release(*a, bind);
293         return ret;
294 }
295
296 static void tcf_pedit_cleanup(struct tc_action *a)
297 {
298         struct tcf_pedit *p = to_pedit(a);
299         struct tcf_pedit_parms *parms;
300
301         parms = rcu_dereference_protected(p->parms, 1);
302
303         if (parms)
304                 call_rcu(&parms->rcu, tcf_pedit_cleanup_rcu);
305 }
306
307 static bool offset_valid(struct sk_buff *skb, int offset)
308 {
309         if (offset > 0 && offset > skb->len)
310                 return false;
311
312         if  (offset < 0 && -offset > skb_headroom(skb))
313                 return false;
314
315         return true;
316 }
317
318 static int pedit_l4_skb_offset(struct sk_buff *skb, int *hoffset, const int header_type)
319 {
320         const int noff = skb_network_offset(skb);
321         int ret = -EINVAL;
322         struct iphdr _iph;
323
324         switch (skb->protocol) {
325         case htons(ETH_P_IP): {
326                 const struct iphdr *iph = skb_header_pointer(skb, noff, sizeof(_iph), &_iph);
327
328                 if (!iph)
329                         goto out;
330                 *hoffset = noff + iph->ihl * 4;
331                 ret = 0;
332                 break;
333         }
334         case htons(ETH_P_IPV6):
335                 ret = ipv6_find_hdr(skb, hoffset, header_type, NULL, NULL) == header_type ? 0 : -EINVAL;
336                 break;
337         }
338 out:
339         return ret;
340 }
341
342 static int pedit_skb_hdr_offset(struct sk_buff *skb,
343                                  enum pedit_header_type htype, int *hoffset)
344 {
345         int ret = -EINVAL;
346         /* 'htype' is validated in the netlink parsing */
347         switch (htype) {
348         case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
349                 if (skb_mac_header_was_set(skb)) {
350                         *hoffset = skb_mac_offset(skb);
351                         ret = 0;
352                 }
353                 break;
354         case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK:
355         case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
356         case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
357                 *hoffset = skb_network_offset(skb);
358                 ret = 0;
359                 break;
360         case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
361                 ret = pedit_l4_skb_offset(skb, hoffset, IPPROTO_TCP);
362                 break;
363         case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
364                 ret = pedit_l4_skb_offset(skb, hoffset, IPPROTO_UDP);
365                 break;
366         default:
367                 break;
368         }
369         return ret;
370 }
371
372 static int tcf_pedit_act(struct sk_buff *skb, const struct tc_action *a,
373                          struct tcf_result *res)
374 {
375         enum pedit_header_type htype = TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK;
376         enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET;
377         struct tcf_pedit *p = to_pedit(a);
378         struct tcf_pedit_key_ex *tkey_ex;
379         struct tcf_pedit_parms *parms;
380         struct tc_pedit_key *tkey;
381         u32 max_offset;
382         int i;
383
384         parms = rcu_dereference_bh(p->parms);
385
386         max_offset = (skb_transport_header_was_set(skb) ?
387                       skb_transport_offset(skb) :
388                       skb_network_offset(skb)) +
389                      parms->tcfp_off_max_hint;
390         if (skb_ensure_writable(skb, min(skb->len, max_offset)))
391                 goto done;
392
393         tcf_lastuse_update(&p->tcf_tm);
394         tcf_action_update_bstats(&p->common, skb);
395
396         tkey = parms->tcfp_keys;
397         tkey_ex = parms->tcfp_keys_ex;
398
399         for (i = parms->tcfp_nkeys; i > 0; i--, tkey++) {
400                 int offset = tkey->off;
401                 int hoffset = 0;
402                 u32 *ptr, hdata;
403                 u32 val;
404                 int rc;
405
406                 if (tkey_ex) {
407                         htype = tkey_ex->htype;
408                         cmd = tkey_ex->cmd;
409
410                         tkey_ex++;
411                 }
412
413                 rc = pedit_skb_hdr_offset(skb, htype, &hoffset);
414                 if (rc) {
415                         pr_info_ratelimited("tc action pedit unable to extract header offset for header type (0x%x)\n", htype);
416                         goto bad;
417                 }
418
419                 if (tkey->offmask) {
420                         u8 *d, _d;
421
422                         if (!offset_valid(skb, hoffset + tkey->at)) {
423                                 pr_info("tc action pedit 'at' offset %d out of bounds\n",
424                                         hoffset + tkey->at);
425                                 goto bad;
426                         }
427                         d = skb_header_pointer(skb, hoffset + tkey->at,
428                                                sizeof(_d), &_d);
429                         if (!d)
430                                 goto bad;
431                         offset += (*d & tkey->offmask) >> tkey->shift;
432                 }
433
434                 if (offset % 4) {
435                         pr_info("tc action pedit offset must be on 32 bit boundaries\n");
436                         goto bad;
437                 }
438
439                 if (!offset_valid(skb, hoffset + offset)) {
440                         pr_info("tc action pedit offset %d out of bounds\n",
441                                 hoffset + offset);
442                         goto bad;
443                 }
444
445                 ptr = skb_header_pointer(skb, hoffset + offset,
446                                          sizeof(hdata), &hdata);
447                 if (!ptr)
448                         goto bad;
449                 /* just do it, baby */
450                 switch (cmd) {
451                 case TCA_PEDIT_KEY_EX_CMD_SET:
452                         val = tkey->val;
453                         break;
454                 case TCA_PEDIT_KEY_EX_CMD_ADD:
455                         val = (*ptr + tkey->val) & ~tkey->mask;
456                         break;
457                 default:
458                         pr_info("tc action pedit bad command (%d)\n",
459                                 cmd);
460                         goto bad;
461                 }
462
463                 *ptr = ((*ptr & tkey->mask) ^ val);
464                 if (ptr == &hdata)
465                         skb_store_bits(skb, hoffset + offset, ptr, 4);
466         }
467
468         goto done;
469
470 bad:
471         spin_lock(&p->tcf_lock);
472         p->tcf_qstats.overlimits++;
473         spin_unlock(&p->tcf_lock);
474 done:
475         return p->tcf_action;
476 }
477
478 static void tcf_pedit_stats_update(struct tc_action *a, u64 bytes, u64 packets,
479                                    u64 drops, u64 lastuse, bool hw)
480 {
481         struct tcf_pedit *d = to_pedit(a);
482         struct tcf_t *tm = &d->tcf_tm;
483
484         tcf_action_update_stats(a, bytes, packets, drops, hw);
485         tm->lastuse = max_t(u64, tm->lastuse, lastuse);
486 }
487
488 static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a,
489                           int bind, int ref)
490 {
491         unsigned char *b = skb_tail_pointer(skb);
492         struct tcf_pedit *p = to_pedit(a);
493         struct tcf_pedit_parms *parms;
494         struct tc_pedit *opt;
495         struct tcf_t t;
496         int s;
497
498         spin_lock_bh(&p->tcf_lock);
499         parms = rcu_dereference_protected(p->parms, 1);
500         s = struct_size(opt, keys, parms->tcfp_nkeys);
501
502         opt = kzalloc(s, GFP_ATOMIC);
503         if (unlikely(!opt)) {
504                 spin_unlock_bh(&p->tcf_lock);
505                 return -ENOBUFS;
506         }
507
508         memcpy(opt->keys, parms->tcfp_keys,
509                flex_array_size(opt, keys, parms->tcfp_nkeys));
510         opt->index = p->tcf_index;
511         opt->nkeys = parms->tcfp_nkeys;
512         opt->flags = parms->tcfp_flags;
513         opt->action = p->tcf_action;
514         opt->refcnt = refcount_read(&p->tcf_refcnt) - ref;
515         opt->bindcnt = atomic_read(&p->tcf_bindcnt) - bind;
516
517         if (parms->tcfp_keys_ex) {
518                 if (tcf_pedit_key_ex_dump(skb, parms->tcfp_keys_ex,
519                                           parms->tcfp_nkeys))
520                         goto nla_put_failure;
521
522                 if (nla_put(skb, TCA_PEDIT_PARMS_EX, s, opt))
523                         goto nla_put_failure;
524         } else {
525                 if (nla_put(skb, TCA_PEDIT_PARMS, s, opt))
526                         goto nla_put_failure;
527         }
528
529         tcf_tm_dump(&t, &p->tcf_tm);
530         if (nla_put_64bit(skb, TCA_PEDIT_TM, sizeof(t), &t, TCA_PEDIT_PAD))
531                 goto nla_put_failure;
532         spin_unlock_bh(&p->tcf_lock);
533
534         kfree(opt);
535         return skb->len;
536
537 nla_put_failure:
538         spin_unlock_bh(&p->tcf_lock);
539         nlmsg_trim(skb, b);
540         kfree(opt);
541         return -1;
542 }
543
544 static int tcf_pedit_offload_act_setup(struct tc_action *act, void *entry_data,
545                                        u32 *index_inc, bool bind,
546                                        struct netlink_ext_ack *extack)
547 {
548         if (bind) {
549                 struct flow_action_entry *entry = entry_data;
550                 int k;
551
552                 for (k = 0; k < tcf_pedit_nkeys(act); k++) {
553                         switch (tcf_pedit_cmd(act, k)) {
554                         case TCA_PEDIT_KEY_EX_CMD_SET:
555                                 entry->id = FLOW_ACTION_MANGLE;
556                                 break;
557                         case TCA_PEDIT_KEY_EX_CMD_ADD:
558                                 entry->id = FLOW_ACTION_ADD;
559                                 break;
560                         default:
561                                 NL_SET_ERR_MSG_MOD(extack, "Unsupported pedit command offload");
562                                 return -EOPNOTSUPP;
563                         }
564                         entry->mangle.htype = tcf_pedit_htype(act, k);
565                         entry->mangle.mask = tcf_pedit_mask(act, k);
566                         entry->mangle.val = tcf_pedit_val(act, k);
567                         entry->mangle.offset = tcf_pedit_offset(act, k);
568                         entry->hw_stats = tc_act_hw_stats(act->hw_stats);
569                         entry++;
570                 }
571                 *index_inc = k;
572         } else {
573                 return -EOPNOTSUPP;
574         }
575
576         return 0;
577 }
578
579 static struct tc_action_ops act_pedit_ops = {
580         .kind           =       "pedit",
581         .id             =       TCA_ID_PEDIT,
582         .owner          =       THIS_MODULE,
583         .act            =       tcf_pedit_act,
584         .stats_update   =       tcf_pedit_stats_update,
585         .dump           =       tcf_pedit_dump,
586         .cleanup        =       tcf_pedit_cleanup,
587         .init           =       tcf_pedit_init,
588         .offload_act_setup =    tcf_pedit_offload_act_setup,
589         .size           =       sizeof(struct tcf_pedit),
590 };
591
592 static __net_init int pedit_init_net(struct net *net)
593 {
594         struct tc_action_net *tn = net_generic(net, act_pedit_ops.net_id);
595
596         return tc_action_net_init(net, tn, &act_pedit_ops);
597 }
598
599 static void __net_exit pedit_exit_net(struct list_head *net_list)
600 {
601         tc_action_net_exit(net_list, act_pedit_ops.net_id);
602 }
603
604 static struct pernet_operations pedit_net_ops = {
605         .init = pedit_init_net,
606         .exit_batch = pedit_exit_net,
607         .id   = &act_pedit_ops.net_id,
608         .size = sizeof(struct tc_action_net),
609 };
610
611 MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
612 MODULE_DESCRIPTION("Generic Packet Editor actions");
613 MODULE_LICENSE("GPL");
614
615 static int __init pedit_init_module(void)
616 {
617         return tcf_register_action(&act_pedit_ops, &pedit_net_ops);
618 }
619
620 static void __exit pedit_cleanup_module(void)
621 {
622         tcf_unregister_action(&act_pedit_ops, &pedit_net_ops);
623 }
624
625 module_init(pedit_init_module);
626 module_exit(pedit_cleanup_module);