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 * |
Aloys Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 66 | vnet_netlink_msg_send (vnet_netlink_msg_t *m, vnet_netlink_msg_t **replies) |
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 | } |
Aloys Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 106 | |
| 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 Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | done: |
Steven | f953dfc | 2017-11-30 16:56:54 -0800 | [diff] [blame] | 118 | close (sock); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 119 | vec_free (m->data); |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 120 | return err; |
| 121 | } |
| 122 | |
| 123 | clib_error_t * |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 124 | vnet_netlink_set_link_name (int ifindex, char *new_ifname) |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 125 | { |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 126 | vnet_netlink_msg_t m; |
| 127 | struct ifinfomsg ifmsg = { 0 }; |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 128 | clib_error_t *err = 0; |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 129 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 130 | ifmsg.ifi_index = ifindex; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 131 | vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST, |
| 132 | &ifmsg, sizeof (struct ifinfomsg)); |
| 133 | |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 134 | vnet_netlink_msg_add_rtattr (&m, IFLA_IFNAME, new_ifname, |
| 135 | strlen (new_ifname) + 1); |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 136 | |
Aloys Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 137 | err = vnet_netlink_msg_send (&m, NULL); |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 138 | if (err) |
| 139 | err = clib_error_return (0, "set link name %U", format_clib_error, err); |
| 140 | return err; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | clib_error_t * |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 144 | 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] | 145 | { |
| 146 | vnet_netlink_msg_t m; |
| 147 | struct ifinfomsg ifmsg = { 0 }; |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 148 | clib_error_t *err = 0; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 149 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 150 | ifmsg.ifi_index = ifindex; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 151 | 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 Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 159 | err = vnet_netlink_msg_send (&m, NULL); |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 160 | if (err) |
| 161 | err = clib_error_return (0, "set link netns %U", format_clib_error, err); |
| 162 | return err; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | clib_error_t * |
| 166 | vnet_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 Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 171 | clib_error_t *err = 0; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 172 | |
| 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 Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 182 | err = vnet_netlink_msg_send (&m, NULL); |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 183 | if (err) |
| 184 | err = clib_error_return (0, "set link master %U", format_clib_error, err); |
| 185 | return err; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | clib_error_t * |
| 189 | vnet_netlink_set_link_addr (int ifindex, u8 * mac) |
| 190 | { |
| 191 | vnet_netlink_msg_t m; |
| 192 | struct ifinfomsg ifmsg = { 0 }; |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 193 | clib_error_t *err = 0; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 194 | |
| 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 Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 200 | err = vnet_netlink_msg_send (&m, NULL); |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 201 | if (err) |
| 202 | err = clib_error_return (0, "set link addr %U", format_clib_error, err); |
| 203 | return err; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | clib_error_t * |
| 207 | vnet_netlink_set_link_state (int ifindex, int up) |
| 208 | { |
| 209 | vnet_netlink_msg_t m; |
| 210 | struct ifinfomsg ifmsg = { 0 }; |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 211 | clib_error_t *err = 0; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 212 | |
Matthew Smith | 8128416 | 2020-04-24 13:43:12 -0500 | [diff] [blame] | 213 | ifmsg.ifi_flags = ((up) ? IFF_UP : 0); |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 214 | 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 Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 219 | err = vnet_netlink_msg_send (&m, NULL); |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 220 | if (err) |
| 221 | err = clib_error_return (0, "set link state %U", format_clib_error, err); |
| 222 | return err; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | clib_error_t * |
Aloys Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 226 | vnet_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; |
Aloys Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 276 | goto done; |
| 277 | } |
| 278 | offset = NLA_ALIGN (attr->nla_len); |
| 279 | len -= offset; |
| 280 | attr = (struct nlattr *) ((u8 *) attr + offset); |
| 281 | } |
| 282 | while (len > sizeof (struct nlattr)); |
| 283 | |
| 284 | /* not found */ |
| 285 | err = clib_error_return (0, "mtu not found in netlink message"); |
| 286 | |
| 287 | done: |
| 288 | vec_foreach (msg, replies) |
| 289 | { |
| 290 | vec_free (msg->data); |
| 291 | } |
| 292 | vec_free (replies); |
| 293 | |
| 294 | return err; |
| 295 | } |
| 296 | |
| 297 | clib_error_t * |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 298 | vnet_netlink_set_link_mtu (int ifindex, int mtu) |
| 299 | { |
| 300 | vnet_netlink_msg_t m; |
| 301 | struct ifinfomsg ifmsg = { 0 }; |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 302 | clib_error_t *err = 0; |
Damjan Marion | 2df3909 | 2017-12-04 20:03:37 +0100 | [diff] [blame] | 303 | |
| 304 | ifmsg.ifi_index = ifindex; |
| 305 | |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 306 | vnet_netlink_msg_init (&m, RTM_SETLINK, NLM_F_REQUEST, |
| 307 | &ifmsg, sizeof (struct ifinfomsg)); |
| 308 | vnet_netlink_msg_add_rtattr (&m, IFLA_MTU, &mtu, sizeof (int)); |
Aloys Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 309 | err = vnet_netlink_msg_send (&m, NULL); |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 310 | if (err) |
| 311 | err = clib_error_return (0, "set link mtu %U", format_clib_error, err); |
| 312 | return err; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | clib_error_t * |
| 316 | vnet_netlink_add_ip4_addr (int ifindex, void *addr, int pfx_len) |
| 317 | { |
| 318 | vnet_netlink_msg_t m; |
| 319 | struct ifaddrmsg ifa = { 0 }; |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 320 | clib_error_t *err = 0; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 321 | |
| 322 | ifa.ifa_family = AF_INET; |
| 323 | ifa.ifa_prefixlen = pfx_len; |
| 324 | ifa.ifa_index = ifindex; |
| 325 | |
| 326 | vnet_netlink_msg_init (&m, RTM_NEWADDR, |
Dave Barach | 7e86947 | 2020-03-13 08:32:28 -0400 | [diff] [blame] | 327 | NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE, |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 328 | &ifa, sizeof (struct ifaddrmsg)); |
| 329 | |
| 330 | vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 4); |
| 331 | vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 4); |
Aloys Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 332 | err = vnet_netlink_msg_send (&m, NULL); |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 333 | if (err) |
| 334 | err = clib_error_return (0, "add ip4 addr %U", format_clib_error, err); |
| 335 | return err; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | clib_error_t * |
| 339 | vnet_netlink_add_ip6_addr (int ifindex, void *addr, int pfx_len) |
| 340 | { |
| 341 | vnet_netlink_msg_t m; |
| 342 | struct ifaddrmsg ifa = { 0 }; |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 343 | clib_error_t *err = 0; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 344 | |
| 345 | ifa.ifa_family = AF_INET6; |
| 346 | ifa.ifa_prefixlen = pfx_len; |
| 347 | ifa.ifa_index = ifindex; |
| 348 | |
| 349 | vnet_netlink_msg_init (&m, RTM_NEWADDR, |
Dave Barach | 7e86947 | 2020-03-13 08:32:28 -0400 | [diff] [blame] | 350 | NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE, |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 351 | &ifa, sizeof (struct ifaddrmsg)); |
| 352 | |
| 353 | vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 16); |
| 354 | vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 16); |
Aloys Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 355 | err = vnet_netlink_msg_send (&m, NULL); |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 356 | if (err) |
| 357 | err = clib_error_return (0, "add ip6 addr %U", format_clib_error, err); |
| 358 | return err; |
Damjan Marion | 91c6ef7 | 2017-12-01 13:34:24 +0100 | [diff] [blame] | 359 | } |
| 360 | |
Damjan Marion | 7866c45 | 2018-01-18 13:35:11 +0100 | [diff] [blame] | 361 | clib_error_t * |
| 362 | vnet_netlink_add_ip4_route (void *dst, u8 dst_len, void *gw) |
| 363 | { |
| 364 | vnet_netlink_msg_t m; |
| 365 | struct rtmsg rtm = { 0 }; |
| 366 | u8 dflt[4] = { 0 }; |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 367 | clib_error_t *err = 0; |
Damjan Marion | 7866c45 | 2018-01-18 13:35:11 +0100 | [diff] [blame] | 368 | |
| 369 | rtm.rtm_family = AF_INET; |
| 370 | rtm.rtm_table = RT_TABLE_MAIN; |
| 371 | rtm.rtm_type = RTN_UNICAST; |
| 372 | rtm.rtm_dst_len = dst_len; |
| 373 | |
| 374 | vnet_netlink_msg_init (&m, RTM_NEWROUTE, |
Dave Barach | 7e86947 | 2020-03-13 08:32:28 -0400 | [diff] [blame] | 375 | NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE, |
Damjan Marion | 7866c45 | 2018-01-18 13:35:11 +0100 | [diff] [blame] | 376 | &rtm, sizeof (struct rtmsg)); |
| 377 | |
| 378 | vnet_netlink_msg_add_rtattr (&m, RTA_GATEWAY, gw, 4); |
| 379 | vnet_netlink_msg_add_rtattr (&m, RTA_DST, dst ? dst : dflt, 4); |
Aloys Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 380 | err = vnet_netlink_msg_send (&m, NULL); |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 381 | if (err) |
| 382 | err = clib_error_return (0, "add ip4 route %U", format_clib_error, err); |
| 383 | return err; |
Damjan Marion | 7866c45 | 2018-01-18 13:35:11 +0100 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | clib_error_t * |
| 387 | vnet_netlink_add_ip6_route (void *dst, u8 dst_len, void *gw) |
| 388 | { |
| 389 | vnet_netlink_msg_t m; |
| 390 | struct rtmsg rtm = { 0 }; |
| 391 | u8 dflt[16] = { 0 }; |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 392 | clib_error_t *err = 0; |
Damjan Marion | 7866c45 | 2018-01-18 13:35:11 +0100 | [diff] [blame] | 393 | |
| 394 | rtm.rtm_family = AF_INET6; |
| 395 | rtm.rtm_table = RT_TABLE_MAIN; |
| 396 | rtm.rtm_type = RTN_UNICAST; |
| 397 | rtm.rtm_dst_len = dst_len; |
| 398 | |
| 399 | vnet_netlink_msg_init (&m, RTM_NEWROUTE, |
Dave Barach | 7e86947 | 2020-03-13 08:32:28 -0400 | [diff] [blame] | 400 | NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE, |
Damjan Marion | 7866c45 | 2018-01-18 13:35:11 +0100 | [diff] [blame] | 401 | &rtm, sizeof (struct rtmsg)); |
| 402 | |
| 403 | vnet_netlink_msg_add_rtattr (&m, RTA_GATEWAY, gw, 16); |
| 404 | vnet_netlink_msg_add_rtattr (&m, RTA_DST, dst ? dst : dflt, 16); |
Aloys Augustin | e39376e | 2021-03-29 22:08:09 +0200 | [diff] [blame] | 405 | err = vnet_netlink_msg_send (&m, NULL); |
Mohsin Kazmi | b9f1f15 | 2020-01-31 12:37:44 +0100 | [diff] [blame] | 406 | if (err) |
| 407 | err = clib_error_return (0, "add ip6 route %U", format_clib_error, err); |
| 408 | return err; |
Damjan Marion | 7866c45 | 2018-01-18 13:35:11 +0100 | [diff] [blame] | 409 | } |
| 410 | |
Pim van Pelt | 1705a6b | 2021-09-09 17:53:09 +0000 | [diff] [blame] | 411 | clib_error_t * |
| 412 | vnet_netlink_del_ip4_addr (int ifindex, void *addr, int pfx_len) |
| 413 | { |
| 414 | vnet_netlink_msg_t m; |
| 415 | struct ifaddrmsg ifa = { 0 }; |
| 416 | clib_error_t *err = 0; |
| 417 | |
| 418 | ifa.ifa_family = AF_INET; |
| 419 | ifa.ifa_prefixlen = pfx_len; |
| 420 | ifa.ifa_index = ifindex; |
| 421 | |
| 422 | vnet_netlink_msg_init (&m, RTM_DELADDR, NLM_F_REQUEST, &ifa, |
| 423 | sizeof (struct ifaddrmsg)); |
| 424 | |
| 425 | vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 4); |
| 426 | vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 4); |
| 427 | err = vnet_netlink_msg_send (&m, NULL); |
| 428 | if (err) |
| 429 | err = clib_error_return (0, "del ip4 addr %U", format_clib_error, err); |
| 430 | return err; |
| 431 | } |
| 432 | |
| 433 | clib_error_t * |
| 434 | vnet_netlink_del_ip6_addr (int ifindex, void *addr, int pfx_len) |
| 435 | { |
| 436 | vnet_netlink_msg_t m; |
| 437 | struct ifaddrmsg ifa = { 0 }; |
| 438 | clib_error_t *err = 0; |
| 439 | |
| 440 | ifa.ifa_family = AF_INET6; |
| 441 | ifa.ifa_prefixlen = pfx_len; |
| 442 | ifa.ifa_index = ifindex; |
| 443 | |
| 444 | vnet_netlink_msg_init (&m, RTM_DELADDR, NLM_F_REQUEST, &ifa, |
| 445 | sizeof (struct ifaddrmsg)); |
| 446 | |
| 447 | vnet_netlink_msg_add_rtattr (&m, IFA_LOCAL, addr, 16); |
| 448 | vnet_netlink_msg_add_rtattr (&m, IFA_ADDRESS, addr, 16); |
| 449 | err = vnet_netlink_msg_send (&m, NULL); |
| 450 | if (err) |
| 451 | err = clib_error_return (0, "del ip6 addr %U", format_clib_error, err); |
| 452 | return err; |
| 453 | } |
| 454 | |
Damjan Marion | 17fdae7 | 2017-11-30 20:56:37 +0100 | [diff] [blame] | 455 | /* |
| 456 | * fd.io coding-style-patch-verification: ON |
| 457 | * |
| 458 | * Local Variables: |
| 459 | * eval: (c-set-style "gnu") |
| 460 | * End: |
| 461 | */ |