Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 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 | * |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 16 | * clatd.h - main routines used by clatd |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 17 | */ |
| 18 | #ifndef __CLATD_H__ |
| 19 | #define __CLATD_H__ |
| 20 | |
Maciej Żenczykowski | 8ab7e13 | 2021-02-03 17:15:41 -0800 | [diff] [blame] | 21 | #include <signal.h> |
Maciej Żenczykowski | 716518d | 2019-04-08 17:46:48 -0700 | [diff] [blame] | 22 | #include <stdlib.h> |
Lorenzo Colitti | 6b2007a | 2014-12-08 12:53:05 +0900 | [diff] [blame] | 23 | #include <sys/uio.h> |
| 24 | |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 25 | struct tun_data; |
| 26 | |
Maciej Żenczykowski | 97de334 | 2023-01-18 17:36:22 +0000 | [diff] [blame] | 27 | // 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) |
Maciej Żenczykowski | f3eeff9 | 2023-03-13 21:50:01 +0000 | [diff] [blame] | 41 | #define MAXMTU (0xFFFF + 28) |
Maciej Żenczykowski | bea3f7f | 2023-01-30 22:29:59 +0000 | [diff] [blame] | 42 | |
Maciej Żenczykowski | f3eeff9 | 2023-03-13 21:50:01 +0000 | [diff] [blame] | 43 | // logcat_hexdump() maximum binary data length, this is the maximum packet size |
| 44 | // plus some extra space for various headers: |
| 45 | // struct tun_pi (4 bytes) |
| 46 | // struct virtio_net_hdr (10 bytes) |
| 47 | // ethernet (14 bytes), potentially including vlan tag (4) or tags (8 or 12) |
| 48 | // plus some extra just-in-case headroom, because it doesn't hurt. |
| 49 | #define MAXDUMPLEN (64 + MAXMTU) |
Maciej Żenczykowski | bea3f7f | 2023-01-30 22:29:59 +0000 | [diff] [blame] | 50 | |
Maciej Żenczykowski | d775fd7 | 2023-03-15 17:10:18 +0000 | [diff] [blame] | 51 | #define CLATD_VERSION "1.7" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 52 | |
Lorenzo Colitti | dce3ddf | 2014-08-25 16:07:12 -0700 | [diff] [blame] | 53 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 54 | |
Maciej Żenczykowski | 8ab7e13 | 2021-02-03 17:15:41 -0800 | [diff] [blame] | 55 | extern volatile sig_atomic_t running; |
| 56 | |
Lorenzo Colitti | eb92f48 | 2019-01-04 14:59:11 +0900 | [diff] [blame] | 57 | void event_loop(struct tun_data *tunnel); |
Maciej Żenczykowski | 716518d | 2019-04-08 17:46:48 -0700 | [diff] [blame] | 58 | |
| 59 | /* function: parse_int |
| 60 | * parses a string as a decimal/hex/octal signed integer |
| 61 | * str - the string to parse |
| 62 | * out - the signed integer to write to, gets clobbered on failure |
| 63 | */ |
| 64 | static inline int parse_int(const char *str, int *out) { |
| 65 | char *end_ptr; |
| 66 | *out = strtol(str, &end_ptr, 0); |
| 67 | return *str && !*end_ptr; |
| 68 | } |
| 69 | |
| 70 | /* function: parse_unsigned |
| 71 | * parses a string as a decimal/hex/octal unsigned integer |
| 72 | * str - the string to parse |
| 73 | * out - the unsigned integer to write to, gets clobbered on failure |
| 74 | */ |
| 75 | static inline int parse_unsigned(const char *str, unsigned *out) { |
| 76 | char *end_ptr; |
| 77 | *out = strtoul(str, &end_ptr, 0); |
| 78 | return *str && !*end_ptr; |
| 79 | } |
Lorenzo Colitti | 6b2007a | 2014-12-08 12:53:05 +0900 | [diff] [blame] | 80 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 81 | #endif /* __CLATD_H__ */ |