Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | // Copyright 2012 Google, Inc. All rights reserved. |
| 2 | // |
| 3 | // Use of this source code is governed by a BSD-style license |
| 4 | // that can be found in the LICENSE file in the root of the source |
| 5 | // tree. |
| 6 | |
| 7 | package layers |
| 8 | |
| 9 | import ( |
| 10 | "fmt" |
| 11 | "strconv" |
| 12 | |
| 13 | "github.com/google/gopacket" |
| 14 | ) |
| 15 | |
| 16 | // TCPPort is a port in a TCP layer. |
| 17 | type TCPPort uint16 |
| 18 | |
| 19 | // UDPPort is a port in a UDP layer. |
| 20 | type UDPPort uint16 |
| 21 | |
| 22 | // RUDPPort is a port in a RUDP layer. |
| 23 | type RUDPPort uint8 |
| 24 | |
| 25 | // SCTPPort is a port in a SCTP layer. |
| 26 | type SCTPPort uint16 |
| 27 | |
| 28 | // UDPLitePort is a port in a UDPLite layer. |
| 29 | type UDPLitePort uint16 |
| 30 | |
| 31 | // RUDPPortNames contains the string names for all RUDP ports. |
| 32 | var RUDPPortNames = map[RUDPPort]string{} |
| 33 | |
| 34 | // UDPLitePortNames contains the string names for all UDPLite ports. |
| 35 | var UDPLitePortNames = map[UDPLitePort]string{} |
| 36 | |
| 37 | // {TCP,UDP,SCTP}PortNames can be found in iana_ports.go |
| 38 | |
| 39 | // String returns the port as "number(name)" if there's a well-known port name, |
| 40 | // or just "number" if there isn't. Well-known names are stored in |
| 41 | // TCPPortNames. |
| 42 | func (a TCPPort) String() string { |
| 43 | if name, ok := TCPPortNames[a]; ok { |
| 44 | return fmt.Sprintf("%d(%s)", a, name) |
| 45 | } |
| 46 | return strconv.Itoa(int(a)) |
| 47 | } |
| 48 | |
| 49 | // LayerType returns a LayerType that would be able to decode the |
| 50 | // application payload. It uses some well-known ports such as 53 for |
| 51 | // DNS. |
| 52 | // |
| 53 | // Returns gopacket.LayerTypePayload for unknown/unsupported port numbers. |
| 54 | func (a TCPPort) LayerType() gopacket.LayerType { |
| 55 | if tcpPortLayerTypeOverride.has(uint16(a)) { |
| 56 | return tcpPortLayerType[a] |
| 57 | } |
| 58 | switch a { |
| 59 | case 53: |
| 60 | return LayerTypeDNS |
| 61 | case 443: // https |
| 62 | return LayerTypeTLS |
| 63 | case 502: // modbustcp |
| 64 | return LayerTypeModbusTCP |
| 65 | case 636: // ldaps |
| 66 | return LayerTypeTLS |
| 67 | case 989: // ftps-data |
| 68 | return LayerTypeTLS |
| 69 | case 990: // ftps |
| 70 | return LayerTypeTLS |
| 71 | case 992: // telnets |
| 72 | return LayerTypeTLS |
| 73 | case 993: // imaps |
| 74 | return LayerTypeTLS |
| 75 | case 994: // ircs |
| 76 | return LayerTypeTLS |
| 77 | case 995: // pop3s |
| 78 | return LayerTypeTLS |
| 79 | case 5061: // ips |
| 80 | return LayerTypeTLS |
| 81 | } |
| 82 | return gopacket.LayerTypePayload |
| 83 | } |
| 84 | |
| 85 | var tcpPortLayerTypeOverride bitfield |
| 86 | |
| 87 | var tcpPortLayerType = map[TCPPort]gopacket.LayerType{} |
| 88 | |
| 89 | // RegisterTCPPortLayerType creates a new mapping between a TCPPort |
| 90 | // and an underlaying LayerType. |
| 91 | func RegisterTCPPortLayerType(port TCPPort, layerType gopacket.LayerType) { |
| 92 | tcpPortLayerTypeOverride.set(uint16(port)) |
| 93 | tcpPortLayerType[port] = layerType |
| 94 | } |
| 95 | |
| 96 | // String returns the port as "number(name)" if there's a well-known port name, |
| 97 | // or just "number" if there isn't. Well-known names are stored in |
| 98 | // UDPPortNames. |
| 99 | func (a UDPPort) String() string { |
| 100 | if name, ok := UDPPortNames[a]; ok { |
| 101 | return fmt.Sprintf("%d(%s)", a, name) |
| 102 | } |
| 103 | return strconv.Itoa(int(a)) |
| 104 | } |
| 105 | |
| 106 | // LayerType returns a LayerType that would be able to decode the |
| 107 | // application payload. It uses some well-known ports such as 53 for |
| 108 | // DNS. |
| 109 | // |
| 110 | // Returns gopacket.LayerTypePayload for unknown/unsupported port numbers. |
| 111 | func (a UDPPort) LayerType() gopacket.LayerType { |
| 112 | if udpPortLayerTypeOverride.has(uint16(a)) { |
| 113 | return udpPortLayerType[a] |
| 114 | } |
| 115 | switch a { |
| 116 | case 53: |
| 117 | return LayerTypeDNS |
| 118 | case 67: |
| 119 | return LayerTypeDHCPv4 |
| 120 | case 68: |
| 121 | return LayerTypeDHCPv4 |
| 122 | case 123: |
| 123 | return LayerTypeNTP |
| 124 | case 546: |
| 125 | return LayerTypeDHCPv6 |
| 126 | case 547: |
| 127 | return LayerTypeDHCPv6 |
| 128 | case 623: |
| 129 | return LayerTypeRMCP |
| 130 | case 1812: |
| 131 | return LayerTypeRADIUS |
| 132 | case 2152: |
| 133 | return LayerTypeGTPv1U |
| 134 | case 3784: |
| 135 | return LayerTypeBFD |
| 136 | case 4789: |
| 137 | return LayerTypeVXLAN |
| 138 | case 5060: |
| 139 | return LayerTypeSIP |
| 140 | case 6081: |
| 141 | return LayerTypeGeneve |
| 142 | case 6343: |
| 143 | return LayerTypeSFlow |
| 144 | } |
| 145 | return gopacket.LayerTypePayload |
| 146 | } |
| 147 | |
| 148 | var udpPortLayerTypeOverride bitfield |
| 149 | |
| 150 | var udpPortLayerType = map[UDPPort]gopacket.LayerType{ |
| 151 | 53: LayerTypeDNS, |
| 152 | 123: LayerTypeNTP, |
| 153 | 4789: LayerTypeVXLAN, |
| 154 | 67: LayerTypeDHCPv4, |
| 155 | 68: LayerTypeDHCPv4, |
| 156 | 546: LayerTypeDHCPv6, |
| 157 | 547: LayerTypeDHCPv6, |
| 158 | 5060: LayerTypeSIP, |
| 159 | 6343: LayerTypeSFlow, |
| 160 | 6081: LayerTypeGeneve, |
| 161 | 3784: LayerTypeBFD, |
| 162 | 2152: LayerTypeGTPv1U, |
| 163 | 623: LayerTypeRMCP, |
| 164 | } |
| 165 | |
| 166 | // RegisterUDPPortLayerType creates a new mapping between a UDPPort |
| 167 | // and an underlaying LayerType. |
| 168 | func RegisterUDPPortLayerType(port UDPPort, layerType gopacket.LayerType) { |
| 169 | udpPortLayerTypeOverride.set(uint16(port)) |
| 170 | udpPortLayerType[port] = layerType |
| 171 | } |
| 172 | |
| 173 | // String returns the port as "number(name)" if there's a well-known port name, |
| 174 | // or just "number" if there isn't. Well-known names are stored in |
| 175 | // RUDPPortNames. |
| 176 | func (a RUDPPort) String() string { |
| 177 | if name, ok := RUDPPortNames[a]; ok { |
| 178 | return fmt.Sprintf("%d(%s)", a, name) |
| 179 | } |
| 180 | return strconv.Itoa(int(a)) |
| 181 | } |
| 182 | |
| 183 | // String returns the port as "number(name)" if there's a well-known port name, |
| 184 | // or just "number" if there isn't. Well-known names are stored in |
| 185 | // SCTPPortNames. |
| 186 | func (a SCTPPort) String() string { |
| 187 | if name, ok := SCTPPortNames[a]; ok { |
| 188 | return fmt.Sprintf("%d(%s)", a, name) |
| 189 | } |
| 190 | return strconv.Itoa(int(a)) |
| 191 | } |
| 192 | |
| 193 | // String returns the port as "number(name)" if there's a well-known port name, |
| 194 | // or just "number" if there isn't. Well-known names are stored in |
| 195 | // UDPLitePortNames. |
| 196 | func (a UDPLitePort) String() string { |
| 197 | if name, ok := UDPLitePortNames[a]; ok { |
| 198 | return fmt.Sprintf("%d(%s)", a, name) |
| 199 | } |
| 200 | return strconv.Itoa(int(a)) |
| 201 | } |