github.com/osrg/gobgp/v3@v3.30.0/pkg/packet/bgp/constant.go (about) 1 // Copyright (C) 2014 Nippon Telegraph and Telephone Corporation. 2 // 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 12 // implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package bgp 17 18 import ( 19 "strconv" 20 "strings" 21 ) 22 23 const AS_TRANS = 23456 24 25 const BGP_PORT = 179 26 27 //go:generate stringer -type=FSMState 28 type FSMState int 29 30 const ( 31 BGP_FSM_IDLE FSMState = iota 32 BGP_FSM_CONNECT 33 BGP_FSM_ACTIVE 34 BGP_FSM_OPENSENT 35 BGP_FSM_OPENCONFIRM 36 BGP_FSM_ESTABLISHED 37 ) 38 39 // partially taken from http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml 40 type Protocol int 41 42 const ( 43 Unknown Protocol = iota 44 ICMP = 0x01 45 IGMP = 0x02 46 TCP = 0x06 47 EGP = 0x08 48 IGP = 0x09 49 UDP = 0x11 50 RSVP = 0x2e 51 GRE = 0x2f 52 OSPF = 0x59 53 IPIP = 0x5e 54 PIM = 0x67 55 SCTP = 0x84 56 ) 57 58 var ProtocolNameMap = map[Protocol]string{ 59 Unknown: "unknown", 60 ICMP: "icmp", 61 IGMP: "igmp", 62 TCP: "tcp", 63 EGP: "egp", 64 IGP: "igp", 65 UDP: "udp", 66 RSVP: "rsvp", 67 GRE: "gre", 68 OSPF: "ospf", 69 IPIP: "ipip", 70 PIM: "pim", 71 SCTP: "sctp", 72 } 73 74 func (p Protocol) String() string { 75 name, ok := ProtocolNameMap[p] 76 if !ok { 77 return strconv.Itoa(int(p)) 78 } 79 return name 80 } 81 82 type TCPFlag int 83 84 const ( 85 _ TCPFlag = iota 86 TCP_FLAG_FIN = 0x01 87 TCP_FLAG_SYN = 0x02 88 TCP_FLAG_RST = 0x04 89 TCP_FLAG_PUSH = 0x08 90 TCP_FLAG_ACK = 0x10 91 TCP_FLAG_URGENT = 0x20 92 TCP_FLAG_ECE = 0x40 93 TCP_FLAG_CWR = 0x80 94 ) 95 96 var TCPFlagNameMap = map[TCPFlag]string{ 97 TCP_FLAG_FIN: "F", 98 TCP_FLAG_SYN: "S", 99 TCP_FLAG_RST: "R", 100 TCP_FLAG_PUSH: "P", 101 TCP_FLAG_ACK: "A", 102 TCP_FLAG_URGENT: "U", 103 TCP_FLAG_CWR: "C", 104 TCP_FLAG_ECE: "E", 105 } 106 107 // Prepares a sorted list of flags because map iterations does not happen 108 // in a consistent order in Golang. 109 var TCPSortedFlags = []TCPFlag{ 110 TCP_FLAG_FIN, 111 TCP_FLAG_SYN, 112 TCP_FLAG_RST, 113 TCP_FLAG_PUSH, 114 TCP_FLAG_ACK, 115 TCP_FLAG_URGENT, 116 TCP_FLAG_ECE, 117 TCP_FLAG_CWR, 118 } 119 120 func (f TCPFlag) String() string { 121 flags := make([]string, 0, len(TCPSortedFlags)) 122 for _, v := range TCPSortedFlags { 123 if f&v > 0 { 124 flags = append(flags, TCPFlagNameMap[v]) 125 } 126 } 127 return strings.Join(flags, "") 128 } 129 130 type BitmaskFlagOp uint8 131 132 const ( 133 BITMASK_FLAG_OP_OR BitmaskFlagOp = iota 134 BITMASK_FLAG_OP_MATCH = 0x01 135 BITMASK_FLAG_OP_NOT = 0x02 136 BITMASK_FLAG_OP_NOT_MATCH = 0x03 137 BITMASK_FLAG_OP_AND = 0x40 138 BITMASK_FLAG_OP_END = 0x80 139 ) 140 141 var BitmaskFlagOpNameMap = map[BitmaskFlagOp]string{ 142 BITMASK_FLAG_OP_OR: " ", 143 BITMASK_FLAG_OP_AND: "&", 144 BITMASK_FLAG_OP_END: "E", 145 BITMASK_FLAG_OP_NOT: "!", 146 BITMASK_FLAG_OP_MATCH: "=", 147 } 148 149 // Note: Meaning of "" is different from that of the numeric operator because 150 // RFC5575 says if the Match bit in the bitmask operand is set, it should be 151 // "strictly" matching against the given value. 152 var BitmaskFlagOpValueMap = map[string]BitmaskFlagOp{ 153 " ": BITMASK_FLAG_OP_OR, 154 "": BITMASK_FLAG_OP_OR, 155 "==": BITMASK_FLAG_OP_MATCH, 156 "=": BITMASK_FLAG_OP_MATCH, 157 "!": BITMASK_FLAG_OP_NOT, 158 "!=": BITMASK_FLAG_OP_NOT_MATCH, 159 "=!": BITMASK_FLAG_OP_NOT_MATCH, // For the backward compatibility 160 "&": BITMASK_FLAG_OP_AND, 161 "E": BITMASK_FLAG_OP_END, 162 } 163 164 func (f BitmaskFlagOp) String() string { 165 ops := make([]string, 0, 3) 166 if f&BITMASK_FLAG_OP_AND > 0 { 167 ops = append(ops, BitmaskFlagOpNameMap[BITMASK_FLAG_OP_AND]) 168 } else { 169 ops = append(ops, BitmaskFlagOpNameMap[BITMASK_FLAG_OP_OR]) 170 } 171 if f&BITMASK_FLAG_OP_NOT > 0 { 172 ops = append(ops, BitmaskFlagOpNameMap[BITMASK_FLAG_OP_NOT]) 173 } 174 if f&BITMASK_FLAG_OP_MATCH > 0 { 175 ops = append(ops, BitmaskFlagOpNameMap[BITMASK_FLAG_OP_MATCH]) 176 } 177 return strings.Join(ops, "") 178 } 179 180 type FragmentFlag int 181 182 const ( 183 FRAG_FLAG_NOT FragmentFlag = iota 184 FRAG_FLAG_DONT = 0x01 185 FRAG_FLAG_IS = 0x02 186 FRAG_FLAG_FIRST = 0x04 187 FRAG_FLAG_LAST = 0x08 188 ) 189 190 var FragmentFlagNameMap = map[FragmentFlag]string{ 191 FRAG_FLAG_NOT: "not-a-fragment", 192 FRAG_FLAG_DONT: "dont-fragment", 193 FRAG_FLAG_IS: "is-fragment", 194 FRAG_FLAG_FIRST: "first-fragment", 195 FRAG_FLAG_LAST: "last-fragment", 196 } 197 198 // Prepares a sorted list of flags because map iterations does not happen 199 // in a consistent order in Golang. 200 var FragmentSortedFlags = []FragmentFlag{ 201 FRAG_FLAG_NOT, 202 FRAG_FLAG_DONT, 203 FRAG_FLAG_IS, 204 FRAG_FLAG_FIRST, 205 FRAG_FLAG_LAST, 206 } 207 208 func (f FragmentFlag) String() string { 209 flags := make([]string, 0, len(FragmentSortedFlags)) 210 for _, v := range FragmentSortedFlags { 211 if f&v > 0 { 212 flags = append(flags, FragmentFlagNameMap[v]) 213 } 214 } 215 // Note: If multiple bits are set, joins them with "+". 216 return strings.Join(flags, "+") 217 } 218 219 type DECNumOp uint8 220 221 const ( 222 DEC_NUM_OP_TRUE DECNumOp = iota // true always with END bit set 223 DEC_NUM_OP_EQ = 0x01 224 DEC_NUM_OP_GT = 0x02 225 DEC_NUM_OP_GT_EQ = 0x03 226 DEC_NUM_OP_LT = 0x04 227 DEC_NUM_OP_LT_EQ = 0x05 228 DEC_NUM_OP_NOT_EQ = 0x06 229 DEC_NUM_OP_FALSE = 0x07 // false always with END bit set 230 DEC_NUM_OP_OR = 0x00 231 DEC_NUM_OP_AND = 0x40 232 DEC_NUM_OP_END = 0x80 233 ) 234 235 var DECNumOpNameMap = map[DECNumOp]string{ 236 DEC_NUM_OP_TRUE: "true", 237 DEC_NUM_OP_EQ: "==", 238 DEC_NUM_OP_GT: ">", 239 DEC_NUM_OP_GT_EQ: ">=", 240 DEC_NUM_OP_LT: "<", 241 DEC_NUM_OP_LT_EQ: "<=", 242 DEC_NUM_OP_NOT_EQ: "!=", 243 DEC_NUM_OP_FALSE: "false", 244 //DEC_NUM_OP_OR: " ", // duplicate with DEC_NUM_OP_TRUE 245 DEC_NUM_OP_AND: "&", 246 DEC_NUM_OP_END: "E", 247 } 248 249 var DECNumOpValueMap = map[string]DECNumOp{ 250 "true": DEC_NUM_OP_TRUE, 251 "": DEC_NUM_OP_EQ, 252 "==": DEC_NUM_OP_EQ, 253 "=": DEC_NUM_OP_EQ, 254 ">": DEC_NUM_OP_GT, 255 ">=": DEC_NUM_OP_GT_EQ, 256 "<": DEC_NUM_OP_LT, 257 "<=": DEC_NUM_OP_LT_EQ, 258 "!=": DEC_NUM_OP_NOT_EQ, 259 "=!": DEC_NUM_OP_NOT_EQ, 260 "!": DEC_NUM_OP_NOT_EQ, 261 "false": DEC_NUM_OP_FALSE, 262 " ": DEC_NUM_OP_OR, 263 "&": DEC_NUM_OP_AND, 264 "E": DEC_NUM_OP_END, 265 } 266 267 func (f DECNumOp) String() string { 268 ops := make([]string, 0) 269 logicFlag := DECNumOp(f & 0xc0) // higher 2 bits 270 if logicFlag&DEC_NUM_OP_AND > 0 { 271 ops = append(ops, DECNumOpNameMap[DEC_NUM_OP_AND]) 272 } else { 273 ops = append(ops, " ") // DEC_NUM_OP_OR 274 } 275 // Omits DEC_NUM_OP_END 276 cmpFlag := DECNumOp(f & 0x7) // lower 3 bits 277 for v, s := range DECNumOpNameMap { 278 if cmpFlag == v { 279 ops = append(ops, s) 280 break 281 } 282 } 283 return strings.Join(ops, "") 284 } 285 286 // Potentially taken from https://www.iana.org/assignments/ieee-802-numbers/ieee-802-numbers.xhtml 287 type EthernetType int 288 289 const ( 290 IPv4 EthernetType = 0x0800 291 ARP EthernetType = 0x0806 292 RARP EthernetType = 0x8035 293 VMTP EthernetType = 0x805B 294 APPLE_TALK EthernetType = 0x809B 295 AARP EthernetType = 0x80F3 296 IPX EthernetType = 0x8137 297 SNMP EthernetType = 0x814C 298 NET_BIOS EthernetType = 0x8191 299 XTP EthernetType = 0x817D 300 IPv6 EthernetType = 0x86DD 301 PPPoE_DISCOVERY EthernetType = 0x8863 302 PPPoE_SESSION EthernetType = 0x8864 303 LOOPBACK EthernetType = 0x9000 304 ) 305 306 var EthernetTypeNameMap = map[EthernetType]string{ 307 IPv4: "ipv4", 308 ARP: "arp", 309 RARP: "rarp", 310 VMTP: "vmtp", 311 APPLE_TALK: "apple-talk", 312 AARP: "aarp", 313 IPX: "ipx", 314 SNMP: "snmp", 315 NET_BIOS: "net-bios", 316 XTP: "xtp", 317 IPv6: "ipv6", 318 PPPoE_DISCOVERY: "pppoe-discovery", 319 PPPoE_SESSION: "pppoe-session", 320 LOOPBACK: "loopback", 321 } 322 323 func (t EthernetType) String() string { 324 if name, ok := EthernetTypeNameMap[t]; ok { 325 return name 326 } 327 return strconv.Itoa(int(t)) 328 }