Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2776495
  • 博文数量: 505
  • 博客积分: 1552
  • 博客等级: 上尉
  • 技术积分: 2514
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-23 18:24
文章分类

全部博文(505)

文章存档

2019年(12)

2018年(15)

2017年(1)

2016年(17)

2015年(14)

2014年(93)

2013年(233)

2012年(108)

2011年(1)

2009年(11)

分类: LINUX

2013-05-05 10:21:44

做过P2P通信的人应该都会用到这个技术,udp打洞。通过udp打洞,可以实现两个nat后的主机进行直接通信。
wiki中关于udp打洞的描述:

通过UDP打洞实现NAT穿越是一种在处于使用了NAT的私有网络中的Internet主机之间建立双向UDP连接的方法。由于NAT的行为是非标准化的,因此它并不能应用于所有类型的NAT。

其基本思想是这样的:让位于NAT后的两台主机都与处于公共地址空间的、众所周知的第三台服务器相连,然后,一旦NAT设备建立好UDP状态信息就转为直接通信,并寄希望于NAT设备会在分组其实是从另外一个主机传送过来的情况下仍然保持当前状态。

这项技术需要一个圆锥型NAT设备才能够正常工作。对称型NAT不能使用这项技术。

这项技术在P2P软件和VoIP电话领域被广泛采用。它是Skype用以绕过防火墙和NAT设备的技术之一。

相同的技术有时还被用于TCP连接——尽管远没有UDP成功。


UDP打洞的过程大体上如下:

主机A和主机B都是通过NAT设备访问互联网,主机S位于互联网上。

1. A和B都与S之间通过UDP进行心跳连接

2. A通知S,要与B通信

3. S把B的公网IP、port告诉A,同时把A的公网IP、port告诉B

4. A向B的公网IP、port发送数据(这个数据包应该会被丢弃,但是打开了B回来的窗户)

5. B向A的公网IP、port发送数据(这个数据包就会被A接受,之后A和B就建立起了连接)


上述能够正常工作的前提的,A连接S和连接B的时候,NAT设备对A做地址转换的时候,需要选择相同的IP、port。

比如:A--->NATA--->S

      A--->NATA--->NATB-->B

那么NATA对A进行NAT的时候,需要选择相同的IP和port进行SNAT。

如果使用Linux作为防火墙,那么非常幸运,Linux就是这么搞的

现在我们来看看linux是如何实现的:

linux通过SNAT netfilter target 进行源地址转换,源码位于net/ipv4/netfilter/nf_nat_rule.c

点击(此处)折叠或打开

  1. /* Source NAT */
  2. static unsigned int
  3. ipt_snat_target(struct sk_buff *skb, const struct xt_target_param *par)
  4. {
  5.     struct nf_conn *ct;
  6.     enum ip_conntrack_info ctinfo;
  7.     const struct nf_nat_multi_range_compat *mr = par->targinfo;

  8.     NF_CT_ASSERT(par->hooknum == NF_INET_POST_ROUTING);

  9.     ct = nf_ct_get(skb, &ctinfo);

  10.     /* Connection must be valid and new. */
  11.     NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED ||
  12.              ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY));
  13.     NF_CT_ASSERT(par->out != NULL);

  14.     return nf_nat_setup_info(ct, &mr->range[0], IP_NAT_MANIP_SRC);
  15. }
这里实际上调用函数nf_nat_setup_info(net/ipv4/netfilter/nf_nat_core.c)

