I am not able to set MTU size of particular interfaces(eth0 or eth1) through Netlink sockets via NETLINK_ROUTE option

StackOverflow https://stackoverflow.com/questions/12732169

  •  05-07-2021
  •  | 
  •  

Question

I have written a program to set the MTU size of the particular interface(say eth0 or eth1) to 1100. And the Request message is send from user space using Netlink sockets via NETLINK_ROUTE option.

The message is sent successfully from user space, but when i verified the ifconfig eth0, the MTU size still shows the old value (1500). Am I verifying correctly? And how do I know the kernel is setting the MTU size correctly or not? Please find my program below and correct me if i am wrong.

 #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;
}
Was it helpful?

Solution

I think you need to write:

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

instead of:

rta->rta_len  = sizeof(unsigned int);

OTHER TIPS

Use RTM_NEWLINK instead of RTM_SETLINK.

req.nh.nlmsg_type = RTM_NEWLINK;

See the manpage example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top