summaryrefslogtreecommitdiff
path: root/usr.sbin/ldpd/address.c
blob: 54d60c0f0d0f0b1226047aaecadfc6e1a96cb6c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*	$OpenBSD: address.c,v 1.4 2010/02/25 17:40:46 claudio Exp $ */

/*
 * Copyright (c) 2009 Michele Marchetto <michele@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>

#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <net/if_dl.h>
#include <unistd.h>

#include <errno.h>
#include <event.h>
#include <stdlib.h>
#include <string.h>

#include "ldpd.h"
#include "ldp.h"
#include "log.h"
#include "ldpe.h"

extern struct ldpd_conf        *leconf;

void	gen_address_list_tlv(struct buf *, struct iface *, u_int16_t);

void
send_address(struct nbr *nbr, struct iface *iface)
{
	struct buf	*buf;
	struct iface	*niface;
	u_int16_t	 size, iface_count = 0;

	if (nbr->iface->passive)
		return;

	log_debug("send_address: neighbor ID %s", inet_ntoa(nbr->id));

	if ((buf = buf_open(LDP_MAX_LEN)) == NULL)
		fatal("send_address");

	/* XXX: multiple address on the same iface? */
	if (iface == NULL)
		LIST_FOREACH(niface, &leconf->iface_list, entry)
			iface_count++;
	else
		iface_count = 1;

	size = LDP_HDR_SIZE + sizeof(struct ldp_msg) +
	    sizeof(struct address_list_tlv) +
	    iface_count * sizeof(struct in_addr);

	gen_ldp_hdr(buf, nbr->iface, size);

	size -= LDP_HDR_SIZE;

	gen_msg_tlv(buf, MSG_TYPE_ADDR, size);

	size -= sizeof(struct ldp_msg);

	gen_address_list_tlv(buf, iface, size);

	evbuf_enqueue(&nbr->wbuf, buf);
}

int
recv_address(struct nbr *nbr, char *buf, u_int16_t len)
{
	struct ldp_msg		*addr;
	struct address_list_tlv	*alt;
	struct in_addr		*address;
	u_int32_t		 addrs_len;

	log_debug("recv_address: neighbor ID %s", inet_ntoa(nbr->id));

	if (nbr->state != NBR_STA_OPER)
		return (-1);

	addr = (struct ldp_msg *)buf;

	if ((len - TLV_HDR_LEN) < ntohs(addr->length)) {
		session_shutdown(nbr, S_BAD_MSG_LEN, addr->msgid, addr->type);
		return (-1);
	}

	buf += sizeof(struct ldp_msg);
	len -= sizeof(struct ldp_msg);

	alt = (struct address_list_tlv *)buf;

	if (len < sizeof(*alt) ||
	    (len - TLV_HDR_LEN) < ntohs(alt->length)) {
		session_shutdown(nbr, S_BAD_TLV_LEN, addr->msgid, addr->type);
		return (-1);
	}

	addrs_len = (ntohs(alt->length) - sizeof(alt->family));

	if (alt->type != TLV_TYPE_ADDRLIST) {
		session_shutdown(nbr, S_UNKNOWN_TLV, addr->msgid, addr->type);
		return (-1);
	}

	/* For now we only support IPv4 */
	if (alt->family != htons(ADDR_IPV4)) {
		send_notification_nbr(nbr, S_UNSUP_ADDR, addr->msgid,
		    addr->type);
		return (-1);
	}

	buf += sizeof(*alt);
	len -= sizeof(*alt);
	address = (struct in_addr *)buf;

	while (addrs_len >= sizeof(address)) {
		ldpe_imsg_compose_lde(IMSG_ADDRESS_ADD, nbr->peerid, 0,
		    address, sizeof(*address));

		address++;
		addrs_len -= sizeof(*address);
	}

	return (ntohs(addr->length));
}

void
gen_address_list_tlv(struct buf *buf, struct iface *iface, u_int16_t size)
{
	struct address_list_tlv	 alt;
	struct iface		*niface;

	/* We want just the size of the value */
	size -= TLV_HDR_LEN;

	bzero(&alt, sizeof(alt));
	alt.type = TLV_TYPE_ADDRLIST;
	alt.length = htons(size);
	/* XXX: just ipv4 for now */
	alt.family = htons(ADDR_IPV4);

	buf_add(buf, &alt, sizeof(alt));

	if (iface == NULL)
		LIST_FOREACH(niface, &leconf->iface_list, entry)
			buf_add(buf, &niface->addr, sizeof(niface->addr));
	else
		buf_add(buf, &iface->addr, sizeof(iface->addr));
}

void
send_address_withdraw(struct nbr *nbr, struct iface *iface)
{
	struct buf	*buf;
	u_int16_t	 size;

	if (nbr->iface->passive)
		return;

	log_debug("send_address_withdraw: neighbor ID %s", inet_ntoa(nbr->id));

	if ((buf = buf_open(LDP_MAX_LEN)) == NULL)
		fatal("send_address_withdraw");

	/* XXX: multiple address on the same iface? */
	size = LDP_HDR_SIZE + sizeof(struct ldp_msg) + sizeof(struct in_addr);

	gen_ldp_hdr(buf, nbr->iface, size);

	size -= LDP_HDR_SIZE;

	gen_msg_tlv(buf, MSG_TYPE_ADDRWITHDRAW, size);

	size -= sizeof(struct ldp_msg);

	gen_address_list_tlv(buf, iface, size);

	evbuf_enqueue(&nbr->wbuf, buf);
}

int
recv_address_withdraw(struct nbr *nbr, char *buf, u_int16_t len)
{
	struct ldp_msg		*aw;
	struct address_list_tlv	*alt;
	struct in_addr		*address;

	log_debug("recv_address_withdraw: neighbor ID %s", inet_ntoa(nbr->id));

	aw = (struct ldp_msg *)buf;

	if ((len - TLV_HDR_LEN) < ntohs(aw->length)) {
		session_shutdown(nbr, S_BAD_MSG_LEN, aw->msgid, aw->type);
		return (-1);
	}

	buf += sizeof(struct ldp_msg);
	len -= sizeof(struct ldp_msg);

	alt = (struct address_list_tlv *)buf;

	if (len < sizeof(*alt) ||
	    (len - TLV_HDR_LEN) < ntohs(alt->length)) {
		session_shutdown(nbr, S_BAD_TLV_LEN, aw->msgid, aw->type);
		return (-1);
	}

	if (alt->type != TLV_TYPE_ADDRLIST) {
		session_shutdown(nbr, S_UNKNOWN_TLV, aw->msgid, aw->type);
		return (-1);
	}

	/* For now we just support IPv4 */
	if (alt->family != AF_INET) {
		send_notification_nbr(nbr, S_UNSUP_ADDR, aw->msgid, aw->type);
		return (-1);
	}

	buf += sizeof(*alt);
	len -= sizeof(*alt);
	address = (struct in_addr *)buf;

	while (len >= sizeof(address)) {
		ldpe_imsg_compose_lde(IMSG_ADDRESS_DEL, nbr->peerid, 0,
		    address, sizeof(*address));

		address++;
		len -= sizeof(*address);
	}

	return (ntohs(aw->length));
}