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