Add journaling restart support, required to produce a robust journaling
[dragonfly.git] / sys / netproto / ns / ns_ip.c
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_ip.c     8.1 (Berkeley) 6/10/93
34  * $FreeBSD: src/sys/netns/ns_ip.c,v 1.9 1999/08/28 00:49:50 peter Exp $
35  * $DragonFly: src/sys/netproto/ns/ns_ip.c,v 1.12 2005/06/09 02:03:39 hsu Exp $
36  */
37
38 /*
39  * Software interface driver for encapsulating ns in ip.
40  */
41
42 #ifdef NSIP
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/errno.h>
50 #include <sys/ioctl.h>
51 #include <sys/protosw.h>
52
53 #include <net/if.h>
54 #include <net/netisr.h>
55 #include <net/route.h>
56
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip.h>
61 #include <netinet/ip_var.h>
62
63 #include <machine/mtpr.h>
64 #include <machine/stdarg.h>
65
66 #include "ns.h"
67 #include "ns_if.h"
68 #include "idp.h"
69
70 struct ifnet_en {
71         struct ifnet ifen_ifnet;
72         struct route ifen_route;
73         struct in_addr ifen_src;
74         struct in_addr ifen_dst;
75         struct ifnet_en *ifen_next;
76 };
77
78 int     nsipoutput(), nsipioctl(), nsipstart();
79 #define LOMTU   (1024+512);
80
81 struct ifnet nsipif;
82 static int nsipif_units;
83 struct ifnet_en *nsip_list;             /* list of all hosts and gateways or
84                                         broadcast addrs */
85
86 struct ifnet_en *
87 nsipattach()
88 {
89         struct ifnet_en *m;
90         struct ifnet *ifp;
91
92         if (nsipif.if_mtu == 0) {
93                 ifp = &nsipif;
94                 if_initname(ifp, "nsip", nsipif_units);
95                 ifp->if_mtu = LOMTU;
96                 ifp->if_ioctl = nsipioctl;
97                 ifp->if_output = nsipoutput;
98                 ifp->if_start = nsipstart;
99                 ifp->if_flags = IFF_POINTOPOINT;
100         }
101
102         MALLOC((m), struct ifnet_en *, sizeof(*m), M_PCB, M_WAITOK);
103         m->ifen_next = nsip_list;
104         nsip_list = m;
105         ifp = &m->ifen_ifnet;
106
107         if_initname(ifp, "nsip", nsipif_units++);
108         ifp->if_name = "nsip";
109         ifp->if_mtu = LOMTU;
110         ifp->if_ioctl = nsipioctl;
111         ifp->if_output = nsipoutput;
112         ifp->if_start = nsipstart;
113         ifp->if_flags = IFF_POINTOPOINT;
114         if_attach(ifp);
115
116         return (m);
117 }
118
119
120 /*
121  * Process an ioctl request.
122  */
123 /* ARGSUSED */
124 nsipioctl(ifp, cmd, data)
125         struct ifnet *ifp;
126         int cmd;
127         caddr_t data;
128 {
129         int error = 0;
130         struct ifreq *ifr;
131
132         switch (cmd) {
133
134         case SIOCSIFADDR:
135                 ifp->if_flags |= IFF_UP;
136                 /* fall into: */
137
138         case SIOCSIFDSTADDR:
139                 /*
140                  * Everything else is done at a higher level.
141                  */
142                 break;
143
144         case SIOCSIFFLAGS:
145                 ifr = (struct ifreq *)data;
146                 if ((ifr->ifr_flags & IFF_UP) == 0)
147                         error = nsip_free(ifp);
148
149
150         default:
151                 error = EINVAL;
152         }
153         return (error);
154 }
155
156 struct mbuf *nsip_badlen;
157 struct mbuf *nsip_lastin;
158 int nsip_hold_input;
159
160 #warning "Audit the second argument, this expect ifp, but gets int from netinet"
161
162 void
163 idpip_input(struct mbuf *m, ...)
164 {
165         struct ip *ip;
166         struct idp *idp;
167         int len, s;
168         struct ifnet *ifp;
169         __va_list ap;
170
171         __va_start(ap, m);
172         ifp = __va_arg(ap, struct ifnet *);
173         __va_end(ap);
174
175         if (nsip_hold_input) {
176                 if (nsip_lastin) {
177                         m_freem(nsip_lastin);
178                 }
179                 nsip_lastin = m_copym(m, 0, (int)M_COPYALL, MB_DONTWAIT);
180         }
181         /*
182          * Get IP and IDP header together in first mbuf.
183          */
184         nsipif.if_ipackets++;
185         s = sizeof (struct ip) + sizeof (struct idp);
186         if (((m->m_flags & M_EXT) || m->m_len < s) &&
187             (m = m_pullup(m, s)) == 0) {
188                 nsipif.if_ierrors++;
189                 return;
190         }
191         ip = mtod(m, struct ip *);
192         if (ip->ip_hl > (sizeof (struct ip) >> 2)) {
193                 ip_stripoptions(m);
194                 if (m->m_len < s) {
195                         if ((m = m_pullup(m, s)) == 0) {
196                                 nsipif.if_ierrors++;
197                                 return;
198                         }
199                         ip = mtod(m, struct ip *);
200                 }
201         }
202
203         /*
204          * Make mbuf data length reflect IDP length.
205          * If not enough data to reflect IDP length, drop.
206          */
207         m->m_data += sizeof (struct ip);
208         m->m_len -= sizeof (struct ip);
209         m->m_pkthdr.len -= sizeof (struct ip);
210         idp = mtod(m, struct idp *);
211         len = ntohs(idp->idp_len);
212         if (len & 1) len++;             /* Preserve Garbage Byte */
213         if (ip->ip_len != len) {
214                 if (len > ip->ip_len) {
215                         nsipif.if_ierrors++;
216                         if (nsip_badlen) m_freem(nsip_badlen);
217                         nsip_badlen = m;
218                         return;
219                 }
220                 /* Any extra will be trimmed off by the NS routines */
221         }
222
223         /*
224          * Place interface pointer before the data
225          * for the receiving protocol.
226          */
227         m->m_pkthdr.rcvif = ifp;
228         /*
229          * Deliver to NS
230          */
231         netisr_dispatch(NETISR_NS, m);
232 }
233
234 /* ARGSUSED */
235 nsipoutput(ifn, m, dst)
236         struct ifnet_en *ifn;
237         struct mbuf *m;
238         struct sockaddr *dst;
239 {
240
241         struct ip *ip;
242         struct route *ro = &(ifn->ifen_route);
243         int len = 0;
244         struct idp *idp = mtod(m, struct idp *);
245         int error;
246
247         ifn->ifen_ifnet.if_opackets++;
248         nsipif.if_opackets++;
249
250
251         /*
252          * Calculate data length and make space
253          * for IP header.
254          */
255         len =  ntohs(idp->idp_len);
256         if (len & 1) len++;             /* Preserve Garbage Byte */
257         /* following clause not necessary on vax */
258         if (3 & (int)m->m_data) {
259                 /* force longword alignment of ip hdr */
260                 struct mbuf *m0 = m_gethdr(MT_HEADER, MB_DONTWAIT);
261                 if (m0 == 0) {
262                         m_freem(m);
263                         return (ENOBUFS);
264                 }
265                 MH_ALIGN(m0, sizeof (struct ip));
266                 m0->m_flags = m->m_flags & M_COPYFLAGS;
267                 m0->m_next = m;
268                 m0->m_len = sizeof (struct ip);
269                 m0->m_pkthdr.len = m0->m_len + m->m_len;
270         } else {
271                 M_PREPEND(m, sizeof (struct ip), MB_DONTWAIT);
272                 if (m == 0)
273                         return (ENOBUFS);
274         }
275         /*
276          * Fill in IP header.
277          */
278         ip = mtod(m, struct ip *);
279         *(long *)ip = 0;
280         ip->ip_p = IPPROTO_IDP;
281         ip->ip_src = ifn->ifen_src;
282         ip->ip_dst = ifn->ifen_dst;
283         ip->ip_len = (u_short)len + sizeof (struct ip);
284         ip->ip_ttl = MAXTTL;
285
286         /*
287          * Output final datagram.
288          */
289         error =  (ip_output(m, (struct mbuf *)0, ro, SO_BROADCAST, NULL));
290         if (error) {
291                 ifn->ifen_ifnet.if_oerrors++;
292                 ifn->ifen_ifnet.if_ierrors = error;
293         }
294         return (error);
295 bad:
296         m_freem(m);
297         return (ENETUNREACH);
298 }
299
300 nsipstart(ifp)
301 struct ifnet *ifp;
302 {
303         panic("nsip_start called");
304 }
305
306 struct ifreq ifr = {"nsip0"};
307
308 nsip_route(m)
309         struct mbuf *m;
310 {
311         struct nsip_req *rq = mtod(m, struct nsip_req *);
312         struct sockaddr_ns *ns_dst = (struct sockaddr_ns *)&rq->rq_ns;
313         struct sockaddr_in *ip_dst = (struct sockaddr_in *)&rq->rq_ip;
314         struct route ro;
315         struct ifnet_en *ifn;
316         struct sockaddr_in *src;
317
318         /*
319          * First, make sure we already have an ns address:
320          */
321         if (ns_hosteqnh(ns_thishost, ns_zerohost))
322                 return (EADDRNOTAVAIL);
323         /*
324          * Now, determine if we can get to the destination
325          */
326         bzero((caddr_t)&ro, sizeof (ro));
327         ro.ro_dst = *(struct sockaddr *)ip_dst;
328         rtalloc(&ro);
329         if (ro.ro_rt == 0 || ro.ro_rt->rt_ifp == 0) {
330                 return (ENETUNREACH);
331         }
332
333         /*
334          * And see how he's going to get back to us:
335          * i.e., what return ip address do we use?
336          */
337         {
338                 struct in_ifaddr *ia;
339                 struct ifnet *ifp = ro.ro_rt->rt_ifp;
340
341                 for (ia = in_ifaddr; ia; ia = ia->ia_next)
342                         if (ia->ia_ifp == ifp)
343                                 break;
344                 if (ia == 0)
345                         ia = in_ifaddr;
346                 if (ia == 0) {
347                         RTFREE(ro.ro_rt);
348                         return (EADDRNOTAVAIL);
349                 }
350                 src = (struct sockaddr_in *)&ia->ia_addr;
351         }
352
353         /*
354          * Is there a free (pseudo-)interface or space?
355          */
356         for (ifn = nsip_list; ifn; ifn = ifn->ifen_next) {
357                 if ((ifn->ifen_ifnet.if_flags & IFF_UP) == 0)
358                         break;
359         }
360         if (ifn == NULL)
361                 ifn = nsipattach();
362         if (ifn == NULL) {
363                 RTFREE(ro.ro_rt);
364                 return (ENOBUFS);
365         }
366         ifn->ifen_route = ro;
367         ifn->ifen_dst =  ip_dst->sin_addr;
368         ifn->ifen_src = src->sin_addr;
369
370         /*
371          * now configure this as a point to point link
372          */
373         ifr.ifr_name[4] = '0' + nsipif_units - 1;
374         ifr.ifr_dstaddr = * (struct sockaddr *) ns_dst;
375         (void)ns_control((struct socket *)0, (int)SIOCSIFDSTADDR, (caddr_t)&ifr,
376                         (struct ifnet *)ifn, NULL);
377         satons_addr(ifr.ifr_addr).x_host = ns_thishost;
378         return (ns_control((struct socket *)0, (int)SIOCSIFADDR, (caddr_t)&ifr,
379                         (struct ifnet *)ifn, NULL));
380 }
381
382 nsip_free(ifp)
383 struct ifnet *ifp;
384 {
385         struct ifnet_en *ifn = (struct ifnet_en *)ifp;
386         struct route *ro = & ifn->ifen_route;
387
388         if (ro->ro_rt) {
389                 RTFREE(ro->ro_rt);
390                 ro->ro_rt = 0;
391         }
392         ifp->if_flags &= ~IFF_UP;
393         return (0);
394 }
395
396 nsip_ctlinput(cmd, sa)
397         int cmd;
398         struct sockaddr *sa;
399 {
400         extern u_char inetctlerrmap[];
401         struct sockaddr_in *sin;
402         int in_rtchange();
403
404         if ((unsigned)cmd >= PRC_NCMDS)
405                 return;
406         if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
407                 return;
408         sin = (struct sockaddr_in *)sa;
409         if (sin->sin_addr.s_addr == INADDR_ANY)
410                 return;
411
412         switch (cmd) {
413
414         case PRC_ROUTEDEAD:
415         case PRC_REDIRECT_NET:
416         case PRC_REDIRECT_HOST:
417         case PRC_REDIRECT_TOSNET:
418         case PRC_REDIRECT_TOSHOST:
419                 nsip_rtchange(&sin->sin_addr);
420                 break;
421         }
422 }
423
424 nsip_rtchange(dst)
425         struct in_addr *dst;
426 {
427         struct ifnet_en *ifn;
428
429         for (ifn = nsip_list; ifn; ifn = ifn->ifen_next) {
430                 if (ifn->ifen_dst.s_addr == dst->s_addr &&
431                         ifn->ifen_route.ro_rt) {
432                                 RTFREE(ifn->ifen_route.ro_rt);
433                                 ifn->ifen_route.ro_rt = 0;
434                 }
435         }
436 }
437 #endif