]>
Commit | Line | Data |
---|---|---|
50f34207 | 1 | /* |
2 | * MPLS definitions | |
3 | * Copyright 2015 Cumulus Networks, Inc. | |
4 | * | |
5 | * This file is part of GNU Zebra. | |
6 | * | |
7 | * GNU Zebra is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published | |
9 | * by the Free Software Foundation; either version 2, or (at your | |
10 | * option) any later version. | |
11 | * | |
12 | * GNU Zebra is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
896014f4 DL |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
50f34207 | 20 | */ |
21 | ||
22 | #ifndef _QUAGGA_MPLS_H | |
23 | #define _QUAGGA_MPLS_H | |
24 | ||
43e52561 | 25 | #include <zebra.h> |
fea12efb | 26 | #include <arpa/inet.h> |
27 | ||
5e244469 RW |
28 | #ifdef __cplusplus |
29 | extern "C" { | |
30 | #endif | |
31 | ||
70e98a7f DS |
32 | #ifdef MPLS_LABEL_MAX |
33 | #undef MPLS_LABEL_MAX | |
34 | #endif | |
35 | ||
f890b146 DS |
36 | #define MPLS_LABEL_HELPSTR \ |
37 | "Specify label(s) for this route\nOne or more " \ | |
38 | "labels in the range (16-1048575) separated by '/'\n" | |
39 | ||
50f34207 | 40 | /* Well-known MPLS label values (RFC 3032 etc). */ |
70e98a7f DS |
41 | #define MPLS_LABEL_IPV4_EXPLICIT_NULL 0 /* [RFC3032] */ |
42 | #define MPLS_LABEL_ROUTER_ALERT 1 /* [RFC3032] */ | |
43 | #define MPLS_LABEL_IPV6_EXPLICIT_NULL 2 /* [RFC3032] */ | |
44 | #define MPLS_LABEL_IMPLICIT_NULL 3 /* [RFC3032] */ | |
45 | #define MPLS_LABEL_ELI 7 /* [RFC6790] */ | |
46 | #define MPLS_LABEL_GAL 13 /* [RFC5586] */ | |
47 | #define MPLS_LABEL_OAM_ALERT 14 /* [RFC3429] */ | |
48 | #define MPLS_LABEL_EXTENSION 15 /* [RFC7274] */ | |
49 | #define MPLS_LABEL_MAX 1048575 | |
42567e00 | 50 | #define MPLS_LABEL_NONE 0xFFFFFFFF /* for internal use only */ |
50f34207 | 51 | |
52 | /* Minimum and maximum label values */ | |
70e98a7f DS |
53 | #define MPLS_LABEL_RESERVED_MIN 0 |
54 | #define MPLS_LABEL_RESERVED_MAX 15 | |
55 | #define MPLS_LABEL_UNRESERVED_MIN 16 | |
56 | #define MPLS_LABEL_UNRESERVED_MAX 1048575 | |
50f34207 | 57 | |
12929668 | 58 | /* Default min and max SRGB label range */ |
7726c479 OD |
59 | /* Even if the SRGB allows to manage different Label space between routers, |
60 | * if an operator want to use the same SRGB for all its router, we must fix | |
61 | * a common range. However, Cisco start its SRGB at 16000 and Juniper ends | |
62 | * its SRGB at 16384 for OSPF. Thus, by fixing the minimum SRGB label to | |
63 | * 8000 we could deal with both Cisco and Juniper. | |
64 | */ | |
65 | #define MPLS_DEFAULT_MIN_SRGB_LABEL 8000 | |
cf9b9f77 OD |
66 | #define MPLS_DEFAULT_MAX_SRGB_LABEL 50000 |
67 | #define MPLS_DEFAULT_MIN_SRGB_SIZE 5000 | |
68 | #define MPLS_DEFAULT_MAX_SRGB_SIZE 20000 | |
12929668 | 69 | |
52dd3aa4 RW |
70 | /* Maximum # labels that can be pushed. */ |
71 | #define MPLS_MAX_LABELS 16 | |
72 | ||
d62a17ae | 73 | #define IS_MPLS_RESERVED_LABEL(label) \ |
70e98a7f | 74 | (label >= MPLS_LABEL_RESERVED_MIN && label <= MPLS_LABEL_RESERVED_MAX) |
50f34207 | 75 | |
d62a17ae | 76 | #define IS_MPLS_UNRESERVED_LABEL(label) \ |
70e98a7f DS |
77 | (label >= MPLS_LABEL_UNRESERVED_MIN \ |
78 | && label <= MPLS_LABEL_UNRESERVED_MAX) | |
50f34207 | 79 | |
80 | /* Definitions for a MPLS label stack entry (RFC 3032). This encodes the | |
81 | * label, EXP, BOS and TTL fields. | |
82 | */ | |
83 | typedef unsigned int mpls_lse_t; | |
84 | ||
85 | #define MPLS_LS_LABEL_MASK 0xFFFFF000 | |
86 | #define MPLS_LS_LABEL_SHIFT 12 | |
87 | #define MPLS_LS_EXP_MASK 0x00000E00 | |
88 | #define MPLS_LS_EXP_SHIFT 9 | |
89 | #define MPLS_LS_S_MASK 0x00000100 | |
90 | #define MPLS_LS_S_SHIFT 8 | |
91 | #define MPLS_LS_TTL_MASK 0x000000FF | |
92 | #define MPLS_LS_TTL_SHIFT 0 | |
93 | ||
d62a17ae | 94 | #define MPLS_LABEL_VALUE(lse) \ |
95 | ((lse & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT) | |
96 | #define MPLS_LABEL_EXP(lse) ((lse & MPLS_LS_EXP_MASK) >> MPLS_LS_EXP_SHIFT) | |
97 | #define MPLS_LABEL_BOS(lse) ((lse & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT) | |
98 | #define MPLS_LABEL_TTL(lse) ((lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT) | |
50f34207 | 99 | |
100 | #define IS_MPLS_LABEL_BOS(ls) (MPLS_LABEL_BOS(ls) == 1) | |
101 | ||
102 | #define MPLS_LABEL_LEN_BITS 20 | |
103 | ||
104 | /* MPLS label value as a 32-bit (mostly we only care about the label value). */ | |
105 | typedef unsigned int mpls_label_t; | |
106 | ||
8ecdb26e DS |
107 | struct mpls_label_stack { |
108 | uint8_t num_labels; | |
109 | uint8_t reserved[3]; | |
110 | mpls_label_t label[0]; /* 1 or more labels */ | |
111 | }; | |
112 | ||
9bedbb1e DW |
113 | /* The MPLS explicit-null label is 0 which means when you memset a mpls_label_t |
114 | * to zero you have set that variable to explicit-null which was probably not | |
115 | * your intent. The work-around is to use one bit to indicate if the | |
116 | * mpls_label_t has been set by the user. MPLS_INVALID_LABEL has this bit clear | |
117 | * so that we can use MPLS_INVALID_LABEL to initialize mpls_label_t variables. | |
118 | */ | |
119 | #define MPLS_INVALID_LABEL 0xFFFDFFFF | |
50f34207 | 120 | |
ce549947 | 121 | /* LSP types. */ |
d62a17ae | 122 | enum lsp_types_t { |
123 | ZEBRA_LSP_NONE = 0, /* No LSP. */ | |
124 | ZEBRA_LSP_STATIC = 1, /* Static LSP. */ | |
125 | ZEBRA_LSP_LDP = 2, /* LDP LSP. */ | |
cf9b9f77 | 126 | ZEBRA_LSP_BGP = 3, /* BGP LSP. */ |
339e36d2 DS |
127 | ZEBRA_LSP_SR = 4, /* Segment Routing LSP. */ |
128 | ZEBRA_LSP_SHARP = 5, /* Identifier for test protocol */ | |
ce549947 RW |
129 | }; |
130 | ||
50f34207 | 131 | /* Functions for basic label operations. */ |
132 | ||
133 | /* Encode a label stack entry from fields; convert to network byte-order as | |
134 | * the Netlink interface expects MPLS labels to be in this format. | |
135 | */ | |
d7c0a89a QY |
136 | static inline mpls_lse_t mpls_lse_encode(mpls_label_t label, uint32_t ttl, |
137 | uint32_t exp, uint32_t bos) | |
50f34207 | 138 | { |
d62a17ae | 139 | mpls_lse_t lse; |
140 | lse = htonl((label << MPLS_LS_LABEL_SHIFT) | (exp << MPLS_LS_EXP_SHIFT) | |
141 | | (bos ? (1 << MPLS_LS_S_SHIFT) : 0) | |
142 | | (ttl << MPLS_LS_TTL_SHIFT)); | |
143 | return lse; | |
50f34207 | 144 | } |
145 | ||
146 | /* Extract the fields from a label stack entry after converting to host-byte | |
147 | * order. This is expected to be called only for messages received over the | |
148 | * Netlink interface. | |
149 | */ | |
d62a17ae | 150 | static inline void mpls_lse_decode(mpls_lse_t lse, mpls_label_t *label, |
d7c0a89a | 151 | uint32_t *ttl, uint32_t *exp, uint32_t *bos) |
50f34207 | 152 | { |
d62a17ae | 153 | mpls_lse_t local_lse; |
50f34207 | 154 | |
d62a17ae | 155 | local_lse = ntohl(lse); |
156 | *label = MPLS_LABEL_VALUE(local_lse); | |
157 | *exp = MPLS_LABEL_EXP(local_lse); | |
158 | *bos = MPLS_LABEL_BOS(local_lse); | |
159 | *ttl = MPLS_LABEL_TTL(local_lse); | |
50f34207 | 160 | } |
161 | ||
28d58fd7 VV |
162 | /* Invalid label index value (when used with BGP Prefix-SID). Should |
163 | * match the BGP definition. | |
164 | */ | |
165 | #define MPLS_INVALID_LABEL_INDEX 0xFFFFFFFF | |
166 | ||
50f34207 | 167 | /* Printable string for labels (with consideration for reserved values). */ |
d62a17ae | 168 | static inline char *label2str(mpls_label_t label, char *buf, size_t len) |
50f34207 | 169 | { |
d62a17ae | 170 | switch (label) { |
70e98a7f | 171 | case MPLS_LABEL_IPV4_EXPLICIT_NULL: |
d62a17ae | 172 | strlcpy(buf, "IPv4 Explicit Null", len); |
173 | return (buf); | |
70e98a7f | 174 | case MPLS_LABEL_ROUTER_ALERT: |
d62a17ae | 175 | strlcpy(buf, "Router Alert", len); |
176 | return (buf); | |
70e98a7f | 177 | case MPLS_LABEL_IPV6_EXPLICIT_NULL: |
0437e105 | 178 | strlcpy(buf, "IPv6 Explicit Null", len); |
d62a17ae | 179 | return (buf); |
70e98a7f | 180 | case MPLS_LABEL_IMPLICIT_NULL: |
d62a17ae | 181 | strlcpy(buf, "implicit-null", len); |
182 | return (buf); | |
70e98a7f | 183 | case MPLS_LABEL_ELI: |
d62a17ae | 184 | strlcpy(buf, "Entropy Label Indicator", len); |
185 | return (buf); | |
70e98a7f | 186 | case MPLS_LABEL_GAL: |
d62a17ae | 187 | strlcpy(buf, "Generic Associated Channel", len); |
188 | return (buf); | |
70e98a7f | 189 | case MPLS_LABEL_OAM_ALERT: |
d62a17ae | 190 | strlcpy(buf, "OAM Alert", len); |
191 | return (buf); | |
70e98a7f | 192 | case MPLS_LABEL_EXTENSION: |
d62a17ae | 193 | strlcpy(buf, "Extension", len); |
194 | return (buf); | |
195 | default: | |
196 | if (label < 16) | |
197 | snprintf(buf, len, "Reserved (%u)", label); | |
198 | else | |
199 | snprintf(buf, len, "%u", label); | |
200 | return (buf); | |
201 | } | |
50f34207 | 202 | } |
203 | ||
036fdaad DS |
204 | /* |
205 | * String to label conversion, labels separated by '/'. | |
206 | */ | |
207 | int mpls_str2label(const char *label_str, uint8_t *num_labels, | |
208 | mpls_label_t *labels); | |
209 | ||
210 | /* | |
211 | * Label to string conversion, labels in string separated by '/'. | |
212 | */ | |
213 | char *mpls_label2str(uint8_t num_labels, mpls_label_t *labels, char *buf, | |
214 | int len, int pretty); | |
eac6e3f0 | 215 | |
5e244469 RW |
216 | #ifdef __cplusplus |
217 | } | |
218 | #endif | |
219 | ||
50f34207 | 220 | #endif |