*/
static const char *sbin_iptables;
+/**
+ * Name and full path of IPTABLES binary.
+ */
+static const char *sbin_ip6tables;
+
/**
* Name and full path of sysctl binary
*/
return 254;
}
#endif
- if (0 == strncmp(argv[6], "1", 2))
+ if (0 == strncmp (argv[6], "1", 2))
nortsetup = 1;
if (0 == nortsetup)
strerror (errno));
return 3;
}
+ if (0 == access ("/sbin/ip6tables", X_OK))
+ sbin_ip6tables = "/sbin/ip6tables";
+ else if (0 == access ("/usr/sbin/ip6tables", X_OK))
+ sbin_ip6tables = "/usr/sbin/ip6tables";
+ else
+ {
+ fprintf (stderr,
+ "Fatal: executable ip6tables not found in approved directories: %s\n",
+ strerror (errno));
+ return 3;
+ }
if (0 == access ("/sbin/ip", X_OK))
sbin_ip = "/sbin/ip";
else if (0 == access ("/usr/sbin/ip", X_OK))
if (0 != fork_and_exec (sbin_iptables, mangle_args))
goto cleanup_rest;
}
+ {
+ char *const mangle_args[] =
+ {
+ "ip6tables", "-m", "owner", "-t", "mangle", "-I", "OUTPUT", "1", "-p",
+ "udp", "--gid-owner", mygid, "--dport", DNS_PORT, "-j",
+ "ACCEPT", NULL
+ };
+ if (0 != fork_and_exec (sbin_ip6tables, mangle_args))
+ goto cleanup_rest;
+ }
/* Mark all of the other DNS traffic using our mark DNS_MARK */
{
char *const mark_args[] =
if (0 != fork_and_exec (sbin_iptables, mark_args))
goto cleanup_mangle_1;
}
+ {
+ char *const mark_args[] =
+ {
+ "ip6tables", "-t", "mangle", "-I", "OUTPUT", "2", "-p",
+ "udp", "--dport", DNS_PORT, "-j", "MARK", "--set-mark", DNS_MARK,
+ NULL
+ };
+ if (0 != fork_and_exec (sbin_ip6tables, mark_args))
+ goto cleanup_mangle_1;
+ }
/* Forward all marked DNS traffic to our DNS_TABLE */
{
char *const forward_args[] =
if (0 != fork_and_exec (sbin_ip, forward_args))
goto cleanup_mark_2;
}
+ {
+ char *const forward_args[] =
+ {
+ "ip", "-6", "rule", "add", "fwmark", DNS_MARK, "table", DNS_TABLE, NULL
+ };
+ if (0 != fork_and_exec (sbin_ip, forward_args))
+ goto cleanup_mark_2;
+ }
/* Finally, add rule in our forwarding table to pass to our virtual interface */
{
char *const route_args[] =
if (0 != fork_and_exec (sbin_ip, route_args))
goto cleanup_forward_3;
}
+ {
+ char *const route_args[] =
+ {
+ "ip", "-6", "route", "add", "default", "dev", dev,
+ "table", DNS_TABLE, NULL
+ };
+ if (0 != fork_and_exec (sbin_ip, route_args))
+ goto cleanup_forward_3;
+ }
}
/* drop privs *except* for the saved UID; this is not perfect, but better
if (0 != fork_and_exec (sbin_ip, route_clean_args))
r += 1;
}
+ if (0 == nortsetup)
+ {
+ char *const route_clean_args[] =
+ {
+ "ip", "-6", "route", "del", "default", "dev", dev,
+ "table", DNS_TABLE, NULL
+ };
+ if (0 != fork_and_exec (sbin_ip, route_clean_args))
+ r += 1;
+ }
cleanup_forward_3:
if (0 == nortsetup)
{
if (0 != fork_and_exec (sbin_ip, forward_clean_args))
r += 2;
}
+ if (0 == nortsetup)
+ {
+ char *const forward_clean_args[] =
+ {
+ "ip", "-6", "rule", "del", "fwmark", DNS_MARK, "table", DNS_TABLE, NULL
+ };
+ if (0 != fork_and_exec (sbin_ip, forward_clean_args))
+ r += 2;
+ }
cleanup_mark_2:
if (0 == nortsetup)
{
if (0 != fork_and_exec (sbin_iptables, mark_clean_args))
r += 4;
}
+ if (0 == nortsetup)
+ {
+ char *const mark_clean_args[] =
+ {
+ "ip6tables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp",
+ "--dport", DNS_PORT, "-j", "MARK", "--set-mark", DNS_MARK, NULL
+ };
+ if (0 != fork_and_exec (sbin_ip6tables, mark_clean_args))
+ r += 4;
+ }
cleanup_mangle_1:
if (0 == nortsetup)
{
if (0 != fork_and_exec (sbin_iptables, mangle_clean_args))
r += 8;
}
+ if (0 == nortsetup)
+ {
+ char *const mangle_clean_args[] =
+ {
+ "ip6tables", "-m", "owner", "-t", "mangle", "-D", "OUTPUT", "-p", "udp",
+ "--gid-owner", mygid, "--dport", DNS_PORT, "-j", "ACCEPT",
+ NULL
+ };
+ if (0 != fork_and_exec (sbin_ip6tables, mangle_clean_args))
+ r += 8;
+ }
cleanup_rest:
/* close virtual interface */
destination_port = src->sin6_port;
GNUNET_TUN_initialize_ipv6_header (&ip6,
IPPROTO_UDP,
- reply_len - sizeof (struct GNUNET_TUN_IPv6Header),
+ reply_len - off - sizeof (struct GNUNET_TUN_IPv6Header),
&dst->sin6_addr,
&src->sin6_addr);
GNUNET_memcpy (&buf[off], &ip6, sizeof (ip6));
ip6 = (const struct GNUNET_TUN_IPv6Header *) &tun[1];
if ( (msize < sizeof (struct GNUNET_TUN_IPv6Header)) ||
(ip6->version != 6) ||
- (ntohs (ip6->payload_length) != msize) ||
+ (ntohs (ip6->payload_length) != msize - sizeof (struct GNUNET_TUN_IPv6Header)) ||
(ip6->next_header != IPPROTO_UDP) )
{
/* non-IP/UDP packet received on TUN (or with extensions) */
_("Received malformed IPv6-UDP packet on TUN interface.\n"));
return GNUNET_OK;
}
- udp = (const struct GNUNET_TUN_UdpHeader*) &ip6[1];
+ udp = (const struct GNUNET_TUN_UdpHeader *) &ip6[1];
msize -= sizeof (struct GNUNET_TUN_IPv6Header);
break;
default:
(DNS_PORT != ntohs (udp->destination_port)) )
{
/* non-DNS packet received on TUN, ignore */
+ GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+ _("DNS interceptor got non-DNS packet (dropped)\n"));
GNUNET_STATISTICS_update (stats,
gettext_noop ("# Non-DNS UDP packet received via TUN interface"),
1, GNUNET_NO);
*
* @param ip header to initialize
* @param protocol protocol to use (i.e. IPPROTO_UDP), technically "next_header" for IPv6
- * @param payload_length number of bytes of payload that follow (excluding IPv4 header)
+ * @param payload_length number of bytes of payload that follow (excluding IPv6 header)
* @param src source IP address to use
* @param dst destination IP address to use
*/
}
+/**
+ * Check if two sockaddrs are equal.
+ *
+ * @param sa one address
+ * @param sb another address
+ * @param include_port also check ports
+ * @return #GNUNET_YES if they are equal
+ */
+int
+GNUNET_TUN_sockaddr_cmp (const struct sockaddr *sa,
+ const struct sockaddr *sb,
+ int include_port)
+{
+ if (sa->sa_family != sb->sa_family)
+ return GNUNET_NO;
+
+ switch (sa->sa_family)
+ {
+ case AF_INET:
+ {
+ const struct sockaddr_in *sa4 = (const struct sockaddr_in *) sa;
+ const struct sockaddr_in *sb4 = (const struct sockaddr_in *) sb;
+ return (sa4->sin_addr.s_addr == sb4->sin_addr.s_addr);
+ }
+ case AF_INET6:
+ {
+ const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *) sa;
+ const struct sockaddr_in6 *sb6 = (const struct sockaddr_in6 *) sb;
+
+ return (0 == memcmp(&sa6->sin6_addr,
+ &sb6->sin6_addr,
+ sizeof (struct in6_addr)));
+ }
+ default:
+ GNUNET_break (0);
+ return GNUNET_SYSERR;
+ }
+}
+
+
/* end of tun.c */