]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netatm/atm_if.c
This commit was generated by cvs2svn to compensate for changes in r147899,
[FreeBSD/FreeBSD.git] / sys / netatm / atm_if.c
1 /*-
2  * ===================================
3  * HARP  |  Host ATM Research Platform
4  * ===================================
5  *
6  *
7  * This Host ATM Research Platform ("HARP") file (the "Software") is
8  * made available by Network Computing Services, Inc. ("NetworkCS")
9  * "AS IS".  NetworkCS does not provide maintenance, improvements or
10  * support of any kind.
11  *
12  * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED,
13  * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
14  * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE
15  * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE.
16  * In no event shall NetworkCS be responsible for any damages, including
17  * but not limited to consequential damages, arising from or relating to
18  * any use of the Software or related support.
19  *
20  * Copyright 1994-1998 Network Computing Services, Inc.
21  *
22  * Copies of this Software may be made, however, the above copyright
23  * notice must be reproduced on all copies.
24  */
25
26 /*
27  * Core ATM Services
28  * -----------------
29  *
30  * ATM interface management
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/sockio.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
42 #include <sys/syslog.h>
43 #include <net/if.h>
44 #include <net/if_types.h>
45 #include <net/if_dl.h>
46 #include <net/route.h>
47 #include <net/bpf.h>
48 #include <netinet/in.h>
49 #include <netinet/in_var.h>
50 #include <netatm/port.h>
51 #include <netatm/queue.h>
52 #include <netatm/atm.h>
53 #include <netatm/atm_sys.h>
54 #include <netatm/atm_sap.h>
55 #include <netatm/atm_cm.h>
56 #include <netatm/atm_if.h>
57 #include <netatm/atm_ioctl.h>
58 #include <netatm/atm_sigmgr.h>
59 #include <netatm/atm_stack.h>
60 #include <netatm/atm_pcb.h>
61 #include <netatm/atm_var.h>
62
63 /*
64  * Local functions
65  */
66 static int      atm_physif_ioctl(int, caddr_t, caddr_t);
67 static int      atm_if_ioctl(struct ifnet *, u_long, caddr_t);
68 static int      atm_ifparse(const char *, char *, size_t, int *);
69
70 /*
71  * Local variables
72  */
73 static int      (*atm_ifouttbl[AF_MAX+1])
74                         (struct ifnet *, KBuffer *, struct sockaddr *)
75                                 = {NULL};
76
77
78 /*
79  * Register an ATM physical interface
80  * 
81  * Each ATM device interface must register itself here upon completing
82  * its internal initialization.  This applies to both linked and loaded
83  * device drivers.  The interface must be registered before a signalling
84  * manager can be attached.
85  *
86  * Arguments:
87  *      cup     pointer to interface's common unit structure
88  *      name    pointer to device name string
89  *      sdp     pointer to interface's stack services
90  *
91  * Returns:
92  *      0       registration successful
93  *      errno   registration failed - reason indicated
94  *
95  */
96 int
97 atm_physif_register(cup, name, sdp)
98         Cmn_unit                *cup;
99         const char              *name;
100         struct stack_defn       *sdp;
101 {
102         struct atm_pif  *pip;
103         int             s;
104
105         /*
106          * See if we need to be initialized
107          */
108         if (!atm_init)
109                 atm_initialize();
110
111         /*
112          * Make sure we're not already registered
113          */
114         if (cup->cu_flags & CUF_REGISTER) {
115                 return (EALREADY);
116         }
117
118         s = splnet();
119
120         /*
121          * Make sure an interface is only registered once
122          */
123         for (pip = atm_interface_head; pip != NULL; pip = pip->pif_next) {
124                 if ((cup->cu_unit == pip->pif_unit) && 
125                     (strcmp(name, pip->pif_name) == 0)) {
126                         (void) splx(s);
127                         return (EEXIST);
128                 }
129         }
130
131         /*
132          * Fill in physical interface parameters
133          */
134         pip = &cup->cu_pif;
135         pip->pif_name = name;
136         pip->pif_unit = cup->cu_unit;
137         pip->pif_flags = PIF_UP;
138         pip->pif_services = sdp;
139         pip->pif_ioctl = atm_physif_ioctl;
140
141         /*
142          * Link in the interface and mark us registered
143          */
144         LINK2TAIL(pip, struct atm_pif, atm_interface_head, pif_next);
145         cup->cu_flags |= CUF_REGISTER;
146
147         (void) splx(s);
148         return (0);
149 }
150
151
152 /*
153  * De-register an ATM physical interface
154  * 
155  * Each ATM interface must de-register itself before downing the interface.  
156  * The interface's signalling manager will be detached and any network
157  * interface and VCC control blocks will be freed.  
158  *
159  * Arguments:
160  *      cup     pointer to interface's common unit structure
161  *
162  * Returns:
163  *      0       de-registration successful
164  *      errno   de-registration failed - reason indicated
165  *
166  */
167 int
168 atm_physif_deregister(cup)
169         Cmn_unit        *cup;
170 {
171         struct atm_pif  *pip = (struct atm_pif *)&cup->cu_pif;
172         Cmn_vcc         *cvp;
173         int     err;
174         int     s = splnet();
175
176         /*
177          * Detach and deregister, if needed
178          */
179         if ((cup->cu_flags & CUF_REGISTER)) {
180
181                 /*
182                  * Detach from signalling manager
183                  */
184                 if (pip->pif_sigmgr != NULL) {
185                         err = atm_sigmgr_detach(pip);
186                         if (err && (err != ENOENT)) {
187                                 (void) splx(s);
188                                 return (err);
189                         }
190                 }
191
192                 /*
193                  * Make sure signalling manager is detached
194                  */
195                 if (pip->pif_sigmgr != NULL) {
196                         (void) splx(s);
197                         return (EBUSY);
198                 }
199
200                 /*
201                  * Unlink interface
202                  */
203                 UNLINK(pip, struct atm_pif, atm_interface_head, pif_next);
204
205                 cup->cu_flags &= ~CUF_REGISTER;
206         }
207
208         /*
209          * Free all of our network interfaces
210          */
211         atm_physif_freenifs(pip, cup->cu_nif_zone);
212
213         /*
214          * Free unit's vcc information
215          */
216         cvp = cup->cu_vcc;
217         while (cvp) {
218                 uma_zfree(cup->cu_vcc_zone, cvp);
219                 cvp = cvp->cv_next;
220         }
221         cup->cu_vcc = (Cmn_vcc *)NULL;
222
223         (void) splx(s);
224
225         return (0);
226 }
227
228
229 /*
230  * Free all network interfaces on a physical interface
231  *
232  * Arguments
233  *      pip             pointer to physical interface structure
234  *
235  * Returns
236  *      none
237  *
238  */
239 void
240 atm_physif_freenifs(pip, zone)
241         struct atm_pif  *pip;
242         uma_zone_t      zone;
243 {
244         struct atm_nif  *nip = pip->pif_nif;
245         int     s = splnet();
246
247         while ( nip ) 
248         {
249                 /*
250                  * atm_nif_detach zeros pointers - save so we can
251                  * walk the chain.
252                  */
253                 struct atm_nif  *nipp = nip->nif_pnext;
254
255                 /*
256                  * Clean up network i/f trails
257                  */
258                 atm_nif_detach(nip);
259                 uma_zfree(zone, nip);
260                 nip = nipp;
261         }
262         pip->pif_nif = (struct atm_nif *)NULL;
263
264         (void) splx(s);
265
266         return;
267 }
268
269 /*
270  * Handle physical interface ioctl's
271  *
272  * See <netatm/atm_ioctl.h> for definitions.
273  *
274  * Called at splnet.
275  *
276  * Arguments:
277  *      code                    Ioctl function (sub)code
278  *      data                    Data block. On input contains command,
279  *                                      on output, contains results
280  *      arg                     Optional code specific arguments
281  *
282  * Returns:
283  *      0                       Request processed successfully
284  *      errno                   Request failed - reason code
285  *
286  */
287 static int
288 atm_physif_ioctl(code, data, arg)
289         int     code;
290         caddr_t data;
291         caddr_t arg;
292 {
293         struct atminfreq        *aip = (struct atminfreq *)data;
294         struct atmsetreq        *asr = (struct atmsetreq *)data;
295         struct atm_pif          *pip;
296         struct atm_nif          *nip;
297         struct sigmgr           *smp;
298         struct siginst          *sip;
299         struct ifnet            *ifp;
300         Cmn_unit                *cup;
301         Atm_config              *acp;
302         caddr_t                 buf = aip->air_buf_addr;
303         struct air_phy_stat_rsp *apsp;
304         struct air_int_rsp      apr;
305         struct air_netif_rsp    anr;
306         struct air_cfg_rsp      acr;
307         u_int                   count;
308         size_t                  len;
309         size_t                  buf_len = aip->air_buf_len;
310         int                     err = 0;
311         char                    ifname[2*IFNAMSIZ];
312         struct ifaddr           *ifa;
313         struct in_ifaddr        *ia;
314         struct sockaddr_dl      *sdl;
315  
316
317         switch ( aip->air_opcode ) {
318
319         case AIOCS_INF_INT:
320                 /*
321                  * Get physical interface information
322                  */
323                 aip = (struct atminfreq *)data;
324                 pip = (struct atm_pif *)arg;
325
326                 /*
327                  * Make sure there's room in user buffer
328                  */
329                 if (aip->air_buf_len < sizeof(apr)) {
330                         err = ENOSPC;
331                         break;
332                 }
333
334                 /*
335                  * Fill in info to be returned
336                  */
337                 bzero((caddr_t)&apr, sizeof(apr));
338                 smp = pip->pif_sigmgr;
339                 sip = pip->pif_siginst;
340                 (void) snprintf(apr.anp_intf, sizeof(apr.anp_intf),
341                         "%s%d", pip->pif_name, pip->pif_unit );
342                 if ( pip->pif_nif )
343                 {
344                         strcpy(apr.anp_nif_pref, pip->pif_nif->nif_ifp->if_dname);
345
346                         nip = pip->pif_nif;
347                         while ( nip ) {
348                                 apr.anp_nif_cnt++;
349                                 nip = nip->nif_pnext;
350                         }
351                 }
352                 if (sip) {
353                         ATM_ADDR_COPY(&sip->si_addr, &apr.anp_addr);
354                         ATM_ADDR_COPY(&sip->si_subaddr, &apr.anp_subaddr);
355                         apr.anp_sig_proto = smp->sm_proto;
356                         apr.anp_sig_state = sip->si_state;
357                 }
358
359                 /*
360                  * Copy data to user buffer
361                  */
362                 err = copyout((caddr_t)&apr, aip->air_buf_addr, sizeof(apr));
363                 if (err)
364                         break;
365
366                 /*
367                  * Update buffer pointer/count
368                  */
369                 aip->air_buf_addr += sizeof(apr);
370                 aip->air_buf_len -= sizeof(apr);
371                 break;
372
373         case AIOCS_INF_NIF:
374                 /*
375                  * Get network interface information
376                  */
377                 aip = (struct atminfreq *)data;
378                 nip = (struct atm_nif *)arg;
379                 ifp = nip->nif_ifp;
380                 pip = nip->nif_pif;
381
382                 /*
383                  * Make sure there's room in user buffer
384                  */
385                 if (aip->air_buf_len < sizeof(anr)) {
386                         err = ENOSPC;
387                         break;
388                 }
389
390                 /*
391                  * Fill in info to be returned
392                  */
393                 bzero((caddr_t)&anr, sizeof(anr));
394                 (void) snprintf(anr.anp_intf, sizeof(anr.anp_intf),
395                     "%s%d", ifp->if_dname, ifp->if_dunit);
396                 IFP_TO_IA(ifp, ia);
397                 if (ia) {
398                         anr.anp_proto_addr = *ia->ia_ifa.ifa_addr;
399                 }
400                 (void) snprintf(anr.anp_phy_intf, sizeof(anr.anp_phy_intf),
401                     "%s%d", pip->pif_name, pip->pif_unit);
402
403                 /*
404                  * Copy data to user buffer
405                  */
406                 err = copyout((caddr_t)&anr, aip->air_buf_addr, sizeof(anr));
407                 if (err)
408                         break;
409
410                 /*
411                  * Update buffer pointer/count
412                  */
413                 aip->air_buf_addr += sizeof(anr);
414                 aip->air_buf_len -= sizeof(anr);
415                 break;
416
417         case AIOCS_INF_PIS:
418                 /*
419                  * Get per interface statistics
420                  */
421                 pip = (struct atm_pif *)arg;
422                 if ( pip == NULL )
423                         return ( ENXIO );
424                 snprintf ( ifname, sizeof(ifname),
425                     "%s%d", pip->pif_name, pip->pif_unit );
426
427                 /*
428                  * Cast response into users buffer
429                  */
430                 apsp = (struct air_phy_stat_rsp *)buf;
431
432                 /*
433                  * Sanity check
434                  */
435                 len = sizeof ( struct air_phy_stat_rsp );
436                 if ( buf_len < len )
437                         return ( ENOSPC );
438
439                 /*
440                  * Copy interface name into response
441                  */
442                 if ((err = copyout ( ifname, apsp->app_intf, IFNAMSIZ)) != 0)
443                         break;
444
445                 /*
446                  * Copy counters
447                  */
448                 if ((err = copyout(&pip->pif_ipdus, &apsp->app_ipdus,
449                     len - sizeof(apsp->app_intf))) != 0)
450                         break;
451
452                 /*
453                  * Adjust buffer elements
454                  */
455                 buf += len;
456                 buf_len -= len;
457
458                 aip->air_buf_addr = buf;
459                 aip->air_buf_len = buf_len;
460                 break;
461
462         case AIOCS_SET_NIF:
463                 /*
464                  * Set NIF - allow user to configure 1 or more logical
465                  *      interfaces per physical interface.
466                  */
467
468                 /*
469                  * Get pointer to physical interface structure from
470                  * ioctl argument.
471                  */
472                 pip = (struct atm_pif *)arg;
473                 cup = (Cmn_unit *)pip;
474
475                 /*
476                  * Sanity check - are we already connected to something?
477                  */
478                 if ( pip->pif_sigmgr )
479                 {
480                         err = EBUSY;
481                         break;
482                 }
483
484                 /*
485                  * Free any previously allocated NIFs
486                  */
487                 atm_physif_freenifs(pip, cup->cu_nif_zone);
488
489                 /*
490                  * Add list of interfaces
491                  */
492                 for ( count = 0; count < asr->asr_nif_cnt; count++ )
493                 {
494                         nip = uma_zalloc(cup->cu_nif_zone, M_WAITOK | M_ZERO);
495                         if ( nip == NULL )
496                         {
497                                 /*
498                                  * Destroy any successful nifs
499                                  */
500                                 atm_physif_freenifs(pip, cup->cu_nif_zone);
501                                 err = ENOMEM;
502                                 break;
503                         }
504
505                         nip->nif_pif = pip;
506                         ifp = nip->nif_ifp = if_alloc(IFT_IPOVERATM);
507                         if (ifp == NULL) {
508                                 uma_zfree(cup->cu_nif_zone, nip);
509                                 /*
510                                  * Destroy any successful nifs
511                                  */
512                                 atm_physif_freenifs(pip, cup->cu_nif_zone);
513                                 break;
514                         }
515
516                         strcpy ( nip->nif_name, asr->asr_nif_pref );
517                         nip->nif_sel = count;
518
519                         if_initname(ifp, nip->nif_name, count);
520                         ifp->if_mtu = ATM_NIF_MTU;
521                         ifp->if_flags = IFF_UP | IFF_BROADCAST | IFF_RUNNING;
522                         ifp->if_output = atm_ifoutput;
523                         ifp->if_ioctl = atm_if_ioctl;
524                         ifp->if_snd.ifq_maxlen = ifqmaxlen;
525                         switch ( cup->cu_config.ac_media ) {
526                         case MEDIA_TAXI_100:
527                                 ifp->if_baudrate = 100000000;
528                                 break;
529                         case MEDIA_TAXI_140:
530                                 ifp->if_baudrate = 140000000;
531                                 break;
532                         case MEDIA_OC3C:
533                         case MEDIA_OC12C:
534                         case MEDIA_UTP155:
535                                 ifp->if_baudrate = 155000000;
536                                 break;
537                         case MEDIA_UTP25:
538                                 ifp->if_baudrate = 25600000;
539                                 break;
540                         case MEDIA_VIRTUAL:
541                                 ifp->if_baudrate = 100000000;   /* XXX */
542                                 break;
543                         case MEDIA_DSL:
544                                 ifp->if_baudrate = 2500000;     /* XXX */
545                                 break;
546                         case MEDIA_UNKNOWN:
547                                 ifp->if_baudrate = 9600;
548                                 break;
549                         }
550                         if ((err = atm_nif_attach(nip)) != 0) {
551                                 if_free(nip->nif_ifp);
552                                 uma_zfree(cup->cu_nif_zone, nip);
553                                 /*
554                                  * Destroy any successful nifs
555                                  */
556                                 atm_physif_freenifs(pip, cup->cu_nif_zone);
557                                 break;
558                         }
559                         /*
560                          * Set macaddr in <Link> address
561                          */
562                         ifp->if_addrlen = 6;
563                         ifa = ifaddr_byindex(ifp->if_index);
564                         if ( ifa ) {
565                                 sdl = (struct sockaddr_dl *)
566                                         ifa->ifa_addr;
567                                 sdl->sdl_type = IFT_ETHER;
568                                 sdl->sdl_alen = ifp->if_addrlen;
569                                 bcopy ( (caddr_t)&cup->cu_config.ac_macaddr,
570                                         LLADDR(sdl), ifp->if_addrlen );
571                         }
572                 }
573                 break;
574
575         case AIOCS_INF_CFG:
576                 /*
577                  * Get adapter configuration information
578                  */
579                 aip = (struct atminfreq *)data;
580                 pip = (struct atm_pif *)arg;
581                 cup = (Cmn_unit *)pip;
582                 acp = &cup->cu_config;
583
584                 /*
585                  * Make sure there's room in user buffer
586                  */
587                 if (aip->air_buf_len < sizeof(acr)) {
588                         err = ENOSPC;
589                         break;
590                 }
591
592                 /*
593                  * Fill in info to be returned
594                  */
595                 bzero((caddr_t)&acr, sizeof(acr));
596                 (void) snprintf(acr.acp_intf, sizeof(acr.acp_intf),
597                     "%s%d", pip->pif_name, pip->pif_unit);
598                 bcopy((caddr_t)acp, (caddr_t)&acr.acp_cfg,
599                                 sizeof(Atm_config));
600
601                 /*
602                  * Copy data to user buffer
603                  */
604                 err = copyout((caddr_t)&acr, aip->air_buf_addr,
605                                 sizeof(acr));
606                 if (err)
607                         break;
608
609                 /*
610                  * Update buffer pointer/count
611                  */
612                 aip->air_buf_addr += sizeof(acr);
613                 aip->air_buf_len -= sizeof(acr);
614                 break;
615
616         case AIOCS_INF_VST:
617                 /*
618                  * Pass off to device-specific handler
619                  */
620                 cup = (Cmn_unit *)arg;
621                 if (cup == NULL)
622                         err = ENXIO;
623                 else
624                         err = (*cup->cu_ioctl)(code, data, arg);
625                 break;
626
627         default:
628                 err = ENOSYS;
629         }
630
631         return ( err );
632 }
633
634
635 /*
636  * Register a Network Convergence Module
637  * 
638  * Each ATM network convergence module must register itself here before
639  * it will receive network interface status notifications. 
640  *
641  * Arguments:
642  *      ncp     pointer to network convergence definition structure
643  *
644  * Returns:
645  *      0       registration successful
646  *      errno   registration failed - reason indicated
647  *
648  */
649 int
650 atm_netconv_register(ncp)
651         struct atm_ncm  *ncp;
652 {
653         struct atm_ncm  *tdp;
654         int             s = splnet();
655
656         /*
657          * See if we need to be initialized
658          */
659         if (!atm_init)
660                 atm_initialize();
661
662         /*
663          * Validate protocol family
664          */
665         if (ncp->ncm_family > AF_MAX) {
666                 (void) splx(s);
667                 return (EINVAL);
668         }
669
670         /*
671          * Ensure no duplicates
672          */
673         for (tdp = atm_netconv_head; tdp != NULL; tdp = tdp->ncm_next) {
674                 if (tdp->ncm_family == ncp->ncm_family) {
675                         (void) splx(s);
676                         return (EEXIST);
677                 }
678         }
679
680         /*
681          * Add module to list
682          */
683         LINK2TAIL(ncp, struct atm_ncm, atm_netconv_head, ncm_next);
684
685         /*
686          * Add new interface output function
687          */
688         atm_ifouttbl[ncp->ncm_family] = ncp->ncm_ifoutput;
689
690         (void) splx(s);
691         return (0);
692 }
693
694
695 /*
696  * De-register an ATM Network Convergence Module
697  * 
698  * Each ATM network convergence provider must de-register its registered 
699  * service(s) before terminating.  Specifically, loaded kernel modules
700  * must de-register their services before unloading themselves.
701  *
702  * Arguments:
703  *      ncp     pointer to network convergence definition structure
704  *
705  * Returns:
706  *      0       de-registration successful 
707  *      errno   de-registration failed - reason indicated
708  *
709  */
710 int
711 atm_netconv_deregister(ncp)
712         struct atm_ncm  *ncp;
713 {
714         int     found, s = splnet();
715
716         /*
717          * Remove module from list
718          */
719         UNLINKF(ncp, struct atm_ncm, atm_netconv_head, ncm_next, found);
720
721         if (!found) {
722                 (void) splx(s);
723                 return (ENOENT);
724         }
725
726         /*
727          * Remove module's interface output function
728          */
729         atm_ifouttbl[ncp->ncm_family] = NULL;
730
731         (void) splx(s);
732         return (0);
733 }
734
735
736 /*
737  * Attach an ATM Network Interface
738  * 
739  * Before an ATM network interface can be used by the system, the owning
740  * device interface must attach the network interface using this function.
741  * The physical interface for this network interface must have been previously
742  * registered (using atm_interface_register).  The network interface will be
743  * added to the kernel's interface list and to the physical interface's list.
744  * The caller is responsible for initializing the control block fields.
745  *
746  * Arguments:
747  *      nip     pointer to atm network interface control block
748  *
749  * Returns:
750  *      0       attach successful
751  *      errno   attach failed - reason indicated
752  *
753  */
754 int
755 atm_nif_attach(nip)
756         struct atm_nif  *nip;
757 {
758         struct atm_pif  *pip, *pip2;
759         struct ifnet    *ifp;
760         struct atm_ncm  *ncp;
761         int             s;
762
763         ifp = nip->nif_ifp;
764         if (ifp == NULL)
765                 return (ENOSPC);
766         pip = nip->nif_pif;
767
768         s = splimp();
769
770         /*
771          * Verify physical interface is registered
772          */
773         for (pip2 = atm_interface_head; pip2 != NULL; pip2 = pip2->pif_next) {
774                 if (pip == pip2)
775                         break;
776         }
777         if ((pip == NULL) || (pip2 == NULL)) {
778                 (void) splx(s);
779                 return (EFAULT);
780         }
781
782         /*
783          * Add to system interface list 
784          */
785         if_attach(ifp);
786
787         /*
788          * Add to BPF interface list
789          * DLT_ATM_RFC_1483 cannot be used because both NULL and LLC/SNAP could
790          * be provisioned
791          */
792         bpfattach(ifp, DLT_ATM_CLIP, T_ATM_LLC_MAX_LEN);
793
794         /*
795          * Add to physical interface list
796          */
797         LINK2TAIL(nip, struct atm_nif, pip->pif_nif, nif_pnext);
798
799         /*
800          * Notify network convergence modules of new network i/f
801          */
802         for (ncp = atm_netconv_head; ncp; ncp = ncp->ncm_next) {
803                 int     err;
804
805                 err = (*ncp->ncm_stat)(NCM_ATTACH, nip, 0);
806                 if (err) {
807                         atm_nif_detach(nip);
808                         (void) splx(s);
809                         return (err);
810                 }
811         }
812
813         (void) splx(s);
814         return (0);
815 }
816
817
818 /*
819  * Detach an ATM Network Interface
820  * 
821  * Before an ATM network interface control block can be freed, all kernel
822  * references to/from this block must be released.  This function will delete
823  * all routing references to the interface and free all interface addresses
824  * for the interface.  The network interface will then be removed from the
825  * kernel's interface list and from the owning physical interface's list.
826  * The caller is responsible for free'ing the control block.
827  *
828  * Arguments:
829  *      nip     pointer to atm network interface control block
830  *
831  * Returns:
832  *      none
833  *
834  */
835 void
836 atm_nif_detach(nip)
837         struct atm_nif  *nip;
838 {
839         struct atm_ncm  *ncp;
840         int             s;
841         struct ifnet    *ifp = nip->nif_ifp;
842
843         s = splimp();
844
845         /*
846          * Notify convergence modules of network i/f demise
847          */
848         for (ncp = atm_netconv_head; ncp; ncp = ncp->ncm_next) {
849                 (void) (*ncp->ncm_stat)(NCM_DETACH, nip, 0);
850         }
851
852         /*
853          * Remove from BPF interface list
854          */
855         bpfdetach(ifp);
856
857         /*
858          * Free all interface routes and addresses,
859          * delete all remaining routes using this interface,
860          * then remove from the system interface list
861          */
862         if_detach(ifp);
863         if_free(ifp);
864
865         /*
866          * Remove from physical interface list
867          */
868         UNLINK(nip, struct atm_nif, nip->nif_pif->pif_nif, nif_pnext);
869
870         (void) splx(s);
871 }
872
873 /*
874  * Set an ATM Network Interface address
875  * 
876  * This is called from a device interface when processing an SIOCSIFADDR
877  * ioctl request.  We just notify all convergence modules of the new address
878  * and hope everyone has non-overlapping interests, since if someone reports
879  * an error we don't go back and tell everyone to undo the change.
880  *
881  * Arguments:
882  *      nip     pointer to atm network interface control block
883  *      ifa     pointer to new interface address
884  *
885  * Returns:
886  *      0       set successful
887  *      errno   set failed - reason indicated
888  *
889  */
890 int
891 atm_nif_setaddr(nip, ifa)
892         struct atm_nif  *nip;
893         struct ifaddr   *ifa;
894 {
895         struct atm_ncm  *ncp;
896         int     err = 0, s = splnet();
897
898         /*
899          * Notify convergence modules of network i/f change
900          */
901         for (ncp = atm_netconv_head; ncp; ncp = ncp->ncm_next) {
902                 err = (*ncp->ncm_stat)(NCM_SETADDR, nip, (intptr_t)ifa);
903                 if (err)
904                         break;
905         }
906         (void) splx(s);
907
908         return (err);
909 }
910
911
912 /*
913  * ATM Interface Packet Output
914  * 
915  * All ATM network interfaces must have their ifnet if_output address set to
916  * this function.  Since no existing network layer code is to be modified 
917  * for ATM support, this function serves as the hook to allow network output
918  * packets to be assigned to their proper outbound VCC.  Each network address
919  * family which is to be supported over ATM must be assigned an output
920  * packet processing function via atm_netconv_register().
921  *
922  * Arguments:
923  *      ifp     pointer to ifnet structure
924  *      m       pointer to packet buffer chain to be output
925  *      dst     pointer to packet's network destination address
926  *
927  * Returns:
928  *      0       packet queued to interface
929  *      errno   output failed - reason indicated
930  *
931  */
932 int
933 atm_ifoutput(ifp, m, dst, rt)
934         struct ifnet    *ifp;
935         KBuffer         *m;
936         struct sockaddr *dst;
937         struct rtentry  *rt;
938 {
939         u_short         fam = dst->sa_family;
940         int             (*func)(struct ifnet *, KBuffer *,
941                                         struct sockaddr *);
942
943         /*
944          * Validate address family
945          */
946         if (fam > AF_MAX) {
947                 KB_FREEALL(m);
948                 return (EAFNOSUPPORT);
949         }
950
951         /*
952          * Hand packet off for dst-to-VCC mapping
953          */
954         func = atm_ifouttbl[fam];
955         if (func == NULL) {
956                 KB_FREEALL(m);
957                 return (EAFNOSUPPORT);
958         }
959         return ((*func)(ifp, m, dst));
960 }
961
962
963 /*
964  * Handle interface ioctl requests. 
965  *
966  * Arguments:
967  *      ifp             pointer to network interface structure
968  *      cmd             IOCTL cmd
969  *      data            arguments to/from ioctl
970  *
971  * Returns:
972  *      error           errno value
973  */
974 static int
975 atm_if_ioctl(ifp, cmd, data)
976         struct ifnet *ifp;
977         u_long  cmd;
978         caddr_t data;
979 {
980         register struct ifreq *ifr = (struct ifreq *)data;
981         struct atm_nif  *nip = IFP2ANIF(ifp);
982         int     error = 0;
983         int     s = splnet();
984
985         switch ( cmd )
986         {
987         case SIOCGIFADDR:
988                 bcopy ( (caddr_t)&(nip->nif_pif->pif_macaddr),
989                         (caddr_t)ifr->ifr_addr.sa_data, 
990                         sizeof(struct mac_addr) );
991                 break;
992
993         case SIOCSIFADDR:
994                 error = atm_nif_setaddr ( nip, (struct ifaddr *)data);
995                 ifp->if_flags |= IFF_UP | IFF_RUNNING | IFF_BROADCAST;
996                 break;
997
998         case SIOCGIFFLAGS:
999                 *(int *)data = ifp->if_flags;
1000                 break;
1001
1002         case SIOCSIFFLAGS:
1003                 break;
1004
1005         default:
1006                 error = EINVAL;
1007                 break;
1008         }
1009
1010         (void) splx(s);
1011         return ( error );
1012 }
1013
1014
1015 /*
1016  * Parse interface name
1017  * 
1018  * Parses an interface name string into a name and a unit component.
1019  *
1020  * Arguments:
1021  *      name    pointer to interface name string
1022  *      namep   address to store interface name
1023  *      size    size available at namep
1024  *      unitp   address to store interface unit number
1025  *
1026  * Returns:
1027  *      0       name parsed
1028  *      else    parse error
1029  *
1030  */
1031 static int
1032 atm_ifparse(const char *name, char *namep, size_t size, int *unitp)
1033 {
1034         const char *cp;
1035         char *np;
1036         size_t len = 0;
1037         int unit = 0;
1038
1039         /*
1040          * Separate supplied string into name and unit parts.
1041          */
1042         cp = name;
1043         np = namep;
1044         while (*cp) {
1045                 if (*cp >= '0' && *cp <= '9')
1046                         break;
1047                 if (++len >= size)
1048                         return (-1);
1049                 *np++ = *cp++;
1050         }
1051         *np = '\0';
1052         while (*cp && *cp >= '0' && *cp <= '9')
1053                 unit = 10 * unit + *cp++ - '0';
1054
1055         *unitp = unit;
1056
1057         return (0);
1058 }
1059
1060
1061 /*
1062  * Locate ATM physical interface via name
1063  * 
1064  * Uses the supplied interface name string to locate a registered
1065  * ATM physical interface.
1066  *
1067  * Arguments:
1068  *      name    pointer to interface name string
1069  *
1070  * Returns:
1071  *      0       interface not found
1072  *      else    pointer to atm physical interface structure
1073  *
1074  */
1075 struct atm_pif *
1076 atm_pifname(name)
1077         char            *name;
1078 {
1079         struct atm_pif  *pip;
1080         char            n[IFNAMSIZ];
1081         int             unit;
1082
1083         /*
1084          * Break down name
1085          */
1086         if (atm_ifparse(name, n, sizeof(n), &unit))
1087                 return ((struct atm_pif *)0);
1088
1089         /*
1090          * Look for the physical interface
1091          */
1092         for (pip = atm_interface_head; pip; pip = pip->pif_next) {
1093                 if ((pip->pif_unit == unit) && (strcmp(pip->pif_name, n) == 0))
1094                         break;
1095         }
1096
1097         return (pip);
1098 }
1099
1100
1101 /*
1102  * Locate ATM network interface via name
1103  * 
1104  * Uses the supplied interface name string to locate an ATM network interface.
1105  *
1106  * Arguments:
1107  *      name    pointer to interface name string
1108  *
1109  * Returns:
1110  *      0       interface not found
1111  *      else    pointer to atm network interface structure
1112  *
1113  */
1114 struct atm_nif *
1115 atm_nifname(name)
1116         char            *name;
1117 {
1118         struct atm_pif  *pip;
1119         struct atm_nif  *nip;
1120         char            n[IFNAMSIZ];
1121         int             unit;
1122
1123         /*
1124          * Break down name
1125          */
1126         if (atm_ifparse(name, n, sizeof(n), &unit))
1127                 return ((struct atm_nif *)0);
1128
1129         /*
1130          * Search thru each physical interface
1131          */
1132         for (pip = atm_interface_head; pip; pip = pip->pif_next) {
1133                 /*
1134                  * Looking for network interface
1135                  */
1136                 for (nip = pip->pif_nif; nip; nip = nip->nif_pnext) {
1137                         struct ifnet    *ifp = (struct ifnet *)nip;
1138                         if ((ifp->if_dunit == unit) && 
1139                             (strcmp(ifp->if_dname, n) == 0))
1140                                 return (nip);
1141                 }
1142         }
1143         return (NULL);
1144 }