1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
#include "iptables.h"
#include "main.h"
#include "str.h"
int (*iptables_add_rule)(const socket_t *local_sock, const str *comment);
int (*iptables_del_rule)(const socket_t *local_sock);
#ifdef WITH_IPTABLES_OPTION
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <libiptc/libiptc.h>
#include <libiptc/libip6tc.h>
#include <libiptc/libxtc.h>
#include <linux/netfilter/xt_comment.h>
#include <glib.h>
#include <errno.h>
#include <sys/file.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "helpers.h"
#include "log.h"
#include "socket.h"
#undef __ALIGN_KERNEL
#define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (__typeof(x))(a) - 1)
#define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
#undef XT_ALIGN
#define XT_ALIGN(s) __ALIGN_KERNEL((s), __alignof__(struct _xt_align))
#ifndef XT_LOCK_NAME
#define XT_LOCK_NAME "/run/xtables.lock"
#endif
struct ipt_matches {
struct xt_entry_match udp_match;
struct xt_udp udp;
struct xt_entry_match comment_match;
struct xt_comment_info comment;
struct xt_standard_target target;
};
struct ipv4_ipt_entry {
struct ipt_entry entry;
struct ipt_matches matches;
};
struct ipv6_ipt_entry {
struct ip6t_entry entry;
struct ipt_matches matches;
};
static mutex_t __xt_lock = MUTEX_STATIC_INIT;
static int __xt_lock_fd = -1;
static void xt_lock(void) {
mutex_lock(&__xt_lock);
__xt_lock_fd = open(XT_LOCK_NAME, O_CREAT, 0600);
if (__xt_lock_fd == -1) {
ilog(LOG_WARN, "Could not open xtables lock file '%s': %s", XT_LOCK_NAME, strerror(errno));
// as per iptables source code, continue anyway
return;
}
if (flock(__xt_lock_fd, LOCK_EX)) {
ilog(LOG_WARN, "Failed to acquire lock file '%s': %s", XT_LOCK_NAME, strerror(errno));
close(__xt_lock_fd);
__xt_lock_fd = -1;
}
}
static void xt_unlock(void) {
if (__xt_lock_fd != -1)
close(__xt_lock_fd);
__xt_lock_fd = -1; // coverity[missing_lock : FALSE]
mutex_unlock(&__xt_lock);
}
static void ip46tables_fill_matches(struct ipt_matches *matches, const socket_t *local_sock,
const str *comment)
{
matches->target.target.u.user.target_size = XT_ALIGN(sizeof(struct xt_standard_target));
strcpy(matches->target.target.u.user.name, "ACCEPT");
strcpy(matches->udp_match.u.user.name, "udp");
matches->udp_match.u.match_size = XT_ALIGN(sizeof(struct xt_entry_match)) + XT_ALIGN(sizeof(struct xt_udp));
matches->udp.dpts[0] = matches->udp.dpts[1] = local_sock->local.port;
matches->udp.spts[0] = 0;
matches->udp.spts[1] = 0xffff;
matches->comment_match.u.match_size = XT_ALIGN(sizeof(struct xt_entry_match))
+ XT_ALIGN(sizeof(struct xt_comment_info));
strcpy(matches->comment_match.u.user.name, "comment");
if (comment)
str_ncpy(matches->comment.comment, sizeof(matches->comment.comment), comment);
}
static void ip4_fill_entry(struct ipv4_ipt_entry *entry, const socket_t *local_sock, const str *comment) {
ZERO(*entry);
entry->entry.ip.proto = IPPROTO_UDP;
entry->entry.ip.dst = local_sock->local.address.ipv4;
memset(&entry->entry.ip.dmsk, 0xff, sizeof(entry->entry.ip.dmsk));
entry->entry.target_offset = G_STRUCT_OFFSET(struct ipv4_ipt_entry, matches.target);
ip46tables_fill_matches(&entry->matches, local_sock, comment);
entry->entry.next_offset = entry->entry.target_offset + entry->matches.target.target.u.user.target_size;
}
static void ip6_fill_entry(struct ipv6_ipt_entry *entry, const socket_t *local_sock, const str *comment) {
ZERO(*entry);
entry->entry.ipv6.proto = IPPROTO_UDP;
entry->entry.ipv6.dst = local_sock->local.address.ipv6;
entry->entry.ipv6.flags |= IP6T_F_PROTO;
memset(&entry->entry.ipv6.dmsk, 0xff, sizeof(entry->entry.ipv6.dmsk));
entry->entry.target_offset = G_STRUCT_OFFSET(struct ipv6_ipt_entry, matches.target);
ip46tables_fill_matches(&entry->matches, local_sock, comment);
entry->entry.next_offset = entry->entry.target_offset + entry->matches.target.target.u.user.target_size;
}
static const char *ip4tables_add_rule(const socket_t *local_sock, const str *comment) {
struct xtc_handle *h;
struct ipv4_ipt_entry entry;
const char *err;
xt_lock();
err = "could not initialize iptables";
h = iptc_init("filter");
if (!h)
goto err2;
ip4_fill_entry(&entry, local_sock, comment);
err = "failed to append iptables entry";
if (!iptc_append_entry(rtpe_config.iptables_chain, &entry.entry, h))
goto err;
err = "failed to commit iptables changes";
if (!iptc_commit(h))
goto err;
err = NULL;
err:
iptc_free(h);
err2:
xt_unlock();
return err;
}
static const char *ip6tables_add_rule(const socket_t *local_sock, const str *comment) {
struct xtc_handle *h;
struct ipv6_ipt_entry entry;
const char *err;
xt_lock();
err = "could not initialize ip6tables";
h = ip6tc_init("filter");
if (!h)
goto err2;
ip6_fill_entry(&entry, local_sock, comment);
err = "failed to append ip6tables entry";
if (!ip6tc_append_entry(rtpe_config.iptables_chain, &entry.entry, h))
goto err;
err = "failed to commit ip6tables changes";
if (!ip6tc_commit(h))
goto err;
err = NULL;
err:
ip6tc_free(h);
err2:
xt_unlock();
return err;
}
static const char *ip4tables_del_rule(const socket_t *local_sock) {
struct xtc_handle *h;
struct ipv4_ipt_entry entry, mask;
const char *err;
xt_lock();
err = "could not initialize iptables";
h = iptc_init("filter");
if (!h)
goto err2;
ip4_fill_entry(&entry, local_sock, NULL);
// match everything except the comment
memset(&mask, 0, sizeof(mask));
memset(&mask.entry, 0xff, sizeof(mask.entry));
memset(&mask.matches.udp_match, 0xff, sizeof(mask.matches.udp_match));
memset(&mask.matches.udp, 0xff, sizeof(mask.matches.udp));
memset(&mask.matches.comment_match, 0xff, sizeof(mask.matches.comment_match));
memset(&mask.matches.target, 0xff, sizeof(mask.matches.target));
err = "failed to delete iptables entry";
if (!iptc_delete_entry(rtpe_config.iptables_chain, &entry.entry, (unsigned char *) &mask, h))
goto err;
err = "failed to commit iptables changes";
if (!iptc_commit(h))
goto err;
err = NULL;
err:
iptc_free(h);
err2:
xt_unlock();
return err;
}
static const char *ip6tables_del_rule(const socket_t *local_sock) {
struct xtc_handle *h;
struct ipv6_ipt_entry entry, mask;
const char *err;
xt_lock();
err = "could not initialize ip6tables";
h = ip6tc_init("filter");
if (!h)
goto err2;
ip6_fill_entry(&entry, local_sock, NULL);
// match everything except the comment
memset(&mask, 0, sizeof(mask));
memset(&mask.entry, 0xff, sizeof(mask.entry));
memset(&mask.matches.udp_match, 0xff, sizeof(mask.matches.udp_match));
memset(&mask.matches.udp, 0xff, sizeof(mask.matches.udp));
memset(&mask.matches.comment_match, 0xff, sizeof(mask.matches.comment_match));
memset(&mask.matches.target, 0xff, sizeof(mask.matches.target));
err = "failed to delete ip6tables entry";
if (!ip6tc_delete_entry(rtpe_config.iptables_chain, &entry.entry, (unsigned char *) &mask, h))
goto err;
err = "failed to commit ip6tables changes";
if (!ip6tc_commit(h))
goto err;
err = NULL;
err:
ip6tc_free(h);
err2:
xt_unlock();
return err;
}
static int __iptables_add_rule(const socket_t *local_sock, const str *comment) {
const char *err;
switch (local_sock->family->af) {
case AF_INET:
err = ip4tables_add_rule(local_sock, comment);
break;
case AF_INET6:
err = ip6tables_add_rule(local_sock, comment);
break;
default:
err = "unsupported socket family";
break;
};
if (err)
ilog(LOG_ERROR, "Error adding iptables rule (for '" STR_FORMAT_M "'): %s (%s)",
STR_FMT_M(comment), err, strerror(errno));
return 0;
}
static int __iptables_del_rule(const socket_t *local_sock) {
const char *err;
if (!local_sock || !local_sock->family)
return 0;
switch (local_sock->family->af) {
case AF_INET:
err = ip4tables_del_rule(local_sock);
break;
case AF_INET6:
err = ip6tables_del_rule(local_sock);
break;
default:
err = "unsupported socket family";
break;
};
if (err)
ilog(LOG_ERROR, "Error deleting iptables rule: %s (%s)",
err, strerror(errno));
return 0;
}
#endif // WITH_IPTABLES_OPTION
static int __iptables_stub(void) {
return 0;
}
void iptables_init(void) {
if (rtpe_config.iptables_chain && !rtpe_config.iptables_chain[0])
rtpe_config.iptables_chain = NULL;
if (!rtpe_config.iptables_chain) {
iptables_add_rule = (void *) __iptables_stub;
iptables_del_rule = (void *) __iptables_stub;
return;
}
#ifdef WITH_IPTABLES_OPTION
iptables_add_rule = __iptables_add_rule;
iptables_del_rule = __iptables_del_rule;
// flush chains
const char *err;
struct xtc_handle *h;
xt_lock();
err = "could not initialize iptables";
h = iptc_init("filter");
if (!h)
goto out;
err = "could not flush iptables chain";
if (!iptc_flush_entries(rtpe_config.iptables_chain, h))
goto err2;
err = "could not commit iptables changes";
if (!iptc_commit(h))
goto err2;
iptc_free(h);
err = "could not initialize ip6tables";
h = ip6tc_init("filter");
if (!h)
goto out;
err = "could not flush ip6tables chain";
if (!ip6tc_flush_entries(rtpe_config.iptables_chain, h))
goto err1;
err = "could not commit iptables changes";
if (!ip6tc_commit(h))
goto err1;
ip6tc_free(h);
err = NULL;
goto out;
err1:
ip6tc_free(h);
goto out;
err2:
iptc_free(h);
goto out;
out:
xt_unlock();
if (err)
ilog(LOG_ERROR, "Failed to flush iptables chain: %s (%s)", err, strerror(errno));
#endif // WITH_IPTABLES_OPTION
}
|