Fix for a DoS attack:
[tinc] / src / net.h
1 /*
2     net.h -- header for net.c
3     Copyright (C) 1998,1999,2000 Ivo Timmermans <zarq@iname.com>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef __TINC_NET_H__
21 #define __TINC_NET_H__
22
23 #include <sys/time.h>
24
25 #include "config.h"
26 #include "conf.h"
27
28 #define MAXSIZE 1700  /* should be a bit more than the MTU for the tapdevice */
29 #define MTU 1600
30
31 #define MAC_ADDR_S "%02x:%02x:%02x:%02x:%02x:%02x"
32 #define MAC_ADDR_V(x) ((unsigned char*)&(x))[0],((unsigned char*)&(x))[1], \
33                       ((unsigned char*)&(x))[2],((unsigned char*)&(x))[3], \
34                       ((unsigned char*)&(x))[4],((unsigned char*)&(x))[5]
35
36 #define IP_ADDR_S "%d.%d.%d.%d"
37
38 #ifdef WORDS_BIGENDIAN
39 # define IP_ADDR_V(x) ((unsigned char*)&(x))[0],((unsigned char*)&(x))[1], \
40                       ((unsigned char*)&(x))[2],((unsigned char*)&(x))[3]
41 #else
42 # define IP_ADDR_V(x) ((unsigned char*)&(x))[3],((unsigned char*)&(x))[2], \
43                       ((unsigned char*)&(x))[1],((unsigned char*)&(x))[0]
44 #endif
45
46 #define MAXBUFSIZE 2048 /* Probably way too much, but it must fit every possible request. */
47
48 typedef unsigned long ip_t;
49 typedef short length_t;
50
51 typedef struct vpn_packet_t {
52   length_t len;         /* the actual number of bytes in the `data' field */
53   unsigned char data[MAXSIZE];
54 } vpn_packet_t;
55
56 typedef struct real_packet_t {
57   length_t len;         /* the length of the entire packet */
58   ip_t from;            /* where the packet came from */
59   vpn_packet_t data;    /* encrypted vpn_packet_t */
60 } real_packet_t;
61
62 typedef struct passphrase_t {
63   unsigned short len;
64   unsigned char *phrase;
65 } passphrase_t;
66
67 typedef struct status_bits_t {
68   int pinged:1;                    /* sent ping */
69   int got_pong:1;                  /* received pong */
70   int meta:1;                      /* meta connection exists */
71   int active:1;                    /* 1 if active.. */
72   int outgoing:1;                  /* I myself asked for this conn */
73   int termreq:1;                   /* the termination of this connection was requested */
74   int remove:1;                    /* Set to 1 if you want this connection removed */
75   int timeout:1;                   /* 1 if gotten timeout */
76   int validkey:1;                  /* 1 if we currently have a valid key for him */
77   int waitingforkey:1;             /* 1 if we already sent out a request */
78   int dataopen:1;                  /* 1 if we have a valid UDP connection open */
79   int unused:22;
80 } status_bits_t;
81
82 typedef struct queue_element_t {
83   void *packet;
84   struct queue_element_t *prev;
85   struct queue_element_t *next;
86 } queue_element_t;
87
88 typedef struct packet_queue_t {
89   queue_element_t *head;
90   queue_element_t *tail;
91 } packet_queue_t;
92
93 typedef struct enc_key_t {
94   int length;
95   char *key;
96   time_t expiry;
97 } enc_key_t;
98
99 typedef struct conn_list_t {
100   ip_t vpn_ip;                     /* his vpn ip */
101   ip_t vpn_mask;                   /* his vpn network address */
102   ip_t real_ip;                    /* his real (internet) ip */
103   char *hostname;                  /* the hostname of its real ip */
104   short unsigned int port;         /* his portnumber */
105   int socket;                      /* our udp vpn socket */
106   int meta_socket;                 /* our tcp meta socket */
107   int protocol_version;            /* used protocol */
108   status_bits_t status;            /* status info */
109   passphrase_t *pp;                /* encoded passphrase */
110   packet_queue_t *sq;              /* pending outgoing packets */
111   packet_queue_t *rq;              /* pending incoming packets (they have no
112                                       valid key to be decrypted with) */
113   enc_key_t *public_key;           /* the other party's public key */
114   enc_key_t *key;                  /* encrypt with this key */
115   char buffer[MAXBUFSIZE+1];       /* metadata input buffer */
116   int buflen;                      /* bytes read into buffer */
117   int reqlen;                      /* length of first request in buffer */
118   time_t last_ping_time;           /* last time we saw some activity from the other end */  
119   int want_ping;                   /* 0 if there's no need to check for activity */
120   struct conn_list_t *nexthop;     /* nearest meta-hop in this direction */
121   struct conn_list_t *next;        /* after all, it's a list of connections */
122 } conn_list_t;
123
124 extern int tap_fd;
125
126 extern int total_tap_in;
127 extern int total_tap_out;
128 extern int total_socket_in;
129 extern int total_socket_out;
130
131 extern conn_list_t *conn_list;
132 extern conn_list_t *myself;
133
134 extern int send_packet(ip_t, vpn_packet_t *);
135 extern int setup_network_connections(void);
136 extern void close_network_connections(void);
137 extern void main_loop(void);
138 extern int setup_vpn_connection(conn_list_t *);
139 extern void terminate_connection(conn_list_t *);
140 extern void flush_queues(conn_list_t*);
141
142 #endif /* __TINC_NET_H__ */