标签 wifidog认证 wifidog安装 wifidog原理 wifidog分析 wifidog配置 wifidog流程 wifidog服务器 wifidog-ddwrt wifidog openwrt 下的文章

【Wifidog协议V1】

Wifidog网关协议V1

  1. 网关心跳(Ping协议)

Wifidog将ping协议作为心跳机制向认证服务器发送当前状态信息。这可以实现为认证服务器每个节点的状态生成中央日志。
Wifidog客户端在conf文件中进行设置,目的是通过http定期启动thread(ping_thread.c)向认证服务器发送状态信息。信息格式如下:
http://auth_sever/ping/?
gw_id=%s
sys_uptime=%lu
sys_memfree=%u
sys_load=%.2f
wifidog_uptime=%lu

通过系统调用wifidog客户端收集的数据
Headers
HTTP/1.0\r\n"
"User-Agent: WiFiDog %s\r\n"
"Host: %s\r\n"
"\r\n",

一个典型的HTTP需求应该是:
GET /ping/?gw_id=001217DA42D2&sys_uptime=742725&sys_memfree=2604&sys_load=0.03&wifidog_uptime=3861 HTTP/1.0
User-Agent: WiFiDog 1.1.3_beta6
Host: auth.ilesansfil.org

  1. 认证服务器认证协议

这个页面描述了当用户已经被认证并允许访问互联网时,为了认证用户和进程,wifidog网关和认证服务器之间的信息传送。
Wifidog客户端将定期的启动一个thread来报告每个用户的连接状况。目前它被用来报告每个用户输入/输出计数器,以显示用户依然在现,并允许认证服务器将不再连接的用户断开。
以下是发给每个在线用户的信息
auth_server:/auth/index.php?
stage=
ip=
mac=
token=
incoming=
outgoing=

注意:stage=计数器/登录,取决于是否是新客户端
即使输入输出变量会在所有信息中出现,但他们只对处于counter阶段的信息有效。其它情况下输入输出经常设置为0。
在做回复时,认证服务器会以有效身份或新用户信息,或者认证服务器错误提示形式进行回复。
回复格式如下:
Auth:
新用户状态为:
0 - AUTH_DENIED - User firewall users are deleted and the user removed.
6 - AUTH_VALIDATION_FAILED - User email validation timeout has occured and user/firewall is deleted
1 - AUTH_ALLOWED - User was valid, add firewall rules if not present
5 - AUTH_VALIDATION - Permit user access to email to get validation email under default rules
-1 - AUTH_ERROR - An error occurred during the validation process

注意:认识服务器错误一般不会改变防火墙或用户状态
标准的URL为:
GET /auth/?stage=counters&ip=7.0.0.107&mac=00:40:05:5F:44:43&token=4f473ae3ddc5c1c2165f7a0973c57a98&incoming=6031353&outgoing=827770 HTTP/1.0
User-Agent: WiFiDog 1.1.3_beta6
Host: auth.ilesansfil.org

  1. 网关重定向浏览器,wifidog的http流程

客户端浏览器在不同情况下会被重定向到其它页面:
初始化请求:
基于捕捉,客户端会被网关重定向到以下URL:
login/?gw_address=%s&gw_port=%d&gw_id=%s&url=%s
例如:https://auth.ilesansfil.org/login/?gw_id=0016B6DA9AE0&gw_address=7.0.0.1&gw_port=2060&url=www.baidu.com
初始化请求之后认证服务器重定向浏览器
客户端将被重定向到网关。 http://"gw_address:gw_port/wifidog/auth?token=xxx
URL示例:http://7.0.0.1:2060/wifidog/auth?token=4f473ae3ddc5c1c2165f7a0973c57a98
网关带着auth去认证服务器上认证token值是否正确
http://auth.ilesansfil.org/auth/?stage=login&ip=xxx&mac=xxx&token=4f473ae3ddc5c1c2165f7a0973c57a98&incoming=0&outgoing=0&gw_id=xxx
如果服务器回复AUTH_DENIED:注意你通常在标准认证服务器上看不到这样的提示。客户端将不会被重定向回网关。
gw_message.php?message=denied
如果服务器回复AUTH_VALIDATION:
gw_message.php?message=activate
如果服务器回复AUTH_ALLOWED:这是门户重定向
portal/?gw_id=%s
如果服务器回复AUTH_VALIDATION_FAILED:注意你将不会在标准认证服务器看到此回复。客户端将不会重定向回网关。
gw_message.php?message=failed_validation
路由器收到AUTH_ALLOWED重定向浏览器到http://auth.ilesansfil.org/portal/?gw_id=xxx, auth server确定成功返回成功页面,认证完成。

