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