xref: /openbsd/usr.sbin/dvmrpd/probe.c (revision 898184e3)
1 /*	$OpenBSD: probe.c,v 1.2 2010/05/26 13:56:07 nicm 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 <arpa/inet.h>
23 #include <sys/time.h>
24 #include <sys/socket.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <event.h>
28 
29 #include "igmp.h"
30 #include "dvmrpd.h"
31 #include "dvmrp.h"
32 #include "log.h"
33 #include "dvmrpe.h"
34 
35 extern struct dvmrpd_conf	*deconf;
36 
37 /* DVMRP probe packet handling */
38 int
39 send_probe(struct iface *iface)
40 {
41 	struct sockaddr_in	 dst;
42 	struct ibuf		*buf;
43 	struct dvmrp_hdr	*dvmrp_hdr;
44 	struct nbr		*nbr;
45 	int			 ret = 0;
46 
47 	if (iface->passive)
48 		return (0);
49 
50 	if ((buf = ibuf_open(iface->mtu - sizeof(struct ip))) == NULL)
51 		fatal("send_probe");
52 
53 	/* DVMRP header */
54 	if (gen_dvmrp_hdr(buf, iface, DVMRP_CODE_PROBE))
55 		goto fail;
56 
57 	/* generation ID */
58 	ibuf_add(buf, &iface->gen_id, sizeof(iface->gen_id));
59 
60 	/* generate neighbor list */
61 	LIST_FOREACH(nbr, &iface->nbr_list, entry) {
62 		if (nbr->state > NBR_STA_DOWN)
63 			ibuf_add(buf, &nbr->id, sizeof(nbr->id));
64 	}
65 
66 	/* set destination address */
67 	dst.sin_family = AF_INET;
68 	dst.sin_len = sizeof(struct sockaddr_in);
69 	inet_aton(AllDVMRPRouters, &dst.sin_addr);
70 
71 	/* update chksum */
72 	dvmrp_hdr = ibuf_seek(buf, 0, sizeof(dvmrp_hdr));
73 	dvmrp_hdr->chksum = in_cksum(buf->buf, buf->wpos);
74 
75 	ret = send_packet(iface, buf->buf, buf->wpos, &dst);
76 	ibuf_free(buf);
77 	return (ret);
78 fail:
79 	log_warn("send_probe");
80 	ibuf_free(buf);
81 	return (-1);
82 }
83 
84 void
85 recv_probe(struct iface *iface, struct in_addr src, u_int32_t src_ip,
86     u_int8_t capabilities, char *buf, u_int16_t len)
87 {
88 	struct nbr	*nbr = NULL;
89 	u_int32_t	 gen_id;
90 	u_int32_t	 nbr_id;
91 
92 	LIST_FOREACH(nbr, &iface->nbr_list, entry) {
93 		if (nbr->id.s_addr == src_ip)
94 			break;
95 	}
96 
97 	memcpy(&gen_id, buf, sizeof(gen_id));
98 	len -= sizeof(gen_id);
99 	buf += sizeof(gen_id);
100 
101 	if (!nbr) {
102 		nbr = nbr_new(src_ip, iface, 0);
103 		nbr->gen_id = gen_id;
104 		nbr->capabilities = capabilities;
105 		nbr->addr = src;
106 	}
107 
108 	nbr_fsm(nbr, NBR_EVT_PROBE_RCVD);
109 
110 	if ((nbr->gen_id != gen_id) || (nbr->capabilities != capabilities)) {
111 		if (!nbr->compat)
112 			nbr_fsm(nbr, NBR_EVT_1_WAY_RCVD);
113 		nbr->gen_id = gen_id;
114 		nbr->capabilities = capabilities;
115 
116 		/* XXX handle nbr change! */
117 	}
118 
119 	while (len >= sizeof(nbr_id)) {
120 		memcpy(&nbr_id, buf, sizeof(nbr_id));
121 		if (nbr_id == iface->addr.s_addr) {
122 			/* seen myself */
123 			if (nbr->state < NBR_STA_2_WAY)
124 			nbr_fsm(nbr, NBR_EVT_2_WAY_RCVD);
125 			break;
126 		}
127 		buf += sizeof(nbr_id);
128 		len -= sizeof(nbr_id);
129 	}
130 
131 	if (len == 0) {
132 		nbr_fsm(nbr, NBR_EVT_1_WAY_RCVD);
133 		return;
134 	}
135 
136 	/* XXX len correct?? */
137 
138 	return;
139 }
140