Merge remote-tracking branch 'mfd/for-mfd-next'
[deliverable/linux.git] / net / sched / act_vlan.c
CommitLineData
c7e2b968
JP
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
ddf97ccd 24static int vlan_net_id;
a85a970a 25static struct tc_action_ops act_vlan_ops;
ddf97ccd 26
c7e2b968
JP
27static int tcf_vlan(struct sk_buff *skb, const struct tc_action *a,
28 struct tcf_result *res)
29{
a85a970a 30 struct tcf_vlan *v = to_vlan(a);
c7e2b968
JP
31 int action;
32 int err;
33
34 spin_lock(&v->tcf_lock);
9c4a4e48 35 tcf_lastuse_update(&v->tcf_tm);
c7e2b968
JP
36 bstats_update(&v->tcf_bstats, skb);
37 action = v->tcf_action;
38
39 switch (v->tcfv_action) {
40 case TCA_VLAN_ACT_POP:
41 err = skb_vlan_pop(skb);
42 if (err)
43 goto drop;
44 break;
45 case TCA_VLAN_ACT_PUSH:
956af371
HHZ
46 err = skb_vlan_push(skb, v->tcfv_push_proto, v->tcfv_push_vid |
47 (v->tcfv_push_prio << VLAN_PRIO_SHIFT));
c7e2b968
JP
48 if (err)
49 goto drop;
50 break;
51 default:
52 BUG();
53 }
54
55 goto unlock;
56
57drop:
58 action = TC_ACT_SHOT;
59 v->tcf_qstats.drops++;
60unlock:
61 spin_unlock(&v->tcf_lock);
62 return action;
63}
64
65static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = {
66 [TCA_VLAN_PARMS] = { .len = sizeof(struct tc_vlan) },
67 [TCA_VLAN_PUSH_VLAN_ID] = { .type = NLA_U16 },
68 [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NLA_U16 },
956af371 69 [TCA_VLAN_PUSH_VLAN_PRIORITY] = { .type = NLA_U8 },
c7e2b968
JP
70};
71
72static int tcf_vlan_init(struct net *net, struct nlattr *nla,
a85a970a 73 struct nlattr *est, struct tc_action **a,
c7e2b968
JP
74 int ovr, int bind)
75{
ddf97ccd 76 struct tc_action_net *tn = net_generic(net, vlan_net_id);
c7e2b968
JP
77 struct nlattr *tb[TCA_VLAN_MAX + 1];
78 struct tc_vlan *parm;
79 struct tcf_vlan *v;
80 int action;
81 __be16 push_vid = 0;
82 __be16 push_proto = 0;
956af371 83 u8 push_prio = 0;
b2313077
WC
84 bool exists = false;
85 int ret = 0, err;
c7e2b968
JP
86
87 if (!nla)
88 return -EINVAL;
89
90 err = nla_parse_nested(tb, TCA_VLAN_MAX, nla, vlan_policy);
91 if (err < 0)
92 return err;
93
94 if (!tb[TCA_VLAN_PARMS])
95 return -EINVAL;
96 parm = nla_data(tb[TCA_VLAN_PARMS]);
5026c9b1
JHS
97 exists = tcf_hash_check(tn, parm->index, a, bind);
98 if (exists && bind)
99 return 0;
100
c7e2b968
JP
101 switch (parm->v_action) {
102 case TCA_VLAN_ACT_POP:
103 break;
104 case TCA_VLAN_ACT_PUSH:
5026c9b1
JHS
105 if (!tb[TCA_VLAN_PUSH_VLAN_ID]) {
106 if (exists)
a85a970a 107 tcf_hash_release(*a, bind);
c7e2b968 108 return -EINVAL;
5026c9b1 109 }
c7e2b968 110 push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
5026c9b1
JHS
111 if (push_vid >= VLAN_VID_MASK) {
112 if (exists)
a85a970a 113 tcf_hash_release(*a, bind);
c7e2b968 114 return -ERANGE;
5026c9b1 115 }
c7e2b968
JP
116
117 if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
118 push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
119 switch (push_proto) {
120 case htons(ETH_P_8021Q):
121 case htons(ETH_P_8021AD):
122 break;
123 default:
124 return -EPROTONOSUPPORT;
125 }
126 } else {
127 push_proto = htons(ETH_P_8021Q);
128 }
956af371
HHZ
129
130 if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY])
131 push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]);
c7e2b968
JP
132 break;
133 default:
5026c9b1 134 if (exists)
a85a970a 135 tcf_hash_release(*a, bind);
c7e2b968
JP
136 return -EINVAL;
137 }
138 action = parm->v_action;
139
5026c9b1 140 if (!exists) {
ddf97ccd 141 ret = tcf_hash_create(tn, parm->index, est, a,
a85a970a 142 &act_vlan_ops, bind, false);
c7e2b968
JP
143 if (ret)
144 return ret;
145
146 ret = ACT_P_CREATED;
147 } else {
a85a970a 148 tcf_hash_release(*a, bind);
c7e2b968
JP
149 if (!ovr)
150 return -EEXIST;
151 }
152
a85a970a 153 v = to_vlan(*a);
c7e2b968
JP
154
155 spin_lock_bh(&v->tcf_lock);
156
157 v->tcfv_action = action;
158 v->tcfv_push_vid = push_vid;
956af371 159 v->tcfv_push_prio = push_prio;
c7e2b968
JP
160 v->tcfv_push_proto = push_proto;
161
162 v->tcf_action = parm->action;
163
164 spin_unlock_bh(&v->tcf_lock);
165
166 if (ret == ACT_P_CREATED)
a85a970a 167 tcf_hash_insert(tn, *a);
c7e2b968
JP
168 return ret;
169}
170
171static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a,
172 int bind, int ref)
173{
174 unsigned char *b = skb_tail_pointer(skb);
a85a970a 175 struct tcf_vlan *v = to_vlan(a);
c7e2b968
JP
176 struct tc_vlan opt = {
177 .index = v->tcf_index,
178 .refcnt = v->tcf_refcnt - ref,
179 .bindcnt = v->tcf_bindcnt - bind,
180 .action = v->tcf_action,
181 .v_action = v->tcfv_action,
182 };
183 struct tcf_t t;
184
185 if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt))
186 goto nla_put_failure;
187
188 if (v->tcfv_action == TCA_VLAN_ACT_PUSH &&
189 (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, v->tcfv_push_vid) ||
0b0f43fe 190 nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL,
956af371
HHZ
191 v->tcfv_push_proto) ||
192 (nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY,
193 v->tcfv_push_prio))))
c7e2b968
JP
194 goto nla_put_failure;
195
48d8ee16 196 tcf_tm_dump(&t, &v->tcf_tm);
9854518e 197 if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD))
c7e2b968
JP
198 goto nla_put_failure;
199 return skb->len;
200
201nla_put_failure:
202 nlmsg_trim(skb, b);
203 return -1;
204}
205
ddf97ccd
WC
206static int tcf_vlan_walker(struct net *net, struct sk_buff *skb,
207 struct netlink_callback *cb, int type,
a85a970a 208 const struct tc_action_ops *ops)
ddf97ccd
WC
209{
210 struct tc_action_net *tn = net_generic(net, vlan_net_id);
211
a85a970a 212 return tcf_generic_walker(tn, skb, cb, type, ops);
ddf97ccd
WC
213}
214
a85a970a 215static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index)
ddf97ccd
WC
216{
217 struct tc_action_net *tn = net_generic(net, vlan_net_id);
218
219 return tcf_hash_search(tn, a, index);
220}
221
c7e2b968
JP
222static struct tc_action_ops act_vlan_ops = {
223 .kind = "vlan",
224 .type = TCA_ACT_VLAN,
225 .owner = THIS_MODULE,
226 .act = tcf_vlan,
227 .dump = tcf_vlan_dump,
228 .init = tcf_vlan_init,
ddf97ccd
WC
229 .walk = tcf_vlan_walker,
230 .lookup = tcf_vlan_search,
a85a970a 231 .size = sizeof(struct tcf_vlan),
ddf97ccd
WC
232};
233
234static __net_init int vlan_init_net(struct net *net)
235{
236 struct tc_action_net *tn = net_generic(net, vlan_net_id);
237
238 return tc_action_net_init(tn, &act_vlan_ops, VLAN_TAB_MASK);
239}
240
241static void __net_exit vlan_exit_net(struct net *net)
242{
243 struct tc_action_net *tn = net_generic(net, vlan_net_id);
244
245 tc_action_net_exit(tn);
246}
247
248static struct pernet_operations vlan_net_ops = {
249 .init = vlan_init_net,
250 .exit = vlan_exit_net,
251 .id = &vlan_net_id,
252 .size = sizeof(struct tc_action_net),
c7e2b968
JP
253};
254
255static int __init vlan_init_module(void)
256{
ddf97ccd 257 return tcf_register_action(&act_vlan_ops, &vlan_net_ops);
c7e2b968
JP
258}
259
260static void __exit vlan_cleanup_module(void)
261{
ddf97ccd 262 tcf_unregister_action(&act_vlan_ops, &vlan_net_ops);
c7e2b968
JP
263}
264
265module_init(vlan_init_module);
266module_exit(vlan_cleanup_module);
267
268MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
269MODULE_DESCRIPTION("vlan manipulation actions");
270MODULE_LICENSE("GPL v2");
This page took 0.167768 seconds and 5 git commands to generate.