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