| 1 |
/* efw_iptables.c -- iptables implementation - updates Linux iptables |
| 2 |
* |
| 3 |
* GPLv2 only - Copyright (C) 2008 - 2012 |
| 4 |
* David Sommerseth <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; version 2 |
| 9 |
* of the License. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
/** |
| 23 |
* @file efw-iptables.c |
| 24 |
* @author David Sommerseth <[email protected]> |
| 25 |
* @date 2008-08-10 |
| 26 |
* |
| 27 |
* @brief Firewall driver for iptables. Understands how to update iptables, in other words. |
| 28 |
* |
| 29 |
*/ |
| 30 |
|
| 31 |
|
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <errno.h> |
| 36 |
#include <unistd.h> |
| 37 |
#include <pthread.h> |
| 38 |
#include <sys/wait.h> |
| 39 |
|
| 40 |
#define EUREPHIA_FWINTF /**< Include the proper eurephiaFWINTF declaration in eurephiaCTX */ |
| 41 |
#include <eurephiafw_struct.h> |
| 42 |
#include <eurephia_context.h> |
| 43 |
#include <eurephia_nullsafe.h> |
| 44 |
#include <eurephia_log.h> |
| 45 |
#include <eurephiafw_helpers.h> |
| 46 |
|
| 47 |
#define INTERFACEVER "1.1" /**< The version of this firewall interface (driver) */ |
| 48 |
#define INTERFACEAPIVER 2 /**< Define the API level this firewall interface uses. */ |
| 49 |
|
| 50 |
/** |
| 51 |
* eFWmode string translation table |
| 52 |
*/ |
| 53 |
static const char *eFWmode_str[7] = { |
| 54 |
"ADD", |
| 55 |
"DELETE", |
| 56 |
"BLACKLIST", |
| 57 |
"FLUSH", |
| 58 |
"INITIALISE", |
| 59 |
"SHUTDOWN", |
| 60 |
NULL |
| 61 |
}; |
| 62 |
|
| 63 |
/** |
| 64 |
* Mandatory function, contains driver information. |
| 65 |
* |
| 66 |
* @return Retuns a static string, containing the version information. |
| 67 |
*/ |
| 68 |
const char *eFWinterfaceVersion() { |
| 69 |
return "eFW-iptables (v"INTERFACEVER") David Sommerseth 2008 (C) GPLv2"; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
/** |
| 74 |
* Mandatory function, contains driver information. |
| 75 |
* |
| 76 |
* @return Retuns an integer which correponds to the API level this driver corresponds to. |
| 77 |
*/ |
| 78 |
int eFWinterfaceAPIversion() { |
| 79 |
return INTERFACEAPIVER; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
int process_input(eurephiaCTX *ctx, const char *fwcmd, const eFWupdateRequest *req); |
| 84 |
int call_iptables(eurephiaCTX *ctx, const char *fwcmd, const char **ipt_args); |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* The main routine of the firewall interface. This loops until it gets a shutdown message. |
| 89 |
* |
| 90 |
* @param fwargs efw_threaddata pointer, with needed information to communicate with the openvpn process. |
| 91 |
*/ |
| 92 |
void eFW_RunFirewall(void *fwargs) { |
| 93 |
efw_threaddata *cfg = (efw_threaddata *) fwargs; |
| 94 |
eurephiaCTX *ctx = (eurephiaCTX *) cfg->ctx; |
| 95 |
int quit = 0; |
| 96 |
unsigned int prio; |
| 97 |
eFWupdateRequest req; |
| 98 |
struct timespec tsp; |
| 99 |
|
| 100 |
DEBUG(ctx, 28, "eFW_RunFirewall: Waiting for eFW master to get ready"); |
| 101 |
sem_wait(cfg->semp_master); |
| 102 |
DEBUG(ctx, 28, "eFW_RunFirewall: Telling eFW master that the worker process is ready"); |
| 103 |
sem_post(cfg->semp_worker); |
| 104 |
|
| 105 |
if( cfg->fw_command == NULL ) { |
| 106 |
eurephia_log(ctx, LOG_FATAL, 0, |
| 107 |
"eFW_RunFirewall: firewall_command is not configured. " |
| 108 |
"iptables will not be updated."); |
| 109 |
exit(3); |
| 110 |
} |
| 111 |
|
| 112 |
eurephia_log(ctx, LOG_INFO, 1, "efw_iptables: Firewall interface started"); |
| 113 |
|
| 114 |
// Main loop ... grab messages of the messague queue until shutdown command is sent, or a failure happens |
| 115 |
while( quit == 0 ) { |
| 116 |
memset(&req, 0, sizeof(req)); |
| 117 |
if( mq_receive(cfg->msgq, (char *)&req, EFW_MSG_SIZE, &prio) == -1 ) { |
| 118 |
eurephia_log(ctx, LOG_FATAL, 0, |
| 119 |
"eFW_RunFirewall: Error while reading messages from queue: %s", |
| 120 |
strerror(errno)); |
| 121 |
exit(2); |
| 122 |
} |
| 123 |
if( req.mode != fwSHUTDOWN ) { |
| 124 |
int res = 0; |
| 125 |
|
| 126 |
DEBUG(ctx, 20, "eFW_RunFirewall: Received %s", eFWmode_str[req.mode]); |
| 127 |
|
| 128 |
res = process_input(ctx, cfg->fw_command, &req); |
| 129 |
if( ! res ) { |
| 130 |
quit = 1; |
| 131 |
eurephia_log(ctx, LOG_FATAL, 0, |
| 132 |
"eFW_RunFirewall: Failed updating iptables"); |
| 133 |
} |
| 134 |
} else { |
| 135 |
quit = 1; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
efwRemoveMessageQueue(ctx, fwargs); |
| 141 |
|
| 142 |
DEBUG(ctx, 28, "eFW_RunFirewall: Telling eFW master that the worker process is about to shut down"); |
| 143 |
sem_post(cfg->semp_worker); |
| 144 |
|
| 145 |
DEBUG(ctx, 28, "eFW_RunFirewall: Waiting for eFW master to acknowledge"); |
| 146 |
// Prepare a timeout |
| 147 |
if( clock_gettime(CLOCK_REALTIME, &tsp) == -1 ) { |
| 148 |
eurephia_log(ctx, LOG_FATAL, 0, "eFW_RunFirewall: Could not prepare timeout for shutdown ack: %s", |
| 149 |
strerror(errno)); |
| 150 |
sleep(10); |
| 151 |
} else { |
| 152 |
tsp.tv_sec += 30; // Wait up to 30 seconds for shutdown ack. |
| 153 |
|
| 154 |
// Wait for acknowledge |
| 155 |
if( sem_timedwait(cfg->semp_master, &tsp) == -1 ) { |
| 156 |
eurephia_log(ctx, LOG_PANIC, 0, "eFW_RunFirewall: Did not receive any shutdown ack: %s", |
| 157 |
strerror(errno)); |
| 158 |
} else { |
| 159 |
eurephia_log(ctx, LOG_INFO, 1, "efw_iptables: Firewall interface is shut down"); |
| 160 |
} |
| 161 |
} |
| 162 |
efwRemoveSemaphores(ctx, fwargs); |
| 163 |
exit(0); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
/** |
| 168 |
* Internal function. Processes firewall update messages recieved via POSIX MQ. |
| 169 |
* |
| 170 |
* @param ctx eurephiaCTX - This is just a shadow context, to make logging possible |
| 171 |
* @param fwcmd The command to be executed, can be 'A'-add, 'D'-delete, 'F'-flush, 'B'-blacklist, 'I'-init |
| 172 |
* @param req Pointer a eFWupdateRequest struct with the iptables update details |
| 173 |
* |
| 174 |
* @return Returns 1 on success, otherwise 0. If 0 is sent, it means the firewall process should shut down, |
| 175 |
* and it should only be used in very critical situations. |
| 176 |
*/ |
| 177 |
int process_input(eurephiaCTX *ctx, const char *fwcmd, const eFWupdateRequest *req) { |
| 178 |
const char *iptables_args[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; |
| 179 |
int ret = 0, argc = 2; |
| 180 |
|
| 181 |
DEBUG(ctx, 36, "eFW_RunFirewall::process_input(ctx, %s, %s, %s, %s, %s)", |
| 182 |
eFWmode_str[req->mode], req->ipaddress, req->macaddress, |
| 183 |
req->rule_destination, req->goto_destination); |
| 184 |
|
| 185 |
// |
| 186 |
// Simple parsing of the input string |
| 187 |
// |
| 188 |
iptables_args[0] = (char *)fwcmd; |
| 189 |
|
| 190 |
switch( req->mode ) { |
| 191 |
case fwADD: |
| 192 |
iptables_args[1] = "-A"; |
| 193 |
case fwDELETE: |
| 194 |
if( iptables_args[1] == NULL ) { |
| 195 |
iptables_args[1] = "-D"; |
| 196 |
} |
| 197 |
// Prepare iptables arguments |
| 198 |
iptables_args[argc++] = req->rule_destination; |
| 199 |
if( strlen_nullsafe(req->ipaddress) > 0 ) { |
| 200 |
iptables_args[argc++] = "-s\0"; |
| 201 |
iptables_args[argc++] = req->ipaddress; |
| 202 |
} |
| 203 |
if( strlen_nullsafe(req->macaddress) > 0 ) { |
| 204 |
iptables_args[argc++] = "-m\0"; |
| 205 |
iptables_args[argc++] = "mac\0"; |
| 206 |
iptables_args[argc++] = "--mac-source\0"; |
| 207 |
iptables_args[argc++] = req->macaddress; |
| 208 |
} |
| 209 |
iptables_args[argc++] = "-m\0"; |
| 210 |
iptables_args[argc++] = "conntrack\0"; |
| 211 |
iptables_args[argc++] = "--ctstate\0"; |
| 212 |
iptables_args[argc++] = "NEW\0"; |
| 213 |
iptables_args[argc++] = "-j\0"; |
| 214 |
iptables_args[argc++] = req->goto_destination; |
| 215 |
iptables_args[argc++] = NULL; |
| 216 |
|
| 217 |
eurephia_log(ctx, LOG_INFO, 3, "eFW_RunFirewall - updating iptables rules " |
| 218 |
"==> mode: %s ipaddr: %s macaddr: '%s' destchain: '%s' jump: '%s'", |
| 219 |
eFWmode_str[req->mode], req->ipaddress, req->macaddress, |
| 220 |
req->rule_destination, req->goto_destination); |
| 221 |
ret = call_iptables(ctx, fwcmd, iptables_args); |
| 222 |
break; |
| 223 |
|
| 224 |
case fwBLACKLIST: |
| 225 |
iptables_args[1] = "-A\0"; |
| 226 |
iptables_args[2] = req->rule_destination; |
| 227 |
iptables_args[3] = "-s\0"; |
| 228 |
iptables_args[4] = req->ipaddress; |
| 229 |
iptables_args[5] = "-j\0"; |
| 230 |
iptables_args[6] = strlen_nullsafe(req->goto_destination) > 0 |
| 231 |
? req->goto_destination : "DROP\0"; |
| 232 |
iptables_args[7] = NULL; |
| 233 |
|
| 234 |
eurephia_log(ctx, LOG_INFO, 3, "eFW_RunFirewall - updating iptables rules " |
| 235 |
"==> mode: BLACKLIST destchain: '%s' IP address: %s Send to: '%s'", |
| 236 |
req->rule_destination, req->ipaddress, req->goto_destination); |
| 237 |
ret = call_iptables(ctx, fwcmd, iptables_args); |
| 238 |
break; |
| 239 |
|
| 240 |
case fwFLUSH: |
| 241 |
iptables_args[1] = "-F\0"; |
| 242 |
iptables_args[2] = req->rule_destination; |
| 243 |
iptables_args[3] = NULL; |
| 244 |
eurephia_log(ctx, LOG_INFO, 3, "eFW_RunFirewall - updating iptables rules " |
| 245 |
"==> mode: FLUSH destchain: '%s'", req->rule_destination); |
| 246 |
ret = call_iptables(ctx, fwcmd, iptables_args); |
| 247 |
break; |
| 248 |
|
| 249 |
case fwINITIALISE: |
| 250 |
// Init chain - flush it and then add needed rule for stateful inspection |
| 251 |
eurephia_log(ctx, LOG_INFO, 3, "eFW_RunFirewall - Initialising iptables chain '%s'", |
| 252 |
req->rule_destination); |
| 253 |
|
| 254 |
// Flush phase |
| 255 |
iptables_args[1] = "-F"; |
| 256 |
iptables_args[2] = req->rule_destination; |
| 257 |
iptables_args[3] = NULL; |
| 258 |
ret = call_iptables(ctx, fwcmd, iptables_args); |
| 259 |
|
| 260 |
// Add stateful inspection |
| 261 |
iptables_args[1] = "-I\0"; |
| 262 |
iptables_args[2] = req->rule_destination; |
| 263 |
iptables_args[3] = "-m\0"; |
| 264 |
iptables_args[4] = "conntrack\0"; |
| 265 |
iptables_args[5] = "--ctstate\0"; |
| 266 |
iptables_args[6] = "ESTABLISHED,RELATED\0"; |
| 267 |
iptables_args[7] = "-j\0"; |
| 268 |
iptables_args[8] = "ACCEPT\0"; |
| 269 |
ret &= call_iptables(ctx, fwcmd, iptables_args); |
| 270 |
break; |
| 271 |
|
| 272 |
default: |
| 273 |
eurephia_log(ctx, LOG_CRITICAL, 0, "eFW_RunFirewall::process_input: Malformed update request"); |
| 274 |
ret = 1; |
| 275 |
} |
| 276 |
return ret; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
/** |
| 281 |
* This function does the actual iptables call. It will fork out a process and do the |
| 282 |
* assigned iptables command. |
| 283 |
* |
| 284 |
* @param ctx eurephiaCTX - shadow context, only with pointers to log files. |
| 285 |
* @param fwcmd String containing full filename to the binary to execute |
| 286 |
* @param ipt_args The iptables arguments |
| 287 |
* |
| 288 |
* @return Returns 1 on success, otherwise 0. When 0 is returned, the complete firewall process will be |
| 289 |
* shut down. |
| 290 |
*/ |
| 291 |
int call_iptables(eurephiaCTX *ctx, const char *fwcmd, const char **ipt_args) { |
| 292 |
pid_t pid; |
| 293 |
int cmdret = -1; |
| 294 |
|
| 295 |
// Fork out a child process which will run the iptables command. Since the execve replaces |
| 296 |
// the current process, we need to do the forking first. |
| 297 |
if( (pid = fork()) < 0) { |
| 298 |
eurephia_log(ctx, LOG_FATAL, 0, |
| 299 |
"eFW_RunFirewall::process_input: Failed to fork process for %s", fwcmd); |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
switch( pid ) { |
| 304 |
case 0: // child process - execute the program and exit |
| 305 |
execve(fwcmd, (char * const*) ipt_args, NULL); |
| 306 |
exit(1); // execve should replace the process, but if it fails to do so, make sure we exit |
| 307 |
|
| 308 |
default: // parent process |
| 309 |
if( waitpid(pid, &cmdret, 0) != pid ) { |
| 310 |
eurephia_log(ctx, LOG_WARNING, 0, |
| 311 |
"eFW_RunFirewall::process_input: Failed to wait for process for %s" |
| 312 |
" to complete (%s)", fwcmd, strerror(errno)); |
| 313 |
} |
| 314 |
eurephia_log(ctx, LOG_INFO, 4, "eFW_RunFirewall - iptables exited with code: %i ", cmdret); |
| 315 |
} |
| 316 |
return 1; |
| 317 |
} |