xref: /netbsd-src/sys/netmpls/mpls_proto.c (revision 1b9578b8c2c1f848eeb16dabbfd7d1f0d9fdefbd)
1 /*	$NetBSD: mpls_proto.c,v 1.2 2011/03/31 19:40:53 dyoung 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.2 2011/03/31 19:40:53 dyoung 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_usrreq(struct socket *, int, struct mbuf *, struct mbuf *,
53 	struct mbuf *, struct lwp *);
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 
68 void mpls_init()
69 {
70 #ifdef MBUFTRACE
71 	MOWNER_ATTACH(&mpls_owner);
72 #endif
73 	memset(&mplsintrq, 0, sizeof(mplsintrq));
74 	mplsintrq.ifq_maxlen = 256;
75 }
76 
77 DOMAIN_DEFINE(mplsdomain);
78 
79 const struct protosw mplssw[] = {
80 	{	.pr_domain = &mplsdomain,
81 		.pr_init = mpls_init,
82 	},
83 	{
84 		.pr_type = SOCK_DGRAM,
85 		.pr_domain = &mplsdomain,
86 		.pr_flags = PR_ATOMIC | PR_ADDR,
87 		.pr_usrreq = mpls_usrreq,
88 	},
89 	{
90 		.pr_type = SOCK_RAW,
91 		.pr_domain = &mplsdomain,
92 		.pr_flags = PR_ATOMIC | PR_ADDR,
93 		.pr_usrreq = mpls_usrreq,
94 	},
95 };
96 
97 struct domain mplsdomain = {
98 	.dom_family = PF_MPLS,
99 	.dom_name = "MPLS",
100 	.dom_init = NULL,
101 	.dom_externalize = NULL,
102 	.dom_dispose = NULL,
103 	.dom_protosw = mplssw,
104 	.dom_protoswNPROTOSW = &mplssw[__arraycount(mplssw)],
105 	.dom_rtattach = rt_inithead,
106 	.dom_rtoffset = offsetof(struct sockaddr_mpls, smpls_addr) << 3,
107 	.dom_maxrtkey = sizeof(union mpls_shim),
108 	.dom_ifattach = NULL,
109 	.dom_ifdetach = NULL,
110 	.dom_ifqueues = { &mplsintrq, NULL },
111 	.dom_link = { NULL },
112 	.dom_mowner = MOWNER_INIT("MPLS", ""),
113 	.dom_sa_cmpofs = offsetof(struct sockaddr_mpls, smpls_addr),
114 	.dom_sa_cmplen = sizeof(union mpls_shim),
115 	.dom_rtcache = LIST_HEAD_INITIALIZER(mplsdomain.dom_rtcache)
116 };
117 
118 static int
119 mpls_usrreq(struct socket *so, int req, struct mbuf *m,
120         struct mbuf *nam, struct mbuf *control, struct lwp *l)
121 {
122 	int error = EOPNOTSUPP;
123 
124 	if ((req == PRU_ATTACH) &&
125 	    (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0)) {
126 		int s = splsoftnet();
127 		error = soreserve(so, 8192, 8192);
128 		splx(s);
129 	}
130 
131 	return error;
132 }
133 
134 /*
135  * Sysctl for MPLS variables.
136  */
137 SYSCTL_SETUP(sysctl_net_mpls_setup, "sysctl net.mpls subtree setup")
138 {
139 
140         sysctl_createv(clog, 0, NULL, NULL,
141                        CTLFLAG_PERMANENT,
142                        CTLTYPE_NODE, "net", NULL,
143                        NULL, 0, NULL, 0,
144                        CTL_NET, CTL_EOL);
145         sysctl_createv(clog, 0, NULL, NULL,
146                        CTLFLAG_PERMANENT,
147                        CTLTYPE_NODE, "mpls", NULL,
148                        NULL, 0, NULL, 0,
149                        CTL_NET, PF_MPLS, CTL_EOL);
150 
151         sysctl_createv(clog, 0, NULL, NULL,
152                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
153                        CTLTYPE_INT, "ttl",
154                        SYSCTL_DESCR("Default TTL"),
155                        NULL, 0, &mpls_defttl, 0,
156                        CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
157 	sysctl_createv(clog, 0, NULL, NULL,
158 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
159 		       CTLTYPE_INT, "forwarding",
160 		       SYSCTL_DESCR("MPLS forwarding"),
161 		       NULL, 0, &mpls_forwarding, 0,
162 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
163 	sysctl_createv(clog, 0, NULL, NULL,
164 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
165 		       CTLTYPE_INT, "accept",
166 		       SYSCTL_DESCR("Accept MPLS Frames"),
167 		       NULL, 0, &mpls_accept, 0,
168 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
169 	sysctl_createv(clog, 0, NULL, NULL,
170 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
171 		       CTLTYPE_INT, "ifq_len",
172 		       SYSCTL_DESCR("MPLS queue length"),
173 		       NULL, 0, &mplsintrq.ifq_maxlen, 0,
174 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
175 #ifdef INET
176 	sysctl_createv(clog, 0, NULL, NULL,
177 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
178 		       CTLTYPE_INT, "inet_mapttl",
179 		       SYSCTL_DESCR("Map IP TTL"),
180 		       NULL, 0, &mpls_mapttl_inet, 0,
181 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
182 	sysctl_createv(clog, 0, NULL, NULL,
183 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
184 		       CTLTYPE_INT, "inet_map_prec",
185 		       SYSCTL_DESCR("Map IP Prec"),
186 		       NULL, 0, &mpls_mapprec_inet, 0,
187 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
188 	sysctl_createv(clog, 0, NULL, NULL,
189 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
190 		       CTLTYPE_INT, "icmp_respond",
191 		       SYSCTL_DESCR("Emit ICMP packets on errors"),
192 		       NULL, 0, &mpls_icmp_respond, 0,
193 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
194 #endif
195 #ifdef INET6
196 	sysctl_createv(clog, 0, NULL, NULL,
197 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
198 		       CTLTYPE_INT, "inet6_mapttl",
199 		       SYSCTL_DESCR("Map IP6 TTL"),
200 		       NULL, 0, &mpls_mapttl_inet6, 0,
201 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
202 	sysctl_createv(clog, 0, NULL, NULL,
203 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
204 		       CTLTYPE_INT, "inet6_map_prec",
205 		       SYSCTL_DESCR("Map IP6 class"),
206 		       NULL, 0, &mpls_mapclass_inet6, 0,
207 		       CTL_NET, PF_MPLS, CTL_CREATE, CTL_EOL);
208 #endif
209 }
210