xlat 0.1.0.alpha1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (191) hide show
  1. checksums.yaml +7 -0
  2. data/.dockerignore +7 -0
  3. data/.rspec +3 -0
  4. data/Cargo.lock +348 -0
  5. data/Cargo.toml +7 -0
  6. data/Dockerfile +49 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +100 -0
  9. data/Rakefile +82 -0
  10. data/benchmark/run.rb +38 -0
  11. data/benchmark/tcp.cfg +23 -0
  12. data/clab/.gitignore +5 -0
  13. data/clab/464xlat-ce-pd.clab.yml +138 -0
  14. data/clab/build.sh +6 -0
  15. data/exe/xlat-siit +91 -0
  16. data/ext/xlat/io_buffer_ext/Cargo.toml +11 -0
  17. data/ext/xlat/io_buffer_ext/extconf.rb +4 -0
  18. data/ext/xlat/io_buffer_ext/src/gvl.rs +49 -0
  19. data/ext/xlat/io_buffer_ext/src/io_buffer.rs +67 -0
  20. data/ext/xlat/io_buffer_ext/src/lib.rs +83 -0
  21. data/fuzz/corpus/010507e2c9f3b5132cbf036c7197f155cd85cca8 +0 -0
  22. data/fuzz/corpus/019a67cbfe27ed6606fe9d9cbee1ba16eb160667 +0 -0
  23. data/fuzz/corpus/021f01b7a6d4ba3e8e45a733c453d6c84f8c4c38 +0 -0
  24. data/fuzz/corpus/02db19829d1a4cbe18127e9ab1f3792e0d7fd5f7 +0 -0
  25. data/fuzz/corpus/059b719daf320e809db75d37ecf7823ba33ca7aa +0 -0
  26. data/fuzz/corpus/080ce49cbdb1983f8b9af09d511c5ec5a6bb14ed +0 -0
  27. data/fuzz/corpus/080d7d8674e7d60ce13b48995567a4ad21a3d652 +0 -0
  28. data/fuzz/corpus/08d488926f31661a1b25f60a723ae1931c762255 +0 -0
  29. data/fuzz/corpus/09ee56fb3259c88ad2cb94951313d792f36da221 +0 -0
  30. data/fuzz/corpus/0bffe4074467b5c4ba2bd5aa33839c5a1b8c1d63 +0 -0
  31. data/fuzz/corpus/0dac63180387255727c844898164ce20ff08fded +0 -0
  32. data/fuzz/corpus/0dc6f6c09c5b7bb4da8d496ac924e7e0d4aa37ad +0 -0
  33. data/fuzz/corpus/0e3e8e947fd9bfed02f927d1a2651e484d231fc0 +0 -0
  34. data/fuzz/corpus/0e5fa83f0ef8ecbf774ac96b8bdeda31395a284d +0 -0
  35. data/fuzz/corpus/12f93fa0b09c44d6c61ccde2731b5554bb180144 +0 -0
  36. data/fuzz/corpus/1659c552b9be677f2c30210430a97c3628f615be +0 -0
  37. data/fuzz/corpus/183aeabfe58d5ccfd5f2ed84e2f5ee4931f14eb7 +0 -0
  38. data/fuzz/corpus/1b9293272fbacbfff7aff4fd951dd7beb6aed3bf +0 -0
  39. data/fuzz/corpus/1c30abedb2d0943ef79b0620f7b3a7e8cdebc67f +0 -0
  40. data/fuzz/corpus/1c479cc892198d0cc389cc681206393c8ae4cbb6 +0 -0
  41. data/fuzz/corpus/1ca9480af5da2221fc320bb281d7c4ff73d5f33c +0 -0
  42. data/fuzz/corpus/1d6087f18146fac3b663bcc635fb470cb651884b +0 -0
  43. data/fuzz/corpus/258d23cbc2c8081782f0dd539270848e275a8e15 +0 -0
  44. data/fuzz/corpus/27512fa5c0ac5f4d8393ab442b50af3e83047c01 +0 -0
  45. data/fuzz/corpus/29933024f32228a8b65419d63dcbb56124db0df7 +0 -0
  46. data/fuzz/corpus/2b19339a2f24978c8413912a8003ae8ba8885684 +0 -0
  47. data/fuzz/corpus/2f8d2861a271e3ee2f658b01ef6ba854d8837aff +0 -0
  48. data/fuzz/corpus/2fed10d20fc4ed25da9cebd41209886304b39465 +0 -0
  49. data/fuzz/corpus/30fd38c3af4877eda7b1ca2dee1a60c4fe0f757c +0 -0
  50. data/fuzz/corpus/3137ba78351b6757255261a4f364e44693c3c85f +0 -0
  51. data/fuzz/corpus/34e68133b209c490f960f9682d33d41aab99e60f +0 -0
  52. data/fuzz/corpus/359870740db1c245d6dfdad1f119c7b17c3f25fc +0 -0
  53. data/fuzz/corpus/37b1bb01b0544125df396b925e2c4242a90d1c52 +0 -0
  54. data/fuzz/corpus/387cab0f8ac36f0f5496f6b0aed7d6c7ca3f2dbf +0 -0
  55. data/fuzz/corpus/3c4e48335e11c3bd066bbb43db4bf82249668869 +0 -0
  56. data/fuzz/corpus/3ec111be62099aaa9b960f4b69c65a3b12962970 +0 -0
  57. data/fuzz/corpus/3fb2f3f01b304e48d9c60477dfe5834693ef80db +0 -0
  58. data/fuzz/corpus/40cf2f11c2ea03c56864f5097d2f989b14762426 +0 -0
  59. data/fuzz/corpus/40cfc6a6a60f2451974522e5450ba6434f61a8bb +0 -0
  60. data/fuzz/corpus/4c3f43f4654332520eb57a9bcfe95951857272ce +0 -0
  61. data/fuzz/corpus/4c85fdf6934e86766274dd668f3f69f15c7bbc11 +0 -0
  62. data/fuzz/corpus/4f708dd44266b8b567e295c03811feef57c0a493 +0 -0
  63. data/fuzz/corpus/50cfff5d1cfc310686d7c1f38787cb77ec6fd892 +0 -0
  64. data/fuzz/corpus/53b2e1cb235c4479711d9ac74a375330d69f9e4c +0 -0
  65. data/fuzz/corpus/560373973be830ca5a34ab8e4d4b25a942ad5ed6 +0 -0
  66. data/fuzz/corpus/57768a4d41ccb6149219ba3eb7fe9e5b191dc4aa +0 -0
  67. data/fuzz/corpus/5a7833a1a882226aa3831298fdb0d23b580016d8 +0 -0
  68. data/fuzz/corpus/5b394ecc62e63d030cfeff7e0c484c233c7e9d63 +0 -0
  69. data/fuzz/corpus/5c10ea32f35ab603f1acd9b3c3ac3c397c1c690a +0 -0
  70. data/fuzz/corpus/5d4f6df161ce9c6465641cea9e0e97771f4249cf +0 -0
  71. data/fuzz/corpus/5f29c11e0f583226dfa98c236b8d48c772bccbf1 +0 -0
  72. data/fuzz/corpus/603d6df8d387f6b191f9b732d8928638e2deab10 +0 -0
  73. data/fuzz/corpus/60e17201e5bc64340b37ac96ff6302546c231eea +0 -0
  74. data/fuzz/corpus/65cc3e13ae39a4cd1b207a3395b5205d3fbcae7b +0 -0
  75. data/fuzz/corpus/692c3712da22c928e0d1b5f91ea7a74ba6fcdf5f +0 -0
  76. data/fuzz/corpus/69ee357cc3bda8a068bc875dda0879bb462b4984 +0 -0
  77. data/fuzz/corpus/6a2310279f4fa43ee4ed3809bc039f8ec7bfdeb4 +0 -0
  78. data/fuzz/corpus/6bfdbaafbe1d257b49f58b7a2fa3fef51894d76a +0 -0
  79. data/fuzz/corpus/6d3c4288760aaf7131fb3caebb5b3aad6a1828fd +0 -0
  80. data/fuzz/corpus/6e2f243fb0020e9bfb797c0100da18a67c8f1cb7 +0 -0
  81. data/fuzz/corpus/6ee7bf4cd8249100046bd4ba28b6646dfd436bd4 +0 -0
  82. data/fuzz/corpus/70e0947673dd1cc2680f907ecfde339688c3fadb +0 -0
  83. data/fuzz/corpus/76b1ab6e0edbfeefd4a23fc8a18f7d5937fe3787 +0 -0
  84. data/fuzz/corpus/77d6e8a03cec0da5803042fd51030e97fb11f5fd +0 -0
  85. data/fuzz/corpus/79b7efd2703e6343b8daf769aea6a76b199d7aa2 +0 -0
  86. data/fuzz/corpus/7ab28e22d79e7f2774ab2b8baaddeedf58f37475 +0 -0
  87. data/fuzz/corpus/7cb99d73cc9c03dc463a7892b27ae78a857c2450 +0 -0
  88. data/fuzz/corpus/81f5010a3c5a44639c0fbb6325c163cd5f8b7ee9 +0 -0
  89. data/fuzz/corpus/8317c66a4178b54bfd405332490dfd82a3949244 +0 -0
  90. data/fuzz/corpus/871b1e5a6ad58fbd654725c0c280f1aa278cd7b5 +0 -0
  91. data/fuzz/corpus/879bd3b24e7c968141bf3c69fdfb11da99af8e8e +0 -0
  92. data/fuzz/corpus/8a6bc02a79659eb0c71d5bc784da0f1b351b4336 +0 -0
  93. data/fuzz/corpus/8d6d41bc7e792d6737c35d7c989c9772ad3ae1d6 +0 -0
  94. data/fuzz/corpus/8ee4e2e6827b38feaa739c7a61535087ae1ca91c +0 -0
  95. data/fuzz/corpus/911a7cad153c517c75ea7d410c4f093865da5b50 +0 -0
  96. data/fuzz/corpus/932c12b633a087eff4f195da9fed03e202190515 +0 -0
  97. data/fuzz/corpus/93c883857d41e5cdaaa4c2b04dd316da73c9c519 +0 -0
  98. data/fuzz/corpus/944e31de1264ab5347f37d1454b6bcdd06b85b51 +0 -0
  99. data/fuzz/corpus/9882bea35622d45012774dbf8f9658c597f07d51 +0 -0
  100. data/fuzz/corpus/989f9271f90207d498fb69eeba16b8393f8cc70b +0 -0
  101. data/fuzz/corpus/9927a043166634231659695111b473cbd19fbeec +0 -0
  102. data/fuzz/corpus/9a33c2ce796569be8450658c6231936816ee55bb +0 -0
  103. data/fuzz/corpus/9a51def27a9547cf9aa059809a2d5482f75ae8af +0 -0
  104. data/fuzz/corpus/9b915fd1452d10a00ae2ecb0d53784a127341e79 +0 -0
  105. data/fuzz/corpus/9de518e7fb61598ac152a91feaf2d7bbe3e8b943 +0 -0
  106. data/fuzz/corpus/9eaee706fb7d021dc5f0ecc79b524de9c74f439e +0 -0
  107. data/fuzz/corpus/9ecb6bbe9d4c7414406e4c002b449279bf898ee4 +0 -0
  108. data/fuzz/corpus/a389244e26c3e525393db7c6b89495981c5160e5 +0 -0
  109. data/fuzz/corpus/a52173807baf225d705386e47e57a115a8cb537c +0 -0
  110. data/fuzz/corpus/a6fdb8761221dfd348c29c9eb224eb5cf5e4849a +0 -0
  111. data/fuzz/corpus/a76ca8a3f3ffb4ff898f896b65a5bae2d0ed002a +0 -0
  112. data/fuzz/corpus/a91ff9c074e9503baa27194d220dca03926a4f3d +0 -0
  113. data/fuzz/corpus/aa66cf1089cea0a8aa7e54394e75d4f2949bba74 +0 -0
  114. data/fuzz/corpus/aafdf57c09c20115384292e1b39cf7dae6674fff +0 -0
  115. data/fuzz/corpus/af0d6601066ec5c6bac2f0a57b5753ef2826463d +0 -0
  116. data/fuzz/corpus/b26766574788c20a8ec6927c19327f3919c7a69f +0 -0
  117. data/fuzz/corpus/b2b582043818797e2a53a72e3f24e545fbda3b57 +0 -0
  118. data/fuzz/corpus/b31e0aa5172beb560c264d8b5c84932870271e9e +0 -0
  119. data/fuzz/corpus/b396625aa6e69a5eef782446bbc09606050abb8a +0 -0
  120. data/fuzz/corpus/b5693370c14ce015236f7746ee63d0af38ec89e4 +0 -0
  121. data/fuzz/corpus/b8fd1ed5f8c575a8ac6a7c468bf93f1f96f8edb4 +0 -0
  122. data/fuzz/corpus/b928cbb72b744adec35cba73db6b09b4c0c8af42 +0 -0
  123. data/fuzz/corpus/b9e36aaac143135fadf4bbcac8d892a7b515cbaf +0 -0
  124. data/fuzz/corpus/bae2b0281a4ce082295254dd4f49183ca6b5f2d3 +0 -0
  125. data/fuzz/corpus/bfdc9d194a5a4ed98adfa14a47cec84ca91aa4ad +0 -0
  126. data/fuzz/corpus/c2f64ce75de11ecb09496d5d95955da7730872e1 +0 -0
  127. data/fuzz/corpus/c45f32e7bfa9ea837d6cdd1844d7c8d354981242 +0 -0
  128. data/fuzz/corpus/c64d7ac2a14b5227524521fab16ff0ca4e5170d6 +0 -0
  129. data/fuzz/corpus/c6f8466a86b7371cbf715317cbd313f609ee739b +0 -0
  130. data/fuzz/corpus/c9d6e62a62fffb21687fae534b8113a07f92aee6 +0 -0
  131. data/fuzz/corpus/d0171338648afb30043ea71f3561c60fd8372a1d +0 -0
  132. data/fuzz/corpus/d0faa1afa3e94f6a51a1a4c0d3dafb2600592593 +0 -0
  133. data/fuzz/corpus/d467fc34aec5d7f2b24a3df67dbf99080dc00681 +0 -0
  134. data/fuzz/corpus/d696b7b20d1cd2a3c2bf47c302afbba1696632c9 +0 -0
  135. data/fuzz/corpus/d6d7695a3c86643a3ad779b75ea4d00fb535a3d9 +0 -0
  136. data/fuzz/corpus/d7b303e5aec6bac4311102a324cbea23663b2b20 +0 -0
  137. data/fuzz/corpus/d8afc09fe51ea23526db161a2a1cdacba0b1fefe +0 -0
  138. data/fuzz/corpus/d9bb42d0a55b8a200e3dfea84ca2e6e265ef2366 +0 -0
  139. data/fuzz/corpus/da6d7c1f2a7d5b0dabe71f048c9c1624c8ebdbd9 +0 -0
  140. data/fuzz/corpus/da70dbded78bc388cd692dc36b9da4697d5b6ad2 +0 -0
  141. data/fuzz/corpus/da71cde284c9f10c6c6952e2e800558720914c30 +0 -0
  142. data/fuzz/corpus/dcdfeaf43771474c3b236ffe79e14dc78b3b09ec +0 -0
  143. data/fuzz/corpus/dcef32e091776dc98ac4a4cf951528857d0b7ce1 +0 -0
  144. data/fuzz/corpus/ddedc95cd0c2db9ad8895fb33227e3e759f57ed9 +0 -0
  145. data/fuzz/corpus/de361721e7f714ac3a55ef5839801e0bf3566b37 +0 -0
  146. data/fuzz/corpus/e1c05af770b1443f8cfeff261affb50b3c551aa0 +0 -0
  147. data/fuzz/corpus/e62f88307794127ffccadf614b7062c3e78ce5c0 +0 -0
  148. data/fuzz/corpus/e85854d63d682e0a16f9aefa477a4337dbc63f5d +0 -0
  149. data/fuzz/corpus/e86666a117e96697f7823d6d61b448a76c7c8c64 +0 -0
  150. data/fuzz/corpus/e91897c8cefdd583e1d60e6a833fab40d775cff1 +0 -0
  151. data/fuzz/corpus/eb8ed69481cf71efdac8df0759d28624f2bc40cc +0 -0
  152. data/fuzz/corpus/ee9308e841589fbabf3013d9366ac6556d82bdf9 +0 -0
  153. data/fuzz/corpus/ef2a0b39689c489d499abc4188b45fafdba66ab8 +0 -0
  154. data/fuzz/corpus/f013d9c3de8fe4958ad84c56c651031314a92e26 +0 -0
  155. data/fuzz/corpus/f19cf033be3041feeee3eefe45ba5adbf2f5d421 +0 -0
  156. data/fuzz/corpus/f1c9fa3baf1730afaa330216edcb5ba34d2590b0 +0 -0
  157. data/fuzz/corpus/f1ed8642840969d76fff003dc68e48a717050e72 +0 -0
  158. data/fuzz/corpus/f2fbe027944e00facddfcb425be9c0269fe3b8b9 +0 -0
  159. data/fuzz/corpus/f7f6ff7ec8323de5100118914bacde5b4cc81a9e +0 -0
  160. data/fuzz/corpus/fade0bf074f861cdb9013fbdd3b084a60d9ac858 +0 -0
  161. data/fuzz/corpus/fd85b215e07f3ce968a5e0b810c6d04930b533e8 +0 -0
  162. data/fuzz/corpus/fd883fb9a67accfc7c61ddda6a781677cc8efc33 +0 -0
  163. data/fuzz/corpus/fd8953a87e3026bdb15d52faa387a6d49586cd66 +0 -0
  164. data/fuzz/corpus/fe46814360987e6b9cdaebf31a8a4ec7d26125b4 +0 -0
  165. data/fuzz/corpus/ffd623a08d5c93aae2ffed33346316d76d9abf1c +0 -0
  166. data/fuzz/instrumentation.rb +13 -0
  167. data/fuzz/run +11 -0
  168. data/fuzz/test_harness.rb +38 -0
  169. data/fuzz/test_tracer.rb +16 -0
  170. data/lib/xlat/adapters/linux_tun.rb +55 -0
  171. data/lib/xlat/address_translation.rb +32 -0
  172. data/lib/xlat/address_translators/rfc6052.rb +46 -0
  173. data/lib/xlat/common.rb +13 -0
  174. data/lib/xlat/pcap.rb +48 -0
  175. data/lib/xlat/protocols/icmp/base.rb +76 -0
  176. data/lib/xlat/protocols/icmp/echo.rb +71 -0
  177. data/lib/xlat/protocols/icmp/error.rb +70 -0
  178. data/lib/xlat/protocols/icmp.rb +14 -0
  179. data/lib/xlat/protocols/ip/ipv4.rb +106 -0
  180. data/lib/xlat/protocols/ip/ipv6.rb +133 -0
  181. data/lib/xlat/protocols/ip.rb +208 -0
  182. data/lib/xlat/protocols/tcp.rb +71 -0
  183. data/lib/xlat/protocols/tcpudp.rb +52 -0
  184. data/lib/xlat/protocols/udp.rb +66 -0
  185. data/lib/xlat/protocols.rb +0 -0
  186. data/lib/xlat/rfc7915.rb +580 -0
  187. data/lib/xlat/runner.rb +51 -0
  188. data/lib/xlat/version.rb +5 -0
  189. data/lib/xlat.rb +8 -0
  190. data/sig/xlat.rbs +4 -0
  191. metadata +246 -0
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is based on the source code available at https://github.com/kazuho/rat under MIT License
4
+ #
5
+ # Copyright (c) 2022 Kazuho Oku
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+
26
+ require_relative './base'
27
+
28
+ module Xlat
29
+ module Protocols
30
+ module Icmp
31
+ class Error < Base
32
+ V4_TYPE_DEST_UNREACH = 3
33
+ V4_TYPE_TIME_EXCEEDED = 11
34
+ V4_TYPE_PARAMETER_PROBLEM = 12
35
+
36
+ V6_TYPE_DEST_UNREACH = 1
37
+ V6_TYPE_PACKET_TOO_BIG = 2
38
+ V6_TYPE_TIME_EXCEEDED = 3
39
+ V6_TYPE_PARAMETER_PROBLEM = 4
40
+
41
+ attr_reader :payload_bytes
42
+ attr_reader :payload_bytes_offset
43
+ attr_reader :payload_bytes_length
44
+
45
+ def _parse
46
+ super
47
+ # Base.parse ensures packet.l4_bytes_length >= 8
48
+
49
+ packet = @packet
50
+
51
+ @payload_bytes = packet.l4_bytes
52
+ @payload_bytes_offset = packet.l4_bytes_offset + 8
53
+ @payload_bytes_length = packet.l4_bytes_length - 8
54
+
55
+ self
56
+ end
57
+
58
+ def apply(cs_delta)
59
+ # @original.apply
60
+
61
+ # # overwrite packet image with orig packet being built
62
+ # @packet.bytes[@packet.l4_start + 8..] = @original.bytes
63
+
64
+ # Base.recalculate_checksum(@packet)
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ require_relative './icmp/base'
3
+ require_relative './icmp/echo'
4
+ require_relative './icmp/error'
5
+
6
+ module Xlat
7
+ module Protocols
8
+ module Icmp
9
+ def self.parse(packet)
10
+ Xlat::Protocols::Icmp::Base.parse(packet)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is based on the source code available at https://github.com/kazuho/rat under MIT License
4
+ #
5
+ # Copyright (c) 2022 Kazuho Oku
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+
26
+ require 'xlat/common'
27
+ require 'xlat/protocols/icmp'
28
+
29
+ module Xlat
30
+ module Protocols
31
+ class Ip
32
+ module Ipv4
33
+ extend Xlat::Common
34
+
35
+ def self.to_i
36
+ 4
37
+ end
38
+
39
+ def self.tuple(bytes, offset)
40
+ bytes.slice(offset + 12, 8)
41
+ end
42
+
43
+ def self.icmp_protocol_id
44
+ Icmp::Base::V4_PROTOCOL_ID
45
+ end
46
+
47
+ def self.icmp_cs_delta(packet)
48
+ 0
49
+ end
50
+
51
+ def self.new_icmp(packet, type)
52
+ case type
53
+ when Icmp::Echo::V4_TYPE_REQUEST
54
+ Icmp::Echo.new(packet, true)
55
+ when Icmp::Echo::V4_TYPE_REPLY
56
+ Icmp::Echo.new(packet, false)
57
+ when Icmp::Error::V4_TYPE_DEST_UNREACH, Icmp::Error::V4_TYPE_TIME_EXCEEDED, Icmp::Error::V4_TYPE_PARAMETER_PROBLEM
58
+ Icmp::Error.new(packet)
59
+ else
60
+ Icmp::Base.new(packet)
61
+ end
62
+ end
63
+
64
+ def self.parse(packet, b0)
65
+ bytes = packet.bytes
66
+ offset = packet.bytes_offset
67
+
68
+ header_length = (b0 & 0x0f) * 4
69
+ return false if header_length < 20
70
+
71
+ # tos?
72
+
73
+ total_length = bytes.get_value(:U16, offset + 2)
74
+ return false if total_length < header_length
75
+ return false unless packet.set_l4_region(header_length, total_length - header_length)
76
+
77
+ # ignore identification
78
+ return false if bytes.get_value(:U16, offset + 6) & 0xbfff != 0 # discard fragments
79
+
80
+ proto = bytes.get_value(:U8, offset + 9)
81
+ packet.proto = proto
82
+
83
+ true
84
+ end
85
+
86
+ def self.apply(bytes, offset, cs_delta, icmp_payload)
87
+ # decrement TTL
88
+ unless icmp_payload
89
+ ttl = bytes.get_value(:U8, offset + 8)
90
+ if ttl > 0
91
+ ttl -= 1
92
+ bytes.set_value(:U8, offset + 8, ttl)
93
+ cs_delta -= 0x0100 # checksum computation is performed per 2 octets
94
+ end
95
+ end
96
+
97
+ if cs_delta != 0
98
+ checksum = bytes.get_value(:U16, offset + 10)
99
+ checksum = Ip.checksum_adjust(checksum, cs_delta)
100
+ bytes.set_value(:U16, offset + 10, checksum)
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is based on the source code available at https://github.com/kazuho/rat under MIT License
4
+ #
5
+ # Copyright (c) 2022 Kazuho Oku
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+
26
+ require 'xlat/common'
27
+ require 'xlat/protocols/icmp'
28
+
29
+ module Xlat
30
+ module Protocols
31
+ class Ip
32
+ module Ipv6
33
+ extend Xlat::Common
34
+
35
+ # https://www.iana.org/assignments/ipv6-parameters/ipv6-parameters.xhtml#extension-header
36
+ # except Fragment (44), ESP (50), AH (51)
37
+ EXTENSIONS = Ractor.make_shareable([0, 43, 60, 135, 139, 140, 253, 254].to_h { |id| [id, true] })
38
+
39
+ def self.to_i
40
+ 6
41
+ end
42
+
43
+ def self.tuple(bytes, offset)
44
+ bytes.slice(offset + 8, 32)
45
+ end
46
+
47
+ def self.icmp_protocol_id
48
+ Icmp::Base::V6_PROTOCOL_ID
49
+ end
50
+
51
+ def self.icmp_cs_delta(packet)
52
+ upper_layer_packet_length = packet.l4_bytes.size - packet.l4_bytes_offset
53
+ Common.sum16be(tuple) + upper_layer_packet_length + packet.proto
54
+ end
55
+
56
+ def self.new_icmp(packet, type)
57
+ case type
58
+ when Icmp::Echo::V6_TYPE_REQUEST
59
+ Icmp::Echo.new(packet, true)
60
+ when Icmp::Echo::V6_TYPE_REPLY
61
+ Icmp::Echo.new(packet, true)
62
+ when Icmp::Error::V6_TYPE_DEST_UNREACH, Icmp::Error::V6_TYPE_PACKET_TOO_BIG, Icmp::Error::V6_TYPE_TIME_EXCEEDED, Icmp::Error::V6_TYPE_PARAMETER_PROBLEM
63
+ Icmp::Error.new(packet)
64
+ else
65
+ Icmp::Base.new(packet)
66
+ end
67
+ end
68
+
69
+ def self.parse(packet, _b0)
70
+ bytes = packet.bytes
71
+ bytes_length = packet.bytes_length
72
+ offset = packet.bytes_offset
73
+
74
+ return false if bytes_length < 40
75
+
76
+ payload_length = bytes.get_value(:U16, offset + 4)
77
+ proto = bytes.get_value(:U8, offset + 6)
78
+
79
+ # [draft-ietf-6man-eh-limits-19] Section 4 suggests IPv6 nodes
80
+ # to process at least 64 bytes long chain of EHs.
81
+ extensions_length_limit = [payload_length, 64].min
82
+ extensions_length = 0
83
+
84
+ while EXTENSIONS[proto]
85
+ extension_start = 40 + extensions_length
86
+ return false if extension_start + 8 > bytes_length # EH is at least 8 byte long
87
+
88
+ proto = bytes.get_value(:U8, offset + extension_start)
89
+ length = bytes.get_value(:U8, offset + extension_start + 1) * 8 + 8
90
+
91
+ extensions_length += length
92
+ return false if extensions_length > extensions_length_limit
93
+
94
+ # TODO: Routing header
95
+ end
96
+
97
+ if proto == 51 # AH
98
+ # AH has a non-standard EH format. It doesn't work with NAT anyway.
99
+ return false
100
+ end
101
+
102
+ # We assume Fragment is at the last of EH chain.
103
+ if proto == 44 # Fragment
104
+ # TODO: handle fragmentation
105
+ return false
106
+ end
107
+
108
+ # ESP is handled as L4 protocol
109
+
110
+ return false unless packet.set_l4_region(40 + extensions_length, payload_length - extensions_length)
111
+ packet.proto = proto
112
+
113
+ true
114
+ end
115
+
116
+ def self.apply(bytes, offset, _cs_delta, icmp_payload)
117
+ # decrement hop limit
118
+ unless icmp_payload
119
+ hop_limit = bytes.get_value(:U8, offset + 7)
120
+ if hop_limit > 0
121
+ hop_limit -= 1
122
+ bytes.set_value(:U8, offset + 7, hop_limit)
123
+ end
124
+ end
125
+
126
+ # IPv6 has no checksum
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
132
+
133
+
@@ -0,0 +1,208 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is based on the source code available at https://github.com/kazuho/rat under MIT License
4
+ #
5
+ # Copyright (c) 2022 Kazuho Oku
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+
26
+ module Xlat
27
+ module Protocols
28
+ class Ip
29
+ attr_accessor :bytes # IO::Buffer containing L3 header
30
+ attr_accessor :bytes_offset # Offset where L3 header begins within `bytes`
31
+ attr_accessor :bytes_length # Length of L3 datagram within `bytes`
32
+ attr_accessor :proto # L4 protocol ID
33
+ attr_accessor :l4_start # L3 header length
34
+ attr_accessor :l4_length # Length of L4 packet, as specified in L3 header
35
+ attr_accessor :l4
36
+ attr_accessor :l4_bytes # IO::Buffer containing L4 packet
37
+ attr_accessor :l4_bytes_offset # Offset where L4 header begins within `l4_bytes`
38
+ attr_accessor :l4_bytes_length # Length of L4 datagram within `l4_bytes`, possibly truncated
39
+ attr_accessor :cs_delta # Accumulated changes to be applied to L4 checksum
40
+
41
+ attr_reader :version
42
+
43
+ def initialize(icmp_payload: false)
44
+ @icmp_payload = icmp_payload
45
+ @_tcp = Tcp.new(self, icmp_payload:)
46
+ @_udp = Udp.new(self, icmp_payload:)
47
+ end
48
+
49
+ def self.parse(bytes)
50
+ new.parse(bytes:)
51
+ end
52
+
53
+ def parse(bytes:, bytes_offset: 0, bytes_length: bytes.size - bytes_offset, l4_bytes: nil, l4_bytes_offset: nil)
54
+ @bytes = bytes
55
+ @bytes_offset = bytes_offset
56
+ @bytes_length = bytes_length
57
+ @proto = nil
58
+ @version = nil
59
+ @l4_start = nil
60
+ @l4_length = nil
61
+ @l4 = nil
62
+ @l4_bytes = l4_bytes
63
+ @l4_bytes_offset = l4_bytes_offset
64
+ @l4_bytes_length = nil
65
+ @cs_delta = 0
66
+
67
+ # mimimum size for IPv4
68
+ return nil if bytes_length < 20
69
+
70
+ b0 = bytes.get_value(:U8, bytes_offset)
71
+ case b0 >> 4
72
+ when 4
73
+ @version = Ipv4
74
+ when 6
75
+ @version = Ipv6
76
+ else
77
+ return nil
78
+ end
79
+
80
+ return nil unless @version.parse(self, b0)
81
+
82
+ case @proto
83
+ when Protocols::Udp::PROTOCOL_ID
84
+ @l4 = @_udp.parse
85
+ when Protocols::Tcp::PROTOCOL_ID
86
+ @l4 = @_tcp.parse
87
+ when @version.icmp_protocol_id
88
+ @l4 = Protocols::Icmp.parse(self)
89
+ end
90
+
91
+ self
92
+ end
93
+
94
+ # @param start [Integer] Length of L3 header in octets
95
+ # @param length [Integer] Length of L4 datagram in octets, as specified in L3 header
96
+ # @return [true, false] Whether the given range is valid
97
+ def set_l4_region(start, length)
98
+ @l4_start = start
99
+ @l4_length = length
100
+
101
+ unless @l4_bytes
102
+ @l4_bytes = @bytes
103
+ @l4_bytes_offset = @bytes_offset + start
104
+ @l4_bytes_length = @bytes_length - start
105
+ end
106
+
107
+ if @l4_bytes_length < length
108
+ if @icmp_payload
109
+ # allow truncation in ICMP payload
110
+ length = @l4_bytes_length
111
+ return false if length < 0
112
+ else
113
+ return false
114
+ end
115
+ end
116
+
117
+ @l4_bytes_length = length
118
+
119
+ true
120
+ end
121
+
122
+ # Convert this packet into different IP version using the supplied buffer as header.
123
+ #
124
+ # @param version [Module] New version
125
+ # @param new_header_bytes [IO::Buffer] Buffer to hold L3 header.
126
+ # The caller is responsible for populating the buffer with a proper content.
127
+ # @param cs_delta [Integer] Checksum delta in the pseudo header to be cancelled with the L4 checksum.
128
+ # @return [Ip] self
129
+ def convert_version!(version, new_header_bytes, cs_delta)
130
+ @bytes = new_header_bytes
131
+ @bytes_offset = 0
132
+ @bytes_length = new_header_bytes.size
133
+ @version = version
134
+ @l4_start = new_header_bytes.size
135
+ @cs_delta += cs_delta
136
+ self
137
+ end
138
+
139
+ def tuple
140
+ @version.tuple(@bytes, @bytes_offset)
141
+ end
142
+
143
+ def apply_changes
144
+ cs_delta = @cs_delta
145
+ @version.apply(@bytes, @bytes_offset, cs_delta, @icmp_payload)
146
+ @l4&.apply(cs_delta)
147
+ @cs_delta = 0
148
+ end
149
+
150
+ def self.checksum(bytes, from = nil, len = nil)
151
+ from = 0 if from.nil?
152
+ len = bytes.size - from if len.nil?
153
+ to = from + len - 1
154
+
155
+ sum = Common.sum16be(bytes.slice(from, len))
156
+ sum += bytes.get_value(:U8, to) * 256 if len.odd?
157
+ sum = (sum & 0xffff) + (sum >> 16) while sum > 65535
158
+ ~sum & 0xffff
159
+ end
160
+
161
+ def self.checksum_list(buffers)
162
+ sum = 0
163
+ offset = 0
164
+ buffers.each do |buf|
165
+ if offset.odd?
166
+ sum += buf.get_value(:U8, 0)
167
+ buf = buf.slice(1)
168
+ offset += 1
169
+ end
170
+ sum += Common.sum16be(buf)
171
+ len = buf.size
172
+ if len.odd?
173
+ sum += buf.get_value(:U8, len - 1) << 8
174
+ end
175
+ offset += len
176
+ end
177
+ sum = (sum & 0xffff) + (sum >> 16) while sum > 65535
178
+ ~sum & 0xffff
179
+ end
180
+
181
+ # this function assumes 0 <= sum <= 65534
182
+ def self.checksum_adjust(checksum, delta)
183
+ delta %= 65535
184
+
185
+ mod65535 = 65534 - checksum
186
+ mod65535 = (mod65535 + delta) % 65535
187
+ 65534 - mod65535
188
+ end
189
+
190
+ def self.addr_to_s(addr)
191
+ case addr.length
192
+ when 4
193
+ addr.unpack('C4').join('.')
194
+ when 16
195
+ addr.unpack('n8').map { |f| format '%x', f }.join(':').gsub(/(:0)+(?=:)/, ':')
196
+ else
197
+ raise "unexpected address length of #{addr.length}"
198
+ end
199
+ end
200
+ end
201
+ end
202
+ end
203
+
204
+ require_relative './icmp'
205
+ require_relative './ip/ipv4'
206
+ require_relative './ip/ipv6'
207
+ require_relative './tcp'
208
+ require_relative './udp'
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is based on the source code available at https://github.com/kazuho/rat under MIT License
4
+ #
5
+ # Copyright (c) 2022 Kazuho Oku
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+
26
+ require_relative './tcpudp'
27
+
28
+ module Xlat
29
+ module Protocols
30
+ class Tcp < Tcpudp
31
+ PROTOCOL_ID = 6
32
+ DATA_OFFSET_OFFSET = 12
33
+ CHECKSUM_OFFSET = 16
34
+ OPTION_KIND_END = 0
35
+ OPTION_KIND_NOOP = 1
36
+ OPTION_KIND_MSS = 2
37
+ FLAG_FIN = 0x01
38
+ FLAG_SYN = 0x02
39
+ FLAG_RST = 0x04
40
+ FLAG_PST = 0x08
41
+ FLAG_ACK = 0x10
42
+ FLAG_URG = 0x20
43
+ FLAG_ECE = 0x40
44
+ FLAG_CWR = 0x80
45
+
46
+ def parse
47
+ packet = @packet
48
+ return nil if packet.l4_bytes_length < (@icmp_payload ? 4 : 20)
49
+
50
+ # bytes = packet.l4_bytes
51
+ # offset = packet.l4_bytes_offset
52
+
53
+ super
54
+ end
55
+
56
+ def apply(cs_delta)
57
+ return if cs_delta.zero?
58
+
59
+ packet = @packet
60
+ return if packet.l4_bytes_length < 18
61
+
62
+ bytes = packet.l4_bytes
63
+ offset = packet.l4_bytes_offset
64
+
65
+ checksum = bytes.get_value(:U16, offset + 16)
66
+ checksum = _adjust_checksum(checksum, cs_delta)
67
+ bytes.set_value(:U16, offset + 16, checksum)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is based on the source code available at https://github.com/kazuho/rat under MIT License
4
+ #
5
+ # Copyright (c) 2022 Kazuho Oku
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+
25
+
26
+ require 'xlat/common'
27
+ require 'xlat/protocols/icmp'
28
+
29
+ module Xlat
30
+ module Protocols
31
+ class Tcpudp
32
+ include Xlat::Common
33
+
34
+ def initialize(packet, icmp_payload:)
35
+ @packet = packet
36
+ @icmp_payload = icmp_payload
37
+ end
38
+
39
+ def parse
40
+ self
41
+ end
42
+
43
+ def tuple
44
+ @bytes.slice(@offset, 4)
45
+ end
46
+
47
+ def _adjust_checksum(checksum, cs_delta)
48
+ Ip.checksum_adjust(checksum, cs_delta)
49
+ end
50
+ end
51
+ end
52
+ end