+int odhcpd_setup_route(const struct in6_addr *addr, const int prefixlen,
+ const struct interface *iface, const struct in6_addr *gw,
+ const uint32_t metric, const bool add)
+{
+ struct nl_msg *msg;
+ struct rtmsg rtm = {
+ .rtm_family = AF_INET6,
+ .rtm_dst_len = prefixlen,
+ .rtm_src_len = 0,
+ .rtm_table = RT_TABLE_MAIN,
+ .rtm_protocol = (add ? RTPROT_STATIC : RTPROT_UNSPEC),
+ .rtm_scope = (add ? (gw ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK) : RT_SCOPE_NOWHERE),
+ .rtm_type = (add ? RTN_UNICAST : RTN_UNSPEC),
+ };
+ int ret = 0;
+
+ msg = nlmsg_alloc_simple(add ? RTM_NEWROUTE : RTM_DELROUTE,
+ add ? NLM_F_CREATE | NLM_F_REPLACE : 0);
+ if (!msg)
+ return -1;
+
+ nlmsg_append(msg, &rtm, sizeof(rtm), 0);
+
+ nla_put(msg, RTA_DST, sizeof(*addr), addr);
+ nla_put_u32(msg, RTA_OIF, iface->ifindex);
+ nla_put_u32(msg, RTA_PRIORITY, metric);
+
+ if (gw)
+ nla_put(msg, RTA_GATEWAY, sizeof(*gw), gw);
+
+ ret = nl_send_auto_complete(rtnl_socket, msg);
+ nlmsg_free(msg);
+
+ if (ret < 0)
+ return ret;
+
+ return nl_wait_for_ack(rtnl_socket);
+}
+
+int odhcpd_setup_proxy_neigh(const struct in6_addr *addr,
+ const struct interface *iface, const bool add)
+{
+ struct nl_msg *msg;
+ struct ndmsg ndm = {
+ .ndm_family = AF_INET6,
+ .ndm_flags = NTF_PROXY,
+ .ndm_ifindex = iface->ifindex,
+ };
+ int ret = 0, flags = NLM_F_REQUEST;
+
+ if (add)
+ flags |= NLM_F_REPLACE | NLM_F_CREATE;
+
+ msg = nlmsg_alloc_simple(add ? RTM_NEWNEIGH : RTM_DELNEIGH, flags);
+ if (!msg)
+ return -1;
+
+ nlmsg_append(msg, &ndm, sizeof(ndm), 0);
+
+ nla_put(msg, NDA_DST, sizeof(*addr), addr);
+
+ ret = nl_send_auto_complete(rtnl_socket, msg);
+ nlmsg_free(msg);
+
+ if (ret < 0)
+ return ret;
+
+ return nl_wait_for_ack(rtnl_socket);
+}
+
+int odhcpd_setup_addr(struct odhcpd_ipaddr *addr,
+ const struct interface *iface, const bool v6,
+ const bool add)
+{
+ struct nl_msg *msg;
+ struct ifaddrmsg ifa = {
+ .ifa_family = v6 ? AF_INET6 : AF_INET,
+ .ifa_prefixlen = addr->prefix,
+ .ifa_flags = 0,
+ .ifa_scope = 0,
+ .ifa_index = iface->ifindex, };
+ int ret = 0, flags = NLM_F_REQUEST;
+
+ if (add)
+ flags |= NLM_F_REPLACE | NLM_F_CREATE;
+
+ msg = nlmsg_alloc_simple(add ? RTM_NEWADDR : RTM_DELADDR, 0);
+ if (!msg)
+ return -1;
+
+ nlmsg_append(msg, &ifa, sizeof(ifa), flags);
+ nla_put(msg, IFA_LOCAL, v6 ? 16 : 4, &addr->addr);
+ if (v6) {
+ struct ifa_cacheinfo cinfo = { .ifa_prefered = 0xffffffffU,
+ .ifa_valid = 0xffffffffU,
+ .cstamp = 0,
+ .tstamp = 0 };
+ time_t now = odhcpd_time();
+
+ if (addr->preferred) {
+ int64_t preferred = addr->preferred - now;
+ if (preferred < 0)
+ preferred = 0;
+ else if (preferred > UINT32_MAX)
+ preferred = UINT32_MAX;
+
+ cinfo.ifa_prefered = preferred;
+ }
+
+ if (addr->valid) {
+ int64_t valid = addr->valid - now;
+ if (valid <= 0) {
+ nlmsg_free(msg);
+ return -1;
+ }
+ else if (valid > UINT32_MAX)
+ valid = UINT32_MAX;
+
+ cinfo.ifa_valid = valid;
+ }
+
+ nla_put(msg, IFA_CACHEINFO, sizeof(cinfo), &cinfo);
+
+ nla_put_u32(msg, IFA_FLAGS, IFA_F_NOPREFIXROUTE);
+ } else {
+ if (addr->broadcast.s_addr)
+ nla_put_u32(msg, IFA_BROADCAST, addr->broadcast.s_addr);
+ }
+
+ ret = nl_send_auto_complete(rtnl_socket, msg);
+ nlmsg_free(msg);
+
+ if (ret < 0)
+ return ret;
+
+ return nl_wait_for_ack(rtnl_socket);
+}