blob: 4db6a59713220dcefac8be970ab06b1ae25bcd5c [file] [log] [blame]
Eric W. Biederman01891972015-03-03 19:10:47 -06001#ifndef MPLS_INTERNAL_H
2#define MPLS_INTERNAL_H
Jiri Benc9095e102016-09-30 19:08:06 +02003#include <net/mpls.h>
Eric W. Biederman01891972015-03-03 19:10:47 -06004
David Ahern15110092017-03-31 07:14:04 -07005/* put a reasonable limit on the number of labels
6 * we will accept from userspace
7 */
8#define MAX_NEW_LABELS 30
9
Eric W. Biederman01891972015-03-03 19:10:47 -060010struct mpls_entry_decoded {
11 u32 label;
12 u8 ttl;
13 u8 tc;
14 u8 bos;
15};
16
Robert Shearman27d69102017-01-16 14:16:37 +000017struct mpls_pcpu_stats {
18 struct mpls_link_stats stats;
19 struct u64_stats_sync syncp;
Robert Shearman03c57742015-04-22 11:14:37 +010020};
21
Robert Shearman27d69102017-01-16 14:16:37 +000022struct mpls_dev {
23 int input_enabled;
David Ahern24045a02017-02-20 08:03:30 -080024 struct net_device *dev;
Robert Shearman27d69102017-01-16 14:16:37 +000025 struct mpls_pcpu_stats __percpu *stats;
26
27 struct ctl_table_header *sysctl;
28 struct rcu_head rcu;
29};
30
31#if BITS_PER_LONG == 32
32
33#define MPLS_INC_STATS_LEN(mdev, len, pkts_field, bytes_field) \
34 do { \
35 __typeof__(*(mdev)->stats) *ptr = \
36 raw_cpu_ptr((mdev)->stats); \
37 local_bh_disable(); \
38 u64_stats_update_begin(&ptr->syncp); \
39 ptr->stats.pkts_field++; \
40 ptr->stats.bytes_field += (len); \
41 u64_stats_update_end(&ptr->syncp); \
42 local_bh_enable(); \
43 } while (0)
44
45#define MPLS_INC_STATS(mdev, field) \
46 do { \
47 __typeof__(*(mdev)->stats) *ptr = \
48 raw_cpu_ptr((mdev)->stats); \
49 local_bh_disable(); \
50 u64_stats_update_begin(&ptr->syncp); \
51 ptr->stats.field++; \
52 u64_stats_update_end(&ptr->syncp); \
53 local_bh_enable(); \
54 } while (0)
55
56#else
57
58#define MPLS_INC_STATS_LEN(mdev, len, pkts_field, bytes_field) \
59 do { \
60 this_cpu_inc((mdev)->stats->stats.pkts_field); \
61 this_cpu_add((mdev)->stats->stats.bytes_field, (len)); \
62 } while (0)
63
64#define MPLS_INC_STATS(mdev, field) \
65 this_cpu_inc((mdev)->stats->stats.field)
66
67#endif
68
Eric W. Biederman01891972015-03-03 19:10:47 -060069struct sk_buff;
70
Roopa Prabhuf8efb732015-10-23 06:03:27 -070071#define LABEL_NOT_SPECIFIED (1 << 20)
Roopa Prabhuf8efb732015-10-23 06:03:27 -070072
73/* This maximum ha length copied from the definition of struct neighbour */
Robert Shearmancf4b24f2015-10-27 00:37:36 +000074#define VIA_ALEN_ALIGN sizeof(unsigned long)
75#define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, VIA_ALEN_ALIGN))
Roopa Prabhuf8efb732015-10-23 06:03:27 -070076
77enum mpls_payload_type {
78 MPT_UNSPEC, /* IPv4 or IPv6 */
79 MPT_IPV4 = 4,
80 MPT_IPV6 = 6,
81
82 /* Other types not implemented:
83 * - Pseudo-wire with or without control word (RFC4385)
84 * - GAL (RFC5586)
85 */
86};
87
88struct mpls_nh { /* next hop label forwarding entry */
89 struct net_device __rcu *nh_dev;
David Ahern39eb8cd2017-03-31 07:13:59 -070090
91 /* nh_flags is accessed under RCU in the packet path; it is
92 * modified handling netdev events with rtnl lock held
93 */
Roopa Prabhuc89359a2015-12-01 22:18:11 -080094 unsigned int nh_flags;
Roopa Prabhuf8efb732015-10-23 06:03:27 -070095 u8 nh_labels;
96 u8 nh_via_alen;
97 u8 nh_via_table;
David Ahern59b20962017-03-31 07:14:01 -070098 u8 nh_reserved1;
99
100 u32 nh_label[0];
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700101};
102
David Ahern59b20962017-03-31 07:14:01 -0700103/* offset of via from beginning of mpls_nh */
104#define MPLS_NH_VIA_OFF(num_labels) \
105 ALIGN(sizeof(struct mpls_nh) + (num_labels) * sizeof(u32), \
106 VIA_ALEN_ALIGN)
107
108/* all nexthops within a route have the same size based on the
109 * max number of labels and max via length across all nexthops
110 */
111#define MPLS_NH_SIZE(num_labels, max_via_alen) \
112 (MPLS_NH_VIA_OFF((num_labels)) + \
113 ALIGN((max_via_alen), VIA_ALEN_ALIGN))
114
Robert Shearman5b441ac2017-03-10 20:43:24 +0000115enum mpls_ttl_propagation {
116 MPLS_TTL_PROP_DEFAULT,
117 MPLS_TTL_PROP_ENABLED,
118 MPLS_TTL_PROP_DISABLED,
119};
120
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000121/* The route, nexthops and vias are stored together in the same memory
122 * block:
123 *
124 * +----------------------+
125 * | mpls_route |
126 * +----------------------+
127 * | mpls_nh 0 |
128 * +----------------------+
David Ahern59b20962017-03-31 07:14:01 -0700129 * | alignment padding | 4 bytes for odd number of labels
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000130 * +----------------------+
131 * | via[rt_max_alen] 0 |
132 * +----------------------+
David Ahern59b20962017-03-31 07:14:01 -0700133 * | alignment padding | via's aligned on sizeof(unsigned long)
134 * +----------------------+
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000135 * | ... |
136 * +----------------------+
David Ahern59b20962017-03-31 07:14:01 -0700137 * | mpls_nh n-1 |
138 * +----------------------+
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000139 * | via[rt_max_alen] n-1 |
140 * +----------------------+
141 */
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700142struct mpls_route { /* next hop label forwarding entry */
143 struct rcu_head rt_rcu;
144 u8 rt_protocol;
145 u8 rt_payload_type;
Robert Shearmancf4b24f2015-10-27 00:37:36 +0000146 u8 rt_max_alen;
Robert Shearman5b441ac2017-03-10 20:43:24 +0000147 u8 rt_ttl_propagate;
David Ahern77ef013a2017-03-31 07:14:00 -0700148 u8 rt_nhn;
David Ahern39eb8cd2017-03-31 07:13:59 -0700149 /* rt_nhn_alive is accessed under RCU in the packet path; it
150 * is modified handling netdev events with rtnl lock held
151 */
David Ahern77ef013a2017-03-31 07:14:00 -0700152 u8 rt_nhn_alive;
David Ahern59b20962017-03-31 07:14:01 -0700153 u8 rt_nh_size;
154 u8 rt_via_offset;
155 u8 rt_reserved1;
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700156 struct mpls_nh rt_nh[0];
157};
158
159#define for_nexthops(rt) { \
David Ahern59b20962017-03-31 07:14:01 -0700160 int nhsel; struct mpls_nh *nh; u8 *__nh; \
161 for (nhsel = 0, nh = (rt)->rt_nh, __nh = (u8 *)((rt)->rt_nh); \
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700162 nhsel < (rt)->rt_nhn; \
David Ahern59b20962017-03-31 07:14:01 -0700163 __nh += rt->rt_nh_size, nh = (struct mpls_nh *)__nh, nhsel++)
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700164
165#define change_nexthops(rt) { \
David Ahern59b20962017-03-31 07:14:01 -0700166 int nhsel; struct mpls_nh *nh; u8 *__nh; \
167 for (nhsel = 0, nh = (struct mpls_nh *)((rt)->rt_nh), \
168 __nh = (u8 *)((rt)->rt_nh); \
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700169 nhsel < (rt)->rt_nhn; \
David Ahern59b20962017-03-31 07:14:01 -0700170 __nh += rt->rt_nh_size, nh = (struct mpls_nh *)__nh, 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_shim_hdr mpls_entry_encode(u32 label, unsigned ttl, unsigned tc, bool bos)
175{
176 struct mpls_shim_hdr result;
177 result.label_stack_entry =
178 cpu_to_be32((label << MPLS_LS_LABEL_SHIFT) |
179 (tc << MPLS_LS_TC_SHIFT) |
180 (bos ? (1 << MPLS_LS_S_SHIFT) : 0) |
181 (ttl << MPLS_LS_TTL_SHIFT));
182 return result;
183}
184
185static inline struct mpls_entry_decoded mpls_entry_decode(struct mpls_shim_hdr *hdr)
186{
187 struct mpls_entry_decoded result;
188 unsigned entry = be32_to_cpu(hdr->label_stack_entry);
189
190 result.label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
191 result.ttl = (entry & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
192 result.tc = (entry & MPLS_LS_TC_MASK) >> MPLS_LS_TC_SHIFT;
193 result.bos = (entry & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT;
194
195 return result;
196}
197
Robert Shearman27d69102017-01-16 14:16:37 +0000198static inline struct mpls_dev *mpls_dev_get(const struct net_device *dev)
199{
200 return rcu_dereference_rtnl(dev->mpls_ptr);
201}
202
Roopa Prabhuface0182015-07-21 10:43:52 +0200203int nla_put_labels(struct sk_buff *skb, int attrtype, u8 labels,
204 const u32 label[]);
David Aherna4ac8c92017-03-31 07:14:03 -0700205int nla_get_labels(const struct nlattr *nla, u8 max_labels, u8 *labels,
Roopa Prabhuface0182015-07-21 10:43:52 +0200206 u32 label[]);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700207int nla_get_via(const struct nlattr *nla, u8 *via_alen, u8 *via_table,
208 u8 via[]);
Roopa Prabhuface0182015-07-21 10:43:52 +0200209bool mpls_output_possible(const struct net_device *dev);
210unsigned int mpls_dev_mtu(const struct net_device *dev);
211bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu);
Robert Shearman27d69102017-01-16 14:16:37 +0000212void mpls_stats_inc_outucastpkts(struct net_device *dev,
213 const struct sk_buff *skb);
Eric W. Biederman966bae32015-03-03 19:13:19 -0600214
Eric W. Biederman01891972015-03-03 19:10:47 -0600215#endif /* MPLS_INTERNAL_H */