aboutsummaryrefslogtreecommitdiff
path: root/sys/netlink/netlink_glue.c
blob: 164bea1aba61df41b3cc1d2b5f1141cf7096b868 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2023 Alexander V. Chernikov <melifaro@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/lock.h>
#include <sys/rmlock.h>
#include <sys/domain.h>
#include <sys/mbuf.h>
#include <sys/protosw.h>
#include <sys/proc.h>
#include <sys/ck.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/sysent.h>
#include <sys/syslog.h>
#include <sys/priv.h> /* priv_check */

#include <net/route.h>
#include <net/route/route_ctl.h>

#include <netlink/netlink.h>
#include <netlink/netlink_ctl.h>
#include <netlink/netlink_var.h>
#include <netlink/route/route_var.h>

/* Standard bits: built-in the kernel */
SYSCTL_NODE(_net, OID_AUTO, netlink, CTLFLAG_RD, 0,
    "RFC3549 Netlink network state socket family");
SYSCTL_NODE(_net_netlink, OID_AUTO, debug, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
    "Netlink per-subsystem debug levels");

MALLOC_DEFINE(M_NETLINK, "netlink", "Memory used for netlink packets");

/* Netlink-related callbacks needed to glue rtsock, netlink and linuxolator */
static void
ignore_route_event(uint32_t fibnum, const struct rib_cmd_info *rc)
{
}

static void
ignore_ifmsg_event(struct ifnet *ifp, int if_flags_mask)
{
}

static struct rtbridge ignore_cb = {
	.route_f = ignore_route_event,
	.ifmsg_f = ignore_ifmsg_event,
};

void *linux_netlink_p = NULL; /* Callback pointer for Linux translator functions */
struct rtbridge *rtsock_callback_p = &ignore_cb;
struct rtbridge *netlink_callback_p = &ignore_cb;


/*
 * nlp accessors.
 * TODO: move to a separate file once the number grows.
 */
bool
nlp_has_priv(struct nlpcb *nlp, int priv)
{
	return (priv_check_cred(nlp->nl_cred, priv) == 0);
}

struct ucred *
nlp_get_cred(struct nlpcb *nlp)
{
	return (nlp->nl_cred);
}

uint32_t
nlp_get_pid(const struct nlpcb *nlp)
{
	return (nlp->nl_process_id);
}

bool
nlp_unconstrained_vnet(const struct nlpcb *nlp)
{
	return (nlp->nl_unconstrained_vnet);
}

#ifndef NETLINK
/* Stub implementations for the loadable functions */

static bool
get_stub_writer(struct nl_writer *nw)
{
	bzero(nw, sizeof(*nw));
	nw->enomem = true;

	return (false);
}

static bool
nlmsg_get_unicast_writer_stub(struct nl_writer *nw, int size, struct nlpcb *nlp)
{
	return (get_stub_writer(nw));
}

static bool
nlmsg_get_group_writer_stub(struct nl_writer *nw, int size, int protocol, int group_id)
{
	return (get_stub_writer(nw));
}

static bool
nlmsg_get_chain_writer_stub(struct nl_writer *nw, int size, struct mbuf **pm)
{
	return (get_stub_writer(nw));
}

static bool
nlmsg_flush_stub(struct nl_writer *nw __unused)
{
	return (false);
}

static void
nlmsg_ignore_limit_stub(struct nl_writer *nw __unused)
{
}

static bool
nlmsg_refill_buffer_stub(struct nl_writer *nw __unused, int required_len __unused)
{
	return (false);
}

static bool
nlmsg_add_stub(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
    uint16_t flags, uint32_t len)
{
	return (false);
}

static bool
nlmsg_end_stub(struct nl_writer *nw __unused)
{
	return (false);
}

static void
nlmsg_abort_stub(struct nl_writer *nw __unused)
{
}

static bool
nlmsg_end_dump_stub(struct nl_writer *nw, int error, struct nlmsghdr *hdr)
{
	return (false);
}

