| Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright 2011 Daniel Drown | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  * http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  * | 
 | 16 |  * translate.h - translate from one version of ip to another | 
 | 17 |  */ | 
 | 18 | #ifndef __TRANSLATE_H__ | 
 | 19 | #define __TRANSLATE_H__ | 
 | 20 |  | 
| Lorenzo Colitti | f939060 | 2014-02-13 12:53:35 +0900 | [diff] [blame] | 21 | #include <linux/icmp.h> | 
| Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 22 | #include <linux/if_tun.h> | 
| junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 23 | #include <netinet/icmp6.h> | 
 | 24 | #include <netinet/in.h> | 
 | 25 | #include <netinet/ip.h> | 
 | 26 | #include <netinet/ip6.h> | 
 | 27 | #include <netinet/ip_icmp.h> | 
 | 28 | #include <netinet/tcp.h> | 
 | 29 | #include <netinet/udp.h> | 
| Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 30 |  | 
| Lorenzo Colitti | f939060 | 2014-02-13 12:53:35 +0900 | [diff] [blame] | 31 | #include "clatd.h" | 
| Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 32 | #include "common.h" | 
| Lorenzo Colitti | f939060 | 2014-02-13 12:53:35 +0900 | [diff] [blame] | 33 |  | 
| junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 34 | #define MAX_TCP_HDR (15 * 4)  // Data offset field is 4 bits and counts in 32-bit words. | 
| Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 35 |  | 
| Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 36 | // Calculates the checksum over all the packet components starting from pos. | 
| Lorenzo Colitti | a4454bf | 2014-02-25 09:27:31 +0900 | [diff] [blame] | 37 | uint16_t packet_checksum(uint32_t checksum, clat_packet packet, clat_packet_index pos); | 
| Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 38 |  | 
 | 39 | // Returns the total length of the packet components after pos. | 
| Lorenzo Colitti | a4454bf | 2014-02-25 09:27:31 +0900 | [diff] [blame] | 40 | uint16_t packet_length(clat_packet packet, clat_packet_index pos); | 
| Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 41 |  | 
 | 42 | // Returns true iff the given IPv6 address is in the plat subnet. | 
 | 43 | int is_in_plat_subnet(const struct in6_addr *addr6); | 
| Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 44 |  | 
 | 45 | // Functions to create tun, IPv4, and IPv6 headers. | 
 | 46 | void fill_tun_header(struct tun_pi *tun_header, uint16_t proto); | 
 | 47 | void fill_ip_header(struct iphdr *ip_targ, uint16_t payload_len, uint8_t protocol, | 
 | 48 |                     const struct ip6_hdr *old_header); | 
 | 49 | void fill_ip6_header(struct ip6_hdr *ip6, uint16_t payload_len, uint8_t protocol, | 
 | 50 |                      const struct iphdr *old_header); | 
 | 51 |  | 
| Lorenzo Colitti | f939060 | 2014-02-13 12:53:35 +0900 | [diff] [blame] | 52 | // Translate and send packets. | 
| Lorenzo Colitti | 91d0f1b | 2014-06-02 15:49:36 +0900 | [diff] [blame] | 53 | void translate_packet(int fd, int to_ipv6, const uint8_t *packet, size_t packetsize); | 
| Lorenzo Colitti | f939060 | 2014-02-13 12:53:35 +0900 | [diff] [blame] | 54 |  | 
| Lorenzo Colitti | 07fc761 | 2013-11-18 13:33:30 +0900 | [diff] [blame] | 55 | // Translate IPv4 and IPv6 packets. | 
| Lorenzo Colitti | a4454bf | 2014-02-25 09:27:31 +0900 | [diff] [blame] | 56 | int ipv4_packet(clat_packet out, clat_packet_index pos, const uint8_t *packet, size_t len); | 
 | 57 | int ipv6_packet(clat_packet out, clat_packet_index pos, const uint8_t *packet, size_t len); | 
