dccp6_usrreq.c revision 1.4 1 /* $KAME: dccp6_usrreq.c,v 1.13 2005/07/27 08:42:56 nishida Exp $ */
2 /* $NetBSD: dccp6_usrreq.c,v 1.4 2015/04/25 14:56:05 rtr Exp $ */
3
4 /*
5 * Copyright (C) 2003 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: dccp6_usrreq.c,v 1.4 2015/04/25 14:56:05 rtr Exp $");
35
36 #include "opt_inet.h"
37 #include "opt_dccp.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/domain.h>
42 #include <sys/kernel.h>
43 #include <sys/pool.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/proc.h>
48 #include <sys/protosw.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/mutex.h>
53 #include <sys/sysctl.h>
54 #include <sys/syslog.h>
55 #include <sys/queue.h>
56
57 #include <net/if.h>
58 #include <net/route.h>
59
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet/in_pcb.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip6.h>
66 #include <netinet/ip_icmp.h>
67 #include <netinet/icmp_var.h>
68 #include <netinet/ip_var.h>
69 #include <netinet6/in6_pcb.h>
70 #include <netinet6/ip6_var.h>
71 #include <netinet6/nd6.h>
72 #include <netinet/dccp.h>
73 #include <netinet/dccp_var.h>
74 #include <netinet6/dccp6_var.h>
75 #include <netinet/dccp_cc_sw.h>
76
77 #if !defined(__FreeBSD__) || __FreeBSD_version < 500000
78 #define INP_INFO_LOCK_INIT(x,y)
79 #define INP_INFO_WLOCK(x)
80 #define INP_INFO_WUNLOCK(x)
81 #define INP_INFO_RLOCK(x)
82 #define INP_INFO_RUNLOCK(x)
83 #define INP_LOCK(x)
84 #define INP_UNLOCK(x)
85 #endif
86
87 #define PULLDOWN_TEST
88
89 int
90 dccp6_input(struct mbuf **mp, int *offp, int proto)
91 {
92 struct mbuf *m = *mp;
93 DCCP_DEBUG((LOG_INFO, "In dccp6_input!\n"));
94 #ifndef PULLDOWN_TEST
95 IP6_EXTHDR_CHECK(m, *offp, sizeof(struct dccphdr), IPPROTO_DONE);
96 #endif
97
98 dccp_input(m, *offp);
99 return IPPROTO_DONE;
100 }
101
102 void *
103 dccp6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
104 {
105 if (sa->sa_family != AF_INET6 ||
106 sa->sa_len != sizeof(struct sockaddr_in6))
107 return NULL;
108
109 /* FIX LATER */
110 return NULL;
111 }
112
113 int
114 dccp6_bind(struct socket *so, struct sockaddr *nam, struct lwp *td)
115 {
116 struct in6pcb *in6p;
117 int error;
118 struct sockaddr_in6 *sin6p = (struct sockaddr_in6 *)nam;
119
120 DCCP_DEBUG((LOG_INFO, "Entering dccp6_bind!\n"));
121 INP_INFO_WLOCK(&dccpbinfo);
122 in6p = sotoin6pcb(so);
123 if (in6p == 0) {
124 INP_INFO_WUNLOCK(&dccpbinfo);
125 DCCP_DEBUG((LOG_INFO, "dccp6_bind: in6p == 0!\n"));
126 return EINVAL;
127 }
128 /* Do not bind to multicast addresses! */
129 if (sin6p->sin6_family == AF_INET6 &&
130 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
131 INP_INFO_WUNLOCK(&dccpbinfo);
132 return EAFNOSUPPORT;
133 }
134 INP_LOCK(inp);
135
136 in6todccpcb(in6p)->inp_vflag &= ~INP_IPV4;
137 in6todccpcb(in6p)->inp_vflag |= INP_IPV6;
138
139 error = in6_pcbbind(in6p, sin6p, td);
140 INP_UNLOCK(inp);
141 INP_INFO_WUNLOCK(&dccpbinfo);
142 return error;
143 }
144
145 int
146 dccp6_connect(struct socket *so, struct mbuf *m, struct lwp *l)
147 {
148 struct in6pcb *in6p;
149 struct dccpcb *dp;
150 int error;
151 struct sockaddr_in6 *sin6;
152 struct sockaddr *nam;
153 char test[2];
154
155 DCCP_DEBUG((LOG_INFO, "Entering dccp6_connect!\n"));
156
157 #ifndef __NetBSD__
158 INP_INFO_WLOCK(&dccpbinfo);
159 inp = sotoinpcb(so);
160 if (inp == 0) {
161 INP_INFO_WUNLOCK(&dccpbinfo);
162 return EINVAL;
163 }
164 INP_LOCK(inp);
165 if (inp->inp_faddr.s_addr != INADDR_ANY) {
166 INP_UNLOCK(inp);
167 INP_INFO_WUNLOCK(&dccpbinfo);
168 return EISCONN;
169 }
170
171 dp = (struct dccpcb *)inp->inp_ppcb;
172 #else
173 in6p = sotoin6pcb(so);
174 if (in6p == 0) {
175 return EINVAL;
176 }
177 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
178 return EISCONN;
179 }
180
181 dp = (struct dccpcb *)in6p->in6p_ppcb;
182 #endif
183 if (dp->state == DCCPS_ESTAB) {
184 DCCP_DEBUG((LOG_INFO, "Why are we in connect when we already have a established connection?\n"));
185 }
186
187 dp->who = DCCP_CLIENT;
188 dp->seq_snd = (((u_int64_t)random() << 32) | random()) % 281474976710656LL;
189
190 nam = mtod(m, struct sockaddr *);
191 sin6 = (struct sockaddr_in6 *)nam;
192 if (sin6->sin6_family == AF_INET6
193 && IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
194 error = EAFNOSUPPORT;
195 goto bad;
196 }
197
198 dp->inp_vflag &= ~INP_IPV4;
199 dp->inp_vflag |= INP_IPV6;
200
201 error = dccp_doconnect(so, m, l, 1);
202
203 if (error != 0)
204 goto bad;
205
206 callout_reset(&dp->retrans_timer, dp->retrans, dccp_retrans_t, dp);
207 callout_reset(&dp->connect_timer, DCCP_CONNECT_TIMER, dccp_connect_t, dp);
208
209 test[0] = dp->pref_cc;
210 #if 0
211 /* FIX THIS LATER */
212 if (dp->pref_cc == 2) {
213 test[1] = 3;
214 } else {
215 test[1] = 2;
216 }
217 dccp_add_feature(dp, DCCP_OPT_CHANGE, DCCP_FEATURE_CC, test, 2);
218 dccp_add_feature(dp, DCCP_OPT_PREFER, DCCP_FEATURE_CC, test, 2);
219 #else
220 /* we only support CCID2 at this moment */
221 dccp_add_feature(dp, DCCP_OPT_CHANGE_R, DCCP_FEATURE_CC, test, 1);
222 #endif
223
224 error = dccp_output(dp, 0);
225
226 bad:
227 INP_UNLOCK(inp);
228 INP_INFO_WUNLOCK(&dccpbinfo);
229 return error;
230 }
231
232
233 int
234 dccp6_listen(struct socket *so, struct lwp *l)
235 {
236 struct in6pcb *in6p;
237 struct dccpcb *dp;
238 int error = 0;
239
240 DCCP_DEBUG((LOG_INFO, "Entering dccp6_listen!\n"));
241
242 INP_INFO_RLOCK(&dccpbinfo);
243 in6p = sotoin6pcb(so);
244 if (in6p == 0) {
245 INP_INFO_RUNLOCK(&dccpbinfo);
246 return EINVAL;
247 }
248 INP_LOCK(inp);
249 INP_INFO_RUNLOCK(&dccpbinfo);
250 dp = in6todccpcb(in6p);
251 DCCP_DEBUG((LOG_INFO, "Checking in6p->in6p_lport!\n"));
252 if (in6p->in6p_lport == 0) {
253 error = in6_pcbbind(in6p, NULL, l);
254 }
255 if (error == 0) {
256 dp->state = DCCPS_LISTEN;
257 dp->who = DCCP_LISTENER;
258 dp->seq_snd = 512;
259 }
260 INP_UNLOCK(inp);
261 return error;
262 }
263
264 int
265 dccp6_accept(struct socket *so, struct sockaddr *nam)
266 {
267 struct in6pcb *in6p = NULL;
268 int error = 0;
269
270 DCCP_DEBUG((LOG_INFO, "Entering dccp6_accept!\n"));
271 if (nam == NULL) {
272 return EINVAL;
273 }
274 if (so->so_state & SS_ISDISCONNECTED) {
275 DCCP_DEBUG((LOG_INFO, "so_state && SS_ISDISCONNECTED!, so->state = %i\n", so->so_state));
276 return ECONNABORTED;
277 }
278
279 INP_INFO_RLOCK(&dccpbinfo);
280 in6p = sotoin6pcb(so);
281 if (in6p == 0) {
282 INP_INFO_RUNLOCK(&dccpbinfo);
283 return EINVAL;
284 }
285 INP_LOCK(inp);
286 INP_INFO_RUNLOCK(&dccpbinfo);
287 in6_setpeeraddr(in6p, (struct sockaddr_in6 *)nam);
288
289 INP_UNLOCK(inp);
290 return error;
291 }
292
293 static int
294 dccp6_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
295 {
296 int error = 0;
297 int family;
298
299 family = so->so_proto->pr_domain->dom_family;
300 switch (family) {
301 case PF_INET6:
302 error = in6_control(so, cmd, nam, ifp);
303 break;
304 default:
305 error = EAFNOSUPPORT;
306 }
307 return (error);
308 }
309
310 static int
311 dccp6_stat(struct socket *so, struct stat *ub)
312 {
313 return 0;
314 }
315
316 static int
317 dccp6_purgeif(struct socket *so, struct ifnet *ifp)
318 {
319 int s;
320
321 s = splsoftnet();
322 mutex_enter(softnet_lock);
323 in6_pcbpurgeif0(&dccpbtable, ifp);
324 in6_purgeif(ifp);
325 in6_pcbpurgeif(&dccpbtable, ifp);
326 mutex_exit(softnet_lock);
327 splx(s);
328
329 return 0;
330 }
331
332 int
333 dccp6_usrreq(struct socket *so, int req, struct mbuf *m,
334 struct mbuf *nam, struct mbuf *control, struct lwp *l)
335 {
336 int error = 0;
337
338 KASSERT(req != PRU_ATTACH);
339 KASSERT(req != PRU_DETACH);
340 KASSERT(req != PRU_ACCEPT);
341 KASSERT(req != PRU_BIND);
342 KASSERT(req != PRU_LISTEN);
343 KASSERT(req != PRU_CONNECT);
344 KASSERT(req != PRU_CONNECT2);
345 KASSERT(req != PRU_DISCONNECT);
346 KASSERT(req != PRU_SHUTDOWN);
347 KASSERT(req != PRU_ABORT);
348 KASSERT(req != PRU_CONTROL);
349 KASSERT(req != PRU_SENSE);
350 KASSERT(req != PRU_PEERADDR);
351 KASSERT(req != PRU_SOCKADDR);
352 KASSERT(req != PRU_RCVD);
353 KASSERT(req != PRU_RCVOOB);
354 KASSERT(req != PRU_SEND);
355 KASSERT(req != PRU_SENDOOB);
356 KASSERT(req != PRU_PURGEIF);
357
358 if (sotoin6pcb(so) == NULL) {
359 error = EINVAL;
360 goto release;
361 }
362
363 switch (req) {
364 case PRU_FASTTIMO:
365 case PRU_SLOWTIMO:
366 case PRU_PROTORCV:
367 case PRU_PROTOSEND:
368 error = EOPNOTSUPP;
369 break;
370
371 default:
372 panic("dccp6_usrreq");
373 }
374
375 release:
376 if (control != NULL)
377 m_freem(control);
378 if (m != NULL)
379 m_freem(m);
380 return error;
381 }
382
383 static int
384 dccp6_attach(struct socket *so, int proto)
385 {
386 return dccp_attach(so, proto);
387 }
388
389 static int
390 dccp6_detach(struct socket *so)
391 {
392 return dccp_detach(so);
393 }
394
395 static int
396 dccp6_connect2(struct socket *so, struct socket *so2)
397 {
398 KASSERT(solocked(so));
399
400 return EOPNOTSUPP;
401 }
402
403 static int
404 dccp6_disconnect(struct socket *so)
405 {
406 return dccp_disconnect(so);
407 }
408
409 static int
410 dccp6_shutdown(struct socket *so)
411 {
412 return dccp_shutdown(so);
413 }
414
415 static int
416 dccp6_abort(struct socket *so)
417 {
418 return dccp_abort(so);
419 }
420
421
422 static int
423 dccp6_peeraddr(struct socket *so, struct sockaddr *nam)
424 {
425 KASSERT(solocked(so));
426 KASSERT(sotoinpcb(so) != NULL);
427 KASSERT(nam != NULL);
428
429 in6_setpeeraddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam);
430 return 0;
431 }
432
433 static int
434 dccp6_sockaddr(struct socket *so, struct sockaddr *nam)
435 {
436 KASSERT(solocked(so));
437 KASSERT(sotoinpcb(so) != NULL);
438 KASSERT(nam != NULL);
439
440 in6_setsockaddr(sotoin6pcb(so), (struct sockaddr_in6 *)nam);
441 return 0;
442 }
443
444 static int
445 dccp6_recvoob(struct socket *so, struct mbuf *m, int flags)
446 {
447 KASSERT(solocked(so));
448
449 return EOPNOTSUPP;
450 }
451
452 static int
453 dccp6_rcvd(struct socket *so, int flags, struct lwp *l)
454 {
455 KASSERT(solocked(so));
456
457 return EOPNOTSUPP;
458 }
459
460 static int
461 dccp6_send(struct socket *so, struct mbuf *m, struct mbuf *nam,
462 struct mbuf *control, struct lwp *l)
463 {
464 return dccp_send(so, m, nam, control, l);
465 }
466
467 static int
468 dccp6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
469 {
470 KASSERT(solocked(so));
471
472 m_freem(m);
473 m_freem(control);
474
475 return EOPNOTSUPP;
476 }
477
478
479 PR_WRAP_USRREQS(dccp6)
480 #define dccp6_attach dccp6_attach_wrapper
481 #define dccp6_detach dccp6_detach_wrapper
482 #define dccp6_accept dccp6_accept_wrapper
483 #define dccp6_bind dccp6_bind_wrapper
484 #define dccp6_listen dccp6_listen_wrapper
485 #define dccp6_connect dccp6_connect_wrapper
486 #define dccp6_connect2 dccp6_connect2_wrapper
487 #define dccp6_disconnect dccp6_disconnect_wrapper
488 #define dccp6_shutdown dccp6_shutdown_wrapper
489 #define dccp6_abort dccp6_abort_wrapper
490 #define dccp6_ioctl dccp6_ioctl_wrapper
491 #define dccp6_stat dccp6_stat_wrapper
492 #define dccp6_peeraddr dccp6_peeraddr_wrapper
493 #define dccp6_sockaddr dccp6_sockaddr_wrapper
494 #define dccp6_rcvd dccp6_rcvd_wrapper
495 #define dccp6_recvoob dccp6_recvoob_wrapper
496 #define dccp6_send dccp6_send_wrapper
497 #define dccp6_sendoob dccp6_sendoob_wrapper
498 #define dccp6_purgeif dccp6_purgeif_wrapper
499 #define dccp6_usrreq dccp6_usrreq_wrapper
500
501 const struct pr_usrreqs dccp6_usrreqs = {
502 .pr_attach = dccp6_attach,
503 .pr_detach = dccp6_detach,
504 .pr_accept = dccp6_accept,
505 .pr_bind = dccp6_bind,
506 .pr_listen = dccp6_listen,
507 .pr_connect = dccp6_connect,
508 .pr_connect2 = dccp6_connect2,
509 .pr_disconnect = dccp6_disconnect,
510 .pr_shutdown = dccp6_shutdown,
511 .pr_abort = dccp6_abort,
512 .pr_ioctl = dccp6_ioctl,
513 .pr_stat = dccp6_stat,
514 .pr_peeraddr = dccp6_peeraddr,
515 .pr_sockaddr = dccp6_sockaddr,
516 .pr_rcvd = dccp6_rcvd,
517 .pr_recvoob = dccp6_recvoob,
518 .pr_send = dccp6_send,
519 .pr_sendoob = dccp6_sendoob,
520 .pr_purgeif = dccp6_purgeif,
521 .pr_generic = dccp6_usrreq,
522 };
523