static int
nl_modify_ifp_generic_stub(struct ifnet *ifp __unused,
    struct nl_parsed_link *lattrs __unused, const struct nlattr_bmask *bm __unused,
    struct nl_pstate *npt __unused)
{
	return (ENOTSUP);
}

static void
nl_store_ifp_cookie_stub(struct nl_pstate *npt __unused, struct ifnet *ifp __unused)
{
}

static struct nlpcb *
nl_get_thread_nlp_stub(struct thread *td __unused)
{
	return (NULL);
}

const static struct nl_function_wrapper nl_stub = {
	.nlmsg_add = nlmsg_add_stub,
	.nlmsg_refill_buffer = nlmsg_refill_buffer_stub,
	.nlmsg_flush = nlmsg_flush_stub,
	.nlmsg_end = nlmsg_end_stub,
	.nlmsg_abort = nlmsg_abort_stub,
	.nlmsg_ignore_limit = nlmsg_ignore_limit_stub,
	.nlmsg_get_unicast_writer = nlmsg_get_unicast_writer_stub,
	.nlmsg_get_group_writer = nlmsg_get_group_writer_stub,
	.nlmsg_get_chain_writer = nlmsg_get_chain_writer_stub,
	.nlmsg_end_dump = nlmsg_end_dump_stub,
	.nl_modify_ifp_generic = nl_modify_ifp_generic_stub,
	.nl_store_ifp_cookie = nl_store_ifp_cookie_stub,
	.nl_get_thread_nlp = nl_get_thread_nlp_stub,
};

/*
 * If the kernel is compiled with netlink as a module,
 *  provide a way to introduce non-stub functioms
 */
static const struct nl_function_wrapper *_nl = &nl_stub;

void
nl_set_functions(const struct nl_function_wrapper *nl)
{
	_nl = (nl != NULL) ? nl : &nl_stub;
}

/* Function wrappers */
bool
nlmsg_get_unicast_writer(struct nl_writer *nw, int size, struct nlpcb *nlp)
{
	return (_nl->nlmsg_get_unicast_writer(nw, size, nlp));
}

bool
nlmsg_get_group_writer(struct nl_writer *nw, int size, int protocol, int group_id)
{
	return (_nl->nlmsg_get_group_writer(nw, size, protocol, group_id));
}

bool
nlmsg_get_chain_writer(struct nl_writer *nw, int size, struct mbuf **pm)
{
	return (_nl->nlmsg_get_chain_writer(nw, size, pm));
}

bool
nlmsg_flush(struct nl_writer *nw)
{
	return (_nl->nlmsg_flush(nw));
}

void nlmsg_ignore_limit(struct nl_writer *nw)
{
	_nl->nlmsg_ignore_limit(nw);
}

bool
nlmsg_refill_buffer(struct nl_writer *nw, int required_len)
{
	return (_nl->nlmsg_refill_buffer(nw, required_len));
}

bool
nlmsg_add(struct nl_writer *nw, uint32_t portid, uint32_t seq, uint16_t type,
    uint16_t flags, uint32_t len)
{
	return (_nl->nlmsg_add(nw, portid, seq, type, flags, len));
}

bool
nlmsg_end(struct nl_writer *nw)
{
	return (_nl->nlmsg_end(nw));
}

void
nlmsg_abort(struct nl_writer *nw)
{
	_nl->nlmsg_abort(nw);
}

bool
nlmsg_end_dump(struct nl_writer *nw, int error, struct nlmsghdr *hdr)
{
	return (_nl->nlmsg_end_dump(nw, error, hdr));
}

int
nl_modify_ifp_generic(struct ifnet *ifp, struct nl_parsed_link *lattrs,
    const struct nlattr_bmask *bm , struct nl_pstate *npt)
{
	return (_nl->nl_modify_ifp_generic(ifp, lattrs, bm, npt));
}

void
nl_store_ifp_cookie(struct nl_pstate *npt, struct ifnet *ifp)
{
	return (_nl->nl_store_ifp_cookie(npt, ifp));
}

struct nlpcb *
nl_get_thread_nlp(struct thread *td)
{
	return (_nl->nl_get_thread_nlp(td));
}

#endif /* !NETLINK */