~ubuntu-branches/ubuntu/saucy/openvpn/saucy-proposed

« back to all changes in this revision

Viewing changes to socket.h

  • Committer: Package Import Robot
  • Author(s): Stéphane Graber
  • Date: 2013-05-24 17:42:45 UTC
  • mfrom: (1.1.19) (10.2.22 sid)
  • Revision ID: package-import@ubuntu.com-20130524174245-g9y6wlforycufqy5
Tags: 2.3.1-2ubuntu1
* Merge from Debian unstable. Remaining changes:
  - debian/openvpn.init.d:
    + Do not use start-stop-daemon and </dev/null to avoid blocking boot.
    + Show per-VPN result messages.
    + Add "--script-security 2" by default for backwards compatabliity.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  OpenVPN -- An application to securely tunnel IP networks
3
 
 *             over a single TCP/UDP port, with support for SSL/TLS-based
4
 
 *             session authentication and key exchange,
5
 
 *             packet encryption, packet authentication, and
6
 
 *             packet compression.
7
 
 *
8
 
 *  Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
9
 
 *
10
 
 *  This program is free software; you can redistribute it and/or modify
11
 
 *  it under the terms of the GNU General Public License version 2
12
 
 *  as published by the Free Software Foundation.
13
 
 *
14
 
 *  This program is distributed in the hope that it will be useful,
15
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
 *  GNU General Public License for more details.
18
 
 *
19
 
 *  You should have received a copy of the GNU General Public License
20
 
 *  along with this program (see the file COPYING included with this
21
 
 *  distribution); if not, write to the Free Software Foundation, Inc.,
22
 
 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 
 */
24
 
 
25
 
#ifndef SOCKET_H
26
 
#define SOCKET_H
27
 
 
28
 
#include "buffer.h"
29
 
#include "common.h"
30
 
#include "error.h"
31
 
#include "proto.h"
32
 
#include "mtu.h"
33
 
#include "win32.h"
34
 
#include "event.h"
35
 
#include "proxy.h"
36
 
#include "socks.h"
37
 
#include "misc.h"
38
 
 
39
 
/*
40
 
 * OpenVPN's default port number as assigned by IANA.
41
 
 */
42
 
#define OPENVPN_PORT 1194
43
 
 
44
 
/*
45
 
 * Maximum size passed passed to setsockopt SNDBUF/RCVBUF
46
 
 */
47
 
#define SOCKET_SND_RCV_BUF_MAX 1000000
48
 
 
49
 
/*
50
 
 * Number of seconds that "resolv-retry infinite"
51
 
 * represents.
52
 
 */
53
 
#define RESOLV_RETRY_INFINITE 1000000000
54
 
 
55
 
/* 
56
 
 * packet_size_type is used to communicate packet size
57
 
 * over the wire when stream oriented protocols are
58
 
 * being used
59
 
 */
60
 
 
61
 
typedef uint16_t packet_size_type;
62
 
 
63
 
/* convert a packet_size_type from host to network order */
64
 
#define htonps(x) htons(x)
65
 
 
66
 
/* convert a packet_size_type from network to host order */
67
 
#define ntohps(x) ntohs(x)
68
 
 
69
 
/* OpenVPN sockaddr struct */
70
 
struct openvpn_sockaddr
71
 
{
72
 
  /*int dummy;*/ /* add offset to force a bug if sa not explicitly dereferenced */
73
 
  union {
74
 
    struct sockaddr sa;
75
 
    struct sockaddr_in in4;
76
 
#ifdef USE_PF_INET6
77
 
    struct sockaddr_in6 in6;
78
 
#endif
79
 
  } addr;
80
 
};
81
 
 
82
 
/* actual address of remote, based on source address of received packets */
83
 
struct link_socket_actual
84
 
{
85
 
  /*int dummy;*/ /* add offset to force a bug if dest not explicitly dereferenced */
86
 
  struct openvpn_sockaddr dest;
87
 
#if ENABLE_IP_PKTINFO
88
 
  union {
89
 
#ifdef HAVE_IN_PKTINFO
90
 
    struct in_pktinfo in4;
91
 
#endif
92
 
#ifdef IP_RECVDSTADDR
93
 
    struct in_addr in4;
94
 
#endif
95
 
#ifdef USE_PF_INET6
96
 
    struct in6_pktinfo in6;
97
 
#endif
98
 
  } pi;
99
 
#endif
100
 
};
101
 
 
102
 
/* IP addresses which are persistant across SIGUSR1s */
103
 
struct link_socket_addr
104
 
{
105
 
  struct openvpn_sockaddr local;
106
 
  struct openvpn_sockaddr remote;   /* initial remote */
107
 
  struct link_socket_actual actual; /* reply to this address */
108
 
};
109
 
 
110
 
struct link_socket_info
111
 
{
112
 
  struct link_socket_addr *lsa;
113
 
  bool connection_established;
114
 
  const char *ipchange_command;
115
 
  const struct plugin_list *plugins;
116
 
  bool remote_float;  
117
 
  int proto;                    /* Protocol (PROTO_x defined below) */
118
 
  int mtu_changed;              /* Set to true when mtu value is changed */
119
 
};
120
 
 
121
 
/*
122
 
 * Used to extract packets encapsulated in streams into a buffer,
123
 
 * in this case IP packets embedded in a TCP stream.
124
 
 */
125
 
struct stream_buf
126
 
