你好,游客 登录 注册 搜索
背景:
阅读新闻

基于Linux路由的访问控制

[日期:2014-04-29] 来源:Linux社区  作者:nono [字体:大 中 小]

代码

是时候展示一下代码了,为了调试方便,我并没有直接编写内核模块,而是把它们移植到了用户态,需要移植的部分不多,也很简单,就是一个hlist相关的东西以及路由表结构体,首先是头文件route_extra.h:

#ifndef ROUTE_EXTRA_H
#define ROUTE_EXTRA_H

#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>

#define __be32  u_int32_t
typedef unsigned int u32;

struct hlist_node {
        struct hlist_node *next, **pprev;
};

struct hlist_head {
        struct hlist_node *first;
};

struct nf_fn_zone {
        struct nf_fn_zone      *fz_next;
        struct hlist_head      *fz_hash;
        int                    fz_divisor;
        u32                    fz_hashmask;
#define FZ_HASHMASK(fz)        ((fz)->fz_hashmask)
        int                    fz_order;
        __be32                  fz_mask;
#define FZ_MASK(fz)            ((fz)->fz_mask)
};

struct nf_fn_hash {
        struct nf_fn_zone      *nf_fn_zones[33];
        struct nf_fn_zone      *nf_fn_zone_list;
};

struct nf_fib_node;
struct nf_action_node {
        struct list_head        list;
        struct hlist_node      nf_dst;
        struct hlist_node      nf_src;
        struct nf_fib_node      *dst_node;
        struct nf_fib_node      *src_node;
        int extra;
        //int extra_len;
        //char extra[0];
};

struct nf_fib_node {
        struct hlist_node      nf_fn_hash;
        struct hlist_head      *act_list;
        __be32                  fn_key;
        int len;
        unsigned int    hash_value;
        struct nf_action_node *def;
};

static __inline__ __be32 inet_make_mask(int logmask)
{
        if (logmask)
                return htonl(~((1<<(32-logmask))-1));
        return 0;
}

static inline unsigned long ffz(unsigned long word)
{
        asm("bsf %1,%0"
                : "=r" (word)
                : "r" (~word));
        return word;
}

static __inline__ int inet_mask_len(__be32 mask)
{
        uint32_t hmask = ntohl(mask);
        if (!hmask)
                return 0;
        return 32 - ffz(~hmask);
}

extern struct nf_fn_hash *acl_route_table;
// 暂时没有导出任何接口
#endif

然后是C文件,这个C文件中带有一个main函数用于测试,我的测试很简单,本来想再写一个使用pthread的查找测试呢,在听见雨势渐减小了之后,我就再也没有动力了...

#include "route_extra.h"

#define offsetof(TYPE,MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
#define hlist_for_each_entry_safe(tpos, pos, n, head, member)            \
        for (pos = (head)->first;                                        \
                pos && ({ n = pos->next; 1; }) &&                                \
                        ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
                        pos = n)

#define hlist_for_each_entry(tpos, pos, head, member)                    \
        for (pos = (head)->first;                                        \
            pos  && ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
            pos = pos->next)

#define hlist_entry(ptr, type, member) container_of(ptr,type,member)

#define container_of(ptr, type, member) ({                      \
        const typeof(((type *)0)->member) * __mptr = (ptr);    \
        (type *)((char *)__mptr - offsetof(type, member)); })

typedef unsigned int u_int32_t;


static inline void INIT_HLIST_NODE(struct hlist_node *h)
{
        h->next = NULL;
        h->pprev = NULL;
}

static inline void __hlist_del(struct hlist_node *n)
{
        struct hlist_node *next = n->next;
        struct hlist_node **pprev = n->pprev;
        *pprev = next;
        if (next) {                                   
                next->pprev = pprev;
        }
}

static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
{
        struct hlist_node *first = h->first;
        n->next = first;
        if (first)
                first->pprev = &n->next;
        h->first = n;
        n->pprev = &h->first;
}

static inline u_int32_t nf_fz_key(u_int32_t dst, struct nf_fn_zone *fz)
{
        return dst & FZ_MASK(fz);
}

static inline u32 nf_fn_hash(u_int32_t key, struct nf_fn_zone *fz)
{
        u32 h = key>>(32 - fz->fz_order);
        h ^= (h>>20);
        h ^= (h>>10);
        h ^= (h>>5);
        h &= FZ_HASHMASK(fz);
        return h;
}

static struct hlist_head *fz_hash_alloc(int divisor)
{
        unsigned long size = divisor * sizeof(struct hlist_head);
        return calloc(1, size);
}

