]> git.saurik.com Git - apple/xnu.git/blame - bsd/netat/at.c
xnu-201.tar.gz
[apple/xnu.git] / bsd / netat / at.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * Copyright (c) 1998 Apple Computer, Inc.
24 */
25
26/* at.c
27 */
28
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/ioctl.h>
32#include <sys/errno.h>
33#include <sys/malloc.h>
34#include <sys/socket.h>
35#include <sys/socketvar.h>
36#include <sys/file.h>
37
38#include <net/if.h>
39#include <net/if_dl.h>
40#include <net/if_types.h>
41#include <net/etherdefs.h>
42#include <net/tokendefs.h>
43#include <net/dlil.h>
44
45#include <netat/appletalk.h>
46#include <netat/sysglue.h>
47#include <netat/at_pcb.h>
48#include <netat/at_var.h>
49#include <netat/ddp.h>
50#include <netat/nbp.h>
51#include <netat/routing_tables.h>
52#include <netat/debug.h>
53
0b4e3aa0 54extern int at_ioctl(struct atpcb *, u_long, caddr_t, int fromKernel);
1c79356b
A
55extern int routerStart(at_kern_err_t *);
56extern void elap_offline(at_ifaddr_t *);
57extern at_ifaddr_t *find_ifID(char *);
58extern at_nvestr_t *getRTRLocalZone(zone_usage_t *);
59extern int setLocalZones(at_nvestr_t *, int);
60
61extern int xpatcnt;
62extern at_ifaddr_t at_interfaces[];
63extern at_ifaddr_t *ifID_home;
64extern TAILQ_HEAD(name_registry, _nve_) name_registry;
65extern int nve_lock;
66
67struct etalk_addr etalk_multicast_addr = {
68 {0x09, 0x00, 0x07, 0xff, 0xff, 0xff}};
69struct etalk_addr ttalk_multicast_addr = {
70 {0xC0, 0x00, 0x40, 0x00, 0x00, 0x00}};
71
72/* called only in router mode */
73static int set_zones(ifz)
74 zone_usage_t *ifz;
75
76/* 1. adds zone to table
77 2. looks up each route entry from zone list
78 3. sets zone bit in each route entry
79
80 returns 0 if successful
81 errno if error occurred
82*/
83{
84 int i;
85 at_ifaddr_t *ifID;
86 short zno;
87 RT_entry *rte;
88
89 zno = zt_add_zone(ifz->zone_name.str, ifz->zone_name.len);
90
91 if (zno == ZT_MAXEDOUT) {
92 dPrintf(D_M_ELAP, D_L_ERROR, ("set_zones: error: table full\n"));
93 return(ENOSPC);
94 }
95 if (ifz->zone_home) {
96 ifID_home->ifZoneName = ifz->zone_name;
97 ifID_home->ifDefZone = zno;
98 }
99
100 for (i=0; i<IF_TOTAL_MAX; i++) {
101 if (ifz->zone_iflist.at_if[i][0]) {
102 if ((ifID = find_ifID(ifz->zone_iflist.at_if[i]))) {
103 rte = rt_blookup(ifID->ifThisCableEnd);
104 if (!rte) {
105 dPrintf(D_M_ELAP, D_L_ERROR,
106 ("set_zones: error: can't find route\n"));
107 } else {
108 zt_set_zmap(zno, rte->ZoneBitMap);
109
110 /* if first zone for this I/F,
111 make default */
112 if (!ifID->ifDefZone)
113 ifID->ifDefZone = zno;
114 }
115 }
116 }
117 }
118
119 return(0);
120} /* set_zones */
121
122/*
123 * Generic internet control operations (ioctl's).
124 * ifp is 0 if not an interface-specific ioctl.
125 */
126
127int at_control(so, cmd, data, ifp)
128 struct socket *so;
129 u_long cmd;
130 caddr_t data;
131 struct ifnet *ifp;
132{
133 struct ifreq *ifr = (struct ifreq *)data;
134 int pat_id = 0, error = 0;
135 struct proc *p = current_proc();
136 at_ifaddr_t *ifID = 0;
137 struct ifaddr *ifa;
138 struct sockaddr_dl *sdl;
139
140 if (cmd == 0x2000ff99) {
141 /* *** this is a temporary hack to get at_send_to_dev() to
142 work with BSD-style sockets instead of the special purpose
143 system calls, ATsocket() and ATioctl().
144 *** */
0b4e3aa0 145 if ((error = at_ioctl((struct atpcb *)so->so_pcb, cmd, data, 0))) {
1c79356b
A
146 if (((struct atpcb *)so->so_pcb)->proto != ATPROTO_LAP) {
147 ((struct atpcb *)so->so_pcb)->proto = ATPROTO_LAP;
0b4e3aa0 148 error = at_ioctl((struct atpcb *)so->so_pcb, cmd, data, 0);
1c79356b
A
149 }
150 }
151 return(error);
152
153 /* *** processing should be
154 return(EINVAL);
155 *** */
156 }
157 /*
158 * Find address for this interface, if it exists.
159 */
160 if (ifp)
161 for (pat_id = 0; pat_id < xpatcnt; pat_id++)
162 if (at_interfaces[pat_id].aa_ifp == ifp) {
163 ifID = &at_interfaces[pat_id];
164 break;
165 }
166
167 switch (cmd) {
168
169 case AIOCGETSTATE:
170 {
171 at_state_t *global_state = (at_state_t *)data;
172
173 *global_state = at_state;
174 return(0);
175 break;
176 }
177
178 case AIOCGETIFCFG:
179 {
180 at_if_cfg_t *cfgp = (at_if_cfg_t *)data;
181
182 ifID = 0;
183 if ((at_state.flags & AT_ST_STARTED) &&
184 ifID_home) {
185 if (strlen(cfgp->ifr_name)) {
186 TAILQ_FOREACH(ifID, &at_ifQueueHd, aa_link) {
187 if (!strncmp(ifID->ifName, cfgp->ifr_name,
188 strlen(ifID->ifName)))
189 break;
190 }
191 } else {
192 ifID = ifID_home;
193 strncpy(cfgp->ifr_name, ifID->ifName,
194 sizeof(ifID->ifName));
195 }
196 if (ifID && ifID->ifState != LAP_OFFLINE) {
197 cfgp->flags = ifID->ifFlags;
198 /* put the IF state into the low order
199 bits of flags */
200 cfgp->flags |= (ifID->ifState & LAP_STATE_MASK);
201 cfgp->node = ifID->ifThisNode;
202 cfgp->router = ifID->ifARouter;
203 cfgp->netStart = ifID->ifThisCableStart;
204 cfgp->netEnd = ifID->ifThisCableEnd;
205 cfgp->zonename = ifID->ifZoneName;
206 return(0);
207 } else
208 return(EINVAL);
209 } else
210 return(ENOTREADY);
211 break;
212 }
213
214 case AIOCSETDEFZONE:
215 {
216 at_def_zone_t *defzonep = (at_def_zone_t *)data;
217
218 /* check for root access */
219 if (error = suser(p->p_ucred, &p->p_acflag))
220 return(EACCES);
221
222 ifID = 0;
223 if ((at_state.flags & AT_ST_STARTED) && ifID_home) {
224 if (strlen(defzonep->ifr_name)) {
225 TAILQ_FOREACH(ifID, &at_ifQueueHd, aa_link) {
226 if (!strncmp(ifID->ifName, defzonep->ifr_name,
227 strlen(ifID->ifName)))
228 break;
229 }
230 } else {
231 ifID = ifID_home;
232 strncpy(defzonep->ifr_name, ifID->ifName,
233 sizeof(ifID->ifName));
234 }
235
236 /* In routing mode the default zone is only set for the
237 default interface. */
238 if (ROUTING_MODE && (ifID != ifID_home))
239 return(EINVAL);
240
241 if (ifID && ifID->ifState != LAP_OFFLINE) {
242 if (zonename_equal(&ifID->ifZoneName,
243 &defzonep->zonename))
244 return(0);
245 else {
246 /* check the zone name */
247 if (MULTIPORT_MODE) {
248 short zno;
249 char ifs_in_zone[IF_TOTAL_MAX];
250
251 if (!(zno = zt_find_zname(&defzonep->zonename)))
252 return(EINVAL);
253
254 getIfUsage(zno-1, ifs_in_zone);
255 if (!ifs_in_zone[ifID->ifPort])
256 return(EINVAL);
257 ifID->ifDefZone = zno+1;
258 } else {
259 int i;
260 at_nvestr_t *zone;
261
262 for (i = 0, zone = getSPLocalZone(i);
263 zone;
264 i++, zone = getSPLocalZone(i)) {
265 if (zonename_equal(zone,
266 &defzonep->zonename))
267 break;
268 }
269 if (!zone)
270 return(EINVAL);
271 }
272 ifID->ifZoneName = defzonep->zonename;
273 (void)regDefaultZone(ifID);
274 return(0);
275 }
276 } else
277 return(EINVAL);
278 } else
279 return(ENOTREADY);
280 break;
281 }
282
283 case AIOCREGLOCALZN:
284 {
285 at_nvestr_t *zone = (at_nvestr_t *)data;
286
287 if (!(at_state.flags & AT_ST_STARTED) || !ifID_home)
288 return(ENOTREADY);
289
290 if (MULTIPORT_MODE)
291 return(EINVAL);
292
293 return(setLocalZones(zone, zone->len));
294
295 break;
296 }
297 case AIOCSETZNUSAGE:
298 if (!(at_state.flags & AT_ST_STARTED) || !ifID_home)
299 return(ENOTREADY);
300
301 if (!ROUTING_MODE)
302 return(EINVAL);
303
304 return(set_zones((zone_usage_t *)data));
305
306 break;
307
308 case AIOCGETZNUSAGE:
309 if (!(at_state.flags & AT_ST_STARTED) || !ifID_home)
310 return(ENOTREADY);
311
312 if (!MULTIPORT_MODE)
313 return(EINVAL);
314
315 if (getRTRLocalZone((zone_usage_t *)data))
316 return(0);
317 else
318 return(ENOENT);
319 break;
320
321 case AIOCNBPREG:
322 {
323 at_nbp_reg_t *nbpP = (at_nbp_reg_t *)data;
324 nve_entry_t nve;
325 int error;
326
327 if (!(at_state.flags & AT_ST_STARTED) || !ifID_home)
328 return(ENOTREADY);
329
330 /* multihoming mode */
331 if (MULTIHOME_MODE) {
332 return(nbp_mh_reg(nbpP));
333 }
334
335 /* single port mode or router mode */
336 if (nbp_fillin_nve(&nbpP->name, &nve) != 0) {
337 /* bad tuple... */
338 return(EINVAL);
339 }
340
341 /* In routing mode when the zone is specified, we need to
342 find an interface on which the specified zone is seeded, so
343 that the zone multicast will be plausible. */
344 if (ROUTING_MODE && !(DEFAULT_ZONE(&nve.zone))) {
345 /* find first segment (interface) which is seeded for
346 this zone */
347 int finished = FALSE;
348 int zno;
349 char ifs_in_zone[IF_TOTAL_MAX];
350 if (!(zno = zt_find_zname(&nve.zone))) {
351 return(EINVAL);
352 }
353 getIfUsage(zno-1, ifs_in_zone);
354
355 TAILQ_FOREACH(ifID, &at_ifQueueHd, aa_link) {
356 if (!ifs_in_zone[ifID->ifPort])
357 /* zone doesn't match */
358 continue;
359 else
360 finished = TRUE;
361 }
362 if (!finished)
363 return(EINVAL);
364 } else
365 ifID = ifID_home;
366
367 nve.address.net = ifID->ifThisNode.s_net;
368 nve.address.node = ifID->ifThisNode.s_node;
369 nve.address.socket = nbpP->addr.socket;
370 nve.ddptype = nbpP->ddptype;
371
372 if (nbp_find_nve(&nve))
373 return(EADDRNOTAVAIL);
374
375 /* Normal case; no tuple found for this name, so insert
376 * this tuple in the registry and return ok response.
377 */
378 ATDISABLE(nve_lock, NVE_LOCK);
379 if ((error = nbp_new_nve_entry(&nve, ifID)) == 0) {
380 nbpP->addr.net = ifID->ifThisNode.s_net;
381 nbpP->addr.node = ifID->ifThisNode.s_node;
382 nbpP->unique_nbp_id = nve.unique_nbp_id;
383 }
384 ATENABLE(nve_lock, NVE_LOCK);
385
386 return(error);
387 break;
388 }
389
390 case AIOCNBPREMOVE:
391 {
392 at_nbp_reg_t *nbpP = (at_nbp_reg_t *)data;
393 nve_entry_t *nve_entry, nve;
394
395 if (!(at_state.flags & AT_ST_STARTED))
396 return(ENOTREADY);
397
398 /* delete by id */
399 if (nbpP->unique_nbp_id) {
400 ATDISABLE(nve_lock, NVE_LOCK);
401 TAILQ_FOREACH(nve_entry, &name_registry, nve_link) {
402 if (nve_entry->unique_nbp_id == nbpP->unique_nbp_id) {
403 /* Found a match! */
404 nbp_delete_entry(nve_entry);
405 ATENABLE(nve_lock, NVE_LOCK);
406 return(0);
407 }
408 }
409 ATENABLE(nve_lock, NVE_LOCK);
410 return(EADDRNOTAVAIL);
411 }
412
413 /* delete by entity */
414 if (nbp_fillin_nve(&nbpP->name, &nve) != 0) {
415 /* bad tuple... */
416 return(EINVAL);
417 }
418
419 if (MULTIHOME_MODE && DEFAULT_ZONE(&nbpP->name.zone)) {
420 /* if mhome & *, remove nve from all default zones */
421 int found = FALSE; /* if any found & deleted */
422
423 TAILQ_FOREACH(ifID, &at_ifQueueHd, aa_link) {
424 nve.zone = ifID->ifZoneName;
425 nve.zone_hash = nbp_strhash(&nve.zone);
426 if ((nve_entry = nbp_find_nve(&nve)) == NULL)
427 continue;
428
429 ATDISABLE(nve_lock, NVE_LOCK);
430 nbp_delete_entry(nve_entry);
431 ATENABLE(nve_lock, NVE_LOCK);
432 found = TRUE;
433 }
434 if (found)
435 return(0);
436 else
437 return(EADDRNOTAVAIL);
438 }
439
440 if ((nve_entry = nbp_find_nve(&nve)) == NULL)
441 /* Can't find the tuple we're looking for, send error*/
442 return(EADDRNOTAVAIL);
443
444 /* Normal case; tuple found for this name, so delete
445 * the entry from the registry and return ok response.
446 */
447 ATDISABLE(nve_lock, NVE_LOCK);
448 nbp_delete_entry(nve_entry);
449 ATENABLE(nve_lock, NVE_LOCK);
450 return(0);
451
452 break;
453 }
454
455 case AIOCSETROUTER:
456 {
457 at_router_params_t *rt = (at_router_params_t *)data;
458
459 /* check for root access */
460 if (error = suser(p->p_ucred, &p->p_acflag))
461 return(EACCES);
462
463 /* when in routing/multihome mode the AIOCSETROUTER IOCTL
464 is done first */
465 if (at_state.flags & AT_ST_STARTED)
466 return(EALREADY);
467
468 /* Setup the routing & zip table size for the router */
469 if (rt->rtmp_table_sz >= RT_MIN && rt->rtmp_table_sz <= RT_MAX)
470 RT_maxentry = rt->rtmp_table_sz;
471 else
472 RT_maxentry = RT_DEFAULT;
473
474 if (rt->zone_table_sz >= ZT_MIN && rt->zone_table_sz <= ZT_MAX)
475 ZT_maxentry = rt->zone_table_sz;
476 else
477 ZT_maxentry = ZT_DEFAULT;
478
479 if (rt_table_init() == ENOBUFS)
480 return(ENOBUFS);
481
482 if (rt->router_mix)
483 RouterMix = (int)rt->router_mix;
484 else
485 RouterMix = RT_MIX_DEFAULT;
486
487 add_ddp_handler(RTMP_SOCKET, rtmp_router_input);
488
489 if (rt->multihome)
490 at_state.flags |= AT_ST_MULTIHOME;
491 else
492 at_state.flags |= AT_ST_ROUTER;
493 break;
494 }
495 case AIOCSTARTROUTER:
496 {
497 at_kern_err_t *keP = (at_kern_err_t *)data;
498
499 /* check for root access */
500 if (suser(p->p_ucred, &p->p_acflag))
501 return(EACCES);
502
503 if (!(at_state.flags & AT_ST_STARTED))
504 return(ENOTREADY);
505
506 bzero(keP, sizeof(at_kern_err_t));
507 error = routerStart(keP);
508
509 break;
510 }
511 case AIOCGETROUTER:
512 {
513 at_router_params_t *rt = (at_router_params_t *)data;
514
515 if (!(at_state.flags & AT_ST_STARTED))
516 return(ENOTREADY);
517
518 rt->multihome = (MULTIHOME_MODE)? 1: 0;
519 rt->rtmp_table_sz = RT_maxentry;
520 rt->zone_table_sz = ZT_maxentry;
521 rt->router_mix = RouterMix;
522
523 break;
524 }
525 case AIOCSTOPATALK:
526 {
527 int *count_only = (int *)data,
528 ret;
529
530 /* check for root access */
531 if (error = suser(p->p_ucred, &p->p_acflag))
532 return(EACCES);
533
534 ret = ddp_shutdown(*count_only);
535 if (*count_only) {
536 *count_only = ret;
537 return(0);
538 } else
539 return((ret == 0)? 0 : EBUSY);
540 break;
541 }
542
543 case SIOCSIFADDR:
544 /* check for root access */
545 if (error = suser(p->p_ucred, &p->p_acflag))
546 error = EACCES;
547 else if (ifID)
548 error = EEXIST;
549 else {
550 int s;
551 if (xpatcnt == 0) {
552 at_state.flags |= AT_ST_STARTED;
553 ddp_brt_init();
554 }
555
556 /* *** find an empty entry *** */
557 ifID = &at_interfaces[xpatcnt];
558 bzero((caddr_t)ifID, sizeof(at_ifaddr_t));
559 strncpy(ifID->ifName, ifr->ifr_name, sizeof(ifID->ifName));
560
561 ifID->aa_ifp = ifp;
562 ifa = &ifID->aa_ifa;
563 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
564 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
565 (sdl->sdl_family == AF_LINK)) {
566 bcopy(LLADDR(sdl), ifID->xaddr, sizeof(ifID->xaddr));
567#ifdef APPLETALK_DEBUG
568 kprintf("SIOCSIFADDR: local enet address is %x.%x.%x.%x.%x.%x\n",
569 ifID->xaddr[0], ifID->xaddr[1],
570 ifID->xaddr[2], ifID->xaddr[3],
571 ifID->xaddr[4], ifID->xaddr[5]);
572#endif
573 break;
574 }
575
576 /* attach the AppleTalk address to the ifnet structure */
577 ifa = &ifID->aa_ifa;
578 ifa->ifa_addr = (struct sockaddr *)&ifID->ifNodeAddress;
579 ifID->ifNodeAddress.sat_len = sizeof(struct sockaddr_at);
580 ifID->ifNodeAddress.sat_family = AF_APPLETALK;
581 /* the address itself will be filled in when ifThisNode
582 is set */
583 s = splnet();
584 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
585 splx(s);
586
587 switch (ifp->if_type) {
588 case IFT_ETHER:
589 ether_attach_at(ifp, &ifID->at_dl_tag,
590 &ifID->aarp_dl_tag);
591 error = 0;
592 ifID->cable_multicast_addr = etalk_multicast_addr;
593
594 xpatcnt++;
595 break;
596 case IFT_FDDI:
597 ifID->cable_multicast_addr = etalk_multicast_addr;
598 ddp_bit_reverse(&ifID->cable_multicast_addr);
599 xpatcnt++;
600 break;
601 case IFT_ISO88025: /* token ring */
602 ifID->cable_multicast_addr = ttalk_multicast_addr;
603 ddp_bit_reverse(&ifID->cable_multicast_addr);
604
605 xpatcnt++;
606 break;
607 default:
608 error = EINVAL;
609 }
610 }
611 break;
612
613 /* complete the initialization started in SIOCSIFADDR */
614 case AIOCSIFADDR:
615 {
616 at_if_cfg_t *cfgp = (at_if_cfg_t *)data;
617
618 if (!(at_state.flags & AT_ST_STARTED))
619 return(ENOTREADY);
620
621 if (!(ifID = find_ifID(cfgp->ifr_name)))
622 return(EINVAL);
623
624 return(lap_online(ifID, cfgp));
625 break;
626 }
627
628#ifdef NOT_YET
629 /* *** this can't be added until AT can handle dynamic addition and
630 deletion of interfaces *** */
631 case SIOCDIFADDR:
632 /* check for root access */
633 if (error = suser(p->p_ucred, &p->p_acflag))
634 error = EACCES;
635 else if (!ifID)
636 error = EINVAL;
637 else
638 elap_offline(ifID);
639 break;
640#endif
641
642 case SIOCSETOT: {
643 int s;
644 struct atpcb *at_pcb, *clonedat_pcb;
645 int cloned_fd = *(int *)data;
646
647 s = splnet(); /* XXX */
648 at_pcb = sotoatpcb(so);
649
650 /* let's make sure it's either -1 or a valid file descriptor */
651 if (cloned_fd != -1) {
652 struct socket *cloned_so;
653 struct file *cloned_fp;
654 error = getsock(p->p_fd, cloned_fd, &cloned_fp);
655 if (error){
656 splx(s); /* XXX */
657 break;
658 }
659 cloned_so = (struct socket *)cloned_fp->f_data;
660 clonedat_pcb = sotoatpcb(cloned_so);
661 } else {
662 clonedat_pcb = NULL;
663 }
664
665 if (clonedat_pcb == NULL) {
666 at_pcb->ddp_flags |= DDPFLG_STRIPHDR;
667 } else {
668 at_pcb->ddp_flags = clonedat_pcb->ddp_flags;
669 }
670 splx(s); /* XXX */
671 break;
672 }
673
674 default:
675 if (ifp == 0 || ifp->if_ioctl == 0)
676 return (EOPNOTSUPP);
677 return dlil_ioctl(0, ifp, cmd, (caddr_t) data);
678 }
679
680 return(error);
681}