{
127
 
  struct buffer buf_init;
128
 
  struct buffer residual;
129
 
  int maxlen;
130
 
  bool residual_fully_formed;
131
 
 
132
 
  struct buffer buf;
133
 
  struct buffer next;
134
 
  int len;     /* -1 if not yet known */
135
 
 
136
 
  bool error;  /* if true, fatal TCP error has occurred,
137
 
                  requiring that connection be restarted */
138
 
#if PORT_SHARE
139
 
# define PS_DISABLED 0
140
 
# define PS_ENABLED  1
141
 
# define PS_FOREIGN  2
142
 
  int port_share_state;
143
 
#endif
144
 
};
145
 
 
146
 
/*
147
 
 * Used to set socket buffer sizes
148
 
 */
149
 
struct socket_buffer_size
150
 
{
151
 
  int rcvbuf;
152
 
  int sndbuf;
153
 
};
154
 
 
155
 
/*
156
 
 * This is the main socket structure used by OpenVPN.  The SOCKET_
157
 
 * defines try to abstract away our implementation differences between
158
 
 * using sockets on Posix vs. Win32.
159
 
 */
160
 
struct link_socket
161
 
{
162
 
  struct link_socket_info info;
163
 
 
164
 
  socket_descriptor_t sd;
165
 
 
166
 
#ifdef ENABLE_SOCKS
167
 
  socket_descriptor_t ctrl_sd;  /* only used for UDP over Socks */
168
 
#endif
169
 
 
170
 
#ifdef WIN32
171
 
  struct overlapped_io reads;
172
 
  struct overlapped_io writes;
173
 
  struct rw_handle rw_handle;
174
 
  struct rw_handle listen_handle; /* For listening on TCP socket in server mode */
175
 
#endif
176
 
 
177
 
  /* used for printing status info only */
178
 
  unsigned int rwflags_debug;
179
 
 
180
 
  /* used for long-term queueing of pre-accepted socket listen */
181
 
  bool listen_persistent_queued;
182
 
 
183
 
  /* Does config file contain any <connection> ... </connection> blocks? */
184
 
  bool connection_profiles_defined;
185
 
 
186
 
  const char *remote_host;
187
 
  int remote_port;
188
 
  const char *local_host;
189
 
  int local_port;
190
 
  bool bind_local;
191
 
 
192
 
# define INETD_NONE   0
193
 
# define INETD_WAIT   1
194
 
# define INETD_NOWAIT 2
195
 
  int inetd;
196
 
 
197
 
# define LS_MODE_DEFAULT           0
198
 
# define LS_MODE_TCP_LISTEN        1
199
 
# define LS_MODE_TCP_ACCEPT_FROM   2
200
 
  int mode;
201
 
 
202
 
  int resolve_retry_seconds;
203
 
  int connect_retry_seconds;
204
 
  int connect_timeout;
205
 
  int connect_retry_max;
206
 
  int mtu_discover_type;
207
 
 
208
 
  struct socket_buffer_size socket_buffer_sizes;
209
 
 
210
 
  int mtu;                      /* OS discovered MTU, or 0 if unknown */
211
 
 
212
 
  bool did_resolve_remote;
213
 
 
214
 
# define SF_USE_IP_PKTINFO (1<<0)
215
 
# define SF_TCP_NODELAY (1<<1)
216
 
# define SF_PORT_SHARE (1<<2)
217
 
# define SF_HOST_RANDOMIZE (1<<3)
218
 
# define SF_GETADDRINFO_DGRAM (1<<4)
219
 
  unsigned int sockflags;
220
 
 
221
 
  /* for stream sockets */
222
 
  struct stream_buf stream_buf;
223
 
  struct buffer stream_buf_data;
224
 
  bool stream_reset;
225
 
 
226
 
#ifdef ENABLE_HTTP_PROXY
227
 
  /* HTTP proxy */
228
 
  struct http_proxy_info *http_proxy;
229
 
#endif
230
 
 
231
 
#ifdef ENABLE_SOCKS
232
 
  /* Socks proxy */
233
 
  struct socks_proxy_info *socks_proxy;
234
 
  struct link_socket_actual socks_relay; /* Socks UDP relay address */
235
 
#endif
236
 
 
237
 
#if defined(ENABLE_HTTP_PROXY) || defined(ENABLE_SOCKS)
238
 
  /* The OpenVPN server we will use the proxy to connect to */
239
 
  const char *proxy_dest_host;
240
 
  int proxy_dest_port;
241
 
#endif
242
 
 
243
 
#if PASSTOS_CAPABILITY
244
 
  /* used to get/set TOS. */
245
 
  uint8_t ptos;
246
 
  bool ptos_defined;
247
 
#endif
248
 
 
249
 
#ifdef ENABLE_DEBUG
250
 
  int gremlin; /* --gremlin bits */
251
 
#endif
252
 
};
253
 
 
254
 
/*
255
 
 * Some Posix/Win32 differences.
256
 
 */
257
 
 
258
 
#ifndef MSG_NOSIGNAL
259
 
#define MSG_NOSIGNAL 0
260
 
#endif
261
 
 
262
 
#ifdef WIN32
263
 
 
264
 
#define openvpn_close_socket(s) closesocket(s)
265
 
 
266
 
int socket_recv_queue (struct link_socket *sock, int maxsize);
267
 
 
268
 
int socket_send_queue (struct link_socket *sock,
269
 
                       struct buffer *buf,
270
 
                       const struct link_socket_actual *to);
271
 
 
272
 
int socket_finalize (
273
 
                     SOCKET s,
274
 
                     struct overlapped_io *io,
275
 
                     struct buffer *buf,
276
 
                     struct link_socket_actual *from);
277
 
 
278
 
#else
279
 
 
280
 
#define openvpn_close_socket(s) close(s)
281
 
 
282
 
#endif
283
 
 
284
 
struct link_socket *link_socket_new (void);
285
 
 
286
 
