Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jiri Pirko | 0c6965d | 2014-11-05 20:51:51 +0100 | [diff] [blame] | 2 | * net/sched/act_pedit.c Generic packet editor |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Jamal Hadi Salim (2002-4) |
| 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/types.h> |
| 13 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/skbuff.h> |
| 17 | #include <linux/rtnetlink.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 21 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <net/pkt_sched.h> |
| 23 | #include <linux/tc_act/tc_pedit.h> |
| 24 | #include <net/tc_act/tc_pedit.h> |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 25 | #include <uapi/linux/tc_act/tc_pedit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Alexey Dobriyan | c7d03a0 | 2016-11-17 04:58:21 +0300 | [diff] [blame] | 27 | static unsigned int pedit_net_id; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 28 | static struct tc_action_ops act_pedit_ops; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 29 | |
Patrick McHardy | 53b2bf3 | 2008-01-23 20:36:30 -0800 | [diff] [blame] | 30 | static const struct nla_policy pedit_policy[TCA_PEDIT_MAX + 1] = { |
jamal | 53f7e35 | 2009-10-11 04:21:38 +0000 | [diff] [blame] | 31 | [TCA_PEDIT_PARMS] = { .len = sizeof(struct tc_pedit) }, |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 32 | [TCA_PEDIT_KEYS_EX] = { .type = NLA_NESTED }, |
Patrick McHardy | 53b2bf3 | 2008-01-23 20:36:30 -0800 | [diff] [blame] | 33 | }; |
| 34 | |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 35 | static const struct nla_policy pedit_key_ex_policy[TCA_PEDIT_KEY_EX_MAX + 1] = { |
| 36 | [TCA_PEDIT_KEY_EX_HTYPE] = { .type = NLA_U16 }, |
Amir Vadai | 853a14b | 2017-02-07 09:56:08 +0200 | [diff] [blame] | 37 | [TCA_PEDIT_KEY_EX_CMD] = { .type = NLA_U16 }, |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | static struct tcf_pedit_key_ex *tcf_pedit_keys_ex_parse(struct nlattr *nla, |
| 41 | u8 n) |
| 42 | { |
| 43 | struct tcf_pedit_key_ex *keys_ex; |
| 44 | struct tcf_pedit_key_ex *k; |
| 45 | const struct nlattr *ka; |
| 46 | int err = -EINVAL; |
| 47 | int rem; |
| 48 | |
| 49 | if (!nla || !n) |
| 50 | return NULL; |
| 51 | |
| 52 | keys_ex = kcalloc(n, sizeof(*k), GFP_KERNEL); |
| 53 | if (!keys_ex) |
| 54 | return ERR_PTR(-ENOMEM); |
| 55 | |
| 56 | k = keys_ex; |
| 57 | |
| 58 | nla_for_each_nested(ka, nla, rem) { |
| 59 | struct nlattr *tb[TCA_PEDIT_KEY_EX_MAX + 1]; |
| 60 | |
| 61 | if (!n) { |
| 62 | err = -EINVAL; |
| 63 | goto err_out; |
| 64 | } |
| 65 | n--; |
| 66 | |
| 67 | if (nla_type(ka) != TCA_PEDIT_KEY_EX) { |
| 68 | err = -EINVAL; |
| 69 | goto err_out; |
| 70 | } |
| 71 | |
| 72 | err = nla_parse_nested(tb, TCA_PEDIT_KEY_EX_MAX, ka, |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 73 | pedit_key_ex_policy, NULL); |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 74 | if (err) |
| 75 | goto err_out; |
| 76 | |
Amir Vadai | 853a14b | 2017-02-07 09:56:08 +0200 | [diff] [blame] | 77 | if (!tb[TCA_PEDIT_KEY_EX_HTYPE] || |
| 78 | !tb[TCA_PEDIT_KEY_EX_CMD]) { |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 79 | err = -EINVAL; |
| 80 | goto err_out; |
| 81 | } |
| 82 | |
| 83 | k->htype = nla_get_u16(tb[TCA_PEDIT_KEY_EX_HTYPE]); |
Amir Vadai | 853a14b | 2017-02-07 09:56:08 +0200 | [diff] [blame] | 84 | k->cmd = nla_get_u16(tb[TCA_PEDIT_KEY_EX_CMD]); |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 85 | |
Amir Vadai | 853a14b | 2017-02-07 09:56:08 +0200 | [diff] [blame] | 86 | if (k->htype > TCA_PEDIT_HDR_TYPE_MAX || |
| 87 | k->cmd > TCA_PEDIT_CMD_MAX) { |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 88 | err = -EINVAL; |
| 89 | goto err_out; |
| 90 | } |
| 91 | |
| 92 | k++; |
| 93 | } |
| 94 | |
Dan Carpenter | c4f65b0 | 2017-06-14 13:29:31 +0300 | [diff] [blame] | 95 | if (n) { |
| 96 | err = -EINVAL; |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 97 | goto err_out; |
Dan Carpenter | c4f65b0 | 2017-06-14 13:29:31 +0300 | [diff] [blame] | 98 | } |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 99 | |
| 100 | return keys_ex; |
| 101 | |
| 102 | err_out: |
| 103 | kfree(keys_ex); |
| 104 | return ERR_PTR(err); |
| 105 | } |
| 106 | |
| 107 | static int tcf_pedit_key_ex_dump(struct sk_buff *skb, |
| 108 | struct tcf_pedit_key_ex *keys_ex, int n) |
| 109 | { |
| 110 | struct nlattr *keys_start = nla_nest_start(skb, TCA_PEDIT_KEYS_EX); |
| 111 | |
| 112 | for (; n > 0; n--) { |
| 113 | struct nlattr *key_start; |
| 114 | |
| 115 | key_start = nla_nest_start(skb, TCA_PEDIT_KEY_EX); |
| 116 | |
Amir Vadai | 853a14b | 2017-02-07 09:56:08 +0200 | [diff] [blame] | 117 | if (nla_put_u16(skb, TCA_PEDIT_KEY_EX_HTYPE, keys_ex->htype) || |
| 118 | nla_put_u16(skb, TCA_PEDIT_KEY_EX_CMD, keys_ex->cmd)) { |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 119 | nlmsg_trim(skb, keys_start); |
| 120 | return -EINVAL; |
| 121 | } |
| 122 | |
| 123 | nla_nest_end(skb, key_start); |
| 124 | |
| 125 | keys_ex++; |
| 126 | } |
| 127 | |
| 128 | nla_nest_end(skb, keys_start); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Benjamin LaHaise | c1b5273 | 2013-01-14 05:15:39 +0000 | [diff] [blame] | 133 | static int tcf_pedit_init(struct net *net, struct nlattr *nla, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 134 | struct nlattr *est, struct tc_action **a, |
Alexander Aring | 589dad6 | 2018-02-15 10:54:56 -0500 | [diff] [blame] | 135 | int ovr, int bind, struct netlink_ext_ack *extack) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 137 | struct tc_action_net *tn = net_generic(net, pedit_net_id); |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 138 | struct nlattr *tb[TCA_PEDIT_MAX + 1]; |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 139 | struct nlattr *pattr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | struct tc_pedit *parm; |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 141 | int ret = 0, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | struct tcf_pedit *p; |
| 143 | struct tc_pedit_key *keys = NULL; |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 144 | struct tcf_pedit_key_ex *keys_ex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | int ksize; |
| 146 | |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 147 | if (nla == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | return -EINVAL; |
| 149 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 150 | err = nla_parse_nested(tb, TCA_PEDIT_MAX, nla, pedit_policy, NULL); |
Patrick McHardy | cee6372 | 2008-01-23 20:33:32 -0800 | [diff] [blame] | 151 | if (err < 0) |
| 152 | return err; |
| 153 | |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 154 | pattr = tb[TCA_PEDIT_PARMS]; |
| 155 | if (!pattr) |
| 156 | pattr = tb[TCA_PEDIT_PARMS_EX]; |
| 157 | if (!pattr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | return -EINVAL; |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 159 | |
| 160 | parm = nla_data(pattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | ksize = parm->nkeys * sizeof(struct tc_pedit_key); |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 162 | if (nla_len(pattr) < sizeof(*parm) + ksize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | return -EINVAL; |
| 164 | |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 165 | keys_ex = tcf_pedit_keys_ex_parse(tb[TCA_PEDIT_KEYS_EX], parm->nkeys); |
| 166 | if (IS_ERR(keys_ex)) |
| 167 | return PTR_ERR(keys_ex); |
| 168 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 169 | if (!tcf_idr_check(tn, parm->index, a, bind)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | if (!parm->nkeys) |
| 171 | return -EINVAL; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 172 | ret = tcf_idr_create(tn, parm->index, est, a, |
| 173 | &act_pedit_ops, bind, false); |
WANG Cong | 8606203 | 2014-02-11 17:07:31 -0800 | [diff] [blame] | 174 | if (ret) |
| 175 | return ret; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 176 | p = to_pedit(*a); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | keys = kmalloc(ksize, GFP_KERNEL); |
| 178 | if (keys == NULL) { |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 179 | tcf_idr_cleanup(*a, est); |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 180 | kfree(keys_ex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | return -ENOMEM; |
| 182 | } |
| 183 | ret = ACT_P_CREATED; |
| 184 | } else { |
Jamal Hadi Salim | 1a29321 | 2013-12-23 08:02:11 -0500 | [diff] [blame] | 185 | if (bind) |
| 186 | return 0; |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 187 | tcf_idr_release(*a, bind); |
Jamal Hadi Salim | 1a29321 | 2013-12-23 08:02:11 -0500 | [diff] [blame] | 188 | if (!ovr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | return -EEXIST; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 190 | p = to_pedit(*a); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 191 | if (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | keys = kmalloc(ksize, GFP_KERNEL); |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 193 | if (!keys) { |
| 194 | kfree(keys_ex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | return -ENOMEM; |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 196 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 200 | spin_lock_bh(&p->tcf_lock); |
| 201 | p->tcfp_flags = parm->flags; |
| 202 | p->tcf_action = parm->action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | if (keys) { |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 204 | kfree(p->tcfp_keys); |
| 205 | p->tcfp_keys = keys; |
| 206 | p->tcfp_nkeys = parm->nkeys; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | } |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 208 | memcpy(p->tcfp_keys, parm->keys, ksize); |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 209 | |
| 210 | kfree(p->tcfp_keys_ex); |
| 211 | p->tcfp_keys_ex = keys_ex; |
| 212 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 213 | spin_unlock_bh(&p->tcf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | if (ret == ACT_P_CREATED) |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 215 | tcf_idr_insert(tn, *a); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | return ret; |
| 217 | } |
| 218 | |
Cong Wang | 9a63b25 | 2017-12-05 12:53:07 -0800 | [diff] [blame] | 219 | static void tcf_pedit_cleanup(struct tc_action *a) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 221 | struct tcf_pedit *p = to_pedit(a); |
WANG Cong | a5b5c95 | 2014-02-11 17:07:32 -0800 | [diff] [blame] | 222 | struct tc_pedit_key *keys = p->tcfp_keys; |
| 223 | kfree(keys); |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 224 | kfree(p->tcfp_keys_ex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Amir Vadai | 95c2027 | 2016-11-28 12:56:40 +0200 | [diff] [blame] | 227 | static bool offset_valid(struct sk_buff *skb, int offset) |
| 228 | { |
| 229 | if (offset > 0 && offset > skb->len) |
| 230 | return false; |
| 231 | |
| 232 | if (offset < 0 && -offset > skb_headroom(skb)) |
| 233 | return false; |
| 234 | |
| 235 | return true; |
| 236 | } |
| 237 | |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 238 | static int pedit_skb_hdr_offset(struct sk_buff *skb, |
| 239 | enum pedit_header_type htype, int *hoffset) |
| 240 | { |
| 241 | int ret = -EINVAL; |
| 242 | |
| 243 | switch (htype) { |
| 244 | case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH: |
| 245 | if (skb_mac_header_was_set(skb)) { |
| 246 | *hoffset = skb_mac_offset(skb); |
| 247 | ret = 0; |
| 248 | } |
| 249 | break; |
| 250 | case TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK: |
| 251 | case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4: |
| 252 | case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6: |
| 253 | *hoffset = skb_network_offset(skb); |
| 254 | ret = 0; |
| 255 | break; |
| 256 | case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP: |
| 257 | case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP: |
| 258 | if (skb_transport_header_was_set(skb)) { |
| 259 | *hoffset = skb_transport_offset(skb); |
| 260 | ret = 0; |
| 261 | } |
| 262 | break; |
| 263 | default: |
| 264 | ret = -EINVAL; |
| 265 | break; |
| 266 | }; |
| 267 | |
| 268 | return ret; |
| 269 | } |
| 270 | |
Eric Dumazet | dc7f9f6 | 2011-07-05 23:25:42 +0000 | [diff] [blame] | 271 | static int tcf_pedit(struct sk_buff *skb, const struct tc_action *a, |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 272 | struct tcf_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 274 | struct tcf_pedit *p = to_pedit(a); |
Florian Westphal | 4749c3ef | 2015-04-30 12:12:00 +0200 | [diff] [blame] | 275 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | |
Pravin B Shelar | 14bbd6a | 2013-02-14 09:44:49 +0000 | [diff] [blame] | 277 | if (skb_unclone(skb, GFP_ATOMIC)) |
Eric Dumazet | cc7ec45 | 2011-01-19 19:26:56 +0000 | [diff] [blame] | 278 | return p->tcf_action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 280 | spin_lock(&p->tcf_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | |
Jamal Hadi Salim | 9c4a4e4 | 2016-06-06 06:32:53 -0400 | [diff] [blame] | 282 | tcf_lastuse_update(&p->tcf_tm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 284 | if (p->tcfp_nkeys > 0) { |
| 285 | struct tc_pedit_key *tkey = p->tcfp_keys; |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 286 | struct tcf_pedit_key_ex *tkey_ex = p->tcfp_keys_ex; |
| 287 | enum pedit_header_type htype = TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK; |
Amir Vadai | 853a14b | 2017-02-07 09:56:08 +0200 | [diff] [blame] | 288 | enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 290 | for (i = p->tcfp_nkeys; i > 0; i--, tkey++) { |
Changli Gao | db2c241 | 2010-06-02 04:55:02 +0000 | [diff] [blame] | 291 | u32 *ptr, _data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | int offset = tkey->off; |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 293 | int hoffset; |
Amir Vadai | 853a14b | 2017-02-07 09:56:08 +0200 | [diff] [blame] | 294 | u32 val; |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 295 | int rc; |
| 296 | |
| 297 | if (tkey_ex) { |
| 298 | htype = tkey_ex->htype; |
Amir Vadai | 853a14b | 2017-02-07 09:56:08 +0200 | [diff] [blame] | 299 | cmd = tkey_ex->cmd; |
| 300 | |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 301 | tkey_ex++; |
| 302 | } |
| 303 | |
| 304 | rc = pedit_skb_hdr_offset(skb, htype, &hoffset); |
| 305 | if (rc) { |
| 306 | pr_info("tc filter pedit bad header type specified (0x%x)\n", |
| 307 | htype); |
| 308 | goto bad; |
| 309 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
| 311 | if (tkey->offmask) { |
Changli Gao | db2c241 | 2010-06-02 04:55:02 +0000 | [diff] [blame] | 312 | char *d, _d; |
| 313 | |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 314 | if (!offset_valid(skb, hoffset + tkey->at)) { |
Amir Vadai | 95c2027 | 2016-11-28 12:56:40 +0200 | [diff] [blame] | 315 | pr_info("tc filter pedit 'at' offset %d out of bounds\n", |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 316 | hoffset + tkey->at); |
Amir Vadai | 95c2027 | 2016-11-28 12:56:40 +0200 | [diff] [blame] | 317 | goto bad; |
| 318 | } |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 319 | d = skb_header_pointer(skb, hoffset + tkey->at, 1, |
Changli Gao | db2c241 | 2010-06-02 04:55:02 +0000 | [diff] [blame] | 320 | &_d); |
| 321 | if (!d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | goto bad; |
Changli Gao | db2c241 | 2010-06-02 04:55:02 +0000 | [diff] [blame] | 323 | offset += (*d & tkey->offmask) >> tkey->shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | if (offset % 4) { |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 327 | pr_info("tc filter pedit" |
| 328 | " offset must be on 32 bit boundaries\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | goto bad; |
| 330 | } |
Amir Vadai | 95c2027 | 2016-11-28 12:56:40 +0200 | [diff] [blame] | 331 | |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 332 | if (!offset_valid(skb, hoffset + offset)) { |
Amir Vadai | 95c2027 | 2016-11-28 12:56:40 +0200 | [diff] [blame] | 333 | pr_info("tc filter pedit offset %d out of bounds\n", |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 334 | hoffset + offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | goto bad; |
| 336 | } |
| 337 | |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 338 | ptr = skb_header_pointer(skb, hoffset + offset, 4, &_data); |
Changli Gao | db2c241 | 2010-06-02 04:55:02 +0000 | [diff] [blame] | 339 | if (!ptr) |
| 340 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | /* just do it, baby */ |
Amir Vadai | 853a14b | 2017-02-07 09:56:08 +0200 | [diff] [blame] | 342 | switch (cmd) { |
| 343 | case TCA_PEDIT_KEY_EX_CMD_SET: |
| 344 | val = tkey->val; |
| 345 | break; |
| 346 | case TCA_PEDIT_KEY_EX_CMD_ADD: |
| 347 | val = (*ptr + tkey->val) & ~tkey->mask; |
| 348 | break; |
| 349 | default: |
| 350 | pr_info("tc filter pedit bad command (%d)\n", |
| 351 | cmd); |
| 352 | goto bad; |
| 353 | } |
| 354 | |
| 355 | *ptr = ((*ptr & tkey->mask) ^ val); |
Changli Gao | db2c241 | 2010-06-02 04:55:02 +0000 | [diff] [blame] | 356 | if (ptr == &_data) |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 357 | skb_store_bits(skb, hoffset + offset, ptr, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | goto done; |
stephen hemminger | 6ff9c36 | 2010-05-12 06:37:05 +0000 | [diff] [blame] | 361 | } else |
| 362 | WARN(1, "pedit BUG: index %d\n", p->tcf_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
| 364 | bad: |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 365 | p->tcf_qstats.overlimits++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | done: |
Eric Dumazet | bfe0d02 | 2011-01-09 08:30:54 +0000 | [diff] [blame] | 367 | bstats_update(&p->tcf_bstats, skb); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 368 | spin_unlock(&p->tcf_lock); |
| 369 | return p->tcf_action; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
| 371 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 372 | static int tcf_pedit_dump(struct sk_buff *skb, struct tc_action *a, |
| 373 | int bind, int ref) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | { |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 375 | unsigned char *b = skb_tail_pointer(skb); |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 376 | struct tcf_pedit *p = to_pedit(a); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | struct tc_pedit *opt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | struct tcf_t t; |
YOSHIFUJI Hideaki | 10297b9 | 2007-02-09 23:25:16 +0900 | [diff] [blame] | 379 | int s; |
| 380 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 381 | s = sizeof(*opt) + p->tcfp_nkeys * sizeof(struct tc_pedit_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | |
| 383 | /* netlink spinlocks held above us - must use ATOMIC */ |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 384 | opt = kzalloc(s, GFP_ATOMIC); |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 385 | if (unlikely(!opt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | return -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 388 | memcpy(opt->keys, p->tcfp_keys, |
| 389 | p->tcfp_nkeys * sizeof(struct tc_pedit_key)); |
| 390 | opt->index = p->tcf_index; |
| 391 | opt->nkeys = p->tcfp_nkeys; |
| 392 | opt->flags = p->tcfp_flags; |
| 393 | opt->action = p->tcf_action; |
| 394 | opt->refcnt = p->tcf_refcnt - ref; |
| 395 | opt->bindcnt = p->tcf_bindcnt - bind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
Amir Vadai | 71d0ed7 | 2017-02-07 09:56:07 +0200 | [diff] [blame] | 397 | if (p->tcfp_keys_ex) { |
| 398 | tcf_pedit_key_ex_dump(skb, p->tcfp_keys_ex, p->tcfp_nkeys); |
| 399 | |
| 400 | if (nla_put(skb, TCA_PEDIT_PARMS_EX, s, opt)) |
| 401 | goto nla_put_failure; |
| 402 | } else { |
| 403 | if (nla_put(skb, TCA_PEDIT_PARMS, s, opt)) |
| 404 | goto nla_put_failure; |
| 405 | } |
Jamal Hadi Salim | 48d8ee1 | 2016-06-06 06:32:55 -0400 | [diff] [blame] | 406 | |
| 407 | tcf_tm_dump(&t, &p->tcf_tm); |
Nicolas Dichtel | 9854518e | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 408 | if (nla_put_64bit(skb, TCA_PEDIT_TM, sizeof(t), &t, TCA_PEDIT_PAD)) |
David S. Miller | 1b34ec4 | 2012-03-29 05:11:39 -0400 | [diff] [blame] | 409 | goto nla_put_failure; |
Jamal Hadi Salim | 48d8ee1 | 2016-06-06 06:32:55 -0400 | [diff] [blame] | 410 | |
Patrick McHardy | 541673c8 | 2006-01-08 22:17:27 -0800 | [diff] [blame] | 411 | kfree(opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | return skb->len; |
| 413 | |
Patrick McHardy | 7ba699c | 2008-01-22 22:11:50 -0800 | [diff] [blame] | 414 | nla_put_failure: |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 415 | nlmsg_trim(skb, b); |
Patrick McHardy | 541673c8 | 2006-01-08 22:17:27 -0800 | [diff] [blame] | 416 | kfree(opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | return -1; |
| 418 | } |
| 419 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 420 | static int tcf_pedit_walker(struct net *net, struct sk_buff *skb, |
| 421 | struct netlink_callback *cb, int type, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 422 | const struct tc_action_ops *ops) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 423 | { |
| 424 | struct tc_action_net *tn = net_generic(net, pedit_net_id); |
| 425 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 426 | return tcf_generic_walker(tn, skb, cb, type, ops); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Alexander Aring | 331a929 | 2018-02-15 10:54:57 -0500 | [diff] [blame^] | 429 | static int tcf_pedit_search(struct net *net, struct tc_action **a, u32 index, |
| 430 | struct netlink_ext_ack *extack) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 431 | { |
| 432 | struct tc_action_net *tn = net_generic(net, pedit_net_id); |
| 433 | |
Chris Mi | 65a206c | 2017-08-30 02:31:59 -0400 | [diff] [blame] | 434 | return tcf_idr_search(tn, a, index); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 435 | } |
| 436 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 437 | static struct tc_action_ops act_pedit_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | .kind = "pedit", |
| 439 | .type = TCA_ACT_PEDIT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | .owner = THIS_MODULE, |
| 441 | .act = tcf_pedit, |
| 442 | .dump = tcf_pedit_dump, |
| 443 | .cleanup = tcf_pedit_cleanup, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | .init = tcf_pedit_init, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 445 | .walk = tcf_pedit_walker, |
| 446 | .lookup = tcf_pedit_search, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 447 | .size = sizeof(struct tcf_pedit), |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 448 | }; |
| 449 | |
| 450 | static __net_init int pedit_init_net(struct net *net) |
| 451 | { |
| 452 | struct tc_action_net *tn = net_generic(net, pedit_net_id); |
| 453 | |
Cong Wang | c7e460c | 2017-11-06 13:47:18 -0800 | [diff] [blame] | 454 | return tc_action_net_init(tn, &act_pedit_ops); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 457 | static void __net_exit pedit_exit_net(struct list_head *net_list) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 458 | { |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 459 | tc_action_net_exit(net_list, pedit_net_id); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | static struct pernet_operations pedit_net_ops = { |
| 463 | .init = pedit_init_net, |
Cong Wang | 039af9c | 2017-12-11 15:35:03 -0800 | [diff] [blame] | 464 | .exit_batch = pedit_exit_net, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 465 | .id = &pedit_net_id, |
| 466 | .size = sizeof(struct tc_action_net), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | }; |
| 468 | |
| 469 | MODULE_AUTHOR("Jamal Hadi Salim(2002-4)"); |
| 470 | MODULE_DESCRIPTION("Generic Packet Editor actions"); |
| 471 | MODULE_LICENSE("GPL"); |
| 472 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 473 | static int __init pedit_init_module(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 475 | return tcf_register_action(&act_pedit_ops, &pedit_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
| 477 | |
David S. Miller | e9ce1cd | 2006-08-21 23:54:55 -0700 | [diff] [blame] | 478 | static void __exit pedit_cleanup_module(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 480 | tcf_unregister_action(&act_pedit_ops, &pedit_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | module_init(pedit_init_module); |
| 484 | module_exit(pedit_cleanup_module); |
| 485 | |