blob: 421d2a5920148f540016b97d2a2bd0f7977abb1b [file] [log] [blame]
Daniel Drowna45056e2012-03-23 10:42:54 -05001/*
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 Colittif9390602014-02-13 12:53:35 +090021#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>
Lorenzo Colittid9084182013-03-22 00:42:21 +090029#include <linux/if_tun.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050030
Lorenzo Colittif9390602014-02-13 12:53:35 +090031#include "clatd.h"
32
Lorenzo Colittid9084182013-03-22 00:42:21 +090033#define MAX_TCP_HDR (15 * 4) // Data offset field is 4 bits and counts in 32-bit words.
Daniel Drowna45056e2012-03-23 10:42:54 -050034
Lorenzo Colittid9084182013-03-22 00:42:21 +090035// A clat_packet is an array of iovec structures representing a packet that we are translating.
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090036// The CLAT_POS_XXX constants represent the array indices within the clat_packet that contain
37// specific parts of the packet. The packet_* functions operate on all the packet segments past a
38// given position.
Lorenzo Colittia4454bf2014-02-25 09:27:31 +090039typedef enum {
Lorenzo Colitti57d480d2014-02-09 10:35:38 +090040 CLAT_POS_TUNHDR, CLAT_POS_IPHDR, CLAT_POS_FRAGHDR, CLAT_POS_TRANSPORTHDR,
41 CLAT_POS_ICMPERR_IPHDR, CLAT_POS_ICMPERR_FRAGHDR, CLAT_POS_ICMPERR_TRANSPORTHDR,
42 CLAT_POS_PAYLOAD, CLAT_POS_MAX
Lorenzo Colittia4454bf2014-02-25 09:27:31 +090043} clat_packet_index;
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090044typedef struct iovec clat_packet[CLAT_POS_MAX];
Lorenzo Colittid9084182013-03-22 00:42:21 +090045
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090046// Calculates the checksum over all the packet components starting from pos.
Lorenzo Colittia4454bf2014-02-25 09:27:31 +090047uint16_t packet_checksum(uint32_t checksum, clat_packet packet, clat_packet_index pos);
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090048
49// Returns the total length of the packet components after pos.
Lorenzo Colittia4454bf2014-02-25 09:27:31 +090050uint16_t packet_length(clat_packet packet, clat_packet_index pos);
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090051
52// Returns true iff the given IPv6 address is in the plat subnet.
53int is_in_plat_subnet(const struct in6_addr *addr6);
Lorenzo Colittid9084182013-03-22 00:42:21 +090054
55// Functions to create tun, IPv4, and IPv6 headers.
56void fill_tun_header(struct tun_pi *tun_header, uint16_t proto);
57void fill_ip_header(struct iphdr *ip_targ, uint16_t payload_len, uint8_t protocol,
58 const struct ip6_hdr *old_header);
59void fill_ip6_header(struct ip6_hdr *ip6, uint16_t payload_len, uint8_t protocol,
60 const struct iphdr *old_header);
61
Lorenzo Colittif9390602014-02-13 12:53:35 +090062// Translate and send packets.
Brian Carlstromfcac4102014-02-24 20:03:01 -080063void translate_packet(const struct tun_data *tunnel, struct tun_pi *tun_header,
64 const uint8_t *packet, size_t packetsize);
Lorenzo Colittif9390602014-02-13 12:53:35 +090065
Lorenzo Colitti07fc7612013-11-18 13:33:30 +090066// Translate IPv4 and IPv6 packets.
Lorenzo Colittia4454bf2014-02-25 09:27:31 +090067int ipv4_packet(clat_packet out, clat_packet_index pos, const uint8_t *packet, size_t len);
68int ipv6_packet(clat_packet out, clat_packet_index pos, const uint8_t *packet, size_t len);
Lorenzo Colitti07fc7612013-11-18 13:33:30 +090069
Lorenzo Colitti57d480d2014-02-09 10:35:38 +090070// Deal with fragmented packets.
71size_t maybe_fill_frag_header(struct ip6_frag *frag_hdr, struct ip6_hdr *ip6_targ,
72 const struct iphdr *old_header);
73uint8_t parse_frag_header(const struct ip6_frag *frag_hdr, struct iphdr *ip_targ);
74
Lorenzo Colittid9084182013-03-22 00:42:21 +090075// Translate ICMP packets.
Lorenzo Colittia4454bf2014-02-25 09:27:31 +090076int icmp_to_icmp6(clat_packet out, clat_packet_index pos, const struct icmphdr *icmp,
77 uint32_t checksum, const uint8_t *payload, size_t payload_size);
78int icmp6_to_icmp(clat_packet out, clat_packet_index pos, const struct icmp6_hdr *icmp6,
Brian Carlstromfcac4102014-02-24 20:03:01 -080079 const uint8_t *payload, size_t payload_size);
Lorenzo Colittid9084182013-03-22 00:42:21 +090080
Lorenzo Colittic9f4c892013-11-18 12:59:44 +090081// Translate generic IP packets.
Lorenzo Colittia4454bf2014-02-25 09:27:31 +090082int generic_packet(clat_packet out, clat_packet_index pos, const uint8_t *payload, size_t len);
Lorenzo Colittic9f4c892013-11-18 12:59:44 +090083
Lorenzo Colittid9084182013-03-22 00:42:21 +090084// Translate TCP and UDP packets.
Lorenzo Colittia4454bf2014-02-25 09:27:31 +090085int tcp_packet(clat_packet out, clat_packet_index pos, const struct tcphdr *tcp,
Lorenzo Colitti5a50c022014-02-10 09:20:05 +090086 uint32_t old_sum, uint32_t new_sum, size_t len);
Lorenzo Colittia4454bf2014-02-25 09:27:31 +090087int udp_packet(clat_packet out, clat_packet_index pos, const struct udphdr *udp,
Lorenzo Colitti5a50c022014-02-10 09:20:05 +090088 uint32_t old_sum, uint32_t new_sum, size_t len);
Lorenzo Colittid9084182013-03-22 00:42:21 +090089
Lorenzo Colittia4454bf2014-02-25 09:27:31 +090090int tcp_translate(clat_packet out, clat_packet_index pos, const struct tcphdr *tcp,
91 size_t header_size, uint32_t old_sum, uint32_t new_sum,
Brian Carlstromfcac4102014-02-24 20:03:01 -080092 const uint8_t *payload, size_t payload_size);
Lorenzo Colittia4454bf2014-02-25 09:27:31 +090093int udp_translate(clat_packet out, clat_packet_index pos, const struct udphdr *udp,
Brian Carlstromfcac4102014-02-24 20:03:01 -080094 uint32_t old_sum, uint32_t new_sum,
95 const uint8_t *payload, size_t payload_size);
Daniel Drowna45056e2012-03-23 10:42:54 -050096
97#endif /* __TRANSLATE_H__ */