本文章由 http://www.wifidog.pro/2014/12/09/wifidog%E5%8D%8F%E8%AE%AE%E6%8E%A5%E5%8F%A3.html 整理编辑,转载请注明出处

wifidog 源码初分析(三)

上一篇分析了 接入设备 在接入路由器,并发起首次 HTTP/80 请求到路由器上时,wifidog 是如何将此 HTTP 请求重定向至 auth-server 的流程。

之后接入设备的浏览器接收到 wifidog 返回的 302 重定向请求后,会将页面重定向至 auth-server 的 /login 页面,并且在此 URL 中会携带一些路由器/网关 参数,以及接入设备的 MAC 地址和客户端访问的源URL(如示例中的 baidu.com)。

POST /login/?gw_address=192.168.1.1&gw_port=2060&gw_id=default&mac=44:94:fc:ef:28:40&url=http%3A//www.baidu.com/ HTTP/1.1

auth-server 收到请求后处理,并返回重定向到 wifidog 的响应(注:同时携带了为此接入设备的用户分配了 token),接入设备的浏览器重定向至路由器上 wifidog 的 http 服务(端口 2060) /wifidog/auth 上(且携带了认证服务器为此接入设备分配的 token),下面介绍下 wifidog 接收到 /wifidog/auth 的访问后的校验流程。
在 wifidog 启动 http 服务前,注册了一个针对访问路径 /wifidog/auth 的回调,如下:

httpdAddCContent(webserver, "/wifidog", "about", 0, NULL, http_callback_about);  
httpdAddCContent(webserver, "/wifidog", "status", 0, NULL, http_callback_status);  
// 注册了针对 /wifidog/auth 的访问回调 http_callback_auth
httpdAddCContent(webserver, "/wifidog", "auth", 0, NULL, http_callback_auth); 

这样对于 接入设备(or 客户端) 重定向过来的 /wifidog/auth 就进入了 http_callback_auth 函数中,如下:

http_callback_auth(httpd *webserver, request *r)  
{  
    t_client    *client;  
    httpVar * token;  
    char    *mac;  
    // 1, 获取条件参数中的 logout 值
    httpVar *logout = httpdGetVariableByName(r, "logout");  
    // 2, 获取条件参数中的 token 值
    if ((token = httpdGetVariableByName(r, "token"))) {  
        /* They supplied variable "token" */
        // 3, 可以看到, 这里要求必须能够通过 ARP 协议获取到 接入设备 的 MAC 地址
        if (!(mac = arp_get(r->clientAddr))) {  
        /* We could not get their MAC address */
            debug(LOG_ERR, "Failed to retrieve MAC address for ip %s", r->clientAddr);  
            send_http_page(r, "WiFiDog Error", "Failed to retrieve your MAC address");  
        } else {  
            /* We have their MAC address */
            LOCK_CLIENT_LIST();  
            // 4, 检查该客户端(接入设备)是否已经在 wifidog 维护的接入客户端列表中
            if ((client = client_list_find(r->clientAddr, mac)) == NULL) {  
                debug(LOG_DEBUG, "New client for %s", r->clientAddr);  
                client_list_append(r->clientAddr, mac, token->value);  
            } else if (logout) {  
                // 5, 退出处理
                t_authresponse  authresponse;  
                s_config *config = config_get_config();  
                unsigned long long incoming = client->counters.incoming;  
                unsigned long long outgoing = client->counters.outgoing;  
                char *ip = safe_strdup(client->ip);  
                char *urlFragment = NULL;  
                t_auth_serv *auth_server = get_auth_server();  
                fw_deny(client->ip, client->mac, client->fw_connection_state);  
                client_list_delete(client);  
                debug(LOG_DEBUG, "Got logout from %s", client->ip);  
                /* Advertise the logout if we have an auth server */
                if (config->auth_servers != NULL) {  
                    UNLOCK_CLIENT_LIST();  
                    auth_server_request(&authresponse, REQUEST_TYPE_LOGOUT, ip, mac, token->value,  
                                        incoming, outgoing);  
                    LOCK_CLIENT_LIST();  
                    /* Re-direct them to auth server */
                    debug(LOG_INFO, "Got manual logout from client ip %s, mac %s, token %s"
                    "- redirecting them to logout message", client->ip, client->mac, client->token);  
                    safe_asprintf(&urlFragment, "%smessage=%s",  
                        auth_server->authserv_msg_script_path_fragment,  
                        GATEWAY_MESSAGE_ACCOUNT_LOGGED_OUT  
                    );  
                    http_send_redirect_to_auth(r, urlFragment, "Redirect to logout message");  
                    free(urlFragment);  
                }  
                free(ip);  
            }  
            else {  
                // 6, 已经登录校验通过
                debug(LOG_DEBUG, "Client for %s is already in the client list", client->ip);  
            }  
            UNLOCK_CLIENT_LIST();  
            if (!logout) {  
                // 7, 到 auth server 上进一步校验 token
                authenticate_client(r);  
            }  
            free(mac);  
        }  
    } else {  
        /* They did not supply variable "token" */
        // 8, 未携带 token, 直接拒绝
        send_http_page(r, "WiFiDog error", "Invalid token");  
    }  
} 

