2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1998 Apple Computer, Inc.
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/ioctl.h>
35 #include <sys/errno.h>
36 #include <sys/malloc.h>
37 #include <sys/socket.h>
38 #include <sys/socketvar.h>
42 #include <net/if_dl.h>
43 #include <net/if_types.h>
46 #include <netat/appletalk.h>
47 #include <netat/sysglue.h>
48 #include <netat/at_pcb.h>
49 #include <netat/at_var.h>
50 #include <netat/ddp.h>
51 #include <netat/nbp.h>
52 #include <netat/routing_tables.h>
53 #include <netat/debug.h>
55 #include <sys/kern_event.h>
57 extern int at_ioctl(struct atpcb
*, u_long
, caddr_t
, int fromKernel
);
58 extern int routerStart(at_kern_err_t
*);
59 extern void elap_offline(at_ifaddr_t
*);
60 extern at_ifaddr_t
*find_ifID(char *);
61 extern at_nvestr_t
*getRTRLocalZone(zone_usage_t
*);
62 extern int setLocalZones(at_nvestr_t
*, int);
65 extern at_ifaddr_t at_interfaces
[];
66 extern at_ifaddr_t
*ifID_home
;
67 extern TAILQ_HEAD(name_registry
, _nve_
) name_registry
;
70 struct etalk_addr etalk_multicast_addr
= {
71 {0x09, 0x00, 0x07, 0xff, 0xff, 0xff}};
72 struct etalk_addr ttalk_multicast_addr
= {
73 {0xC0, 0x00, 0x40, 0x00, 0x00, 0x00}};
75 /* called only in router mode */
76 static int set_zones(ifz
)
79 /* 1. adds zone to table
80 2. looks up each route entry from zone list
81 3. sets zone bit in each route entry
83 returns 0 if successful
84 errno if error occurred
92 zno
= zt_add_zone(ifz
->zone_name
.str
, ifz
->zone_name
.len
);
94 if (zno
== ZT_MAXEDOUT
) {
95 dPrintf(D_M_ELAP
, D_L_ERROR
, ("set_zones: error: table full\n"));
99 ifID_home
->ifZoneName
= ifz
->zone_name
;
100 ifID_home
->ifDefZone
= zno
;
103 for (i
=0; i
<IF_TOTAL_MAX
; i
++) {
104 if (ifz
->zone_iflist
.at_if
[i
][0]) {
105 if ((ifID
= find_ifID(ifz
->zone_iflist
.at_if
[i
]))) {
106 rte
= rt_blookup(ifID
->ifThisCableEnd
);
108 dPrintf(D_M_ELAP
, D_L_ERROR
,
109 ("set_zones: error: can't find route\n"));
111 zt_set_zmap(zno
, rte
->ZoneBitMap
);
113 /* if first zone for this I/F,
115 if (!ifID
->ifDefZone
)
116 ifID
->ifDefZone
= zno
;
126 * Generic internet control operations (ioctl's).
127 * ifp is 0 if not an interface-specific ioctl.
130 int at_control(so
, cmd
, data
, ifp
)
136 struct ifreq
*ifr
= (struct ifreq
*)data
;
137 int pat_id
= 0, error
= 0;
138 struct proc
*p
= current_proc();
139 at_ifaddr_t
*ifID
= 0;
141 struct sockaddr_dl
*sdl
;
143 if (cmd
== 0x2000ff99) {
144 /* *** this is a temporary hack to get at_send_to_dev() to
145 work with BSD-style sockets instead of the special purpose
146 system calls, ATsocket() and ATioctl().
148 if ((error
= at_ioctl((struct atpcb
*)so
->so_pcb
, cmd
, 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
, cmd
, data
, 0);
156 /* *** processing should be
161 * Find address for this interface, if it exists.
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
];
174 at_state_t
*global_state
= (at_state_t
*)data
;
176 *global_state
= at_state
;
183 at_if_cfg_t
*cfgp
= (at_if_cfg_t
*)data
;
186 if ((at_state
.flags
& AT_ST_STARTED
) &&
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
)))
196 strncpy(cfgp
->ifr_name
, ifID
->ifName
,
197 sizeof(ifID
->ifName
));
199 if (ifID
&& ifID
->ifState
!= LAP_OFFLINE
) {
200 cfgp
->flags
= ifID
->ifFlags
;
201 /* put the IF state into the low order
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
;
219 at_def_zone_t
*defzonep
= (at_def_zone_t
*)data
;
221 /* check for root access */
222 if (error
= suser(p
->p_ucred
, &p
->p_acflag
))
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
)))
235 strncpy(defzonep
->ifr_name
, ifID
->ifName
,
236 sizeof(ifID
->ifName
));
239 /* In routing mode the default zone is only set for the
240 default interface. */
241 if (ROUTING_MODE
&& (ifID
!= ifID_home
))
244 if (ifID
&& ifID
->ifState
!= LAP_OFFLINE
) {
245 if (zonename_equal(&ifID
->ifZoneName
,
246 &defzonep
->zonename
))
249 /* check the zone name */
250 if (MULTIPORT_MODE
) {
252 at_ifnames_t ifs_in_zone
;
254 if (!(zno
= zt_find_zname(&defzonep
->zonename
)))
257 getIfUsage(zno
-1, &ifs_in_zone
);
258 if (!ifs_in_zone
.at_if
[ifID
->ifPort
])
260 ifID
->ifDefZone
= zno
+1;
265 for (i
= 0, zone
= getSPLocalZone(i
);
267 i
++, zone
= getSPLocalZone(i
)) {
268 if (zonename_equal(zone
,
269 &defzonep
->zonename
))
275 ifID
->ifZoneName
= defzonep
->zonename
;
276 (void)regDefaultZone(ifID
);
278 /* AppleTalk zone was changed. Send event with zone info. */
279 atalk_post_msg(ifID
->aa_ifp
, KEV_ATALK_ZONEUPDATED
, 0, &(ifID
->ifZoneName
));
292 at_nvestr_t
*zone
= (at_nvestr_t
*)data
;
294 if (!(at_state
.flags
& AT_ST_STARTED
) || !ifID_home
)
300 return(setLocalZones(zone
, zone
->len
));
305 if (!(at_state
.flags
& AT_ST_STARTED
) || !ifID_home
)
311 return(set_zones((zone_usage_t
*)data
));
316 if (!(at_state
.flags
& AT_ST_STARTED
) || !ifID_home
)
322 if (getRTRLocalZone((zone_usage_t
*)data
))
330 at_nbp_reg_t
*nbpP
= (at_nbp_reg_t
*)data
;
334 if (!(at_state
.flags
& AT_ST_STARTED
) || !ifID_home
)
337 /* multihoming mode */
338 if (MULTIHOME_MODE
) {
339 return(nbp_mh_reg(nbpP
));
342 /* single port mode or router mode */
343 if (nbp_fillin_nve(&nbpP
->name
, &nve
) != 0) {
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
354 int finished
= FALSE
;
356 at_ifnames_t ifs_in_zone
;
357 if (!(zno
= zt_find_zname(&nve
.zone
))) {
360 getIfUsage(zno
-1, &ifs_in_zone
);
362 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
363 if (!ifs_in_zone
.at_if
[ifID
->ifPort
])
364 /* zone doesn't match */
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
;
381 if (nbp_find_nve(&nve
))
382 return(EADDRNOTAVAIL
);
384 /* Normal case; no tuple found for this name, so insert
385 * this tuple in the registry and return ok response.
387 ATDISABLE(nve_lock
, NVE_LOCK
);
388 if ((error
= nbp_new_nve_entry(&nve
, ifID
)) == 0) {
389 nbpP
->addr
.net
= ifID
->ifThisNode
.s_net
;
390 nbpP
->addr
.node
= ifID
->ifThisNode
.s_node
;
391 nbpP
->unique_nbp_id
= nve
.unique_nbp_id
;
393 ATENABLE(nve_lock
, NVE_LOCK
);
401 at_nbp_reg_t
*nbpP
= (at_nbp_reg_t
*)data
;
402 nve_entry_t
*nve_entry
, nve
;
404 if (!(at_state
.flags
& AT_ST_STARTED
))
408 if (nbpP
->unique_nbp_id
) {
409 ATDISABLE(nve_lock
, NVE_LOCK
);
410 TAILQ_FOREACH(nve_entry
, &name_registry
, nve_link
) {
411 if (nve_entry
->unique_nbp_id
== nbpP
->unique_nbp_id
) {
413 nbp_delete_entry(nve_entry
);
414 ATENABLE(nve_lock
, NVE_LOCK
);
418 ATENABLE(nve_lock
, NVE_LOCK
);
419 return(EADDRNOTAVAIL
);
422 /* delete by entity */
423 if (nbp_fillin_nve(&nbpP
->name
, &nve
) != 0) {
428 if (MULTIHOME_MODE
&& DEFAULT_ZONE(&nbpP
->name
.zone
)) {
429 /* if mhome & *, remove nve from all default zones */
430 int found
= FALSE
; /* if any found & deleted */
432 TAILQ_FOREACH(ifID
, &at_ifQueueHd
, aa_link
) {
433 nve
.zone
= ifID
->ifZoneName
;
434 nve
.zone_hash
= nbp_strhash(&nve
.zone
);
435 if ((nve_entry
= nbp_find_nve(&nve
)) == NULL
)
438 ATDISABLE(nve_lock
, NVE_LOCK
);
439 nbp_delete_entry(nve_entry
);
440 ATENABLE(nve_lock
, NVE_LOCK
);
446 return(EADDRNOTAVAIL
);
449 if ((nve_entry
= nbp_find_nve(&nve
)) == NULL
)
450 /* Can't find the tuple we're looking for, send error*/
451 return(EADDRNOTAVAIL
);
453 /* Normal case; tuple found for this name, so delete
454 * the entry from the registry and return ok response.
456 ATDISABLE(nve_lock
, NVE_LOCK
);
457 nbp_delete_entry(nve_entry
);
458 ATENABLE(nve_lock
, NVE_LOCK
);
466 at_router_params_t
*rt
= (at_router_params_t
*)data
;
468 /* check for root access */
469 if (error
= suser(p
->p_ucred
, &p
->p_acflag
))
472 /* when in routing/multihome mode the AIOCSETROUTER IOCTL
474 if (at_state
.flags
& AT_ST_STARTED
)
477 /* Setup the routing & zip table size for the router */
478 if (rt
->rtmp_table_sz
>= RT_MIN
&& rt
->rtmp_table_sz
<= RT_MAX
)
479 RT_maxentry
= rt
->rtmp_table_sz
;
481 RT_maxentry
= RT_DEFAULT
;
483 if (rt
->zone_table_sz
>= ZT_MIN
&& rt
->zone_table_sz
<= ZT_MAX
)
484 ZT_maxentry
= rt
->zone_table_sz
;
486 ZT_maxentry
= ZT_DEFAULT
;
488 if (rt_table_init() == ENOBUFS
)
492 RouterMix
= (int)rt
->router_mix
;
494 RouterMix
= RT_MIX_DEFAULT
;
496 add_ddp_handler(RTMP_SOCKET
, rtmp_router_input
);
499 at_state
.flags
|= AT_ST_MULTIHOME
;
501 at_state
.flags
|= AT_ST_ROUTER
;
504 case AIOCSTARTROUTER
:
506 at_kern_err_t
*keP
= (at_kern_err_t
*)data
;
508 /* check for root access */
509 if (suser(p
->p_ucred
, &p
->p_acflag
))
512 if (!(at_state
.flags
& AT_ST_STARTED
))
515 bzero(keP
, sizeof(at_kern_err_t
));
516 error
= routerStart(keP
);
522 at_router_params_t
*rt
= (at_router_params_t
*)data
;
524 if (!(at_state
.flags
& AT_ST_STARTED
))
527 rt
->multihome
= (MULTIHOME_MODE
)? 1: 0;
528 rt
->rtmp_table_sz
= RT_maxentry
;
529 rt
->zone_table_sz
= ZT_maxentry
;
530 rt
->router_mix
= RouterMix
;
536 int *count_only
= (int *)data
,
539 /* check for root access */
540 if (error
= suser(p
->p_ucred
, &p
->p_acflag
))
543 ret
= ddp_shutdown(*count_only
);
545 if (*count_only
!= 0)
554 /* AppleTalk was successfully shut down. Send event. */
555 atalk_post_msg(0, KEV_ATALK_DISABLED
, 0, 0);
566 /* check for root access */
567 if (error
= suser(p
->p_ucred
, &p
->p_acflag
))
574 at_state
.flags
|= AT_ST_STARTING
;
578 /* *** find an empty entry *** */
579 ifID
= &at_interfaces
[xpatcnt
];
580 bzero((caddr_t
)ifID
, sizeof(at_ifaddr_t
));
581 strncpy(ifID
->ifName
, ifr
->ifr_name
, sizeof(ifID
->ifName
));
585 TAILQ_FOREACH(ifa
, &ifp
->if_addrhead
, ifa_link
)
586 if ((sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
) &&
587 (sdl
->sdl_family
== AF_LINK
)) {
588 bcopy(LLADDR(sdl
), ifID
->xaddr
, sizeof(ifID
->xaddr
));
589 #ifdef APPLETALK_DEBUG
590 kprintf("SIOCSIFADDR: local enet address is %x.%x.%x.%x.%x.%x\n",
591 ifID
->xaddr
[0], ifID
->xaddr
[1],
592 ifID
->xaddr
[2], ifID
->xaddr
[3],
593 ifID
->xaddr
[4], ifID
->xaddr
[5]);
598 /* attach the AppleTalk address to the ifnet structure */
600 ifa
->ifa_addr
= (struct sockaddr
*)&ifID
->ifNodeAddress
;
601 ifID
->ifNodeAddress
.sat_len
= sizeof(struct sockaddr_at
);
602 ifID
->ifNodeAddress
.sat_family
= AF_APPLETALK
;
603 /* the address itself will be filled in when ifThisNode
606 TAILQ_INSERT_TAIL(&ifp
->if_addrhead
, ifa
, ifa_link
);
609 switch (ifp
->if_type
) {
611 ether_attach_at(ifp
, &ifID
->at_dl_tag
,
614 ifID
->cable_multicast_addr
= etalk_multicast_addr
;
619 ifID
->cable_multicast_addr
= etalk_multicast_addr
;
620 ddp_bit_reverse(&ifID
->cable_multicast_addr
);
623 case IFT_ISO88025
: /* token ring */
624 ifID
->cable_multicast_addr
= ttalk_multicast_addr
;
625 ddp_bit_reverse(&ifID
->cable_multicast_addr
);
635 /* complete the initialization started in SIOCSIFADDR */
638 at_if_cfg_t
*cfgp
= (at_if_cfg_t
*)data
;
640 if (!(at_state
.flags
& AT_ST_STARTING
))
643 if (!(ifID
= find_ifID(cfgp
->ifr_name
)))
646 return(lap_online(ifID
, cfgp
));
651 /* *** this can't be added until AT can handle dynamic addition and
652 deletion of interfaces *** */
654 /* check for root access */
655 if (error
= suser(p
->p_ucred
, &p
->p_acflag
))
666 struct atpcb
*at_pcb
, *clonedat_pcb
;
667 int cloned_fd
= *(int *)data
;
669 s
= splnet(); /* XXX */
670 at_pcb
= sotoatpcb(so
);
672 /* let's make sure it's either -1 or a valid file descriptor */
673 if (cloned_fd
!= -1) {
674 struct socket
*cloned_so
;
675 struct file
*cloned_fp
;
676 error
= getsock(p
->p_fd
, cloned_fd
, &cloned_fp
);
681 cloned_so
= (struct socket
*)cloned_fp
->f_data
;
682 clonedat_pcb
= sotoatpcb(cloned_so
);
687 if (clonedat_pcb
== NULL
) {
688 at_pcb
->ddp_flags
|= DDPFLG_STRIPHDR
;
690 at_pcb
->ddp_flags
= clonedat_pcb
->ddp_flags
;
697 if (ifp
== 0 || ifp
->if_ioctl
== 0)
699 return dlil_ioctl(0, ifp
, cmd
, (caddr_t
) data
);
705 /* From dlil_post_msg() */
706 void atalk_post_msg(struct ifnet
*ifp
, u_long event_code
, struct at_addr
*address
, at_nvestr_t
*zone
)
708 struct kev_atalk_data at_event_data
;
709 struct kev_msg ev_msg
;
711 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
712 ev_msg
.kev_class
= KEV_NETWORK_CLASS
;
713 ev_msg
.kev_subclass
= KEV_ATALK_SUBCLASS
;
714 ev_msg
.event_code
= event_code
;
716 bzero(&at_event_data
, sizeof(struct kev_atalk_data
));
719 strncpy(&at_event_data
.link_data
.if_name
[0], ifp
->if_name
, IFNAMSIZ
);
720 at_event_data
.link_data
.if_family
= ifp
->if_family
;
721 at_event_data
.link_data
.if_unit
= (unsigned long) ifp
->if_unit
;
725 at_event_data
.node_data
.address
= *address
;
727 else if (zone
!= 0) {
728 at_event_data
.node_data
.zone
= *zone
;
731 ev_msg
.dv
[0].data_length
= sizeof(struct kev_atalk_data
);
732 ev_msg
.dv
[0].data_ptr
= &at_event_data
;
733 ev_msg
.dv
[1].data_length
= 0;
735 kev_post_msg(&ev_msg
);