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_DEBUG);
59
60 /*
61 * Generic modification interface handler.
62 * Responsible for changing network stack interface attributes
63 * such as state, mtu or description.
64 */
65 static int
modify_generic(struct ifnet * ifp,struct nl_parsed_link * lattrs,const struct nlattr_bmask * bm,struct nlpcb * nlp,struct nl_pstate * npt)66 modify_generic(struct ifnet *ifp, struct nl_parsed_link *lattrs,
67 const struct nlattr_bmask *bm, struct nlpcb *nlp, struct nl_pstate *npt)
68 {
69 int error;
70
71 if (lattrs->ifla_ifalias != NULL) {
72 if (nlp_has_priv(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 getmicrotime(&ifp->if_lastchange);
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) && (lattrs->ifi_flags & IFF_UP) == 0) {
86 /* Request to down the interface */
87 if_down(ifp);
88 }
89
90 if (lattrs->ifla_mtu > 0) {
91 if (nlp_has_priv(nlp, PRIV_NET_SETIFMTU)) {
92 struct ifreq ifr = { .ifr_mtu = lattrs->ifla_mtu };
93 error = ifhwioctl(SIOCSIFMTU, ifp, (char *)&ifr, curthread);
94 } else {
95 nlmsg_report_err_msg(npt, "Not enough privileges to set mtu");
96 return (EPERM);
97 }
98 }
99
100 if (lattrs->ifi_change & IFF_PROMISC) {
101 error = ifpromisc(ifp, lattrs->ifi_flags & IFF_PROMISC);
102 if (error != 0) {
103 nlmsg_report_err_msg(npt, "unable to set promisc");
104 return (error);
105 }
106 }
107
108 return (0);
109 }
110
111 /*
112 * Saves the resulting ifindex and ifname to report them
113 * to userland along with the operation result.
114 * NLA format:
115 * NLMSGERR_ATTR_COOKIE(nested)
116 * IFLA_NEW_IFINDEX(u32)
117 * IFLA_IFNAME(string)
118 */
119 static void
store_cookie(struct nl_pstate * npt,struct ifnet * ifp)120 store_cookie(struct nl_pstate *npt, struct ifnet *ifp)
121 {
122 int ifname_len = strlen(if_name(ifp));
123 uint32_t ifindex = (uint32_t)ifp->if_index;
124
125 int nla_len = sizeof(struct nlattr) * 3 +
126 sizeof(ifindex) + NL_ITEM_ALIGN(ifname_len + 1);
127 struct nlattr *nla_cookie = npt_alloc(npt, nla_len);
128
129 /* Nested TLV */
130 nla_cookie->nla_len = nla_len;
131 nla_cookie->nla_type = NLMSGERR_ATTR_COOKIE;
132
133 struct nlattr *nla = nla_cookie + 1;
134 nla->nla_len = sizeof(struct nlattr) + sizeof(ifindex);
135 nla->nla_type = IFLA_NEW_IFINDEX;
136 memcpy(NLA_DATA(nla), &ifindex, sizeof(ifindex));
137
138 nla = NLA_NEXT(nla);
139 nla->nla_len = sizeof(struct nlattr) + ifname_len + 1;
140 nla->nla_type = IFLA_IFNAME;
141 strlcpy(NLA_DATA(nla), if_name(ifp), ifname_len + 1);
142
143 nlmsg_report_cookie(npt, nla_cookie);
144 }
145
146 static int
create_generic_ifd(struct nl_parsed_link * lattrs,const struct nlattr_bmask * bm,struct ifc_data * ifd,struct nlpcb * nlp,struct nl_pstate * npt)147 create_generic_ifd(struct nl_parsed_link *lattrs, const struct nlattr_bmask *bm,
148 struct ifc_data *ifd, struct nlpcb *nlp, struct nl_pstate *npt)
149 {
150 int error = 0;
151
152 struct ifnet *ifp = NULL;
153 error = ifc_create_ifp(lattrs->ifla_ifname, ifd, &ifp);
154
155 NLP_LOG(LOG_DEBUG2, nlp, "clone for %s returned %d", lattrs->ifla_ifname, error);
156
157 if (error == 0) {
158 struct epoch_tracker et;
159
160 NET_EPOCH_ENTER(et);
161 bool success = if_try_ref(ifp);
162 NET_EPOCH_EXIT(et);
163 if (!success)
164 return (EINVAL);
165 error = modify_generic(ifp, lattrs, bm, nlp, npt);
166 if (error == 0)
167 store_cookie(npt, ifp);
168 if_rele(ifp);
169 }
170
171 return (error);
172 }
173 /*
174 * Generic creation interface handler.
175 * Responsible for creating interfaces w/o parameters and setting
176 * misc attributes such as state, mtu or description.
177 */
178 static int
create_generic(struct nl_parsed_link * lattrs,const struct nlattr_bmask * bm,struct nlpcb * nlp,struct nl_pstate * npt)179 create_generic(struct nl_parsed_link *lattrs, const struct nlattr_bmask *bm,
180 struct nlpcb *nlp, struct nl_pstate *npt)
181 {
182 struct ifc_data ifd = {};
183
184 return (create_generic_ifd(lattrs, bm, &ifd, nlp, npt));
185 }
186
187 struct nl_cloner generic_cloner = {
188 .name = "_default_",
189 .create_f = create_generic,
190 .modify_f = modify_generic,
191 };
192
193 /*
194 *
195 * {len=76, type=RTM_NEWLINK, flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, seq=1662892737, pid=0},
196 * {ifi_family=AF_UNSPEC, ifi_type=ARPHRD_NETROM, ifi_index=0, ifi_flags=0, ifi_change=0},
197 * [
198 * {{nla_len=8, nla_type=IFLA_LINK}, 2},
199 * {{nla_len=12, nla_type=IFLA_IFNAME}, "xvlan22"},
200 * {{nla_len=24, nla_type=IFLA_LINKINFO},
201 * [
202 * {{nla_len=8, nla_type=IFLA_INFO_KIND}, "vlan"...},
203 * {{nla_len=12, nla_type=IFLA_INFO_DATA}, "\x06\x00\x01\x00\x16\x00\x00\x00"}]}]}, iov_len=76}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 76
204 */
205
206 struct nl_parsed_vlan {
207 uint16_t vlan_id;
208 uint16_t vlan_proto;
209 struct ifla_vlan_flags vlan_flags;
210 };
211
212 #define _OUT(_field) offsetof(struct nl_parsed_vlan, _field)
213 static const struct nlattr_parser nla_p_vlan[] = {
214 { .type = IFLA_VLAN_ID, .off = _OUT(vlan_id), .cb = nlattr_get_uint16 },
215 { .type = IFLA_VLAN_FLAGS, .off = _OUT(vlan_flags), .cb = nlattr_get_nla },
216 { .type = IFLA_VLAN_PROTOCOL, .off = _OUT(vlan_proto), .cb = nlattr_get_uint16 },
217 };
218 #undef _OUT
219 NL_DECLARE_ATTR_PARSER(vlan_parser, nla_p_vlan);
220
221 static int
create_vlan(struct nl_parsed_link * lattrs,const struct nlattr_bmask * bm,struct nlpcb * nlp,struct nl_pstate * npt)222 create_vlan(struct nl_parsed_link *lattrs, const struct nlattr_bmask *bm,
223 struct nlpcb *nlp, struct nl_pstate *npt)
224 {
225 struct epoch_tracker et;
226 struct ifnet *ifp;
227 int error;
228
229 /*
230 * lattrs.ifla_ifname is the new interface name
231 * lattrs.ifi_index contains parent interface index
232 * lattrs.ifla_idata contains un-parsed vlan data
233 */
234
235 struct nl_parsed_vlan attrs = {
236 .vlan_id = 0xFEFE,
237 .vlan_proto = ETHERTYPE_VLAN
238 };
239 NLP_LOG(LOG_DEBUG3, nlp, "nested: %p len %d", lattrs->ifla_idata, lattrs->ifla_idata->nla_len);
240
241 if (lattrs->ifla_idata == NULL) {
242 NLMSG_REPORT_ERR_MSG(npt, "vlan id is required, guessing not supported");
243 return (ENOTSUP);
244 }
245
246 error = nl_parse_nested(lattrs->ifla_idata, &vlan_parser, npt, &attrs);
247 if (error != 0)
248 return (error);
249 if (attrs.vlan_id > 4095) {
250 NLMSG_REPORT_ERR_MSG(npt, "Invalid VID: %d", attrs.vlan_id);
251 return (EINVAL);
252 }
253 if (attrs.vlan_proto != ETHERTYPE_VLAN && attrs.vlan_proto != ETHERTYPE_QINQ) {
254 NLMSG_REPORT_ERR_MSG(npt, "Unsupported ethertype: 0x%04X", attrs.vlan_proto);
255 return (ENOTSUP);
256 }
257
258 NET_EPOCH_ENTER(et);
259 ifp = ifnet_byindex_ref(lattrs->ifi_index);
260 NET_EPOCH_EXIT(et);
261 if (ifp == NULL) {
262 NLP_LOG(LOG_DEBUG, nlp, "unable to find parent interface %u",
263 lattrs->ifi_index);
264 return (ENOENT);
265 }
266
267 struct vlanreq params = {
268 .vlr_tag = attrs.vlan_id,
269 .vlr_proto = attrs.vlan_proto,
270 };
271 strlcpy(params.vlr_parent, if_name(ifp), sizeof(params.vlr_parent));
272 struct ifc_data ifd = { .flags = IFC_F_SYSSPACE, .params = ¶ms };
273
274 error = create_generic_ifd(lattrs, bm, &ifd, nlp, npt);
275
276 if_rele(ifp);
277 return (error);
278 }
279
280 static int
dump_vlan(struct ifnet * ifp,struct nl_writer * nw)281 dump_vlan(struct ifnet *ifp, struct nl_writer *nw)
282 {
283 return (0);
284 }
285
286 static struct nl_cloner vlan_cloner = {
287 .name = "vlan",
288 .create_f = create_vlan,
289 .modify_f = modify_generic,
290 .dump_f = dump_vlan,
291
292 };
293
294 static const struct nlhdr_parser *all_parsers[] = { &vlan_parser };
295
296 void
rtnl_iface_drivers_register(void)297 rtnl_iface_drivers_register(void)
298 {
299 rtnl_iface_add_cloner(&vlan_cloner);
300 NL_VERIFY_PARSERS(all_parsers);
301 }
302
303
304