blob: f63be507a93368d16b45b6b681811857a2a29b50 [file] [log] [blame]
Pau Espin Pedrolfdd732b2017-10-13 14:32:24 +02001/*
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>
Pau Espin Pedrolfdd732b2017-10-13 14:32:24 +02005 *
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.
Pau Espin Pedrolfdd732b2017-10-13 14:32:24 +020010 *
jjako52c24142002-12-16 13:33:51 +000011 */
12
13#ifndef _TUN_H
14#define _TUN_H
15
Harald Weltec55ece82017-08-12 15:09:08 +020016#include <net/if.h>
17
Harald Welte2e48a442017-08-03 00:47:03 +020018#include "../lib/in46_addr.h"
19
Harald Weltebed35df2011-11-02 13:06:18 +010020#define PACKET_MAX 8196 /* Maximum packet size we receive */
jjakoa7cd2492003-04-11 09:40:12 +000021#define TUN_SCRIPTSIZE 256
22#define TUN_ADDRSIZE 128
23#define TUN_NLBUFSIZE 1024
jjako52c24142002-12-16 13:33:51 +000024
Harald Welte63ebccd2017-08-02 21:10:09 +020025#include "config.h"
26#ifndef HAVE_IPHDR
27struct 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 */
jjakoa7cd2492003-04-11 09:40:12 +000050
jjako52c24142002-12-16 13:33:51 +000051/* ***********************************************************
52 * Information storage for each tun instance
53 *************************************************************/
54
55struct tun_t {
Harald Weltebed35df2011-11-02 13:06:18 +010056 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 Welte881e97e2017-08-12 14:56:10 +020064 /* to be used by libgtp callers/users (to attach their own private state) */
65 void *priv;
jjako52c24142002-12-16 13:33:51 +000066};
67
Harald Weltedda21ed2017-08-12 15:07:02 +020068extern int tun_new(struct tun_t **tun, const char *dev_name);
jjakoa7cd2492003-04-11 09:40:12 +000069extern int tun_free(struct tun_t *tun);
70extern int tun_decaps(struct tun_t *this);
jjako52c24142002-12-16 13:33:51 +000071extern int tun_encaps(struct tun_t *tun, void *pack, unsigned len);
72
jjakoa7cd2492003-04-11 09:40:12 +000073extern int tun_addaddr(struct tun_t *this, struct in_addr *addr,
74 struct in_addr *dstaddr, struct in_addr *netmask);
75
Harald Welte2e48a442017-08-03 00:47:03 +020076extern int tun_setaddr(struct tun_t *this, struct in46_addr *our_adr,
77 struct in46_addr *his_adr, size_t prefixlen);
jjakoa7cd2492003-04-11 09:40:12 +000078
Harald Weltebed35df2011-11-02 13:06:18 +010079int tun_addroute(struct tun_t *this, struct in_addr *dst,
jjakoa7cd2492003-04-11 09:40:12 +000080 struct in_addr *gateway, struct in_addr *mask);
81
Harald Weltebed35df2011-11-02 13:06:18 +010082extern int tun_set_cb_ind(struct tun_t *this,
83 int (*cb_ind) (struct tun_t * tun, void *pack,
84 unsigned len));
jjakoa7cd2492003-04-11 09:40:12 +000085
Harald Weltebed35df2011-11-02 13:06:18 +010086extern int tun_runscript(struct tun_t *tun, char *script);
jjakoa7cd2492003-04-11 09:40:12 +000087
Harald Weltef85fe972017-09-24 20:00:34 +080088int tun_ipv6_linklocal_get(const struct tun_t *tun, struct in6_addr *ia);
89
Harald Weltebed35df2011-11-02 13:06:18 +010090#endif /* !_TUN_H */