Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 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> |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 22 | |
| 23 | #include <linux/netlink.h> |
| 24 | #include <linux/rtnetlink.h> |
| 25 | |
| 26 | #include <vlib/vlib.h> |
| 27 | #include <vlib/unix/unix.h> |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 28 | #include <vnet/devices/netlink.h> |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 29 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 30 | typedef struct |
| 31 | { |
| 32 | u8 *data; |
| 33 | } vnet_netlink_msg_t; |
| 34 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 35 | static void |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 36 | vnet_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 Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 41 | clib_memset (m, 0, sizeof (vnet_netlink_msg_t)); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 42 | vec_add2 (m->data, p, NLMSG_SPACE (msg_len)); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 43 | ASSERT (m->data == p); |
| 44 | |
| 45 | nh = (struct nlmsghdr *) p; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 46 | nh->nlmsg_flags = flags | NLM_F_ACK; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 47 | nh->nlmsg_type = type; |
| 48 | clib_memcpy (m->data + sizeof (struct nlmsghdr), msg_data, msg_len); |
| 49 | } |
| 50 | |
| 51 | static void |
| 52 | vnet_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 Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 58 | vec_add2 (m->data, p, RTA_SPACE (rta_data_len)); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 59 | 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 | |
| 65 | static clib_error_t * |
| 66 | vnet_netlink_msg_send (vnet_netlink_msg_t * m) |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 67 | { |
| 68 | clib_error_t *err = 0; |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 69 | struct sockaddr_nl ra = { 0 }; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 70 | int len, sock; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 71 | struct nlmsghdr *nh = (struct nlmsghdr *) m->data; |
| 72 | nh->nlmsg_len = vec_len (m->data); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 73 | char buf[4096]; |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 74 | |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 75 | if ((sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1) |
Steven | f953dfc | 2017-11-30 16:56:54 -0800 | [diff] [blame] | 76 | return clib_error_return_unix (0, "socket(AF_NETLINK)"); |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 77 | |
| 78 | ra.nl_family = AF_NETLINK; |
Jon Loeliger | 258fefd | 2018-03-02 13:05:12 -0600 | [diff] [blame] | 79 | ra.nl_pid = 0; |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 80 | |
| 81 | if ((bind (sock, (struct sockaddr *) &ra, sizeof (ra))) == -1) |
Steven | f953dfc | 2017-11-30 16:56:54 -0800 | [diff] [blame] | 82 | { |
| 83 | err = clib_error_return_unix (0, "bind"); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 84 | goto done; |
Steven | f953dfc | 2017-11-30 16:56:54 -0800 | [diff] [blame] | 85 | } |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 86 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 87 | if ((send (sock, m->data, vec_len (m->data), 0)) == -1) |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 88 | err = clib_error_return_unix (0, "send"); |
| 89 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 90 | 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 | |
| 108 | done: |
Steven | f953dfc | 2017-11-30 16:56:54 -0800 | [diff] [blame] | 109 | close (sock); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 110 | vec_free (m->data); |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 111 | return err; |
| 112 | } |
| 113 | |
| 114 | clib_error_t * |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 115 | vnet_netlink_set_link_name (int ifindex, char *new_ifname) |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 116 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 117 | vnet_netlink_msg_t m; |
| 118 | struct ifinfomsg ifmsg = { 0 }; |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 119 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 120 | ifmsg.ifi_index = ifindex; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 121 | vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST, |
| 122 | &ifmsg, sizeof (struct ifinfomsg)); |
| 123 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 124 | vnet_netlink_msg_add_rtattr (&m, IFLA_IFNAME, new_ifname, |
| 125 | strlen (new_ifname) + 1); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 126 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 127 | return vnet_netlink_msg_send (&m); |
| 128 | } |
| 129 | |
| 130 | clib_error_t * |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 131 | vnet_netlink_set_link_netns (int ifindex, int netns_fd, char *new_ifname) |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 132 | { |
| 133 | vnet_netlink_msg_t m; |
| 134 | struct ifinfomsg ifmsg = { 0 }; |
| 135 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 136 | ifmsg.ifi_index = ifindex; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 137 | 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 | |
| 148 | clib_error_t * |
| 149 | vnet_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 | |
| 167 | clib_error_t * |
| 168 | vnet_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 | |
| 181 | clib_error_t * |
| 182 | vnet_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 | |
| 196 | clib_error_t * |
| 197 | vnet_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 Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 204 | 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 | |
| 210 | clib_error_t * |
| 211 | vnet_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 | |
| 229 | clib_error_t * |
| 230 | vnet_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 Marion | 7866c45 | 2018-01-18 13:35:11 +0100 | [diff] [blame] | 248 | clib_error_t * |
| 249 | vnet_netlink_add_ip4_route (void *dst, u8 dst_len, void *gw) |
| 250 | { |
| 251 | vnet_netlink_msg_t m; |
| 252 | struct rtmsg rtm = { 0 }; |
| 253 | u8 dflt[4] = { 0 }; |
| 254 | |
| 255 | rtm.rtm_family = AF_INET; |
| 256 | rtm.rtm_table = RT_TABLE_MAIN; |
| 257 | rtm.rtm_type = RTN_UNICAST; |
| 258 | rtm.rtm_dst_len = dst_len; |
| 259 | |
| 260 | vnet_netlink_msg_init (&m, RTM_NEWROUTE, |
| 261 | NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL, |
| 262 | &rtm, sizeof (struct rtmsg)); |
| 263 | |
| 264 | vnet_netlink_msg_add_rtattr (&m, RTA_GATEWAY, gw, 4); |
| 265 | vnet_netlink_msg_add_rtattr (&m, RTA_DST, dst ? dst : dflt, 4); |
| 266 | return vnet_netlink_msg_send (&m); |
| 267 | } |
| 268 | |
| 269 | clib_error_t * |
| 270 | vnet_netlink_add_ip6_route (void *dst, u8 dst_len, void *gw) |
| 271 | { |
| 272 | vnet_netlink_msg_t m; |
| 273 | struct rtmsg rtm = { 0 }; |
| 274 | u8 dflt[16] = { 0 }; |
| 275 | |
| 276 | rtm.rtm_family = AF_INET6; |
| 277 | rtm.rtm_table = RT_TABLE_MAIN; |
| 278 | rtm.rtm_type = RTN_UNICAST; |
| 279 | rtm.rtm_dst_len = dst_len; |
| 280 | |
| 281 | vnet_netlink_msg_init (&m, RTM_NEWROUTE, |
| 282 | NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL, |
| 283 | &rtm, sizeof (struct rtmsg)); |
| 284 | |
| 285 | vnet_netlink_msg_add_rtattr (&m, RTA_GATEWAY, gw, 16); |
| 286 | vnet_netlink_msg_add_rtattr (&m, RTA_DST, dst ? dst : dflt, 16); |
| 287 | return vnet_netlink_msg_send (&m); |
| 288 | } |
| 289 | |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 290 | /* |
| 291 | * fd.io coding-style-patch-verification: ON |
| 292 | * |
| 293 | * Local Variables: |
| 294 | * eval: (c-set-style "gnu") |
| 295 | * End: |
| 296 | */ |