af_atalk.c revision 1.10 1 /* $NetBSD: af_atalk.c,v 1.10 2008/05/06 21:16:52 dyoung Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #ifndef INET_ONLY
33
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: af_atalk.c,v 1.10 2008/05/06 21:16:52 dyoung Exp $");
37 #endif /* not lint */
38
39 #include <sys/param.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42
43 #include <net/if.h>
44
45 #include <netatalk/at.h>
46
47 #include <err.h>
48 #include <errno.h>
49 #include <string.h>
50 #include <stdlib.h>
51 #include <stdio.h>
52 #include <util.h>
53
54 #include "env.h"
55 #include "parse.h"
56 #include "extern.h"
57 #include "af_atalk.h"
58
59 struct ifaliasreq at_addreq __attribute__((aligned(4)));
60 static struct netrange at_nr; /* AppleTalk net range */
61
62 struct pinteger phase = PINTEGER_INITIALIZER1(&phase, "phase",
63 1, 2, 10, setatphase, "phase", &command_root.pb_parser);
64
65 struct pstr parse_range = PSTR_INITIALIZER(&range, "range", setatrange, "range",
66 &command_root.pb_parser);
67
68 void
69 at_getaddr(const struct paddr_prefix *pfx, int which)
70 {
71 if (which == MASK)
72 errx(EXIT_FAILURE, "AppleTalk does not use netmasks");
73
74 memcpy(&at_addreq.ifra_addr, &pfx->pfx_addr,
75 MIN(sizeof(at_addreq.ifra_addr), pfx->pfx_addr.sa_len));
76 }
77
78 int
79 setatrange(prop_dictionary_t env, prop_dictionary_t xenv)
80 {
81 char range[24];
82 u_short first = 123, last = 123;
83
84 if (getargstr(env, "range", range, sizeof(range)) == -1)
85 return -1;
86
87 if (sscanf(range, "%hu-%hu", &first, &last) != 2 ||
88 first == 0 || last == 0 || first > last)
89 errx(EXIT_FAILURE, "%s: illegal net range: %u-%u", range,
90 first, last);
91 at_nr.nr_firstnet = htons(first);
92 at_nr.nr_lastnet = htons(last);
93 return 0;
94 }
95
96 int
97 setatphase(prop_dictionary_t env, prop_dictionary_t xenv)
98 {
99 if (!prop_dictionary_get_uint8(env, "phase", &at_nr.nr_phase)) {
100 errno = ENOENT;
101 return -1;
102 }
103 return 0;
104 }
105
106 void
107 checkatrange(struct sockaddr *sa)
108 {
109 struct sockaddr_at *sat = (struct sockaddr_at *) sa;
110
111 if (at_nr.nr_phase == 0)
112 at_nr.nr_phase = 2; /* Default phase 2 */
113 if (at_nr.nr_firstnet == 0)
114 at_nr.nr_firstnet = /* Default range of one */
115 at_nr.nr_lastnet = sat->sat_addr.s_net;
116 printf("\tatalk %d.%d range %d-%d phase %d\n",
117 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
118 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
119 if (ntohs(at_nr.nr_firstnet) > ntohs(sat->sat_addr.s_net) ||
120 ntohs(at_nr.nr_lastnet) < ntohs(sat->sat_addr.s_net))
121 errx(EXIT_FAILURE, "AppleTalk address is not in range");
122 *((struct netrange *)&sat->sat_zero) = at_nr;
123 }
124
125 void
126 at_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
127 {
128 struct sockaddr_at *sat, null_sat;
129 struct netrange *nr;
130 struct ifreq ifr;
131 int af, s;
132 const char *ifname;
133 unsigned short flags;
134
135 if ((s = getsock(AF_APPLETALK)) == -1) {
136 if (errno == EAFNOSUPPORT)
137 return;
138 err(EXIT_FAILURE, "getsock");
139 }
140 if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
141 err(EXIT_FAILURE, "%s: getifinfo", __func__);
142
143 if ((af = getaf(env)) == -1)
144 af = AF_APPLETALK;
145 else if (af != AF_APPLETALK)
146 return;
147
148 memset(&ifr, 0, sizeof(ifr));
149 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
150 ifr.ifr_addr.sa_family = af;
151 if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
152 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
153 if (!force)
154 return;
155 (void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
156 } else
157 warn("SIOCGIFADDR");
158 }
159 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
160 sat = (struct sockaddr_at *)&ifr.ifr_addr;
161
162 (void) memset(&null_sat, 0, sizeof(null_sat));
163
164 nr = (struct netrange *) &sat->sat_zero;
165 printf("\tatalk %d.%d range %d-%d phase %d",
166 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
167 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
168
169 if (flags & IFF_POINTOPOINT) {
170 if (ioctl(s, SIOCGIFDSTADDR, &ifr) == -1) {
171 if (errno == EADDRNOTAVAIL)
172 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
173 else
174 warn("SIOCGIFDSTADDR");
175 }
176 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
177 sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
178 if (!sat)
179 sat = &null_sat;
180 printf("--> %d.%d",
181 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
182 }
183 if (flags & IFF_BROADCAST) {
184 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
185 sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
186 if (sat)
187 printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
188 sat->sat_addr.s_node);
189 }
190 printf("\n");
191 }
192
193 #endif /* ! INET_ONLY */
194