void socket_bind (socket_descriptor_t sd,
287
 
                  struct openvpn_sockaddr *local,
288
 
                  const char *prefix);
289
 
 
290
 
int openvpn_connect (socket_descriptor_t sd,
291
 
                     struct openvpn_sockaddr *remote,
292
 
                     int connect_timeout,
293
 
                     volatile int *signal_received);
294
 
 
295
 
/*
296
 
 * Initialize link_socket object.
297
 
 */
298
 
 
299
 
void
300
 
link_socket_init_phase1 (struct link_socket *sock,
301
 
                         const bool connection_profiles_defined,
302
 
                         const char *local_host,
303
 
                         int local_port,
304
 
                         const char *remote_host,
305
 
                         int remote_port,
306
 
                         int proto,
307
 
                         int mode,
308
 
                         const struct link_socket *accept_from,
309
 
#ifdef ENABLE_HTTP_PROXY
310
 
                         struct http_proxy_info *http_proxy,
311
 
#endif
312
 
#ifdef ENABLE_SOCKS
313
 
                         struct socks_proxy_info *socks_proxy,
314
 
#endif
315
 
#ifdef ENABLE_DEBUG
316
 
                         int gremlin,
317
 
#endif
318
 
                         bool bind_local,
319
 
                         bool remote_float,
320
 
                         int inetd,
321
 
                         struct link_socket_addr *lsa,
322
 
                         const char *ipchange_command,
323
 
                         const struct plugin_list *plugins,
324
 
                         int resolve_retry_seconds,
325
 
                         int connect_retry_seconds,
326
 
                         int connect_timeout,
327
 
                         int connect_retry_max,
328
 
                         int mtu_discover_type,
329
 
                         int rcvbuf,
330
 
                         int sndbuf,
331
 
                         unsigned int sockflags);
332
 
 
333
 
void link_socket_init_phase2 (struct link_socket *sock,
334
 
                              const struct frame *frame,
335
 
                              volatile int *signal_received);
336
 
 
337
 
void socket_adjust_frame_parameters (struct frame *frame, int proto);
338
 
 
339
 
void frame_adjust_path_mtu (struct frame *frame, int pmtu, int proto);
340
 
 
341
 
void link_socket_close (struct link_socket *sock);
342
 
 
343
 
void sd_close (socket_descriptor_t *sd);
344
 
 
345
 
#define PS_SHOW_PORT_IF_DEFINED (1<<0)
346
 
#define PS_SHOW_PORT            (1<<1)
347
 
#define PS_SHOW_PKTINFO         (1<<2)
348
 
#define PS_DONT_SHOW_ADDR       (1<<3)
349
 
 
350
 
const char *print_sockaddr_ex (const struct openvpn_sockaddr *addr,
351
 
                               const char* separator,
352
 
                               const unsigned int flags,
353
 
                               struct gc_arena *gc);
354
 
 
355
 
 
356
 
const char *print_sockaddr (const struct openvpn_sockaddr *addr,
357
 
                            struct gc_arena *gc);
358
 
 
359
 
const char *print_link_socket_actual_ex (const struct link_socket_actual *act,
360
 
                                         const char* separator,
361
 
                                         const unsigned int flags,
362
 
                                         struct gc_arena *gc);
363
 
 
364
 
const char *print_link_socket_actual (const struct link_socket_actual *act,
365
 
                                      struct gc_arena *gc);
366
 
 
367
 
 
368
 
#define IA_EMPTY_IF_UNDEF (1<<0)
369
 
#define IA_NET_ORDER      (1<<1)
370
 
const char *print_in_addr_t (in_addr_t addr, unsigned int flags, struct gc_arena *gc);
371
 
const char *print_in6_addr  (struct in6_addr addr6, unsigned int flags, struct gc_arena *gc);
372
 
struct in6_addr add_in6_addr( struct in6_addr base, uint32_t add );
373
 
 
374
 
#define SA_IP_PORT        (1<<0)
375
 
#define SA_SET_IF_NONZERO (1<<1)
376
 
void setenv_sockaddr (struct env_set *es,
377
 
                      const char *name_prefix,
378
 
                      const struct openvpn_sockaddr *addr,
379
 
                      const bool flags);
380
 
 
381
 
void setenv_in_addr_t (struct env_set *es,
382
 
                       const char *name_prefix,
383
 
                       in_addr_t addr,
384
 
                       const bool flags);
385
 
 
386
 
void setenv_link_socket_actual (struct env_set *es,
387
 
                                const char *name_prefix,
388
 
                                const struct link_socket_actual *act,
389
 
                                const bool flags);
390
 
 
391
 
void bad_address_length (int actual, int expected);
392
 
 
393
 
#ifdef USE_PF_INET6
394
 
/* IPV4_INVALID_ADDR: returned by link_socket_current_remote()
395
 
 * to ease redirect-gateway logic for ipv4 tunnels on ipv6 endpoints
396
 
 */
397
 
#define IPV4_INVALID_ADDR 0xffffffff
398
 
#endif
399
 
in_addr_t link_socket_current_remote (const struct link_socket_info *info);
400
 
 
401
 
void link_socket_connection_initiated (const struct buffer *buf,
402
 
                                       struct link_socket_info *info,
403
 
                                       const struct link_socket_actual *addr,
404
 
                                       const char *common_name,
405
 
                                       struct env_set *es);
406
 
 
407
 
void link_socket_bad_incoming_addr (struct buffer *buf,
408
 
                                    const struct link_socket_info *info,
409
 
                                    const struct link_socket_actual *from_addr);
410
 
 
411
 
void link_socket_bad_outgoing_addr (void);
412
 
 
413
 
