| 1 |
;;; |
| 2 |
;;; rfc.ip - Internet 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 |
;; EXPERIMENTAL |
| 35 |
|
| 36 |
(define-module rfc.ip |
| 37 |
(use gauche.uvector) |
| 38 |
(use binary.io) |
| 39 |
(use util.match) |
| 40 |
(use scheme.list) |
| 41 |
(use srfi.13) |
| 42 |
(export ip-version ip-header-length ip-protocol |
| 43 |
ip-source-address ip-destination-address |
| 44 |
ipv4-global-address? |
| 45 |
)) |
| 46 |
(select-module rfc.ip) |
| 47 |
|
| 48 |
;;============================================================ |
| 49 |
;; Packet accessors |
| 50 |
;; |
| 51 |
|
| 52 |
(define (ip-version packet offset) |
| 53 |
(ash (get-u8 packet offset) -4)) |
| 54 |
|
| 55 |
;; returns the final protocol and offset |
| 56 |
(define (%ipv6-skip-header-extensions packet offset) |
| 57 |
(let loop ((nexthdr (get-u8 packet (+ 6 offset))) |
| 58 |
(off (+ 40 offset))) |
| 59 |
(if (memv nexthdr '(0 ; hop-by-hop options |
| 60 |
43 ; routing options |
| 61 |
60 ; destination options |
| 62 |
)) |
| 63 |
(loop (get-u8 packet off) |
| 64 |
(+ off (* (+ (get-u8 packet (+ off 1)) 1) 8))) |
| 65 |
(values nexthdr (- off offset))))) |
| 66 |
|
| 67 |
(define-macro (if-v4 packet offset v4 v6) |
| 68 |
`(case (ip-version ,packet ,offset) |
| 69 |
((4) ,v4) |
| 70 |
((6) ,v6) |
| 71 |
(else |
| 72 |
(errorf "unknown IP protocol version ~a in packet ~s" |
| 73 |
(ip-version ,packet ,offset) ,packet)))) |
| 74 |
|
| 75 |
(define (ip-header-length packet offset) |
| 76 |
(if-v4 packet offset |
| 77 |
(* (logand (get-u8 packet offset) #x0f) 4) |
| 78 |
(values-ref (%ipv6-skip-header-extensions packet offset) 1))) |
| 79 |
|
| 80 |
(define (ip-protocol packet offset) |
| 81 |
(if-v4 packet offset |
| 82 |
(get-u8 packet (+ 9 offset)) |
| 83 |
(values-ref (%ipv6-skip-header-extensions packet offset) 0))) |
| 84 |
|
| 85 |
;; These may allocate. Should we have non-allocation version? |
| 86 |
(define (ip-source-address packet offset) |
| 87 |
(if-v4 packet offset |
| 88 |
(get-u32be packet (+ 12 offset)) |
| 89 |
(+ (ash (get-u64be packet (+ 8 offset)) 64) |
| 90 |
(get-u64be packet (+ 16 offset))))) |
| 91 |
|
| 92 |
(define (ip-destination-address packet offset) |
| 93 |
(if-v4 packet offset |
| 94 |
(get-u32be packet (+ 16 offset)) |
| 95 |
(+ (ash (get-u64be packet (+ 24 offset)) 64) |
| 96 |
(get-u64be packet (+ 32 offset))))) |
| 97 |
|
| 98 |
;;============================================================ |
| 99 |
;; Adderss utility |
| 100 |
;; |
| 101 |
|
| 102 |
;; addr :: Integer |
| 103 |
;; http://www.iana.org/assignments/ipv4-address-space/ |
| 104 |
(define (ipv4-global-address? addr) |
| 105 |
(let ((x/24 (ash addr -8)) |
| 106 |
(x/16 (ash addr -16)) |
| 107 |
(x/12 (ash addr -20)) |
| 108 |
(x/8 (ash addr -24))) |
| 109 |
(not (or (memv x/8 '(0 ; 0.0.0.0/8 self-identification |
| 110 |
10 ; 10.0.0.0/8 private |
| 111 |
127)) ; 127.0.0.0/8 loopback |
| 112 |
(eqv? x/12 #xac1) ; 172.16.0.0/12 private |
| 113 |
(memv x/16 '(#xa9fe ; 169.254.0.0/16 link local |
| 114 |
#xc0a8 ; 192.168.0.0/16 private |
| 115 |
#xc612 ; 198.18.0.0/15 benchmark |
| 116 |
#xc613)) ; 198.18.0.0/15 benchmark |
| 117 |
(memv x/24 '(#xc00002 ; 192.0.2.0/24 test-net |
| 118 |
#xc05863))) ; 192.88.99.0/24 6to4 anycast relay |
| 119 |
))) |