network - Completely revamp the netisr / dispatch code
[dragonfly.git] / sys / netproto / natm / natm.c
1 /*      $NetBSD: natm.c,v 1.5 1996/11/09 03:26:26 chuck Exp $   */
2 /* $FreeBSD: src/sys/netnatm/natm.c,v 1.12 2000/02/13 03:32:03 peter Exp $ */
3 /* $DragonFly: src/sys/netproto/natm/natm.c,v 1.31 2008/09/24 14:26:39 sephe Exp $ */
4
5 /*
6  *
7  * Copyright (c) 1996 Charles D. Cranor and Washington University.
8  * All rights reserved.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by Charles D. Cranor and
21  *      Washington University.
22  * 4. The name of the author may not be used to endorse or promote products
23  *    derived from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 /*
38  * natm.c: native mode ATM access (both aal0 and aal5).
39  */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/proc.h>
45 #include <sys/sockio.h>
46 #include <sys/protosw.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51
52 #include <sys/thread2.h>
53 #include <sys/msgport2.h>
54 #include <sys/mplock2.h>
55
56 #include <net/if.h>
57 #include <net/if_atm.h>
58 #include <net/netisr.h>
59
60 #include <netinet/in.h>
61
62 #include "natm.h"
63
64 static u_long natm5_sendspace = 16*1024;
65 static u_long natm5_recvspace = 16*1024;
66
67 static u_long natm0_sendspace = 16*1024;
68 static u_long natm0_recvspace = 16*1024;
69
70 /*
71  * user requests
72  */
73 #ifdef FREEBSD_USRREQS
74 /*
75  * FreeBSD new usrreqs supersedes pr_usrreq.
76  */
77 static int natm_usr_attach (struct socket *, int, struct pru_attach_info *);
78 static int natm_usr_detach (struct socket *);
79 static int natm_usr_connect (struct socket *, struct sockaddr *,
80                                  struct thread *);
81 static int natm_usr_disconnect (struct socket *);
82 static int natm_usr_shutdown (struct socket *);
83 static int natm_usr_send (struct socket *, int, struct mbuf *,
84                               struct sockaddr *, struct mbuf *, 
85                               struct thread *);
86 static int natm_usr_peeraddr (struct socket *, struct sockaddr **);
87 static int natm_usr_control (struct socket *, u_long, caddr_t,
88                                  struct ifnet *, struct thread *);
89 static int natm_usr_abort (struct socket *);
90 static int natm_usr_bind (struct socket *, struct sockaddr *, 
91                               struct thread *);
92 static int natm_usr_sockaddr (struct socket *, struct sockaddr **);
93
94 static int
95 natm_usr_attach(struct socket *so, int proto, struct pru_attach_info *ai)
96 {
97     struct natmpcb *npcb;
98     int error = 0;
99
100     crit_enter();
101     npcb = (struct natmpcb *) so->so_pcb;
102
103     if (npcb) {
104         error = EISCONN;
105         goto out;
106     }
107
108     if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
109         if (proto == PROTO_NATMAAL5) 
110             error = soreserve(so, natm5_sendspace, natm5_recvspace,
111                               ai->sb_rlimit);
112         else
113             error = soreserve(so, natm0_sendspace, natm0_recvspace,
114                               ai->sb_rlimit);
115         if (error)
116           goto out;
117     }
118
119     so->so_pcb = (caddr_t) (npcb = npcb_alloc(M_WAITOK));
120     npcb->npcb_socket = so;
121  out:
122     crit_exit();
123     return (error);
124 }
125
126 static int
127 natm_usr_detach(struct socket *so)
128 {
129     struct natmpcb *npcb;
130     int error = 0;
131
132     crit_enter();
133     npcb = (struct natmpcb *) so->so_pcb;
134     if (npcb == NULL) {
135         error = EINVAL;
136         goto out;
137     }
138
139     /*
140      * we turn on 'drain' *before* we sofree.
141      */
142     npcb_free(npcb, NPCB_DESTROY);      /* drain */
143     so->so_pcb = NULL;
144     sofree(so);
145  out:
146     crit_exit();
147     return (error);
148 }
149
150 static int
151 natm_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
152 {
153     struct natmpcb *npcb;
154     struct sockaddr_natm *snatm;
155     struct atm_pseudoioctl api;
156     struct ifnet *ifp;
157     int error = 0;
158     int proto;
159
160     crit_enter();
161     proto = so->so_proto->pr_protocol;
162     npcb = (struct natmpcb *) so->so_pcb;
163     if (npcb == NULL) {
164         error = EINVAL;
165         goto out;
166     }
167
168     /*
169      * validate nam and npcb
170      */
171
172     snatm = (struct sockaddr_natm *)nam;
173     if (snatm->snatm_len != sizeof(*snatm) ||
174         (npcb->npcb_flags & NPCB_FREE) == 0) {
175         error = EINVAL;
176         goto out;
177     }
178     if (snatm->snatm_family != AF_NATM) {
179         error = EAFNOSUPPORT;
180         goto out;
181     }
182
183     snatm->snatm_if[IFNAMSIZ-1] = '\0';  /* XXX ensure null termination
184                                             since ifunit() uses strcmp */
185
186     /*
187      * convert interface string to ifp, validate.
188      */
189
190     ifp = ifunit(snatm->snatm_if);
191     if (ifp == NULL || (ifp->if_flags & IFF_RUNNING) == 0) {
192         error = ENXIO;
193         goto out;
194     }
195     if (ifp->if_output != atm_output) {
196         error = EAFNOSUPPORT;
197         goto out;
198     }
199
200     /*
201      * register us with the NATM PCB layer
202      */
203
204     if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) {
205         error = EADDRINUSE;
206         goto out;
207     }
208
209     /*
210      * enable rx
211      */
212
213     ATM_PH_FLAGS(&api.aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
214     ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
215     ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
216     api.rxhand = npcb;
217     ifnet_serialize_all(ifp);
218     if (ifp->if_ioctl == NULL || 
219         ifp->if_ioctl(ifp, SIOCATMENA, (caddr_t) &api,
220                       td->td_proc->p_ucred) != 0) {
221         ifnet_deserialize_all(ifp);
222         npcb_free(npcb, NPCB_REMOVE);
223         error = EIO;
224         goto out;
225     }
226     ifnet_deserialize_all(ifp);
227
228     soisconnected(so);
229
230  out:
231     crit_exit();
232     return (error);
233 }
234
235 static int
236 natm_usr_disconnect(struct socket *so)
237 {
238     struct natmpcb *npcb;
239     struct atm_pseudoioctl api;
240     struct ifnet *ifp;
241     int error = 0;
242
243     crit_enter();
244     npcb = (struct natmpcb *) so->so_pcb;
245     if (npcb == NULL) {
246         error = EINVAL;
247         goto out;
248     }
249
250     if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
251         kprintf("natm: disconnected check\n");
252         error = EIO;
253         goto out;
254     }
255     ifp = npcb->npcb_ifp;
256
257     /*
258      * disable rx
259      */
260
261     ATM_PH_FLAGS(&api.aph) = ATM_PH_AAL5;
262     ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
263     ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
264     api.rxhand = npcb;
265     if (ifp->if_ioctl != NULL) {
266         ifnet_serialize_all(ifp);
267         ifp->if_ioctl(ifp, SIOCATMDIS, (caddr_t) &api, NULL);
268         ifnet_deserialize_all(ifp);
269     }
270
271     npcb_free(npcb, NPCB_REMOVE);
272     soisdisconnected(so);
273
274  out:
275     crit_exit();
276     return (error);
277 }
278
279 static int
280 natm_usr_shutdown(struct socket *so)
281 {
282     socantsendmore(so);
283     return 0;
284 }
285
286 static int
287 natm_usr_send(struct socket *so, int flags, struct mbuf *m, 
288               struct sockaddr *nam, struct mbuf *control, struct thread *td)
289 {
290     struct natmpcb *npcb;
291     struct atm_pseudohdr *aph;
292     int error = 0;
293     int proto = so->so_proto->pr_protocol;
294
295     crit_enter();
296
297     npcb = (struct natmpcb *) so->so_pcb;
298     if (npcb == NULL) {
299         error = EINVAL;
300         goto out;
301     }
302
303     if (control && control->m_len) {
304         m_freem(control);
305         m_freem(m);
306         error = EINVAL;
307         goto out;
308     }
309
310     /*
311      * send the data.   we must put an atm_pseudohdr on first
312      */
313
314     M_PREPEND(m, sizeof(*aph), M_WAITOK);
315     if (m == NULL) {
316         error = ENOBUFS;
317         goto out;
318     }
319     aph = mtod(m, struct atm_pseudohdr *);
320     ATM_PH_VPI(aph) = npcb->npcb_vpi;
321     ATM_PH_SETVCI(aph, npcb->npcb_vci);
322     ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
323
324     error = atm_output(npcb->npcb_ifp, m, NULL, NULL);
325
326  out:
327     crit_exit();
328     return (error);
329 }
330
331 static int
332 natm_usr_peeraddr(struct socket *so, struct sockaddr **nam)
333 {
334     struct natmpcb *npcb;
335     struct sockaddr_natm *snatm, ssnatm;
336     int error = 0;
337
338     crit_enter();
339     npcb = (struct natmpcb *) so->so_pcb;
340     if (npcb == NULL) {
341         error = EINVAL;
342         goto out;
343     }
344
345     snatm = &ssnatm;
346     bzero(snatm, sizeof(*snatm));
347     snatm->snatm_len = sizeof(*snatm);
348     snatm->snatm_family = AF_NATM;
349     strlcpy(snatm->snatm_if, npcb->npcb_ifp->if_xname,
350         sizeof(snatm->snatm_if));
351     snatm->snatm_vci = npcb->npcb_vci;
352     snatm->snatm_vpi = npcb->npcb_vpi;
353     *nam = dup_sockaddr((struct sockaddr *)snatm);
354
355  out:
356     crit_exit();
357     return (error);
358 }
359
360 static int
361 natm_usr_control(struct socket *so, u_long cmd, caddr_t arg,
362                  struct ifnet *ifp, struct thread *td)
363 {
364     struct natmpcb *npcb;
365     struct atm_rawioctl ario;
366     int error = 0;
367
368     npcb = (struct natmpcb *) so->so_pcb;
369     if (npcb == NULL) {
370         error = EINVAL;
371         goto out;
372     }
373
374     /*
375      * raw atm ioctl.   comes in as a SIOCRAWATM.   we convert it to
376      * SIOCXRAWATM and pass it to the driver.
377      */
378     if (cmd == SIOCRAWATM) {
379         if (npcb->npcb_ifp == NULL) {
380             error = ENOTCONN;
381             goto out;
382         }
383         ario.npcb = npcb;
384         ario.rawvalue = *((int *)arg);
385         ifnet_serialize_all(npcb->npcb_ifp);
386         error = npcb->npcb_ifp->if_ioctl(npcb->npcb_ifp, 
387                                          SIOCXRAWATM, (caddr_t) &ario,
388                                          td->td_proc->p_ucred);
389         ifnet_deserialize_all(npcb->npcb_ifp);
390         if (!error) {
391             if (ario.rawvalue) 
392                 npcb->npcb_flags |= NPCB_RAW;
393             else
394                 npcb->npcb_flags &= ~(NPCB_RAW);
395         }
396     }
397     else
398         error = EOPNOTSUPP;
399  out:
400     return (error);
401 }
402
403 static int
404 natm_usr_abort(struct socket *so)
405 {
406     return natm_usr_shutdown(so);
407 }
408
409 static int
410 natm_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
411 {
412     return EOPNOTSUPP;
413 }
414
415 static int
416 natm_usr_sockaddr(struct socket *so, struct sockaddr **nam)
417 {
418     return EOPNOTSUPP;
419 }
420
421 /* xxx - should be const */
422 struct pr_usrreqs natm_usrreqs = {
423         .pru_abort = natm_usr_abort,
424         .pru_accept = pru_accept_notsupp,
425         .pru_attach = natm_usr_attach,
426         .pru_bind = natm_usr_bind,
427         .pru_connect = natm_usr_connect,
428         .pru_connect2 = pru_connect2_notsupp,
429         .pru_control = natm_usr_control,
430         .pru_detach = natm_usr_detach,
431         .pru_disconnect = natm_usr_disconnect,
432         .pru_listen = pru_listen_notsupp,
433         .pru_peeraddr = natm_usr_peeraddr,
434         .pru_rcvd = pru_rcvd_notsupp,
435         .pru_rcvoob = pru_rcvoob_notsupp,
436         .pru_send = natm_usr_send,
437         .pru_sense = pru_sense_null,
438         .pru_shutdown = natm_usr_shutdown,
439         .pru_sockaddr = natm_usr_sockaddr,
440         .pru_sosend = sosend,
441         .pru_soreceive = soreceive
442 };
443
444 #else  /* !FREEBSD_USRREQS */
445
446 #if defined(__NetBSD__) || defined(__OpenBSD__)
447 int
448 natm_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
449             struct mbuf *control, struct proc *p)
450 #elif defined(__DragonFly__)
451 int
452 natm_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
453             struct mbuf *control)
454 #endif
455 {
456   int error = 0;
457   struct natmpcb *npcb;
458   struct sockaddr_natm *snatm;
459   struct atm_pseudoioctl api;
460   struct atm_pseudohdr *aph;
461   struct atm_rawioctl ario;
462   struct ifnet *ifp;
463   int proto = so->so_proto->pr_protocol;
464
465   crit_enter();
466
467   npcb = (struct natmpcb *) so->so_pcb;
468
469   if (npcb == NULL && req != PRU_ATTACH) {
470     error = EINVAL;
471     goto done;
472   }
473     
474
475   switch (req) {
476     case PRU_ATTACH:                    /* attach protocol to up */
477
478       if (npcb) {
479         error = EISCONN;
480         break;
481       }
482
483       if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) {
484         if (proto == PROTO_NATMAAL5) 
485           error = soreserve(so, natm5_sendspace, natm5_recvspace);
486         else
487           error = soreserve(so, natm0_sendspace, natm0_recvspace);
488         if (error)
489           break;
490       }
491
492       so->so_pcb = (caddr_t) (npcb = npcb_alloc(M_WAITOK));
493       npcb->npcb_socket = so;
494
495       break;
496
497     case PRU_DETACH:                    /* detach protocol from up */
498
499       /*
500        * we turn on 'drain' *before* we sofree.
501        */
502
503       npcb_free(npcb, NPCB_DESTROY);    /* drain */
504       so->so_pcb = NULL;
505       sofree(so);
506
507       break;
508
509     case PRU_CONNECT:                   /* establish connection to peer */
510
511       /*
512        * validate nam and npcb
513        */
514
515       if (nam->m_len != sizeof(*snatm)) {
516         error = EINVAL;
517         break;
518       }
519       snatm = mtod(nam, struct sockaddr_natm *);
520       if (snatm->snatm_len != sizeof(*snatm) ||
521                 (npcb->npcb_flags & NPCB_FREE) == 0) {
522         error = EINVAL;
523         break;
524       }
525       if (snatm->snatm_family != AF_NATM) {
526         error = EAFNOSUPPORT;
527         break;
528       }
529
530       snatm->snatm_if[IFNAMSIZ-1] = '\0';  /* XXX ensure null termination
531                                                 since ifunit() uses strcmp */
532
533       /*
534        * convert interface string to ifp, validate.
535        */
536
537       ifp = ifunit(snatm->snatm_if);
538       if (ifp == NULL || (ifp->if_flags & IFF_RUNNING) == 0) {
539         error = ENXIO;
540         break;
541       }
542       if (ifp->if_output != atm_output) {
543         error = EAFNOSUPPORT;
544         break;
545       }
546
547
548       /*
549        * register us with the NATM PCB layer
550        */
551
552       if (npcb_add(npcb, ifp, snatm->snatm_vci, snatm->snatm_vpi) != npcb) {
553         error = EADDRINUSE;
554         break;
555       }
556
557       /*
558        * enable rx
559        */
560
561       ATM_PH_FLAGS(&api.aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
562       ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
563       ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
564       api.rxhand = npcb;
565       ifnet_serialize_all(ifp);
566       if (ifp->if_ioctl == NULL || 
567           ifp->if_ioctl(ifp, SIOCATMENA, (caddr_t) &api, NULL) != 0) {
568         ifnet_deserialize_all(ifp);
569         npcb_free(npcb, NPCB_REMOVE);
570         error = EIO;
571         break;
572       }
573       ifnet_deserialize_all(ifp);
574
575       soisconnected(so);
576
577       break;
578
579     case PRU_DISCONNECT:                /* disconnect from peer */
580
581       if ((npcb->npcb_flags & NPCB_CONNECTED) == 0) {
582         kprintf("natm: disconnected check\n");
583         error = EIO;
584         break;
585       }
586       ifp = npcb->npcb_ifp;
587
588       /*
589        * disable rx
590        */
591
592       ATM_PH_FLAGS(&api.aph) = ATM_PH_AAL5;
593       ATM_PH_VPI(&api.aph) = npcb->npcb_vpi;
594       ATM_PH_SETVCI(&api.aph, npcb->npcb_vci);
595       api.rxhand = npcb;
596       ifnet_serialize_all(ifp);
597       if (ifp->if_ioctl != NULL)
598           ifp->if_ioctl(ifp, SIOCATMDIS, (caddr_t) &api, NULL);
599       ifnet_deserialize_all(ifp);
600
601       npcb_free(npcb, NPCB_REMOVE);
602       soisdisconnected(so);
603
604       break;
605
606     case PRU_SHUTDOWN:                  /* won't send any more data */
607       socantsendmore(so);
608       break;
609
610     case PRU_SEND:                      /* send this data */
611       if (control && control->m_len) {
612         m_freem(control);
613         m_freem(m);
614         error = EINVAL;
615         break;
616       }
617
618       /*
619        * send the data.   we must put an atm_pseudohdr on first
620        */
621
622       M_PREPEND(m, sizeof(*aph), M_WAITOK);
623       if (m == NULL) {
624         error = ENOBUFS;
625         break;
626       }
627       aph = mtod(m, struct atm_pseudohdr *);
628       ATM_PH_VPI(aph) = npcb->npcb_vpi;
629       ATM_PH_SETVCI(aph, npcb->npcb_vci);
630       ATM_PH_FLAGS(aph) = (proto == PROTO_NATMAAL5) ? ATM_PH_AAL5 : 0;
631
632       error = atm_output(npcb->npcb_ifp, m, NULL, NULL);
633
634       break;
635
636     case PRU_SENSE:                     /* return status into m */
637       /* return zero? */
638       break;
639
640     case PRU_PEERADDR:                  /* fetch peer's address */
641       snatm = mtod(nam, struct sockaddr_natm *);
642       bzero(snatm, sizeof(*snatm));
643       nam->m_len = snatm->snatm_len = sizeof(*snatm);
644       snatm->snatm_family = AF_NATM;
645 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
646       bcopy(npcb->npcb_ifp->if_xname, snatm->snatm_if, sizeof(snatm->snatm_if));
647 #elif defined(__DragonFly__)
648       ksnprintf(snatm->snatm_if, sizeof(snatm->snatm_if),
649         "%s%d", npcb->npcb_ifp->if_name, npcb->npcb_ifp->if_unit);
650 #endif
651       snatm->snatm_vci = npcb->npcb_vci;
652       snatm->snatm_vpi = npcb->npcb_vpi;
653       break;
654
655     case PRU_CONTROL:                   /* control operations on protocol */
656       /*
657        * raw atm ioctl.   comes in as a SIOCRAWATM.   we convert it to
658        * SIOCXRAWATM and pass it to the driver.
659        */
660       if ((u_long)m == SIOCRAWATM) {
661         if (npcb->npcb_ifp == NULL) {
662           error = ENOTCONN;
663           break;
664         }
665         ario.npcb = npcb;
666         ario.rawvalue = *((int *)nam);
667         ifnet_serialize_all(npcb->npcb_ifp);
668         error = npcb->npcb_ifp->if_ioctl(npcb->npcb_ifp, SIOCXRAWATM,
669                                          (caddr_t) &ario, NULL);
670         ifnet_deserialize_all(npcb->npcb_ifp);
671         if (!error) {
672           if (ario.rawvalue) 
673             npcb->npcb_flags |= NPCB_RAW;
674           else
675             npcb->npcb_flags &= ~(NPCB_RAW);
676         }
677
678         break;
679       }
680
681       error = EOPNOTSUPP;
682       break;
683
684     case PRU_BIND:                      /* bind socket to address */
685     case PRU_LISTEN:                    /* listen for connection */
686     case PRU_ACCEPT:                    /* accept connection from peer */
687     case PRU_CONNECT2:                  /* connect two sockets */
688     case PRU_ABORT:                     /* abort (fast DISCONNECT, DETATCH) */
689                                         /* (only happens if LISTEN socket) */
690     case PRU_RCVD:                      /* have taken data; more room now */
691     case PRU_FASTTIMO:                  /* 200ms timeout */
692     case PRU_SLOWTIMO:                  /* 500ms timeout */
693     case PRU_RCVOOB:                    /* retrieve out of band data */
694     case PRU_SENDOOB:                   /* send out of band data */
695     case PRU_PROTORCV:                  /* receive from below */
696     case PRU_PROTOSEND:                 /* send to below */
697     case PRU_SOCKADDR:                  /* fetch socket's address */
698 #ifdef DIAGNOSTIC
699       kprintf("natm: PRU #%d unsupported\n", req);
700 #endif
701       error = EOPNOTSUPP;
702       break;
703    
704     default: panic("natm usrreq");
705   }
706
707 done:
708   crit_exit();
709   return(error);
710 }
711
712 #endif  /* !FREEBSD_USRREQS */
713
714 /* 
715  * natm0_sysctl: not used, but here in case we want to add something
716  * later...
717  */
718
719 int
720 natm0_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
721              void *newp, size_t newlen)
722 {
723   /* All sysctl names at this level are terminal. */
724   if (namelen != 1)
725     return (ENOTDIR);
726   return (ENOPROTOOPT);
727 }
728
729 /* 
730  * natm5_sysctl: not used, but here in case we want to add something
731  * later...
732  */
733
734 int
735 natm5_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
736              void *newp, size_t newlen)
737 {
738   /* All sysctl names at this level are terminal. */
739   if (namelen != 1)
740     return (ENOTDIR);
741   return (ENOPROTOOPT);
742 }
743
744 static void natmintr(struct netmsg *);
745
746 #if defined(__DragonFly__)
747 static void
748 netisr_natm_setup(void *dummy __unused)
749 {
750         netisr_register(NETISR_NATM, natmintr, NULL);
751 }
752 SYSINIT(natm_setup, SI_BOOT2_KLD, SI_ORDER_ANY, netisr_natm_setup, NULL);
753 #endif
754
755 void
756 natm_init(void)
757 {
758   LIST_INIT(&natm_pcbs);
759
760   netisr_register(NETISR_NATM, natmintr, NULL);
761 }
762
763 /*
764  * natmintr: software interrupt
765  *
766  * note: we expect a socket pointer in rcvif rather than an interface
767  * pointer.    we can get the interface pointer from the so's PCB if
768  * we really need it.
769  */
770 static void
771 natmintr(struct netmsg *msg)
772 {
773   struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
774   struct socket *so;
775   struct natmpcb *npcb;
776
777 #ifdef DIAGNOSTIC
778   if ((m->m_flags & M_PKTHDR) == 0)
779     panic("natmintr no HDR");
780 #endif
781
782   get_mplock();
783
784   npcb = (struct natmpcb *) m->m_pkthdr.rcvif; /* XXX: overloaded */
785   so = npcb->npcb_socket;
786
787   crit_enter();
788   npcb->npcb_inq--;
789   crit_exit();
790
791   if (npcb->npcb_flags & NPCB_DRAIN) {
792     m_freem(m);
793     if (npcb->npcb_inq == 0)
794       FREE(npcb, M_PCB);                        /* done! */
795     goto out;
796   }
797
798   if (npcb->npcb_flags & NPCB_FREE) {
799     m_freem(m);                                 /* drop */
800     goto out;
801   }
802
803 #ifdef NEED_TO_RESTORE_IFP
804   m->m_pkthdr.rcvif = npcb->npcb_ifp;
805 #else
806 #ifdef DIAGNOSTIC
807 m->m_pkthdr.rcvif = NULL;       /* null it out to be safe */
808 #endif
809 #endif
810
811   if (ssb_space(&so->so_rcv) > m->m_pkthdr.len ||
812      ((npcb->npcb_flags & NPCB_RAW) != 0 && so->so_rcv.ssb_cc < NPCB_RAWCC) ) {
813 #ifdef NATM_STAT
814     natm_sookcnt++;
815     natm_sookbytes += m->m_pkthdr.len;
816 #endif
817     sbappendrecord(&so->so_rcv.sb, m);
818     sorwakeup(so);
819   } else {
820 #ifdef NATM_STAT
821     natm_sodropcnt++;
822     natm_sodropbytes += m->m_pkthdr.len;
823 #endif
824     m_freem(m);
825   }
826 out:
827   rel_mplock();
828   /* msg was embedded in the mbuf, do not reply! */
829 }
830