void setenv_trusted (struct env_set *es, const struct link_socket_info *info);
414
 
 
415
 
bool link_socket_update_flags (struct link_socket *ls, unsigned int sockflags);
416
 
void link_socket_update_buffer_sizes (struct link_socket *ls, int rcvbuf, int sndbuf);
417
 
 
418
 
/*
419
 
 * Low-level functions
420
 
 */
421
 
 
422
 
/* return values of openvpn_inet_aton */
423
 
#define OIA_HOSTNAME   0
424
 
#define OIA_IP         1
425
 
#define OIA_ERROR     -1
426
 
int openvpn_inet_aton (const char *dotted_quad, struct in_addr *addr);
427
 
 
428
 
/* integrity validation on pulled options */
429
 
bool ip_addr_dotted_quad_safe (const char *dotted_quad);
430
 
bool ip_or_dns_addr_safe (const char *addr, const bool allow_fqdn);
431
 
bool mac_addr_safe (const char *mac_addr);
432
 
bool ipv6_addr_safe (const char *ipv6_text_addr);
433
 
 
434
 
socket_descriptor_t create_socket_tcp (void);
435
 
 
436
 
socket_descriptor_t socket_do_accept (socket_descriptor_t sd,
437
 
                                      struct link_socket_actual *act,
438
 
                                      const bool nowait);
439
 
/*
440
 
 * proto related
441
 
 */
442
 
bool proto_is_net(int proto);
443
 
bool proto_is_dgram(int proto);
444
 
bool proto_is_udp(int proto);
445
 
bool proto_is_tcp(int proto);
446
 
 
447
 
 
448
 
#if UNIX_SOCK_SUPPORT
449
 
 
450
 
socket_descriptor_t create_socket_unix (void);
451
 
 
452
 
void socket_bind_unix (socket_descriptor_t sd,
453
 
                       struct sockaddr_un *local,
454
 
                       const char *prefix);
455
 
 
456
 
socket_descriptor_t socket_accept_unix (socket_descriptor_t sd,
457
 
                                        struct sockaddr_un *remote);
458
 
 
459
 
int socket_connect_unix (socket_descriptor_t sd,
460
 
                         struct sockaddr_un *remote);
461
 
 
462
 
void sockaddr_unix_init (struct sockaddr_un *local, const char *path);
463
 
 
464
 
const char *sockaddr_unix_name (const struct sockaddr_un *local, const char *null);
465
 
 
466
 
void socket_delete_unix (const struct sockaddr_un *local);
467
 
 
468
 
bool unix_socket_get_peer_uid_gid (const socket_descriptor_t sd, int *uid, int *gid);
469
 
 
470
 
#endif
471
 
 
472
 
/*
473
 
 * DNS resolution
474
 
 */
475
 
 
476
 
struct resolve_list {
477
 
  int len;
478
 
  in_addr_t data[16];
479
 
};
480
 
 
481
 
#define GETADDR_RESOLVE               (1<<0)
482
 
#define GETADDR_FATAL                 (1<<1)
483
 
#define GETADDR_HOST_ORDER            (1<<2)
484
 
#define GETADDR_MENTION_RESOLVE_RETRY (1<<3)
485
 
#define GETADDR_FATAL_ON_SIGNAL       (1<<4)
486
 
#define GETADDR_WARN_ON_SIGNAL        (1<<5)
487
 
#define GETADDR_MSG_VIRT_OUT          (1<<6)
488
 
#define GETADDR_TRY_ONCE              (1<<7)
489
 
#define GETADDR_UPDATE_MANAGEMENT_STATE (1<<8)
490
 
#define GETADDR_RANDOMIZE             (1<<9)
491
 
 
492
 
/* [ab]use flags bits to get socktype info downstream */
493
 
/* TODO(jjo): resolve tradeoff between hackiness|args-overhead */
494
 
#define GETADDR_DGRAM                 (1<<10)
495
 
#define dnsflags_to_socktype(flags) ((flags & GETADDR_DGRAM) ? SOCK_DGRAM : SOCK_STREAM)
496
 
 
497
 
in_addr_t getaddr (unsigned int flags,
498
 
                   const char *hostname,
499
 
                   int resolve_retry_seconds,
500
 
                   bool *succeeded,
501
 
                   volatile int *signal_received);
502
 
 
503
 
in_addr_t getaddr_multi (unsigned int flags,
504
 
                         const char *hostname,
505
 
                         int resolve_retry_seconds,
506
 
                         bool *succeeded,
507
 
                         volatile int *signal_received,
508
 
                         struct resolve_list *reslist);
509
 
 
510
 
/*
511
 
 * Transport protocol naming and other details.
512
 
 */
513
 
 
514
 
/* 
515
 
 * Use enum's instead of #define to allow for easier
516
 
 * optional proto support
517
 
 */
518
 
enum proto_num {
519
 
        PROTO_NONE, /* catch for uninitialized */
520
 
        PROTO_UDPv4,
521
 
        PROTO_TCPv4_SERVER,
522
 
        PROTO_TCPv4_CLIENT,
523
 
        PROTO_TCPv4,
524
 
#ifdef USE_PF_INET6
525
 
        PROTO_UDPv6,
526
 
        PROTO_TCPv6_SERVER,
527
 
        PROTO_TCPv6_CLIENT,
528
 
        PROTO_TCPv6,
529
 
#endif
530
 
        PROTO_N
531
 
};
532
 
 
533
 
int ascii2proto (const char* proto_name);
534
 
const char *proto2ascii (int proto, bool display_form);
535
 
const char *proto2ascii_all (struct gc_arena *gc);
536
 
int proto_remote (int proto, bool remote);
537
 
const char *addr_family_name(int af);
538
 
 
539
 
