xref: /freebsd/sys/netlink/route/iface_drivers.c (revision 4e8d558c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2022 Alexander V. Chernikov <melifaro@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include "opt_netlink.h"
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 #include <sys/types.h>
34 #include <sys/malloc.h>
35 #include <sys/socket.h>
36 #include <sys/sockio.h>
37 #include <sys/syslog.h>
38 #include <sys/socketvar.h>
39 
40 #include <net/ethernet.h>
41 #include <net/if.h>
42 #include <net/if_dl.h>
43 #include <net/if_media.h>
44 #include <net/if_var.h>
45 #include <net/if_clone.h>
46 #include <net/if_vlan_var.h>
47 #include <net/route.h>
48 #include <net/route/nhop.h>
49 #include <net/route/route_ctl.h>
50 #include <netlink/netlink.h>
51 #include <netlink/netlink_ctl.h>
52 #include <netlink/netlink_route.h>
53 #include <netlink/route/route_var.h>
54 
55 #include <netinet6/scope6_var.h> /* scope deembedding */
56 
57 #define	DEBUG_MOD_NAME	nl_iface_drivers
58 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
59 #include <netlink/netlink_debug.h>
60 _DECLARE_DEBUG(LOG_INFO);
61 
62 /*
63  * Generic modification interface handler.
64  * Responsible for changing network stack interface attributes
65  * such as state, mtu or description.
66  */
67 int
68 _nl_modify_ifp_generic(struct ifnet *ifp, struct nl_parsed_link *lattrs,
69     const struct nlattr_bmask *bm, struct nl_pstate *npt)
70 {
71 	int error;
72 
73 	if (lattrs->ifla_ifalias != NULL) {
74 		if (nlp_has_priv(npt->nlp, PRIV_NET_SETIFDESCR)) {
75 			int len = strlen(lattrs->ifla_ifalias) + 1;
76 			char *buf = if_allocdescr(len, M_WAITOK);
77 
78 			memcpy(buf, lattrs->ifla_ifalias, len);
79 			if_setdescr(ifp, buf);
80 			getmicrotime(&ifp->if_lastchange);
81 		} else {
82 			nlmsg_report_err_msg(npt, "Not enough privileges to set descr");
83 			return (EPERM);
84 		}
85 	}
86 
87 	if ((lattrs->ifi_change & IFF_UP) && (lattrs->ifi_flags & IFF_UP) == 0) {
88 		/* Request to down the interface */
89 		if_down(ifp);
90 	}
91 
92 	if (lattrs->ifla_mtu > 0) {
93 		if (nlp_has_priv(npt->nlp, PRIV_NET_SETIFMTU)) {
94 			struct ifreq ifr = { .ifr_mtu = lattrs->ifla_mtu };
95 			error = ifhwioctl(SIOCSIFMTU, ifp, (char *)&ifr, curthread);
96 		} else {
97 			nlmsg_report_err_msg(npt, "Not enough privileges to set mtu");
98 			return (EPERM);
99 		}
100 	}
101 
102 	if (lattrs->ifi_change & IFF_PROMISC) {
103 		error = ifpromisc(ifp, lattrs->ifi_flags & IFF_PROMISC);
104 		if (error != 0) {
105 			nlmsg_report_err_msg(npt, "unable to set promisc");
106 			return (error);
107 		}
108 	}
109 
110 	return (0);
111 }
112 
113 /*
114  * Saves the resulting ifindex and ifname to report them
115  *  to userland along with the operation result.
116  * NLA format:
117  * NLMSGERR_ATTR_COOKIE(nested)
118  *  IFLA_NEW_IFINDEX(u32)
119  *  IFLA_IFNAME(string)
120  */
121 void
122 _nl_store_ifp_cookie(struct nl_pstate *npt, struct ifnet *ifp)
123 {
124 	int ifname_len = strlen(if_name(ifp));
125 	uint32_t ifindex = (uint32_t)ifp->if_index;
126 
127 	int nla_len = sizeof(struct nlattr) * 3 +
128 		sizeof(ifindex) + NL_ITEM_ALIGN(ifname_len + 1);
129 	struct nlattr *nla_cookie = npt_alloc(npt, nla_len);
130 
131 	/* Nested TLV */
132 	nla_cookie->nla_len = nla_len;
133 	nla_cookie->nla_type = NLMSGERR_ATTR_COOKIE;
134 
135 	struct nlattr *nla = nla_cookie + 1;
136 	nla->nla_len = sizeof(struct nlattr) + sizeof(ifindex);
137 	nla->nla_type = IFLA_NEW_IFINDEX;
138 	memcpy(NLA_DATA(nla), &ifindex, sizeof(ifindex));
139 
140 	nla = NLA_NEXT(nla);
141 	nla->nla_len = sizeof(struct nlattr) + ifname_len + 1;
142 	nla->nla_type = IFLA_IFNAME;
143 	strlcpy(NLA_DATA(nla), if_name(ifp), ifname_len + 1);
144 
145 	nlmsg_report_cookie(npt, nla_cookie);
146 }
147 
148