Commit | Line | Data |
---|---|---|
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 | ||
24 | static int tcf_vlan(struct sk_buff *skb, const struct tc_action *a, | |
25 | struct tcf_result *res) | |
26 | { | |
27 | struct tcf_vlan *v = a->priv; | |
28 | int action; | |
29 | int err; | |
30 | ||
31 | spin_lock(&v->tcf_lock); | |
32 | v->tcf_tm.lastuse = jiffies; | |
33 | bstats_update(&v->tcf_bstats, skb); | |
34 | action = v->tcf_action; | |
35 | ||
9edbf4a0 SL |
36 | /* Ensure 'data' points at mac_header prior calling vlan manipulating |
37 | * functions. | |
38 | */ | |
39 | if (skb_at_tc_ingress(skb)) | |
40 | skb_push_rcsum(skb, skb->mac_len); | |
41 | ||
c7e2b968 JP |
42 | switch (v->tcfv_action) { |
43 | case TCA_VLAN_ACT_POP: | |
44 | err = skb_vlan_pop(skb); | |
45 | if (err) | |
46 | goto drop; | |
47 | break; | |
48 | case TCA_VLAN_ACT_PUSH: | |
49 | err = skb_vlan_push(skb, v->tcfv_push_proto, v->tcfv_push_vid); | |
50 | if (err) | |
51 | goto drop; | |
52 | break; | |
53 | default: | |
54 | BUG(); | |
55 | } | |
56 | ||
57 | goto unlock; | |
58 | ||
59 | drop: | |
60 | action = TC_ACT_SHOT; | |
61 | v->tcf_qstats.drops++; | |
62 | unlock: | |
9edbf4a0 SL |
63 | if (skb_at_tc_ingress(skb)) |
64 | skb_pull_rcsum(skb, skb->mac_len); | |
65 | ||
c7e2b968 JP |
66 | spin_unlock(&v->tcf_lock); |
67 | return action; | |
68 | } | |
69 | ||
70 | static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = { | |
71 | [TCA_VLAN_PARMS] = { .len = sizeof(struct tc_vlan) }, | |
72 | [TCA_VLAN_PUSH_VLAN_ID] = { .type = NLA_U16 }, | |
73 | [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NLA_U16 }, | |
74 | }; | |
75 | ||
76 | static int tcf_vlan_init(struct net *net, struct nlattr *nla, | |
77 | struct nlattr *est, struct tc_action *a, | |
78 | int ovr, int bind) | |
79 | { | |
80 | struct nlattr *tb[TCA_VLAN_MAX + 1]; | |
81 | struct tc_vlan *parm; | |
82 | struct tcf_vlan *v; | |
83 | int action; | |
84 | __be16 push_vid = 0; | |
85 | __be16 push_proto = 0; | |
86 | int ret = 0; | |
87 | int err; | |
88 | ||
89 | if (!nla) | |
90 | return -EINVAL; | |
91 | ||
92 | err = nla_parse_nested(tb, TCA_VLAN_MAX, nla, vlan_policy); | |
93 | if (err < 0) | |
94 | return err; | |
95 | ||
96 | if (!tb[TCA_VLAN_PARMS]) | |
97 | return -EINVAL; | |
98 | parm = nla_data(tb[TCA_VLAN_PARMS]); | |
99 | switch (parm->v_action) { | |
100 | case TCA_VLAN_ACT_POP: | |
101 | break; | |
102 | case TCA_VLAN_ACT_PUSH: | |
103 | if (!tb[TCA_VLAN_PUSH_VLAN_ID]) | |
104 | return -EINVAL; | |
105 | push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]); | |
106 | if (push_vid >= VLAN_VID_MASK) | |
107 | return -ERANGE; | |
108 | ||
109 | if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) { | |
110 | push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]); | |
111 | switch (push_proto) { | |
112 | case htons(ETH_P_8021Q): | |
113 | case htons(ETH_P_8021AD): | |
114 | break; | |
115 | default: | |
116 | return -EPROTONOSUPPORT; | |
117 | } | |
118 | } else { | |
119 | push_proto = htons(ETH_P_8021Q); | |
120 | } | |
121 | break; | |
122 | default: | |
123 | return -EINVAL; | |
124 | } | |
125 | action = parm->v_action; | |
126 | ||
127 | if (!tcf_hash_check(parm->index, a, bind)) { | |
519c818e ED |
128 | ret = tcf_hash_create(parm->index, est, a, sizeof(*v), |
129 | bind, false); | |
c7e2b968 JP |
130 | if (ret) |
131 | return ret; | |
132 | ||
133 | ret = ACT_P_CREATED; | |
134 | } else { | |
135 | if (bind) | |
136 | return 0; | |
137 | tcf_hash_release(a, bind); | |
138 | if (!ovr) | |
139 | return -EEXIST; | |
140 | } | |
141 | ||
142 | v = to_vlan(a); | |
143 | ||
144 | spin_lock_bh(&v->tcf_lock); | |
145 | ||
146 | v->tcfv_action = action; | |
147 | v->tcfv_push_vid = push_vid; | |
148 | v->tcfv_push_proto = push_proto; | |
149 | ||
150 | v->tcf_action = parm->action; | |
151 | ||
152 | spin_unlock_bh(&v->tcf_lock); | |
153 | ||
154 | if (ret == ACT_P_CREATED) | |
155 | tcf_hash_insert(a); | |
156 | return ret; | |
157 | } | |
158 | ||
159 | static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a, | |
160 | int bind, int ref) | |
161 | { | |
162 | unsigned char *b = skb_tail_pointer(skb); | |
163 | struct tcf_vlan *v = a->priv; | |
164 | struct tc_vlan opt = { | |
165 | .index = v->tcf_index, | |
166 | .refcnt = v->tcf_refcnt - ref, | |
167 | .bindcnt = v->tcf_bindcnt - bind, | |
168 | .action = v->tcf_action, | |
169 | .v_action = v->tcfv_action, | |
170 | }; | |
171 | struct tcf_t t; | |
172 | ||
173 | if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt)) | |
174 | goto nla_put_failure; | |
175 | ||
176 | if (v->tcfv_action == TCA_VLAN_ACT_PUSH && | |
177 | (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, v->tcfv_push_vid) || | |
178 | nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL, v->tcfv_push_proto))) | |
179 | goto nla_put_failure; | |
180 | ||
181 | t.install = jiffies_to_clock_t(jiffies - v->tcf_tm.install); | |
182 | t.lastuse = jiffies_to_clock_t(jiffies - v->tcf_tm.lastuse); | |
183 | t.expires = jiffies_to_clock_t(v->tcf_tm.expires); | |
184 | if (nla_put(skb, TCA_VLAN_TM, sizeof(t), &t)) | |
185 | goto nla_put_failure; | |
186 | return skb->len; | |
187 | ||
188 | nla_put_failure: | |
189 | nlmsg_trim(skb, b); | |
190 | return -1; | |
191 | } | |
192 | ||
193 | static struct tc_action_ops act_vlan_ops = { | |
194 | .kind = "vlan", | |
195 | .type = TCA_ACT_VLAN, | |
196 | .owner = THIS_MODULE, | |
197 | .act = tcf_vlan, | |
198 | .dump = tcf_vlan_dump, | |
199 | .init = tcf_vlan_init, | |
200 | }; | |
201 | ||
202 | static int __init vlan_init_module(void) | |
203 | { | |
204 | return tcf_register_action(&act_vlan_ops, VLAN_TAB_MASK); | |
205 | } | |
206 | ||
207 | static void __exit vlan_cleanup_module(void) | |
208 | { | |
209 | tcf_unregister_action(&act_vlan_ops); | |
210 | } | |
211 | ||
212 | module_init(vlan_init_module); | |
213 | module_exit(vlan_cleanup_module); | |
214 | ||
215 | MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); | |
216 | MODULE_DESCRIPTION("vlan manipulation actions"); | |
217 | MODULE_LICENSE("GPL v2"); |