在该函数中主要处理了 客户端退出,非法校验,以及 客户端校验等流程,下面分别描述注释中的各个步骤:
1,对于客户端退出,则会携带 logout 参数信息,并走到第 5 步(当然,如果连 token 参数都没有的话,会直接走到第 8 步,也就是拒绝);
2,按照正常的认证流程,会携带由认证服务器分配的 token 参数;
3,正如注释说明的,这里要求必须能够通过 ARP 协议获取到 接入设备 的 MAC 地址;(其实通过查看 arg_get 的实现,可以看到是直接解析 /proc/net/arp 文件 -- ARP cache -- 来获取对应客户端 IP 地址的 MAC 信息的),类似如下:
[asd@ubuntu ~]#more /proc/net/arp
IP address HW type Flags HW address Mask Device
192.168.1.203 0x1 0x2 18:03:73:d5:1b:a2 * eth0
192.168.1.1 0x1 0x2 00:21:27:63:c0:ce * eth0
4,在能够获取到该客户端的 MAC 地址后,根据客户端的 IP 和 MAC 地址检查该客户端是否已经在 wifidog 维护的接入设备(or客户端)列表中,如果不在,则追加到此列表中(关于此列表的数据结构在后面再详细描述);
5,如果该客户端已经存在,且本次访问是要求 logout 退出的,则进入此退出处理的流程,该流程主要包括几个步骤:关闭该客户端 ip/mac 的出口(outgoing)规则 --> 从客户端列表中删除该客户端记录 --> 通知认证服务器该客户端退出(且携带该客户端的token, 上下行流量等信息) --> 返回重定向至 认证服务器 的 #define DEFAULT_AUTHSERVMSGPATHFRAGMENT "gw_message.php?" 访问路径(携带一个已退出的 message);
6,如果该客户端已经登录校验过,且本次访问非 logout 退出,则直接跳转到第 7 步;
7,这一步就是 token 校验的过程,具体实现在 authenticate_client 函数中:

