]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/netat/at.c
xnu-792.10.96.tar.gz
[apple/xnu.git] / bsd / netat / at.c
... / ...
CommitLineData
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#include <sys/kauth.h>
38
39#include <net/if.h>
40#include <net/if_dl.h>
41#include <net/if_types.h>
42#include <net/dlil.h>
43
44#include <netat/appletalk.h>
45#include <netat/sysglue.h>
46#include <netat/at_pcb.h>
47#include <netat/at_var.h>
48#include <netat/ddp.h>
49#include <netat/nbp.h>
50#include <netat/routing_tables.h>
51#include <netat/debug.h>
52
53#include <sys/kern_event.h>
54
55extern int at_ioctl(struct atpcb *, u_long, caddr_t, int fromKernel);
56extern int routerStart(at_kern_err_t *);
57extern void elap_offline(at_ifaddr_t *);
58extern at_ifaddr_t *find_ifID(char *);
59extern at_nvestr_t *getRTRLocalZone(zone_usage_t *);
60extern int setLocalZones(at_nvestr_t *, int);
61
62extern int xpatcnt;
63extern at_ifaddr_t at_interfaces[];
64extern at_ifaddr_t *ifID_home;
65extern TAILQ_HEAD(name_registry, _nve_) name_registry;
66extern int nve_lock;
67
68struct etalk_addr etalk_multicast_addr = {
69 {0x09, 0x00, 0x07, 0xff, 0xff, 0xff}};
70struct etalk_addr ttalk_multicast_addr = {
71 {0xC0, 0x00, 0x40, 0x00, 0x00, 0x00}};
72
73/* called only in router mode */
74static int set_zones(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
128at_control(so, cmd, data, ifp)
129 struct socket *so;
130 u_long cmd;
131 caddr_t data;
132 struct ifnet *ifp;
133{
134 struct ifreq *ifr = (struct ifreq *)data;
135 int pat_id = 0, error = 0;
136 struct proc *p = current_proc();
137 at_ifaddr_t *ifID = 0;
138 struct ifaddr *ifa;
139 struct sockaddr_dl *sdl;
140
141 if ((cmd & 0xffff) == 0xff99) {
142 u_long fixed_command;
143 /* *** this is a temporary hack to get at_send_to_dev() to
144 work with BSD-style sockets instead of the special purpose
145 system calls, ATsocket() and ATioctl().
146 *** */
147 fixed_command = _IOW(0, 0xff99, user_addr_t);
148 if ((error = at_ioctl((struct atpcb *)so->so_pcb, fixed_command, data, 0))) {
149 if (((struct atpcb *)so->so_pcb)->proto != ATPROTO_LAP) {
150 ((struct atpcb *)so->so_pcb)->proto = ATPROTO_LAP;
151 error = at_ioctl((struct atpcb *)so->so_pcb, fixed_command, data , 0);
152 }
153 }
154 return(error);
155
156 /* *** processing should be
157 return(EINVAL);
158 *** */
159 }
160 /*
161 * Find address for this interface, if it exists.
162 */
163 if (ifp)
164 for (pat_id = 0; pat_id < xpatcnt; pat_id++)
165 if (at_interfaces[pat_id].aa_ifp == ifp) {
166 ifID = &at_interfaces[pat_id];
167 break;
168 }
169
170 switch (cmd) {
171
172 case AIOCGETSTATE:
173 {
174 at_state_t *global_state = (at_state_t *)data;
175
176 *global_state = at_state;
177 return(0);
178 break;
179 }
180
181 case AIOCGETIFCFG:
182 {
183 at_if_cfg_t *cfgp = (at_if_cfg_t *)data;
184
185 ifID = 0;
186 if ((at_state.flags & AT_ST_STARTED) &&
187 ifID_home) {
188 if (strlen(cfgp->ifr_name)) {
189 TAILQ_FOREACH(ifID, &at_ifQueueHd, aa_link) {
190 if (!strncmp(ifID->ifName, cfgp->ifr_name,
191 strlen(ifID->ifName)))
192 break;
193 }
194 } else {
195 ifID = ifID_home;
196 strncpy(cfgp->ifr_name, ifID->ifName,
197 sizeof(ifID->ifName));
198 }
199 if (ifID && ifID->ifState != LAP_OFFLINE) {
200 cfgp->flags = ifID->ifFlags;
201 /* put the IF state into the low order
202 bits of flags */
203 cfgp->flags |= (ifID->ifState & LAP_STATE_MASK);
204 cfgp->node = ifID->ifThisNode;
205 cfgp->router = ifID->ifARouter;
206 cfgp->netStart = ifID->ifThisCableStart;
207 cfgp->netEnd = ifID->ifThisCableEnd;
208 cfgp->zonename = ifID->ifZoneName;
209 return(0);
210 } else
211 return(EINVAL);
212 } else
213 return(ENOTREADY);
214 break;
215 }
216
217 case AIOCSETDEFZONE:
218 {
219 at_def_zone_t *defzonep = (at_def_zone_t *)data;
220
221 /* check for root access */
222 if (error = suser(kauth_cred_get(), 0))
223 return(EACCES);
224
225 ifID = 0;
226 if ((at_state.flags & AT_ST_STARTED) && ifID_home) {
227 if (strlen(defzonep->ifr_name)) {
228 TAILQ_FOREACH(ifID, &at_ifQueueHd, aa_link) {
229 if (!strncmp(ifID->ifName, defzonep->ifr_name,
230 strlen(ifID->ifName)))
231 break;
232 }
233 } else {
234 ifID = ifID_home;
235 strncpy(defzonep->ifr_name, ifID->ifName,
236 sizeof(ifID->ifName));
237 }
238
239 /* In routing mode the default zone is only set for the
240 default interface. */
241 if (ROUTING_MODE && (ifID != ifID_home))
242 return(EINVAL);
243
244 if (ifID && ifID->ifState != LAP_OFFLINE) {
245 if (zonename_equal(&ifID->ifZoneName,
246 &defzonep->zonename))
247 return(0);
248 else {
249 /* check the zone name */
250 if (MULTIPORT_MODE) {
251 short zno;
252 at_ifnames_t ifs_in_zone;
253
254 if (!(zno = zt_find_zname(&defzonep->zonename)))
255 return(EINVAL);
256
257 getIfUsage(zno-1, &ifs_in_zone);
258 if (!ifs_in_zone.at_if[ifID->ifPort])
259 return(EINVAL);
260 ifID->ifDefZone = zno+1;
261 } else {
262 int i;
263 at_nvestr_t *zone;
264
265 for (i = 0, zone = getSPLocalZone(i);
266 zone;
267 i++, zone = getSPLocalZone(i)) {
268 if (zonename_equal(zone,
269 &defzonep->zonename))
270 break;
271 }
272 if (!zone)
273 return(EINVAL);
274 }
275 ifID->ifZoneName = defzonep->zonename;
276 (void)regDefaultZone(ifID);
277
278 /* AppleTalk zone was changed. Send event with zone info. */
279 atalk_post_msg(ifID->aa_ifp, KEV_ATALK_ZONEUPDATED, 0, &(ifID->ifZoneName));
280
281 return(0);
282 }
283 } else
284 return(EINVAL);
285 } else
286 return(ENOTREADY);
287 break;
288 }
289
290 case AIOCREGLOCALZN:
291 {
292 at_nvestr_t *zone = (at_nvestr_t *)data;
293
294 if (!(at_state.flags & AT_ST_STARTED) || !ifID_home)
295 return(ENOTREADY);
296
297 if (MULTIPORT_MODE)
298 return(EINVAL);
299
300 return(setLocalZones(zone, zone->len));
301
302 break;
303 }
304 case AIOCSETZNUSAGE:
305 if (!(at_state.flags & AT_ST_STARTED) || !ifID_home)
306 return(ENOTREADY);
307
308 if (!ROUTING_MODE)
309 return(EINVAL);
310
311 return(set_zones((zone_usage_t *)data));
312
313 break;
314
315 case AIOCGETZNUSAGE:
316 if (!(at_state.flags & AT_ST_STARTED) || !ifID_home)
317 return(ENOTREADY);
318
319 if (!MULTIPORT_MODE)
320 return(EINVAL);
321
322 if (getRTRLocalZone((zone_usage_t *)data))
323 return(0);
324 else
325 return(ENOENT);
326 break;
327
328 case AIOCNBPREG:
329 {
330 at_nbp_reg_t *nbpP = (at_nbp_reg_t *)data;
331 nve_entry_t nve;
332 int error2;
333
334 if (!(at_state.flags & AT_ST_STARTED) || !ifID_home)
335 return(ENOTREADY);
336
337 /* multihoming mode */
338 if (MULTIHOME_MODE) {
339 return(nbp_mh_reg(nbpP));
340 }
341
342 /* single port mode or router mode */
343 if (nbp_fillin_nve(&nbpP->name, &nve) != 0) {
344 /* bad tuple... */
345 return(EINVAL);
346 }
347
348 /* In routing mode when the zone is specified, we need to
349 find an interface on which the specified zone is seeded, so
350 that the zone multicast will be plausible. */
351 if (ROUTING_MODE && !(DEFAULT_ZONE(&nve.zone))) {
352 /* find first segment (interface) which is seeded for
353 this zone */
354 int finished = FALSE;
355 int zno;
356 at_ifnames_t ifs_in_zone;
357 if (!(zno = zt_find_zname(&nve.zone))) {
358 return(EINVAL);
359 }
360 getIfUsage(zno-1, &ifs_in_zone);
361
362 TAILQ_FOREACH(ifID, &at_ifQueueHd, aa_link) {
363 if (!ifs_in_zone.at_if[ifID->ifPort])
364 /* zone doesn't match */
365 continue;
366 else {
367 finished = TRUE;
368 break;
369 }
370 }
371 if (!finished)
372 return(EINVAL);
373 } else
374 ifID = ifID_home;
375
376 nve.address.net = ifID->ifThisNode.s_net;
377 nve.address.node = ifID->ifThisNode.s_node;
378 nve.address.socket = nbpP->addr.socket;
379 nve.ddptype = nbpP->ddptype;
380
381 if (nbp_find_nve(&nve))
382 return(EADDRNOTAVAIL);
383
384 /* Normal case; no tuple found for this name, so insert
385 * this tuple in the registry and return ok response.
386 */
387 if ((error2 = nbp_new_nve_entry(&nve, ifID)) == 0) {
388 nbpP->addr.net = ifID->ifThisNode.s_net;
389 nbpP->addr.node = ifID->ifThisNode.s_node;
390 nbpP->unique_nbp_id = nve.unique_nbp_id;
391 }
392
393 return(error2);
394 break;
395 }
396
397 case AIOCNBPREMOVE:
398 {
399 at_nbp_reg_t *nbpP = (at_nbp_reg_t *)data;
400 nve_entry_t *nve_entry, nve;
401
402 if (!(at_state.flags & AT_ST_STARTED))
403 return(ENOTREADY);
404
405 /* delete by id */
406 if (nbpP->unique_nbp_id) {
407 TAILQ_FOREACH(nve_entry, &name_registry, nve_link) {
408 if (nve_entry->unique_nbp_id == nbpP->unique_nbp_id) {
409 /* Found a match! */
410 nbp_delete_entry(nve_entry);
411 return(0);
412 }
413 }
414 return(EADDRNOTAVAIL);
415 }
416
417 /* delete by entity */
418 if (nbp_fillin_nve(&nbpP->name, &nve) != 0) {
419 /* bad tuple... */
420 return(EINVAL);
421 }
422
423 if (MULTIHOME_MODE && DEFAULT_ZONE(&nbpP->name.zone)) {
424 /* if mhome & *, remove nve from all default zones */
425 int found = FALSE; /* if any found & deleted */
426
427 TAILQ_FOREACH(ifID, &at_ifQueueHd, aa_link) {
428 nve.zone = ifID->ifZoneName;
429 nve.zone_hash = nbp_strhash(&nve.zone);
430 if ((nve_entry = nbp_find_nve(&nve)) == NULL)
431 continue;
432
433 nbp_delete_entry(nve_entry);
434 found = TRUE;
435 }
436 if (found)
437 return(0);
438 else
439 return(EADDRNOTAVAIL);
440 }
441
442 if ((nve_entry = nbp_find_nve(&nve)) == NULL)
443 /* Can't find the tuple we're looking for, send error*/
444 return(EADDRNOTAVAIL);
445
446 /* Normal case; tuple found for this name, so delete
447 * the entry from the registry and return ok response.
448 */
449 nbp_delete_entry(nve_entry);
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(kauth_cred_get(), 0))
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(kauth_cred_get(), 0))
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(kauth_cred_get(), 0))
532 return(EACCES);
533
534 ret = ddp_shutdown(*count_only);
535
536 if (*count_only != 0)
537 {
538 *count_only = ret;
539 return(0);
540 }
541 else
542 {
543 if (ret == 0)
544 {
545 /* AppleTalk was successfully shut down. Send event. */
546 atalk_post_msg(0, KEV_ATALK_DISABLED, 0, 0);
547 return 0;
548 }
549 else
550 return EBUSY;
551 }
552
553 break;
554 }
555
556 case SIOCSIFADDR:
557 /* check for root access */
558 if (error = suser(kauth_cred_get(), 0))
559 error = EACCES;
560 else if (ifID)
561 error = EEXIST;
562 else {
563 int s;
564 if (xpatcnt == 0) {
565 at_state.flags |= AT_ST_STARTING;
566 ddp_brt_init();
567 }
568
569 /* *** find an empty entry *** */
570 ifID = &at_interfaces[xpatcnt];
571 bzero((caddr_t)ifID, sizeof(at_ifaddr_t));
572 strncpy(ifID->ifName, ifr->ifr_name, sizeof(ifID->ifName));
573
574 ifID->aa_ifp = ifp;
575 ifa = &ifID->aa_ifa;
576 ifnet_lock_exclusive(ifp);
577 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
578 if ((sdl = (struct sockaddr_dl *)ifa->ifa_addr) &&
579 (sdl->sdl_family == AF_LINK)) {
580 bcopy(LLADDR(sdl), ifID->xaddr, sizeof(ifID->xaddr));
581#ifdef APPLETALK_DEBUG
582 kprintf("SIOCSIFADDR: local enet address is %x.%x.%x.%x.%x.%x\n",
583 ifID->xaddr[0], ifID->xaddr[1],
584 ifID->xaddr[2], ifID->xaddr[3],
585 ifID->xaddr[4], ifID->xaddr[5]);
586#endif
587 break;
588 }
589
590 /* attach the AppleTalk address to the ifnet structure */
591 ifa = &ifID->aa_ifa;
592 ifa->ifa_addr = (struct sockaddr *)&ifID->ifNodeAddress;
593 ifID->ifNodeAddress.sat_len = sizeof(struct sockaddr_at);
594 ifID->ifNodeAddress.sat_family = AF_APPLETALK;
595 /* the address itself will be filled in when ifThisNode
596 is set */
597 if_attach_ifa(ifp, ifa);
598 ifnet_lock_done(ifp);
599
600 switch (ifp->if_type) {
601 case IFT_ETHER:
602 case IFT_L2VLAN:
603 case IFT_IEEE8023ADLAG: /* bonded ethernet */
604 ether_attach_at(ifp);
605 error = 0;
606 ifID->cable_multicast_addr = etalk_multicast_addr;
607
608 xpatcnt++;
609 break;
610 case IFT_FDDI:
611 ifID->cable_multicast_addr = etalk_multicast_addr;
612 ddp_bit_reverse(&ifID->cable_multicast_addr);
613 xpatcnt++;
614 break;
615 case IFT_ISO88025: /* token ring */
616 ifID->cable_multicast_addr = ttalk_multicast_addr;
617 ddp_bit_reverse(&ifID->cable_multicast_addr);
618
619 xpatcnt++;
620 break;
621 default:
622 error = EINVAL;
623 }
624 }
625 break;
626
627 /* complete the initialization started in SIOCSIFADDR */
628 case AIOCSIFADDR:
629 {
630 at_if_cfg_t *cfgp = (at_if_cfg_t *)data;
631
632 if (!(at_state.flags & AT_ST_STARTING))
633 return(ENOTREADY);
634
635 if (!(ifID = find_ifID(cfgp->ifr_name)))
636 return(EINVAL);
637
638 return(lap_online(ifID, cfgp));
639 break;
640 }
641
642#ifdef NOT_YET
643 /* *** this can't be added until AT can handle dynamic addition and
644 deletion of interfaces *** */
645 case SIOCDIFADDR:
646 /* check for root access */
647 if (error = suser(kauth_cred_get(), 0))
648 error = EACCES;
649 else if (!ifID)
650 error = EINVAL;
651 else
652 elap_offline(ifID);
653 break;
654#endif
655
656 case SIOCSETOT: {
657 struct atpcb *at_pcb, *clonedat_pcb;
658 int cloned_fd = *(int *)data;
659
660 at_pcb = sotoatpcb(so);
661
662 /* let's make sure it's either -1 or a valid file descriptor */
663 if (cloned_fd != -1) {
664 struct socket *cloned_so;
665 error = file_socket(cloned_fd, &cloned_so);
666 if (error)
667 break;
668 clonedat_pcb = sotoatpcb(cloned_so);
669 } else {
670 clonedat_pcb = NULL;
671 }
672
673 if (clonedat_pcb == NULL) {
674 at_pcb->ddp_flags |= DDPFLG_STRIPHDR;
675 } else {
676 at_pcb->ddp_flags = clonedat_pcb->ddp_flags;
677 }
678 file_drop(cloned_fd);
679 break;
680 }
681
682 default:
683 if (ifp == 0 || ifp->if_ioctl == 0)
684 return (EOPNOTSUPP);
685 return dlil_ioctl(0, ifp, cmd, (caddr_t) data);
686 }
687
688 return(error);
689}
690
691/* From dlil_post_msg() */
692void atalk_post_msg(struct ifnet *ifp, u_long event_code, struct at_addr *address, at_nvestr_t *zone)
693{
694 struct kev_atalk_data at_event_data;
695 struct kev_msg ev_msg;
696
697 ev_msg.vendor_code = KEV_VENDOR_APPLE;
698 ev_msg.kev_class = KEV_NETWORK_CLASS;
699 ev_msg.kev_subclass = KEV_ATALK_SUBCLASS;
700 ev_msg.event_code = event_code;
701
702 bzero(&at_event_data, sizeof(struct kev_atalk_data));
703
704 if (ifp != 0) {
705 strncpy(&at_event_data.link_data.if_name[0], ifp->if_name, IFNAMSIZ);
706 at_event_data.link_data.if_family = ifp->if_family;
707 at_event_data.link_data.if_unit = (unsigned long) ifp->if_unit;
708 }
709
710 if (address != 0) {
711 at_event_data.node_data.address = *address;
712 }
713 else if (zone != 0) {
714 at_event_data.node_data.zone = *zone;
715 }
716
717 ev_msg.dv[0].data_length = sizeof(struct kev_atalk_data);
718 ev_msg.dv[0].data_ptr = &at_event_data;
719 ev_msg.dv[1].data_length = 0;
720
721 kev_post_msg(&ev_msg);
722}