簡體   English   中英

帶有 static 的套接字選項 IP_MULTICAST_IF 多播路由從多播切換到單播 MAC 尋址

[英]Socket option IP_MULTICAST_IF with static multicast route switches from multicast to unicast MAC addressing

我希望專家對套接字選項 IP_MULTICAST_IF(“設置多播接口”)與 static 多播路由的使用的建議。

在 LAN 上,多播 IP 數據報通常在多播以太網幀(IP/MAC 多播目標地址映射)中發送。 在多宿主 Linux 系統(內核 5.11)上,我注意到套接字選項IP_MULTICAST_IF將行為修改如下:

  • 如果沒有 static 路由,多播 IP 數據報始終在多播以太網幀中發送,無論是否帶有IP_MULTICAST_IF
  • 使用 static 路由,沒有IP_MULTICAST_IF ,多播 IP 數據報在多播以太網幀中發送。
  • 使用 static 路由和IP_MULTICAST_IF ,多播 IP 數據報在單播以太網幀中發送到網關。

第一個問題:對於多播數據包的 static 路由,多播 IP 數據報應該以多播以太網幀還是單播以太網幀發送到網關?

第二個問題:不管第一個問題的答案是什么,為什么套接字選項 IP_MULTICAST_IF 會從多播切換到單播 MAC 尋址?

Linux 手冊頁(“man 7 ip”)不是很明確:

IP_MULTICAST_IF (since Linux 1.2)
    Set  the  local device for a multicast socket.  The argument for setsockopt(2) is an ip_mreqn or (since Linux 3.5)
    ip_mreq structure similar to IP_ADD_MEMBERSHIP, or an in_addr structure.  (The kernel determines  which  structure
    is being passed based on the size passed in optlen.)  For getsockopt(2), the argument is an in_addr structure.

這是在兩個 Linux 虛擬機之間重現此問題的示例配置。

第一個系統“vmubuntu”,在接口 ens38 上運行 Wireshark 的接收器:

ens33: 00:0C:29:46:B7:CE  192.168.98.3   / 24  -> NAT, default route
ens38: 00:50:56:39:F0:03  192.168.233.11 / 24  -> host local vmware

第二個系統,“vmfedora”,發件人系統:

ens33: 00:0C:29:B6:33:8D  192.168.98.2   / 24  -> NAT, default route
ens37: 00:50:56:29:34:37  192.168.233.10 / 24  -> host local vmware

我們在“vmfedora”上聲明一個 static 路由用於多播流量,使用“vmubuntu”作為網關。 請注意,一般 IP 流量的默認路由在另一個接口上。

$ uname -a
Linux vmfedora 5.11.15-200.fc33.x86_64 #1 SMP Fri Apr 16 13:41:20 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
$
$ sudo route add -net 224.0.0.0 netmask 240.0.0.0 gw 192.168.233.11 dev ens37
$
$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.98.1    0.0.0.0         UG    20100  0        0 ens33
192.168.98.0    0.0.0.0         255.255.255.0   U     100    0        0 ens33
192.168.233.0   0.0.0.0         255.255.255.0   U     101    0        0 ens37
224.0.0.0       192.168.233.11  240.0.0.0       UG    0      0        0 ens37

讓我們將多播數據包從“vmfedora”發送到路由范圍內的 239.230.2.44:3044。 我們將套接字綁定到本地地址 192.168.233.10,這也是 static 路由的出接口。 我們不使用套接字選項IP_MULTICAST_IF (下面的示例代碼)。

在“vmubuntu”上,Wireshark 報告了這一點:

Internet Protocol Version 4, Src: 192.168.233.10, Dst: 239.230.2.44
Ethernet II, Src: VMware_29:34:37 (00:50:56:29:34:37), Dst: IPv4mcast_66:02:2c (01:00:5e:66:02:2c)
    Destination: IPv4mcast_66:02:2c (01:00:5e:66:02:2c)
        Address: IPv4mcast_66:02:2c (01:00:5e:66:02:2c)
        .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
        .... ...1 .... .... .... .... = IG bit: Group address (multicast/broadcast)
    Source: VMware_29:34:37 (00:50:56:29:34:37)
        Address: VMware_29:34:37 (00:50:56:29:34:37)
        .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
        .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
    Type: IPv4 (0x0800)