/** Authenticates a single client against the central server and returns when done
* Alters the firewall rules depending on what the auth server says
@param r httpd request struct
*/
void
authenticate_client(request *r)
{
    t_client    *client;
    t_authresponse  auth_response;
    char    *mac,
            *token;
    char *urlFragment = NULL;
    s_config    *config = NULL;
    t_auth_serv *auth_server = NULL;

    LOCK_CLIENT_LIST();

    client = client_list_find_by_ip(r->clientAddr);

    if (client == NULL) {
            debug(LOG_ERR, "Could not find client for %s", r->clientAddr);
            UNLOCK_CLIENT_LIST();
            return;
    }

    mac = safe_strdup(client->mac);
    token = safe_strdup(client->token);

    UNLOCK_CLIENT_LIST();

    /* 
     * At this point we've released the lock while we do an HTTP request since it could
     * take multiple seconds to do and the gateway would effectively be frozen if we
     * kept the lock.
     */
    auth_server_request(&auth_response, REQUEST_TYPE_LOGIN, r->clientAddr, mac, token, 0, 0);

    LOCK_CLIENT_LIST();

    /* can't trust the client to still exist after n seconds have passed */
    client = client_list_find(r->clientAddr, mac);

    if (client == NULL) {
            debug(LOG_ERR, "Could not find client node for %s (%s)", r->clientAddr, mac);
            UNLOCK_CLIENT_LIST();
            free(token);
            free(mac);
            return;
    }

    free(token);
    free(mac);

    /* Prepare some variables we'll need below */
    config = config_get_config();
    auth_server = get_auth_server();

    switch(auth_response.authcode) {

    case AUTH_ERROR:
            /* Error talking to central server */
            debug(LOG_ERR, "Got %d from central server authenticating token %s from %s at %s", auth_response, client->token, client->ip, client->mac);
            send_http_page(r, "Error!", "Error: We did not get a valid answer from the central server");
            break;

    case AUTH_DENIED:
            /* Central server said invalid token */
            debug(LOG_INFO, "Got DENIED from central server authenticating token %s from %s at %s - redirecting them to denied message", client->token, client->ip, client->mac);
            safe_asprintf(&urlFragment, "%smessage=%s",
                    auth_server->authserv_msg_script_path_fragment,
                    GATEWAY_MESSAGE_DENIED
    );
            http_send_redirect_to_auth(r, urlFragment, "Redirect to denied message");
            free(urlFragment);
            break;

    case AUTH_VALIDATION:
            /* They just got validated for X minutes to check their email */
            debug(LOG_INFO, "Got VALIDATION from central server authenticating token %s from %s at %s"
                        "- adding to firewall and redirecting them to activate message", client->token,
                            client->ip, client->mac);
            client->fw_connection_state = FW_MARK_PROBATION;
            fw_allow(client->ip, client->mac, FW_MARK_PROBATION);
            safe_asprintf(&urlFragment, "%smessage=%s",
                    auth_server->authserv_msg_script_path_fragment,
                    GATEWAY_MESSAGE_ACTIVATE_ACCOUNT
            );
            http_send_redirect_to_auth(r, urlFragment, "Redirect to activate message");
            free(urlFragment);
            break;

    case AUTH_ALLOWED:
            /* Logged in successfully as a regular account */
            debug(LOG_INFO, "Got ALLOWED from central server authenticating token %s from %s at %s - "
                        "adding to firewall and redirecting them to portal", client->token, client->ip, client->mac);
            client->fw_connection_state = FW_MARK_KNOWN;
            fw_allow(client->ip, client->mac, FW_MARK_KNOWN);
            served_this_session++;
            safe_asprintf(&urlFragment, "%sgw_id=%s",
                    auth_server->authserv_portal_script_path_fragment,
                    config->gw_id
            );
            http_send_redirect_to_auth(r, urlFragment, "Redirect to portal");
            free(urlFragment);
            break;

    case AUTH_VALIDATION_FAILED:
            /* Client had X minutes to validate account by email and didn't = too late */
            debug(LOG_INFO, "Got VALIDATION_FAILED from central server authenticating token %s from %s at %s "
                    "- redirecting them to failed_validation message", client->token, client->ip, client->mac);
            safe_asprintf(&urlFragment, "%smessage=%s",
                    auth_server->authserv_msg_script_path_fragment,
                    GATEWAY_MESSAGE_ACCOUNT_VALIDATION_FAILED
            );
            http_send_redirect_to_auth(r, urlFragment, "Redirect to failed validation message");
            free(urlFragment);
            break;

    default:
            debug(LOG_WARNING, "I don't know what the validation code %d means for token %s from %s at %s - sending error message", auth_response.authcode, client->token, client->ip, client->mac);
            send_http_page(r, "Internal Error", "We can not validate your request at this time");
            break;
    }

    UNLOCK_CLIENT_LIST();
    return;
}

这里主要是两大步骤:
1,通过调用 auth_server_request(&auth_response, REQUEST_TYPE_LOGIN, r->clientAddr, mac, token, 0, 0); 让 认证服务器 对该客户端的 token 进行校验;

2,根据认证服务器返回的 token 校验结果进行不同的处理(主要是对该客户端的防火墙过滤规则进行不同的设置),这里主要以 AUTH_ALLOWED 校验结果进行分析,这里主要是两个动作:

2.1,通过 fw_allow 函数调用对此客户端"放行";

2.2,返回重定向至认证服务器的 portal 路径访问的响应;

这里就简要分析一下 fw_allow 函数的实现,查看fw_allow的实现可以看到真正设置allow客户端通过防火墙的动作是在iptables_fw_access中实现的,如下:

