Merge from vendor branch BINUTILS:
[dragonfly.git] / sys / netproto / ipx / ipx_ip.c
1 /*
2  * Copyright (c) 1995, Mike Mitchell
3  * Copyright (c) 1984, 1985, 1986, 1987, 1993
4  *      The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by the University of
17  *      California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  *      @(#)ipx_ip.c
35  *
36  * $FreeBSD: src/sys/netipx/ipx_ip.c,v 1.24.2.2 2003/01/23 21:06:48 sam Exp $
37  * $DragonFly: src/sys/netproto/ipx/ipx_ip.c,v 1.8 2004/01/06 03:17:28 dillon Exp $
38  */
39
40 /*
41  * Software interface driver for encapsulating IPX in IP.
42  */
43
44 #include "opt_inet.h"
45 #include "opt_ipx.h"
46
47 #ifdef IPXIP
48 #ifndef INET
49 #error The option IPXIP requires option INET.
50 #endif
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/malloc.h>
55 #include <sys/mbuf.h>
56 #include <sys/protosw.h>
57 #include <sys/socket.h>
58 #include <sys/socketvar.h>
59 #include <sys/sockio.h>
60
61 #include <net/if.h>
62 #include <net/netisr.h>
63 #include <net/route.h>
64
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip.h>
69 #include <netinet/ip_var.h>
70
71 #include "ipx.h"
72 #include "ipx_if.h"
73 #include "ipx_ip.h"
74 #include "ipx_var.h"
75
76 static struct   ifnet ipxipif;
77 static int      ipxipif_units;
78
79 /* list of all hosts and gateways or broadcast addrs */
80 static struct   ifnet_en *ipxip_list;
81
82 static  struct ifnet_en *ipxipattach(void);
83 static  int ipxip_free(struct ifnet *ifp);
84 static  int ipxipioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
85 static  int ipxipoutput(struct ifnet *ifp, struct mbuf *m,
86                         struct sockaddr *dst, struct rtentry *rt);
87 static  void ipxip_rtchange(struct in_addr *dst);
88 static  void ipxipstart(struct ifnet *ifp);
89
90 static struct ifnet_en *
91 ipxipattach()
92 {
93         struct ifnet_en *m;
94         struct ifnet *ifp;
95
96         if (ipxipif.if_mtu == 0) {
97                 ifp = &ipxipif;
98                 if_initname(ifp, "ipxip", ipxipif_units);
99                 ifp->if_mtu = LOMTU;
100                 ifp->if_ioctl = ipxipioctl;
101                 ifp->if_output = ipxipoutput;
102                 ifp->if_start = ipxipstart;
103                 ifp->if_flags = IFF_POINTOPOINT;
104         }
105
106         MALLOC((m), struct ifnet_en *, sizeof(*m), M_PCB, M_NOWAIT | M_ZERO);
107         if (m == NULL)
108                 return (NULL);
109         m->ifen_next = ipxip_list;
110         ipxip_list = m;
111         ifp = &m->ifen_ifnet;
112
113         if_initname(ifp, "ipxip", ipxipif_units++);
114         ifp->if_mtu = LOMTU;
115         ifp->if_ioctl = ipxipioctl;
116         ifp->if_output = ipxipoutput;
117         ifp->if_start = ipxipstart;
118         ifp->if_flags = IFF_POINTOPOINT;
119         if_attach(ifp);
120
121         return (m);
122 }
123
124
125 /*
126  * Process an ioctl request.
127  */
128 static int
129 ipxipioctl(ifp, cmd, data)
130         struct ifnet *ifp;
131         u_long cmd;
132         caddr_t data;
133 {
134         int error = 0;
135         struct ifreq *ifr;
136
137         switch (cmd) {
138
139         case SIOCSIFADDR:
140                 ifp->if_flags |= IFF_UP;
141                 /* fall into: */
142
143         case SIOCSIFDSTADDR:
144                 /*
145                  * Everything else is done at a higher level.
146                  */
147                 break;
148
149         case SIOCSIFFLAGS:
150                 ifr = (struct ifreq *)data;
151                 if ((ifr->ifr_flags & IFF_UP) == 0)
152                         error = ipxip_free(ifp);
153
154
155         default:
156                 error = EINVAL;
157         }
158         return (error);
159 }
160
161 static struct mbuf *ipxip_badlen;
162 static struct mbuf *ipxip_lastin;
163 static int ipxip_hold_input;
164
165 void
166 ipxip_input(m, hlen, dummy)
167         struct mbuf *m;
168         int hlen;
169         int dummy;
170 {
171         struct ip *ip;
172         struct ipx *ipx;
173         int len, s;
174
175         if (ipxip_hold_input) {
176                 if (ipxip_lastin != NULL) {
177                         m_freem(ipxip_lastin);
178                 }
179                 ipxip_lastin = m_copym(m, 0, (int)M_COPYALL, M_DONTWAIT);
180         }
181         /*
182          * Get IP and IPX header together in first mbuf.
183          */
184         ipxipif.if_ipackets++;
185         s = sizeof(struct ip) + sizeof(struct ipx);
186         if (((m->m_flags & M_EXT) || m->m_len < s) &&
187             (m = m_pullup(m, s)) == NULL) {
188                 ipxipif.if_ierrors++;
189                 return;
190         }
191         ip = mtod(m, struct ip *);
192         if (ip->ip_hl > (sizeof(struct ip) >> 2)) {
193                 ip_stripoptions(m, (struct mbuf *)NULL);
194                 if (m->m_len < s) {
195                         if ((m = m_pullup(m, s)) == NULL) {
196                                 ipxipif.if_ierrors++;
197                                 return;
198                         }
199                         ip = mtod(m, struct ip *);
200                 }
201         }
202
203         /*
204          * Make mbuf data length reflect IPX length.
205          * If not enough data to reflect IPX 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         ipx = mtod(m, struct ipx *);
211         len = ntohs(ipx->ipx_len);
212         if (len & 1)
213                 len++;          /* Preserve Garbage Byte */
214         if (ip->ip_len != len) {
215                 if (len > ip->ip_len) {
216                         ipxipif.if_ierrors++;
217                         if (ipxip_badlen)
218                                 m_freem(ipxip_badlen);
219                         ipxip_badlen = m;
220                         return;
221                 }
222                 /* Any extra will be trimmed off by the IPX routines */
223         }
224
225         /*
226          * Deliver to IPX
227          */
228         netisr_dispatch(NETISR_IPX, m);
229 }
230
231 static int
232 ipxipoutput(ifp, m, dst, rt)
233         struct ifnet *ifp;
234         struct mbuf *m;
235         struct sockaddr *dst;
236         struct rtentry *rt;
237 {
238         struct ifnet_en *ifn = (struct ifnet_en *)ifp;
239         struct ip *ip;
240         struct route *ro = &(ifn->ifen_route);
241         int len = 0;
242         struct ipx *ipx = mtod(m, struct ipx *);
243         int error;
244
245         ifn->ifen_ifnet.if_opackets++;
246         ipxipif.if_opackets++;
247
248         /*
249          * Calculate data length and make space
250          * for IP header.
251          */
252         len =  ntohs(ipx->ipx_len);
253         if (len & 1)
254                 len++;          /* Preserve Garbage Byte */
255         /* following clause not necessary on vax */
256         if (3 & (int)m->m_data) {
257                 /* force longword alignment of ip hdr */
258                 struct mbuf *m0 = m_gethdr(MT_HEADER, M_DONTWAIT);
259                 if (m0 == NULL) {
260                         m_freem(m);
261                         return (ENOBUFS);
262                 }
263                 MH_ALIGN(m0, sizeof(struct ip));
264                 m0->m_flags = m->m_flags & M_COPYFLAGS;
265                 m0->m_next = m;
266                 m0->m_len = sizeof(struct ip);
267                 m0->m_pkthdr.len = m0->m_len + m->m_len;
268                 m->m_flags &= ~M_PKTHDR;
269                 m = m0;
270         } else {
271                 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
272                 if (m == NULL)
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 *)NULL, ro, SO_BROADCAST, NULL, NULL));
290         if (error) {
291                 ifn->ifen_ifnet.if_oerrors++;
292                 ifn->ifen_ifnet.if_ierrors = error;
293         }
294         return (error);
295         m_freem(m);
296         return (ENETUNREACH);
297 }
298
299 static void
300 ipxipstart(ifp)
301 struct ifnet *ifp;
302 {
303         panic("ipxip_start called\n");
304 }
305
306 static struct ifreq ifr_ipxip = {"ipxip0"};
307
308 int
309 ipxip_route(so, sopt)
310         struct socket *so;
311         struct sockopt *sopt;
312 {
313         int error;
314         struct ifnet_en *ifn;
315         struct sockaddr_in *src;
316         struct ipxip_req rq;
317         struct sockaddr_ipx *ipx_dst;
318         struct sockaddr_in *ip_dst;
319         struct route ro;
320
321         error = sooptcopyin(sopt, &rq, sizeof rq, sizeof rq);
322         if (error)
323                 return (error);
324         ipx_dst = (struct sockaddr_ipx *)&rq.rq_ipx;
325         ip_dst = (struct sockaddr_in *)&rq.rq_ip;
326
327         /*
328          * First, make sure we already have an IPX address:
329          */
330         if (ipx_ifaddr == NULL)
331                 return (EADDRNOTAVAIL);
332         /*
333          * Now, determine if we can get to the destination
334          */
335         bzero((caddr_t)&ro, sizeof(ro));
336         ro.ro_dst = *(struct sockaddr *)ip_dst;
337         rtalloc(&ro);
338         if (ro.ro_rt == NULL || ro.ro_rt->rt_ifp == NULL) {
339                 return (ENETUNREACH);
340         }
341
342         /*
343          * And see how he's going to get back to us:
344          * i.e., what return ip address do we use?
345          */
346         {
347                 struct in_ifaddr *ia;
348                 struct ifnet *ifp = ro.ro_rt->rt_ifp;
349
350                 for (ia = TAILQ_FIRST(&in_ifaddrhead); ia != NULL; 
351                      ia = TAILQ_NEXT(ia, ia_link))
352                         if (ia->ia_ifp == ifp)
353                                 break;
354                 if (ia == NULL)
355                         ia = TAILQ_FIRST(&in_ifaddrhead);
356                 if (ia == NULL) {
357                         RTFREE(ro.ro_rt);
358                         return (EADDRNOTAVAIL);
359                 }
360                 src = (struct sockaddr_in *)&ia->ia_addr;
361         }
362
363         /*
364          * Is there a free (pseudo-)interface or space?
365          */
366         for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
367                 if ((ifn->ifen_ifnet.if_flags & IFF_UP) == 0)
368                         break;
369         }
370         if (ifn == NULL)
371                 ifn = ipxipattach();
372         if (ifn == NULL) {
373                 RTFREE(ro.ro_rt);
374                 return (ENOBUFS);
375         }
376         ifn->ifen_route = ro;
377         ifn->ifen_dst =  ip_dst->sin_addr;
378         ifn->ifen_src = src->sin_addr;
379
380         /*
381          * now configure this as a point to point link
382          */
383         ifr_ipxip.ifr_name[4] = '0' + ipxipif_units - 1;
384         ifr_ipxip.ifr_dstaddr = *(struct sockaddr *)ipx_dst;
385         ipx_control(so, (int)SIOCSIFDSTADDR, (caddr_t)&ifr_ipxip,
386                         (struct ifnet *)ifn, sopt->sopt_td);
387
388         /* use any of our addresses */
389         satoipx_addr(ifr_ipxip.ifr_addr).x_host = 
390                         ipx_ifaddr->ia_addr.sipx_addr.x_host;
391
392         return (ipx_control(so, (int)SIOCSIFADDR, (caddr_t)&ifr_ipxip,
393                         (struct ifnet *)ifn, sopt->sopt_td));
394 }
395
396 static int
397 ipxip_free(ifp)
398 struct ifnet *ifp;
399 {
400         struct ifnet_en *ifn = (struct ifnet_en *)ifp;
401         struct route *ro = & ifn->ifen_route;
402
403         if (ro->ro_rt != NULL) {
404                 RTFREE(ro->ro_rt);
405                 ro->ro_rt = NULL;
406         }
407         ifp->if_flags &= ~IFF_UP;
408         return (0);
409 }
410
411 void
412 ipxip_ctlinput(cmd, sa, dummy)
413         int cmd;
414         struct sockaddr *sa;
415         void *dummy;
416 {
417         struct sockaddr_in *sin;
418
419         if ((unsigned)cmd >= PRC_NCMDS)
420                 return;
421         if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
422                 return;
423         sin = (struct sockaddr_in *)sa;
424         if (sin->sin_addr.s_addr == INADDR_ANY)
425                 return;
426
427         switch (cmd) {
428
429         case PRC_ROUTEDEAD:
430         case PRC_REDIRECT_NET:
431         case PRC_REDIRECT_HOST:
432         case PRC_REDIRECT_TOSNET:
433         case PRC_REDIRECT_TOSHOST:
434                 ipxip_rtchange(&sin->sin_addr);
435                 break;
436         }
437 }
438
439 static void
440 ipxip_rtchange(dst)
441         struct in_addr *dst;
442 {
443         struct ifnet_en *ifn;
444
445         for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
446                 if (ifn->ifen_dst.s_addr == dst->s_addr &&
447                         ifn->ifen_route.ro_rt != NULL) {
448                                 RTFREE(ifn->ifen_route.ro_rt);
449                                 ifn->ifen_route.ro_rt = NULL;
450                 }
451         }
452 }
453 #endif /* IPXIP */