xref: /freebsd/sys/netlink/route/iface_drivers.c (revision 431856c868de12d7af127cdf7e9aeb1b99d0ac99)
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 <sys/cdefs.h>
29 #include "opt_inet.h"
30 #include "opt_inet6.h"
31 #include <sys/types.h>
32 #include <sys/malloc.h>
33 #include <sys/socket.h>
34 #include <sys/sockio.h>
35 #include <sys/syslog.h>
36 #include <sys/socketvar.h>
37 
38 #include <net/ethernet.h>
39 #include <net/if.h>
40 #include <net/if_dl.h>
41 #include <net/if_media.h>
42 #include <net/if_var.h>
43 #include <net/if_clone.h>
44 #include <net/if_vlan_var.h>
45 #include <net/route.h>
46 #include <net/route/nhop.h>
47 #include <net/route/route_ctl.h>
48 #include <netlink/netlink.h>
49 #include <netlink/netlink_ctl.h>
50 #include <netlink/netlink_route.h>
51 #include <netlink/route/route_var.h>
52 
53 #include <netinet6/scope6_var.h> /* scope deembedding */
54 
55 #define	DEBUG_MOD_NAME	nl_iface_drivers
56 #define	DEBUG_MAX_LEVEL	LOG_DEBUG3
57 #include <netlink/netlink_debug.h>
58 _DECLARE_DEBUG(LOG_INFO);
59 
60 /*
61  * Generic modification interface handler.
62  * Responsible for changing network stack interface attributes
63  * such as state, mtu or description.
64  */
65 int
_nl_modify_ifp_generic(struct ifnet * ifp,struct nl_parsed_link * lattrs,const struct nlattr_bmask * bm,struct nl_pstate * npt)66 _nl_modify_ifp_generic(struct ifnet *ifp, struct nl_parsed_link *lattrs,
67     const struct nlattr_bmask *bm, struct nl_pstate *npt)
68 {
69 	int error;
70 
71 	if (lattrs->ifla_ifalias != NULL) {
72 		if (nlp_has_priv(npt->nlp, PRIV_NET_SETIFDESCR)) {
73 			int len = strlen(lattrs->ifla_ifalias) + 1;
74 			char *buf = if_allocdescr(len, M_WAITOK);
75 
76 			memcpy(buf, lattrs->ifla_ifalias, len);
77 			if_setdescr(ifp, buf);
78 			if_setlastchange(ifp);
79 		} else {
80 			nlmsg_report_err_msg(npt, "Not enough privileges to set descr");
81 			return (EPERM);
82 		}
83 	}
84 
85 	if ((lattrs->ifi_change & IFF_UP) != 0 || lattrs->ifi_change == 0) {
86 		/* Request to up or down the interface */
87 		if (lattrs->ifi_flags & IFF_UP)
88 			if_up(ifp);
89 		else
90 			if_down(ifp);
91 	}
92 
93 	if (lattrs->ifla_mtu > 0) {
94 		if (nlp_has_priv(npt->nlp, PRIV_NET_SETIFMTU)) {
95 			struct ifreq ifr = { .ifr_mtu = lattrs->ifla_mtu };
96 			error = ifhwioctl(SIOCSIFMTU, ifp, (char *)&ifr,
97 			    curthread);
98 			if (error != 0) {
99 				nlmsg_report_err_msg(npt, "Failed to set mtu");
100 				return (error);
101 			}
102 		} else {
103 			nlmsg_report_err_msg(npt, "Not enough privileges to set mtu");
104 			return (EPERM);
105 		}
106 	}
107 
108 	if ((lattrs->ifi_change & IFF_PROMISC) != 0 ||
109 	    lattrs->ifi_change == 0)
110 		/*
111 		 * When asking for IFF_PROMISC, set permanent flag instead
112 		 * (IFF_PPROMISC) as we have no way of doing promiscuity
113 		 * reference counting through ifpromisc().  Every call to this
114 		 * function either sets or unsets IFF_PROMISC, and ifi_change
115 		 * is usually set to 0xFFFFFFFF.
116 		 */
117 		if_setppromisc(ifp, (lattrs->ifi_flags & IFF_PROMISC) != 0);
118 
119 	if (lattrs->ifla_address != NULL) {
120 		if (nlp_has_priv(npt->nlp, PRIV_NET_SETIFMAC)) {
121 			error = if_setlladdr(ifp,
122 			    NLA_DATA(lattrs->ifla_address),
123 			    NLA_DATA_LEN(lattrs->ifla_address));
124 			if (error != 0) {
125 				nlmsg_report_err_msg(npt,
126 				    "setting IFLA_ADDRESS failed with error code: %d",
127 				    error);
128 				return (error);
129 			}
130 		} else {
131 			nlmsg_report_err_msg(npt,
132 			    "Not enough privileges to set IFLA_ADDRESS");
133 			return (EPERM);
134 		}
135 	}
136 
137 	return (0);
138 }
139 
140 /*
141  * Saves the resulting ifindex and ifname to report them
142  *  to userland along with the operation result.
143  * NLA format:
144  * NLMSGERR_ATTR_COOKIE(nested)
145  *  IFLA_NEW_IFINDEX(u32)
146  *  IFLA_IFNAME(string)
147  */
148 void
_nl_store_ifp_cookie(struct nl_pstate * npt,struct ifnet * ifp)149 _nl_store_ifp_cookie(struct nl_pstate *npt, struct ifnet *ifp)
150 {
151 	int ifname_len = strlen(if_name(ifp));
152 	uint32_t ifindex = (uint32_t)if_getindex(ifp);
153 
154 	int nla_len = sizeof(struct nlattr) * 3 +
155 		sizeof(ifindex) + NL_ITEM_ALIGN(ifname_len + 1);
156 	struct nlattr *nla_cookie = npt_alloc(npt, nla_len);
157 
158 	/* Nested TLV */
159 	nla_cookie->nla_len = nla_len;
160 	nla_cookie->nla_type = NLMSGERR_ATTR_COOKIE;
161 
162 	struct nlattr *nla = nla_cookie + 1;
163 	nla->nla_len = sizeof(struct nlattr) + sizeof(ifindex);
164 	nla->nla_type = IFLA_NEW_IFINDEX;
165 	memcpy(NLA_DATA(nla), &ifindex, sizeof(ifindex));
166 
167 	nla = NLA_NEXT(nla);
168 	nla->nla_len = sizeof(struct nlattr) + ifname_len + 1;
169 	nla->nla_type = IFLA_IFNAME;
170 	strlcpy(NLA_DATA(nla), if_name(ifp), ifname_len + 1);
171 
172 	nlmsg_report_cookie(npt, nla_cookie);
173 }
174 
175