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