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