/*
540
 
 * Overhead added to packets by various protocols.
541
 
 */
542
 
#define IPv4_UDP_HEADER_SIZE              28
543
 
#define IPv4_TCP_HEADER_SIZE              40
544
 
#define IPv6_UDP_HEADER_SIZE              48
545
 
#define IPv6_TCP_HEADER_SIZE              60
546
 
 
547
 
extern const int proto_overhead[];
548
 
 
549
 
static inline int
550
 
datagram_overhead (int proto)
551
 
{
552
 
  ASSERT (proto >= 0 && proto < PROTO_N);
553
 
  return proto_overhead [proto];
554
 
}
555
 
 
556
 
/*
557
 
 * Misc inline functions
558
 
 */
559
 
 
560
 
static inline bool
561
 
legal_ipv4_port (int port)
562
 
{
563
 
  return port > 0 && port < 65536;
564
 
}
565
 
 
566
 
static inline int
567
 
is_proto_tcp(const int p)
568
 
{
569
 
  return p > 0; /* depends on the definition of PROTO_x */
570
 
}
571
 
 
572
 
static inline bool
573
 
link_socket_proto_connection_oriented (int proto)
574
 
{
575
 
  return !proto_is_dgram(proto);
576
 
}
577
 
 
578
 
static inline bool
579
 
link_socket_connection_oriented (const struct link_socket *sock)
580
 
{
581
 
  if (sock)
582
 
    return link_socket_proto_connection_oriented (sock->info.proto);
583
 
  else
584
 
    return false;
585
 
}
586
 
 
587
 
static inline bool
588
 
addr_defined (const struct openvpn_sockaddr *addr)
589
 
{
590
 
  if (!addr) return 0;
591
 
  switch (addr->addr.sa.sa_family) {
592
 
    case AF_INET: return addr->addr.in4.sin_addr.s_addr != 0;
593
 
#ifdef USE_PF_INET6
594
 
    case AF_INET6: return !IN6_IS_ADDR_UNSPECIFIED(&addr->addr.in6.sin6_addr);
595
 
#endif
596
 
    default: return 0;
597
 
  }
598
 
}
599
 
static inline bool
600
 
addr_defined_ipi (const struct link_socket_actual *lsa)
601
 
{
602
 
#if ENABLE_IP_PKTINFO
603
 
  if (!lsa) return 0;
604
 
  switch (lsa->dest.addr.sa.sa_family) {
605
 
#ifdef HAVE_IN_PKTINFO
606
 
    case AF_INET: return lsa->pi.in4.ipi_spec_dst.s_addr != 0;
607
 
#endif
608
 
#ifdef IP_RECVDSTADDR
609
 
    case AF_INET: return lsa->pi.in4.s_addr != 0;
610
 
#endif
611
 
#ifdef USE_PF_INET6
612
 
    case AF_INET6: return !IN6_IS_ADDR_UNSPECIFIED(&lsa->pi.in6.ipi6_addr);
613
 
#endif
614
 
    default: return 0;
615
 
  }
616
 
#else
617
 
  ASSERT(0);
618
 
#endif
619
 
  return false;
620
 
}
621
 
 
622
 
static inline bool
623
 
link_socket_actual_defined (const struct link_socket_actual *act)
624
 
{
625
 
  return act && addr_defined (&act->dest);
626
 
}
627
 
 
628
 
static inline bool
629
 
addr_match (const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
630
 
{
631
 
  switch(a1->addr.sa.sa_family) {
632
 
    case AF_INET:
633
 
      return a1->addr.in4.sin_addr.s_addr == a2->addr.in4.sin_addr.s_addr;
634
 
#ifdef USE_PF_INET6
635
 
    case AF_INET6:
636
 
      return IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, &a2->addr.in6.sin6_addr);
637
 
#endif
638
 
  }
639
 
  ASSERT(0);
640
 
  return false;
641
 
}
642
 
 
643
 
static inline in_addr_t
644
 
addr_host (const struct openvpn_sockaddr *addr)
645
 
{
646
 
  /* 
647
 
   * "public" addr returned is checked against ifconfig for
648
 
   * possible clash: non sense for now given
649
 
   * that we do ifconfig only IPv4
650
 
   */
651
 
#if defined(USE_PF_INET6) 
652
 
  if(addr->addr.sa.sa_family != AF_INET)
653
 
    return 0;
654
 
#else 
655
 
  ASSERT(addr->addr.sa.sa_family == AF_INET);
656
 
#endif
657
 
  return ntohl (addr->addr.in4.sin_addr.s_addr);
658
 
}
659
 
 
660
 
static inline bool
661
 
addr_port_match (const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
662
 
{
663
 
  switch(a1->addr.sa.sa_family) {
664
 
    case AF_INET:
665
 
      return a1->addr.in4.sin_addr.s_addr == a2->addr.in4.sin_addr.s_addr
666
 
        && a1->addr.in4.sin_port == a2->addr.in4.sin_port;
667
 
#ifdef USE_PF_INET6
668
 
    case AF_INET6:
669
 
      return IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, &a2->addr.in6.sin6_addr) 
670
 
        && a1->addr.in6.sin6_port == a2->addr.in6.sin6_port;
671
 
#endif
672
 
  }
673
 
  ASSERT(0);
674
 
  return false;
675
 
}
676
 
 
677
 
static inline bool
678
 
addr_match_proto (const struct openvpn_sockaddr *a1,
679
 
                  const struct openvpn_sockaddr *a2,
680
 
                  const int proto)
681
 
{
682
 
  return link_socket_proto_connection_oriented (proto)
683
 
    ? addr_match (a1, a2)
684
 
    : addr_port_match (a1, a2);
685
 
}
686
 
 
687
 
