2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1998 Apple Computer, Inc.
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>
37 #include <sys/kauth.h>
40 #include <net/if_dl.h>
41 #include <net/if_types.h>
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>
53 #include <sys/kern_event.h>
55 extern int at_ioctl(struct atpcb
*, u_long
, caddr_t
, int fromKernel
);
56 extern int routerStart(at_kern_err_t
*);
57 extern void elap_offline(at_ifaddr_t
*);
58 extern at_ifaddr_t
*find_ifID(char *);
59 extern at_nvestr_t
*getRTRLocalZone(zone_usage_t
*);
60 extern int setLocalZones(at_nvestr_t
*, int);
63 extern at_ifaddr_t at_interfaces
[];
64 extern at_ifaddr_t
*ifID_home
;
65 extern TAILQ_HEAD(name_registry
, _nve_
) name_registry
;
68 struct etalk_addr etalk_multicast_addr
= {
69 {0x09, 0x00, 0x07, 0xff, 0xff, 0xff}};
70 struct etalk_addr ttalk_multicast_addr
= {
71 {0xC0, 0x00, 0x40, 0x00, 0x00, 0x00}};
73 /* called only in router mode */
74 static int set_zones(ifz
)
77 /* 1. adds zone to table
78 2. looks up each route entry from zone list
79 3. sets zone bit in each route entry
81 returns 0 if successful
82 errno if error occurred
90 zno
= zt_add_zone(ifz
->zone_name
.str
, ifz
->zone_name
.len
);
92 if (zno
== ZT_MAXEDOUT
) {
93 dPrintf(D_M_ELAP
, D_L_ERROR
, ("set_zones: error: table full\n"));
97 ifID_home
->ifZoneName
= ifz
->zone_name
;
98 ifID_home
->ifDefZone
= zno
;
101 for (i
=0; i
<IF_TOTAL_MAX
; i
++) {
102 if (ifz
->zone_iflist
.at_if
[i
][0]) {
103 if ((ifID
= find_ifID(ifz
->zone_iflist
.at_if
[i
]))) {
104 rte
= rt_blookup(ifID
->ifThisCableEnd
);
106 dPrintf(D_M_ELAP
, D_L_ERROR
,
107 ("set_zones: error: can't find route\n"));
109 zt_set_zmap(zno
, rte
->ZoneBitMap
);
111 /* if first zone for this I/F,
113 if (!ifID
->ifDefZone
)
114 ifID
->ifDefZone
= zno
;
124 * Generic internet control operations (ioctl's).
125 * ifp is 0 if not an interface-specific ioctl.
129 at_control(so
, cmd
, data
, ifp
)
135 struct ifreq
*ifr
= (struct ifreq
*)data
;
136 int pat_id
= 0, error
= 0;
137 struct proc
*p
= current_proc();
138 at_ifaddr_t
*ifID
= 0;
140 struct sockaddr_dl
*sdl
;
142 if ((cmd
& 0xffff) == 0xff99) {
143 u_long fixed_command
;
144 char ioctl_buffer
[32];
145 /* *** this is a temporary hack to get at_send_to_dev() to
146 work with BSD-style sockets instead of the special purpose
147 system calls, ATsocket() and ATioctl().
149 fixed_command
= _IOW(0, 0xff99, user_addr_t
);
150 if ((error
= at_ioctl((struct atpcb
*)so
->so_pcb
, fixed_command
, data
, 0))) {
151 if (((struct atpcb
*)so
->so_pcb
)->proto
!= ATPROTO_LAP
) {
152 ((struct atpcb
*)so
->so_pcb
)->proto
= ATPROTO_LAP
;
153 error
= at_ioctl((struct atpcb
*)so
->so_pcb
, fixed_command
, data
, 0);
158 /* *** processing should be
163 * Find address for this interface, if it exists.
166 for (pat_id
= 0; pat_id
< xpatcnt
; pat_id
++)
167 if (at_interfaces
[pat_id
].aa_ifp
== ifp
) {
168 ifID
= &at_interfaces
[pat_id
];
176 at_state_t
*global_state
= (at_state_t
*)data
;
178 *global_state
= at_state
;
185 at_if_cfg_t
*cfgp
= (at_if_cfg_t
*)data
;
188 if ((at_state
.flags
& AT_ST_STARTED
) &&
190 if (strlen(cfgp
->ifr_name
)) {
191 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
192 if (!strncmp(ifID
->ifName
, cfgp
->ifr_name
,
193 strlen(ifID
->ifName
)))
198 strncpy(cfgp
->ifr_name
, ifID
->ifName
,
199 sizeof(ifID
->ifName
));
201 if (ifID
&& ifID
->ifState
!= LAP_OFFLINE
) {
202 cfgp
->flags
= ifID
->ifFlags
;
203 /* put the IF state into the low order
205 cfgp
->flags
|= (ifID
->ifState
& LAP_STATE_MASK
);
206 cfgp
->node
= ifID
->ifThisNode
;
207 cfgp
->router
= ifID
->ifARouter
;
208 cfgp
->netStart
= ifID
->ifThisCableStart
;
209 cfgp
->netEnd
= ifID
->ifThisCableEnd
;
210 cfgp
->zonename
= ifID
->ifZoneName
;
221 at_def_zone_t
*defzonep
= (at_def_zone_t
*)data
;
223 /* check for root access */
224 if (error
= suser(kauth_cred_get(), 0))
228 if ((at_state
.flags
& AT_ST_STARTED
) && ifID_home
) {
229 if (strlen(defzonep
->ifr_name
)) {
230 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
231 if (!strncmp(ifID
->ifName
, defzonep
->ifr_name
,
232 strlen(ifID
->ifName
)))
237 strncpy(defzonep
->ifr_name
, ifID
->ifName
,
238 sizeof(ifID
->ifName
));
241 /* In routing mode the default zone is only set for the
242 default interface. */
243 if (ROUTING_MODE
&& (ifID
!= ifID_home
))
246 if (ifID
&& ifID
->ifState
!= LAP_OFFLINE
) {
247 if (zonename_equal(&ifID
->ifZoneName
,
248 &defzonep
->zonename
))
251 /* check the zone name */
252 if (MULTIPORT_MODE
) {
254 at_ifnames_t ifs_in_zone
;
256 if (!(zno
= zt_find_zname(&defzonep
->zonename
)))
259 getIfUsage(zno
-1, &ifs_in_zone
);
260 if (!ifs_in_zone
.at_if
[ifID
->ifPort
])
262 ifID
->ifDefZone
= zno
+1;
267 for (i
= 0, zone
= getSPLocalZone(i
);
269 i
++, zone
= getSPLocalZone(i
)) {
270 if (zonename_equal(zone
,
271 &defzonep
->zonename
))
277 ifID
->ifZoneName
= defzonep
->zonename
;
278 (void)regDefaultZone(ifID
);
280 /* AppleTalk zone was changed. Send event with zone info. */
281 atalk_post_msg(ifID
->aa_ifp
, KEV_ATALK_ZONEUPDATED
, 0, &(ifID
->ifZoneName
));
294 at_nvestr_t
*zone
= (at_nvestr_t
*)data
;
296 if (!(at_state
.flags
& AT_ST_STARTED
) || !ifID_home
)
302 return(setLocalZones(zone
, zone
->len
));
307 if (!(at_state
.flags
& AT_ST_STARTED
) || !ifID_home
)
313 return(set_zones((zone_usage_t
*)data
));
318 if (!(at_state
.flags
& AT_ST_STARTED
) || !ifID_home
)
324 if (getRTRLocalZone((zone_usage_t
*)data
))
332 at_nbp_reg_t
*nbpP
= (at_nbp_reg_t
*)data
;
336 if (!(at_state
.flags
& AT_ST_STARTED
) || !ifID_home
)
339 /* multihoming mode */
340 if (MULTIHOME_MODE
) {
341 return(nbp_mh_reg(nbpP
));
344 /* single port mode or router mode */
345 if (nbp_fillin_nve(&nbpP
->name
, &nve
) != 0) {
350 /* In routing mode when the zone is specified, we need to
351 find an interface on which the specified zone is seeded, so
352 that the zone multicast will be plausible. */
353 if (ROUTING_MODE
&& !(DEFAULT_ZONE(&nve
.zone
))) {
354 /* find first segment (interface) which is seeded for
356 int finished
= FALSE
;
358 at_ifnames_t ifs_in_zone
;
359 if (!(zno
= zt_find_zname(&nve
.zone
))) {
362 getIfUsage(zno
-1, &ifs_in_zone
);
364 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
365 if (!ifs_in_zone
.at_if
[ifID
->ifPort
])
366 /* zone doesn't match */
378 nve
.address
.net
= ifID
->ifThisNode
.s_net
;
379 nve
.address
.node
= ifID
->ifThisNode
.s_node
;
380 nve
.address
.socket
= nbpP
->addr
.socket
;
381 nve
.ddptype
= nbpP
->ddptype
;
383 if (nbp_find_nve(&nve
))
384 return(EADDRNOTAVAIL
);
386 /* Normal case; no tuple found for this name, so insert
387 * this tuple in the registry and return ok response.
389 ATDISABLE(nve_lock
, NVE_LOCK
);
390 if ((error2
= nbp_new_nve_entry(&nve
, ifID
)) == 0) {
391 nbpP
->addr
.net
= ifID
->ifThisNode
.s_net
;
392 nbpP
->addr
.node
= ifID
->ifThisNode
.s_node
;
393 nbpP
->unique_nbp_id
= nve
.unique_nbp_id
;
395 ATENABLE(nve_lock
, NVE_LOCK
);
403 at_nbp_reg_t
*nbpP
= (at_nbp_reg_t
*)data
;
404 nve_entry_t
*nve_entry
, nve
;
406 if (!(at_state
.flags
& AT_ST_STARTED
))
410 if (nbpP
->unique_nbp_id
) {
411 ATDISABLE(nve_lock
, NVE_LOCK
);
412 TAILQ_FOREACH(nve_entry
, &name_registry
, nve_link
) {
413 if (nve_entry
->unique_nbp_id
== nbpP
->unique_nbp_id
) {
415 nbp_delete_entry(nve_entry
);
416 ATENABLE(nve_lock
, NVE_LOCK
);
420 ATENABLE(nve_lock
, NVE_LOCK
);
421 return(EADDRNOTAVAIL
);
424 /* delete by entity */
425 if (nbp_fillin_nve(&nbpP
->name
, &nve
) != 0) {
430 if (MULTIHOME_MODE
&& DEFAULT_ZONE(&nbpP
->name
.zone
)) {
431 /* if mhome & *, remove nve from all default zones */
432 int found
= FALSE
; /* if any found & deleted */
434 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
435 nve
.zone
= ifID
->ifZoneName
;
436 nve
.zone_hash
= nbp_strhash(&nve
.zone
);
437 if ((nve_entry
= nbp_find_nve(&nve
)) == NULL
)
440 ATDISABLE(nve_lock
, NVE_LOCK
);
441 nbp_delete_entry(nve_entry
);
442 ATENABLE(nve_lock
, NVE_LOCK
);
448 return(EADDRNOTAVAIL
);
451 if ((nve_entry
= nbp_find_nve(&nve
)) == NULL
)
452 /* Can't find the tuple we're looking for, send error*/
453 return(EADDRNOTAVAIL
);
455 /* Normal case; tuple found for this name, so delete
456 * the entry from the registry and return ok response.
458 ATDISABLE(nve_lock
, NVE_LOCK
);
459 nbp_delete_entry(nve_entry
);
460 ATENABLE(nve_lock
, NVE_LOCK
);
468 at_router_params_t
*rt
= (at_router_params_t
*)data
;
470 /* check for root access */
471 if (error
= suser(kauth_cred_get(), 0))
474 /* when in routing/multihome mode the AIOCSETROUTER IOCTL
476 if (at_state
.flags
& AT_ST_STARTED
)
479 /* Setup the routing & zip table size for the router */
480 if (rt
->rtmp_table_sz
>= RT_MIN
&& rt
->rtmp_table_sz
<= RT_MAX
)
481 RT_maxentry
= rt
->rtmp_table_sz
;
483 RT_maxentry
= RT_DEFAULT
;
485 if (rt
->zone_table_sz
>= ZT_MIN
&& rt
->zone_table_sz
<= ZT_MAX
)
486 ZT_maxentry
= rt
->zone_table_sz
;
488 ZT_maxentry
= ZT_DEFAULT
;
490 if (rt_table_init() == ENOBUFS
)
494 RouterMix
= (int)rt
->router_mix
;
496 RouterMix
= RT_MIX_DEFAULT
;
498 add_ddp_handler(RTMP_SOCKET
, rtmp_router_input
);
501 at_state
.flags
|= AT_ST_MULTIHOME
;
503 at_state
.flags
|= AT_ST_ROUTER
;
506 case AIOCSTARTROUTER
:
508 at_kern_err_t
*keP
= (at_kern_err_t
*)data
;
510 /* check for root access */
511 if (suser(kauth_cred_get(), 0))
514 if (!(at_state
.flags
& AT_ST_STARTED
))
517 bzero(keP
, sizeof(at_kern_err_t
));
518 error
= routerStart(keP
);
524 at_router_params_t
*rt
= (at_router_params_t
*)data
;
526 if (!(at_state
.flags
& AT_ST_STARTED
))
529 rt
->multihome
= (MULTIHOME_MODE
)? 1: 0;
530 rt
->rtmp_table_sz
= RT_maxentry
;
531 rt
->zone_table_sz
= ZT_maxentry
;
532 rt
->router_mix
= RouterMix
;
538 int *count_only
= (int *)data
,
541 /* check for root access */
542 if (error
= suser(kauth_cred_get(), 0))
545 ret
= ddp_shutdown(*count_only
);
547 if (*count_only
!= 0)
556 /* AppleTalk was successfully shut down. Send event. */
557 atalk_post_msg(0, KEV_ATALK_DISABLED
, 0, 0);
568 /* check for root access */
569 if (error
= suser(kauth_cred_get(), 0))
576 at_state
.flags
|= AT_ST_STARTING
;
580 /* *** find an empty entry *** */
581 ifID
= &at_interfaces
[xpatcnt
];
582 bzero((caddr_t
)ifID
, sizeof(at_ifaddr_t
));
583 strncpy(ifID
->ifName
, ifr
->ifr_name
, sizeof(ifID
->ifName
));
587 ifnet_lock_exclusive(ifp
);
588 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
)
589 if ((sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
) &&
590 (sdl
->sdl_family
== AF_LINK
)) {
591 bcopy(LLADDR(sdl
), ifID
->xaddr
, sizeof(ifID
->xaddr
));
592 #ifdef APPLETALK_DEBUG
593 kprintf("SIOCSIFADDR: local enet address is %x.%x.%x.%x.%x.%x\n",
594 ifID
->xaddr
[0], ifID
->xaddr
[1],
595 ifID
->xaddr
[2], ifID
->xaddr
[3],
596 ifID
->xaddr
[4], ifID
->xaddr
[5]);
601 /* attach the AppleTalk address to the ifnet structure */
603 ifa
->ifa_addr
= (struct sockaddr
*)&ifID
->ifNodeAddress
;
604 ifID
->ifNodeAddress
.sat_len
= sizeof(struct sockaddr_at
);
605 ifID
->ifNodeAddress
.sat_family
= AF_APPLETALK
;
606 /* the address itself will be filled in when ifThisNode
608 if_attach_ifa(ifp
, ifa
);
609 ifnet_lock_done(ifp
);
611 switch (ifp
->if_type
) {
614 case IFT_IEEE8023ADLAG
: /* bonded ethernet */
615 ether_attach_at(ifp
);
617 ifID
->cable_multicast_addr
= etalk_multicast_addr
;
622 ifID
->cable_multicast_addr
= etalk_multicast_addr
;
623 ddp_bit_reverse(&ifID
->cable_multicast_addr
);
626 case IFT_ISO88025
: /* token ring */
627 ifID
->cable_multicast_addr
= ttalk_multicast_addr
;
628 ddp_bit_reverse(&ifID
->cable_multicast_addr
);
638 /* complete the initialization started in SIOCSIFADDR */
641 at_if_cfg_t
*cfgp
= (at_if_cfg_t
*)data
;
643 if (!(at_state
.flags
& AT_ST_STARTING
))
646 if (!(ifID
= find_ifID(cfgp
->ifr_name
)))
649 return(lap_online(ifID
, cfgp
));
654 /* *** this can't be added until AT can handle dynamic addition and
655 deletion of interfaces *** */
657 /* check for root access */
658 if (error
= suser(kauth_cred_get(), 0))
669 struct atpcb
*at_pcb
, *clonedat_pcb
;
670 int cloned_fd
= *(int *)data
;
672 s
= splnet(); /* XXX */
673 at_pcb
= sotoatpcb(so
);
675 /* let's make sure it's either -1 or a valid file descriptor */
676 if (cloned_fd
!= -1) {
677 struct socket
*cloned_so
;
678 error
= file_socket(cloned_fd
, &cloned_so
);
683 clonedat_pcb
= sotoatpcb(cloned_so
);
688 if (clonedat_pcb
== NULL
) {
689 at_pcb
->ddp_flags
|= DDPFLG_STRIPHDR
;
691 at_pcb
->ddp_flags
= clonedat_pcb
->ddp_flags
;
694 file_drop(cloned_fd
);
699 if (ifp
== 0 || ifp
->if_ioctl
== 0)
701 return dlil_ioctl(0, ifp
, cmd
, (caddr_t
) data
);
707 /* From dlil_post_msg() */
708 void atalk_post_msg(struct ifnet
*ifp
, u_long event_code
, struct at_addr
*address
, at_nvestr_t
*zone
)
710 struct kev_atalk_data at_event_data
;
711 struct kev_msg ev_msg
;
713 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
714 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
715 ev_msg
.kev_subclass
= KEV_ATALK_SUBCLASS
;
716 ev_msg
.event_code
= event_code
;
718 bzero(&at_event_data
, sizeof(struct kev_atalk_data
));
721 strncpy(&at_event_data
.link_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
722 at_event_data
.link_data
.if_family
= ifp
->if_family
;
723 at_event_data
.link_data
.if_unit
= (unsigned long) ifp
->if_unit
;
727 at_event_data
.node_data
.address
= *address
;
729 else if (zone
!= 0) {
730 at_event_data
.node_data
.zone
= *zone
;
733 ev_msg
.dv
[0].data_length
= sizeof(struct kev_atalk_data
);
734 ev_msg
.dv
[0].data_ptr
= &at_event_data
;
735 ev_msg
.dv
[1].data_length
= 0;
737 kev_post_msg(&ev_msg
);