af_atalk.c revision 1.7 1 /* $NetBSD: af_atalk.c,v 1.7 2008/05/06 04:33:42 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.7 2008/05/06 04:33:42 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 prop_number_t num;
100
101 num = (prop_number_t)prop_dictionary_get(env, "phase");
102 if (num == NULL) {
103 errno = ENOENT;
104 return -1;
105 }
106 at_nr.nr_phase = (uint8_t)prop_number_integer_value(num);
107 return 0;
108 }
109
110 void
111 checkatrange(struct sockaddr *sa)
112 {
113 struct sockaddr_at *sat = (struct sockaddr_at *) sa;
114
115 if (at_nr.nr_phase == 0)
116 at_nr.nr_phase = 2; /* Default phase 2 */
117 if (at_nr.nr_firstnet == 0)
118 at_nr.nr_firstnet = /* Default range of one */
119 at_nr.nr_lastnet = sat->sat_addr.s_net;
120 printf("\tatalk %d.%d range %d-%d phase %d\n",
121 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
122 ntohs(at_nr.nr_firstnet), ntohs(at_nr.nr_lastnet), at_nr.nr_phase);
123 if (ntohs(at_nr.nr_firstnet) > ntohs(sat->sat_addr.s_net) ||
124 ntohs(at_nr.nr_lastnet) < ntohs(sat->sat_addr.s_net))
125 errx(EXIT_FAILURE, "AppleTalk address is not in range");
126 *((struct netrange *)&sat->sat_zero) = at_nr;
127 }
128
129 void
130 at_status(prop_dictionary_t env, prop_dictionary_t oenv, int force)
131 {
132 struct sockaddr_at *sat, null_sat;
133 struct netrange *nr;
134 struct ifreq ifr;
135 int af, s;
136 const char *ifname;
137 unsigned short flags;
138
139 if ((s = getsock(AF_APPLETALK)) == -1) {
140 if (errno == EAFNOSUPPORT)
141 return;
142 err(EXIT_FAILURE, "socket");
143 }
144 if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
145 err(EXIT_FAILURE, "%s: getifinfo", __func__);
146
147 if ((af = getaf(env)) == -1)
148 err(EXIT_FAILURE, "%s: getaf", __func__);
149
150 memset(&ifr, 0, sizeof(ifr));
151 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
152 ifr.ifr_addr.sa_family = af;
153 if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
154 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
155 if (!force)
156 return;
157 (void) memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
158 } else
159 warn("SIOCGIFADDR");
160 }
161 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
162 sat = (struct sockaddr_at *)&ifr.ifr_addr;
163
164 (void) memset(&null_sat, 0, sizeof(null_sat));
165
166 nr = (struct netrange *) &sat->sat_zero;
167 printf("\tatalk %d.%d range %d-%d phase %d",
168 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node,
169 ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet), nr->nr_phase);
170
171 if (flags & IFF_POINTOPOINT) {
172 if (ioctl(s, SIOCGIFDSTADDR, &ifr) == -1) {
173 if (errno == EADDRNOTAVAIL)
174 memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
175 else
176 warn("SIOCGIFDSTADDR");
177 }
178 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
179 sat = (struct sockaddr_at *)&ifr.ifr_dstaddr;
180 if (!sat)
181 sat = &null_sat;
182 printf("--> %d.%d",
183 ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node);
184 }
185 if (flags & IFF_BROADCAST) {
186 /* note RTAX_BRD overlap with IFF_POINTOPOINT */
187 sat = (struct sockaddr_at *)&ifr.ifr_broadaddr;
188 if (sat)
189 printf(" broadcast %d.%d", ntohs(sat->sat_addr.s_net),
190 sat->sat_addr.s_node);
191 }
192 printf("\n");
193 }
194
195 #endif /* ! INET_ONLY */
196