RAW实现

struct proto raw_prot = {
 .name     = "RAW",
 .owner     = THIS_MODULE,
 .close     = raw_close,
 .destroy    = raw_destroy,
 .connect    = ip4_datagram_connect,
 .disconnect    = udp_disconnect,
 .ioctl     = raw_ioctl,
 .init     = raw_init,
 .setsockopt    = raw_setsockopt,
 .getsockopt    = raw_getsockopt,
 .sendmsg    = raw_sendmsg,
 .recvmsg    = raw_recvmsg,
 .bind     = raw_bind,
 .backlog_rcv    = raw_rcv_skb,
 .release_cb    = ip4_datagram_release_cb,
 .hash     = raw_hash_sk,
 .unhash     = raw_unhash_sk,
 .obj_size    = sizeof(struct raw_sock),
 .h.raw_hash    = &raw_v4_hashinfo,
#ifdef CONFIG_COMPAT
 .compat_setsockopt = compat_raw_setsockopt,
 .compat_getsockopt = compat_raw_getsockopt,
 .compat_ioctl    = compat_raw_ioctl,
#endif
};

       {
        .type =       SOCK_RAW,
        .protocol =   IPPROTO_IP, /* wild card */
        .prot =       &raw_prot,
        .ops =        &inet_sockraw_ops,
        .flags =      INET_PROTOSW_REUSE,
       }

 

 rc = proto_register(&raw_prot, 1);
 if (rc)
  goto out_unregister_udp_proto;

 

 

数据接收:


static int ip_local_deliver_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
{
 __skb_pull(skb, skb_network_header_len(skb));

 rcu_read_lock();
 {
  int protocol = ip_hdr(skb)->protocol;
  const struct net_protocol *ipprot;
  int raw;

 resubmit:
  raw = raw_local_deliver(skb, protocol);

  ipprot = rcu_dereference(inet_protos[protocol]);
  if (ipprot) {
   int ret;

   if (!ipprot->no_policy) {
    if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
     kfree_skb(skb);
     goto out;
    }
    nf_reset(skb);
   }
   ret = ipprot->handler(skb);//对于RAW协议来说,原始套接字并无L4协议,它是直接通过上面raw_local_deliver()发送。
   if (ret < 0) {
    protocol = -ret;
    goto resubmit;
   }
   __IP_INC_STATS(net, IPSTATS_MIB_INDELIVERS);
  } else {
   if (!raw) {
    if (xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
     __IP_INC_STATS(net, IPSTATS_MIB_INUNKNOWNPROTOS);
     icmp_send(skb, ICMP_DEST_UNREACH,
        ICMP_PROT_UNREACH, 0);
    }
    kfree_skb(skb);
   } else {
    __IP_INC_STATS(net, IPSTATS_MIB_INDELIVERS);
    consume_skb(skb);
   }
  }
 }
 out:
 rcu_read_unlock();

 return 0;
}

 

 

 

int raw_local_deliver(struct sk_buff *skb, int protocol)
{
 int hash;
 struct sock *raw_sk;

 hash = protocol & (RAW_HTABLE_SIZE - 1);
 raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);

 /* If there maybe a raw socket we must check - if not we
  * don't care less
  */
 if (raw_sk && !raw_v4_input(skb, ip_hdr(skb), hash))
  raw_sk = NULL;

 return raw_sk != NULL;

}

 

 

 


/* IP input processing comes here for RAW socket delivery.
 * Caller owns SKB, so we must make clones.
 *
 * RFC 1122: SHOULD pass TOS value up to the transport layer.
 * -> It does. And not only TOS, but all IP header.
 */
static int raw_v4_input(struct sk_buff *skb, const struct iphdr *iph, int hash)
{
 struct sock *sk;
 struct hlist_head *head;
 int delivered = 0;
 struct net *net;

 read_lock(&raw_v4_hashinfo.lock);
 head = &raw_v4_hashinfo.ht[hash];
 if (hlist_empty(head))
  goto out;

 net = dev_net(skb->dev);
 sk = __raw_v4_lookup(net, __sk_head(head), iph->protocol,
        iph->saddr, iph->daddr,
        skb->dev->ifindex);

 while (sk) {
  delivered = 1;
  if ((iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) &&
      ip_mc_sf_allow(sk, iph->daddr, iph->saddr,
       skb->dev->ifindex)) {
   struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);

   /* Not releasing hash table! */
   if (clone)
    raw_rcv(sk, clone);
  }
  sk = __raw_v4_lookup(net, sk_next(sk), iph->protocol,
         iph->saddr, iph->daddr,
         skb->dev->ifindex);
 }
out:
 read_unlock(&raw_v4_hashinfo.lock);
 return delivered;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值