c - 我无法通过 NETLINK_ROUTE 选项通过 Netlink 套接字设置特定接口(interface)(eth0 或 eth1)的 MTU 大小

标签 c netlink mtu

我编写了一个程序,将特定接口(interface)(例如 eth0eth1)的 MTU 大小设置为 1100。请求消息是从用户空间发送的通过 NETLINK_ROUTE 选项的 Netlink 套接字。

消息已从用户空间成功发送,但是当我验证 ifconfig eth0 时,MTU 大小仍然显示旧值 (1500)。我验证正确吗?我如何知道内核是否正确设置了 MTU 大小?请在下面找到我的程序,如果我错了,请纠正我。

 #include <stdio.h>
 #include <stdlib.h>
 #include <net/if.h>
 #include <string.h>
 #include <sys/socket.h>
 #include <linux/netlink.h>
 #include <linux/rtnetlink.h>
 #include <stdbool.h>

  struct {
     struct nlmsghdr nh;
     struct ifinfomsg  ifinfo;
     char   data[100];
  }req;

  int ret;

  struct rtattr  *rta;

  /* MTU Set */
  unsigned int mtu = 1100;

  /* rtNetlinkSockFd */
  int rtNetlinkSockFd = -1;

  int main()
  {

     int ret;

     rtNetlinkSockFd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);

     if(rtNetlinkSockFd < 0)
     {
       printf("Creation of NetLink Socket is failed \n");
       return -1;
     }

     /* Memset the Requested Structure */
     memset( &req, 0x00, sizeof(req));

     /* Populate the Netlink Header Fields */
     req.nh.nlmsg_len   = NLMSG_LENGTH(sizeof(struct ifinfomsg));


 /* Link Layer: RTM_NEWLINK, RTM_DELLINK, RTM_GETLINK, RTM_SETLINK */
 req.nh.nlmsg_type  = RTM_SETLINK;

 /*   NLM_F_REQUEST   Must be set on all request messages. */
 req.nh.nlmsg_flags = NLM_F_REQUEST;

 req.nh.nlmsg_seq   = 0;
 req.nh.nlmsg_pid   = 0; //getpid();

 /* Populate the Ifinfo Structure Attributes */ 
 req.ifinfo.ifi_family = AF_UNSPEC;

 /* Give the Interface Name and get the Index */
 req.ifinfo.ifi_index  = if_nametoindex("eth0");

 printf(" The NetLink Ifi_index :%d\n", req.ifinfo.ifi_index);

 /* ifi_change is reserved for future use and
 * should be always set to 0xFFFFFFFF. */
 req.ifinfo.ifi_change = 0xFFFFFFFF;
 req.ifinfo.ifi_type   = 0;
 req.ifinfo.ifi_flags  = 0;

 /* RTA is Pointed to (req+32) it means points to the DATA */
 rta = (struct rtattr *)(((char *) &req) +  NLMSG_ALIGN(req.nh.nlmsg_len));

 /*  IFLA_MTU      unsigned int       MTU of the device. */
 rta->rta_type = IFLA_MTU;

 /* Len Attribute */
 rta->rta_len  = sizeof(unsigned int);

 req.nh.nlmsg_len = NLMSG_ALIGN(req.nh.nlmsg_len) + RTA_LENGTH(sizeof(mtu));

 memcpy(RTA_DATA(rta), &mtu, sizeof(mtu));

 ret = send(rtNetlinkSockFd, &req, req.nh.nlmsg_len, 0);

 if (ret < 0) 
 {
    printf( "netlink: Sending failed: (assume operstate is not supported)");
 }
 else
 {
   printf( "netlink: Sent successfully");
 }
return 0;
}

最佳答案

我认为你需要写:

rta->rta_len = RTA_LENGTH(sizeof(unsigned int));

而不是:

rta->rta_len  = sizeof(unsigned int);

关于c - 我无法通过 NETLINK_ROUTE 选项通过 Netlink 套接字设置特定接口(interface)(eth0 或 eth1)的 MTU 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12732169/

相关文章:

c - 使用 3.X linux 内核的 C 中的 Netlink 套接字

Android BLE 连接成功后有一些延迟

c - 为具有多个接口(interface)的设备设置 MTU

c - 使用 NETLINK 的 VLAN 信息

linux - 为什么即使包含 "RTMGRP_LINK undeclared"也会收到 "rtnetlink.h"错误?

c - 如何在结构中存储函数指针?

c - 在 C 中获取双字的高位字的最有效方法是什么?

c - 如何跳过链表中的起始节点

c++ - 使用可变宏时获取 "Error: #28: expression must have a constant value "