blob: e810a3b2542c70b63bf9730d96009ea6db754982 [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 *
Lorenzo Colittieb92f482019-01-04 14:59:11 +090016 * clatd.h - main routines used by clatd
Daniel Drowna45056e2012-03-23 10:42:54 -050017 */
18#ifndef __CLATD_H__
19#define __CLATD_H__
20
Maciej Żenczykowski8ab7e132021-02-03 17:15:41 -080021#include <signal.h>
Maciej Żenczykowski716518d2019-04-08 17:46:48 -070022#include <stdlib.h>
Lorenzo Colitti6b2007a2014-12-08 12:53:05 +090023#include <sys/uio.h>
24
Lorenzo Colittieb92f482019-01-04 14:59:11 +090025struct tun_data;
26
Maciej Żenczykowski97de3342023-01-18 17:36:22 +000027// IPv4 header has a u16 total length field, for maximum L3 mtu of 0xFFFF.
28//
29// Translating IPv4 to IPv6 requires removing the IPv4 header (20) and adding
30// an IPv6 header (40), possibly with an extra ipv6 fragment extension header (8).
31//
32// As such the maximum IPv4 L3 mtu size is 0xFFFF (by u16 tot_len field)
33// and the maximum IPv6 L3 mtu size is 0xFFFF + 28 (which is larger)
34//
35// A received non-jumbogram IPv6 frame could potentially be u16 payload_len = 0xFFFF
36// + sizeof ipv6 header = 40, bytes in size. But such a packet cannot be meaningfully
37// converted to IPv4 (it's too large). As such the restriction is the same: 0xFFFF + 28
38//
39// (since there's no jumbogram support in IPv4, IPv6 jumbograms cannot be meaningfully
40// converted to IPv4 anyway, and are thus entirely unsupported)
41//
42// We bump it by one more, since it makes truncation more obvious.
43// ie. if we ever read >= MAXMTU bytes we should discard.
44#define MAXMTU (0xFFFF + 28 + 1)
45#define PACKETLEN (sizeof(struct tun_pi) + MAXMTU)
Maciej Żenczykowski50303532020-06-02 14:46:45 -070046#define CLATD_VERSION "1.5"
Daniel Drowna45056e2012-03-23 10:42:54 -050047
Lorenzo Colittidce3ddf2014-08-25 16:07:12 -070048#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
49
Daniel Drowna45056e2012-03-23 10:42:54 -050050// how frequently (in seconds) to poll for an address change while traffic is passing
51#define INTERFACE_POLL_FREQUENCY 30
52
53// how frequently (in seconds) to poll for an address change while there is no traffic
54#define NO_TRAFFIC_INTERFACE_POLL_FREQUENCY 90
55
Maciej Żenczykowski8ab7e132021-02-03 17:15:41 -080056extern volatile sig_atomic_t running;
57
Lorenzo Colittieb92f482019-01-04 14:59:11 +090058int ipv6_address_changed(const char *interface);
Lorenzo Colittieb92f482019-01-04 14:59:11 +090059void event_loop(struct tun_data *tunnel);
Maciej Żenczykowski716518d2019-04-08 17:46:48 -070060
61/* function: parse_int
62 * parses a string as a decimal/hex/octal signed integer
63 * str - the string to parse
64 * out - the signed integer to write to, gets clobbered on failure
65 */
66static inline int parse_int(const char *str, int *out) {
67 char *end_ptr;
68 *out = strtol(str, &end_ptr, 0);
69 return *str && !*end_ptr;
70}
71
72/* function: parse_unsigned
73 * parses a string as a decimal/hex/octal unsigned integer
74 * str - the string to parse
75 * out - the unsigned integer to write to, gets clobbered on failure
76 */
77static inline int parse_unsigned(const char *str, unsigned *out) {
78 char *end_ptr;
79 *out = strtoul(str, &end_ptr, 0);
80 return *str && !*end_ptr;
81}
Lorenzo Colitti6b2007a2014-12-08 12:53:05 +090082
Daniel Drowna45056e2012-03-23 10:42:54 -050083#endif /* __CLATD_H__ */