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