Sat Jan 22 19:35:01 2011 UTC ()
use SLIST_FOREACH_SAFE when deleting a peer in loop


(kefren)
diff -r1.3 -r1.4 src/usr.sbin/ldpd/socketops.c

cvs diff -r1.3 -r1.4 src/usr.sbin/ldpd/socketops.c (switch to unified diff)

--- src/usr.sbin/ldpd/socketops.c 2010/12/30 11:29:21 1.3
+++ src/usr.sbin/ldpd/socketops.c 2011/01/22 19:35:00 1.4
@@ -1,1053 +1,1052 @@ @@ -1,1053 +1,1052 @@
1/* $NetBSD: socketops.c,v 1.3 2010/12/30 11:29:21 kefren Exp $ */ 1/* $NetBSD: socketops.c,v 1.4 2011/01/22 19:35:00 kefren Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2010 The NetBSD Foundation, Inc. 4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Mihai Chelaru <kefren@NetBSD.org> 8 * by Mihai Chelaru <kefren@NetBSD.org>
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/types.h> 32#include <sys/types.h>
33#include <sys/stat.h> 33#include <sys/stat.h>
34#include <sys/socket.h> 34#include <sys/socket.h>
35#include <sys/ioctl.h> 35#include <sys/ioctl.h>
36#include <net/if.h> 36#include <net/if.h>
37#include <netinet/in.h> 37#include <netinet/in.h>
38#include <arpa/inet.h> 38#include <arpa/inet.h>
39 39
40#include <errno.h> 40#include <errno.h>
41#include <signal.h> 41#include <signal.h>
42#include <stdlib.h> 42#include <stdlib.h>
43#include <unistd.h> 43#include <unistd.h>
44#include <strings.h> 44#include <strings.h>
45#include <stdio.h> 45#include <stdio.h>
46#include <ifaddrs.h> 46#include <ifaddrs.h>
47#include <poll.h> 47#include <poll.h>
48 48
49#include "fsm.h" 49#include "fsm.h"
50#include "ldp.h" 50#include "ldp.h"
51#include "ldp_command.h" 51#include "ldp_command.h"
52#include "tlv.h" 52#include "tlv.h"
53#include "ldp_peer.h" 53#include "ldp_peer.h"
54#include "notifications.h" 54#include "notifications.h"
55#include "tlv_stack.h" 55#include "tlv_stack.h"
56#include "mpls_interface.h" 56#include "mpls_interface.h"
57#include "label.h" 57#include "label.h"
58#include "mpls_routes.h" 58#include "mpls_routes.h"
59#include "ldp_errors.h" 59#include "ldp_errors.h"
60#include "socketops.h" 60#include "socketops.h"
61 61
62int ls; /* TCP listening socket on port 646 */ 62int ls; /* TCP listening socket on port 646 */
63int route_socket; /* used to see when a route is added/deleted */ 63int route_socket; /* used to see when a route is added/deleted */
64int hello_socket; /* hello multicast listener - transmitter */ 64int hello_socket; /* hello multicast listener - transmitter */
65int command_socket; /* Listening socket for interface command */ 65int command_socket; /* Listening socket for interface command */
66int current_msg_id = 0x233; 66int current_msg_id = 0x233;
67int command_port = LDP_COMMAND_PORT; 67int command_port = LDP_COMMAND_PORT;
68extern int replay_index; 68extern int replay_index;
69extern struct rt_msg replay_rt[REPLAY_MAX]; 69extern struct rt_msg replay_rt[REPLAY_MAX];
70extern struct com_sock csockets[MAX_COMMAND_SOCKETS]; 70extern struct com_sock csockets[MAX_COMMAND_SOCKETS];
71 71
72int ldp_hello_time = LDP_HELLO_TIME; 72int ldp_hello_time = LDP_HELLO_TIME;
73int ldp_keepalive_time = LDP_KEEPALIVE_TIME; 73int ldp_keepalive_time = LDP_KEEPALIVE_TIME;
74int ldp_holddown_time = LDP_HOLDTIME; 74int ldp_holddown_time = LDP_HOLDTIME;
75 75
76void recv_pdu(int); 76void recv_pdu(int);
77void send_hello_alarm(int); 77void send_hello_alarm(int);
78void bail_out(int); 78void bail_out(int);
79static int get_local_addr(struct sockaddr_dl *, struct in_addr *); 79static int get_local_addr(struct sockaddr_dl *, struct in_addr *);
80 80
81int  81int
82create_hello_socket() 82create_hello_socket()
83{ 83{
84 struct ip_mreq mcast_addr; 84 struct ip_mreq mcast_addr;
85 int s = socket(PF_INET, SOCK_DGRAM, 17); 85 int s = socket(PF_INET, SOCK_DGRAM, 17);
86 86
87 if (s < 0) 87 if (s < 0)
88 return s; 88 return s;
89 89
90 /* 90 /*
91 * RFC3036 specifies we should listen to all subnet routers multicast 91 * RFC3036 specifies we should listen to all subnet routers multicast
92 * group 92 * group
93 */ 93 */
94 mcast_addr.imr_multiaddr.s_addr = inet_addr(ALL_ROUTERS); 94 mcast_addr.imr_multiaddr.s_addr = inet_addr(ALL_ROUTERS);
95 mcast_addr.imr_interface.s_addr = htonl(INADDR_ANY); 95 mcast_addr.imr_interface.s_addr = htonl(INADDR_ANY);
96 96
97 socket_reuse_port(s); 97 socket_reuse_port(s);
98 /* Bind it to port 646 on specific address */ 98 /* Bind it to port 646 on specific address */
99 if (bind_socket(s, htonl(INADDR_ANY)) == -1) { 99 if (bind_socket(s, htonl(INADDR_ANY)) == -1) {
100 warnp("Cannot bind hello socket\n"); 100 warnp("Cannot bind hello socket\n");
101 close(s); 101 close(s);
102 return -1; 102 return -1;
103 } 103 }
104 /* We don't need to receive back our messages */ 104 /* We don't need to receive back our messages */
105 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &(uint8_t){0}, 105 if (setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &(uint8_t){0},
106 sizeof(uint8_t)) == -1) { 106 sizeof(uint8_t)) == -1) {
107 fatalp("setsockopt: %s", strerror(errno)); 107 fatalp("setsockopt: %s", strerror(errno));
108 close(s); 108 close(s);
109 return -1; 109 return -1;
110 } 110 }
111 /* Finally join the group */ 111 /* Finally join the group */
112 if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mcast_addr, 112 if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mcast_addr,
113 sizeof(mcast_addr)) == -1) { 113 sizeof(mcast_addr)) == -1) {
114 fatalp("setsockopt: %s", strerror(errno)); 114 fatalp("setsockopt: %s", strerror(errno));
115 close(s); 115 close(s);
116 return -1; 116 return -1;
117 } 117 }
118 /* TTL:1, TOS: 0xc0 */ 118 /* TTL:1, TOS: 0xc0 */
119 if (set_mcast_ttl(s) == -1) { 119 if (set_mcast_ttl(s) == -1) {
120 close(s); 120 close(s);
121 return -1; 121 return -1;
122 } 122 }
123 if (set_tos(s) == -1) { 123 if (set_tos(s) == -1) {
124 fatalp("set_tos: %s", strerror(errno)); 124 fatalp("set_tos: %s", strerror(errno));
125 close(s); 125 close(s);
126 return -1; 126 return -1;
127 } 127 }
128 if (setsockopt(s, IPPROTO_IP, IP_RECVIF, &(uint32_t){1}, sizeof(uint32_t)) == -1) { 128 if (setsockopt(s, IPPROTO_IP, IP_RECVIF, &(uint32_t){1}, sizeof(uint32_t)) == -1) {
129 fatalp("Cannot set IP_RECVIF\n"); 129 fatalp("Cannot set IP_RECVIF\n");
130 close(s); 130 close(s);
131 return -1; 131 return -1;
132 } 132 }
133 hello_socket = s; 133 hello_socket = s;
134 return hello_socket; 134 return hello_socket;
135} 135}
136 136
137/* Sets the TTL to 1 as we don't want to transmit outside this subnet */ 137/* Sets the TTL to 1 as we don't want to transmit outside this subnet */
138int  138int
139set_ttl(int s) 139set_ttl(int s)
140{ 140{
141 int ret; 141 int ret;
142 if ((ret = setsockopt(s, IPPROTO_IP, IP_TTL, &(int){1}, sizeof(int))) 142 if ((ret = setsockopt(s, IPPROTO_IP, IP_TTL, &(int){1}, sizeof(int)))
143 == -1) 143 == -1)
144 fatalp("set_ttl: %s", strerror(errno)); 144 fatalp("set_ttl: %s", strerror(errno));
145 return ret; 145 return ret;
146} 146}
147 147
148/* Sets multicast TTL to 1 */ 148/* Sets multicast TTL to 1 */
149int 149int
150set_mcast_ttl(int s) 150set_mcast_ttl(int s)
151{ 151{
152 int ret; 152 int ret;
153 if ((ret = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &(int){1}, 153 if ((ret = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &(int){1},
154 sizeof(int))) == -1) 154 sizeof(int))) == -1)
155 fatalp("set_mcast_ttl: %s", strerror(errno)); 155 fatalp("set_mcast_ttl: %s", strerror(errno));
156 return ret; 156 return ret;
157} 157}
158 158
159/* Sets TOS to 0xc0 aka IP Precedence 6 */ 159/* Sets TOS to 0xc0 aka IP Precedence 6 */
160int  160int
161set_tos(int s) 161set_tos(int s)
162{ 162{
163 int ret; 163 int ret;
164 if ((ret = setsockopt(s, IPPROTO_IP, IP_TOS, &(int){0xc0}, 164 if ((ret = setsockopt(s, IPPROTO_IP, IP_TOS, &(int){0xc0},
165 sizeof(int))) == -1) 165 sizeof(int))) == -1)
166 fatalp("set_tos: %s", strerror(errno)); 166 fatalp("set_tos: %s", strerror(errno));
167 return ret; 167 return ret;
168} 168}
169 169
170int  170int
171socket_reuse_port(int s) 171socket_reuse_port(int s)
172{ 172{
173 int ret; 173 int ret;
174 if ((ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &(int){1}, 174 if ((ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &(int){1},
175 sizeof(int))) == -1) 175 sizeof(int))) == -1)
176 fatalp("socket_reuse_port: %s", strerror(errno)); 176 fatalp("socket_reuse_port: %s", strerror(errno));
177 return ret; 177 return ret;
178} 178}
179 179
180/* binds an UDP socket */ 180/* binds an UDP socket */
181int  181int
182bind_socket(int s, uint32_t addr) 182bind_socket(int s, uint32_t addr)
183{ 183{
184 struct sockaddr_in sa; 184 struct sockaddr_in sa;
185 185
186 sa.sin_len = sizeof(sa); 186 sa.sin_len = sizeof(sa);
187 sa.sin_family = AF_INET; 187 sa.sin_family = AF_INET;
188 sa.sin_port = htons(LDP_PORT); 188 sa.sin_port = htons(LDP_PORT);
189 sa.sin_addr.s_addr = addr; 189 sa.sin_addr.s_addr = addr;
190 if (bind(s, (struct sockaddr *) (&sa), sizeof(sa))) { 190 if (bind(s, (struct sockaddr *) (&sa), sizeof(sa))) {
191 fatalp("bind_socket: %s", strerror(errno)); 191 fatalp("bind_socket: %s", strerror(errno));
192 return -1; 192 return -1;
193 } 193 }
194 return 0; 194 return 0;
195} 195}
196 196
197/* Create / bind the TCP socket */ 197/* Create / bind the TCP socket */
198int  198int
199create_listening_socket(void) 199create_listening_socket(void)
200{ 200{
201 struct sockaddr_in sa; 201 struct sockaddr_in sa;
202 int s; 202 int s;
203 203
204 sa.sin_len = sizeof(sa); 204 sa.sin_len = sizeof(sa);
205 sa.sin_family = AF_INET; 205 sa.sin_family = AF_INET;
206 sa.sin_port = htons(LDP_PORT); 206 sa.sin_port = htons(LDP_PORT);
207 sa.sin_addr.s_addr = htonl(INADDR_ANY); 207 sa.sin_addr.s_addr = htonl(INADDR_ANY);
208 208
209 s = socket(PF_INET, SOCK_STREAM, 6); 209 s = socket(PF_INET, SOCK_STREAM, 6);
210 if (s < 0) 210 if (s < 0)
211 return s; 211 return s;
212 if (bind(s, (struct sockaddr *) & sa, sizeof(sa))) { 212 if (bind(s, (struct sockaddr *) & sa, sizeof(sa))) {
213 fatalp("bind: %s", strerror(errno)); 213 fatalp("bind: %s", strerror(errno));
214 close(s); 214 close(s);
215 return -1; 215 return -1;
216 } 216 }
217 if (listen(s, 10) == -1) { 217 if (listen(s, 10) == -1) {
218 fatalp("listen: %s", strerror(errno)); 218 fatalp("listen: %s", strerror(errno));
219 close(s); 219 close(s);
220 return -1; 220 return -1;
221 } 221 }
222/* if (set_tos(s) == -1) { 222/* if (set_tos(s) == -1) {
223 fatalp("set_tos: %s", strerror(errno)); 223 fatalp("set_tos: %s", strerror(errno));
224 close(s); 224 close(s);
225 return -1; 225 return -1;
226 } 226 }
227*/ return s; 227*/ return s;
228} 228}
229 229
230/* 230/*
231 * It's ugly. We need a function to pass all tlvs and create pdu but since I 231 * It's ugly. We need a function to pass all tlvs and create pdu but since I
232 * use UDP socket only to send hellos, I didn't bother 232 * use UDP socket only to send hellos, I didn't bother
233 */ 233 */
234void  234void
235send_hello(void) 235send_hello(void)
236{ 236{
237 struct hello_tlv *t; 237 struct hello_tlv *t;
238 struct common_hello_tlv *cht; 238 struct common_hello_tlv *cht;
239 struct ldp_pdu *spdu; 239 struct ldp_pdu *spdu;
240 struct transport_address_tlv *trtlv; 240 struct transport_address_tlv *trtlv;
241 void *v; 241 void *v;
242 struct sockaddr_in sadest; /* Destination ALL_ROUTERS */ 242 struct sockaddr_in sadest; /* Destination ALL_ROUTERS */
243 int sb = 0; /* sent bytes */ 243 int sb = 0; /* sent bytes */
244 struct ifaddrs *ifa, *ifb; 244 struct ifaddrs *ifa, *ifb;
245 struct sockaddr_in *if_sa; 245 struct sockaddr_in *if_sa;
246 char lastifname[20]; 246 char lastifname[20];
247 247
248#define HELLO_MSG_SIZE (sizeof(struct ldp_pdu) + /* PDU */ \ 248#define HELLO_MSG_SIZE (sizeof(struct ldp_pdu) + /* PDU */ \
249 TLV_TYPE_LENGTH + MSGID_SIZE + /* Hello TLV */ \ 249 TLV_TYPE_LENGTH + MSGID_SIZE + /* Hello TLV */ \
250 /* Common Hello TLV */ \ 250 /* Common Hello TLV */ \
251 sizeof(struct common_hello_tlv) + \ 251 sizeof(struct common_hello_tlv) + \
252 /* IPv4 Transport Address */ \ 252 /* IPv4 Transport Address */ \
253 sizeof(struct transport_address_tlv)) 253 sizeof(struct transport_address_tlv))
254 254
255 if ((v = calloc(1, HELLO_MSG_SIZE)) == NULL) { 255 if ((v = calloc(1, HELLO_MSG_SIZE)) == NULL) {
256 fatalp("malloc problem in send_hello()\n"); 256 fatalp("malloc problem in send_hello()\n");
257 return; 257 return;
258 } 258 }
259 259
260 spdu = (struct ldp_pdu *)((char *)v); 260 spdu = (struct ldp_pdu *)((char *)v);
261 t = (struct hello_tlv *)(spdu + 1); 261 t = (struct hello_tlv *)(spdu + 1);
262 cht = &t->ch; /* Hello tlv struct includes CHT */ 262 cht = &t->ch; /* Hello tlv struct includes CHT */
263 trtlv = (struct transport_address_tlv *)(t + 1); 263 trtlv = (struct transport_address_tlv *)(t + 1);
264 264
265 /* Prepare PDU envelope */ 265 /* Prepare PDU envelope */
266 spdu->version = htons(LDP_VERSION); 266 spdu->version = htons(LDP_VERSION);
267 spdu->length = htons(HELLO_MSG_SIZE - PDU_VER_LENGTH); 267 spdu->length = htons(HELLO_MSG_SIZE - PDU_VER_LENGTH);
268 inet_aton(LDP_ID, &spdu->ldp_id); 268 inet_aton(LDP_ID, &spdu->ldp_id);
269 269
270 /* Prepare Hello TLV */ 270 /* Prepare Hello TLV */
271 t->type = htons(LDP_HELLO); 271 t->type = htons(LDP_HELLO);
272 t->length = htons(MSGID_SIZE + 272 t->length = htons(MSGID_SIZE +
273 sizeof(struct common_hello_tlv) + 273 sizeof(struct common_hello_tlv) +
274 sizeof(struct transport_address_tlv)); 274 sizeof(struct transport_address_tlv));
275 /* 275 /*
276 * I used ID 0 instead of htonl(get_message_id()) because I've 276 * I used ID 0 instead of htonl(get_message_id()) because I've
277 * seen hellos from a cisco router doing the same thing 277 * seen hellos from a cisco router doing the same thing
278 */ 278 */
279 t->messageid = 0; 279 t->messageid = 0;
280 280
281 /* Prepare Common Hello attributes */ 281 /* Prepare Common Hello attributes */
282 cht->type = htons(TLV_COMMON_HELLO); 282 cht->type = htons(TLV_COMMON_HELLO);
283 cht->length = htons(sizeof(cht->holdtime) + sizeof(cht->res)); 283 cht->length = htons(sizeof(cht->holdtime) + sizeof(cht->res));
284 cht->holdtime = htons(ldp_holddown_time); 284 cht->holdtime = htons(ldp_holddown_time);
285 cht->res = 0; 285 cht->res = 0;
286 286
287 /* 287 /*
288 * Prepare Transport Address TLV RFC3036 says: "If this optional TLV 288 * Prepare Transport Address TLV RFC3036 says: "If this optional TLV
289 * is not present the IPv4 source address for the UDP packet carrying 289 * is not present the IPv4 source address for the UDP packet carrying
290 * the Hello should be used." But we send it because everybody seems 290 * the Hello should be used." But we send it because everybody seems
291 * to do so 291 * to do so
292 */ 292 */
293 trtlv->type = htons(TLV_IPV4_TRANSPORT); 293 trtlv->type = htons(TLV_IPV4_TRANSPORT);
294 trtlv->length = htons(sizeof(struct in_addr)); 294 trtlv->length = htons(sizeof(struct in_addr));
295 /* trtlv->address will be set for each socket */ 295 /* trtlv->address will be set for each socket */
296 296
297 /* Destination sockaddr */ 297 /* Destination sockaddr */
298 memset(&sadest, 0, sizeof(sadest)); 298 memset(&sadest, 0, sizeof(sadest));
299 sadest.sin_len = sizeof(sadest); 299 sadest.sin_len = sizeof(sadest);
300 sadest.sin_family = AF_INET; 300 sadest.sin_family = AF_INET;
301 sadest.sin_port = htons(LDP_PORT); 301 sadest.sin_port = htons(LDP_PORT);
302 inet_aton(ALL_ROUTERS, &sadest.sin_addr); 302 inet_aton(ALL_ROUTERS, &sadest.sin_addr);
303 303
304 if (getifaddrs(&ifa) == -1) { 304 if (getifaddrs(&ifa) == -1) {
305 free(v); 305 free(v);
306 return; 306 return;
307 } 307 }
308  308
309 lastifname[0] = '\0'; 309 lastifname[0] = '\0';
310 for (ifb = ifa; ifb; ifb = ifb->ifa_next) { 310 for (ifb = ifa; ifb; ifb = ifb->ifa_next) {
311 if_sa = (struct sockaddr_in *) ifb->ifa_addr; 311 if_sa = (struct sockaddr_in *) ifb->ifa_addr;
312 if (if_sa->sin_family != AF_INET) 312 if (if_sa->sin_family != AF_INET)
313 continue; 313 continue;
314 if (ntohl(if_sa->sin_addr.s_addr) >> 24 == IN_LOOPBACKNET) 314 if (ntohl(if_sa->sin_addr.s_addr) >> 24 == IN_LOOPBACKNET)
315 continue; 315 continue;
316 /* Send only once per interface, using master address */ 316 /* Send only once per interface, using master address */
317 if (strcmp(ifb->ifa_name, lastifname) == 0) 317 if (strcmp(ifb->ifa_name, lastifname) == 0)
318 continue; 318 continue;
319 debugp("Sending hello on %s\n", ifb->ifa_name); 319 debugp("Sending hello on %s\n", ifb->ifa_name);
320 if (setsockopt(hello_socket, IPPROTO_IP, IP_MULTICAST_IF, 320 if (setsockopt(hello_socket, IPPROTO_IP, IP_MULTICAST_IF,
321 &if_sa->sin_addr, sizeof(struct in_addr)) == -1) { 321 &if_sa->sin_addr, sizeof(struct in_addr)) == -1) {
322 warnp("setsockopt failed: %s\n", strerror(errno)); 322 warnp("setsockopt failed: %s\n", strerror(errno));
323 continue; 323 continue;
324 } 324 }
325 trtlv->address.s_addr = if_sa->sin_addr.s_addr; 325 trtlv->address.s_addr = if_sa->sin_addr.s_addr;
326 326
327 strlcpy(lastifname, ifb->ifa_name, sizeof(lastifname)); 327 strlcpy(lastifname, ifb->ifa_name, sizeof(lastifname));
328 328
329 /* Send to the wire */ 329 /* Send to the wire */
330 sb = sendto(hello_socket, v, HELLO_MSG_SIZE, 330 sb = sendto(hello_socket, v, HELLO_MSG_SIZE,
331 0, (struct sockaddr *) & sadest, sizeof(sadest)); 331 0, (struct sockaddr *) & sadest, sizeof(sadest));
332 if (sb < (int)HELLO_MSG_SIZE) 332 if (sb < (int)HELLO_MSG_SIZE)
333 fatalp("send: %s", strerror(errno)); 333 fatalp("send: %s", strerror(errno));
334 else 334 else
335 debugp("Send %d bytes (PDU: %d, Hello TLV: %d, CH: %d)\n", 335 debugp("Send %d bytes (PDU: %d, Hello TLV: %d, CH: %d)\n",
336 sb, (int) (sizeof(struct ldp_pdu) - PDU_VER_LENGTH), 336 sb, (int) (sizeof(struct ldp_pdu) - PDU_VER_LENGTH),
337 (int) (TLV_TYPE_LENGTH + MSGID_SIZE), 337 (int) (TLV_TYPE_LENGTH + MSGID_SIZE),
338 (int) (sizeof(struct common_hello_tlv))); 338 (int) (sizeof(struct common_hello_tlv)));
339 339
340 } 340 }
341 freeifaddrs(ifa); 341 freeifaddrs(ifa);
342 free(v); 342 free(v);
343} 343}
344 344
345int 345int
346get_message_id(void) 346get_message_id(void)
347{ 347{
348 current_msg_id++; 348 current_msg_id++;
349 return current_msg_id; 349 return current_msg_id;
350} 350}
351 351
352static int 352static int
353get_local_addr(struct sockaddr_dl *sdl, struct in_addr *sin) 353get_local_addr(struct sockaddr_dl *sdl, struct in_addr *sin)
354{ 354{
355 struct ifaddrs *ifa, *ifb; 355 struct ifaddrs *ifa, *ifb;
356 struct sockaddr_in *sinet; 356 struct sockaddr_in *sinet;
357 357
358 if (sdl == NULL) 358 if (sdl == NULL)
359 return -1; 359 return -1;
360 360
361 if (getifaddrs(&ifa) == -1) 361 if (getifaddrs(&ifa) == -1)
362 return -1; 362 return -1;
363 for (ifb = ifa; ifb; ifb = ifb->ifa_next) 363 for (ifb = ifa; ifb; ifb = ifb->ifa_next)
364 if (ifb->ifa_addr->sa_family == AF_INET) { 364 if (ifb->ifa_addr->sa_family == AF_INET) {
365 if (if_nametoindex(ifb->ifa_name) != sdl->sdl_index) 365 if (if_nametoindex(ifb->ifa_name) != sdl->sdl_index)
366 continue; 366 continue;
367 sinet = (struct sockaddr_in*) ifb->ifa_addr; 367 sinet = (struct sockaddr_in*) ifb->ifa_addr;
368 sin->s_addr = sinet->sin_addr.s_addr; 368 sin->s_addr = sinet->sin_addr.s_addr;
369 freeifaddrs(ifa); 369 freeifaddrs(ifa);
370 return 0; 370 return 0;
371 } 371 }
372 freeifaddrs(ifa); 372 freeifaddrs(ifa);
373 return -1; 373 return -1;
374} 374}
375 375
376/* Receive PDUs on Multicast UDP socket */ 376/* Receive PDUs on Multicast UDP socket */
377void 377void
378recv_pdu(int sock) 378recv_pdu(int sock)
379{ 379{
380 struct ldp_pdu rpdu; 380 struct ldp_pdu rpdu;
381 int c, i; 381 int c, i;
382 struct msghdr msg; 382 struct msghdr msg;
383 struct iovec iov[1]; 383 struct iovec iov[1];
384 unsigned char recvspace[MAX_PDU_SIZE]; 384 unsigned char recvspace[MAX_PDU_SIZE];
385 struct hello_tlv *t; 385 struct hello_tlv *t;
386 struct sockaddr_in fromsa; 386 struct sockaddr_in fromsa;
387 struct sockaddr_dl *sdl = NULL; 387 struct sockaddr_dl *sdl = NULL;
388 struct in_addr my_ldp_addr, local_addr; 388 struct in_addr my_ldp_addr, local_addr;
389 struct cmsghdr *cmptr; 389 struct cmsghdr *cmptr;
390 union { 390 union {
391 struct cmsghdr cm; 391 struct cmsghdr cm;
392 char control[1024]; 392 char control[1024];
393 } control_un; 393 } control_un;
394 394
395 debugp("Entering RECV_PDU\n"); 395 debugp("Entering RECV_PDU\n");
396 396
397 memset(&msg, 0, sizeof(msg)); 397 memset(&msg, 0, sizeof(msg));
398 msg.msg_control = control_un.control; 398 msg.msg_control = control_un.control;
399 msg.msg_controllen = sizeof(control_un.control); 399 msg.msg_controllen = sizeof(control_un.control);
400 msg.msg_flags = 0; 400 msg.msg_flags = 0;
401 msg.msg_name = &fromsa; 401 msg.msg_name = &fromsa;
402 msg.msg_namelen = sizeof(fromsa); 402 msg.msg_namelen = sizeof(fromsa);
403 iov[0].iov_base = recvspace; 403 iov[0].iov_base = recvspace;
404 iov[0].iov_len = sizeof(recvspace); 404 iov[0].iov_len = sizeof(recvspace);
405 msg.msg_iov = iov; 405 msg.msg_iov = iov;
406 msg.msg_iovlen = 1; 406 msg.msg_iovlen = 1;
407 407
408 c = recvmsg(sock, &msg, MSG_WAITALL); 408 c = recvmsg(sock, &msg, MSG_WAITALL);
409 debugp("Incoming PDU size: %d\n", c); 409 debugp("Incoming PDU size: %d\n", c);
410 410
411 debugp("PDU from: %s\n", inet_ntoa(fromsa.sin_addr)); 411 debugp("PDU from: %s\n", inet_ntoa(fromsa.sin_addr));
412 412
413 /* Check to see if this is larger than MIN_PDU_SIZE */ 413 /* Check to see if this is larger than MIN_PDU_SIZE */
414 if (c < MIN_PDU_SIZE) 414 if (c < MIN_PDU_SIZE)
415 return; 415 return;
416 416
417 /* Read the PDU */ 417 /* Read the PDU */
418 i = get_pdu(recvspace, &rpdu); 418 i = get_pdu(recvspace, &rpdu);
419 419
420 /* We currently understand Version 1 */ 420 /* We currently understand Version 1 */
421 if (rpdu.version != LDP_VERSION) { 421 if (rpdu.version != LDP_VERSION) {
422 fatalp("recv_pdu: Version mismatch\n"); 422 fatalp("recv_pdu: Version mismatch\n");
423 return; 423 return;
424 } 424 }
425 425
426 /* Maybe it's our hello */ 426 /* Maybe it's our hello */
427 inet_aton(LDP_ID, &my_ldp_addr); 427 inet_aton(LDP_ID, &my_ldp_addr);
428 if (rpdu.ldp_id.s_addr == my_ldp_addr.s_addr) { 428 if (rpdu.ldp_id.s_addr == my_ldp_addr.s_addr) {
429 fatalp("Received our PDU..\n"); /* it should be not looped */ 429 fatalp("Received our PDU..\n"); /* it should be not looped */
430 return; 430 return;
431 } 431 }
432 432
433 if (msg.msg_controllen < (socklen_t)sizeof(struct cmsghdr) || 433 if (msg.msg_controllen < (socklen_t)sizeof(struct cmsghdr) ||
434 (msg.msg_flags & MSG_CTRUNC)) 434 (msg.msg_flags & MSG_CTRUNC))
435 local_addr.s_addr = my_ldp_addr.s_addr; 435 local_addr.s_addr = my_ldp_addr.s_addr;
436 else { 436 else {
437 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr != NULL; 437 for (cmptr = CMSG_FIRSTHDR(&msg); cmptr != NULL;
438 cmptr = CMSG_NXTHDR(&msg, cmptr)) 438 cmptr = CMSG_NXTHDR(&msg, cmptr))
439 if (cmptr->cmsg_level == IPPROTO_IP && 439 if (cmptr->cmsg_level == IPPROTO_IP &&
440 cmptr->cmsg_type == IP_RECVIF) { 440 cmptr->cmsg_type == IP_RECVIF) {
441 sdl = (struct sockaddr_dl *) CMSG_DATA(cmptr); 441 sdl = (struct sockaddr_dl *) CMSG_DATA(cmptr);
442 break; 442 break;
443 } 443 }
444 if (get_local_addr(sdl, &local_addr) != 0) 444 if (get_local_addr(sdl, &local_addr) != 0)
445 local_addr.s_addr = my_ldp_addr.s_addr; 445 local_addr.s_addr = my_ldp_addr.s_addr;
446 } 446 }
447 447
448 448
449 debugp("Read %d bytes from address %s Length: %.4d Version: %d\n", 449 debugp("Read %d bytes from address %s Length: %.4d Version: %d\n",
450 c, inet_ntoa(rpdu.ldp_id), rpdu.length, rpdu.version); 450 c, inet_ntoa(rpdu.ldp_id), rpdu.length, rpdu.version);
451 451
452 /* Fill the TLV messages */ 452 /* Fill the TLV messages */
453 t = get_hello_tlv(recvspace + i, c - i); 453 t = get_hello_tlv(recvspace + i, c - i);
454 run_ldp_hello(&rpdu, t, &fromsa.sin_addr, &local_addr, sock); 454 run_ldp_hello(&rpdu, t, &fromsa.sin_addr, &local_addr, sock);
455} 455}
456 456
457void  457void
458send_hello_alarm(int unused) 458send_hello_alarm(int unused)
459{ 459{
460 struct ldp_peer *p; 460 struct ldp_peer *p, *ptmp;
461 struct hello_info *hi, *hinext; 461 struct hello_info *hi, *hinext;
462 time_t t = time(NULL); 462 time_t t = time(NULL);
463 int olderrno = errno; 463 int olderrno = errno;
464 464
465 /* Send hellos */ 465 /* Send hellos */
466 if (!(t % ldp_hello_time)) 466 if (!(t % ldp_hello_time))
467 send_hello(); 467 send_hello();
468 468
469 /* Timeout -- */ 469 /* Timeout -- */
470 SLIST_FOREACH(p, &ldp_peer_head, peers) 470 SLIST_FOREACH(p, &ldp_peer_head, peers)
471 p->timeout--; 471 p->timeout--;
472 472
473 /* Check for timeout */ 473 /* Check for timeout */
474check_peer: 474 SLIST_FOREACH_SAFE(p, &ldp_peer_head, peers, ptmp)
475 SLIST_FOREACH(p, &ldp_peer_head, peers) 
476 if (p->timeout < 1) 475 if (p->timeout < 1)
477 switch (p->state) { 476 switch (p->state) {
478 case LDP_PEER_HOLDDOWN: 477 case LDP_PEER_HOLDDOWN:
479 debugp("LDP holddown expired for peer %s\n", 478 debugp("LDP holddown expired for peer %s\n",
480 inet_ntoa(p->ldp_id)); 479 inet_ntoa(p->ldp_id));
481 ldp_peer_delete(p); 480 ldp_peer_delete(p);
482 goto check_peer; 481 break;
483 case LDP_PEER_ESTABLISHED: 482 case LDP_PEER_ESTABLISHED:
484 case LDP_PEER_CONNECTED: 483 case LDP_PEER_CONNECTED:
485 send_notification(p, 0, 484 send_notification(p, 0,
486 NOTIF_KEEP_ALIVE_TIMER_EXPIRED); 485 NOTIF_KEEP_ALIVE_TIMER_EXPIRED);
487 warnp("Keepalive expired for %s\n", 486 warnp("Keepalive expired for %s\n",
488 inet_ntoa(p->ldp_id)); 487 inet_ntoa(p->ldp_id));
489 ldp_peer_holddown(p); 488 ldp_peer_holddown(p);
490 break; 489 break;
491 } /* switch */ 490 } /* switch */
492 491
493 /* send keepalives */ 492 /* send keepalives */
494 if (!(t % ldp_keepalive_time)) { 493 if (!(t % ldp_keepalive_time)) {
495 SLIST_FOREACH(p, &ldp_peer_head, peers)  494 SLIST_FOREACH(p, &ldp_peer_head, peers)
496 if (p->state == LDP_PEER_ESTABLISHED) { 495 if (p->state == LDP_PEER_ESTABLISHED) {
497 debugp("Sending KeepAlive to %s\n", 496 debugp("Sending KeepAlive to %s\n",
498 inet_ntoa(p->ldp_id)); 497 inet_ntoa(p->ldp_id));
499 keep_alive(p); 498 keep_alive(p);
500 } 499 }
501 } 500 }
502 501
503 /* Decrement hello info keepalives */ 502 /* Decrement hello info keepalives */
504 SLIST_FOREACH(hi, &hello_info_head, infos) 503 SLIST_FOREACH(hi, &hello_info_head, infos)
505 hi->keepalive--; 504 hi->keepalive--;
506 505
507 /* Check hello keepalives */ 506 /* Check hello keepalives */
508 SLIST_FOREACH_SAFE(hi, &hello_info_head, infos, hinext) 507 SLIST_FOREACH_SAFE(hi, &hello_info_head, infos, hinext)
509 if (hi->keepalive < 1) 508 if (hi->keepalive < 1)
510 SLIST_REMOVE(&hello_info_head, hi, hello_info, infos); 509 SLIST_REMOVE(&hello_info_head, hi, hello_info, infos);
511 510
512 /* Set the alarm again and bail out */ 511 /* Set the alarm again and bail out */
513 alarm(1); 512 alarm(1);
514 errno = olderrno; 513 errno = olderrno;
515} 514}
516 515
517void  516void
518bail_out(int x) 517bail_out(int x)
519{ 518{
520 ldp_peer_holddown_all(); 519 ldp_peer_holddown_all();
521 flush_mpls_routes(); 520 flush_mpls_routes();
522 exit(0); 521 exit(0);
523} 522}
524 523
525/* 524/*
526 * The big poll that catches every single event 525 * The big poll that catches every single event
527 * on every socket. 526 * on every socket.
528 */ 527 */
529void  528void
530the_big_loop(void) 529the_big_loop(void)
531{ 530{
532 int sock_error; 531 int sock_error;
533 uint32_t i; 532 uint32_t i;
534 socklen_t sock_error_size = sizeof(int); 533 socklen_t sock_error_size = sizeof(int);
535 struct ldp_peer *p; 534 struct ldp_peer *p;
536 struct com_sock *cs; 535 struct com_sock *cs;
537 struct pollfd pfd[MAX_POLL_FDS]; 536 struct pollfd pfd[MAX_POLL_FDS];
538 537
539 SLIST_INIT(&hello_info_head); 538 SLIST_INIT(&hello_info_head);
540 539
541 signal(SIGALRM, send_hello_alarm); 540 signal(SIGALRM, send_hello_alarm);
542 signal(SIGPIPE, SIG_IGN); 541 signal(SIGPIPE, SIG_IGN);
543 signal(SIGTERM, bail_out); 542 signal(SIGTERM, bail_out);
544 send_hello_alarm(1); 543 send_hello_alarm(1);
545 544
546 route_socket = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC); 545 route_socket = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
547 546
548 if (bind_current_routes() != LDP_E_OK) 547 if (bind_current_routes() != LDP_E_OK)
549 fatalp("Cannot get current routes\n"); 548 fatalp("Cannot get current routes\n");
550 549
551 for (;;) { 550 for (;;) {
552 nfds_t pollsum = 4; 551 nfds_t pollsum = 4;
553 552
554 pfd[0].fd = ls; 553 pfd[0].fd = ls;
555 pfd[0].events = POLLRDNORM; 554 pfd[0].events = POLLRDNORM;
556 pfd[0].revents = 0; 555 pfd[0].revents = 0;
557 556
558 pfd[1].fd = route_socket; 557 pfd[1].fd = route_socket;
559 pfd[1].events = POLLRDNORM; 558 pfd[1].events = POLLRDNORM;
560 pfd[1].revents = 0; 559 pfd[1].revents = 0;
561 560
562 pfd[2].fd = command_socket; 561 pfd[2].fd = command_socket;
563 pfd[2].events = POLLRDNORM; 562 pfd[2].events = POLLRDNORM;
564 pfd[2].revents = 0; 563 pfd[2].revents = 0;
565 564
566 /* Hello socket */ 565 /* Hello socket */
567 pfd[3].fd = hello_socket; 566 pfd[3].fd = hello_socket;
568 pfd[3].events = POLLIN; 567 pfd[3].events = POLLIN;
569 pfd[3].revents = 0; 568 pfd[3].revents = 0;
570 569
571 /* Command sockets */ 570 /* Command sockets */
572 for (i=0; i < MAX_COMMAND_SOCKETS; i++) 571 for (i=0; i < MAX_COMMAND_SOCKETS; i++)
573 if (csockets[i].socket != -1) { 572 if (csockets[i].socket != -1) {
574 pfd[pollsum].fd = csockets[i].socket; 573 pfd[pollsum].fd = csockets[i].socket;
575 pfd[pollsum].events = POLLIN; 574 pfd[pollsum].events = POLLIN;
576 pfd[pollsum].revents = 0; 575 pfd[pollsum].revents = 0;
577 pollsum++; 576 pollsum++;
578 } 577 }
579 578
580 /* LDP Peer sockets */ 579 /* LDP Peer sockets */
581 SLIST_FOREACH(p, &ldp_peer_head, peers) { 580 SLIST_FOREACH(p, &ldp_peer_head, peers) {
582 if (p->socket < 1) 581 if (p->socket < 1)
583 continue; 582 continue;
584 switch (p->state) { 583 switch (p->state) {
585 case LDP_PEER_CONNECTED: 584 case LDP_PEER_CONNECTED:
586 case LDP_PEER_ESTABLISHED: 585 case LDP_PEER_ESTABLISHED:
587 pfd[pollsum].fd = p->socket; 586 pfd[pollsum].fd = p->socket;
588 pfd[pollsum].events = POLLRDNORM; 587 pfd[pollsum].events = POLLRDNORM;
589 pfd[pollsum].revents = 0; 588 pfd[pollsum].revents = 0;
590 pollsum++; 589 pollsum++;
591 break; 590 break;
592 case LDP_PEER_CONNECTING: 591 case LDP_PEER_CONNECTING:
593 pfd[pollsum].fd = p->socket; 592 pfd[pollsum].fd = p->socket;
594 pfd[pollsum].events = POLLWRNORM; 593 pfd[pollsum].events = POLLWRNORM;
595 pfd[pollsum].revents = 0; 594 pfd[pollsum].revents = 0;
596 pollsum++; 595 pollsum++;
597 break; 596 break;
598 } 597 }
599 } 598 }
600 599
601 if (pollsum >= MAX_POLL_FDS) { 600 if (pollsum >= MAX_POLL_FDS) {
602 fatalp("Too many sockets. Increase MAX_POLL_FDS\n"); 601 fatalp("Too many sockets. Increase MAX_POLL_FDS\n");
603 return; 602 return;
604 } 603 }
605 if (poll(pfd, pollsum, INFTIM) < 0) { 604 if (poll(pfd, pollsum, INFTIM) < 0) {
606 if (errno != EINTR) 605 if (errno != EINTR)
607 fatalp("poll: %s", strerror(errno)); 606 fatalp("poll: %s", strerror(errno));
608 continue; 607 continue;
609 } 608 }
610 609
611 for (i = 0; i < pollsum; i++) { 610 for (i = 0; i < pollsum; i++) {
612 if ((pfd[i].revents & POLLRDNORM) || 611 if ((pfd[i].revents & POLLRDNORM) ||
613 (pfd[i].revents & POLLIN)) { 612 (pfd[i].revents & POLLIN)) {
614 if(pfd[i].fd == ls) { 613 if(pfd[i].fd == ls) {
615 new_peer_connection(); 614 new_peer_connection();
616 } else if (pfd[i].fd == route_socket) { 615 } else if (pfd[i].fd == route_socket) {
617 struct rt_msg xbuf; 616 struct rt_msg xbuf;
618 int l, to_read; 617 int l, to_read;
619 do { 618 do {
620 l = recv(route_socket, &xbuf, 619 l = recv(route_socket, &xbuf,
621 sizeof(struct rt_msg), MSG_PEEK); 620 sizeof(struct rt_msg), MSG_PEEK);
622 } while ((l == -1) && (errno == EINTR)); 621 } while ((l == -1) && (errno == EINTR));
623 622
624 if (l == -1) 623 if (l == -1)
625 break; 624 break;
626 625
627 to_read = l; 626 to_read = l;
628 l = 0; 627 l = 0;
629 do { 628 do {
630 l += recv(route_socket, &xbuf, 629 l += recv(route_socket, &xbuf,
631 to_read - l, MSG_WAITALL); 630 to_read - l, MSG_WAITALL);
632 } while (l != to_read); 631 } while (l != to_read);
633 632
634 check_route(&xbuf, to_read); 633 check_route(&xbuf, to_read);
635 634
636 } else if (pfd[i].fd == hello_socket) { 635 } else if (pfd[i].fd == hello_socket) {
637 /* Receiving hello socket */ 636 /* Receiving hello socket */
638 recv_pdu(pfd[i].fd); 637 recv_pdu(pfd[i].fd);
639 } else if (pfd[i].fd == command_socket) { 638 } else if (pfd[i].fd == command_socket) {
640 command_accept(command_socket); 639 command_accept(command_socket);
641 } else if ((cs = is_command_socket(pfd[i].fd)) 640 } else if ((cs = is_command_socket(pfd[i].fd))
642 != NULL) { 641 != NULL) {
643 command_dispatch(cs); 642 command_dispatch(cs);
644 } else { 643 } else {
645 /* ldp peer socket */ 644 /* ldp peer socket */
646 p = get_ldp_peer_by_socket(pfd[i].fd); 645 p = get_ldp_peer_by_socket(pfd[i].fd);
647 if (p) 646 if (p)
648 recv_session_pdu(p); 647 recv_session_pdu(p);
649 } 648 }
650 } else if(pfd[i].revents & POLLWRNORM) { 649 } else if(pfd[i].revents & POLLWRNORM) {
651 p = get_ldp_peer_by_socket(pfd[i].fd); 650 p = get_ldp_peer_by_socket(pfd[i].fd);
652 if (!p) 651 if (!p)
653 continue; 652 continue;
654 if ((getsockopt(pfd[i].fd, SOL_SOCKET, SO_ERROR, 653 if ((getsockopt(pfd[i].fd, SOL_SOCKET, SO_ERROR,
655 &sock_error, &sock_error_size) != 0) || 654 &sock_error, &sock_error_size) != 0) ||
656 (sock_error)) { 655 (sock_error)) {
657 ldp_peer_holddown(p); 656 ldp_peer_holddown(p);
658 } else { 657 } else {
659 p->state = LDP_PEER_CONNECTED; 658 p->state = LDP_PEER_CONNECTED;
660 send_initialize(p); 659 send_initialize(p);
661 } 660 }
662 } 661 }
663 } 662 }
664 663
665 for (int ri = 0; ri < replay_index; ri++) { 664 for (int ri = 0; ri < replay_index; ri++) {
666 debugp("Replaying: PID %d, SEQ %d\n", 665 debugp("Replaying: PID %d, SEQ %d\n",
667 replay_rt[ri].m_rtm.rtm_pid, 666 replay_rt[ri].m_rtm.rtm_pid,
668 replay_rt[ri].m_rtm.rtm_seq); 667 replay_rt[ri].m_rtm.rtm_seq);
669 check_route(&replay_rt[ri], sizeof(struct rt_msg)); 668 check_route(&replay_rt[ri], sizeof(struct rt_msg));
670 } 669 }
671 replay_index = 0; 670 replay_index = 0;
672 } /* for (;;) */ 671 } /* for (;;) */
673} 672}
674 673
675void  674void
676new_peer_connection() 675new_peer_connection()
677{ 676{
678 struct sockaddr_in sa, sin_me; 677 struct sockaddr_in sa, sin_me;
679 int s; 678 int s;
680 679
681 s = accept(ls, (struct sockaddr *) & sa, 680 s = accept(ls, (struct sockaddr *) & sa,
682 & (socklen_t) { sizeof(struct sockaddr_in) } ); 681 & (socklen_t) { sizeof(struct sockaddr_in) } );
683 if (s < 0) { 682 if (s < 0) {
684 fatalp("accept: %s", strerror(errno)); 683 fatalp("accept: %s", strerror(errno));
685 return; 684 return;
686 } 685 }
687 686
688 if (get_ldp_peer(&sa.sin_addr) != NULL) { 687 if (get_ldp_peer(&sa.sin_addr) != NULL) {
689 close(s); 688 close(s);
690 return; 689 return;
691 } 690 }
692 691
693 warnp("Accepted a connection from %s\n", inet_ntoa(sa.sin_addr)); 692 warnp("Accepted a connection from %s\n", inet_ntoa(sa.sin_addr));
694 693
695 if (getsockname(s, (struct sockaddr *)&sin_me, 694 if (getsockname(s, (struct sockaddr *)&sin_me,
696 & (socklen_t) { sizeof(struct sockaddr_in) } )) { 695 & (socklen_t) { sizeof(struct sockaddr_in) } )) {
697 fatalp("new_peer_connection(): cannot getsockname\n"); 696 fatalp("new_peer_connection(): cannot getsockname\n");
698 close(s); 697 close(s);
699 return; 698 return;
700 } 699 }
701 700
702 if (ntohl(sa.sin_addr.s_addr) < ntohl(sin_me.sin_addr.s_addr)) { 701 if (ntohl(sa.sin_addr.s_addr) < ntohl(sin_me.sin_addr.s_addr)) {
703 fatalp("Peer %s: connect from lower ID\n", 702 fatalp("Peer %s: connect from lower ID\n",
704 inet_ntoa(sa.sin_addr)); 703 inet_ntoa(sa.sin_addr));
705 close(s); 704 close(s);
706 return; 705 return;
707 } 706 }
708 /* XXX: sa.sin_addr ain't peer LDP ID ... */ 707 /* XXX: sa.sin_addr ain't peer LDP ID ... */
709 ldp_peer_new(&sa.sin_addr, &sa.sin_addr, NULL, ldp_holddown_time, s); 708 ldp_peer_new(&sa.sin_addr, &sa.sin_addr, NULL, ldp_holddown_time, s);
710 709
711} 710}
712 711
713void  712void
714send_initialize(struct ldp_peer * p) 713send_initialize(struct ldp_peer * p)
715{ 714{
716 struct init_tlv ti; 715 struct init_tlv ti;
717 716
718 ti.type = htons(LDP_INITIALIZE); 717 ti.type = htons(LDP_INITIALIZE);
719 ti.length = htons(sizeof(struct init_tlv) - TLV_TYPE_LENGTH); 718 ti.length = htons(sizeof(struct init_tlv) - TLV_TYPE_LENGTH);
720 ti.messageid = htonl(get_message_id()); 719 ti.messageid = htonl(get_message_id());
721 ti.cs_type = htons(TLV_COMMON_SESSION); 720 ti.cs_type = htons(TLV_COMMON_SESSION);
722 ti.cs_len = htons(CS_LEN); 721 ti.cs_len = htons(CS_LEN);
723 ti.cs_version = htons(LDP_VERSION); 722 ti.cs_version = htons(LDP_VERSION);
724 ti.cs_keepalive = htons(2 * ldp_keepalive_time); 723 ti.cs_keepalive = htons(2 * ldp_keepalive_time);
725 ti.cs_adpvlim = 0; 724 ti.cs_adpvlim = 0;
726 ti.cs_maxpdulen = htons(MAX_PDU_SIZE); 725 ti.cs_maxpdulen = htons(MAX_PDU_SIZE);
727 ti.cs_peeraddress.s_addr = p->ldp_id.s_addr; 726 ti.cs_peeraddress.s_addr = p->ldp_id.s_addr;
728 ti.cs_peeraddrspace = 0; 727 ti.cs_peeraddrspace = 0;
729 728
730 send_tlv(p, (struct tlv *) (void *) &ti); 729 send_tlv(p, (struct tlv *) (void *) &ti);
731} 730}
732 731
733void  732void
734keep_alive(struct ldp_peer * p) 733keep_alive(struct ldp_peer * p)
735{ 734{
736 struct ka_tlv kt; 735 struct ka_tlv kt;
737 736
738 kt.type = htons(LDP_KEEPALIVE); 737 kt.type = htons(LDP_KEEPALIVE);
739 kt.length = htons(sizeof(kt.messageid)); 738 kt.length = htons(sizeof(kt.messageid));
740 kt.messageid = htonl(get_message_id()); 739 kt.messageid = htonl(get_message_id());
741 740
742 send_tlv(p, (struct tlv *) (void *) &kt); 741 send_tlv(p, (struct tlv *) (void *) &kt);
743 742
744} 743}
745 744
746void  745void
747recv_session_pdu(struct ldp_peer * p) 746recv_session_pdu(struct ldp_peer * p)
748{ 747{
749 struct ldp_pdu *rpdu; 748 struct ldp_pdu *rpdu;
750 struct address_tlv *atlv; 749 struct address_tlv *atlv;
751 struct al_tlv *altlv; 750 struct al_tlv *altlv;
752 struct init_tlv *itlv; 751 struct init_tlv *itlv;
753 struct label_map_tlv *lmtlv; 752 struct label_map_tlv *lmtlv;
754 struct fec_tlv *fectlv; 753 struct fec_tlv *fectlv;
755 struct label_tlv *__packed labeltlv; 754 struct label_tlv *__packed labeltlv;
756 struct notification_tlv *nottlv; 755 struct notification_tlv *nottlv;
757 struct hello_info *hi; 756 struct hello_info *hi;
758 757
759 int c; 758 int c;
760 int32_t wo = 0; 759 int32_t wo = 0;
761 struct tlv *ttmp; 760 struct tlv *ttmp;
762 unsigned char recvspace[MAX_PDU_SIZE]; 761 unsigned char recvspace[MAX_PDU_SIZE];
763 762
764 memset(recvspace, 0, MAX_PDU_SIZE); 763 memset(recvspace, 0, MAX_PDU_SIZE);
765 764
766 c = recv(p->socket, (void *) recvspace, MAX_PDU_SIZE, MSG_PEEK); 765 c = recv(p->socket, (void *) recvspace, MAX_PDU_SIZE, MSG_PEEK);
767 766
768 debugp("Ready to read %d bytes\n", c); 767 debugp("Ready to read %d bytes\n", c);
769 768
770 if (c < 1) { /* Session closed */ 769 if (c < 1) { /* Session closed */
771 warnp("Error in connection with %s\n", inet_ntoa(p->ldp_id)); 770 warnp("Error in connection with %s\n", inet_ntoa(p->ldp_id));
772 ldp_peer_holddown(p); 771 ldp_peer_holddown(p);
773 return; 772 return;
774 } 773 }
775 if (c > MAX_PDU_SIZE) { 774 if (c > MAX_PDU_SIZE) {
776 debugp("Incoming PDU size exceeds MAX_PDU_SIZE !\n"); 775 debugp("Incoming PDU size exceeds MAX_PDU_SIZE !\n");
777 return; 776 return;
778 } 777 }
779 if (c < MIN_PDU_SIZE) { 778 if (c < MIN_PDU_SIZE) {
780 debugp("PDU too small received from peer %s\n", inet_ntoa(p->ldp_id)); 779 debugp("PDU too small received from peer %s\n", inet_ntoa(p->ldp_id));
781 return; 780 return;
782 } 781 }
783 rpdu = (struct ldp_pdu *) recvspace; 782 rpdu = (struct ldp_pdu *) recvspace;
784 /* XXX: buggy messages may crash the whole thing */ 783 /* XXX: buggy messages may crash the whole thing */
785 c = recv(p->socket, (void *) recvspace, 784 c = recv(p->socket, (void *) recvspace,
786 ntohs(rpdu->length) + PDU_VER_LENGTH, MSG_WAITALL); 785 ntohs(rpdu->length) + PDU_VER_LENGTH, MSG_WAITALL);
787 rpdu = (struct ldp_pdu *) recvspace; 786 rpdu = (struct ldp_pdu *) recvspace;
788 787
789 /* Check if it's somehow OK... */ 788 /* Check if it's somehow OK... */
790 if (check_recv_pdu(p, rpdu, c) != 0) 789 if (check_recv_pdu(p, rpdu, c) != 0)
791 return; 790 return;
792 791
793 debugp("Read %d bytes, PDU size: %d bytes\n", c, ntohs(rpdu->length)); 792 debugp("Read %d bytes, PDU size: %d bytes\n", c, ntohs(rpdu->length));
794 wo = sizeof(struct ldp_pdu); 793 wo = sizeof(struct ldp_pdu);
795 794
796 while (wo + TLV_TYPE_LENGTH < (uint)c) { 795 while (wo + TLV_TYPE_LENGTH < (uint)c) {
797 796
798 ttmp = (struct tlv *) (&recvspace[wo]); 797 ttmp = (struct tlv *) (&recvspace[wo]);
799 798
800 if ((ntohs(ttmp->type) != LDP_KEEPALIVE) && 799 if ((ntohs(ttmp->type) != LDP_KEEPALIVE) &&
801 (ntohs(ttmp->type) != LDP_LABEL_MAPPING)) { 800 (ntohs(ttmp->type) != LDP_LABEL_MAPPING)) {
802 debugp("Got Type: 0x%.4X (Length: %d) from %s\n", 801 debugp("Got Type: 0x%.4X (Length: %d) from %s\n",
803 ntohs(ttmp->type), ntohs(ttmp->length), 802 ntohs(ttmp->type), ntohs(ttmp->length),
804 inet_ntoa(p->ldp_id)); 803 inet_ntoa(p->ldp_id));
805 } else 804 } else
806 debugp("Got Type: 0x%.4X (Length: %d) from %s\n", 805 debugp("Got Type: 0x%.4X (Length: %d) from %s\n",
807 ntohs(ttmp->type), ntohs(ttmp->length), 806 ntohs(ttmp->type), ntohs(ttmp->length),
808 inet_ntoa(p->ldp_id)); 807 inet_ntoa(p->ldp_id));
809 808
810 /* Should we get the message ? */ 809 /* Should we get the message ? */
811 if (p->state != LDP_PEER_ESTABLISHED && 810 if (p->state != LDP_PEER_ESTABLISHED &&
812 ntohs(ttmp->type) != LDP_INITIALIZE && 811 ntohs(ttmp->type) != LDP_INITIALIZE &&
813 ntohs(ttmp->type) != LDP_KEEPALIVE) 812 ntohs(ttmp->type) != LDP_KEEPALIVE)
814 break; 813 break;
815 /* The big switch */ 814 /* The big switch */
816 switch (ntohs(ttmp->type)) { 815 switch (ntohs(ttmp->type)) {
817 case LDP_INITIALIZE: 816 case LDP_INITIALIZE:
818 itlv = (struct init_tlv *)ttmp; 817 itlv = (struct init_tlv *)ttmp;
819 /* Check size */ 818 /* Check size */
820 if (ntohs(itlv->length) < 819 if (ntohs(itlv->length) <
821 sizeof(struct init_tlv) - TLV_TYPE_LENGTH) { 820 sizeof(struct init_tlv) - TLV_TYPE_LENGTH) {
822 send_notification(p, 0, 821 send_notification(p, 0,
823 NOTIF_BAD_PDU_LEN | NOTIF_FATAL); 822 NOTIF_BAD_PDU_LEN | NOTIF_FATAL);
824 ldp_peer_holddown(p); 823 ldp_peer_holddown(p);
825 break; 824 break;
826 } 825 }
827 /* Check version */ 826 /* Check version */
828 if (ntohs(itlv->cs_version) != LDP_VERSION) { 827 if (ntohs(itlv->cs_version) != LDP_VERSION) {
829 send_notification(p, ntohl(itlv->messageid), 828 send_notification(p, ntohl(itlv->messageid),
830 NOTIF_BAD_LDP_VER | NOTIF_FATAL); 829 NOTIF_BAD_LDP_VER | NOTIF_FATAL);
831 ldp_peer_holddown(p); 830 ldp_peer_holddown(p);
832 break; 831 break;
833 } 832 }
834 /* Check if we got any hello from this one */ 833 /* Check if we got any hello from this one */
835 SLIST_FOREACH(hi, &hello_info_head, infos) 834 SLIST_FOREACH(hi, &hello_info_head, infos)
836 if (hi->ldp_id.s_addr == rpdu->ldp_id.s_addr) 835 if (hi->ldp_id.s_addr == rpdu->ldp_id.s_addr)
837 break; 836 break;
838 if (hi == NULL) { 837 if (hi == NULL) {
839 send_notification(p, ntohl(itlv->messageid), 838 send_notification(p, ntohl(itlv->messageid),
840 NOTIF_SESSION_REJECTED_NO_HELLO | NOTIF_FATAL); 839 NOTIF_SESSION_REJECTED_NO_HELLO | NOTIF_FATAL);
841 ldp_peer_holddown(p); 840 ldp_peer_holddown(p);
842 break; 841 break;
843 } 842 }
844 843
845 if (!p->master) { 844 if (!p->master) {
846 keep_alive(p); 845 keep_alive(p);
847 send_initialize(p); 846 send_initialize(p);
848 } else { 847 } else {
849 p->state = LDP_PEER_ESTABLISHED; 848 p->state = LDP_PEER_ESTABLISHED;
850 p->established_t = time(NULL); 849 p->established_t = time(NULL);
851 keep_alive(p); 850 keep_alive(p);
852 851
853 /* 852 /*
854 * Recheck here ldp id because we accepted 853 * Recheck here ldp id because we accepted
855 * connection without knowing who is it for sure 854 * connection without knowing who is it for sure
856 */ 855 */
857 p->ldp_id.s_addr = rpdu->ldp_id.s_addr; 856 p->ldp_id.s_addr = rpdu->ldp_id.s_addr;
858 857
859 fatalp("LDP neighbour %s is UP\n", 858 fatalp("LDP neighbour %s is UP\n",
860 inet_ntoa(p->ldp_id)); 859 inet_ntoa(p->ldp_id));
861 mpls_add_ldp_peer(p); 860 mpls_add_ldp_peer(p);
862 send_addresses(p); 861 send_addresses(p);
863 send_all_bindings(p); 862 send_all_bindings(p);
864 } 863 }
865 break; 864 break;
866 case LDP_KEEPALIVE: 865 case LDP_KEEPALIVE:
867 if ((p->state == LDP_PEER_CONNECTED) && (!p->master)) { 866 if ((p->state == LDP_PEER_CONNECTED) && (!p->master)) {
868 p->state = LDP_PEER_ESTABLISHED; 867 p->state = LDP_PEER_ESTABLISHED;
869 p->established_t = time(NULL); 868 p->established_t = time(NULL);
870 fatalp("LDP neighbour %s is UP\n", 869 fatalp("LDP neighbour %s is UP\n",
871 inet_ntoa(p->ldp_id)); 870 inet_ntoa(p->ldp_id));
872 mpls_add_ldp_peer(p); 871 mpls_add_ldp_peer(p);
873 send_addresses(p); 872 send_addresses(p);
874 send_all_bindings(p); 873 send_all_bindings(p);
875 } 874 }
876 p->timeout = p->holdtime; 875 p->timeout = p->holdtime;
877 break; 876 break;
878 case LDP_ADDRESS: 877 case LDP_ADDRESS:
879 /* Add peer addresses */ 878 /* Add peer addresses */
880 atlv = (struct address_tlv *) ttmp; 879 atlv = (struct address_tlv *) ttmp;
881 altlv = (struct al_tlv *) (&atlv[1]); 880 altlv = (struct al_tlv *) (&atlv[1]);
882 add_ifaddresses(p, altlv); 881 add_ifaddresses(p, altlv);
883 print_bounded_addresses(p); 882 print_bounded_addresses(p);
884 break; 883 break;
885 case LDP_ADDRESS_WITHDRAW: 884 case LDP_ADDRESS_WITHDRAW:
886 atlv = (struct address_tlv *) ttmp; 885 atlv = (struct address_tlv *) ttmp;
887 altlv = (struct al_tlv *) (&atlv[1]); 886 altlv = (struct al_tlv *) (&atlv[1]);
888 del_ifaddresses(p, altlv); 887 del_ifaddresses(p, altlv);
889 break; 888 break;
890 case LDP_LABEL_MAPPING: 889 case LDP_LABEL_MAPPING:
891 lmtlv = (struct label_map_tlv *) ttmp; 890 lmtlv = (struct label_map_tlv *) ttmp;
892 fectlv = (struct fec_tlv *) (&lmtlv[1]); 891 fectlv = (struct fec_tlv *) (&lmtlv[1]);
893 labeltlv = (struct label_tlv *)((unsigned char *)fectlv 892 labeltlv = (struct label_tlv *)((unsigned char *)fectlv
894 + ntohs(fectlv->length) + TLV_TYPE_LENGTH); 893 + ntohs(fectlv->length) + TLV_TYPE_LENGTH);
895 map_label(p, fectlv, labeltlv); 894 map_label(p, fectlv, labeltlv);
896 break; 895 break;
897 case LDP_LABEL_REQUEST: 896 case LDP_LABEL_REQUEST:
898 lmtlv = (struct label_map_tlv *) ttmp; 897 lmtlv = (struct label_map_tlv *) ttmp;
899 fectlv = (struct fec_tlv *) (&lmtlv[1]); 898 fectlv = (struct fec_tlv *) (&lmtlv[1]);
900 switch (request_respond(p, lmtlv, fectlv)) { 899 switch (request_respond(p, lmtlv, fectlv)) {
901 case LDP_E_BAD_FEC: 900 case LDP_E_BAD_FEC:
902 send_notification(p, ntohl(lmtlv->messageid), 901 send_notification(p, ntohl(lmtlv->messageid),
903 NOTIF_UNKNOWN_TLV); 902 NOTIF_UNKNOWN_TLV);
904 break; 903 break;
905 case LDP_E_BAD_AF: 904 case LDP_E_BAD_AF:
906 send_notification(p, ntohl(lmtlv->messageid), 905 send_notification(p, ntohl(lmtlv->messageid),
907 NOTIF_UNSUPPORTED_AF); 906 NOTIF_UNSUPPORTED_AF);
908 break; 907 break;
909 case LDP_E_NO_SUCH_ROUTE: 908 case LDP_E_NO_SUCH_ROUTE:
910 send_notification(p, ntohl(lmtlv->messageid), 909 send_notification(p, ntohl(lmtlv->messageid),
911 NOTIF_NO_ROUTE); 910 NOTIF_NO_ROUTE);
912 break; 911 break;
913 } 912 }
914 break; 913 break;
915 case LDP_LABEL_WITHDRAW: 914 case LDP_LABEL_WITHDRAW:
916 lmtlv = (struct label_map_tlv *) ttmp; 915 lmtlv = (struct label_map_tlv *) ttmp;
917 fectlv = (struct fec_tlv *) (&lmtlv[1]); 916 fectlv = (struct fec_tlv *) (&lmtlv[1]);
918 if (withdraw_label(p, fectlv) == LDP_E_OK) { 917 if (withdraw_label(p, fectlv) == LDP_E_OK) {
919 /* Send RELEASE */ 918 /* Send RELEASE */
920 prepare_release(ttmp); 919 prepare_release(ttmp);
921 send_tlv(p, ttmp); 920 send_tlv(p, ttmp);
922 } 921 }
923 break; 922 break;
924 case LDP_LABEL_RELEASE: 923 case LDP_LABEL_RELEASE:
925 /* 924 /*
926 * XXX: we need to make a timed queue... 925 * XXX: we need to make a timed queue...
927 * For now I just assume peers are processing messages 926 * For now I just assume peers are processing messages
928 * correctly so I just ignore confirmations 927 * correctly so I just ignore confirmations
929 */ 928 */
930 wo = -1; /* Ignore rest of message */ 929 wo = -1; /* Ignore rest of message */
931 break; 930 break;
932 case LDP_LABEL_ABORT: 931 case LDP_LABEL_ABORT:
933 /* XXX: For now I pretend I can process everything 932 /* XXX: For now I pretend I can process everything
934 * RFC 3036, Section 3.5.9.1 933 * RFC 3036, Section 3.5.9.1
935 * If an LSR receives a Label Abort Request Message after it 934 * If an LSR receives a Label Abort Request Message after it
936 * has responded to the Label Request in question with a Label 935 * has responded to the Label Request in question with a Label
937 * Mapping message or a Notification message, it ignores the 936 * Mapping message or a Notification message, it ignores the
938 * abort request. 937 * abort request.
939 */ 938 */
940 wo = -1; 939 wo = -1;
941 break; 940 break;
942 case LDP_NOTIFICATION: 941 case LDP_NOTIFICATION:
943 nottlv = (struct notification_tlv *) ttmp; 942 nottlv = (struct notification_tlv *) ttmp;
944 nottlv->st_code = ntohl(nottlv->st_code); 943 nottlv->st_code = ntohl(nottlv->st_code);
945 fatalp("Got notification 0x%X from peer %s\n", 944 fatalp("Got notification 0x%X from peer %s\n",
946 nottlv->st_code, inet_ntoa(p->ldp_id)); 945 nottlv->st_code, inet_ntoa(p->ldp_id));
947 if (nottlv->st_code >> 31) { 946 if (nottlv->st_code >> 31) {
948 fatalp("LDP peer %s signalized %s\n", 947 fatalp("LDP peer %s signalized %s\n",
949 inet_ntoa(p->ldp_id), 948 inet_ntoa(p->ldp_id),
950 NOTIF_STR[(nottlv->st_code << 1) >> 1]); 949 NOTIF_STR[(nottlv->st_code << 1) >> 1]);
951 ldp_peer_holddown(p); 950 ldp_peer_holddown(p);
952 wo = -1; 951 wo = -1;
953 } 952 }
954 break; 953 break;
955 case LDP_HELLO: 954 case LDP_HELLO:
956 /* No hellos should came on tcp session */ 955 /* No hellos should came on tcp session */
957 wo = -1; 956 wo = -1;
958 break; 957 break;
959 default: 958 default:
960 warnp("Unknown TLV received from %s\n", 959 warnp("Unknown TLV received from %s\n",
961 inet_ntoa(p->ldp_id)); 960 inet_ntoa(p->ldp_id));
962 debug_tlv(ttmp); 961 debug_tlv(ttmp);
963 wo = -1;/* discard the rest of the message */ 962 wo = -1;/* discard the rest of the message */
964 break; 963 break;
965 } 964 }
966 if (wo < 0) { 965 if (wo < 0) {
967 debugp("Discarding the rest of the message\n"); 966 debugp("Discarding the rest of the message\n");
968 break; 967 break;
969 } else { 968 } else {
970 wo += ntohs(ttmp->length) + TLV_TYPE_LENGTH; 969 wo += ntohs(ttmp->length) + TLV_TYPE_LENGTH;
971 debugp("WORKED ON %u bytes (Left %d)\n", wo, c - wo); 970 debugp("WORKED ON %u bytes (Left %d)\n", wo, c - wo);
972 } 971 }
973 } /* while */ 972 } /* while */
974 973
975} 974}
976 975
977/* Sends a pdu, tlv pair to a connected peer */ 976/* Sends a pdu, tlv pair to a connected peer */
978int  977int
979send_message(struct ldp_peer * p, struct ldp_pdu * pdu, struct tlv * t) 978send_message(struct ldp_peer * p, struct ldp_pdu * pdu, struct tlv * t)
980{ 979{
981 unsigned char sendspace[MAX_PDU_SIZE]; 980 unsigned char sendspace[MAX_PDU_SIZE];
982 981
983 /* Check if peer is connected */ 982 /* Check if peer is connected */
984 switch (p->state) { 983 switch (p->state) {
985 case LDP_PEER_CONNECTED: 984 case LDP_PEER_CONNECTED:
986 case LDP_PEER_ESTABLISHED: 985 case LDP_PEER_ESTABLISHED:
987 break; 986 break;
988 default: 987 default:
989 return -1; 988 return -1;
990 } 989 }
991 990
992 /* Check length validity first */ 991 /* Check length validity first */
993 if (ntohs(pdu->length) != 992 if (ntohs(pdu->length) !=
994 ntohs(t->length) + TLV_TYPE_LENGTH + PDU_PAYLOAD_LENGTH) { 993 ntohs(t->length) + TLV_TYPE_LENGTH + PDU_PAYLOAD_LENGTH) {
995 fatalp("LDP: TLV - PDU incompability. Message discarded\n"); 994 fatalp("LDP: TLV - PDU incompability. Message discarded\n");
996 fatalp("LDP: TLV len %d - PDU len %d\n", ntohs(t->length), 995 fatalp("LDP: TLV len %d - PDU len %d\n", ntohs(t->length),
997 ntohs(pdu->length)); 996 ntohs(pdu->length));
998 return -1; 997 return -1;
999 } 998 }
1000 if (ntohs(t->length) + PDU_VER_LENGTH > MAX_PDU_SIZE) { 999 if (ntohs(t->length) + PDU_VER_LENGTH > MAX_PDU_SIZE) {
1001 fatalp("Message to large discarded\n"); 1000 fatalp("Message to large discarded\n");
1002 return -1; 1001 return -1;
1003 } 1002 }
1004 /* Arrange them in a buffer and send */ 1003 /* Arrange them in a buffer and send */
1005 memcpy(sendspace, pdu, sizeof(struct ldp_pdu)); 1004 memcpy(sendspace, pdu, sizeof(struct ldp_pdu));
1006 memcpy(sendspace + sizeof(struct ldp_pdu), t, 1005 memcpy(sendspace + sizeof(struct ldp_pdu), t,
1007 ntohs(t->length) + TLV_TYPE_LENGTH); 1006 ntohs(t->length) + TLV_TYPE_LENGTH);
1008 1007
1009 /* Report keepalives only for DEBUG */ 1008 /* Report keepalives only for DEBUG */
1010 if ((ntohs(t->type) != 0x201) && (ntohs(t->type) != 0x400)) { 1009 if ((ntohs(t->type) != 0x201) && (ntohs(t->type) != 0x400)) {
1011 debugp("Sending message type 0x%.4X to %s (size: %d)\n", 1010 debugp("Sending message type 0x%.4X to %s (size: %d)\n",
1012 ntohs(t->type), inet_ntoa(p->ldp_id), ntohs(t->length)); 1011 ntohs(t->type), inet_ntoa(p->ldp_id), ntohs(t->length));
1013 } else 1012 } else
1014 /* downgraded from warnp to debugp for now */ 1013 /* downgraded from warnp to debugp for now */
1015 debugp("Sending message type 0x%.4X to %s (size: %d)\n", 1014 debugp("Sending message type 0x%.4X to %s (size: %d)\n",
1016 ntohs(t->type), inet_ntoa(p->ldp_id), ntohs(t->length)); 1015 ntohs(t->type), inet_ntoa(p->ldp_id), ntohs(t->length));
1017 1016
1018 /* Send it finally */ 1017 /* Send it finally */
1019 return send(p->socket, sendspace, 1018 return send(p->socket, sendspace,
1020 ntohs(pdu->length) + PDU_VER_LENGTH, 0); 1019 ntohs(pdu->length) + PDU_VER_LENGTH, 0);
1021} 1020}
1022 1021
1023/* 1022/*
1024 * Encapsulates TLV into a PDU and sends it to a peer 1023 * Encapsulates TLV into a PDU and sends it to a peer
1025 */ 1024 */
1026int  1025int
1027send_tlv(struct ldp_peer * p, struct tlv * t) 1026send_tlv(struct ldp_peer * p, struct tlv * t)
1028{ 1027{
1029 struct ldp_pdu pdu; 1028 struct ldp_pdu pdu;
1030 1029
1031 pdu.version = htons(LDP_VERSION); 1030 pdu.version = htons(LDP_VERSION);
1032 inet_aton(LDP_ID, &pdu.ldp_id); 1031 inet_aton(LDP_ID, &pdu.ldp_id);
1033 pdu.label_space = 0; 1032 pdu.label_space = 0;
1034 pdu.length = htons(ntohs(t->length) + TLV_TYPE_LENGTH + 1033 pdu.length = htons(ntohs(t->length) + TLV_TYPE_LENGTH +
1035 PDU_PAYLOAD_LENGTH); 1034 PDU_PAYLOAD_LENGTH);
1036 1035
1037 return send_message(p, &pdu, t); 1036 return send_message(p, &pdu, t);
1038} 1037}
1039 1038
1040 1039
1041int  1040int
1042send_addresses(struct ldp_peer * p) 1041send_addresses(struct ldp_peer * p)
1043{ 1042{
1044 struct address_list_tlv *t; 1043 struct address_list_tlv *t;
1045 int ret; 1044 int ret;
1046 1045
1047 t = build_address_list_tlv(); 1046 t = build_address_list_tlv();
1048 1047
1049 ret = send_tlv(p, (struct tlv *) t); 1048 ret = send_tlv(p, (struct tlv *) t);
1050 free(t); 1049 free(t);
1051 return ret; 1050 return ret;
1052 1051
1053} 1052}