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 | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 21 | #include <linux/if_tun.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 22 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 23 | #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] | 24 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 25 | // A clat_packet is an array of iovec structures representing a packet that we are translating. |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 26 | // The CLAT_POS_XXX constants represent the array indices within the clat_packet that contain |
| 27 | // specific parts of the packet. The packet_* functions operate on all the packet segments past a |
| 28 | // given position. |
| 29 | enum clat_packet_index { CLAT_POS_TUNHDR, CLAT_POS_IPHDR, CLAT_POS_TRANSPORTHDR, |
Lorenzo Colitti | cd70b35 | 2013-04-10 12:24:56 +0900 | [diff] [blame] | 30 | CLAT_POS_ICMPERR_IPHDR, CLAT_POS_ICMPERR_TRANSPORTHDR, |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 31 | CLAT_POS_PAYLOAD, CLAT_POS_MAX }; |
| 32 | typedef struct iovec clat_packet[CLAT_POS_MAX]; |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 33 | |
Lorenzo Colitti | ee80ca6 | 2013-04-10 16:52:22 +0900 | [diff] [blame] | 34 | // Calculates the checksum over all the packet components starting from pos. |
| 35 | uint16_t packet_checksum(uint32_t checksum, clat_packet packet, int pos); |
| 36 | |
| 37 | // Returns the total length of the packet components after pos. |
| 38 | uint16_t packet_length(clat_packet packet, int pos); |
| 39 | |
| 40 | // Returns true iff the given IPv6 address is in the plat subnet. |
| 41 | int is_in_plat_subnet(const struct in6_addr *addr6); |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 42 | |
| 43 | // Functions to create tun, IPv4, and IPv6 headers. |
| 44 | void fill_tun_header(struct tun_pi *tun_header, uint16_t proto); |
| 45 | void fill_ip_header(struct iphdr *ip_targ, uint16_t payload_len, uint8_t protocol, |
| 46 | const struct ip6_hdr *old_header); |
| 47 | void fill_ip6_header(struct ip6_hdr *ip6, uint16_t payload_len, uint8_t protocol, |
| 48 | const struct iphdr *old_header); |
| 49 | |
Lorenzo Colitti | 07fc761 | 2013-11-18 13:33:30 +0900 | [diff] [blame] | 50 | // Translate IPv4 and IPv6 packets. |
| 51 | int ipv4_packet(clat_packet out, int pos, const char *packet, size_t len); |
| 52 | int ipv6_packet(clat_packet out, int pos, const char *packet, size_t len); |
| 53 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 54 | // Translate ICMP packets. |
| 55 | int icmp_to_icmp6(clat_packet out, int pos, const struct icmphdr *icmp, uint32_t checksum, |
| 56 | const char *payload, size_t payload_size); |
Lorenzo Colitti | 9477a46 | 2013-11-18 15:56:02 +0900 | [diff] [blame] | 57 | int icmp6_to_icmp(clat_packet out, int pos, const struct icmp6_hdr *icmp6, |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 58 | const char *payload, size_t payload_size); |
| 59 | |
Lorenzo Colitti | c9f4c89 | 2013-11-18 12:59:44 +0900 | [diff] [blame^] | 60 | // Translate generic IP packets. |
| 61 | int generic_packet(clat_packet out, int pos, const char *payload, size_t len); |
| 62 | |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 63 | // Translate TCP and UDP packets. |
| 64 | int tcp_packet(clat_packet out, int pos, const struct tcphdr *tcp, uint32_t checksum, size_t len); |
| 65 | int udp_packet(clat_packet out, int pos, const struct udphdr *udp, uint32_t checksum, size_t len); |
| 66 | |
| 67 | int tcp_translate(clat_packet out, int pos, const struct tcphdr *tcp, size_t header_size, |
| 68 | uint32_t checksum, const char *payload, size_t payload_size); |
| 69 | int udp_translate(clat_packet out, int pos, const struct udphdr *udp, uint32_t checksum, |
| 70 | const char *payload, size_t payload_size); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 71 | |
| 72 | #endif /* __TRANSLATE_H__ */ |