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