BFD: SHA1 authentication
[vpp.git] / src / vnet / bfd / bfd_protocol.c
1 /*
2  * Copyright (c) 2011-2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vnet/bfd/bfd_protocol.h>
16
17 u8
18 bfd_pkt_get_version (const bfd_pkt_t * pkt)
19 {
20   return pkt->head.vers_diag >> 5;
21 }
22
23 void
24 bfd_pkt_set_version (bfd_pkt_t * pkt, int version)
25 {
26   pkt->head.vers_diag =
27     (version << 5) | (pkt->head.vers_diag & ((1 << 5) - 1));
28 }
29
30 u8
31 bfd_pkt_get_diag_code (const bfd_pkt_t * pkt)
32 {
33   return pkt->head.vers_diag & ((1 << 5) - 1);
34 }
35
36 void
37 bfd_pkt_set_diag_code (bfd_pkt_t * pkt, int value)
38 {
39   pkt->head.vers_diag =
40     (pkt->head.vers_diag & ~((1 << 5) - 1)) | (value & ((1 << 5) - 1));
41 }
42
43 u8
44 bfd_pkt_get_state (const bfd_pkt_t * pkt)
45 {
46   return pkt->head.sta_flags >> 6;
47 }
48
49 void
50 bfd_pkt_set_state (bfd_pkt_t * pkt, int value)
51 {
52   pkt->head.sta_flags = (value << 6) | (pkt->head.sta_flags & ((1 << 6) - 1));
53 }
54
55 u8
56 bfd_pkt_get_poll (const bfd_pkt_t * pkt)
57 {
58   return (pkt->head.sta_flags >> 5) & 1;
59 }
60
61 void
62 bfd_pkt_set_poll (bfd_pkt_t * pkt)
63 {
64   pkt->head.sta_flags |= 1 << 5;
65 }
66
67 u8
68 bfd_pkt_get_final (const bfd_pkt_t * pkt)
69 {
70   return (pkt->head.sta_flags >> 4) & 1;
71 }
72
73 void
74 bfd_pkt_set_final (bfd_pkt_t * pkt)
75 {
76   pkt->head.sta_flags |= 1 << 4;
77 }
78
79 u8
80 bfd_pkt_get_control_plane_independent (const bfd_pkt_t * pkt)
81 {
82   return (pkt->head.sta_flags >> 3) & 1;
83 }
84
85 void
86 bfd_pkt_set_control_plane_independent (bfd_pkt_t * pkt)
87 {
88   pkt->head.sta_flags |= 1 << 3;
89 }
90
91 u8
92 bfd_pkt_get_auth_present (const bfd_pkt_t * pkt)
93 {
94   return (pkt->head.sta_flags >> 2) & 1;
95 }
96
97 void
98 bfd_pkt_set_auth_present (bfd_pkt_t * pkt)
99 {
100   pkt->head.sta_flags |= 1 << 2;
101 }
102
103 u8
104 bfd_pkt_get_demand (const bfd_pkt_t * pkt)
105 {
106   return (pkt->head.sta_flags >> 1) & 1;
107 }
108
109 void
110 bfd_pkt_set_demand (bfd_pkt_t * pkt)
111 {
112   pkt->head.sta_flags |= 1 << 1;
113 }
114
115 u8
116 bfd_pkt_get_multipoint (const bfd_pkt_t * pkt)
117 {
118   return (pkt->head.sta_flags >> 0) & 1;
119 }
120
121 void
122 bfd_pkt_set_multipoint (bfd_pkt_t * pkt)
123 {
124   pkt->head.sta_flags |= 1 << 0;
125 }
126
127 u32
128 bfd_max_len_for_auth_type (bfd_auth_type_e auth_type)
129 {
130 #define F(t, l, n, s) \
131   if (auth_type == t) \
132     {                 \
133       return l;       \
134     }
135   foreach_bfd_auth_type (F);
136 #undef F
137   return 0;
138 }
139
140 const char *
141 bfd_auth_type_str (bfd_auth_type_e auth_type)
142 {
143 #define F(t, l, n, s) \
144   if (auth_type == t) \
145     {                 \
146       return s;       \
147     }
148   foreach_bfd_auth_type (F);
149 #undef F
150   return "UNKNOWN";
151 }
152
153 /*
154  * fd.io coding-style-patch-verification: ON
155  *
156  * Local Variables:
157  * eval: (c-set-style "gnu")
158  * End:
159  */