]> git.proxmox.com Git - mirror_frr.git/blob - lib/mpls.h
Merge pull request #538 from qlyoung/fix-stack-access-2
[mirror_frr.git] / lib / mpls.h
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 *
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
20 */
21
22 #ifndef _QUAGGA_MPLS_H
23 #define _QUAGGA_MPLS_H
24
25 #include <arpa/inet.h>
26
27 /* Well-known MPLS label values (RFC 3032 etc). */
28 #define MPLS_V4_EXP_NULL_LABEL 0
29 #define MPLS_RA_LABEL 1
30 #define MPLS_V6_EXP_NULL_LABEL 2
31 #define MPLS_IMP_NULL_LABEL 3
32 #define MPLS_ENTROPY_LABEL_INDICATOR 7
33 #define MPLS_GAL_LABEL 13
34 #define MPLS_OAM_ALERT_LABEL 14
35 #define MPLS_EXTENSION_LABEL 15
36
37 /* Minimum and maximum label values */
38 #define MPLS_MIN_RESERVED_LABEL 0
39 #define MPLS_MAX_RESERVED_LABEL 15
40 #define MPLS_MIN_UNRESERVED_LABEL 16
41 #define MPLS_MAX_UNRESERVED_LABEL 1048575
42
43 #define IS_MPLS_RESERVED_LABEL(label) \
44 (label >= MPLS_MIN_RESERVED_LABEL && label <= MPLS_MAX_RESERVED_LABEL)
45
46 #define IS_MPLS_UNRESERVED_LABEL(label) \
47 (label >= MPLS_MIN_UNRESERVED_LABEL && label <= MPLS_MAX_UNRESERVED_LABEL)
48
49 /* Definitions for a MPLS label stack entry (RFC 3032). This encodes the
50 * label, EXP, BOS and TTL fields.
51 */
52 typedef unsigned int mpls_lse_t;
53
54 #define MPLS_LS_LABEL_MASK 0xFFFFF000
55 #define MPLS_LS_LABEL_SHIFT 12
56 #define MPLS_LS_EXP_MASK 0x00000E00
57 #define MPLS_LS_EXP_SHIFT 9
58 #define MPLS_LS_S_MASK 0x00000100
59 #define MPLS_LS_S_SHIFT 8
60 #define MPLS_LS_TTL_MASK 0x000000FF
61 #define MPLS_LS_TTL_SHIFT 0
62
63 #define MPLS_LABEL_VALUE(lse) \
64 ((lse & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT)
65 #define MPLS_LABEL_EXP(lse) \
66 ((lse & MPLS_LS_EXP_MASK) >> MPLS_LS_EXP_SHIFT)
67 #define MPLS_LABEL_BOS(lse) \
68 ((lse & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT)
69 #define MPLS_LABEL_TTL(lse) \
70 ((lse & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT)
71
72 #define IS_MPLS_LABEL_BOS(ls) (MPLS_LABEL_BOS(ls) == 1)
73
74 #define MPLS_LABEL_LEN_BITS 20
75
76 /* MPLS label value as a 32-bit (mostly we only care about the label value). */
77 typedef unsigned int mpls_label_t;
78
79 #define MPLS_NO_LABEL 0xFFFFFFFF
80 #define MPLS_INVALID_LABEL 0xFFFFFFFF
81
82 /* LSP types. */
83 enum lsp_types_t
84 {
85 ZEBRA_LSP_NONE = 0, /* No LSP. */
86 ZEBRA_LSP_STATIC = 1, /* Static LSP. */
87 ZEBRA_LSP_LDP = 2, /* LDP LSP. */
88 ZEBRA_LSP_BGP = 3 /* BGP LSP. */
89 };
90
91 /* Functions for basic label operations. */
92
93 /* Encode a label stack entry from fields; convert to network byte-order as
94 * the Netlink interface expects MPLS labels to be in this format.
95 */
96 static inline mpls_lse_t
97 mpls_lse_encode (mpls_label_t label, u_int32_t ttl,
98 u_int32_t exp, u_int32_t bos)
99 {
100 mpls_lse_t lse;
101 lse = htonl ((label << MPLS_LS_LABEL_SHIFT) |
102 (exp << MPLS_LS_EXP_SHIFT) |
103 (bos ? (1 << MPLS_LS_S_SHIFT) : 0) |
104 (ttl << MPLS_LS_TTL_SHIFT));
105 return lse;
106 }
107
108 /* Extract the fields from a label stack entry after converting to host-byte
109 * order. This is expected to be called only for messages received over the
110 * Netlink interface.
111 */
112 static inline void
113 mpls_lse_decode (mpls_lse_t lse, mpls_label_t *label,
114 u_int32_t *ttl, u_int32_t *exp, u_int32_t *bos)
115 {
116 mpls_lse_t local_lse;
117
118 local_lse = ntohl (lse);
119 *label = MPLS_LABEL_VALUE(local_lse);
120 *exp = MPLS_LABEL_EXP(local_lse);
121 *bos = MPLS_LABEL_BOS(local_lse);
122 *ttl = MPLS_LABEL_TTL(local_lse);
123 }
124
125 /* Invalid label index value (when used with BGP Prefix-SID). Should
126 * match the BGP definition.
127 */
128 #define MPLS_INVALID_LABEL_INDEX 0xFFFFFFFF
129
130
131 /* Printable string for labels (with consideration for reserved values). */
132 static inline char *
133 label2str (mpls_label_t label, char *buf, size_t len)
134 {
135 switch(label) {
136 case MPLS_V4_EXP_NULL_LABEL:
137 strlcpy(buf, "IPv4 Explicit Null", len);
138 return(buf);
139 case MPLS_RA_LABEL:
140 strlcpy(buf, "Router Alert", len);
141 return(buf);
142 case MPLS_V6_EXP_NULL_LABEL:
143 strlcpy(buf, "IPv6 Explict Null", len);
144 return(buf);
145 case MPLS_IMP_NULL_LABEL:
146 strlcpy(buf, "implicit-null", len);
147 return(buf);
148 case MPLS_ENTROPY_LABEL_INDICATOR:
149 strlcpy(buf, "Entropy Label Indicator", len);
150 return(buf);
151 case MPLS_GAL_LABEL:
152 strlcpy(buf, "Generic Associated Channel", len);
153 return(buf);
154 case MPLS_OAM_ALERT_LABEL:
155 strlcpy(buf, "OAM Alert", len);
156 return(buf);
157 case MPLS_EXTENSION_LABEL:
158 strlcpy(buf, "Extension", len);
159 return(buf);
160 default:
161 if (label < 16)
162 snprintf(buf, len, "Reserved (%u)", label);
163 else
164 snprintf(buf, len, "%u", label);
165 return(buf);
166 }
167 }
168
169 /* constants used by ldpd */
170 #define MPLS_LABEL_IPV4NULL 0 /* IPv4 Explicit NULL Label */
171 #define MPLS_LABEL_RTALERT 1 /* Router Alert Label */
172 #define MPLS_LABEL_IPV6NULL 2 /* IPv6 Explicit NULL Label */
173 #define MPLS_LABEL_IMPLNULL 3 /* Implicit NULL Label */
174 /* MPLS_LABEL_RESERVED 4-15 */ /* Values 4-15 are reserved */
175 #define MPLS_LABEL_RESERVED_MAX 15
176 #define MPLS_LABEL_MAX ((1 << 20) - 1)
177
178 #endif