18 #include <sys/types.h>
23 #include <linux/netlink.h>
24 #include <linux/rtnetlink.h>
37 void *msg_data,
int msg_len)
45 nh = (
struct nlmsghdr *) p;
46 nh->nlmsg_flags =
flags | NLM_F_ACK;
53 void *rta_data,
int rta_data_len)
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);
69 struct sockaddr_nl ra = { 0 };
71 struct nlmsghdr *
nh = (
struct nlmsghdr *) m->
data;
75 if ((sock = socket (AF_NETLINK, SOCK_RAW, NETLINK_ROUTE)) == -1)
78 ra.nl_family = AF_NETLINK;
81 if ((bind (sock, (
struct sockaddr *) &ra,
sizeof (ra))) == -1)
90 if ((
len = recv (sock,
buf,
sizeof (
buf), 0)) == -1)
93 for (
nh = (
struct nlmsghdr *)
buf; NLMSG_OK (
nh,
len);
96 if (
nh->nlmsg_type == NLMSG_DONE)
99 if (
nh->nlmsg_type == NLMSG_ERROR)
101 struct nlmsgerr *e = (
struct nlmsgerr *) NLMSG_DATA (
nh);
127 struct ifinfomsg ifmsg = { 0 };
130 ifmsg.ifi_index = ifindex;
132 &ifmsg,
sizeof (
struct ifinfomsg));
135 strlen (new_ifname) + 1);
147 struct ifinfomsg ifmsg = { 0 };
150 ifmsg.ifi_index = ifindex;
152 &ifmsg,
sizeof (
struct ifinfomsg));
157 strlen (new_ifname) + 1);
169 struct ifinfomsg ifmsg = { 0 };
173 ifmsg.ifi_index = ifindex;
180 &ifmsg,
sizeof (
struct ifinfomsg));
192 struct ifinfomsg ifmsg = { 0 };
195 ifmsg.ifi_index = ifindex;
198 &ifmsg,
sizeof (
struct ifinfomsg));
210 struct ifinfomsg ifmsg = { 0 };
213 ifmsg.ifi_flags = ((up) ? IFF_UP : 0);
214 ifmsg.ifi_change = IFF_UP;
215 ifmsg.ifi_index = ifindex;
218 &ifmsg,
sizeof (
struct ifinfomsg));
229 struct ifinfomsg ifmsg = { 0 };
236 ifmsg.ifi_index = ifindex;
239 sizeof (
struct ifinfomsg));
255 struct nlmsghdr *
nh = (
struct nlmsghdr *) replies[0].
data;
256 if (
nh->nlmsg_type != RTM_NEWLINK)
259 0,
"netlink reply has wrong type: %d != RTM_NEWLINK",
nh->nlmsg_type);
263 offset = NLMSG_HDRLEN + NLMSG_ALIGN (
sizeof (
struct ifinfomsg));
264 attr = (
struct nlattr *) ((
u8 *)
nh +
offset);
269 if ((attr->nla_type & NLA_TYPE_MASK) == IFLA_MTU)
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);
279 offset = NLA_ALIGN (attr->nla_len);
281 attr = (
struct nlattr *) ((
u8 *) attr +
offset);
283 while (
len >
sizeof (
struct nlattr));
302 struct ifinfomsg ifmsg = { 0 };
305 ifmsg.ifi_index = ifindex;
308 &ifmsg,
sizeof (
struct ifinfomsg));
320 struct ifaddrmsg ifa = { 0 };
323 ifa.ifa_family = AF_INET;
324 ifa.ifa_prefixlen = pfx_len;
325 ifa.ifa_index = ifindex;
328 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
329 &ifa,
sizeof (
struct ifaddrmsg));
343 struct ifaddrmsg ifa = { 0 };
346 ifa.ifa_family = AF_INET6;
347 ifa.ifa_prefixlen = pfx_len;
348 ifa.ifa_index = ifindex;
351 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
352 &ifa,
sizeof (
struct ifaddrmsg));
366 struct rtmsg rtm = { 0 };
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;
376 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
377 &rtm,
sizeof (
struct rtmsg));
391 struct rtmsg rtm = { 0 };
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;
401 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE,
402 &rtm,
sizeof (
struct rtmsg));