1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/types.h>
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/spinlock.h>
18
19#include <linux/netlink.h>
20#include <net/netlink.h>
21#include <linux/rtnetlink.h>
22
23#include <linux/skbuff.h>
24
25#include <net/ip.h>
26#include <net/ipv6.h>
27#include <net/icmp.h>
28#include <linux/icmpv6.h>
29#include <linux/igmp.h>
30#include <net/tcp.h>
31#include <net/udp.h>
32#include <net/ip6_checksum.h>
33#include <net/sctp/checksum.h>
34
35#include <net/act_api.h>
36
37#include <linux/tc_act/tc_csum.h>
38#include <net/tc_act/tc_csum.h>
39
40static const struct nla_policy csum_policy[TCA_CSUM_MAX + 1] = {
41 [TCA_CSUM_PARMS] = { .len = sizeof(struct tc_csum), },
42};
43
44static int csum_net_id;
45static struct tc_action_ops act_csum_ops;
46
47static int tcf_csum_init(struct net *net, struct nlattr *nla,
48 struct nlattr *est, struct tc_action **a, int ovr,
49 int bind)
50{
51 struct tc_action_net *tn = net_generic(net, csum_net_id);
52 struct nlattr *tb[TCA_CSUM_MAX + 1];
53 struct tc_csum *parm;
54 struct tcf_csum *p;
55 int ret = 0, err;
56
57 if (nla == NULL)
58 return -EINVAL;
59
60 err = nla_parse_nested(tb, TCA_CSUM_MAX, nla, csum_policy);
61 if (err < 0)
62 return err;
63
64 if (tb[TCA_CSUM_PARMS] == NULL)
65 return -EINVAL;
66 parm = nla_data(tb[TCA_CSUM_PARMS]);
67
68 if (!tcf_idr_check(tn, parm->index, a, bind)) {
69 ret = tcf_idr_create(tn, parm->index, est, a,
70 &act_csum_ops, bind, false);
71 if (ret)
72 return ret;
73 ret = ACT_P_CREATED;
74 } else {
75 if (bind)
76 return 0;
77 tcf_idr_release(*a, bind);
78 if (!ovr)
79 return -EEXIST;
80 }
81
82 p = to_tcf_csum(*a);
83 spin_lock_bh(&p->tcf_lock);
84 p->tcf_action = parm->action;
85 p->update_flags = parm->update_flags;
86 spin_unlock_bh(&p->tcf_lock);
87
88 if (ret == ACT_P_CREATED)
89 tcf_idr_insert(tn, *a);
90
91 return ret;
92}
93
94
95
96
97
98
99
100
101
102
103
104static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
105 unsigned int ihl, unsigned int ipl,
106 unsigned int jhl)
107{
108 int ntkoff = skb_network_offset(skb);
109 int hl = ihl + jhl;
110
111 if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
112 skb_try_make_writable(skb, hl + ntkoff))
113 return NULL;
114 else
115 return (void *)(skb_network_header(skb) + ihl);
116}
117
118static int tcf_csum_ipv4_icmp(struct sk_buff *skb, unsigned int ihl,
119 unsigned int ipl)
120{
121 struct icmphdr *icmph;
122
123 icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
124 if (icmph == NULL)
125 return 0;
126
127 icmph->checksum = 0;
128 skb->csum = csum_partial(icmph, ipl - ihl, 0);
129 icmph->checksum = csum_fold(skb->csum);
130
131 skb->ip_summed = CHECKSUM_NONE;
132
133 return 1;
134}
135
136static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
137 unsigned int ihl, unsigned int ipl)
138{
139 struct igmphdr *igmph;
140
141 igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
142 if (igmph == NULL)
143 return 0;
144
145 igmph->csum = 0;
146 skb->csum = csum_partial(igmph, ipl - ihl, 0);
147 igmph->csum = csum_fold(skb->csum);
148
149 skb->ip_summed = CHECKSUM_NONE;
150
151 return 1;
152}
153
154static int tcf_csum_ipv6_icmp(struct sk_buff *skb, unsigned int ihl,
155 unsigned int ipl)
156{
157 struct icmp6hdr *icmp6h;
158 const struct ipv6hdr *ip6h;
159
160 icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
161 if (icmp6h == NULL)
162 return 0;
163
164 ip6h = ipv6_hdr(skb);
165 icmp6h->icmp6_cksum = 0;
166 skb->csum = csum_partial(icmp6h, ipl - ihl, 0);
167 icmp6h->icmp6_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
168 ipl - ihl, IPPROTO_ICMPV6,
169 skb->csum);
170
171 skb->ip_summed = CHECKSUM_NONE;
172
173 return 1;
174}
175
176static int tcf_csum_ipv4_tcp(struct sk_buff *skb, unsigned int ihl,
177 unsigned int ipl)
178{
179 struct tcphdr *tcph;
180 const struct iphdr *iph;
181
182 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
183 return 1;
184
185 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
186 if (tcph == NULL)
187 return 0;
188
189 iph = ip_hdr(skb);
190 tcph->check = 0;
191 skb->csum = csum_partial(tcph, ipl - ihl, 0);
192 tcph->check = tcp_v4_check(ipl - ihl,
193 iph->saddr, iph->daddr, skb->csum);
194
195 skb->ip_summed = CHECKSUM_NONE;
196
197 return 1;
198}
199
200static int tcf_csum_ipv6_tcp(struct sk_buff *skb, unsigned int ihl,
201 unsigned int ipl)
202{
203 struct tcphdr *tcph;
204 const struct ipv6hdr *ip6h;
205
206 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
207 return 1;
208
209 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
210 if (tcph == NULL)
211 return 0;
212
213 ip6h = ipv6_hdr(skb);
214 tcph->check = 0;
215 skb->csum = csum_partial(tcph, ipl - ihl, 0);
216 tcph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
217 ipl - ihl, IPPROTO_TCP,
218 skb->csum);
219
220 skb->ip_summed = CHECKSUM_NONE;
221
222 return 1;
223}
224
225static int tcf_csum_ipv4_udp(struct sk_buff *skb, unsigned int ihl,
226 unsigned int ipl, int udplite)
227{
228 struct udphdr *udph;
229 const struct iphdr *iph;
230 u16 ul;
231
232 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
233 return 1;
234
235
236
237
238
239
240
241
242 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
243 if (udph == NULL)
244 return 0;
245
246 iph = ip_hdr(skb);
247 ul = ntohs(udph->len);
248
249 if (udplite || udph->check) {
250
251 udph->check = 0;
252
253 if (udplite) {
254 if (ul == 0)
255 skb->csum = csum_partial(udph, ipl - ihl, 0);
256 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
257 skb->csum = csum_partial(udph, ul, 0);
258 else
259 goto ignore_obscure_skb;
260 } else {
261 if (ul != ipl - ihl)
262 goto ignore_obscure_skb;
263
264 skb->csum = csum_partial(udph, ul, 0);
265 }
266
267 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
268 ul, iph->protocol,
269 skb->csum);
270
271 if (!udph->check)
272 udph->check = CSUM_MANGLED_0;
273 }
274
275 skb->ip_summed = CHECKSUM_NONE;
276
277ignore_obscure_skb:
278 return 1;
279}
280
281static int tcf_csum_ipv6_udp(struct sk_buff *skb, unsigned int ihl,
282 unsigned int ipl, int udplite)
283{
284 struct udphdr *udph;
285 const struct ipv6hdr *ip6h;
286 u16 ul;
287
288 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
289 return 1;
290
291
292
293
294
295
296
297
298 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
299 if (udph == NULL)
300 return 0;
301
302 ip6h = ipv6_hdr(skb);
303 ul = ntohs(udph->len);
304
305 udph->check = 0;
306
307 if (udplite) {
308 if (ul == 0)
309 skb->csum = csum_partial(udph, ipl - ihl, 0);
310
311 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
312 skb->csum = csum_partial(udph, ul, 0);
313
314 else
315 goto ignore_obscure_skb;
316 } else {
317 if (ul != ipl - ihl)
318 goto ignore_obscure_skb;
319
320 skb->csum = csum_partial(udph, ul, 0);
321 }
322
323 udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
324 udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
325 skb->csum);
326
327 if (!udph->check)
328 udph->check = CSUM_MANGLED_0;
329
330 skb->ip_summed = CHECKSUM_NONE;
331
332ignore_obscure_skb:
333 return 1;
334}
335
336static int tcf_csum_sctp(struct sk_buff *skb, unsigned int ihl,
337 unsigned int ipl)
338{
339 struct sctphdr *sctph;
340
341 if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_SCTP)
342 return 1;
343
344 sctph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*sctph));
345 if (!sctph)
346 return 0;
347
348 sctph->checksum = sctp_compute_cksum(skb,
349 skb_network_offset(skb) + ihl);
350 skb->ip_summed = CHECKSUM_NONE;
351
352 return 1;
353}
354
355static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
356{
357 const struct iphdr *iph;
358 int ntkoff;
359
360 ntkoff = skb_network_offset(skb);
361
362 if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
363 goto fail;
364
365 iph = ip_hdr(skb);
366
367 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
368 case IPPROTO_ICMP:
369 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
370 if (!tcf_csum_ipv4_icmp(skb, iph->ihl * 4,
371 ntohs(iph->tot_len)))
372 goto fail;
373 break;
374 case IPPROTO_IGMP:
375 if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
376 if (!tcf_csum_ipv4_igmp(skb, iph->ihl * 4,
377 ntohs(iph->tot_len)))
378 goto fail;
379 break;
380 case IPPROTO_TCP:
381 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
382 if (!tcf_csum_ipv4_tcp(skb, iph->ihl * 4,
383 ntohs(iph->tot_len)))
384 goto fail;
385 break;
386 case IPPROTO_UDP:
387 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
388 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
389 ntohs(iph->tot_len), 0))
390 goto fail;
391 break;
392 case IPPROTO_UDPLITE:
393 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
394 if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
395 ntohs(iph->tot_len), 1))
396 goto fail;
397 break;
398 case IPPROTO_SCTP:
399 if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
400 !tcf_csum_sctp(skb, iph->ihl * 4, ntohs(iph->tot_len)))
401 goto fail;
402 break;
403 }
404
405 if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
406 if (skb_try_make_writable(skb, sizeof(*iph) + ntkoff))
407 goto fail;
408
409 ip_send_check(ip_hdr(skb));
410 }
411
412 return 1;
413
414fail:
415 return 0;
416}
417
418static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh, unsigned int ixhl,
419 unsigned int *pl)
420{
421 int off, len, optlen;
422 unsigned char *xh = (void *)ip6xh;
423
424 off = sizeof(*ip6xh);
425 len = ixhl - off;
426
427 while (len > 1) {
428 switch (xh[off]) {
429 case IPV6_TLV_PAD1:
430 optlen = 1;
431 break;
432 case IPV6_TLV_JUMBO:
433 optlen = xh[off + 1] + 2;
434 if (optlen != 6 || len < 6 || (off & 3) != 2)
435
436 return 0;
437 *pl = ntohl(*(__be32 *)(xh + off + 2));
438 goto done;
439 default:
440 optlen = xh[off + 1] + 2;
441 if (optlen > len)
442
443 goto done;
444 break;
445 }
446 off += optlen;
447 len -= optlen;
448 }
449
450done:
451 return 1;
452}
453
454static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
455{
456 struct ipv6hdr *ip6h;
457 struct ipv6_opt_hdr *ip6xh;
458 unsigned int hl, ixhl;
459 unsigned int pl;
460 int ntkoff;
461 u8 nexthdr;
462
463 ntkoff = skb_network_offset(skb);
464
465 hl = sizeof(*ip6h);
466
467 if (!pskb_may_pull(skb, hl + ntkoff))
468 goto fail;
469
470 ip6h = ipv6_hdr(skb);
471
472 pl = ntohs(ip6h->payload_len);
473 nexthdr = ip6h->nexthdr;
474
475 do {
476 switch (nexthdr) {
477 case NEXTHDR_FRAGMENT:
478 goto ignore_skb;
479 case NEXTHDR_ROUTING:
480 case NEXTHDR_HOP:
481 case NEXTHDR_DEST:
482 if (!pskb_may_pull(skb, hl + sizeof(*ip6xh) + ntkoff))
483 goto fail;
484 ip6xh = (void *)(skb_network_header(skb) + hl);
485 ixhl = ipv6_optlen(ip6xh);
486 if (!pskb_may_pull(skb, hl + ixhl + ntkoff))
487 goto fail;
488 ip6xh = (void *)(skb_network_header(skb) + hl);
489 if ((nexthdr == NEXTHDR_HOP) &&
490 !(tcf_csum_ipv6_hopopts(ip6xh, ixhl, &pl)))
491 goto fail;
492 nexthdr = ip6xh->nexthdr;
493 hl += ixhl;
494 break;
495 case IPPROTO_ICMPV6:
496 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
497 if (!tcf_csum_ipv6_icmp(skb,
498 hl, pl + sizeof(*ip6h)))
499 goto fail;
500 goto done;
501 case IPPROTO_TCP:
502 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
503 if (!tcf_csum_ipv6_tcp(skb,
504 hl, pl + sizeof(*ip6h)))
505 goto fail;
506 goto done;
507 case IPPROTO_UDP:
508 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
509 if (!tcf_csum_ipv6_udp(skb, hl,
510 pl + sizeof(*ip6h), 0))
511 goto fail;
512 goto done;
513 case IPPROTO_UDPLITE:
514 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
515 if (!tcf_csum_ipv6_udp(skb, hl,
516 pl + sizeof(*ip6h), 1))
517 goto fail;
518 goto done;
519 case IPPROTO_SCTP:
520 if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
521 !tcf_csum_sctp(skb, hl, pl + sizeof(*ip6h)))
522 goto fail;
523 goto done;
524 default:
525 goto ignore_skb;
526 }
527 } while (pskb_may_pull(skb, hl + 1 + ntkoff));
528
529done:
530ignore_skb:
531 return 1;
532
533fail:
534 return 0;
535}
536
537static int tcf_csum(struct sk_buff *skb, const struct tc_action *a,
538 struct tcf_result *res)
539{
540 struct tcf_csum *p = to_tcf_csum(a);
541 int action;
542 u32 update_flags;
543
544 spin_lock(&p->tcf_lock);
545 tcf_lastuse_update(&p->tcf_tm);
546 bstats_update(&p->tcf_bstats, skb);
547 action = p->tcf_action;
548 update_flags = p->update_flags;
549 spin_unlock(&p->tcf_lock);
550
551 if (unlikely(action == TC_ACT_SHOT))
552 goto drop;
553
554 switch (tc_skb_protocol(skb)) {
555 case cpu_to_be16(ETH_P_IP):
556 if (!tcf_csum_ipv4(skb, update_flags))
557 goto drop;
558 break;
559 case cpu_to_be16(ETH_P_IPV6):
560 if (!tcf_csum_ipv6(skb, update_flags))
561 goto drop;
562 break;
563 }
564
565 return action;
566
567drop:
568 spin_lock(&p->tcf_lock);
569 p->tcf_qstats.drops++;
570 spin_unlock(&p->tcf_lock);
571 return TC_ACT_SHOT;
572}
573
574static int tcf_csum_dump(struct sk_buff *skb, struct tc_action *a, int bind,
575 int ref)
576{
577 unsigned char *b = skb_tail_pointer(skb);
578 struct tcf_csum *p = to_tcf_csum(a);
579 struct tc_csum opt = {
580 .update_flags = p->update_flags,
581 .index = p->tcf_index,
582 .action = p->tcf_action,
583 .refcnt = p->tcf_refcnt - ref,
584 .bindcnt = p->tcf_bindcnt - bind,
585 };
586 struct tcf_t t;
587
588 if (nla_put(skb, TCA_CSUM_PARMS, sizeof(opt), &opt))
589 goto nla_put_failure;
590
591 tcf_tm_dump(&t, &p->tcf_tm);
592 if (nla_put_64bit(skb, TCA_CSUM_TM, sizeof(t), &t, TCA_CSUM_PAD))
593 goto nla_put_failure;
594
595 return skb->len;
596
597nla_put_failure:
598 nlmsg_trim(skb, b);
599 return -1;
600}
601
602static int tcf_csum_walker(struct net *net, struct sk_buff *skb,
603 struct netlink_callback *cb, int type,
604 const struct tc_action_ops *ops)
605{
606 struct tc_action_net *tn = net_generic(net, csum_net_id);
607
608 return tcf_generic_walker(tn, skb, cb, type, ops);
609}
610
611static int tcf_csum_search(struct net *net, struct tc_action **a, u32 index)
612{
613 struct tc_action_net *tn = net_generic(net, csum_net_id);
614
615 return tcf_idr_search(tn, a, index);
616}
617
618static struct tc_action_ops act_csum_ops = {
619 .kind = "csum",
620 .type = TCA_ACT_CSUM,
621 .owner = THIS_MODULE,
622 .act = tcf_csum,
623 .dump = tcf_csum_dump,
624 .init = tcf_csum_init,
625 .walk = tcf_csum_walker,
626 .lookup = tcf_csum_search,
627 .size = sizeof(struct tcf_csum),
628};
629
630static __net_init int csum_init_net(struct net *net)
631{
632 struct tc_action_net *tn = net_generic(net, csum_net_id);
633
634 return tc_action_net_init(tn, &act_csum_ops);
635}
636
637static void __net_exit csum_exit_net(struct net *net)
638{
639 struct tc_action_net *tn = net_generic(net, csum_net_id);
640
641 tc_action_net_exit(tn);
642}
643
644static struct pernet_operations csum_net_ops = {
645 .init = csum_init_net,
646 .exit = csum_exit_net,
647 .id = &csum_net_id,
648 .size = sizeof(struct tc_action_net),
649};
650
651MODULE_DESCRIPTION("Checksum updating actions");
652MODULE_LICENSE("GPL");
653
654static int __init csum_init_module(void)
655{
656 return tcf_register_action(&act_csum_ops, &csum_net_ops);
657}
658
659static void __exit csum_cleanup_module(void)
660{
661 tcf_unregister_action(&act_csum_ops, &csum_net_ops);
662}
663
664module_init(csum_init_module);
665module_exit(csum_cleanup_module);
666