|
27 | 27 | #include "logging.h" |
28 | 28 | #include "process.h" |
29 | 29 |
|
30 | | -/* TODO: NOT IMPLEMENTED */ |
31 | 30 | int fh_ipt6_flush(int auto_create) |
32 | 31 | { |
33 | | - (void) auto_create; |
| 32 | + int res; |
| 33 | + size_t i, cnt; |
| 34 | + char *ipt_flush_cmd[] = {"ip6tables", "-w", "-t", "mangle", |
| 35 | + "-F", "FAKEHTTP", NULL}; |
| 36 | + char *ipt_create_cmds[][32] = { |
| 37 | + {"ip6tables", "-w", "-t", "mangle", "-N", "FAKEHTTP", NULL}, |
34 | 38 |
|
35 | | - return -1; |
| 39 | + {"ip6tables", "-w", "-t", "mangle", "-I", "INPUT", "-j", "FAKEHTTP", |
| 40 | + NULL}, |
| 41 | + |
| 42 | + {"ip6tables", "-w", "-t", "mangle", "-I", "FORWARD", "-j", "FAKEHTTP", |
| 43 | + NULL}}; |
| 44 | + |
| 45 | + res = fh_execute_command(ipt_flush_cmd, 1, NULL); |
| 46 | + if (res < 0) { |
| 47 | + if (!auto_create) { |
| 48 | + E(T(fh_execute_command)); |
| 49 | + return -1; |
| 50 | + } |
| 51 | + |
| 52 | + cnt = sizeof(ipt_create_cmds) / sizeof(*ipt_create_cmds); |
| 53 | + for (i = 0; i < cnt; i++) { |
| 54 | + res = fh_execute_command(ipt_create_cmds[i], 0, NULL); |
| 55 | + if (res < 0) { |
| 56 | + E(T(fh_execute_command)); |
| 57 | + return -1; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return 0; |
36 | 63 | } |
37 | 64 |
|
38 | 65 |
|
39 | | -/* TODO: NOT IMPLEMENTED */ |
40 | 66 | int fh_ipt6_add(void) |
41 | 67 | { |
42 | | - return -1; |
| 68 | + char xmark_str[64], nfqnum_str[32], iface_str[32]; |
| 69 | + size_t i, ipt_cmds_cnt, ipt_opt_cmds_cnt; |
| 70 | + int res; |
| 71 | + char *ipt_cmds[][32] = { |
| 72 | + /* |
| 73 | + exclude marked packets |
| 74 | + */ |
| 75 | + {"ip6tables", "-w", "-t", "mangle", "-A", "FAKEHTTP", "-m", "mark", |
| 76 | + "--mark", xmark_str, "-j", "CONNMARK", "--set-xmark", xmark_str, |
| 77 | + NULL}, |
| 78 | + |
| 79 | + {"ip6tables", "-w", "-t", "mangle", "-A", "FAKEHTTP", "-m", "connmark", |
| 80 | + "--mark", xmark_str, "-j", "MARK", "--set-xmark", xmark_str, NULL}, |
| 81 | + |
| 82 | + {"ip6tables", "-w", "-t", "mangle", "-A", "FAKEHTTP", "-m", "mark", |
| 83 | + "--mark", xmark_str, "-j", "RETURN", NULL}, |
| 84 | + |
| 85 | + /* |
| 86 | + exclude special IPv6 addresses |
| 87 | + */ |
| 88 | + {"ip6tables", "-w", "-t", "mangle", "-A", "FAKEHTTP", "-s", "::/127", |
| 89 | + "-j", "RETURN", NULL}, |
| 90 | + |
| 91 | + {"ip6tables", "-w", "-t", "mangle", "-A", "FAKEHTTP", "-s", |
| 92 | + "::ffff:0:0/96", "-j", "RETURN", NULL}, |
| 93 | + |
| 94 | + {"ip6tables", "-w", "-t", "mangle", "-A", "FAKEHTTP", "-s", |
| 95 | + "64:ff9b::/96", "-j", "RETURN", NULL}, |
| 96 | + |
| 97 | + {"ip6tables", "-w", "-t", "mangle", "-A", "FAKEHTTP", "-s", |
| 98 | + "64:ff9b:1::/48", "-j", "RETURN", NULL}, |
| 99 | + |
| 100 | + {"ip6tables", "-w", "-t", "mangle", "-A", "FAKEHTTP", "-s", |
| 101 | + "2002::/16", "-j", "RETURN", NULL}, |
| 102 | + |
| 103 | + {"ip6tables", "-w", "-t", "mangle", "-A", "FAKEHTTP", "-s", "fc00::/7", |
| 104 | + "-j", "RETURN", NULL}, |
| 105 | + |
| 106 | + {"ip6tables", "-w", "-t", "mangle", "-A", "FAKEHTTP", "-s", |
| 107 | + "fe80::/10", "-j", "RETURN", NULL}, |
| 108 | + |
| 109 | + /* |
| 110 | + send to nfqueue |
| 111 | + */ |
| 112 | + {"ip6tables", "-w", "-t", "mangle", "-A", "FAKEHTTP", "-i", iface_str, |
| 113 | + "-p", "tcp", "--tcp-flags", "ACK,FIN,RST", "ACK", "-j", "NFQUEUE", |
| 114 | + "--queue-bypass", "--queue-num", nfqnum_str, NULL}}; |
| 115 | + |
| 116 | + char *ipt_opt_cmds[][32] = { |
| 117 | + /* |
| 118 | + exclude packets from connections with more than 32 packets |
| 119 | + */ |
| 120 | + {"ip6tables", "-w", "-t", "mangle", "-I", "FAKEHTTP", "-m", |
| 121 | + "connbytes", "!", "--connbytes", "0:32", "--connbytes-dir", "both", |
| 122 | + "--connbytes-mode", "packets", "-j", "RETURN", NULL}, |
| 123 | + |
| 124 | + /* |
| 125 | + exclude big packets |
| 126 | + */ |
| 127 | + {"ip6tables", "-w", "-t", "mangle", "-I", "FAKEHTTP", "-m", "length", |
| 128 | + "!", "--length", "0:120", "-j", "RETURN", NULL}}; |
| 129 | + |
| 130 | + ipt_cmds_cnt = sizeof(ipt_cmds) / sizeof(*ipt_cmds); |
| 131 | + ipt_opt_cmds_cnt = sizeof(ipt_opt_cmds) / sizeof(*ipt_opt_cmds); |
| 132 | + |
| 133 | + res = snprintf(xmark_str, sizeof(xmark_str), "%" PRIu32 "/%" PRIu32, |
| 134 | + g_ctx.fwmark, g_ctx.fwmask); |
| 135 | + if (res < 0 || (size_t) res >= sizeof(xmark_str)) { |
| 136 | + E("ERROR: snprintf(): %s", "failure"); |
| 137 | + return -1; |
| 138 | + } |
| 139 | + |
| 140 | + res = snprintf(nfqnum_str, sizeof(nfqnum_str), "%" PRIu32, g_ctx.nfqnum); |
| 141 | + if (res < 0 || (size_t) res >= sizeof(nfqnum_str)) { |
| 142 | + E("ERROR: snprintf(): %s", "failure"); |
| 143 | + return -1; |
| 144 | + } |
| 145 | + |
| 146 | + res = snprintf(iface_str, sizeof(iface_str), "%s", g_ctx.iface); |
| 147 | + if (res < 0 || (size_t) res >= sizeof(iface_str)) { |
| 148 | + E("ERROR: snprintf(): %s", "failure"); |
| 149 | + return -1; |
| 150 | + } |
| 151 | + |
| 152 | + for (i = 0; i < ipt_cmds_cnt; i++) { |
| 153 | + res = fh_execute_command(ipt_cmds[i], 0, NULL); |
| 154 | + if (res < 0) { |
| 155 | + E(T(fh_execute_command)); |
| 156 | + return -1; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + for (i = 0; i < ipt_opt_cmds_cnt; i++) { |
| 161 | + fh_execute_command(ipt_opt_cmds[i], 1, NULL); |
| 162 | + } |
| 163 | + |
| 164 | + return 0; |
43 | 165 | } |
0 commit comments