2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1998 Apple Computer, Inc.
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/ioctl.h>
38 #include <sys/errno.h>
39 #include <sys/malloc.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
43 #include <sys/kauth.h>
46 #include <net/if_dl.h>
47 #include <net/if_types.h>
50 #include <netat/appletalk.h>
51 #include <netat/sysglue.h>
52 #include <netat/at_pcb.h>
53 #include <netat/at_var.h>
54 #include <netat/ddp.h>
55 #include <netat/nbp.h>
56 #include <netat/routing_tables.h>
57 #include <netat/debug.h>
59 #include <sys/kern_event.h>
61 extern int at_ioctl(struct atpcb
*, u_long
, caddr_t
, int fromKernel
);
62 extern int routerStart(at_kern_err_t
*);
63 extern void elap_offline(at_ifaddr_t
*);
64 extern at_ifaddr_t
*find_ifID(char *);
65 extern at_nvestr_t
*getRTRLocalZone(zone_usage_t
*);
66 extern int setLocalZones(at_nvestr_t
*, int);
69 extern at_ifaddr_t at_interfaces
[];
70 extern at_ifaddr_t
*ifID_home
;
71 extern TAILQ_HEAD(name_registry
, _nve_
) name_registry
;
74 struct etalk_addr etalk_multicast_addr
= {
75 {0x09, 0x00, 0x07, 0xff, 0xff, 0xff}};
76 struct etalk_addr ttalk_multicast_addr
= {
77 {0xC0, 0x00, 0x40, 0x00, 0x00, 0x00}};
79 /* called only in router mode */
80 static int set_zones(zone_usage_t
*ifz
)
82 /* 1. adds zone to table
83 2. looks up each route entry from zone list
84 3. sets zone bit in each route entry
86 returns 0 if successful
87 errno if error occurred
95 zno
= zt_add_zone(ifz
->zone_name
.str
, ifz
->zone_name
.len
);
97 if (zno
== ZT_MAXEDOUT
) {
98 dPrintf(D_M_ELAP
, D_L_ERROR
, ("set_zones: error: table full\n"));
101 if (ifz
->zone_home
) {
102 ifID_home
->ifZoneName
= ifz
->zone_name
;
103 ifID_home
->ifDefZone
= zno
;
106 for (i
=0; i
<IF_TOTAL_MAX
; i
++) {
107 if (ifz
->zone_iflist
.at_if
[i
][0]) {
108 if ((ifID
= find_ifID(ifz
->zone_iflist
.at_if
[i
]))) {
109 rte
= rt_blookup(ifID
->ifThisCableEnd
);
111 dPrintf(D_M_ELAP
, D_L_ERROR
,
112 ("set_zones: error: can't find route\n"));
114 zt_set_zmap(zno
, rte
->ZoneBitMap
);
116 /* if first zone for this I/F,
118 if (!ifID
->ifDefZone
)
119 ifID
->ifDefZone
= zno
;
129 * Generic internet control operations (ioctl's).
130 * ifp is 0 if not an interface-specific ioctl.
134 at_control(so
, cmd
, data
, ifp
)
140 struct ifreq
*ifr
= (struct ifreq
*)data
;
141 int pat_id
= 0, error
= 0;
142 struct proc
*p
= current_proc();
143 at_ifaddr_t
*ifID
= 0;
145 struct sockaddr_dl
*sdl
;
147 if ((cmd
& 0xffff) == 0xff99) {
148 u_long fixed_command
;
149 /* *** this is a temporary hack to get at_send_to_dev() to
150 work with BSD-style sockets instead of the special purpose
151 system calls, ATsocket() and ATioctl().
153 fixed_command
= _IOW(0, 0xff99, user_addr_t
);
154 if ((error
= at_ioctl((struct atpcb
*)so
->so_pcb
, fixed_command
, data
, 0))) {
155 if (((struct atpcb
*)so
->so_pcb
)->proto
!= ATPROTO_LAP
) {
156 ((struct atpcb
*)so
->so_pcb
)->proto
= ATPROTO_LAP
;
157 error
= at_ioctl((struct atpcb
*)so
->so_pcb
, fixed_command
, data
, 0);
162 /* *** processing should be
167 * Find address for this interface, if it exists.
170 for (pat_id
= 0; pat_id
< xpatcnt
; pat_id
++)
171 if (at_interfaces
[pat_id
].aa_ifp
== ifp
) {
172 ifID
= &at_interfaces
[pat_id
];
180 at_state_t
*global_state
= (at_state_t
*)data
;
182 *global_state
= at_state
;
189 at_if_cfg_t
*cfgp
= (at_if_cfg_t
*)data
;
192 if ((at_state
.flags
& AT_ST_STARTED
) &&
194 if (strlen(cfgp
->ifr_name
)) {
195 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
196 if (!strncmp(ifID
->ifName
, cfgp
->ifr_name
,
197 strlen(ifID
->ifName
)))
202 strlcpy(cfgp
->ifr_name
, ifID
->ifName
,
203 sizeof(cfgp
->ifr_name
));
205 if (ifID
&& ifID
->ifState
!= LAP_OFFLINE
) {
206 cfgp
->flags
= ifID
->ifFlags
;
207 /* put the IF state into the low order
209 cfgp
->flags
|= (ifID
->ifState
& LAP_STATE_MASK
);
210 cfgp
->node
= ifID
->ifThisNode
;
211 cfgp
->router
= ifID
->ifARouter
;
212 cfgp
->netStart
= ifID
->ifThisCableStart
;
213 cfgp
->netEnd
= ifID
->ifThisCableEnd
;
214 cfgp
->zonename
= ifID
->ifZoneName
;
225 at_def_zone_t
*defzonep
= (at_def_zone_t
*)data
;
227 /* check for root access */
228 if (error
= suser(kauth_cred_get(), 0))
232 if ((at_state
.flags
& AT_ST_STARTED
) && ifID_home
) {
233 if (strlen(defzonep
->ifr_name
)) {
234 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
235 if (!strncmp(ifID
->ifName
, defzonep
->ifr_name
,
236 strlen(ifID
->ifName
)))
241 strlcpy(defzonep
->ifr_name
, ifID
->ifName
,
242 sizeof(defzonep
->ifr_name
));
245 /* In routing mode the default zone is only set for the
246 default interface. */
247 if (ROUTING_MODE
&& (ifID
!= ifID_home
))
250 if (ifID
&& ifID
->ifState
!= LAP_OFFLINE
) {
251 if (zonename_equal(&ifID
->ifZoneName
,
252 &defzonep
->zonename
))
255 /* check the zone name */
256 if (MULTIPORT_MODE
) {
258 at_ifnames_t ifs_in_zone
;
260 if (!(zno
= zt_find_zname(&defzonep
->zonename
)))
263 getIfUsage(zno
-1, &ifs_in_zone
);
264 if (!ifs_in_zone
.at_if
[ifID
->ifPort
])
266 ifID
->ifDefZone
= zno
+1;
271 for (i
= 0, zone
= getSPLocalZone(i
);
273 i
++, zone
= getSPLocalZone(i
)) {
274 if (zonename_equal(zone
,
275 &defzonep
->zonename
))
281 ifID
->ifZoneName
= defzonep
->zonename
;
282 (void)regDefaultZone(ifID
);
284 /* AppleTalk zone was changed. Send event with zone info. */
285 atalk_post_msg(ifID
->aa_ifp
, KEV_ATALK_ZONEUPDATED
, 0, &(ifID
->ifZoneName
));
298 at_nvestr_t
*zone
= (at_nvestr_t
*)data
;
300 if (!(at_state
.flags
& AT_ST_STARTED
) || !ifID_home
)
306 return(setLocalZones(zone
, zone
->len
));
311 if (!(at_state
.flags
& AT_ST_STARTED
) || !ifID_home
)
317 return(set_zones((zone_usage_t
*)data
));
322 if (!(at_state
.flags
& AT_ST_STARTED
) || !ifID_home
)
328 if (getRTRLocalZone((zone_usage_t
*)data
))
336 at_nbp_reg_t
*nbpP
= (at_nbp_reg_t
*)data
;
340 if (!(at_state
.flags
& AT_ST_STARTED
) || !ifID_home
)
343 /* multihoming mode */
344 if (MULTIHOME_MODE
) {
345 return(nbp_mh_reg(nbpP
));
348 /* single port mode or router mode */
349 if (nbp_fillin_nve(&nbpP
->name
, &nve
) != 0) {
354 /* In routing mode when the zone is specified, we need to
355 find an interface on which the specified zone is seeded, so
356 that the zone multicast will be plausible. */
357 if (ROUTING_MODE
&& !(DEFAULT_ZONE(&nve
.zone
))) {
358 /* find first segment (interface) which is seeded for
360 int finished
= FALSE
;
362 at_ifnames_t ifs_in_zone
;
363 if (!(zno
= zt_find_zname(&nve
.zone
))) {
366 getIfUsage(zno
-1, &ifs_in_zone
);
368 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
369 if (!ifs_in_zone
.at_if
[ifID
->ifPort
])
370 /* zone doesn't match */
382 nve
.address
.net
= ifID
->ifThisNode
.s_net
;
383 nve
.address
.node
= ifID
->ifThisNode
.s_node
;
384 nve
.address
.socket
= nbpP
->addr
.socket
;
385 nve
.ddptype
= nbpP
->ddptype
;
387 if (nbp_find_nve(&nve
))
388 return(EADDRNOTAVAIL
);
390 /* Normal case; no tuple found for this name, so insert
391 * this tuple in the registry and return ok response.
393 if ((error2
= nbp_new_nve_entry(&nve
, ifID
)) == 0) {
394 nbpP
->addr
.net
= ifID
->ifThisNode
.s_net
;
395 nbpP
->addr
.node
= ifID
->ifThisNode
.s_node
;
396 nbpP
->unique_nbp_id
= nve
.unique_nbp_id
;
405 at_nbp_reg_t
*nbpP
= (at_nbp_reg_t
*)data
;
406 nve_entry_t
*nve_entry
, nve
;
408 if (!(at_state
.flags
& AT_ST_STARTED
))
412 if (nbpP
->unique_nbp_id
) {
413 TAILQ_FOREACH(nve_entry
, &name_registry
, nve_link
) {
414 if (nve_entry
->unique_nbp_id
== nbpP
->unique_nbp_id
) {
416 nbp_delete_entry(nve_entry
);
420 return(EADDRNOTAVAIL
);
423 /* delete by entity */
424 if (nbp_fillin_nve(&nbpP
->name
, &nve
) != 0) {
429 if (MULTIHOME_MODE
&& DEFAULT_ZONE(&nbpP
->name
.zone
)) {
430 /* if mhome & *, remove nve from all default zones */
431 int found
= FALSE
; /* if any found & deleted */
433 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
434 nve
.zone
= ifID
->ifZoneName
;
435 nve
.zone_hash
= nbp_strhash(&nve
.zone
);
436 if ((nve_entry
= nbp_find_nve(&nve
)) == NULL
)
439 nbp_delete_entry(nve_entry
);
445 return(EADDRNOTAVAIL
);
448 if ((nve_entry
= nbp_find_nve(&nve
)) == NULL
)
449 /* Can't find the tuple we're looking for, send error*/
450 return(EADDRNOTAVAIL
);
452 /* Normal case; tuple found for this name, so delete
453 * the entry from the registry and return ok response.
455 nbp_delete_entry(nve_entry
);
463 at_router_params_t
*rt
= (at_router_params_t
*)data
;
465 /* check for root access */
466 if (error
= suser(kauth_cred_get(), 0))
469 /* when in routing/multihome mode the AIOCSETROUTER IOCTL
471 if (at_state
.flags
& AT_ST_STARTED
)
474 /* Setup the routing & zip table size for the router */
475 if (rt
->rtmp_table_sz
>= RT_MIN
&& rt
->rtmp_table_sz
<= RT_MAX
)
476 RT_maxentry
= rt
->rtmp_table_sz
;
478 RT_maxentry
= RT_DEFAULT
;
480 if (rt
->zone_table_sz
>= ZT_MIN
&& rt
->zone_table_sz
<= ZT_MAX
)
481 ZT_maxentry
= rt
->zone_table_sz
;
483 ZT_maxentry
= ZT_DEFAULT
;
485 if (rt_table_init() == ENOBUFS
)
489 RouterMix
= (int)rt
->router_mix
;
491 RouterMix
= RT_MIX_DEFAULT
;
493 add_ddp_handler(RTMP_SOCKET
, rtmp_router_input
);
496 at_state
.flags
|= AT_ST_MULTIHOME
;
498 at_state
.flags
|= AT_ST_ROUTER
;
501 case AIOCSTARTROUTER
:
503 at_kern_err_t
*keP
= (at_kern_err_t
*)data
;
505 /* check for root access */
506 if (suser(kauth_cred_get(), 0))
509 if (!(at_state
.flags
& AT_ST_STARTED
))
512 bzero(keP
, sizeof(at_kern_err_t
));
513 error
= routerStart(keP
);
519 at_router_params_t
*rt
= (at_router_params_t
*)data
;
521 if (!(at_state
.flags
& AT_ST_STARTED
))
524 rt
->multihome
= (MULTIHOME_MODE
)? 1: 0;
525 rt
->rtmp_table_sz
= RT_maxentry
;
526 rt
->zone_table_sz
= ZT_maxentry
;
527 rt
->router_mix
= RouterMix
;
533 int *count_only
= (int *)data
,
536 /* check for root access */
537 if (error
= suser(kauth_cred_get(), 0))
540 ret
= ddp_shutdown(*count_only
);
542 if (*count_only
!= 0)
551 /* AppleTalk was successfully shut down. Send event. */
552 atalk_post_msg(0, KEV_ATALK_DISABLED
, 0, 0);
563 /* check for root access */
564 if (error
= suser(kauth_cred_get(), 0))
571 at_state
.flags
|= AT_ST_STARTING
;
575 /* *** find an empty entry *** */
576 ifID
= &at_interfaces
[xpatcnt
];
577 bzero((caddr_t
)ifID
, sizeof(at_ifaddr_t
));
578 strlcpy(ifID
->ifName
, ifr
->ifr_name
, sizeof(ifID
->ifName
));
582 ifnet_lock_exclusive(ifp
);
583 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
)
584 if ((sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
) &&
585 (sdl
->sdl_family
== AF_LINK
)) {
586 bcopy(LLADDR(sdl
), ifID
->xaddr
, sizeof(ifID
->xaddr
));
587 #ifdef APPLETALK_DEBUG
588 kprintf("SIOCSIFADDR: local enet address is %x.%x.%x.%x.%x.%x\n",
589 ifID
->xaddr
[0], ifID
->xaddr
[1],
590 ifID
->xaddr
[2], ifID
->xaddr
[3],
591 ifID
->xaddr
[4], ifID
->xaddr
[5]);
596 /* attach the AppleTalk address to the ifnet structure */
598 ifa
->ifa_addr
= (struct sockaddr
*)&ifID
->ifNodeAddress
;
599 ifID
->ifNodeAddress
.sat_len
= sizeof(struct sockaddr_at
);
600 ifID
->ifNodeAddress
.sat_family
= AF_APPLETALK
;
601 /* the address itself will be filled in when ifThisNode
603 if_attach_ifa(ifp
, ifa
);
604 ifnet_lock_done(ifp
);
606 switch (ifp
->if_type
) {
609 case IFT_IEEE8023ADLAG
: /* bonded ethernet */
610 ether_attach_at(ifp
);
612 ifID
->cable_multicast_addr
= etalk_multicast_addr
;
617 ifID
->cable_multicast_addr
= etalk_multicast_addr
;
618 ddp_bit_reverse(&ifID
->cable_multicast_addr
);
621 case IFT_ISO88025
: /* token ring */
622 ifID
->cable_multicast_addr
= ttalk_multicast_addr
;
623 ddp_bit_reverse(&ifID
->cable_multicast_addr
);
633 /* complete the initialization started in SIOCSIFADDR */
636 at_if_cfg_t
*cfgp
= (at_if_cfg_t
*)data
;
638 if (!(at_state
.flags
& AT_ST_STARTING
))
641 if (!(ifID
= find_ifID(cfgp
->ifr_name
)))
644 return(lap_online(ifID
, cfgp
));
649 /* *** this can't be added until AT can handle dynamic addition and
650 deletion of interfaces *** */
652 /* check for root access */
653 if (error
= suser(kauth_cred_get(), 0))
663 struct atpcb
*at_pcb
, *clonedat_pcb
;
664 int cloned_fd
= *(int *)data
;
666 at_pcb
= sotoatpcb(so
);
668 /* let's make sure it's either -1 or a valid file descriptor */
669 if (cloned_fd
!= -1) {
670 struct socket
*cloned_so
;
671 error
= file_socket(cloned_fd
, &cloned_so
);
674 clonedat_pcb
= sotoatpcb(cloned_so
);
679 if (clonedat_pcb
== NULL
) {
680 at_pcb
->ddp_flags
|= DDPFLG_STRIPHDR
;
682 at_pcb
->ddp_flags
= clonedat_pcb
->ddp_flags
;
684 file_drop(cloned_fd
);
689 if (ifp
== 0 || ifp
->if_ioctl
== 0)
691 return dlil_ioctl(0, ifp
, cmd
, (caddr_t
) data
);
697 /* From dlil_post_msg() */
698 void atalk_post_msg(struct ifnet
*ifp
, u_long event_code
, struct at_addr
*address
, at_nvestr_t
*zone
)
700 struct kev_atalk_data at_event_data
;
701 struct kev_msg ev_msg
;
703 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
704 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
705 ev_msg
.kev_subclass
= KEV_ATALK_SUBCLASS
;
706 ev_msg
.event_code
= event_code
;
708 bzero(&at_event_data
, sizeof(struct kev_atalk_data
));
711 strlcpy(&at_event_data
.link_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
712 at_event_data
.link_data
.if_family
= ifp
->if_family
;
713 at_event_data
.link_data
.if_unit
= (unsigned long) ifp
->if_unit
;
717 at_event_data
.node_data
.address
= *address
;
719 else if (zone
!= 0) {
720 at_event_data
.node_data
.zone
= *zone
;
723 ev_msg
.dv
[0].data_length
= sizeof(struct kev_atalk_data
);
724 ev_msg
.dv
[0].data_ptr
= &at_event_data
;
725 ev_msg
.dv
[1].data_length
= 0;
727 kev_post_msg(&ev_msg
);