1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * net/sched/act_skbmod.c skb data modifier 4 * 5 * Copyright (c) 2016 Jamal Hadi Salim <jhs@mojatatu.com> 6 */ 7 8 #include <linux/module.h> 9 #include <linux/if_arp.h> 10 #include <linux/init.h> 11 #include <linux/kernel.h> 12 #include <linux/skbuff.h> 13 #include <linux/rtnetlink.h> 14 #include <net/inet_ecn.h> 15 #include <net/netlink.h> 16 #include <net/pkt_sched.h> 17 #include <net/pkt_cls.h> 18 #include <net/tc_wrapper.h> 19 20 #include <linux/tc_act/tc_skbmod.h> 21 #include <net/tc_act/tc_skbmod.h> 22 23 static struct tc_action_ops act_skbmod_ops; 24 25 TC_INDIRECT_SCOPE int tcf_skbmod_act(struct sk_buff *skb, 26 const struct tc_action *a, 27 struct tcf_result *res) 28 { 29 struct tcf_skbmod *d = to_skbmod(a); 30 struct tcf_skbmod_params *p; 31 int max_edit_len, err; 32 u64 flags; 33 34 tcf_lastuse_update(&d->tcf_tm); 35 bstats_update(this_cpu_ptr(d->common.cpu_bstats), skb); 36 37 p = rcu_dereference_bh(d->skbmod_p); 38 if (unlikely(p->action == TC_ACT_SHOT)) 39 goto drop; 40 41 flags = p->flags; 42 43 /* tcf_skbmod_init() guarantees "flags" to be one of the following: 44 * 1. a combination of SKBMOD_F_{DMAC,SMAC,ETYPE} 45 * 2. SKBMOD_F_SWAPMAC 46 * 3. SKBMOD_F_ECN 47 * SKBMOD_F_ECN only works with IP packets; all other flags only work with Ethernet 48 * packets. 49 */ 50 if (flags == SKBMOD_F_ECN) { 51 switch (skb_protocol(skb, true)) { 52 case cpu_to_be16(ETH_P_IP): 53 max_edit_len = sizeof(struct iphdr); 54 break; 55 case cpu_to_be16(ETH_P_IPV6): 56 max_edit_len = sizeof(struct ipv6hdr); 57 break; 58 default: 59 goto out; 60 } 61 max_edit_len += skb_network_offset(skb); 62 } else { 63 if (!skb->dev || skb->dev->type != ARPHRD_ETHER) 64 goto out; 65 max_edit_len = ETH_HLEN; 66 } 67 68 err = skb_ensure_writable(skb, max_edit_len); 69 if (unlikely(err)) /* best policy is to drop on the floor */ 70 goto drop; 71 72 if (flags & SKBMOD_F_DMAC) 73 ether_addr_copy(eth_hdr(skb)->h_dest, p->eth_dst); 74 if (flags & SKBMOD_F_SMAC) 75 ether_addr_copy(eth_hdr(skb)->h_source, p->eth_src); 76 if (flags & SKBMOD_F_ETYPE) 77 eth_hdr(skb)->h_proto = p->eth_type; 78 79 if (flags & SKBMOD_F_SWAPMAC) { 80 u16 tmpaddr[ETH_ALEN / 2]; /* ether_addr_copy() requirement */ 81 /*XXX: I am sure we can come up with more efficient swapping*/ 82 ether_addr_copy((u8 *)tmpaddr, eth_hdr(skb)->h_dest); 83 ether_addr_copy(eth_hdr(skb)->h_dest, eth_hdr(skb)->h_source); 84 ether_addr_copy(eth_hdr(skb)->h_source, (u8 *)tmpaddr); 85 } 86 87 if (flags & SKBMOD_F_ECN) 88 INET_ECN_set_ce(skb); 89 90 out: 91 return p->action; 92 93 drop: 94 qstats_cpu_overlimit_inc(d->common.cpu_qstats); 95 return TC_ACT_SHOT; 96 } 97 98 static const struct nla_policy skbmod_policy[TCA_SKBMOD_MAX + 1] = { 99 [TCA_SKBMOD_PARMS] = { .len = sizeof(struct tc_skbmod) }, 100 [TCA_SKBMOD_DMAC] = { .len = ETH_ALEN }, 101 [TCA_SKBMOD_SMAC] = { .len = ETH_ALEN }, 102 [TCA_SKBMOD_ETYPE] = { .type = NLA_U16 }, 103 }; 104 105 static int tcf_skbmod_init(struct net *net, struct nlattr *nla, 106 struct nlattr *est, struct tc_action **a, 107 struct tcf_proto *tp, u32 flags, 108 struct netlink_ext_ack *extack) 109 { 110 struct tc_action_net *tn = net_generic(net, act_skbmod_ops.net_id); 111 bool ovr = flags & TCA_ACT_FLAGS_REPLACE; 112 bool bind = flags & TCA_ACT_FLAGS_BIND; 113 struct nlattr *tb[TCA_SKBMOD_MAX + 1]; 114 struct tcf_skbmod_params *p, *p_old; 115 struct tcf_chain *goto_ch = NULL; 116 struct tc_skbmod *parm; 117 u32 lflags = 0, index; 118 struct tcf_skbmod *d; 119 bool exists = false; 120 u8 *daddr = NULL; 121 u8 *saddr = NULL; 122 u16 eth_type = 0; 123 int ret = 0, err; 124 125 if (!nla) 126 return -EINVAL; 127 128 err = nla_parse_nested_deprecated(tb, TCA_SKBMOD_MAX, nla, 129 skbmod_policy, NULL); 130 if (err < 0) 131 return err; 132 133 if (!tb[TCA_SKBMOD_PARMS]) 134 return -EINVAL; 135 136 if (tb[TCA_SKBMOD_DMAC]) { 137 daddr = nla_data(tb[TCA_SKBMOD_DMAC]); 138 lflags |= SKBMOD_F_DMAC; 139 } 140 141 if (tb[TCA_SKBMOD_SMAC]) { 142 saddr = nla_data(tb[TCA_SKBMOD_SMAC]); 143 lflags |= SKBMOD_F_SMAC; 144 } 145 146 if (tb[TCA_SKBMOD_ETYPE]) { 147 eth_type = nla_get_u16(tb[TCA_SKBMOD_ETYPE]); 148 lflags |= SKBMOD_F_ETYPE; 149 } 150 151 parm = nla_data(tb[TCA_SKBMOD_PARMS]); 152 index = parm->index; 153 if (parm->flags & SKBMOD_F_SWAPMAC) 154 lflags = SKBMOD_F_SWAPMAC; 155 if (parm->flags & SKBMOD_F_ECN) 156 lflags = SKBMOD_F_ECN; 157 158 err = tcf_idr_check_alloc(tn, &index, a, bind); 159 if (err < 0) 160 return err; 161 exists = err; 162 if (exists && bind) 163 return ACT_P_BOUND; 164 165 if (!lflags) { 166 if (exists) 167 tcf_idr_release(*a, bind); 168 else 169 tcf_idr_cleanup(tn, index); 170 return -EINVAL; 171 } 172 173 if (!exists) { 174 ret = tcf_idr_create(tn, index, est, a, 175 &act_skbmod_ops, bind, true, flags); 176 if (ret) { 177 tcf_idr_cleanup(tn, index); 178 return ret; 179 } 180 181 ret = ACT_P_CREATED; 182 } else if (!ovr) { 183 tcf_idr_release(*a, bind); 184 return -EEXIST; 185 } 186 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); 187 if (err < 0) 188 goto release_idr; 189 190 d = to_skbmod(*a); 191 192 p = kzalloc_obj(struct tcf_skbmod_params); 193 if (unlikely(!p)) { 194 err = -ENOMEM; 195 goto put_chain; 196 } 197 198 p->flags = lflags; 199 p->action = parm->action; 200 if (ovr) 201 spin_lock_bh(&d->tcf_lock); 202 /* Protected by tcf_lock if overwriting existing action. */ 203 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); 204 p_old = rcu_dereference_protected(d->skbmod_p, 1); 205 206 if (lflags & SKBMOD_F_DMAC) 207 ether_addr_copy(p->eth_dst, daddr); 208 if (lflags & SKBMOD_F_SMAC) 209 ether_addr_copy(p->eth_src, saddr); 210 if (lflags & SKBMOD_F_ETYPE) 211 p->eth_type = htons(eth_type); 212 213 rcu_assign_pointer(d->skbmod_p, p); 214 if (ovr) 215 spin_unlock_bh(&d->tcf_lock); 216 217 if (p_old) 218 kfree_rcu(p_old, rcu); 219 if (goto_ch) 220 tcf_chain_put_by_act(goto_ch); 221 222 return ret; 223 put_chain: 224 if (goto_ch) 225 tcf_chain_put_by_act(goto_ch); 226 release_idr: 227 tcf_idr_release(*a, bind); 228 return err; 229 } 230 231 static void tcf_skbmod_cleanup(struct tc_action *a) 232 { 233 struct tcf_skbmod *d = to_skbmod(a); 234 struct tcf_skbmod_params *p; 235 236 p = rcu_dereference_protected(d->skbmod_p, 1); 237 if (p) 238 kfree_rcu(p, rcu); 239 } 240 241 static int tcf_skbmod_dump(struct sk_buff *skb, struct tc_action *a, 242 int bind, int ref) 243 { 244 struct tcf_skbmod *d = to_skbmod(a); 245 unsigned char *b = skb_tail_pointer(skb); 246 struct tcf_skbmod_params *p; 247 struct tc_skbmod opt; 248 struct tcf_t t; 249 250 memset(&opt, 0, sizeof(opt)); 251 opt.index = d->tcf_index; 252 opt.refcnt = refcount_read(&d->tcf_refcnt) - ref; 253 opt.bindcnt = atomic_read(&d->tcf_bindcnt) - bind; 254 rcu_read_lock(); 255 p = rcu_dereference(d->skbmod_p); 256 opt.action = p->action; 257 opt.flags = p->flags; 258 if (nla_put(skb, TCA_SKBMOD_PARMS, sizeof(opt), &opt)) 259 goto nla_put_failure; 260 if ((p->flags & SKBMOD_F_DMAC) && 261 nla_put(skb, TCA_SKBMOD_DMAC, ETH_ALEN, p->eth_dst)) 262 goto nla_put_failure; 263 if ((p->flags & SKBMOD_F_SMAC) && 264 nla_put(skb, TCA_SKBMOD_SMAC, ETH_ALEN, p->eth_src)) 265 goto nla_put_failure; 266 if ((p->flags & SKBMOD_F_ETYPE) && 267 nla_put_u16(skb, TCA_SKBMOD_ETYPE, ntohs(p->eth_type))) 268 goto nla_put_failure; 269 270 tcf_tm_dump(&t, &d->tcf_tm); 271 if (nla_put_64bit(skb, TCA_SKBMOD_TM, sizeof(t), &t, TCA_SKBMOD_PAD)) 272 goto nla_put_failure; 273 274 rcu_read_unlock(); 275 return skb->len; 276 nla_put_failure: 277 rcu_read_unlock(); 278 nlmsg_trim(skb, b); 279 return -1; 280 } 281 282 static struct tc_action_ops act_skbmod_ops = { 283 .kind = "skbmod", 284 .id = TCA_ACT_SKBMOD, 285 .owner = THIS_MODULE, 286 .act = tcf_skbmod_act, 287 .dump = tcf_skbmod_dump, 288 .init = tcf_skbmod_init, 289 .cleanup = tcf_skbmod_cleanup, 290 .size = sizeof(struct tcf_skbmod), 291 }; 292 MODULE_ALIAS_NET_ACT("skbmod"); 293 294 static __net_init int skbmod_init_net(struct net *net) 295 { 296 struct tc_action_net *tn = net_generic(net, act_skbmod_ops.net_id); 297 298 return tc_action_net_init(net, tn, &act_skbmod_ops); 299 } 300 301 static void __net_exit skbmod_exit_net(struct list_head *net_list) 302 { 303 tc_action_net_exit(net_list, act_skbmod_ops.net_id); 304 } 305 306 static struct pernet_operations skbmod_net_ops = { 307 .init = skbmod_init_net, 308 .exit_batch = skbmod_exit_net, 309 .id = &act_skbmod_ops.net_id, 310 .size = sizeof(struct tc_action_net), 311 }; 312 313 MODULE_AUTHOR("Jamal Hadi Salim, <jhs@mojatatu.com>"); 314 MODULE_DESCRIPTION("SKB data mod-ing"); 315 MODULE_LICENSE("GPL"); 316 317 static int __init skbmod_init_module(void) 318 { 319 return tcf_register_action(&act_skbmod_ops, &skbmod_net_ops); 320 } 321 322 static void __exit skbmod_cleanup_module(void) 323 { 324 tcf_unregister_action(&act_skbmod_ops, &skbmod_net_ops); 325 } 326 327 module_init(skbmod_init_module); 328 module_exit(skbmod_cleanup_module); 329