]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/ifconfig/af_atalk.c
Copy xen includes in to RELENG_6 branch
[FreeBSD/FreeBSD.git] / sbin / ifconfig / af_atalk.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #ifndef lint
31 static const char rcsid[] =
32   "$FreeBSD$";
33 #endif /* not lint */
34
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <net/if.h>
39 #include <net/route.h>          /* for RTX_IFA */
40
41 #include <netatalk/at.h>
42
43 #include <err.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 #include <arpa/inet.h>
50
51 #include "ifconfig.h"
52
53 static struct netrange at_nr;           /* AppleTalk net range */
54 static struct ifaliasreq at_addreq;
55
56 /* XXX  FIXME -- should use strtoul for better parsing. */
57 static void
58 setatrange(const char *range, int dummy __unused, int s, 
59     const struct afswtch *afp)
60 {
61         u_int   first = 123, last = 123;
62
63         if (sscanf(range, "%u-%u", &first, &last) != 2
64             || first == 0 || first > 0xffff
65             || last == 0 || last > 0xffff || first > last)
66                 errx(1, "%s: illegal net range: %u-%u", range, first, last);
67         at_nr.nr_firstnet = htons(first);
68         at_nr.nr_lastnet = htons(last);
69 }
70
71 static void
72 setatphase(const char *phase, int dummy __unused, int s, 
73     const struct afswtch *afp)
74 {
75         if (!strcmp(phase, "1"))
76                 at_nr.nr_phase = 1;
77         else if (!strcmp(phase, "2"))
78                 at_nr.nr_phase = 2;
79         else
80                 errx(1, "%s: illegal phase", phase);
81 }
82
83 static void
84 at_status(int s __unused, const struct rt_addrinfo * info)
85 {
86         struct sockaddr_at *sat, null_sat;
87         struct netrange *nr;
88
89         memset(&null_sat, 0, sizeof(null_sat));
90
91         sat = (struct sockaddr_at *)info->rti_info[RTAX_IFA];
92         if (sat == NULL)
93                 return;
94         nr = &sat->sat_range.r_netrange;
95         printf("\tatalk %d.%d range %d-%d phase %d",
96                 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
97                 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
98         if (flags & IFF_POINTOPOINT) {
99                 /* note RTAX_BRD overlap with IFF_BROADCAST */
100                 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
101                 if (!sat)
102                         sat = &null_sat;
103                 printf("--> %d.%d",
104                         ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
105         }
106         if (flags & IFF_BROADCAST) {
107                 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
108                 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
109                 if (sat)
110                         printf(" broadcast %d.%d",
111                                 ntohs(sat->sat_addr.s_net),
112                                 sat->sat_addr.s_node);
113         }
114
115         putchar('\n');
116 }
117
118 static void
119 at_getaddr(const char *addr, int which)
120 {
121         struct sockaddr_at *sat = (struct sockaddr_at *) &at_addreq.ifra_addr;
122         u_int net, node;
123
124         sat->sat_family = AF_APPLETALK;
125         sat->sat_len = sizeof(*sat);
126         if (which == MASK)
127                 errx(1, "AppleTalk does not use netmasks");
128         if (sscanf(addr, "%u.%u", &net, &node) != 2
129             || net > 0xffff || node > 0xfe)
130                 errx(1, "%s: illegal address", addr);
131         sat->sat_addr.s_net = htons(net);
132         sat->sat_addr.s_node = node;
133 }
134
135 static void
136 at_postproc(int s, const struct afswtch *afp)
137 {
138         struct sockaddr_at *sat = (struct sockaddr_at *) &at_addreq.ifra_addr;
139
140         if (at_nr.nr_phase == 0)
141                 at_nr.nr_phase = 2;     /* Default phase 2 */
142         if (at_nr.nr_firstnet == 0)
143                 at_nr.nr_firstnet =     /* Default range of one */
144                 at_nr.nr_lastnet = sat->sat_addr.s_net;
145         printf("\tatalk %d.%d range %d-%d phase %d\n",
146                 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
147                 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet),
148                 at_nr.nr_phase);
149         if ((u_short) ntohs(at_nr.nr_firstnet) >
150                         (u_short) ntohs(sat->sat_addr.s_net)
151                     || (u_short) ntohs(at_nr.nr_lastnet) <
152                         (u_short) ntohs(sat->sat_addr.s_net))
153                 errx(1, "AppleTalk address is not in range");
154         sat->sat_range.r_netrange = at_nr;
155 }
156
157 static struct cmd atalk_cmds[] = {
158         DEF_CMD_ARG("range",    setatrange),
159         DEF_CMD_ARG("phase",    setatphase),
160 };
161
162 static struct afswtch af_atalk = {
163         .af_name        = "atalk",
164         .af_af          = AF_APPLETALK,
165         .af_status      = at_status,
166         .af_getaddr     = at_getaddr,
167         .af_postproc    = at_postproc,
168         .af_difaddr     = SIOCDIFADDR,
169         .af_aifaddr     = SIOCAIFADDR,
170         .af_ridreq      = &at_addreq,
171         .af_addreq      = &at_addreq,
172 };
173
174 static __constructor void
175 atalk_ctor(void)
176 {
177 #define N(a)    (sizeof(a) / sizeof(a[0]))
178         int i;
179
180         for (i = 0; i < N(atalk_cmds);  i++)
181                 cmd_register(&atalk_cmds[i]);
182         af_register(&af_atalk);
183 #undef N
184 }