Chinaunix首页 | 论坛 | 博客
  • 博客访问: 74564
  • 博文数量: 8
  • 博客积分: 171
  • 博客等级: 入伍新兵
  • 技术积分: 165
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-30 11:32
文章分类

全部博文(8)

文章存档

2012年(3)

2011年(5)

我的朋友

分类: LINUX

2011-10-13 10:51:32

1.  注册一个伪设备
    static const struct net_device_ops ipip_netdev_ops = {
        .ndo_uninit     = ipip_tunnel_uninit,
        .ndo_start_xmit = ipip_tunnel_xmit,
        .ndo_do_ioctl   = ipip_tunnel_ioctl,
        .ndo_change_mtu = ipip_tunnel_change_mtu,
        
}; 
tunnel 的peer两端ip地址都保存在ip_tunnel->param中(ip_tunnel_param)

2. 发包
ipip_tunnel_xmit:
1)tunnel之后的包不能再路由到本tunnel
2) 可能会重新分配skb
3) 直接在原来ip头前面加一个ip头,恒为20字节。挪动network_header和transport_header。
4)做完之后call ip_local_out,之前会nf_reset。表示会过两次ip协议栈,过两次netfilter chain

3.收包
ip_rcv -> ip_local_deliver_finish中会找L4层handler,处理rcv
对于ipip,走到 ipip_rcv (注册在xfrm_tunnel中的ipip_handler),ipip_init中会注册
1. 挪动network_header和transport_header把开头20个字节偏掉,ipip头设为mac_header,把skb->dev指向tunnel->dev
2. nf_reset。skb->protocol(L2协议)设为ETH_P_IP 
3. netif_rx->入backlog, 注册软中断NET_RX->net_rx_action->process_backlog
  ->__netif_receive_skb->ip_rcv, 重走ip协议栈,重走所有netfilter chain

4. ip tunnel 注册, 走的ioctl
 新建一个ipip tunnel. 可给定local ip. remote ip. dev
譬如ip tunnel add tunnel mode ipip  remote xx local yy dev zz
其中remote, local, dev都可不设置,类似ipip自己初始化时创建的设备tunnel0

根据既不设置remote和local, 只设置remote,只设置local,同时设置remote和local分了4个hash表。
struct ipip_net {
        struct ip_tunnel *tunnels_r_l[HASH_SIZE];
        struct ip_tunnel *tunnels_r[HASH_SIZE];
        struct ip_tunnel *tunnels_l[HASH_SIZE];
        struct ip_tunnel *tunnels_wc[1];
        struct ip_tunnel **tunnels[4];
        
        struct net_device *fb_tunnel_dev;
};   
用户ip tunnel设定的选项保存在ip_tunnel.param中,即ip_tunnel_param
指定的dev设备作为路由表查找的指定出口设备,类似socket的bind_output_dev。如果不指定,就是让
路由抉择选择哪个出口出去。   
阅读(5830) | 评论(0) | 转发(0) |
0

上一篇:linux bridge实现(1)

下一篇:记录点东西

给主人留下些什么吧!~~