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