ip_mroute.c revision 1.1 1 /*
2 * Copyright (c) 1989 Stephen Deering
3 * Copyright (c) 1992 Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Stephen Deering of Stanford University.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * @(#)ip_mroute.c 7.4 (Berkeley) 11/19/92
38 */
39
40 /*
41 * Procedures for the kernel part of DVMRP,
42 * a Distance-Vector Multicast Routing Protocol.
43 * (See RFC-1075.)
44 *
45 * Written by David Waitzman, BBN Labs, August 1988.
46 * Modified by Steve Deering, Stanford, February 1989.
47 *
48 * MROUTING 1.1
49 */
50
51 #ifndef MROUTING
52 int ip_mrtproto; /* for netstat only */
53 #else
54
55 #include <sys/param.h>
56 #include <sys/errno.h>
57 #include <sys/ioctl.h>
58 #include <sys/malloc.h>
59 #include <sys/mbuf.h>
60 #include <sys/protosw.h>
61 #include <sys/socket.h>
62 #include <sys/socketvar.h>
63 #include <sys/time.h>
64
65 #include <net/af.h>
66 #include <net/if.h>
67 #include <net/route.h>
68 #include <net/raw_cb.h>
69
70 #include <netinet/in.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/in_pcb.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip_var.h>
76
77 #include <netinet/igmp.h>
78 #include <netinet/igmp_var.h>
79 #include <netinet/ip_mroute.h>
80
81 /* Static forwards */
82 static int ip_mrouter_init __P((struct socket *));
83 static int add_vif __P((struct vifctl *));
84 static int del_vif __P((vifi_t *vifip));
85 static int add_lgrp __P((struct lgrplctl *));
86 static int del_lgrp __P((struct lgrplctl *));
87 static int grplst_member __P((struct vif *, struct in_addr));
88 static u_long nethash __P((u_long in));
89 static int add_mrt __P((struct mrtctl *));
90 static int del_mrt __P((struct in_addr *));
91 static struct mrt *mrtfind __P((u_long));
92 static void phyint_send __P((struct ip *, struct vif *, struct mbuf *));
93 static void srcrt_send __P((struct ip *, struct vif *, struct mbuf *));
94 static void encap_send __P((struct ip *, struct vif *, struct mbuf *));
95 static multiencap_decap __P((struct mbuf *, int hlen));
96
97 #define INSIZ sizeof(struct in_addr)
98 #define same(a1, a2) (bcmp((caddr_t)(a1), (caddr_t)(a2), INSIZ) == 0)
99 #define satosin(sa) ((struct sockaddr_in *)(sa))
100
101 /*
102 * Globals. All but ip_mrouter and ip_mrtproto could be static,
103 * except for netstat or debugging purposes.
104 */
105 struct socket *ip_mrouter = NULL;
106 int ip_mrtproto = IGMP_DVMRP; /* for netstat only */
107
108 struct mrt *mrttable[MRTHASHSIZ];
109 struct vif viftable[MAXVIFS];
110 struct mrtstat mrtstat;
111
112 /*
113 * 'Interfaces' associated with decapsulator (so we can tell
114 * packets that went through it from ones that get reflected
115 * by a broken gateway). These interfaces are never linked into
116 * the system ifnet list & no routes point to them. I.e., packets
117 * can't be sent this way. They only exist as a placeholder for
118 * multicast source verification.
119 */
120 struct ifnet multicast_decap_if[MAXVIFS];
121
122 #define ENCAP_TTL 64
123 #define ENCAP_PROTO 4
124
125 /* prototype IP hdr for encapsulated packets */
126 struct ip multicast_encap_iphdr = {
127 #if defined(ultrix) || defined(i386)
128 sizeof(struct ip) >> 2, IPVERSION,
129 #else
130 IPVERSION, sizeof(struct ip) >> 2,
131 #endif
132 0, /* tos */
133 sizeof(struct ip), /* total length */
134 0, /* id */
135 0, /* frag offset */
136 ENCAP_TTL, ENCAP_PROTO,
137 0, /* checksum */
138 };
139
140 /*
141 * Private variables.
142 */
143 static vifi_t numvifs = 0;
144 static struct mrt *cached_mrt = NULL;
145 static u_long cached_origin;
146 static u_long cached_originmask;
147
148 static int (*encap_oldrawip)();
149
150 /*
151 * one-back cache used by multiencap_decap to locate a tunnel's vif
152 * given a datagram's src ip address.
153 */
154 static u_long last_encap_src;
155 static struct vif *last_encap_vif;
156
157 /*
158 * A simple hash function: returns MRTHASHMOD of the low-order octet of
159 * the argument's network or subnet number.
160 */
161 static u_long
162 nethash(n)
163 u_long n;
164 {
165 struct in_addr in;
166
167 in.s_addr = n;
168 n = in_netof(in);
169 while ((n & 0xff) == 0)
170 n >>= 8;
171 return (MRTHASHMOD(n));
172 }
173
174 /*
175 * this is a direct-mapped cache used to speed the mapping from a
176 * datagram source address to the associated multicast route. Note
177 * that unlike mrttable, the hash is on IP address, not IP net number.
178 */
179 #define MSRCHASHSIZ 1024
180 #define MSRCHASH(a) ((((a) >> 20) ^ ((a) >> 10) ^ (a)) & (MSRCHASHSIZ - 1))
181 struct mrt *mrtsrchash[MSRCHASHSIZ];
182
183 /*
184 * Find a route for a given origin IP address.
185 */
186 #define MRTFIND(o, rt) { \
187 register u_int _mrhash = o; \
188 _mrhash = MSRCHASH(_mrhash); \
189 ++mrtstat.mrts_mrt_lookups; \
190 rt = mrtsrchash[_mrhash]; \
191 if (rt == NULL || \
192 (o & rt->mrt_originmask.s_addr) != rt->mrt_origin.s_addr) \
193 if ((rt = mrtfind(o)) != NULL) \
194 mrtsrchash[_mrhash] = rt; \
195 }
196
197 static struct mrt *
198 mrtfind(origin)
199 u_long origin;
200 {
201 register struct mrt *rt;
202 register u_int hash;
203
204 mrtstat.mrts_mrt_misses++;
205
206 hash = nethash(origin);
207 for (rt = mrttable[hash]; rt; rt = rt->mrt_next) {
208 if ((origin & rt->mrt_originmask.s_addr) ==
209 rt->mrt_origin.s_addr)
210 return (rt);
211 }
212 return (NULL);
213 }
214
215 /*
216 * Handle DVMRP setsockopt commands to modify the multicast routing tables.
217 */
218 int
219 ip_mrouter_cmd(cmd, so, m)
220 register int cmd;
221 register struct socket *so;
222 register struct mbuf *m;
223 {
224 register int error = 0;
225
226 if (cmd != DVMRP_INIT && so != ip_mrouter)
227 error = EACCES;
228 else switch (cmd) {
229
230 case DVMRP_INIT:
231 error = ip_mrouter_init(so);
232 break;
233
234 case DVMRP_DONE:
235 error = ip_mrouter_done();
236 break;
237
238 case DVMRP_ADD_VIF:
239 if (m == NULL || m->m_len < sizeof(struct vifctl))
240 error = EINVAL;
241 else
242 error = add_vif(mtod(m, struct vifctl *));
243 break;
244
245 case DVMRP_DEL_VIF:
246 if (m == NULL || m->m_len < sizeof(short))
247 error = EINVAL;
248 else
249 error = del_vif(mtod(m, vifi_t *));
250 break;
251
252 case DVMRP_ADD_LGRP:
253 if (m == NULL || m->m_len < sizeof(struct lgrplctl))
254 error = EINVAL;
255 else
256 error = add_lgrp(mtod(m, struct lgrplctl *));
257 break;
258
259 case DVMRP_DEL_LGRP:
260 if (m == NULL || m->m_len < sizeof(struct lgrplctl))
261 error = EINVAL;
262 else
263 error = del_lgrp(mtod(m, struct lgrplctl *));
264 break;
265
266 case DVMRP_ADD_MRT:
267 if (m == NULL || m->m_len < sizeof(struct mrtctl))
268 error = EINVAL;
269 else
270 error = add_mrt(mtod(m, struct mrtctl *));
271 break;
272
273 case DVMRP_DEL_MRT:
274 if (m == NULL || m->m_len < sizeof(struct in_addr))
275 error = EINVAL;
276 else
277 error = del_mrt(mtod(m, struct in_addr *));
278 break;
279
280 default:
281 error = EOPNOTSUPP;
282 break;
283 }
284 return (error);
285 }
286
287 /*
288 * Enable multicast routing
289 */
290 static int
291 ip_mrouter_init(so)
292 register struct socket *so;
293 {
294 if (so->so_type != SOCK_RAW ||
295 so->so_proto->pr_protocol != IPPROTO_IGMP)
296 return (EOPNOTSUPP);
297
298 if (ip_mrouter != NULL)
299 return (EADDRINUSE);
300
301 ip_mrouter = so;
302
303 return (0);
304 }
305
306 /*
307 * Disable multicast routing
308 */
309 int
310 ip_mrouter_done()
311 {
312 register vifi_t vifi;
313 register int i;
314 register struct ifnet *ifp;
315 register int s;
316 struct ifreq ifr;
317
318 s = splnet();
319
320 /*
321 * For each phyint in use, free its local group list and
322 * disable promiscuous reception of all IP multicasts.
323 */
324 for (vifi = 0; vifi < numvifs; vifi++) {
325 if (viftable[vifi].v_lcl_addr.s_addr != 0 &&
326 !(viftable[vifi].v_flags & VIFF_TUNNEL)) {
327 if (viftable[vifi].v_lcl_grps)
328 free(viftable[vifi].v_lcl_grps, M_MRTABLE);
329 satosin(&ifr.ifr_addr)->sin_family = AF_INET;
330 satosin(&ifr.ifr_addr)->sin_addr.s_addr = INADDR_ANY;
331 ifp = viftable[vifi].v_ifp;
332 (*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)&ifr);
333 }
334 }
335 bzero((caddr_t)viftable, sizeof(viftable));
336 numvifs = 0;
337
338 /*
339 * Free any multicast route entries.
340 */
341 for (i = 0; i < MRTHASHSIZ; i++)
342 if (mrttable[i])
343 free(mrttable[i], M_MRTABLE);
344 bzero((caddr_t)mrttable, sizeof(mrttable));
345 bzero((caddr_t)mrtsrchash, sizeof(mrtsrchash));
346
347 ip_mrouter = NULL;
348
349 splx(s);
350 return (0);
351 }
352
353 /*
354 * Add a vif to the vif table
355 */
356 static int
357 add_vif(vifcp)
358 register struct vifctl *vifcp;
359 {
360 register struct vif *vifp = viftable + vifcp->vifc_vifi;
361 register struct ifaddr *ifa;
362 register struct ifnet *ifp;
363 struct ifreq ifr;
364 register int error, s;
365 static struct sockaddr_in sin = { sizeof(sin), AF_INET };
366
367 if (vifcp->vifc_vifi >= MAXVIFS)
368 return (EINVAL);
369 if (vifp->v_lcl_addr.s_addr != 0)
370 return (EADDRINUSE);
371
372 /* Find the interface with an address in AF_INET family */
373 sin.sin_addr = vifcp->vifc_lcl_addr;
374 ifa = ifa_ifwithaddr((struct sockaddr *)&sin);
375 if (ifa == 0)
376 return (EADDRNOTAVAIL);
377 ifp = ifa->ifa_ifp;
378
379 if (vifcp->vifc_flags & VIFF_TUNNEL) {
380 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) {
381 /*
382 * An encapsulating tunnel is wanted. If we
383 * haven't done so already, put our decap routine
384 * in front of raw_input so we have a chance to
385 * decapsulate incoming packets. Then set the
386 * arrival 'interface' to be the decapsulator.
387 */
388 if (encap_oldrawip == 0) {
389 extern struct protosw inetsw[];
390 extern u_char ip_protox[];
391 register int pr = ip_protox[ENCAP_PROTO];
392
393 encap_oldrawip = inetsw[pr].pr_input;
394 inetsw[pr].pr_input = multiencap_decap;
395 for (s = 0; s < MAXVIFS; ++s) {
396 multicast_decap_if[s].if_name =
397 "mdecap";
398 multicast_decap_if[s].if_unit = s;
399 }
400 }
401 ifp = &multicast_decap_if[vifcp->vifc_vifi];
402 } else {
403 ifp = 0;
404 }
405 } else {
406 /* Make sure the interface supports multicast */
407 if ((ifp->if_flags & IFF_MULTICAST) == 0)
408 return EOPNOTSUPP;
409
410 /*
411 * Enable promiscuous reception of all
412 * IP multicasts from the if
413 */
414 ((struct sockaddr_in *)&ifr.ifr_addr)->sin_family = AF_INET;
415 ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr =
416 INADDR_ANY;
417 s = splnet();
418 error = (*ifp->if_ioctl)(ifp, SIOCADDMULTI, (caddr_t)&ifr);
419 splx(s);
420 if (error)
421 return error;
422 }
423
424 s = splnet();
425 vifp->v_flags = vifcp->vifc_flags;
426 vifp->v_threshold = vifcp->vifc_threshold;
427 vifp->v_lcl_addr = vifcp->vifc_lcl_addr;
428 vifp->v_ifp = ifa->ifa_ifp;
429 vifp->v_rmt_addr = vifcp->vifc_rmt_addr;
430 splx(s);
431
432 /* Adjust numvifs up if the vifi is higher than numvifs */
433 if (numvifs <= vifcp->vifc_vifi)
434 numvifs = vifcp->vifc_vifi + 1;
435
436 splx(s);
437 return (0);
438 }
439
440 /*
441 * Delete a vif from the vif table
442 */
443 static int
444 del_vif(vifip)
445 register vifi_t *vifip;
446 {
447 register struct vif *vifp = viftable + *vifip;
448 register struct ifnet *ifp;
449 register int i, s;
450 struct ifreq ifr;
451
452 if (*vifip >= numvifs)
453 return (EINVAL);
454 if (vifp->v_lcl_addr.s_addr == 0)
455 return (EADDRNOTAVAIL);
456
457 s = splnet();
458
459 if (!(vifp->v_flags & VIFF_TUNNEL)) {
460 if (vifp->v_lcl_grps)
461 free(vifp->v_lcl_grps, M_MRTABLE);
462 satosin(&ifr.ifr_addr)->sin_family = AF_INET;
463 satosin(&ifr.ifr_addr)->sin_addr.s_addr = INADDR_ANY;
464 ifp = vifp->v_ifp;
465 (*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)&ifr);
466 }
467 if (vifp == last_encap_vif) {
468 last_encap_vif = 0;
469 last_encap_src = 0;
470 }
471 bzero((caddr_t)vifp, sizeof (*vifp));
472
473 /* Adjust numvifs down */
474 for (i = numvifs - 1; i >= 0; i--)
475 if (viftable[i].v_lcl_addr.s_addr != 0)
476 break;
477 numvifs = i + 1;
478
479 splx(s);
480 return (0);
481 }
482
483 /*
484 * Add the multicast group in the lgrpctl to the list of local multicast
485 * group memberships associated with the vif indexed by gcp->lgc_vifi.
486 */
487 static int
488 add_lgrp(gcp)
489 register struct lgrplctl *gcp;
490 {
491 register struct vif *vifp;
492 register int s;
493
494 if (gcp->lgc_vifi >= numvifs)
495 return (EINVAL);
496
497 vifp = viftable + gcp->lgc_vifi;
498 if (vifp->v_lcl_addr.s_addr == 0 || (vifp->v_flags & VIFF_TUNNEL))
499 return (EADDRNOTAVAIL);
500
501 /* If not enough space in existing list, allocate a larger one */
502 s = splnet();
503 if (vifp->v_lcl_grps_n + 1 >= vifp->v_lcl_grps_max) {
504 register int num;
505 register struct in_addr *ip;
506
507 num = vifp->v_lcl_grps_max;
508 if (num <= 0)
509 num = 32; /* initial number */
510 else
511 num += num; /* double last number */
512 ip = (struct in_addr *)malloc(num * sizeof(*ip),
513 M_MRTABLE, M_NOWAIT);
514 if (ip == NULL) {
515 splx(s);
516 return (ENOBUFS);
517 }
518
519 bzero((caddr_t)ip, num * sizeof(*ip)); /* XXX paranoid */
520 bcopy((caddr_t)vifp->v_lcl_grps, (caddr_t)ip,
521 vifp->v_lcl_grps_n * sizeof(*ip));
522
523 vifp->v_lcl_grps_max = num;
524 if (vifp->v_lcl_grps)
525 free(vifp->v_lcl_grps, M_MRTABLE);
526 vifp->v_lcl_grps = ip;
527 }
528
529 vifp->v_lcl_grps[vifp->v_lcl_grps_n++] = gcp->lgc_gaddr;
530
531 if (gcp->lgc_gaddr.s_addr == vifp->v_cached_group)
532 vifp->v_cached_result = 1;
533
534 splx(s);
535 return (0);
536 }
537
538 /*
539 * Delete the the local multicast group associated with the vif
540 * indexed by gcp->lgc_vifi.
541 */
542
543 static int
544 del_lgrp(gcp)
545 register struct lgrplctl *gcp;
546 {
547 register struct vif *vifp;
548 register int i, error, s;
549
550 if (gcp->lgc_vifi >= numvifs)
551 return (EINVAL);
552 vifp = viftable + gcp->lgc_vifi;
553 if (vifp->v_lcl_addr.s_addr == 0 || (vifp->v_flags & VIFF_TUNNEL))
554 return (EADDRNOTAVAIL);
555
556 s = splnet();
557
558 if (gcp->lgc_gaddr.s_addr == vifp->v_cached_group)
559 vifp->v_cached_result = 0;
560
561 error = EADDRNOTAVAIL;
562 for (i = 0; i < vifp->v_lcl_grps_n; ++i)
563 if (same(&gcp->lgc_gaddr, &vifp->v_lcl_grps[i])) {
564 error = 0;
565 --vifp->v_lcl_grps_n;
566 for (; i < vifp->v_lcl_grps_n; ++i)
567 vifp->v_lcl_grps[i] = vifp->v_lcl_grps[i + 1];
568 error = 0;
569 break;
570 }
571
572 splx(s);
573 return (error);
574 }
575
576 /*
577 * Return 1 if gaddr is a member of the local group list for vifp.
578 */
579 static int
580 grplst_member(vifp, gaddr)
581 register struct vif *vifp;
582 struct in_addr gaddr;
583 {
584 register int i, s;
585 register u_long addr;
586
587 mrtstat.mrts_grp_lookups++;
588
589 addr = gaddr.s_addr;
590 if (addr == vifp->v_cached_group)
591 return (vifp->v_cached_result);
592
593 mrtstat.mrts_grp_misses++;
594
595 for (i = 0; i < vifp->v_lcl_grps_n; ++i)
596 if (addr == vifp->v_lcl_grps[i].s_addr) {
597 s = splnet();
598 vifp->v_cached_group = addr;
599 vifp->v_cached_result = 1;
600 splx(s);
601 return (1);
602 }
603 s = splnet();
604 vifp->v_cached_group = addr;
605 vifp->v_cached_result = 0;
606 splx(s);
607 return (0);
608 }
609
610 /*
611 * Add an mrt entry
612 */
613 static int
614 add_mrt(mrtcp)
615 register struct mrtctl *mrtcp;
616 {
617 struct mrt *rt;
618 u_long hash;
619 int s;
620
621 if (rt = mrtfind(mrtcp->mrtc_origin.s_addr)) {
622 /* Just update the route */
623 s = splnet();
624 rt->mrt_parent = mrtcp->mrtc_parent;
625 VIFM_COPY(mrtcp->mrtc_children, rt->mrt_children);
626 VIFM_COPY(mrtcp->mrtc_leaves, rt->mrt_leaves);
627 splx(s);
628 return (0);
629 }
630
631 s = splnet();
632
633 rt = (struct mrt *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT);
634 if (rt == NULL) {
635 splx(s);
636 return (ENOBUFS);
637 }
638
639 /*
640 * insert new entry at head of hash chain
641 */
642 rt->mrt_origin = mrtcp->mrtc_origin;
643 rt->mrt_originmask = mrtcp->mrtc_originmask;
644 rt->mrt_parent = mrtcp->mrtc_parent;
645 VIFM_COPY(mrtcp->mrtc_children, rt->mrt_children);
646 VIFM_COPY(mrtcp->mrtc_leaves, rt->mrt_leaves);
647 /* link into table */
648 hash = nethash(mrtcp->mrtc_origin.s_addr);
649 rt->mrt_next = mrttable[hash];
650 mrttable[hash] = rt;
651
652 splx(s);
653 return (0);
654 }
655
656 /*
657 * Delete an mrt entry
658 */
659 static int
660 del_mrt(origin)
661 register struct in_addr *origin;
662 {
663 register struct mrt *rt, *prev_rt;
664 register u_long hash = nethash(origin->s_addr);
665 register struct mrt **cmrt, **cmrtend;
666 register int s;
667
668 for (prev_rt = rt = mrttable[hash]; rt; prev_rt = rt, rt = rt->mrt_next)
669 if (origin->s_addr == rt->mrt_origin.s_addr)
670 break;
671 if (!rt)
672 return (ESRCH);
673
674 s = splnet();
675
676 cmrt = mrtsrchash;
677 cmrtend = cmrt + MSRCHASHSIZ;
678 for ( ; cmrt < cmrtend; ++cmrt)
679 if (*cmrt == rt)
680 *cmrt = 0;
681
682 if (prev_rt == rt)
683 mrttable[hash] = rt->mrt_next;
684 else
685 prev_rt->mrt_next = rt->mrt_next;
686 free(rt, M_MRTABLE);
687
688 splx(s);
689 return (0);
690 }
691
692 /*
693 * IP multicast forwarding function. This function assumes that the packet
694 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
695 * pointed to by "ifp", and the packet is to be relayed to other networks
696 * that have members of the packet's destination IP multicast group.
697 *
698 * The packet is returned unscathed to the caller, unless it is tunneled
699 * or erroneous, in which case a non-zero return value tells the caller to
700 * discard it.
701 */
702
703 #define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */
704 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
705
706 int
707 ip_mforward(ip, ifp, m)
708 register struct ip *ip;
709 register struct ifnet *ifp;
710 register struct mbuf *m;
711 {
712 register struct mrt *rt;
713 register struct vif *vifp;
714 register int vifi;
715 register u_char *ipoptions;
716 u_long tunnel_src;
717
718 if (ip->ip_hl < (IP_HDR_LEN + TUNNEL_LEN) >> 2 ||
719 (ipoptions = (u_char *)(ip + 1))[1] != IPOPT_LSRR ) {
720 /*
721 * Packet arrived via a physical interface.
722 */
723 tunnel_src = 0;
724 } else {
725 /*
726 * Packet arrived through a tunnel.
727 *
728 * A tunneled packet has a single NOP option and a
729 * two-element loose-source-and-record-route (LSRR)
730 * option immediately following the fixed-size part of
731 * the IP header. At this point in processing, the IP
732 * header should contain the following IP addresses:
733 *
734 * original source - in the source address field
735 * destination group - in the destination address field
736 * remote tunnel end-point - in the first element of LSRR
737 * one of this host's addrs - in the second element of LSRR
738 *
739 * NOTE: RFC-1075 would have the original source and
740 * remote tunnel end-point addresses swapped. However,
741 * that could cause delivery of ICMP error messages to
742 * innocent applications on intermediate routing
743 * hosts! Therefore, we hereby change the spec.
744 */
745
746 /*
747 * Verify that the tunnel options are well-formed.
748 */
749 if (ipoptions[0] != IPOPT_NOP ||
750 ipoptions[2] != 11 || /* LSRR option length */
751 ipoptions[3] != 12 || /* LSRR address pointer */
752 (tunnel_src = *(u_long *)(&ipoptions[4])) == 0) {
753 mrtstat.mrts_bad_tunnel++;
754 return (1);
755 }
756
757 /*
758 * Delete the tunnel options from the packet.
759 */
760 ovbcopy((caddr_t)(ipoptions + TUNNEL_LEN), (caddr_t)ipoptions,
761 (unsigned)(m->m_len - (IP_HDR_LEN + TUNNEL_LEN)));
762 m->m_len -= TUNNEL_LEN;
763 ip->ip_len -= TUNNEL_LEN;
764 ip->ip_hl -= TUNNEL_LEN >> 2;
765 }
766
767 /*
768 * Don't forward a packet with time-to-live of zero or one,
769 * or a packet destined to a local-only group.
770 */
771 if (ip->ip_ttl <= 1 ||
772 ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP)
773 return ((int)tunnel_src);
774
775 /*
776 * Don't forward if we don't have a route for the packet's origin.
777 */
778 MRTFIND(ip->ip_src.s_addr, rt)
779 if (rt == NULL) {
780 mrtstat.mrts_no_route++;
781 return ((int)tunnel_src);
782 }
783
784 /*
785 * Don't forward if it didn't arrive from the
786 * parent vif for its origin.
787 *
788 * Notes: v_ifp is zero for src route tunnels, multicast_decap_if
789 * for encapsulated tunnels and a real ifnet for non-tunnels so
790 * the first part of the if catches wrong physical interface or
791 * tunnel type; v_rmt_addr is zero for non-tunneled packets so
792 * the 2nd part catches both packets that arrive via a tunnel
793 * that shouldn't and packets that arrive via the wrong tunnel.
794 */
795 vifi = rt->mrt_parent;
796 if (viftable[vifi].v_ifp != ifp ||
797 (ifp == 0 && viftable[vifi].v_rmt_addr.s_addr != tunnel_src)) {
798 /* came in the wrong interface */
799 ++mrtstat.mrts_wrong_if;
800 return (int)tunnel_src;
801 }
802
803 /*
804 * For each vif, decide if a copy of the packet should be forwarded.
805 * Forward if:
806 * - the ttl exceeds the vif's threshold AND
807 * - the vif is a child in the origin's route AND
808 * - ( the vif is not a leaf in the origin's route OR
809 * the destination group has members on the vif )
810 *
811 * (This might be speeded up with some sort of cache -- someday.)
812 */
813 for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++) {
814 if (ip->ip_ttl > vifp->v_threshold &&
815 VIFM_ISSET(vifi, rt->mrt_children) &&
816 (!VIFM_ISSET(vifi, rt->mrt_leaves) ||
817 grplst_member(vifp, ip->ip_dst))) {
818 if (vifp->v_flags & VIFF_SRCRT)
819 srcrt_send(ip, vifp, m);
820 else if (vifp->v_flags & VIFF_TUNNEL)
821 encap_send(ip, vifp, m);
822 else
823 phyint_send(ip, vifp, m);
824 }
825 }
826 return ((int)tunnel_src);
827 }
828
829 static void
830 phyint_send(ip, vifp, m)
831 register struct ip *ip;
832 register struct vif *vifp;
833 register struct mbuf *m;
834 {
835 register struct mbuf *mb_copy;
836 register struct ip_moptions *imo;
837 register int error;
838 struct ip_moptions simo;
839
840 mb_copy = m_copy(m, 0, M_COPYALL);
841 if (mb_copy == NULL)
842 return;
843
844 imo = &simo;
845 imo->imo_multicast_ifp = vifp->v_ifp;
846 imo->imo_multicast_ttl = ip->ip_ttl - 1;
847 imo->imo_multicast_loop = 1;
848
849 error = ip_output(mb_copy, NULL, NULL,
850 IP_FORWARDING|IP_MULTICASTOPTS, imo);
851 }
852
853 static void
854 srcrt_send(ip, vifp, m)
855 register struct ip *ip;
856 register struct vif *vifp;
857 register struct mbuf *m;
858 {
859 register struct mbuf *mb_copy, *mb_opts;
860 register struct ip *ip_copy;
861 register int error;
862 register u_char *cp;
863
864 /*
865 * Make sure that adding the tunnel options won't exceed the
866 * maximum allowed number of option bytes.
867 */
868 if (ip->ip_hl > (60 - TUNNEL_LEN) >> 2) {
869 mrtstat.mrts_cant_tunnel++;
870 return;
871 }
872
873 mb_copy = m_copy(m, 0, M_COPYALL);
874 if (mb_copy == NULL)
875 return;
876 ip_copy = mtod(mb_copy, struct ip *);
877 ip_copy->ip_ttl--;
878 ip_copy->ip_dst = vifp->v_rmt_addr; /* remote tunnel end-point */
879 /*
880 * Adjust the ip header length to account for the tunnel options.
881 */
882 ip_copy->ip_hl += TUNNEL_LEN >> 2;
883 ip_copy->ip_len += TUNNEL_LEN;
884 MGETHDR(mb_opts, M_DONTWAIT, MT_HEADER);
885 if (mb_opts == NULL) {
886 m_freem(mb_copy);
887 return;
888 }
889 /*
890 * 'Delete' the base ip header from the mb_copy chain
891 */
892 mb_copy->m_len -= IP_HDR_LEN;
893 mb_copy->m_data += IP_HDR_LEN;
894 /*
895 * Make mb_opts be the new head of the packet chain.
896 * Any options of the packet were left in the old packet chain head
897 */
898 mb_opts->m_next = mb_copy;
899 mb_opts->m_len = IP_HDR_LEN + TUNNEL_LEN;
900 mb_opts->m_data += MSIZE - mb_opts->m_len;
901 /*
902 * Copy the base ip header from the mb_copy chain to the new head mbuf
903 */
904 bcopy((caddr_t)ip_copy, mtod(mb_opts, caddr_t), IP_HDR_LEN);
905 /*
906 * Add the NOP and LSRR after the base ip header
907 */
908 cp = mtod(mb_opts, u_char *) + IP_HDR_LEN;
909 *cp++ = IPOPT_NOP;
910 *cp++ = IPOPT_LSRR;
911 *cp++ = 11; /* LSRR option length */
912 *cp++ = 8; /* LSSR pointer to second element */
913 *(u_long*)cp = vifp->v_lcl_addr.s_addr; /* local tunnel end-point */
914 cp += 4;
915 *(u_long*)cp = ip->ip_dst.s_addr; /* destination group */
916
917 error = ip_output(mb_opts, NULL, NULL, IP_FORWARDING, NULL);
918 }
919
920 static void
921 encap_send(ip, vifp, m)
922 register struct ip *ip;
923 register struct vif *vifp;
924 register struct mbuf *m;
925 {
926 register struct mbuf *mb_copy;
927 register struct ip *ip_copy;
928 register int i, len = ip->ip_len;
929
930 /*
931 * copy the old packet & pullup it's IP header into the
932 * new mbuf so we can modify it. Try to fill the new
933 * mbuf since if we don't the ethernet driver will.
934 */
935 MGETHDR(mb_copy, M_DONTWAIT, MT_HEADER);
936 if (mb_copy == NULL)
937 return;
938 mb_copy->m_data += 16;
939 mb_copy->m_len = sizeof(multicast_encap_iphdr);
940 if ((mb_copy->m_next = m_copy(m, 0, M_COPYALL)) == NULL) {
941 m_freem(mb_copy);
942 return;
943 }
944 i = MHLEN - 16;
945 if (i > len)
946 i = len;
947 mb_copy = m_pullup(mb_copy, i);
948 if (mb_copy == NULL)
949 return;
950
951 /*
952 * fill in the encapsulating IP header.
953 */
954 ip_copy = mtod(mb_copy, struct ip *);
955 *ip_copy = multicast_encap_iphdr;
956 ip_copy->ip_id = htons(ip_id++);
957 ip_copy->ip_len += len;
958 ip_copy->ip_src = vifp->v_lcl_addr;
959 ip_copy->ip_dst = vifp->v_rmt_addr;
960
961 /*
962 * turn the encapsulated IP header back into a valid one.
963 */
964 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr));
965 --ip->ip_ttl;
966 HTONS(ip->ip_len);
967 HTONS(ip->ip_off);
968 ip->ip_sum = 0;
969 #if defined(LBL) && !defined(ultrix) && !defined(i386)
970 ip->ip_sum = ~oc_cksum((caddr_t)ip, ip->ip_hl << 2, 0);
971 #else
972 mb_copy->m_data += sizeof(multicast_encap_iphdr);
973 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2);
974 mb_copy->m_data -= sizeof(multicast_encap_iphdr);
975 #endif
976 ip_output(mb_copy, (struct mbuf *)0, (struct route *)0,
977 IP_FORWARDING, (struct mbuf *)0);
978 }
979
980 /*
981 * De-encapsulate a packet and feed it back through ip input (this
982 * routine is called whenever IP gets a packet with proto type
983 * ENCAP_PROTO and a local destination address).
984 */
985 multiencap_decap(m, hlen)
986 register struct mbuf *m;
987 int hlen;
988 {
989 struct ifnet *ifp;
990 register struct ip *ip = mtod(m, struct ip *);
991 register int s;
992 register struct ifqueue *ifq;
993 register struct vif *vifp;
994
995 if (ip->ip_p != ENCAP_PROTO) {
996 (*encap_oldrawip)(m, hlen);
997 return;
998 }
999 /*
1000 * dump the packet if it's not to a multicast destination or if
1001 * we don't have an encapsulating tunnel with the source.
1002 * Note: This code assumes that the remote site IP address
1003 * uniquely identifies the tunnel (i.e., that this site has
1004 * at most one tunnel with the remote site).
1005 */
1006 if (! IN_MULTICAST(ntohl(((struct ip *)((char *)ip + hlen))->ip_dst.s_addr))) {
1007 ++mrtstat.mrts_bad_tunnel;
1008 m_freem(m);
1009 return;
1010 }
1011 if (ip->ip_src.s_addr != last_encap_src) {
1012 register struct vif *vife;
1013
1014 vifp = viftable;
1015 vife = vifp + numvifs;
1016 last_encap_src = ip->ip_src.s_addr;
1017 last_encap_vif = 0;
1018 for ( ; vifp < vife; ++vifp)
1019 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) {
1020 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT))
1021 == VIFF_TUNNEL)
1022 last_encap_vif = vifp;
1023 break;
1024 }
1025 }
1026 if ((vifp = last_encap_vif) == 0) {
1027 mrtstat.mrts_cant_tunnel++; /*XXX*/
1028 m_freem(m);
1029 return;
1030 }
1031 ifp = vifp->v_ifp;
1032 m->m_data += hlen;
1033 m->m_len -= hlen;
1034 m->m_pkthdr.rcvif = ifp;
1035 m->m_pkthdr.len -= hlen;
1036 ifq = &ipintrq;
1037 s = splimp();
1038 if (IF_QFULL(ifq)) {
1039 IF_DROP(ifq);
1040 m_freem(m);
1041 } else {
1042 IF_ENQUEUE(ifq, m);
1043 /*
1044 * normally we would need a "schednetisr(NETISR_IP)"
1045 * here but we were called by ip_input and it is going
1046 * to loop back & try to dequeue the packet we just
1047 * queued as soon as we return so we avoid the
1048 * unnecessary software interrrupt.
1049 */
1050 }
1051 splx(s);
1052 }
1053 #endif
1054