blob: 1cd0767960c01f60061cae69a01a20a137c70ad9 [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
jjakoa7cd2492003-04-11 09:40:12 +00002 * TUN interface functions.
3 * Copyright (C) 2002, 2003 Mondru AB.
Harald Welte2e48a442017-08-03 00:47:03 +02004 * Copyright (C) 2017 by Harald Welte <laforge@gnumonks.org>
jjako52c24142002-12-16 13:33:51 +00005 *
jjakoa7cd2492003-04-11 09:40:12 +00006 * 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.
jjako52c24142002-12-16 13:33:51 +000010 *
jjako52c24142002-12-16 13:33:51 +000011 */
12
13#ifndef _TUN_H
14#define _TUN_H
15
Harald Welte2e48a442017-08-03 00:47:03 +020016#include "../lib/in46_addr.h"
17
Harald Weltebed35df2011-11-02 13:06:18 +010018#define PACKET_MAX 8196 /* Maximum packet size we receive */
jjakoa7cd2492003-04-11 09:40:12 +000019#define TUN_SCRIPTSIZE 256
20#define TUN_ADDRSIZE 128
21#define TUN_NLBUFSIZE 1024
jjako52c24142002-12-16 13:33:51 +000022
Harald Welte63ebccd2017-08-02 21:10:09 +020023#include "config.h"
24#ifndef HAVE_IPHDR
25struct iphdr
26 {
27#if __BYTE_ORDER == __LITTLE_ENDIAN
28 unsigned int ihl:4;
29 unsigned int version:4;
30#elif __BYTE_ORDER == __BIG_ENDIAN
31 unsigned int version:4;
32 unsigned int ihl:4;
33#else
34# error "Please fix <bits/endian.h>"
35#endif
36 u_int8_t tos;
37 u_int16_t tot_len;
38 u_int16_t id;
39 u_int16_t frag_off;
40 u_int8_t ttl;
41 u_int8_t protocol;
42 u_int16_t check;
43 u_int32_t saddr;
44 u_int32_t daddr;
45 /*The options start here. */
46 };
47#endif /* !HAVE_IPHDR */
jjakoa7cd2492003-04-11 09:40:12 +000048
jjako52c24142002-12-16 13:33:51 +000049/* ***********************************************************
50 * Information storage for each tun instance
51 *************************************************************/
52
53struct tun_t {
Harald Weltebed35df2011-11-02 13:06:18 +010054 int fd; /* File descriptor to tun interface */
55 struct in_addr addr;
56 struct in_addr dstaddr;
57 struct in_addr netmask;
58 int addrs; /* Number of allocated IP addresses */
59 int routes; /* One if we allocated an automatic route */
60 char devname[IFNAMSIZ]; /* Name of the tun device */
61 int (*cb_ind) (struct tun_t * tun, void *pack, unsigned len);
jjako52c24142002-12-16 13:33:51 +000062};
63
jjakoa7cd2492003-04-11 09:40:12 +000064extern int tun_new(struct tun_t **tun);
65extern int tun_free(struct tun_t *tun);
66extern int tun_decaps(struct tun_t *this);
jjako52c24142002-12-16 13:33:51 +000067extern int tun_encaps(struct tun_t *tun, void *pack, unsigned len);
68
jjakoa7cd2492003-04-11 09:40:12 +000069extern int tun_addaddr(struct tun_t *this, struct in_addr *addr,
70 struct in_addr *dstaddr, struct in_addr *netmask);
71
Harald Welte2e48a442017-08-03 00:47:03 +020072extern int tun_setaddr(struct tun_t *this, struct in46_addr *our_adr,
73 struct in46_addr *his_adr, size_t prefixlen);
jjakoa7cd2492003-04-11 09:40:12 +000074
Harald Weltebed35df2011-11-02 13:06:18 +010075int tun_addroute(struct tun_t *this, struct in_addr *dst,
jjakoa7cd2492003-04-11 09:40:12 +000076 struct in_addr *gateway, struct in_addr *mask);
77
Harald Weltebed35df2011-11-02 13:06:18 +010078extern int tun_set_cb_ind(struct tun_t *this,
79 int (*cb_ind) (struct tun_t * tun, void *pack,
80 unsigned len));
jjakoa7cd2492003-04-11 09:40:12 +000081
Harald Weltebed35df2011-11-02 13:06:18 +010082extern int tun_runscript(struct tun_t *tun, char *script);
jjakoa7cd2492003-04-11 09:40:12 +000083
Harald Weltebed35df2011-11-02 13:06:18 +010084#endif /* !_TUN_H */