blob: 6f7c0ffe4326ed15075d03361b6c2c14359a893d [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"
Pau Espin Pedrola037e592017-10-16 14:41:37 +020026
27/* ipv6 ip type flags for tun_ipv6_local_get() */
28enum {
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 Welte63ebccd2017-08-02 21:10:09 +020036#ifndef HAVE_IPHDR
37struct 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 */
jjakoa7cd2492003-04-11 09:40:12 +000060
jjako52c24142002-12-16 13:33:51 +000061/* ***********************************************************
62 * Information storage for each tun instance
63 *************************************************************/
64
65struct tun_t {
Harald Weltebed35df2011-11-02 13:06:18 +010066 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 Welte881e97e2017-08-12 14:56:10 +020074 /* to be used by libgtp callers/users (to attach their own private state) */
75 void *priv;
jjako52c24142002-12-16 13:33:51 +000076};
77
Harald Weltedda21ed2017-08-12 15:07:02 +020078extern int tun_new(struct tun_t **tun, const char *dev_name);
jjakoa7cd2492003-04-11 09:40:12 +000079extern int tun_free(struct tun_t *tun);
80extern int tun_decaps(struct tun_t *this);
jjako52c24142002-12-16 13:33:51 +000081extern int tun_encaps(struct tun_t *tun, void *pack, unsigned len);
82
Pau Espin Pedrolf5e40b72017-12-14 14:01:23 +010083extern int tun_addaddr(struct tun_t *this, struct in46_addr *addr,
84 struct in46_addr *dstaddr, size_t prefixlen);
jjakoa7cd2492003-04-11 09:40:12 +000085
Harald Welte2e48a442017-08-03 00:47:03 +020086extern int tun_setaddr(struct tun_t *this, struct in46_addr *our_adr,
87 struct in46_addr *his_adr, size_t prefixlen);
jjakoa7cd2492003-04-11 09:40:12 +000088
Harald Weltebed35df2011-11-02 13:06:18 +010089int tun_addroute(struct tun_t *this, struct in_addr *dst,
jjakoa7cd2492003-04-11 09:40:12 +000090 struct in_addr *gateway, struct in_addr *mask);
91
Harald Weltebed35df2011-11-02 13:06:18 +010092extern int tun_set_cb_ind(struct tun_t *this,
93 int (*cb_ind) (struct tun_t * tun, void *pack,
94 unsigned len));
jjakoa7cd2492003-04-11 09:40:12 +000095
Harald Weltebed35df2011-11-02 13:06:18 +010096extern int tun_runscript(struct tun_t *tun, char *script);
jjakoa7cd2492003-04-11 09:40:12 +000097
Harald Welte4c7d2912017-11-08 15:19:17 +090098int netdev_ip_local_get(const char *devname, struct in46_prefix *prefix_list,
99 size_t prefix_size, int flags);
100
Pau Espin Pedrola037e592017-10-16 14:41:37 +0200101int tun_ip_local_get(const struct tun_t *tun, struct in46_prefix *prefix_list,
102 size_t prefix_size, int flags);
Harald Weltef85fe972017-09-24 20:00:34 +0800103
Harald Weltebed35df2011-11-02 13:06:18 +0100104#endif /* !_TUN_H */