]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netipx/ipx_ip.c
This commit was generated by cvs2svn to compensate for changes in r153667,
[FreeBSD/FreeBSD.git] / sys / netipx / 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
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
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/if_types.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 #include <netinet/ip_options.h>
72
73 #include <netipx/ipx.h>
74 #include <netipx/ipx_if.h>
75 #include <netipx/ipx_ip.h>
76 #include <netipx/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 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()
94 {
95         register struct ifnet_en *m;
96         register 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         MALLOC((m), struct ifnet_en *, sizeof(*m), M_PCB, M_NOWAIT | M_ZERO);
109         if (m == NULL)
110                 return (NULL);
111         m->ifen_next = ipxip_list;
112         ipxip_list = m;
113         ifp = m->ifen_ifp = if_alloc(IFT_IPXIP);
114         if (ifp == NULL) {
115                 FREE(m, M_PCB);
116                 return (NULL);
117         }
118
119         if_initname(ifp, "ipxip", ipxipif_units++);
120         ifp->if_mtu = LOMTU;
121         ifp->if_ioctl = ipxipioctl;
122         ifp->if_output = ipxipoutput;
123         ifp->if_start = ipxipstart;
124         ifp->if_flags = IFF_POINTOPOINT;
125         ifp->if_softc = m;
126         if_attach(ifp);
127
128         return (m);
129 }
130
131
132 /*
133  * Process an ioctl request.
134  */
135 static int
136 ipxipioctl(ifp, cmd, data)
137         register struct ifnet *ifp;
138         u_long cmd;
139         caddr_t data;
140 {
141         int error = 0;
142         struct ifreq *ifr;
143
144         switch (cmd) {
145
146         case SIOCSIFADDR:
147                 ifp->if_flags |= IFF_UP;
148                 /* FALLTHROUGH */
149
150         case SIOCSIFDSTADDR:
151                 /*
152                  * Everything else is done at a higher level.
153                  */
154                 break;
155
156         case SIOCSIFFLAGS:
157                 ifr = (struct ifreq *)data;
158                 if ((ifr->ifr_flags & IFF_UP) == 0)
159                         error = ipxip_free(ifp);
160
161
162         default:
163                 error = EINVAL;
164         }
165         return (error);
166 }
167
168 static struct mbuf *ipxip_badlen;
169 static struct mbuf *ipxip_lastin;
170 static int ipxip_hold_input;
171
172 void
173 ipxip_input(m, hlen)
174         register struct mbuf *m;
175         int hlen;
176 {
177         register struct ip *ip;
178         register struct ipx *ipx;
179         int len, s;
180
181         if (ipxip_hold_input) {
182                 if (ipxip_lastin != NULL) {
183                         m_freem(ipxip_lastin);
184                 }
185                 ipxip_lastin = m_copym(m, 0, (int)M_COPYALL, M_DONTWAIT);
186         }
187         /*
188          * Get IP and IPX header together in first mbuf.
189          */
190         ipxipif.if_ipackets++;
191         s = sizeof(struct ip) + sizeof(struct ipx);
192         if (((m->m_flags & M_EXT) || m->m_len < s) &&
193             (m = m_pullup(m, s)) == NULL) {
194                 ipxipif.if_ierrors++;
195                 return;
196         }
197         ip = mtod(m, struct ip *);
198         if (ip->ip_hl > (sizeof(struct ip) >> 2)) {
199                 ip_stripoptions(m, (struct mbuf *)NULL);
200                 if (m->m_len < s) {
201                         if ((m = m_pullup(m, s)) == NULL) {
202                                 ipxipif.if_ierrors++;
203                                 return;
204                         }
205                         ip = mtod(m, struct ip *);
206                 }
207         }
208
209         /*
210          * Make mbuf data length reflect IPX length.
211          * If not enough data to reflect IPX length, drop.
212          */
213         m->m_data += sizeof(struct ip);
214         m->m_len -= sizeof(struct ip);
215         m->m_pkthdr.len -= sizeof(struct ip);
216         ipx = mtod(m, struct ipx *);
217         len = ntohs(ipx->ipx_len);
218         if (len & 1)
219                 len++;          /* Preserve Garbage Byte */
220         if (ip->ip_len != len) {
221                 if (len > ip->ip_len) {
222                         ipxipif.if_ierrors++;
223                         if (ipxip_badlen)
224                                 m_freem(ipxip_badlen);
225                         ipxip_badlen = m;
226                         return;
227                 }
228                 /* Any extra will be trimmed off by the IPX routines */
229         }
230
231         /*
232          * Deliver to IPX
233          */
234         netisr_dispatch(NETISR_IPX, m);
235 }
236
237 static int
238 ipxipoutput(ifp, m, dst, rt)
239         struct ifnet *ifp;
240         struct mbuf *m;
241         struct sockaddr *dst;
242         struct rtentry *rt;
243 {
244         register struct ifnet_en *ifn = (struct ifnet_en *)ifp->if_softc;
245         register struct ip *ip;
246         register struct route *ro = &(ifn->ifen_route);
247         register int len = 0;
248         register struct ipx *ipx = mtod(m, struct ipx *);
249         int error;
250
251         ifn->ifen_ifp->if_opackets++;
252         ipxipif.if_opackets++;
253
254         /*
255          * Calculate data length and make space
256          * for IP header.
257          */
258         len =  ntohs(ipx->ipx_len);
259         if (len & 1)
260                 len++;          /* Preserve Garbage Byte */
261         /* following clause not necessary on vax */
262         if (3 & (intptr_t)m->m_data) {
263                 /* force longword alignment of ip hdr */
264                 struct mbuf *m0 = m_gethdr(MT_DATA, M_DONTWAIT);
265                 if (m0 == NULL) {
266                         m_freem(m);
267                         return (ENOBUFS);
268                 }
269                 MH_ALIGN(m0, sizeof(struct ip));
270                 m0->m_flags = m->m_flags & M_COPYFLAGS;
271                 m0->m_next = m;
272                 m0->m_len = sizeof(struct ip);
273                 m0->m_pkthdr.len = m0->m_len + m->m_len;
274                 m->m_flags &= ~M_PKTHDR;
275                 m = m0;
276         } else {
277                 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
278                 if (m == NULL)
279                         return (ENOBUFS);
280         }
281         /*
282          * Fill in IP header.
283          */
284         ip = mtod(m, struct ip *);
285         *(long *)ip = 0;
286         ip->ip_p = IPPROTO_IDP;
287         ip->ip_src = ifn->ifen_src;
288         ip->ip_dst = ifn->ifen_dst;
289         ip->ip_len = (u_short)len + sizeof(struct ip);
290         ip->ip_ttl = MAXTTL;
291
292         /*
293          * Output final datagram.
294          */
295         error =  (ip_output(m, (struct mbuf *)NULL, ro, SO_BROADCAST, NULL, NULL));
296         if (error) {
297                 ifn->ifen_ifp->if_oerrors++;
298                 ifn->ifen_ifp->if_ierrors = error;
299         }
300         return (error);
301         m_freem(m);
302         return (ENETUNREACH);
303 }
304
305 static void
306 ipxipstart(ifp)
307 struct ifnet *ifp;
308 {
309         panic("ipxip_start called\n");
310 }
311
312 static struct ifreq ifr_ipxip = {"ipxip0"};
313
314 int
315 ipxip_route(so, sopt)
316         struct socket *so;
317         struct sockopt *sopt;
318 {
319         int error;
320         struct ifnet_en *ifn;
321         struct sockaddr_in *src;
322         struct ipxip_req rq;
323         struct sockaddr_ipx *ipx_dst;
324         struct sockaddr_in *ip_dst;
325         struct route ro;
326
327         error = sooptcopyin(sopt, &rq, sizeof rq, sizeof rq);
328         if (error)
329                 return (error);
330         ipx_dst = (struct sockaddr_ipx *)&rq.rq_ipx;
331         ip_dst = (struct sockaddr_in *)&rq.rq_ip;
332
333         /*
334          * First, make sure we already have an IPX address:
335          */
336         if (ipx_ifaddr == NULL)
337                 return (EADDRNOTAVAIL);
338         /*
339          * Now, determine if we can get to the destination
340          */
341         bzero((caddr_t)&ro, sizeof(ro));
342         ro.ro_dst = *(struct sockaddr *)ip_dst;
343         rtalloc_ign(&ro, 0);
344         if (ro.ro_rt == NULL || ro.ro_rt->rt_ifp == NULL) {
345                 return (ENETUNREACH);
346         }
347
348         /*
349          * And see how he's going to get back to us:
350          * i.e., what return ip address do we use?
351          */
352         {
353                 register struct in_ifaddr *ia;
354                 struct ifnet *ifp = ro.ro_rt->rt_ifp;
355
356                 for (ia = TAILQ_FIRST(&in_ifaddrhead); ia != NULL;
357                      ia = TAILQ_NEXT(ia, ia_link))
358                         if (ia->ia_ifp == ifp)
359                                 break;
360                 if (ia == NULL)
361                         ia = TAILQ_FIRST(&in_ifaddrhead);
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_ifp->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(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(so, (int)SIOCSIFADDR, (caddr_t)&ifr_ipxip,
399                         (struct ifnet *)ifn, sopt->sopt_td));
400 }
401
402 static int
403 ipxip_free(ifp)
404 struct ifnet *ifp;
405 {
406         register struct ifnet_en *ifn = (struct ifnet_en *)ifp->if_softc;
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(cmd, sa, dummy)
419         int cmd;
420         struct sockaddr *sa;
421         void *dummy;
422 {
423         struct sockaddr_in *sin;
424
425         if ((unsigned)cmd >= PRC_NCMDS)
426                 return;
427         if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
428                 return;
429         sin = (struct sockaddr_in *)sa;
430         if (sin->sin_addr.s_addr == INADDR_ANY)
431                 return;
432
433         switch (cmd) {
434
435         case PRC_ROUTEDEAD:
436         case PRC_REDIRECT_NET:
437         case PRC_REDIRECT_HOST:
438         case PRC_REDIRECT_TOSNET:
439         case PRC_REDIRECT_TOSHOST:
440                 ipxip_rtchange(&sin->sin_addr);
441                 break;
442         }
443 }
444
445 static void
446 ipxip_rtchange(dst)
447         register struct in_addr *dst;
448 {
449         register 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 */