Linux Netfilter discussions
 help / color / mirror / Atom feed
* Libmnl - Adding IPv6 Address in the interface
@ 2016-08-29  6:52 Khawar Shehzad
  2016-08-29 11:40 ` Pablo Neira Ayuso
  0 siblings, 1 reply; 2+ messages in thread
From: Khawar Shehzad @ 2016-08-29  6:52 UTC (permalink / raw)
  To: netfilter

Hi,
I hope all are fine.
You can view my question here as well ( 
http://stackoverflow.com/questions/39183969/adding-an-ipv6-address-to-the-interface-using-libmnl-and-rtnetlink 
)

I am trying to add an IPv6 address to an ethernet interface, using 
libmnl. After constructing a message and sending to the kernel, I saw 
that it was not added to the interface, even though the return codes for 
the kernel reply did not contain any error. Kindly can anybody have a 
look, and help me correct it. Am I supposed to add more attributes to 
the nlmsghdr or something else?


     #include <assert.h>
     #include <string.h>
     #include <inttypes.h>
     #include <sys/socket.h>
     #include <arpa/inet.h>
     #include <libmnl/libmnl.h>
     #include <linux/rtnetlink.h>
     #include <net/if.h>
     #include <time.h>
     static struct mnl_socket *nl;
     static unsigned int nlportid;

     int mnl_init(void);
     int mnl_init(){
         nl = mnl_socket_open(NETLINK_ROUTE);
         if(nl == NULL){
             printf("Error: mnl_socket_open\n");
             return 0;
         }
         if(mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0){
             printf("Error: mnl_socket_bind\n");
             return 0;
         }
         nlportid = mnl_socket_get_portid(nl);
         return 1;
     }
     int add_to_interface(const char* eip){
         char buf[MNL_SOCKET_BUFFER_SIZE];
         struct nlmsghdr *nlh;
         struct ifaddrmsg *ifm;
         int ret;
         uint8_t seq;

         nlh = mnl_nlmsg_put_header(buf);

         nlh->nlmsg_type = RTM_NEWADDR;
         nlh->nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE|NLM_F_EXCL;
         nlh->nlmsg_seq = seq = time(NULL);

         ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));

         ifm->ifa_family = AF_INET6;
         ifm->ifa_prefixlen = 64;
         ifm->ifa_flags = IFA_F_PERMANENT;
         ifm->ifa_scope = RT_SCOPE_UNIVERSE;
         /* TODO get interaface name from user or configuration*/
         ifm->ifa_index = if_nametoindex("eth0");
         unsigned char eipn[16];
         inet_pton(AF_INET6, eip, eipn);
         mnl_attr_put(nlh, IFA_ADDRESS, 16,eipn);

         mnl_nlmsg_fprintf(stdout, nlh, nlh->nlmsg_len, sizeof(struct 
ifaddrmsg));

         if(mnl_socket_sendto(nl,nlh, nlh->nlmsg_len) < 0){
             printf("Error: mnl_socket_sendto");
             return 0;
         }
         ret = mnl_socket_recvfrom(nl,buf, sizeof(buf));
         if(ret == -1){
             printf("Error: mnl_socket_recvfrom");
             return 0;
         }
         ret = mnl_cb_run(buf, ret, seq, nlportid, NULL, NULL);
         return 0;
     }

     int main(int argc, char *argv[]){
         if(mnl_init()){
             add_to_interface("2001::20c:29ff:fe5f:13c7/64"); // for testing
         }
     }

Any help will be much appreciated.
Cheers,
Khawar


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Libmnl - Adding IPv6 Address in the interface
  2016-08-29  6:52 Libmnl - Adding IPv6 Address in the interface Khawar Shehzad
@ 2016-08-29 11:40 ` Pablo Neira Ayuso
  0 siblings, 0 replies; 2+ messages in thread
From: Pablo Neira Ayuso @ 2016-08-29 11:40 UTC (permalink / raw)
  To: Khawar Shehzad; +Cc: netfilter

On Mon, Aug 29, 2016 at 07:52:34AM +0100, Khawar Shehzad wrote:
>         ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
> 
>         ifm->ifa_family = AF_INET6;
>         ifm->ifa_prefixlen = 64;
>         ifm->ifa_flags = IFA_F_PERMANENT;
>         ifm->ifa_scope = RT_SCOPE_UNIVERSE;
>         /* TODO get interaface name from user or configuration*/
>         ifm->ifa_index = if_nametoindex("eth0");
>         unsigned char eipn[16];
>         inet_pton(AF_INET6, eip, eipn);

eipn should be 'struct in6_addr' instead.

>         mnl_attr_put(nlh, IFA_ADDRESS, 16,eipn);

So this looks like:

          mnl_attr_put(nlh, IFA_ADDRESS, sizeof(eipn), &eipn);

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-08-29 11:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-08-29  6:52 Libmnl - Adding IPv6 Address in the interface Khawar Shehzad
2016-08-29 11:40 ` Pablo Neira Ayuso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox