Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 183457 | Differences between
and this patch

Collapse All | Expand All

(-)a/jjo-tests/libtest.sh (+115 lines)
Line 0 Link Here
1
#!/bin/bash
2
3
typeset -i test_num=0 # increments w/each test_define
4
typeset test_msg	# prefix: "test nr#"  for msgs
5
typeset test_name	# test name given	
6
typeset test_sanename	# test name with all whitespace (and alike) replaced by '_'
7
8
TEST_CLEANUP=":"
9
10
say () {
11
	echo "$@" >&3
12
}
13
err () {
14
	echo "$@" >&4
15
}
16
debug () {
17
	echo "$@" >&4
18
}
19
test_define() {
20
	test_name="$*"
21
	test_sanename="$(echo -n $test_name| tr -c '[A-Za-z0-9._]' _ )"
22
	test_num=test_num+1
23
	test_msg="test $test_num"
24
	test_bg_cleanup 
25
}
26
test_expect_success () {
27
	local msg="$1" 
28
	test $# -ge 2 || { err "usage error: test_expect_failure msg cmd args ..."; return 1; }
29
	say -n "$test_msg: -- $msg (expecting success) "
30
	shift
31
	(eval "$@" 4>&1) && { say -e "\n$test_msg: OK %%% $test_sanename" ;  return 0 ; }
32
	say -e "\n$test_msg: FAILED %%% $test_sanename" 
33
	return 1
34
}
35
36
test_expect_failure () {
37
	local msg="$1" 
38
	test $# -ge 2 || { err "usage error: test_expect_failure msg cmd args ...";  return 1;}
39
	say -n "$test_msg: -- $msg (expecting failure) "
40
	shift
41
	(eval "$@") && { say -e "\n$test_msg: FAILED %%% $test_sanename" >&3 ;  return 0 ; }
42
	say -e "\n$test_msg: OK %%% $test_sanename"
43
	return 1
44
}
45
46
test_bg_egrep() {
47
	local nsecs="$1"
48
	local txt="$2"
49
	local ret
50
	shift 2
51
	test_expect_success "$test_name" \
52
		"set -m ; $@ &> $t/out-$test_sanename &
53
		s=1;
54
		for i in \$(seq 1 $nsecs);do 
55
			say -n '.'
56
			kill -0 %1 || { egrep failed $t/out-$test_sanename >&4; break; }
57
			o=\$(egrep \"$txt\" $t/out-$test_sanename ) && { debug \$o; s=0; break; }
58
			sleep 1;
59
		done;
60
		kill %1
61
		wait
62
		exit \$s
63
		" 2>/dev/null 4>$t/err
64
	ret=$?
65
	test $ret -eq 0 && return 0
66
	say "$test_msg: see: $t/out-$test_sanename*" 
67
	return $ret
68
}
69
# run command in *current* shell ignoring stderr, evals args passed
70
quiet2() {
71
	#backup fd=2
72
	exec 250>&2
73
	exec 2>/dev/null
74
	eval "$@"
75
	#close auxfd
76
	exec 2>&250
77
	exec 250>&-
78
}
79
test_bg_prev(){ local out=$t/out-$test_sanename-prev; $@ >& $out & }
80
test_bg_cleanup() { quiet2 "$TEST_CLEANUP;${@:-:};kill % 2>/dev/null;wait" ;}
81
test_set_cleanup () { TEST_CLEANUP="${*:-:}" ;}
82
83
t=/tmp
84
exec 3>&1
85
exec 4>&2
86
87
#=========
88
89
ln -sf $PWD/openvpn $t/openvpn-test
90
export OPENVPN=$t/openvpn-test
91
92
test_set_cleanup "killall $OPENVPN"
93
94
trap 'test_bg_cleanup;exit' 0 2 15
95
96
test_define "UDP6 loopback"
97
test_bg_egrep 30 "Initialization Sequence Completed" ../jjo-tests/run-udp6-0-loopback-self.sh
98
99
test_define "UDP6 loopback byname"
100
test_bg_egrep 30 "Initialization Sequence Completed" ../jjo-tests/run-udp6-0-loopback-byname.sh
101
102
test_define "TCP6 loopback"
103
test_bg_prev ../jjo-tests/run-tcp6-0-loopback-server.sh
104
test_bg_egrep 30 "Initialization Sequence Completed" ../jjo-tests/run-tcp6-0-loopback-client.sh
105
106
test_define "TCP6 loopback byname"
107
test_bg_prev ../jjo-tests/run-tcp6-0-loopback-server-byname.sh
108
test_bg_egrep 30 "Initialization Sequence Completed" ../jjo-tests/run-tcp6-0-loopback-client-byname.sh
109
110
test_define "UDP4 loopback"
111
test_bg_egrep 30 "Initialization Sequence Completed" ../jjo-tests/run-udp4-0-loopback-self.sh
112
113
test_define "TCP4 loopback"
114
test_bg_prev ../jjo-tests/run-tcp4-0-loopback-server.sh 
115
test_bg_egrep 30 "Initialization Sequence Completed" ../jjo-tests/run-tcp4-0-loopback-client.sh
(-)a/jjo-tests/qemu-ifup-2.jjo.sh (+12 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
#sudo sh -c "/sbin/ifconfig $1 hw ether 00:00:00:00:00:01; /sbin/ifconfig $1 192.168.254.1"
3
export PATH=/sbin:/usr/sbin:$PATH
4
NUM=2
5
DEV=qemutun$NUM
6
sudo sh -x <<EOF
7
ip li set $1 name $DEV
8
ip li set $DEV address 00:00:00:00:00:01
9
ip li set $DEV up
10
ip ad add 192.168.254.1 dev $DEV
11
ip ro add 192.168.254.$NUM dev $DEV
12
EOF
(-)a/jjo-tests/qemu-ifup-3.jjo.sh (+12 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
#sudo sh -c "/sbin/ifconfig $1 hw ether 00:00:00:00:00:01; /sbin/ifconfig $1 192.168.254.1"
3
export PATH=/sbin:/usr/sbin:$PATH
4
NUM=3
5
DEV=qemutun$NUM
6
sudo sh -x <<EOF
7
ip li set $1 name $DEV
8
ip li set $DEV address 00:00:00:00:00:01
9
ip li set $DEV up
10
ip ad add 192.168.254.1 dev $DEV
11
ip ro add 192.168.254.$NUM dev $DEV
12
EOF
(-)a/jjo-tests/run-afunix-0-loop.sh (+4 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
rm -fv /tmp/o.s
4
${OPENVPN} --proto unix-dgram --local /tmp/o.s --remote /tmp/o.s --dev tun --ifconfig 1.1.1.1 1.1.1.2 --secret ../openvpn.key $*
(-)a/jjo-tests/run-afunix-1-o1.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN} --proto unix-dgram --local /tmp/o1.s --remote /tmp/o2.s --dev tun --ifconfig 1.1.1.1 1.1.1.2 --secret ../openvpn.key $*
(-)a/jjo-tests/run-afunix-1-o2.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN} --proto unix-dgram --local /tmp/o2.s --remote /tmp/o1.s --dev tun --ifconfig 1.1.1.2 1.1.1.1 --secret ../openvpn.key $*
(-)a/jjo-tests/run-oxg-1.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN}  --dev tun --proto udp --remote localhost --lport 5020 --rport 1194 --secret ../openvpn.key --ifconfig 1.1.1.1 1.1.1.2 "$@"
(-)a/jjo-tests/run-oxg-2.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN}  --dev tun --proto udp --remote localhost --lport 5020 --rport 1194 --secret ../openvpn.key --ifconfig 1.1.1.2 1.1.1.1 "$@"
(-)a/jjo-tests/run-tcp4-0-loopback-client.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN} --dev null --proto tcp-client --remote localhost --rport 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-tcp4-0-loopback-server.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN}  --dev null --proto tcp-server --remote localhost --lport 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-tcp6-0-loopback-client-byname.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN}  --dev null --proto tcp6-client --remote ip6-localhost --rport 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-tcp6-0-loopback-client.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN}  --dev null --proto tcp6-client --remote ::1 --rport 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-tcp6-0-loopback-server-byname.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN}  --dev null --proto tcp6-server --local ip6-localhost --lport 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-tcp6-0-loopback-server.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${OPENVPN}  --dev null --proto tcp6-server --local :: --lport 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-udp4-0-loopback-self.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${GDB} ${OPENVPN} --dev null --proto udp --remote localhost --port 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-udp4-1-qemu-guest.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${GDB} ${OPENVPN} --dev tun --proto udp --remote 10.0.2.2 --port 5011 --secret ../openvpn.key --ifconfig 1.1.1.253 1.1.1.1
(-)a/jjo-tests/run-udp4-1-qemu-host.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${GDB} ${OPENVPN} --dev tun --proto udp --float --port 5011 --secret ../openvpn.key --ifconfig 1.1.1.1 1.1.1.253
(-)a/jjo-tests/run-udp4-2-MH.sh (+4 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
#pass --local ADDR --remote ADDR
3
: ${OPENVPN:=./openvpn}
4
${GDB} ${OPENVPN} --dev tun --proto udp --port 5011 --secret ../openvpn.key --ifconfig-noexec "$@"
(-)a/jjo-tests/run-udp6-0-loopback-byname.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${GDB} ${OPENVPN} --dev null --proto udp6 --remote ip6-localhost --port 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-udp6-0-loopback-self.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${GDB} ${OPENVPN} --dev null --proto udp6 --remote ::1 --port 5011 --secret ../openvpn.key "$@"
(-)a/jjo-tests/run-udp6-1-carpediem.sh (+4 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
REM6="fe80::2e0:7dff:fee3:ef17"
3
: ${OPENVPN:=./openvpn}
4
${GDB} ${OPENVPN} --dev tun --proto udp6 --remote "$REM6" --port 5010 --secret ../openvpn.key --ifconfig 1.1.1.253 1.1.1.1
(-)a/jjo-tests/run-udp6-1-cx.sh (+3 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
${GDB} ${OPENVPN} --verb 5 --dev tun --proto udp6 --port 5010 --secret /home/jjo/src/juanjo/openvpn.key --float --ifconfig 1.1.1.1 1.1.1.253
(-)a/jjo-tests/run-udp6-2-qemu-guest.sh (+8 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
case "$1" in
4
--freebsd) shift;set --   ed0 "$@";;
5
--openbsd) shift;set --   ne3 --dev tun0 "$@";;
6
esac
7
DEV=${1:?missing devname};shift
8
${GDB} ${OPENVPN} --dev tun --proto udp6 --remote fe80::200:ff:fe00:1%$DEV --port 5010 --secret ../openvpn.key "$@" --ifconfig 1.1.1.253 1.1.1.1
(-)a/jjo-tests/run-udp6-2-qemu-host.sh (+4 lines)
Line 0 Link Here
1
#!/bin/sh -x
2
: ${OPENVPN:=./openvpn}
3
DEV=${1:?missing devname};shift
4
${GDB} ${OPENVPN} --dev tun --proto udp6 --remote fe80::5054:ff:fe12:3456%$DEV --port 5010 --secret ../openvpn.key --ifconfig  1.1.1.1 1.1.1.253 "$@"
(-)a/buffer.c (+13 lines)
Lines 215-220 } Link Here
215
  return ret;
215
  return ret;
216
}
216
}
217
217
218
void buf_puts(struct buffer *buf, const char *str)
219
{
220
  uint8_t *ptr = BEND (buf);
221
  int cap = buf_forward_capacity (buf);
222
  if (cap > 0)
223
    {
224
      strncpynt ((char *)ptr,str, cap);
225
      *(buf->data + buf->capacity - 1) = 0; /* windows vsnprintf needs this */
226
      buf->len += (int) strlen ((char *)ptr);
227
    }
228
}
229
 
230
218
/*
231
/*
219
 * This is necessary due to certain buggy implementations of snprintf,
232
 * This is necessary due to certain buggy implementations of snprintf,
220
 * that don't guarantee null termination for size > 0.
233
 * that don't guarantee null termination for size > 0.
(-)a/configure.ac (+16 lines)
Lines 135-140 [MULTIHOME="$enableval"], Link Here
135
   [MULTIHOME="yes"]
135
   [MULTIHOME="yes"]
136
)
136
)
137
137
138
AC_ARG_ENABLE(ipv6,
139
   [  --disable-ipv6          Disable UDP/IPv6 support],
140
   [PF_INET6="$enableval"],
141
   [PF_INET6="yes"]
142
)
143
138
AC_ARG_ENABLE(port-share,
144
AC_ARG_ENABLE(port-share,
139
   [  --disable-port-share    Disable TCP server port-share support (--port-share)],
145
   [  --disable-port-share    Disable TCP server port-share support (--port-share)],
140
   [PORT_SHARE="$enableval"],
146
   [PORT_SHARE="$enableval"],
Lines 524-529 LDFLAGS="$LDFLAGS -Wl,--fatal-warnings" Link Here
524
AC_CHECK_FUNC(epoll_create, AC_DEFINE(HAVE_EPOLL_CREATE, 1, [epoll_create function is defined]))
530
AC_CHECK_FUNC(epoll_create, AC_DEFINE(HAVE_EPOLL_CREATE, 1, [epoll_create function is defined]))
525
LDFLAGS="$OLDLDFLAGS"
531
LDFLAGS="$OLDLDFLAGS"
526
532
533
dnl ipv6 support
534
if test "$PF_INET6" = "yes"; then
535
  AC_CHECKING([for struct sockaddr_in6 for IPv6 support])
536
  AC_CHECK_TYPE(
537
      [struct sockaddr_in6],
538
      [AC_DEFINE(USE_PF_INET6, 1, [struct sockaddr_in6 is needed for IPv6 peer support])],
539
      [],
540
      [#include "syshead.h"])
541
fi
542
527
dnl
543
dnl
528
dnl check for valgrind tool
544
dnl check for valgrind tool
529
dnl
545
dnl
(-)a/init.c (-15 / +18 lines)
Lines 884-890 #ifdef ENABLE_MANAGEMENT Link Here
884
      const char *detail = "SUCCESS";
884
      const char *detail = "SUCCESS";
885
      if (c->c1.tuntap)
885
      if (c->c1.tuntap)
886
	tun_local = c->c1.tuntap->local;
886
	tun_local = c->c1.tuntap->local;
887
      tun_remote = htonl (c->c1.link_socket_addr.actual.dest.sa.sin_addr.s_addr);
887
      tun_remote = htonl (c->c1.link_socket_addr.actual.dest.addr.in4.sin_addr.s_addr);
888
      if (flags & ISC_ERRORS)
888
      if (flags & ISC_ERRORS)
889
	detail = "ERROR";
889
	detail = "ERROR";
890
      management_set_state (management,
890
      management_set_state (management,
Lines 1289-1295 do_deferred_options (struct context *c, Link Here
1289
#ifdef ENABLE_OCC
1289
#ifdef ENABLE_OCC
1290
  if (found & OPT_P_EXPLICIT_NOTIFY)
1290
  if (found & OPT_P_EXPLICIT_NOTIFY)
1291
    {
1291
    {
1292
      if (c->options.ce.proto != PROTO_UDPv4 && c->options.explicit_exit_notification)
1292
      if (!proto_is_udp(c->options.ce.proto) && c->options.explicit_exit_notification)
1293
	{
1293
	{
1294
	  msg (D_PUSH, "OPTIONS IMPORT: --explicit-exit-notify can only be used with --proto udp");
1294
	  msg (D_PUSH, "OPTIONS IMPORT: --explicit-exit-notify can only be used with --proto udp");
1295
	  c->options.explicit_exit_notification = 0;
1295
	  c->options.explicit_exit_notification = 0;
Lines 1384-1395 #endif Link Here
1384
  switch (c->options.ce.proto)
1384
  switch (c->options.ce.proto)
1385
    {
1385
    {
1386
    case PROTO_UDPv4:
1386
    case PROTO_UDPv4:
1387
#ifdef USE_PF_INET6
1388
    case PROTO_UDPv6:
1389
#endif
1387
      if (proxy)
1390
      if (proxy)
1388
	sec = c->options.ce.connect_retry_seconds;
1391
	sec = c->options.ce.connect_retry_seconds;
1389
      break;
1392
      break;
1393
#ifdef USE_PF_INET6
1394
    case PROTO_TCPv6_SERVER:
1395
#endif
1390
    case PROTO_TCPv4_SERVER:
1396
    case PROTO_TCPv4_SERVER:
1391
      sec = 1;
1397
      sec = 1;
1392
      break;
1398
      break;
1399
#ifdef USE_PF_INET6
1400
    case PROTO_TCPv6_CLIENT:
1401
#endif
1393
    case PROTO_TCPv4_CLIENT:
1402
    case PROTO_TCPv4_CLIENT:
1394
      sec = c->options.ce.connect_retry_seconds;
1403
      sec = c->options.ce.connect_retry_seconds;
1395
      break;
1404
      break;
Lines 2504-2510 do_setup_fast_io (struct context *c) Link Here
2504
#ifdef WIN32
2513
#ifdef WIN32
2505
      msg (M_INFO, "NOTE: --fast-io is disabled since we are running on Windows");
2514
      msg (M_INFO, "NOTE: --fast-io is disabled since we are running on Windows");
2506
#else
2515
#else
2507
      if (c->options.ce.proto != PROTO_UDPv4)
2516
      if (!proto_is_udp(c->options.ce.proto))
2508
	msg (M_INFO, "NOTE: --fast-io is disabled since we are not using UDP");
2517
	msg (M_INFO, "NOTE: --fast-io is disabled since we are not using UDP");
2509
      else
2518
      else
2510
	{
2519
	{
Lines 2764-2770 init_instance (struct context *c, const Link Here
2764
  /* link_socket_mode allows CM_CHILD_TCP
2773
  /* link_socket_mode allows CM_CHILD_TCP
2765
     instances to inherit acceptable fds
2774
     instances to inherit acceptable fds
2766
     from a top-level parent */
