blob: 9aae205c54f611034f72c146740bcff97fce8d00 [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>
Damjan Marion17fdae72017-11-30 20:56:37 +010022
23#include <linux/netlink.h>
24#include <linux/rtnetlink.h>
25
26#include <vlib/vlib.h>
27#include <vlib/unix/unix.h>
Damjan Marion2df39092017-12-04 20:03:37 +010028#include <vnet/devices/netlink.h>
Damjan Marion17fdae72017-11-30 20:56:37 +010029
Damjan Marion91c6ef72017-12-01 13:34:24 +010030typedef struct
31{
32 u8 *data;
33} vnet_netlink_msg_t;
34
Damjan Marion2df39092017-12-04 20:03:37 +010035static void
Damjan Marion91c6ef72017-12-01 13:34:24 +010036vnet_netlink_msg_init (vnet_netlink_msg_t * m, u16 type, u16 flags,
37 void *msg_data, int msg_len)
38{
39 struct nlmsghdr *nh;
40 u8 *p;
Dave Barachb7b92992018-10-17 10:38:51 -040041 clib_memset (m, 0, sizeof (vnet_netlink_msg_t));
Damjan Marion2df39092017-12-04 20:03:37 +010042 vec_add2 (m->data, p, NLMSG_SPACE (msg_len));
Damjan Marion91c6ef72017-12-01 13:34:24 +010043 ASSERT (m->data == p);
44
45 nh = (struct nlmsghdr *) p;
Damjan Marion2df39092017-12-04 20:03:37 +010046 nh->nlmsg_flags = flags | NLM_F_ACK;
Damjan Marion91c6ef72017-12-01 13:34:24 +010047 nh->nlmsg_type = type;
48 clib_memcpy (m->data + sizeof (struct nlmsghdr), msg_data, msg_len);
49}
50
51static void
52vnet_netlink_msg_add_rtattr (vnet_netlink_msg_t * m, u16 rta_type,
53 void *rta_data, int rta_data_len)
54{
55 struct rtattr *rta;
56 u8 *p;
57
Damjan Marion2df39092017-12-04 20:03:37 +010058 vec_add2 (m->data, p, RTA_SPACE (rta_data_len));
Damjan Marion91c6ef72017-12-01 13:34:24 +010059 rta = (struct rtattr *) p;
60 rta->rta_type = rta_type;
61 rta->rta_len = RTA_LENGTH (rta_data_len);
62 clib_memcpy (RTA_DATA (rta), rta_data, rta_data_len);
63}
64
65static clib_error_t *
Aloys Augustine39376e2021-03-29 22:08:09 +020066vnet_netlink_msg_send (vnet_netlink_msg_t *m, vnet_netlink_msg_t **replies)
Damjan Marion17fdae72017-11-30 20:56:37 +010067{
68 clib_error_t *err = 0;
Damjan Marion17fdae72017-11-30 20:56:37 +010069 struct sockaddr_nl ra = { 0 };
Damjan Marion2df39092017-12-04 20:03:37 +010070 int len, sock;
Damjan Marion91c6ef72017-12-01 13:34:24 +010071 struct nlmsghdr *nh = (struct nlmsghdr *) m->data;
72 nh->nlmsg_len = vec_len (m->data);
Damjan Marion2df39092017-12-04 20:03:37 +010073 char buf[4096];
Damjan Marion17fdae72017-11-30 20:56:37 +010074
Damjan Marion17fdae72017-11-30 20:56:37 +010075 if ((sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)
Stevenf953dfc2017-11-30 16:56:54 -080076 return clib_error_return_unix (0, "socket(AF_NETLINK)");
Damjan Marion17fdae72017-11-30 20:56:37 +010077
78 ra.nl_family = AF_NETLINK;
Jon Loeliger258fefd2018-03-02 13:05:12 -060079 ra.nl_pid = 0;
Damjan Marion17fdae72017-11-30 20:56:37 +010080
81 if ((bind (sock, (struct sockaddr *) &ra, sizeof (ra))) == -1)
Stevenf953dfc2017-11-30 16:56:54 -080082 {
83 err = clib_error_return_unix (0, "bind");
Damjan Marion2df39092017-12-04 20:03:37 +010084 goto done;
Stevenf953dfc2017-11-30 16:56:54 -080085 }
Damjan Marion17fdae72017-11-30 20:56:37 +010086
Damjan Marion91c6ef72017-12-01 13:34:24 +010087 if ((send (sock, m->data, vec_len (m->data), 0)) == -1)
Damjan Marion17fdae72017-11-30 20:56:37 +010088 err = clib_error_return_unix (0, "send");
89
Damjan Marion2df39092017-12-04 20:03:37 +010090 if ((len = recv (sock, buf, sizeof (buf), 0)) == -1)
91 err = clib_error_return_unix (0, "recv");
92
93 for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len);
94 nh = NLMSG_NEXT (nh, len))
95 {
96 if (nh->nlmsg_type == NLMSG_DONE)
97 goto done;
98
99 if (nh->nlmsg_type == NLMSG_ERROR)
100 {
101 struct nlmsgerr *e = (struct nlmsgerr *) NLMSG_DATA (nh);
102 if (e->error)
103 err = clib_error_return (0, "netlink error %d", e->error);
104 goto done;
105 }
Aloys Augustine39376e2021-03-29 22:08:09 +0200106
107 if (replies)
108 {
109 vnet_netlink_msg_t msg = { NULL };
110 u8 *p;
111 vec_add2 (msg.data, p, nh->nlmsg_len);
112 clib_memcpy (p, nh, nh->nlmsg_len);
113 vec_add1 (*replies, msg);
114 }
Damjan Marion2df39092017-12-04 20:03:37 +0100115 }
116
117done:
Stevenf953dfc2017-11-30 16:56:54 -0800118 close (sock);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100119 vec_free (m->data);
Damjan Marion17fdae72017-11-30 20:56:37 +0100120 return err;
121}
122
123clib_error_t *
Damjan Marion2df39092017-12-04 20:03:37 +0100124vnet_netlink_set_link_name (int ifindex, char *new_ifname)
Damjan Marion17fdae72017-11-30 20:56:37 +0100125{
Damjan Marion91c6ef72017-12-01 13:34:24 +0100126 vnet_netlink_msg_t m;
127 struct ifinfomsg ifmsg = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100128 clib_error_t *err = 0;
Damjan Marion17fdae72017-11-30 20:56:37 +0100129
Damjan Marion91c6ef72017-12-01 13:34:24 +0100130 ifmsg.ifi_index = ifindex;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100131 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
132 &ifmsg, sizeof (struct ifinfomsg));
133
Damjan Marion2df39092017-12-04 20:03:37 +0100134 vnet_netlink_msg_add_rtattr (&m, IFLA_IFNAME, new_ifname,
135 strlen (new_ifname) + 1);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100136
Aloys Augustine39376e2021-03-29 22:08:09 +0200137 err = vnet_netlink_msg_send (&m, NULL);
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100138 if (err)
139 err = clib_error_return (0, "set link name %U", format_clib_error, err);
140 return err;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100141}
142
143clib_error_t *
Damjan Marion2df39092017-12-04 20:03:37 +0100144vnet_netlink_set_link_netns (int ifindex, int netns_fd, char *new_ifname)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100145{
146 vnet_netlink_msg_t m;
147 struct ifinfomsg ifmsg = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100148 clib_error_t *err = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100149
Damjan Marion91c6ef72017-12-01 13:34:24 +0100150 ifmsg.ifi_index = ifindex;
Damjan Marion2df39092017-12-04 20:03:37 +0100151 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
152 &ifmsg, sizeof (struct ifinfomsg));
153
154 vnet_netlink_msg_add_rtattr (&m, IFLA_NET_NS_FD, &netns_fd, sizeof (int));
155 if (new_ifname)
156 vnet_netlink_msg_add_rtattr (&m, IFLA_IFNAME, new_ifname,
157 strlen (new_ifname) + 1);
158
Aloys Augustine39376e2021-03-29 22:08:09 +0200159 err = vnet_netlink_msg_send (&m, NULL);
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100160 if (err)
161 err = clib_error_return (0, "set link netns %U", format_clib_error, err);
162 return err;
Damjan Marion2df39092017-12-04 20:03:37 +0100163}
164
165clib_error_t *
166vnet_netlink_set_link_master (int ifindex, char *master_ifname)
167{
168 vnet_netlink_msg_t m;
169 struct ifinfomsg ifmsg = { 0 };
170 int i;
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100171 clib_error_t *err = 0;
Damjan Marion2df39092017-12-04 20:03:37 +0100172
173 ifmsg.ifi_index = ifindex;
174
175 if ((i = if_nametoindex (master_ifname)) == 0)
176 clib_error_return_unix (0, "unknown master interface '%s'",
177 master_ifname);
178
179 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
180 &ifmsg, sizeof (struct ifinfomsg));
181 vnet_netlink_msg_add_rtattr (&m, IFLA_MASTER, &i, sizeof (int));
Aloys Augustine39376e2021-03-29 22:08:09 +0200182 err = vnet_netlink_msg_send (&m, NULL);
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100183 if (err)
184 err = clib_error_return (0, "set link master %U", format_clib_error, err);
185 return err;
Damjan Marion2df39092017-12-04 20:03:37 +0100186}
187
188clib_error_t *
189vnet_netlink_set_link_addr (int ifindex, u8 * mac)
190{
191 vnet_netlink_msg_t m;
192 struct ifinfomsg ifmsg = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100193 clib_error_t *err = 0;
Damjan Marion2df39092017-12-04 20:03:37 +0100194
195 ifmsg.ifi_index = ifindex;
196
197 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
198 &ifmsg, sizeof (struct ifinfomsg));
199 vnet_netlink_msg_add_rtattr (&m, IFLA_ADDRESS, mac, 6);
Aloys Augustine39376e2021-03-29 22:08:09 +0200200 err = vnet_netlink_msg_send (&m, NULL);
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100201 if (err)
202 err = clib_error_return (0, "set link addr %U", format_clib_error, err);
203 return err;
Damjan Marion2df39092017-12-04 20:03:37 +0100204}
205
206clib_error_t *
207vnet_netlink_set_link_state (int ifindex, int up)
208{
209 vnet_netlink_msg_t m;
210 struct ifinfomsg ifmsg = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100211 clib_error_t *err = 0;
Damjan Marion2df39092017-12-04 20:03:37 +0100212
Matthew Smith81284162020-04-24 13:43:12 -0500213 ifmsg.ifi_flags = ((up) ? IFF_UP : 0);
Damjan Marion2df39092017-12-04 20:03:37 +0100214 ifmsg.ifi_change = IFF_UP;
215 ifmsg.ifi_index = ifindex;
216
217 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
218 &ifmsg, sizeof (struct ifinfomsg));
Aloys Augustine39376e2021-03-29 22:08:09 +0200219 err = vnet_netlink_msg_send (&m, NULL);
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100220 if (err)
221 err = clib_error_return (0, "set link state %U", format_clib_error, err);
222 return err;
Damjan Marion2df39092017-12-04 20:03:37 +0100223}
224
225clib_error_t *
Aloys Augustine39376e2021-03-29 22:08:09 +0200226vnet_netlink_get_link_mtu (int ifindex, u32 *mtu)
227{
228 vnet_netlink_msg_t m, *msg;
229 struct ifinfomsg ifmsg = { 0 };
230 struct nlattr *attr;
231 clib_error_t *err = 0;
232 vnet_netlink_msg_t *replies = NULL;
233 int len = 0, offset = 0;
234 u32 msg_mtu;
235
236 ifmsg.ifi_index = ifindex;
237
238 vnet_netlink_msg_init (&m, RTM_GETLINK, NLM_F_REQUEST, &ifmsg,
239 sizeof (struct ifinfomsg));
240 // vnet_netlink_msg_add_rtattr (&m, IFLA_MTU, &mtu, sizeof (int));
241 err = vnet_netlink_msg_send (&m, &replies);
242 if (err)
243 {
244 err = clib_error_return (0, "get link mtu %U", format_clib_error, err);
245 goto done;
246 }
247
248 if (vec_len (replies) != 1)
249 {
250 err = clib_error_return (0, "got %d != 1 netlink reply msg",
251 vec_len (replies));
252 goto done;
253 }
254
255 struct nlmsghdr *nh = (struct nlmsghdr *) replies[0].data;
256 if (nh->nlmsg_type != RTM_NEWLINK)
257 {
258 err = clib_error_return (
259 0, "netlink reply has wrong type: %d != RTM_NEWLINK", nh->nlmsg_type);
260 goto done;
261 }
262
263 offset = NLMSG_HDRLEN + NLMSG_ALIGN (sizeof (struct ifinfomsg));
264 attr = (struct nlattr *) ((u8 *) nh + offset);
265 len = nh->nlmsg_len - offset;
266
267 do
268 {
269 if ((attr->nla_type & NLA_TYPE_MASK) == IFLA_MTU)
270 {
271 msg_mtu = *(u32 *) ((u8 *) attr + NLA_HDRLEN);
272 if (attr->nla_type & NLA_F_NET_BYTEORDER)
273 *mtu = clib_net_to_host_u32 (msg_mtu);
274 else
275 *mtu = msg_mtu;
276 clib_warning ("mtu: %d", *mtu);
277 goto done;
278 }
279 offset = NLA_ALIGN (attr->nla_len);
280 len -= offset;
281 attr = (struct nlattr *) ((u8 *) attr + offset);
282 }
283 while (len > sizeof (struct nlattr));
284
285 /* not found */
286 err = clib_error_return (0, "mtu not found in netlink message");
287
288done:
289 vec_foreach (msg, replies)
290 {
291 vec_free (msg->data);
292 }
293 vec_free (replies);
294
295 return err;
296}
297
298clib_error_t *
Damjan Marion2df39092017-12-04 20:03:37 +0100299vnet_netlink_set_link_mtu (int ifindex, int mtu)
300{
301 vnet_netlink_msg_t m;
302 struct ifinfomsg ifmsg = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100303 clib_error_t *err = 0;
Damjan Marion2df39092017-12-04 20:03:37 +0100304
305 ifmsg.ifi_index = ifindex;
306
Damjan Marion91c6ef72017-12-01 13:34:24 +0100307 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
308 &ifmsg, sizeof (struct ifinfomsg));
309 vnet_netlink_msg_add_rtattr (&m, IFLA_MTU, &mtu, sizeof (int));
Aloys Augustine39376e2021-03-29 22:08:09 +0200310 err = vnet_netlink_msg_send (&m, NULL);
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100311 if (err)
312 err = clib_error_return (0, "set link mtu %U", format_clib_error, err);
313 return err;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100314}
315
316clib_error_t *
317vnet_netlink_add_ip4_addr (int ifindex, void *addr, int pfx_len)
318{
319 vnet_netlink_msg_t m;
320 struct ifaddrmsg ifa = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100321 clib_error_t *err = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100322
323 ifa.ifa_family = AF_INET;
324 ifa.ifa_prefixlen = pfx_len;
325 ifa.ifa_index = ifindex;
326
327 vnet_netlink_msg_init (&m, RTM_NEWADDR,
Dave Barach7e869472020-03-13 08:32:28 -0400328 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
Damjan Marion91c6ef72017-12-01 13:34:24 +0100329 &ifa, sizeof (struct ifaddrmsg));
330
331 vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 4);
332 vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 4);
Aloys Augustine39376e2021-03-29 22:08:09 +0200333 err = vnet_netlink_msg_send (&m, NULL);
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100334 if (err)
335 err = clib_error_return (0, "add ip4 addr %U", format_clib_error, err);
336 return err;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100337}
338
339clib_error_t *
340vnet_netlink_add_ip6_addr (int ifindex, void *addr, int pfx_len)
341{
342 vnet_netlink_msg_t m;
343 struct ifaddrmsg ifa = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100344 clib_error_t *err = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100345
346 ifa.ifa_family = AF_INET6;
347 ifa.ifa_prefixlen = pfx_len;
348 ifa.ifa_index = ifindex;
349
350 vnet_netlink_msg_init (&m, RTM_NEWADDR,
Dave Barach7e869472020-03-13 08:32:28 -0400351 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
Damjan Marion91c6ef72017-12-01 13:34:24 +0100352 &ifa, sizeof (struct ifaddrmsg));
353
354 vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 16);
355 vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 16);
Aloys Augustine39376e2021-03-29 22:08:09 +0200356 err = vnet_netlink_msg_send (&m, NULL);
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100357 if (err)
358 err = clib_error_return (0, "add ip6 addr %U", format_clib_error, err);
359 return err;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100360}
361
Damjan Marion7866c452018-01-18 13:35:11 +0100362clib_error_t *
363vnet_netlink_add_ip4_route (void *dst, u8 dst_len, void *gw)
364{
365 vnet_netlink_msg_t m;
366 struct rtmsg rtm = { 0 };
367 u8 dflt[4] = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100368 clib_error_t *err = 0;
Damjan Marion7866c452018-01-18 13:35:11 +0100369
370 rtm.rtm_family = AF_INET;
371 rtm.rtm_table = RT_TABLE_MAIN;
372 rtm.rtm_type = RTN_UNICAST;
373 rtm.rtm_dst_len = dst_len;
374
375 vnet_netlink_msg_init (&m, RTM_NEWROUTE,
Dave Barach7e869472020-03-13 08:32:28 -0400376 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
Damjan Marion7866c452018-01-18 13:35:11 +0100377 &rtm, sizeof (struct rtmsg));
378
379 vnet_netlink_msg_add_rtattr (&m, RTA_GATEWAY, gw, 4);
380 vnet_netlink_msg_add_rtattr (&m, RTA_DST, dst ? dst : dflt, 4);
Aloys Augustine39376e2021-03-29 22:08:09 +0200381 err = vnet_netlink_msg_send (&m, NULL);
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100382 if (err)
383 err = clib_error_return (0, "add ip4 route %U", format_clib_error, err);
384 return err;
Damjan Marion7866c452018-01-18 13:35:11 +0100385}
386
387clib_error_t *
388vnet_netlink_add_ip6_route (void *dst, u8 dst_len, void *gw)
389{
390 vnet_netlink_msg_t m;
391 struct rtmsg rtm = { 0 };
392 u8 dflt[16] = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100393 clib_error_t *err = 0;
Damjan Marion7866c452018-01-18 13:35:11 +0100394
395 rtm.rtm_family = AF_INET6;
396 rtm.rtm_table = RT_TABLE_MAIN;
397 rtm.rtm_type = RTN_UNICAST;
398 rtm.rtm_dst_len = dst_len;
399
400 vnet_netlink_msg_init (&m, RTM_NEWROUTE,
Dave Barach7e869472020-03-13 08:32:28 -0400401 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
Damjan Marion7866c452018-01-18 13:35:11 +0100402 &rtm, sizeof (struct rtmsg));
403
404 vnet_netlink_msg_add_rtattr (&m, RTA_GATEWAY, gw, 16);
405 vnet_netlink_msg_add_rtattr (&m, RTA_DST, dst ? dst : dflt, 16);
Aloys Augustine39376e2021-03-29 22:08:09 +0200406 err = vnet_netlink_msg_send (&m, NULL);
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100407 if (err)
408 err = clib_error_return (0, "add ip6 route %U", format_clib_error, err);
409 return err;
Damjan Marion7866c452018-01-18 13:35:11 +0100410}
411
Damjan Marion17fdae72017-11-30 20:56:37 +0100412/*
413 * fd.io coding-style-patch-verification: ON
414 *
415 * Local Variables:
416 * eval: (c-set-style "gnu")
417 * End:
418 */