/* Set if a specific client has access through the firewall */
int iptables_fw_access(fw_access_t type, const char *ip, const char *mac, int tag)
{
  int rc;

  fw_quiet = 0;

  switch(type) {
     case FW_ACCESS_ALLOW:
        iptables_do_command("-t mangle -A " TABLE_WIFIDOG_OUTGOING " -s %s -m mac --mac-source %s -j MARK --set-mark %d", ip, mac, tag);
        rc = iptables_do_command("-t mangle -A " TABLE_WIFIDOG_INCOMING " -d %s -j ACCEPT", ip);
        break;
     case FW_ACCESS_DENY:
        iptables_do_command("-t mangle -D " TABLE_WIFIDOG_OUTGOING " -s %s -m mac --mac-source %s -j MARK --set-mark %d", ip, mac, tag);
        rc = iptables_do_command("-t mangle -D " TABLE_WIFIDOG_INCOMING " -d %s -j ACCEPT", ip);
        break;
     default:
        rc = -1;
        break;
     }

     return rc;
}

同样的,我们这里主要分析一下ALLOW时的iptables的防火墙设置规则,对执行的两个iptables命令展开来就是下面两个步骤:

1) 在mangle表中追加WiFiDog_$ID$_Outgoing外出过滤链,该链的规则如下几条:

a) IP 地址为该客户端的IP地址;

b) MAC地址为该客户端的MAC地址;

c) 设置MARK为FW_MARK_KNOWN;

iptables –t mangle –AWiFiDog_$ID$_Outgoing -s 客户端IP地址 -m mac --mac-source 客户端MAC地址 -j MARK --set-markFW_MARK_KNOWN

2)在mangle表中追加一条[接受所有目的地址为此客户端IP地址的] WifiDog_$ID$_Incoming输入过滤链;

iptables -t mangle -AWiFiDog_$ID$_Incoming -d 客户端IP地址 -j ACCEPT

最后,Auth server重定向客户端浏览器到www.baidu.com

本文章由 http://www.wifidog.pro/2014/12/08/wifidog-%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90.html 整理编辑,转载请注明出处

wifidog 源码初分析(二)

上一篇分析了接入设备的首次浏览器访问请求如何通过 防火墙过滤规则 重定向到 wifidog 的 HTTP 服务中,本篇主要分析了 wifidog 在接收到 接入设备的 HTTP 访问请求后,如何将此 HTTP 请求重定向到 认证服务器(auth-server) 上。

通过上面的防火墙规则,会将通过上面的防火墙规则,会将HTTP请求的外部IP地址和端口通过NAT方式重定向至本地wifidog内嵌HTTP服务器的地址和端口上,并由内嵌HTTP服务器进行服务,而内嵌HTTP服务器的路径和回调处理如下:

if ((webserver = httpdCreate(config->gw_address, config->gw_port)) == NULL) {  
    debug(LOG_ERR, "Could not create web server: %s", strerror(errno));  
    exit(1);  
}  
debug(LOG_DEBUG, "Assigning callbacks to web server");  
httpdAddCContent(webserver, "/", "wifidog", 0, NULL, http_callback_wifidog);  
httpdAddCContent(webserver, "/wifidog", "", 0, NULL, http_callback_wifidog);  
httpdAddCContent(webserver, "/wifidog", "about", 0, NULL, http_callback_about);  
httpdAddCContent(webserver, "/wifidog", "status", 0, NULL, http_callback_status);  
httpdAddCContent(webserver, "/wifidog", "auth", 0, NULL, http_callback_auth);  
httpdAddC404Content(webserver, http_callback_404);

客户端首次访问时回调客户端首次访问时回调http_callback_404函数,在该函数中根据获取的客户端信息来配置重定向的URL fragment,如下:

