1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Checksum updating actions
4 *
5 * Copyright (c) 2010 Gregoire Baron <baronchon@n7mm.org>
6 */
7
8 #include <linux/types.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/spinlock.h>
13
14 #include <linux/netlink.h>
15 #include <net/netlink.h>
16 #include <linux/rtnetlink.h>
17
18 #include <linux/skbuff.h>
19
20 #include <net/ip.h>
21 #include <net/ipv6.h>
22 #include <net/icmp.h>
23 #include <linux/icmpv6.h>
24 #include <linux/igmp.h>
25 #include <net/tcp.h>
26 #include <net/udp.h>
27 #include <net/ip6_checksum.h>
28 #include <net/sctp/checksum.h>
29
30 #include <net/act_api.h>
31 #include <net/pkt_cls.h>
32
33 #include <linux/tc_act/tc_csum.h>
34 #include <net/tc_act/tc_csum.h>
35 #include <net/tc_wrapper.h>
36
37 static const struct nla_policy csum_policy[TCA_CSUM_MAX + 1] = {
38 [TCA_CSUM_PARMS] = { .len = sizeof(struct tc_csum), },
39 };
40
41 static struct tc_action_ops act_csum_ops;
42
tcf_csum_init(struct net * net,struct nlattr * nla,struct nlattr * est,struct tc_action ** a,struct tcf_proto * tp,u32 flags,struct netlink_ext_ack * extack)43 static int tcf_csum_init(struct net *net, struct nlattr *nla,
44 struct nlattr *est, struct tc_action **a,
45 struct tcf_proto *tp,
46 u32 flags, struct netlink_ext_ack *extack)
47 {
48 struct tc_action_net *tn = net_generic(net, act_csum_ops.net_id);
49 bool bind = flags & TCA_ACT_FLAGS_BIND;
50 struct tcf_csum_params *params_new;
51 struct nlattr *tb[TCA_CSUM_MAX + 1];
52 struct tcf_chain *goto_ch = NULL;
53 struct tc_csum *parm;
54 struct tcf_csum *p;
55 int ret = 0, err;
56 u32 index;
57
58 if (nla == NULL)
59 return -EINVAL;
60
61 err = nla_parse_nested_deprecated(tb, TCA_CSUM_MAX, nla, csum_policy,
62 NULL);
63 if (err < 0)
64 return err;
65
66 if (tb[TCA_CSUM_PARMS] == NULL)
67 return -EINVAL;
68 parm = nla_data(tb[TCA_CSUM_PARMS]);
69 index = parm->index;
70 err = tcf_idr_check_alloc(tn, &index, a, bind);
71 if (!err) {
72 ret = tcf_idr_create_from_flags(tn, index, est, a,
73 &act_csum_ops, bind, flags);
74 if (ret) {
75 tcf_idr_cleanup(tn, index);
76 return ret;
77 }
78 ret = ACT_P_CREATED;
79 } else if (err > 0) {
80 if (bind) /* dont override defaults */
81 return ACT_P_BOUND;
82 if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
83 tcf_idr_release(*a, bind);
84 return -EEXIST;
85 }
86 } else {
87 return err;
88 }
89
90 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
91 if (err < 0)
92 goto release_idr;
93
94 p = to_tcf_csum(*a);
95
96 params_new = kzalloc_obj(*params_new);
97 if (unlikely(!params_new)) {
98 err = -ENOMEM;
99 goto put_chain;
100 }
101 params_new->update_flags = parm->update_flags;
102 params_new->action = parm->action;
103
104 spin_lock_bh(&p->tcf_lock);
105 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
106 params_new = rcu_replace_pointer(p->params, params_new,
107 lockdep_is_held(&p->tcf_lock));
108 spin_unlock_bh(&p->tcf_lock);
109
110 if (goto_ch)
111 tcf_chain_put_by_act(goto_ch);
112 if (params_new)
113 kfree_rcu(params_new, rcu);
114
115 return ret;
116 put_chain:
117 if (goto_ch)
118 tcf_chain_put_by_act(goto_ch);
119 release_idr:
120 tcf_idr_release(*a, bind);
121 return err;
122 }
123
124 /**
125 * tcf_csum_skb_nextlayer - Get next layer pointer
126 * @skb: sk_buff to use
127 * @ihl: previous summed headers length
128 * @ipl: complete packet length
129 * @jhl: next header length
130 *
131 * Check the expected next layer availability in the specified sk_buff.
132 * Return the next layer pointer if pass, NULL otherwise.
133 */
tcf_csum_skb_nextlayer(struct sk_buff * skb,unsigned int ihl,unsigned int ipl,unsigned int jhl)134 static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
135 unsigned int ihl, unsigned int ipl,
136 unsigned int jhl)
137 {
138 int ntkoff = skb_network_offset(skb);
139 int hl = ihl + jhl;
140
141 if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
142 skb_try_make_writable(skb, hl + ntkoff))
143 return NULL;
144 else
145 return (void *)(skb_network_header(skb) + ihl);
146 }
147
tcf_csum_ipv4_icmp(struct sk_buff * skb,unsigned int ihl,unsigned int ipl)148 static int tcf_csum_ipv4_icmp(struct sk_buff *skb, unsigned int ihl,
149 unsigned int ipl)
150 {
151 struct icmphdr *icmph;
152
153 icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
154 if (icmph == NULL)
155 return 0;
156
157 icmph->checksum = 0;
158 skb->csum = csum_partial(icmph, ipl - ihl, 0);
159 icmph->checksum = csum_fold(skb->csum);
160
161 skb->ip_summed = CHECKSUM_NONE;
162
163 return 1;
164 }
165
tcf_csum_ipv4_igmp(struct sk_buff * skb,unsigned int ihl,unsigned int ipl)166 static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
167 unsigned int ihl, unsigned int ipl)
168 {
169 struct igmphdr *igmph;
170
171 igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
172 if (igmph == NULL)
173 return 0;
174
175 igmph->csum = 0;
176 skb->csum = csum_partial(igmph, ipl - ihl, 0);
177 igmph->csum = csum_fold(skb->csum);
178
179 skb->ip_summed = CHECKSUM_NONE;
180
181 return 1;
182 }
183
tcf_csum_ipv6_icmp(struct sk_buff * skb,unsigned int ihl,unsigned int ipl)184 static int tcf_csum_ipv6_icmp(struct sk_buff *skb, unsigned int ihl,
185 unsigned int ipl)
186 {
187 struct icmp6hdr *icmp6h;
188 const struct ipv6hdr *ip6h;
189
190 icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
191 if (icmp6h == NULL)
192 return 0;
193
194 ip6h = ipv6_hdr(skb);
195 icmp6h->icmp6_cksum = 0;
196 skb->csum = csum_partial(icmp6h, ipl - ihl, 0);
197 icmp6h->icmp6_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
198 ipl - ihl, IPPROTO_ICMPV6,
199 skb->csum);
200
201 skb->ip_summed = CHECKSUM_NONE;
202
203 return 1;
204 }
205
tcf_csum_ipv4_tcp(struct sk_buff * skb,unsigned int ihl,unsigned int ipl)206 static int tcf_csum_ipv4_tcp(struct sk_buff *skb, unsigned int ihl,
207 unsigned int ipl)
208 {
209 struct tcphdr *tcph;
210 const struct iphdr *iph;
211
212 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
213 return 1;
214
215 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
216 if (tcph == NULL)
217 return 0;
218
219 iph = ip_hdr(skb);
220 tcph->check = 0;
221 skb->csum = csum_partial(tcph, ipl - ihl, 0);
222 tcph->check = tcp_v4_check(ipl - ihl,
223 iph->saddr, iph->daddr, skb->csum);
224
225 skb->ip_summed = CHECKSUM_NONE;
226
227 return 1;
228 }
229
tcf_csum_ipv6_tcp(struct sk_buff * skb,unsigned int ihl,unsigned int ipl)230 static int tcf_csum_ipv6_tcp(struct sk_buff *skb, unsigned int ihl,
231 unsigned int ipl)
232 {
233 struct tcphdr *tcph;
234 const struct ipv6hdr *ip6h;
235
236 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
237 return 1;
238
239 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
240 if (tcph == NULL)
241 return 0;
242
243 ip6h = ipv6_hdr(skb);
244 tcph->check = 0;
245 skb->csum = csum_partial(tcph, ipl - ihl, 0);
246 tcph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
247 ipl - ihl, IPPROTO_TCP,
248 skb->csum);
249
250 skb->ip_summed = CHECKSUM_NONE;
251
252 return 1;
253 }
254
tcf_csum_ipv4_udp(struct sk_buff * skb,unsigned int ihl,unsigned int ipl,int udplite)255 static int tcf_csum_ipv4_udp(struct sk_buff *skb, unsigned int ihl,
256 unsigned int ipl, int udplite)
257 {
258 struct udphdr *udph;
259 const struct iphdr *iph;
260 u16 ul;
261
262 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type &
263 (SKB_GSO_UDP | SKB_GSO_UDP_L4 |
264 SKB_GSO_UDP_TUNNEL | SKB_GSO_UDP_TUNNEL_CSUM))
265 return 1;
266
267 /*
268 * Support both UDP and UDPLITE checksum algorithms, Don't use
269 * udph->len to get the real length without any protocol check,
270 * UDPLITE uses udph->len for another thing,
271 * Use iph->tot_len, or just ipl.
272 */
273
274 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
275 if (udph == NULL)
276 return 0;
277
278 iph = ip_hdr(skb);
279 ul = udp_get_len_short(udph);
280
281 if (udplite || udph->check) {
282
283 udph->check = 0;
284
285 if (udplite) {
286 if (ul == 0)
287 skb->csum = csum_partial(udph, ipl - ihl, 0);
288 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
289 skb->csum = csum_partial(udph, ul, 0);
290 else
291 goto ignore_obscure_skb;
292 } else {
293 if (ul != ipl - ihl)
294 goto ignore_obscure_skb;
295
296 skb->csum = csum_partial(udph, ul, 0);
297 }
298
299 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
300 ul, iph->protocol,
301 skb->csum);
302
303 if (!udph->check)
304 udph->check = CSUM_MANGLED_0;
305 }
306
307 skb->ip_summed = CHECKSUM_NONE;
308
309 ignore_obscure_skb:
310 return 1;
311 }
312
tcf_csum_ipv6_udp(struct sk_buff * skb,unsigned int ihl,unsigned int ipl,int udplite)313 static int tcf_csum_ipv6_udp(struct sk_buff *skb, unsigned int ihl,
314 unsigned int ipl, int udplite)
315 {
316 struct udphdr *udph;
317 const struct ipv6hdr *ip6h;
318 u16 ul;
319
320 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type &
321 (SKB_GSO_UDP | SKB_GSO_UDP_L4 |
322 SKB_GSO_UDP_TUNNEL | SKB_GSO_UDP_TUNNEL_CSUM))
323 return 1;
324
325 /*
326 * Support both UDP and UDPLITE checksum algorithms, Don't use
327 * udph->len to get the real length without any protocol check,
328 * UDPLITE uses udph->len for another thing,
329 * Use ip6h->payload_len + sizeof(*ip6h) ... , or just ipl.
330 */
331
332 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
333 if (udph == NULL)
334 return 0;
335
336 ip6h = ipv6_hdr(skb);
337 ul = udp_get_len_short(udph);
338
339 udph->check = 0;
340
341 if (udplite) {
342 if (ul == 0)
343 skb->csum = csum_partial(udph, ipl - ihl, 0);
344
345 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
346 skb->csum = csum_partial(udph, ul, 0);
347
348 else
349 goto ignore_obscure_skb;
350 } else {
351 if (ul != ipl - ihl)
352 goto ignore_obscure_skb;
353
354 skb->csum = csum_partial(udph, ul, 0);
355 }
356
357 udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
358 udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
359 skb->csum);
360
361 if (!udph->check)
362 udph->check = CSUM_MANGLED_0;
363
364 skb->ip_summed = CHECKSUM_NONE;
365
366 ignore_obscure_skb:
367 return 1;
368 }
369
tcf_csum_sctp(struct sk_buff * skb,unsigned int ihl,unsigned int ipl)370 static int tcf_csum_sctp(struct sk_buff *skb, unsigned int ihl,
371 unsigned int ipl)
372 {
373 struct sctphdr *sctph;
374
375 if (skb_is_gso(skb) && skb_is_gso_sctp(skb))
376 return 1;
377
378 sctph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*sctph));
379 if (!sctph)
380 return 0;
381
382 sctph->checksum = sctp_compute_cksum(skb,
383 skb_network_offset(skb) + ihl);
384 skb_reset_csum_not_inet(skb);
385
386 return 1;
387 }
388
tcf_csum_ipv4(struct sk_buff * skb,u32 update_flags)389 static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
390 {
391 const struct iphdr *iph;
392 int ntkoff;
393
394 ntkoff = skb_network_offset(skb);
395
396 if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
397 goto fail;
398
399 iph = ip_hdr(skb);
400
401 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
402 case IPPROTO_ICMP:
403 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
404 if (!tcf_csum_ipv4_icmp(skb, iph->ihl * 4,
405 ntohs(iph->tot_len)))
406 goto fail;
407 break;
408 case IPPROTO_IGMP:
409 if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
410 if (!tcf_csum_ipv4_igmp(skb, iph->ihl * 4,
411 ntohs(iph->tot_len)))
412 goto fail;
413 break;
414 case IPPROTO_TCP:
415 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
416 if (!tcf_csum_ipv4_tcp(skb, iph->ihl * 4,
417 ntohs(iph->tot_len)))
418 goto fail;
419 break;
420 case IPPROTO_UDP:
421 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
422 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
423 ntohs(iph->tot_len), 0))
424 goto fail;
425 break;
426 case IPPROTO_UDPLITE:
427 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
428 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
429 ntohs(iph->tot_len), 1))
430 goto fail;
431 break;
432 case IPPROTO_SCTP:
433 if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
434 !tcf_csum_sctp(skb, iph->ihl * 4, ntohs(iph->tot_len)))
435 goto fail;
436 break;
437 }
438
439 if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
440 if (skb_try_make_writable(skb, sizeof(*iph) + ntkoff))
441 goto fail;
442
443 ip_send_check(ip_hdr(skb));
444 }
445
446 return 1;
447
448 fail:
449 return 0;
450 }
451
tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr * ip6xh,unsigned int ixhl,unsigned int * pl)452 static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh, unsigned int ixhl,
453 unsigned int *pl)
454 {
455 int off, len, optlen;
456 unsigned char *xh = (void *)ip6xh;
457
458 off = sizeof(*ip6xh);
459 len = ixhl - off;
460
461 while (len > 1) {
462 switch (xh[off]) {
463 case IPV6_TLV_PAD1:
464 optlen = 1;
465 break;
466 case IPV6_TLV_JUMBO:
467 optlen = xh[off + 1] + 2;
468 if (optlen != 6 || len < 6 || (off & 3) != 2)
469 /* wrong jumbo option length/alignment */
470 return 0;
471 *pl = ntohl(*(__be32 *)(xh + off + 2));
472 goto done;
473 default:
474 optlen = xh[off + 1] + 2;
475 if (optlen > len)
476 /* ignore obscure options */
477 goto done;
478 break;
479 }
480 off += optlen;
481 len -= optlen;
482 }
483
484 done:
485 return 1;
486 }
487
tcf_csum_ipv6(struct sk_buff * skb,u32 update_flags)488 static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
489 {
490 struct ipv6hdr *ip6h;
491 struct ipv6_opt_hdr *ip6xh;
492 unsigned int hl, ixhl;
493 unsigned int pl;
494 int ntkoff;
495 u8 nexthdr;
496
497 ntkoff = skb_network_offset(skb);
498
499 hl = sizeof(*ip6h);
500
501 if (!pskb_may_pull(skb, hl + ntkoff))
502 goto fail;
503
504 ip6h = ipv6_hdr(skb);
505
506 pl = ntohs(ip6h->payload_len);
507 nexthdr = ip6h->nexthdr;
508
509 do {
510 switch (nexthdr) {
511 case NEXTHDR_FRAGMENT:
512 goto ignore_skb;
513 case NEXTHDR_ROUTING:
514 case NEXTHDR_HOP:
515 case NEXTHDR_DEST:
516 if (!pskb_may_pull(skb, hl + sizeof(*ip6xh) + ntkoff))
517 goto fail;
518 ip6xh = (void *)(skb_network_header(skb) + hl);
519 ixhl = ipv6_optlen(ip6xh);
520 if (!pskb_may_pull(skb, hl + ixhl + ntkoff))
521 goto fail;
522 ip6xh = (void *)(skb_network_header(skb) + hl);
523 if ((nexthdr == NEXTHDR_HOP) &&
524 !(tcf_csum_ipv6_hopopts(ip6xh, ixhl, &pl)))
525 goto fail;
526 nexthdr = ip6xh->nexthdr;
527 hl += ixhl;
528 break;
529 case IPPROTO_ICMPV6:
530 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
531 if (!tcf_csum_ipv6_icmp(skb,
532 hl, pl + sizeof(*ip6h)))
533 goto fail;
534 goto done;
535 case IPPROTO_TCP:
536 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
537 if (!tcf_csum_ipv6_tcp(skb,
538 hl, pl + sizeof(*ip6h)))
539 goto fail;
540 goto done;
541 case IPPROTO_UDP:
542 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
543 if (!tcf_csum_ipv6_udp(skb, hl,
544 pl + sizeof(*ip6h), 0))
545 goto fail;
546 goto done;
547 case IPPROTO_UDPLITE:
548 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
549 if (!tcf_csum_ipv6_udp(skb, hl,
550 pl + sizeof(*ip6h), 1))
551 goto fail;
552 goto done;
553 case IPPROTO_SCTP:
554 if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
555 !tcf_csum_sctp(skb, hl, pl + sizeof(*ip6h)))
556 goto fail;
557 goto done;
558 default:
559 goto ignore_skb;
560 }
561 } while (pskb_may_pull(skb, hl + 1 + ntkoff));
562
563 done:
564 ignore_skb:
565 return 1;
566
567 fail:
568 return 0;
569 }
570
tcf_csum_act(struct sk_buff * skb,const struct tc_action * a,struct tcf_result * res)571 TC_INDIRECT_SCOPE int tcf_csum_act(struct sk_buff *skb,
572 const struct tc_action *a,
573 struct tcf_result *res)
574 {
575 struct tcf_csum *p = to_tcf_csum(a);
576 bool orig_vlan_tag_present = false;
577 unsigned int vlan_hdr_count = 0;
578 struct tcf_csum_params *params;
579 u32 update_flags;
580 __be16 protocol;
581 int action;
582
583 params = rcu_dereference_bh(p->params);
584
585 tcf_lastuse_update(&p->tcf_tm);
586 tcf_action_update_bstats(&p->common, skb);
587
588 action = params->action;
589 if (unlikely(action == TC_ACT_SHOT))
590 goto drop;
591
592 update_flags = params->update_flags;
593 protocol = skb_protocol(skb, false);
594 again:
595 switch (protocol) {
596 case cpu_to_be16(ETH_P_IP):
597 if (!tcf_csum_ipv4(skb, update_flags))
598 goto drop;
599 break;
600 case cpu_to_be16(ETH_P_IPV6):
601 if (!tcf_csum_ipv6(skb, update_flags))
602 goto drop;
603 break;
604 case cpu_to_be16(ETH_P_8021AD):
605 fallthrough;
606 case cpu_to_be16(ETH_P_8021Q):
607 if (skb_vlan_tag_present(skb) && !orig_vlan_tag_present) {
608 protocol = skb->protocol;
609 orig_vlan_tag_present = true;
610 } else {
611 struct vlan_hdr *vlan;
612
613 if (!pskb_may_pull(skb, VLAN_HLEN))
614 goto drop;
615
616 vlan = (struct vlan_hdr *)skb->data;
617 protocol = vlan->h_vlan_encapsulated_proto;
618 skb_pull(skb, VLAN_HLEN);
619 skb_reset_network_header(skb);
620 vlan_hdr_count++;
621 }
622 goto again;
623 }
624
625 out:
626 /* Restore the skb for the pulled VLAN tags */
627 while (vlan_hdr_count--) {
628 skb_push(skb, VLAN_HLEN);
629 skb_reset_network_header(skb);
630 }
631
632 return action;
633
634 drop:
635 tcf_action_inc_drop_qstats(&p->common);
636 action = TC_ACT_SHOT;
637 goto out;
638 }
639
tcf_csum_dump(struct sk_buff * skb,struct tc_action * a,int bind,int ref)640 static int tcf_csum_dump(struct sk_buff *skb, struct tc_action *a, int bind,
641 int ref)
642 {
643 const struct tcf_csum *p = to_tcf_csum(a);
644 unsigned char *b = skb_tail_pointer(skb);
645 const struct tcf_csum_params *params;
646 struct tc_csum opt = {
647 .index = p->tcf_index,
648 .refcnt = refcount_read(&p->tcf_refcnt) - ref,
649 .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
650 };
651 struct tcf_t t;
652
653 rcu_read_lock();
654 params = rcu_dereference(p->params);
655 opt.action = params->action;
656 opt.update_flags = params->update_flags;
657
658 if (nla_put(skb, TCA_CSUM_PARMS, sizeof(opt), &opt))
659 goto nla_put_failure;
660
661 tcf_tm_dump(&t, &p->tcf_tm);
662 if (nla_put_64bit(skb, TCA_CSUM_TM, sizeof(t), &t, TCA_CSUM_PAD))
663 goto nla_put_failure;
664 rcu_read_unlock();
665
666 return skb->len;
667
668 nla_put_failure:
669 rcu_read_unlock();
670 nlmsg_trim(skb, b);
671 return -1;
672 }
673
tcf_csum_cleanup(struct tc_action * a)674 static void tcf_csum_cleanup(struct tc_action *a)
675 {
676 struct tcf_csum *p = to_tcf_csum(a);
677 struct tcf_csum_params *params;
678
679 params = rcu_dereference_protected(p->params, 1);
680 if (params)
681 kfree_rcu(params, rcu);
682 }
683
tcf_csum_get_fill_size(const struct tc_action * act)684 static size_t tcf_csum_get_fill_size(const struct tc_action *act)
685 {
686 return nla_total_size(sizeof(struct tc_csum));
687 }
688
tcf_csum_offload_act_setup(struct tc_action * act,void * entry_data,u32 * index_inc,bool bind,struct netlink_ext_ack * extack)689 static int tcf_csum_offload_act_setup(struct tc_action *act, void *entry_data,
690 u32 *index_inc, bool bind,
691 struct netlink_ext_ack *extack)
692 {
693 if (bind) {
694 struct flow_action_entry *entry = entry_data;
695
696 entry->id = FLOW_ACTION_CSUM;
697 entry->csum_flags = tcf_csum_update_flags(act);
698 *index_inc = 1;
699 } else {
700 struct flow_offload_action *fl_action = entry_data;
701
702 fl_action->id = FLOW_ACTION_CSUM;
703 }
704
705 return 0;
706 }
707
708 static struct tc_action_ops act_csum_ops = {
709 .kind = "csum",
710 .id = TCA_ID_CSUM,
711 .owner = THIS_MODULE,
712 .act = tcf_csum_act,
713 .dump = tcf_csum_dump,
714 .init = tcf_csum_init,
715 .cleanup = tcf_csum_cleanup,
716 .get_fill_size = tcf_csum_get_fill_size,
717 .offload_act_setup = tcf_csum_offload_act_setup,
718 .size = sizeof(struct tcf_csum),
719 };
720 MODULE_ALIAS_NET_ACT("csum");
721
csum_init_net(struct net * net)722 static __net_init int csum_init_net(struct net *net)
723 {
724 struct tc_action_net *tn = net_generic(net, act_csum_ops.net_id);
725
726 return tc_action_net_init(net, tn, &act_csum_ops);
727 }
728
csum_exit_net(struct list_head * net_list)729 static void __net_exit csum_exit_net(struct list_head *net_list)
730 {
731 tc_action_net_exit(net_list, act_csum_ops.net_id);
732 }
733
734 static struct pernet_operations csum_net_ops = {
735 .init = csum_init_net,
736 .exit_batch = csum_exit_net,
737 .id = &act_csum_ops.net_id,
738 .size = sizeof(struct tc_action_net),
739 };
740
741 MODULE_DESCRIPTION("Checksum updating actions");
742 MODULE_LICENSE("GPL");
743
csum_init_module(void)744 static int __init csum_init_module(void)
745 {
746 return tcf_register_action(&act_csum_ops, &csum_net_ops);
747 }
748
csum_cleanup_module(void)749 static void __exit csum_cleanup_module(void)
750 {
751 tcf_unregister_action(&act_csum_ops, &csum_net_ops);
752 }
753
754 module_init(csum_init_module);
755 module_exit(csum_cleanup_module);
756