blob: be41f1cc10eed458dab0a171ee561af8a7b7acb5 [file] [log] [blame]
Grégoire Baroneb4d4062010-08-18 13:10:35 +00001/*
2 * Checksum updating actions
3 *
4 * Copyright (c) 2010 Gregoire Baron <baronchon@n7mm.org>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
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>
Stephen Rothwell24362432010-08-22 20:31:14 -070032#include <net/ip6_checksum.h>
Grégoire Baroneb4d4062010-08-18 13:10:35 +000033
34#include <net/act_api.h>
35
36#include <linux/tc_act/tc_csum.h>
37#include <net/tc_act/tc_csum.h>
38
39#define CSUM_TAB_MASK 15
40static struct tcf_common *tcf_csum_ht[CSUM_TAB_MASK + 1];
41static u32 csum_idx_gen;
42static DEFINE_RWLOCK(csum_lock);
43
44static struct tcf_hashinfo csum_hash_info = {
45 .htab = tcf_csum_ht,
46 .hmask = CSUM_TAB_MASK,
47 .lock = &csum_lock,
48};
49
50static const struct nla_policy csum_policy[TCA_CSUM_MAX + 1] = {
51 [TCA_CSUM_PARMS] = { .len = sizeof(struct tc_csum), },
52};
53
54static int tcf_csum_init(struct nlattr *nla, struct nlattr *est,
55 struct tc_action *a, int ovr, int bind)
56{
57 struct nlattr *tb[TCA_CSUM_MAX + 1];
58 struct tc_csum *parm;
59 struct tcf_common *pc;
60 struct tcf_csum *p;
61 int ret = 0, err;
62
63 if (nla == NULL)
64 return -EINVAL;
65
66 err = nla_parse_nested(tb, TCA_CSUM_MAX, nla,csum_policy);
67 if (err < 0)
68 return err;
69
70 if (tb[TCA_CSUM_PARMS] == NULL)
71 return -EINVAL;
72 parm = nla_data(tb[TCA_CSUM_PARMS]);
73
74 pc = tcf_hash_check(parm->index, a, bind, &csum_hash_info);
75 if (!pc) {
76 pc = tcf_hash_create(parm->index, est, a, sizeof(*p), bind, &csum_idx_gen, &csum_hash_info);
77 if (IS_ERR(pc))
78 return PTR_ERR(pc);
79 p = to_tcf_csum(pc);
80 ret = ACT_P_CREATED;
81 } else {
82 p = to_tcf_csum(pc);
83 if (!ovr) {
84 tcf_hash_release(pc, bind, &csum_hash_info);
85 return -EEXIST;
86 }
87 }
88
89 spin_lock_bh(&p->tcf_lock);
90 p->tcf_action = parm->action;
91 p->update_flags = parm->update_flags;
92 spin_unlock_bh(&p->tcf_lock);
93
94 if (ret == ACT_P_CREATED)
95 tcf_hash_insert(pc, &csum_hash_info);
96
97 return ret;
98}
99
100static int tcf_csum_cleanup(struct tc_action *a, int bind)
101{
102 struct tcf_csum *p = a->priv;
103 return tcf_hash_release(&p->common, bind, &csum_hash_info);
104}
105
106/**
107 * tcf_csum_skb_nextlayer - Get next layer pointer
108 * @skb: sk_buff to use
109 * @ihl: previous summed headers length
110 * @ipl: complete packet length
111 * @jhl: next header length
112 *
113 * Check the expected next layer availability in the specified sk_buff.
114 * Return the next layer pointer if pass, NULL otherwise.
115 */
116static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
117 unsigned int ihl, unsigned int ipl,
118 unsigned int jhl)
119{
120 int ntkoff = skb_network_offset(skb);
121 int hl = ihl + jhl;
122
123 if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
124 (skb_cloned(skb) &&
125 !skb_clone_writable(skb, hl + ntkoff) &&
126 pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
127 return NULL;
128 else
129 return (void *)(skb_network_header(skb) + ihl);
130}
131
132static int tcf_csum_ipv4_icmp(struct sk_buff *skb,
133 unsigned int ihl, unsigned int ipl)
134{
135 struct icmphdr *icmph;
136
137 icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
138 if (icmph == NULL)
139 return 0;
140
141 icmph->checksum = 0;
142 skb->csum = csum_partial(icmph, ipl - ihl, 0);
143 icmph->checksum = csum_fold(skb->csum);
144
145 skb->ip_summed = CHECKSUM_NONE;
146
147 return 1;
148}
149
150static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
151 unsigned int ihl, unsigned int ipl)
152{
153 struct igmphdr *igmph;
154
155 igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
156 if (igmph == NULL)
157 return 0;
158
159 igmph->csum = 0;
160 skb->csum = csum_partial(igmph, ipl - ihl, 0);
161 igmph->csum = csum_fold(skb->csum);
162
163 skb->ip_summed = CHECKSUM_NONE;
164
165 return 1;
166}
167
168static int tcf_csum_ipv6_icmp(struct sk_buff *skb, struct ipv6hdr *ip6h,
169 unsigned int ihl, unsigned int ipl)
170{
171 struct icmp6hdr *icmp6h;
172
173 icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
174 if (icmp6h == NULL)
175 return 0;
176
177 icmp6h->icmp6_cksum = 0;
178 skb->csum = csum_partial(icmp6h, ipl - ihl, 0);
179 icmp6h->icmp6_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
180 ipl - ihl, IPPROTO_ICMPV6,
181 skb->csum);
182
183 skb->ip_summed = CHECKSUM_NONE;
184
185 return 1;
186}
187
188static int tcf_csum_ipv4_tcp(struct sk_buff *skb, struct iphdr *iph,
189 unsigned int ihl, unsigned int ipl)
190{
191 struct tcphdr *tcph;
192
193 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
194 if (tcph == NULL)
195 return 0;
196
197 tcph->check = 0;
198 skb->csum = csum_partial(tcph, ipl - ihl, 0);
199 tcph->check = tcp_v4_check(ipl - ihl,
200 iph->saddr, iph->daddr, skb->csum);
201
202 skb->ip_summed = CHECKSUM_NONE;
203
204 return 1;
205}
206
207static int tcf_csum_ipv6_tcp(struct sk_buff *skb, struct ipv6hdr *ip6h,
208 unsigned int ihl, unsigned int ipl)
209{
210 struct tcphdr *tcph;
211
212 tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
213 if (tcph == NULL)
214 return 0;
215
216 tcph->check = 0;
217 skb->csum = csum_partial(tcph, ipl - ihl, 0);
218 tcph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
219 ipl - ihl, IPPROTO_TCP,
220 skb->csum);
221
222 skb->ip_summed = CHECKSUM_NONE;
223
224 return 1;
225}
226
227static int tcf_csum_ipv4_udp(struct sk_buff *skb, struct iphdr *iph,
228 unsigned int ihl, unsigned int ipl, int udplite)
229{
230 struct udphdr *udph;
231 u16 ul;
232
233 /* Support both UDP and UDPLITE checksum algorithms,
234 * Don't use udph->len to get the real length without any protocol check,
235 * UDPLITE uses udph->len for another thing,
236 * Use iph->tot_len, or just ipl.
237 */
238
239 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
240 if (udph == NULL)
241 return 0;
242
243 ul = ntohs(udph->len);
244
245 if (udplite || udph->check) {
246
247 udph->check = 0;
248
249 if (udplite) {
250 if (ul == 0)
251 skb->csum = csum_partial(udph, ipl - ihl, 0);
252
253 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
254 skb->csum = csum_partial(udph, ul, 0);
255
256 else
257 goto ignore_obscure_skb;
258 } else {
259 if (ul != ipl - ihl)
260 goto ignore_obscure_skb;
261
262 skb->csum = csum_partial(udph, ul, 0);
263 }
264
265 udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
266 ul, iph->protocol,
267 skb->csum);
268
269 if (!udph->check)
270 udph->check = CSUM_MANGLED_0;
271 }
272
273 skb->ip_summed = CHECKSUM_NONE;
274
275ignore_obscure_skb:
276 return 1;
277}
278
279static int tcf_csum_ipv6_udp(struct sk_buff *skb, struct ipv6hdr *ip6h,
280 unsigned int ihl, unsigned int ipl, int udplite)
281{
282 struct udphdr *udph;
283 u16 ul;
284
285 /* Support both UDP and UDPLITE checksum algorithms,
286 * Don't use udph->len to get the real length without any protocol check,
287 * UDPLITE uses udph->len for another thing,
288 * Use ip6h->payload_len + sizeof(*ip6h) ... , or just ipl.
289 */
290
291 udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
292 if (udph == NULL)
293 return 0;
294
295 ul = ntohs(udph->len);
296
297 udph->check = 0;
298
299 if (udplite) {
300 if (ul == 0)
301 skb->csum = csum_partial(udph, ipl - ihl, 0);
302
303 else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
304 skb->csum = csum_partial(udph, ul, 0);
305
306 else
307 goto ignore_obscure_skb;
308 } else {
309 if (ul != ipl - ihl)
310 goto ignore_obscure_skb;
311
312 skb->csum = csum_partial(udph, ul, 0);
313 }
314
315 udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
316 udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
317 skb->csum);
318
319 if (!udph->check)
320 udph->check = CSUM_MANGLED_0;
321
322 skb->ip_summed = CHECKSUM_NONE;
323
324ignore_obscure_skb:
325 return 1;
326}
327
328static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
329{
330 struct iphdr *iph;
331 int ntkoff;
332
333 ntkoff = skb_network_offset(skb);
334
335 if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
336 goto fail;
337
338 iph = ip_hdr(skb);
339
340 switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
341 case IPPROTO_ICMP:
342 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
343 if (!tcf_csum_ipv4_icmp(skb,
344 iph->ihl * 4, ntohs(iph->tot_len)))
345 goto fail;
346 break;
347 case IPPROTO_IGMP:
348 if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
349 if (!tcf_csum_ipv4_igmp(skb,
350 iph->ihl * 4, ntohs(iph->tot_len)))
351 goto fail;
352 break;
353 case IPPROTO_TCP:
354 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
355 if (!tcf_csum_ipv4_tcp(skb, iph,
356 iph->ihl * 4, ntohs(iph->tot_len)))
357 goto fail;
358 break;
359 case IPPROTO_UDP:
360 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
361 if (!tcf_csum_ipv4_udp(skb, iph,
362 iph->ihl * 4, ntohs(iph->tot_len), 0))
363 goto fail;
364 break;
365 case IPPROTO_UDPLITE:
366 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
367 if (!tcf_csum_ipv4_udp(skb, iph,
368 iph->ihl * 4, ntohs(iph->tot_len), 1))
369 goto fail;
370 break;
371 }
372
373 if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
374 if (skb_cloned(skb) &&
375 !skb_clone_writable(skb, sizeof(*iph) + ntkoff) &&
376 pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
377 goto fail;
378
379 ip_send_check(iph);
380 }
381
382 return 1;
383
384fail:
385 return 0;
386}
387
388static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh,
389 unsigned int ixhl, unsigned int *pl)
390{
391 int off, len, optlen;
392 unsigned char *xh = (void *)ip6xh;
393
394 off = sizeof(*ip6xh);
395 len = ixhl - off;
396
397 while (len > 1) {
398 switch (xh[off])
399 {
400 case IPV6_TLV_PAD0:
401 optlen = 1;
402 break;
403 case IPV6_TLV_JUMBO:
404 optlen = xh[off + 1] + 2;
405 if (optlen != 6 || len < 6 || (off & 3) != 2)
406 /* wrong jumbo option length/alignment */
407 return 0;
408 *pl = ntohl(*(__be32 *)(xh + off + 2));
409 goto done;
410 default:
411 optlen = xh[off + 1] + 2;
412 if (optlen > len)
413 /* ignore obscure options */
414 goto done;
415 break;
416 }
417 off += optlen;
418 len -= optlen;
419 }
420
421done:
422 return 1;
423}
424
425static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
426{
427 struct ipv6hdr *ip6h;
428 struct ipv6_opt_hdr *ip6xh;
429 unsigned int hl, ixhl;
430 unsigned int pl;
431 int ntkoff;
432 u8 nexthdr;
433
434 ntkoff = skb_network_offset(skb);
435
436 hl = sizeof(*ip6h);
437
438 if (!pskb_may_pull(skb, hl + ntkoff))
439 goto fail;
440
441 ip6h = ipv6_hdr(skb);
442
443 pl = ntohs(ip6h->payload_len);
444 nexthdr = ip6h->nexthdr;
445
446 do {
447 switch (nexthdr) {
448 case NEXTHDR_FRAGMENT:
449 goto ignore_skb;
450 case NEXTHDR_ROUTING:
451 case NEXTHDR_HOP:
452 case NEXTHDR_DEST:
453 if (!pskb_may_pull(skb, hl + sizeof(*ip6xh) + ntkoff))
454 goto fail;
455 ip6xh = (void *)(skb_network_header(skb) + hl);
456 ixhl = ipv6_optlen(ip6xh);
457 if (!pskb_may_pull(skb, hl + ixhl + ntkoff))
458 goto fail;
459 if ((nexthdr == NEXTHDR_HOP) &&
460 !(tcf_csum_ipv6_hopopts(ip6xh, ixhl, &pl)))
461 goto fail;
462 nexthdr = ip6xh->nexthdr;
463 hl += ixhl;
464 break;
465 case IPPROTO_ICMPV6:
466 if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
467 if (!tcf_csum_ipv6_icmp(skb, ip6h,
468 hl, pl + sizeof(*ip6h)))
469 goto fail;
470 goto done;
471 case IPPROTO_TCP:
472 if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
473 if (!tcf_csum_ipv6_tcp(skb, ip6h,
474 hl, pl + sizeof(*ip6h)))
475 goto fail;
476 goto done;
477 case IPPROTO_UDP:
478 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
479 if (!tcf_csum_ipv6_udp(skb, ip6h,
480 hl, pl + sizeof(*ip6h), 0))
481 goto fail;
482 goto done;
483 case IPPROTO_UDPLITE:
484 if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
485 if (!tcf_csum_ipv6_udp(skb, ip6h,
486 hl, pl + sizeof(*ip6h), 1))
487 goto fail;
488 goto done;
489 default:
490 goto ignore_skb;
491 }
492 } while (pskb_may_pull(skb, hl + 1 + ntkoff));
493
494done:
495ignore_skb:
496 return 1;
497
498fail:
499 return 0;
500}
501
502static int tcf_csum(struct sk_buff *skb,
503 struct tc_action *a, struct tcf_result *res)
504{
505 struct tcf_csum *p = a->priv;
506 int action;
507 u32 update_flags;
508
509 spin_lock(&p->tcf_lock);
510 p->tcf_tm.lastuse = jiffies;
511 p->tcf_bstats.bytes += qdisc_pkt_len(skb);
512 p->tcf_bstats.packets++;
513 action = p->tcf_action;
514 update_flags = p->update_flags;
515 spin_unlock(&p->tcf_lock);
516
517 if (unlikely(action == TC_ACT_SHOT))
518 goto drop;
519
520 switch (skb->protocol) {
521 case cpu_to_be16(ETH_P_IP):
522 if (!tcf_csum_ipv4(skb, update_flags))
523 goto drop;
524 break;
525 case cpu_to_be16(ETH_P_IPV6):
526 if (!tcf_csum_ipv6(skb, update_flags))
527 goto drop;
528 break;
529 }
530
531 return action;
532
533drop:
534 spin_lock(&p->tcf_lock);
535 p->tcf_qstats.drops++;
536 spin_unlock(&p->tcf_lock);
537 return TC_ACT_SHOT;
538}
539
540static int tcf_csum_dump(struct sk_buff *skb,
541 struct tc_action *a, int bind, int ref)
542{
543 unsigned char *b = skb_tail_pointer(skb);
544 struct tcf_csum *p = a->priv;
545 struct tc_csum opt = {
546 .update_flags = p->update_flags,
547
548 .index = p->tcf_index,
549 .action = p->tcf_action,
550 .refcnt = p->tcf_refcnt - ref,
551 .bindcnt = p->tcf_bindcnt - bind,
552 };
553 struct tcf_t t;
554
555 NLA_PUT(skb, TCA_CSUM_PARMS, sizeof(opt), &opt);
556 t.install = jiffies_to_clock_t(jiffies - p->tcf_tm.install);
557 t.lastuse = jiffies_to_clock_t(jiffies - p->tcf_tm.lastuse);
558 t.expires = jiffies_to_clock_t(p->tcf_tm.expires);
559 NLA_PUT(skb, TCA_CSUM_TM, sizeof(t), &t);
560
561 return skb->len;
562
563nla_put_failure:
564 nlmsg_trim(skb, b);
565 return -1;
566}
567
568static struct tc_action_ops act_csum_ops = {
569 .kind = "csum",
570 .hinfo = &csum_hash_info,
571 .type = TCA_ACT_CSUM,
572 .capab = TCA_CAP_NONE,
573 .owner = THIS_MODULE,
574 .act = tcf_csum,
575 .dump = tcf_csum_dump,
576 .cleanup = tcf_csum_cleanup,
577 .lookup = tcf_hash_search,
578 .init = tcf_csum_init,
579 .walk = tcf_generic_walker
580};
581
582MODULE_DESCRIPTION("Checksum updating actions");
583MODULE_LICENSE("GPL");
584
585static int __init csum_init_module(void)
586{
587 return tcf_register_action(&act_csum_ops);
588}
589
590static void __exit csum_cleanup_module(void)
591{
592 tcf_unregister_action(&act_csum_ops);
593}
594
595module_init(csum_init_module);
596module_exit(csum_cleanup_module);