static inline void
688
 
addr_zero_host(struct openvpn_sockaddr *addr)
689
 
{
690
 
   switch(addr->addr.sa.sa_family) {
691
 
     case AF_INET:
692
 
       addr->addr.in4.sin_addr.s_addr = 0;
693
 
       break;
694
 
#ifdef USE_PF_INET6
695
 
     case AF_INET6: 
696
 
       memset(&addr->addr.in6.sin6_addr, 0, sizeof (struct in6_addr));
697
 
       break;
698
 
#endif
699
 
   }
700
 
}
701
 
 
702
 
static inline void
703
 
addr_copy_sa(struct openvpn_sockaddr *dst, const struct openvpn_sockaddr *src)
704
 
{
705
 
  dst->addr = src->addr;
706
 
}
707
 
 
708
 
static inline void
709
 
addr_copy_host(struct openvpn_sockaddr *dst, const struct openvpn_sockaddr *src)
710
 
{
711
 
   switch(src->addr.sa.sa_family) {
712
 
     case AF_INET:
713
 
       dst->addr.in4.sin_addr.s_addr = src->addr.in4.sin_addr.s_addr;
714
 
       break;
715
 
#ifdef USE_PF_INET6
716
 
     case AF_INET6: 
717
 
       dst->addr.in6.sin6_addr = src->addr.in6.sin6_addr;
718
 
       break;
719
 
#endif
720
 
   }
721
 
}
722
 
 
723
 
static inline bool
724
 
addr_inet4or6(struct sockaddr *addr)
725
 
{
726
 
        return addr->sa_family == AF_INET || addr->sa_family == AF_INET6;
727
 
}
728
 
 
729
 
int addr_guess_family(int proto, const char *name);
730
 
static inline int
731
 
af_addr_size(unsigned short af)
732
 
{
733
 
#if defined(USE_PF_INET6) || defined (USE_PF_UNIX)
734
 
   switch(af) {
735
 
     case AF_INET: return sizeof (struct sockaddr_in);
736
 
#ifdef USE_PF_UNIX
737
 
     case AF_UNIX: return sizeof (struct sockaddr_un);
738
 
#endif
739
 
#ifdef USE_PF_INET6
740
 
     case AF_INET6: return sizeof (struct sockaddr_in6);
741
 
#endif
742
 
     default: 
743
 
#if 0
744
 
      /* could be called from socket_do_accept() with empty addr */
745
 
      msg (M_ERR, "Bad address family: %d\n", af);
746
 
      ASSERT(0);
747
 
#endif
748
 
        return 0;
749
 
   }
750
 
#else /* only AF_INET */
751
 
   return sizeof(struct sockaddr_in);
752
 
#endif
753
 
}
754
 
 
755
 
static inline bool
756
 
link_socket_actual_match (const struct link_socket_actual *a1, const struct link_socket_actual *a2)
757
 
{
758
 
  return addr_port_match (&a1->dest, &a2->dest);
759
 
}
760
 
 
761
 
#if PORT_SHARE
762
 
 
763
 
static inline bool
764
 
socket_foreign_protocol_detected (const struct link_socket *sock)
765
 
{
766
 
  return link_socket_connection_oriented (sock)
767
 
    && sock->stream_buf.port_share_state == PS_FOREIGN;
768
 
}
769
 
 
770
 
static inline const struct buffer *
771
 
socket_foreign_protocol_head (const struct link_socket *sock)
772
 
{
773
 
  return &sock->stream_buf.buf;
774
 
}
775
 
 
776
 
static inline int
777
 
socket_foreign_protocol_sd (const struct link_socket *sock)
778
 
{
779
 
  return sock->sd;
780
 
}
781
 
 
782
 
#endif
783
 
 
784
 
static inline bool
785
 
socket_connection_reset (const struct link_socket *sock, int status)
786
 
{
787
 
  if (link_socket_connection_oriented (sock))
788
 
    {
789
 
      if (sock->stream_reset || sock->stream_buf.error)
790
 
        return true;
791
 
      else if (status < 0)
792
 
        {
793
 
          const int err = openvpn_errno_socket ();
794
 
#ifdef WIN32
795
 
          return err == WSAECONNRESET || err == WSAECONNABORTED;
796
 
#else
797
 
          return err == ECONNRESET;
798
 
#endif
799
 
        }
800
 
    }
801
 
  return false;
802
 
}
803
 
 
804
 
static inline bool
805
 
link_socket_verify_incoming_addr (struct buffer *buf,
806
 
                                  const struct link_socket_info *info,
807
 
                                  const struct link_socket_actual *from_addr)
808
 
{
809
 
  if (buf->len > 0)
810
 
    {
811
 
      switch (from_addr->dest.addr.sa.sa_family) {
812
 
#ifdef USE_PF_INET6
813
 
        case AF_INET6:
814
 
#endif
815
 
        case AF_INET:
816
 
          if (!link_socket_actual_defined (from_addr))
817
 
            return false;
818
 
          if (info->remote_float || !addr_defined (&info->lsa->remote))
819
 
            return true;
820
 
          if (addr_match_proto (&from_addr->dest, &info->lsa->remote, info->proto))
821
 
            return true;
822
 
      }
823
 
    }
824
 
  return false;
825
 
}
826
 
 
827
 
static inline void
828
 
link_socket_get_outgoing_addr (struct buffer *buf,
829
 
                              const struct link_socket_info *info,
830
 
                              struct link_socket_actual **act)
831
 
