Style(9) cleanup.
[dragonfly.git] / sys / netproto / ns / ns.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1984, 1985, 1986, 1987, 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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)ns.c 8.2 (Berkeley) 11/15/93
34 * $FreeBSD: src/sys/netns/ns.c,v 1.9 1999/08/28 00:49:47 peter Exp $
57106eda 35 * $DragonFly: src/sys/netproto/ns/ns.c,v 1.6 2004/02/16 20:37:20 dillon Exp $
984263bc
MD
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
7b51e07b 40#include <sys/malloc.h>
984263bc
MD
41#include <sys/mbuf.h>
42#include <sys/ioctl.h>
43#include <sys/protosw.h>
44#include <sys/errno.h>
45#include <sys/socket.h>
46#include <sys/socketvar.h>
47
48#include <net/if.h>
49#include <net/route.h>
50
1f2de5d4
MD
51#include "ns.h"
52#include "ns_if.h"
984263bc 53
7b51e07b
DR
54#include "opt_ns.h"
55
984263bc
MD
56#ifdef NS
57
58struct ns_ifaddr *ns_ifaddr;
59int ns_interfaces;
57106eda 60
984263bc
MD
61extern struct sockaddr_ns ns_netmask, ns_hostmask;
62
63/*
64 * Generic internet control operations (ioctl's).
65 */
66/* ARGSUSED */
7b51e07b 67int
984263bc
MD
68ns_control(so, cmd, data, ifp)
69 struct socket *so;
70 int cmd;
71 caddr_t data;
34ee9825 72 struct ifnet *ifp;
984263bc 73{
34ee9825
RG
74 struct ifreq *ifr = (struct ifreq *)data;
75 struct ns_aliasreq *ifra = (struct ns_aliasreq *)data;
76 struct ns_ifaddr *ia;
7b51e07b 77 struct ifaddr *ifa = NULL; /* XXX used ininitialized ?*/
984263bc 78 struct ns_ifaddr *oia;
7b51e07b
DR
79 int dstIsNew, hostIsNew;
80 int error = 0; /* initalize because of scoping */
984263bc
MD
81
82 /*
83 * Find address for this interface, if it exists.
84 */
85 if (ifp == 0)
86 return (EADDRNOTAVAIL);
87 for (ia = ns_ifaddr; ia; ia = ia->ia_next)
88 if (ia->ia_ifp == ifp)
89 break;
90
91 switch (cmd) {
92
93 case SIOCGIFADDR:
94 if (ia == (struct ns_ifaddr *)0)
95 return (EADDRNOTAVAIL);
96 *(struct sockaddr_ns *)&ifr->ifr_addr = ia->ia_addr;
97 return (0);
98
99
100 case SIOCGIFBRDADDR:
101 if (ia == (struct ns_ifaddr *)0)
102 return (EADDRNOTAVAIL);
103 if ((ifp->if_flags & IFF_BROADCAST) == 0)
104 return (EINVAL);
105 *(struct sockaddr_ns *)&ifr->ifr_dstaddr = ia->ia_broadaddr;
106 return (0);
107
108 case SIOCGIFDSTADDR:
109 if (ia == (struct ns_ifaddr *)0)
110 return (EADDRNOTAVAIL);
111 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
112 return (EINVAL);
113 *(struct sockaddr_ns *)&ifr->ifr_dstaddr = ia->ia_dstaddr;
114 return (0);
115 }
116
7b51e07b 117#ifdef NS_PRIV_SOCKETS
984263bc
MD
118 if ((so->so_state & SS_PRIV) == 0)
119 return (EPERM);
7b51e07b 120#endif
984263bc
MD
121
122 switch (cmd) {
123 case SIOCAIFADDR:
124 case SIOCDIFADDR:
125 if (ifra->ifra_addr.sns_family == AF_NS)
126 for (oia = ia; ia; ia = ia->ia_next) {
127 if (ia->ia_ifp == ifp &&
128 ns_neteq(ia->ia_addr.sns_addr,
129 ifra->ifra_addr.sns_addr))
130 break;
131 }
132 if (cmd == SIOCDIFADDR && ia == 0)
133 return (EADDRNOTAVAIL);
134 /* FALLTHROUGH */
135
136 case SIOCSIFADDR:
137 case SIOCSIFDSTADDR:
138 if (ia == (struct ns_ifaddr *)0) {
139 oia = (struct ns_ifaddr *)
140 malloc(sizeof *ia, M_IFADDR, M_WAITOK);
141 if (oia == (struct ns_ifaddr *)NULL)
142 return (ENOBUFS);
143 bzero((caddr_t)oia, sizeof(*oia));
7b51e07b 144 if ((ia = ns_ifaddr) != NULL) {
984263bc
MD
145 for ( ; ia->ia_next; ia = ia->ia_next)
146 ;
147 ia->ia_next = oia;
148 } else
149 ns_ifaddr = oia;
150 ia = oia;
7b51e07b
DR
151
152 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
984263bc
MD
153 ia->ia_ifp = ifp;
154 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
155
156 ia->ia_ifa.ifa_netmask =
157 (struct sockaddr *)&ns_netmask;
158
159 ia->ia_ifa.ifa_dstaddr =
160 (struct sockaddr *)&ia->ia_dstaddr;
161 if (ifp->if_flags & IFF_BROADCAST) {
162 ia->ia_broadaddr.sns_family = AF_NS;
163 ia->ia_broadaddr.sns_len = sizeof(ia->ia_addr);
164 ia->ia_broadaddr.sns_addr.x_host = ns_broadhost;
165 }
166 ns_interfaces++;
167 }
168 }
169
170 switch (cmd) {
984263bc
MD
171 case SIOCSIFDSTADDR:
172 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
173 return (EINVAL);
174 if (ia->ia_flags & IFA_ROUTE) {
175 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
176 ia->ia_flags &= ~IFA_ROUTE;
177 }
178 if (ifp->if_ioctl) {
7b51e07b
DR
179 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
180 (caddr_t)ia);
984263bc
MD
181 if (error)
182 return (error);
183 }
184 *(struct sockaddr *)&ia->ia_dstaddr = ifr->ifr_dstaddr;
185 return (0);
186
187 case SIOCSIFADDR:
7b51e07b 188 return (ns_ifinit(ifp, (struct ns_ifaddr *)ia,
984263bc
MD
189 (struct sockaddr_ns *)&ifr->ifr_addr, 1));
190
191 case SIOCDIFADDR:
7b51e07b
DR
192 ns_ifscrub(ifp, (struct ns_ifaddr *)ia);
193 /* XXX not on list */
984263bc 194 oia = ia;
7b51e07b
DR
195 TAILQ_REMOVE(&ifp->if_addrhead, (struct ifaddr *)ia, ifa_link);
196 if (oia == (ia = ns_ifaddr)) {
197 ns_ifaddr = ia->ia_next;
198 } else {
199 while (ia->ia_next && (ia->ia_next != oia)) {
200 ia = ia->ia_next;
201 }
202 if (ia->ia_next)
203 ia->ia_next = oia->ia_next;
204 else
205 printf("Didn't unlink nsifadr from list\n");
206 }
984263bc
MD
207 IFAFREE((&oia->ia_ifa));
208 if (0 == --ns_interfaces) {
209 /*
210 * We reset to virginity and start all over again
211 */
212 ns_thishost = ns_zerohost;
213 }
214 return (0);
215
216 case SIOCAIFADDR:
217 dstIsNew = 0; hostIsNew = 1;
218 if (ia->ia_addr.sns_family == AF_NS) {
219 if (ifra->ifra_addr.sns_len == 0) {
220 ifra->ifra_addr = ia->ia_addr;
221 hostIsNew = 0;
222 } else if (ns_neteq(ifra->ifra_addr.sns_addr,
223 ia->ia_addr.sns_addr))
224 hostIsNew = 0;
225 }
226 if ((ifp->if_flags & IFF_POINTOPOINT) &&
227 (ifra->ifra_dstaddr.sns_family == AF_NS)) {
228 if (hostIsNew == 0)
7b51e07b 229 ns_ifscrub(ifp, (struct ns_ifaddr *)ia);
984263bc
MD
230 ia->ia_dstaddr = ifra->ifra_dstaddr;
231 dstIsNew = 1;
232 }
233 if (ifra->ifra_addr.sns_family == AF_NS &&
234 (hostIsNew || dstIsNew))
7b51e07b
DR
235 error = ns_ifinit(ifp, (struct ns_ifra_addr *)ia,
236 &ifra->ifra_addr, 0);
984263bc
MD
237 return (error);
238
239 default:
240 if (ifp->if_ioctl == 0)
241 return (EOPNOTSUPP);
242 return ((*ifp->if_ioctl)(ifp, cmd, data));
243 }
244}
245
246/*
247* Delete any previous route for an old address.
248*/
7b51e07b 249void
984263bc 250ns_ifscrub(ifp, ia)
34ee9825
RG
251 struct ifnet *ifp;
252 struct ns_ifaddr *ia;
984263bc
MD
253{
254 if (ia->ia_flags & IFA_ROUTE) {
255 if (ifp->if_flags & IFF_POINTOPOINT) {
256 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
257 } else
258 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
259 ia->ia_flags &= ~IFA_ROUTE;
260 }
261}
262/*
263 * Initialize an interface's internet address
264 * and routing table entry.
265 */
7b51e07b 266int
984263bc 267ns_ifinit(ifp, ia, sns, scrub)
34ee9825
RG
268 struct ifnet *ifp;
269 struct ns_ifaddr *ia;
270 struct sockaddr_ns *sns;
7b51e07b 271 int scrub;
984263bc
MD
272{
273 struct sockaddr_ns oldaddr;
34ee9825 274 union ns_host *h = &ia->ia_addr.sns_addr.x_host;
984263bc
MD
275 int s = splimp(), error;
276
277 /*
278 * Set up new addresses.
279 */
280 oldaddr = ia->ia_addr;
281 ia->ia_addr = *sns;
282 /*
283 * The convention we shall adopt for naming is that
284 * a supplied address of zero means that "we don't care".
285 * if there is a single interface, use the address of that
286 * interface as our 6 byte host address.
287 * if there are multiple interfaces, use any address already
288 * used.
289 *
290 * Give the interface a chance to initialize
291 * if this is its first address,
292 * and to validate the address if necessary.
293 */
294 if (ns_hosteqnh(ns_thishost, ns_zerohost)) {
295 if (ifp->if_ioctl &&
7b51e07b
DR
296 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR,
297 (caddr_t)ia))) {
984263bc
MD
298 ia->ia_addr = oldaddr;
299 splx(s);
300 return (error);
301 }
302 ns_thishost = *h;
303 } else if (ns_hosteqnh(sns->sns_addr.x_host, ns_zerohost)
304 || ns_hosteqnh(sns->sns_addr.x_host, ns_thishost)) {
305 *h = ns_thishost;
306 if (ifp->if_ioctl &&
7b51e07b
DR
307 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR,
308 (caddr_t)ia))) {
984263bc
MD
309 ia->ia_addr = oldaddr;
310 splx(s);
311 return (error);
312 }
313 if (!ns_hosteqnh(ns_thishost,*h)) {
314 ia->ia_addr = oldaddr;
315 splx(s);
316 return (EINVAL);
317 }
318 } else {
319 ia->ia_addr = oldaddr;
320 splx(s);
321 return (EINVAL);
322 }
323 ia->ia_ifa.ifa_metric = ifp->if_metric;
324 /*
325 * Add route for the network.
326 */
327 if (scrub) {
328 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
329 ns_ifscrub(ifp, ia);
330 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
331 }
332 if (ifp->if_flags & IFF_POINTOPOINT)
333 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
334 else {
335 ia->ia_broadaddr.sns_addr.x_net = ia->ia_addr.sns_addr.x_net;
336 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP);
337 }
338 ia->ia_flags |= IFA_ROUTE;
339 return (0);
340}
341
342/*
343 * Return address info for specified internet network.
344 */
345struct ns_ifaddr *
346ns_iaonnetof(dst)
34ee9825 347 struct ns_addr *dst;
984263bc 348{
34ee9825
RG
349 struct ns_ifaddr *ia;
350 struct ns_addr *compare;
351 struct ifnet *ifp;
984263bc
MD
352 struct ns_ifaddr *ia_maybe = 0;
353 union ns_net net = dst->x_net;
354
355 for (ia = ns_ifaddr; ia; ia = ia->ia_next) {
7b51e07b 356 if ((ifp = ia->ia_ifp) != NULL) {
984263bc
MD
357 if (ifp->if_flags & IFF_POINTOPOINT) {
358 compare = &satons_addr(ia->ia_dstaddr);
359 if (ns_hosteq(*dst, *compare))
360 return (ia);
361 if (ns_neteqnn(net, ia->ia_addr.sns_addr.x_net))
362 ia_maybe = ia;
363 } else {
364 if (ns_neteqnn(net, ia->ia_addr.sns_addr.x_net))
365 return (ia);
366 }
367 }
368 }
369 return (ia_maybe);
370}
371#endif