Pau Espin Pedrol | fdd732b | 2017-10-13 14:32:24 +0200 | [diff] [blame] | 1 | /* |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 2 | * TUN interface functions. |
| 3 | * Copyright (C) 2002, 2003 Mondru AB. |
Harald Welte | 2e48a44 | 2017-08-03 00:47:03 +0200 | [diff] [blame] | 4 | * Copyright (C) 2017 by Harald Welte <laforge@gnumonks.org> |
Pau Espin Pedrol | fdd732b | 2017-10-13 14:32:24 +0200 | [diff] [blame] | 5 | * |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 6 | * The contents of this file may be used under the terms of the GNU |
| 7 | * General Public License Version 2, provided that the above copyright |
| 8 | * notice and this permission notice is included in all copies or |
| 9 | * substantial portions of the software. |
Pau Espin Pedrol | fdd732b | 2017-10-13 14:32:24 +0200 | [diff] [blame] | 10 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #ifndef _TUN_H |
| 14 | #define _TUN_H |
| 15 | |
Harald Welte | c55ece8 | 2017-08-12 15:09:08 +0200 | [diff] [blame] | 16 | #include <net/if.h> |
| 17 | |
Harald Welte | 2e48a44 | 2017-08-03 00:47:03 +0200 | [diff] [blame] | 18 | #include "../lib/in46_addr.h" |
| 19 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 20 | #define PACKET_MAX 8196 /* Maximum packet size we receive */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 21 | #define TUN_SCRIPTSIZE 256 |
| 22 | #define TUN_ADDRSIZE 128 |
| 23 | #define TUN_NLBUFSIZE 1024 |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 24 | |
Harald Welte | 63ebccd | 2017-08-02 21:10:09 +0200 | [diff] [blame] | 25 | #include "config.h" |
Pau Espin Pedrol | a037e59 | 2017-10-16 14:41:37 +0200 | [diff] [blame] | 26 | |
| 27 | /* ipv6 ip type flags for tun_ipv6_local_get() */ |
| 28 | enum { |
| 29 | IP_TYPE_IPv4 = 1, |
| 30 | IP_TYPE_IPv6_LINK = 2, |
| 31 | IP_TYPE_IPv6_NONLINK = 4, |
| 32 | }; |
| 33 | #define IP_TYPE_IPv6 (IP_TYPE_IPv6_LINK | IP_TYPE_IPv6_NONLINK) |
| 34 | |
| 35 | |
Harald Welte | 63ebccd | 2017-08-02 21:10:09 +0200 | [diff] [blame] | 36 | #ifndef HAVE_IPHDR |
| 37 | struct iphdr |
| 38 | { |
| 39 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 40 | unsigned int ihl:4; |
| 41 | unsigned int version:4; |
| 42 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 43 | unsigned int version:4; |
| 44 | unsigned int ihl:4; |
| 45 | #else |
| 46 | # error "Please fix <bits/endian.h>" |
| 47 | #endif |
| 48 | u_int8_t tos; |
| 49 | u_int16_t tot_len; |
| 50 | u_int16_t id; |
| 51 | u_int16_t frag_off; |
| 52 | u_int8_t ttl; |
| 53 | u_int8_t protocol; |
| 54 | u_int16_t check; |
| 55 | u_int32_t saddr; |
| 56 | u_int32_t daddr; |
| 57 | /*The options start here. */ |
| 58 | }; |
| 59 | #endif /* !HAVE_IPHDR */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 60 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 61 | /* *********************************************************** |
| 62 | * Information storage for each tun instance |
| 63 | *************************************************************/ |
| 64 | |
| 65 | struct tun_t { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 66 | int fd; /* File descriptor to tun interface */ |
| 67 | struct in_addr addr; |
| 68 | struct in_addr dstaddr; |
| 69 | struct in_addr netmask; |
| 70 | int addrs; /* Number of allocated IP addresses */ |
| 71 | int routes; /* One if we allocated an automatic route */ |
| 72 | char devname[IFNAMSIZ]; /* Name of the tun device */ |
| 73 | int (*cb_ind) (struct tun_t * tun, void *pack, unsigned len); |
Harald Welte | 881e97e | 2017-08-12 14:56:10 +0200 | [diff] [blame] | 74 | /* to be used by libgtp callers/users (to attach their own private state) */ |
| 75 | void *priv; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 76 | }; |
| 77 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 78 | extern int tun_new(struct tun_t **tun, const char *dev_name); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 79 | extern int tun_free(struct tun_t *tun); |
| 80 | extern int tun_decaps(struct tun_t *this); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 81 | extern int tun_encaps(struct tun_t *tun, void *pack, unsigned len); |
| 82 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 83 | extern int tun_addaddr(struct tun_t *this, struct in_addr *addr, |
| 84 | struct in_addr *dstaddr, struct in_addr *netmask); |
| 85 | |
Harald Welte | 2e48a44 | 2017-08-03 00:47:03 +0200 | [diff] [blame] | 86 | extern int tun_setaddr(struct tun_t *this, struct in46_addr *our_adr, |
| 87 | struct in46_addr *his_adr, size_t prefixlen); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 88 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 89 | int tun_addroute(struct tun_t *this, struct in_addr *dst, |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 90 | struct in_addr *gateway, struct in_addr *mask); |
| 91 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 92 | extern int tun_set_cb_ind(struct tun_t *this, |
| 93 | int (*cb_ind) (struct tun_t * tun, void *pack, |
| 94 | unsigned len)); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 95 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 96 | extern int tun_runscript(struct tun_t *tun, char *script); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 97 | |
Pau Espin Pedrol | a037e59 | 2017-10-16 14:41:37 +0200 | [diff] [blame] | 98 | int tun_ip_local_get(const struct tun_t *tun, struct in46_prefix *prefix_list, |
| 99 | size_t prefix_size, int flags); |
Harald Welte | f85fe97 | 2017-09-24 20:00:34 +0800 | [diff] [blame] | 100 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 101 | #endif /* !_TUN_H */ |