FreeBSD/Linux Kernel Cross Reference
sys/netnatm/natm.c
1 /*-
2 * Copyright (c) 2005-2006 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * Copyright (c) 1996 Charles D. Cranor and Washington University.
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by Charles D. Cranor and
40 * Washington University.
41 * 4. The name of the author may not be used to endorse or promote products
42 * derived from this software without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
48 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
49 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
53 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 *
55 * $NetBSD: natm.c,v 1.5 1996/11/09 03:26:26 chuck Exp $
56 */
57
58 /*
59 * natm.c: Native mode ATM access (both aal0 and aal5).
60 */
61
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
64
65 #include <sys/param.h>
66 #include <sys/conf.h>
67 #include <sys/kernel.h>
68 #include <sys/lock.h>
69 #include <sys/malloc.h>
70 #include <sys/mbuf.h>
71 #include <sys/protosw.h>
72 #include <sys/signalvar.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/sockio.h>
76 #include <sys/sx.h>
77 #include <sys/systm.h>
78 #include <sys/sysctl.h>
79
80 #include <net/if.h>
81 #include <net/if_atm.h>
82 #include <net/netisr.h>
83
84 #include <netinet/in.h>
85
86 #include <netnatm/natm.h>
87
88 static const u_long natm5_sendspace = 16*1024;
89 static const u_long natm5_recvspace = 16*1024;
90
91 static const u_long natm0_sendspace = 16*1024;
92 static const u_long natm0_recvspace = 16*1024;
93
94 /*
95 * netnatm global subsystem lock, protects all global data structures in
96 * netnatm.
97 */
98 struct mtx natm_mtx;
99
100 /*
101 * User socket requests.
102 */
103 static int natm_usr_attach(struct socket *, int, d_thread_t *);
104 static void natm_usr_detach(struct socket *);
105 static int natm_usr_connect(struct socket *, struct sockaddr *,
106 d_thread_t *);
107 static int natm_usr_disconnect(struct socket *);
108 static int natm_usr_shutdown(struct socket *);
109 static int natm_usr_send(struct socket *, int, struct mbuf *,
110 struct sockaddr *, struct mbuf *, d_thread_t *);
111 static int natm_usr_peeraddr(struct socket *, struct sockaddr **);
112 static int natm_usr_control(struct socket *, u_long, caddr_t,
113 struct ifnet *, d_thread_t *);
114 static void natm_usr_abort(struct socket *);
115 static int natm_usr_bind(struct socket *, struct sockaddr *,
116 d_thread_t *);
117 static int natm_usr_sockaddr(struct socket *, struct sockaddr **);
118
119 static int
120 natm_usr_attach(struct socket *so, int proto, d_thread_t *p)
121 {
122 struct natmpcb *npcb;
123 int error = 0;
124
125 npcb = (struct natmpcb *)so->so_pcb;
126 KASSERT(npcb == NULL, ("natm_usr_attach: so_pcb != NULL"));
127
128 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
129 if (proto == PROTO_NATMAAL5)
130 error = soreserve(so, natm5_sendspace,
131 natm5_recvspace);
132 else
133 error = soreserve(so, natm0_sendspace,
134 natm0_recvspace);
135 if (error)
136 return (error);
137 }
138 so->so_pcb = npcb = npcb_alloc(M_WAITOK);
139 npcb->npcb_socket = so;
140 return (error);
141 }
142
143 static void
144 natm_usr_detach(struct socket *so)
145 {
146 struct natmpcb *npcb;
147
148 npcb = (struct natmpcb *)so->so_pcb;
149 KASSERT(npcb != NULL, ("natm_usr_detach: npcb == NULL"));
150
151 NATM_LOCK();
152 npcb_free(npcb, NPCB_DESTROY); /* drain */
153 so->so_pcb = NULL;
154 NATM_UNLOCK();
155 }
156
157 static int
158 natm_usr_connect(struct socket *so, struct sockaddr *nam, d_thread_t *p)
159 {
160 struct natmpcb *npcb;
161 struct sockaddr_natm *snatm;
162 struct atmio_openvcc op;
163 struct ifnet *ifp;
164 int error = 0;
165 int proto = so->so_proto->pr_protocol;
166
167 npcb = (struct natmpcb *)so->so_pcb;
168 KASSERT(npcb != NULL, ("natm_usr_connect: npcb == NULL"));
169
170 /*
171 * Validate nam and npcb.
172 */
173 NATM_LOCK();
174 snatm = (struct sockaddr_natm *)nam;
175 if (snatm->snatm_len != sizeof(*snatm) ||
176 (npcb->npcb_flags & NPCB_FREE) == 0) {
177 NATM_UNLOCK();
178 return (EINVAL);
179 }
180 if (snatm->snatm_family != AF_NATM) {
181 NATM_UNLOCK();
182 return (EAFNOSUPPORT);
183 }
184
185 snatm->snatm_if[IFNAMSIZ - 1] = '\0'; /* XXX ensure null termination
186 since ifunit() uses strcmp */
187
188 /*
189 * Convert interface string to ifp, validate.
190 */
191 ifp = ifunit(snatm->snatm_if);
192 if (ifp == NULL || (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
193 NATM_UNLOCK();
194 return (ENXIO);
195 }
196 if (ifp->if_output != atm_output) {
197 NATM_UNLOCK();
198 return (EAFNOSUPPORT);
199 }
200
201 /*
202 * Register us with the NATM PCB layer.
203 */
204 if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) {
205 NATM_UNLOCK();
206 return (EADDRINUSE);
207 }
208
209 /*
210 * Open the channel.
211 *
212 * XXXRW: Eventually desirable to hold mutex over ioctl?
213 */
214 bzero(&op, sizeof(op));
215 op.rxhand = npcb;
216 op.param.flags = ATMIO_FLAG_PVC;
217 op.param.vpi = npcb->npcb_vpi;
218 op.param.vci = npcb->npcb_vci;
219 op.param.rmtu = op.param.tmtu = ifp->if_mtu;
220 op.param.aal = (proto == PROTO_NATMAAL5) ? ATMIO_AAL_5 : ATMIO_AAL_0;
221 op.param.traffic = ATMIO_TRAFFIC_UBR;
222 NATM_UNLOCK();
223
224 IFF_LOCKGIANT(ifp);
225 if (ifp->if_ioctl == NULL ||
226 ifp->if_ioctl(ifp, SIOCATMOPENVCC, (caddr_t)&op) != 0) {
227 IFF_UNLOCKGIANT(ifp);
228 return (EIO);
229 }
230 IFF_UNLOCKGIANT(ifp);
231 soisconnected(so);
232 return (error);
233 }
234
235 static int
236 natm_usr_disconnect(struct socket *so)
237 {
238 struct natmpcb *npcb;
239 struct atmio_closevcc cl;
240 struct ifnet *ifp;
241 int error = 0;
242
243 npcb = (struct natmpcb *)so->so_pcb;
244 KASSERT(npcb != NULL, ("natm_usr_disconnect: npcb == NULL"));
245
246 NATM_LOCK();
247 if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
248 NATM_UNLOCK();
249 printf("natm: disconnected check\n");
250 return (EIO);
251 }
252 ifp = npcb->npcb_ifp;
253
254 /*
255 * Disable rx.
256 *
257 * XXXRW: Eventually desirable to hold mutex over ioctl?
258 */
259 cl.vpi = npcb->npcb_vpi;
260 cl.vci = npcb->npcb_vci;
261 NATM_UNLOCK();
262 if (ifp->if_ioctl != NULL) {
263 IFF_LOCKGIANT(ifp);
264 ifp->if_ioctl(ifp, SIOCATMCLOSEVCC, (caddr_t)&cl);
265 IFF_UNLOCKGIANT(ifp);
266 }
267 soisdisconnected(so);
268 return (error);
269 }
270
271 static int
272 natm_usr_shutdown(struct socket *so)
273 {
274
275 socantsendmore(so);
276 return (0);
277 }
278
279 static int
280 natm_usr_send(struct socket *so, int flags, struct mbuf *m,
281 struct sockaddr *nam, struct mbuf *control, d_thread_t *p)
282 {
283 struct natmpcb *npcb;
284 struct atm_pseudohdr *aph;
285 int error = 0;
286 int proto = so->so_proto->pr_protocol;
287
288 npcb = (struct natmpcb *)so->so_pcb;
289 KASSERT(npcb != NULL, ("natm_usr_send: npcb == NULL"));
290
291 NATM_LOCK();
292 if (control && control->m_len) {
293 NATM_UNLOCK();
294 m_freem(control);
295 m_freem(m);
296 return (EINVAL);
297 }
298
299 /*
300 * Send the data. We must put an atm_pseudohdr on first.
301 */
302 M_PREPEND(m, sizeof(*aph), M_DONTWAIT);
303 if (m == NULL) {
304 NATM_UNLOCK();
305 m_freem(control);
306 return (ENOBUFS);
307 }
308 aph = mtod(m, struct atm_pseudohdr *);
309 ATM_PH_VPI(aph) = npcb->npcb_vpi;
310 ATM_PH_SETVCI(aph, npcb->npcb_vci);
311 ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
312 error = atm_output(npcb->npcb_ifp, m, NULL, NULL);
313 NATM_UNLOCK();
314 return (error);
315 }
316
317 static int
318 natm_usr_peeraddr(struct socket *so, struct sockaddr **nam)
319 {
320 struct natmpcb *npcb;
321 struct sockaddr_natm *snatm, ssnatm;
322
323 npcb = (struct natmpcb *)so->so_pcb;
324 KASSERT(npcb != NULL, ("natm_usr_peeraddr: npcb == NULL"));
325
326 NATM_LOCK();
327 snatm = &ssnatm;
328 bzero(snatm, sizeof(*snatm));
329 snatm->snatm_len = sizeof(*snatm);
330 snatm->snatm_family = AF_NATM;
331 strlcpy(snatm->snatm_if, npcb->npcb_ifp->if_xname,
332 sizeof(snatm->snatm_if));
333 snatm->snatm_vci = npcb->npcb_vci;
334 snatm->snatm_vpi = npcb->npcb_vpi;
335 NATM_UNLOCK();
336 *nam = sodupsockaddr((struct sockaddr *)snatm, M_WAITOK);
337 return (0);
338 }
339
340 static int
341 natm_usr_control(struct socket *so, u_long cmd, caddr_t arg,
342 struct ifnet *ifp, d_thread_t *p)
343 {
344 struct natmpcb *npcb;
345 int error;
346
347 npcb = (struct natmpcb *)so->so_pcb;
348 KASSERT(npcb != NULL, ("natm_usr_control: npcb == NULL"));
349
350 if (ifp == NULL || ifp->if_ioctl == NULL)
351 return (EOPNOTSUPP);
352 IFF_LOCKGIANT(ifp);
353 error = ((*ifp->if_ioctl)(ifp, cmd, arg));
354 IFF_UNLOCKGIANT(ifp);
355 return (error);
356 }
357
358 static void
359 natm_usr_abort(struct socket *so)
360 {
361
362 }
363
364 static void
365 natm_usr_close(struct socket *so)
366 {
367
368 }
369
370 static int
371 natm_usr_bind(struct socket *so, struct sockaddr *nam, d_thread_t *p)
372 {
373
374 return (EOPNOTSUPP);
375 }
376
377 static int
378 natm_usr_sockaddr(struct socket *so, struct sockaddr **nam)
379 {
380
381 return (EOPNOTSUPP);
382 }
383
384 /* xxx - should be const */
385 struct pr_usrreqs natm_usrreqs = {
386 .pru_abort = natm_usr_abort,
387 .pru_attach = natm_usr_attach,
388 .pru_bind = natm_usr_bind,
389 .pru_connect = natm_usr_connect,
390 .pru_control = natm_usr_control,
391 .pru_detach = natm_usr_detach,
392 .pru_disconnect = natm_usr_disconnect,
393 .pru_peeraddr = natm_usr_peeraddr,
394 .pru_send = natm_usr_send,
395 .pru_shutdown = natm_usr_shutdown,
396 .pru_sockaddr = natm_usr_sockaddr,
397 .pru_close = natm_usr_close,
398 };
399
400 /*
401 * natmintr: interrupt
402 *
403 * Note: we expect a socket pointer in rcvif rather than an interface
404 * pointer. We can get the interface pointer from the so's PCB if we really
405 * need it.
406 */
407 void
408 natmintr(struct mbuf *m)
409 {
410 struct socket *so;
411 struct natmpcb *npcb;
412
413 #ifdef DIAGNOSTIC
414 M_ASSERTPKTHDR(m);
415 #endif
416
417 NATM_LOCK();
418 npcb = (struct natmpcb *)m->m_pkthdr.rcvif; /* XXX: overloaded */
419 so = npcb->npcb_socket;
420
421 npcb->npcb_inq--;
422
423 if (npcb->npcb_flags & NPCB_DRAIN) {
424 if (npcb->npcb_inq == 0)
425 FREE(npcb, M_PCB); /* done! */
426 NATM_UNLOCK();
427 m_freem(m);
428 return;
429 }
430
431 if (npcb->npcb_flags & NPCB_FREE) {
432 NATM_UNLOCK();
433 m_freem(m); /* drop */
434 return;
435 }
436
437 #ifdef NEED_TO_RESTORE_IFP
438 m->m_pkthdr.rcvif = npcb->npcb_ifp;
439 #else
440 #ifdef DIAGNOSTIC
441 m->m_pkthdr.rcvif = NULL; /* null it out to be safe */
442 #endif
443 #endif
444
445 if (sbspace(&so->so_rcv) > m->m_pkthdr.len) {
446 #ifdef NATM_STAT
447 natm_sookcnt++;
448 natm_sookbytes += m->m_pkthdr.len;
449 #endif
450 sbappendrecord(&so->so_rcv, m);
451 sorwakeup(so);
452 NATM_UNLOCK();
453 } else {
454 #ifdef NATM_STAT
455 natm_sodropcnt++;
456 natm_sodropbytes += m->m_pkthdr.len;
457 #endif
458 NATM_UNLOCK();
459 m_freem(m);
460 }
461 }
462
463 /*
464 * natm0_sysctl: not used, but here in case we want to add something
465 * later...
466 */
467 int
468 natm0_sysctl(SYSCTL_HANDLER_ARGS)
469 {
470
471 /* All sysctl names at this level are terminal. */
472 return (ENOENT);
473 }
474
475 /*
476 * natm5_sysctl: not used, but here in case we want to add something
477 * later...
478 */
479 int
480 natm5_sysctl(SYSCTL_HANDLER_ARGS)
481 {
482
483 /* All sysctl names at this level are terminal. */
484 return (ENOENT);
485 }
Cache object: de2ce746e5d2c8c8d9e0f4cc734cae3f
|