FreeBSD/Linux Kernel Cross Reference
sys/netns/ns_ip.c
1 /* $NetBSD: ns_ip.c,v 1.39 2004/04/21 18:40:41 itojun Exp $ */
2
3 /*
4 * Copyright (c) 1984, 1985, 1986, 1987, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)ns_ip.c 8.2 (Berkeley) 1/9/95
32 */
33
34 /*
35 * Software interface driver for encapsulating ns in ip.
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: ns_ip.c,v 1.39 2004/04/21 18:40:41 itojun Exp $");
40
41 #include "opt_ns.h" /* options NSIP, needed by ns_if.h */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/errno.h>
50 #include <sys/ioctl.h>
51 #include <sys/protosw.h>
52
53 #include <machine/stdarg.h>
54 #include <machine/cpu.h> /* XXX for setsoftnet(). This must die. */
55
56 #include <net/if.h>
57 #include <net/netisr.h>
58 #include <net/route.h>
59
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/in_var.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip_var.h>
65
66 #include <netns/ns.h>
67 #include <netns/ns_if.h>
68 #include <netns/ns_var.h>
69 #include <netns/idp.h>
70
71 struct ifnet_en {
72 struct ifnet ifen_ifnet;
73 struct route ifen_route;
74 struct in_addr ifen_src;
75 struct in_addr ifen_dst;
76 struct ifnet_en *ifen_next;
77 };
78
79 int nsipoutput (struct ifnet *, struct mbuf *m, struct sockaddr *,
80 struct rtentry *);
81 int nsipioctl (struct ifnet *, u_long, caddr_t);
82 void nsipstart (struct ifnet *);
83 int nsip_route (struct mbuf *);
84 void nsip_rtchange (struct in_addr *);
85 #define LOMTU (1024+512);
86
87 int nsipif_unit; /* XXX */
88 struct ifnet nsipif;
89 struct ifnet_en *nsip_list; /* list of all hosts and gateways or
90 broadcast addrs */
91
92 struct ifnet_en *
93 nsipattach(void)
94 {
95 struct ifnet_en *m;
96 struct ifnet *ifp;
97
98 if (nsipif.if_mtu == 0) {
99 ifp = &nsipif;
100 snprintf(ifp->if_xname, sizeof(ifp->if_xname), "nsip%d",
101 nsipif_unit);
102 ifp->if_mtu = LOMTU;
103 ifp->if_ioctl = nsipioctl;
104 ifp->if_output = nsipoutput;
105 ifp->if_start = nsipstart;
106 ifp->if_flags = IFF_POINTOPOINT;
107 }
108
109 MALLOC((m), struct ifnet_en *, sizeof(*m), M_PCB, M_NOWAIT);
110 if (m == NULL) return (NULL);
111 m->ifen_next = nsip_list;
112 nsip_list = m;
113 ifp = &m->ifen_ifnet;
114
115 snprintf(ifp->if_xname, sizeof(ifp->if_xname), "nsip%d", nsipif_unit++);
116 ifp->if_mtu = LOMTU;
117 ifp->if_ioctl = nsipioctl;
118 ifp->if_output = nsipoutput;
119 ifp->if_start = nsipstart;
120 ifp->if_flags = IFF_POINTOPOINT;
121 if_attach(ifp);
122 if_alloc_sadl(ifp);
123
124 /*
125 * XXX Emulate the side effect of incrementing nsipif.if_unit
126 * XXX in the days before if_xname.
127 */
128 bzero(nsipif.if_xname, sizeof(nsipif.if_xname));
129 snprintf(nsipif.if_xname, sizeof(nsipif.if_xname), "nsip%d",
130 nsipif_unit);
131
132 return (m);
133 }
134
135
136 /*
137 * Process an ioctl request.
138 */
139 /* ARGSUSED */
140 int
141 nsipioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
142 {
143 int error = 0;
144 struct ifreq *ifr;
145
146 switch (cmd) {
147
148 case SIOCSIFADDR:
149 ifp->if_flags |= IFF_UP;
150 /* fall into: */
151
152 /*FALLTHROUGH*/
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 = nsip_free(ifp);
163
164
165 default:
166 error = EINVAL;
167 }
168 return (error);
169 }
170
171 struct mbuf *nsip_badlen;
172 struct mbuf *nsip_lastin;
173 int nsip_hold_input;
174
175 void
176 idpip_input(struct mbuf *m, ...)
177 {
178 struct ifnet *ifp;
179 struct ip *ip;
180 struct idp *idp;
181 struct ifqueue *ifq = &nsintrq;
182 int len, s;
183 va_list ap;
184 va_start(ap, m);
185 ifp = va_arg(ap, struct ifnet *);
186 va_end(ap);
187
188 if (nsip_hold_input) {
189 if (nsip_lastin) {
190 m_freem(nsip_lastin);
191 }
192 nsip_lastin = m_copym(m, 0, (int)M_COPYALL, M_DONTWAIT);
193 }
194 /*
195 * Get IP and IDP header together in first mbuf.
196 */
197 nsipif.if_ipackets++;
198 s = sizeof (struct ip) + sizeof (struct idp);
199 if (((m->m_flags & M_EXT) || m->m_len < s) &&
200 (m = m_pullup(m, s)) == 0) {
201 nsipif.if_ierrors++;
202 return;
203 }
204 ip = mtod(m, struct ip *);
205 if (ip->ip_hl > (sizeof (struct ip) >> 2)) {
206 ip_stripoptions(m, (struct mbuf *)0);
207 if (m->m_len < s) {
208 if ((m = m_pullup(m, s)) == 0) {
209 nsipif.if_ierrors++;
210 return;
211 }
212 ip = mtod(m, struct ip *);
213 }
214 }
215
216 /*
217 * Make mbuf data length reflect IDP length.
218 * If not enough data to reflect IDP length, drop.
219 */
220 m->m_data += sizeof (struct ip);
221 m->m_len -= sizeof (struct ip);
222 m->m_pkthdr.len -= sizeof (struct ip);
223 idp = mtod(m, struct idp *);
224 len = ntohs(idp->idp_len);
225 if (len & 1) len++; /* Preserve Garbage Byte */
226 if (ntohs(ip->ip_len) - (ip->ip_hl << 2) != len) {
227 if (len > ntohs(ip->ip_len) - (ip->ip_hl << 2)) {
228 nsipif.if_ierrors++;
229 if (nsip_badlen) m_freem(nsip_badlen);
230 nsip_badlen = m;
231 return;
232 }
233 /* Any extra will be trimmed off by the NS routines */
234 }
235
236 /*
237 * Place interface pointer before the data
238 * for the receiving protocol.
239 */
240 m->m_pkthdr.rcvif = ifp;
241 /*
242 * Deliver to NS
243 */
244 s = splnet();
245 if (IF_QFULL(ifq)) {
246 IF_DROP(ifq);
247 m_freem(m);
248 splx(s);
249 return;
250 }
251 IF_ENQUEUE(ifq, m);
252 schednetisr(NETISR_NS);
253 splx(s);
254 return;
255 }
256
257 /* ARGSUSED */
258 int
259 nsipoutput(struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst,
260 struct rtentry *rt)
261 {
262 struct ifnet_en *ifn = (struct ifnet_en *) ifp;
263
264 struct ip *ip;
265 struct route *ro = &(ifn->ifen_route);
266 int len = 0;
267 struct idp *idp = mtod(m, struct idp *);
268 int error;
269
270 ifn->ifen_ifnet.if_opackets++;
271 nsipif.if_opackets++;
272
273
274 /*
275 * Calculate data length and make space
276 * for IP header.
277 */
278 len = ntohs(idp->idp_len);
279 if (len & 1) len++; /* Preserve Garbage Byte */
280 /* following clause not necessary on vax */
281 if (3 & (int)m->m_data) {
282 /* force longword alignment of ip hdr */
283 struct mbuf *m0 = m_gethdr(M_DONTWAIT, MT_HEADER);
284 if (m0 == 0) {
285 m_freem(m);
286 return (ENOBUFS);
287 }
288 MH_ALIGN(m0, sizeof (struct ip));
289 m0->m_flags = m->m_flags & M_COPYFLAGS;
290 m0->m_next = m;
291 m0->m_len = sizeof (struct ip);
292 m0->m_pkthdr.len = m0->m_len + m->m_len;
293 m_tag_delete_chain(m, NULL);
294 m->m_flags &= ~M_PKTHDR;
295 } else {
296 M_PREPEND(m, sizeof (struct ip), M_DONTWAIT);
297 if (m == 0)
298 return (ENOBUFS);
299 }
300 /*
301 * Fill in IP header.
302 */
303 ip = mtod(m, struct ip *);
304 *(int32_t *)ip = 0;
305 ip->ip_p = IPPROTO_IDP;
306 ip->ip_src = ifn->ifen_src;
307 ip->ip_dst = ifn->ifen_dst;
308 if (len + sizeof (struct ip) > IP_MAXPACKET) {
309 m_freem(m);
310 return (EMSGSIZE);
311 }
312 ip->ip_len = htons(len + sizeof (struct ip));
313 ip->ip_ttl = MAXTTL;
314
315 /*
316 * Output final datagram.
317 */
318 error = ip_output(m, (struct mbuf *)0, ro, SO_BROADCAST,
319 (struct ip_moptions *)NULL, (struct socket *)NULL);
320 if (error) {
321 ifn->ifen_ifnet.if_oerrors++;
322 ifn->ifen_ifnet.if_ierrors = error;
323 }
324 return (error);
325 }
326
327 void
328 nsipstart(struct ifnet *ifp)
329 {
330 panic("nsip_start called");
331 }
332
333 struct ifreq ifr = {"nsip0"}; /* XXX */
334
335 int
336 nsip_route(struct mbuf *m)
337 {
338 struct nsip_req *rq = mtod(m, struct nsip_req *);
339 struct sockaddr_ns *ns_dst = satosns(&rq->rq_ns);
340 struct sockaddr_in *ip_dst = satosin(&rq->rq_ip);
341 struct route ro;
342 struct ifnet_en *ifn;
343 struct sockaddr_in *src;
344
345 /*
346 * First, make sure we already have an ns address:
347 */
348 if (ns_hosteqnh(ns_thishost, ns_zerohost))
349 return (EADDRNOTAVAIL);
350 /*
351 * Now, determine if we can get to the destination
352 */
353 bzero((caddr_t)&ro, sizeof (ro));
354 ro.ro_dst = *sintosa(ip_dst);
355 rtalloc(&ro);
356 if (ro.ro_rt == 0 || ro.ro_rt->rt_ifp == 0) {
357 return (ENETUNREACH);
358 }
359
360 /*
361 * And see how he's going to get back to us:
362 * i.e., what return ip address do we use?
363 */
364 {
365 struct in_ifaddr *ia;
366 struct ifnet *ifp = ro.ro_rt->rt_ifp;
367
368 for (ia = in_ifaddrhead.tqh_first; ia != 0;
369 ia = ia->ia_list.tqe_next)
370 if (ia->ia_ifp == ifp)
371 break;
372 if (ia == 0)
373 ia = in_ifaddrhead.tqh_first;
374 if (ia == 0) {
375 RTFREE(ro.ro_rt);
376 return (EADDRNOTAVAIL);
377 }
378 src = satosin(&ia->ia_addr);
379 }
380
381 /*
382 * Is there a free (pseudo-)interface or space?
383 */
384 for (ifn = nsip_list; ifn; ifn = ifn->ifen_next) {
385 if ((ifn->ifen_ifnet.if_flags & IFF_UP) == 0)
386 break;
387 }
388 if (ifn == NULL)
389 ifn = nsipattach();
390 if (ifn == NULL) {
391 RTFREE(ro.ro_rt);
392 return (ENOBUFS);
393 }
394 ifn->ifen_route = ro;
395 ifn->ifen_dst = ip_dst->sin_addr;
396 ifn->ifen_src = src->sin_addr;
397
398 /*
399 * now configure this as a point to point link
400 */
401 bzero(ifr.ifr_name, sizeof(ifr.ifr_name));
402 snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "nsip%d", nsipif_unit - 1);
403 ifr.ifr_dstaddr = *snstosa(ns_dst);
404 (void)ns_control((struct socket *)0, SIOCSIFDSTADDR, (caddr_t)&ifr,
405 (struct ifnet *)ifn, NULL);
406 satons_addr(ifr.ifr_addr).x_host = ns_thishost;
407 return (ns_control((struct socket *)0, SIOCSIFADDR, (caddr_t)&ifr,
408 (struct ifnet *)ifn, NULL));
409 }
410
411 int
412 nsip_free(struct ifnet *ifp)
413 {
414 struct ifnet_en *ifn = (struct ifnet_en *)ifp;
415 struct route *ro = & ifn->ifen_route;
416
417 if (ro->ro_rt) {
418 RTFREE(ro->ro_rt);
419 ro->ro_rt = 0;
420 }
421 ifp->if_flags &= ~IFF_UP;
422 return (0);
423 }
424
425 void *
426 nsip_ctlinput(int cmd, struct sockaddr *sa, void *v)
427 {
428 struct sockaddr_in *sin;
429
430 if ((unsigned)cmd >= PRC_NCMDS)
431 return NULL;
432 if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK)
433 return NULL;
434 sin = satosin(sa);
435 if (sin->sin_addr.s_addr == INADDR_ANY)
436 return NULL;
437
438 switch (cmd) {
439
440 case PRC_ROUTEDEAD:
441 case PRC_REDIRECT_NET:
442 case PRC_REDIRECT_HOST:
443 case PRC_REDIRECT_TOSNET:
444 case PRC_REDIRECT_TOSHOST:
445 nsip_rtchange(&sin->sin_addr);
446 break;
447 }
448 return NULL;
449 }
450
451 void
452 nsip_rtchange(struct in_addr *dst)
453 {
454 struct ifnet_en *ifn;
455
456 for (ifn = nsip_list; ifn; ifn = ifn->ifen_next) {
457 if (ifn->ifen_dst.s_addr == dst->s_addr &&
458 ifn->ifen_route.ro_rt) {
459 RTFREE(ifn->ifen_route.ro_rt);
460 ifn->ifen_route.ro_rt = 0;
461 }
462 }
463 }
Cache object: 5d7b7dfd0a494bc9e2dee4f3a851e6b4
|