blob: 5994366007c2b3827f275f8c4be3ebf16d4bdac4 [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;
Damjan Marion91c6ef72017-12-01 13:34:24 +010041 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;
79 ra.nl_pid = getpid ();
80
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 };
Damjan Marion17fdae72017-11-30 20:56:37 +0100119
Damjan Marion91c6ef72017-12-01 13:34:24 +0100120 ifmsg.ifi_index = ifindex;
Damjan Marion91c6ef72017-12-01 13:34:24 +0100121 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
122 &ifmsg, sizeof (struct ifinfomsg));
123
Damjan Marion2df39092017-12-04 20:03:37 +0100124 vnet_netlink_msg_add_rtattr (&m, IFLA_IFNAME, new_ifname,
125 strlen (new_ifname) + 1);
Damjan Marion91c6ef72017-12-01 13:34:24 +0100126
Damjan Marion91c6ef72017-12-01 13:34:24 +0100127 return vnet_netlink_msg_send (&m);
128}
129
130clib_error_t *
Damjan Marion2df39092017-12-04 20:03:37 +0100131vnet_netlink_set_link_netns (int ifindex, int netns_fd, char *new_ifname)
Damjan Marion91c6ef72017-12-01 13:34:24 +0100132{
133 vnet_netlink_msg_t m;
134 struct ifinfomsg ifmsg = { 0 };
135
Damjan Marion91c6ef72017-12-01 13:34:24 +0100136 ifmsg.ifi_index = ifindex;
Damjan Marion2df39092017-12-04 20:03:37 +0100137 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
138 &ifmsg, sizeof (struct ifinfomsg));
139
140 vnet_netlink_msg_add_rtattr (&m, IFLA_NET_NS_FD, &netns_fd, sizeof (int));
141 if (new_ifname)
142 vnet_netlink_msg_add_rtattr (&m, IFLA_IFNAME, new_ifname,
143 strlen (new_ifname) + 1);
144
145 return vnet_netlink_msg_send (&m);
146}
147
148clib_error_t *
149vnet_netlink_set_link_master (int ifindex, char *master_ifname)
150{
151 vnet_netlink_msg_t m;
152 struct ifinfomsg ifmsg = { 0 };
153 int i;
154
155 ifmsg.ifi_index = ifindex;
156
157 if ((i = if_nametoindex (master_ifname)) == 0)
158 clib_error_return_unix (0, "unknown master interface '%s'",
159 master_ifname);
160
161 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
162 &ifmsg, sizeof (struct ifinfomsg));
163 vnet_netlink_msg_add_rtattr (&m, IFLA_MASTER, &i, sizeof (int));
164 return vnet_netlink_msg_send (&m);
165}
166
167clib_error_t *
168vnet_netlink_set_link_addr (int ifindex, u8 * mac)
169{
170 vnet_netlink_msg_t m;
171 struct ifinfomsg ifmsg = { 0 };
172
173 ifmsg.ifi_index = ifindex;
174
175 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
176 &ifmsg, sizeof (struct ifinfomsg));
177 vnet_netlink_msg_add_rtattr (&m, IFLA_ADDRESS, mac, 6);
178 return vnet_netlink_msg_send (&m);
179}
180
181clib_error_t *
182vnet_netlink_set_link_state (int ifindex, int up)
183{
184 vnet_netlink_msg_t m;
185 struct ifinfomsg ifmsg = { 0 };
186
187 ifmsg.ifi_flags = IFF_UP;
188 ifmsg.ifi_change = IFF_UP;
189 ifmsg.ifi_index = ifindex;
190
191 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
192 &ifmsg, sizeof (struct ifinfomsg));
193 return vnet_netlink_msg_send (&m);
194}
195
196clib_error_t *
197vnet_netlink_set_link_mtu (int ifindex, int mtu)
198{
199 vnet_netlink_msg_t m;
200 struct ifinfomsg ifmsg = { 0 };
201
202 ifmsg.ifi_index = ifindex;
203
Damjan Marion91c6ef72017-12-01 13:34:24 +0100204 vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST,
205 &ifmsg, sizeof (struct ifinfomsg));
206 vnet_netlink_msg_add_rtattr (&m, IFLA_MTU, &mtu, sizeof (int));
207 return vnet_netlink_msg_send (&m);
208}
209
210clib_error_t *
211vnet_netlink_add_ip4_addr (int ifindex, void *addr, int pfx_len)
212{
213 vnet_netlink_msg_t m;
214 struct ifaddrmsg ifa = { 0 };
215
216 ifa.ifa_family = AF_INET;
217 ifa.ifa_prefixlen = pfx_len;
218 ifa.ifa_index = ifindex;
219
220 vnet_netlink_msg_init (&m, RTM_NEWADDR,
221 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL,
222 &ifa, sizeof (struct ifaddrmsg));
223
224 vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 4);
225 vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 4);
226 return vnet_netlink_msg_send (&m);
227}
228
229clib_error_t *
230vnet_netlink_add_ip6_addr (int ifindex, void *addr, int pfx_len)
231{
232 vnet_netlink_msg_t m;
233 struct ifaddrmsg ifa = { 0 };
234
235 ifa.ifa_family = AF_INET6;
236 ifa.ifa_prefixlen = pfx_len;
237 ifa.ifa_index = ifindex;
238
239 vnet_netlink_msg_init (&m, RTM_NEWADDR,
240 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL,
241 &ifa, sizeof (struct ifaddrmsg));
242
243 vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 16);
244 vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 16);
245 return vnet_netlink_msg_send (&m);
246}
247
Damjan Marion17fdae72017-11-30 20:56:37 +0100248/*
249 * fd.io coding-style-patch-verification: ON
250 *
251 * Local Variables:
252 * eval: (c-set-style "gnu")
253 * End:
254 */