Fix for a DoS attack:
[tinc] / src / netutl.c
1 /*
2     netutl.c -- some supporting network utility code
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 #include "config.h"
21
22 #include <arpa/inet.h>
23 #include <netdb.h>
24 #include <netinet/in.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/socket.h>
29 #include <syslog.h>
30
31 #include <utils.h>
32 #include <xalloc.h>
33
34 #include "encr.h"
35 #include "net.h"
36 #include "netutl.h"
37
38 /*
39   look for a connection associated with the given vpn ip,
40   return its connection structure.
41   Skips connections that are not activated!
42 */
43 conn_list_t *lookup_conn(ip_t ip)
44 {
45   conn_list_t *p = conn_list;
46 cp
47   /* Exact match suggested by James B. MacLean */
48   for(p = conn_list; p != NULL; p = p->next)
49     if((ip  == p->vpn_ip) && p->active)
50       return p;
51   for(p = conn_list; p != NULL; p = p->next)
52     if(((ip & p->vpn_mask) == (p->vpn_ip & p->vpn_mask)) && p->active)
53       return p;
54 cp
55   return NULL;
56 }
57
58 /*
59   free a queue and all of its elements
60 */
61 void destroy_queue(packet_queue_t *pq)
62 {
63   queue_element_t *p, *q;
64 cp
65   for(p = pq->head; p != NULL; p = q)
66     {
67       q = p->next;
68       if(p->packet)
69         free(p->packet);
70       free(p);
71     }
72
73   free(pq);
74 cp
75 }
76
77 /*
78   free a conn_list_t element and all its pointers
79 */
80 void free_conn_element(conn_list_t *p)
81 {
82 cp
83   if(p->hostname)
84     free(p->hostname);
85   if(p->sq)
86     destroy_queue(p->sq);
87   if(p->rq)
88     destroy_queue(p->rq);
89   free_key(p->public_key);
90   free_key(p->key);
91   free(p);
92 cp
93 }
94
95 /*
96   remove all marked connections
97 */
98 void prune_conn_list(void)
99 {
100   conn_list_t *p, *prev = NULL, *next = NULL;
101 cp
102   for(p = conn_list; p != NULL; )
103     {
104       next = p->next;
105
106       if(p->status.remove)
107         {
108           if(prev)
109             prev->next = next;
110           else
111             conn_list = next;
112
113           free_conn_element(p);
114         }
115       else
116         prev = p;
117
118       p = next;
119     }
120 cp
121 }
122
123 /*
124   creates new conn_list element, and initializes it
125 */
126 conn_list_t *new_conn_list(void)
127 {
128   conn_list_t *p = xmalloc(sizeof(*p));
129 cp
130   /* initialise all those stupid pointers at once */
131   memset(p, '\0', sizeof(*p));
132   p->vpn_mask = (ip_t)(~0L); /* If this isn't done, it would be a
133                                 wastebucket for all packets with
134                                 unknown destination. */
135   p->nexthop = p;
136 cp
137   return p;
138 }
139
140 /*
141   free all elements of conn_list
142 */
143 void destroy_conn_list(void)
144 {
145   conn_list_t *p, *next;
146 cp
147   for(p = conn_list; p != NULL; )
148     {
149       next = p->next;
150       free_conn_element(p);
151       p = next;
152     }
153
154   conn_list = NULL;
155 cp
156 }
157
158 /*
159   look up the name associated with the ip
160   address `addr'
161 */
162 char *hostlookup(unsigned long addr)
163 {
164   char *name;
165   struct hostent *host = NULL;
166   struct in_addr in;
167 cp
168   in.s_addr = addr;
169
170   host = gethostbyaddr((char *)&in, sizeof(in), AF_INET);
171
172   if(host)
173     {
174       name = xmalloc(strlen(host->h_name)+20);
175       sprintf(name, "%s (%s)", host->h_name, inet_ntoa(in));
176     }
177   else
178     {
179       name = xmalloc(20);
180       sprintf(name, "%s", inet_ntoa(in));
181     }
182 cp
183   return name;
184 }
185
186 /*
187   Turn a string into an IP addy with netmask
188   return NULL on failure
189 */
190 ip_mask_t *strtoip(char *str)
191 {
192   ip_mask_t *ip;
193   int masker;
194   char *q, *p;
195   struct hostent *h;
196 cp
197   p = str;
198   if((q = strchr(p, '/')))
199     {
200       *q = '\0';
201       q++; /* q now points to netmask part, or NULL if no mask */
202     }
203
204   if(!(h = gethostbyname(p)))
205     {
206       fprintf(stderr, "Error looking up `%s': %s\n", p, sys_errlist[h_errno]);
207       return NULL;
208     }
209
210   masker = 0;
211   if(q)
212     {
213       masker = strtol(q, &p, 10);
214       if(q == p || (*p))
215         return NULL;
216     }
217
218   ip = xmalloc(sizeof(*ip));
219   ip->ip = ntohl(*((ip_t*)(h->h_addr_list[0])));
220
221   ip->mask = masker ? ~((1 << (32 - masker)) - 1) : 0;
222 cp
223   return ip;
224 }
225
226 void dump_conn_list(void)
227 {
228   conn_list_t *p;
229 cp
230   syslog(LOG_DEBUG, "Connection list:");
231
232   for(p = conn_list; p != NULL; p = p->next)
233     {
234       syslog(LOG_DEBUG, " " IP_ADDR_S "/" IP_ADDR_S ": %04x (%d|%d)",
235              IP_ADDR_V(p->vpn_ip), IP_ADDR_V(p->vpn_mask), p->status,
236              p->socket, p->meta_socket);
237     }
238 cp
239 }