]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netipx/ipx_ip.c
Improve ipx_ip.c's approximation of style(9).
[FreeBSD/FreeBSD.git] / sys / netipx / ipx_ip.c
1 /*-
2  * Copyright (c) 1984, 1985, 1986, 1987, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * Copyright (c) 1995, Mike Mitchell
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  * 1. Redistributions of source code must retain the above copyright
35  *    notice, this list of conditions and the following disclaimer.
36  * 2. Redistributions in binary form must reproduce the above copyright
37  *    notice, this list of conditions and the following disclaimer in the
38  *    documentation and/or other materials provided with the distribution.
39  * 3. All advertising materials mentioning features or use of this software
40  *    must display the following acknowledgement:
41  *      This product includes software developed by the University of
42  *      California, Berkeley and its contributors.
43  * 4. Neither the name of the University nor the names of its contributors
44  *    may be used to endorse or promote products derived from this software
45  *    without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57  * SUCH DAMAGE.
58  *
59  *      @(#)ipx_ip.c
60  */
61
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
64
65 /*
66  * Software interface driver for encapsulating IPX in IP.
67  */
68
69 #include "opt_inet.h"
70 #include "opt_ipx.h"
71
72 #ifdef IPXIP
73 #ifndef INET
74 #error The option IPXIP requires option INET.
75 #endif
76
77 #include <sys/param.h>
78 #include <sys/kernel.h>
79 #include <sys/systm.h>
80 #include <sys/malloc.h>
81 #include <sys/mbuf.h>
82 #include <sys/protosw.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/sockio.h>
86
87 #include <net/if.h>
88 #include <net/if_types.h>
89 #include <net/netisr.h>
90 #include <net/route.h>
91
92 #include <netinet/in.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/in_var.h>
95 #include <netinet/ip.h>
96 #include <netinet/ip_var.h>
97 #include <netinet/ip_options.h>
98
99 #include <netipx/ipx.h>
100 #include <netipx/ipx_if.h>
101 #include <netipx/ipx_ip.h>
102 #include <netipx/ipx_var.h>
103
104 NET_NEEDS_GIANT("ipx_ip");
105
106 static struct            ifnet ipxipif;
107 static int               ipxipif_units;
108
109 /* List of all hosts and gateways or broadcast addrs. */
110 static struct ifnet_en  *ipxip_list;
111
112 static struct ifreq      ifr_ipxip = {"ipxip0"};
113 static struct mbuf      *ipxip_badlen;
114 static struct mbuf      *ipxip_lastin;
115 static int               ipxip_hold_input;
116
117 static struct ifnet_en  *ipxipattach(void);
118 static int               ipxip_free(struct ifnet *ifp);
119 static int               ipxipioctl(struct ifnet *ifp, u_long cmd,
120                             caddr_t data);
121 static int               ipxipoutput(struct ifnet *ifp, struct mbuf *m,
122                             struct sockaddr *dst, struct rtentry *rt);
123 static void              ipxip_rtchange(struct in_addr *dst);
124 static void              ipxipstart(struct ifnet *ifp);
125
126 static struct ifnet_en *
127 ipxipattach(void)
128 {
129         struct ifnet_en *m;
130         struct ifnet *ifp;
131
132         if (ipxipif.if_mtu == 0) {
133                 ifp = &ipxipif;
134                 if_initname(ifp, "ipxip", ipxipif_units);
135                 ifp->if_mtu = LOMTU;
136                 ifp->if_ioctl = ipxipioctl;
137                 ifp->if_output = ipxipoutput;
138                 ifp->if_start = ipxipstart;
139                 ifp->if_flags = IFF_POINTOPOINT;
140         }
141
142         MALLOC((m), struct ifnet_en *, sizeof(*m), M_PCB, M_NOWAIT | M_ZERO);
143         if (m == NULL)
144                 return (NULL);
145         m->ifen_next = ipxip_list;
146         ipxip_list = m;
147         ifp = m->ifen_ifp = if_alloc(IFT_IPXIP);
148         if (ifp == NULL) {
149                 FREE(m, M_PCB);
150                 return (NULL);
151         }
152
153         if_initname(ifp, "ipxip", ipxipif_units++);
154         ifp->if_mtu = LOMTU;
155         ifp->if_ioctl = ipxipioctl;
156         ifp->if_output = ipxipoutput;
157         ifp->if_start = ipxipstart;
158         ifp->if_flags = IFF_POINTOPOINT;
159         ifp->if_softc = m;
160         if_attach(ifp);
161
162         return (m);
163 }
164
165 /*
166  * Process an ioctl request.
167  */
168 static int
169 ipxipioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
170 {
171         int error = 0;
172         struct ifreq *ifr;
173
174         switch (cmd) {
175         case SIOCSIFADDR:
176                 ifp->if_flags |= IFF_UP;
177                 /* FALLTHROUGH */
178
179         case SIOCSIFDSTADDR:
180                 /*
181                  * Everything else is done at a higher level.
182                  */
183                 break;
184
185         case SIOCSIFFLAGS:
186                 ifr = (struct ifreq *)data;
187                 if ((ifr->ifr_flags & IFF_UP) == 0)
188                         error = ipxip_free(ifp);
189
190         default:
191                 error = EINVAL;
192         }
193         return (error);
194 }
195
196 void
197 ipxip_input(struct mbuf *m, int hlen)
198 {
199         struct ip *ip;
200         struct ipx *ipx;
201         int len, s;
202
203         if (ipxip_hold_input) {
204                 if (ipxip_lastin != NULL)
205                         m_freem(ipxip_lastin);
206                 ipxip_lastin = m_copym(m, 0, (int)M_COPYALL, M_DONTWAIT);
207         }
208
209         /*
210          * Get IP and IPX header together in first mbuf.
211          */
212         ipxipif.if_ipackets++;
213         s = sizeof(struct ip) + sizeof(struct ipx);
214         if (((m->m_flags & M_EXT) || m->m_len < s) &&
215             (m = m_pullup(m, s)) == NULL) {
216                 ipxipif.if_ierrors++;
217                 return;
218         }
219         ip = mtod(m, struct ip *);
220         if (ip->ip_hl > (sizeof(struct ip) >> 2)) {
221                 ip_stripoptions(m, (struct mbuf *)NULL);
222                 if (m->m_len < s) {
223                         if ((m = m_pullup(m, s)) == NULL) {
224                                 ipxipif.if_ierrors++;
225                                 return;
226                         }
227                         ip = mtod(m, struct ip *);
228                 }
229         }
230
231         /*
232          * Make mbuf data length reflect IPX length.  If not enough data to
233          * reflect IPX length, drop.
234          */
235         m->m_data += sizeof(struct ip);
236         m->m_len -= sizeof(struct ip);
237         m->m_pkthdr.len -= sizeof(struct ip);
238         ipx = mtod(m, struct ipx *);
239         len = ntohs(ipx->ipx_len);
240         if (len & 1)
241                 len++;          /* Preserve Garbage Byte */
242         if (ip->ip_len != len) {
243                 if (len > ip->ip_len) {
244                         ipxipif.if_ierrors++;
245                         if (ipxip_badlen)
246                                 m_freem(ipxip_badlen);
247                         ipxip_badlen = m;
248                         return;
249                 }
250                 /*
251                  * Any extra will be trimmed off by the IPX routines.
252                  */
253         }
254
255         /*
256          * Deliver to IPX.
257          */
258         netisr_dispatch(NETISR_IPX, m);
259 }
260
261 static int
262 ipxipoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
263     struct rtentry *rt)
264 {
265         struct ifnet_en *ifn = (struct ifnet_en *)ifp->if_softc;
266         struct route *ro = &(ifn->ifen_route);
267         struct ipx *ipx = mtod(m, struct ipx *);
268         struct ip *ip;
269         int len = 0;
270         int error;
271
272         ifn->ifen_ifp->if_opackets++;
273         ipxipif.if_opackets++;
274
275         /*
276          * Calculate data length and make space for IP header.
277          */
278         len =  ntohs(ipx->ipx_len);
279         if (len & 1)
280                 len++;          /* Preserve Garbage Byte */
281
282         /*
283          * Following clause not necessary on VAX.
284          */
285         if (3 & (intptr_t)m->m_data) {
286                 /* force longword alignment of ip hdr */
287                 struct mbuf *m0 = m_gethdr(MT_DATA, M_DONTWAIT);
288                 if (m0 == NULL) {
289                         m_freem(m);
290                         return (ENOBUFS);
291                 }
292                 MH_ALIGN(m0, sizeof(struct ip));
293                 m0->m_flags = m->m_flags & M_COPYFLAGS;
294                 m0->m_next = m;
295                 m0->m_len = sizeof(struct ip);
296                 m0->m_pkthdr.len = m0->m_len + m->m_len;
297                 m->m_flags &= ~M_PKTHDR;
298                 m = m0;
299         } else {
300                 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
301                 if (m == NULL)
302                         return (ENOBUFS);
303         }
304
305         /*
306          * Fill in IP header.
307          */
308         ip = mtod(m, struct ip *);
309         *(long *)ip = 0;
310         ip->ip_p = IPPROTO_IDP;
311         ip->ip_src = ifn->ifen_src;
312         ip->ip_dst = ifn->ifen_dst;
313         ip->ip_len = (u_short)len + sizeof(struct ip);
314         ip->ip_ttl = MAXTTL;
315
316         /*
317          * Output final datagram.
318          */
319         error =  ip_output(m, (struct mbuf *)NULL, ro, SO_BROADCAST, NULL,
320             NULL);
321         if (error) {
322                 ifn->ifen_ifp->if_oerrors++;
323                 ifn->ifen_ifp->if_ierrors = error;
324         }
325         return (error);
326 }
327
328 static void
329 ipxipstart(struct ifnet *ifp)
330 {
331
332         panic("ipxip_start called\n");
333 }
334
335 int
336 ipxip_route(struct socket *so, struct sockopt *sopt)
337 {
338         struct in_ifaddr *ia;
339         struct ifnet_en *ifn;
340         struct sockaddr_in *src;
341         struct ipxip_req rq;
342         struct sockaddr_ipx *ipx_dst;
343         struct sockaddr_in *ip_dst;
344         struct ifnet *ifp;
345         struct route ro;
346         int error;
347
348         error = sooptcopyin(sopt, &rq, sizeof rq, sizeof rq);
349         if (error)
350                 return (error);
351         ipx_dst = (struct sockaddr_ipx *)&rq.rq_ipx;
352         ip_dst = (struct sockaddr_in *)&rq.rq_ip;
353
354         /*
355          * First, make sure we already have an IPX address:
356          */
357         if (ipx_ifaddr == NULL)
358                 return (EADDRNOTAVAIL);
359
360         /*
361          * Now, determine if we can get to the destination.
362          */
363         bzero((caddr_t)&ro, sizeof(ro));
364         ro.ro_dst = *(struct sockaddr *)ip_dst;
365         rtalloc_ign(&ro, 0);
366         if (ro.ro_rt == NULL || ro.ro_rt->rt_ifp == NULL)
367                 return (ENETUNREACH);
368
369         /*
370          * And see how he's going to get back to us: i.e., what return IP
371          * address do we use?
372          */
373         ifp = ro.ro_rt->rt_ifp;
374         for (ia = TAILQ_FIRST(&in_ifaddrhead); ia != NULL;
375              ia = TAILQ_NEXT(ia, ia_link))
376                 if (ia->ia_ifp == ifp)
377                         break;
378         if (ia == NULL)
379                 ia = TAILQ_FIRST(&in_ifaddrhead);
380         if (ia == NULL) {
381                 RTFREE(ro.ro_rt);
382                 return (EADDRNOTAVAIL);
383         }
384         src = (struct sockaddr_in *)&ia->ia_addr;
385
386         /*
387          * Is there a free (pseudo-)interface or space?
388          */
389         for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
390                 if ((ifn->ifen_ifp->if_flags & IFF_UP) == 0)
391                         break;
392         }
393         if (ifn == NULL)
394                 ifn = ipxipattach();
395         if (ifn == NULL) {
396                 RTFREE(ro.ro_rt);
397                 return (ENOBUFS);
398         }
399         ifn->ifen_route = ro;
400         ifn->ifen_dst =  ip_dst->sin_addr;
401         ifn->ifen_src = src->sin_addr;
402
403         /*
404          * Now configure this as a point to point link.
405          */
406         ifr_ipxip.ifr_name[4] = '0' + ipxipif_units - 1;
407         ifr_ipxip.ifr_dstaddr = *(struct sockaddr *)ipx_dst;
408         ipx_control(so, (int)SIOCSIFDSTADDR, (caddr_t)&ifr_ipxip,
409                         (struct ifnet *)ifn, sopt->sopt_td);
410
411         /*
412          * Use any of our addresses.
413          */
414         satoipx_addr(ifr_ipxip.ifr_addr).x_host =
415             ipx_ifaddr->ia_addr.sipx_addr.x_host;
416
417         return (ipx_control(so, (int)SIOCSIFADDR, (caddr_t)&ifr_ipxip,
418             (struct ifnet *)ifn, sopt->sopt_td));
419 }
420
421 static int
422 ipxip_free(struct ifnet *ifp)
423 {
424         struct ifnet_en *ifn = (struct ifnet_en *)ifp->if_softc;
425         struct route *ro = & ifn->ifen_route;
426
427         if (ro->ro_rt != NULL) {
428                 RTFREE(ro->ro_rt);
429                 ro->ro_rt = NULL;
430         }
431         ifp->if_flags &= ~IFF_UP;
432         return (0);
433 }
434
435 void
436 ipxip_ctlinput(int cmd, struct sockaddr *sa, void *dummy)
437 {
438         struct sockaddr_in *sin;
439
440         if ((unsigned)cmd >= PRC_NCMDS)
441                 return;
442         if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
443                 return;
444         sin = (struct sockaddr_in *)sa;
445         if (sin->sin_addr.s_addr == INADDR_ANY)
446                 return;
447
448         switch (cmd) {
449         case PRC_ROUTEDEAD:
450         case PRC_REDIRECT_NET:
451         case PRC_REDIRECT_HOST:
452         case PRC_REDIRECT_TOSNET:
453         case PRC_REDIRECT_TOSHOST:
454                 ipxip_rtchange(&sin->sin_addr);
455                 break;
456         }
457 }
458
459 static void
460 ipxip_rtchange(struct in_addr *dst)
461 {
462         struct ifnet_en *ifn;
463
464         for (ifn = ipxip_list; ifn != NULL; ifn = ifn->ifen_next) {
465                 if (ifn->ifen_dst.s_addr == dst->s_addr &&
466                         ifn->ifen_route.ro_rt != NULL) {
467                                 RTFREE(ifn->ifen_route.ro_rt);
468                                 ifn->ifen_route.ro_rt = NULL;
469                 }
470         }
471 }
472 #endif /* IPXIP */