点击(此处)折叠或打开

  1. unsigned int
  2. nf_nat_setup_info(struct nf_conn *ct,
  3.          const struct nf_nat_range *range,
  4.          enum nf_nat_manip_type maniptype)
  5. {
  6.     struct net *net = nf_ct_net(ct);
  7.     struct nf_conntrack_tuple curr_tuple, new_tuple;
  8.     struct nf_conn_nat *nat;
  9.     int have_to_hash = !(ct->status & IPS_NAT_DONE_MASK);

  10.     /* nat helper or nfctnetlink also setup binding */
  11.     nat = nfct_nat(ct);
  12.     if (!nat) {
  13.         nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
  14.         if (nat == NULL) {
  15.             pr_debug("failed to add NAT extension\n");
  16.             return NF_ACCEPT;
  17.         }
  18.     }

  19.     NF_CT_ASSERT(maniptype == IP_NAT_MANIP_SRC ||
  20.          maniptype == IP_NAT_MANIP_DST);
  21.     BUG_ON(nf_nat_initialized(ct, maniptype));

  22.     /* What we've got will look like inverse of reply. Normally
  23.      this is what is in the conntrack, except for prior
  24.      manipulations (future optimization: if num_manips == 0,
  25.      orig_tp =
  26.      conntrack->tuplehash[IP_CT_DIR_ORIGINAL].tuple) */
  27.     nf_ct_invert_tuplepr(&curr_tuple,
  28.              &ct->tuplehash[IP_CT_DIR_REPLY].tuple);

  29.     get_unique_tuple(&new_tuple, &curr_tuple, range, ct, maniptype);

  30.     if (!nf_ct_tuple_equal(&new_tuple, &curr_tuple)) {
  31.         struct nf_conntrack_tuple reply;

  32.         /* Alter conntrack table so will recognize replies. */
  33.         nf_ct_invert_tuplepr(&reply, &new_tuple);
  34.         nf_conntrack_alter_reply(ct, &reply);

  35.         /* Non-atomic: we own this at the moment. */
  36.         if (maniptype == IP_NAT_MANIP_SRC)
  37.             ct->status |= IPS_SRC_NAT;
  38.         else
  39.             ct->status |= IPS_DST_NAT;
  40.     }

  41.     /* Place in source hash if this is the first time. */
  42.     if (have_to_hash) {
  43.         unsigned int srchash;

  44.         srchash = hash_by_src(net, &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
  45.         spin_lock_bh(&nf_nat_lock);
  46.         /* nf_conntrack_alter_reply might re-allocate exntension aera */
  47.         nat = nfct_nat(ct);
  48.         nat->ct = ct;
  49.         hlist_add_head_rcu(&nat->bysource,
  50.                  &net->ipv4.nat_bysource[srchash]);
  51.         spin_unlock_bh(&nf_nat_lock);
  52.     }

  53.     /* It's done. */
  54.     if (maniptype == IP_NAT_MANIP_DST)
  55.         set_bit(IPS_DST_NAT_DONE_BIT, &ct->status);
  56.     else
  57.         set_bit(IPS_SRC_NAT_DONE_BIT, &ct->status);

  58.     return NF_ACCEPT;
  59. }
这里第33行,进行snat的端口和ip,核心就在这个函数里面

点击(此处)折叠或打开

  1. static void
  2. get_unique_tuple(struct nf_conntrack_tuple *tuple,
  3.          const struct nf_conntrack_tuple *orig_tuple,
  4.          const struct nf_nat_range *range,
  5.          struct nf_conn *ct,
  6.          enum nf_nat_manip_type maniptype)
  7. {
  8.     struct net *net = nf_ct_net(ct);
  9.     const struct nf_nat_protocol *proto;

  10.     /* 1) If this srcip/proto/src-proto-part is currently mapped,
  11.      and that same mapping gives a unique tuple within the given
  12.      range, use that.

  13.      This is only required for source (ie. NAT/masq) mappings.
  14.      So far, we don't do local source mappings, so multiple
  15.      manips not an issue. */
  16.     if (maniptype == IP_NAT_MANIP_SRC &&
  17.      !(range->flags & IP_NAT_RANGE_PROTO_RANDOM)) {
  18.         if (find_appropriate_src(net, orig_tuple, tuple, range)) {
  19.             pr_debug("get_unique_tuple: Found current src map\n");
  20.             if (!nf_nat_used_tuple(tuple, ct))
  21.                 return;
  22.         }
  23.     }

  24.     /* 2) Select the least-used IP/proto combination in the given
  25.      range. */
  26.     *tuple = *orig_tuple;
  27.     find_best_ips_proto(tuple, range, ct, maniptype);

  28.     /* 3) The per-protocol part of the manip is made to map into
  29.      the range to make a unique tuple. */

  30.     rcu_read_lock();
  31.     proto = __nf_nat_proto_find(orig_tuple->dst.protonum);

  32.     /* Change protocol info to have some randomization */
  33.     if (range->flags & IP_NAT_RANGE_PROTO_RANDOM) {
  34.         proto->unique_tuple(tuple, range, maniptype, ct);
  35.         goto out;
  36.     }

  37.     /* Only bother mapping if it's not already in range and unique */
  38.     if ((!(range->flags & IP_NAT_RANGE_PROTO_SPECIFIED) ||
  39.      proto->in_range(tuple, maniptype, &range->min, &range->max)) &&
  40.      !nf_nat_used_tuple(tuple, ct))
  41.         goto out;

  42.     /* Last change: get protocol to try to obtain unique tuple. */
  43.     proto->unique_tuple(tuple, range, maniptype, ct);
  44. out:
  45.     rcu_read_unlock();
  46. }
这个函数的第20行,就是一个选择的原则。如果这个port、ip、protocol已经被snat过,那么这次snat优先选择之前使用的ip和port

点击(此处)折叠或打开

  1. /* Only called for SRC manip */
  2. static int
  3. find_appropriate_src(struct net *net,
  4.          const struct nf_conntrack_tuple *tuple,
  5.          struct nf_conntrack_tuple *result,
  6.          const struct nf_nat_range *range)
  7. {
  8.     unsigned int h = hash_by_src(net, tuple);
  9.     const struct nf_conn_nat *nat;
  10.     const struct nf_conn *ct;
  11.     const struct hlist_node *n;

  12.     rcu_read_lock();
  13.     hlist_for_each_entry_rcu(nat, n, &net->ipv4.nat_bysource[h], bysource) {
  14.         ct = nat->ct;
  15.         if (same_src(ct, tuple)) {
  16.             /* Copy source part from reply tuple. */
  17.             nf_ct_invert_tuplepr(result,
  18.                  &ct->tuplehash[IP_CT_DIR_REPLY].tuple);
  19.             result->dst = tuple->dst;

  20.             if (in_range(result, range)) {
  21.                 rcu_read_unlock();
  22.                 return 1;
  23.             }
  24.         }
  25.     }
  26.     rcu_read_unlock();
  27.     return 0;
  28. }


阅读(760) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~