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