xref: /openbsd-src/usr.sbin/dvmrpd/prune.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: prune.c,v 1.6 2015/12/07 19:14:49 mmcc Exp $ */
2 
3 /*
4  * Copyright (c) 2005, 2006 Esben Norby <norby@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <netinet/ip.h>
23 #include <arpa/inet.h>
24 
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "igmp.h"
29 #include "dvmrpd.h"
30 #include "dvmrp.h"
31 #include "log.h"
32 #include "dvmrpe.h"
33 
34 /* DVMRP prune packet handling */
35 int
36 send_prune(struct nbr *nbr, struct prune *p)
37 {
38 	struct sockaddr_in	 dst;
39 	struct ibuf		*buf;
40 	struct dvmrp_hdr	*dvmrp_hdr;
41 	struct prune_hdr	 prune;
42 	int			 ret = 0;
43 
44 	log_debug("send_prune: interface %s nbr %s", nbr->iface->name,
45 	    inet_ntoa(nbr->addr));
46 
47 	if (nbr->iface->passive)
48 		return (0);
49 
50 	memset(&prune, 0, sizeof(prune));
51 
52 	dst.sin_family = AF_INET;
53 	dst.sin_len = sizeof(struct sockaddr_in);
54 	dst.sin_addr = nbr->addr;
55 
56 	if ((buf = ibuf_open(nbr->iface->mtu - sizeof(struct ip))) == NULL)
57 		fatal("send_prune");
58 
59 	/* DVMRP header */
60 	if (gen_dvmrp_hdr(buf, nbr->iface, DVMRP_CODE_PRUNE))
61 		goto fail;
62 
63 	prune.src_host_addr = p->origin.s_addr;
64 	prune.group_addr = p->group.s_addr;
65 
66 	/* XXX */
67 	prune.lifetime = htonl(MAX_PRUNE_LIFETIME);
68 	prune.src_netmask = p->netmask.s_addr;
69 
70 	ibuf_add(buf, &prune, sizeof(prune));
71 
72 	/* update chksum */
73 	dvmrp_hdr = ibuf_seek(buf, 0, sizeof(*dvmrp_hdr));
74 	dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos);
75 
76 	ret = send_packet(nbr->iface, buf->buf, buf->wpos, &dst);
77 	ibuf_free(buf);
78 
79 	return (ret);
80 fail:
81 	log_warn("send_prune");
82 	ibuf_free(buf);
83 	return (-1);
84 }
85 
86 void
87 recv_prune(struct nbr *nbr, char *buf, u_int16_t len)
88 {
89 	struct prune		 p;
90 	struct prune_hdr	*prune;
91 
92 	log_debug("recv_prune: neighbor ID %s", inet_ntoa(nbr->id));
93 
94 	if (len < PRUNE_MIN_LEN) {
95 		log_debug("recv_prune: packet malformed from %s",
96 		    inet_ntoa(nbr->id));
97 		return;
98 	}
99 
100 	memset(&p, 0, sizeof(p));
101 
102 	prune = (struct prune_hdr *)buf;
103 
104 	p.origin.s_addr = prune->src_host_addr;
105 	p.group.s_addr = prune->group_addr;
106 	p.lifetime = prune->lifetime;
107 
108 	if (len >= sizeof(*prune))
109 		p.netmask.s_addr = prune->src_netmask;
110 
111 	p.ifindex = nbr->iface->ifindex;
112 
113 	dvmrpe_imsg_compose_rde(IMSG_RECV_PRUNE, nbr->peerid, 0, &p, sizeof(p));
114 
115 	return;
116 }
117