xref: /netbsd-src/sys/netmpls/mpls_proto.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: mpls_proto.c,v 1.11 2014/07/01 05:49:19 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@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.11 2014/07/01 05:49:19 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_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_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
99 {
100 	return EOPNOTSUPP;
101 }
102 
103 static int
104 mpls_usrreq(struct socket *so, int req, struct mbuf *m,
105     struct mbuf *nam, struct mbuf *control, struct lwp *l)
106 {
107 	return EOPNOTSUPP;
108 }
109 
110 /*
111  * Sysctl for MPLS variables.
112  */
113 static void
114 sysctl_net_mpls_setup(struct sysctllog **clog)
115 {
116 
117         sysctl_createv(clog, 0, NULL, NULL,
118                        CTLFLAG_PERMANENT,
119                        CTLTYPE_NODE, "mpls", NULL,
120                        NULL, 0, NULL, 0,
121                        CTL_NET, PF_MPLS, CTL_EOL);
122 
123         sysctl_createv(clog, 0, NULL, NULL,
124                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
125                        CTLTYPE_INT, "ttl",
126                        SYSCTL_DESCR("Default TTL"),
127                        NULL, 0, &mpls_defttl, 0,
128                        CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
129 	sysctl_createv(clog, 0, NULL, NULL,
130 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
131 		       CTLTYPE_INT, "forwarding",
132 		       SYSCTL_DESCR("MPLS forwarding"),
133 		       NULL, 0, &mpls_forwarding, 0,
134 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
135 	sysctl_createv(clog, 0, NULL, NULL,
136 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
137 		       CTLTYPE_INT, "accept",
138 		       SYSCTL_DESCR("Accept MPLS Frames"),
139 		       NULL, 0, &mpls_accept, 0,
140 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
141 	sysctl_createv(clog, 0, NULL, NULL,
142 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
143 		       CTLTYPE_INT, "ifq_len",
144 		       SYSCTL_DESCR("MPLS queue length"),
145 		       NULL, 0, &mplsintrq.ifq_maxlen, 0,
146 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
147 	sysctl_createv(clog, 0, NULL, NULL,
148 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
149 		       CTLTYPE_INT, "rfc4182",
150 		       SYSCTL_DESCR("RFC 4182 conformance"),
151 		       NULL, 0, &mpls_rfc4182, 0,
152 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
153 #ifdef INET
154 	sysctl_createv(clog, 0, NULL, NULL,
155 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
156 		       CTLTYPE_INT, "inet_mapttl",
157 		       SYSCTL_DESCR("Map IP TTL"),
158 		       NULL, 0, &mpls_mapttl_inet, 0,
159 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
160 	sysctl_createv(clog, 0, NULL, NULL,
161 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
162 		       CTLTYPE_INT, "inet_map_prec",
163 		       SYSCTL_DESCR("Map IP Prec"),
164 		       NULL, 0, &mpls_mapprec_inet, 0,
165 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
166 	sysctl_createv(clog, 0, NULL, NULL,
167 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
168 		       CTLTYPE_INT, "icmp_respond",
169 		       SYSCTL_DESCR("Emit ICMP packets on errors"),
170 		       NULL, 0, &mpls_icmp_respond, 0,
171 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
172 #endif
173 #ifdef INET6
174 	sysctl_createv(clog, 0, NULL, NULL,
175 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
176 		       CTLTYPE_INT, "inet6_mapttl",
177 		       SYSCTL_DESCR("Map IP6 TTL"),
178 		       NULL, 0, &mpls_mapttl_inet6, 0,
179 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
180 	sysctl_createv(clog, 0, NULL, NULL,
181 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
182 		       CTLTYPE_INT, "inet6_map_prec",
183 		       SYSCTL_DESCR("Map IP6 class"),
184 		       NULL, 0, &mpls_mapclass_inet6, 0,
185 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
186 #endif
187 }
188 
189 DOMAIN_DEFINE(mplsdomain);
190 
191 PR_WRAP_USRREQS(mpls)
192 #define	mpls_attach	mpls_attach_wrapper
193 #define	mpls_detach	mpls_detach_wrapper
194 #define	mpls_ioctl	mpls_ioctl_wrapper
195 #define	mpls_usrreq	mpls_usrreq_wrapper
196 
197 static const struct pr_usrreqs mpls_usrreqs = {
198 	.pr_attach	= mpls_attach,
199 	.pr_detach	= mpls_detach,
200 	.pr_ioctl	= mpls_ioctl,
201 	.pr_generic	= mpls_usrreq,
202 };
203 
204 const struct protosw mplssw[] = {
205 	{	.pr_domain = &mplsdomain,
206 		.pr_init = mpls_init,
207 	},
208 	{
209 		.pr_type = SOCK_DGRAM,
210 		.pr_domain = &mplsdomain,
211 		.pr_flags = PR_ATOMIC | PR_ADDR,
212 		.pr_usrreqs = &mpls_usrreqs,
213 	},
214 	{
215 		.pr_type = SOCK_RAW,
216 		.pr_domain = &mplsdomain,
217 		.pr_flags = PR_ATOMIC | PR_ADDR,
218 		.pr_usrreqs = &mpls_usrreqs,
219 	},
220 };
221 
222 struct domain mplsdomain = {
223 	.dom_family = PF_MPLS,
224 	.dom_name = "MPLS",
225 	.dom_init = NULL,
226 	.dom_externalize = NULL,
227 	.dom_dispose = NULL,
228 	.dom_protosw = mplssw,
229 	.dom_protoswNPROTOSW = &mplssw[__arraycount(mplssw)],
230 	.dom_rtattach = rt_inithead,
231 	.dom_rtoffset = offsetof(struct sockaddr_mpls, smpls_addr) << 3,
232 	.dom_maxrtkey = sizeof(union mpls_shim),
233 	.dom_ifattach = NULL,
234 	.dom_ifdetach = NULL,
235 	.dom_ifqueues = { &mplsintrq, NULL },
236 	.dom_link = { NULL },
237 	.dom_mowner = MOWNER_INIT("MPLS", ""),
238 	.dom_sa_cmpofs = offsetof(struct sockaddr_mpls, smpls_addr),
239 	.dom_sa_cmplen = sizeof(union mpls_shim),
240 	.dom_rtcache = LIST_HEAD_INITIALIZER(mplsdomain.dom_rtcache)
241 };
242