5eda2ad23fd68a710c1f272ac9da0c141c7179c3
[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  */
38
39 /*
40  * Software interface driver for encapsulating IPX in IP.
41  */
42
43 #include "opt_inet.h"
44 #include "opt_ipx.h"
45
46 #ifdef IPXIP
47 #ifndef INET
48 #error The option IPXIP requires option INET.
49 #endif
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/malloc.h>
54 #include <sys/mbuf.h>
55 #include <sys/protosw.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/sockio.h>
59
60 #include <sys/msgport2.h>
61
62 #include <net/if.h>
63 #include <net/netisr.h>
64 #include <net/route.h>
65
66 #include <netinet/in.h>
67 #include <netinet/in_systm.h>
68 #include <netinet/in_var.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71
72 #include "ipx.h"
73 #include "ipx_if.h"
74 #include "ipx_ip.h"
75 #include "ipx_var.h"
76
77 static struct   ifnet ipxipif;
78 static int      ipxipif_units;
79
80 /* list of all hosts and gateways or broadcast addrs */
81 static struct   ifnet_en *ipxip_list;
82
83 static  struct ifnet_en *ipxipattach(void);
84 static  int ipxip_free(struct ifnet *ifp);
85 static  int ipxipioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
86                        struct ucred *cr);
87 static  int ipxipoutput(struct ifnet *ifp, struct mbuf *m,
88                         struct sockaddr *dst, struct rtentry *rt);
89 static  void ipxip_rtchange(struct in_addr *dst);
90 static  void ipxipstart(struct ifnet *ifp);
91
92 static struct ifnet_en *
93 ipxipattach(void)
94 {
95         struct ifnet_en *m;
96         struct ifnet *ifp;
97
98         if (ipxipif.if_mtu == 0) {
99                 ifp = &ipxipif;
100                 if_initname(ifp, "ipxip", ipxipif_units);
101                 ifp->if_mtu = LOMTU;
102                 ifp->if_ioctl = ipxipioctl;
103                 ifp->if_output = ipxipoutput;
104                 ifp->if_start = ipxipstart;
105                 ifp->if_flags = IFF_POINTOPOINT;
106         }
107
108         (m) = kmalloc(sizeof(*m), M_PCB, M_WAITOK | M_ZERO);
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, NULL);
120
121         return (m);
122 }
123
124
125 /*
126  * Process an ioctl request.
127  */
128 static int
129 ipxipioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
130 {
131         int error = 0;
132         struct ifreq *ifr;
133
134         switch (cmd) {
135
136         case SIOCSIFADDR:
137                 ifp->if_flags |= IFF_UP;
138                 /* fall into: */
139
140         case SIOCSIFDSTADDR:
141                 /*
142                  * Everything else is done at a higher level.
143                  */
144                 break;
145
146         case SIOCSIFFLAGS:
147                 ifr = (struct ifreq *)data;
148                 if ((ifr->ifr_flags & IFF_UP) == 0)
149                         error = ipxip_free(ifp);
150
151
152         default:
153                 error = EINVAL;
154         }
155         return (error);
156 }
157
158 static struct mbuf *ipxip_badlen;
159 static struct mbuf *ipxip_lastin;
160 static int ipxip_hold_input;
161
162 int
163 ipxip_input(struct mbuf **mp, int *offp, int proto)
164 {
165         struct mbuf *m = *mp;
166         struct ip *ip;
167         struct ipx *ipx;
168         int len, s;
169
170         if (ipxip_hold_input) {
171                 if (ipxip_lastin != NULL) {
172                         m_freem(ipxip_lastin);
173                 }
174                 ipxip_lastin = m_copym(m, 0, (int)M_COPYALL, MB_DONTWAIT);
175         }
176         /*
177          * Get IP and IPX header together in first mbuf.
178          */
179         ipxipif.if_ipackets++;
180         s = sizeof(struct ip) + sizeof(struct ipx);
181         if (((m->m_flags & M_EXT) || m->m_len < s) &&
182             (m = m_pullup(m, s)) == NULL) {
183                 ipxipif.if_ierrors++;
184                 return(IPPROTO_DONE);
185         }
186         ip = mtod(m, struct ip *);
187         if (ip->ip_hl > (sizeof(struct ip) >> 2)) {
188                 ip_stripoptions(m);
189                 if (m->m_len < s) {
190                         if ((m = m_pullup(m, s)) == NULL) {
191                                 ipxipif.if_ierrors++;
192                                 return(IPPROTO_DONE);
193                         }
194                         ip = mtod(m, struct ip *);
195                 }
196         }
197
198         /*
199          * Make mbuf data length reflect IPX length.
200          * If not enough data to reflect IPX length, drop.
201          */
202         m->m_data += sizeof(struct ip);
203         m->m_len -= sizeof(struct ip);
204         m->m_pkthdr.len -= sizeof(struct ip);
205         ipx = mtod(m, struct ipx *);
206         len = ntohs(ipx->ipx_len);
207         if (len & 1)
208                 len++;          /* Preserve Garbage Byte */
209         if (ip->ip_len != len) {
210                 if (len > ip->ip_len) {
211                         ipxipif.if_ierrors++;
212                         if (ipxip_badlen)
213                                 m_freem(ipxip_badlen);
214                         ipxip_badlen = m;
215                         return(IPPROTO_DONE);
216                 }
217                 /* Any extra will be trimmed off by the IPX routines */
218         }
219
220         /*
221          * Deliver to IPX
222          */
223         netisr_queue(NETISR_IPX, m);
224         return(IPPROTO_DONE);
225 }
226
227 static int
228 ipxipoutput_serialized(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
229                        struct rtentry *rt)
230 {
231         struct ifnet_en *ifn = (struct ifnet_en *)ifp;
232         struct ip *ip;
233         struct route *ro = &(ifn->ifen_route);
234         int len = 0;
235         struct ipx *ipx = mtod(m, struct ipx *);
236         int error;
237
238         ifn->ifen_ifnet.if_opackets++;
239         ipxipif.if_opackets++;
240
241         /*
242          * Calculate data length and make space
243          * for IP header.
244          */
245         len =  ntohs(ipx->ipx_len);
246         if (len & 1)
247                 len++;          /* Preserve Garbage Byte */
248         /* following clause not necessary on vax */
249         if (3 & (intptr_t)m->m_data) {
250                 /* force longword alignment of ip hdr */
251                 struct mbuf *m0 = m_gethdr(MT_HEADER, MB_DONTWAIT);
252                 if (m0 == NULL) {
253                         m_freem(m);
254                         return (ENOBUFS);
255                 }
256                 MH_ALIGN(m0, sizeof(struct ip));
257                 m0->m_flags = m->m_flags & M_COPYFLAGS;
258                 m0->m_next = m;
259                 m0->m_len = sizeof(struct ip);
260                 m0->m_pkthdr.len = m0->m_len + m->m_len;
261                 m = m0;
262         } else {
263                 M_PREPEND(m, sizeof(struct ip), MB_DONTWAIT);
264                 if (m == NULL)
265                         return (ENOBUFS);
266         }
267         /*
268          * Fill in IP header.
269          */
270         ip = mtod(m, struct ip *);
271         *(long *)ip = 0;
272         ip->ip_p = IPPROTO_IDP;
273         ip->ip_src = ifn->ifen_src;
274         ip->ip_dst = ifn->ifen_dst;
275         ip->ip_len = (u_short)len + sizeof(struct ip);
276         ip->ip_ttl = MAXTTL;
277
278         /*
279          * Output final datagram.
280          */
281         error =  (ip_output(m, NULL, ro, SO_BROADCAST, NULL, NULL));
282         if (error) {
283                 ifn->ifen_ifnet.if_oerrors++;
284                 ifn->ifen_ifnet.if_ierrors = error;
285         }
286         return (error);
287         m_freem(m);
288         return (ENETUNREACH);
289 }
290
291 static int
292 ipxipoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
293             struct rtentry *rt)
294 {
295         int error;
296
297         ifnet_serialize_tx(ifp);
298         error = ipxipoutput_serialized(ifp, m, dst, rt);
299         ifnet_deserialize_tx(ifp);
300
301         return error;
302 }
303
304 static void
305 ipxipstart(struct ifnet *ifp)
306 {
307         panic("ipxip_start called\n");
308 }
309
310 static struct ifreq ifr_ipxip = {"ipxip0"};
311
312 int
313 ipxip_route(struct socket *so, struct sockopt *sopt)
314 {
315         int error;
316         struct ifnet_en *ifn;
317         struct sockaddr_in *src;
318         struct ipxip_req rq;
319         struct sockaddr_ipx *ipx_dst;
320         struct sockaddr_in *ip_dst;
321         struct route ro;
322
323         error = sooptcopyin(sopt, &rq, sizeof rq, sizeof rq);
324         if (error)
325                 return (error);
326         ipx_dst = (struct sockaddr_ipx *)&rq.rq_ipx;
327         ip_dst = (struct sockaddr_in *)&rq.rq_ip;
328
329         /*
330          * First, make sure we already have an IPX address:
331          */
332         if (ipx_ifaddr == NULL)
333                 return (EADDRNOTAVAIL);
334         /*
335          * Now, determine if we can get to the destination
336          */
337         bzero((caddr_t)&ro, sizeof(ro));
338         ro.ro_dst = *(struct sockaddr *)ip_dst;
339         rtalloc(&ro);
340         if (ro.ro_rt == NULL || ro.ro_rt->rt_ifp == NULL) {
341                 return (ENETUNREACH);
342         }
343
344         /*
345          * And see how he's going to get back to us:
346          * i.e., what return ip address do we use?
347          */
348         {
349                 struct in_ifaddr *ia;
350                 struct in_ifaddr_container *iac;
351                 struct ifnet *ifp = ro.ro_rt->rt_ifp;
352
353                 ia = NULL;
354                 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
355                         if (iac->ia->ia_ifp == ifp) {
356                                 ia = iac->ia;
357                                 break;
358                         }
359                 }
360                 if (ia == NULL && !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
361                         ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
362                 if (ia == NULL) {
363                         RTFREE(ro.ro_rt);
364                         return (EADDRNOTAVAIL);
365                 }
366                 src = (struct sockaddr_in *)&ia->ia_addr;
367         }
368
369         /*
370          * Is there a free (pseudo-)interface or space?
371          */
372         for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
373                 if ((ifn->ifen_ifnet.if_flags & IFF_UP) == 0)
374                         break;
375         }
376         if (ifn == NULL)
377                 ifn = ipxipattach();
378         if (ifn == NULL) {
379                 RTFREE(ro.ro_rt);
380                 return (ENOBUFS);
381         }
382         ifn->ifen_route = ro;
383         ifn->ifen_dst =  ip_dst->sin_addr;
384         ifn->ifen_src = src->sin_addr;
385
386         /*
387          * now configure this as a point to point link
388          */
389         ifr_ipxip.ifr_name[4] = '0' + ipxipif_units - 1;
390         ifr_ipxip.ifr_dstaddr = *(struct sockaddr *)ipx_dst;
391         ipx_control_oncpu(so, (int)SIOCSIFDSTADDR, (caddr_t)&ifr_ipxip,
392                         (struct ifnet *)ifn, sopt->sopt_td);
393
394         /* use any of our addresses */
395         satoipx_addr(ifr_ipxip.ifr_addr).x_host = 
396                         ipx_ifaddr->ia_addr.sipx_addr.x_host;
397
398         return (ipx_control_oncpu(so, (int)SIOCSIFADDR, (caddr_t)&ifr_ipxip,
399                         (struct ifnet *)ifn, sopt->sopt_td));
400 }
401
402 static int
403 ipxip_free(struct ifnet *ifp)
404 {
405         struct ifnet_en *ifn = (struct ifnet_en *)ifp;
406         struct route *ro = & ifn->ifen_route;
407
408         if (ro->ro_rt != NULL) {
409                 RTFREE(ro->ro_rt);
410                 ro->ro_rt = NULL;
411         }
412         ifp->if_flags &= ~IFF_UP;
413         return (0);
414 }
415
416 void
417 ipxip_ctlinput(netmsg_t msg)
418 {
419         int cmd = msg->ctlinput.nm_cmd;
420         struct sockaddr *sa = msg->ctlinput.nm_arg;
421         struct sockaddr_in *sin;
422
423         if ((unsigned)cmd >= PRC_NCMDS)
424                 goto out;
425         if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
426                 goto out;
427         sin = (struct sockaddr_in *)sa;
428         if (sin->sin_addr.s_addr == INADDR_ANY)
429                 goto out;
430
431         switch (cmd) {
432
433         case PRC_ROUTEDEAD:
434         case PRC_REDIRECT_NET:
435         case PRC_REDIRECT_HOST:
436         case PRC_REDIRECT_TOSNET:
437         case PRC_REDIRECT_TOSHOST:
438                 ipxip_rtchange(&sin->sin_addr);
439                 break;
440         }
441 out:
442         lwkt_replymsg(&msg->lmsg, 0);
443 }
444
445 static void
446 ipxip_rtchange(struct in_addr *dst)
447 {
448         struct ifnet_en *ifn;
449
450         for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
451                 if (ifn->ifen_dst.s_addr == dst->s_addr &&
452                         ifn->ifen_route.ro_rt != NULL) {
453                                 RTFREE(ifn->ifen_route.ro_rt);
454                                 ifn->ifen_route.ro_rt = NULL;
455                 }
456         }
457 }
458 #endif /* IPXIP */