mpls_proto.c revision 1.21 1 /* $NetBSD: mpls_proto.c,v 1.21 2014/08/05 05:24:27 rtr Exp $ */
2
3 /*
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Mihai Chelaru <kefren (at) NetBSD.org>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: mpls_proto.c,v 1.21 2014/08/05 05:24:27 rtr Exp $");
34
35 #include "opt_inet.h"
36 #include "opt_mbuftrace.h"
37
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/protosw.h>
41 #include <sys/domain.h>
42 #include <sys/socketvar.h>
43 #include <sys/sysctl.h>
44
45 #include <net/route.h>
46
47 #include <netmpls/mpls.h>
48 #include <netmpls/mpls_var.h>
49
50 struct ifqueue mplsintrq;
51
52 static int mpls_attach(struct socket *, int);
53 static void sysctl_net_mpls_setup(struct sysctllog **);
54
55 #ifdef MBUFTRACE
56 struct mowner mpls_owner = MOWNER_INIT("MPLS", "");
57 #endif
58
59 int mpls_defttl = 255;
60 int mpls_mapttl_inet = 1;
61 int mpls_mapttl_inet6 = 1;
62 int mpls_icmp_respond = 0;
63 int mpls_forwarding = 0;
64 int mpls_frame_accept = 0;
65 int mpls_mapprec_inet = 1;
66 int mpls_mapclass_inet6 = 1;
67 int mpls_rfc4182 = 1;
68
69 void mpls_init(void)
70 {
71 #ifdef MBUFTRACE
72 MOWNER_ATTACH(&mpls_owner);
73 #endif
74 memset(&mplsintrq, 0, sizeof(mplsintrq));
75 mplsintrq.ifq_maxlen = 256;
76
77 sysctl_net_mpls_setup(NULL);
78 }
79
80 static int
81 mpls_attach(struct socket *so, int proto)
82 {
83 int error = EOPNOTSUPP;
84
85 sosetlock(so);
86 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
87 error = soreserve(so, 8192, 8192);
88 }
89 return error;
90 }
91
92 static void
93 mpls_detach(struct socket *so)
94 {
95 }
96
97 static int
98 mpls_accept(struct socket *so, struct mbuf *nam)
99 {
100 KASSERT(solocked(so));
101
102 return EOPNOTSUPP;
103 }
104
105 static int
106 mpls_bind(struct socket *so, struct mbuf *nam, struct lwp *l)
107 {
108 KASSERT(solocked(so));
109
110 return EOPNOTSUPP;
111 }
112
113 static int
114 mpls_listen(struct socket *so, struct lwp *l)
115 {
116 KASSERT(solocked(so));
117
118 return EOPNOTSUPP;
119 }
120
121 static int
122 mpls_connect(struct socket *so, struct mbuf *nam, struct lwp *l)
123 {
124 KASSERT(solocked(so));
125
126 return EOPNOTSUPP;
127 }
128
129 static int
130 mpls_disconnect(struct socket *so)
131 {
132 KASSERT(solocked(so));
133
134 return EOPNOTSUPP;
135 }
136
137 static int
138 mpls_shutdown(struct socket *so)
139 {
140 KASSERT(solocked(so));
141
142 return EOPNOTSUPP;
143 }
144
145 static int
146 mpls_abort(struct socket *so)
147 {
148 KASSERT(solocked(so));
149
150 return EOPNOTSUPP;
151 }
152
153 static int
154 mpls_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
155 {
156 return EOPNOTSUPP;
157 }
158
159 static int
160 mpls_stat(struct socket *so, struct stat *ub)
161 {
162 KASSERT(solocked(so));
163
164 return EOPNOTSUPP;
165 }
166
167 static int
168 mpls_peeraddr(struct socket *so, struct mbuf *nam)
169 {
170 KASSERT(solocked(so));
171
172 return EOPNOTSUPP;
173 }
174
175 static int
176 mpls_sockaddr(struct socket *so, struct mbuf *nam)
177 {
178 KASSERT(solocked(so));
179
180 return EOPNOTSUPP;
181 }
182
183 static int
184 mpls_recvoob(struct socket *so, struct mbuf *m, int flags)
185 {
186 KASSERT(solocked(so));
187
188 return EOPNOTSUPP;
189 }
190
191 static int
192 mpls_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
193 {
194 KASSERT(solocked(so));
195
196 return EOPNOTSUPP;
197 }
198
199 static int
200 mpls_usrreq(struct socket *so, int req, struct mbuf *m,
201 struct mbuf *nam, struct mbuf *control, struct lwp *l)
202 {
203
204 KASSERT(req != PRU_ATTACH);
205 KASSERT(req != PRU_DETACH);
206 KASSERT(req != PRU_ACCEPT);
207 KASSERT(req != PRU_BIND);
208 KASSERT(req != PRU_LISTEN);
209 KASSERT(req != PRU_CONNECT);
210 KASSERT(req != PRU_DISCONNECT);
211 KASSERT(req != PRU_SHUTDOWN);
212 KASSERT(req != PRU_ABORT);
213 KASSERT(req != PRU_CONTROL);
214 KASSERT(req != PRU_SENSE);
215 KASSERT(req != PRU_PEERADDR);
216 KASSERT(req != PRU_SOCKADDR);
217 KASSERT(req != PRU_RCVOOB);
218 KASSERT(req != PRU_SENDOOB);
219
220 return EOPNOTSUPP;
221 }
222
223 /*
224 * Sysctl for MPLS variables.
225 */
226 static void
227 sysctl_net_mpls_setup(struct sysctllog **clog)
228 {
229
230 sysctl_createv(clog, 0, NULL, NULL,
231 CTLFLAG_PERMANENT,
232 CTLTYPE_NODE, "mpls", NULL,
233 NULL, 0, NULL, 0,
234 CTL_NET, PF_MPLS, CTL_EOL);
235
236 sysctl_createv(clog, 0, NULL, NULL,
237 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
238 CTLTYPE_INT, "ttl",
239 SYSCTL_DESCR("Default TTL"),
240 NULL, 0, &mpls_defttl, 0,
241 CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
242 sysctl_createv(clog, 0, NULL, NULL,
243 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
244 CTLTYPE_INT, "forwarding",
245 SYSCTL_DESCR("MPLS forwarding"),
246 NULL, 0, &mpls_forwarding, 0,
247 CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
248 sysctl_createv(clog, 0, NULL, NULL,
249 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
250 CTLTYPE_INT, "accept",
251 SYSCTL_DESCR("Accept MPLS Frames"),
252 NULL, 0, &mpls_frame_accept, 0,
253 CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
254 sysctl_createv(clog, 0, NULL, NULL,
255 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
256 CTLTYPE_INT, "ifq_len",
257 SYSCTL_DESCR("MPLS queue length"),
258 NULL, 0, &mplsintrq.ifq_maxlen, 0,
259 CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
260 sysctl_createv(clog, 0, NULL, NULL,
261 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
262 CTLTYPE_INT, "rfc4182",
263 SYSCTL_DESCR("RFC 4182 conformance"),
264 NULL, 0, &mpls_rfc4182, 0,
265 CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
266 #ifdef INET
267 sysctl_createv(clog, 0, NULL, NULL,
268 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
269 CTLTYPE_INT, "inet_mapttl",
270 SYSCTL_DESCR("Map IP TTL"),
271 NULL, 0, &mpls_mapttl_inet, 0,
272 CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
273 sysctl_createv(clog, 0, NULL, NULL,
274 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
275 CTLTYPE_INT, "inet_map_prec",
276 SYSCTL_DESCR("Map IP Prec"),
277 NULL, 0, &mpls_mapprec_inet, 0,
278 CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
279 sysctl_createv(clog, 0, NULL, NULL,
280 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
281 CTLTYPE_INT, "icmp_respond",
282 SYSCTL_DESCR("Emit ICMP packets on errors"),
283 NULL, 0, &mpls_icmp_respond, 0,
284 CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
285 #endif
286 #ifdef INET6
287 sysctl_createv(clog, 0, NULL, NULL,
288 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
289 CTLTYPE_INT, "inet6_mapttl",
290 SYSCTL_DESCR("Map IP6 TTL"),
291 NULL, 0, &mpls_mapttl_inet6, 0,
292 CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
293 sysctl_createv(clog, 0, NULL, NULL,
294 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
295 CTLTYPE_INT, "inet6_map_prec",
296 SYSCTL_DESCR("Map IP6 class"),
297 NULL, 0, &mpls_mapclass_inet6, 0,
298 CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
299 #endif
300 }
301
302 DOMAIN_DEFINE(mplsdomain);
303
304 PR_WRAP_USRREQS(mpls)
305 #define mpls_attach mpls_attach_wrapper
306 #define mpls_detach mpls_detach_wrapper
307 #define mpls_accept mpls_accept_wrapper
308 #define mpls_bind mpls_bind_wrapper
309 #define mpls_listen mpls_listen_wrapper
310 #define mpls_connect mpls_connect_wrapper
311 #define mpls_disconnect mpls_disconnect_wrapper
312 #define mpls_shutdown mpls_shutdown_wrapper
313 #define mpls_abort mpls_abort_wrapper
314 #define mpls_ioctl mpls_ioctl_wrapper
315 #define mpls_stat mpls_stat_wrapper
316 #define mpls_peeraddr mpls_peeraddr_wrapper
317 #define mpls_sockaddr mpls_sockaddr_wrapper
318 #define mpls_recvoob mpls_recvoob_wrapper
319 #define mpls_sendoob mpls_sendoob_wrapper
320 #define mpls_usrreq mpls_usrreq_wrapper
321
322 static const struct pr_usrreqs mpls_usrreqs = {
323 .pr_attach = mpls_attach,
324 .pr_detach = mpls_detach,
325 .pr_accept = mpls_accept,
326 .pr_bind = mpls_bind,
327 .pr_listen = mpls_listen,
328 .pr_connect = mpls_connect,
329 .pr_disconnect = mpls_disconnect,
330 .pr_shutdown = mpls_shutdown,
331 .pr_abort = mpls_abort,
332 .pr_ioctl = mpls_ioctl,
333 .pr_stat = mpls_stat,
334 .pr_peeraddr = mpls_peeraddr,
335 .pr_sockaddr = mpls_sockaddr,
336 .pr_recvoob = mpls_recvoob,
337 .pr_sendoob = mpls_sendoob,
338 .pr_generic = mpls_usrreq,
339 };
340
341 const struct protosw mplssw[] = {
342 { .pr_domain = &mplsdomain,
343 .pr_init = mpls_init,
344 },
345 {
346 .pr_type = SOCK_DGRAM,
347 .pr_domain = &mplsdomain,
348 .pr_flags = PR_ATOMIC | PR_ADDR,
349 .pr_usrreqs = &mpls_usrreqs,
350 },
351 {
352 .pr_type = SOCK_RAW,
353 .pr_domain = &mplsdomain,
354 .pr_flags = PR_ATOMIC | PR_ADDR,
355 .pr_usrreqs = &mpls_usrreqs,
356 },
357 };
358
359 struct domain mplsdomain = {
360 .dom_family = PF_MPLS,
361 .dom_name = "MPLS",
362 .dom_init = NULL,
363 .dom_externalize = NULL,
364 .dom_dispose = NULL,
365 .dom_protosw = mplssw,
366 .dom_protoswNPROTOSW = &mplssw[__arraycount(mplssw)],
367 .dom_rtattach = rt_inithead,
368 .dom_rtoffset = offsetof(struct sockaddr_mpls, smpls_addr) << 3,
369 .dom_maxrtkey = sizeof(union mpls_shim),
370 .dom_ifattach = NULL,
371 .dom_ifdetach = NULL,
372 .dom_ifqueues = { &mplsintrq, NULL },
373 .dom_link = { NULL },
374 .dom_mowner = MOWNER_INIT("MPLS", ""),
375 .dom_sa_cmpofs = offsetof(struct sockaddr_mpls, smpls_addr),
376 .dom_sa_cmplen = sizeof(union mpls_shim),
377 .dom_rtcache = LIST_HEAD_INITIALIZER(mplsdomain.dom_rtcache)
378 };
379