1 /* $OpenBSD: prune.c,v 1.7 2023/06/26 10:08:56 claudio 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
send_prune(struct nbr * nbr,struct prune * p)36 send_prune(struct nbr *nbr, struct prune *p)
37 {
38 struct sockaddr_in dst;
39 struct ibuf *buf;
40 struct prune_hdr prune;
41 int ret = 0;
42
43 log_debug("send_prune: interface %s nbr %s", nbr->iface->name,
44 inet_ntoa(nbr->addr));
45
46 if (nbr->iface->passive)
47 return (0);
48
49 memset(&prune, 0, sizeof(prune));
50
51 dst.sin_family = AF_INET;
52 dst.sin_len = sizeof(struct sockaddr_in);
53 dst.sin_addr = nbr->addr;
54
55 if ((buf = ibuf_open(nbr->iface->mtu - sizeof(struct ip))) == NULL)
56 fatal("send_prune");
57
58 /* DVMRP header */
59 if (gen_dvmrp_hdr(buf, nbr->iface, DVMRP_CODE_PRUNE))
60 goto fail;
61
62 prune.src_host_addr = p->origin.s_addr;
63 prune.group_addr = p->group.s_addr;
64
65 /* XXX */
66 prune.lifetime = htonl(MAX_PRUNE_LIFETIME);
67 prune.src_netmask = p->netmask.s_addr;
68
69 ibuf_add(buf, &prune, sizeof(prune));
70
71 ret = send_packet(nbr->iface, buf, &dst);
72 ibuf_free(buf);
73
74 return (ret);
75 fail:
76 log_warn("send_prune");
77 ibuf_free(buf);
78 return (-1);
79 }
80
81 void
recv_prune(struct nbr * nbr,char * buf,u_int16_t len)82 recv_prune(struct nbr *nbr, char *buf, u_int16_t len)
83 {
84 struct prune p;
85 struct prune_hdr *prune;
86
87 log_debug("recv_prune: neighbor ID %s", inet_ntoa(nbr->id));
88
89 if (len < PRUNE_MIN_LEN) {
90 log_debug("recv_prune: packet malformed from %s",
91 inet_ntoa(nbr->id));
92 return;
93 }
94
95 memset(&p, 0, sizeof(p));
96
97 prune = (struct prune_hdr *)buf;
98
99 p.origin.s_addr = prune->src_host_addr;
100 p.group.s_addr = prune->group_addr;
101 p.lifetime = prune->lifetime;
102
103 if (len >= sizeof(*prune))
104 p.netmask.s_addr = prune->src_netmask;
105
106 p.ifindex = nbr->iface->ifindex;
107
108 dvmrpe_imsg_compose_rde(IMSG_RECV_PRUNE, nbr->peerid, 0, &p, sizeof(p));
109
110 return;
111 }
112