我正在尝试制作IGMPV2成员资格请求数据包,并将其发送到RAW插座上。

RFC 3376指出:

IGMP消息封装在IPv4数据报中,IP协议编号为2。本文档中描述的每个IGMP消息均以1个IP-live为1,Intoctwork控制的IP优先级(例如,Service 0xc0的类型),并在其IP标头中带有IP路由器警报选项[RFC-2113

因此必须设置IP_ROUTER_ALERT标志。

我正在尝试制造数据包(例如仅IGMP标头和有效载荷)的严格必要,因此我使用SetSockopt来编辑IP选项。

一些有用的变量:

#define C_IP_MULTICAST_TTL 1
#define C_IP_ROUTER_ALERT 1

int sockfd = 0;
int ecsockopt = 0;
int bytes_num = 0;

int ip_multicast_ttl = C_IP_MULTICAST_TTL;
int ip_router_alert = C_IP_ROUTER_ALERT;

这是我打开原始插座的方式:

sock_domain = AF_INET;
sock_type = SOCK_RAW;
sock_proto = IPPROTO_IGMP;

if ((ecsockopt = socket(sock_domain,sock_type,sock_proto)) < 0) {
  printf("Error %d: Can't open socket.\n", errno);
  return 1;
} else {
  printf("** Socket opened.\n");
}
sockfd = ecsockopt;

然后,我设置了TTL和路由器警报选项:

// Set the sent packets TTL
if((ecsockopt = setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &ip_multicast_ttl, sizeof(ip_multicast_ttl))) < 0) {
  printf("Error %d: Can't set TTL.\n", ecsockopt);
  return 1;
} else {
  printf("** TTL set.\n");
}

// Set the Router Alert
if((ecsockopt = setsockopt(sockfd, IPPROTO_IP, IP_ROUTER_ALERT, &ip_router_alert, sizeof(ip_router_alert))) < 0) {
  printf("Error %d: Can't set Router Alert.\n", ecsockopt);
  return 1;
} else {
  printf("** Router Alert set.\n");
}

IP_ROUTER_ALERT的SETSOCKOPT返回0。锻造数据包后,我以这种方式发送它:

// Send the packet
if((bytes_num = sendto(sockfd, packet, packet_size, 0, (struct sockaddr*) &mgroup1_addr, sizeof(mgroup1_addr))) < 0) {
  printf("Error %d: Can't send Membership report message.\n", bytes_num);
  return 1;
} else {
  printf("** Membership report message sent. (bytes=%d)\n",bytes_num);
}

发送了数据包,但是缺少IP_ROUTER_ALERT选项(用Wireshark检查)。难道我做错了什么?还有其他一些方法可以设置IP_ROUTER_ALERT选项吗?

提前致谢。

有帮助吗?

解决方案

最后我发现 IP_ROUTER_ALERT 必须由Linux内核设置。 IGMP会员资格请求是在 IP_ADD_MEMBERSHIP 完成,内核负责设置 IP_ROUTER_ALERT 旗帜。

其他提示

我不知道为什么您的代码不起作用(对我来说看起来不错),但是我可以建议解决方法:在您的原始套筒上再放下一层并构建以太网框架。您可能还想看看 libnet, ,它为您处理这样的建筑包。

文档 状态:

将所有转发的数据包传递,并将IP路由器警报选项设置为此套接字。仅适用于原始插座。例如,对于用户空间RSVP守护程序很有用。敲击数据包没有由内核转发,用户有责任再次发送它们。插座绑定被忽略,此类数据包仅通过协议过滤。期望一个整数标志。

这听起来好像选项仅在 接收 插座上的数据包,而不是发送它们时。如果您要发送原始数据包,您不能只是 设置所需选项 您自己在IP标题中?

作为参考,我会建议其中的许多IGMP Aware程序之一。一个例子是 igmpproxy: https://github.com/vitoni/igmpproxy/blob/logging/src/igmp.c#l54

/*
 * Open and initialize the igmp socket, and fill in the non-changing
 * IP header fields in the output packet buffer.
 */
void initIgmp(void) {
    struct ip *ip;

    recv_buf = malloc(RECV_BUF_SIZE);
    send_buf = malloc(RECV_BUF_SIZE);

    k_hdr_include(true);    /* include IP header when sending */
    k_set_rcvbuf(256*1024,48*1024); /* lots of input buffering        */
    k_set_ttl(1);       /* restrict multicasts to one hop */
    k_set_loop(false);      /* disable multicast loopback     */

    ip         = (struct ip *)send_buf;
    memset(ip, 0, sizeof(struct ip));
    /*
     * Fields zeroed that aren't filled in later:
     * - IP ID (let the kernel fill it in)
     * - Offset (we don't send fragments)
     * - Checksum (let the kernel fill it in)
     */
    ip->ip_v   = IPVERSION;
    ip->ip_hl  = (sizeof(struct ip) + 4) >> 2; /* +4 for Router Alert option */
    ip->ip_tos = 0xc0;      /* Internet Control */
    ip->ip_ttl = MAXTTL;    /* applies to unicasts only */
    ip->ip_p   = IPPROTO_IGMP;

    allhosts_group   = htonl(INADDR_ALLHOSTS_GROUP);
    allrouters_group = htonl(INADDR_ALLRTRS_GROUP);
    alligmp3_group   = htonl(INADDR_ALLIGMPV3_GROUP);
}

https://github.com/vitoni/igmpproxy/blob/logging/src/igmp.c#l271

/*
 * Construct an IGMP message in the output packet buffer.  The caller may
 * have already placed data in that buffer, of length 'datalen'.
 */
static void buildIgmp(uint32_t src, uint32_t dst, int type, int code, uint32_t group, int datalen) {
    struct ip *ip;
    struct igmp *igmp;
    extern int curttl;

    ip                      = (struct ip *)send_buf;
    ip->ip_src.s_addr       = src;
    ip->ip_dst.s_addr       = dst;
    ip_set_len(ip, IP_HEADER_RAOPT_LEN + IGMP_MINLEN + datalen);

    if (IN_MULTICAST(ntohl(dst))) {
        ip->ip_ttl = curttl;
    } else {
        ip->ip_ttl = MAXTTL;
    }

    /* Add Router Alert option */
    ((unsigned char*)send_buf+MIN_IP_HEADER_LEN)[0] = IPOPT_RA;
    ((unsigned char*)send_buf+MIN_IP_HEADER_LEN)[1] = 0x04;
    ((unsigned char*)send_buf+MIN_IP_HEADER_LEN)[2] = 0x00;
    ((unsigned char*)send_buf+MIN_IP_HEADER_LEN)[3] = 0x00;

    igmp                    = (struct igmp *)(send_buf + IP_HEADER_RAOPT_LEN);
    igmp->igmp_type         = type;
    igmp->igmp_code         = code;
    igmp->igmp_group.s_addr = group;
    igmp->igmp_cksum        = 0;
    igmp->igmp_cksum        = inetChksum((unsigned short *)igmp,
                                         IP_HEADER_RAOPT_LEN + datalen);

}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top