Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- tun0.c 2017-11-29 22:57:44.000000000 +0300
- +++ tun0_changeip.c 2017-12-04 17:36:04.000000000 +0300
- @@ -16,6 +16,47 @@
- #include <stdarg.h>
- #include <netinet/ip.h>
- #include <netinet/ip6.h>
- +/* for IPPROTO_XXX definitions */
- +#include <netinet/in.h>
- +/* for TCP header */
- +#include <netinet/tcp.h>
- +/* for UDP header */
- +#include <netinet/udp.h>
- +
- +/* the code snippet below is taken "as is" from the openvpn project, file src/openvpn/proto.h
- +/* it's used to correct internet checksums for IP, TCP and UDP headers after IP header modifications
- +/*
- + * The following macro is used to update an
- + * internet checksum. "acc" is a 32-bit
- + * accumulation of all the changes to the
- + * checksum (adding in old 16-bit words and
- + * subtracting out new words), and "cksum"
- + * is the checksum value to be updated.
- + */
- +#define ADJUST_CHECKSUM(acc, cksum) { \
- + int _acc = acc; \
- + _acc += (cksum); \
- + if (_acc < 0) { \
- + _acc = -_acc; \
- + _acc = (_acc >> 16) + (_acc & 0xffff); \
- + _acc += _acc >> 16; \
- + (cksum) = (uint16_t) ~_acc; \
- + } else { \
- + _acc = (_acc >> 16) + (_acc & 0xffff); \
- + _acc += _acc >> 16; \
- + (cksum) = (uint16_t) _acc; \
- + } \
- +}
- +
- +#define ADD_CHECKSUM_32(acc, u32) { \
- + acc += (u32) & 0xffff; \
- + acc += (u32) >> 16; \
- +}
- +
- +#define SUB_CHECKSUM_32(acc, u32) { \
- + acc -= (u32) & 0xffff; \
- + acc -= (u32) >> 16; \
- +}
- /* buffer for reading from tun/tap interface, must be >= 1500 */
- #define BUFSIZE 2000
- @@ -96,6 +137,13 @@
- set_if_flags(tun_name, IFF_UP);
- + /* For now just hardcode the source and replacement IP addresses */
- + struct in_addr tun_ip_input, tun_ip_output;
- + if (!inet_aton("10.0.0.1", &tun_ip_input) || !inet_aton("10.0.0.2", &tun_ip_output)) {
- + printf("inet_aton() failed!\n");
- + exit(1);
- + }
- +
- struct layer3_frame
- {
- uint16_t flags; // FLAGS from TUN
- @@ -135,14 +183,50 @@
- if (ipv == 4) {
- puts("PARSING V4!");
- - const struct ip* ippacket = (struct ip*)(l3p->payload);
- + struct ip* ippacket = (struct ip*)(l3p->payload);
- printf("VER:%d\n", ippacket->ip_v);
- struct in_addr ip_dst = ippacket->ip_dst;
- struct sockaddr_in sin;
- sin.sin_family = AF_INET;
- sin.sin_addr = ip_dst;
- - printf("The IP address is %s\n", inet_ntoa(ip_dst));
- + printf("The source IP address is %s\n", inet_ntoa(ippacket->ip_src));
- + printf("The destination IP address is %s\n", inet_ntoa(ippacket->ip_dst));
- +
- + /* Switch IP addresses */
- + int accumulate = 0;
- + if (ippacket->ip_src.s_addr == tun_ip_input.s_addr) {
- + /* pre-adjust IP checksum */
- + ADD_CHECKSUM_32(accumulate, ippacket->ip_src.s_addr);
- + /* change source IP */
- + ippacket->ip_src = tun_ip_output;
- + /* post-adjust IP checksum */
- + SUB_CHECKSUM_32(accumulate, ippacket->ip_src.s_addr);
- + }
- + else if (ippacket->ip_dst.s_addr == tun_ip_output.s_addr) {
- + /* pre-adjust IP checksum */
- + ADD_CHECKSUM_32(accumulate, ippacket->ip_dst.s_addr);
- + /* change destination IP */
- + ippacket->ip_dst = tun_ip_input;
- + /* post-adjust IP checksum */
- + SUB_CHECKSUM_32(accumulate, ippacket->ip_dst.s_addr);
- + }
- +
- + printf("New source IP address is %s\n", inet_ntoa(ippacket->ip_src));
- + printf("New destination IP address is %s\n", inet_ntoa(ippacket->ip_dst));
- +
- + /* Correct packet checksums */
- + ADJUST_CHECKSUM(accumulate, ippacket->ip_sum);
- + if (ippacket->ip_p == IPPROTO_TCP &&
- + nread >= sizeof(struct layer3_frame) + sizeof(struct ip) + sizeof(struct tcphdr)) {
- + struct tcphdr* tcp_hdr = (struct tcphdr*)(l3p->payload + sizeof(struct ip));
- + ADJUST_CHECKSUM(accumulate, tcp_hdr->check);
- + }
- + else if (ippacket->ip_p == IPPROTO_UDP &&
- + nread >= sizeof(struct layer3_frame) + sizeof(struct ip) + sizeof(struct udphdr)) {
- + struct udphdr* udp_hdr = (struct udphdr*)(l3p->payload + sizeof(struct ip));
- + ADJUST_CHECKSUM(accumulate, udp_hdr->check);
- + }
- /*
- int s = socket (AF_INET, SOCK_RAW, IPPROTO_RAW);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement