xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | /* |
| 2 | * net/sched/act_skbmod.c skb data modifier |
| 3 | * |
| 4 | * Copyright (c) 2016 Jamal Hadi Salim <jhs@mojatatu.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/skbuff.h> |
| 16 | #include <linux/rtnetlink.h> |
| 17 | #include <net/netlink.h> |
| 18 | #include <net/pkt_sched.h> |
| 19 | |
| 20 | #include <linux/tc_act/tc_skbmod.h> |
| 21 | #include <net/tc_act/tc_skbmod.h> |
| 22 | |
| 23 | static unsigned int skbmod_net_id; |
| 24 | static struct tc_action_ops act_skbmod_ops; |
| 25 | |
| 26 | #define MAX_EDIT_LEN ETH_HLEN |
| 27 | static int tcf_skbmod_act(struct sk_buff *skb, const struct tc_action *a, |
| 28 | struct tcf_result *res) |
| 29 | { |
| 30 | struct tcf_skbmod *d = to_skbmod(a); |
| 31 | int action; |
| 32 | struct tcf_skbmod_params *p; |
| 33 | u64 flags; |
| 34 | int err; |
| 35 | |
| 36 | tcf_lastuse_update(&d->tcf_tm); |
| 37 | bstats_cpu_update(this_cpu_ptr(d->common.cpu_bstats), skb); |
| 38 | |
| 39 | /* XXX: if you are going to edit more fields beyond ethernet header |
| 40 | * (example when you add IP header replacement or vlan swap) |
| 41 | * then MAX_EDIT_LEN needs to change appropriately |
| 42 | */ |
| 43 | err = skb_ensure_writable(skb, MAX_EDIT_LEN); |
| 44 | if (unlikely(err)) /* best policy is to drop on the floor */ |
| 45 | goto drop; |
| 46 | |
| 47 | action = READ_ONCE(d->tcf_action); |
| 48 | if (unlikely(action == TC_ACT_SHOT)) |
| 49 | goto drop; |
| 50 | |
| 51 | p = rcu_dereference_bh(d->skbmod_p); |
| 52 | flags = p->flags; |
| 53 | if (flags & SKBMOD_F_DMAC) |
| 54 | ether_addr_copy(eth_hdr(skb)->h_dest, p->eth_dst); |
| 55 | if (flags & SKBMOD_F_SMAC) |
| 56 | ether_addr_copy(eth_hdr(skb)->h_source, p->eth_src); |
| 57 | if (flags & SKBMOD_F_ETYPE) |
| 58 | eth_hdr(skb)->h_proto = p->eth_type; |
| 59 | |
| 60 | if (flags & SKBMOD_F_SWAPMAC) { |
| 61 | u16 tmpaddr[ETH_ALEN / 2]; /* ether_addr_copy() requirement */ |
| 62 | /*XXX: I am sure we can come up with more efficient swapping*/ |
| 63 | ether_addr_copy((u8 *)tmpaddr, eth_hdr(skb)->h_dest); |
| 64 | ether_addr_copy(eth_hdr(skb)->h_dest, eth_hdr(skb)->h_source); |
| 65 | ether_addr_copy(eth_hdr(skb)->h_source, (u8 *)tmpaddr); |
| 66 | } |
| 67 | |
| 68 | return action; |
| 69 | |
| 70 | drop: |
| 71 | qstats_overlimit_inc(this_cpu_ptr(d->common.cpu_qstats)); |
| 72 | return TC_ACT_SHOT; |
| 73 | } |
| 74 | |
| 75 | static const struct nla_policy skbmod_policy[TCA_SKBMOD_MAX + 1] = { |
| 76 | [TCA_SKBMOD_PARMS] = { .len = sizeof(struct tc_skbmod) }, |
| 77 | [TCA_SKBMOD_DMAC] = { .len = ETH_ALEN }, |
| 78 | [TCA_SKBMOD_SMAC] = { .len = ETH_ALEN }, |
| 79 | [TCA_SKBMOD_ETYPE] = { .type = NLA_U16 }, |
| 80 | }; |
| 81 | |
| 82 | static int tcf_skbmod_init(struct net *net, struct nlattr *nla, |
| 83 | struct nlattr *est, struct tc_action **a, |
| 84 | int ovr, int bind, bool rtnl_held, |
| 85 | struct netlink_ext_ack *extack) |
| 86 | { |
| 87 | struct tc_action_net *tn = net_generic(net, skbmod_net_id); |
| 88 | struct nlattr *tb[TCA_SKBMOD_MAX + 1]; |
| 89 | struct tcf_skbmod_params *p, *p_old; |
| 90 | struct tc_skbmod *parm; |
| 91 | u32 lflags = 0, index; |
| 92 | struct tcf_skbmod *d; |
| 93 | bool exists = false; |
| 94 | u8 *daddr = NULL; |
| 95 | u8 *saddr = NULL; |
| 96 | u16 eth_type = 0; |
| 97 | int ret = 0, err; |
| 98 | |
| 99 | if (!nla) |
| 100 | return -EINVAL; |
| 101 | |
| 102 | err = nla_parse_nested(tb, TCA_SKBMOD_MAX, nla, skbmod_policy, NULL); |
| 103 | if (err < 0) |
| 104 | return err; |
| 105 | |
| 106 | if (!tb[TCA_SKBMOD_PARMS]) |
| 107 | return -EINVAL; |
| 108 | |
| 109 | if (tb[TCA_SKBMOD_DMAC]) { |
| 110 | daddr = nla_data(tb[TCA_SKBMOD_DMAC]); |
| 111 | lflags |= SKBMOD_F_DMAC; |
| 112 | } |
| 113 | |
| 114 | if (tb[TCA_SKBMOD_SMAC]) { |
| 115 | saddr = nla_data(tb[TCA_SKBMOD_SMAC]); |
| 116 | lflags |= SKBMOD_F_SMAC; |
| 117 | } |
| 118 | |
| 119 | if (tb[TCA_SKBMOD_ETYPE]) { |
| 120 | eth_type = nla_get_u16(tb[TCA_SKBMOD_ETYPE]); |
| 121 | lflags |= SKBMOD_F_ETYPE; |
| 122 | } |
| 123 | |
| 124 | parm = nla_data(tb[TCA_SKBMOD_PARMS]); |
| 125 | index = parm->index; |
| 126 | if (parm->flags & SKBMOD_F_SWAPMAC) |
| 127 | lflags = SKBMOD_F_SWAPMAC; |
| 128 | |
| 129 | err = tcf_idr_check_alloc(tn, &index, a, bind); |
| 130 | if (err < 0) |
| 131 | return err; |
| 132 | exists = err; |
| 133 | if (exists && bind) |
| 134 | return 0; |
| 135 | |
| 136 | if (!lflags) { |
| 137 | if (exists) |
| 138 | tcf_idr_release(*a, bind); |
| 139 | else |
| 140 | tcf_idr_cleanup(tn, index); |
| 141 | return -EINVAL; |
| 142 | } |
| 143 | |
| 144 | if (!exists) { |
| 145 | ret = tcf_idr_create(tn, index, est, a, |
| 146 | &act_skbmod_ops, bind, true); |
| 147 | if (ret) { |
| 148 | tcf_idr_cleanup(tn, index); |
| 149 | return ret; |
| 150 | } |
| 151 | |
| 152 | ret = ACT_P_CREATED; |
| 153 | } else if (!ovr) { |
| 154 | tcf_idr_release(*a, bind); |
| 155 | return -EEXIST; |
| 156 | } |
| 157 | |
| 158 | d = to_skbmod(*a); |
| 159 | |
| 160 | p = kzalloc(sizeof(struct tcf_skbmod_params), GFP_KERNEL); |
| 161 | if (unlikely(!p)) { |
| 162 | tcf_idr_release(*a, bind); |
| 163 | return -ENOMEM; |
| 164 | } |
| 165 | |
| 166 | p->flags = lflags; |
| 167 | d->tcf_action = parm->action; |
| 168 | |
| 169 | if (ovr) |
| 170 | spin_lock_bh(&d->tcf_lock); |
| 171 | /* Protected by tcf_lock if overwriting existing action. */ |
| 172 | p_old = rcu_dereference_protected(d->skbmod_p, 1); |
| 173 | |
| 174 | if (lflags & SKBMOD_F_DMAC) |
| 175 | ether_addr_copy(p->eth_dst, daddr); |
| 176 | if (lflags & SKBMOD_F_SMAC) |
| 177 | ether_addr_copy(p->eth_src, saddr); |
| 178 | if (lflags & SKBMOD_F_ETYPE) |
| 179 | p->eth_type = htons(eth_type); |
| 180 | |
| 181 | rcu_assign_pointer(d->skbmod_p, p); |
| 182 | if (ovr) |
| 183 | spin_unlock_bh(&d->tcf_lock); |
| 184 | |
| 185 | if (p_old) |
| 186 | kfree_rcu(p_old, rcu); |
| 187 | |
| 188 | if (ret == ACT_P_CREATED) |
| 189 | tcf_idr_insert(tn, *a); |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | static void tcf_skbmod_cleanup(struct tc_action *a) |
| 194 | { |
| 195 | struct tcf_skbmod *d = to_skbmod(a); |
| 196 | struct tcf_skbmod_params *p; |
| 197 | |
| 198 | p = rcu_dereference_protected(d->skbmod_p, 1); |
| 199 | if (p) |
| 200 | kfree_rcu(p, rcu); |
| 201 | } |
| 202 | |
| 203 | static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a, |
| 204 | int bind, int ref) |
| 205 | { |
| 206 | struct tcf_skbmod *d = to_skbmod(a); |
| 207 | unsigned char *b = skb_tail_pointer(skb); |
| 208 | struct tcf_skbmod_params *p; |
| 209 | struct tc_skbmod opt = { |
| 210 | .index = d->tcf_index, |
| 211 | .refcnt = refcount_read(&d->tcf_refcnt) - ref, |
| 212 | .bindcnt = atomic_read(&d->tcf_bindcnt) - bind, |
| 213 | }; |
| 214 | struct tcf_t t; |
| 215 | |
| 216 | spin_lock_bh(&d->tcf_lock); |
| 217 | opt.action = d->tcf_action; |
| 218 | p = rcu_dereference_protected(d->skbmod_p, |
| 219 | lockdep_is_held(&d->tcf_lock)); |
| 220 | opt.flags = p->flags; |
| 221 | if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt)) |
| 222 | goto nla_put_failure; |
| 223 | if ((p->flags & SKBMOD_F_DMAC) && |
| 224 | nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst)) |
| 225 | goto nla_put_failure; |
| 226 | if ((p->flags & SKBMOD_F_SMAC) && |
| 227 | nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src)) |
| 228 | goto nla_put_failure; |
| 229 | if ((p->flags & SKBMOD_F_ETYPE) && |
| 230 | nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type))) |
| 231 | goto nla_put_failure; |
| 232 | |
| 233 | tcf_tm_dump(&t, &d->tcf_tm); |
| 234 | if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD)) |
| 235 | goto nla_put_failure; |
| 236 | |
| 237 | spin_unlock_bh(&d->tcf_lock); |
| 238 | return skb->len; |
| 239 | nla_put_failure: |
| 240 | spin_unlock_bh(&d->tcf_lock); |
| 241 | nlmsg_trim(skb, b); |
| 242 | return -1; |
| 243 | } |
| 244 | |
| 245 | static int tcf_skbmod_walker(struct net *net, struct sk_buff *skb, |
| 246 | struct netlink_callback *cb, int type, |
| 247 | const struct tc_action_ops *ops, |
| 248 | struct netlink_ext_ack *extack) |
| 249 | { |
| 250 | struct tc_action_net *tn = net_generic(net, skbmod_net_id); |
| 251 | |
| 252 | return tcf_generic_walker(tn, skb, cb, type, ops, extack); |
| 253 | } |
| 254 | |
| 255 | static int tcf_skbmod_search(struct net *net, struct tc_action **a, u32 index, |
| 256 | struct netlink_ext_ack *extack) |
| 257 | { |
| 258 | struct tc_action_net *tn = net_generic(net, skbmod_net_id); |
| 259 | |
| 260 | return tcf_idr_search(tn, a, index); |
| 261 | } |
| 262 | |
| 263 | static struct tc_action_ops act_skbmod_ops = { |
| 264 | .kind = "skbmod", |
| 265 | .type = TCA_ACT_SKBMOD, |
| 266 | .owner = THIS_MODULE, |
| 267 | .act = tcf_skbmod_act, |
| 268 | .dump = tcf_skbmod_dump, |
| 269 | .init = tcf_skbmod_init, |
| 270 | .cleanup = tcf_skbmod_cleanup, |
| 271 | .walk = tcf_skbmod_walker, |
| 272 | .lookup = tcf_skbmod_search, |
| 273 | .size = sizeof(struct tcf_skbmod), |
| 274 | }; |
| 275 | |
| 276 | static __net_init int skbmod_init_net(struct net *net) |
| 277 | { |
| 278 | struct tc_action_net *tn = net_generic(net, skbmod_net_id); |
| 279 | |
| 280 | return tc_action_net_init(net, tn, &act_skbmod_ops); |
| 281 | } |
| 282 | |
| 283 | static void __net_exit skbmod_exit_net(struct list_head *net_list) |
| 284 | { |
| 285 | tc_action_net_exit(net_list, skbmod_net_id); |
| 286 | } |
| 287 | |
| 288 | static struct pernet_operations skbmod_net_ops = { |
| 289 | .init = skbmod_init_net, |
| 290 | .exit_batch = skbmod_exit_net, |
| 291 | .id = &skbmod_net_id, |
| 292 | .size = sizeof(struct tc_action_net), |
| 293 | }; |
| 294 | |
| 295 | MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>"); |
| 296 | MODULE_DESCRIPTION("SKB data mod-ing"); |
| 297 | MODULE_LICENSE("GPL"); |
| 298 | |
| 299 | static int __init skbmod_init_module(void) |
| 300 | { |
| 301 | return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops); |
| 302 | } |
| 303 | |
| 304 | static void __exit skbmod_cleanup_module(void) |
| 305 | { |
| 306 | tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops); |
| 307 | } |
| 308 | |
| 309 | module_init(skbmod_init_module); |
| 310 | module_exit(skbmod_cleanup_module); |