#include <sys/cdefs.h>
#include "opt_inet.h"
#include "opt_inet6.h"
#include <sys/types.h>
#include <sys/malloc.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/syslog.h>
#include <sys/socketvar.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_var.h>
#include <net/if_clone.h>
#include <net/if_vlan_var.h>
#include <net/route.h>
#include <net/route/nhop.h>
#include <net/route/route_ctl.h>
#include <netlink/netlink.h>
#include <netlink/netlink_ctl.h>
#include <netlink/netlink_route.h>
#include <netlink/route/route_var.h>
#include <netinet6/scope6_var.h>
#define DEBUG_MOD_NAME nl_iface_drivers
#define DEBUG_MAX_LEVEL LOG_DEBUG3
#include <netlink/netlink_debug.h>
_DECLARE_DEBUG(LOG_INFO);
int
_nl_modify_ifp_generic(struct ifnet *ifp, struct nl_parsed_link *lattrs,
const struct nlattr_bmask *bm, struct nl_pstate *npt)
{
int error;
if (lattrs->ifla_ifalias != NULL) {
if (!nlp_has_priv(npt->nlp, PRIV_NET_SETIFDESCR)) {
nlmsg_report_err_msg(npt, "Not enough privileges to set descr");
return (EPERM);
}
int len = strlen(lattrs->ifla_ifalias) + 1;
char *buf = if_allocdescr(len, M_WAITOK);
memcpy(buf, lattrs->ifla_ifalias, len);
if_setdescr(ifp, buf);
if_setlastchange(ifp);
}
if ((lattrs->ifi_change & IFF_UP) != 0 || lattrs->ifi_change == 0) {
if (!nlp_has_priv(npt->nlp, PRIV_NET_SETIFFLAGS)) {
nlmsg_report_err_msg(npt, "Not enough privileges to set flags");
return (EPERM);
}
if (lattrs->ifi_flags & IFF_UP)
if_up(ifp);
else
if_down(ifp);
}
if (lattrs->ifla_mtu > 0) {
if (!nlp_has_priv(npt->nlp, PRIV_NET_SETIFMTU)) {
nlmsg_report_err_msg(npt, "Not enough privileges to set mtu");
return (EPERM);
}
struct ifreq ifr = { .ifr_mtu = lattrs->ifla_mtu };
error = ifhwioctl(SIOCSIFMTU, ifp, (char *)&ifr,
curthread);
if (error != 0) {
nlmsg_report_err_msg(npt, "Failed to set mtu");
return (error);
}
}
if ((lattrs->ifi_change & IFF_PROMISC) != 0 ||
lattrs->ifi_change == 0) {
if (!nlp_has_priv(npt->nlp, PRIV_NET_SETIFFLAGS)) {
nlmsg_report_err_msg(npt, "Not enough privileges to set promisc");
return (EPERM);
}
if_setppromisc(ifp, (lattrs->ifi_flags & IFF_PROMISC) != 0);
}
if (lattrs->ifla_address != NULL) {
if (!nlp_has_priv(npt->nlp, PRIV_NET_SETIFMAC)) {
nlmsg_report_err_msg(npt,
"Not enough privileges to set IFLA_ADDRESS");
return (EPERM);
}
error = if_setlladdr(ifp,
NLA_DATA(lattrs->ifla_address),
NLA_DATA_LEN(lattrs->ifla_address));
if (error != 0) {
nlmsg_report_err_msg(npt,
"setting IFLA_ADDRESS failed with error code: %d",
error);
return (error);
}
}
return (0);
}
void
_nl_store_ifp_cookie(struct nl_pstate *npt, struct ifnet *ifp)
{
int ifname_len = strlen(if_name(ifp));
uint32_t ifindex = (uint32_t)if_getindex(ifp);
int nla_len = sizeof(struct nlattr) * 3 +
sizeof(ifindex) + NL_ITEM_ALIGN(ifname_len + 1);
struct nlattr *nla_cookie = npt_alloc(npt, nla_len);
if (nla_cookie == NULL)
return;
nla_cookie->nla_len = nla_len;
nla_cookie->nla_type = NLMSGERR_ATTR_COOKIE;
struct nlattr *nla = nla_cookie + 1;
nla->nla_len = sizeof(struct nlattr) + sizeof(ifindex);
nla->nla_type = IFLA_NEW_IFINDEX;
memcpy(NLA_DATA(nla), &ifindex, sizeof(ifindex));
nla = NLA_NEXT(nla);
nla->nla_len = sizeof(struct nlattr) + ifname_len + 1;
nla->nla_type = IFLA_IFNAME;
strlcpy(NLA_DATA(nla), if_name(ifp), ifname_len + 1);
nlmsg_report_cookie(npt, nla_cookie);
}