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" |
| 26 | #ifndef HAVE_IPHDR |
| 27 | struct iphdr |
| 28 | { |
| 29 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 30 | unsigned int ihl:4; |
| 31 | unsigned int version:4; |
| 32 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 33 | unsigned int version:4; |
| 34 | unsigned int ihl:4; |
| 35 | #else |
| 36 | # error "Please fix <bits/endian.h>" |
| 37 | #endif |
| 38 | u_int8_t tos; |
| 39 | u_int16_t tot_len; |
| 40 | u_int16_t id; |
| 41 | u_int16_t frag_off; |
| 42 | u_int8_t ttl; |
| 43 | u_int8_t protocol; |
| 44 | u_int16_t check; |
| 45 | u_int32_t saddr; |
| 46 | u_int32_t daddr; |
| 47 | /*The options start here. */ |
| 48 | }; |
| 49 | #endif /* !HAVE_IPHDR */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 50 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 51 | /* *********************************************************** |
| 52 | * Information storage for each tun instance |
| 53 | *************************************************************/ |
| 54 | |
| 55 | struct tun_t { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 56 | int fd; /* File descriptor to tun interface */ |
| 57 | struct in_addr addr; |
| 58 | struct in_addr dstaddr; |
| 59 | struct in_addr netmask; |
| 60 | int addrs; /* Number of allocated IP addresses */ |
| 61 | int routes; /* One if we allocated an automatic route */ |
| 62 | char devname[IFNAMSIZ]; /* Name of the tun device */ |
| 63 | int (*cb_ind) (struct tun_t * tun, void *pack, unsigned len); |
Harald Welte | 881e97e | 2017-08-12 14:56:10 +0200 | [diff] [blame] | 64 | /* to be used by libgtp callers/users (to attach their own private state) */ |
| 65 | void *priv; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 68 | extern int tun_new(struct tun_t **tun, const char *dev_name); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 69 | extern int tun_free(struct tun_t *tun); |
| 70 | extern int tun_decaps(struct tun_t *this); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 71 | extern int tun_encaps(struct tun_t *tun, void *pack, unsigned len); |
| 72 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 73 | extern int tun_addaddr(struct tun_t *this, struct in_addr *addr, |
| 74 | struct in_addr *dstaddr, struct in_addr *netmask); |
| 75 | |
Harald Welte | 2e48a44 | 2017-08-03 00:47:03 +0200 | [diff] [blame] | 76 | extern int tun_setaddr(struct tun_t *this, struct in46_addr *our_adr, |
| 77 | struct in46_addr *his_adr, size_t prefixlen); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 78 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 79 | int tun_addroute(struct tun_t *this, struct in_addr *dst, |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 80 | struct in_addr *gateway, struct in_addr *mask); |
| 81 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 82 | extern int tun_set_cb_ind(struct tun_t *this, |
| 83 | int (*cb_ind) (struct tun_t * tun, void *pack, |
| 84 | unsigned len)); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 85 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 86 | extern int tun_runscript(struct tun_t *tun, char *script); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 87 | |
Harald Welte | f85fe97 | 2017-09-24 20:00:34 +0800 | [diff] [blame] | 88 | int tun_ipv6_linklocal_get(const struct tun_t *tun, struct in6_addr *ia); |
| 89 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 90 | #endif /* !_TUN_H */ |