static struct nf_fn_zone * fn_new_zone(struct nf_fn_hash *table, int z)
{
        int i;
        struct nf_fn_zone *fz = calloc(1, sizeof(struct nf_fn_zone));
        if (!fz)
                return NULL;
        if (z) {
                fz->fz_divisor = 16;
        } else {
                fz->fz_divisor = 1;
        }
        fz->fz_hashmask = (fz->fz_divisor - 1);
        fz->fz_hash = fz_hash_alloc(fz->fz_divisor);
        if (!fz->fz_hash) {
                free(fz);
                return NULL;
        }
        fz->fz_order = z;
        fz->fz_mask = inet_make_mask(z);

        for (i=z+1; i<=32; i++)
                if (table->nf_fn_zones[i])
                        break;
        if (i>32) {
                fz->fz_next = table->nf_fn_zone_list;
                table->nf_fn_zone_list = fz;
        } else {
                fz->fz_next = table->nf_fn_zones[i]->fz_next;
                table->nf_fn_zones[i]->fz_next = fz;
        }
        table->nf_fn_zones[z] = fz;
        return fz;
}

// 路由表操作接口:1.查找;2.删除。参数过于多,类似Win32 API,风格不好,但使用方便
int nf_route_table_opt(struct nf_fn_hash *table,
                        const u_int32_t dst,
                        const u_int32_t mask,
                        int del_option,
                        struct nf_fib_node **resf)
{
        int rv = 1;
        struct nf_fn_zone *fz;
        struct nf_fib_node *del_node = NULL;

        if (NULL == table) {
                printf("INFO:route_table uninitialed!");
                return 1;
        } 
        for (fz = table->nf_fn_zone_list; fz; fz = fz->fz_next) {
                struct hlist_head *head;
                struct hlist_node *node;
                struct nf_fib_node *f;

                u_int32_t k = nf_fz_key(dst, fz);
                head = &fz->fz_hash[nf_fn_hash(k, fz)];
                hlist_for_each_entry(f, node, head, nf_fn_hash) {
                        if (f->fn_key == k){
                                if ( 1 == del_option && mask == FZ_MASK(fz)){
                                        del_node = f;
                                } else if (0 == del_option){
                                        *resf = f;
                                }
                                rv=0;
                                goto out;
                        }
                }
        }
        rv = 1;
out:
        if (del_node) {
                __hlist_del(&del_node->nf_fn_hash);
                free(del_node);
        }
        return rv;
}

static inline void fib_insert_node(struct nf_fn_zone *fz, struct nf_fib_node *f)
{
        struct hlist_head *head = &fz->fz_hash[nf_fn_hash(f->fn_key, fz)];
        hlist_add_head(&f->nf_fn_hash, head);
}

int nf_route_table_search(struct nf_fn_hash *table, u_int32_t dst, struct nf_fib_node **resf)
{
        return  nf_route_table_opt(table, dst, 32, 0, resf);
}

int nf_route_table_delete(struct nf_fn_hash *table, u_int32_t network, u_int32_t mask)
{
        return  nf_route_table_opt(table, network, mask, 1, NULL);
}

int nf_route_table_add(struct nf_fn_hash *table, u_int32_t network, u_int32_t netmask, struct nf_fib_node **node)
{
        struct nf_fib_node *new_f;
        struct nf_fn_zone *fz;
        new_f = calloc(1, sizeof(struct nf_fib_node));
        new_f->len = inet_mask_len(netmask);
        new_f->act_list = fz_hash_alloc(16);
        new_f->fn_key = network;

        if (new_f->len > 32) {
                return -1;
        }

        INIT_HLIST_NODE(&new_f->nf_fn_hash);
        if ( NULL == table){
                return -13;
        }
        fz = table->nf_fn_zones[new_f->len];
        if (!fz && !(fz = fn_new_zone(table,new_f->len))) {
                return -1;
        }
        fib_insert_node(fz, new_f);
        // danger
        *node = new_f;
        return 0;
}

void nf_route_table_clear(struct nf_fn_hash *table)
{
        struct nf_fn_zone *fz,*old;
        if (NULL == table) {
                printf("INFO:route_table was NULL,no need to clear!");
                return;
        } 
        for (fz = table->nf_fn_zone_list; fz; ) {
                if (fz != NULL){
                        free(fz->fz_hash);
                        fz->fz_hash=NULL;
                        old = fz;
                        fz = fz->fz_next;
                        free(old);
                        old=NULL;
                }
        }
        free(table);
        return;
}

int nf_route_policy_search(struct nf_fn_hash *src_table,
                                struct nf_fn_hash *dst_table,
                                uint32_t src_addr,
                                uint32_t dst_addr)
{
        int rv = 0;
        int flag = 0;
        struct nf_fib_node *src_node;
        struct nf_fib_node *dst_node;
        int src_hash = 0;
        int dst_hash = 0;
        struct nf_action_node *action;
        struct hlist_head *head;
        struct hlist_node *node;

        rv = nf_route_table_search(src_table, src_addr, &src_node);
        if (rv) {
                rv = -100;
                goto ret;
        }
        rv = nf_route_table_search(dst_table, dst_addr, &dst_node);
        if (rv) {
                rv = -200;
                goto ret;
        }
      // 注意,由于雨越下越小,我就没有实现着色
        head = &src_node->act_list[dst_node->hash_value];
        hlist_for_each_entry(action, node, head, nf_src) {
                if (action->dst_node == dst_node){
                        rv = action->extra;
                        flag = 1;
                }
        }
        if (flag == 0) {
                rv = src_node->def->extra;
        }

ret:
        return rv;
}