/** The 404 handler is also responsible for redirecting to the auth server */
void http_callback_404(httpd *webserver, request *r)
{
    char        tmp_url[MAX_BUF],
        *url;
    s_config    *config = config_get_config();
    t_auth_serv *auth_server = get_auth_server();

    memset(tmp_url, 0, sizeof(tmp_url));
    /* 
     * XXX Note the code below assumes that the client's request is a plain
     * http request to a standard port. At any rate, this handler is called only
     * if the internet/auth server is down so it's not a huge loss, but still.
     */
    snprintf(tmp_url, (sizeof(tmp_url) - 1), "http://%s%s%s%s",
                    r->request.host,
                    r->request.path,
                    r->request.query[0] ? "?" : "",
                    r->request.query);
    url = httpdUrlEncode(tmp_url);

    if (!is_online()) {
        /* The internet connection is down at the moment  - apologize and do not redirect anywhere */
        char * buf;
        safe_asprintf(&buf, 
            "<p>We apologize, but it seems that the internet connection that powers this hotspot is temporarily unavailable.</p>"
            "<p>If at all possible, please notify the owners of this hotspot that the internet connection is out of service.</p>"
            "<p>The maintainers of this network are aware of this disruption.  We hope that this situation will be resolved soon.</p>"
            "<p>In a while please <a href='%s'>click here</a> to try your request again.</p>", tmp_url);

        send_http_page(r, "Uh oh! Internet access unavailable!", buf);
        free(buf);
        debug(LOG_INFO, "Sent %s an apology since I am not online - no point sending them to auth server", r->clientAddr);
    }
    else if (!is_auth_online()) {
        /* The auth server is down at the moment - apologize and do not redirect anywhere */
        char * buf;
        safe_asprintf(&buf, 
            "<p>We apologize, but it seems that we are currently unable to re-direct you to the login screen.</p>"
            "<p>The maintainers of this network are aware of this disruption.  We hope that this situation will be resolved soon.</p>"
        "<p>In a couple of minutes please <a href='%s'>click here</a> to try your request again.</p>", tmp_url);

        send_http_page(r, "Uh oh! Login screen unavailable!", buf);
        free(buf);
        debug(LOG_INFO, "Sent %s an apology since auth server not online - no point sending them to auth server", r->clientAddr);
    }
    else {
        /* Re-direct them to auth server */
        char *urlFragment;
        safe_asprintf(&urlFragment, "%sgw_address=%s&gw_port=%d&gw_id=%s&url=%s",
            auth_server->authserv_login_script_path_fragment,
            config->gw_address,
            config->gw_port, 
            config->gw_id,
            url);
        debug(LOG_INFO, "Captured %s requesting [%s] and re-directing them to login page", r->clientAddr, url);
        http_send_redirect_to_auth(r, urlFragment, "Redirect to login page");
        free(urlFragment);
    }
    free(url);
}

上面代码基本不用解释,具体重定向至auth server的消息在下面的 http_send_redirect_to_auth 函数中实现:

void http_send_redirect_to_auth(request *r, char *urlFragment, char *text)
{
    char *protocol = NULL;
    int port = 80;
    t_auth_serv *auth_server = get_auth_server();

    if (auth_server->authserv_use_ssl) {
        protocol = "https";
        port = auth_server->authserv_ssl_port;
    } else {
        protocol = "http";
        port = auth_server->authserv_http_port;
    }

    char *url = NULL;
    safe_asprintf(&url, "%s://%s:%d%s%s",
        protocol,
        auth_server->authserv_hostname,
        port,
        auth_server->authserv_path,
        urlFragment
    );
    http_send_redirect(r, url, text);
    free(url);  
}

具体的重定向URL给个实例:
POST /login/?gw_address=192.168.1.1&gw_port=2060&gw_id=default&mac=44:94:fc:ef:28:40&url=http%3A//www.baidu.com/ HTTP/1.1
gw_address,路由器的LAN地址

gw_port:为wifidog的监听端口

gw_id:路由器的标识名

mac:客户端设备的MAC地址

url:为客户端访问的原URL(以便于重定向)

本文章由 http://www.wifidog.pro/2014/12/08/wifidog%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90-1.html 整理编辑,转载请注明出处

wifidog 源码初分析(一)

wifidog 的核心还是依赖于 iptables 防火墙过滤规则来实现的,所以建议对 iptables 有了了解后再去阅读 wifidog 的源码。
在路由器上启动 wifidog 之后,wifidog 在启动时会初始化一堆的防火墙规则,如下:

