LINT pass. Cleanup missed proc->thread conversions and get rid of warnings.
[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.4 2003/07/23 02:30:22 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 <netipx/ipx.h>
72 #include <netipx/ipx_if.h>
73 #include <netipx/ipx_ip.h>
74 #include <netipx/ipx_var.h>
75
76 static struct   ifnet ipxipif;
77
78 /* list of all hosts and gateways or broadcast addrs */
79 static struct   ifnet_en *ipxip_list;
80
81 static  struct ifnet_en *ipxipattach(void);
82 static  int ipxip_free(struct ifnet *ifp);
83 static  int ipxipioctl(struct ifnet *ifp, u_long cmd, caddr_t data);
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);
88
89 static struct ifnet_en *
90 ipxipattach()
91 {
92         register struct ifnet_en *m;
93         register struct ifnet *ifp;
94
95         if (ipxipif.if_mtu == 0) {
96                 ifp = &ipxipif;
97                 ifp->if_name = "ipxip";
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         MALLOC((m), struct ifnet_en *, sizeof(*m), M_PCB, M_NOWAIT | M_ZERO);
106         if (m == NULL)
107                 return (NULL);
108         m->ifen_next = ipxip_list;
109         ipxip_list = m;
110         ifp = &m->ifen_ifnet;
111
112         ifp->if_name = "ipxip";
113         ifp->if_mtu = LOMTU;
114         ifp->if_ioctl = ipxipioctl;
115         ifp->if_output = ipxipoutput;
116         ifp->if_start = ipxipstart;
117         ifp->if_flags = IFF_POINTOPOINT;
118         ifp->if_unit = ipxipif.if_unit++;
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         register 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         register struct ip *ip;
172         register struct ipx *ipx;
173         register struct ifqueue *ifq = &ipxintrq;
174         int len, s;
175
176         if (ipxip_hold_input) {
177                 if (ipxip_lastin != NULL) {
178                         m_freem(ipxip_lastin);
179                 }
180                 ipxip_lastin = m_copym(m, 0, (int)M_COPYALL, M_DONTWAIT);
181         }
182         /*
183          * Get IP and IPX header together in first mbuf.
184          */
185         ipxipif.if_ipackets++;
186         s = sizeof(struct ip) + sizeof(struct ipx);
187         if (((m->m_flags & M_EXT) || m->m_len < s) &&
188             (m = m_pullup(m, s)) == NULL) {
189                 ipxipif.if_ierrors++;
190                 return;
191         }
192         ip = mtod(m, struct ip *);
193         if (ip->ip_hl > (sizeof(struct ip) >> 2)) {
194                 ip_stripoptions(m, (struct mbuf *)NULL);
195                 if (m->m_len < s) {
196                         if ((m = m_pullup(m, s)) == NULL) {
197                                 ipxipif.if_ierrors++;
198                                 return;
199                         }
200                         ip = mtod(m, struct ip *);
201                 }
202         }
203
204         /*
205          * Make mbuf data length reflect IPX length.
206          * If not enough data to reflect IPX length, drop.
207          */
208         m->m_data += sizeof(struct ip);
209         m->m_len -= sizeof(struct ip);
210         m->m_pkthdr.len -= sizeof(struct ip);
211         ipx = mtod(m, struct ipx *);
212         len = ntohs(ipx->ipx_len);
213         if (len & 1)
214                 len++;          /* Preserve Garbage Byte */
215         if (ip->ip_len != len) {
216                 if (len > ip->ip_len) {
217                         ipxipif.if_ierrors++;
218                         if (ipxip_badlen)
219                                 m_freem(ipxip_badlen);
220                         ipxip_badlen = m;
221                         return;
222                 }
223                 /* Any extra will be trimmed off by the IPX routines */
224         }
225
226         /*
227          * Deliver to IPX
228          */
229         s = splimp();
230         if (IF_QFULL(ifq)) {
231                 IF_DROP(ifq);
232                 m_freem(m);
233                 splx(s);
234                 return;
235         }
236         IF_ENQUEUE(ifq, m);
237         schednetisr(NETISR_IPX);
238         splx(s);
239         return;
240 }
241
242 static int
243 ipxipoutput(ifp, m, dst, rt)
244         struct ifnet *ifp;
245         struct mbuf *m;
246         struct sockaddr *dst;
247         struct rtentry *rt;
248 {
249         register struct ifnet_en *ifn = (struct ifnet_en *)ifp;
250         register struct ip *ip;
251         register struct route *ro = &(ifn->ifen_route);
252         register int len = 0;
253         register struct ipx *ipx = mtod(m, struct ipx *);
254         int error;
255
256         ifn->ifen_ifnet.if_opackets++;
257         ipxipif.if_opackets++;
258
259         /*
260          * Calculate data length and make space
261          * for IP header.
262          */
263         len =  ntohs(ipx->ipx_len);
264         if (len & 1)
265                 len++;          /* Preserve Garbage Byte */
266         /* following clause not necessary on vax */
267         if (3 & (int)m->m_data) {
268                 /* force longword alignment of ip hdr */
269                 struct mbuf *m0 = m_gethdr(MT_HEADER, M_DONTWAIT);
270                 if (m0 == NULL) {
271                         m_freem(m);
272                         return (ENOBUFS);
273                 }
274                 MH_ALIGN(m0, sizeof(struct ip));
275                 m0->m_flags = m->m_flags & M_COPYFLAGS;
276                 m0->m_next = m;
277                 m0->m_len = sizeof(struct ip);
278                 m0->m_pkthdr.len = m0->m_len + m->m_len;
279                 m->m_flags &= ~M_PKTHDR;
280                 m = m0;
281         } else {
282                 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
283                 if (m == NULL)
284                         return (ENOBUFS);
285         }
286         /*
287          * Fill in IP header.
288          */
289         ip = mtod(m, struct ip *);
290         *(long *)ip = 0;
291         ip->ip_p = IPPROTO_IDP;
292         ip->ip_src = ifn->ifen_src;
293         ip->ip_dst = ifn->ifen_dst;
294         ip->ip_len = (u_short)len + sizeof(struct ip);
295         ip->ip_ttl = MAXTTL;
296
297         /*
298          * Output final datagram.
299          */
300         error =  (ip_output(m, (struct mbuf *)NULL, ro, SO_BROADCAST, NULL, NULL));
301         if (error) {
302                 ifn->ifen_ifnet.if_oerrors++;
303                 ifn->ifen_ifnet.if_ierrors = error;
304         }
305         return (error);
306         m_freem(m);
307         return (ENETUNREACH);
308 }
309
310 static void
311 ipxipstart(ifp)
312 struct ifnet *ifp;
313 {
314         panic("ipxip_start called\n");
315 }
316
317 static struct ifreq ifr_ipxip = {"ipxip0"};
318
319 int
320 ipxip_route(so, sopt)
321         struct socket *so;
322         struct sockopt *sopt;
323 {
324         int error;
325         struct ifnet_en *ifn;
326         struct sockaddr_in *src;
327         struct ipxip_req rq;
328         struct sockaddr_ipx *ipx_dst;
329         struct sockaddr_in *ip_dst;
330         struct route ro;
331
332         error = sooptcopyin(sopt, &rq, sizeof rq, sizeof rq);
333         if (error)
334                 return (error);
335         ipx_dst = (struct sockaddr_ipx *)&rq.rq_ipx;
336         ip_dst = (struct sockaddr_in *)&rq.rq_ip;
337
338         /*
339          * First, make sure we already have an IPX address:
340          */
341         if (ipx_ifaddr == NULL)
342                 return (EADDRNOTAVAIL);
343         /*
344          * Now, determine if we can get to the destination
345          */
346         bzero((caddr_t)&ro, sizeof(ro));
347         ro.ro_dst = *(struct sockaddr *)ip_dst;
348         rtalloc(&ro);
349         if (ro.ro_rt == NULL || ro.ro_rt->rt_ifp == NULL) {
350                 return (ENETUNREACH);
351         }
352
353         /*
354          * And see how he's going to get back to us:
355          * i.e., what return ip address do we use?
356          */
357         {
358                 register struct in_ifaddr *ia;
359                 struct ifnet *ifp = ro.ro_rt->rt_ifp;
360
361                 for (ia = TAILQ_FIRST(&in_ifaddrhead); ia != NULL; 
362                      ia = TAILQ_NEXT(ia, ia_link))
363                         if (ia->ia_ifp == ifp)
364                                 break;
365                 if (ia == NULL)
366                         ia = TAILQ_FIRST(&in_ifaddrhead);
367                 if (ia == NULL) {
368                         RTFREE(ro.ro_rt);
369                         return (EADDRNOTAVAIL);
370                 }
371                 src = (struct sockaddr_in *)&ia->ia_addr;
372         }
373
374         /*
375          * Is there a free (pseudo-)interface or space?
376          */
377         for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
378                 if ((ifn->ifen_ifnet.if_flags & IFF_UP) == 0)
379                         break;
380         }
381         if (ifn == NULL)
382                 ifn = ipxipattach();
383         if (ifn == NULL) {
384                 RTFREE(ro.ro_rt);
385                 return (ENOBUFS);
386         }
387         ifn->ifen_route = ro;
388         ifn->ifen_dst =  ip_dst->sin_addr;
389         ifn->ifen_src = src->sin_addr;
390
391         /*
392          * now configure this as a point to point link
393          */
394         ifr_ipxip.ifr_name[4] = '0' + ipxipif.if_unit - 1;
395         ifr_ipxip.ifr_dstaddr = *(struct sockaddr *)ipx_dst;
396         ipx_control(so, (int)SIOCSIFDSTADDR, (caddr_t)&ifr_ipxip,
397                         (struct ifnet *)ifn, sopt->sopt_td);
398
399         /* use any of our addresses */
400         satoipx_addr(ifr_ipxip.ifr_addr).x_host = 
401                         ipx_ifaddr->ia_addr.sipx_addr.x_host;
402
403         return (ipx_control(so, (int)SIOCSIFADDR, (caddr_t)&ifr_ipxip,
404                         (struct ifnet *)ifn, sopt->sopt_td));
405 }
406
407 static int
408 ipxip_free(ifp)
409 struct ifnet *ifp;
410 {
411         register struct ifnet_en *ifn = (struct ifnet_en *)ifp;
412         struct route *ro = & ifn->ifen_route;
413
414         if (ro->ro_rt != NULL) {
415                 RTFREE(ro->ro_rt);
416                 ro->ro_rt = NULL;
417         }
418         ifp->if_flags &= ~IFF_UP;
419         return (0);
420 }
421
422 void
423 ipxip_ctlinput(cmd, sa, dummy)
424         int cmd;
425         struct sockaddr *sa;
426         void *dummy;
427 {
428         struct sockaddr_in *sin;
429
430         if ((unsigned)cmd >= PRC_NCMDS)
431                 return;
432         if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
433                 return;
434         sin = (struct sockaddr_in *)sa;
435         if (sin->sin_addr.s_addr == INADDR_ANY)
436                 return;
437
438         switch (cmd) {
439
440         case PRC_ROUTEDEAD:
441         case PRC_REDIRECT_NET:
442         case PRC_REDIRECT_HOST:
443         case PRC_REDIRECT_TOSNET:
444         case PRC_REDIRECT_TOSHOST:
445                 ipxip_rtchange(&sin->sin_addr);
446                 break;
447         }
448 }
449
450 static void
451 ipxip_rtchange(dst)
452         register struct in_addr *dst;
453 {
454         register struct ifnet_en *ifn;
455
456         for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
457                 if (ifn->ifen_dst.s_addr == dst->s_addr &&
458                         ifn->ifen_route.ro_rt != NULL) {
459                                 RTFREE(ifn->ifen_route.ro_rt);
460                                 ifn->ifen_route.ro_rt = NULL;
461                 }
462         }
463 }
464 #endif /* IPXIP */