2775
     from a top-level parent */
2776
#ifdef USE_PF_INET6
2777
  if (c->options.ce.proto == PROTO_TCPv4_SERVER || c->options.ce.proto == PROTO_TCPv6_SERVER)
2778
#else
2767
  if (c->options.ce.proto == PROTO_TCPv4_SERVER)
2779
  if (c->options.ce.proto == PROTO_TCPv4_SERVER)
2780
#endif
2768
    {
2781
    {
2769
      if (c->mode == CM_TOP)
2782
      if (c->mode == CM_TOP)
2770
	link_socket_mode = LS_MODE_TCP_LISTEN;
2783
	link_socket_mode = LS_MODE_TCP_LISTEN;
Lines 3047-3063 inherit_context_child (struct context *d Link Here
3047
{
3060
{
3048
  CLEAR (*dest);
3061
  CLEAR (*dest);
3049
3062
3050
  switch (src->options.ce.proto)
3063
  dest->mode = proto_is_dgram(src->options.ce.proto)? CM_CHILD_UDP : CM_CHILD_TCP;
3051
    {
3052
    case PROTO_UDPv4:
3053
      dest->mode = CM_CHILD_UDP;
3054
      break;
3055
    case PROTO_TCPv4_SERVER:
3056
      dest->mode = CM_CHILD_TCP;
3057
      break;
3058
    default:
3059
      ASSERT (0);
3060
    }
3061
3064
3062
  dest->gc = gc_new ();
3065
  dest->gc = gc_new ();
3063
3066
Lines 3163-3169 Link Here
3163
  dest->c2.es_owned = false;
3166
  dest->c2.es_owned = false;
3164
  dest->c2.event_set = NULL;
3167
  dest->c2.event_set = NULL;
3165
  if (src->options.ce.proto == PROTO_UDPv4)
3168
  if (proto_is_dgram(src->options.ce.proto))
3166
    do_event_set_init (dest, false);
3169
    do_event_set_init (dest, false);
3167
}
3170
}
(-)a/manage.c (-5 / +5 lines)
Lines 1669-1677 man_settings_init (struct man_settings * Link Here
1669
      /*
1669
      /*
1670
       * Initialize socket address
1670
       * Initialize socket address
1671
       */
1671
       */
1672
      ms->local.sa.sin_family = AF_INET;
1672
      ms->local.addr.in4.sin_family = AF_INET;
1673
      ms->local.sa.sin_addr.s_addr = 0;
1673
      ms->local.addr.in4.sin_addr.s_addr = 0;
1674
      ms->local.sa.sin_port = htons (port);
1674
      ms->local.addr.in4.sin_port = htons (port);
1675
1675
1676
      /*
1676
      /*
1677
       * Run management over tunnel, or
1677
       * Run management over tunnel, or
Lines 1683-1689 man_settings_init (struct man_settings * Link Here
1683
	}
1683
	}
1684
      else
1684
      else
1685
	{
1685
	{
1686
	  ms->local.sa.sin_addr.s_addr = getaddr
1686
	  ms->local.addr.in4.sin_addr.s_addr = getaddr
1687
	    (GETADDR_RESOLVE|GETADDR_WARN_ON_SIGNAL|GETADDR_FATAL, addr, 0, NULL, NULL);
1687
	    (GETADDR_RESOLVE|GETADDR_WARN_ON_SIGNAL|GETADDR_FATAL, addr, 0, NULL, NULL);
1688
	}
1688
	}
1689
      
1689
      
Lines 2022-2028 management_post_tunnel_open (struct mana Link Here
2022
      && man->connection.state == MS_INITIAL)
2022
      && man->connection.state == MS_INITIAL)
2023
    {
2023
    {
2024
      /* listen on our local TUN/TAP IP address */
2024
      /* listen on our local TUN/TAP IP address */
2025
      man->settings.local.sa.sin_addr.s_addr = htonl (tun_local_ip);
2025
      man->settings.local.addr.in4.sin_addr.s_addr = htonl (tun_local_ip);
2026
      man_connection_init (man);
2026
      man_connection_init (man);
2027
    }
2027
    }
2028
2028
(-)a/mroute.c (-4 / +56 lines)
Lines 226-250 bool mroute_extract_openvpn_sockaddr (st Link Here
226
				      const struct openvpn_sockaddr *osaddr,
226
				      const struct openvpn_sockaddr *osaddr,
227
				      bool use_port)
227
				      bool use_port)
228
{
228
{
229
  if (osaddr->sa.sin_family == AF_INET)
229
  switch (osaddr->addr.sa.sa_family) 
230
  {
231
    case AF_INET:
230
    {
232
    {
231
      if (use_port)
233
      if (use_port)
232
	{
234
	{
233
	  addr->type = MR_ADDR_IPV4 | MR_WITH_PORT;
235
	  addr->type = MR_ADDR_IPV4 | MR_WITH_PORT;
234
	  addr->netbits = 0;
236
	  addr->netbits = 0;
235
	  addr->len = 6;
237
	  addr->len = 6;
236
	  memcpy (addr->addr, &osaddr->sa.sin_addr.s_addr, 4);
238
	  memcpy (addr->addr, &osaddr->addr.in4.sin_addr.s_addr, 4);
237
	  memcpy (addr->addr + 4, &osaddr->sa.sin_port, 2);
239
	  memcpy (addr->addr + 4, &osaddr->addr.in4.sin_port, 2);
238
	}
240
	}
239
      else
241
      else
240
	{
242
	{
241
	  addr->type = MR_ADDR_IPV4;
243
	  addr->type = MR_ADDR_IPV4;
242
	  addr->netbits = 0;
244
	  addr->netbits = 0;
243
	  addr->len = 4;
245
	  addr->len = 4;
244
	  memcpy (addr->addr, &osaddr->sa.sin_addr.s_addr, 4);
246
	  memcpy (addr->addr, &osaddr->addr.in4.sin_addr.s_addr, 4);
245
	}
247
	}
246
      return true;
248
      return true;
247
    }
249
    }
250
#ifdef USE_PF_INET6
251
    case AF_INET6:
252
      if (use_port)
253
	{
254
	  addr->type = MR_ADDR_IPV6 | MR_WITH_PORT;
255
	  addr->netbits = 0;
256
	  addr->len = 18;
257
	  memcpy (addr->addr, &osaddr->addr.in6.sin6_addr, 16);
258
	  memcpy (addr->addr + 16, &osaddr->addr.in6.sin6_port, 2);
259
	}
260
      else
261
	{
262
	  addr->type = MR_ADDR_IPV6;
263
	  addr->netbits = 0;
264
	  addr->len = 16;
265
	  memcpy (addr->addr, &osaddr->addr.in6.sin6_addr, 16);
266
	}
267
      return true;
268
#endif
269
  }
248
  return false;
270
  return false;
249
}
271
}
250
272
Lines 337-343 mroute_addr_print (const struct mroute_a Link Here
337
	  }
359
	  }
338
	  break;
360
	  break;
339
	case MR_ADDR_IPV6:
361
	case MR_ADDR_IPV6:
362
#ifdef USE_PF_INET6
363
          {
364
	    struct buffer buf;
365
	    struct sockaddr_in6 sin6;
366
	    int port;
367
	    char buf6[INET6_ADDRSTRLEN] = "";
368
	    memset(&sin6, 0, sizeof sin6);
369
	    sin6.sin6_family = AF_INET6;
370
	    buf_set_read (&buf, maddr.addr, maddr.len);
371
            if (buf_read(&buf, &sin6.sin6_addr, sizeof (sin6.sin6_addr)))
372
            {
373
              if (getnameinfo((struct sockaddr *)&sin6, sizeof (struct sockaddr_in6),
374
                                      buf6, sizeof (buf6), NULL, 0, NI_NUMERICHOST) != 0)
375
                {
376
                  buf_printf (&out, "MR_ADDR_IPV6 getnameinfo() err");
377
                  break;
378
		}
379
              buf_puts (&out, buf6);
380
	      if (maddr.type & MR_WITH_NETBITS)
381
	        buf_printf (&out, "/%d", maddr.netbits);
382
              if (maddr.type & MR_WITH_PORT)
383
                {
384
                  port = buf_read_u16 (&buf);
385
                  if (port >= 0)
386
                    buf_printf (&out, ":%d", port);
387
                }
388
	    }
389
          }
390
#else /* old pre IPV6 1-line code: */
340
	  buf_printf (&out, "IPV6"); 
391
	  buf_printf (&out, "IPV6"); 
392
#endif
341
	  break;
393
	  break;
342
	default:
394
	default:
343
	  buf_printf (&out, "UNKNOWN"); 
395
	  buf_printf (&out, "UNKNOWN"); 
