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