2013/01/28

Linux 的 contrack (使用 kernel 3.2)


一樣看 ipv4 的部份,hook 資訊在 ~/net/ipv4/netfilter/nf_contrack_l3proto_ipv4.c
static struct nf_hook_ops ipv4_conntrack_ops[] __read_mostly = {
{
    .hookipv4_conntrack_in,
    .owner= THIS_MODULE,
    .pf= NFPROTO_IPV4,
    .hooknum= NF_INET_PRE_ROUTING,    PRE_ROUTING 會呼叫 ipv4_conntrack_in()
    .priority= NF_IP_PRI_CONNTRACK,
},{

    NF_INET_LOCAL_OUT 對應 ipv4_conntrack_local
},{
    NF_INET_POST_ROUTING 對應 ipv4_confirm
},{
    NF_INET_LOCAL_IN 對應 ipv4_confirm
}
ipv4_conntrack_in() {
    return nf_conntrack_in();    ~/net/netfilter/nf_conntrack_core.c
}
nf_conntrack_in() {
    if (skb->nfct) {    封包的 conntrack 存在表示這個封包是 loopbak 或是 (untarcked?),直接略過
    }
    l3proto = __nf_ct_l3proto_find();    ipv4 傳入 PF_INET,ipv6 傳入 PF_INET6,這樣作以後就可以支援更多的第三層協議
    ret = l3proto->get_l4proto();    取得第四層協議編號
    if (ret <= 0) {}    不認識的第四層協議就回報錯誤
    l4proto = __nf_ct_l4proto_find();    l4 協議實作程式在 ~/net/netfilter/nf_conntrack_proto_XXX.c
        例如:nf_conntrack_proto_tcp.c
        其 ipv4 的相關資料定義在 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp4
        其 ipv6 的相關資料定義在 struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp6
    if (l4proto->error != NULL) {    有宣告說要檢查錯誤的協議,->error 指向錯誤檢查 function
        ret = l4proto->error();    檢查封包是不是正常
        if (ret <= 0) {}    檢查出錯誤就回報錯誤
    }
    ct = resolve_normal_ct();    依據 l3 跟 l4 的協議資料獲取 conntrack
    ret = l4proto->packet();    進行 l4 的 packet
    if (set_reply && !test_and_set_bit(IPS_SEEN_REPLY_BIT, &ct->status))
        nf_conntrack_event_cache(IPCT_REPLY, ct);    設置 contrack event
}
resolve_normal_ct() {
    if (!nf_ct_get_tuple() {}    先把 tuple 找出來    hash = hash_conntrack_raw()    這兩行再把 hash 找出來
    h = __nf_conntrack_find_get()
    if (!h) {
        h = init_conntrack()    如果沒有找到 hash 就建立一個新的 hash
    }
    ct = nf_ct_tuplehash_to_ctrack(h);    把 hash 轉成 conntrack
    if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) {
        方向是 Rep
    } else {
        方向是 Org,依據 ct->status 區分成三種狀態,ESTABLISHED、RELATED、NEW
    }    skb->nfct = &ct->ct_general;    這兩行將 ct 訊息回填 skb
    skb->nfctinfo = *ctinfo;
}
ipv4_conntrack_local() {    /* root is playing with raw sockets. */
    如果封包長度不符合 ip 的規定,那一定是 root 作 raw sockets 產生的
    return nf_conntrack_in()    呼叫 nf_conntrack_in
}
ipv4_confirm() {    ct = nf_ct_get(skb, &ctinfo);    取得 conntrack
    help = nfct_help(ct);
}

Linux 的 NAT (使用 kernel 3.2)


init: 以 ipv4 為例,初始化程式放置在 ~/net/ipv4/netfilter/nf_nat_rule.c
int __init nf_nat_rule_init(void)
      ret = xt_register_target(&ipt_snat_reg);    註冊 snat 的 target
      ret = xt_register_target(&ipt_dnat_reg);    註冊 dnat 的 target
static struct xt_target ipt_snat_reg __read_mostly = {
    .name= "SNAT",
    .targetipt_snat_target,
    .targetsize= sizeof(struct nf_nat_multi_range_compat),
    .table= "nat",        table: nat,iptables 裡面用 -t nat 來使用
    
.hooks= (1 << NF_INET_POST_ROUTING) | (1 << NF_INET_LOCAL_IN),    只用到兩個 hook
    .checkentry= ipt_snat_checkentry,    指定規則檢查函數,同樣實做在 nf_nat_rule.c
    .family= AF_INET,
}    dnat 只有 .hooks 跟 .checkentry 不同
ipt_snat_target() {
    ct = nf_ct_get(skb, &ctinfo);    取得 conntrack 訊息
    /* Connection must be valid and new. */    作驗證
    return nf_nat_setup_info(ct, &mr->range[0], IP_NAT_MANIP_SRC);
}
nf_nat_setup_info() {    實做在 ~/net/ipv4/netfilter/nf_nat_core.c
    struct nf_conntrack_tuple ...;    定義在 ~/include/net/netfilter/nf_conntrack_tuple.h,用來作連線識別
    nat = nfct_nat(ct);    從 conntrack 取出 nat 資訊
    if (!nat) {    如果沒有,就新增一個
        nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
    }
    nf_ct_invert_tuplepr(&curr_tuple, ...)    根據 reply 逆推當前的 tuple
    get_unique_tuple(&new_tuple, ...)    獲取作過 snat 之後的 tuple
    if (!nf_ct_tuple_equal(&new_tuple, &curr_tuple)) {
        兩者不相等表示 tuple 發生變化了,要更新 conntrack 裡面 reply 的 tuple
    }
    if (maniptype == IP_NAT_MANIP_SRC) {
        第一次作 NAT 必須將 ORIGINAL 的 tuple 加入 hash 列表
    }
    /* It's done. */    打上 NAT 完成標記
}

2013/01/17

get_vlan_id()


unsigned short get_vlan_id(struct sk_buff *skb)
{
    struct vlan_hdr *hdr;
    unsigned short tci;               
    unsigned short id = 0;
     
    if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
        hdr = (struct vlan_hdr *)(skb->data);
        tci = hdr->h_vlan_TCI;
        id = (tci & VLAN_VID_MASK) >> 8;
    }
     
    return id;
}

/* This function will return Vlan id of a skb. */