int nf_route_policy_add(struct nf_fn_hash *src_table,
                        struct nf_fn_hash *dst_table,
                        uint32_t src_network,
                        uint32_t dst_network,
                        uint32_t src_netmask,
                        uint32_t dst_netmask,
                        struct nf_action_node *action,
                        struct nf_action_node *def)
{
        int rv = 0;
        struct nf_fib_node *src_node;
        struct nf_fib_node *dst_node;
        struct hlist_head *src_head;
        struct hlist_head *dst_head;
        rv = nf_route_table_add(src_table, src_network, src_netmask, &src_node);
        if (rv) {
                rv = 1;
                goto ret;
        }
        src_node->def = def;
        // 注意hash值的计算
        src_node->hash_value = ((unsigned int)src_node)%16;

        rv = nf_route_table_add(dst_table, dst_network, dst_netmask, &dst_node);
        if (rv) {
                rv = 1;
                goto ret;
        }
        dst_node->def = def;
        dst_node->hash_value = ((unsigned int)dst_node)%16;

        action->src_node = src_node;
        action->dst_node = dst_node;

        INIT_HLIST_NODE(&action->nf_src);
        INIT_HLIST_NODE(&action->nf_dst);

        src_head = &src_node->act_list[dst_node->hash_value];
        hlist_add_head(&action->nf_src, src_head);
        dst_head = &src_node->act_list[src_node->hash_value];
        hlist_add_head(&action->nf_dst, dst_head);

ret:
        return rv;
}

int main(int argc, char **argv)
{
        int rv = 0;
        struct nf_action_node *def, *act1, *act2;
        struct nf_fn_hash *src_table;
        struct nf_fn_hash *dst_table;
        struct nf_fib_node *src_node = NULL;
        struct nf_fib_node *dst_node = NULL;
        src_table = calloc(1, sizeof(struct nf_fn_hash));
        dst_table = calloc(1, sizeof(struct nf_fn_hash));
        /* 添加默认路由项 */
        def = calloc(1, sizeof(struct nf_action_node));
        def->extra = 10000;

        act1 = calloc(1, sizeof(struct nf_action_node));
        act1->extra = 1;
        act2 = calloc(1, sizeof(struct nf_action_node));
        act2->extra = 2;
        rv = nf_route_policy_add(src_table, dst_table, 0x00000000, 0x00000000, 0x00000000, 0x00000000, act1, def);

        rv = nf_route_policy_add(src_table, dst_table, 0x0000a8c0, 0x00001010, 0x0000ffff, 0x0000ffff, act1, def);
        rv = nf_route_policy_search(src_table, dst_table, 0x0102a9c0, 0x01021010);
        printf("%d\n", rv);

        return 0;
}

以上的代码中对于extra的定义太随意,本来想进一步扩展呢?可是雨越来越小了,对于我来讲,不下暴雨的每一天都是煎熬。爱探险的朵拉,JI JI JI JI朵拉...捣蛋鬼别捣蛋!

并行执行的含义

而不是意义!并行执行是我这个设计的最大特色,在多核环境下,你要知道的是,虽然整个匹配过程分3步走,但是其实只有一步,两次路由查找是可以同时进行的,你会看到,以上的描述或者代码几乎都是重复性的,也就是同一个逻辑写两遍,全都是对称的,但是要知道,光这是不够的,更多的工作在于你如何把这些对称的操作塞进两个执行绪而不引入得不偿失的开销,就像叔本华理解的那样。最大的开销就是同步开销。

世界就是这样,生活也是这样,你怀着激动的心情引入了复杂,有时得到的却没有你付出的多,而正是这份激动让你继续前行,直到再也负担不了...鉴于此,我不得不说一下balance的艺术。

balance的思想和艺术

实际上,整个世界是balance构成的,中庸之道?好像也不是,否则世界就不会前行了。对于文明和文化,真正的balance在于崩塌和重建之间的balance,设计方面的balance设计与此不同,实用的技术和唯美欣赏艺术都是静态的,对于技术的美,在于跷跷板两边的重量在不断的变化,你必须改变支点保持balance,从早期的终端时代到现在的后终端时代,你会看到这种balance。对于像绘画,音乐这样的艺术,balance恰恰在崩塌的恰到好处,反差在崩塌中体现,最终趋于一点,过程中释放的能量触动你的神经。

linux
相关资讯       Linux路由 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款