Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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> |
Stephen Rothwell | 2436243 | 2010-08-22 20:31:14 -0700 | [diff] [blame] | 32 | #include <net/ip6_checksum.h> |
Davide Caratti | c008b33 | 2017-01-09 11:24:21 +0100 | [diff] [blame] | 33 | #include <net/sctp/checksum.h> |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 34 | |
| 35 | #include <net/act_api.h> |
Davide Caratti | f5c29d8 | 2019-03-20 15:00:01 +0100 | [diff] [blame] | 36 | #include <net/pkt_cls.h> |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 37 | |
| 38 | #include <linux/tc_act/tc_csum.h> |
| 39 | #include <net/tc_act/tc_csum.h> |
| 40 | |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 41 | static const struct nla_policy csum_policy[TCA_CSUM_MAX + 1] = { |
| 42 | [TCA_CSUM_PARMS] = { .len = sizeof(struct tc_csum), }, |
| 43 | }; |
| 44 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 45 | static unsigned int csum_net_id; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 46 | static struct tc_action_ops act_csum_ops; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 47 | |
| 48 | static int tcf_csum_init(struct net *net, struct nlattr *nla, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 49 | struct nlattr *est, struct tc_action **a, int ovr, |
Davide Caratti | 85d0966 | 2019-03-20 14:59:59 +0100 | [diff] [blame] | 50 | int bind, bool rtnl_held, struct tcf_proto *tp, |
Vlad Buslov | 789871b | 2018-07-05 17:24:25 +0300 | [diff] [blame] | 51 | struct netlink_ext_ack *extack) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 52 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 53 | struct tc_action_net *tn = net_generic(net, csum_net_id); |
Vlad Buslov | b6a2b97 | 2018-08-10 20:51:42 +0300 | [diff] [blame] | 54 | struct tcf_csum_params *params_new; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 55 | struct nlattr *tb[TCA_CSUM_MAX + 1]; |
Davide Caratti | f5c29d8 | 2019-03-20 15:00:01 +0100 | [diff] [blame] | 56 | struct tcf_chain *goto_ch = NULL; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 57 | struct tc_csum *parm; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 58 | struct tcf_csum *p; |
| 59 | int ret = 0, err; |
| 60 | |
| 61 | if (nla == NULL) |
| 62 | return -EINVAL; |
| 63 | |
Johannes Berg | 8cb0817 | 2019-04-26 14:07:28 +0200 | [diff] [blame] | 64 | err = nla_parse_nested_deprecated(tb, TCA_CSUM_MAX, nla, csum_policy, |
| 65 | NULL); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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 | |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 73 | err = tcf_idr_check_alloc(tn, &parm->index, a, bind); |
| 74 | if (!err) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 75 | ret = tcf_idr_create(tn, parm->index, est, a, |
Davide Caratti | f6052cf | 2018-01-22 18:14:31 +0100 | [diff] [blame] | 76 | &act_csum_ops, bind, true); |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 77 | if (ret) { |
| 78 | tcf_idr_cleanup(tn, parm->index); |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 79 | return ret; |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 80 | } |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 81 | ret = ACT_P_CREATED; |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 82 | } else if (err > 0) { |
Jamal Hadi Salim | 1a29321 | 2013-12-23 08:02:11 -0500 | [diff] [blame] | 83 | if (bind)/* dont override defaults */ |
| 84 | return 0; |
Vlad Buslov | 4e8ddd7 | 2018-07-05 17:24:30 +0300 | [diff] [blame] | 85 | if (!ovr) { |
| 86 | tcf_idr_release(*a, bind); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 87 | return -EEXIST; |
Vlad Buslov | 4e8ddd7 | 2018-07-05 17:24:30 +0300 | [diff] [blame] | 88 | } |
Vlad Buslov | 0190c1d | 2018-07-05 17:24:32 +0300 | [diff] [blame] | 89 | } else { |
| 90 | return err; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Davide Caratti | f5c29d8 | 2019-03-20 15:00:01 +0100 | [diff] [blame] | 93 | err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); |
| 94 | if (err < 0) |
| 95 | goto release_idr; |
| 96 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 97 | p = to_tcf_csum(*a); |
Davide Caratti | 9c5f69b | 2018-01-22 18:14:32 +0100 | [diff] [blame] | 98 | |
| 99 | params_new = kzalloc(sizeof(*params_new), GFP_KERNEL); |
| 100 | if (unlikely(!params_new)) { |
Davide Caratti | f5c29d8 | 2019-03-20 15:00:01 +0100 | [diff] [blame] | 101 | err = -ENOMEM; |
| 102 | goto put_chain; |
Davide Caratti | 9c5f69b | 2018-01-22 18:14:32 +0100 | [diff] [blame] | 103 | } |
Davide Caratti | 9c5f69b | 2018-01-22 18:14:32 +0100 | [diff] [blame] | 104 | params_new->update_flags = parm->update_flags; |
Vlad Buslov | b6a2b97 | 2018-08-10 20:51:42 +0300 | [diff] [blame] | 105 | |
Vlad Buslov | 653cd28 | 2018-08-14 21:46:16 +0300 | [diff] [blame] | 106 | spin_lock_bh(&p->tcf_lock); |
Davide Caratti | f5c29d8 | 2019-03-20 15:00:01 +0100 | [diff] [blame] | 107 | goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); |
Vlad Buslov | b6a2b97 | 2018-08-10 20:51:42 +0300 | [diff] [blame] | 108 | rcu_swap_protected(p->params, params_new, |
| 109 | lockdep_is_held(&p->tcf_lock)); |
Vlad Buslov | 653cd28 | 2018-08-14 21:46:16 +0300 | [diff] [blame] | 110 | spin_unlock_bh(&p->tcf_lock); |
Vlad Buslov | b6a2b97 | 2018-08-10 20:51:42 +0300 | [diff] [blame] | 111 | |
Davide Caratti | f5c29d8 | 2019-03-20 15:00:01 +0100 | [diff] [blame] | 112 | if (goto_ch) |
| 113 | tcf_chain_put_by_act(goto_ch); |
Vlad Buslov | b6a2b97 | 2018-08-10 20:51:42 +0300 | [diff] [blame] | 114 | if (params_new) |
| 115 | kfree_rcu(params_new, rcu); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 116 | |
| 117 | if (ret == ACT_P_CREATED) |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 118 | tcf_idr_insert(tn, *a); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 119 | |
| 120 | return ret; |
Davide Caratti | f5c29d8 | 2019-03-20 15:00:01 +0100 | [diff] [blame] | 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; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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) || |
Daniel Borkmann | 3697649 | 2016-02-19 23:05:25 +0100 | [diff] [blame] | 147 | skb_try_make_writable(skb, hl + ntkoff)) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 148 | return NULL; |
| 149 | else |
| 150 | return (void *)(skb_network_header(skb) + ihl); |
| 151 | } |
| 152 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 153 | static int tcf_csum_ipv4_icmp(struct sk_buff *skb, unsigned int ihl, |
| 154 | unsigned int ipl) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 189 | static int tcf_csum_ipv6_icmp(struct sk_buff *skb, unsigned int ihl, |
| 190 | unsigned int ipl) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 191 | { |
| 192 | struct icmp6hdr *icmp6h; |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 193 | const struct ipv6hdr *ip6h; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 194 | |
| 195 | icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h)); |
| 196 | if (icmp6h == NULL) |
| 197 | return 0; |
| 198 | |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 199 | ip6h = ipv6_hdr(skb); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 211 | static int tcf_csum_ipv4_tcp(struct sk_buff *skb, unsigned int ihl, |
| 212 | unsigned int ipl) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 213 | { |
| 214 | struct tcphdr *tcph; |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 215 | const struct iphdr *iph; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 216 | |
Davide Caratti | add641e | 2017-03-23 10:39:40 +0100 | [diff] [blame] | 217 | if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) |
| 218 | return 1; |
| 219 | |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 220 | tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph)); |
| 221 | if (tcph == NULL) |
| 222 | return 0; |
| 223 | |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 224 | iph = ip_hdr(skb); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 235 | static int tcf_csum_ipv6_tcp(struct sk_buff *skb, unsigned int ihl, |
| 236 | unsigned int ipl) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 237 | { |
| 238 | struct tcphdr *tcph; |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 239 | const struct ipv6hdr *ip6h; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 240 | |
Davide Caratti | add641e | 2017-03-23 10:39:40 +0100 | [diff] [blame] | 241 | if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) |
| 242 | return 1; |
| 243 | |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 244 | tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph)); |
| 245 | if (tcph == NULL) |
| 246 | return 0; |
| 247 | |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 248 | ip6h = ipv6_hdr(skb); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 260 | static int tcf_csum_ipv4_udp(struct sk_buff *skb, unsigned int ihl, |
| 261 | unsigned int ipl, int udplite) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 262 | { |
| 263 | struct udphdr *udph; |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 264 | const struct iphdr *iph; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 265 | u16 ul; |
| 266 | |
Willem de Bruijn | 0c19f846 | 2017-11-21 10:22:25 -0500 | [diff] [blame] | 267 | if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) |
| 268 | return 1; |
| 269 | |
Changli Gao | 0eec32f | 2010-08-23 03:27:58 +0000 | [diff] [blame] | 270 | /* |
| 271 | * Support both UDP and UDPLITE checksum algorithms, Don't use |
| 272 | * udph->len to get the real length without any protocol check, |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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 | |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 281 | iph = ip_hdr(skb); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 291 | else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl)) |
| 292 | skb->csum = csum_partial(udph, ul, 0); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 316 | static int tcf_csum_ipv6_udp(struct sk_buff *skb, unsigned int ihl, |
| 317 | unsigned int ipl, int udplite) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 318 | { |
| 319 | struct udphdr *udph; |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 320 | const struct ipv6hdr *ip6h; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 321 | u16 ul; |
| 322 | |
Willem de Bruijn | 0c19f846 | 2017-11-21 10:22:25 -0500 | [diff] [blame] | 323 | if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) |
| 324 | return 1; |
| 325 | |
Changli Gao | 0eec32f | 2010-08-23 03:27:58 +0000 | [diff] [blame] | 326 | /* |
| 327 | * Support both UDP and UDPLITE checksum algorithms, Don't use |
| 328 | * udph->len to get the real length without any protocol check, |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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 | |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 337 | ip6h = ipv6_hdr(skb); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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 | |
Davide Caratti | c008b33 | 2017-01-09 11:24:21 +0100 | [diff] [blame] | 371 | static int tcf_csum_sctp(struct sk_buff *skb, unsigned int ihl, |
| 372 | unsigned int ipl) |
| 373 | { |
| 374 | struct sctphdr *sctph; |
| 375 | |
Daniel Axtens | 1dd27cd | 2018-03-09 14:06:09 +1100 | [diff] [blame] | 376 | if (skb_is_gso(skb) && skb_is_gso_sctp(skb)) |
Davide Caratti | c008b33 | 2017-01-09 11:24:21 +0100 | [diff] [blame] | 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; |
Davide Caratti | dba0030 | 2017-05-18 15:44:40 +0200 | [diff] [blame] | 386 | skb->csum_not_inet = 0; |
Davide Caratti | c008b33 | 2017-01-09 11:24:21 +0100 | [diff] [blame] | 387 | |
| 388 | return 1; |
| 389 | } |
| 390 | |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 391 | static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags) |
| 392 | { |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 393 | const struct iphdr *iph; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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) |
Changli Gao | 0eec32f | 2010-08-23 03:27:58 +0000 | [diff] [blame] | 406 | if (!tcf_csum_ipv4_icmp(skb, iph->ihl * 4, |
| 407 | ntohs(iph->tot_len))) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 408 | goto fail; |
| 409 | break; |
| 410 | case IPPROTO_IGMP: |
| 411 | if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP) |
Changli Gao | 0eec32f | 2010-08-23 03:27:58 +0000 | [diff] [blame] | 412 | if (!tcf_csum_ipv4_igmp(skb, iph->ihl * 4, |
| 413 | ntohs(iph->tot_len))) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 414 | goto fail; |
| 415 | break; |
| 416 | case IPPROTO_TCP: |
| 417 | if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP) |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 418 | if (!tcf_csum_ipv4_tcp(skb, iph->ihl * 4, |
Changli Gao | 0eec32f | 2010-08-23 03:27:58 +0000 | [diff] [blame] | 419 | ntohs(iph->tot_len))) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 420 | goto fail; |
| 421 | break; |
| 422 | case IPPROTO_UDP: |
| 423 | if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP) |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 424 | if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4, |
Changli Gao | 0eec32f | 2010-08-23 03:27:58 +0000 | [diff] [blame] | 425 | ntohs(iph->tot_len), 0)) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 426 | goto fail; |
| 427 | break; |
| 428 | case IPPROTO_UDPLITE: |
| 429 | if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE) |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 430 | if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4, |
Changli Gao | 0eec32f | 2010-08-23 03:27:58 +0000 | [diff] [blame] | 431 | ntohs(iph->tot_len), 1)) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 432 | goto fail; |
| 433 | break; |
Davide Caratti | c008b33 | 2017-01-09 11:24:21 +0100 | [diff] [blame] | 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; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) { |
Daniel Borkmann | 3697649 | 2016-02-19 23:05:25 +0100 | [diff] [blame] | 442 | if (skb_try_make_writable(skb, sizeof(*iph) + ntkoff)) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 443 | goto fail; |
| 444 | |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 445 | ip_send_check(ip_hdr(skb)); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | return 1; |
| 449 | |
| 450 | fail: |
| 451 | return 0; |
| 452 | } |
| 453 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 454 | static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh, unsigned int ixhl, |
| 455 | unsigned int *pl) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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) { |
Changli Gao | 0eec32f | 2010-08-23 03:27:58 +0000 | [diff] [blame] | 464 | switch (xh[off]) { |
Eldad Zack | 1de5a71 | 2012-05-17 06:00:25 +0000 | [diff] [blame] | 465 | case IPV6_TLV_PAD1: |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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; |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 524 | ip6xh = (void *)(skb_network_header(skb) + hl); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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) |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 533 | if (!tcf_csum_ipv6_icmp(skb, |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 534 | hl, pl + sizeof(*ip6h))) |
| 535 | goto fail; |
| 536 | goto done; |
| 537 | case IPPROTO_TCP: |
| 538 | if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP) |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 539 | if (!tcf_csum_ipv6_tcp(skb, |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 540 | hl, pl + sizeof(*ip6h))) |
| 541 | goto fail; |
| 542 | goto done; |
| 543 | case IPPROTO_UDP: |
| 544 | if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP) |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 545 | if (!tcf_csum_ipv6_udp(skb, hl, |
Changli Gao | 0eec32f | 2010-08-23 03:27:58 +0000 | [diff] [blame] | 546 | pl + sizeof(*ip6h), 0)) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 547 | goto fail; |
| 548 | goto done; |
| 549 | case IPPROTO_UDPLITE: |
| 550 | if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE) |
Eric Dumazet | d14a489 | 2013-04-12 11:07:47 -0700 | [diff] [blame] | 551 | if (!tcf_csum_ipv6_udp(skb, hl, |
Changli Gao | 0eec32f | 2010-08-23 03:27:58 +0000 | [diff] [blame] | 552 | pl + sizeof(*ip6h), 1)) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 553 | goto fail; |
| 554 | goto done; |
Davide Caratti | c008b33 | 2017-01-09 11:24:21 +0100 | [diff] [blame] | 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; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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 | |
Jamal Hadi Salim | c831549 | 2018-08-12 09:34:51 -0400 | [diff] [blame] | 573 | static int tcf_csum_act(struct sk_buff *skb, const struct tc_action *a, |
| 574 | struct tcf_result *res) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 575 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 576 | struct tcf_csum *p = to_tcf_csum(a); |
Eli Britstein | 2ecba2d | 2019-02-26 09:57:34 +0000 | [diff] [blame] | 577 | bool orig_vlan_tag_present = false; |
| 578 | unsigned int vlan_hdr_count = 0; |
Davide Caratti | 9c5f69b | 2018-01-22 18:14:32 +0100 | [diff] [blame] | 579 | struct tcf_csum_params *params; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 580 | u32 update_flags; |
Eli Britstein | 2ecba2d | 2019-02-26 09:57:34 +0000 | [diff] [blame] | 581 | __be16 protocol; |
Davide Caratti | 9c5f69b | 2018-01-22 18:14:32 +0100 | [diff] [blame] | 582 | int action; |
| 583 | |
Paolo Abeni | 7fd4b28 | 2018-07-30 14:30:43 +0200 | [diff] [blame] | 584 | params = rcu_dereference_bh(p->params); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 585 | |
Jamal Hadi Salim | 9c4a4e4 | 2016-06-06 06:32:53 -0400 | [diff] [blame] | 586 | tcf_lastuse_update(&p->tcf_tm); |
Davide Caratti | f6052cf | 2018-01-22 18:14:31 +0100 | [diff] [blame] | 587 | bstats_cpu_update(this_cpu_ptr(p->common.cpu_bstats), skb); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 588 | |
Davide Caratti | 11a245e | 2018-07-06 21:01:05 +0200 | [diff] [blame] | 589 | action = READ_ONCE(p->tcf_action); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 590 | if (unlikely(action == TC_ACT_SHOT)) |
Paolo Abeni | 7fd4b28 | 2018-07-30 14:30:43 +0200 | [diff] [blame] | 591 | goto drop; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 592 | |
Davide Caratti | 9c5f69b | 2018-01-22 18:14:32 +0100 | [diff] [blame] | 593 | update_flags = params->update_flags; |
Eli Britstein | 2ecba2d | 2019-02-26 09:57:34 +0000 | [diff] [blame] | 594 | protocol = tc_skb_protocol(skb); |
| 595 | again: |
| 596 | switch (protocol) { |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 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; |
Eli Britstein | 2ecba2d | 2019-02-26 09:57:34 +0000 | [diff] [blame] | 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); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | return action; |
| 629 | |
| 630 | drop: |
Davide Caratti | f6052cf | 2018-01-22 18:14:31 +0100 | [diff] [blame] | 631 | qstats_drop_inc(this_cpu_ptr(p->common.cpu_qstats)); |
Eli Britstein | 2ecba2d | 2019-02-26 09:57:34 +0000 | [diff] [blame] | 632 | action = TC_ACT_SHOT; |
| 633 | goto out; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 636 | static int tcf_csum_dump(struct sk_buff *skb, struct tc_action *a, int bind, |
| 637 | int ref) |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 638 | { |
| 639 | unsigned char *b = skb_tail_pointer(skb); |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 640 | struct tcf_csum *p = to_tcf_csum(a); |
Davide Caratti | 9c5f69b | 2018-01-22 18:14:32 +0100 | [diff] [blame] | 641 | struct tcf_csum_params *params; |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 642 | struct tc_csum opt = { |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 643 | .index = p->tcf_index, |
Vlad Buslov | 036bb44 | 2018-07-05 17:24:24 +0300 | [diff] [blame] | 644 | .refcnt = refcount_read(&p->tcf_refcnt) - ref, |
| 645 | .bindcnt = atomic_read(&p->tcf_bindcnt) - bind, |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 646 | }; |
| 647 | struct tcf_t t; |
| 648 | |
Vlad Buslov | 653cd28 | 2018-08-14 21:46:16 +0300 | [diff] [blame] | 649 | spin_lock_bh(&p->tcf_lock); |
Vlad Buslov | b6a2b97 | 2018-08-10 20:51:42 +0300 | [diff] [blame] | 650 | params = rcu_dereference_protected(p->params, |
| 651 | lockdep_is_held(&p->tcf_lock)); |
| 652 | opt.action = p->tcf_action; |
Davide Caratti | 9c5f69b | 2018-01-22 18:14:32 +0100 | [diff] [blame] | 653 | opt.update_flags = params->update_flags; |
| 654 | |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 655 | if (nla_put(skb, TCA_CSUM_PARMS, sizeof(opt), &opt)) |
| 656 | goto nla_put_failure; |
Jamal Hadi Salim | 48d8ee1 | 2016-06-06 06:32:55 -0400 | [diff] [blame] | 657 | |
| 658 | tcf_tm_dump(&t, &p->tcf_tm); |
Nicolas Dichtel | 9854518 | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 659 | if (nla_put_64bit(skb, TCA_CSUM_TM, sizeof(t), &t, TCA_CSUM_PAD)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 660 | goto nla_put_failure; |
Vlad Buslov | 653cd28 | 2018-08-14 21:46:16 +0300 | [diff] [blame] | 661 | spin_unlock_bh(&p->tcf_lock); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 662 | |
| 663 | return skb->len; |
| 664 | |
| 665 | nla_put_failure: |
Vlad Buslov | 653cd28 | 2018-08-14 21:46:16 +0300 | [diff] [blame] | 666 | spin_unlock_bh(&p->tcf_lock); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 667 | nlmsg_trim(skb, b); |
| 668 | return -1; |
| 669 | } |
| 670 | |
Davide Caratti | 9c5f69b | 2018-01-22 18:14:32 +0100 | [diff] [blame] | 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); |
Davide Caratti | aab378a | 2018-03-16 00:00:54 +0100 | [diff] [blame] | 677 | if (params) |
| 678 | kfree_rcu(params, rcu); |
Davide Caratti | 9c5f69b | 2018-01-22 18:14:32 +0100 | [diff] [blame] | 679 | } |
| 680 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 681 | static int tcf_csum_walker(struct net *net, struct sk_buff *skb, |
| 682 | struct netlink_callback *cb, int type, |
Alexander Aring | 4178010 | 2018-02-15 10:54:58 -0500 | [diff] [blame] | 683 | const struct tc_action_ops *ops, |
| 684 | struct netlink_ext_ack *extack) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 685 | { |
| 686 | struct tc_action_net *tn = net_generic(net, csum_net_id); |
| 687 | |
Alexander Aring | b362014 | 2018-02-15 10:54:59 -0500 | [diff] [blame] | 688 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 689 | } |
| 690 | |
Cong Wang | f061b48 | 2018-08-29 10:15:35 -0700 | [diff] [blame] | 691 | static int tcf_csum_search(struct net *net, struct tc_action **a, u32 index) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 692 | { |
| 693 | struct tc_action_net *tn = net_generic(net, csum_net_id); |
| 694 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 695 | return tcf_idr_search(tn, a, index); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 696 | } |
| 697 | |
Craig Dillabaugh | 29e6eee | 2018-05-01 10:17:43 -0400 | [diff] [blame] | 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 | |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 703 | static struct tc_action_ops act_csum_ops = { |
Changli Gao | 0eec32f | 2010-08-23 03:27:58 +0000 | [diff] [blame] | 704 | .kind = "csum", |
Eli Cohen | eddd2cf | 2019-02-10 14:25:00 +0200 | [diff] [blame] | 705 | .id = TCA_ID_CSUM, |
Changli Gao | 0eec32f | 2010-08-23 03:27:58 +0000 | [diff] [blame] | 706 | .owner = THIS_MODULE, |
Jamal Hadi Salim | c831549 | 2018-08-12 09:34:51 -0400 | [diff] [blame] | 707 | .act = tcf_csum_act, |
Changli Gao | 0eec32f | 2010-08-23 03:27:58 +0000 | [diff] [blame] | 708 | .dump = tcf_csum_dump, |
Changli Gao | 0eec32f | 2010-08-23 03:27:58 +0000 | [diff] [blame] | 709 | .init = tcf_csum_init, |
Davide Caratti | 9c5f69b | 2018-01-22 18:14:32 +0100 | [diff] [blame] | 710 | .cleanup = tcf_csum_cleanup, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 711 | .walk = tcf_csum_walker, |
| 712 | .lookup = tcf_csum_search, |
Craig Dillabaugh | 29e6eee | 2018-05-01 10:17:43 -0400 | [diff] [blame] | 713 | .get_fill_size = tcf_csum_get_fill_size, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 714 | .size = sizeof(struct tcf_csum), |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 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 | |
Cong Wang | c7e460c | 2017-11-06 13:47:18 -0800 | [diff] [blame] | 721 | return tc_action_net_init(tn, &act_csum_ops); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 722 | } |
| 723 | |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 724 | static void __net_exit csum_exit_net(struct list_head *net_list) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 725 | { |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 726 | tc_action_net_exit(net_list, csum_net_id); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | static struct pernet_operations csum_net_ops = { |
| 730 | .init = csum_init_net, |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 731 | .exit_batch = csum_exit_net, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 732 | .id = &csum_net_id, |
| 733 | .size = sizeof(struct tc_action_net), |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 734 | }; |
| 735 | |
| 736 | MODULE_DESCRIPTION("Checksum updating actions"); |
| 737 | MODULE_LICENSE("GPL"); |
| 738 | |
| 739 | static int __init csum_init_module(void) |
| 740 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 741 | return tcf_register_action(&act_csum_ops, &csum_net_ops); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | static void __exit csum_cleanup_module(void) |
| 745 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 746 | tcf_unregister_action(&act_csum_ops, &csum_net_ops); |
Grégoire Baron | eb4d406 | 2010-08-18 13:10:35 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | module_init(csum_init_module); |
| 750 | module_exit(csum_cleanup_module); |