| 1 |
;;; |
| 2 |
;;; rfc.icmp - Internet Control Message Protocol |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2007-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
|
| 35 |
;; EXPERIMENTAL |
| 36 |
|
| 37 |
;; RFC 792 Internet Control Message Protocol |
| 38 |
;; RFC 1256 ICMP Router Discovery Messages |
| 39 |
;; RFC 1393 Traceroute Using an IP Option |
| 40 |
;; RFC 1788 ICMP Domain Name Messages |
| 41 |
;; RFC 1812 Requirements for IP Version 4 Routers |
| 42 |
;; RFC 2002 IP Mobility Supprot |
| 43 |
;; RFC 2463 ICMPv6 |
| 44 |
;; RFC 2521 ICMP Security Failures Messages |
| 45 |
;; Also useful: |
| 46 |
;; http://www.iana.org/assignments/icmp-parameters |
| 47 |
|
| 48 |
(define-module rfc.icmp |
| 49 |
(use gauche.uvector) |
| 50 |
(use gauche.net) |
| 51 |
(use binary.io) |
| 52 |
(export icmp4-message-type->string |
| 53 |
ICMP4_ECHOREPLY ICMP4_DEST_UNREACH ICMP4_SOURCE_QUENCH |
| 54 |
ICMP4_REDIRECT ICMP4_ECHO ICMP4_ROUTER_ADVERT ICMP4_ROUTER_SOLICIT |
| 55 |
ICMP4_TIME_EXCEEDED ICMP4_PARAMETERPROB ICMP4_TIMESTAMP |
| 56 |
ICMP4_TIMESTAMPREPLY ICMP4_INFO_REQUEST ICMP4_INFO_REPLY |
| 57 |
ICMP4_ADDRESS ICMP4_ADDRESSREPLY ICMP4_TRACEROUTE |
| 58 |
ICMP4_DGRAMCONVERROR ICMP4_DOMAIN_REQUEST ICMP4_DOMAIN_REPLY |
| 59 |
ICMP4_SECURITYFAILURE |
| 60 |
|
| 61 |
icmp4-unreach-code->string |
| 62 |
ICMP4_NET_UNREACH ICMP4_HOST_UNREACH ICMP4_PROT_UNREACH |
| 63 |
ICMP4_PORT_UNREACH ICMP4_FRAG_NEEDED ICMP4_SR_FAILED |
| 64 |
ICMP4_NET_UNKNOWN ICMP4_HOST_UNKNOWN ICMP4_HOST_ISOLATED |
| 65 |
ICMP4_NET_ANO ICMP4_HOST_ANO ICMP4_NET_UNR_TOS ICMP4_HOST_UNR_TOS |
| 66 |
ICMP4_PKT_FILTERED ICMP4_PREC_VIOLATION ICMP4_PREC_CUTOFF |
| 67 |
|
| 68 |
icmp4-redirect-code->string |
| 69 |
ICMP4_REDIR_NET ICMP4_REDIR_HOST ICMP4_REDIR_NETTOS |
| 70 |
ICMP4_REDIR_HOSTTOS |
| 71 |
|
| 72 |
icmp4-router-code->string |
| 73 |
ICMP4_ROUTER_NORMAL ICMP4_ROUTER_NOCOMMON |
| 74 |
|
| 75 |
icmp4-exceeded-code->string |
| 76 |
ICMP4_EXC_TTL ICMP4_EXC_FRAGTIME |
| 77 |
|
| 78 |
icmp4-parameter-code->string |
| 79 |
ICMP4_PARAM_PTR ICMP4_PARAM_MISSING ICMP4_PARAM_BADLENGTH |
| 80 |
|
| 81 |
icmp4-security-code->string |
| 82 |
ICMP4_SEC_BADSPI ICMP4_SEC_AUTHFAILED ICMP4_SEC_DECOMPFAILED |
| 83 |
ICMP4_SEC_DECRYPTFAILED ICMP4_SEC_NEEDAUTHENTICATION |
| 84 |
ICMP4_SEC_NEEDAUTHORIZATION |
| 85 |
|
| 86 |
icmp6-message-type->string |
| 87 |
ICMP6_DEST_UNREACH ICMP6_PACKET_TOO_BIG ICMP6_TIME_EXCEEDED |
| 88 |
ICMP6_PARAMETERPROB ICMP6_ECHO ICMP6_ECHOREPLY |
| 89 |
|
| 90 |
icmp6-unreach-code->string |
| 91 |
ICMP6_UNREACH_NOROUTE ICMP6_UNREACH_ADMIN ICMP6_UNREACH_BEYONDSCOPE |
| 92 |
ICMP6_UNREACH_ADDR ICMP6_UNREACH_NOPORT |
| 93 |
|
| 94 |
icmp6-exceeded-code->string |
| 95 |
ICMP6_EXC_TRANSIT ICMP6_EXC_REASSEMBLY |
| 96 |
|
| 97 |
icmp6-parameter-code->string |
| 98 |
ICMP6_PARAM_HEADER ICMP6_PARAM_NEXTHEADER ICMP6_PARAM_OPTION |
| 99 |
|
| 100 |
icmp-fill-header! |
| 101 |
icmp4-fill-checksum! icmp4-fill-echo! |
| 102 |
icmp6-fill-echo! |
| 103 |
|
| 104 |
icmp-packet-type icmp-packet-code |
| 105 |
icmp-echo-ident icmp-echo-sequence |
| 106 |
icmp4-describe-packet icmp6-describe-packet |
| 107 |
)) |
| 108 |
(select-module rfc.icmp) |
| 109 |
|
| 110 |
;;============================================================ |
| 111 |
;; Constants |
| 112 |
;; |
| 113 |
;; NB: it seems that these symbols in C slightly differ among the |
| 114 |
;; underlying ICMP library, e.g. between GNU/Linux and BSD. |
| 115 |
;; I follow the GNU convention, for it is a bit more descriptive. |
| 116 |
|
| 117 |
(define-syntax define-named-code |
| 118 |
(syntax-rules () |
| 119 |
((_ name ->string (sym val expl) ...) |
| 120 |
(begin |
| 121 |
(define-constant sym val) ... |
| 122 |
(define (->string c) |
| 123 |
(case c |
| 124 |
((val) expl) ... |
| 125 |
(else (format "Unknown code ~s for ~a" c name)))))))) |
| 126 |
|
| 127 |
;;------------------------------------------------------------- |
| 128 |
;; ICMPv4 |
| 129 |
;; |
| 130 |
|
| 131 |
;; ICMP message types. |
| 132 |
(define-named-code "ICMP message types" |
| 133 |
icmp4-message-type->string |
| 134 |
(ICMP4_ECHOREPLY 0 "Echo Reply") |
| 135 |
(ICMP4_DEST_UNREACH 3 "Destination Unreachable") |
| 136 |
(ICMP4_SOURCE_QUENCH 4 "Source Quench ") |
| 137 |
(ICMP4_REDIRECT 5 "Redirect (change route)") |
| 138 |
(ICMP4_ECHO 8 "Echo Request") |
| 139 |
(ICMP4_ROUTER_ADVERT 9 "Router Advertisement") |
| 140 |
(ICMP4_ROUTER_SOLICIT 10 "Router solicitation") |
| 141 |
(ICMP4_TIME_EXCEEDED 11 "Time Exceeded") |
| 142 |
(ICMP4_PARAMETERPROB 12 "Parameter Problem") |
| 143 |
(ICMP4_TIMESTAMP 13 "Timestamp Request") |
| 144 |
(ICMP4_TIMESTAMPREPLY 14 "Timestamp Reply") |
| 145 |
(ICMP4_INFO_REQUEST 15 "Information Request") |
| 146 |
(ICMP4_INFO_REPLY 16 "Information Reply") |
| 147 |
(ICMP4_ADDRESS 17 "Address Mask Request") |
| 148 |
(ICMP4_ADDRESSREPLY 18 "Address Mask Reply") |
| 149 |
(ICMP4_TRACEROUTE 30 "Traceroute") |
| 150 |
(ICMP4_DGRAMCONVERROR 31 "Datagram Conversion Error") |
| 151 |
(ICMP4_DOMAIN_REQUEST 37 "Domain Name Request") |
| 152 |
(ICMP4_DOMAIN_REPLY 38 "Domain Name Reply") |
| 153 |
(ICMP4_SECURITYFAILURE 40 "Security Failures") |
| 154 |
) |
| 155 |
|
| 156 |
;; Codes |
| 157 |
(define-named-code "ICMP UNREACH codes" |
| 158 |
icmp4-unreach-code->string |
| 159 |
(ICMP4_NET_UNREACH 0 "Network Unreachable") |
| 160 |
(ICMP4_HOST_UNREACH 1 "Host Unreachable") |
| 161 |
(ICMP4_PROT_UNREACH 2 "Protocol Unreachable") |
| 162 |
(ICMP4_PORT_UNREACH 3 "Port Unreachable") |
| 163 |
(ICMP4_FRAG_NEEDED 4 "Fragmentation Needed/DF set") |
| 164 |
(ICMP4_SR_FAILED 5 "Source Route failed") |
| 165 |
(ICMP4_NET_UNKNOWN 6 "Unknown Net") |
| 166 |
(ICMP4_HOST_UNKNOWN 7 "Unknown Host") |
| 167 |
(ICMP4_HOST_ISOLATED 8 "Source Host Isolated") |
| 168 |
(ICMP4_NET_ANO 9 "Dest Network Prohibited") |
| 169 |
(ICMP4_HOST_ANO 10 "Dest Host Prohibited") |
| 170 |
(ICMP4_NET_UNR_TOS 11 "Dest Netowork Unreachable for TOS") |
| 171 |
(ICMP4_HOST_UNR_TOS 12 "Dest Host Unreachable for TOS") |
| 172 |
(ICMP4_PKT_FILTERED 13 "Packet Filtered") |
| 173 |
(ICMP4_PREC_VIOLATION 14 "Precedence Violation") |
| 174 |
(ICMP4_PREC_CUTOFF 15 "Precedence Cut Off") |
| 175 |
) |
| 176 |
|
| 177 |
(define-named-code "ICMP REDIRECT codes" |
| 178 |
icmp4-redirect-code->string |
| 179 |
(ICMP4_REDIR_NET 0 "Redirect Net") |
| 180 |
(ICMP4_REDIR_HOST 1 "Redirect Host") |
| 181 |
(ICMP4_REDIR_NETTOS 2 "Redirect Net for TOS") |
| 182 |
(ICMP4_REDIR_HOSTTOS 3 "Redirect Host for TOS") |
| 183 |
) |
| 184 |
|
| 185 |
(define-named-code "ICMP ROUTER ADVERTISE codes" |
| 186 |
icmp4-router-code->string |
| 187 |
(ICMP4_ROUTER_NORMAL 0 "Normal Router Advertisement") |
| 188 |
(ICMP4_ROUTER_NOCOMMON 16 "Does not route common traffic") |
| 189 |
) |
| 190 |
|
| 191 |
(define-named-code "ICMP TIME EXCEEDED codes" |
| 192 |
icmp4-exceeded-code->string |
| 193 |
(ICMP4_EXC_TTL 0 "TTL count exceeded") |
| 194 |
(ICMP4_EXC_FRAGTIME 1 "Fragment Reass time exceeded") |
| 195 |
) |
| 196 |
|
| 197 |
(define-named-code "ICMP PARAMETER PROBLEM codes" |
| 198 |
icmp4-parameter-code->string |
| 199 |
(ICMP4_PARAM_PTR 0 "Pointer indicates the error") |
| 200 |
(ICMP4_PARAM_MISSING 1 "Missing Required Option") |
| 201 |
(ICMP4_PARAM_BADLENGTH 2 "Bad Length") |
| 202 |
) |
| 203 |
|
| 204 |
(define-named-code "ICMP SECURITY FAILURE codes" |
| 205 |
icmp4-security-code->string |
| 206 |
(ICMP4_SEC_BADSPI 0 "Bad SPI") |
| 207 |
(ICMP4_SEC_AUTHFAILED 1 "Authentication Failed") |
| 208 |
(ICMP4_SEC_DECOMPFAILED 2 "Decompression Failed") |
| 209 |
(ICMP4_SEC_DECRYPTFAILED 3 "Decryption Failed") |
| 210 |
(ICMP4_SEC_NEEDAUTHENTICATION 4 "Need Authentication") |
| 211 |
(ICMP4_SEC_NEEDAUTHORIZATION 5 "Need Authorization") |
| 212 |
) |
| 213 |
|
| 214 |
;;------------------------------------------------------------- |
| 215 |
;; ICMPv6 |
| 216 |
;; |
| 217 |
(define-named-code "ICMP6 message types" |
| 218 |
icmp6-message-type->string |
| 219 |
(ICMP6_DEST_UNREACH 1 "Destination unreachable") |
| 220 |
(ICMP6_PACKET_TOO_BIG 2 "Packet Too Big") |
| 221 |
(ICMP6_TIME_EXCEEDED 3 "Time Exceeded") |
| 222 |
(ICMP6_PARAMETERPROB 4 "Parameter problem") |
| 223 |
|
| 224 |
(ICMP6_ECHO 128 "Echo request") |
| 225 |
(ICMP6_ECHOREPLY 129 "Echo reply") |
| 226 |
) |
| 227 |
|
| 228 |
(define-named-code "ICMP6 UNREACH codes" |
| 229 |
icmp6-unreach-code->string |
| 230 |
(ICMP6_UNREACH_NOROUTE 0 "No route to destination") |
| 231 |
(ICMP6_UNREACH_ADMIN 1 "Communication administratively prohibited") |
| 232 |
(ICMP6_UNREACH_BEYONDSCOPE 2 "Beyond scope of source address") |
| 233 |
(ICMP6_UNREACH_ADDR 3 "Address unreachable") |
| 234 |
(ICMP6_UNREACH_NOPORT 4 "Bad port") |
| 235 |
) |
| 236 |
|
| 237 |
(define-named-code "ICMP6 TIME EXCEEDED codes" |
| 238 |
icmp6-exceeded-code->string |
| 239 |
(ICMP6_EXC_TRANSIT 0 "Hop limit == 0 in transit") |
| 240 |
(ICMP6_EXC_REASSEMBLY 1 "Reassembly time out") |
| 241 |
) |
| 242 |
|
| 243 |
(define-named-code "ICMP6 PARAMETER PROBLEM codes" |
| 244 |
icmp6-parameter-code->string |
| 245 |
(ICMP6_PARAM_HEADER 0 "Erroneous header field") |
| 246 |
(ICMP6_PARAM_NEXTHEADER 1 "Unrecognized next header") |
| 247 |
(ICMP6_PARAM_OPTION 2 "Unrecognized IPv6 option") |
| 248 |
) |
| 249 |
|
| 250 |
;;============================================================ |
| 251 |
;; Packet construction |
| 252 |
;; |
| 253 |
|
| 254 |
;; The low-level 'fill' routines operates on pre-allocated u8vector |
| 255 |
;; as buffer. They don't allocate, so that they can be used |
| 256 |
;; in inner loops. |
| 257 |
|
| 258 |
;; Fill ICMP/ICMP6 packet header (except checksum) |
| 259 |
(define-inline (icmp-fill-header! buf type code) |
| 260 |
(put-u8! buf 0 type) |
| 261 |
(put-u8! buf 1 code) |
| 262 |
(put-u16be! buf 2 0)) |
| 263 |
|
| 264 |
(define (icmp4-fill-checksum! buf size) |
| 265 |
(put-u16be! buf 2 (inet-checksum buf size))) |
| 266 |
|
| 267 |
(define (icmp4-fill-echo! buf ident seq data) |
| 268 |
(icmp-fill-header! buf ICMP4_ECHO 0) |
| 269 |
(put-u16be! buf 4 ident) |
| 270 |
(put-u16be! buf 6 seq) |
| 271 |
(u8vector-copy! buf 8 data) |
| 272 |
(icmp4-fill-checksum! buf (+ (u8vector-length data) 8))) |
| 273 |
|
| 274 |
;; We don't need to fill out checksum fields, for the kernel calculates it. |
| 275 |
(define (icmp6-fill-echo! buf ident seq data) |
| 276 |
(icmp-fill-header! buf ICMP6_ECHO 0) |
| 277 |
(put-u16be! buf 4 ident) |
| 278 |
(put-u16be! buf 6 seq) |
| 279 |
(u8vector-copy! buf 8 data)) |
| 280 |
|
| 281 |
;;============================================================ |
| 282 |
;; Packet decomposition |
| 283 |
;; |
| 284 |
|
| 285 |
;; common to v4/v6 |
| 286 |
(define (icmp-packet-type buf offset) |
| 287 |
(get-u8 buf (+ 0 offset))) |
| 288 |
|
| 289 |
;; common to v4/v6 |
| 290 |
(define (icmp-packet-code buf offset) |
| 291 |
(get-u8 buf (+ 1 offset))) |
| 292 |
|
| 293 |
;; common to v4/v6 |
| 294 |
(define (icmp-echo-ident buf offset) |
| 295 |
(get-u16be buf (+ 4 offset))) |
| 296 |
|
| 297 |
;; common to v4/v6 |
| 298 |
(define (icmp-echo-sequence buf offset) |
| 299 |
(get-u16be buf (+ 6 offset))) |
| 300 |
|
| 301 |
(define (icmp4-describe-packet buf offset) |
| 302 |
(let ((type (icmp-packet-type buf offset)) |
| 303 |
(code (icmp-packet-code buf offset))) |
| 304 |
(format #t "ICMP packet type=~a(~a)\n" |
| 305 |
type (icmp4-message-type->string type)) |
| 306 |
(cond |
| 307 |
((memv type `(,ICMP4_ECHOREPLY ,ICMP4_ECHO)) |
| 308 |
(format #t " ident=~2,'0x seq=~2,'0x\n" |
| 309 |
(icmp-echo-ident buf offset) (icmp-echo-sequence buf offset))) |
| 310 |
((assv type `((,ICMP4_DEST_UNREACH . ,icmp4-unreach-code->string) |
| 311 |
(,ICMP4_REDIRECT . ,icmp4-redirect-code->string) |
| 312 |
(,ICMP4_ROUTER_ADVERT . ,icmp4-router-code->string) |
| 313 |
(,ICMP4_TIME_EXCEEDED . ,icmp4-exceeded-code->string) |
| 314 |
(,ICMP4_PARAMETERPROB . ,icmp4-parameter-code->string) |
| 315 |
(,ICMP4_SECURITYFAILURE . ,icmp4-security-code->string))) |
| 316 |
=> (lambda (p) |
| 317 |
(format #t " code=~a(~a)\n" code ((cdr p) code)))) |
| 318 |
))) |
| 319 |
|
| 320 |
(define (icmp6-describe-packet buf offset) |
| 321 |
(let ((type (icmp-packet-type buf offset)) |
| 322 |
(code (icmp-packet-code buf offset))) |
| 323 |
(format #t "ICMPv6 packet type=~a(~a)\n" |
| 324 |
type (icmp6-message-type->string type)) |
| 325 |
(cond |
| 326 |
((= type ICMP6_ECHOREPLY) |
| 327 |
(format #t " ident=~2,'0x seq=~2,'0x\n" |
| 328 |
(icmp-echo-ident buf offset) (icmp-echo-sequence buf offset))) |
| 329 |
((assv type `((,ICMP6_DEST_UNREACH . ,icmp6-unreach-code->string) |
| 330 |
(,ICMP6_TIME_EXCEEDED . ,icmp6-exceeded-code->string) |
| 331 |
(,ICMP6_PARAMETERPROB . ,icmp6-parameter-code->string))) |
| 332 |
=> (lambda (p) |
| 333 |
(format #t " code=~a(~a)\n" code ((cdr p) code)))) |
| 334 |
))) |