virtio: fix coverity warnings
[vpp.git] / src / vnet / devices / netlink.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2017 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <net/if.h>
22 #include <linux/if_tun.h>
23 #include <sys/ioctl.h>
24 #include <linux/virtio_net.h>
25 #include <linux/vhost.h>
26 #include <sys/eventfd.h>
27
28 #include <linux/netlink.h>
29 #include <linux/rtnetlink.h>
30
31 #include <vlib/vlib.h>
32 #include <vlib/unix/unix.h>
33
34 clib_error_t *
35 vnet_netlink_set_if_attr (int ifindex, unsigned short rta_type, void *data,
36                           int data_len)
37 {
38   clib_error_t *err = 0;
39   int sock;
40   struct sockaddr_nl ra = { 0 };
41   struct
42   {
43     struct nlmsghdr nh;
44     struct ifinfomsg ifmsg;
45     char attrbuf[512];
46   } req;
47   struct rtattr *rta;
48
49   memset (&req, 0, sizeof (req));
50   if ((sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)
51     return clib_error_return_unix (0, "socket(AF_NETLINK)");
52
53   ra.nl_family = AF_NETLINK;
54   ra.nl_pid = getpid ();
55
56   if ((bind (sock, (struct sockaddr *) &ra, sizeof (ra))) == -1)
57     {
58       err = clib_error_return_unix (0, "bind");
59       goto error;
60     }
61
62   req.nh.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifinfomsg));
63   req.nh.nlmsg_flags = NLM_F_REQUEST;
64   req.nh.nlmsg_type = RTM_SETLINK;
65   req.ifmsg.ifi_family = AF_UNSPEC;
66   req.ifmsg.ifi_index = ifindex;
67   req.ifmsg.ifi_change = 0xffffffff;
68   rta = (struct rtattr *) (((char *) &req) + NLMSG_ALIGN (req.nh.nlmsg_len));
69   rta->rta_type = rta_type;
70   rta->rta_len = RTA_LENGTH (data_len);
71   req.nh.nlmsg_len = NLMSG_ALIGN (req.nh.nlmsg_len) + RTA_LENGTH (data_len);
72   memcpy (RTA_DATA (rta), data, data_len);
73
74   if ((send (sock, &req, req.nh.nlmsg_len, 0)) == -1)
75     err = clib_error_return_unix (0, "send");
76
77 error:
78   close (sock);
79   return err;
80 }
81
82 clib_error_t *
83 vnet_netlink_set_if_mtu (int ifindex, int mtu)
84 {
85   clib_error_t *err;
86
87   err = vnet_netlink_set_if_attr (ifindex, IFLA_MTU, &mtu, sizeof (int));
88   return err;
89 }
90
91 clib_error_t *
92 vnet_netlink_set_if_namespace (int ifindex, char *net_ns)
93 {
94   clib_error_t *err;
95   int ns_fd;
96   u8 *s;
97   s = format (0, "/var/run/netns/%s%c", net_ns, 0);
98   ns_fd = open ((char *) s, O_RDONLY);
99   vec_free (s);
100   if (ns_fd == -1)
101     return clib_error_return (0, "namespace '%s' doesn't exist", net_ns);
102
103   err =
104     vnet_netlink_set_if_attr (ifindex, IFLA_NET_NS_FD, &ns_fd, sizeof (int));
105   close (ns_fd);
106   return err;
107 }
108
109 /*
110  * fd.io coding-style-patch-verification: ON
111  *
112  * Local Variables:
113  * eval: (c-set-style "gnu")
114  * End:
115  */