Remove advertising clause from all that isn't contrib or userland bin.
[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  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  * 
30  *      @(#)ipx_ip.c
31  *
32  * $FreeBSD: src/sys/netipx/ipx_ip.c,v 1.24.2.2 2003/01/23 21:06:48 sam Exp $
33  */
34
35 /*
36  * Software interface driver for encapsulating IPX in IP.
37  */
38
39 #include "opt_inet.h"
40 #include "opt_ipx.h"
41
42 #ifdef IPXIP
43 #ifndef INET
44 #error The option IPXIP requires option INET.
45 #endif
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/malloc.h>
50 #include <sys/mbuf.h>
51 #include <sys/protosw.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sockio.h>
55
56 #include <sys/msgport2.h>
57
58 #include <net/if.h>
59 #include <net/ifq_var.h>
60 #include <net/netisr.h>
61 #include <net/route.h>
62
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_var.h>
68
69 #include "ipx.h"
70 #include "ipx_if.h"
71 #include "ipx_ip.h"
72 #include "ipx_var.h"
73
74 static struct   ifnet ipxipif;
75 static int      ipxipif_units;
76
77 /* list of all hosts and gateways or broadcast addrs */
78 static struct   ifnet_en *ipxip_list;
79
80 static  struct ifnet_en *ipxipattach(void);
81 static  int ipxip_free(struct ifnet *ifp);
82 static  int ipxipioctl(struct ifnet *ifp, u_long cmd, caddr_t data,
83                        struct ucred *cr);
84 static  int ipxipoutput(struct ifnet *ifp, struct mbuf *m,
85                         struct sockaddr *dst, struct rtentry *rt);
86 static  void ipxip_rtchange(struct in_addr *dst);
87 static  void ipxipstart(struct ifnet *ifp, struct ifaltq_subque *);
88
89 static struct ifnet_en *
90 ipxipattach(void)
91 {
92         struct ifnet_en *m;
93         struct ifnet *ifp;
94
95         if (ipxipif.if_mtu == 0) {
96                 ifp = &ipxipif;
97                 if_initname(ifp, "ipxip", ipxipif_units);
98                 ifp->if_mtu = LOMTU;
99                 ifp->if_ioctl = ipxipioctl;
100                 ifp->if_output = ipxipoutput;
101                 ifp->if_start = ipxipstart;
102                 ifp->if_flags = IFF_POINTOPOINT;
103         }
104
105         (m) = kmalloc(sizeof(*m), M_PCB, M_WAITOK | M_ZERO);
106         m->ifen_next = ipxip_list;
107         ipxip_list = m;
108         ifp = &m->ifen_ifnet;
109
110         if_initname(ifp, "ipxip", ipxipif_units++);
111         ifp->if_mtu = LOMTU;
112         ifp->if_ioctl = ipxipioctl;
113         ifp->if_output = ipxipoutput;
114         ifp->if_start = ipxipstart;
115         ifp->if_flags = IFF_POINTOPOINT;
116         if_attach(ifp, NULL);
117
118         return (m);
119 }
120
121
122 /*
123  * Process an ioctl request.
124  */
125 static int
126 ipxipioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
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 = ipxip_free(ifp);
147
148
149         default:
150                 error = EINVAL;
151         }
152         return (error);
153 }
154
155 static struct mbuf *ipxip_badlen;
156 static struct mbuf *ipxip_lastin;
157 static int ipxip_hold_input;
158
159 int
160 ipxip_input(struct mbuf **mp, int *offp, int proto)
161 {
162         struct mbuf *m = *mp;
163         struct ip *ip;
164         struct ipx *ipx;
165         int len, s;
166
167         if (ipxip_hold_input) {
168                 if (ipxip_lastin != NULL) {
169                         m_freem(ipxip_lastin);
170                 }
171                 ipxip_lastin = m_copym(m, 0, (int)M_COPYALL, MB_DONTWAIT);
172         }
173         /*
174          * Get IP and IPX header together in first mbuf.
175          */
176         IFNET_STAT_INC(&ipxipif, ipackets, 1);
177         s = sizeof(struct ip) + sizeof(struct ipx);
178         if (((m->m_flags & M_EXT) || m->m_len < s) &&
179             (m = m_pullup(m, s)) == NULL) {
180                 IFNET_STAT_INC(&ipxipif, ierrors, 1);
181                 return(IPPROTO_DONE);
182         }
183         ip = mtod(m, struct ip *);
184         if (ip->ip_hl > (sizeof(struct ip) >> 2)) {
185                 ip_stripoptions(m);
186                 if (m->m_len < s) {
187                         if ((m = m_pullup(m, s)) == NULL) {
188                                 IFNET_STAT_INC(&ipxipif, ierrors, 1);
189                                 return(IPPROTO_DONE);
190                         }
191                         ip = mtod(m, struct ip *);
192                 }
193         }
194
195         /*
196          * Make mbuf data length reflect IPX length.
197          * If not enough data to reflect IPX length, drop.
198          */
199         m->m_data += sizeof(struct ip);
200         m->m_len -= sizeof(struct ip);
201         m->m_pkthdr.len -= sizeof(struct ip);
202         ipx = mtod(m, struct ipx *);
203         len = ntohs(ipx->ipx_len);
204         if (len & 1)
205                 len++;          /* Preserve Garbage Byte */
206         if (ip->ip_len != len) {
207                 if (len > ip->ip_len) {
208                         IFNET_STAT_INC(&ipxipif, ierrors, 1);
209                         if (ipxip_badlen)
210                                 m_freem(ipxip_badlen);
211                         ipxip_badlen = m;
212                         return(IPPROTO_DONE);
213                 }
214                 /* Any extra will be trimmed off by the IPX routines */
215         }
216
217         /*
218          * Deliver to IPX
219          */
220         netisr_queue(NETISR_IPX, m);
221         return(IPPROTO_DONE);
222 }
223
224 static int
225 ipxipoutput_serialized(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
226                        struct rtentry *rt)
227 {
228         struct ifnet_en *ifn = (struct ifnet_en *)ifp;
229         struct ip *ip;
230         struct route *ro = &(ifn->ifen_route);
231         int len = 0;
232         struct ipx *ipx = mtod(m, struct ipx *);
233         int error;
234
235         IFNET_STAT_INC(&ifn->ifen_ifnet, opackets, 1);
236         IFNET_STAT_INC(&ipxipif, opackets, 1);
237
238         /*
239          * Calculate data length and make space
240          * for IP header.
241          */
242         len =  ntohs(ipx->ipx_len);
243         if (len & 1)
244                 len++;          /* Preserve Garbage Byte */
245         /* following clause not necessary on vax */
246         if (3 & (intptr_t)m->m_data) {
247                 /* force longword alignment of ip hdr */
248                 struct mbuf *m0 = m_gethdr(MT_HEADER, MB_DONTWAIT);
249                 if (m0 == NULL) {
250                         m_freem(m);
251                         return (ENOBUFS);
252                 }
253                 MH_ALIGN(m0, sizeof(struct ip));
254                 m0->m_flags = m->m_flags & M_COPYFLAGS;
255                 m0->m_next = m;
256                 m0->m_len = sizeof(struct ip);
257                 m0->m_pkthdr.len = m0->m_len + m->m_len;
258                 m = m0;
259         } else {
260                 M_PREPEND(m, sizeof(struct ip), MB_DONTWAIT);
261                 if (m == NULL)
262                         return (ENOBUFS);
263         }
264         /*
265          * Fill in IP header.
266          */
267         ip = mtod(m, struct ip *);
268         *(long *)ip = 0;
269         ip->ip_p = IPPROTO_IDP;
270         ip->ip_src = ifn->ifen_src;
271         ip->ip_dst = ifn->ifen_dst;
272         ip->ip_len = (u_short)len + sizeof(struct ip);
273         ip->ip_ttl = MAXTTL;
274
275         /*
276          * Output final datagram.
277          */
278         error =  (ip_output(m, NULL, ro, SO_BROADCAST, NULL, NULL));
279         if (error) {
280                 IFNET_STAT_INC(&ifn->ifen_ifnet, oerrors, 1);
281                 IFNET_STAT_SET(&ifn->ifen_ifnet, ierrors, error);
282         }
283         return (error);
284         m_freem(m);
285         return (ENETUNREACH);
286 }
287
288 static int
289 ipxipoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
290             struct rtentry *rt)
291 {
292         struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
293         int error;
294
295         ifsq_serialize_hw(ifsq);
296         error = ipxipoutput_serialized(ifp, m, dst, rt);
297         ifsq_deserialize_hw(ifsq);
298
299         return error;
300 }
301
302 static void
303 ipxipstart(struct ifnet *ifp, struct ifaltq_subque *ifsq __unused)
304 {
305         panic("ipxip_start called");
306 }
307
308 static struct ifreq ifr_ipxip = {"ipxip0"};
309
310 int
311 ipxip_route(struct socket *so, 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 in_ifaddr_container *iac;
349                 struct ifnet *ifp = ro.ro_rt->rt_ifp;
350
351                 ia = NULL;
352                 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
353                         if (iac->ia->ia_ifp == ifp) {
354                                 ia = iac->ia;
355                                 break;
356                         }
357                 }
358                 if (ia == NULL && !TAILQ_EMPTY(&in_ifaddrheads[mycpuid]))
359                         ia = TAILQ_FIRST(&in_ifaddrheads[mycpuid])->ia;
360                 if (ia == NULL) {
361                         RTFREE(ro.ro_rt);
362                         return (EADDRNOTAVAIL);
363                 }
364                 src = (struct sockaddr_in *)&ia->ia_addr;
365         }
366
367         /*
368          * Is there a free (pseudo-)interface or space?
369          */
370         for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
371                 if ((ifn->ifen_ifnet.if_flags & IFF_UP) == 0)
372                         break;
373         }
374         if (ifn == NULL)
375                 ifn = ipxipattach();
376         if (ifn == NULL) {
377                 RTFREE(ro.ro_rt);
378                 return (ENOBUFS);
379         }
380         ifn->ifen_route = ro;
381         ifn->ifen_dst =  ip_dst->sin_addr;
382         ifn->ifen_src = src->sin_addr;
383
384         /*
385          * now configure this as a point to point link
386          */
387         ifr_ipxip.ifr_name[4] = '0' + ipxipif_units - 1;
388         ifr_ipxip.ifr_dstaddr = *(struct sockaddr *)ipx_dst;
389         ipx_control_oncpu(so, (int)SIOCSIFDSTADDR, (caddr_t)&ifr_ipxip,
390                         (struct ifnet *)ifn, sopt->sopt_td);
391
392         /* use any of our addresses */
393         satoipx_addr(ifr_ipxip.ifr_addr).x_host = 
394                         ipx_ifaddr->ia_addr.sipx_addr.x_host;
395
396         return (ipx_control_oncpu(so, (int)SIOCSIFADDR, (caddr_t)&ifr_ipxip,
397                         (struct ifnet *)ifn, sopt->sopt_td));
398 }
399
400 static int
401 ipxip_free(struct ifnet *ifp)
402 {
403         struct ifnet_en *ifn = (struct ifnet_en *)ifp;
404         struct route *ro = & ifn->ifen_route;
405
406         if (ro->ro_rt != NULL) {
407                 RTFREE(ro->ro_rt);
408                 ro->ro_rt = NULL;
409         }
410         ifp->if_flags &= ~IFF_UP;
411         return (0);
412 }
413
414 void
415 ipxip_ctlinput(netmsg_t msg)
416 {
417         int cmd = msg->ctlinput.nm_cmd;
418         struct sockaddr *sa = msg->ctlinput.nm_arg;
419         struct sockaddr_in *sin;
420
421         if ((unsigned)cmd >= PRC_NCMDS)
422                 goto out;
423         if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
424                 goto out;
425         sin = (struct sockaddr_in *)sa;
426         if (sin->sin_addr.s_addr == INADDR_ANY)
427                 goto out;
428
429         switch (cmd) {
430
431         case PRC_ROUTEDEAD:
432         case PRC_REDIRECT_NET:
433         case PRC_REDIRECT_HOST:
434         case PRC_REDIRECT_TOSNET:
435         case PRC_REDIRECT_TOSHOST:
436                 ipxip_rtchange(&sin->sin_addr);
437                 break;
438         }
439 out:
440         lwkt_replymsg(&msg->lmsg, 0);
441 }
442
443 static void
444 ipxip_rtchange(struct in_addr *dst)
445 {
446         struct ifnet_en *ifn;
447
448         for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
449                 if (ifn->ifen_dst.s_addr == dst->s_addr &&
450                         ifn->ifen_route.ro_rt != NULL) {
451                                 RTFREE(ifn->ifen_route.ro_rt);
452                                 ifn->ifen_route.ro_rt = NULL;
453                 }
454         }
455 }
456 #endif /* IPXIP */