blob: 732a5c17e9862940aee1f032fb98b48abefdc80a [file] [log] [blame]
Eric W. Biederman01891972015-03-03 19:10:47 -06001#ifndef MPLS_INTERNAL_H
2#define MPLS_INTERNAL_H
3
Eric W. Biederman01891972015-03-03 19:10:47 -06004struct mpls_shim_hdr {
5 __be32 label_stack_entry;
6};
7
8struct mpls_entry_decoded {
9 u32 label;
10 u8 ttl;
11 u8 tc;
12 u8 bos;
13};
14
Robert Shearman03c57742015-04-22 11:14:37 +010015struct mpls_dev {
Robert Shearman37bde792015-04-22 11:14:38 +010016 int input_enabled;
17
18 struct ctl_table_header *sysctl;
Robert Shearman25cc8f02015-06-05 18:54:45 +010019 struct rcu_head rcu;
Robert Shearman03c57742015-04-22 11:14:37 +010020};
21
Eric W. Biederman01891972015-03-03 19:10:47 -060022struct sk_buff;
23
Roopa Prabhuf8efb732015-10-23 06:03:27 -070024#define LABEL_NOT_SPECIFIED (1 << 20)
25#define MAX_NEW_LABELS 2
26
27/* This maximum ha length copied from the definition of struct neighbour */
Robert Shearmancf4b24f2015-10-27 00:37:36 +000028#define VIA_ALEN_ALIGN sizeof(unsigned long)
29#define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, VIA_ALEN_ALIGN))
Roopa Prabhuf8efb732015-10-23 06:03:27 -070030
31enum mpls_payload_type {
32 MPT_UNSPEC, /* IPv4 or IPv6 */
33 MPT_IPV4 = 4,
34 MPT_IPV6 = 6,
35
36 /* Other types not implemented:
37 * - Pseudo-wire with or without control word (RFC4385)
38 * - GAL (RFC5586)
39 */
40};
41
42struct mpls_nh { /* next hop label forwarding entry */
43 struct net_device __rcu *nh_dev;
Roopa Prabhuc89359a2015-12-01 22:18:11 -080044 unsigned int nh_flags;
Roopa Prabhuf8efb732015-10-23 06:03:27 -070045 u32 nh_label[MAX_NEW_LABELS];
46 u8 nh_labels;
47 u8 nh_via_alen;
48 u8 nh_via_table;
Roopa Prabhuf8efb732015-10-23 06:03:27 -070049};
50
Robert Shearmancf4b24f2015-10-27 00:37:36 +000051/* The route, nexthops and vias are stored together in the same memory
52 * block:
53 *
54 * +----------------------+
55 * | mpls_route |
56 * +----------------------+
57 * | mpls_nh 0 |
58 * +----------------------+
59 * | ... |
60 * +----------------------+
61 * | mpls_nh n-1 |
62 * +----------------------+
63 * | alignment padding |
64 * +----------------------+
65 * | via[rt_max_alen] 0 |
66 * +----------------------+
67 * | ... |
68 * +----------------------+
69 * | via[rt_max_alen] n-1 |
70 * +----------------------+
71 */
Roopa Prabhuf8efb732015-10-23 06:03:27 -070072struct mpls_route { /* next hop label forwarding entry */
73 struct rcu_head rt_rcu;
74 u8 rt_protocol;
75 u8 rt_payload_type;
Robert Shearmancf4b24f2015-10-27 00:37:36 +000076 u8 rt_max_alen;
77 unsigned int rt_nhn;
Roopa Prabhuc89359a2015-12-01 22:18:11 -080078 unsigned int rt_nhn_alive;
Roopa Prabhuf8efb732015-10-23 06:03:27 -070079 struct mpls_nh rt_nh[0];
80};
81
82#define for_nexthops(rt) { \
83 int nhsel; struct mpls_nh *nh; \
84 for (nhsel = 0, nh = (rt)->rt_nh; \
85 nhsel < (rt)->rt_nhn; \
86 nh++, nhsel++)
87
88#define change_nexthops(rt) { \
89 int nhsel; struct mpls_nh *nh; \
90 for (nhsel = 0, nh = (struct mpls_nh *)((rt)->rt_nh); \
91 nhsel < (rt)->rt_nhn; \
92 nh++, nhsel++)
93
94#define endfor_nexthops(rt) }
95
Eric W. Biederman01891972015-03-03 19:10:47 -060096static inline struct mpls_shim_hdr *mpls_hdr(const struct sk_buff *skb)
97{
98 return (struct mpls_shim_hdr *)skb_network_header(skb);
99}
100
101static inline struct mpls_shim_hdr mpls_entry_encode(u32 label, unsigned ttl, unsigned tc, bool bos)
102{
103 struct mpls_shim_hdr result;
104 result.label_stack_entry =
105 cpu_to_be32((label << MPLS_LS_LABEL_SHIFT) |
106 (tc << MPLS_LS_TC_SHIFT) |
107 (bos ? (1 << MPLS_LS_S_SHIFT) : 0) |
108 (ttl << MPLS_LS_TTL_SHIFT));
109 return result;
110}
111
112static inline struct mpls_entry_decoded mpls_entry_decode(struct mpls_shim_hdr *hdr)
113{
114 struct mpls_entry_decoded result;
115 unsigned entry = be32_to_cpu(hdr->label_stack_entry);
116
117 result.label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
118 result.ttl = (entry & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
119 result.tc = (entry & MPLS_LS_TC_MASK) >> MPLS_LS_TC_SHIFT;
120 result.bos = (entry & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT;
121
122 return result;
123}
124
Roopa Prabhuface0182015-07-21 10:43:52 +0200125int nla_put_labels(struct sk_buff *skb, int attrtype, u8 labels,
126 const u32 label[]);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700127int nla_get_labels(const struct nlattr *nla, u32 max_labels, u8 *labels,
Roopa Prabhuface0182015-07-21 10:43:52 +0200128 u32 label[]);
Roopa Prabhuf8efb732015-10-23 06:03:27 -0700129int nla_get_via(const struct nlattr *nla, u8 *via_alen, u8 *via_table,
130 u8 via[]);
Roopa Prabhuface0182015-07-21 10:43:52 +0200131bool mpls_output_possible(const struct net_device *dev);
132unsigned int mpls_dev_mtu(const struct net_device *dev);
133bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu);
Eric W. Biederman966bae32015-03-03 19:13:19 -0600134
Eric W. Biederman01891972015-03-03 19:10:47 -0600135#endif /* MPLS_INTERNAL_H */