我們可以看到,帶有多播目標地址的 IP 數據報是在具有相應多播目標地址的以太網幀中發送的。

現在,讓我們在bind()之后添加套接字選項IP_MULTICAST_IF Wireshark 報告了這一點:

Internet Protocol Version 4, Src: 192.168.233.10, Dst: 239.230.2.44
Ethernet II, Src: VMware_29:34:37 (00:50:56:29:34:37), Dst: VMware_39:f0:03 (00:50:56:39:f0:03)
    Destination: VMware_39:f0:03 (00:50:56:39:f0:03)
        Address: VMware_39:f0:03 (00:50:56:39:f0:03)
        .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
        .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
    Source: VMware_29:34:37 (00:50:56:29:34:37)
        Address: VMware_29:34:37 (00:50:56:29:34:37)
        .... ..0. .... .... .... .... = LG bit: Globally unique address (factory default)
        .... ...0 .... .... .... .... = IG bit: Individual address (unicast)
    Type: IPv4 (0x0800)

現在,我們看到帶有多播目標地址的 IP 數據報在以網關的單播 MAC 地址作為目標地址的以太網幀中發送。

唯一的區別是套接字選項IP_MULTICAST_IF

如果您想重現此內容,請參閱下面的代碼。 它需要一個強制性的命令行參數:套接字綁定到的本地接口的 IP 地址。 第二個可選的命令行參數是“-f”,用於強制套接字選項IP_MULTICAST_IF (默認情況下未設置)。

感謝您對此選項的解釋。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

int main(int argc, char* argv[])
{
    struct sockaddr_in dest;
    dest.sin_family = AF_INET;
    dest.sin_port = htons(3044);
    inet_pton(AF_INET, "239.230.2.44", &dest.sin_addr);

    struct sockaddr_in local;
    local.sin_family = AF_INET;
    local.sin_port = 0;
    local.sin_addr.s_addr = 0;

    int use_mcast_if = 0;

    for (int arg = 1; arg < argc; ++arg) {
        if (strcmp(argv[arg], "-f") == 0) {
            use_mcast_if = 1;
        }
        else if (inet_pton(AF_INET, argv[arg], &local.sin_addr) != 1) {
            fprintf(stderr, "invalid local address: %s\n", argv[arg]);
            return EXIT_FAILURE;
        }
    }

    const int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (sock < 0) {
        perror("socket()");
        return EXIT_FAILURE;
    }
    
    if (bind(sock, (struct sockaddr*)&local, sizeof(local)) < 0) {
        perror("bind()");
        return EXIT_FAILURE;
    }

    if (use_mcast_if && setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &local.sin_addr, sizeof(local.sin_addr)) < 0) {
        perror("setsockopt(IP_MULTICAST_IF)");
        return EXIT_FAILURE;
    }

    const int data = 0x12345678;
    for (int i = 0; i < 10; ++i) {
        sendto(sock, &data, sizeof(data), 0, (struct sockaddr*)&dest, sizeof(dest));
    }

    close(sock);
    return EXIT_SUCCESS;
}

Linux 通常不會在沒有特殊軟件(例如 mrouted 或 pimd)的情況下處理多播路由。 我在 CentOS 7 VM(3.10 內核)上嘗試了這個,並看到了 static 路由的單播 MAC 地址,無論我是否使用IP_MULTICAST_IF

以下有關 ServerFault 的帖子對此進行了更詳細的介紹:

https://serverfault.com/questions/814259/use-ip-route-add-to-add-multicast-routes-to-multiple-interfaces

TL;DR -- 不要使用 static 多播路由。 堅持設置IP_MULTICAST_IF

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM