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