Skip to content

Commit 533bb17

Browse files
Add netlink functions to delete Ip address
Signed-off-by: francescomessina <francescomessina92@hotmail.com>
1 parent c185ed3 commit 533bb17

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

src/polycubed/src/utils/netlink.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,26 @@ struct nlmsghdr* Netlink::netlink_ip_alloc() {
668668
return nlmsg;
669669
}
670670

671+
struct nlmsghdr* Netlink::netlink_ip_dealloc() {
672+
size_t len = NLMSG_ALIGN(SIZE_ALIGN) + NLMSG_ALIGN(sizeof(struct nlmsghdr *));
673+
struct nlmsghdr *nlmsg = (struct nlmsghdr *) malloc(len);
674+
memset(nlmsg, 0, len);
675+
676+
struct nl_ipreq *uni = (struct nl_ipreq *)nlmsg;
677+
uni->ifaddrmsg.ifa_family = AF_INET;
678+
uni->ifaddrmsg.ifa_scope = 0;
679+
680+
nlmsg->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
681+
nlmsg->nlmsg_type = RTM_DELADDR;
682+
// NLM_F_REQUEST Must be set on all request messages
683+
// NLM_F_ACK Request for an acknowledgment on success
684+
// NLM_F_CREATE Create object if it doesn't already exist
685+
// NLM_F_EXCL Don't replace if the object already exists
686+
nlmsg->nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK|NLM_F_CREATE|NLM_F_EXCL;
687+
688+
return nlmsg;
689+
}
690+
671691
struct nlmsghdr* Netlink::netlink_ipv6_alloc() {
672692
size_t len = NLMSG_ALIGN(SIZE_ALIGN) + NLMSG_ALIGN(sizeof(struct nlmsghdr *));
673693
struct nlmsghdr *nlmsg = (struct nlmsghdr *) malloc(len);
@@ -845,6 +865,42 @@ void Netlink::set_iface_ip(const std::string &iface, const std::string &ip, int
845865
netlink_nl_send(nlmsg);
846866
}
847867

868+
void Netlink::unset_iface_ip(const std::string &iface, const std::string &ip, int prefix) {
869+
struct nlmsghdr *nlmsg = netlink_ip_dealloc();
870+
struct nl_ipreq *uni = (struct nl_ipreq *)nlmsg;
871+
struct rtattr *rta;
872+
struct in_addr ia;
873+
874+
int index = get_iface_index(iface);
875+
if (index == -1) {
876+
logger->error("unset_iface_ip: iface {0} does not exist", iface);
877+
throw std::runtime_error("unset_iface_ip: iface does not exist");
878+
}
879+
880+
uni->ifaddrmsg.ifa_index = index;
881+
uni->ifaddrmsg.ifa_prefixlen = prefix;
882+
883+
if (inet_pton(AF_INET, ip.c_str(), &ia) <= 0) {
884+
free(nlmsg);
885+
logger->error("unset_iface_ip: Error in inet_pton");
886+
throw std::runtime_error("unset_iface_ip: Error in inet_pton");
887+
}
888+
889+
rta = NLMSG_TAIL(nlmsg);
890+
rta->rta_type = IFA_LOCAL;
891+
rta->rta_len = RTA_LENGTH(sizeof(struct in_addr));
892+
memcpy(RTA_DATA(rta), &ia, sizeof(struct in_addr));
893+
nlmsg->nlmsg_len = NLMSG_ALIGN(nlmsg->nlmsg_len) + RTA_ALIGN(rta->rta_len);
894+
895+
rta = NLMSG_TAIL(nlmsg);
896+
rta->rta_type = IFA_ADDRESS;
897+
rta->rta_len = RTA_LENGTH(sizeof(struct in_addr));
898+
memcpy(RTA_DATA(rta), &ia, sizeof(struct in_addr));
899+
nlmsg->nlmsg_len = NLMSG_ALIGN(nlmsg->nlmsg_len) + RTA_ALIGN(rta->rta_len);
900+
901+
netlink_nl_send(nlmsg);
902+
}
903+
848904
void Netlink::set_iface_ipv6(const std::string &iface, const std::string &ipv6) {
849905
struct nlmsghdr *nlmsg = netlink_ipv6_alloc();
850906
struct nl_ipreq *uni = (struct nl_ipreq *)nlmsg;

src/polycubed/src/utils/netlink.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class Netlink {
8080
void set_iface_status(const std::string &iface, IFACE_STATUS status);
8181
void set_iface_mac(const std::string &iface, const std::string &mac);
8282
void set_iface_ip(const std::string &iface, const std::string &ip, int prefix);
83+
void unset_iface_ip(const std::string &iface, const std::string &ip, int prefix);
8384
void set_iface_ipv6(const std::string &iface, const std::string &ip);
8485
void move_iface_into_ns(const std::string &iface, int fd);
8586

@@ -123,6 +124,7 @@ class Netlink {
123124

124125
struct nlmsghdr* netlink_alloc();
125126
struct nlmsghdr* netlink_ip_alloc();
127+
struct nlmsghdr* netlink_ip_dealloc();
126128
struct nlmsghdr* netlink_ipv6_alloc();
127129
int netlink_nl_send(struct nlmsghdr *nlmsg);
128130

0 commit comments

Comments
 (0)