/** Initialize the firewall rules
*/
int iptables_fw_init(void)
{
    const s_config *config;
    char * ext_interface = NULL;
    int gw_port = 0;
    t_trusted_mac *p;

    fw_quiet = 0;

    LOCK_CONFIG();
    config = config_get_config();
    gw_port = config->gw_port;
    if (config->external_interface) {
            ext_interface = safe_strdup(config->external_interface);
    } else {
            ext_interface = get_ext_iface();
    }

    if (ext_interface == NULL) {
            UNLOCK_CONFIG();
            debug(LOG_ERR, "FATAL: no external interface");
            return 0;
    }
    /*
     *
     * Everything in the MANGLE table
     *
     */

    /* Create new chains */
    iptables_do_command("-t mangle -N " TABLE_WIFIDOG_TRUSTED);
    iptables_do_command("-t mangle -N " TABLE_WIFIDOG_OUTGOING);
    iptables_do_command("-t mangle -N " TABLE_WIFIDOG_INCOMING);

    /* Assign links and rules to these new chains */
    iptables_do_command("-t mangle -I PREROUTING 1 -i %s -j " TABLE_WIFIDOG_OUTGOING, config->gw_interface);
    iptables_do_command("-t mangle -I PREROUTING 1 -i %s -j " TABLE_WIFIDOG_TRUSTED, config->gw_interface);//this rule will be inserted before the prior one
    iptables_do_command("-t mangle -I POSTROUTING 1 -o %s -j " TABLE_WIFIDOG_INCOMING, config->gw_interface);

    for (p = config->trustedmaclist; p != NULL; p = p->next)
            iptables_do_command("-t mangle -A " TABLE_WIFIDOG_TRUSTED " -m mac --mac-source %s -j MARK --set-mark %d", p->mac, FW_MARK_KNOWN);

    /*
     *
     * Everything in the NAT table
     *
     */

    /* Create new chains */
    iptables_do_command("-t nat -N " TABLE_WIFIDOG_OUTGOING);
    iptables_do_command("-t nat -N " TABLE_WIFIDOG_WIFI_TO_ROUTER);
    iptables_do_command("-t nat -N " TABLE_WIFIDOG_WIFI_TO_INTERNET);
    iptables_do_command("-t nat -N " TABLE_WIFIDOG_GLOBAL);
    iptables_do_command("-t nat -N " TABLE_WIFIDOG_UNKNOWN);
    iptables_do_command("-t nat -N " TABLE_WIFIDOG_AUTHSERVERS);

    /* Assign links and rules to these new chains */
    iptables_do_command("-t nat -A PREROUTING -i %s -j " TABLE_WIFIDOG_OUTGOING, config->gw_interface);

    iptables_do_command("-t nat -A " TABLE_WIFIDOG_OUTGOING " -d %s -j " TABLE_WIFIDOG_WIFI_TO_ROUTER, config->gw_address);
    iptables_do_command("-t nat -A " TABLE_WIFIDOG_WIFI_TO_ROUTER " -j ACCEPT");

    iptables_do_command("-t nat -A " TABLE_WIFIDOG_OUTGOING " -j " TABLE_WIFIDOG_WIFI_TO_INTERNET);
    iptables_do_command("-t nat -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m mark --mark 0x%u -j ACCEPT", FW_MARK_KNOWN);
    iptables_do_command("-t nat -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m mark --mark 0x%u -j ACCEPT", FW_MARK_PROBATION);
    iptables_do_command("-t nat -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -j " TABLE_WIFIDOG_UNKNOWN);

    iptables_do_command("-t nat -A " TABLE_WIFIDOG_UNKNOWN " -j " TABLE_WIFIDOG_AUTHSERVERS);
    iptables_do_command("-t nat -A " TABLE_WIFIDOG_UNKNOWN " -j " TABLE_WIFIDOG_GLOBAL);
    iptables_do_command("-t nat -A " TABLE_WIFIDOG_UNKNOWN " -p tcp --dport 80 -j REDIRECT --to-ports %d", gw_port);


    /*
     *
     * Everything in the FILTER table
     *
     */

    /* Create new chains */
    iptables_do_command("-t filter -N " TABLE_WIFIDOG_WIFI_TO_INTERNET);
    iptables_do_command("-t filter -N " TABLE_WIFIDOG_AUTHSERVERS);
    iptables_do_command("-t filter -N " TABLE_WIFIDOG_LOCKED);
    iptables_do_command("-t filter -N " TABLE_WIFIDOG_GLOBAL);
    iptables_do_command("-t filter -N " TABLE_WIFIDOG_VALIDATE);
    iptables_do_command("-t filter -N " TABLE_WIFIDOG_KNOWN);
    iptables_do_command("-t filter -N " TABLE_WIFIDOG_UNKNOWN);

    /* Assign links and rules to these new chains */

    /* Insert at the beginning */
    iptables_do_command("-t filter -I FORWARD -i %s -j " TABLE_WIFIDOG_WIFI_TO_INTERNET, config->gw_interface);


    iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m state --state INVALID -j DROP");

    /* XXX: Why this? it means that connections setup after authentication
       stay open even after the connection is done... 
       iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m state --state RELATED,ESTABLISHED -j ACCEPT");*/

    //Won't this rule NEVER match anyway?!?!? benoitg, 2007-06-23
    //iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -i %s -m state --state NEW -j DROP", ext_interface);

    /* TCPMSS rule for PPPoE */
    iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -o %s -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu", ext_interface);

    iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -j " TABLE_WIFIDOG_AUTHSERVERS);
    iptables_fw_set_authservers();

    iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m mark --mark 0x%u -j " TABLE_WIFIDOG_LOCKED, FW_MARK_LOCKED);
    iptables_load_ruleset("filter", "locked-users", TABLE_WIFIDOG_LOCKED);

    iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -j " TABLE_WIFIDOG_GLOBAL);
    iptables_load_ruleset("filter", "global", TABLE_WIFIDOG_GLOBAL);
    iptables_load_ruleset("nat", "global", TABLE_WIFIDOG_GLOBAL);

    iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m mark --mark 0x%u -j " TABLE_WIFIDOG_VALIDATE, FW_MARK_PROBATION);
    iptables_load_ruleset("filter", "validating-users", TABLE_WIFIDOG_VALIDATE);

    iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -m mark --mark 0x%u -j " TABLE_WIFIDOG_KNOWN, FW_MARK_KNOWN);
    iptables_load_ruleset("filter", "known-users", TABLE_WIFIDOG_KNOWN);

    iptables_do_command("-t filter -A " TABLE_WIFIDOG_WIFI_TO_INTERNET " -j " TABLE_WIFIDOG_UNKNOWN);
    iptables_load_ruleset("filter", "unknown-users", TABLE_WIFIDOG_UNKNOWN);
    iptables_do_command("-t filter -A " TABLE_WIFIDOG_UNKNOWN " -j REJECT --reject-with icmp-port-unreachable");

    UNLOCK_CONFIG();
    return 1;
}