{
832
 
  if (buf->len > 0)
833
 
    {
834
 
      struct link_socket_addr *lsa = info->lsa;
835
 
      if (link_socket_actual_defined (&lsa->actual))
836
 
        *act = &lsa->actual;
837
 
      else
838
 
        {
839
 
          link_socket_bad_outgoing_addr ();
840
 
          buf->len = 0;
841
 
          *act = NULL;
842
 
        }
843
 
    }
844
 
}
845
 
 
846
 
static inline void
847
 
link_socket_set_outgoing_addr (const struct buffer *buf,
848
 
                               struct link_socket_info *info,
849
 
                               const struct link_socket_actual *act,
850
 
                               const char *common_name,
851
 
                               struct env_set *es)
852
 
{
853
 
  if (!buf || buf->len > 0)
854
 
    {
855
 
      struct link_socket_addr *lsa = info->lsa;
856
 
      if (
857
 
          /* new or changed address? */
858
 
          (!info->connection_established
859
 
           || !addr_match_proto (&act->dest, &lsa->actual.dest, info->proto))
860
 
          /* address undef or address == remote or --float */
861
 
          && (info->remote_float
862
 
              || !addr_defined (&lsa->remote)
863
 
              || addr_match_proto (&act->dest, &lsa->remote, info->proto))
864
 
          )
865
 
        {
866
 
          link_socket_connection_initiated (buf, info, act, common_name, es);
867
 
        }
868
 
    }
869
 
}
870
 
 
871
 
/*
872
 
 * Stream buffer handling -- stream_buf is a helper class
873
 
 * to assist in the packetization of stream transport protocols
874
 
 * such as TCP.
875
 
 */
876
 
 
877
 
void stream_buf_init (struct stream_buf *sb,
878
 
                      struct buffer *buf,
879
 
                      const unsigned int sockflags,
880
 
                      const int proto);
881
 
 
882
 
void stream_buf_close (struct stream_buf* sb);
883
 
bool stream_buf_added (struct stream_buf *sb, int length_added);
884
 
 
885
 
static inline bool
886
 
stream_buf_read_setup (struct link_socket* sock)
887
 
{
888
 
  bool stream_buf_read_setup_dowork (struct link_socket* sock);
889
 
  if (link_socket_connection_oriented (sock))
890
 
    return stream_buf_read_setup_dowork (sock);
891
 
  else
892
 
    return true;
893
 
}
894
 
 
895
 
/*
896
 
 * Socket Read Routines
897
 
 */
898
 
 
899
 
int link_socket_read_tcp (struct link_socket *sock,
900
 
                          struct buffer *buf);
901
 
 
902
 
#ifdef WIN32
903
 
 
904
 
static inline int
905
 
link_socket_read_udp_win32 (struct link_socket *sock,
906
 
                            struct buffer *buf,
907
 
                            struct link_socket_actual *from)
908
 
{
909
 
  return socket_finalize (sock->sd, &sock->reads, buf, from);
910
 
}
911
 
 
912
 
#else
913
 
 
914
 
int link_socket_read_udp_posix (struct link_socket *sock,
915
 
                                struct buffer *buf,
916
 
                                int maxsize,
917
 
                                struct link_socket_actual *from);
918
 
 
919
 
#endif
920
 
 
921
 
/* read a TCP or UDP packet from link */
922
 
static inline int
923
 
link_socket_read (struct link_socket *sock,
924
 
                  struct buffer *buf,
925
 
                  int maxsize,
926
 
                  struct link_socket_actual *from)
927
 
{
928
 
  if (proto_is_udp(sock->info.proto)) /* unified UDPv4 and UDPv6 */
929
 
    {
930
 
      int res;
931
 
 
932
 
#ifdef WIN32
933
 
      res = link_socket_read_udp_win32 (sock, buf, from);
934
 
#else
935
 
      res = link_socket_read_udp_posix (sock, buf, maxsize, from);
936
 
#endif
937
 
      return res;
938
 
    }
939
 
  else if (proto_is_tcp(sock->info.proto)) /* unified TCPv4 and TCPv6 */
940
 
    {
941
 
      /* from address was returned by accept */
942
 
      addr_copy_sa(&from->dest, &sock->info.lsa->actual.dest);
943
 
      return link_socket_read_tcp (sock, buf);
944
 
    }
945
 
  else
946
 
    {
947
 
      ASSERT (0);
948
 
      return -1; /* NOTREACHED */
949
 
    }
950
 
}
951
 
 
952
 
/*
953
 
 * Socket Write routines
954
 
 */
955
 
 
956
 
int link_socket_write_tcp (struct link_socket *sock,
957
 
                           struct buffer *buf,
958
 
                           struct link_socket_actual *to);
959
 
 
960
 
#ifdef WIN32
961
 
 
962
 
static inline int
963
 
link_socket_write_win32 (struct link_socket *sock,
964
 
                         struct buffer *buf,
965
 
                         struct link_socket_actual *to)
966
 
{
967
 
  int err = 0;
968
 
  int status = 0;
969
 
  if (overlapped_io_active (&sock->writes))
970
 
    {
971
 
      status = socket_finalize (sock->sd, &sock->writes, NULL, NULL);
972
 
      if (status < 0)
973
 
        err = WSAGetLastError ();
974
 
    }
975
 
  socket_send_queue (sock, buf, to);
976
 
  if (status < 0)
977
 
    {
978
 
      WSASetLastError (err);
979
 
      return status;
980
 
    }
981
 
  else
982
 
    return BLEN (buf);
983
 
}
984
 
 
985
 
#else
986
 
 
987
 
static inline int
988
 
link_socket_write_udp_posix (struct link_socket *sock,
989
 
                             struct buffer *buf,
990
 
                             struct link_socket_actual *to)
991
 
