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 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 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 new_lse = tcf_mpls_get_lse(mpls_hdr(skb), p, false); 109 if (skb_mpls_update_lse(skb, new_lse)) 110 goto drop; 111 break; 112 case TCA_MPLS_ACT_DEC_TTL: 113 if (skb_mpls_dec_ttl(skb)) 114 goto drop; 115 break; 116 } 117 118 if (skb_at_tc_ingress(skb)) 119 skb_pull_rcsum(skb, skb->mac_len); 120 121 return ret; 122 123 drop: 124 qstats_drop_inc(this_cpu_ptr(m->common.cpu_qstats)); 125 return TC_ACT_SHOT; 126 } 127 128 static int valid_label(const struct nlattr *attr, 129 struct netlink_ext_ack *extack) 130 { 131 const u32 *label = nla_data(attr); 132 133 if (*label & ~MPLS_LABEL_MASK || *label == MPLS_LABEL_IMPLNULL) { 134 NL_SET_ERR_MSG_MOD(extack, "MPLS label out of range"); 135 return -EINVAL; 136 } 137 138 return 0; 139 } 140 141 static const struct nla_policy mpls_policy[TCA_MPLS_MAX + 1] = { 142 [TCA_MPLS_PARMS] = NLA_POLICY_EXACT_LEN(sizeof(struct tc_mpls)), 143 [TCA_MPLS_PROTO] = { .type = NLA_U16 }, 144 [TCA_MPLS_LABEL] = NLA_POLICY_VALIDATE_FN(NLA_U32, valid_label), 145 [TCA_MPLS_TC] = NLA_POLICY_RANGE(NLA_U8, 0, 7), 146 [TCA_MPLS_TTL] = NLA_POLICY_MIN(NLA_U8, 1), 147 [TCA_MPLS_BOS] = NLA_POLICY_RANGE(NLA_U8, 0, 1), 148 }; 149 150 static int tcf_mpls_init(struct net *net, struct nlattr *nla, 151 struct nlattr *est, struct tc_action **a, 152 int ovr, int bind, bool rtnl_held, 153 struct tcf_proto *tp, u32 flags, 154 struct netlink_ext_ack *extack) 155 { 156 struct tc_action_net *tn = net_generic(net, mpls_net_id); 157 struct nlattr *tb[TCA_MPLS_MAX + 1]; 158 struct tcf_chain *goto_ch = NULL; 159 struct tcf_mpls_params *p; 160 struct tc_mpls *parm; 161 bool exists = false; 162 struct tcf_mpls *m; 163 int ret = 0, err; 164 u8 mpls_ttl = 0; 165 u32 index; 166 167 if (!nla) { 168 NL_SET_ERR_MSG_MOD(extack, "Missing netlink attributes"); 169 return -EINVAL; 170 } 171 172 err = nla_parse_nested(tb, TCA_MPLS_MAX, nla, mpls_policy, extack); 173 if (err < 0) 174 return err; 175 176 if (!tb[TCA_MPLS_PARMS]) { 177 NL_SET_ERR_MSG_MOD(extack, "No MPLS params"); 178 return -EINVAL; 179 } 180 parm = nla_data(tb[TCA_MPLS_PARMS]); 181 index = parm->index; 182 183 /* Verify parameters against action type. */ 184 switch (parm->m_action) { 185 case TCA_MPLS_ACT_POP: 186 if (!tb[TCA_MPLS_PROTO]) { 187 NL_SET_ERR_MSG_MOD(extack, "Protocol must be set for MPLS pop"); 188 return -EINVAL; 189 } 190 if (!eth_proto_is_802_3(nla_get_be16(tb[TCA_MPLS_PROTO]))) { 191 NL_SET_ERR_MSG_MOD(extack, "Invalid protocol type for MPLS pop"); 192 return -EINVAL; 193 } 194 if (tb[TCA_MPLS_LABEL] || tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] || 195 tb[TCA_MPLS_BOS]) { 196 NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC or BOS cannot be used with MPLS pop"); 197 return -EINVAL; 198 } 199 break; 200 case TCA_MPLS_ACT_DEC_TTL: 201 if (tb[TCA_MPLS_PROTO] || tb[TCA_MPLS_LABEL] || 202 tb[TCA_MPLS_TTL] || tb[TCA_MPLS_TC] || tb[TCA_MPLS_BOS]) { 203 NL_SET_ERR_MSG_MOD(extack, "Label, TTL, TC, BOS or protocol cannot be used with MPLS dec_ttl"); 204 return -EINVAL; 205 } 206 break; 207 case TCA_MPLS_ACT_PUSH: 208 case TCA_MPLS_ACT_MAC_PUSH: 209 if (!tb[TCA_MPLS_LABEL]) { 210 NL_SET_ERR_MSG_MOD(extack, "Label is required for MPLS push"); 211 return -EINVAL; 212 } 213 if (tb[TCA_MPLS_PROTO] && 214 !eth_p_mpls(nla_get_be16(tb[TCA_MPLS_PROTO]))) { 215 NL_SET_ERR_MSG_MOD(extack, "Protocol must be an MPLS type for MPLS push"); 216 return -EPROTONOSUPPORT; 217 } 218 /* Push needs a TTL - if not specified, set a default value. */ 219 if (!tb[TCA_MPLS_TTL]) { 220 #if IS_ENABLED(CONFIG_MPLS) 221 mpls_ttl = net->mpls.default_ttl ? 222 net->mpls.default_ttl : ACT_MPLS_TTL_DEFAULT; 223 #else 224 mpls_ttl = ACT_MPLS_TTL_DEFAULT; 225 #endif 226 } 227 break; 228 case TCA_MPLS_ACT_MODIFY: 229 if (tb[TCA_MPLS_PROTO]) { 230 NL_SET_ERR_MSG_MOD(extack, "Protocol cannot be used with MPLS modify"); 231 return -EINVAL; 232 } 233 break; 234 default: 235 NL_SET_ERR_MSG_MOD(extack, "Unknown MPLS action"); 236 return -EINVAL; 237 } 238 239 err = tcf_idr_check_alloc(tn, &index, a, bind); 240 if (err < 0) 241 return err; 242 exists = err; 243 if (exists && bind) 244 return 0; 245 246 if (!exists) { 247 ret = tcf_idr_create(tn, index, est, a, 248 &act_mpls_ops, bind, true, 0); 249 if (ret) { 250 tcf_idr_cleanup(tn, index); 251 return ret; 252 } 253 254 ret = ACT_P_CREATED; 255 } else if (!ovr) { 256 tcf_idr_release(*a, bind); 257 return -EEXIST; 258 } 259 260 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack); 261 if (err < 0) 262 goto release_idr; 263 264 m = to_mpls(*a); 265 266 p = kzalloc(sizeof(*p), GFP_KERNEL); 267 if (!p) { 268 err = -ENOMEM; 269 goto put_chain; 270 } 271 272 p->tcfm_action = parm->m_action; 273 p->tcfm_label = tb[TCA_MPLS_LABEL] ? nla_get_u32(tb[TCA_MPLS_LABEL]) : 274 ACT_MPLS_LABEL_NOT_SET; 275 p->tcfm_tc = tb[TCA_MPLS_TC] ? nla_get_u8(tb[TCA_MPLS_TC]) : 276 ACT_MPLS_TC_NOT_SET; 277 p->tcfm_ttl = tb[TCA_MPLS_TTL] ? nla_get_u8(tb[TCA_MPLS_TTL]) : 278 mpls_ttl; 279 p->tcfm_bos = tb[TCA_MPLS_BOS] ? nla_get_u8(tb[TCA_MPLS_BOS]) : 280 ACT_MPLS_BOS_NOT_SET; 281 p->tcfm_proto = tb[TCA_MPLS_PROTO] ? nla_get_be16(tb[TCA_MPLS_PROTO]) : 282 htons(ETH_P_MPLS_UC); 283 284 spin_lock_bh(&m->tcf_lock); 285 goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch); 286 p = rcu_replace_pointer(m->mpls_p, p, lockdep_is_held(&m->tcf_lock)); 287 spin_unlock_bh(&m->tcf_lock); 288 289 if (goto_ch) 290 tcf_chain_put_by_act(goto_ch); 291 if (p) 292 kfree_rcu(p, rcu); 293 294 return ret; 295 put_chain: 296 if (goto_ch) 297 tcf_chain_put_by_act(goto_ch); 298 release_idr: 299 tcf_idr_release(*a, bind); 300 return err; 301 } 302 303 static void tcf_mpls_cleanup(struct tc_action *a) 304 { 305 struct tcf_mpls *m = to_mpls(a); 306 struct tcf_mpls_params *p; 307 308 p = rcu_dereference_protected(m->mpls_p, 1); 309 if (p) 310 kfree_rcu(p, rcu); 311 } 312 313 static int tcf_mpls_dump(struct sk_buff *skb, struct tc_action *a, 314 int bind, int ref) 315 { 316 unsigned char *b = skb_tail_pointer(skb); 317 struct tcf_mpls *m = to_mpls(a); 318 struct tcf_mpls_params *p; 319 struct tc_mpls opt = { 320 .index = m->tcf_index, 321 .refcnt = refcount_read(&m->tcf_refcnt) - ref, 322 .bindcnt = atomic_read(&m->tcf_bindcnt) - bind, 323 }; 324 struct tcf_t t; 325 326 spin_lock_bh(&m->tcf_lock); 327 opt.action = m->tcf_action; 328 p = rcu_dereference_protected(m->mpls_p, lockdep_is_held(&m->tcf_lock)); 329 opt.m_action = p->tcfm_action; 330 331 if (nla_put(skb, TCA_MPLS_PARMS, sizeof(opt), &opt)) 332 goto nla_put_failure; 333 334 if (p->tcfm_label != ACT_MPLS_LABEL_NOT_SET && 335 nla_put_u32(skb, TCA_MPLS_LABEL, p->tcfm_label)) 336 goto nla_put_failure; 337 338 if (p->tcfm_tc != ACT_MPLS_TC_NOT_SET && 339 nla_put_u8(skb, TCA_MPLS_TC, p->tcfm_tc)) 340 goto nla_put_failure; 341 342 if (p->tcfm_ttl && nla_put_u8(skb, TCA_MPLS_TTL, p->tcfm_ttl)) 343 goto nla_put_failure; 344 345 if (p->tcfm_bos != ACT_MPLS_BOS_NOT_SET && 346 nla_put_u8(skb, TCA_MPLS_BOS, p->tcfm_bos)) 347 goto nla_put_failure; 348 349 if (nla_put_be16(skb, TCA_MPLS_PROTO, p->tcfm_proto)) 350 goto nla_put_failure; 351 352 tcf_tm_dump(&t, &m->tcf_tm); 353 354 if (nla_put_64bit(skb, TCA_MPLS_TM, sizeof(t), &t, TCA_MPLS_PAD)) 355 goto nla_put_failure; 356 357 spin_unlock_bh(&m->tcf_lock); 358 359 return skb->len; 360 361 nla_put_failure: 362 spin_unlock_bh(&m->tcf_lock); 363 nlmsg_trim(skb, b); 364 return -EMSGSIZE; 365 } 366 367 static int tcf_mpls_walker(struct net *net, struct sk_buff *skb, 368 struct netlink_callback *cb, int type, 369 const struct tc_action_ops *ops, 370 struct netlink_ext_ack *extack) 371 { 372 struct tc_action_net *tn = net_generic(net, mpls_net_id); 373 374 return tcf_generic_walker(tn, skb, cb, type, ops, extack); 375 } 376 377 static int tcf_mpls_search(struct net *net, struct tc_action **a, u32 index) 378 { 379 struct tc_action_net *tn = net_generic(net, mpls_net_id); 380 381 return tcf_idr_search(tn, a, index); 382 } 383 384 static struct tc_action_ops act_mpls_ops = { 385 .kind = "mpls", 386 .id = TCA_ID_MPLS, 387 .owner = THIS_MODULE, 388 .act = tcf_mpls_act, 389 .dump = tcf_mpls_dump, 390 .init = tcf_mpls_init, 391 .cleanup = tcf_mpls_cleanup, 392 .walk = tcf_mpls_walker, 393 .lookup = tcf_mpls_search, 394 .size = sizeof(struct tcf_mpls), 395 }; 396 397 static __net_init int mpls_init_net(struct net *net) 398 { 399 struct tc_action_net *tn = net_generic(net, mpls_net_id); 400 401 return tc_action_net_init(net, tn, &act_mpls_ops); 402 } 403 404 static void __net_exit mpls_exit_net(struct list_head *net_list) 405 { 406 tc_action_net_exit(net_list, mpls_net_id); 407 } 408 409 static struct pernet_operations mpls_net_ops = { 410 .init = mpls_init_net, 411 .exit_batch = mpls_exit_net, 412 .id = &mpls_net_id, 413 .size = sizeof(struct tc_action_net), 414 }; 415 416 static int __init mpls_init_module(void) 417 { 418 return tcf_register_action(&act_mpls_ops, &mpls_net_ops); 419 } 420 421 static void __exit mpls_cleanup_module(void) 422 { 423 tcf_unregister_action(&act_mpls_ops, &mpls_net_ops); 424 } 425 426 module_init(mpls_init_module); 427 module_exit(mpls_cleanup_module); 428 429 MODULE_SOFTDEP("post: mpls_gso"); 430 MODULE_AUTHOR("Netronome Systems <oss-drivers@netronome.com>"); 431 MODULE_LICENSE("GPL"); 432 MODULE_DESCRIPTION("MPLS manipulation actions"); 433