Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/skbuff.h> |
| 14 | #include <linux/rtnetlink.h> |
| 15 | #include <linux/if_vlan.h> |
| 16 | #include <net/netlink.h> |
| 17 | #include <net/pkt_sched.h> |
| 18 | |
| 19 | #include <linux/tc_act/tc_vlan.h> |
| 20 | #include <net/tc_act/tc_vlan.h> |
| 21 | |
| 22 | #define VLAN_TAB_MASK 15 |
| 23 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 24 | static int vlan_net_id; |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 25 | static struct tc_action_ops act_vlan_ops; |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 26 | |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 27 | static int tcf_vlan(struct sk_buff *skb, const struct tc_action *a, |
| 28 | struct tcf_result *res) |
| 29 | { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 30 | struct tcf_vlan *v = to_vlan(a); |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 31 | int action; |
| 32 | int err; |
Shmulik Ladkani | 45a497f | 2016-09-19 19:11:10 +0300 | [diff] [blame] | 33 | u16 tci; |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 34 | |
| 35 | spin_lock(&v->tcf_lock); |
Jamal Hadi Salim | 9c4a4e4 | 2016-06-06 06:32:53 -0400 | [diff] [blame] | 36 | tcf_lastuse_update(&v->tcf_tm); |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 37 | bstats_update(&v->tcf_bstats, skb); |
| 38 | action = v->tcf_action; |
| 39 | |
| 40 | switch (v->tcfv_action) { |
| 41 | case TCA_VLAN_ACT_POP: |
| 42 | err = skb_vlan_pop(skb); |
| 43 | if (err) |
| 44 | goto drop; |
| 45 | break; |
| 46 | case TCA_VLAN_ACT_PUSH: |
Hadar Hen Zion | 956af37 | 2016-08-17 13:36:14 +0300 | [diff] [blame] | 47 | err = skb_vlan_push(skb, v->tcfv_push_proto, v->tcfv_push_vid | |
| 48 | (v->tcfv_push_prio << VLAN_PRIO_SHIFT)); |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 49 | if (err) |
| 50 | goto drop; |
| 51 | break; |
Shmulik Ladkani | 45a497f | 2016-09-19 19:11:10 +0300 | [diff] [blame] | 52 | case TCA_VLAN_ACT_MODIFY: |
| 53 | /* No-op if no vlan tag (either hw-accel or in-payload) */ |
| 54 | if (!skb_vlan_tagged(skb)) |
| 55 | goto unlock; |
| 56 | /* extract existing tag (and guarantee no hw-accel tag) */ |
| 57 | if (skb_vlan_tag_present(skb)) { |
| 58 | tci = skb_vlan_tag_get(skb); |
| 59 | skb->vlan_tci = 0; |
| 60 | } else { |
| 61 | /* in-payload vlan tag, pop it */ |
| 62 | err = __skb_vlan_pop(skb, &tci); |
| 63 | if (err) |
| 64 | goto drop; |
| 65 | } |
| 66 | /* replace the vid */ |
| 67 | tci = (tci & ~VLAN_VID_MASK) | v->tcfv_push_vid; |
| 68 | /* replace prio bits, if tcfv_push_prio specified */ |
| 69 | if (v->tcfv_push_prio) { |
| 70 | tci &= ~VLAN_PRIO_MASK; |
| 71 | tci |= v->tcfv_push_prio << VLAN_PRIO_SHIFT; |
| 72 | } |
| 73 | /* put updated tci as hwaccel tag */ |
| 74 | __vlan_hwaccel_put_tag(skb, v->tcfv_push_proto, tci); |
| 75 | break; |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 76 | default: |
| 77 | BUG(); |
| 78 | } |
| 79 | |
| 80 | goto unlock; |
| 81 | |
| 82 | drop: |
| 83 | action = TC_ACT_SHOT; |
| 84 | v->tcf_qstats.drops++; |
| 85 | unlock: |
| 86 | spin_unlock(&v->tcf_lock); |
| 87 | return action; |
| 88 | } |
| 89 | |
| 90 | static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = { |
| 91 | [TCA_VLAN_PARMS] = { .len = sizeof(struct tc_vlan) }, |
| 92 | [TCA_VLAN_PUSH_VLAN_ID] = { .type = NLA_U16 }, |
| 93 | [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NLA_U16 }, |
Hadar Hen Zion | 956af37 | 2016-08-17 13:36:14 +0300 | [diff] [blame] | 94 | [TCA_VLAN_PUSH_VLAN_PRIORITY] = { .type = NLA_U8 }, |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | static int tcf_vlan_init(struct net *net, struct nlattr *nla, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 98 | struct nlattr *est, struct tc_action **a, |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 99 | int ovr, int bind) |
| 100 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 101 | struct tc_action_net *tn = net_generic(net, vlan_net_id); |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 102 | struct nlattr *tb[TCA_VLAN_MAX + 1]; |
| 103 | struct tc_vlan *parm; |
| 104 | struct tcf_vlan *v; |
| 105 | int action; |
| 106 | __be16 push_vid = 0; |
| 107 | __be16 push_proto = 0; |
Hadar Hen Zion | 956af37 | 2016-08-17 13:36:14 +0300 | [diff] [blame] | 108 | u8 push_prio = 0; |
WANG Cong | b231307 | 2016-06-13 13:46:28 -0700 | [diff] [blame] | 109 | bool exists = false; |
| 110 | int ret = 0, err; |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 111 | |
| 112 | if (!nla) |
| 113 | return -EINVAL; |
| 114 | |
| 115 | err = nla_parse_nested(tb, TCA_VLAN_MAX, nla, vlan_policy); |
| 116 | if (err < 0) |
| 117 | return err; |
| 118 | |
| 119 | if (!tb[TCA_VLAN_PARMS]) |
| 120 | return -EINVAL; |
| 121 | parm = nla_data(tb[TCA_VLAN_PARMS]); |
Jamal Hadi Salim | 5026c9b | 2016-05-10 16:49:26 -0400 | [diff] [blame] | 122 | exists = tcf_hash_check(tn, parm->index, a, bind); |
| 123 | if (exists && bind) |
| 124 | return 0; |
| 125 | |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 126 | switch (parm->v_action) { |
| 127 | case TCA_VLAN_ACT_POP: |
| 128 | break; |
| 129 | case TCA_VLAN_ACT_PUSH: |
Shmulik Ladkani | 45a497f | 2016-09-19 19:11:10 +0300 | [diff] [blame] | 130 | case TCA_VLAN_ACT_MODIFY: |
Jamal Hadi Salim | 5026c9b | 2016-05-10 16:49:26 -0400 | [diff] [blame] | 131 | if (!tb[TCA_VLAN_PUSH_VLAN_ID]) { |
| 132 | if (exists) |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 133 | tcf_hash_release(*a, bind); |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 134 | return -EINVAL; |
Jamal Hadi Salim | 5026c9b | 2016-05-10 16:49:26 -0400 | [diff] [blame] | 135 | } |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 136 | push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]); |
Jamal Hadi Salim | 5026c9b | 2016-05-10 16:49:26 -0400 | [diff] [blame] | 137 | if (push_vid >= VLAN_VID_MASK) { |
| 138 | if (exists) |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 139 | tcf_hash_release(*a, bind); |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 140 | return -ERANGE; |
Jamal Hadi Salim | 5026c9b | 2016-05-10 16:49:26 -0400 | [diff] [blame] | 141 | } |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 142 | |
| 143 | if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) { |
| 144 | push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]); |
| 145 | switch (push_proto) { |
| 146 | case htons(ETH_P_8021Q): |
| 147 | case htons(ETH_P_8021AD): |
| 148 | break; |
| 149 | default: |
| 150 | return -EPROTONOSUPPORT; |
| 151 | } |
| 152 | } else { |
| 153 | push_proto = htons(ETH_P_8021Q); |
| 154 | } |
Hadar Hen Zion | 956af37 | 2016-08-17 13:36:14 +0300 | [diff] [blame] | 155 | |
| 156 | if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY]) |
| 157 | push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]); |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 158 | break; |
| 159 | default: |
Jamal Hadi Salim | 5026c9b | 2016-05-10 16:49:26 -0400 | [diff] [blame] | 160 | if (exists) |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 161 | tcf_hash_release(*a, bind); |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 162 | return -EINVAL; |
| 163 | } |
| 164 | action = parm->v_action; |
| 165 | |
Jamal Hadi Salim | 5026c9b | 2016-05-10 16:49:26 -0400 | [diff] [blame] | 166 | if (!exists) { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 167 | ret = tcf_hash_create(tn, parm->index, est, a, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 168 | &act_vlan_ops, bind, false); |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 169 | if (ret) |
| 170 | return ret; |
| 171 | |
| 172 | ret = ACT_P_CREATED; |
| 173 | } else { |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 174 | tcf_hash_release(*a, bind); |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 175 | if (!ovr) |
| 176 | return -EEXIST; |
| 177 | } |
| 178 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 179 | v = to_vlan(*a); |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 180 | |
| 181 | spin_lock_bh(&v->tcf_lock); |
| 182 | |
| 183 | v->tcfv_action = action; |
| 184 | v->tcfv_push_vid = push_vid; |
Hadar Hen Zion | 956af37 | 2016-08-17 13:36:14 +0300 | [diff] [blame] | 185 | v->tcfv_push_prio = push_prio; |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 186 | v->tcfv_push_proto = push_proto; |
| 187 | |
| 188 | v->tcf_action = parm->action; |
| 189 | |
| 190 | spin_unlock_bh(&v->tcf_lock); |
| 191 | |
| 192 | if (ret == ACT_P_CREATED) |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 193 | tcf_hash_insert(tn, *a); |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a, |
| 198 | int bind, int ref) |
| 199 | { |
| 200 | unsigned char *b = skb_tail_pointer(skb); |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 201 | struct tcf_vlan *v = to_vlan(a); |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 202 | struct tc_vlan opt = { |
| 203 | .index = v->tcf_index, |
| 204 | .refcnt = v->tcf_refcnt - ref, |
| 205 | .bindcnt = v->tcf_bindcnt - bind, |
| 206 | .action = v->tcf_action, |
| 207 | .v_action = v->tcfv_action, |
| 208 | }; |
| 209 | struct tcf_t t; |
| 210 | |
| 211 | if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt)) |
| 212 | goto nla_put_failure; |
| 213 | |
Shmulik Ladkani | 45a497f | 2016-09-19 19:11:10 +0300 | [diff] [blame] | 214 | if ((v->tcfv_action == TCA_VLAN_ACT_PUSH || |
| 215 | v->tcfv_action == TCA_VLAN_ACT_MODIFY) && |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 216 | (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, v->tcfv_push_vid) || |
Jamal Hadi Salim | 0b0f43f | 2016-06-05 10:41:32 -0400 | [diff] [blame] | 217 | nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL, |
Hadar Hen Zion | 956af37 | 2016-08-17 13:36:14 +0300 | [diff] [blame] | 218 | v->tcfv_push_proto) || |
| 219 | (nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY, |
| 220 | v->tcfv_push_prio)))) |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 221 | goto nla_put_failure; |
| 222 | |
Jamal Hadi Salim | 48d8ee1 | 2016-06-06 06:32:55 -0400 | [diff] [blame] | 223 | tcf_tm_dump(&t, &v->tcf_tm); |
Nicolas Dichtel | 9854518 | 2016-04-26 10:06:18 +0200 | [diff] [blame] | 224 | if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD)) |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 225 | goto nla_put_failure; |
| 226 | return skb->len; |
| 227 | |
| 228 | nla_put_failure: |
| 229 | nlmsg_trim(skb, b); |
| 230 | return -1; |
| 231 | } |
| 232 | |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 233 | static int tcf_vlan_walker(struct net *net, struct sk_buff *skb, |
| 234 | struct netlink_callback *cb, int type, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 235 | const struct tc_action_ops *ops) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 236 | { |
| 237 | struct tc_action_net *tn = net_generic(net, vlan_net_id); |
| 238 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 239 | return tcf_generic_walker(tn, skb, cb, type, ops); |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 240 | } |
| 241 | |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 242 | static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index) |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 243 | { |
| 244 | struct tc_action_net *tn = net_generic(net, vlan_net_id); |
| 245 | |
| 246 | return tcf_hash_search(tn, a, index); |
| 247 | } |
| 248 | |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 249 | static struct tc_action_ops act_vlan_ops = { |
| 250 | .kind = "vlan", |
| 251 | .type = TCA_ACT_VLAN, |
| 252 | .owner = THIS_MODULE, |
| 253 | .act = tcf_vlan, |
| 254 | .dump = tcf_vlan_dump, |
| 255 | .init = tcf_vlan_init, |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 256 | .walk = tcf_vlan_walker, |
| 257 | .lookup = tcf_vlan_search, |
WANG Cong | a85a970 | 2016-07-25 16:09:41 -0700 | [diff] [blame] | 258 | .size = sizeof(struct tcf_vlan), |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | static __net_init int vlan_init_net(struct net *net) |
| 262 | { |
| 263 | struct tc_action_net *tn = net_generic(net, vlan_net_id); |
| 264 | |
| 265 | return tc_action_net_init(tn, &act_vlan_ops, VLAN_TAB_MASK); |
| 266 | } |
| 267 | |
| 268 | static void __net_exit vlan_exit_net(struct net *net) |
| 269 | { |
| 270 | struct tc_action_net *tn = net_generic(net, vlan_net_id); |
| 271 | |
| 272 | tc_action_net_exit(tn); |
| 273 | } |
| 274 | |
| 275 | static struct pernet_operations vlan_net_ops = { |
| 276 | .init = vlan_init_net, |
| 277 | .exit = vlan_exit_net, |
| 278 | .id = &vlan_net_id, |
| 279 | .size = sizeof(struct tc_action_net), |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 280 | }; |
| 281 | |
| 282 | static int __init vlan_init_module(void) |
| 283 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 284 | return tcf_register_action(&act_vlan_ops, &vlan_net_ops); |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static void __exit vlan_cleanup_module(void) |
| 288 | { |
WANG Cong | ddf97cc | 2016-02-22 15:57:53 -0800 | [diff] [blame] | 289 | tcf_unregister_action(&act_vlan_ops, &vlan_net_ops); |
Jiri Pirko | c7e2b96 | 2014-11-19 14:05:03 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | module_init(vlan_init_module); |
| 293 | module_exit(vlan_cleanup_module); |
| 294 | |
| 295 | MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); |
| 296 | MODULE_DESCRIPTION("vlan manipulation actions"); |
| 297 | MODULE_LICENSE("GPL v2"); |