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