(-)a/mtcp.c (+1 lines)
Lines 153-158 multi_tcp_instance_specific_init (struct Link Here
153
  ASSERT (mi->context.c2.link_socket);
153
  ASSERT (mi->context.c2.link_socket);
154
  ASSERT (mi->context.c2.link_socket->info.lsa);
154
  ASSERT (mi->context.c2.link_socket->info.lsa);
155
  ASSERT (mi->context.c2.link_socket->mode == LS_MODE_TCP_ACCEPT_FROM);
155
  ASSERT (mi->context.c2.link_socket->mode == LS_MODE_TCP_ACCEPT_FROM);
156
  ASSERT (mi->context.c2.link_socket->info.lsa->actual.dest.addr.sa.sa_family == AF_INET);
156
  if (!mroute_extract_openvpn_sockaddr (&mi->real, &mi->context.c2.link_socket->info.lsa->actual.dest, true))
157
  if (!mroute_extract_openvpn_sockaddr (&mi->real, &mi->context.c2.link_socket->info.lsa->actual.dest, true))
157
    {
158
    {
158
      msg (D_MULTI_ERRORS, "MULTI TCP: TCP client address is undefined");
159
      msg (D_MULTI_ERRORS, "MULTI TCP: TCP client address is undefined");
(-)a/multi.c (-15 / +10 lines)
Lines 1053-1060 multi_learn_in_addr_t (struct multi_cont Link Here
1053
  struct mroute_addr addr;
1053
  struct mroute_addr addr;
1054
1054
1055
  CLEAR (remote_si);
1055
  CLEAR (remote_si);
1056
  remote_si.sa.sin_family = AF_INET;
1056
  remote_si.addr.in4.sin_family = AF_INET;
1057
  remote_si.sa.sin_addr.s_addr = htonl (a);
1057
  remote_si.addr.in4.sin_addr.s_addr = htonl (a);
1058
  ASSERT (mroute_extract_openvpn_sockaddr (&addr, &remote_si, false));
1058
  ASSERT (mroute_extract_openvpn_sockaddr (&addr, &remote_si, false));
1059
1059
1060
  if (netbits >= 0)
1060
  if (netbits >= 0)
Lines 2464-2472 management_callback_kill_by_addr (void * Link Here
2464
  int count = 0;
2464
  int count = 0;
2465
2465
2466
  CLEAR (saddr);
2466
  CLEAR (saddr);
2467
  saddr.sa.sin_family = AF_INET;
2467
  saddr.addr.in4.sin_family = AF_INET;
2468
  saddr.sa.sin_addr.s_addr = htonl (addr);
2468
  saddr.addr.in4.sin_addr.s_addr = htonl (addr);
2469
  saddr.sa.sin_port = htons (port);
2469
  saddr.addr.in4.sin_port = htons (port);
2470
  if (mroute_extract_openvpn_sockaddr (&maddr, &saddr, true))
2470
  if (mroute_extract_openvpn_sockaddr (&maddr, &saddr, true))
2471
    {
2471
    {
2472
      hash_iterator_init (m->iter, &hi, true);
2472
      hash_iterator_init (m->iter, &hi, true);
Lines 2614-2631 tunnel_server (struct context *top) Link Here
2614
{
2614
{
2615
  ASSERT (top->options.mode == MODE_SERVER);
2615
  ASSERT (top->options.mode == MODE_SERVER);
2616
2616
2617
  switch (top->options.ce.proto) {
2617
  if (proto_is_dgram(top->options.ce.proto))
2618
  case PROTO_UDPv4:
2618
    tunnel_server_udp(top);
2619
    tunnel_server_udp (top);
2619
  else
2620
    break;
2620
    tunnel_server_tcp(top);
2621
  case PROTO_TCPv4_SERVER:
2622
    tunnel_server_tcp (top);
2623
    break;
2624
  default:
2625
    ASSERT (0);
2626
  }
2627
}
2621
}
2628
2622
2623
2629
#else
2624
#else
2630
static void dummy(void) {}
2625
static void dummy(void) {}
2631
#endif /* P2MP_SERVER */
2626
#endif /* P2MP_SERVER */
(-)a/occ.c (-1 / +1 lines)
Lines 369-375 process_received_occ_msg (struct context Link Here
369
	       c->c2.max_send_size_remote,
369
	       c->c2.max_send_size_remote,
370
	       c->c2.max_recv_size_local);
370
	       c->c2.max_recv_size_local);
371
	  if (!c->options.fragment
371
	  if (!c->options.fragment
372
	      && c->options.ce.proto == PROTO_UDPv4
372
	      && (proto_is_dgram(c->options.ce.proto))
373
	      && c->c2.max_send_size_local > TUN_MTU_MIN
373
	      && c->c2.max_send_size_local > TUN_MTU_MIN
374
	      && (c->c2.max_recv_size_remote < c->c2.max_send_size_local
374
	      && (c->c2.max_recv_size_remote < c->c2.max_send_size_local
375
		  || c->c2.max_recv_size_local < c->c2.max_send_size_remote))
375
		  || c->c2.max_recv_size_local < c->c2.max_send_size_remote))
(-)a/ps.c (-3 / +3 lines)
Lines 332-340 sock_addr_set (struct openvpn_sockaddr * Link Here
332
	       const int port)
332
	       const int port)
333
{
333
{
334
  CLEAR (*osaddr);
334
  CLEAR (*osaddr);
335
  osaddr->sa.sin_family = AF_INET;
335
  osaddr->addr.in4.sin_family = AF_INET;
336
  osaddr->sa.sin_addr.s_addr = htonl (addr);
336
  osaddr->addr.in4.sin_addr.s_addr = htonl (addr);
337
  osaddr->sa.sin_port = htons (port);
337
  osaddr->addr.in4.sin_port = htons (port);
338
}
338
}
339
339
340
static inline void
340
static inline void
(-)a/socket.h (-31 / +202 lines)
Lines 70-76 #define ntohps(x) ntohs(x) Link Here
70
struct openvpn_sockaddr
70
struct openvpn_sockaddr
71
{
71
{
72
  /*int dummy;*/ /* add offset to force a bug if sa not explicitly dereferenced */
72
  /*int dummy;*/ /* add offset to force a bug if sa not explicitly dereferenced */
73
  struct sockaddr_in sa;
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;
74
};
80
};
75
81
76
/* actual address of remote, based on source address of received packets */
82
/* actual address of remote, based on source address of received packets */
Lines 79-85 { Link Here
79
  /*int dummy;*/ /* add offset to force a bug if dest not explicitly dereferenced */
85
  /*int dummy;*/ /* add offset to force a bug if dest not explicitly dereferenced */
80
  struct openvpn_sockaddr dest;
86
  struct openvpn_sockaddr dest;
81
#if ENABLE_IP_PKTINFO
87
#if ENABLE_IP_PKTINFO
82
  struct in_pktinfo pi;
88
  union {
89
    struct in_pktinfo in4;
90
#ifdef USE_PF_INET6
91
    struct in6_pktinfo in6;
92
#endif
93
  } pi;
83
#endif
94
#endif
84
};
95
};
85
96
Lines 403-408 socket_descriptor_t create_socket_tcp (v Link Here
403
socket_descriptor_t socket_do_accept (socket_descriptor_t sd,
414
socket_descriptor_t socket_do_accept (socket_descriptor_t sd,
404
				      struct link_socket_actual *act,
415
				      struct link_socket_actual *act,
405
				      const bool nowait);
416
				      const bool nowait);
417
/*
418
 * proto related
419
 */
420
bool proto_is_net(int proto);
421
bool proto_is_dgram(int proto);
422
bool proto_is_udp(int proto);
423
bool proto_is_tcp(int proto);
424
406
425
407
/*
426
/*
408
 * DNS resolution
427
 * DNS resolution
Lines 428-450 in_addr_t getaddr (unsigned int flags, Link Here
428
 * Transport protocol naming and other details.
447
 * Transport protocol naming and other details.
429
 */
448
 */
430
449
431
#define PROTO_UDPv4        0
450
#if 0 /* PRE UDPv6/TCPv6 code */
432
#define PROTO_TCPv4_SERVER 1
451
#define PROTO_NONE         0 /* catch for uninitialized */
433
#define PROTO_TCPv4_CLIENT 2
452
#define PROTO_UDPv4        1
434
#define PROTO_TCPv4        3
453
#define PROTO_TCPv4_SERVER 2
435
#define PROTO_N            4
454
#define PROTO_TCPv4_CLIENT 3
455
#define PROTO_TCPv4        4
456
#define PROTO_UDPv6        5
457
#define PROTO_TCPv6_SERVER 6
458
#define PROTO_TCPv6_CLIENT 7
459
#define PROTO_TCPv6        8
460
#define PROTO_N            9
461
#endif /* if 0 */
462
463
/* 
464
 * Use enum's instead of #define to allow for easier
465
 * optional proto support
466
 */
467
enum proto_num {
468
	PROTO_NONE, /* catch for uninitialized */
469
	PROTO_UDPv4,
470
	PROTO_TCPv4_SERVER,
471
	PROTO_TCPv4_CLIENT,
472
	PROTO_TCPv4,
473
	PROTO_UDPv6,
474
	PROTO_TCPv6_SERVER,
475
	PROTO_TCPv6_CLIENT,
476
	PROTO_TCPv6,
477
	PROTO_N
478
};
436
479
437
int ascii2proto (const char* proto_name);
480
int ascii2proto (const char* proto_name);
438
const char *proto2ascii (int proto, bool display_form);
481
const char *proto2ascii (int proto, bool display_form);
439
const char *proto2ascii_all (struct gc_arena *gc);
482
const char *proto2ascii_all (struct gc_arena *gc);
440
int proto_remote (int proto, bool remote);
483
int proto_remote (int proto, bool remote);
484
const char *addr_family_name(int af);
441
485
442
/*
486
/*
443
 * Overhead added to packets by various protocols.
487
 * Overhead added to packets by various protocols.
444
 */
488
 */
445
#define IPv4_UDP_HEADER_SIZE              28
489
#define IPv4_UDP_HEADER_SIZE              28
446
#define IPv4_TCP_HEADER_SIZE              40
490
#define IPv4_TCP_HEADER_SIZE              40
447
#define IPv6_UDP_HEADER_SIZE              40
491
#define IPv6_UDP_HEADER_SIZE              48
492
#define IPv6_TCP_HEADER_SIZE              60
448
493
449
extern const int proto_overhead[];
494
extern const int proto_overhead[];
450
495
Lines 468-474 legal_ipv4_port (int port) Link Here
468
static inline bool
513
static inline bool
469
link_socket_proto_connection_oriented (int proto)
514
link_socket_proto_connection_oriented (int proto)
470
{
515
{
471
  return proto == PROTO_TCPv4_SERVER || proto == PROTO_TCPv4_CLIENT;
516
  return !proto_is_dgram(proto);
472
}
517
}
473
518
474
static inline bool
519
static inline bool
Lines 483-489 link_socket_connection_oriented (const s Link Here
483
static inline bool
528
static inline bool
484
addr_defined (const struct openvpn_sockaddr *addr)
529
addr_defined (const struct openvpn_sockaddr *addr)
485
{
530
{
486
  return addr->sa.sin_addr.s_addr != 0;
531
  if (!addr) return 0;
532
  switch (addr->addr.sa.sa_family) {
533
    case AF_INET: return addr->addr.in4.sin_addr.s_addr != 0;
534
#ifdef USE_PF_INET6
535
    case AF_INET6: return !IN6_IS_ADDR_UNSPECIFIED(&addr->addr.in6.sin6_addr);
536
#endif
537
    default: return 0;
538
  }
539
}
540
static inline bool
541
addr_defined_ipi (const struct link_socket_actual *lsa)
542
{
543
#if ENABLE_IP_PKTINFO
544
  if (!lsa) return 0;
545
  switch (lsa->dest.addr.sa.sa_family) {
546
    case AF_INET: return lsa->pi.in4.ipi_spec_dst.s_addr != 0;
547
#ifdef USE_PF_INET6
548
    case AF_INET6: return !IN6_IS_ADDR_UNSPECIFIED(&lsa->pi.in6.ipi6_addr);
549
#endif
550
    default: return 0;
551
  }
552
#else
553
  ASSERT(0);
554
#endif
487
}
555
}
488
556
489
static inline bool
557
static inline bool
Lines 495-514 link_socket_actual_defined (const struct Link Here
495
static inline bool
563
static inline bool
496
addr_match (const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
564
addr_match (const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
497
{
565
{
498
  return a1->sa.sin_addr.s_addr == a2->sa.sin_addr.s_addr;
566
  switch(a1->addr.sa.sa_family) {
567
    case AF_INET:
568
      return a1->addr.in4.sin_addr.s_addr == a2->addr.in4.sin_addr.s_addr;
569
#ifdef USE_PF_INET6
570
    case AF_INET6:
571
      return IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, &a2->addr.in6.sin6_addr);
572
#endif
573
  }
574
  ASSERT(0);
575
  return false;
499
}
576
}
500
577
501
static inline in_addr_t
578
static inline in_addr_t
502
addr_host (const struct openvpn_sockaddr *s)
579
addr_host (const struct openvpn_sockaddr *addr)
503
{
580
{
504
  return ntohl (s->sa.sin_addr.s_addr);
581
  /* 
582
   * "public" addr returned is checked against ifconfig for
583
   * possible clash: non sense for now given
584
   * that we do ifconfig only IPv4
585
   */
586
#if defined(USE_PF_INET6) 
587
  if(addr->addr.sa.sa_family != AF_INET)
588
    return 0;
589
#else 
590
  ASSERT(addr->addr.sa.sa_family == AF_INET);
591
#endif
592
  return ntohl (addr->addr.in4.sin_addr.s_addr);
505
}
593
}
506
594
507
static inline bool
595
static inline bool
508
addr_port_match (const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
596
addr_port_match (const struct openvpn_sockaddr *a1, const struct openvpn_sockaddr *a2)
509
{
597
{
510
  return a1->sa.sin_addr.s_addr == a2->sa.sin_addr.s_addr
598
  switch(a1->addr.sa.sa_family) {
511
    && a1->sa.sin_port == a2->sa.sin_port;
599
    case AF_INET:
600
      return a1->addr.in4.sin_addr.s_addr == a2->addr.in4.sin_addr.s_addr
601
	&& a1->addr.in4.sin_port == a2->addr.in4.sin_port;
602
#ifdef USE_PF_INET6
603
    case AF_INET6:
604
      return IN6_ARE_ADDR_EQUAL(&a1->addr.in6.sin6_addr, &a2->addr.in6.sin6_addr) 
605
	&& a1->addr.in6.sin6_port == a2->addr.in6.sin6_port;
606
#endif
607
  }
608
  ASSERT(0);
609
  return false;
512
}
610
}
513
611
514
static inline bool
612
static inline bool
Lines 521-526 addr_match_proto (const struct openvpn_s Link Here
521
    : addr_port_match (a1, a2);
619
    : addr_port_match (a1, a2);
522
}
620
}
523
621
622
static inline void
623
addr_zero_host(struct openvpn_sockaddr *addr)
624
{
625
   switch(addr->addr.sa.sa_family) {
626
     case AF_INET:
627
       addr->addr.in4.sin_addr.s_addr = 0;
628
       break;
629
#ifdef USE_PF_INET6
630
     case AF_INET6: 
631
       memset(&addr->addr.in6.sin6_addr, 0, sizeof (struct in6_addr));
632
       break;
633
#endif
634
   }
635
}
636
637
static inline void
638
addr_copy_sa(struct openvpn_sockaddr *dst, const struct openvpn_sockaddr *src)
639
{
640
  dst->addr = src->addr;
641
}
642
643
static inline void
644
addr_copy_host(struct openvpn_sockaddr *dst, const struct openvpn_sockaddr *src)
645
{
646
   switch(src->addr.sa.sa_family) {
647
     case AF_INET:
648
       dst->addr.in4.sin_addr.s_addr = src->addr.in4.sin_addr.s_addr;
649
       break;
650
#ifdef USE_PF_INET6
651
     case AF_INET6: 
652
       dst->addr.in6.sin6_addr = src->addr.in6.sin6_addr;
653
       break;
654
#endif
655
   }
656
}
657
658
static inline bool
659
addr_inet4or6(struct sockaddr *addr)
660
{
661
	return addr->sa_family == AF_INET || addr->sa_family == AF_INET6;
662
}
663
664
int addr_guess_family(int proto, const char *name);
665
static inline int
666
af_addr_size(sa_family_t af)
667
{
668
#if defined(USE_PF_INET6) || defined (USE_PF_UNIX)
669
   switch(af) {
670
     case AF_INET: return sizeof (struct sockaddr_in);
671
#ifdef USE_PF_UNIX
672
     case AF_UNIX: return sizeof (struct sockaddr_un);
673
#endif
674
#ifdef USE_PF_INET6
675
     case AF_INET6: return sizeof (struct sockaddr_in6);
676
#endif
677
     default: 
678
#if 0
679
      /* could be called from socket_do_accept() with empty addr */
680
      msg (M_ERR, "Bad address family: %d\n", addr->sa_family);
681
      ASSERT(0);
682
#endif
683
     	return 0;
684
   }
685
#else /* only AF_INET */
686
   return sizeof(struct sockaddr_in);
687
#endif
688
}
689
524
static inline bool
690
static inline bool
525
link_socket_actual_match (const struct link_socket_actual *a1, const struct link_socket_actual *a2)
691
link_socket_actual_match (const struct link_socket_actual *a1, const struct link_socket_actual *a2)
526
{
692
{
Lines 577-590 link_socket_verify_incoming_addr (struct Link Here
577
{
743
{
578
  if (buf->len > 0)
744
  if (buf->len > 0)
579
    {
745
    {
580
      if (from_addr->dest.sa.sin_family != AF_INET)
746
      switch (from_addr->dest.addr.sa.sa_family) {
581
	return false;
747
#ifdef USE_PF_INET6
582
      if (!link_socket_actual_defined (from_addr))
748
	case AF_INET6:
583
	return false;
749
#endif
584
      if (info->remote_float || !addr_defined (&info->lsa->remote))
750
	case AF_INET:
585
	return true;
751
	  if (!link_socket_actual_defined (from_addr))
586
      if (addr_match_proto (&from_addr->dest, &info->lsa->remote, info->proto))
752
	    return false;
587
	return true;
753
	  if (info->remote_float || !addr_defined (&info->lsa->remote))
754
	    return true;
755
	  if (addr_match_proto (&from_addr->dest, &info->lsa->remote, info->proto))
756
	    return true;
757
      }
588
    }
758
    }
589
  return false;
759
  return false;
590
}
760
}
Lines 690-696 link_socket_read (struct link_socket *so Link Here
690
		  int maxsize,
860
		  int maxsize,
691
		  struct link_socket_actual *from)
861
		  struct link_socket_actual *from)
692
{
862
{
693
  if (sock->info.proto == PROTO_UDPv4)
863
  if (proto_is_udp(sock->info.proto)) /* unified UDPv4 and UDPv6 */
694
    {
864
    {
695
      int res;
865
      int res;
696
866
Lines 701-710 #else Link Here
701
#endif
871
#endif
702
      return res;
872
      return res;
703
    }
873
    }
704
  else if (sock->info.proto == PROTO_TCPv4_SERVER || sock->info.proto == PROTO_TCPv4_CLIENT)
874
  else if (proto_is_tcp(sock->info.proto)) /* unified TCPv4 and TCPv6 */
705
    {
875
    {
706
      /* from address was returned by accept */
876
      /* from address was returned by accept */
707
      from->dest.sa = sock->info.lsa->actual.dest.sa;
877
      addr_copy_sa(&from->dest, &sock->info.lsa->actual.dest);
708
      return link_socket_read_tcp (sock, buf);
878
      return link_socket_read_tcp (sock, buf);
709
    }
879
    }
710
  else
880
  else
Lines 759-771 #if ENABLE_IP_PKTINFO Link Here
759
					   struct buffer *buf,
929
					   struct buffer *buf,
760
					   struct link_socket_actual *to);
930
					   struct link_socket_actual *to);
761
931
762
  if (sock->sockflags & SF_USE_IP_PKTINFO)
932
  if (proto_is_udp(sock->info.proto) && (sock->sockflags & SF_USE_IP_PKTINFO)
933
	  && addr_defined_ipi(to))
763
    return link_socket_write_udp_posix_sendmsg (sock, buf, to);
934
    return link_socket_write_udp_posix_sendmsg (sock, buf, to);
764
  else
935
  else
765
#endif
936
#endif
766
    return sendto (sock->sd, BPTR (buf), BLEN (buf), 0,
937
    return sendto (sock->sd, BPTR (buf), BLEN (buf), 0,
767
		   (struct sockaddr *) &to->dest.sa,
938
		   (struct sockaddr *) &to->dest.addr.sa,
768
		   (socklen_t) sizeof (to->dest.sa));
939
		   (socklen_t) af_addr_size(to->dest.addr.sa.sa_family));
769
}
940
}
770
941
771
static inline int
942
static inline int
Lines 796-806 link_socket_write (struct link_socket *s Link Here
796
		   struct buffer *buf,
967
		   struct buffer *buf,
797
		   struct link_socket_actual *to)
968
		   struct link_socket_actual *to)
798
{
969
{
799
  if (sock->info.proto == PROTO_UDPv4)
970
  if (proto_is_udp(sock->info.proto)) /* unified UDPv4 and UDPv6 */
800
    {
971
    {
801
      return link_socket_write_udp (sock, buf, to);
972
      return link_socket_write_udp (sock, buf, to);
802
    }
973
    }
803
  else if (sock->info.proto == PROTO_TCPv4_SERVER || sock->info.proto == PROTO_TCPv4_CLIENT)
974
  else if (proto_is_tcp(sock->info.proto)) /* unified TCPv4 and TCPv6 */
804
    {
975
    {
805
      return link_socket_write_tcp (sock, buf, to);
976
      return link_socket_write_tcp (sock, buf, to);
806
    }
977
    }
(-)a/socks.c (-9 / +9 lines)
Lines 174-182 recv_socks_reply (socket_descriptor_t sd Link Here
174
174
175
  if (addr != NULL)
175
  if (addr != NULL)
176
    {
176
    {
177
      addr->sa.sin_family = AF_INET;
177
      addr->addr.in4.sin_family = AF_INET;
178
      addr->sa.sin_addr.s_addr = htonl (INADDR_ANY);
178
      addr->addr.in4.sin_addr.s_addr = htonl (INADDR_ANY);
179
      addr->sa.sin_port = htons (0);
179
      addr->addr.in4.sin_port = htons (0);
180
    }
180
    }
181
181
182
  while (len < 4 + alen + 2)
182
  while (len < 4 + alen + 2)
Lines 263-270 recv_socks_reply (socket_descriptor_t sd Link Here
263
  /* ATYP == 1 (IP V4 address) */
263
  /* ATYP == 1 (IP V4 address) */
264
  if (atyp == '\x01' && addr != NULL)
264
  if (atyp == '\x01' && addr != NULL)
265
    {
265
    {
266
      memcpy (&addr->sa.sin_addr, buf + 4, sizeof (addr->sa.sin_addr));
266
      memcpy (&addr->addr.in4.sin_addr, buf + 4, sizeof (addr->addr.in4.sin_addr));
267
      memcpy (&addr->sa.sin_port, buf + 8, sizeof (addr->sa.sin_port));
267
      memcpy (&addr->addr.in4.sin_port, buf + 8, sizeof (addr->addr.in4.sin_port));
268
    }
268
    }
269
269
270
270
Lines 382-389 socks_process_incoming_udp (struct buffe Link Here
382
  if (atyp != 1)		/* ATYP == 1 (IP V4) */
382
  if (atyp != 1)		/* ATYP == 1 (IP V4) */
383
    goto error;
383
    goto error;
384
384
385
  buf_read (buf, &from->dest.sa.sin_addr, sizeof (from->dest.sa.sin_addr));
385
  buf_read (buf, &from->dest.addr.in4.sin_addr, sizeof (from->dest.addr.in4.sin_addr));
386
  buf_read (buf, &from->dest.sa.sin_port, sizeof (from->dest.sa.sin_port));
386
  buf_read (buf, &from->dest.addr.in4.sin_port, sizeof (from->dest.addr.in4.sin_port));
387
387
388
  return;
388
  return;
389
389
Lines 415-422 socks_process_outgoing_udp (struct buffe Link Here
415
  buf_write_u16 (&head, 0);	/* RSV = 0 */
415
  buf_write_u16 (&head, 0);	/* RSV = 0 */
416
  buf_write_u8 (&head, 0);	/* FRAG = 0 */
416
  buf_write_u8 (&head, 0);	/* FRAG = 0 */
417
  buf_write_u8 (&head, '\x01'); /* ATYP = 1 (IP V4) */
417
  buf_write_u8 (&head, '\x01'); /* ATYP = 1 (IP V4) */
418
  buf_write (&head, &to->dest.sa.sin_addr, sizeof (to->dest.sa.sin_addr));
418
  buf_write (&head, &to->dest.addr.in4.sin_addr, sizeof (to->dest.addr.in4.sin_addr));
419
  buf_write (&head, &to->dest.sa.sin_port, sizeof (to->dest.sa.sin_port));
419
  buf_write (&head, &to->dest.addr.in4.sin_port, sizeof (to->dest.addr.in4.sin_port));
420
420
421
  return 10;
421
  return 10;
422
}
422
}
(-)a/socket.c (-208 / +613 lines)
Lines 36-45 Link Here
36
#include "memdbg.h"
36
#include "memdbg.h"
37
37
38
const int proto_overhead[] = { /* indexed by PROTO_x */
38
const int proto_overhead[] = { /* indexed by PROTO_x */
39
  IPv4_UDP_HEADER_SIZE,
39
  0,
40
  IPv4_UDP_HEADER_SIZE, /* IPv4 */
40
  IPv4_TCP_HEADER_SIZE,
41
  IPv4_TCP_HEADER_SIZE,
41
  IPv4_TCP_HEADER_SIZE,
42
  IPv4_TCP_HEADER_SIZE,
42
  IPv4_TCP_HEADER_SIZE
43
#ifdef USE_PF_INET6
44
  IPv6_UDP_HEADER_SIZE, /* IPv6 */
45
  IPv6_TCP_HEADER_SIZE,
46
  IPv6_TCP_HEADER_SIZE,
47
  IPv6_TCP_HEADER_SIZE,
48
#endif
43
};
49
};
44
50
45
/*
51
/*
Lines 299-318 Link Here
299
	       struct openvpn_sockaddr *addr,
305
	       struct openvpn_sockaddr *addr,
300
	       bool *changed)
306
	       bool *changed)
301
{
307
{
302
  if (host && addr)
308
  switch(addr->addr.sa.sa_family) {
303
    {
309
    case AF_INET:
304
      const in_addr_t new_addr = getaddr (
310
      if (host && addr)
305
					  GETADDR_RESOLVE|GETADDR_UPDATE_MANAGEMENT_STATE,
311
      {
306
					  host,
312
	const in_addr_t new_addr = getaddr (
307
					  1,
313
	    GETADDR_RESOLVE|GETADDR_UPDATE_MANAGEMENT_STATE,
308
					  NULL,
314
	    host,
309
					  NULL);
315
	    1,
310
      if (new_addr && addr->sa.sin_addr.s_addr != new_addr)
316
	    NULL,
317
	    NULL);
318
	if (new_addr && addr->addr.in4.sin_addr.s_addr != new_addr)
311
	{
319
	{
312
	  addr->sa.sin_addr.s_addr = new_addr;
320
	  addr->addr.in4.sin_addr.s_addr = new_addr;
313
	  *changed = true;
321
	  *changed = true;
314
	}
322
	}
315
    }
323
      }
324
      break;
325
#ifdef USE_PF_INET6
326
    case AF_INET6: /* jjoFIXME: should adapt getaddr() for AF_INET6 */
327
      if (host && addr)
328
      {
329
	struct addrinfo hints , *ai;
330
	int err;
331
	memset(&hints, 0, sizeof hints);
332
	hints.ai_flags=AI_PASSIVE;
333
	hints.ai_family=AF_INET6;
334
	if ((err=getaddrinfo(host, NULL, &hints, &ai))==0)
335
	{
336
	  struct sockaddr_in6 *sin6=(struct sockaddr_in6*)ai->ai_addr;
337
	  if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &addr->addr.in6.sin6_addr))
338
	  {
339
	    int port=addr->addr.in6.sin6_port; /* backup current port for easier copy, restore later */
340
	    addr->addr.in6=*sin6; /* ipv6 requires also eg. sin6_scope_id => easy to full copy*/
341
	    addr->addr.in6.sin6_port=port;
342
	  }
343
	  freeaddrinfo(ai);
344
	}
345
      }
346
      break;
347
#endif
348
    default:
349
    	ASSERT(0);
350
  }
316
}
351
}
317
352
318
static int
353
static int
Lines 505-510 Link Here
505
  return sd;
540
  return sd;
506
}
541
}
507
542
543
#ifdef USE_PF_INET6
544
static socket_descriptor_t
545
create_socket_udp6 (const unsigned int flags)
546
{
547
  socket_descriptor_t sd;
548
549
  if ((sd = socket (PF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
550
    msg (M_SOCKERR, "UDP: Cannot create UDP6 socket");
551
#if ENABLE_IP_PKTINFO
552
  else if (flags & SF_USE_IP_PKTINFO)
553
    {
554
      int pad = 1;
555
      setsockopt (sd, IPPROTO_IPV6, IPV6_PKTINFO, (void*)&pad, sizeof(pad));
556
    }
557
#endif
558
  return sd;
559
}
560
561
static socket_descriptor_t
562
create_socket_tcp6 (void)
563
{
564
  socket_descriptor_t sd;
565
566
  if ((sd = socket (PF_INET6, SOCK_STREAM, IPPROTO_TCP)) < 0)
567
    msg (M_SOCKERR, "Cannot create TCP6 socket");
568
569
  /* set SO_REUSEADDR on socket */
570
  {
571
    int on = 1;
572
    if (setsockopt (sd, SOL_SOCKET, SO_REUSEADDR,
573
		    (void *) &on, sizeof (on)) < 0)
574
      msg (M_SOCKERR, "TCP: Cannot setsockopt SO_REUSEADDR on TCP6 socket");
575
  }
576
577
  return sd;
578
}
579
580
#endif
508
static void
581
static void
509
create_socket (struct link_socket *sock)
582
create_socket (struct link_socket *sock)
510
{
583
{
Lines 523-528 Link Here
523
    {
596
    {
524
      sock->sd = create_socket_tcp ();
597
      sock->sd = create_socket_tcp ();
525
    }
598
    }
599
#ifdef USE_PF_INET6
600
  else if (sock->info.proto == PROTO_TCPv6_SERVER
601
	   || sock->info.proto == PROTO_TCPv6_CLIENT)
602
    {
603
      sock->sd = create_socket_tcp6 ();
604
    }
605
  else if (sock->info.proto == PROTO_UDPv6)
606
    {
607
      sock->sd = create_socket_udp6 (sock->sockflags);
608
    }
609
#endif
526
  else
610
  else
527
    {
611
    {
528
      ASSERT (0);
612
      ASSERT (0);
Lines 560-566 Link Here
560
		  struct link_socket_actual *act,
644
		  struct link_socket_actual *act,
561
		  const bool nowait)
645
		  const bool nowait)
562
{
646
{
563
  socklen_t remote_len = sizeof (act->dest.sa);
647
  /* af_addr_size WILL return 0 in this case if AFs other than AF_INET
648
   * are compiled because act is empty here.
649
   * could use getsockname() to support later remote_len check
650
   */
651
  socklen_t remote_len_af = af_addr_size(act->dest.addr.sa.sa_family);
652
  socklen_t remote_len = sizeof(act->dest.addr);
564
  socket_descriptor_t new_sd = SOCKET_UNDEFINED;
653
  socket_descriptor_t new_sd = SOCKET_UNDEFINED;
565
654
566
  CLEAR (*act);
655
  CLEAR (*act);
Lines 568-574 Link Here
568
#ifdef HAVE_GETPEERNAME
657
#ifdef HAVE_GETPEERNAME
569
  if (nowait)
658
  if (nowait)
570
    {
659
    {
571
      new_sd = getpeername (sd, (struct sockaddr *) &act->dest.sa, &remote_len);
660
      new_sd = getpeername (sd, &act->dest.addr.sa, &remote_len);
572
661
573
      if (!socket_defined (new_sd))
662
      if (!socket_defined (new_sd))
574
	msg (D_LINK_ERRORS | M_ERRNO_SOCK, "TCP: getpeername() failed");
663
	msg (D_LINK_ERRORS | M_ERRNO_SOCK, "TCP: getpeername() failed");
Lines 581-587 Link Here
581
#endif
670
#endif
582
  else
671
  else
583
    {
672
    {
584
      new_sd = accept (sd, (struct sockaddr *) &act->dest.sa, &remote_len);
673
      new_sd = accept (sd, &act->dest.addr.sa, &remote_len);
585
    }
674
    }
586
675
587
#if 0 /* For debugging only, test the effect of accept() failures */
676
#if 0 /* For debugging only, test the effect of accept() failures */
Lines 597-603 Link Here
597
    {
686
    {
598
      msg (D_LINK_ERRORS | M_ERRNO_SOCK, "TCP: accept(%d) failed", sd);
687
      msg (D_LINK_ERRORS | M_ERRNO_SOCK, "TCP: accept(%d) failed", sd);
599
    }
688
    }
600
  else if (remote_len != sizeof (act->dest.sa))
689
  /* only valid if we have remote_len_af!=0 */
690
  else if (remote_len_af && remote_len != remote_len_af)
601
    {
691
    {
602
      msg (D_LINK_ERRORS, "TCP: Received strange incoming connection with unknown address length=%d", remote_len);
692
      msg (D_LINK_ERRORS, "TCP: Received strange incoming connection with unknown address length=%d", remote_len);
603
      openvpn_close_socket (new_sd);
693
      openvpn_close_socket (new_sd);
Lines 698-704 Link Here
698
{
788
{
699
  struct gc_arena gc = gc_new ();
789
  struct gc_arena gc = gc_new ();
700
790
701
  if (bind (sd, (struct sockaddr *) &local->sa, sizeof (local->sa)))
791
  if (bind (sd, &local->addr.sa, af_addr_size(local->addr.sa.sa_family)))
702
    {
792
    {
703
      const int errnum = openvpn_errno_socket ();
793
      const int errnum = openvpn_errno_socket ();
704
      msg (M_FATAL, "%s: Socket bind failed on local address %s: %s",
794
      msg (M_FATAL, "%s: Socket bind failed on local address %s: %s",
Lines 719-725 Link Here
719
809
720
#ifdef CONNECT_NONBLOCK
810
#ifdef CONNECT_NONBLOCK
721
  set_nonblock (sd);
811
  set_nonblock (sd);
722
  status = connect (sd, (struct sockaddr *) &remote->sa, sizeof (remote->sa));
812
  status = connect (sd, &remote->addr.sa, af_addr_size(remote->addr.sa.sa_family));
723
  if (status)
813
  if (status)
724
    status = openvpn_errno_socket ();
814
    status = openvpn_errno_socket ();
725
  if (status == EINPROGRESS)
815
  if (status == EINPROGRESS)
Lines 854-870 Link Here
854
      if (*signal_received)
944
      if (*signal_received)
855
	goto done;
945
	goto done;
856
946
857
      *sd = create_socket_tcp ();
947
      switch(remote->addr.sa.sa_family) {
858
      if (bind_local)
948
        case AF_INET:
859
        socket_bind (*sd, local, "TCP Client");
949
          *sd = create_socket_tcp ();
860
      update_remote (remote_dynamic, remote, remote_changed);
950
          if (bind_local)
861
    }
951
            socket_bind (*sd, local, "TCP Client");
952
          update_remote (remote_dynamic, remote, remote_changed);
953
          break;
954
  #ifdef USE_PF_INET6
955
        case AF_INET6:
956
          *sd = create_socket_tcp6 ();
957
          if (bind_local)
958
            socket_bind (*sd, local, "TCP6 Client");
959
          update_remote (remote_dynamic, remote, remote_changed);
960
          break;
961
  #endif
962
        default:
963
          msg(M_FATAL, "Only TCP is supported for connection oriented, sa_family=%d",
964
              remote->addr.sa.sa_family);
965
     }
966
  }
862
967
863
  msg (M_INFO, "TCP connection established with %s", 
968
  msg (M_INFO, "TCP connection established with %s",
864
       print_sockaddr (remote, &gc));
969
       print_sockaddr (remote, &gc));
865
970
866
 done:
971
  done:
867
  gc_free (&gc);
972
    gc_free (&gc);
868
}
973
}
869
974
870
/* For stream protocols, allocate a buffer to build up packet.
975
/* For stream protocols, allocate a buffer to build up packet.
Lines 918-934 Link Here
918
1023
919
  /* resolve local address if undefined */
1024
  /* resolve local address if undefined */
920
  if (!addr_defined (&sock->info.lsa->local))
1025
  if (!addr_defined (&sock->info.lsa->local))
921
    {
1026
  {
922
      sock->info.lsa->local.sa.sin_family = AF_INET;
1027
    /* may return AF_{INET|INET6} guessed from local_host */
923
      sock->info.lsa->local.sa.sin_addr.s_addr =
1028
    switch(addr_guess_family(sock->info.proto, sock->local_host)) {
924
	(sock->local_host ? getaddr (GETADDR_RESOLVE | GETADDR_WARN_ON_SIGNAL | GETADDR_FATAL,
1029
      case AF_INET:
925
				     sock->local_host,
1030
	sock->info.lsa->local.addr.in4.sin_family = AF_INET;
926
				     0,
1031
	sock->info.lsa->local.addr.in4.sin_addr.s_addr =
927
				     NULL,
1032
	  (sock->local_host ? getaddr (GETADDR_RESOLVE | GETADDR_WARN_ON_SIGNAL | GETADDR_FATAL,
928
				     NULL)
1033
				       sock->local_host,
929
	 : htonl (INADDR_ANY));
1034
				       0,
930
      sock->info.lsa->local.sa.sin_port = htons (sock->local_port);
1035
				       NULL,
1036
				       NULL)
1037
	   : htonl (INADDR_ANY));
1038
	sock->info.lsa->local.addr.in4.sin_port = htons (sock->local_port);
1039
	break;
1040
#ifdef USE_PF_INET6
1041
      case AF_INET6:
1042
	{
1043
	  struct addrinfo hints , *ai;
1044
	  int err;
1045
	  memset(&hints, 0, sizeof hints);
1046
	  hints.ai_flags=AI_PASSIVE;
1047
	  hints.ai_family=AF_INET6;
1048
	  /* if no local_host provided, ask for IN6ADDR_ANY ... */
1049
	  if ((err=getaddrinfo(sock->local_host? sock->local_host : "::", 
1050
		  NULL, &hints, &ai))==0) {
1051
	    sock->info.lsa->local.addr.in6 = *((struct sockaddr_in6*)(ai->ai_addr));
1052
	    freeaddrinfo(ai);
1053
	  } else {
1054
	    msg (M_FATAL, "getaddrinfo() failed for local \"%s\": %s",
1055
		sock->local_host,
1056
		gai_strerror(err));
1057
	  }
1058
	  sock->info.lsa->local.addr.in6.sin6_port = htons (sock->local_port);
1059
	}
1060
	break;
1061
#endif
931
    }
1062
    }
1063
  }
932
  
1064
  
933
  /* bind to local address/port */
1065
  /* bind to local address/port */
934
  if (sock->bind_local)
1066
  if (sock->bind_local)
Lines 955-1055 Link Here
955
    {
1087
    {
956
      /* resolve remote address if undefined */
1088
      /* resolve remote address if undefined */
957
      if (!addr_defined (&sock->info.lsa->remote))
1089
      if (!addr_defined (&sock->info.lsa->remote))
958
	{
1090
      {
959
	  sock->info.lsa->remote.sa.sin_family = AF_INET;
1091
        switch(addr_guess_family(sock->info.proto, sock->remote_host))
960
	  sock->info.lsa->remote.sa.sin_addr.s_addr = 0;
1092
        {
1093
        case AF_INET:
1094
          sock->info.lsa->remote.addr.in4.sin_family = AF_INET;
1095
          sock->info.lsa->remote.addr.in4.sin_addr.s_addr = 0;
961
1096
962
	  if (sock->remote_host)
1097
	  if (sock->remote_host)
963
	    {
1098
          {
964
	      unsigned int flags = GETADDR_RESOLVE|GETADDR_UPDATE_MANAGEMENT_STATE;
1099
            unsigned int flags = GETADDR_RESOLVE|GETADDR_UPDATE_MANAGEMENT_STATE;
965
	      int retry = 0;
1100
            int retry = 0;
966
	      bool status = false;
1101
            bool status = false;
967
1102
968
	      if (sock->connection_profiles_defined && sock->resolve_retry_seconds == RESOLV_RETRY_INFINITE)
1103
            if (sock->connection_profiles_defined && sock->resolve_retry_seconds == RESOLV_RETRY_INFINITE)
969
		{
1104
              {
970
		  if (phase == 2)
1105
                if (phase == 2)
971
		    flags |= (GETADDR_TRY_ONCE | GETADDR_FATAL);
1106
                  flags |= (GETADDR_TRY_ONCE | GETADDR_FATAL);
972
		  retry = 0;
1107
                retry = 0;
973
		}
1108
              }
974
	      else if (phase == 1)
1109
              else if (phase == 1)
975
		{
1110
                {
976
		  if (sock->resolve_retry_seconds)
1111
                  if (sock->resolve_retry_seconds)
977
		    {
1112
                    {
978
		      retry = 0;
1113
                      retry = 0;
979
		    }
1114
                    }
980
		  else
981
		    {
982
		      flags |= (GETADDR_FATAL | GETADDR_MENTION_RESOLVE_RETRY);
983
		      retry = 0;
984
		    }
985
		}
986
	      else if (phase == 2)
987
		{
988
		  if (sock->resolve_retry_seconds)
989
		    {
990
		      flags |= GETADDR_FATAL;
991
		      retry = sock->resolve_retry_seconds;
992
		    }
993
		  else
1115
		  else
994
		    {
1116
                    {
995
		      ASSERT (0);
1117
	               flags |= (GETADDR_FATAL | GETADDR_MENTION_RESOLVE_RETRY);
996
		    }
1118
	               retry = 0;
997
		}
1119
	            }
998
	      else
1120
	          }
999
		{
1121
	          else if (phase == 2)
1000
		  ASSERT (0);
1122
	          {
1001
		}
1123
	            if (sock->resolve_retry_seconds)
1002
1124
	            {
1003
	      sock->info.lsa->remote.sa.sin_addr.s_addr = getaddr (
1125
	              flags |= GETADDR_FATAL;
1004
		    flags,
1126
	              retry = sock->resolve_retry_seconds;
1005
		    sock->remote_host,
1127
	            }
1006
		    retry,
1128
	            else
1007
		    &status,
1129
	            {
1008
		    signal_received);
1130
	              ASSERT (0);
1009
	      
1131
	            }
1010
	      dmsg (D_SOCKET_DEBUG, "RESOLVE_REMOTE flags=0x%04x phase=%d rrs=%d sig=%d status=%d",
1132
	          }
1011
		   flags,
1133
	          else
1012
		   phase,
1134
	          {
1013
		   retry,
1135
	            ASSERT (0);
1014
		   signal_received ? *signal_received : -1,
1136
	          }
1015
		   status);
1137
1016
1138
	          sock->info.lsa->remote.addr.in4.sin_addr.s_addr = getaddr (
1017
	      if (signal_received)
1139
	              flags,
1018
		{
1140
	              sock->remote_host,
1019
		  if (*signal_received)
1141
	              retry,
1142
	              &status,
1143
	              signal_received);
1144
	
1145
	          dmsg (D_SOCKET_DEBUG, "RESOLVE_REMOTE flags=0x%04x phase=%d rrs=%d sig=%d status=%d",
1146
	              flags,
1147
	              phase,
1148
	              retry,
1149
	              signal_received ? *signal_received : -1,
1150
	              status);
1151
1152
	          if (signal_received)
1153
	          {
1154
	            if (*signal_received)
1155
	              goto done;
1156
	          }
1157
	          if (!status)
1158
	          {
1159
		    if (signal_received)
1160
		      *signal_received = SIGUSR1;
1020
		    goto done;
1161
		    goto done;
1021
		}
1162
		  }
1022
	      if (!status)
1163
	        }
1023
		{
1024
		  if (signal_received)
1025
		    *signal_received = SIGUSR1;
1026
		  goto done;
1027
		}
1028
	    }
1029
1164
1030
	  sock->info.lsa->remote.sa.sin_port = htons (sock->remote_port);
1165
          sock->info.lsa->remote.addr.in4.sin_port = htons (sock->remote_port);
1031
	}
1166
          break;
1032
  
1167
  
1168
  #ifdef USE_PF_INET6
1169
        case AF_INET6: /* jjoFIXME: ipv6 signal logic */
1170
          {
1171
            struct addrinfo hints , *ai;
1172
            int err;
1173
            memset(&hints, 0, sizeof hints);
1174
            hints.ai_flags=0;
1175
            hints.ai_family=AF_INET6;
1176
            if ((err=getaddrinfo(sock->remote_host? sock->remote_host : "::" , NULL, &hints, &ai))==0) {
1177
              sock->info.lsa->remote.addr.in6 = *((struct sockaddr_in6*)(ai->ai_addr));
1178
              freeaddrinfo(ai);
1179
            } else {
1180
              msg (M_FATAL, "getaddrinfo() failed for remote \"%s\": %s",
1181
                  sock->remote_host,
1182
                  gai_strerror(err));
1183
            }
1184
            sock->info.lsa->remote.addr.in6.sin6_port = htons (sock->remote_port);
1185
          }
1186
          break;
1187
  #endif
1188
        }
1189
      }
1033
      /* should we re-use previous active remote address? */
1190
      /* should we re-use previous active remote address? */
1034
      if (link_socket_actual_defined (&sock->info.lsa->actual))
1191
      if (link_socket_actual_defined (&sock->info.lsa->actual))
1035
	{
1192
      {
1036
	  msg (M_INFO, "TCP/UDP: Preserving recently used remote address: %s",
1193
        msg (M_INFO, "TCP/UDP: Preserving recently used remote address: %s",
1037
	       print_link_socket_actual (&sock->info.lsa->actual, &gc));
1194
          print_link_socket_actual (&sock->info.lsa->actual, &gc));
1038
	  if (remote_dynamic)
1195
        if (remote_dynamic)
1039
	    *remote_dynamic = NULL;
1196
        *remote_dynamic = NULL;
1040
	}
1197
      }
1041
      else
1198
      else
1042
	{
1199
      {
1043
	  CLEAR (sock->info.lsa->actual);
1200
        CLEAR (sock->info.lsa->actual);
1044
	  sock->info.lsa->actual.dest = sock->info.lsa->remote;
1201
        sock->info.lsa->actual.dest = sock->info.lsa->remote;
1045
	}
1202
      }
1046
1047
      /* remember that we finished */
1203
      /* remember that we finished */
1048
      sock->did_resolve_remote = true;
1204
      sock->did_resolve_remote = true;
1049
    }
1205
    }
1050
1206
1051
 done:
1207
  done:
1052
  gc_free (&gc);
1208
   gc_free (&gc);
1053
}
1209
}
1054
1210
1055
struct link_socket *
1211
struct link_socket *
Lines 1268-1274 Link Here
1268
	goto done;
1424
	goto done;
1269
1425
1270
      /* TCP client/server */
1426
      /* TCP client/server */
1271
      if (sock->info.proto == PROTO_TCPv4_SERVER)
1427
      if (sock->info.proto == PROTO_TCPv4_SERVER
1428
#ifdef USE_PF_INET6
1429
	||sock->info.proto == PROTO_TCPv6_SERVER
1430
#endif
1431
	)
1272
	{
1432
	{
1273
	  switch (sock->mode)
1433
	  switch (sock->mode)
1274
	    {
1434
	    {
Lines 1303-1309 Link Here
1303
	      ASSERT (0);
1463
	      ASSERT (0);
1304
	    }
1464
	    }
1305
	}
1465
	}
1306
      else if (sock->info.proto == PROTO_TCPv4_CLIENT)
1466
      else if (sock->info.proto == PROTO_TCPv4_CLIENT
1467
#ifdef USE_PF_INET6
1468
	||sock->info.proto == PROTO_TCPv6_CLIENT
1469
#endif
1470
	)
1307
	{
1471
	{
1308
1472
1309
#ifdef GENERAL_PROXY_SUPPORT
1473
#ifdef GENERAL_PROXY_SUPPORT
Lines 1388-1395 Link Here
1388
	  sock->remote_port = sock->proxy_dest_port;
1552
	  sock->remote_port = sock->proxy_dest_port;
1389
	  sock->did_resolve_remote = false;
1553
	  sock->did_resolve_remote = false;
1390
1554
1391
	  sock->info.lsa->actual.dest.sa.sin_addr.s_addr = 0;
1555
	  addr_zero_host(&sock->info.lsa->actual.dest);
1392
	  sock->info.lsa->remote.sa.sin_addr.s_addr = 0;
1556
	  addr_zero_host(&sock->info.lsa->remote);
1393
1557
1394
	  resolve_remote (sock, 1, NULL, signal_received);
1558
	  resolve_remote (sock, 1, NULL, signal_received);
1395
1559
Lines 1404-1410 Link Here
1404
      if (remote_changed)
1568
      if (remote_changed)
1405
	{
1569
	{
1406
	  msg (M_INFO, "TCP/UDP: Dynamic remote address changed during TCP connection establishment");
1570
	  msg (M_INFO, "TCP/UDP: Dynamic remote address changed during TCP connection establishment");
1407
	  sock->info.lsa->remote.sa.sin_addr.s_addr = sock->info.lsa->actual.dest.sa.sin_addr.s_addr;
1571
	  addr_copy_host(&sock->info.lsa->remote, &sock->info.lsa->actual.dest);
1408
	}
1572
	}
1409
    }
1573
    }
1410
1574
Lines 1593-1605 Link Here
1593
{
1757
{
1594
  struct gc_arena gc = gc_new ();
1758
  struct gc_arena gc = gc_new ();
1595
1759
1596
  msg (D_LINK_ERRORS,
1760
  switch(from_addr->dest.addr.sa.sa_family)
1597
       "TCP/UDP: Incoming packet rejected from %s[%d], expected peer address: %s (allow this incoming source address/port by removing --remote or adding --float)",
1761
  {
1598
       print_link_socket_actual (from_addr, &gc),
1762
    case AF_INET:
1599
       (int)from_addr->dest.sa.sin_family,
1763
#ifdef USE_PF_INET6
1600
       print_sockaddr (&info->lsa->remote, &gc));
1764
    case AF_INET6:
1765
#endif
1766
      msg (D_LINK_ERRORS,
1767
	  "TCP/UDP: Incoming packet rejected from %s[%d], expected peer address: %s (allow this incoming source address/port by removing --remote or adding --float)",
1768
	  print_link_socket_actual (from_addr, &gc),
1769
	  (int)from_addr->dest.addr.sa.sa_family,
1770
	  print_sockaddr (&info->lsa->remote, &gc));
1771
      break;
1772
  }
1601
  buf->len = 0;
1773
  buf->len = 0;
1602
1603
  gc_free (&gc);
1774
  gc_free (&gc);
1604
}
1775
}
1605
1776
Lines 1614-1623 Link Here
1614
{
1785
{
1615
  const struct link_socket_addr *lsa = info->lsa;
1786
  const struct link_socket_addr *lsa = info->lsa;
1616
1787
1788
/* 
1789
 * This logic supports "redirect-gateway" semantic, which 
1790
 * makes sense only for PF_INET routes over PF_INET endpoints
1791
 *
1792
 * Maybe in the future consider PF_INET6 endpoints also ...
1793
 * by now just ignore it
1794
 *
1795
 */
1796
#if defined ( USE_PF_INET6 )
1797
  if(lsa->actual.dest.addr.sa.sa_family != AF_INET)
1798
	  return 0;
1799
#else
1800
  ASSERT(lsa->actual.dest.addr.sa.sa_family == AF_INET);
1801
#endif
1802
1617
  if (link_socket_actual_defined (&lsa->actual))
1803
  if (link_socket_actual_defined (&lsa->actual))
1618
    return ntohl (lsa->actual.dest.sa.sin_addr.s_addr);
1804
    return ntohl (lsa->actual.dest.addr.in4.sin_addr.s_addr);
1619
  else if (addr_defined (&lsa->remote))
1805
  else if (addr_defined (&lsa->remote))
1620
    return ntohl (lsa->remote.sa.sin_addr.s_addr);
1806
    return ntohl (lsa->remote.addr.in4.sin_addr.s_addr);
1621
  else
1807
  else
1622
    return 0;
1808
    return 0;
1623
}
1809
}
Lines 1844-1871 Link Here
1844
		   const unsigned int flags,
2030
		   const unsigned int flags,
1845
		   struct gc_arena *gc)
2031
		   struct gc_arena *gc)
1846
{
2032
{
1847
  if (addr)
2033
   struct buffer out;
1848
    {
2034
   bool addr_is_defined;
1849
      struct buffer out = alloc_buf_gc (64, gc);
2035
   if (!addr) {
1850
      const int port = ntohs (addr->sa.sin_port);
2036
     return "[NULL]";
1851
2037
   }
1852
      mutex_lock_static (L_INET_NTOA);
2038
   addr_is_defined =  addr_defined (addr);
1853
      if (!(flags & PS_DONT_SHOW_ADDR))
2039
   switch(addr->addr.sa.sa_family) {
1854
	buf_printf (&out, "%s", (addr_defined (addr) ? inet_ntoa (addr->sa.sin_addr) : "[undef]"));
2040
     case AF_INET:
1855
      mutex_unlock_static (L_INET_NTOA);
2041
       {
1856
2042
       const int port= ntohs (addr->addr.in4.sin_port);
1857
      if (((flags & PS_SHOW_PORT) || (addr_defined (addr) && (flags & PS_SHOW_PORT_IF_DEFINED)))
2043
       out = alloc_buf_gc (128, gc);
1858
	  && port)
2044
       buf_puts (&out, "[AF_INET]");
1859
	{
2045
       mutex_lock_static (L_INET_NTOA);
1860
	  if (separator)
2046
       buf_puts (&out, (addr_is_defined ? inet_ntoa (addr->addr.in4.sin_addr) : "[undef]"));
1861
	    buf_printf (&out, "%s", separator);
2047
       mutex_unlock_static (L_INET_NTOA);
1862
2048
1863
	  buf_printf (&out, "%d", port);
2049
       if (((flags & PS_SHOW_PORT) || (addr_is_defined && (flags & PS_SHOW_PORT_IF_DEFINED)))
1864
	}
2050
          && port)
1865
      return BSTR (&out);
2051
       {
1866
    }
2052
         if (separator)
1867
  else
2053
	   buf_printf (&out, "%s", separator);
1868
    return "[NULL]";
2054
2055
	 buf_printf (&out, "%d", port);
2056
       }
2057
       }
2058
       break;
2059
 #ifdef USE_PF_INET6
2060
     case AF_INET6:
2061
       {
2062
       const int port= ntohs (addr->addr.in6.sin6_port);
2063
       char buf[INET6_ADDRSTRLEN] = "[undef]";
2064
       out = alloc_buf_gc (128, gc);
2065
       buf_puts (&out, "[AF_INET6]");
2066
       if (addr_is_defined)
2067
       {
2068
         getnameinfo(&addr->addr.sa, sizeof (struct sockaddr_in6),
2069
             buf, sizeof (buf), NULL, 0, NI_NUMERICHOST);
2070
         buf_puts (&out, buf);
2071
       }
2072
       if (((flags & PS_SHOW_PORT) || (addr_is_defined && (flags & PS_SHOW_PORT_IF_DEFINED)))
2073
           && port)
2074
       {
2075
         if (separator)
2076
           buf_puts (&out, separator);
2077
2078
         buf_printf (&out, "%d", port);
2079
       }
2080
       }
2081
       break;
2082
 #endif
2083
   }
2084
    return BSTR (&out);
1869
}
2085
}
1870
2086
1871
const char *
2087
const char *
Lines 1885-1896 Link Here
1885
      struct buffer out = alloc_buf_gc (128, gc);
2101
      struct buffer out = alloc_buf_gc (128, gc);
1886
      buf_printf (&out, "%s", print_sockaddr_ex (&act->dest, separator, flags, gc));
2102
      buf_printf (&out, "%s", print_sockaddr_ex (&act->dest, separator, flags, gc));
1887
#if ENABLE_IP_PKTINFO
2103
#if ENABLE_IP_PKTINFO
1888
      if ((flags & PS_SHOW_PKTINFO) && act->pi.ipi_spec_dst.s_addr)
2104
      if ((flags & PS_SHOW_PKTINFO) && addr_defined_ipi(act))
1889
	{
2105
	{
2106
	  switch(act->dest.addr.sa.sa_family)
2107
	  {
2108
	case AF_INET:
2109
	  {
1890
	  struct openvpn_sockaddr sa;
2110
	  struct openvpn_sockaddr sa;
1891
	  CLEAR (sa);
2111
	  CLEAR (sa);
1892
	  sa.sa.sin_addr = act->pi.ipi_spec_dst;
2112
	  sa.addr.in4.sin_addr = act->pi.in4.ipi_spec_dst;
1893
	  buf_printf (&out, " (via %s)", print_sockaddr_ex (&sa, separator, 0, gc));
2113
	  buf_printf (&out, " (via %s)", print_sockaddr_ex (&sa, separator, 0, gc));
2114
	  }
2115
	  break;
2116
#ifdef USE_PF_INET6
2117
	case AF_INET6:
2118
	  {
2119
	    struct sockaddr_in6 sin6;
2120
	    char buf[INET6_ADDRSTRLEN] = "[undef]";
2121
	    memset(&sin6, 0, sizeof sin6);
2122
	    sin6.sin6_family = AF_INET6;
2123
	    sin6.sin6_addr = act->pi.in6.ipi6_addr;
2124
	    {
2125
	      if (getnameinfo((struct sockaddr *)&sin6, sizeof (struct sockaddr_in6),
2126
		    buf, sizeof (buf), NULL, 0, NI_NUMERICHOST) == 0)
2127
		buf_printf (&out, " (via %s)", buf);
2128
	      else
2129
		buf_printf (&out, " (via [getnameinfo() err])");
2130
	    }
2131
	  }
2132
	  break;
2133
#endif
2134
	  }
2135
1894
	}
2136
	}
1895
#endif
2137
#endif
1896
      return BSTR (&out);
2138
      return BSTR (&out);
Lines 1926-1946 Link Here
1926
setenv_sockaddr (struct env_set *es, const char *name_prefix, const struct openvpn_sockaddr *addr, const bool flags)
2168
setenv_sockaddr (struct env_set *es, const char *name_prefix, const struct openvpn_sockaddr *addr, const bool flags)
1927
{
2169
{
1928
  char name_buf[256];
2170
  char name_buf[256];
2171
  char buf[128];
1929
2172
1930
  if (flags & SA_IP_PORT)
2173
  switch(addr->addr.sa.sa_family) {
1931
    openvpn_snprintf (name_buf, sizeof (name_buf), "%s_ip", name_prefix);
2174
    case AF_INET:
1932
  else
2175
      if (flags & SA_IP_PORT)
1933
    openvpn_snprintf (name_buf, sizeof (name_buf), "%s", name_prefix);
2176
	openvpn_snprintf (name_buf, sizeof (name_buf), "%s_ip", name_prefix);
2177
      else
2178
	openvpn_snprintf (name_buf, sizeof (name_buf), "%s", name_prefix);
1934
2179
1935
  mutex_lock_static (L_INET_NTOA);
2180
      mutex_lock_static (L_INET_NTOA);
1936
  setenv_str (es, name_buf, inet_ntoa (addr->sa.sin_addr));
2181
      setenv_str (es, name_buf, inet_ntoa (addr->addr.in4.sin_addr));
1937
  mutex_unlock_static (L_INET_NTOA);
2182
      mutex_unlock_static (L_INET_NTOA);
2183
2184
      if ((flags & SA_IP_PORT) && addr->addr.in4.sin_port)
2185
      {
2186
	openvpn_snprintf (name_buf, sizeof (name_buf), "%s_port", name_prefix);
2187
	setenv_int (es, name_buf, ntohs (addr->addr.in4.sin_port));
2188
      }
2189
      break;
2190
#ifdef USE_PF_INET6
2191
    case AF_INET6:
2192
      openvpn_snprintf (name_buf, sizeof (name_buf), "%s_ip6", name_prefix);
2193
      getnameinfo(&addr->addr.sa, sizeof (struct sockaddr_in6),
2194
	  buf, sizeof(buf), NULL, 0, NI_NUMERICHOST);
2195
      setenv_str (es, name_buf, buf);
1938
2196
1939
  if ((flags & SA_IP_PORT) && addr->sa.sin_port)
1940
    {
1941
      openvpn_snprintf (name_buf, sizeof (name_buf), "%s_port", name_prefix);
2197
      openvpn_snprintf (name_buf, sizeof (name_buf), "%s_port", name_prefix);
1942
      setenv_int (es, name_buf, ntohs (addr->sa.sin_port));
2198
      setenv_int (es, name_buf, ntohs (addr->addr.in6.sin6_port));
1943
    }
2199
      break;
2200
#endif
2201
  }
1944
}
2202
}
1945
2203
1946
void
2204
void
Lines 1950-1956 Link Here
1950
    {
2208
    {
1951
      struct openvpn_sockaddr si;
2209
      struct openvpn_sockaddr si;
1952
      CLEAR (si);
2210
      CLEAR (si);
1953
      si.sa.sin_addr.s_addr = htonl (addr);
2211
      si.addr.in4.sin_family = AF_INET;
2212
      si.addr.in4.sin_addr.s_addr = htonl (addr);
1954
      setenv_sockaddr (es, name_prefix, &si, flags);
2213
      setenv_sockaddr (es, name_prefix, &si, flags);
1955
    }
2214
    }
1956
}
2215
}
Lines 1971-1986 Link Here
1971
struct proto_names {
2230
struct proto_names {
1972
  const char *short_form;
2231
  const char *short_form;
1973
  const char *display_form;
2232
  const char *display_form;
2233
  bool	is_dgram;
2234
  bool	is_net;
2235
  sa_family_t proto_af;
1974
};
2236
};
1975
2237
1976
/* Indexed by PROTO_x */
2238
/* Indexed by PROTO_x */
1977
static const struct proto_names proto_names[] = {
2239
static const struct proto_names proto_names[PROTO_N] = {
1978
  {"udp",        "UDPv4"},
2240
  {"proto-uninitialized",        "proto-NONE",0,0, AF_UNSPEC},
1979
  {"tcp-server", "TCPv4_SERVER"},
2241
  {"udp",        "UDPv4",1,1, AF_INET},
1980
  {"tcp-client", "TCPv4_CLIENT"},
2242
  {"tcp-server", "TCPv4_SERVER",0,1, AF_INET},
1981
  {"tcp",        "TCPv4"}
2243
  {"tcp-client", "TCPv4_CLIENT",0,1, AF_INET},
2244
  {"tcp",        "TCPv4",0,1, AF_INET},
2245
#ifdef USE_PF_INET6
2246
  {"udp6"       ,"UDPv6",1,1, AF_INET6},
2247
  {"tcp6-server","TCPv6_SERVER",0,1, AF_INET6},
2248
  {"tcp6-client","TCPv6_CLIENT",0,1, AF_INET6},
2249
  {"tcp6"       ,"TCPv6",0,1, AF_INET6},
2250
#endif
1982
};
2251
};
1983
2252
2253
bool
2254
proto_is_net(int proto)
2255
{
2256
  if (proto < 0 || proto >= PROTO_N)
2257
    ASSERT(0);
2258
  return proto_names[proto].is_net;
2259
}
2260
bool
2261
proto_is_dgram(int proto)
2262
{
2263
  if (proto < 0 || proto >= PROTO_N)
2264
    ASSERT(0);
2265
  return proto_names[proto].is_dgram;
2266
}
2267
bool
2268
proto_is_udp(int proto)
2269
{
2270
  if (proto < 0 || proto >= PROTO_N)
2271
    ASSERT(0);
2272
  return proto_names[proto].is_dgram&&proto_names[proto].is_net;
2273
}
2274
bool
2275
proto_is_tcp(int proto)
2276
{
2277
  if (proto < 0 || proto >= PROTO_N)
2278
    ASSERT(0);
2279
  return (!proto_names[proto].is_dgram)&&proto_names[proto].is_net;
2280
}
2281
2282
sa_family_t 
2283
proto_sa_family(int proto)
2284
{
2285
  if (proto < 0 || proto >= PROTO_N)
2286
    ASSERT(0);
2287
  return proto_names[proto].proto_af;
2288
}
2289
1984
int
2290
int
1985
ascii2proto (const char* proto_name)
2291
ascii2proto (const char* proto_name)
1986
{
2292
{
Lines 2020-2025 Link Here
2020
  return BSTR (&out);
2326
  return BSTR (&out);
2021
}
2327
}
2022
2328
2329
int
2330
addr_guess_family(int proto, const char *name) 
2331
{
2332
  sa_family_t ret;
2333
  if (proto) {
2334
    return proto_sa_family(proto);	/* already stamped */
2335
  } 
2336
#ifdef USE_PF_UNIX
2337
  else if (name && name[0] == '/') {
2338
    return AF_UNIX;
2339
  }
2340
#endif
2341
#ifdef USE_PF_INET6
2342
  else {
2343
    struct addrinfo hints , *ai;
2344
    int err;
2345
    memset(&hints, 0, sizeof hints);
2346
    hints.ai_flags=AI_NUMERICHOST;
2347
    if ((err=getaddrinfo(name, NULL, &hints, &ai))==0) {
2348
      ret=ai->ai_family;
2349
      freeaddrinfo(ai);
2350
      return ret;
2351
    }
2352
  }
2353
#endif
2354
  return AF_INET;	/* default */
2355
}
2356
const char *
2357
addr_family_name (int af) 
2358
{
2359
  switch (af) {
2360
    case AF_INET: return "AF_INET";
2361
    case AF_INET6: return "AF_INET6";
2362
#ifdef USE_PF_UNIX
2363
    case AF_UNIX: return "AF_UNIX";
2364
#endif
2365
  }
2366
  return "AF_UNSPEC";
2367
}
2368
2023
/*
2369
/*
2024
 * Given a local proto, return local proto
2370
 * Given a local proto, return local proto
2025
 * if !remote, or compatible remote proto
2371
 * if !remote, or compatible remote proto
Lines 2034-2043 Link Here
2034
  ASSERT (proto >= 0 && proto < PROTO_N);
2380
  ASSERT (proto >= 0 && proto < PROTO_N);
2035
  if (remote)
2381
  if (remote)
2036
    {
2382
    {
2037
      if (proto == PROTO_TCPv4_SERVER)
2383
      switch (proto)
2038
	return PROTO_TCPv4_CLIENT;
2384
      {
2039
      if (proto == PROTO_TCPv4_CLIENT)
2385
	case PROTO_TCPv4_SERVER: return PROTO_TCPv4_CLIENT;
2040
	return PROTO_TCPv4_SERVER;
2386
	case PROTO_TCPv4_CLIENT: return PROTO_TCPv4_SERVER;
2387
#ifdef USE_PF_INET6
2388
	case PROTO_TCPv6_SERVER: return PROTO_TCPv6_CLIENT;
2389
	case PROTO_TCPv6_CLIENT: return PROTO_TCPv6_SERVER;
2390
#endif
2391
      }
2041
    }
2392
    }
2042
  return proto;
2393
  return proto;
2043
}
2394
}
Lines 2096-2105 Link Here
2096
#if ENABLE_IP_PKTINFO
2447
#if ENABLE_IP_PKTINFO
2097
2448
2098
#pragma pack(1) /* needed to keep structure size consistent for 32 vs. 64-bit architectures */
2449
#pragma pack(1) /* needed to keep structure size consistent for 32 vs. 64-bit architectures */
2099
struct openvpn_pktinfo
2450
struct openvpn_in4_pktinfo
2451
{
2452
  struct cmsghdr cmsghdr;
2453
  struct in_pktinfo pi;
2454
};
2455
#ifdef USE_PF_INET6
2456
struct openvpn_in6_pktinfo
2100
{
2457
{
2101
  struct cmsghdr cmsghdr;
2458
  struct cmsghdr cmsghdr;
2102
  struct in_pktinfo in_pktinfo;
2459
  struct in6_pktinfo pi6;
2460
};
2461
#endif
2462
2463
union openvpn_pktinfo {
2464
      struct openvpn_in4_pktinfo cmsgpi;
2465
#ifdef USE_PF_INET6
2466
      struct openvpn_in6_pktinfo cmsgpi6;
2467
#endif
2103
};
2468
};
2104
#pragma pack()
2469
#pragma pack()
2105
2470
Lines 2110-2124 Link Here
2110
				    struct link_socket_actual *from)
2475
				    struct link_socket_actual *from)
2111
{
2476
{
2112
  struct iovec iov;
2477
  struct iovec iov;
2113
  struct openvpn_pktinfo opi;
2478
  union openvpn_pktinfo opi;
2114
  struct msghdr mesg;
2479
  struct msghdr mesg;
2115
  socklen_t fromlen = sizeof (from->dest.sa);
2480
  socklen_t fromlen = sizeof (from->dest.addr);
2116
2481
2117
  iov.iov_base = BPTR (buf);
2482
  iov.iov_base = BPTR (buf);
2118
  iov.iov_len = maxsize;
2483
  iov.iov_len = maxsize;
2119
  mesg.msg_iov = &iov;
2484
  mesg.msg_iov = &iov;
2120
  mesg.msg_iovlen = 1;
2485
  mesg.msg_iovlen = 1;
2121
  mesg.msg_name = &from->dest.sa;
2486
  mesg.msg_name = &from->dest.addr;
2122
  mesg.msg_namelen = fromlen;
2487
  mesg.msg_namelen = fromlen;
2123
  mesg.msg_control = &opi;
2488
  mesg.msg_control = &opi;
2124
  mesg.msg_controllen = sizeof (opi);
2489
  mesg.msg_controllen = sizeof (opi);
Lines 2135-2143 Link Here
2135
	  && cmsg->cmsg_len >= sizeof (opi))
2500
	  && cmsg->cmsg_len >= sizeof (opi))
2136
	{
2501
	{
2137
	  struct in_pktinfo *pkti = (struct in_pktinfo *) CMSG_DATA (cmsg);
2502
	  struct in_pktinfo *pkti = (struct in_pktinfo *) CMSG_DATA (cmsg);
2138
	  from->pi.ipi_ifindex = pkti->ipi_ifindex;
2503
	  from->pi.in4.ipi_ifindex = pkti->ipi_ifindex;
2139
	  from->pi.ipi_spec_dst = pkti->ipi_spec_dst;
2504
	  from->pi.in4.ipi_spec_dst = pkti->ipi_spec_dst;
2140
	}
2505
	}
2506
#ifdef USE_PF_INET6
2507
      else if (cmsg != NULL
2508
	  && CMSG_NXTHDR (&mesg, cmsg) == NULL
2509
	  && cmsg->cmsg_level == IPPROTO_IPV6 
2510
	  && cmsg->cmsg_type == IPV6_PKTINFO
2511
	  && cmsg->cmsg_len >= sizeof (struct openvpn_in6_pktinfo))
2512
	{
2513
	  struct in6_pktinfo *pkti6 = (struct in6_pktinfo *) CMSG_DATA (cmsg);
2514
	  from->pi.in6.ipi6_ifindex = pkti6->ipi6_ifindex;
2515
	  from->pi.in6.ipi6_addr = pkti6->ipi6_addr;
2516
	}
2517
#endif
2141
    }
2518
    }
2142
  return fromlen;
2519
  return fromlen;
2143
}
2520
}
Lines 2149-2166 Link Here
2149
			    int maxsize,
2526
			    int maxsize,
2150
			    struct link_socket_actual *from)
2527
			    struct link_socket_actual *from)
2151
{
2528
{
2152
  socklen_t fromlen = sizeof (from->dest.sa);
2529
  socklen_t fromlen = sizeof (from->dest.addr);
2153
  from->dest.sa.sin_addr.s_addr = 0;
2530
  socklen_t expectedlen = af_addr_size(proto_sa_family(sock->info.proto));
2531
  addr_zero_host(&from->dest);
2154
  ASSERT (buf_safe (buf, maxsize));
2532
  ASSERT (buf_safe (buf, maxsize));
2155
#if ENABLE_IP_PKTINFO
2533
#if ENABLE_IP_PKTINFO
2156
  if (sock->sockflags & SF_USE_IP_PKTINFO)
2534
  /* Both PROTO_UDPv4 and PROTO_UDPv6 */
2535
  if (proto_is_udp(sock->info.proto) && sock->sockflags & SF_USE_IP_PKTINFO)
2157
    fromlen = link_socket_read_udp_posix_recvmsg (sock, buf, maxsize, from);
2536
    fromlen = link_socket_read_udp_posix_recvmsg (sock, buf, maxsize, from);
2158
  else
2537
  else
2159
#endif
2538
#endif
2160
    buf->len = recvfrom (sock->sd, BPTR (buf), maxsize, 0,
2539
    buf->len = recvfrom (sock->sd, BPTR (buf), maxsize, 0,
2161
			 (struct sockaddr *) &from->dest.sa, &fromlen);
2540
			 &from->dest.addr.sa, &fromlen);
2162
  if (fromlen != sizeof (from->dest.sa))
2541
  if (buf->len >= 0 && expectedlen && fromlen != expectedlen)
2163
    bad_address_length (fromlen, sizeof (from->dest.sa));
2542
    bad_address_length (fromlen, expectedlen);
2164
  return buf->len;
2543
  return buf->len;
2165
}
2544
}
2166
2545
Lines 2197-2222 Link Here
2197
  struct iovec iov;
2576
  struct iovec iov;
2198
  struct msghdr mesg;
2577
  struct msghdr mesg;
2199
  struct cmsghdr *cmsg;
2578
  struct cmsghdr *cmsg;
2200
  struct in_pktinfo *pkti;
2201
  struct openvpn_pktinfo opi;
2202
2579
2203
  iov.iov_base = BPTR (buf);
2580
  iov.iov_base = BPTR (buf);
2204
  iov.iov_len = BLEN (buf);
2581
  iov.iov_len = BLEN (buf);
2205
  mesg.msg_iov = &iov;
2582
  mesg.msg_iov = &iov;
2206
  mesg.msg_iovlen = 1;
2583
  mesg.msg_iovlen = 1;
2207
  mesg.msg_name = &to->dest.sa;
2584
  switch (sock->info.lsa->remote.addr.sa.sa_family)
2208
  mesg.msg_namelen = sizeof (to->dest.sa);
2585
  {
2209
  mesg.msg_control = &opi;
2586
    case AF_INET: {
2210
  mesg.msg_controllen = sizeof (opi);
2587
	  struct openvpn_in4_pktinfo opi;
2211
  mesg.msg_flags = 0;
2588
	  struct in_pktinfo *pkti;
2212
  cmsg = CMSG_FIRSTHDR (&mesg);
2589
	  mesg.msg_name = &to->dest.addr.sa;
2213
  cmsg->cmsg_len = sizeof (opi);
2590
	  mesg.msg_namelen = sizeof (struct sockaddr_in);
2214
  cmsg->cmsg_level = SOL_IP;
2591
	  mesg.msg_control = &opi;
2215
  cmsg->cmsg_type = IP_PKTINFO;
2592
	  mesg.msg_controllen = sizeof (opi);
2216
  pkti = (struct in_pktinfo *) CMSG_DATA (cmsg);
2593
	  mesg.msg_flags = 0;
2217
  pkti->ipi_ifindex = to->pi.ipi_ifindex;
2594
	  cmsg = CMSG_FIRSTHDR (&mesg);
2218
  pkti->ipi_spec_dst = to->pi.ipi_spec_dst;
2595
	  cmsg->cmsg_len = sizeof (opi);
2219
  pkti->ipi_addr.s_addr = 0;
2596
	  cmsg->cmsg_level = SOL_IP;
2597
	  cmsg->cmsg_type = IP_PKTINFO;
2598
	  pkti = (struct in_pktinfo *) CMSG_DATA (cmsg);
2599
	  pkti->ipi_ifindex = to->pi.in4.ipi_ifindex;
2600
	  pkti->ipi_spec_dst = to->pi.in4.ipi_spec_dst;
2601
	  pkti->ipi_addr.s_addr = 0;
2602
	  break;
2603
    }
2604
#ifdef USE_PF_INET6
2605
    case AF_INET6: {
2606
	  struct openvpn_in6_pktinfo opi6;
2607
	  struct in6_pktinfo *pkti6;
2608
	  mesg.msg_name = &to->dest.addr.sa;
2609
	  mesg.msg_namelen = sizeof (struct sockaddr_in6);
2610
	  mesg.msg_control = &opi6;
2611
	  mesg.msg_controllen = sizeof (opi6);
2612
	  mesg.msg_flags = 0;
2613
	  cmsg = CMSG_FIRSTHDR (&mesg);
2614
	  cmsg->cmsg_len = sizeof (opi6);
2615
	  cmsg->cmsg_level = IPPROTO_IPV6;
2616
	  cmsg->cmsg_type = IPV6_PKTINFO;
2617
	  pkti6 = (struct in6_pktinfo *) CMSG_DATA (cmsg);
2618
	  pkti6->ipi6_ifindex = to->pi.in6.ipi6_ifindex;
2619
	  pkti6->ipi6_addr = to->pi.in6.ipi6_addr;
2620
	  break;
2621
    }
2622
#endif
2623
    default: ASSERT(0);
2624
  }
2220
  return sendmsg (sock->sd, &mesg, 0);
2625
  return sendmsg (sock->sd, &mesg, 0);
2221
}
2626
}
2222
2627
Lines 2360-2366 Link Here
2360
	{
2765
	{
2361
	  /* set destination address for UDP writes */
2766
	  /* set destination address for UDP writes */
2362
	  sock->writes.addr_defined = true;
2767
	  sock->writes.addr_defined = true;
2363
	  sock->writes.addr = to->dest.sa;
2768
	  sock->writes.addr = to->dest.addr.in4;
2364
	  sock->writes.addrlen = sizeof (sock->writes.addr);
2769
	  sock->writes.addrlen = sizeof (sock->writes.addr);
2365
2770
2366
	  status = WSASendTo(
2771
	  status = WSASendTo(
Lines 2516-2525 Link Here
2516
	{
2921
	{
2517
	  if (io->addrlen != sizeof (io->addr))
2922
	  if (io->addrlen != sizeof (io->addr))
2518
	    bad_address_length (io->addrlen, sizeof (io->addr));
2923
	    bad_address_length (io->addrlen, sizeof (io->addr));
2519
	  from->dest.sa = io->addr;
2924
	  from->dest.addr.sa = io->addr;
2520
	}
2925
	}
2521
      else
2926
      else
2522
	CLEAR (from->dest.sa);
2927
	CLEAR (from->dest.addr.sa);
2523
    }
2928
    }
2524
  
2929
  
2525
  if (buf)
2930
  if (buf)
(-)a/options.c (-15 / +35 lines)
Lines 73-78 Link Here
73
#ifdef ENABLE_PKCS11
73
#ifdef ENABLE_PKCS11
74
  " [PKCS11]"
74
  " [PKCS11]"
75
#endif
75
#endif
76
#ifdef ENABLE_IP_PKTINFO
77
  " [MH]"
78
#endif
79
#ifdef USE_PF_INET6
80
  " [PF_INET6]"
81
#endif
76
  " built on " __DATE__
82
  " built on " __DATE__
77
;
83
;
78
84
Lines 93-98 Link Here
93
  "--mode m        : Major mode, m = 'p2p' (default, point-to-point) or 'server'.\n"
99
  "--mode m        : Major mode, m = 'p2p' (default, point-to-point) or 'server'.\n"
94
  "--proto p       : Use protocol p for communicating with peer.\n"
100
  "--proto p       : Use protocol p for communicating with peer.\n"
95
  "                  p = udp (default), tcp-server, or tcp-client\n"
101
  "                  p = udp (default), tcp-server, or tcp-client\n"
102
#ifdef USE_PF_INET6
103
  "                  p = udp6, tcp6-server, or tcp6-client (IPv6)\n"
104
#endif
96
  "--connect-retry n : For --proto tcp-client, number of seconds to wait\n"
105
  "--connect-retry n : For --proto tcp-client, number of seconds to wait\n"
97
  "                    between connection retries (default=%d).\n"
106
  "                    between connection retries (default=%d).\n"
98
  "--connect-timeout n : For --proto tcp-client, connection timeout (in seconds).\n"
107
  "--connect-timeout n : For --proto tcp-client, connection timeout (in seconds).\n"
Lines 1488-1497 Link Here
1488
   * Sanity check on TCP mode options
1497
   * Sanity check on TCP mode options
1489
   */
1498
   */
1490
1499
1491
  if (ce->connect_retry_defined && ce->proto != PROTO_TCPv4_CLIENT)
1500
  if (ce->connect_retry_defined && ce->proto != PROTO_TCPv4_CLIENT && ce->proto != PROTO_TCPv6_CLIENT)
1492
    msg (M_USAGE, "--connect-retry doesn't make sense unless also used with --proto tcp-client");
1501
    msg (M_USAGE, "--connect-retry doesn't make sense unless also used with --proto tcp-client");
1493
1502
1494
  if (ce->connect_timeout_defined && ce->proto != PROTO_TCPv4_CLIENT)
1503
  if (ce->connect_timeout_defined && ce->proto != PROTO_TCPv4_CLIENT && ce->proto != PROTO_TCPv6_CLIENT)
1495
    msg (M_USAGE, "--connect-timeout doesn't make sense unless also used with --proto tcp-client");
1504
    msg (M_USAGE, "--connect-timeout doesn't make sense unless also used with --proto tcp-client");
1496
1505
1497
  /*
1506
  /*
Lines 1501-1507 Link Here
1501
    msg (M_USAGE, "only one of --tun-mtu or --link-mtu may be defined (note that --ifconfig implies --link-mtu %d)", LINK_MTU_DEFAULT);
1510
    msg (M_USAGE, "only one of --tun-mtu or --link-mtu may be defined (note that --ifconfig implies --link-mtu %d)", LINK_MTU_DEFAULT);
1502
1511
1503
#ifdef ENABLE_OCC
1512
#ifdef ENABLE_OCC
1504
  if (ce->proto != PROTO_UDPv4 && options->mtu_test)
1513
  if (!proto_is_udp(ce->proto) && options->mtu_test)
1505
    msg (M_USAGE, "--mtu-test only makes sense with --proto udp");
1514
    msg (M_USAGE, "--mtu-test only makes sense with --proto udp");
1506
#endif
1515
#endif
1507
1516
Lines 1514-1520 Link Here
1514
   * Sanity check on --local, --remote, and --ifconfig
1523
   * Sanity check on --local, --remote, and --ifconfig
1515
   */
1524
   */
1516
1525
1517
  if (string_defined_equal (ce->local, ce->remote)
1526
1527
  if (proto_is_net(ce->proto)
1528
      && string_defined_equal (ce->local, ce->remote)
1518
      && ce->local_port == ce->remote_port)
1529
      && ce->local_port == ce->remote_port)
1519
    msg (M_USAGE, "--remote and --local addresses are the same");
1530
    msg (M_USAGE, "--remote and --local addresses are the same");
1520
  
1531
  
Lines 1575-1590 Link Here
1575
   */
1586
   */
1576
1587
1577
#ifdef ENABLE_FRAGMENT
1588
#ifdef ENABLE_FRAGMENT
1578
  if (ce->proto != PROTO_UDPv4 && options->fragment)
1589
  if (!proto_is_udp(ce->proto) && options->fragment)
1579
    msg (M_USAGE, "--fragment can only be used with --proto udp");
1590
    msg (M_USAGE, "--fragment can only be used with --proto udp");
1580
#endif
1591
#endif
1581
1592
1582
#ifdef ENABLE_OCC
1593
#ifdef ENABLE_OCC
1583
  if (ce->proto != PROTO_UDPv4 && options->explicit_exit_notification)
1594
  if (!proto_is_udp(ce->proto) && options->explicit_exit_notification)
1584
    msg (M_USAGE, "--explicit-exit-notify can only be used with --proto udp");
1595
    msg (M_USAGE, "--explicit-exit-notify can only be used with --proto udp");
1585
#endif
1596
#endif
1586
1597
1587
  if (!ce->remote && ce->proto == PROTO_TCPv4_CLIENT)
1598
  if (!ce->remote && (ce->proto == PROTO_TCPv4_CLIENT||ce->proto == PROTO_TCPv6_CLIENT))
1588
    msg (M_USAGE, "--remote MUST be used in TCP Client mode");
1599
    msg (M_USAGE, "--remote MUST be used in TCP Client mode");
1589
1600
1590
#ifdef ENABLE_HTTP_PROXY
1601
#ifdef ENABLE_HTTP_PROXY
Lines 1602-1608 Link Here
1602
    msg (M_USAGE, "--socks-proxy can not be used in TCP Server mode");
1613
    msg (M_USAGE, "--socks-proxy can not be used in TCP Server mode");
1603
#endif
1614
#endif
1604
1615
1605
  if (ce->proto == PROTO_TCPv4_SERVER && connection_list_defined (options))
1616
  if ((ce->proto == PROTO_TCPv4_SERVER||ce->proto == PROTO_TCPv6_SERVER)
1617
         && connection_list_defined (options))
1606
    msg (M_USAGE, "TCP server mode allows at most one --remote address");
1618
    msg (M_USAGE, "TCP server mode allows at most one --remote address");
1607
1619
1608
#if P2MP_SERVER
1620
#if P2MP_SERVER
Lines 1616-1622 Link Here
1616
	msg (M_USAGE, "--mode server only works with --dev tun or --dev tap");
1628
	msg (M_USAGE, "--mode server only works with --dev tun or --dev tap");
1617
      if (options->pull)
1629
      if (options->pull)
1618
	msg (M_USAGE, "--pull cannot be used with --mode server");
1630
	msg (M_USAGE, "--pull cannot be used with --mode server");
1619
      if (!(ce->proto == PROTO_UDPv4 || ce->proto == PROTO_TCPv4_SERVER))
1631
      if (!(proto_is_udp(ce->proto) || ce->proto == PROTO_TCPv4_SERVER || ce->proto == PROTO_TCPv6_SERVER))
1620
	msg (M_USAGE, "--mode server currently only supports --proto udp or --proto tcp-server");
1632
	msg (M_USAGE, "--mode server currently only supports --proto udp or --proto tcp-server");
1621
#if PORT_SHARE
1633
#if PORT_SHARE
1622
      if ((options->port_share_host || options->port_share_port) && ce->proto != PROTO_TCPv4_SERVER)
1634
      if ((options->port_share_host || options->port_share_port) && ce->proto != PROTO_TCPv4_SERVER)
Lines 1648-1656 Link Here
1648
	msg (M_USAGE, "--inetd cannot be used with --mode server");
1660
	msg (M_USAGE, "--inetd cannot be used with --mode server");
1649
      if (options->ipchange)
1661
      if (options->ipchange)
1650
	msg (M_USAGE, "--ipchange cannot be used with --mode server (use --client-connect instead)");
1662
	msg (M_USAGE, "--ipchange cannot be used with --mode server (use --client-connect instead)");
1651
      if (!(ce->proto == PROTO_UDPv4 || ce->proto == PROTO_TCPv4_SERVER))
1663
      if (!(proto_is_dgram(ce->proto) || ce->proto == PROTO_TCPv4_SERVER || ce->proto == PROTO_TCPv6_SERVER ))
1652
	msg (M_USAGE, "--mode server currently only supports --proto udp or --proto tcp-server");
1664
        msg (M_USAGE, "--mode server currently only supports --proto udp or --proto tcp-server (also udp6/tcp6)");
1653
      if (ce->proto != PROTO_UDPv4 && (options->cf_max || options->cf_per))
1665
      if (!proto_is_udp(ce->proto) && (options->cf_max || options->cf_per))
1654
	msg (M_USAGE, "--connect-freq only works with --mode server --proto udp.  Try --max-clients instead.");
1666
	msg (M_USAGE, "--connect-freq only works with --mode server --proto udp.  Try --max-clients instead.");
1655
      if (!(dev == DEV_TYPE_TAP || (dev == DEV_TYPE_TUN && options->topology == TOP_SUBNET)) && options->ifconfig_pool_netmask)
1667
      if (!(dev == DEV_TYPE_TAP || (dev == DEV_TYPE_TUN && options->topology == TOP_SUBNET)) && options->ifconfig_pool_netmask)
1656
	msg (M_USAGE, "The third parameter to --ifconfig-pool (netmask) is only valid in --dev tap mode");
1668
	msg (M_USAGE, "The third parameter to --ifconfig-pool (netmask) is only valid in --dev tap mode");
Lines 1730-1736 Link Here
1730
  /*
1742
  /*
1731
   * Check consistency of replay options
1743
   * Check consistency of replay options
1732
   */
1744
   */
1733
  if ((ce->proto != PROTO_UDPv4)
1745
  if ((!proto_is_udp(ce->proto))
1734
      && (options->replay_window != defaults.replay_window
1746
      && (options->replay_window != defaults.replay_window
1735
	  || options->replay_time != defaults.replay_time))
1747
	  || options->replay_time != defaults.replay_time))
1736
    msg (M_USAGE, "--replay-window only makes sense with --proto udp");
1748
    msg (M_USAGE, "--replay-window only makes sense with --proto udp");
Lines 1892-1897 Link Here
1892
    {
1904
    {
1893
      if (ce->proto == PROTO_TCPv4)
1905
      if (ce->proto == PROTO_TCPv4)
1894
	ce->proto = PROTO_TCPv4_SERVER;
1906
	ce->proto = PROTO_TCPv4_SERVER;
1907
#ifdef USE_PF_INET6
1908
      else if (ce->proto == PROTO_TCPv6)
1909
        ce->proto = PROTO_TCPv6_SERVER;
1910
#endif
1895
    }
1911
    }
1896
#endif
1912
#endif
1897
#if P2MP
1913
#if P2MP
Lines 1899-1908 Link Here
1899
    {
1915
    {
1900
      if (ce->proto == PROTO_TCPv4)
1916
      if (ce->proto == PROTO_TCPv4)
1901
	ce->proto = PROTO_TCPv4_CLIENT;
1917
	ce->proto = PROTO_TCPv4_CLIENT;
1902
    }
1918
#ifdef USE_PF_INET6
1919
      else if (ce->proto == PROTO_TCPv6)
1920
        ce->proto = PROTO_TCPv6_CLIENT;
1921
#endif
1922
}
1903
#endif
1923
#endif
1904
1924
1905
  if (ce->proto == PROTO_TCPv4_CLIENT && !ce->local && !ce->local_port_defined && !ce->bind_defined)
1925
  if ((ce->proto == PROTO_TCPv4_CLIENT||ce->proto == PROTO_TCPv6_CLIENT) && !ce->local && !ce->local_port_defined && !ce->bind_defined)
1906
    ce->bind_local = false;
1926
    ce->bind_local = false;
1907
1927
1908
#ifdef ENABLE_SOCKS
1928
#ifdef ENABLE_SOCKS

Return to bug 183457