blob: b3330dd89b9ba82657ab922462f81d53a305d7b4 [file] [log] [blame]
Damjan Marion17fdae72017-11-30 20:56:37 +01001/*
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
Damjan Marion91c6ef72017-12-01 13:34:24 +010034typedef struct
35{
36 u8 *data;
37} vnet_netlink_msg_t;
38
39void
40vnet_netlink_msg_init (vnet_netlink_msg_t * m, u16 type, u16 flags,
41 void *msg_data, int msg_len)
42{
43 struct nlmsghdr *nh;
44 u8 *p;
45 int len = NLMSG_LENGTH (msg_len);
46 memset (m, 0, sizeof (vnet_netlink_msg_t));
47 vec_add2 (m->data, p, len);
48 ASSERT (m->data == p);
49
50 nh = (struct nlmsghdr *) p;
51 nh->nlmsg_flags = flags;
52 nh->nlmsg_type = type;
53 clib_memcpy (m->data + sizeof (struct nlmsghdr), msg_data, msg_len);
54}
55
56static void
57vnet_netlink_msg_add_rtattr (vnet_netlink_msg_t * m, u16 rta_type,
58 void *rta_data, int rta_data_len)
59{
60 struct rtattr *rta;
61 u8 *p;
62
63 vec_add2 (m->data, p, RTA_LENGTH (rta_data_len));
64 rta = (struct rtattr *) p;
65 rta->rta_type = rta_type;
66 rta->rta_len = RTA_LENGTH (rta_data_len);
67 clib_memcpy (RTA_DATA (rta), rta_data, rta_data_len);
68}
69
70static clib_error_t *
71vnet_netlink_msg_send (vnet_netlink_msg_t * m)
Damjan Marion17fdae72017-11-30 20:56:37 +010072{
73 clib_error_t *err = 0;
Damjan Marion17fdae72017-11-30 20:56:37 +010074 struct sockaddr_nl ra = { 0 };
Damjan Marion91c6ef72017-12-01 13:34:24 +010075 int sock;
76 struct nlmsghdr *nh = (struct nlmsghdr *) m->data;
77 nh->nlmsg_len = vec_len (m->data);
Damjan Marion17fdae72017-11-30 20:56:37 +010078
Damjan Marion17fdae72017-11-30 20:56:37 +010079 if ((sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)
Stevenf953dfc2017-11-30 16:56:54 -080080 return clib_error_return_unix (0, "socket(AF_NETLINK)");
Damjan Marion17fdae72017-11-30 20:56:37 +010081
82 ra.nl_family = AF_NETLINK;
83 ra.nl_pid = getpid ();
84
85 if ((bind (sock, (struct sockaddr *) &ra, sizeof (ra))) == -1)
Stevenf953dfc2017-11-30 16:56:54 -080086 {
87 err = clib_error_return_unix (0, "bind");
88 goto error;
89 }
Damjan Marion17fdae72017-11-30 20:56:37 +010090
Damjan Marion91c6ef72017-12-01 13:34:24 +010091 if ((send (sock, m->data, vec_len (m->data), 0)) == -1)
Damjan Marion17fdae72017-11-30 20:56:37 +010092 err = clib_error_return_unix (0, "send");
93
94error:
Stevenf953dfc2017-11-30 16:56:54 -080095 close (sock);
Damjan Marion91c6ef72017-12-01 13:34:24 +010096 vec_free (m->data);
Damjan Marion17fdae72017-11-30 20:56:37 +010097 return err;
98}
99
100clib_error_t *
101vnet_netlink_set_if_namespace (int ifindex, char *net_ns)
102{
Damjan Marion91c6ef72017-12-01 13:34:24 +0100103 vnet_netlink_msg_t m;
104 struct ifinfomsg ifmsg = { 0 };
Damjan Marion17fdae72017-11-30 20:56:37 +0100105
Damjan Marion91c6ef72017-12-01 13:34:24 +0100106 clib_error_t *err;
107 int data;
108 u16 type;
109 u8 *s;
110
111 if (strncmp (net_ns, "pid:", 4) == 0)
112 {
113 data = atoi (net_ns + 4);
114 type = IFLA_NET_NS_PID;
115 }
116 else
117 {
118 if (net_ns[0] == '/')
119 s = format (0, "%s%c", net_ns, 0);
120 else
121 s = format (0, "/var/run/netns/%s%c", net_ns, 0);
122
123 data = open ((char *) s, O_RDONLY);
124 type = IFLA_NET_NS_FD;
125 vec_free (s);
126 if (data == -1)
127 return clib_error_return (0, "namespace '%s' doesn't exist", net_ns);
128 }
129
130 ifmsg.ifi_family = AF_UNSPEC;
131 ifmsg.ifi_index = ifindex;
132 ifmsg.ifi_change = 0xffffffff;
133 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
134 &ifmsg, sizeof (struct ifinfomsg));
135
136 vnet_netlink_msg_add_rtattr (&m, type, &data, sizeof (int));
137 err = vnet_netlink_msg_send (&m);
138
139 if (type == IFLA_NET_NS_FD)
140 close (data);
Damjan Marion17fdae72017-11-30 20:56:37 +0100141 return err;
142}
143
Damjan Marion91c6ef72017-12-01 13:34:24 +0100144clib_error_t *
145vnet_netlink_set_if_master (int ifindex, int master_ifindex)
146{
147 vnet_netlink_msg_t m;
148 struct ifinfomsg ifmsg = { 0 };
149
150 ifmsg.ifi_family = AF_UNSPEC;
151 ifmsg.ifi_index = ifindex;
152 ifmsg.ifi_change = 0xffffffff;
153 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
154 &ifmsg, sizeof (struct ifinfomsg));
155 vnet_netlink_msg_add_rtattr (&m, IFLA_MASTER, &master_ifindex,
156 sizeof (int));
157 return vnet_netlink_msg_send (&m);
158}
159
160clib_error_t *
161vnet_netlink_set_if_mtu (int ifindex, int mtu)
162{
163 vnet_netlink_msg_t m;
164 struct ifinfomsg ifmsg = { 0 };
165
166 ifmsg.ifi_family = AF_UNSPEC;
167 ifmsg.ifi_index = ifindex;
168 ifmsg.ifi_change = 0xffffffff;
169 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
170 &ifmsg, sizeof (struct ifinfomsg));
171 vnet_netlink_msg_add_rtattr (&m, IFLA_MTU, &mtu, sizeof (int));
172 return vnet_netlink_msg_send (&m);
173}
174
175clib_error_t *
176vnet_netlink_add_ip4_addr (int ifindex, void *addr, int pfx_len)
177{
178 vnet_netlink_msg_t m;
179 struct ifaddrmsg ifa = { 0 };
180
181 ifa.ifa_family = AF_INET;
182 ifa.ifa_prefixlen = pfx_len;
183 ifa.ifa_index = ifindex;
184
185 vnet_netlink_msg_init (&m, RTM_NEWADDR,
186 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL,
187 &ifa, sizeof (struct ifaddrmsg));
188
189 vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 4);
190 vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 4);
191 return vnet_netlink_msg_send (&m);
192}
193
194clib_error_t *
195vnet_netlink_add_ip6_addr (int ifindex, void *addr, int pfx_len)
196{
197 vnet_netlink_msg_t m;
198 struct ifaddrmsg ifa = { 0 };
199
200 ifa.ifa_family = AF_INET6;
201 ifa.ifa_prefixlen = pfx_len;
202 ifa.ifa_index = ifindex;
203
204 vnet_netlink_msg_init (&m, RTM_NEWADDR,
205 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL,
206 &ifa, sizeof (struct ifaddrmsg));
207
208 vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 16);
209 vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 16);
210 return vnet_netlink_msg_send (&m);
211}
212
Damjan Marion17fdae72017-11-30 20:56:37 +0100213/*
214 * fd.io coding-style-patch-verification: ON
215 *
216 * Local Variables:
217 * eval: (c-set-style "gnu")
218 * End:
219 */