blob: 34309e1b9166504a2d5d59f4cd35a1e363fe53c2 [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 *
66vnet_netlink_msg_send (vnet_netlink_msg_t * m)
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 }
106 }
107
108done:
Stevenf953dfc2017-11-30 16:56:54 -0800109 close (sock);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100110 vec_free (m->data);
Damjan Marion17fdae72017-11-30 20:56:37 +0100111 return err;
112}
113
114clib_error_t *
Damjan Marion2df39092017-12-04 20:03:37 +0100115vnet_netlink_set_link_name (int ifindex, char *new_ifname)
Damjan Marion17fdae72017-11-30 20:56:37 +0100116{
Damjan Marion91c6ef72017-12-01 13:34:24 +0100117 vnet_netlink_msg_t m;
118 struct ifinfomsg ifmsg = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100119 clib_error_t *err = 0;
Damjan Marion17fdae72017-11-30 20:56:37 +0100120
Damjan Marion91c6ef72017-12-01 13:34:24 +0100121 ifmsg.ifi_index = ifindex;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100122 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
123 &ifmsg, sizeof (struct ifinfomsg));
124
Damjan Marion2df39092017-12-04 20:03:37 +0100125 vnet_netlink_msg_add_rtattr (&m, IFLA_IFNAME, new_ifname,
126 strlen (new_ifname) + 1);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100127
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100128 err = vnet_netlink_msg_send (&m);
129 if (err)
130 err = clib_error_return (0, "set link name %U", format_clib_error, err);
131 return err;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100132}
133
134clib_error_t *
Damjan Marion2df39092017-12-04 20:03:37 +0100135vnet_netlink_set_link_netns (int ifindex, int netns_fd, char *new_ifname)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100136{
137 vnet_netlink_msg_t m;
138 struct ifinfomsg ifmsg = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100139 clib_error_t *err = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100140
Damjan Marion91c6ef72017-12-01 13:34:24 +0100141 ifmsg.ifi_index = ifindex;
Damjan Marion2df39092017-12-04 20:03:37 +0100142 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
143 &ifmsg, sizeof (struct ifinfomsg));
144
145 vnet_netlink_msg_add_rtattr (&m, IFLA_NET_NS_FD, &netns_fd, sizeof (int));
146 if (new_ifname)
147 vnet_netlink_msg_add_rtattr (&m, IFLA_IFNAME, new_ifname,
148 strlen (new_ifname) + 1);
149
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100150 err = vnet_netlink_msg_send (&m);
151 if (err)
152 err = clib_error_return (0, "set link netns %U", format_clib_error, err);
153 return err;
Damjan Marion2df39092017-12-04 20:03:37 +0100154}
155
156clib_error_t *
157vnet_netlink_set_link_master (int ifindex, char *master_ifname)
158{
159 vnet_netlink_msg_t m;
160 struct ifinfomsg ifmsg = { 0 };
161 int i;
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100162 clib_error_t *err = 0;
Damjan Marion2df39092017-12-04 20:03:37 +0100163
164 ifmsg.ifi_index = ifindex;
165
166 if ((i = if_nametoindex (master_ifname)) == 0)
167 clib_error_return_unix (0, "unknown master interface '%s'",
168 master_ifname);
169
170 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
171 &ifmsg, sizeof (struct ifinfomsg));
172 vnet_netlink_msg_add_rtattr (&m, IFLA_MASTER, &i, sizeof (int));
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100173 err = vnet_netlink_msg_send (&m);
174 if (err)
175 err = clib_error_return (0, "set link master %U", format_clib_error, err);
176 return err;
Damjan Marion2df39092017-12-04 20:03:37 +0100177}
178
179clib_error_t *
180vnet_netlink_set_link_addr (int ifindex, u8 * mac)
181{
182 vnet_netlink_msg_t m;
183 struct ifinfomsg ifmsg = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100184 clib_error_t *err = 0;
Damjan Marion2df39092017-12-04 20:03:37 +0100185
186 ifmsg.ifi_index = ifindex;
187
188 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
189 &ifmsg, sizeof (struct ifinfomsg));
190 vnet_netlink_msg_add_rtattr (&m, IFLA_ADDRESS, mac, 6);
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100191 err = vnet_netlink_msg_send (&m);
192 if (err)
193 err = clib_error_return (0, "set link addr %U", format_clib_error, err);
194 return err;
Damjan Marion2df39092017-12-04 20:03:37 +0100195}
196
197clib_error_t *
198vnet_netlink_set_link_state (int ifindex, int up)
199{
200 vnet_netlink_msg_t m;
201 struct ifinfomsg ifmsg = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100202 clib_error_t *err = 0;
Damjan Marion2df39092017-12-04 20:03:37 +0100203
Matthew Smith81284162020-04-24 13:43:12 -0500204 ifmsg.ifi_flags = ((up) ? IFF_UP : 0);
Damjan Marion2df39092017-12-04 20:03:37 +0100205 ifmsg.ifi_change = IFF_UP;
206 ifmsg.ifi_index = ifindex;
207
208 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
209 &ifmsg, sizeof (struct ifinfomsg));
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100210 err = vnet_netlink_msg_send (&m);
211 if (err)
212 err = clib_error_return (0, "set link state %U", format_clib_error, err);
213 return err;
Damjan Marion2df39092017-12-04 20:03:37 +0100214}
215
216clib_error_t *
217vnet_netlink_set_link_mtu (int ifindex, int mtu)
218{
219 vnet_netlink_msg_t m;
220 struct ifinfomsg ifmsg = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100221 clib_error_t *err = 0;
Damjan Marion2df39092017-12-04 20:03:37 +0100222
223 ifmsg.ifi_index = ifindex;
224
Damjan Marion91c6ef72017-12-01 13:34:24 +0100225 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
226 &ifmsg, sizeof (struct ifinfomsg));
227 vnet_netlink_msg_add_rtattr (&m, IFLA_MTU, &mtu, sizeof (int));
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100228 err = vnet_netlink_msg_send (&m);
229 if (err)
230 err = clib_error_return (0, "set link mtu %U", format_clib_error, err);
231 return err;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100232}
233
234clib_error_t *
235vnet_netlink_add_ip4_addr (int ifindex, void *addr, int pfx_len)
236{
237 vnet_netlink_msg_t m;
238 struct ifaddrmsg ifa = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100239 clib_error_t *err = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100240
241 ifa.ifa_family = AF_INET;
242 ifa.ifa_prefixlen = pfx_len;
243 ifa.ifa_index = ifindex;
244
245 vnet_netlink_msg_init (&m, RTM_NEWADDR,
Dave Barach7e869472020-03-13 08:32:28 -0400246 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
Damjan Marion91c6ef72017-12-01 13:34:24 +0100247 &ifa, sizeof (struct ifaddrmsg));
248
249 vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 4);
250 vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 4);
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100251 err = vnet_netlink_msg_send (&m);
252 if (err)
253 err = clib_error_return (0, "add ip4 addr %U", format_clib_error, err);
254 return err;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100255}
256
257clib_error_t *
258vnet_netlink_add_ip6_addr (int ifindex, void *addr, int pfx_len)
259{
260 vnet_netlink_msg_t m;
261 struct ifaddrmsg ifa = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100262 clib_error_t *err = 0;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100263
264 ifa.ifa_family = AF_INET6;
265 ifa.ifa_prefixlen = pfx_len;
266 ifa.ifa_index = ifindex;
267
268 vnet_netlink_msg_init (&m, RTM_NEWADDR,
Dave Barach7e869472020-03-13 08:32:28 -0400269 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
Damjan Marion91c6ef72017-12-01 13:34:24 +0100270 &ifa, sizeof (struct ifaddrmsg));
271
272 vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 16);
273 vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 16);
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100274 err = vnet_netlink_msg_send (&m);
275 if (err)
276 err = clib_error_return (0, "add ip6 addr %U", format_clib_error, err);
277 return err;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100278}
279
Damjan Marion7866c452018-01-18 13:35:11 +0100280clib_error_t *
281vnet_netlink_add_ip4_route (void *dst, u8 dst_len, void *gw)
282{
283 vnet_netlink_msg_t m;
284 struct rtmsg rtm = { 0 };
285 u8 dflt[4] = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100286 clib_error_t *err = 0;
Damjan Marion7866c452018-01-18 13:35:11 +0100287
288 rtm.rtm_family = AF_INET;
289 rtm.rtm_table = RT_TABLE_MAIN;
290 rtm.rtm_type = RTN_UNICAST;
291 rtm.rtm_dst_len = dst_len;
292
293 vnet_netlink_msg_init (&m, RTM_NEWROUTE,
Dave Barach7e869472020-03-13 08:32:28 -0400294 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
Damjan Marion7866c452018-01-18 13:35:11 +0100295 &rtm, sizeof (struct rtmsg));
296
297 vnet_netlink_msg_add_rtattr (&m, RTA_GATEWAY, gw, 4);
298 vnet_netlink_msg_add_rtattr (&m, RTA_DST, dst ? dst : dflt, 4);
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100299 err = vnet_netlink_msg_send (&m);
300 if (err)
301 err = clib_error_return (0, "add ip4 route %U", format_clib_error, err);
302 return err;
Damjan Marion7866c452018-01-18 13:35:11 +0100303}
304
305clib_error_t *
306vnet_netlink_add_ip6_route (void *dst, u8 dst_len, void *gw)
307{
308 vnet_netlink_msg_t m;
309 struct rtmsg rtm = { 0 };
310 u8 dflt[16] = { 0 };
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100311 clib_error_t *err = 0;
Damjan Marion7866c452018-01-18 13:35:11 +0100312
313 rtm.rtm_family = AF_INET6;
314 rtm.rtm_table = RT_TABLE_MAIN;
315 rtm.rtm_type = RTN_UNICAST;
316 rtm.rtm_dst_len = dst_len;
317
318 vnet_netlink_msg_init (&m, RTM_NEWROUTE,
Dave Barach7e869472020-03-13 08:32:28 -0400319 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
Damjan Marion7866c452018-01-18 13:35:11 +0100320 &rtm, sizeof (struct rtmsg));
321
322 vnet_netlink_msg_add_rtattr (&m, RTA_GATEWAY, gw, 16);
323 vnet_netlink_msg_add_rtattr (&m, RTA_DST, dst ? dst : dflt, 16);
Mohsin Kazmib9f1f152020-01-31 12:37:44 +0100324 err = vnet_netlink_msg_send (&m);
325 if (err)
326 err = clib_error_return (0, "add ip6 route %U", format_clib_error, err);
327 return err;
Damjan Marion7866c452018-01-18 13:35:11 +0100328}
329
Damjan Marion17fdae72017-11-30 20:56:37 +0100330/*
331 * fd.io coding-style-patch-verification: ON
332 *
333 * Local Variables:
334 * eval: (c-set-style "gnu")
335 * End:
336 */