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