]> git.proxmox.com Git - mirror_frr.git/blame - isisd/isis_pdu.c
lib: add array_size() helper
[mirror_frr.git] / isisd / isis_pdu.c
CommitLineData
eb5d44eb 1/*
2 * IS-IS Rout(e)ing protocol - isis_pdu.c
3 * PDU processing
4 *
5 * Copyright (C) 2001,2002 Sampo Saaristo
6 * Tampere University of Technology
7 * Institute of Communications Engineering
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public Licenseas published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
eb5d44eb 24#include <zebra.h>
eb5d44eb 25
26#include "memory.h"
27#include "thread.h"
28#include "linklist.h"
29#include "log.h"
30#include "stream.h"
31#include "vty.h"
3f045a08 32#include "hash.h"
eb5d44eb 33#include "prefix.h"
34#include "if.h"
6a270cd9 35#include "checksum.h"
3f045a08 36#include "md5.h"
eb5d44eb 37
38#include "isisd/dict.h"
39#include "isisd/include-netbsd/iso.h"
40#include "isisd/isis_constants.h"
41#include "isisd/isis_common.h"
3f045a08 42#include "isisd/isis_flags.h"
eb5d44eb 43#include "isisd/isis_adjacency.h"
44#include "isisd/isis_circuit.h"
45#include "isisd/isis_network.h"
46#include "isisd/isis_misc.h"
47#include "isisd/isis_dr.h"
eb5d44eb 48#include "isisd/isis_tlv.h"
49#include "isisd/isisd.h"
50#include "isisd/isis_dynhn.h"
51#include "isisd/isis_lsp.h"
52#include "isisd/isis_pdu.h"
53#include "isisd/iso_checksum.h"
54#include "isisd/isis_csm.h"
55#include "isisd/isis_events.h"
56
eb5d44eb 57#define ISIS_MINIMUM_FIXED_HDR_LEN 15
f390d2c7 58#define ISIS_MIN_PDU_LEN 13 /* partial seqnum pdu with id_len=2 */
eb5d44eb 59
60#ifndef PNBBY
61#define PNBBY 8
62#endif /* PNBBY */
63
64/* Utility mask array. */
e38e0df0 65static u_char maskbit[] = {
eb5d44eb 66 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
67};
68
69/*
70 * HELPER FUNCS
71 */
72
73/*
74 * Compares two sets of area addresses
75 */
f390d2c7 76static int
eb5d44eb 77area_match (struct list *left, struct list *right)
78{
79 struct area_addr *addr1, *addr2;
3fdb2dd9 80 struct listnode *node1, *node2;
eb5d44eb 81
3fdb2dd9 82 for (ALL_LIST_ELEMENTS_RO (left, node1, addr1))
f390d2c7 83 {
3fdb2dd9 84 for (ALL_LIST_ELEMENTS_RO (right, node2, addr2))
f390d2c7 85 {
86 if (addr1->addr_len == addr2->addr_len &&
87 !memcmp (addr1->area_addr, addr2->area_addr, (int) addr1->addr_len))
88 return 1; /* match */
eb5d44eb 89 }
90 }
91
f390d2c7 92 return 0; /* mismatch */
eb5d44eb 93}
94
95/*
96 * Check if ip2 is in the ip1's network (function like Prefix.h:prefix_match() )
97 * param ip1 the IS interface ip address structure
98 * param ip2 the IIH's ip address
99 * return 0 the IIH's IP is not in the IS's subnetwork
100 * 1 the IIH's IP is in the IS's subnetwork
101 */
92365889 102static int
f390d2c7 103ip_same_subnet (struct prefix_ipv4 *ip1, struct in_addr *ip2)
eb5d44eb 104{
105 u_char *addr1, *addr2;
53c997c9 106 int shift, offset, offsetloop;
eb5d44eb 107 int len;
f390d2c7 108
109 addr1 = (u_char *) & ip1->prefix.s_addr;
110 addr2 = (u_char *) & ip2->s_addr;
eb5d44eb 111 len = ip1->prefixlen;
112
113 shift = len % PNBBY;
53c997c9 114 offsetloop = offset = len / PNBBY;
eb5d44eb 115
53c997c9 116 while (offsetloop--)
117 if (addr1[offsetloop] != addr2[offsetloop])
118 return 0;
eb5d44eb 119
f390d2c7 120 if (shift)
53c997c9 121 if (maskbit[shift] & (addr1[offset] ^ addr2[offset]))
122 return 0;
eb5d44eb 123
f390d2c7 124 return 1; /* match */
125}
eb5d44eb 126
127/*
128 * Compares two set of ip addresses
129 * param left the local interface's ip addresses
130 * param right the iih interface's ip address
131 * return 0 no match;
132 * 1 match;
133 */
f390d2c7 134static int
eb5d44eb 135ip_match (struct list *left, struct list *right)
136{
137 struct prefix_ipv4 *ip1;
138 struct in_addr *ip2;
3fdb2dd9 139 struct listnode *node1, *node2;
eb5d44eb 140
e082ac1d 141 if ((left == NULL) || (right == NULL))
142 return 0;
143
3fdb2dd9 144 for (ALL_LIST_ELEMENTS_RO (left, node1, ip1))
f390d2c7 145 {
3fdb2dd9 146 for (ALL_LIST_ELEMENTS_RO (right, node2, ip2))
f390d2c7 147 {
148 if (ip_same_subnet (ip1, ip2))
149 {
150 return 1; /* match */
151 }
eb5d44eb 152 }
f390d2c7 153
eb5d44eb 154 }
155 return 0;
156}
157
158/*
159 * Checks whether we should accept a PDU of given level
160 */
161static int
162accept_level (int level, int circuit_t)
163{
f390d2c7 164 int retval = ((circuit_t & level) == level); /* simple approach */
eb5d44eb 165
166 return retval;
167}
168
3f045a08
JB
169/*
170 * Verify authentication information
171 * Support cleartext and HMAC MD5 authentication
172 */
173static int
174authentication_check (struct isis_passwd *remote, struct isis_passwd *local,
175 struct stream *stream, uint32_t auth_tlv_offset)
eb5d44eb 176{
3f045a08
JB
177 unsigned char digest[ISIS_AUTH_MD5_SIZE];
178
179 /* Auth fail () - passwd type mismatch */
180 if (local->type != remote->type)
181 return ISIS_ERROR;
182
183 switch (local->type)
184 {
185 /* No authentication required */
186 case ISIS_PASSWD_TYPE_UNUSED:
187 break;
188
189 /* Cleartext (ISO 10589) */
190 case ISIS_PASSWD_TYPE_CLEARTXT:
191 /* Auth fail () - passwd len mismatch */
192 if (remote->len != local->len)
193 return ISIS_ERROR;
194 return memcmp (local->passwd, remote->passwd, local->len);
195
196 /* HMAC MD5 (RFC 3567) */
197 case ISIS_PASSWD_TYPE_HMAC_MD5:
198 /* Auth fail () - passwd len mismatch */
199 if (remote->len != ISIS_AUTH_MD5_SIZE)
200 return ISIS_ERROR;
201 /* Set the authentication value to 0 before the check */
202 memset (STREAM_DATA (stream) + auth_tlv_offset + 3, 0,
203 ISIS_AUTH_MD5_SIZE);
204 /* Compute the digest */
205 hmac_md5 (STREAM_DATA (stream), stream_get_endp (stream),
206 (unsigned char *) &(local->passwd), local->len,
207 (caddr_t) &digest);
208 /* Copy back the authentication value after the check */
209 memcpy (STREAM_DATA (stream) + auth_tlv_offset + 3,
210 remote->passwd, ISIS_AUTH_MD5_SIZE);
211 return memcmp (digest, remote->passwd, ISIS_AUTH_MD5_SIZE);
212
213 default:
214 zlog_err ("Unsupported authentication type");
215 return ISIS_ERROR;
216 }
217
218 /* Authentication pass when no authentication is configured */
219 return ISIS_OK;
220}
221
222static int
223lsp_authentication_check (struct stream *stream, struct isis_area *area,
224 int level, struct isis_passwd *passwd)
225{
226 struct isis_link_state_hdr *hdr;
227 uint32_t expected = 0, found = 0, auth_tlv_offset = 0;
e38e0df0 228 uint16_t checksum, rem_lifetime, pdu_len;
3f045a08
JB
229 struct tlvs tlvs;
230 int retval = ISIS_OK;
231
232 hdr = (struct isis_link_state_hdr *) (STREAM_PNT (stream));
e38e0df0 233 pdu_len = ntohs (hdr->pdu_len);
3f045a08
JB
234 expected |= TLVFLAG_AUTH_INFO;
235 auth_tlv_offset = stream_get_getp (stream) + ISIS_LSP_HDR_LEN;
236 retval = parse_tlvs (area->area_tag, STREAM_PNT (stream) + ISIS_LSP_HDR_LEN,
e38e0df0 237 pdu_len - ISIS_FIXED_HDR_LEN - ISIS_LSP_HDR_LEN,
3f045a08
JB
238 &expected, &found, &tlvs, &auth_tlv_offset);
239
240 if (retval != ISIS_OK)
241 {
242 zlog_err ("ISIS-Upd (%s): Parse failed L%d LSP %s, seq 0x%08x, "
243 "cksum 0x%04x, lifetime %us, len %u",
244 area->area_tag, level, rawlspid_print (hdr->lsp_id),
245 ntohl (hdr->seq_num), ntohs (hdr->checksum),
e38e0df0 246 ntohs (hdr->rem_lifetime), pdu_len);
3f045a08
JB
247 if ((isis->debugs & DEBUG_UPDATE_PACKETS) &&
248 (isis->debugs & DEBUG_PACKET_DUMP))
249 zlog_dump_data (STREAM_DATA (stream), stream_get_endp (stream));
250 return retval;
251 }
252
253 if (!(found & TLVFLAG_AUTH_INFO))
f390d2c7 254 {
3f045a08
JB
255 zlog_err ("No authentication tlv in LSP");
256 return ISIS_ERROR;
f390d2c7 257 }
3f045a08
JB
258
259 if (tlvs.auth_info.type != ISIS_PASSWD_TYPE_CLEARTXT &&
260 tlvs.auth_info.type != ISIS_PASSWD_TYPE_HMAC_MD5)
f390d2c7 261 {
3f045a08
JB
262 zlog_err ("Unknown authentication type in LSP");
263 return ISIS_ERROR;
f390d2c7 264 }
3f045a08
JB
265
266 /*
267 * RFC 5304 set checksum and remaining lifetime to zero before
268 * verification and reset to old values after verification.
269 */
270 checksum = hdr->checksum;
271 rem_lifetime = hdr->rem_lifetime;
272 hdr->checksum = 0;
273 hdr->rem_lifetime = 0;
274 retval = authentication_check (&tlvs.auth_info, passwd, stream,
275 auth_tlv_offset);
276 hdr->checksum = checksum;
277 hdr->rem_lifetime = rem_lifetime;
278
279 return retval;
eb5d44eb 280}
281
282/*
283 * Processing helper functions
284 */
3f045a08
JB
285static void
286del_addr (void *val)
287{
288 XFREE (MTYPE_ISIS_TMP, val);
289}
290
291static void
292tlvs_to_adj_area_addrs (struct tlvs *tlvs, struct isis_adjacency *adj)
293{
294 struct listnode *node;
295 struct area_addr *area_addr, *malloced;
296
297 if (adj->area_addrs)
298 {
299 adj->area_addrs->del = del_addr;
300 list_delete (adj->area_addrs);
301 }
302 adj->area_addrs = list_new ();
303 if (tlvs->area_addrs)
304 {
305 for (ALL_LIST_ELEMENTS_RO (tlvs->area_addrs, node, area_addr))
306 {
307 malloced = XMALLOC (MTYPE_ISIS_TMP, sizeof (struct area_addr));
308 memcpy (malloced, area_addr, sizeof (struct area_addr));
309 listnode_add (adj->area_addrs, malloced);
310 }
311 }
312}
313
92365889 314static void
f390d2c7 315tlvs_to_adj_nlpids (struct tlvs *tlvs, struct isis_adjacency *adj)
eb5d44eb 316{
317 int i;
318 struct nlpids *tlv_nlpids;
319
f390d2c7 320 if (tlvs->nlpids)
321 {
eb5d44eb 322
f390d2c7 323 tlv_nlpids = tlvs->nlpids;
eb5d44eb 324
f390d2c7 325 adj->nlpids.count = tlv_nlpids->count;
eb5d44eb 326
f390d2c7 327 for (i = 0; i < tlv_nlpids->count; i++)
328 {
329 adj->nlpids.nlpids[i] = tlv_nlpids->nlpids[i];
330 }
eb5d44eb 331 }
eb5d44eb 332}
333
92365889 334static void
f390d2c7 335tlvs_to_adj_ipv4_addrs (struct tlvs *tlvs, struct isis_adjacency *adj)
eb5d44eb 336{
3fdb2dd9 337 struct listnode *node;
eb5d44eb 338 struct in_addr *ipv4_addr, *malloced;
339
f390d2c7 340 if (adj->ipv4_addrs)
341 {
3f045a08 342 adj->ipv4_addrs->del = del_addr;
f390d2c7 343 list_delete (adj->ipv4_addrs);
344 }
eb5d44eb 345 adj->ipv4_addrs = list_new ();
f390d2c7 346 if (tlvs->ipv4_addrs)
347 {
3fdb2dd9 348 for (ALL_LIST_ELEMENTS_RO (tlvs->ipv4_addrs, node, ipv4_addr))
f390d2c7 349 {
350 malloced = XMALLOC (MTYPE_ISIS_TMP, sizeof (struct in_addr));
351 memcpy (malloced, ipv4_addr, sizeof (struct in_addr));
352 listnode_add (adj->ipv4_addrs, malloced);
353 }
eb5d44eb 354 }
eb5d44eb 355}
356
357#ifdef HAVE_IPV6
92365889 358static void
f390d2c7 359tlvs_to_adj_ipv6_addrs (struct tlvs *tlvs, struct isis_adjacency *adj)
eb5d44eb 360{
3fdb2dd9 361 struct listnode *node;
eb5d44eb 362 struct in6_addr *ipv6_addr, *malloced;
363
f390d2c7 364 if (adj->ipv6_addrs)
365 {
3f045a08 366 adj->ipv6_addrs->del = del_addr;
f390d2c7 367 list_delete (adj->ipv6_addrs);
368 }
eb5d44eb 369 adj->ipv6_addrs = list_new ();
f390d2c7 370 if (tlvs->ipv6_addrs)
371 {
3fdb2dd9 372 for (ALL_LIST_ELEMENTS_RO (tlvs->ipv6_addrs, node, ipv6_addr))
f390d2c7 373 {
374 malloced = XMALLOC (MTYPE_ISIS_TMP, sizeof (struct in6_addr));
375 memcpy (malloced, ipv6_addr, sizeof (struct in6_addr));
376 listnode_add (adj->ipv6_addrs, malloced);
377 }
eb5d44eb 378 }
eb5d44eb 379
380}
381#endif /* HAVE_IPV6 */
382
eb5d44eb 383/*
384 * RECEIVE SIDE
385 */
386
387/*
388 * Process P2P IIH
389 * ISO - 10589
390 * Section 8.2.5 - Receiving point-to-point IIH PDUs
391 *
392 */
393static int
394process_p2p_hello (struct isis_circuit *circuit)
395{
396 int retval = ISIS_OK;
397 struct isis_p2p_hello_hdr *hdr;
398 struct isis_adjacency *adj;
3f045a08 399 u_int32_t expected = 0, found = 0, auth_tlv_offset = 0;
e38e0df0 400 uint16_t pdu_len;
eb5d44eb 401 struct tlvs tlvs;
402
3f045a08
JB
403 if (isis->debugs & DEBUG_ADJ_PACKETS)
404 {
405 zlog_debug ("ISIS-Adj (%s): Rcvd P2P IIH on %s, cirType %s, cirID %u",
406 circuit->area->area_tag, circuit->interface->name,
407 circuit_t2string (circuit->is_type), circuit->circuit_id);
408 if (isis->debugs & DEBUG_PACKET_DUMP)
409 zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
410 stream_get_endp (circuit->rcv_stream));
411 }
412
413 if (circuit->circ_type != CIRCUIT_T_P2P)
414 {
415 zlog_warn ("p2p hello on non p2p circuit");
416 return ISIS_WARNING;
417 }
418
f390d2c7 419 if ((stream_get_endp (circuit->rcv_stream) -
420 stream_get_getp (circuit->rcv_stream)) < ISIS_P2PHELLO_HDRLEN)
421 {
422 zlog_warn ("Packet too short");
423 return ISIS_WARNING;
424 }
eb5d44eb 425
426 /* 8.2.5.1 PDU acceptance tests */
427
428 /* 8.2.5.1 a) external domain untrue */
429 /* FIXME: not useful at all? */
430
431 /* 8.2.5.1 b) ID Length mismatch */
432 /* checked at the handle_pdu */
433
434 /* 8.2.5.2 IIH PDU Processing */
435
436 /* 8.2.5.2 a) 1) Maximum Area Addresses */
437 /* Already checked, and can also be ommited */
438
439 /*
440 * Get the header
441 */
f390d2c7 442 hdr = (struct isis_p2p_hello_hdr *) STREAM_PNT (circuit->rcv_stream);
e38e0df0 443 pdu_len = ntohs (hdr->pdu_len);
eb5d44eb 444
a22ab5a5
AS
445 if (pdu_len < (ISIS_FIXED_HDR_LEN + ISIS_P2PHELLO_HDRLEN) ||
446 pdu_len > ISO_MTU(circuit) ||
e38e0df0 447 pdu_len > stream_get_endp (circuit->rcv_stream))
f390d2c7 448 {
3f045a08
JB
449 zlog_warn ("ISIS-Adj (%s): Rcvd P2P IIH from (%s) with "
450 "invalid pdu length %d",
e38e0df0 451 circuit->area->area_tag, circuit->interface->name, pdu_len);
3f045a08 452 return ISIS_WARNING;
f390d2c7 453 }
eb5d44eb 454
e38e0df0
SV
455 /*
456 * Set the stream endp to PDU length, ignoring additional padding
457 * introduced by transport chips.
458 */
459 if (pdu_len < stream_get_endp (circuit->rcv_stream))
460 stream_set_endp (circuit->rcv_stream, pdu_len);
461
462 stream_forward_getp (circuit->rcv_stream, ISIS_P2PHELLO_HDRLEN);
463
eb5d44eb 464 /*
465 * Lets get the TLVS now
466 */
467 expected |= TLVFLAG_AREA_ADDRS;
468 expected |= TLVFLAG_AUTH_INFO;
469 expected |= TLVFLAG_NLPID;
470 expected |= TLVFLAG_IPV4_ADDR;
471 expected |= TLVFLAG_IPV6_ADDR;
472
3f045a08 473 auth_tlv_offset = stream_get_getp (circuit->rcv_stream);
eb5d44eb 474 retval = parse_tlvs (circuit->area->area_tag,
475 STREAM_PNT (circuit->rcv_stream),
e38e0df0
SV
476 pdu_len - ISIS_P2PHELLO_HDRLEN - ISIS_FIXED_HDR_LEN,
477 &expected, &found, &tlvs, &auth_tlv_offset);
f390d2c7 478
479 if (retval > ISIS_WARNING)
480 {
3f045a08 481 zlog_warn ("parse_tlvs() failed");
f390d2c7 482 free_tlvs (&tlvs);
483 return retval;
484 };
eb5d44eb 485
3f045a08
JB
486 if (!(found & TLVFLAG_AREA_ADDRS))
487 {
488 zlog_warn ("No Area addresses TLV in P2P IS to IS hello");
489 free_tlvs (&tlvs);
490 return ISIS_WARNING;
491 }
492
eb5d44eb 493 /* 8.2.5.1 c) Authentication */
f390d2c7 494 if (circuit->passwd.type)
495 {
496 if (!(found & TLVFLAG_AUTH_INFO) ||
3f045a08
JB
497 authentication_check (&tlvs.auth_info, &circuit->passwd,
498 circuit->rcv_stream, auth_tlv_offset))
499 {
500 isis_event_auth_failure (circuit->area->area_tag,
501 "P2P hello authentication failure",
502 hdr->source_id);
503 free_tlvs (&tlvs);
504 return ISIS_OK;
505 }
eb5d44eb 506 }
eb5d44eb 507
3f045a08
JB
508 /*
509 * check if it's own interface ip match iih ip addrs
510 */
511 if ((found & TLVFLAG_IPV4_ADDR) == 0 ||
512 ip_match (circuit->ip_addrs, tlvs.ipv4_addrs) == 0)
513 {
514 zlog_warn ("ISIS-Adj: No usable IP interface addresses "
515 "in LAN IIH from %s\n", circuit->interface->name);
516 free_tlvs (&tlvs);
517 return ISIS_WARNING;
518 }
519
520 /*
521 * My interpertation of the ISO, if no adj exists we will create one for
522 * the circuit
523 */
524 adj = circuit->u.p2p.neighbor;
525 if (!adj || adj->level != hdr->circuit_t)
526 {
527 if (!adj)
528 {
529 adj = isis_new_adj (hdr->source_id, NULL, hdr->circuit_t, circuit);
530 if (adj == NULL)
531 return ISIS_ERROR;
532 }
533 else
534 {
535 adj->level = hdr->circuit_t;
536 }
537 circuit->u.p2p.neighbor = adj;
538 isis_adj_state_change (adj, ISIS_ADJ_INITIALIZING, NULL);
539 adj->sys_type = ISIS_SYSTYPE_UNKNOWN;
540 }
541
542 /* 8.2.6 Monitoring point-to-point adjacencies */
543 adj->hold_time = ntohs (hdr->hold_time);
544 adj->last_upd = time (NULL);
545
eb5d44eb 546 /* we do this now because the adj may not survive till the end... */
3f045a08
JB
547 tlvs_to_adj_area_addrs (&tlvs, adj);
548
549 /* which protocol are spoken ??? */
550 if (found & TLVFLAG_NLPID)
551 tlvs_to_adj_nlpids (&tlvs, adj);
eb5d44eb 552
553 /* we need to copy addresses to the adj */
3f045a08
JB
554 if (found & TLVFLAG_IPV4_ADDR)
555 tlvs_to_adj_ipv4_addrs (&tlvs, adj);
eb5d44eb 556
557#ifdef HAVE_IPV6
3f045a08
JB
558 if (found & TLVFLAG_IPV6_ADDR)
559 tlvs_to_adj_ipv6_addrs (&tlvs, adj);
eb5d44eb 560#endif /* HAVE_IPV6 */
561
562 /* lets take care of the expiry */
f390d2c7 563 THREAD_TIMER_OFF (adj->t_expire);
564 THREAD_TIMER_ON (master, adj->t_expire, isis_adj_expire, adj,
565 (long) adj->hold_time);
eb5d44eb 566
567 /* 8.2.5.2 a) a match was detected */
f390d2c7 568 if (area_match (circuit->area->area_addrs, tlvs.area_addrs))
569 {
570 /* 8.2.5.2 a) 2) If the system is L1 - table 5 */
571 if (circuit->area->is_type == IS_LEVEL_1)
572 {
573 switch (hdr->circuit_t)
574 {
575 case IS_LEVEL_1:
576 case IS_LEVEL_1_AND_2:
577 if (adj->adj_state != ISIS_ADJ_UP)
578 {
579 /* (4) adj state up */
580 isis_adj_state_change (adj, ISIS_ADJ_UP, NULL);
581 /* (5) adj usage level 1 */
582 adj->adj_usage = ISIS_ADJ_LEVEL1;
583 }
584 else if (adj->adj_usage == ISIS_ADJ_LEVEL1)
585 {
586 ; /* accept */
587 }
588 break;
589 case IS_LEVEL_2:
590 if (adj->adj_state != ISIS_ADJ_UP)
591 {
592 /* (7) reject - wrong system type event */
593 zlog_warn ("wrongSystemType");
3f045a08 594 free_tlvs (&tlvs);
f390d2c7 595 return ISIS_WARNING; /* Reject */
596 }
597 else if (adj->adj_usage == ISIS_ADJ_LEVEL1)
598 {
599 /* (6) down - wrong system */
600 isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Wrong System");
601 }
602 break;
603 }
604 }
eb5d44eb 605
f390d2c7 606 /* 8.2.5.2 a) 3) If the system is L1L2 - table 6 */
607 if (circuit->area->is_type == IS_LEVEL_1_AND_2)
608 {
609 switch (hdr->circuit_t)
610 {
611 case IS_LEVEL_1:
612 if (adj->adj_state != ISIS_ADJ_UP)
613 {
614 /* (6) adj state up */
615 isis_adj_state_change (adj, ISIS_ADJ_UP, NULL);
616 /* (7) adj usage level 1 */
617 adj->adj_usage = ISIS_ADJ_LEVEL1;
618 }
619 else if (adj->adj_usage == ISIS_ADJ_LEVEL1)
620 {
621 ; /* accept */
622 }
623 else if ((adj->adj_usage == ISIS_ADJ_LEVEL1AND2) ||
624 (adj->adj_usage == ISIS_ADJ_LEVEL2))
625 {
626 /* (8) down - wrong system */
627 isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Wrong System");
628 }
629 break;
630 case IS_LEVEL_2:
631 if (adj->adj_state != ISIS_ADJ_UP)
632 {
633 /* (6) adj state up */
634 isis_adj_state_change (adj, ISIS_ADJ_UP, NULL);
635 /* (9) adj usage level 2 */
636 adj->adj_usage = ISIS_ADJ_LEVEL2;
637 }
638 else if ((adj->adj_usage == ISIS_ADJ_LEVEL1) ||
639 (adj->adj_usage == ISIS_ADJ_LEVEL1AND2))
640 {
641 /* (8) down - wrong system */
642 isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Wrong System");
643 }
644 else if (adj->adj_usage == ISIS_ADJ_LEVEL2)
645 {
646 ; /* Accept */
647 }
648 break;
649 case IS_LEVEL_1_AND_2:
650 if (adj->adj_state != ISIS_ADJ_UP)
651 {
652 /* (6) adj state up */
653 isis_adj_state_change (adj, ISIS_ADJ_UP, NULL);
654 /* (10) adj usage level 1 */
655 adj->adj_usage = ISIS_ADJ_LEVEL1AND2;
656 }
657 else if ((adj->adj_usage == ISIS_ADJ_LEVEL1) ||
658 (adj->adj_usage == ISIS_ADJ_LEVEL2))
659 {
660 /* (8) down - wrong system */
661 isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Wrong System");
662 }
663 else if (adj->adj_usage == ISIS_ADJ_LEVEL1AND2)
664 {
665 ; /* Accept */
666 }
667 break;
668 }
669 }
eb5d44eb 670
f390d2c7 671 /* 8.2.5.2 a) 4) If the system is L2 - table 7 */
672 if (circuit->area->is_type == IS_LEVEL_2)
673 {
674 switch (hdr->circuit_t)
675 {
676 case IS_LEVEL_1:
677 if (adj->adj_state != ISIS_ADJ_UP)
678 {
679 /* (5) reject - wrong system type event */
680 zlog_warn ("wrongSystemType");
3f045a08 681 free_tlvs (&tlvs);
f390d2c7 682 return ISIS_WARNING; /* Reject */
683 }
684 else if ((adj->adj_usage == ISIS_ADJ_LEVEL1AND2) ||
685 (adj->adj_usage == ISIS_ADJ_LEVEL2))
686 {
687 /* (6) down - wrong system */
688 isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Wrong System");
689 }
690 break;
691 case IS_LEVEL_1_AND_2:
692 case IS_LEVEL_2:
693 if (adj->adj_state != ISIS_ADJ_UP)
694 {
695 /* (7) adj state up */
696 isis_adj_state_change (adj, ISIS_ADJ_UP, NULL);
697 /* (8) adj usage level 2 */
698 adj->adj_usage = ISIS_ADJ_LEVEL2;
699 }
700 else if (adj->adj_usage == ISIS_ADJ_LEVEL1AND2)
701 {
702 /* (6) down - wrong system */
703 isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Wrong System");
704 }
705 else if (adj->adj_usage == ISIS_ADJ_LEVEL2)
706 {
707 ; /* Accept */
708 }
709 break;
710 }
711 }
eb5d44eb 712 }
eb5d44eb 713 /* 8.2.5.2 b) if no match was detected */
3f045a08 714 else if (listcount (circuit->area->area_addrs) > 0)
eb5d44eb 715 {
f390d2c7 716 if (circuit->area->is_type == IS_LEVEL_1)
717 {
718 /* 8.2.5.2 b) 1) is_type L1 and adj is not up */
719 if (adj->adj_state != ISIS_ADJ_UP)
720 {
721 isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Area Mismatch");
722 /* 8.2.5.2 b) 2)is_type L1 and adj is up */
723 }
724 else
725 {
726 isis_adj_state_change (adj, ISIS_ADJ_DOWN,
727 "Down - Area Mismatch");
728 }
729 }
730 /* 8.2.5.2 b 3 If the system is L2 or L1L2 - table 8 */
731 else
732 {
733 switch (hdr->circuit_t)
734 {
735 case IS_LEVEL_1:
736 if (adj->adj_state != ISIS_ADJ_UP)
737 {
738 /* (6) reject - Area Mismatch event */
739 zlog_warn ("AreaMismatch");
3f045a08 740 free_tlvs (&tlvs);
f390d2c7 741 return ISIS_WARNING; /* Reject */
742 }
743 else if (adj->adj_usage == ISIS_ADJ_LEVEL1)
744 {
745 /* (7) down - area mismatch */
746 isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Area Mismatch");
747
748 }
749 else if ((adj->adj_usage == ISIS_ADJ_LEVEL1AND2) ||
750 (adj->adj_usage == ISIS_ADJ_LEVEL2))
751 {
752 /* (7) down - wrong system */
753 isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Wrong System");
754 }
755 break;
756 case IS_LEVEL_1_AND_2:
757 case IS_LEVEL_2:
758 if (adj->adj_state != ISIS_ADJ_UP)
759 {
760 /* (8) adj state up */
761 isis_adj_state_change (adj, ISIS_ADJ_UP, NULL);
762 /* (9) adj usage level 2 */
763 adj->adj_usage = ISIS_ADJ_LEVEL2;
764 }
765 else if (adj->adj_usage == ISIS_ADJ_LEVEL1)
766 {
767 /* (7) down - wrong system */
768 isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Wrong System");
769 }
770 else if (adj->adj_usage == ISIS_ADJ_LEVEL1AND2)
771 {
772 if (hdr->circuit_t == IS_LEVEL_2)
773 {
774 /* (7) down - wrong system */
775 isis_adj_state_change (adj, ISIS_ADJ_DOWN,
776 "Wrong System");
777 }
778 else
779 {
780 /* (7) down - area mismatch */
781 isis_adj_state_change (adj, ISIS_ADJ_DOWN,
782 "Area Mismatch");
783 }
784 }
785 else if (adj->adj_usage == ISIS_ADJ_LEVEL2)
786 {
787 ; /* Accept */
788 }
789 break;
790 }
791 }
eb5d44eb 792 }
3f045a08
JB
793 else
794 {
795 /* down - area mismatch */
796 isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Area Mismatch");
797 }
eb5d44eb 798 /* 8.2.5.2 c) if the action was up - comparing circuit IDs */
799 /* FIXME - Missing parts */
800
eb5d44eb 801 /* some of my own understanding of the ISO, why the heck does
802 * it not say what should I change the system_type to...
803 */
f390d2c7 804 switch (adj->adj_usage)
805 {
eb5d44eb 806 case ISIS_ADJ_LEVEL1:
807 adj->sys_type = ISIS_SYSTYPE_L1_IS;
808 break;
809 case ISIS_ADJ_LEVEL2:
810 adj->sys_type = ISIS_SYSTYPE_L2_IS;
811 break;
812 case ISIS_ADJ_LEVEL1AND2:
813 adj->sys_type = ISIS_SYSTYPE_L2_IS;
814 break;
815 case ISIS_ADJ_NONE:
816 adj->sys_type = ISIS_SYSTYPE_UNKNOWN;
817 break;
f390d2c7 818 }
eb5d44eb 819
820 adj->circuit_t = hdr->circuit_t;
3f045a08
JB
821
822 if (isis->debugs & DEBUG_ADJ_PACKETS)
823 {
824 zlog_debug ("ISIS-Adj (%s): Rcvd P2P IIH from (%s), cir type %s,"
825 " cir id %02d, length %d",
826 circuit->area->area_tag, circuit->interface->name,
827 circuit_t2string (circuit->is_type),
e38e0df0 828 circuit->circuit_id, pdu_len);
3f045a08 829 }
eb5d44eb 830
831 free_tlvs (&tlvs);
832
833 return retval;
834}
835
eb5d44eb 836/*
837 * Process IS-IS LAN Level 1/2 Hello PDU
838 */
f390d2c7 839static int
840process_lan_hello (int level, struct isis_circuit *circuit, u_char * ssnpa)
eb5d44eb 841{
842 int retval = ISIS_OK;
843 struct isis_lan_hello_hdr hdr;
844 struct isis_adjacency *adj;
3f045a08 845 u_int32_t expected = 0, found = 0, auth_tlv_offset = 0;
eb5d44eb 846 struct tlvs tlvs;
847 u_char *snpa;
3fdb2dd9 848 struct listnode *node;
eb5d44eb 849
3f045a08
JB
850 if (isis->debugs & DEBUG_ADJ_PACKETS)
851 {
852 zlog_debug ("ISIS-Adj (%s): Rcvd L%d LAN IIH on %s, cirType %s, "
853 "cirID %u",
854 circuit->area->area_tag, level, circuit->interface->name,
855 circuit_t2string (circuit->is_type), circuit->circuit_id);
856 if (isis->debugs & DEBUG_PACKET_DUMP)
857 zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
858 stream_get_endp (circuit->rcv_stream));
859 }
860
861 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
862 {
863 zlog_warn ("lan hello on non broadcast circuit");
864 return ISIS_WARNING;
865 }
866
f390d2c7 867 if ((stream_get_endp (circuit->rcv_stream) -
868 stream_get_getp (circuit->rcv_stream)) < ISIS_LANHELLO_HDRLEN)
869 {
870 zlog_warn ("Packet too short");
871 return ISIS_WARNING;
872 }
eb5d44eb 873
f390d2c7 874 if (circuit->ext_domain)
875 {
529d65b3 876 zlog_debug ("level %d LAN Hello received over circuit with "
877 "externalDomain = true", level);
f390d2c7 878 return ISIS_WARNING;
879 }
eb5d44eb 880
3f045a08 881 if (!accept_level (level, circuit->is_type))
f390d2c7 882 {
883 if (isis->debugs & DEBUG_ADJ_PACKETS)
884 {
529d65b3 885 zlog_debug ("ISIS-Adj (%s): Interface level mismatch, %s",
886 circuit->area->area_tag, circuit->interface->name);
f390d2c7 887 }
888 return ISIS_WARNING;
eb5d44eb 889 }
eb5d44eb 890
891#if 0
892 /* Cisco's debug message compatability */
f390d2c7 893 if (!accept_level (level, circuit->area->is_type))
894 {
895 if (isis->debugs & DEBUG_ADJ_PACKETS)
896 {
529d65b3 897 zlog_debug ("ISIS-Adj (%s): is type mismatch",
898 circuit->area->area_tag);
f390d2c7 899 }
900 return ISIS_WARNING;
eb5d44eb 901 }
eb5d44eb 902#endif
903 /*
904 * Fill the header
905 */
906 hdr.circuit_t = stream_getc (circuit->rcv_stream);
907 stream_get (hdr.source_id, circuit->rcv_stream, ISIS_SYS_ID_LEN);
908 hdr.hold_time = stream_getw (circuit->rcv_stream);
f390d2c7 909 hdr.pdu_len = stream_getw (circuit->rcv_stream);
910 hdr.prio = stream_getc (circuit->rcv_stream);
eb5d44eb 911 stream_get (hdr.lan_id, circuit->rcv_stream, ISIS_SYS_ID_LEN + 1);
912
a22ab5a5
AS
913 if (hdr.pdu_len < (ISIS_FIXED_HDR_LEN + ISIS_LANHELLO_HDRLEN) ||
914 hdr.pdu_len > ISO_MTU(circuit) ||
e38e0df0 915 hdr.pdu_len > stream_get_endp (circuit->rcv_stream))
f390d2c7 916 {
3f045a08
JB
917 zlog_warn ("ISIS-Adj (%s): Rcvd LAN IIH from (%s) with "
918 "invalid pdu length %d",
919 circuit->area->area_tag, circuit->interface->name,
920 hdr.pdu_len);
e38e0df0 921 return ISIS_WARNING;
3f045a08
JB
922 }
923
e38e0df0
SV
924 /*
925 * Set the stream endp to PDU length, ignoring additional padding
926 * introduced by transport chips.
927 */
928 if (hdr.pdu_len < stream_get_endp (circuit->rcv_stream))
929 stream_set_endp (circuit->rcv_stream, hdr.pdu_len);
930
3f045a08
JB
931 if (hdr.circuit_t != IS_LEVEL_1 &&
932 hdr.circuit_t != IS_LEVEL_2 &&
933 hdr.circuit_t != IS_LEVEL_1_AND_2 &&
934 (level & hdr.circuit_t) == 0)
935 {
936 zlog_err ("Level %d LAN Hello with Circuit Type %d", level,
937 hdr.circuit_t);
f390d2c7 938 return ISIS_ERROR;
939 }
3f045a08 940
eb5d44eb 941 /*
942 * Then get the tlvs
943 */
944 expected |= TLVFLAG_AUTH_INFO;
945 expected |= TLVFLAG_AREA_ADDRS;
946 expected |= TLVFLAG_LAN_NEIGHS;
947 expected |= TLVFLAG_NLPID;
948 expected |= TLVFLAG_IPV4_ADDR;
949 expected |= TLVFLAG_IPV6_ADDR;
950
3f045a08 951 auth_tlv_offset = stream_get_getp (circuit->rcv_stream);
eb5d44eb 952 retval = parse_tlvs (circuit->area->area_tag,
3f045a08
JB
953 STREAM_PNT (circuit->rcv_stream),
954 hdr.pdu_len - ISIS_LANHELLO_HDRLEN - ISIS_FIXED_HDR_LEN,
955 &expected, &found, &tlvs,
956 &auth_tlv_offset);
eb5d44eb 957
f390d2c7 958 if (retval > ISIS_WARNING)
959 {
960 zlog_warn ("parse_tlvs() failed");
961 goto out;
962 }
eb5d44eb 963
f390d2c7 964 if (!(found & TLVFLAG_AREA_ADDRS))
965 {
966 zlog_warn ("No Area addresses TLV in Level %d LAN IS to IS hello",
967 level);
eb5d44eb 968 retval = ISIS_WARNING;
969 goto out;
970 }
f390d2c7 971
3f045a08 972 /* Verify authentication, either cleartext of HMAC MD5 */
f390d2c7 973 if (circuit->passwd.type)
974 {
975 if (!(found & TLVFLAG_AUTH_INFO) ||
3f045a08
JB
976 authentication_check (&tlvs.auth_info, &circuit->passwd,
977 circuit->rcv_stream, auth_tlv_offset))
978 {
979 isis_event_auth_failure (circuit->area->area_tag,
980 "LAN hello authentication failure",
981 hdr.source_id);
982 retval = ISIS_WARNING;
983 goto out;
984 }
f390d2c7 985 }
eb5d44eb 986
987 /*
988 * Accept the level 1 adjacency only if a match between local and
989 * remote area addresses is found
990 */
3f045a08
JB
991 if (listcount (circuit->area->area_addrs) == 0 ||
992 (level == IS_LEVEL_1 &&
993 area_match (circuit->area->area_addrs, tlvs.area_addrs) == 0))
f390d2c7 994 {
995 if (isis->debugs & DEBUG_ADJ_PACKETS)
996 {
529d65b3 997 zlog_debug ("ISIS-Adj (%s): Area mismatch, level %d IIH on %s",
998 circuit->area->area_tag, level,
999 circuit->interface->name);
f390d2c7 1000 }
1001 retval = ISIS_OK;
1002 goto out;
eb5d44eb 1003 }
eb5d44eb 1004
1005 /*
1006 * it's own IIH PDU - discard silently
f390d2c7 1007 */
1008 if (!memcmp (circuit->u.bc.snpa, ssnpa, ETH_ALEN))
1009 {
529d65b3 1010 zlog_debug ("ISIS-Adj (%s): it's own IIH PDU - discarded",
1011 circuit->area->area_tag);
eb5d44eb 1012
f390d2c7 1013 retval = ISIS_OK;
1014 goto out;
1015 }
eb5d44eb 1016
1017 /*
1018 * check if it's own interface ip match iih ip addrs
1019 */
3f045a08
JB
1020 if ((found & TLVFLAG_IPV4_ADDR) == 0 ||
1021 ip_match (circuit->ip_addrs, tlvs.ipv4_addrs) == 0)
f390d2c7 1022 {
3f045a08
JB
1023 zlog_debug ("ISIS-Adj: No usable IP interface addresses "
1024 "in LAN IIH from %s\n", circuit->interface->name);
f390d2c7 1025 retval = ISIS_WARNING;
1026 goto out;
1027 }
eb5d44eb 1028
1029 adj = isis_adj_lookup (hdr.source_id, circuit->u.bc.adjdb[level - 1]);
3f045a08
JB
1030 if ((adj == NULL) || (memcmp(adj->snpa, ssnpa, ETH_ALEN)) ||
1031 (adj->level != level))
f390d2c7 1032 {
3f045a08
JB
1033 if (!adj)
1034 {
1035 /*
1036 * Do as in 8.4.2.5
1037 */
1038 adj = isis_new_adj (hdr.source_id, ssnpa, level, circuit);
1039 if (adj == NULL)
1040 {
1041 retval = ISIS_ERROR;
1042 goto out;
1043 }
1044 }
1045 else
1046 {
1047 if (ssnpa) {
1048 memcpy (adj->snpa, ssnpa, 6);
1049 } else {
1050 memset (adj->snpa, ' ', 6);
1051 }
1052 adj->level = level;
1053 }
f390d2c7 1054 isis_adj_state_change (adj, ISIS_ADJ_INITIALIZING, NULL);
eb5d44eb 1055
3f045a08
JB
1056 if (level == IS_LEVEL_1)
1057 adj->sys_type = ISIS_SYSTYPE_L1_IS;
f390d2c7 1058 else
3f045a08 1059 adj->sys_type = ISIS_SYSTYPE_L2_IS;
f390d2c7 1060 list_delete_all_node (circuit->u.bc.lan_neighs[level - 1]);
1061 isis_adj_build_neigh_list (circuit->u.bc.adjdb[level - 1],
3f045a08 1062 circuit->u.bc.lan_neighs[level - 1]);
eb5d44eb 1063 }
eb5d44eb 1064
a211d65d 1065 if(adj->dis_record[level-1].dis==ISIS_IS_DIS)
1066 switch (level)
1067 {
1068 case 1:
1069 if (memcmp (circuit->u.bc.l1_desig_is, hdr.lan_id, ISIS_SYS_ID_LEN + 1))
1070 {
3f045a08 1071 thread_add_event (master, isis_event_dis_status_change, circuit, 0);
64a7afd6 1072 memcpy (&circuit->u.bc.l1_desig_is, hdr.lan_id,
1073 ISIS_SYS_ID_LEN + 1);
a211d65d 1074 }
1075 break;
1076 case 2:
1077 if (memcmp (circuit->u.bc.l2_desig_is, hdr.lan_id, ISIS_SYS_ID_LEN + 1))
1078 {
3f045a08 1079 thread_add_event (master, isis_event_dis_status_change, circuit, 0);
64a7afd6 1080 memcpy (&circuit->u.bc.l2_desig_is, hdr.lan_id,
1081 ISIS_SYS_ID_LEN + 1);
a211d65d 1082 }
1083 break;
1084 }
eb5d44eb 1085
1086 adj->hold_time = hdr.hold_time;
f390d2c7 1087 adj->last_upd = time (NULL);
1088 adj->prio[level - 1] = hdr.prio;
eb5d44eb 1089
1090 memcpy (adj->lanid, hdr.lan_id, ISIS_SYS_ID_LEN + 1);
1091
3f045a08
JB
1092 tlvs_to_adj_area_addrs (&tlvs, adj);
1093
eb5d44eb 1094 /* which protocol are spoken ??? */
f390d2c7 1095 if (found & TLVFLAG_NLPID)
eb5d44eb 1096 tlvs_to_adj_nlpids (&tlvs, adj);
1097
1098 /* we need to copy addresses to the adj */
f390d2c7 1099 if (found & TLVFLAG_IPV4_ADDR)
eb5d44eb 1100 tlvs_to_adj_ipv4_addrs (&tlvs, adj);
1101
1102#ifdef HAVE_IPV6
f390d2c7 1103 if (found & TLVFLAG_IPV6_ADDR)
eb5d44eb 1104 tlvs_to_adj_ipv6_addrs (&tlvs, adj);
1105#endif /* HAVE_IPV6 */
1106
1107 adj->circuit_t = hdr.circuit_t;
1108
1109 /* lets take care of the expiry */
f390d2c7 1110 THREAD_TIMER_OFF (adj->t_expire);
1111 THREAD_TIMER_ON (master, adj->t_expire, isis_adj_expire, adj,
3f045a08 1112 (long) adj->hold_time);
eb5d44eb 1113
1114 /*
1115 * If the snpa for this circuit is found from LAN Neighbours TLV
1116 * we have two-way communication -> adjacency can be put to state "up"
1117 */
1118
f390d2c7 1119 if (found & TLVFLAG_LAN_NEIGHS)
3f045a08
JB
1120 {
1121 if (adj->adj_state != ISIS_ADJ_UP)
f390d2c7 1122 {
3f045a08
JB
1123 for (ALL_LIST_ELEMENTS_RO (tlvs.lan_neighs, node, snpa))
1124 {
1125 if (!memcmp (snpa, circuit->u.bc.snpa, ETH_ALEN))
1126 {
1127 isis_adj_state_change (adj, ISIS_ADJ_UP,
1128 "own SNPA found in LAN Neighbours TLV");
1129 }
1130 }
1131 }
1132 else
1133 {
1134 int found = 0;
1135 for (ALL_LIST_ELEMENTS_RO (tlvs.lan_neighs, node, snpa))
1136 if (!memcmp (snpa, circuit->u.bc.snpa, ETH_ALEN))
1137 {
1138 found = 1;
1139 break;
1140 }
1141 if (found == 0)
1142 isis_adj_state_change (adj, ISIS_ADJ_INITIALIZING,
1143 "own SNPA not found in LAN Neighbours TLV");
eb5d44eb 1144 }
3f045a08
JB
1145 }
1146 else if (adj->adj_state == ISIS_ADJ_UP)
1147 {
1148 isis_adj_state_change (adj, ISIS_ADJ_INITIALIZING,
1149 "no LAN Neighbours TLV found");
1150 }
eb5d44eb 1151
f390d2c7 1152out:
f390d2c7 1153 if (isis->debugs & DEBUG_ADJ_PACKETS)
1154 {
529d65b3 1155 zlog_debug ("ISIS-Adj (%s): Rcvd L%d LAN IIH from %s on %s, cirType %s, "
1156 "cirID %u, length %ld",
1157 circuit->area->area_tag,
1158 level, snpa_print (ssnpa), circuit->interface->name,
3f045a08 1159 circuit_t2string (circuit->is_type),
29e50b23 1160 circuit->circuit_id,
3f045a08 1161 stream_get_endp (circuit->rcv_stream));
f390d2c7 1162 }
eb5d44eb 1163
1164 free_tlvs (&tlvs);
1165
1166 return retval;
1167}
1168
1169/*
1170 * Process Level 1/2 Link State
1171 * ISO - 10589
1172 * Section 7.3.15.1 - Action on receipt of a link state PDU
f390d2c7 1173 */
1174static int
1175process_lsp (int level, struct isis_circuit *circuit, u_char * ssnpa)
eb5d44eb 1176{
1177 struct isis_link_state_hdr *hdr;
1178 struct isis_adjacency *adj = NULL;
1179 struct isis_lsp *lsp, *lsp0 = NULL;
1180 int retval = ISIS_OK, comp = 0;
1181 u_char lspid[ISIS_SYS_ID_LEN + 2];
1182 struct isis_passwd *passwd;
e38e0df0 1183 uint16_t pdu_len;
eb5d44eb 1184
3f045a08
JB
1185 if (isis->debugs & DEBUG_UPDATE_PACKETS)
1186 {
1187 zlog_debug ("ISIS-Upd (%s): Rcvd L%d LSP on %s, cirType %s, cirID %u",
1188 circuit->area->area_tag, level, circuit->interface->name,
1189 circuit_t2string (circuit->is_type), circuit->circuit_id);
1190 if (isis->debugs & DEBUG_PACKET_DUMP)
1191 zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
1192 stream_get_endp (circuit->rcv_stream));
1193 }
1194
f390d2c7 1195 if ((stream_get_endp (circuit->rcv_stream) -
1196 stream_get_getp (circuit->rcv_stream)) < ISIS_LSP_HDR_LEN)
1197 {
1198 zlog_warn ("Packet too short");
1199 return ISIS_WARNING;
1200 }
eb5d44eb 1201
1202 /* Reference the header */
f390d2c7 1203 hdr = (struct isis_link_state_hdr *) STREAM_PNT (circuit->rcv_stream);
e38e0df0
SV
1204 pdu_len = ntohs (hdr->pdu_len);
1205
1206 /* lsp length check */
a22ab5a5 1207 if (pdu_len < (ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN) ||
e38e0df0
SV
1208 pdu_len > ISO_MTU(circuit) ||
1209 pdu_len > stream_get_endp (circuit->rcv_stream))
1210 {
1211 zlog_debug ("ISIS-Upd (%s): LSP %s invalid LSP length %d",
1212 circuit->area->area_tag,
1213 rawlspid_print (hdr->lsp_id), pdu_len);
1214
1215 return ISIS_WARNING;
1216 }
1217
1218 /*
1219 * Set the stream endp to PDU length, ignoring additional padding
1220 * introduced by transport chips.
1221 */
1222 if (pdu_len < stream_get_endp (circuit->rcv_stream))
1223 stream_set_endp (circuit->rcv_stream, pdu_len);
f390d2c7 1224
1225 if (isis->debugs & DEBUG_UPDATE_PACKETS)
1226 {
529d65b3 1227 zlog_debug ("ISIS-Upd (%s): Rcvd L%d LSP %s, seq 0x%08x, cksum 0x%04x, "
3f045a08 1228 "lifetime %us, len %u, on %s",
529d65b3 1229 circuit->area->area_tag,
1230 level,
1231 rawlspid_print (hdr->lsp_id),
1232 ntohl (hdr->seq_num),
1233 ntohs (hdr->checksum),
1234 ntohs (hdr->rem_lifetime),
e38e0df0 1235 pdu_len,
15935e9a 1236 circuit->interface->name);
f390d2c7 1237 }
eb5d44eb 1238
e38e0df0
SV
1239 /* lsp is_type check */
1240 if ((hdr->lsp_bits & IS_LEVEL_1_AND_2) != IS_LEVEL_1 &&
1241 (hdr->lsp_bits & IS_LEVEL_1_AND_2) != IS_LEVEL_1_AND_2)
3f045a08 1242 {
e38e0df0 1243 zlog_debug ("ISIS-Upd (%s): LSP %s invalid LSP is type %x",
3f045a08 1244 circuit->area->area_tag,
e38e0df0
SV
1245 rawlspid_print (hdr->lsp_id), hdr->lsp_bits);
1246 /* continue as per RFC1122 Be liberal in what you accept, and
1247 * conservative in what you send */
3f045a08 1248 }
eb5d44eb 1249
1250 /* Checksum sanity check - FIXME: move to correct place */
1251 /* 12 = sysid+pdu+remtime */
f390d2c7 1252 if (iso_csum_verify (STREAM_PNT (circuit->rcv_stream) + 4,
e38e0df0 1253 pdu_len - 12, &hdr->checksum))
f390d2c7 1254 {
529d65b3 1255 zlog_debug ("ISIS-Upd (%s): LSP %s invalid LSP checksum 0x%04x",
1256 circuit->area->area_tag,
1257 rawlspid_print (hdr->lsp_id), ntohs (hdr->checksum));
f390d2c7 1258
1259 return ISIS_WARNING;
1260 }
eb5d44eb 1261
1262 /* 7.3.15.1 a) 1 - external domain circuit will discard lsps */
f390d2c7 1263 if (circuit->ext_domain)
1264 {
529d65b3 1265 zlog_debug
f390d2c7 1266 ("ISIS-Upd (%s): LSP %s received at level %d over circuit with "
1267 "externalDomain = true", circuit->area->area_tag,
1268 rawlspid_print (hdr->lsp_id), level);
1269
1270 return ISIS_WARNING;
1271 }
eb5d44eb 1272
1273 /* 7.3.15.1 a) 2,3 - manualL2OnlyMode not implemented */
3f045a08 1274 if (!accept_level (level, circuit->is_type))
f390d2c7 1275 {
529d65b3 1276 zlog_debug ("ISIS-Upd (%s): LSP %s received at level %d over circuit of"
1277 " type %s",
1278 circuit->area->area_tag,
1279 rawlspid_print (hdr->lsp_id),
3f045a08 1280 level, circuit_t2string (circuit->is_type));
f390d2c7 1281
1282 return ISIS_WARNING;
1283 }
eb5d44eb 1284
1285 /* 7.3.15.1 a) 4 - need to make sure IDLength matches */
1286
1287 /* 7.3.15.1 a) 5 - maximum area match, can be ommited since we only use 3 */
1288
1289 /* 7.3.15.1 a) 7 - password check */
3f045a08
JB
1290 (level == IS_LEVEL_1) ? (passwd = &circuit->area->area_passwd) :
1291 (passwd = &circuit->area->domain_passwd);
f390d2c7 1292 if (passwd->type)
1293 {
3f045a08
JB
1294 if (lsp_authentication_check (circuit->rcv_stream, circuit->area,
1295 level, passwd))
f390d2c7 1296 {
1297 isis_event_auth_failure (circuit->area->area_tag,
1298 "LSP authentication failure", hdr->lsp_id);
1299 return ISIS_WARNING;
1300 }
eb5d44eb 1301 }
eb5d44eb 1302 /* Find the LSP in our database and compare it to this Link State header */
1303 lsp = lsp_search (hdr->lsp_id, circuit->area->lspdb[level - 1]);
1304 if (lsp)
f390d2c7 1305 comp = lsp_compare (circuit->area->area_tag, lsp, hdr->seq_num,
1306 hdr->checksum, hdr->rem_lifetime);
1307 if (lsp && (lsp->own_lsp
eb5d44eb 1308#ifdef TOPOLOGY_GENERATE
f390d2c7 1309 || lsp->from_topology
eb5d44eb 1310#endif /* TOPOLOGY_GENERATE */
f390d2c7 1311 ))
eb5d44eb 1312 goto dontcheckadj;
1313
1314 /* 7.3.15.1 a) 6 - Must check that we have an adjacency of the same level */
1315 /* for broadcast circuits, snpa should be compared */
eb5d44eb 1316
f390d2c7 1317 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
1318 {
1319 adj = isis_adj_lookup_snpa (ssnpa, circuit->u.bc.adjdb[level - 1]);
1320 if (!adj)
1321 {
529d65b3 1322 zlog_debug ("(%s): DS ======= LSP %s, seq 0x%08x, cksum 0x%04x, "
1323 "lifetime %us on %s",
1324 circuit->area->area_tag,
1325 rawlspid_print (hdr->lsp_id),
1326 ntohl (hdr->seq_num),
1327 ntohs (hdr->checksum),
1328 ntohs (hdr->rem_lifetime), circuit->interface->name);
f390d2c7 1329 return ISIS_WARNING; /* Silently discard */
1330 }
eb5d44eb 1331 }
eb5d44eb 1332 /* for non broadcast, we just need to find same level adj */
f390d2c7 1333 else
1334 {
1335 /* If no adj, or no sharing of level */
1336 if (!circuit->u.p2p.neighbor)
1337 {
1338 return ISIS_OK; /* Silently discard */
1339 }
1340 else
1341 {
3f045a08 1342 if (((level == IS_LEVEL_1) &&
f390d2c7 1343 (circuit->u.p2p.neighbor->adj_usage == ISIS_ADJ_LEVEL2)) ||
3f045a08 1344 ((level == IS_LEVEL_2) &&
f390d2c7 1345 (circuit->u.p2p.neighbor->adj_usage == ISIS_ADJ_LEVEL1)))
1346 return ISIS_WARNING; /* Silently discard */
3f045a08 1347 adj = circuit->u.p2p.neighbor;
f390d2c7 1348 }
eb5d44eb 1349 }
3f045a08 1350
f390d2c7 1351dontcheckadj:
eb5d44eb 1352 /* 7.3.15.1 a) 7 - Passwords for level 1 - not implemented */
1353
1354 /* 7.3.15.1 a) 8 - Passwords for level 2 - not implemented */
1355
f390d2c7 1356 /* 7.3.15.1 a) 9 - OriginatingLSPBufferSize - not implemented FIXME: do it */
1357
1358 /* 7.3.15.1 b) - If the remaining life time is 0, we perform 7.3.16.4 */
1359 if (hdr->rem_lifetime == 0)
1360 {
1361 if (!lsp)
1362 {
1363 /* 7.3.16.4 a) 1) No LSP in db -> send an ack, but don't save */
1364 /* only needed on explicit update, eg - p2p */
1365 if (circuit->circ_type == CIRCUIT_T_P2P)
1366 ack_lsp (hdr, circuit, level);
1367 return retval; /* FIXME: do we need a purge? */
1368 }
1369 else
1370 {
1371 if (memcmp (hdr->lsp_id, isis->sysid, ISIS_SYS_ID_LEN))
1372 {
1373 /* LSP by some other system -> do 7.3.16.4 b) */
1374 /* 7.3.16.4 b) 1) */
1375 if (comp == LSP_NEWER)
1376 {
3f045a08 1377 lsp_update (lsp, circuit->rcv_stream, circuit->area, level);
f390d2c7 1378 /* ii */
3f045a08 1379 lsp_set_all_srmflags (lsp);
f390d2c7 1380 /* iii */
1381 ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
1382 /* v */
1383 ISIS_FLAGS_CLEAR_ALL (lsp->SSNflags); /* FIXME: OTHER than c */
1384 /* iv */
1385 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
1386 ISIS_SET_FLAG (lsp->SSNflags, circuit);
1387
1388 } /* 7.3.16.4 b) 2) */
1389 else if (comp == LSP_EQUAL)
1390 {
1391 /* i */
1392 ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
1393 /* ii */
1394 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
1395 ISIS_SET_FLAG (lsp->SSNflags, circuit);
1396 } /* 7.3.16.4 b) 3) */
1397 else
1398 {
1399 ISIS_SET_FLAG (lsp->SRMflags, circuit);
1400 ISIS_CLEAR_FLAG (lsp->SSNflags, circuit);
1401 }
1402 }
3f045a08
JB
1403 else if (lsp->lsp_header->rem_lifetime != 0)
1404 {
1405 /* our own LSP -> 7.3.16.4 c) */
1406 if (comp == LSP_NEWER)
1407 {
1408 lsp_inc_seqnum (lsp, ntohl (hdr->seq_num));
1409 lsp_set_all_srmflags (lsp);
1410 }
1411 else
1412 {
1413 ISIS_SET_FLAG (lsp->SRMflags, circuit);
1414 ISIS_CLEAR_FLAG (lsp->SSNflags, circuit);
1415 }
1416 if (isis->debugs & DEBUG_UPDATE_PACKETS)
1417 zlog_debug ("ISIS-Upd (%s): (1) re-originating LSP %s new "
1418 "seq 0x%08x", circuit->area->area_tag,
1419 rawlspid_print (hdr->lsp_id),
1420 ntohl (lsp->lsp_header->seq_num));
1421 }
f390d2c7 1422 }
1423 return retval;
eb5d44eb 1424 }
eb5d44eb 1425 /* 7.3.15.1 c) - If this is our own lsp and we don't have it initiate a
1426 * purge */
f390d2c7 1427 if (memcmp (hdr->lsp_id, isis->sysid, ISIS_SYS_ID_LEN) == 0)
1428 {
1429 if (!lsp)
1430 {
1431 /* 7.3.16.4: initiate a purge */
1432 lsp_purge_non_exist (hdr, circuit->area);
1433 return ISIS_OK;
1434 }
1435 /* 7.3.15.1 d) - If this is our own lsp and we have it */
1436
1437 /* In 7.3.16.1, If an Intermediate system R somewhere in the domain
1438 * has information that the current sequence number for source S is
1439 * "greater" than that held by S, ... */
1440
e38e0df0 1441 if (ntohl (hdr->seq_num) > ntohl (lsp->lsp_header->seq_num))
f390d2c7 1442 {
1443 /* 7.3.16.1 */
3f045a08 1444 lsp_inc_seqnum (lsp, ntohl (hdr->seq_num));
c89c05dd 1445 if (isis->debugs & DEBUG_UPDATE_PACKETS)
1446 zlog_debug ("ISIS-Upd (%s): (2) re-originating LSP %s new seq "
1447 "0x%08x", circuit->area->area_tag,
1448 rawlspid_print (hdr->lsp_id),
1449 ntohl (lsp->lsp_header->seq_num));
f390d2c7 1450 }
e38e0df0
SV
1451 /* If the received LSP is older or equal,
1452 * resend the LSP which will act as ACK */
1453 lsp_set_all_srmflags (lsp);
eb5d44eb 1454 }
f390d2c7 1455 else
1456 {
1457 /* 7.3.15.1 e) - This lsp originated on another system */
1458
1459 /* 7.3.15.1 e) 1) LSP newer than the one in db or no LSP in db */
1460 if ((!lsp || comp == LSP_NEWER))
1461 {
f390d2c7 1462 /*
1463 * If this lsp is a frag, need to see if we have zero lsp present
1464 */
1465 if (LSP_FRAGMENT (hdr->lsp_id) != 0)
1466 {
1467 memcpy (lspid, hdr->lsp_id, ISIS_SYS_ID_LEN + 1);
1468 LSP_FRAGMENT (lspid) = 0;
1469 lsp0 = lsp_search (lspid, circuit->area->lspdb[level - 1]);
1470 if (!lsp0)
1471 {
3f045a08 1472 zlog_debug ("Got lsp frag, while zero lsp not in database");
f390d2c7 1473 return ISIS_OK;
1474 }
1475 }
3f045a08
JB
1476 /* i */
1477 if (!lsp)
1478 {
1479 lsp = lsp_new_from_stream_ptr (circuit->rcv_stream,
e38e0df0 1480 pdu_len, lsp0,
3f045a08
JB
1481 circuit->area, level);
1482 lsp_insert (lsp, circuit->area->lspdb[level - 1]);
1483 }
1484 else /* exists, so we overwrite */
1485 {
1486 lsp_update (lsp, circuit->rcv_stream, circuit->area, level);
1487 }
f390d2c7 1488 /* ii */
3f045a08 1489 lsp_set_all_srmflags (lsp);
f390d2c7 1490 /* iii */
1491 ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
1492
1493 /* iv */
1494 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
1495 ISIS_SET_FLAG (lsp->SSNflags, circuit);
1496 /* FIXME: v) */
1497 }
1498 /* 7.3.15.1 e) 2) LSP equal to the one in db */
1499 else if (comp == LSP_EQUAL)
1500 {
1501 ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
3f045a08 1502 lsp_update (lsp, circuit->rcv_stream, circuit->area, level);
f390d2c7 1503 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
3f045a08 1504 ISIS_SET_FLAG (lsp->SSNflags, circuit);
f390d2c7 1505 }
1506 /* 7.3.15.1 e) 3) LSP older than the one in db */
1507 else
1508 {
1509 ISIS_SET_FLAG (lsp->SRMflags, circuit);
1510 ISIS_CLEAR_FLAG (lsp->SSNflags, circuit);
1511 }
eb5d44eb 1512 }
eb5d44eb 1513 return retval;
1514}
1515
1516/*
1517 * Process Sequence Numbers
1518 * ISO - 10589
1519 * Section 7.3.15.2 - Action on receipt of a sequence numbers PDU
1520 */
1521
92365889 1522static int
f390d2c7 1523process_snp (int snp_type, int level, struct isis_circuit *circuit,
1524 u_char * ssnpa)
eb5d44eb 1525{
1526 int retval = ISIS_OK;
1527 int cmp, own_lsp;
1528 char typechar = ' ';
e38e0df0 1529 uint16_t pdu_len;
eb5d44eb 1530 struct isis_adjacency *adj;
1531 struct isis_complete_seqnum_hdr *chdr = NULL;
1532 struct isis_partial_seqnum_hdr *phdr = NULL;
3f045a08 1533 uint32_t found = 0, expected = 0, auth_tlv_offset = 0;
eb5d44eb 1534 struct isis_lsp *lsp;
1535 struct lsp_entry *entry;
1eb8ef25 1536 struct listnode *node, *nnode;
1537 struct listnode *node2, *nnode2;
eb5d44eb 1538 struct tlvs tlvs;
1539 struct list *lsp_list = NULL;
1540 struct isis_passwd *passwd;
1541
f390d2c7 1542 if (snp_type == ISIS_SNP_CSNP_FLAG)
1543 {
1544 /* getting the header info */
1545 typechar = 'C';
1546 chdr =
1547 (struct isis_complete_seqnum_hdr *) STREAM_PNT (circuit->rcv_stream);
e38e0df0
SV
1548 stream_forward_getp (circuit->rcv_stream, ISIS_CSNP_HDRLEN);
1549 pdu_len = ntohs (chdr->pdu_len);
a22ab5a5 1550 if (pdu_len < (ISIS_FIXED_HDR_LEN + ISIS_CSNP_HDRLEN) ||
e38e0df0
SV
1551 pdu_len > ISO_MTU(circuit) ||
1552 pdu_len > stream_get_endp (circuit->rcv_stream))
f390d2c7 1553 {
e38e0df0
SV
1554 zlog_warn ("Received a CSNP with bogus length %d", pdu_len);
1555 return ISIS_WARNING;
f390d2c7 1556 }
eb5d44eb 1557 }
f390d2c7 1558 else
1559 {
1560 typechar = 'P';
1561 phdr =
1562 (struct isis_partial_seqnum_hdr *) STREAM_PNT (circuit->rcv_stream);
e38e0df0
SV
1563 stream_forward_getp (circuit->rcv_stream, ISIS_PSNP_HDRLEN);
1564 pdu_len = ntohs (phdr->pdu_len);
a22ab5a5 1565 if (pdu_len < (ISIS_FIXED_HDR_LEN + ISIS_PSNP_HDRLEN) ||
e38e0df0
SV
1566 pdu_len > ISO_MTU(circuit) ||
1567 pdu_len > stream_get_endp (circuit->rcv_stream))
f390d2c7 1568 {
e38e0df0
SV
1569 zlog_warn ("Received a CSNP with bogus length %d", pdu_len);
1570 return ISIS_WARNING;
f390d2c7 1571 }
eb5d44eb 1572 }
eb5d44eb 1573
e38e0df0
SV
1574 /*
1575 * Set the stream endp to PDU length, ignoring additional padding
1576 * introduced by transport chips.
1577 */
1578 if (pdu_len < stream_get_endp (circuit->rcv_stream))
1579 stream_set_endp (circuit->rcv_stream, pdu_len);
1580
eb5d44eb 1581 /* 7.3.15.2 a) 1 - external domain circuit will discard snp pdu */
f390d2c7 1582 if (circuit->ext_domain)
1583 {
eb5d44eb 1584
529d65b3 1585 zlog_debug ("ISIS-Snp (%s): Rcvd L%d %cSNP on %s, "
1586 "skipping: circuit externalDomain = true",
1587 circuit->area->area_tag,
1588 level, typechar, circuit->interface->name);
eb5d44eb 1589
f390d2c7 1590 return ISIS_OK;
1591 }
eb5d44eb 1592
1593 /* 7.3.15.2 a) 2,3 - manualL2OnlyMode not implemented */
3f045a08 1594 if (!accept_level (level, circuit->is_type))
f390d2c7 1595 {
eb5d44eb 1596
529d65b3 1597 zlog_debug ("ISIS-Snp (%s): Rcvd L%d %cSNP on %s, "
1598 "skipping: circuit type %s does not match level %d",
1599 circuit->area->area_tag,
1600 level,
1601 typechar,
1602 circuit->interface->name,
3f045a08 1603 circuit_t2string (circuit->is_type), level);
eb5d44eb 1604
1605 return ISIS_OK;
1606 }
f390d2c7 1607
1608 /* 7.3.15.2 a) 4 - not applicable for CSNP only PSNPs on broadcast */
1609 if ((snp_type == ISIS_SNP_PSNP_FLAG) &&
3f045a08
JB
1610 (circuit->circ_type == CIRCUIT_T_BROADCAST) &&
1611 (!circuit->u.bc.is_dr[level - 1]))
f390d2c7 1612 {
3f045a08
JB
1613 zlog_debug ("ISIS-Snp (%s): Rcvd L%d %cSNP from %s on %s, "
1614 "skipping: we are not the DIS",
1615 circuit->area->area_tag,
1616 level,
1617 typechar, snpa_print (ssnpa), circuit->interface->name);
f390d2c7 1618
3f045a08 1619 return ISIS_OK;
f390d2c7 1620 }
eb5d44eb 1621
1622 /* 7.3.15.2 a) 5 - need to make sure IDLength matches - already checked */
1623
1624 /* 7.3.15.2 a) 6 - maximum area match, can be ommited since we only use 3
1625 * - already checked */
1626
1627 /* 7.3.15.2 a) 7 - Must check that we have an adjacency of the same level */
1628 /* for broadcast circuits, snpa should be compared */
1629 /* FIXME : Do we need to check SNPA? */
f390d2c7 1630 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
1631 {
1632 if (snp_type == ISIS_SNP_CSNP_FLAG)
1633 {
1634 adj =
1635 isis_adj_lookup (chdr->source_id, circuit->u.bc.adjdb[level - 1]);
1636 }
1637 else
1638 {
1639 /* a psnp on a broadcast, how lovely of Juniper :) */
1640 adj =
1641 isis_adj_lookup (phdr->source_id, circuit->u.bc.adjdb[level - 1]);
1642 }
1643 if (!adj)
1644 return ISIS_OK; /* Silently discard */
1645 }
1646 else
1647 {
1648 if (!circuit->u.p2p.neighbor)
3f045a08
JB
1649 {
1650 zlog_warn ("no p2p neighbor on circuit %s", circuit->interface->name);
1651 return ISIS_OK; /* Silently discard */
1652 }
f390d2c7 1653 }
eb5d44eb 1654
1655 /* 7.3.15.2 a) 8 - Passwords for level 1 - not implemented */
1656
1657 /* 7.3.15.2 a) 9 - Passwords for level 2 - not implemented */
1658
1659 memset (&tlvs, 0, sizeof (struct tlvs));
1660
1661 /* parse the SNP */
1662 expected |= TLVFLAG_LSP_ENTRIES;
1663 expected |= TLVFLAG_AUTH_INFO;
3f045a08
JB
1664
1665 auth_tlv_offset = stream_get_getp (circuit->rcv_stream);
eb5d44eb 1666 retval = parse_tlvs (circuit->area->area_tag,
f390d2c7 1667 STREAM_PNT (circuit->rcv_stream),
e38e0df0 1668 pdu_len - stream_get_getp (circuit->rcv_stream),
3f045a08 1669 &expected, &found, &tlvs, &auth_tlv_offset);
eb5d44eb 1670
f390d2c7 1671 if (retval > ISIS_WARNING)
1672 {
1673 zlog_warn ("something went very wrong processing SNP");
1674 free_tlvs (&tlvs);
1675 return retval;
1676 }
eb5d44eb 1677
3f045a08 1678 if (level == IS_LEVEL_1)
1cbc562b 1679 passwd = &circuit->area->area_passwd;
1680 else
1681 passwd = &circuit->area->domain_passwd;
1682
1683 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_RECV))
f390d2c7 1684 {
1cbc562b 1685 if (passwd->type)
3f045a08
JB
1686 {
1687 if (!(found & TLVFLAG_AUTH_INFO) ||
1688 authentication_check (&tlvs.auth_info, passwd,
1689 circuit->rcv_stream, auth_tlv_offset))
1690 {
1691 isis_event_auth_failure (circuit->area->area_tag,
1692 "SNP authentication" " failure",
1693 phdr ? phdr->source_id :
1694 chdr->source_id);
1695 free_tlvs (&tlvs);
1696 return ISIS_OK;
1697 }
1698 }
f390d2c7 1699 }
eb5d44eb 1700
1701 /* debug isis snp-packets */
f390d2c7 1702 if (isis->debugs & DEBUG_SNP_PACKETS)
1703 {
529d65b3 1704 zlog_debug ("ISIS-Snp (%s): Rcvd L%d %cSNP from %s on %s",
1705 circuit->area->area_tag,
1706 level,
1707 typechar, snpa_print (ssnpa), circuit->interface->name);
f390d2c7 1708 if (tlvs.lsp_entries)
1709 {
3fdb2dd9 1710 for (ALL_LIST_ELEMENTS_RO (tlvs.lsp_entries, node, entry))
f390d2c7 1711 {
529d65b3 1712 zlog_debug ("ISIS-Snp (%s): %cSNP entry %s, seq 0x%08x,"
1713 " cksum 0x%04x, lifetime %us",
1714 circuit->area->area_tag,
1715 typechar,
1716 rawlspid_print (entry->lsp_id),
1717 ntohl (entry->seq_num),
1718 ntohs (entry->checksum), ntohs (entry->rem_lifetime));
f390d2c7 1719 }
1720 }
eb5d44eb 1721 }
eb5d44eb 1722
1723 /* 7.3.15.2 b) Actions on LSP_ENTRIES reported */
f390d2c7 1724 if (tlvs.lsp_entries)
1725 {
3fdb2dd9 1726 for (ALL_LIST_ELEMENTS_RO (tlvs.lsp_entries, node, entry))
f390d2c7 1727 {
1728 lsp = lsp_search (entry->lsp_id, circuit->area->lspdb[level - 1]);
1729 own_lsp = !memcmp (entry->lsp_id, isis->sysid, ISIS_SYS_ID_LEN);
1730 if (lsp)
1731 {
1732 /* 7.3.15.2 b) 1) is this LSP newer */
1733 cmp = lsp_compare (circuit->area->area_tag, lsp, entry->seq_num,
1734 entry->checksum, entry->rem_lifetime);
1735 /* 7.3.15.2 b) 2) if it equals, clear SRM on p2p */
1736 if (cmp == LSP_EQUAL)
1737 {
3f045a08
JB
1738 /* if (circuit->circ_type != CIRCUIT_T_BROADCAST) */
1739 ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
f390d2c7 1740 }
3f045a08 1741 /* 7.3.15.2 b) 3) if it is older, clear SSN and set SRM */
f390d2c7 1742 else if (cmp == LSP_OLDER)
1743 {
1744 ISIS_CLEAR_FLAG (lsp->SSNflags, circuit);
1745 ISIS_SET_FLAG (lsp->SRMflags, circuit);
1746 }
3f045a08 1747 /* 7.3.15.2 b) 4) if it is newer, set SSN and clear SRM on p2p */
f390d2c7 1748 else
1749 {
f390d2c7 1750 if (own_lsp)
1751 {
1752 lsp_inc_seqnum (lsp, ntohl (entry->seq_num));
1753 ISIS_SET_FLAG (lsp->SRMflags, circuit);
1754 }
1755 else
1756 {
1757 ISIS_SET_FLAG (lsp->SSNflags, circuit);
3f045a08
JB
1758 /* if (circuit->circ_type != CIRCUIT_T_BROADCAST) */
1759 ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
f390d2c7 1760 }
1761 }
1762 }
1763 else
1764 {
1765 /* 7.3.15.2 b) 5) if it was not found, and all of those are not 0,
1766 * insert it and set SSN on it */
1767 if (entry->rem_lifetime && entry->checksum && entry->seq_num &&
1768 memcmp (entry->lsp_id, isis->sysid, ISIS_SYS_ID_LEN))
1769 {
1770 lsp = lsp_new (entry->lsp_id, ntohs (entry->rem_lifetime),
1771 0, 0, entry->checksum, level);
3f045a08 1772 lsp->area = circuit->area;
f390d2c7 1773 lsp_insert (lsp, circuit->area->lspdb[level - 1]);
3f045a08 1774 ISIS_FLAGS_CLEAR_ALL (lsp->SRMflags);
f390d2c7 1775 ISIS_SET_FLAG (lsp->SSNflags, circuit);
1776 }
1777 }
eb5d44eb 1778 }
1779 }
eb5d44eb 1780
1781 /* 7.3.15.2 c) on CSNP set SRM for all in range which were not reported */
f390d2c7 1782 if (snp_type == ISIS_SNP_CSNP_FLAG)
1783 {
1784 /*
3f045a08
JB
1785 * Build a list from our own LSP db bounded with
1786 * start_lsp_id and stop_lsp_id
f390d2c7 1787 */
1788 lsp_list = list_new ();
1789 lsp_build_list_nonzero_ht (chdr->start_lsp_id, chdr->stop_lsp_id,
1790 lsp_list, circuit->area->lspdb[level - 1]);
1791
1792 /* Fixme: Find a better solution */
1793 if (tlvs.lsp_entries)
1794 {
1eb8ef25 1795 for (ALL_LIST_ELEMENTS (tlvs.lsp_entries, node, nnode, entry))
f390d2c7 1796 {
1eb8ef25 1797 for (ALL_LIST_ELEMENTS (lsp_list, node2, nnode2, lsp))
f390d2c7 1798 {
1799 if (lsp_id_cmp (lsp->lsp_header->lsp_id, entry->lsp_id) == 0)
1800 {
1801 list_delete_node (lsp_list, node2);
1802 break;
1803 }
1804 }
1805 }
1806 }
1807 /* on remaining LSPs we set SRM (neighbor knew not of) */
3fdb2dd9 1808 for (ALL_LIST_ELEMENTS_RO (lsp_list, node, lsp))
f390d2c7 1809 ISIS_SET_FLAG (lsp->SRMflags, circuit);
f390d2c7 1810 /* lets free it */
3f045a08
JB
1811 list_delete (lsp_list);
1812
eb5d44eb 1813 }
eb5d44eb 1814
1815 free_tlvs (&tlvs);
1816 return retval;
1817}
1818
92365889 1819static int
f390d2c7 1820process_csnp (int level, struct isis_circuit *circuit, u_char * ssnpa)
eb5d44eb 1821{
3f045a08
JB
1822 if (isis->debugs & DEBUG_SNP_PACKETS)
1823 {
1824 zlog_debug ("ISIS-Snp (%s): Rcvd L%d CSNP on %s, cirType %s, cirID %u",
1825 circuit->area->area_tag, level, circuit->interface->name,
1826 circuit_t2string (circuit->is_type), circuit->circuit_id);
1827 if (isis->debugs & DEBUG_PACKET_DUMP)
1828 zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
1829 stream_get_endp (circuit->rcv_stream));
1830 }
1831
eb5d44eb 1832 /* Sanity check - FIXME: move to correct place */
f390d2c7 1833 if ((stream_get_endp (circuit->rcv_stream) -
1834 stream_get_getp (circuit->rcv_stream)) < ISIS_CSNP_HDRLEN)
1835 {
1836 zlog_warn ("Packet too short ( < %d)", ISIS_CSNP_HDRLEN);
1837 return ISIS_WARNING;
1838 }
eb5d44eb 1839
1840 return process_snp (ISIS_SNP_CSNP_FLAG, level, circuit, ssnpa);
1841}
1842
92365889 1843static int
f390d2c7 1844process_psnp (int level, struct isis_circuit *circuit, u_char * ssnpa)
eb5d44eb 1845{
3f045a08
JB
1846 if (isis->debugs & DEBUG_SNP_PACKETS)
1847 {
1848 zlog_debug ("ISIS-Snp (%s): Rcvd L%d PSNP on %s, cirType %s, cirID %u",
1849 circuit->area->area_tag, level, circuit->interface->name,
1850 circuit_t2string (circuit->is_type), circuit->circuit_id);
1851 if (isis->debugs & DEBUG_PACKET_DUMP)
1852 zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
1853 stream_get_endp (circuit->rcv_stream));
1854 }
1855
f390d2c7 1856 if ((stream_get_endp (circuit->rcv_stream) -
1857 stream_get_getp (circuit->rcv_stream)) < ISIS_PSNP_HDRLEN)
1858 {
3f045a08 1859 zlog_warn ("Packet too short ( < %d)", ISIS_PSNP_HDRLEN);
f390d2c7 1860 return ISIS_WARNING;
1861 }
eb5d44eb 1862
1863 return process_snp (ISIS_SNP_PSNP_FLAG, level, circuit, ssnpa);
1864}
1865
eb5d44eb 1866/*
1867 * Process ISH
1868 * ISO - 10589
1869 * Section 8.2.2 - Receiving ISH PDUs by an intermediate system
1870 * FIXME: sample packet dump, need to figure 0x81 - looks like NLPid
f390d2c7 1871 * 0x82 0x15 0x01 0x00 0x04 0x01 0x2c 0x59
1872 * 0x38 0x08 0x47 0x00 0x01 0x00 0x02 0x00
1873 * 0x03 0x00 0x81 0x01 0xcc
eb5d44eb 1874 */
92365889 1875static int
eb5d44eb 1876process_is_hello (struct isis_circuit *circuit)
1877{
1878 struct isis_adjacency *adj;
1879 int retval = ISIS_OK;
1880 u_char neigh_len;
1881 u_char *sysid;
1882
3f045a08
JB
1883 if (isis->debugs & DEBUG_ADJ_PACKETS)
1884 {
1885 zlog_debug ("ISIS-Adj (%s): Rcvd ISH on %s, cirType %s, cirID %u",
1886 circuit->area->area_tag, circuit->interface->name,
1887 circuit_t2string (circuit->is_type), circuit->circuit_id);
1888 if (isis->debugs & DEBUG_PACKET_DUMP)
1889 zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
1890 stream_get_endp (circuit->rcv_stream));
1891 }
1892
eb5d44eb 1893 /* In this point in time we are not yet able to handle is_hellos
1894 * on lan - Sorry juniper...
1895 */
1896 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
1897 return retval;
1898
1899 neigh_len = stream_getc (circuit->rcv_stream);
f390d2c7 1900 sysid = STREAM_PNT (circuit->rcv_stream) + neigh_len - 1 - ISIS_SYS_ID_LEN;
eb5d44eb 1901 adj = circuit->u.p2p.neighbor;
f390d2c7 1902 if (!adj)
1903 {
1904 /* 8.2.2 */
41b36e90 1905 adj = isis_new_adj (sysid, NULL, 0, circuit);
f390d2c7 1906 if (adj == NULL)
1907 return ISIS_ERROR;
eb5d44eb 1908
f390d2c7 1909 isis_adj_state_change (adj, ISIS_ADJ_INITIALIZING, NULL);
1910 adj->sys_type = ISIS_SYSTYPE_UNKNOWN;
1911 circuit->u.p2p.neighbor = adj;
1912 }
1913 /* 8.2.2 a) */
1914 if ((adj->adj_state == ISIS_ADJ_UP) && memcmp (adj->sysid, sysid,
1915 ISIS_SYS_ID_LEN))
1916 {
1917 /* 8.2.2 a) 1) FIXME: adjStateChange(down) event */
1918 /* 8.2.2 a) 2) delete the adj */
1919 XFREE (MTYPE_ISIS_ADJACENCY, adj);
1920 /* 8.2.2 a) 3) create a new adj */
41b36e90 1921 adj = isis_new_adj (sysid, NULL, 0, circuit);
f390d2c7 1922 if (adj == NULL)
1923 return ISIS_ERROR;
1924
1925 /* 8.2.2 a) 3) i */
1926 isis_adj_state_change (adj, ISIS_ADJ_INITIALIZING, NULL);
1927 /* 8.2.2 a) 3) ii */
1928 adj->sys_type = ISIS_SYSTYPE_UNKNOWN;
1929 /* 8.2.2 a) 4) quite meaningless */
1930 }
eb5d44eb 1931 /* 8.2.2 b) ignore on condition */
f390d2c7 1932 if ((adj->adj_state == ISIS_ADJ_INITIALIZING) &&
1933 (adj->sys_type == ISIS_SYSTYPE_IS))
1934 {
1935 /* do nothing */
1936 }
1937 else
1938 {
1939 /* 8.2.2 c) respond with a p2p IIH */
1940 send_hello (circuit, 1);
1941 }
eb5d44eb 1942 /* 8.2.2 d) type is IS */
f390d2c7 1943 adj->sys_type = ISIS_SYSTYPE_IS;
eb5d44eb 1944 /* 8.2.2 e) FIXME: Circuit type of? */
1945
eb5d44eb 1946 return retval;
1947}
1948
eb5d44eb 1949/*
1950 * PDU Dispatcher
1951 */
1952
92365889 1953static int
f390d2c7 1954isis_handle_pdu (struct isis_circuit *circuit, u_char * ssnpa)
eb5d44eb 1955{
eb5d44eb 1956 struct isis_fixed_hdr *hdr;
eb5d44eb 1957
f390d2c7 1958 int retval = ISIS_OK;
eb5d44eb 1959
1960 /*
1961 * Let's first read data from stream to the header
1962 */
f390d2c7 1963 hdr = (struct isis_fixed_hdr *) STREAM_DATA (circuit->rcv_stream);
eb5d44eb 1964
f390d2c7 1965 if ((hdr->idrp != ISO10589_ISIS) && (hdr->idrp != ISO9542_ESIS))
1966 {
3f045a08 1967 zlog_err ("Not an IS-IS or ES-IS packet IDRP=%02x", hdr->idrp);
f390d2c7 1968 return ISIS_ERROR;
1969 }
eb5d44eb 1970
1971 /* now we need to know if this is an ISO 9542 packet and
1972 * take real good care of it, waaa!
1973 */
f390d2c7 1974 if (hdr->idrp == ISO9542_ESIS)
1975 {
3f045a08
JB
1976 zlog_err ("No support for ES-IS packet IDRP=%02x", hdr->idrp);
1977 return ISIS_ERROR;
f390d2c7 1978 }
3f045a08
JB
1979 stream_set_getp (circuit->rcv_stream, ISIS_FIXED_HDR_LEN);
1980
eb5d44eb 1981 /*
1982 * and then process it
1983 */
1984
f390d2c7 1985 if (hdr->length < ISIS_MINIMUM_FIXED_HDR_LEN)
1986 {
1987 zlog_err ("Fixed header length = %d", hdr->length);
1988 return ISIS_ERROR;
1989 }
eb5d44eb 1990
f390d2c7 1991 if (hdr->version1 != 1)
1992 {
1993 zlog_warn ("Unsupported ISIS version %u", hdr->version1);
1994 return ISIS_WARNING;
1995 }
eb5d44eb 1996 /* either 6 or 0 */
f390d2c7 1997 if ((hdr->id_len != 0) && (hdr->id_len != ISIS_SYS_ID_LEN))
1998 {
1999 zlog_err
2000 ("IDFieldLengthMismatch: ID Length field in a received PDU %u, "
2001 "while the parameter for this IS is %u", hdr->id_len,
2002 ISIS_SYS_ID_LEN);
2003 return ISIS_ERROR;
2004 }
eb5d44eb 2005
f390d2c7 2006 if (hdr->version2 != 1)
2007 {
2008 zlog_warn ("Unsupported ISIS version %u", hdr->version2);
2009 return ISIS_WARNING;
2010 }
3f045a08
JB
2011
2012 if (circuit->is_passive)
2013 {
2014 zlog_warn ("Received ISIS PDU on passive circuit %s",
2015 circuit->interface->name);
2016 return ISIS_WARNING;
2017 }
2018
eb5d44eb 2019 /* either 3 or 0 */
f390d2c7 2020 if ((hdr->max_area_addrs != 0)
2021 && (hdr->max_area_addrs != isis->max_area_addrs))
2022 {
2023 zlog_err ("maximumAreaAddressesMismatch: maximumAreaAdresses in a "
2024 "received PDU %u while the parameter for this IS is %u",
2025 hdr->max_area_addrs, isis->max_area_addrs);
2026 return ISIS_ERROR;
2027 }
eb5d44eb 2028
f390d2c7 2029 switch (hdr->pdu_type)
2030 {
2031 case L1_LAN_HELLO:
2032 retval = process_lan_hello (ISIS_LEVEL1, circuit, ssnpa);
2033 break;
2034 case L2_LAN_HELLO:
2035 retval = process_lan_hello (ISIS_LEVEL2, circuit, ssnpa);
2036 break;
2037 case P2P_HELLO:
2038 retval = process_p2p_hello (circuit);
2039 break;
2040 case L1_LINK_STATE:
2041 retval = process_lsp (ISIS_LEVEL1, circuit, ssnpa);
2042 break;
2043 case L2_LINK_STATE:
2044 retval = process_lsp (ISIS_LEVEL2, circuit, ssnpa);
2045 break;
2046 case L1_COMPLETE_SEQ_NUM:
2047 retval = process_csnp (ISIS_LEVEL1, circuit, ssnpa);
2048 break;
2049 case L2_COMPLETE_SEQ_NUM:
2050 retval = process_csnp (ISIS_LEVEL2, circuit, ssnpa);
2051 break;
2052 case L1_PARTIAL_SEQ_NUM:
2053 retval = process_psnp (ISIS_LEVEL1, circuit, ssnpa);
2054 break;
2055 case L2_PARTIAL_SEQ_NUM:
2056 retval = process_psnp (ISIS_LEVEL2, circuit, ssnpa);
2057 break;
2058 default:
2059 return ISIS_ERROR;
2060 }
eb5d44eb 2061
2062 return retval;
2063}
2064
eb5d44eb 2065#ifdef GNU_LINUX
2066int
2067isis_receive (struct thread *thread)
2068{
eb5d44eb 2069 struct isis_circuit *circuit;
2070 u_char ssnpa[ETH_ALEN];
2071 int retval;
2072
2073 /*
2074 * Get the circuit
2075 */
2076 circuit = THREAD_ARG (thread);
2077 assert (circuit);
2078
2079 if (circuit->rcv_stream == NULL)
f390d2c7 2080 circuit->rcv_stream = stream_new (ISO_MTU (circuit));
eb5d44eb 2081 else
2082 stream_reset (circuit->rcv_stream);
2083
2084 retval = circuit->rx (circuit, ssnpa);
f390d2c7 2085 circuit->t_read = NULL;
eb5d44eb 2086
2087 if (retval == ISIS_OK)
2088 retval = isis_handle_pdu (circuit, ssnpa);
2089
2090 /*
2091 * prepare for next packet.
2092 */
3f045a08
JB
2093 if (!circuit->is_passive)
2094 {
2095 THREAD_READ_ON (master, circuit->t_read, isis_receive, circuit,
2096 circuit->fd);
2097 }
eb5d44eb 2098
2099 return retval;
2100}
2101
2102#else
2103int
2104isis_receive (struct thread *thread)
2105{
eb5d44eb 2106 struct isis_circuit *circuit;
2107 u_char ssnpa[ETH_ALEN];
2108 int retval;
2109
2110 /*
2111 * Get the circuit
2112 */
2113 circuit = THREAD_ARG (thread);
2114 assert (circuit);
2115
f390d2c7 2116 circuit->t_read = NULL;
eb5d44eb 2117
2118 if (circuit->rcv_stream == NULL)
f390d2c7 2119 circuit->rcv_stream = stream_new (ISO_MTU (circuit));
eb5d44eb 2120 else
2121 stream_reset (circuit->rcv_stream);
2122
2123 retval = circuit->rx (circuit, ssnpa);
2124
2125 if (retval == ISIS_OK)
2126 retval = isis_handle_pdu (circuit, ssnpa);
2127
2128 /*
2129 * prepare for next packet.
2130 */
3f045a08
JB
2131 if (!circuit->is_passive)
2132 {
2133 circuit->t_read = thread_add_timer_msec (master, isis_receive, circuit,
2134 listcount
2135 (circuit->area->circuit_list) *
2136 100);
2137 }
eb5d44eb 2138
2139 return retval;
2140}
2141
2142#endif
2143
2144 /* filling of the fixed isis header */
2145void
2146fill_fixed_hdr (struct isis_fixed_hdr *hdr, u_char pdu_type)
2147{
2148 memset (hdr, 0, sizeof (struct isis_fixed_hdr));
2149
2150 hdr->idrp = ISO10589_ISIS;
2151
f390d2c7 2152 switch (pdu_type)
2153 {
2154 case L1_LAN_HELLO:
2155 case L2_LAN_HELLO:
2156 hdr->length = ISIS_LANHELLO_HDRLEN;
2157 break;
2158 case P2P_HELLO:
2159 hdr->length = ISIS_P2PHELLO_HDRLEN;
2160 break;
2161 case L1_LINK_STATE:
2162 case L2_LINK_STATE:
2163 hdr->length = ISIS_LSP_HDR_LEN;
2164 break;
2165 case L1_COMPLETE_SEQ_NUM:
2166 case L2_COMPLETE_SEQ_NUM:
2167 hdr->length = ISIS_CSNP_HDRLEN;
2168 break;
2169 case L1_PARTIAL_SEQ_NUM:
2170 case L2_PARTIAL_SEQ_NUM:
2171 hdr->length = ISIS_PSNP_HDRLEN;
2172 break;
2173 default:
2174 zlog_warn ("fill_fixed_hdr(): unknown pdu type %d", pdu_type);
2175 return;
2176 }
eb5d44eb 2177 hdr->length += ISIS_FIXED_HDR_LEN;
2178 hdr->pdu_type = pdu_type;
2179 hdr->version1 = 1;
f390d2c7 2180 hdr->id_len = 0; /* ISIS_SYS_ID_LEN - 0==6 */
eb5d44eb 2181 hdr->version2 = 1;
f390d2c7 2182 hdr->max_area_addrs = 0; /* isis->max_area_addrs - 0==3 */
eb5d44eb 2183}
2184
eb5d44eb 2185/*
2186 * SEND SIDE
2187 */
92365889 2188static void
eb5d44eb 2189fill_fixed_hdr_andstream (struct isis_fixed_hdr *hdr, u_char pdu_type,
f390d2c7 2190 struct stream *stream)
eb5d44eb 2191{
f390d2c7 2192 fill_fixed_hdr (hdr, pdu_type);
eb5d44eb 2193
2194 stream_putc (stream, hdr->idrp);
2195 stream_putc (stream, hdr->length);
2196 stream_putc (stream, hdr->version1);
2197 stream_putc (stream, hdr->id_len);
2198 stream_putc (stream, hdr->pdu_type);
2199 stream_putc (stream, hdr->version2);
2200 stream_putc (stream, hdr->reserved);
2201 stream_putc (stream, hdr->max_area_addrs);
2202
2203 return;
2204}
2205
eb5d44eb 2206int
2207send_hello (struct isis_circuit *circuit, int level)
2208{
2209 struct isis_fixed_hdr fixed_hdr;
2210 struct isis_lan_hello_hdr hello_hdr;
2211 struct isis_p2p_hello_hdr p2p_hello_hdr;
3f045a08
JB
2212 unsigned char hmac_md5_hash[ISIS_AUTH_MD5_SIZE];
2213 unsigned long len_pointer, length, auth_tlv_offset = 0;
eb5d44eb 2214 u_int32_t interval;
eb5d44eb 2215 int retval;
2216
3f045a08
JB
2217 if (circuit->is_passive)
2218 return ISIS_OK;
2219
f390d2c7 2220 if (circuit->interface->mtu == 0)
2221 {
2222 zlog_warn ("circuit has zero MTU");
2223 return ISIS_WARNING;
2224 }
eb5d44eb 2225
2226 if (!circuit->snd_stream)
f390d2c7 2227 circuit->snd_stream = stream_new (ISO_MTU (circuit));
eb5d44eb 2228 else
2229 stream_reset (circuit->snd_stream);
2230
2231 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
3f045a08 2232 if (level == IS_LEVEL_1)
f390d2c7 2233 fill_fixed_hdr_andstream (&fixed_hdr, L1_LAN_HELLO,
2234 circuit->snd_stream);
eb5d44eb 2235 else
f390d2c7 2236 fill_fixed_hdr_andstream (&fixed_hdr, L2_LAN_HELLO,
2237 circuit->snd_stream);
eb5d44eb 2238 else
f390d2c7 2239 fill_fixed_hdr_andstream (&fixed_hdr, P2P_HELLO, circuit->snd_stream);
eb5d44eb 2240
2241 /*
2242 * Fill LAN Level 1 or 2 Hello PDU header
2243 */
2244 memset (&hello_hdr, 0, sizeof (struct isis_lan_hello_hdr));
f390d2c7 2245 interval = circuit->hello_multiplier[level - 1] *
eb5d44eb 2246 circuit->hello_interval[level - 1];
2247 if (interval > USHRT_MAX)
2248 interval = USHRT_MAX;
3f045a08 2249 hello_hdr.circuit_t = circuit->is_type;
eb5d44eb 2250 memcpy (hello_hdr.source_id, isis->sysid, ISIS_SYS_ID_LEN);
f390d2c7 2251 hello_hdr.hold_time = htons ((u_int16_t) interval);
eb5d44eb 2252
f390d2c7 2253 hello_hdr.pdu_len = 0; /* Update the PDU Length later */
9985f83c 2254 len_pointer = stream_get_endp (circuit->snd_stream) + 3 + ISIS_SYS_ID_LEN;
eb5d44eb 2255
2256 /* copy the shared part of the hello to the p2p hello if needed */
f390d2c7 2257 if (circuit->circ_type == CIRCUIT_T_P2P)
2258 {
2259 memcpy (&p2p_hello_hdr, &hello_hdr, 5 + ISIS_SYS_ID_LEN);
2260 p2p_hello_hdr.local_id = circuit->circuit_id;
2261 /* FIXME: need better understanding */
2262 stream_put (circuit->snd_stream, &p2p_hello_hdr, ISIS_P2PHELLO_HDRLEN);
2263 }
2264 else
2265 {
3f045a08
JB
2266 hello_hdr.prio = circuit->priority[level - 1];
2267 if (level == IS_LEVEL_1)
f390d2c7 2268 {
2269 memcpy (hello_hdr.lan_id, circuit->u.bc.l1_desig_is,
2270 ISIS_SYS_ID_LEN + 1);
2271 }
3f045a08 2272 else if (level == IS_LEVEL_2)
f390d2c7 2273 {
2274 memcpy (hello_hdr.lan_id, circuit->u.bc.l2_desig_is,
2275 ISIS_SYS_ID_LEN + 1);
2276 }
2277 stream_put (circuit->snd_stream, &hello_hdr, ISIS_LANHELLO_HDRLEN);
2278 }
eb5d44eb 2279
2280 /*
3f045a08 2281 * Then the variable length part.
eb5d44eb 2282 */
3f045a08 2283
eb5d44eb 2284 /* add circuit password */
3f045a08
JB
2285 switch (circuit->passwd.type)
2286 {
2287 /* Cleartext */
2288 case ISIS_PASSWD_TYPE_CLEARTXT:
2289 if (tlv_add_authinfo (circuit->passwd.type, circuit->passwd.len,
2290 circuit->passwd.passwd, circuit->snd_stream))
2291 return ISIS_WARNING;
2292 break;
2293
2294 /* HMAC MD5 */
2295 case ISIS_PASSWD_TYPE_HMAC_MD5:
2296 /* Remember where TLV is written so we can later overwrite the MD5 hash */
2297 auth_tlv_offset = stream_get_endp (circuit->snd_stream);
2298 memset(&hmac_md5_hash, 0, ISIS_AUTH_MD5_SIZE);
2299 if (tlv_add_authinfo (circuit->passwd.type, ISIS_AUTH_MD5_SIZE,
2300 hmac_md5_hash, circuit->snd_stream))
2301 return ISIS_WARNING;
2302 break;
2303
2304 default:
2305 break;
2306 }
2307
eb5d44eb 2308 /* Area Addresses TLV */
3f045a08
JB
2309 if (listcount (circuit->area->area_addrs) == 0)
2310 return ISIS_WARNING;
2311 if (tlv_add_area_addrs (circuit->area->area_addrs, circuit->snd_stream))
2312 return ISIS_WARNING;
eb5d44eb 2313
2314 /* LAN Neighbors TLV */
f390d2c7 2315 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
2316 {
3f045a08
JB
2317 if (level == IS_LEVEL_1 && circuit->u.bc.lan_neighs[0] &&
2318 listcount (circuit->u.bc.lan_neighs[0]) > 0)
f390d2c7 2319 if (tlv_add_lan_neighs (circuit->u.bc.lan_neighs[0],
2320 circuit->snd_stream))
2321 return ISIS_WARNING;
3f045a08
JB
2322 if (level == IS_LEVEL_2 && circuit->u.bc.lan_neighs[1] &&
2323 listcount (circuit->u.bc.lan_neighs[1]) > 0)
f390d2c7 2324 if (tlv_add_lan_neighs (circuit->u.bc.lan_neighs[1],
2325 circuit->snd_stream))
2326 return ISIS_WARNING;
2327 }
eb5d44eb 2328
2329 /* Protocols Supported TLV */
f390d2c7 2330 if (circuit->nlpids.count > 0)
eb5d44eb 2331 if (tlv_add_nlpid (&circuit->nlpids, circuit->snd_stream))
2332 return ISIS_WARNING;
2333 /* IP interface Address TLV */
3f045a08
JB
2334 if (circuit->ip_router && circuit->ip_addrs &&
2335 listcount (circuit->ip_addrs) > 0)
eb5d44eb 2336 if (tlv_add_ip_addrs (circuit->ip_addrs, circuit->snd_stream))
2337 return ISIS_WARNING;
2338
f390d2c7 2339#ifdef HAVE_IPV6
eb5d44eb 2340 /* IPv6 Interface Address TLV */
f390d2c7 2341 if (circuit->ipv6_router && circuit->ipv6_link &&
3f045a08 2342 listcount (circuit->ipv6_link) > 0)
eb5d44eb 2343 if (tlv_add_ipv6_addrs (circuit->ipv6_link, circuit->snd_stream))
2344 return ISIS_WARNING;
2345#endif /* HAVE_IPV6 */
2346
3f045a08 2347 if (circuit->pad_hellos)
eb5d44eb 2348 if (tlv_add_padding (circuit->snd_stream))
2349 return ISIS_WARNING;
2350
9985f83c 2351 length = stream_get_endp (circuit->snd_stream);
eb5d44eb 2352 /* Update PDU length */
f390d2c7 2353 stream_putw_at (circuit->snd_stream, len_pointer, (u_int16_t) length);
eb5d44eb 2354
3f045a08
JB
2355 /* For HMAC MD5 we need to compute the md5 hash and store it */
2356 if (circuit->passwd.type == ISIS_PASSWD_TYPE_HMAC_MD5)
2357 {
2358 hmac_md5 (STREAM_DATA (circuit->snd_stream),
2359 stream_get_endp (circuit->snd_stream),
2360 (unsigned char *) &circuit->passwd.passwd, circuit->passwd.len,
2361 (caddr_t) &hmac_md5_hash);
2362 /* Copy the hash into the stream */
2363 memcpy (STREAM_DATA (circuit->snd_stream) + auth_tlv_offset + 3,
2364 hmac_md5_hash, ISIS_AUTH_MD5_SIZE);
2365 }
eb5d44eb 2366
f390d2c7 2367 if (isis->debugs & DEBUG_ADJ_PACKETS)
2368 {
2369 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
2370 {
529d65b3 2371 zlog_debug ("ISIS-Adj (%s): Sent L%d LAN IIH on %s, length %ld",
2372 circuit->area->area_tag, level, circuit->interface->name,
29e50b23 2373 /* FIXME: use %z when we stop supporting old compilers. */
3f045a08 2374 length);
f390d2c7 2375 }
2376 else
2377 {
529d65b3 2378 zlog_debug ("ISIS-Adj (%s): Sent P2P IIH on %s, length %ld",
2379 circuit->area->area_tag, circuit->interface->name,
29e50b23 2380 /* FIXME: use %z when we stop supporting old compilers. */
3f045a08 2381 length);
f390d2c7 2382 }
3f045a08
JB
2383 if (isis->debugs & DEBUG_PACKET_DUMP)
2384 zlog_dump_data (STREAM_DATA (circuit->snd_stream),
2385 stream_get_endp (circuit->snd_stream));
eb5d44eb 2386 }
eb5d44eb 2387
3f045a08
JB
2388 retval = circuit->tx (circuit, level);
2389 if (retval != ISIS_OK)
2390 zlog_err ("ISIS-Adj (%s): Send L%d IIH on %s failed",
2391 circuit->area->area_tag, level, circuit->interface->name);
eb5d44eb 2392
3f045a08 2393 return retval;
eb5d44eb 2394}
2395
2396int
2397send_lan_l1_hello (struct thread *thread)
2398{
eb5d44eb 2399 struct isis_circuit *circuit;
2400 int retval;
2401
2402 circuit = THREAD_ARG (thread);
2403 assert (circuit);
2404 circuit->u.bc.t_send_lan_hello[0] = NULL;
2405
2406 if (circuit->u.bc.run_dr_elect[0])
f390d2c7 2407 retval = isis_dr_elect (circuit, 1);
eb5d44eb 2408
3f045a08 2409 retval = send_hello (circuit, 1);
eb5d44eb 2410
2411 /* set next timer thread */
f390d2c7 2412 THREAD_TIMER_ON (master, circuit->u.bc.t_send_lan_hello[0],
2413 send_lan_l1_hello, circuit,
2414 isis_jitter (circuit->hello_interval[0], IIH_JITTER));
eb5d44eb 2415
2416 return retval;
2417}
2418
2419int
2420send_lan_l2_hello (struct thread *thread)
2421{
2422 struct isis_circuit *circuit;
2423 int retval;
2424
2425 circuit = THREAD_ARG (thread);
2426 assert (circuit);
2427 circuit->u.bc.t_send_lan_hello[1] = NULL;
2428
2429 if (circuit->u.bc.run_dr_elect[1])
2430 retval = isis_dr_elect (circuit, 2);
2431
3f045a08 2432 retval = send_hello (circuit, 2);
eb5d44eb 2433
f390d2c7 2434 /* set next timer thread */
2435 THREAD_TIMER_ON (master, circuit->u.bc.t_send_lan_hello[1],
2436 send_lan_l2_hello, circuit,
2437 isis_jitter (circuit->hello_interval[1], IIH_JITTER));
eb5d44eb 2438
2439 return retval;
2440}
2441
2442int
2443send_p2p_hello (struct thread *thread)
2444{
2445 struct isis_circuit *circuit;
2446
2447 circuit = THREAD_ARG (thread);
2448 assert (circuit);
2449 circuit->u.p2p.t_send_p2p_hello = NULL;
2450
f390d2c7 2451 send_hello (circuit, 1);
eb5d44eb 2452
f390d2c7 2453 /* set next timer thread */
2454 THREAD_TIMER_ON (master, circuit->u.p2p.t_send_p2p_hello, send_p2p_hello,
2455 circuit, isis_jitter (circuit->hello_interval[1],
2456 IIH_JITTER));
eb5d44eb 2457
2458 return ISIS_OK;
2459}
2460
92365889 2461static int
f390d2c7 2462build_csnp (int level, u_char * start, u_char * stop, struct list *lsps,
2463 struct isis_circuit *circuit)
eb5d44eb 2464{
2465 struct isis_fixed_hdr fixed_hdr;
2466 struct isis_passwd *passwd;
eb5d44eb 2467 unsigned long lenp;
2468 u_int16_t length;
3f045a08
JB
2469 unsigned char hmac_md5_hash[ISIS_AUTH_MD5_SIZE];
2470 unsigned long auth_tlv_offset = 0;
2471 int retval = ISIS_OK;
2472
2473 if (circuit->snd_stream == NULL)
2474 circuit->snd_stream = stream_new (ISO_MTU (circuit));
2475 else
2476 stream_reset (circuit->snd_stream);
eb5d44eb 2477
3f045a08 2478 if (level == IS_LEVEL_1)
f390d2c7 2479 fill_fixed_hdr_andstream (&fixed_hdr, L1_COMPLETE_SEQ_NUM,
2480 circuit->snd_stream);
eb5d44eb 2481 else
f390d2c7 2482 fill_fixed_hdr_andstream (&fixed_hdr, L2_COMPLETE_SEQ_NUM,
2483 circuit->snd_stream);
eb5d44eb 2484
2485 /*
2486 * Fill Level 1 or 2 Complete Sequence Numbers header
2487 */
2488
9985f83c 2489 lenp = stream_get_endp (circuit->snd_stream);
f390d2c7 2490 stream_putw (circuit->snd_stream, 0); /* PDU length - when we know it */
eb5d44eb 2491 /* no need to send the source here, it is always us if we csnp */
2492 stream_put (circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
2493 /* with zero circuit id - ref 9.10, 9.11 */
2494 stream_putc (circuit->snd_stream, 0x00);
2495
2496 stream_put (circuit->snd_stream, start, ISIS_SYS_ID_LEN + 2);
2497 stream_put (circuit->snd_stream, stop, ISIS_SYS_ID_LEN + 2);
2498
2499 /*
2500 * And TLVs
2501 */
3f045a08 2502 if (level == IS_LEVEL_1)
eb5d44eb 2503 passwd = &circuit->area->area_passwd;
2504 else
2505 passwd = &circuit->area->domain_passwd;
2506
1cbc562b 2507 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
3f045a08
JB
2508 {
2509 switch (passwd->type)
2510 {
2511 /* Cleartext */
2512 case ISIS_PASSWD_TYPE_CLEARTXT:
2513 if (tlv_add_authinfo (ISIS_PASSWD_TYPE_CLEARTXT, passwd->len,
2514 passwd->passwd, circuit->snd_stream))
2515 return ISIS_WARNING;
2516 break;
2517
2518 /* HMAC MD5 */
2519 case ISIS_PASSWD_TYPE_HMAC_MD5:
2520 /* Remember where TLV is written so we can later overwrite the MD5 hash */
2521 auth_tlv_offset = stream_get_endp (circuit->snd_stream);
2522 memset(&hmac_md5_hash, 0, ISIS_AUTH_MD5_SIZE);
2523 if (tlv_add_authinfo (ISIS_PASSWD_TYPE_HMAC_MD5, ISIS_AUTH_MD5_SIZE,
2524 hmac_md5_hash, circuit->snd_stream))
2525 return ISIS_WARNING;
2526 break;
2527
2528 default:
2529 break;
f390d2c7 2530 }
3f045a08
JB
2531 }
2532
2533 retval = tlv_add_lsp_entries (lsps, circuit->snd_stream);
2534 if (retval != ISIS_OK)
2535 return retval;
2536
9985f83c 2537 length = (u_int16_t) stream_get_endp (circuit->snd_stream);
eb5d44eb 2538 /* Update PU length */
2539 stream_putw_at (circuit->snd_stream, lenp, length);
2540
3f045a08
JB
2541 /* For HMAC MD5 we need to compute the md5 hash and store it */
2542 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND) &&
2543 passwd->type == ISIS_PASSWD_TYPE_HMAC_MD5)
2544 {
2545 hmac_md5 (STREAM_DATA (circuit->snd_stream),
2546 stream_get_endp(circuit->snd_stream),
2547 (unsigned char *) &passwd->passwd, passwd->len,
2548 (caddr_t) &hmac_md5_hash);
2549 /* Copy the hash into the stream */
2550 memcpy (STREAM_DATA (circuit->snd_stream) + auth_tlv_offset + 3,
2551 hmac_md5_hash, ISIS_AUTH_MD5_SIZE);
2552 }
2553
eb5d44eb 2554 return retval;
2555}
2556
3f045a08
JB
2557/*
2558 * Count the maximum number of lsps that can be accomodated by a given size.
2559 */
2560static uint16_t
2561get_max_lsp_count (uint16_t size)
2562{
2563 uint16_t tlv_count;
2564 uint16_t lsp_count;
2565 uint16_t remaining_size;
2566
2567 /* First count the full size TLVs */
2568 tlv_count = size / MAX_LSP_ENTRIES_TLV_SIZE;
2569 lsp_count = tlv_count * (MAX_LSP_ENTRIES_TLV_SIZE / LSP_ENTRIES_LEN);
2570
2571 /* The last TLV, if any */
2572 remaining_size = size % MAX_LSP_ENTRIES_TLV_SIZE;
2573 if (remaining_size - 2 >= LSP_ENTRIES_LEN)
2574 lsp_count += (remaining_size - 2) / LSP_ENTRIES_LEN;
2575
2576 return lsp_count;
2577}
2578
2579/*
2580 * Calculate the length of Authentication Info. TLV.
2581 */
2582static uint16_t
2583auth_tlv_length (int level, struct isis_circuit *circuit)
2584{
2585 struct isis_passwd *passwd;
2586 uint16_t length;
2587
2588 if (level == IS_LEVEL_1)
2589 passwd = &circuit->area->area_passwd;
2590 else
2591 passwd = &circuit->area->domain_passwd;
2592
2593 /* Also include the length of TLV header */
2594 length = AUTH_INFO_HDRLEN;
2595 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
2596 {
2597 switch (passwd->type)
2598 {
2599 /* Cleartext */
2600 case ISIS_PASSWD_TYPE_CLEARTXT:
2601 length += passwd->len;
2602 break;
2603
2604 /* HMAC MD5 */
2605 case ISIS_PASSWD_TYPE_HMAC_MD5:
2606 length += ISIS_AUTH_MD5_SIZE;
2607 break;
2608
2609 default:
2610 break;
2611 }
2612 }
2613
2614 return length;
2615}
2616
2617/*
2618 * Calculate the maximum number of lsps that can be accomodated in a CSNP/PSNP.
2619 */
2620static uint16_t
2621max_lsps_per_snp (int snp_type, int level, struct isis_circuit *circuit)
2622{
2623 int snp_hdr_len;
2624 int auth_tlv_len;
2625 uint16_t lsp_count;
2626
2627 snp_hdr_len = ISIS_FIXED_HDR_LEN;
2628 if (snp_type == ISIS_SNP_CSNP_FLAG)
2629 snp_hdr_len += ISIS_CSNP_HDRLEN;
2630 else
2631 snp_hdr_len += ISIS_PSNP_HDRLEN;
2632
2633 auth_tlv_len = auth_tlv_length (level, circuit);
2634 lsp_count = get_max_lsp_count (
2635 stream_get_size (circuit->snd_stream) - snp_hdr_len - auth_tlv_len);
e38e0df0 2636 return lsp_count;
3f045a08
JB
2637}
2638
eb5d44eb 2639/*
2640 * FIXME: support multiple CSNPs
2641 */
2642
2643int
2644send_csnp (struct isis_circuit *circuit, int level)
2645{
eb5d44eb 2646 u_char start[ISIS_SYS_ID_LEN + 2];
2647 u_char stop[ISIS_SYS_ID_LEN + 2];
2648 struct list *list = NULL;
3fdb2dd9 2649 struct listnode *node;
eb5d44eb 2650 struct isis_lsp *lsp;
3f045a08
JB
2651 u_char num_lsps, loop = 1;
2652 int i, retval = ISIS_OK;
2653
2654 if (circuit->area->lspdb[level - 1] == NULL ||
2655 dict_count (circuit->area->lspdb[level - 1]) == 0)
2656 return retval;
eb5d44eb 2657
f390d2c7 2658 memset (start, 0x00, ISIS_SYS_ID_LEN + 2);
eb5d44eb 2659 memset (stop, 0xff, ISIS_SYS_ID_LEN + 2);
2660
3f045a08
JB
2661 num_lsps = max_lsps_per_snp (ISIS_SNP_CSNP_FLAG, level, circuit);
2662
2663 while (loop)
f390d2c7 2664 {
2665 list = list_new ();
3f045a08
JB
2666 lsp_build_list (start, stop, num_lsps, list,
2667 circuit->area->lspdb[level - 1]);
2668 /*
2669 * Update the stop lsp_id before encoding this CSNP.
2670 */
2671 if (listcount (list) < num_lsps)
2672 {
2673 memset (stop, 0xff, ISIS_SYS_ID_LEN + 2);
2674 }
f390d2c7 2675 else
3f045a08
JB
2676 {
2677 node = listtail (list);
2678 lsp = listgetdata (node);
2679 memcpy (stop, lsp->lsp_header->lsp_id, ISIS_SYS_ID_LEN + 2);
2680 }
f390d2c7 2681
2682 retval = build_csnp (level, start, stop, list, circuit);
3f045a08
JB
2683 if (retval != ISIS_OK)
2684 {
2685 zlog_err ("ISIS-Snp (%s): Build L%d CSNP on %s failed",
2686 circuit->area->area_tag, level, circuit->interface->name);
2687 list_delete (list);
2688 return retval;
2689 }
f390d2c7 2690
2691 if (isis->debugs & DEBUG_SNP_PACKETS)
3f045a08
JB
2692 {
2693 zlog_debug ("ISIS-Snp (%s): Sent L%d CSNP on %s, length %ld",
2694 circuit->area->area_tag, level, circuit->interface->name,
2695 stream_get_endp (circuit->snd_stream));
2696 for (ALL_LIST_ELEMENTS_RO (list, node, lsp))
2697 {
2698 zlog_debug ("ISIS-Snp (%s): CSNP entry %s, seq 0x%08x,"
2699 " cksum 0x%04x, lifetime %us",
2700 circuit->area->area_tag,
2701 rawlspid_print (lsp->lsp_header->lsp_id),
2702 ntohl (lsp->lsp_header->seq_num),
2703 ntohs (lsp->lsp_header->checksum),
2704 ntohs (lsp->lsp_header->rem_lifetime));
2705 }
2706 if (isis->debugs & DEBUG_PACKET_DUMP)
2707 zlog_dump_data (STREAM_DATA (circuit->snd_stream),
2708 stream_get_endp (circuit->snd_stream));
2709 }
2710
2711 retval = circuit->tx (circuit, level);
2712 if (retval != ISIS_OK)
2713 {
2714 zlog_err ("ISIS-Snp (%s): Send L%d CSNP on %s failed",
2715 circuit->area->area_tag, level,
2716 circuit->interface->name);
2717 list_delete (list);
2718 return retval;
2719 }
eb5d44eb 2720
3f045a08
JB
2721 /*
2722 * Start lsp_id of the next CSNP should be one plus the
2723 * stop lsp_id in this current CSNP.
2724 */
2725 memcpy (start, stop, ISIS_SYS_ID_LEN + 2);
2726 loop = 0;
2727 for (i = ISIS_SYS_ID_LEN + 1; i >= 0; --i)
2728 {
2729 if (start[i] < (u_char)0xff)
2730 {
2731 start[i] += 1;
2732 loop = 1;
2733 break;
2734 }
2735 }
2736 memset (stop, 0xff, ISIS_SYS_ID_LEN + 2);
f390d2c7 2737 list_delete (list);
f390d2c7 2738 }
3f045a08 2739
eb5d44eb 2740 return retval;
2741}
2742
2743int
2744send_l1_csnp (struct thread *thread)
2745{
2746 struct isis_circuit *circuit;
2747 int retval = ISIS_OK;
2748
2749 circuit = THREAD_ARG (thread);
2750 assert (circuit);
2751
2752 circuit->t_send_csnp[0] = NULL;
2753
f390d2c7 2754 if (circuit->circ_type == CIRCUIT_T_BROADCAST && circuit->u.bc.is_dr[0])
2755 {
2756 send_csnp (circuit, 1);
2757 }
eb5d44eb 2758 /* set next timer thread */
f390d2c7 2759 THREAD_TIMER_ON (master, circuit->t_send_csnp[0], send_l1_csnp, circuit,
2760 isis_jitter (circuit->csnp_interval[0], CSNP_JITTER));
eb5d44eb 2761
2762 return retval;
2763}
2764
2765int
2766send_l2_csnp (struct thread *thread)
2767{
2768 struct isis_circuit *circuit;
2769 int retval = ISIS_OK;
2770
2771 circuit = THREAD_ARG (thread);
2772 assert (circuit);
2773
2774 circuit->t_send_csnp[1] = NULL;
2775
f390d2c7 2776 if (circuit->circ_type == CIRCUIT_T_BROADCAST && circuit->u.bc.is_dr[1])
2777 {
2778 send_csnp (circuit, 2);
2779 }
eb5d44eb 2780 /* set next timer thread */
f390d2c7 2781 THREAD_TIMER_ON (master, circuit->t_send_csnp[1], send_l2_csnp, circuit,
2782 isis_jitter (circuit->csnp_interval[1], CSNP_JITTER));
d70f99e1 2783
eb5d44eb 2784 return retval;
2785}
2786
92365889 2787static int
eb5d44eb 2788build_psnp (int level, struct isis_circuit *circuit, struct list *lsps)
2789{
2790 struct isis_fixed_hdr fixed_hdr;
2791 unsigned long lenp;
2792 u_int16_t length;
eb5d44eb 2793 struct isis_lsp *lsp;
2794 struct isis_passwd *passwd;
3fdb2dd9 2795 struct listnode *node;
3f045a08
JB
2796 unsigned char hmac_md5_hash[ISIS_AUTH_MD5_SIZE];
2797 unsigned long auth_tlv_offset = 0;
2798 int retval = ISIS_OK;
eb5d44eb 2799
3f045a08
JB
2800 if (circuit->snd_stream == NULL)
2801 circuit->snd_stream = stream_new (ISO_MTU (circuit));
2802 else
2803 stream_reset (circuit->snd_stream);
2804
2805 if (level == IS_LEVEL_1)
f390d2c7 2806 fill_fixed_hdr_andstream (&fixed_hdr, L1_PARTIAL_SEQ_NUM,
2807 circuit->snd_stream);
eb5d44eb 2808 else
2809 fill_fixed_hdr_andstream (&fixed_hdr, L2_PARTIAL_SEQ_NUM,
f390d2c7 2810 circuit->snd_stream);
eb5d44eb 2811
2812 /*
2813 * Fill Level 1 or 2 Partial Sequence Numbers header
2814 */
9985f83c 2815 lenp = stream_get_endp (circuit->snd_stream);
f390d2c7 2816 stream_putw (circuit->snd_stream, 0); /* PDU length - when we know it */
eb5d44eb 2817 stream_put (circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
2818 stream_putc (circuit->snd_stream, circuit->idx);
2819
2820 /*
2821 * And TLVs
2822 */
2823
3f045a08 2824 if (level == IS_LEVEL_1)
eb5d44eb 2825 passwd = &circuit->area->area_passwd;
2826 else
2827 passwd = &circuit->area->domain_passwd;
2828
1cbc562b 2829 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
3f045a08
JB
2830 {
2831 switch (passwd->type)
2832 {
2833 /* Cleartext */
2834 case ISIS_PASSWD_TYPE_CLEARTXT:
2835 if (tlv_add_authinfo (ISIS_PASSWD_TYPE_CLEARTXT, passwd->len,
2836 passwd->passwd, circuit->snd_stream))
2837 return ISIS_WARNING;
2838 break;
2839
2840 /* HMAC MD5 */
2841 case ISIS_PASSWD_TYPE_HMAC_MD5:
2842 /* Remember where TLV is written so we can later overwrite the MD5 hash */
2843 auth_tlv_offset = stream_get_endp (circuit->snd_stream);
2844 memset(&hmac_md5_hash, 0, ISIS_AUTH_MD5_SIZE);
2845 if (tlv_add_authinfo (ISIS_PASSWD_TYPE_HMAC_MD5, ISIS_AUTH_MD5_SIZE,
2846 hmac_md5_hash, circuit->snd_stream))
2847 return ISIS_WARNING;
2848 break;
2849
2850 default:
2851 break;
f390d2c7 2852 }
3f045a08
JB
2853 }
2854
2855 retval = tlv_add_lsp_entries (lsps, circuit->snd_stream);
2856 if (retval != ISIS_OK)
2857 return retval;
eb5d44eb 2858
f390d2c7 2859 if (isis->debugs & DEBUG_SNP_PACKETS)
2860 {
3fdb2dd9 2861 for (ALL_LIST_ELEMENTS_RO (lsps, node, lsp))
f390d2c7 2862 {
529d65b3 2863 zlog_debug ("ISIS-Snp (%s): PSNP entry %s, seq 0x%08x,"
2864 " cksum 0x%04x, lifetime %us",
2865 circuit->area->area_tag,
2866 rawlspid_print (lsp->lsp_header->lsp_id),
2867 ntohl (lsp->lsp_header->seq_num),
2868 ntohs (lsp->lsp_header->checksum),
2869 ntohs (lsp->lsp_header->rem_lifetime));
f390d2c7 2870 }
eb5d44eb 2871 }
eb5d44eb 2872
9985f83c 2873 length = (u_int16_t) stream_get_endp (circuit->snd_stream);
eb5d44eb 2874 /* Update PDU length */
2875 stream_putw_at (circuit->snd_stream, lenp, length);
2876
3f045a08
JB
2877 /* For HMAC MD5 we need to compute the md5 hash and store it */
2878 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND) &&
2879 passwd->type == ISIS_PASSWD_TYPE_HMAC_MD5)
2880 {
2881 hmac_md5 (STREAM_DATA (circuit->snd_stream),
2882 stream_get_endp(circuit->snd_stream),
2883 (unsigned char *) &passwd->passwd, passwd->len,
2884 (caddr_t) &hmac_md5_hash);
2885 /* Copy the hash into the stream */
2886 memcpy (STREAM_DATA (circuit->snd_stream) + auth_tlv_offset + 3,
2887 hmac_md5_hash, ISIS_AUTH_MD5_SIZE);
2888 }
2889
eb5d44eb 2890 return ISIS_OK;
2891}
2892
2893/*
2894 * 7.3.15.4 action on expiration of partial SNP interval
2895 * level 1
2896 */
92365889 2897static int
eb5d44eb 2898send_psnp (int level, struct isis_circuit *circuit)
2899{
eb5d44eb 2900 struct isis_lsp *lsp;
2901 struct list *list = NULL;
3fdb2dd9 2902 struct listnode *node;
3f045a08
JB
2903 u_char num_lsps;
2904 int retval = ISIS_OK;
eb5d44eb 2905
3f045a08
JB
2906 if (circuit->circ_type == CIRCUIT_T_BROADCAST &&
2907 circuit->u.bc.is_dr[level - 1])
2908 return ISIS_OK;
eb5d44eb 2909
3f045a08
JB
2910 if (circuit->area->lspdb[level - 1] == NULL ||
2911 dict_count (circuit->area->lspdb[level - 1]) == 0)
2912 return ISIS_OK;
f390d2c7 2913
e38e0df0
SV
2914 if (! circuit->snd_stream)
2915 return ISIS_ERROR;
2916
3f045a08 2917 num_lsps = max_lsps_per_snp (ISIS_SNP_PSNP_FLAG, level, circuit);
f390d2c7 2918
3f045a08
JB
2919 while (1)
2920 {
2921 list = list_new ();
2922 lsp_build_list_ssn (circuit, num_lsps, list,
2923 circuit->area->lspdb[level - 1]);
2924
2925 if (listcount (list) == 0)
2926 {
2927 list_delete (list);
2928 return ISIS_OK;
2929 }
2930
2931 retval = build_psnp (level, circuit, list);
2932 if (retval != ISIS_OK)
2933 {
2934 zlog_err ("ISIS-Snp (%s): Build L%d PSNP on %s failed",
2935 circuit->area->area_tag, level, circuit->interface->name);
2936 list_delete (list);
2937 return retval;
2938 }
f390d2c7 2939
3f045a08
JB
2940 if (isis->debugs & DEBUG_SNP_PACKETS)
2941 {
2942 zlog_debug ("ISIS-Snp (%s): Sent L%d PSNP on %s, length %ld",
2943 circuit->area->area_tag, level,
2944 circuit->interface->name,
2945 stream_get_endp (circuit->snd_stream));
2946 if (isis->debugs & DEBUG_PACKET_DUMP)
2947 zlog_dump_data (STREAM_DATA (circuit->snd_stream),
2948 stream_get_endp (circuit->snd_stream));
2949 }
2950
2951 retval = circuit->tx (circuit, level);
2952 if (retval != ISIS_OK)
2953 {
2954 zlog_err ("ISIS-Snp (%s): Send L%d PSNP on %s failed",
2955 circuit->area->area_tag, level,
2956 circuit->interface->name);
2957 list_delete (list);
2958 return retval;
2959 }
f390d2c7 2960
3f045a08
JB
2961 /*
2962 * sending succeeded, we can clear SSN flags of this circuit
2963 * for the LSPs in list
2964 */
2965 for (ALL_LIST_ELEMENTS_RO (list, node, lsp))
2966 ISIS_CLEAR_FLAG (lsp->SSNflags, circuit);
2967 list_delete (list);
eb5d44eb 2968 }
eb5d44eb 2969
2970 return retval;
2971}
2972
2973int
2974send_l1_psnp (struct thread *thread)
2975{
2976
2977 struct isis_circuit *circuit;
2978 int retval = ISIS_OK;
2979
2980 circuit = THREAD_ARG (thread);
2981 assert (circuit);
2982
2983 circuit->t_send_psnp[0] = NULL;
2984
2985 send_psnp (1, circuit);
2986 /* set next timer thread */
f390d2c7 2987 THREAD_TIMER_ON (master, circuit->t_send_psnp[0], send_l1_psnp, circuit,
2988 isis_jitter (circuit->psnp_interval[0], PSNP_JITTER));
eb5d44eb 2989
2990 return retval;
2991}
2992
2993/*
2994 * 7.3.15.4 action on expiration of partial SNP interval
2995 * level 2
2996 */
2997int
2998send_l2_psnp (struct thread *thread)
2999{
eb5d44eb 3000 struct isis_circuit *circuit;
3001 int retval = ISIS_OK;
3002
3003 circuit = THREAD_ARG (thread);
3004 assert (circuit);
3005
3006 circuit->t_send_psnp[1] = NULL;
3007
3008 send_psnp (2, circuit);
3009
3010 /* set next timer thread */
f390d2c7 3011 THREAD_TIMER_ON (master, circuit->t_send_psnp[1], send_l2_psnp, circuit,
3012 isis_jitter (circuit->psnp_interval[1], PSNP_JITTER));
eb5d44eb 3013
3014 return retval;
3015}
3016
eb5d44eb 3017/*
3018 * ISO 10589 - 7.3.14.3
3019 */
3020int
3021send_lsp (struct thread *thread)
3022{
3023 struct isis_circuit *circuit;
3024 struct isis_lsp *lsp;
3025 struct listnode *node;
3f045a08 3026 int retval = ISIS_OK;
eb5d44eb 3027
3028 circuit = THREAD_ARG (thread);
3029 assert (circuit);
3030
3f045a08
JB
3031 if (circuit->state != C_STATE_UP || circuit->is_passive == 1)
3032 {
3033 return retval;
3034 }
eb5d44eb 3035
0fece074
AS
3036 node = listhead (circuit->lsp_queue);
3037
3038 /*
3039 * Handle case where there are no LSPs on the queue. This can
3040 * happen, for instance, if an adjacency goes down before this
3041 * thread gets a chance to run.
3042 */
3043 if (!node)
3044 {
3045 return retval;
3046 }
3047
3048 lsp = listgetdata(node);
eb5d44eb 3049
3f045a08
JB
3050 /*
3051 * Do not send if levels do not match
3052 */
3053 if (!(lsp->level & circuit->is_type))
3054 {
3055 list_delete_node (circuit->lsp_queue, node);
3056 return retval;
3057 }
f390d2c7 3058
3f045a08
JB
3059 /*
3060 * Do not send if we do not have adjacencies in state up on the circuit
3061 */
3062 if (circuit->upadjcount[lsp->level - 1] == 0)
3063 {
3064 list_delete_node (circuit->lsp_queue, node);
3065 return retval;
3066 }
f390d2c7 3067
3f045a08
JB
3068 /* copy our lsp to the send buffer */
3069 stream_copy (circuit->snd_stream, lsp->pdu);
eb5d44eb 3070
3f045a08
JB
3071 if (isis->debugs & DEBUG_UPDATE_PACKETS)
3072 {
3073 zlog_debug
3074 ("ISIS-Upd (%s): Sent L%d LSP %s, seq 0x%08x, cksum 0x%04x,"
3075 " lifetime %us on %s", circuit->area->area_tag, lsp->level,
3076 rawlspid_print (lsp->lsp_header->lsp_id),
3077 ntohl (lsp->lsp_header->seq_num),
3078 ntohs (lsp->lsp_header->checksum),
3079 ntohs (lsp->lsp_header->rem_lifetime),
3080 circuit->interface->name);
3081 if (isis->debugs & DEBUG_PACKET_DUMP)
3082 zlog_dump_data (STREAM_DATA (circuit->snd_stream),
3083 stream_get_endp (circuit->snd_stream));
3084 }
3085
3086 retval = circuit->tx (circuit, lsp->level);
3087 if (retval != ISIS_OK)
3088 {
3089 zlog_err ("ISIS-Upd (%s): Send L%d LSP on %s failed",
3090 circuit->area->area_tag, lsp->level,
3091 circuit->interface->name);
3092 return retval;
3093 }
f390d2c7 3094
3f045a08
JB
3095 /*
3096 * If the sending succeeded, we can del the lsp from circuits
3097 * lsp_queue
3098 */
3099 list_delete_node (circuit->lsp_queue, node);
f390d2c7 3100
3f045a08
JB
3101 /* Set the last-cleared time if the queue is empty. */
3102 /* TODO: Is is possible that new lsps keep being added to the queue
3103 * that the queue is never empty? */
3104 if (list_isempty (circuit->lsp_queue))
3105 circuit->lsp_queue_last_cleared = time (NULL);
3106
3107 /*
3108 * On broadcast circuits also the SRMflag can be cleared
3109 */
3110 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
3111 ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
eb5d44eb 3112
3113 return retval;
f390d2c7 3114}
eb5d44eb 3115
3116int
f390d2c7 3117ack_lsp (struct isis_link_state_hdr *hdr, struct isis_circuit *circuit,
3118 int level)
eb5d44eb 3119{
3120 unsigned long lenp;
3121 int retval;
3122 u_int16_t length;
3123 struct isis_fixed_hdr fixed_hdr;
3124
3125 if (!circuit->snd_stream)
f390d2c7 3126 circuit->snd_stream = stream_new (ISO_MTU (circuit));
eb5d44eb 3127 else
3128 stream_reset (circuit->snd_stream);
3129
3f045a08
JB
3130 // fill_llc_hdr (stream);
3131 if (level == IS_LEVEL_1)
f390d2c7 3132 fill_fixed_hdr_andstream (&fixed_hdr, L1_PARTIAL_SEQ_NUM,
3133 circuit->snd_stream);
eb5d44eb 3134 else
f390d2c7 3135 fill_fixed_hdr_andstream (&fixed_hdr, L2_PARTIAL_SEQ_NUM,
3136 circuit->snd_stream);
eb5d44eb 3137
3138
9985f83c 3139 lenp = stream_get_endp (circuit->snd_stream);
f390d2c7 3140 stream_putw (circuit->snd_stream, 0); /* PDU length */
3141 stream_put (circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
eb5d44eb 3142 stream_putc (circuit->snd_stream, circuit->idx);
f390d2c7 3143 stream_putc (circuit->snd_stream, 9); /* code */
3144 stream_putc (circuit->snd_stream, 16); /* len */
eb5d44eb 3145
f390d2c7 3146 stream_putw (circuit->snd_stream, ntohs (hdr->rem_lifetime));
3147 stream_put (circuit->snd_stream, hdr->lsp_id, ISIS_SYS_ID_LEN + 2);
3148 stream_putl (circuit->snd_stream, ntohl (hdr->seq_num));
3149 stream_putw (circuit->snd_stream, ntohs (hdr->checksum));
eb5d44eb 3150
9985f83c 3151 length = (u_int16_t) stream_get_endp (circuit->snd_stream);
eb5d44eb 3152 /* Update PDU length */
3153 stream_putw_at (circuit->snd_stream, lenp, length);
3154
3155 retval = circuit->tx (circuit, level);
3f045a08
JB
3156 if (retval != ISIS_OK)
3157 zlog_err ("ISIS-Upd (%s): Send L%d LSP PSNP on %s failed",
3158 circuit->area->area_tag, level,
3159 circuit->interface->name);
eb5d44eb 3160
3161 return retval;
3162}