1 /*
2  * iana_afi and safi definitions.
3  * Copyright (C) 2018-2019 Cumulus Networks, Inc.
4  * Donald Sharp
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2 of the License, or (at your option)
9  * any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; see the file COPYING; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 #ifndef __IANA_AFI_H__
21 
22 #include <prefix.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /*
29  * The above AFI and SAFI definitions are for internal use. The protocol
30  * definitions (IANA values) as for example used in BGP protocol packets
31  * are defined below and these will get mapped to/from the internal values
32  * in the appropriate places.
33  * The rationale is that the protocol (IANA) values may be sparse and are
34  * not optimal for use in data-structure sizing.
35  * Note: Only useful (i.e., supported) values are defined below.
36  */
37 typedef enum {
38 	IANA_AFI_RESERVED = 0,
39 	IANA_AFI_IPV4 = 1,
40 	IANA_AFI_IPV6 = 2,
41 	IANA_AFI_L2VPN = 25,
42 } iana_afi_t;
43 
44 typedef enum {
45 	IANA_SAFI_RESERVED = 0,
46 	IANA_SAFI_UNICAST = 1,
47 	IANA_SAFI_MULTICAST = 2,
48 	IANA_SAFI_LABELED_UNICAST = 4,
49 	IANA_SAFI_ENCAP = 7,
50 	IANA_SAFI_EVPN = 70,
51 	IANA_SAFI_MPLS_VPN = 128,
52 	IANA_SAFI_FLOWSPEC = 133
53 } iana_safi_t;
54 
afi_iana2int(iana_afi_t afi)55 static inline afi_t afi_iana2int(iana_afi_t afi)
56 {
57 	switch (afi) {
58 	case IANA_AFI_IPV4:
59 		return AFI_IP;
60 	case IANA_AFI_IPV6:
61 		return AFI_IP6;
62 	case IANA_AFI_L2VPN:
63 		return AFI_L2VPN;
64 	default:
65 		return AFI_MAX;
66 	}
67 }
68 
afi_int2iana(afi_t afi)69 static inline iana_afi_t afi_int2iana(afi_t afi)
70 {
71 	switch (afi) {
72 	case AFI_IP:
73 		return IANA_AFI_IPV4;
74 	case AFI_IP6:
75 		return IANA_AFI_IPV6;
76 	case AFI_L2VPN:
77 		return IANA_AFI_L2VPN;
78 	default:
79 		return IANA_AFI_RESERVED;
80 	}
81 }
82 
iana_afi2str(iana_afi_t afi)83 static inline const char *iana_afi2str(iana_afi_t afi)
84 {
85 	return afi2str(afi_iana2int(afi));
86 }
87 
safi_iana2int(iana_safi_t safi)88 static inline safi_t safi_iana2int(iana_safi_t safi)
89 {
90 	switch (safi) {
91 	case IANA_SAFI_UNICAST:
92 		return SAFI_UNICAST;
93 	case IANA_SAFI_MULTICAST:
94 		return SAFI_MULTICAST;
95 	case IANA_SAFI_MPLS_VPN:
96 		return SAFI_MPLS_VPN;
97 	case IANA_SAFI_ENCAP:
98 		return SAFI_ENCAP;
99 	case IANA_SAFI_EVPN:
100 		return SAFI_EVPN;
101 	case IANA_SAFI_LABELED_UNICAST:
102 		return SAFI_LABELED_UNICAST;
103 	case IANA_SAFI_FLOWSPEC:
104 		return SAFI_FLOWSPEC;
105 	default:
106 		return SAFI_MAX;
107 	}
108 }
109 
safi_int2iana(safi_t safi)110 static inline iana_safi_t safi_int2iana(safi_t safi)
111 {
112 	switch (safi) {
113 	case SAFI_UNICAST:
114 		return IANA_SAFI_UNICAST;
115 	case SAFI_MULTICAST:
116 		return IANA_SAFI_MULTICAST;
117 	case SAFI_MPLS_VPN:
118 		return IANA_SAFI_MPLS_VPN;
119 	case SAFI_ENCAP:
120 		return IANA_SAFI_ENCAP;
121 	case SAFI_EVPN:
122 		return IANA_SAFI_EVPN;
123 	case SAFI_LABELED_UNICAST:
124 		return IANA_SAFI_LABELED_UNICAST;
125 	case SAFI_FLOWSPEC:
126 		return IANA_SAFI_FLOWSPEC;
127 	default:
128 		return IANA_SAFI_RESERVED;
129 	}
130 }
131 
iana_safi2str(iana_safi_t safi)132 static inline const char *iana_safi2str(iana_safi_t safi)
133 {
134 	return safi2str(safi_iana2int(safi));
135 }
136 
137 #ifdef __cplusplus
138 }
139 #endif
140 
141 #endif
142