xref: /openbsd/usr.sbin/dvmrpd/packet.c (revision e8127df6)
1 /*	$OpenBSD: packet.c,v 1.6 2021/01/19 11:46:10 claudio Exp $ */
2 
3 /*
4  * Copyright (c) 2004, 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 <sys/time.h>
22 
23 #include <netinet/in.h>
24 #include <netinet/ip.h>
25 #include <netinet/ip_mroute.h>
26 #include <arpa/inet.h>
27 
28 #include <errno.h>
29 #include <event.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "igmp.h"
34 #include "dvmrpd.h"
35 #include "dvmrp.h"
36 #include "log.h"
37 #include "dvmrpe.h"
38 
39 int		 ip_hdr_sanity_check(const struct ip *, u_int16_t);
40 int		 dvmrp_hdr_sanity_check(const struct ip *, struct dvmrp_hdr *,
41 		     u_int16_t, const struct iface *);
42 struct iface	*find_iface(struct dvmrpd_conf *, struct in_addr);
43 
44 extern struct dvmrpd_conf	*deconf;
45 
46 int
47 gen_dvmrp_hdr(struct ibuf *buf, struct iface *iface, u_int8_t code)
48 {
49 	struct dvmrp_hdr	dvmrp_hdr;
50 
51 	memset(&dvmrp_hdr, 0, sizeof(dvmrp_hdr));
52 	dvmrp_hdr.type = PKT_TYPE_DVMRP;
53 	dvmrp_hdr.code = code;
54 	dvmrp_hdr.chksum = 0;				/* updated later */
55 	dvmrp_hdr.capabilities = DVMRP_CAP_DEFAULT;	/* XXX update */
56 	dvmrp_hdr.minor_version = DVMRP_MINOR_VERSION;
57 	dvmrp_hdr.major_version = DVMRP_MAJOR_VERSION;
58 
59 	return (ibuf_add(buf, &dvmrp_hdr, sizeof(dvmrp_hdr)));
60 }
61 
62 /* send and receive packets */
63 int
64 send_packet(struct iface *iface, void *pkt, size_t len, struct sockaddr_in *dst)
65 {
66 	if (iface->passive) {
67 		log_warnx("send_packet: cannot send packet on passive "
68 		    "interface %s", iface->name);
69 		return (-1);
70 	}
71 
72 	/* set outgoing interface for multicast traffic */
73 	if (IN_MULTICAST(ntohl(dst->sin_addr.s_addr)))
74 		if (if_set_mcast(iface) == -1) {
75 			log_warn("send_packet: error setting multicast "
76 			    "interface, %s", iface->name);
77 			return (-1);
78 		}
79 
80 	if (sendto(iface->fd, pkt, len, 0,
81 	    (struct sockaddr *)dst, sizeof(*dst)) == -1 ) {
82 		log_warn("send_packet: error sending packet on interface %s",
83 		    iface->name);
84 		return (-1);
85 	}
86 
87 	return (0);
88 }
89 
90 void
91 recv_packet(int fd, short event, void *bula)
92 {
93 	struct dvmrpd_conf	*xconf = bula;
94 	struct ip		 ip_hdr;
95 	struct dvmrp_hdr	*dvmrp_hdr;
96 	struct iface		*iface;
97 	struct nbr		*nbr = NULL;
98 	struct in_addr		 addr;
99 	char			*buf;
100 	char			 pkt[READ_BUF_SIZE];
101 	ssize_t			 r;
102 	u_int16_t		 len;
103 	int			 l;
104 
105 	if (event != EV_READ)
106 		return;
107 
108 	if ((r = recvfrom(fd, pkt, sizeof(pkt), 0, NULL, NULL)) == -1) {
109 		if (errno != EAGAIN && errno != EINTR)
110 			log_debug("recv_packet: error receiving packet");
111 		return;
112 	}
113 
114 	len = (u_int16_t)r;
115 	buf = pkt;
116 
117 	/* IP header sanity checks */
118 	if (len < sizeof(ip_hdr)) {
119 		log_warnx("recv_packet: bad packet size");
120 		return;
121 	}
122 
123 	memcpy(&ip_hdr, buf, sizeof(ip_hdr));
124 	if ((l = ip_hdr_sanity_check(&ip_hdr, len)) == -1)
125 		return;
126 	buf += l;
127 	len -= l;
128 
129 	/* find a matching interface */
130 	if ((iface = find_iface(xconf, ip_hdr.ip_src)) == NULL) {
131 		log_debug("recv_packet: cannot find valid interface, ip src %s",
132 		    inet_ntoa(ip_hdr.ip_src));
133 		return;
134 	}
135 
136 	/* header sanity checks */
137 	if (len < sizeof(*dvmrp_hdr)) {
138 		log_warnx("recv_packet: bad packet size");
139 		return;
140 	}
141 	dvmrp_hdr = (struct dvmrp_hdr *)buf;
142 
143 	switch (dvmrp_hdr->type) {
144 	/* DVMRP */
145 	case PKT_TYPE_DVMRP:
146 		if ((l = dvmrp_hdr_sanity_check(&ip_hdr, dvmrp_hdr, len,
147 		    iface)) == -1)
148 			return;
149 
150 		/*
151 		 * mrouted compat
152 		 *
153 		 * Old mrouted versions, send route reports before establishing
154 		 * 2-WAY neighbor relationships.
155 		 */
156 		if ((nbr_find_ip(iface, ip_hdr.ip_src.s_addr) == NULL) &&
157 		    (dvmrp_hdr->code == DVMRP_CODE_REPORT)) {
158 			log_debug("recv_packet: route report from neighbor"
159 			    " ID %s, compat", inet_ntoa(ip_hdr.ip_src));
160 			nbr = nbr_new(ip_hdr.ip_src.s_addr, iface, 0);
161 			nbr_fsm(nbr, NBR_EVT_PROBE_RCVD);
162 			nbr->compat = 1;
163 			nbr->addr = ip_hdr.ip_src;
164 		}
165 
166 		if ((dvmrp_hdr->type == PKT_TYPE_DVMRP) &&
167 		    (dvmrp_hdr->code != DVMRP_CODE_PROBE))
168 			/* find neighbor */
169 			if ((nbr = nbr_find_ip(iface, ip_hdr.ip_src.s_addr))
170 			    == NULL) {
171 				log_debug("recv_packet: unknown neighbor ID");
172 				return;
173 			}
174 
175 		buf += sizeof(*dvmrp_hdr);
176 		len = l - sizeof(*dvmrp_hdr);
177 
178 		inet_aton(AllDVMRPRouters, &addr);
179 		if ((ip_hdr.ip_dst.s_addr != addr.s_addr) &&
180 		    (ip_hdr.ip_dst.s_addr != iface->addr.s_addr)) {
181 			log_debug("recv_packet: interface %s, invalid"
182 			    " destination IP address %s", iface->name,
183 			    inet_ntoa(ip_hdr.ip_dst));
184 			break;
185 		}
186 
187 		switch (dvmrp_hdr->code) {
188 		case DVMRP_CODE_PROBE:
189 			recv_probe(iface, ip_hdr.ip_src, ip_hdr.ip_src.s_addr,
190 			    dvmrp_hdr->capabilities, buf, len);
191 			break;
192 		case DVMRP_CODE_REPORT:
193 			recv_report(nbr, buf, len);
194 			break;
195 		case DVMRP_CODE_ASK_NBRS2:
196 			recv_ask_nbrs2(nbr, buf,len);
197 			break;
198 		case DVMRP_CODE_NBRS2:
199 			recv_nbrs2(nbr, buf,len);
200 			break;
201 		case DVMRP_CODE_PRUNE:
202 			recv_prune(nbr, buf, len);
203 			break;
204 		case DVMRP_CODE_GRAFT:
205 			recv_graft(nbr, buf,len);
206 			break;
207 		case DVMRP_CODE_GRAFT_ACK:
208 			recv_graft_ack(nbr, buf,len);
209 			break;
210 		default:
211 			log_debug("recv_packet: unknown DVMRP packet type, "
212 			    "interface %s", iface->name);
213 		}
214 		break;
215 	/* IGMP */
216 	case PKT_TYPE_MEMBER_QUERY:
217 		recv_igmp_query(iface, ip_hdr.ip_src, buf, len);
218 		break;
219 	case PKT_TYPE_MEMBER_REPORTv1:
220 	case PKT_TYPE_MEMBER_REPORTv2:
221 		recv_igmp_report(iface, ip_hdr.ip_src, buf, len,
222 		    dvmrp_hdr->type);
223 		break;
224 	case PKT_TYPE_LEAVE_GROUPv2:
225 		recv_igmp_leave(iface, ip_hdr.ip_src, buf, len);
226 		break;
227 	default:
228 		log_debug("recv_packet: unknown IGMP packet type, interface %s",
229 		    iface->name);
230 	}
231 }
232 
233 int
234 ip_hdr_sanity_check(const struct ip *ip_hdr, u_int16_t len)
235 {
236 	if (ntohs(ip_hdr->ip_len) != len) {
237 		log_debug("recv_packet: invalid IP packet length %u",
238 		    ntohs(ip_hdr->ip_len));
239 		return (-1);
240 	}
241 
242 	if (ip_hdr->ip_p != IPPROTO_IGMP)
243 		/* this is enforced by the socket itself */
244 		fatalx("recv_packet: invalid IP proto");
245 
246 	return (ip_hdr->ip_hl << 2);
247 }
248 
249 int
250 dvmrp_hdr_sanity_check(const struct ip *ip_hdr, struct dvmrp_hdr *dvmrp_hdr,
251     u_int16_t len, const struct iface *iface)
252 {
253 	/* we only support DVMRPv3 */
254 	if (dvmrp_hdr->major_version != DVMRP_MAJOR_VERSION) {
255 		log_debug("recv_packet: invalid DVMRP version");
256 		return (-1);
257 	}
258 
259 	/* XXX enforce minor version as well, but not yet */
260 
261 	/* XXX chksum */
262 
263 	return (len);
264 }
265 
266 struct iface *
267 find_iface(struct dvmrpd_conf *xconf, struct in_addr src)
268 {
269 	struct iface	*iface = NULL;
270 
271 	/* returned interface needs to be active */
272 	LIST_FOREACH(iface, &xconf->iface_list, entry) {
273 		if (iface->fd > 0 &&
274 		    (iface->type == IF_TYPE_POINTOPOINT) &&
275 		    (iface->dst.s_addr == src.s_addr) &&
276 		    !iface->passive)
277 			return (iface);
278 
279 		if (iface->fd > 0 && (iface->addr.s_addr &
280 		    iface->mask.s_addr) == (src.s_addr &
281 		    iface->mask.s_addr) && !iface->passive)
282 			return (iface);
283 	}
284 
285 	return (NULL);
286 }
287