Home | History | Annotate | Download | only in android-clat
      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 
     21 #include <netinet/in.h>
     22 #include <netinet/ip.h>
     23 #include <netinet/ip_icmp.h>
     24 #include <netinet/udp.h>
     25 #include <netinet/tcp.h>
     26 #include <netinet/ip6.h>
     27 #include <netinet/icmp6.h>
     28 #include <linux/icmp.h>
     29 #include <linux/if_tun.h>
     30 
     31 #include "clatd.h"
     32 
     33 #define MAX_TCP_HDR (15 * 4)   // Data offset field is 4 bits and counts in 32-bit words.
     34 
     35 // Calculates the checksum over all the packet components starting from pos.
     36 uint16_t packet_checksum(uint32_t checksum, clat_packet packet, clat_packet_index pos);
     37 
     38 // Returns the total length of the packet components after pos.
     39 uint16_t packet_length(clat_packet packet, clat_packet_index pos);
     40 
     41 // Returns true iff the given IPv6 address is in the plat subnet.
     42 int is_in_plat_subnet(const struct in6_addr *addr6);
     43 
     44 // Functions to create tun, IPv4, and IPv6 headers.
     45 void fill_tun_header(struct tun_pi *tun_header, uint16_t proto);
     46 void fill_ip_header(struct iphdr *ip_targ, uint16_t payload_len, uint8_t protocol,
     47                     const struct ip6_hdr *old_header);
     48 void fill_ip6_header(struct ip6_hdr *ip6, uint16_t payload_len, uint8_t protocol,
     49                      const struct iphdr *old_header);
     50 
     51 // Translate and send packets.
     52 void translate_packet(int fd, int to_ipv6, const uint8_t *packet, size_t packetsize);
     53 
     54 // Translate IPv4 and IPv6 packets.
     55 int ipv4_packet(clat_packet out, clat_packet_index pos, const uint8_t *packet, size_t len);
     56 int ipv6_packet(clat_packet out, clat_packet_index pos, const uint8_t *packet, size_t len);
     57 
     58 // Deal with fragmented packets.
     59 size_t maybe_fill_frag_header(struct ip6_frag *frag_hdr, struct ip6_hdr *ip6_targ,
     60                               const struct iphdr *old_header);
     61 uint8_t parse_frag_header(const struct ip6_frag *frag_hdr, struct iphdr *ip_targ);
     62 
     63 // Deal with fragmented packets.
     64 size_t maybe_fill_frag_header(struct ip6_frag *frag_hdr, struct ip6_hdr *ip6_targ,
     65                               const struct iphdr *old_header);
     66 uint8_t parse_frag_header(const struct ip6_frag *frag_hdr, struct iphdr *ip_targ);
     67 
     68 // Translate ICMP packets.
     69 int icmp_to_icmp6(clat_packet out, clat_packet_index pos, const struct icmphdr *icmp,
     70                   uint32_t checksum, const uint8_t *payload, size_t payload_size);
     71 int icmp6_to_icmp(clat_packet out, clat_packet_index pos, const struct icmp6_hdr *icmp6,
     72                   const uint8_t *payload, size_t payload_size);
     73 
     74 // Translate generic IP packets.
     75 int generic_packet(clat_packet out, clat_packet_index pos, const uint8_t *payload, size_t len);
     76 
     77 // Translate TCP and UDP packets.
     78 int tcp_packet(clat_packet out, clat_packet_index pos, const struct tcphdr *tcp,
     79                uint32_t old_sum, uint32_t new_sum, size_t len);
     80 int udp_packet(clat_packet out, clat_packet_index pos, const struct udphdr *udp,
     81                uint32_t old_sum, uint32_t new_sum, size_t len);
     82 
     83 int tcp_translate(clat_packet out, clat_packet_index pos, const struct tcphdr *tcp,
     84                   size_t header_size, uint32_t old_sum, uint32_t new_sum,
     85                   const uint8_t *payload, size_t payload_size);
     86 int udp_translate(clat_packet out, clat_packet_index pos, const struct udphdr *udp,
     87                   uint32_t old_sum, uint32_t new_sum,
     88                   const uint8_t *payload, size_t payload_size);
     89 
     90 #endif /* __TRANSLATE_H__ */
     91