| Lorenzo Colitti | 07fc761 | 2013-11-18 13:33:30 +0900 | [diff] [blame] | 58 |  | 
| Lorenzo Colitti | 57d480d | 2014-02-09 10:35:38 +0900 | [diff] [blame] | 59 | // Deal with fragmented packets. | 
 | 60 | size_t maybe_fill_frag_header(struct ip6_frag *frag_hdr, struct ip6_hdr *ip6_targ, | 
 | 61 |                               const struct iphdr *old_header); | 
 | 62 | uint8_t parse_frag_header(const struct ip6_frag *frag_hdr, struct iphdr *ip_targ); | 
 | 63 |  | 
| Lorenzo Colitti | 2b4cc73 | 2014-02-09 10:35:38 +0900 | [diff] [blame] | 64 | // Deal with fragmented packets. | 
 | 65 | size_t maybe_fill_frag_header(struct ip6_frag *frag_hdr, struct ip6_hdr *ip6_targ, | 
 | 66 |                               const struct iphdr *old_header); | 
 | 67 | uint8_t parse_frag_header(const struct ip6_frag *frag_hdr, struct iphdr *ip_targ); | 
 | 68 |  | 
| Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 69 | // Translate ICMP packets. | 
| Lorenzo Colitti | a4454bf | 2014-02-25 09:27:31 +0900 | [diff] [blame] | 70 | int icmp_to_icmp6(clat_packet out, clat_packet_index pos, const struct icmphdr *icmp, | 
 | 71 |                   uint32_t checksum, const uint8_t *payload, size_t payload_size); | 
 | 72 | int icmp6_to_icmp(clat_packet out, clat_packet_index pos, const struct icmp6_hdr *icmp6, | 
| Brian Carlstrom | fcac410 | 2014-02-24 20:03:01 -0800 | [diff] [blame] | 73 |                   const uint8_t *payload, size_t payload_size); | 
| Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 74 |  | 
| Lorenzo Colitti | c9f4c89 | 2013-11-18 12:59:44 +0900 | [diff] [blame] | 75 | // Translate generic IP packets. | 
| Lorenzo Colitti | a4454bf | 2014-02-25 09:27:31 +0900 | [diff] [blame] | 76 | int generic_packet(clat_packet out, clat_packet_index pos, const uint8_t *payload, size_t len); | 
| Lorenzo Colitti | c9f4c89 | 2013-11-18 12:59:44 +0900 | [diff] [blame] | 77 |  | 
| Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 78 | // Translate TCP and UDP packets. | 
| junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 79 | int tcp_packet(clat_packet out, clat_packet_index pos, const struct tcphdr *tcp, uint32_t old_sum, | 
 | 80 |                uint32_t new_sum, size_t len); | 
 | 81 | int udp_packet(clat_packet out, clat_packet_index pos, const struct udphdr *udp, uint32_t old_sum, | 
 | 82 |                uint32_t new_sum, size_t len); | 
| Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 83 |  | 
| Lorenzo Colitti | a4454bf | 2014-02-25 09:27:31 +0900 | [diff] [blame] | 84 | int tcp_translate(clat_packet out, clat_packet_index pos, const struct tcphdr *tcp, | 
| junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 85 |                   size_t header_size, uint32_t old_sum, uint32_t new_sum, const uint8_t *payload, | 
 | 86 |                   size_t payload_size); | 
| Lorenzo Colitti | a4454bf | 2014-02-25 09:27:31 +0900 | [diff] [blame] | 87 | int udp_translate(clat_packet out, clat_packet_index pos, const struct udphdr *udp, | 
| junyulai | c4e591a | 2018-11-26 22:36:10 +0900 | [diff] [blame] | 88 |                   uint32_t old_sum, uint32_t new_sum, const uint8_t *payload, size_t payload_size); | 
| Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 89 |  | 
 | 90 | #endif /* __TRANSLATE_H__ */ |