1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2019 Netronome Systems, Inc. */
3
4 #include <linux/if_arp.h>
5 #include <linux/init.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/mpls.h>
9 #include <linux/rtnetlink.h>
10 #include <linux/skbuff.h>
11 #include <linux/tc_act/tc_mpls.h>
12 #include <net/mpls.h>
13 #include <net/netlink.h>
14 #include <net/pkt_sched.h>
15 #include <net/pkt_cls.h>
16 #include <net/tc_act/tc_mpls.h>
17
18 static unsigned int mpls_net_id;
19 static struct tc_action_ops act_mpls_ops;
20
21 #define ACT_MPLS_TTL_DEFAULT 255
22
tcf_mpls_get_lse(struct mpls_shim_hdr * lse,struct tcf_mpls_params * p,bool set_bos)23 static __be32 tcf_mpls_get_lse(struct mpls_shim_hdr *lse,
24 struct tcf_mpls_params *p, bool set_bos)
25 {
26 u32 new_lse = 0;
27
28 if (lse)
29 new_lse = be32_to_cpu(lse->label_stack_entry);
30
31 if (p->tcfm_label != ACT_MPLS_LABEL_NOT_SET) {
32 new_lse &= ~MPLS_LS_LABEL_MASK;
33 new_lse |= p->tcfm_label << MPLS_LS_LABEL_SHIFT;
34 }
35 if (p->tcfm_ttl) {
36 new_lse &= ~MPLS_LS_TTL_MASK;
37 new_lse |= p->tcfm_ttl << MPLS_LS_TTL_SHIFT;
38 }
39 if (p->tcfm_tc != ACT_MPLS_TC_NOT_SET) {
40 new_lse &= ~MPLS_LS_TC_MASK;
41 new_lse |= p->tcfm_tc << MPLS_LS_TC_SHIFT;
42 }
43 if (p->tcfm_bos != ACT_MPLS_BOS_NOT_SET) {
44 new_lse &= ~MPLS_LS_S_MASK;
45 new_lse |= p->tcfm_bos << MPLS_LS_S_SHIFT;
46 } else if (set_bos) {
47 new_lse |= 1 << MPLS_LS_S_SHIFT;
48 }
49
50 return cpu_to_be32(new_lse);
51 }
52
tcf_mpls_act(struct sk_buff * skb,const struct tc_action * a,struct tcf_result * res)53 static int tcf_mpls_act(struct sk_buff *skb, const struct tc_action *a,
54 struct tcf_result *res)
55 {
56 struct tcf_mpls *m = to_mpls(a);
57 struct tcf_mpls_params *p;
58 __be32 new_lse;
59 int ret, mac_len;
60
61 tcf_lastuse_update(&m->tcf_tm);
62 bstats_cpu_update(this_cpu_ptr(m->common.cpu_bstats), skb);
63
64 /* Ensure 'data' points at mac_header prior calling mpls manipulating
65 * functions.
66 */
67 if (skb_at_tc_ingress(skb)) {
68 skb_push_rcsum(skb, skb->mac_len);
69 mac_len = skb->mac_len;
70 } else {
71 mac_len = skb_network_header(skb) - skb_mac_header(skb);
72 }
73
74 ret = READ_ONCE(m->tcf_action);
75
76 p = rcu_dereference_bh(m->mpls_p);
77
78 switch (p->tcfm_action) {
79 case TCA_MPLS_ACT_POP:
80 if (skb_mpls_pop(skb, p->tcfm_proto, mac_len,
81 skb->dev && skb->dev->type == ARPHRD_ETHER))
82 goto drop;
83 break;
84 case TCA_MPLS_ACT_PUSH:
85 new_lse = tcf_mpls_get_lse(NULL, p, !eth_p_mpls(skb_protocol(skb, true)));
86 if (skb_mpls_push(skb, new_lse, p->tcfm_proto, mac_len,
87 skb->dev && skb->dev->type == ARPHRD_ETHER))
88 goto drop;
89 break;
90 case TCA_MPLS_ACT_MAC_PUSH:
91 if (skb_vlan_tag_present(skb)) {
92 if (__vlan_insert_inner_tag(skb, skb->vlan_proto,
93 skb_vlan_tag_get(skb),
94 ETH_HLEN) < 0)
95 goto drop;
96
97 skb->protocol = skb->vlan_proto;
98 __vlan_hwaccel_clear_tag(skb);
99 }
100
101 new_lse = tcf_mpls_get_lse(NULL, p, mac_len ||
102 !eth_p_mpls(skb->protocol));
103
104 if (skb_mpls_push(skb, new_lse, p->tcfm_proto, 0, false))
105 goto drop;
106 break;
107 case TCA_MPLS_ACT_MODIFY:
108 if (!pskb_may_pull(skb,
109 skb_network_offset(skb) + MPLS_HLEN))
110 goto drop;
111 new_lse = tcf_mpls_get_lse(mpls_hdr(skb), p, false);
112 if (skb_mpls_update_lse(skb, new_lse))
113 goto drop;
114 break;
115 case TCA_MPLS_ACT_DEC_TTL:
116 if (skb_mpls_dec_ttl(skb))
117 goto drop;
118 break;
119 }
120
121 if (skb_at_tc_ingress(skb))
122 skb_pull_rcsum(skb, skb->mac_len);
123
124 return ret;
125
126 drop:
127 qstats_drop_inc(this_cpu_ptr(m->common.cpu_qstats));
128 return TC_ACT_SHOT;
129 }
130
valid_label(const struct nlattr * attr,struct netlink_ext_ack * extack)131 static int valid_label(const struct nlattr *attr,
132 struct netlink_ext_ack *extack)
133 {
134 const u32 *label = nla_data(attr);
135
136 if (nla_len(attr) != sizeof(*label)) {
137 NL_SET_ERR_MSG_MOD(extack, "Invalid MPLS label length");
138 return -EINVAL;
139 }
140
141 if (*label & ~MPLS_LABEL_MASK || *label == MPLS_LABEL_IMPLNULL) {
142 NL_SET_ERR_MSG_MOD(extack, "MPLS label out of range");
143 return -EINVAL;
144 }
145
146 return 0;
147 }
148
149 static const struct nla_policy mpls_policy[TCA_MPLS_MAX + 1] = {
150 [TCA_MPLS_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_mpls)),
151 [TCA_MPLS_PROTO] = { .type = NLA_U16 },
152 [TCA_MPLS_LABEL] = NLA_POLICY_VALIDATE_FN(NLA_BINARY,
153 valid_label),
154 [TCA_MPLS_TC] = NLA_POLICY_RANGE(NLA_U8, 0, 7),
155 [TCA_MPLS_TTL] = NLA_POLICY_MIN(NLA_U8, 1),
156 [TCA_MPLS_BOS] = NLA_POLICY_RANGE(NLA_U8, 0, 1),
157 };
158
tcf_mpls_init(struct net * net,struct nlattr * nla,struct nlattr * est,struct tc_action ** a,struct tcf_proto * tp,u32 flags,struct netlink_ext_ack * extack)159 static int tcf_mpls_init(struct net *net, struct nlattr *nla,
160 struct nlattr *est, struct tc_action **a,
161 struct tcf_proto *tp, u32 flags,
162 struct netlink_ext_ack *extack)
163 {
164 struct tc_action_net *tn = net_generic(net, mpls_net_id);
165 bool bind = flags & TCA_ACT_FLAGS_BIND;
166 struct nlattr *tb[TCA_MPLS_MAX + 1];
167 struct tcf_chain *goto_ch = NULL;
168 struct tcf_mpls_params *p;
169 struct tc_mpls *parm;
170 bool exists = false;
171 struct tcf_mpls *m;
172 int ret = 0, err;
173 u8 mpls_ttl = 0;
174 u32 index;
175
176 if (!nla) {
177 NL_SET_ERR_MSG_MOD(extack, "Missing netlink attributes");
178 return -EINVAL;
179 }
180
181 err = nla_parse_nested(tb, TCA_MPLS_MAX, nla, mpls_policy, extack);
182 if (err < 0)
183 return err;
184
185 if (!tb[TCA_MPLS_PARMS]) {
186 NL_SET_ERR_MSG_MOD(extack, "No MPLS params");
187 return -EINVAL;
188 }
189 parm = nla_data(tb[TCA_MPLS_PARMS]);
190 index = parm->index;
191
192 err = tcf_idr_check_alloc(tn, &index, a, bind);
193 if (err < 0)
194 return err;
195 exists = err;
196 if (exists && bind)
197 return 0;
198
199 if (!exists) {
200 ret = tcf_idr_create(tn, index, est, a, &act_mpls_ops, bind,
201 true, flags);
202 if (ret) {
203 tcf_idr_cleanup(tn, index);
204 return ret;
205 }
206
207 ret = ACT_P_CREATED;
208 } else if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
209 tcf_idr_release(*a, bind);
210 return -EEXIST;
211 }
212
213 /* Verify parameters against action type. */
214 switch (parm->m_action) {
215 case TCA_MPLS_ACT_POP:
216 if (!tb[TCA_MPLS_PROTO]) {
217 NL_SET_ERR_MSG_MOD(extack, "Protocol must be set for MPLS pop");
218 err = -EINVAL;
219 goto release_idr;
220 }
221 if (!eth_proto_is_802_3(nla_get_be16(tb[TCA_MPLS_PROTO]))) {
222 NL_SET_ERR_MSG_MOD(extack, "Invalid protocol type for MPLS pop");
223 err = -EINVAL;
224 goto release_idr;
225 }
226 if (tb[TCA_MPLS_LABEL] || tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] ||
227 tb[TCA_MPLS_BOS]) {
228 NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC or BOS cannot be used with MPLS pop");
229 err = -EINVAL;
230 goto release_idr;
231 }
232 break;
233 case TCA_MPLS_ACT_DEC_TTL:
234 if (tb[TCA_MPLS_PROTO] || tb[TCA_MPLS_LABEL] ||
235 tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] || tb[TCA_MPLS_BOS]) {
236 NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC, BOS or protocol cannot be used with MPLS dec_ttl");
237 err = -EINVAL;
238 goto release_idr;
239 }
240 break;
241 case TCA_MPLS_ACT_PUSH:
242 case TCA_MPLS_ACT_MAC_PUSH:
243 if (!tb[TCA_MPLS_LABEL]) {
244 NL_SET_ERR_MSG_MOD(extack, "Label is required for MPLS push");
245 err = -EINVAL;
246 goto release_idr;
247 }
248 if (tb[TCA_MPLS_PROTO] &&
249 !eth_p_mpls(nla_get_be16(tb[TCA_MPLS_PROTO]))) {
250 NL_SET_ERR_MSG_MOD(extack, "Protocol must be an MPLS type for MPLS push");
251 err = -EPROTONOSUPPORT;
252 goto release_idr;
253 }
254 /* Push needs a TTL - if not specified, set a default value. */
255 if (!tb[TCA_MPLS_TTL]) {
256 #if IS_ENABLED(CONFIG_MPLS)
257 mpls_ttl = net->mpls.default_ttl ?
258 net->mpls.default_ttl : ACT_MPLS_TTL_DEFAULT;
259 #else
260 mpls_ttl = ACT_MPLS_TTL_DEFAULT;
261 #endif
262 }
263 break;
264 case TCA_MPLS_ACT_MODIFY:
265 if (tb[TCA_MPLS_PROTO]) {
266 NL_SET_ERR_MSG_MOD(extack, "Protocol cannot be used with MPLS modify");
267 err = -EINVAL;
268 goto release_idr;
269 }
270 break;
271 default:
272 NL_SET_ERR_MSG_MOD(extack, "Unknown MPLS action");
273 err = -EINVAL;
274 goto release_idr;
275 }
276
277 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
278 if (err < 0)
279 goto release_idr;
280
281 m = to_mpls(*a);
282
283 p = kzalloc(sizeof(*p), GFP_KERNEL);
284 if (!p) {
285 err = -ENOMEM;
286 goto put_chain;
287 }
288
289 p->tcfm_action = parm->m_action;
290 p->tcfm_label = tb[TCA_MPLS_LABEL] ? nla_get_u32(tb[TCA_MPLS_LABEL]) :
291 ACT_MPLS_LABEL_NOT_SET;
292 p->tcfm_tc = tb[TCA_MPLS_TC] ? nla_get_u8(tb[TCA_MPLS_TC]) :
293 ACT_MPLS_TC_NOT_SET;
294 p->tcfm_ttl = tb[TCA_MPLS_TTL] ? nla_get_u8(tb[TCA_MPLS_TTL]) :
295 mpls_ttl;
296 p->tcfm_bos = tb[TCA_MPLS_BOS] ? nla_get_u8(tb[TCA_MPLS_BOS]) :
297 ACT_MPLS_BOS_NOT_SET;
298 p->tcfm_proto = tb[TCA_MPLS_PROTO] ? nla_get_be16(tb[TCA_MPLS_PROTO]) :
299 htons(ETH_P_MPLS_UC);
300
301 spin_lock_bh(&m->tcf_lock);
302 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
303 p = rcu_replace_pointer(m->mpls_p, p, lockdep_is_held(&m->tcf_lock));
304 spin_unlock_bh(&m->tcf_lock);
305
306 if (goto_ch)
307 tcf_chain_put_by_act(goto_ch);
308 if (p)
309 kfree_rcu(p, rcu);
310
311 return ret;
312 put_chain:
313 if (goto_ch)
314 tcf_chain_put_by_act(goto_ch);
315 release_idr:
316 tcf_idr_release(*a, bind);
317 return err;
318 }
319
tcf_mpls_cleanup(struct tc_action * a)320 static void tcf_mpls_cleanup(struct tc_action *a)
321 {
322 struct tcf_mpls *m = to_mpls(a);
323 struct tcf_mpls_params *p;
324
325 p = rcu_dereference_protected(m->mpls_p, 1);
326 if (p)
327 kfree_rcu(p, rcu);
328 }
329
tcf_mpls_dump(struct sk_buff * skb,struct tc_action * a,int bind,int ref)330 static int tcf_mpls_dump(struct sk_buff *skb, struct tc_action *a,
331 int bind, int ref)
332 {
333 unsigned char *b = skb_tail_pointer(skb);
334 struct tcf_mpls *m = to_mpls(a);
335 struct tcf_mpls_params *p;
336 struct tc_mpls opt = {
337 .index = m->tcf_index,
338 .refcnt = refcount_read(&m->tcf_refcnt) - ref,
339 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind,
340 };
341 struct tcf_t t;
342
343 spin_lock_bh(&m->tcf_lock);
344 opt.action = m->tcf_action;
345 p = rcu_dereference_protected(m->mpls_p, lockdep_is_held(&m->tcf_lock));
346 opt.m_action = p->tcfm_action;
347
348 if (nla_put(skb, TCA_MPLS_PARMS, sizeof(opt), &opt))
349 goto nla_put_failure;
350
351 if (p->tcfm_label != ACT_MPLS_LABEL_NOT_SET &&
352 nla_put_u32(skb, TCA_MPLS_LABEL, p->tcfm_label))
353 goto nla_put_failure;
354
355 if (p->tcfm_tc != ACT_MPLS_TC_NOT_SET &&
356 nla_put_u8(skb, TCA_MPLS_TC, p->tcfm_tc))
357 goto nla_put_failure;
358
359 if (p->tcfm_ttl && nla_put_u8(skb, TCA_MPLS_TTL, p->tcfm_ttl))
360 goto nla_put_failure;
361
362 if (p->tcfm_bos != ACT_MPLS_BOS_NOT_SET &&
363 nla_put_u8(skb, TCA_MPLS_BOS, p->tcfm_bos))
364 goto nla_put_failure;
365
366 if (nla_put_be16(skb, TCA_MPLS_PROTO, p->tcfm_proto))
367 goto nla_put_failure;
368
369 tcf_tm_dump(&t, &m->tcf_tm);
370
371 if (nla_put_64bit(skb, TCA_MPLS_TM, sizeof(t), &t, TCA_MPLS_PAD))
372 goto nla_put_failure;
373
374 spin_unlock_bh(&m->tcf_lock);
375
376 return skb->len;
377
378 nla_put_failure:
379 spin_unlock_bh(&m->tcf_lock);
380 nlmsg_trim(skb, b);
381 return -EMSGSIZE;
382 }
383
tcf_mpls_walker(struct net * net,struct sk_buff * skb,struct netlink_callback * cb,int type,const struct tc_action_ops * ops,struct netlink_ext_ack * extack)384 static int tcf_mpls_walker(struct net *net, struct sk_buff *skb,
385 struct netlink_callback *cb, int type,
386 const struct tc_action_ops *ops,
387 struct netlink_ext_ack *extack)
388 {
389 struct tc_action_net *tn = net_generic(net, mpls_net_id);
390
391 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
392 }
393
tcf_mpls_search(struct net * net,struct tc_action ** a,u32 index)394 static int tcf_mpls_search(struct net *net, struct tc_action **a, u32 index)
395 {
396 struct tc_action_net *tn = net_generic(net, mpls_net_id);
397
398 return tcf_idr_search(tn, a, index);
399 }
400
401 static struct tc_action_ops act_mpls_ops = {
402 .kind = "mpls",
403 .id = TCA_ID_MPLS,
404 .owner = THIS_MODULE,
405 .act = tcf_mpls_act,
406 .dump = tcf_mpls_dump,
407 .init = tcf_mpls_init,
408 .cleanup = tcf_mpls_cleanup,
409 .walk = tcf_mpls_walker,
410 .lookup = tcf_mpls_search,
411 .size = sizeof(struct tcf_mpls),
412 };
413
mpls_init_net(struct net * net)414 static __net_init int mpls_init_net(struct net *net)
415 {
416 struct tc_action_net *tn = net_generic(net, mpls_net_id);
417
418 return tc_action_net_init(net, tn, &act_mpls_ops);
419 }
420
mpls_exit_net(struct list_head * net_list)421 static void __net_exit mpls_exit_net(struct list_head *net_list)
422 {
423 tc_action_net_exit(net_list, mpls_net_id);
424 }
425
426 static struct pernet_operations mpls_net_ops = {
427 .init = mpls_init_net,
428 .exit_batch = mpls_exit_net,
429 .id = &mpls_net_id,
430 .size = sizeof(struct tc_action_net),
431 };
432
mpls_init_module(void)433 static int __init mpls_init_module(void)
434 {
435 return tcf_register_action(&act_mpls_ops, &mpls_net_ops);
436 }
437
mpls_cleanup_module(void)438 static void __exit mpls_cleanup_module(void)
439 {
440 tcf_unregister_action(&act_mpls_ops, &mpls_net_ops);
441 }
442
443 module_init(mpls_init_module);
444 module_exit(mpls_cleanup_module);
445
446 MODULE_SOFTDEP("post: mpls_gso");
447 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>");
448 MODULE_LICENSE("GPL");
449 MODULE_DESCRIPTION("MPLS manipulation actions");
450