53928662f2767022ba911f8ecb820b590fd11400
[dragonfly.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  * $FreeBSD: src/sbin/ifconfig/af_atalk.c,v 1.1 2004/12/08 19:18:07 sam Exp $
30  */
31
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <net/if.h>
36 #include <net/route.h>          /* for RTX_IFA */
37
38 #include <netatalk/at.h>
39
40 #include <err.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45
46 #include <arpa/inet.h>
47
48 #include "ifconfig.h"
49
50 static struct netrange at_nr;           /* AppleTalk net range */
51 static struct ifaliasreq at_addreq;
52
53 /* XXX  FIXME -- should use strtoul for better parsing. */
54 static void
55 setatrange(const char *range, int dummy __unused, int s, 
56     const struct afswtch *afp)
57 {
58         u_int   first = 123, last = 123;
59
60         if (sscanf(range, "%u-%u", &first, &last) != 2
61             || first == 0 || first > 0xffff
62             || last == 0 || last > 0xffff || first > last)
63                 errx(1, "%s: illegal net range: %u-%u", range, first, last);
64         at_nr.nr_firstnet = htons(first);
65         at_nr.nr_lastnet = htons(last);
66 }
67
68 static void
69 setatphase(const char *phase, int dummy __unused, int s, 
70     const struct afswtch *afp)
71 {
72         if (!strcmp(phase, "1"))
73                 at_nr.nr_phase = 1;
74         else if (!strcmp(phase, "2"))
75                 at_nr.nr_phase = 2;
76         else
77                 errx(1, "%s: illegal phase", phase);
78 }
79
80 static void
81 at_status(int s __unused, const struct rt_addrinfo * info)
82 {
83         struct sockaddr_at *sat, null_sat;
84         struct netrange *nr;
85
86         memset(&null_sat, 0, sizeof(null_sat));
87
88         sat = (struct sockaddr_at *)info->rti_info[RTAX_IFA];
89         if (sat == NULL)
90                 return;
91         nr = &sat->sat_range.r_netrange;
92         printf("\tatalk %d.%d range %d-%d phase %d",
93                 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
94                 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
95         if (flags & IFF_POINTOPOINT) {
96                 /* note RTAX_BRD overlap with IFF_BROADCAST */
97                 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
98                 if (!sat)
99                         sat = &null_sat;
100                 printf("--> %d.%d",
101                         ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
102         }
103         if (flags & IFF_BROADCAST) {
104                 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
105                 sat = (struct sockaddr_at *)info->rti_info[RTAX_BRD];
106                 if (sat)
107                         printf(" broadcast %d.%d",
108                                 ntohs(sat->sat_addr.s_net),
109                                 sat->sat_addr.s_node);
110         }
111
112         putchar('\n');
113 }
114
115 static void
116 at_getaddr(const char *addr, int which)
117 {
118         struct sockaddr_at *sat = (struct sockaddr_at *) &at_addreq.ifra_addr;
119         u_int net, node;
120
121         sat->sat_family = AF_APPLETALK;
122         sat->sat_len = sizeof(*sat);
123         if (which == MASK)
124                 errx(1, "AppleTalk does not use netmasks");
125         if (sscanf(addr, "%u.%u", &net, &node) != 2
126             || net > 0xffff || node > 0xfe)
127                 errx(1, "%s: illegal address", addr);
128         sat->sat_addr.s_net = htons(net);
129         sat->sat_addr.s_node = node;
130 }
131
132 static void
133 at_postproc(int s, const struct afswtch *afp)
134 {
135         struct sockaddr_at *sat = (struct sockaddr_at *) &at_addreq.ifra_addr;
136
137         if (at_nr.nr_phase == 0)
138                 at_nr.nr_phase = 2;     /* Default phase 2 */
139         if (at_nr.nr_firstnet == 0)
140                 at_nr.nr_firstnet =     /* Default range of one */
141                 at_nr.nr_lastnet = sat->sat_addr.s_net;
142         printf("\tatalk %d.%d range %d-%d phase %d\n",
143                 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
144                 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet),
145                 at_nr.nr_phase);
146         if ((u_short) ntohs(at_nr.nr_firstnet) >
147                         (u_short) ntohs(sat->sat_addr.s_net)
148                     || (u_short) ntohs(at_nr.nr_lastnet) <
149                         (u_short) ntohs(sat->sat_addr.s_net))
150                 errx(1, "AppleTalk address is not in range");
151         sat->sat_range.r_netrange = at_nr;
152 }
153
154 static struct cmd atalk_cmds[] = {
155         DEF_CMD_ARG("range",    setatrange),
156         DEF_CMD_ARG("phase",    setatphase),
157 };
158
159 static struct afswtch af_atalk = {
160         .af_name        = "atalk",
161         .af_af          = AF_APPLETALK,
162         .af_status      = at_status,
163         .af_getaddr     = at_getaddr,
164         .af_postproc    = at_postproc,
165         .af_difaddr     = SIOCDIFADDR,
166         .af_aifaddr     = SIOCAIFADDR,
167         .af_ridreq      = &at_addreq,
168         .af_addreq      = &at_addreq,
169 };
170
171 static __constructor(101) void
172 atalk_ctor(void)
173 {
174 #define N(a)    (sizeof(a) / sizeof(a[0]))
175         int i;
176
177         for (i = 0; i < N(atalk_cmds);  i++)
178                 cmd_register(&atalk_cmds[i]);
179         af_register(&af_atalk);
180 #undef N
181 }