{
992
 
#if ENABLE_IP_PKTINFO
993
 
  int link_socket_write_udp_posix_sendmsg (struct link_socket *sock,
994
 
                                           struct buffer *buf,
995
 
                                           struct link_socket_actual *to);
996
 
 
997
 
  if (proto_is_udp(sock->info.proto) && (sock->sockflags & SF_USE_IP_PKTINFO)
998
 
          && addr_defined_ipi(to))
999
 
    return link_socket_write_udp_posix_sendmsg (sock, buf, to);
1000
 
  else
1001
 
#endif
1002
 
    return sendto (sock->sd, BPTR (buf), BLEN (buf), 0,
1003
 
                   (struct sockaddr *) &to->dest.addr.sa,
1004
 
                   (socklen_t) af_addr_size(to->dest.addr.sa.sa_family));
1005
 
}
1006
 
 
1007
 
static inline int
1008
 
link_socket_write_tcp_posix (struct link_socket *sock,
1009
 
                             struct buffer *buf,
1010
 
                             struct link_socket_actual *to)
1011
 
{
1012
 
  return send (sock->sd, BPTR (buf), BLEN (buf), MSG_NOSIGNAL);
1013
 
}
1014
 
 
1015
 
#endif
1016
 
 
1017
 
static inline int
1018
 
link_socket_write_udp (struct link_socket *sock,
1019
 
                       struct buffer *buf,
1020
 
                       struct link_socket_actual *to)
1021
 
{
1022
 
#ifdef WIN32
1023
 
  return link_socket_write_win32 (sock, buf, to);
1024
 
#else
1025
 
  return link_socket_write_udp_posix (sock, buf, to);
1026
 
#endif
1027
 
}
1028
 
 
1029
 
/* write a TCP or UDP packet to link */
1030
 
static inline int
1031
 
link_socket_write (struct link_socket *sock,
1032
 
                   struct buffer *buf,
1033
 
                   struct link_socket_actual *to)
1034
 
{
1035
 
  if (proto_is_udp(sock->info.proto)) /* unified UDPv4 and UDPv6 */
1036
 
    {
1037
 
      return link_socket_write_udp (sock, buf, to);
1038
 
    }
1039
 
  else if (proto_is_tcp(sock->info.proto)) /* unified TCPv4 and TCPv6 */
1040
 
    {
1041
 
      return link_socket_write_tcp (sock, buf, to);
1042
 
    }
1043
 
  else
1044
 
    {
1045
 
      ASSERT (0);
1046
 
      return -1; /* NOTREACHED */
1047
 
    }
1048
 
}
1049
 
 
1050
 
#if PASSTOS_CAPABILITY
1051
 
 
1052
 
/*
1053
 
 * Extract TOS bits.  Assumes that ipbuf is a valid IPv4 packet.
1054
 
 */
1055
 
static inline void
1056
 
link_socket_extract_tos (struct link_socket *ls, const struct buffer *ipbuf)
1057
 
{
1058
 
  if (ls && ipbuf)
1059
 
    {
1060
 
      struct openvpn_iphdr *iph = (struct openvpn_iphdr *) BPTR (ipbuf);
1061
 
      ls->ptos = iph->tos;
1062
 
      ls->ptos_defined = true;
1063
 
    }
1064
 
}
1065
 
 
1066
 
/*
1067
 
 * Set socket properties to reflect TOS bits which were extracted
1068
 
 * from tunnel packet.
1069
 
 */
1070
 
static inline void
1071
 
link_socket_set_tos (struct link_socket *ls)
1072
 
{
1073
 
  if (ls && ls->ptos_defined)
1074
 
    setsockopt (ls->sd, IPPROTO_IP, IP_TOS, &ls->ptos, sizeof (ls->ptos));
1075
 
}
1076
 
 
1077
 
#endif
1078
 
 
1079
 
/*
1080
 
 * Socket I/O wait functions
1081
 
 */
1082
 
 
1083
 
static inline bool
1084
 
socket_read_residual (const struct link_socket *s)
1085
 
{
1086
 
  return s && s->stream_buf.residual_fully_formed;
1087
 
}
1088
 
 
1089
 
static inline event_t
1090
 
socket_event_handle (const struct link_socket *s)
1091
 
{
1092
 
#ifdef WIN32
1093
 
  return &s->rw_handle;
1094
 
#else
1095
 
  return s->sd;
1096
 
#endif
1097
 
}
1098
 
 
1099
 
event_t socket_listen_event_handle (struct link_socket *s);
1100
 
 
1101
 
unsigned int
1102
 
socket_set (struct link_socket *s,
1103
 
            struct event_set *es,
1104
 
            unsigned int rwflags,
1105
 
            void *arg,
1106
 
            unsigned int *persistent);
1107
 
 
1108
 
static inline void
1109
 
socket_set_listen_persistent (struct link_socket *s,
1110
 
                              struct event_set *es,
1111
 
                              void *arg)
1112
 
{
1113
 
  if (s && !s->listen_persistent_queued)
1114
 
    {
1115
 
      event_ctl (es, socket_listen_event_handle (s), EVENT_READ, arg);
1116
 
      s->listen_persistent_queued = true;
1117
 
    }
1118
 
}
1119
 
 
1120
 
static inline void
1121
 
socket_reset_listen_persistent (struct link_socket *s)
1122
 
{
1123
 
#ifdef WIN32
1124
 
  reset_net_event_win32 (&s->listen_handle, s->sd);
1125
 
#endif
1126
 
}
1127
 
 
1128
 
const char *socket_stat (const struct link_socket *s, unsigned int rwflags, struct gc_arena *gc);
1129
 
 
1130
 
#endif /* SOCKET_H */