blob: 3378254af8e860a8fdac6a36a0fe20e17b6e02f3 [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.
39enum clat_packet_index { CLAT_POS_TUNHDR, CLAT_POS_IPHDR, CLAT_POS_TRANSPORTHDR,
Lorenzo Colitticd70b352013-04-10 12:24:56 +090040 CLAT_POS_ICMPERR_IPHDR, CLAT_POS_ICMPERR_TRANSPORTHDR,
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090041 CLAT_POS_PAYLOAD, CLAT_POS_MAX };
42typedef struct iovec clat_packet[CLAT_POS_MAX];
Lorenzo Colittid9084182013-03-22 00:42:21 +090043
Lorenzo Colittiee80ca62013-04-10 16:52:22 +090044// Calculates the checksum over all the packet components starting from pos.
45uint16_t packet_checksum(uint32_t checksum, clat_packet packet, int pos);
46
47// Returns the total length of the packet components after pos.
48uint16_t packet_length(clat_packet packet, int pos);
49
50// Returns true iff the given IPv6 address is in the plat subnet.
51int is_in_plat_subnet(const struct in6_addr *addr6);
Lorenzo Colittid9084182013-03-22 00:42:21 +090052
53// Functions to create tun, IPv4, and IPv6 headers.
54void fill_tun_header(struct tun_pi *tun_header, uint16_t proto);
55void fill_ip_header(struct iphdr *ip_targ, uint16_t payload_len, uint8_t protocol,
56 const struct ip6_hdr *old_header);
57void fill_ip6_header(struct ip6_hdr *ip6, uint16_t payload_len, uint8_t protocol,
58 const struct iphdr *old_header);
59
Lorenzo Colittif9390602014-02-13 12:53:35 +090060// Translate and send packets.
61void translate_packet(const struct tun_data *tunnel, struct tun_pi *tun_header, const char *packet,
62 size_t packetsize);
63
Lorenzo Colitti07fc7612013-11-18 13:33:30 +090064// Translate IPv4 and IPv6 packets.
65int ipv4_packet(clat_packet out, int pos, const char *packet, size_t len);
66int ipv6_packet(clat_packet out, int pos, const char *packet, size_t len);
67
Lorenzo Colittid9084182013-03-22 00:42:21 +090068// Translate ICMP packets.
69int icmp_to_icmp6(clat_packet out, int pos, const struct icmphdr *icmp, uint32_t checksum,
70 const char *payload, size_t payload_size);
Lorenzo Colitti9477a462013-11-18 15:56:02 +090071int icmp6_to_icmp(clat_packet out, int pos, const struct icmp6_hdr *icmp6,
Lorenzo Colittid9084182013-03-22 00:42:21 +090072 const char *payload, size_t payload_size);
73
Lorenzo Colittic9f4c892013-11-18 12:59:44 +090074// Translate generic IP packets.
75int generic_packet(clat_packet out, int pos, const char *payload, size_t len);
76
Lorenzo Colittid9084182013-03-22 00:42:21 +090077// Translate TCP and UDP packets.
Lorenzo Colitti5a50c022014-02-10 09:20:05 +090078int tcp_packet(clat_packet out, int pos, const struct tcphdr *tcp,
79 uint32_t old_sum, uint32_t new_sum, size_t len);
80int udp_packet(clat_packet out, int pos, const struct udphdr *udp,
81 uint32_t old_sum, uint32_t new_sum, size_t len);
Lorenzo Colittid9084182013-03-22 00:42:21 +090082
83int tcp_translate(clat_packet out, int pos, const struct tcphdr *tcp, size_t header_size,
Lorenzo Colitti5a50c022014-02-10 09:20:05 +090084 uint32_t old_sum, uint32_t new_sum, const char *payload, size_t payload_size);
85int udp_translate(clat_packet out, int pos, const struct udphdr *udp,
86 uint32_t old_sum, uint32_t new_sum, const char *payload, size_t payload_size);
Daniel Drowna45056e2012-03-23 10:42:54 -050087
88#endif /* __TRANSLATE_H__ */