blob: b9f492ddf93be46ca2fd590e2660b688d5eb338e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Eric W. Biederman01891972015-03-03 19:10:47 -06002#ifndef MPLS_INTERNAL_H
3#define MPLS_INTERNAL_H
Jiri Benc9095e102016-09-30 19:08:06 +02004#include <net/mpls.h>
Eric W. Biederman01891972015-03-03 19:10:47 -06005
David Ahern15110092017-03-31 07:14:04 -07006/* put a reasonable limit on the number of labels
7 * we will accept from userspace
8 */
9#define MAX_NEW_LABELS 30
10
Eric W. Biederman01891972015-03-03 19:10:47 -060011struct mpls_entry_decoded {
12 u32 label;
13 u8 ttl;
14 u8 tc;
15 u8 bos;
16};
17
Robert Shearman27d69102017-01-16 14:16:37 +000018struct mpls_pcpu_stats {
19 struct mpls_link_stats stats;
20 struct u64_stats_sync syncp;
Robert Shearman03c57742015-04-22 11:14:37 +010021};
22
Robert Shearman27d69102017-01-16 14:16:37 +000023struct mpls_dev {
24 int input_enabled;
David Ahern24045a02017-02-20 08:03:30 -080025 struct net_device *dev;
Robert Shearman27d69102017-01-16 14:16:37 +000026 struct mpls_pcpu_stats __percpu *stats;
27
28 struct ctl_table_header *sysctl;
29 struct rcu_head rcu;
30};
31
32#if BITS_PER_LONG == 32
33
34#define MPLS_INC_STATS_LEN(mdev, len, pkts_field, bytes_field) \
35 do { \
36 __typeof__(*(mdev)->stats) *ptr = \
37 raw_cpu_ptr((mdev)->stats); \
38 local_bh_disable(); \
39 u64_stats_update_begin(&ptr->syncp); \
40 ptr->stats.pkts_field++; \
41 ptr->stats.bytes_field += (len); \
42 u64_stats_update_end(&ptr->syncp); \
43 local_bh_enable(); \
44 } while (0)
45
46#define MPLS_INC_STATS(mdev, field) \
47 do { \
48 __typeof__(*(mdev)->stats) *ptr = \
49 raw_cpu_ptr((mdev)->stats); \
50 local_bh_disable(); \
51 u64_stats_update_begin(&ptr->syncp); \
52 ptr->stats.field++; \
53 u64_stats_update_end(&ptr->syncp); \
54 local_bh_enable(); \
55 } while (0)
56
57#else
58
59#define MPLS_INC_STATS_LEN(mdev, len, pkts_field, bytes_field) \
60 do { \
61 this_cpu_inc((mdev)->stats->stats.pkts_field); \
62 this_cpu_add((mdev)->stats->stats.bytes_field, (len)); \
63 } while (0)
64
65#define MPLS_INC_STATS(mdev, field) \
66 this_cpu_inc((mdev)->stats->stats.field)
67
68#endif
69
Eric W. Biederman01891972015-03-03 19:10:47 -060070struct sk_buff;
71
Roopa Prabhuf8efb732015-10-23 06:03:27 -070072#define LABEL_NOT_SPECIFIED (1 << 20)
Roopa Prabhuf8efb732015-10-23 06:03:27 -070073
74/* This maximum ha length copied from the definition of struct neighbour */
Robert Shearmancf4b24f2015-10-27 00:37:36 +000075#define VIA_ALEN_ALIGN sizeof(unsigned long)
76#define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, VIA_ALEN_ALIGN))
Roopa Prabhuf8efb732015-10-23 06:03:27 -070077
78enum mpls_payload_type {
79 MPT_UNSPEC, /* IPv4 or IPv6 */
80 MPT_IPV4 = 4,
81 MPT_IPV6 = 6,
82
83 /* Other types not implemented:
84 * - Pseudo-wire with or without control word (RFC4385)
85 * - GAL (RFC5586)
86 */
87};
88
89struct mpls_nh { /* next hop label forwarding entry */
Benjamin Poirier18916812021-11-29 15:15:06 +090090 struct net_device *nh_dev;
David Ahern39eb8cd2017-03-31 07:13:59 -070091
92 /* nh_flags is accessed under RCU in the packet path; it is
93 * modified handling netdev events with rtnl lock held
94 */
Roopa Prabhuc89359a2015-12-01 22:18:11 -080095 unsigned int nh_flags;
Roopa Prabhuf8efb732015-10-23 06:03:27 -070096 u8 nh_labels;
97 u8 nh_via_alen;
98 u8 nh_via_table;
David Ahern59b20962017-03-31 07:14:01 -070099 u8 nh_reserved1;
100
Gustavo A. R. Silva680a9312020-02-28 07:26:15 -0600101 u32 nh_label[];
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700102};
103
David Ahern59b20962017-03-31 07:14:01 -0700104/* offset of via from beginning of mpls_nh */
105#define MPLS_NH_VIA_OFF(num_labels) \
106 ALIGN(sizeof(struct mpls_nh) + (num_labels) * sizeof(u32), \
107 VIA_ALEN_ALIGN)
108
109/* all nexthops within a route have the same size based on the
110 * max number of labels and max via length across all nexthops
111 */
112#define MPLS_NH_SIZE(num_labels, max_via_alen) \
113 (MPLS_NH_VIA_OFF((num_labels)) + \
114 ALIGN((max_via_alen), VIA_ALEN_ALIGN))
115
Robert Shearman5b441ac2017-03-10 20:43:24 +0000116enum mpls_ttl_propagation {
117 MPLS_TTL_PROP_DEFAULT,
118 MPLS_TTL_PROP_ENABLED,
119 MPLS_TTL_PROP_DISABLED,
120};
121
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000122/* The route, nexthops and vias are stored together in the same memory
123 * block:
124 *
125 * +----------------------+
126 * | mpls_route |
127 * +----------------------+
128 * | mpls_nh 0 |
129 * +----------------------+
David Ahern59b20962017-03-31 07:14:01 -0700130 * | alignment padding | 4 bytes for odd number of labels
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000131 * +----------------------+
132 * | via[rt_max_alen] 0 |
133 * +----------------------+
David Ahern59b20962017-03-31 07:14:01 -0700134 * | alignment padding | via's aligned on sizeof(unsigned long)
135 * +----------------------+
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000136 * | ... |
137 * +----------------------+
David Ahern59b20962017-03-31 07:14:01 -0700138 * | mpls_nh n-1 |
139 * +----------------------+
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000140 * | via[rt_max_alen] n-1 |
141 * +----------------------+
142 */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700143struct mpls_route { /* next hop label forwarding entry */
144 struct rcu_head rt_rcu;
145 u8 rt_protocol;
146 u8 rt_payload_type;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000147 u8 rt_max_alen;
Robert Shearman5b441ac2017-03-10 20:43:24 +0000148 u8 rt_ttl_propagate;
David Ahern77ef013a2017-03-31 07:14:00 -0700149 u8 rt_nhn;
David Ahern39eb8cd2017-03-31 07:13:59 -0700150 /* rt_nhn_alive is accessed under RCU in the packet path; it
151 * is modified handling netdev events with rtnl lock held
152 */
David Ahern77ef013a2017-03-31 07:14:00 -0700153 u8 rt_nhn_alive;
David Ahern59b20962017-03-31 07:14:01 -0700154 u8 rt_nh_size;
155 u8 rt_via_offset;
156 u8 rt_reserved1;
Gustavo A. R. Silva680a9312020-02-28 07:26:15 -0600157 struct mpls_nh rt_nh[];
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700158};
159
160#define for_nexthops(rt) { \
Benjamin Poirierf05b0b92021-11-29 15:23:16 +0900161 int nhsel; const struct mpls_nh *nh; \
Benjamin Poirier69d9c0d2021-11-29 15:23:15 +0900162 for (nhsel = 0, nh = (rt)->rt_nh; \
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700163 nhsel < (rt)->rt_nhn; \
Benjamin Poirier69d9c0d2021-11-29 15:23:15 +0900164 nh = (void *)nh + (rt)->rt_nh_size, nhsel++)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700165
166#define change_nexthops(rt) { \
Benjamin Poirier69d9c0d2021-11-29 15:23:15 +0900167 int nhsel; struct mpls_nh *nh; \
168 for (nhsel = 0, nh = (rt)->rt_nh; \
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700169 nhsel < (rt)->rt_nhn; \
Benjamin Poirier69d9c0d2021-11-29 15:23:15 +0900170 nh = (void *)nh + (rt)->rt_nh_size, nhsel++)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700171
172#define endfor_nexthops(rt) }
173
Eric W. Biederman01891972015-03-03 19:10:47 -0600174static inline struct mpls_entry_decoded mpls_entry_decode(struct mpls_shim_hdr *hdr)
175{
176 struct mpls_entry_decoded result;
177 unsigned entry = be32_to_cpu(hdr->label_stack_entry);
178
179 result.label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
180 result.ttl = (entry & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
181 result.tc = (entry & MPLS_LS_TC_MASK) >> MPLS_LS_TC_SHIFT;
182 result.bos = (entry & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT;
183
184 return result;
185}
186
Robert Shearman27d69102017-01-16 14:16:37 +0000187static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
188{
189 return rcu_dereference_rtnl(dev->mpls_ptr);
190}
191
Roopa Prabhuface0182015-07-21 10:43:52 +0200192int nla_put_labels(struct sk_buff *skb, int attrtype, u8 labels,
193 const u32 label[]);
David Aherna4ac8c92017-03-31 07:14:03 -0700194int nla_get_labels(const struct nlattr *nla, u8 max_labels, u8 *labels,
David Aherna1f10ab2017-05-27 16:19:29 -0600195 u32 label[], struct netlink_ext_ack *extack);
Roopa Prabhuface0182015-07-21 10:43:52 +0200196bool mpls_output_possible(const struct net_device *dev);
197unsigned int mpls_dev_mtu(const struct net_device *dev);
198bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu);
Robert Shearman27d69102017-01-16 14:16:37 +0000199void mpls_stats_inc_outucastpkts(struct net_device *dev,
200 const struct sk_buff *skb);
Eric W. Biederman966bae32015-03-03 19:13:19 -0600201
Eric W. Biederman01891972015-03-03 19:10:47 -0600202#endif /* MPLS_INTERNAL_H */