在该 防火墙规则的初始化过程中,会首先清除掉已有的防火墙规则,重新创建新的过滤链,另外,除了通过iptables_do_command("-t nat -A "TABLE_WIFIDOG_UNKNOWN " -p tcp --dport 80 -j REDIRECT --to-ports %d",gw_port); 这个命令将 接入设备的 80 端口(HTTP)的访问重定向至网关自身的 HTTP 的端口之外,还通过iptables_fw_set_authservers(); 函数设置了 鉴权服务器(auth-server) 的防火墙规则:

void iptables_fw_set_authservers(void)
{
    const s_config *config;
    t_auth_serv *auth_server;

    config = config_get_config();

    for (auth_server = config->auth_servers; auth_server != NULL; auth_server = auth_server->next) {
        if (auth_server->last_ip && strcmp(auth_server->last_ip, "0.0.0.0") != 0) {
            iptables_do_command("-t filter -A " TABLE_WIFIDOG_AUTHSERVERS " -d %s -j ACCEPT", auth_server->last_ip);
            iptables_do_command("-t nat -A " TABLE_WIFIDOG_AUTHSERVERS " -d %s -j ACCEPT", auth_server->last_ip);
        }
    }
}

首先从上面的代码可以看出 wifidog 支持多个鉴权服务器,并且针对每一个鉴权服务器设置了如下两条规则:
1)在filter表中追加一条[任何访问鉴权服务器都被接受]的WiFiDog_$ID$AuthServers过滤链:iptables -t filter -A WiFiDog$ID$AuthServers -d auth-server地址 -j ACCEPT
2)在nat表中追加一条[任何访问鉴权服务器都被接受]的WiFiDog
$ID$AuthServers过滤链:iptables -t nat -A WiFiDog$ID$_AuthServers -d auth-server地址 -j ACCEPT
这样确保可以访问鉴权服务器,而不是拒绝所有的出口访问。

本文章由 http://www.wifidog.pro/2014/12/08/wifidog%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90.html整理编辑,转载请注明出处