]> git.proxmox.com Git - mirror_frr.git/blame - isisd/isis_pdu.c
isisd: send/receive *SNPs with new parser
[mirror_frr.git] / isisd / isis_pdu.c
CommitLineData
eb5d44eb 1/*
d62a17ae 2 * IS-IS Rout(e)ing protocol - isis_pdu.c
eb5d44eb 3 * PDU processing
4 *
5 * Copyright (C) 2001,2002 Sampo Saaristo
d62a17ae 6 * Tampere University of Technology
eb5d44eb 7 * Institute of Communications Engineering
8 *
d62a17ae 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)
eb5d44eb 12 * any later version.
13 *
d62a17ae 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
eb5d44eb 17 * more details.
896014f4
DL
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; see the file COPYING; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
eb5d44eb 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"
eb5d44eb 39#include "isisd/isis_constants.h"
40#include "isisd/isis_common.h"
3f045a08 41#include "isisd/isis_flags.h"
eb5d44eb 42#include "isisd/isis_adjacency.h"
43#include "isisd/isis_circuit.h"
44#include "isisd/isis_network.h"
45#include "isisd/isis_misc.h"
46#include "isisd/isis_dr.h"
eb5d44eb 47#include "isisd/isis_tlv.h"
48#include "isisd/isisd.h"
49#include "isisd/isis_dynhn.h"
50#include "isisd/isis_lsp.h"
51#include "isisd/isis_pdu.h"
52#include "isisd/iso_checksum.h"
53#include "isisd/isis_csm.h"
54#include "isisd/isis_events.h"
f8c06e2c 55#include "isisd/isis_te.h"
99894f9a 56#include "isisd/isis_mt.h"
0c1bd758 57#include "isisd/isis_tlvs2.h"
eb5d44eb 58
eb5d44eb 59#define ISIS_MINIMUM_FIXED_HDR_LEN 15
f390d2c7 60#define ISIS_MIN_PDU_LEN 13 /* partial seqnum pdu with id_len=2 */
eb5d44eb 61
62#ifndef PNBBY
63#define PNBBY 8
64#endif /* PNBBY */
65
eb5d44eb 66/*
67 * HELPER FUNCS
68 */
69
eb5d44eb 70/*
d62a17ae 71 * Checks whether we should accept a PDU of given level
eb5d44eb 72 */
d62a17ae 73static int accept_level(int level, int circuit_t)
eb5d44eb 74{
d62a17ae 75 int retval = ((circuit_t & level) == level); /* simple approach */
eb5d44eb 76
d62a17ae 77 return retval;
eb5d44eb 78}
79
3f045a08
JB
80/*
81 * Verify authentication information
82 * Support cleartext and HMAC MD5 authentication
83 */
d62a17ae 84static int authentication_check(struct isis_passwd *remote,
85 struct isis_passwd *local,
86 struct stream *stream, uint32_t auth_tlv_offset)
eb5d44eb 87{
d62a17ae 88 unsigned char digest[ISIS_AUTH_MD5_SIZE];
89
90 /* Auth fail () - passwd type mismatch */
91 if (local->type != remote->type)
92 return ISIS_ERROR;
93
94 switch (local->type) {
95 /* No authentication required */
96 case ISIS_PASSWD_TYPE_UNUSED:
97 break;
98
99 /* Cleartext (ISO 10589) */
100 case ISIS_PASSWD_TYPE_CLEARTXT:
101 /* Auth fail () - passwd len mismatch */
102 if (remote->len != local->len)
103 return ISIS_ERROR;
104 return memcmp(local->passwd, remote->passwd, local->len);
105
106 /* HMAC MD5 (RFC 3567) */
107 case ISIS_PASSWD_TYPE_HMAC_MD5:
108 /* Auth fail () - passwd len mismatch */
109 if (remote->len != ISIS_AUTH_MD5_SIZE)
110 return ISIS_ERROR;
111 /* Set the authentication value to 0 before the check */
112 memset(STREAM_DATA(stream) + auth_tlv_offset + 3, 0,
113 ISIS_AUTH_MD5_SIZE);
114 /* Compute the digest */
115 hmac_md5(STREAM_DATA(stream), stream_get_endp(stream),
116 (unsigned char *)&(local->passwd), local->len,
117 (unsigned char *)&digest);
118 /* Copy back the authentication value after the check */
119 memcpy(STREAM_DATA(stream) + auth_tlv_offset + 3,
120 remote->passwd, ISIS_AUTH_MD5_SIZE);
121 return memcmp(digest, remote->passwd, ISIS_AUTH_MD5_SIZE);
122
123 default:
124 zlog_err("Unsupported authentication type");
125 return ISIS_ERROR;
126 }
127
128 /* Authentication pass when no authentication is configured */
129 return ISIS_OK;
3f045a08
JB
130}
131
d62a17ae 132static int lsp_authentication_check(struct stream *stream,
133 struct isis_area *area, int level,
134 struct isis_passwd *passwd)
3f045a08 135{
d62a17ae 136 struct isis_link_state_hdr *hdr;
137 uint32_t expected = 0, found = 0, auth_tlv_offset = 0;
138 uint16_t checksum, rem_lifetime, pdu_len;
139 struct tlvs tlvs;
140 int retval = ISIS_OK;
141
142 hdr = (struct isis_link_state_hdr *)(STREAM_PNT(stream));
143 pdu_len = ntohs(hdr->pdu_len);
144 expected |= TLVFLAG_AUTH_INFO;
145 auth_tlv_offset = stream_get_getp(stream) + ISIS_LSP_HDR_LEN;
146 retval = parse_tlvs(area->area_tag,
147 STREAM_PNT(stream) + ISIS_LSP_HDR_LEN,
148 pdu_len - ISIS_FIXED_HDR_LEN - ISIS_LSP_HDR_LEN,
149 &expected, &found, &tlvs, &auth_tlv_offset);
150
151 if (retval != ISIS_OK) {
152 zlog_err(
153 "ISIS-Upd (%s): Parse failed L%d LSP %s, seq 0x%08x, "
154 "cksum 0x%04x, lifetime %us, len %u",
155 area->area_tag, level, rawlspid_print(hdr->lsp_id),
156 ntohl(hdr->seq_num), ntohs(hdr->checksum),
157 ntohs(hdr->rem_lifetime), pdu_len);
158 if ((isis->debugs & DEBUG_UPDATE_PACKETS)
159 && (isis->debugs & DEBUG_PACKET_DUMP))
160 zlog_dump_data(STREAM_DATA(stream),
161 stream_get_endp(stream));
162 return retval;
163 }
3f045a08 164
d62a17ae 165 if (!(found & TLVFLAG_AUTH_INFO)) {
166 zlog_err("No authentication tlv in LSP");
167 return ISIS_ERROR;
168 }
3f045a08 169
d62a17ae 170 if (tlvs.auth_info.type != ISIS_PASSWD_TYPE_CLEARTXT
171 && tlvs.auth_info.type != ISIS_PASSWD_TYPE_HMAC_MD5) {
172 zlog_err("Unknown authentication type in LSP");
173 return ISIS_ERROR;
174 }
3f045a08 175
d62a17ae 176 /*
177 * RFC 5304 set checksum and remaining lifetime to zero before
178 * verification and reset to old values after verification.
179 */
180 checksum = hdr->checksum;
181 rem_lifetime = hdr->rem_lifetime;
182 hdr->checksum = 0;
183 hdr->rem_lifetime = 0;
184 retval = authentication_check(&tlvs.auth_info, passwd, stream,
185 auth_tlv_offset);
186 hdr->checksum = checksum;
187 hdr->rem_lifetime = rem_lifetime;
188
189 return retval;
eb5d44eb 190}
191
eb5d44eb 192/*
d62a17ae 193 * RECEIVE SIDE
eb5d44eb 194 */
195
0c1bd758
CF
196struct iih_info {
197 struct isis_circuit *circuit;
198 u_char *ssnpa;
199 int level;
eb5d44eb 200
0c1bd758
CF
201 uint8_t circ_type;
202 uint8_t sys_id[ISIS_SYS_ID_LEN];
203 uint16_t holdtime;
204 uint16_t pdu_len;
d62a17ae 205
0c1bd758 206 uint8_t circuit_id;
eb5d44eb 207
0c1bd758
CF
208 uint8_t priority;
209 uint8_t dis[ISIS_SYS_ID_LEN + 1];
d8fba7d9 210
0c1bd758
CF
211 bool v4_usable;
212 bool v6_usable;
eb5d44eb 213
0c1bd758
CF
214 struct isis_tlvs *tlvs;
215};
eb5d44eb 216
0c1bd758
CF
217static int process_p2p_hello(struct iih_info *iih)
218{
d62a17ae 219 /*
220 * My interpertation of the ISO, if no adj exists we will create one for
221 * the circuit
222 */
0c1bd758 223 struct isis_adjacency *adj = iih->circuit->u.p2p.neighbor;
d62a17ae 224 /* If an adjacency exists, check it is with the source of the hello
225 * packets */
226 if (adj) {
0c1bd758 227 if (memcmp(iih->sys_id, adj->sysid, ISIS_SYS_ID_LEN)) {
d62a17ae 228 zlog_debug(
229 "hello source and adjacency do not match, set adj down\n");
230 isis_adj_state_change(adj, ISIS_ADJ_DOWN,
231 "adj do not exist");
0c1bd758 232 return ISIS_OK;
f390d2c7 233 }
d62a17ae 234 }
0c1bd758 235 if (!adj || adj->level != iih->circ_type) {
d62a17ae 236 if (!adj) {
0c1bd758
CF
237 adj = isis_new_adj(iih->sys_id, NULL, iih->circ_type,
238 iih->circuit);
d62a17ae 239 } else {
0c1bd758 240 adj->level = iih->circ_type;
f390d2c7 241 }
0c1bd758 242 iih->circuit->u.p2p.neighbor = adj;
d62a17ae 243 /* Build lsp with the new neighbor entry when a new
244 * adjacency is formed. Set adjacency circuit type to
245 * IIH PDU header circuit type before lsp is regenerated
246 * when an adjacency is up. This will result in the new
247 * adjacency entry getting added to the lsp tlv neighbor list.
248 */
0c1bd758 249 adj->circuit_t = iih->circ_type;
d62a17ae 250 isis_adj_state_change(adj, ISIS_ADJ_INITIALIZING, NULL);
251 adj->sys_type = ISIS_SYSTYPE_UNKNOWN;
f390d2c7 252 }
eb5d44eb 253
d62a17ae 254 /* 8.2.6 Monitoring point-to-point adjacencies */
0c1bd758 255 adj->hold_time = iih->holdtime;
d62a17ae 256 adj->last_upd = time(NULL);
257
0c1bd758
CF
258 bool changed;
259 isis_tlvs_to_adj(iih->tlvs, adj, &changed);
260 changed |= tlvs_to_adj_mt_set(iih->tlvs, iih->v4_usable, iih->v6_usable,
261 adj);
d62a17ae 262
263 /* Update MPLS TE Remote IP address parameter if possible */
0c1bd758
CF
264 if (IS_MPLS_TE(isisMplsTE) && iih->circuit->mtc
265 && IS_CIRCUIT_TE(iih->circuit->mtc) && adj->ipv4_address_count)
266 set_circuitparams_rmt_ipaddr(iih->circuit->mtc,
267 adj->ipv4_addresses[0]);
d62a17ae 268
269 /* lets take care of the expiry */
270 THREAD_TIMER_OFF(adj->t_expire);
271 thread_add_timer(master, isis_adj_expire, adj, (long)adj->hold_time,
272 &adj->t_expire);
273
274 /* 8.2.5.2 a) a match was detected */
0c1bd758
CF
275 if (isis_tlvs_area_addresses_match(iih->tlvs,
276 iih->circuit->area->area_addrs)) {
d62a17ae 277 /* 8.2.5.2 a) 2) If the system is L1 - table 5 */
0c1bd758
CF
278 if (iih->circuit->area->is_type == IS_LEVEL_1) {
279 switch (iih->circ_type) {
d62a17ae 280 case IS_LEVEL_1:
281 case IS_LEVEL_1_AND_2:
282 if (adj->adj_state != ISIS_ADJ_UP) {
283 /* (4) adj state up */
284 isis_adj_state_change(adj, ISIS_ADJ_UP,
285 NULL);
286 /* (5) adj usage level 1 */
287 adj->adj_usage = ISIS_ADJ_LEVEL1;
288 } else if (adj->adj_usage == ISIS_ADJ_LEVEL1) {
289 ; /* accept */
290 }
291 break;
292 case IS_LEVEL_2:
293 if (adj->adj_state != ISIS_ADJ_UP) {
294 /* (7) reject - wrong system type event
295 */
296 zlog_warn("wrongSystemType");
0c1bd758 297 return ISIS_WARNING;
d62a17ae 298 } else if (adj->adj_usage == ISIS_ADJ_LEVEL1) {
299 /* (6) down - wrong system */
300 isis_adj_state_change(adj,
301 ISIS_ADJ_DOWN,
302 "Wrong System");
303 }
304 break;
305 }
f390d2c7 306 }
d62a17ae 307
308 /* 8.2.5.2 a) 3) If the system is L1L2 - table 6 */
0c1bd758
CF
309 if (iih->circuit->area->is_type == IS_LEVEL_1_AND_2) {
310 switch (iih->circ_type) {
d62a17ae 311 case IS_LEVEL_1:
312 if (adj->adj_state != ISIS_ADJ_UP) {
313 /* (6) adj state up */
314 isis_adj_state_change(adj, ISIS_ADJ_UP,
315 NULL);
316 /* (7) adj usage level 1 */
317 adj->adj_usage = ISIS_ADJ_LEVEL1;
318 } else if (adj->adj_usage == ISIS_ADJ_LEVEL1) {
319 ; /* accept */
320 } else if ((adj->adj_usage
321 == ISIS_ADJ_LEVEL1AND2)
322 || (adj->adj_usage
323 == ISIS_ADJ_LEVEL2)) {
324 /* (8) down - wrong system */
325 isis_adj_state_change(adj,
326 ISIS_ADJ_DOWN,
327 "Wrong System");
328 }
329 break;
330 case IS_LEVEL_2:
331 if (adj->adj_state != ISIS_ADJ_UP) {
332 /* (6) adj state up */
333 isis_adj_state_change(adj, ISIS_ADJ_UP,
334 NULL);
335 /* (9) adj usage level 2 */
336 adj->adj_usage = ISIS_ADJ_LEVEL2;
337 } else if ((adj->adj_usage == ISIS_ADJ_LEVEL1)
338 || (adj->adj_usage
339 == ISIS_ADJ_LEVEL1AND2)) {
340 /* (8) down - wrong system */
341 isis_adj_state_change(adj,
342 ISIS_ADJ_DOWN,
343 "Wrong System");
344 } else if (adj->adj_usage == ISIS_ADJ_LEVEL2) {
345 ; /* Accept */
346 }
347 break;
348 case IS_LEVEL_1_AND_2:
349 if (adj->adj_state != ISIS_ADJ_UP) {
350 /* (6) adj state up */
351 isis_adj_state_change(adj, ISIS_ADJ_UP,
352 NULL);
353 /* (10) adj usage level 1 */
354 adj->adj_usage = ISIS_ADJ_LEVEL1AND2;
355 } else if ((adj->adj_usage == ISIS_ADJ_LEVEL1)
356 || (adj->adj_usage
357 == ISIS_ADJ_LEVEL2)) {
358 /* (8) down - wrong system */
359 isis_adj_state_change(adj,
360 ISIS_ADJ_DOWN,
361 "Wrong System");
362 } else if (adj->adj_usage
363 == ISIS_ADJ_LEVEL1AND2) {
364 ; /* Accept */
365 }
366 break;
367 }
f390d2c7 368 }
d62a17ae 369
370 /* 8.2.5.2 a) 4) If the system is L2 - table 7 */
0c1bd758
CF
371 if (iih->circuit->area->is_type == IS_LEVEL_2) {
372 switch (iih->circ_type) {
d62a17ae 373 case IS_LEVEL_1:
374 if (adj->adj_state != ISIS_ADJ_UP) {
375 /* (5) reject - wrong system type event
376 */
377 zlog_warn("wrongSystemType");
0c1bd758 378 return ISIS_WARNING;
d62a17ae 379 } else if ((adj->adj_usage
380 == ISIS_ADJ_LEVEL1AND2)
381 || (adj->adj_usage
382 == ISIS_ADJ_LEVEL2)) {
383 /* (6) down - wrong system */
384 isis_adj_state_change(adj,
385 ISIS_ADJ_DOWN,
386 "Wrong System");
387 }
388 break;
389 case IS_LEVEL_1_AND_2:
390 case IS_LEVEL_2:
391 if (adj->adj_state != ISIS_ADJ_UP) {
392 /* (7) adj state up */
393 isis_adj_state_change(adj, ISIS_ADJ_UP,
394 NULL);
395 /* (8) adj usage level 2 */
396 adj->adj_usage = ISIS_ADJ_LEVEL2;
397 } else if (adj->adj_usage
398 == ISIS_ADJ_LEVEL1AND2) {
399 /* (6) down - wrong system */
400 isis_adj_state_change(adj,
401 ISIS_ADJ_DOWN,
402 "Wrong System");
403 } else if (adj->adj_usage == ISIS_ADJ_LEVEL2) {
404 ; /* Accept */
405 }
406 break;
407 }
f390d2c7 408 }
d62a17ae 409 }
410 /* 8.2.5.2 b) if no match was detected */
0c1bd758
CF
411 else if (listcount(iih->circuit->area->area_addrs) > 0) {
412 if (iih->circuit->area->is_type == IS_LEVEL_1) {
d62a17ae 413 /* 8.2.5.2 b) 1) is_type L1 and adj is not up */
414 if (adj->adj_state != ISIS_ADJ_UP) {
415 isis_adj_state_change(adj, ISIS_ADJ_DOWN,
416 "Area Mismatch");
417 /* 8.2.5.2 b) 2)is_type L1 and adj is up */
418 } else {
419 isis_adj_state_change(adj, ISIS_ADJ_DOWN,
420 "Down - Area Mismatch");
421 }
f390d2c7 422 }
d62a17ae 423 /* 8.2.5.2 b 3 If the system is L2 or L1L2 - table 8 */
424 else {
0c1bd758 425 switch (iih->circ_type) {
d62a17ae 426 case IS_LEVEL_1:
427 if (adj->adj_state != ISIS_ADJ_UP) {
428 /* (6) reject - Area Mismatch event */
429 zlog_warn("AreaMismatch");
0c1bd758 430 return ISIS_WARNING;
d62a17ae 431 } else if (adj->adj_usage == ISIS_ADJ_LEVEL1) {
432 /* (7) down - area mismatch */
433 isis_adj_state_change(adj,
434 ISIS_ADJ_DOWN,
435 "Area Mismatch");
436
437 } else if ((adj->adj_usage
438 == ISIS_ADJ_LEVEL1AND2)
439 || (adj->adj_usage
440 == ISIS_ADJ_LEVEL2)) {
441 /* (7) down - wrong system */
442 isis_adj_state_change(adj,
443 ISIS_ADJ_DOWN,
444 "Wrong System");
445 }
446 break;
447 case IS_LEVEL_1_AND_2:
448 case IS_LEVEL_2:
449 if (adj->adj_state != ISIS_ADJ_UP) {
450 /* (8) adj state up */
451 isis_adj_state_change(adj, ISIS_ADJ_UP,
452 NULL);
453 /* (9) adj usage level 2 */
454 adj->adj_usage = ISIS_ADJ_LEVEL2;
455 } else if (adj->adj_usage == ISIS_ADJ_LEVEL1) {
456 /* (7) down - wrong system */
457 isis_adj_state_change(adj,
458 ISIS_ADJ_DOWN,
459 "Wrong System");
460 } else if (adj->adj_usage
461 == ISIS_ADJ_LEVEL1AND2) {
0c1bd758 462 if (iih->circ_type == IS_LEVEL_2) {
d62a17ae 463 /* (7) down - wrong system */
464 isis_adj_state_change(
465 adj, ISIS_ADJ_DOWN,
466 "Wrong System");
467 } else {
468 /* (7) down - area mismatch */
469 isis_adj_state_change(
470 adj, ISIS_ADJ_DOWN,
471 "Area Mismatch");
472 }
473 } else if (adj->adj_usage == ISIS_ADJ_LEVEL2) {
474 ; /* Accept */
475 }
476 break;
477 }
f390d2c7 478 }
d62a17ae 479 } else {
480 /* down - area mismatch */
481 isis_adj_state_change(adj, ISIS_ADJ_DOWN, "Area Mismatch");
f390d2c7 482 }
d8fba7d9 483
0c1bd758 484 if (adj->adj_state == ISIS_ADJ_UP && changed) {
d62a17ae 485 lsp_regenerate_schedule(adj->circuit->area,
486 isis_adj_usage2levels(adj->adj_usage),
487 0);
488 }
eb5d44eb 489
d62a17ae 490 /* 8.2.5.2 c) if the action was up - comparing circuit IDs */
491 /* FIXME - Missing parts */
492
493 /* some of my own understanding of the ISO, why the heck does
494 * it not say what should I change the system_type to...
495 */
496 switch (adj->adj_usage) {
497 case ISIS_ADJ_LEVEL1:
498 adj->sys_type = ISIS_SYSTYPE_L1_IS;
499 break;
500 case ISIS_ADJ_LEVEL2:
501 adj->sys_type = ISIS_SYSTYPE_L2_IS;
502 break;
503 case ISIS_ADJ_LEVEL1AND2:
504 adj->sys_type = ISIS_SYSTYPE_L2_IS;
505 break;
506 case ISIS_ADJ_NONE:
507 adj->sys_type = ISIS_SYSTYPE_UNKNOWN;
508 break;
509 }
eb5d44eb 510
d62a17ae 511 if (isis->debugs & DEBUG_ADJ_PACKETS) {
512 zlog_debug(
513 "ISIS-Adj (%s): Rcvd P2P IIH from (%s), cir type %s,"
514 " cir id %02d, length %d",
0c1bd758
CF
515 iih->circuit->area->area_tag,
516 iih->circuit->interface->name,
517 circuit_t2string(iih->circuit->is_type),
518 iih->circuit->circuit_id, iih->pdu_len);
d62a17ae 519 }
eb5d44eb 520
0c1bd758 521 return ISIS_OK;
eb5d44eb 522}
523
0c1bd758 524static int process_lan_hello(struct iih_info *iih)
eb5d44eb 525{
d62a17ae 526 struct isis_adjacency *adj;
0c1bd758
CF
527 adj = isis_adj_lookup(iih->sys_id,
528 iih->circuit->u.bc.adjdb[iih->level - 1]);
529 if ((adj == NULL) || (memcmp(adj->snpa, iih->ssnpa, ETH_ALEN))
530 || (adj->level != iih->level)) {
531 if (!adj) {
532 /* Do as in 8.4.2.5 */
533 adj = isis_new_adj(iih->sys_id, iih->ssnpa, iih->level,
534 iih->circuit);
535 } else {
536 if (iih->ssnpa) {
537 memcpy(adj->snpa, iih->ssnpa, 6);
538 } else {
539 memset(adj->snpa, ' ', 6);
540 }
541 adj->level = iih->level;
542 }
543 isis_adj_state_change(adj, ISIS_ADJ_INITIALIZING, NULL);
d62a17ae 544
0c1bd758
CF
545 if (iih->level == IS_LEVEL_1)
546 adj->sys_type = ISIS_SYSTYPE_L1_IS;
547 else
548 adj->sys_type = ISIS_SYSTYPE_L2_IS;
549 list_delete_all_node(
550 iih->circuit->u.bc.lan_neighs[iih->level - 1]);
551 isis_adj_build_neigh_list(
552 iih->circuit->u.bc.adjdb[iih->level - 1],
553 iih->circuit->u.bc.lan_neighs[iih->level - 1]);
554 }
555
556 if (adj->dis_record[iih->level - 1].dis == ISIS_IS_DIS) {
557 u_char *dis = (iih->level == 1)
558 ? iih->circuit->u.bc.l1_desig_is
559 : iih->circuit->u.bc.l2_desig_is;
560
561 if (memcmp(dis, iih->dis, ISIS_SYS_ID_LEN + 1)) {
562 thread_add_event(master, isis_event_dis_status_change,
563 iih->circuit, 0, NULL);
564 memcpy(dis, iih->dis, ISIS_SYS_ID_LEN + 1);
565 }
d62a17ae 566 }
3f045a08 567
0c1bd758
CF
568 adj->circuit_t = iih->circ_type;
569 adj->hold_time = iih->holdtime;
570 adj->last_upd = time(NULL);
571 adj->prio[iih->level - 1] = iih->priority;
572 memcpy(adj->lanid, iih->dis, ISIS_SYS_ID_LEN + 1);
3f045a08 573
0c1bd758
CF
574 bool changed;
575 isis_tlvs_to_adj(iih->tlvs, adj, &changed);
576 changed |= tlvs_to_adj_mt_set(iih->tlvs, iih->v4_usable, iih->v6_usable,
577 adj);
578
579 /* lets take care of the expiry */
580 THREAD_TIMER_OFF(adj->t_expire);
581 thread_add_timer(master, isis_adj_expire, adj, (long)adj->hold_time,
582 &adj->t_expire);
583
584 /*
585 * If the snpa for this circuit is found from LAN Neighbours TLV
586 * we have two-way communication -> adjacency can be put to state "up"
587 */
588 bool own_snpa_found =
589 isis_tlvs_own_snpa_found(iih->tlvs, iih->circuit->u.bc.snpa);
590
591 if (adj->adj_state != ISIS_ADJ_UP) {
592 if (own_snpa_found) {
593 isis_adj_state_change(
594 adj, ISIS_ADJ_UP,
595 "own SNPA found in LAN Neighbours TLV");
596 }
597 } else {
598 if (!own_snpa_found) {
599 isis_adj_state_change(
600 adj, ISIS_ADJ_INITIALIZING,
601 "own SNPA not found in LAN Neighbours TLV");
602 }
d62a17ae 603 }
eb5d44eb 604
0c1bd758
CF
605 if (adj->adj_state == ISIS_ADJ_UP && changed)
606 lsp_regenerate_schedule(adj->circuit->area, iih->level, 0);
607
608 if (isis->debugs & DEBUG_ADJ_PACKETS) {
d62a17ae 609 zlog_debug(
0c1bd758
CF
610 "ISIS-Adj (%s): Rcvd L%d LAN IIH from %s on %s, cirType %s, cirID %u, length %zd",
611 iih->circuit->area->area_tag, iih->level,
612 snpa_print(iih->ssnpa), iih->circuit->interface->name,
613 circuit_t2string(iih->circuit->is_type),
614 iih->circuit->circuit_id,
615 stream_get_endp(iih->circuit->rcv_stream));
d62a17ae 616 }
0c1bd758
CF
617 return ISIS_OK;
618}
eb5d44eb 619
17c9dcd5
CF
620static int pdu_len_validate(uint16_t pdu_len, struct isis_circuit *circuit)
621{
622 if (pdu_len < stream_get_getp(circuit->rcv_stream)
623 || pdu_len > ISO_MTU(circuit)
624 || pdu_len > stream_get_endp(circuit->rcv_stream))
625 return 1;
626
627 if (pdu_len < stream_get_endp(circuit->rcv_stream))
628 stream_set_endp(circuit->rcv_stream, pdu_len);
629 return 0;
630}
631
0c1bd758
CF
632static int process_hello(uint8_t pdu_type, struct isis_circuit *circuit,
633 u_char *ssnpa)
634{
635 bool p2p_hello = (pdu_type == P2P_HELLO);
636 int level = p2p_hello ? 0
637 : (pdu_type == L1_LAN_HELLO) ? ISIS_LEVEL1
638 : ISIS_LEVEL2;
639 const char *pdu_name =
640 p2p_hello
641 ? "P2P IIH"
642 : (level == ISIS_LEVEL1) ? "L1 LAN IIH" : "L2 LAN IIH";
643
644 if (isis->debugs & DEBUG_ADJ_PACKETS) {
645 zlog_debug("ISIS-Adj (%s): Rcvd %s on %s, cirType %s, cirID %u",
646 circuit->area->area_tag, pdu_name,
647 circuit->interface->name,
648 circuit_t2string(circuit->is_type),
649 circuit->circuit_id);
650 if (isis->debugs & DEBUG_PACKET_DUMP)
651 zlog_dump_data(STREAM_DATA(circuit->rcv_stream),
652 stream_get_endp(circuit->rcv_stream));
653 }
654
655 if (p2p_hello) {
656 if (circuit->circ_type != CIRCUIT_T_P2P) {
657 zlog_warn("p2p hello on non p2p circuit");
658 return ISIS_WARNING;
659 }
660 } else {
661 if (circuit->circ_type != CIRCUIT_T_BROADCAST) {
662 zlog_warn("lan hello on non broadcast circuit");
663 return ISIS_WARNING;
664 }
665
666 if (circuit->ext_domain) {
d62a17ae 667 zlog_debug(
0c1bd758
CF
668 "level %d LAN Hello received over circuit with externalDomain = true",
669 level);
670 return ISIS_WARNING;
671 }
672
673 if (!accept_level(level, circuit->is_type)) {
674 if (isis->debugs & DEBUG_ADJ_PACKETS) {
675 zlog_debug(
676 "ISIS-Adj (%s): Interface level mismatch, %s",
677 circuit->area->area_tag,
678 circuit->interface->name);
679 }
680 return ISIS_WARNING;
d62a17ae 681 }
f390d2c7 682 }
eb5d44eb 683
0c1bd758
CF
684 struct iih_info iih = {
685 .circuit = circuit, .ssnpa = ssnpa, .level = level};
686
687 /* Generic IIH Header */
688 iih.circ_type = stream_getc(circuit->rcv_stream) & 0x03;
689 stream_get(iih.sys_id, circuit->rcv_stream, ISIS_SYS_ID_LEN);
690 iih.holdtime = stream_getw(circuit->rcv_stream);
691 iih.pdu_len = stream_getw(circuit->rcv_stream);
692
693 if (p2p_hello) {
694 iih.circuit_id = stream_getc(circuit->rcv_stream);
695 } else {
696 iih.priority = stream_getc(circuit->rcv_stream);
697 stream_get(iih.dis, circuit->rcv_stream, ISIS_SYS_ID_LEN + 1);
f390d2c7 698 }
0c1bd758 699
17c9dcd5 700 if (pdu_len_validate(iih.pdu_len, circuit)) {
d62a17ae 701 zlog_warn(
0c1bd758
CF
702 "ISIS-Adj (%s): Rcvd %s from (%s) with invalid pdu length %" PRIu16,
703 circuit->area->area_tag, pdu_name,
704 circuit->interface->name, iih.pdu_len);
d62a17ae 705 return ISIS_WARNING;
706 }
3f045a08 707
0c1bd758 708 if (!p2p_hello && !(level & iih.circ_type)) {
d62a17ae 709 zlog_err("Level %d LAN Hello with Circuit Type %d", level,
0c1bd758 710 iih.circ_type);
d62a17ae 711 return ISIS_ERROR;
712 }
3f045a08 713
0c1bd758
CF
714 const char *error_log;
715 int retval = ISIS_WARNING;
d62a17ae 716
0c1bd758
CF
717 if (isis_unpack_tlvs(STREAM_READABLE(circuit->rcv_stream),
718 circuit->rcv_stream, &iih.tlvs, &error_log)) {
719 zlog_warn("isis_unpack_tlvs() failed: %s", error_log);
d62a17ae 720 goto out;
721 }
eb5d44eb 722
0c1bd758
CF
723 if (!iih.tlvs->area_addresses.count) {
724 zlog_warn("No Area addresses TLV in %s", pdu_name);
d62a17ae 725 goto out;
726 }
f390d2c7 727
0c1bd758
CF
728 if (!iih.tlvs->protocols_supported.count) {
729 zlog_warn("No supported protocols TLV in %s", pdu_name);
d62a17ae 730 goto out;
731 }
b72f345d 732
0c1bd758
CF
733 if (!isis_tlvs_auth_is_valid(iih.tlvs, &circuit->passwd,
734 circuit->rcv_stream)) {
735 isis_event_auth_failure(circuit->area->area_tag,
736 "IIH authentication failure",
737 iih.sys_id);
738 goto out;
d62a17ae 739 }
eb5d44eb 740
0c1bd758
CF
741 if (!memcmp(iih.sys_id, isis->sysid, ISIS_SYS_ID_LEN)) {
742 zlog_warn(
743 "ISIS-Adj (%s): Received IIH with own sysid - discard",
744 circuit->area->area_tag);
745 goto out;
d62a17ae 746 }
19f78ceb 747
0c1bd758
CF
748 if (!p2p_hello
749 && (listcount(circuit->area->area_addrs) == 0
750 || (level == ISIS_LEVEL1
751 && !isis_tlvs_area_addresses_match(
752 iih.tlvs, circuit->area->area_addrs)))) {
d62a17ae 753 if (isis->debugs & DEBUG_ADJ_PACKETS) {
754 zlog_debug(
755 "ISIS-Adj (%s): Area mismatch, level %d IIH on %s",
756 circuit->area->area_tag, level,
757 circuit->interface->name);
758 }
d62a17ae 759 goto out;
f390d2c7 760 }
eb5d44eb 761
0c1bd758
CF
762 iih.v4_usable = (circuit->ip_addrs && listcount(circuit->ip_addrs)
763 && iih.tlvs->ipv4_address.count);
d62a17ae 764
0c1bd758
CF
765 iih.v6_usable = (circuit->ipv6_link && listcount(circuit->ipv6_link)
766 && iih.tlvs->ipv6_address.count);
28a8cfcb 767
0c1bd758 768 if (!iih.v4_usable && !iih.v6_usable)
d62a17ae 769 goto out;
d8fba7d9 770
0c1bd758 771 retval = p2p_hello ? process_p2p_hello(&iih) : process_lan_hello(&iih);
f390d2c7 772out:
0c1bd758 773 isis_free_tlvs(iih.tlvs);
eb5d44eb 774
d62a17ae 775 return retval;
eb5d44eb 776}
777
778/*
779 * Process Level 1/2 Link State
780 * ISO - 10589
781 * Section 7.3.15.1 - Action on receipt of a link state PDU
f390d2c7 782 */
d62a17ae 783static int process_lsp(int level, struct isis_circuit *circuit,
784 const u_char *ssnpa)
eb5d44eb 785{
d62a17ae 786 struct isis_link_state_hdr *hdr;
787 struct isis_adjacency *adj = NULL;
788 struct isis_lsp *lsp, *lsp0 = NULL;
789 int retval = ISIS_OK, comp = 0;
790 u_char lspid[ISIS_SYS_ID_LEN + 2];
791 struct isis_passwd *passwd;
792 uint16_t pdu_len;
793 int lsp_confusion;
794
795 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
796 zlog_debug(
797 "ISIS-Upd (%s): Rcvd L%d LSP on %s, cirType %s, cirID %u",
798 circuit->area->area_tag, level,
799 circuit->interface->name,
800 circuit_t2string(circuit->is_type),
801 circuit->circuit_id);
802 if (isis->debugs & DEBUG_PACKET_DUMP)
803 zlog_dump_data(STREAM_DATA(circuit->rcv_stream),
804 stream_get_endp(circuit->rcv_stream));
805 }
f390d2c7 806
d62a17ae 807 if ((stream_get_endp(circuit->rcv_stream)
808 - stream_get_getp(circuit->rcv_stream))
809 < ISIS_LSP_HDR_LEN) {
810 zlog_warn("Packet too short");
811 return ISIS_WARNING;
812 }
eb5d44eb 813
d62a17ae 814 /* Reference the header */
815 hdr = (struct isis_link_state_hdr *)STREAM_PNT(circuit->rcv_stream);
816 pdu_len = ntohs(hdr->pdu_len);
eb5d44eb 817
d62a17ae 818 /* lsp length check */
819 if (pdu_len < (ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN)
820 || pdu_len > ISO_MTU(circuit)
821 || pdu_len > stream_get_endp(circuit->rcv_stream)) {
822 zlog_debug("ISIS-Upd (%s): LSP %s invalid LSP length %d",
823 circuit->area->area_tag, rawlspid_print(hdr->lsp_id),
824 pdu_len);
eb5d44eb 825
d62a17ae 826 return ISIS_WARNING;
f390d2c7 827 }
d62a17ae 828
829 /*
830 * Set the stream endp to PDU length, ignoring additional padding
831 * introduced by transport chips.
832 */
833 if (pdu_len < stream_get_endp(circuit->rcv_stream))
834 stream_set_endp(circuit->rcv_stream, pdu_len);
835
836 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
837 zlog_debug(
838 "ISIS-Upd (%s): Rcvd L%d LSP %s, seq 0x%08x, cksum 0x%04x, "
839 "lifetime %us, len %u, on %s",
840 circuit->area->area_tag, level,
841 rawlspid_print(hdr->lsp_id), ntohl(hdr->seq_num),
842 ntohs(hdr->checksum), ntohs(hdr->rem_lifetime), pdu_len,
843 circuit->interface->name);
f390d2c7 844 }
d62a17ae 845
846 /* lsp is_type check */
847 if ((hdr->lsp_bits & IS_LEVEL_1_AND_2) != IS_LEVEL_1
848 && (hdr->lsp_bits & IS_LEVEL_1_AND_2) != IS_LEVEL_1_AND_2) {
849 zlog_debug("ISIS-Upd (%s): LSP %s invalid LSP is type %x",
850 circuit->area->area_tag, rawlspid_print(hdr->lsp_id),
851 hdr->lsp_bits);
852 /* continue as per RFC1122 Be liberal in what you accept, and
853 * conservative in what you send */
f390d2c7 854 }
d62a17ae 855
856 /* Checksum sanity check - FIXME: move to correct place */
857 /* 12 = sysid+pdu+remtime */
858 if (iso_csum_verify(STREAM_PNT(circuit->rcv_stream) + 4, pdu_len - 12,
859 hdr->checksum,
860 offsetof(struct isis_link_state_hdr, checksum)
861 - 4)) {
862 zlog_debug("ISIS-Upd (%s): LSP %s invalid LSP checksum 0x%04x",
863 circuit->area->area_tag, rawlspid_print(hdr->lsp_id),
864 ntohs(hdr->checksum));
865
866 return ISIS_WARNING;
f390d2c7 867 }
3f045a08 868
d62a17ae 869 /* 7.3.15.1 a) 1 - external domain circuit will discard lsps */
870 if (circuit->ext_domain) {
871 zlog_debug(
872 "ISIS-Upd (%s): LSP %s received at level %d over circuit with "
873 "externalDomain = true",
874 circuit->area->area_tag, rawlspid_print(hdr->lsp_id),
875 level);
eb5d44eb 876
d62a17ae 877 return ISIS_WARNING;
878 }
eb5d44eb 879
d62a17ae 880 /* 7.3.15.1 a) 2,3 - manualL2OnlyMode not implemented */
881 if (!accept_level(level, circuit->is_type)) {
882 zlog_debug(
883 "ISIS-Upd (%s): LSP %s received at level %d over circuit of"
884 " type %s",
885 circuit->area->area_tag, rawlspid_print(hdr->lsp_id),
886 level, circuit_t2string(circuit->is_type));
f390d2c7 887
d62a17ae 888 return ISIS_WARNING;
889 }
0250758d 890
d62a17ae 891 /* 7.3.15.1 a) 4 - need to make sure IDLength matches */
892
893 /* 7.3.15.1 a) 5 - maximum area match, can be ommited since we only use
894 * 3 */
895
896 /* 7.3.15.1 a) 7 - password check */
897 (level == IS_LEVEL_1) ? (passwd = &circuit->area->area_passwd)
898 : (passwd = &circuit->area->domain_passwd);
899 if (passwd->type) {
900 if (lsp_authentication_check(circuit->rcv_stream, circuit->area,
901 level, passwd)) {
902 isis_event_auth_failure(circuit->area->area_tag,
903 "LSP authentication failure",
904 hdr->lsp_id);
905 return ISIS_WARNING;
906 }
f390d2c7 907 }
d62a17ae 908 /* Find the LSP in our database and compare it to this Link State header
909 */
910 lsp = lsp_search(hdr->lsp_id, circuit->area->lspdb[level - 1]);
911 if (lsp)
17c9dcd5
CF
912 comp = lsp_compare(circuit->area->area_tag, lsp,
913 ntohl(hdr->seq_num), ntohs(hdr->checksum),
914 ntohs(hdr->rem_lifetime));
d62a17ae 915 if (lsp && (lsp->own_lsp))
916 goto dontcheckadj;
917
918 /* 7.3.15.1 a) 6 - Must check that we have an adjacency of the same
919 * level */
920 /* for broadcast circuits, snpa should be compared */
921
922 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
923 adj = isis_adj_lookup_snpa(ssnpa,
924 circuit->u.bc.adjdb[level - 1]);
925 if (!adj) {
926 zlog_debug(
927 "(%s): DS ======= LSP %s, seq 0x%08x, cksum 0x%04x, "
928 "lifetime %us on %s",
929 circuit->area->area_tag,
930 rawlspid_print(hdr->lsp_id),
931 ntohl(hdr->seq_num), ntohs(hdr->checksum),
932 ntohs(hdr->rem_lifetime),
933 circuit->interface->name);
934 return ISIS_WARNING; /* Silently discard */
935 }
936 }
937 /* for non broadcast, we just need to find same level adj */
938 else {
939 /* If no adj, or no sharing of level */
940 if (!circuit->u.p2p.neighbor) {
941 return ISIS_OK; /* Silently discard */
942 } else {
943 if (((level == IS_LEVEL_1)
944 && (circuit->u.p2p.neighbor->adj_usage
945 == ISIS_ADJ_LEVEL2))
946 || ((level == IS_LEVEL_2)
947 && (circuit->u.p2p.neighbor->adj_usage
948 == ISIS_ADJ_LEVEL1)))
949 return ISIS_WARNING; /* Silently discard */
f390d2c7 950 }
f390d2c7 951 }
f390d2c7 952
d62a17ae 953dontcheckadj:
954 /* 7.3.15.1 a) 7 - Passwords for level 1 - not implemented */
955
956 /* 7.3.15.1 a) 8 - Passwords for level 2 - not implemented */
957
958 /* 7.3.15.1 a) 9 - OriginatingLSPBufferSize - not implemented FIXME: do
959 * it */
960
961 /* 7.3.16.2 - If this is an LSP from another IS with identical seq_num
962 * but
963 * wrong checksum, initiate a purge. */
964 if (lsp && (lsp->lsp_header->seq_num == hdr->seq_num)
965 && (lsp->lsp_header->checksum != hdr->checksum)) {
966 zlog_warn(
967 "ISIS-Upd (%s): LSP %s seq 0x%08x with confused checksum received.",
968 circuit->area->area_tag, rawlspid_print(hdr->lsp_id),
969 ntohl(hdr->seq_num));
970 hdr->rem_lifetime = 0;
971 lsp_confusion = 1;
972 } else
973 lsp_confusion = 0;
974
975 /* 7.3.15.1 b) - If the remaining life time is 0, we perform 7.3.16.4 */
976 if (hdr->rem_lifetime == 0) {
977 if (!lsp) {
978 /* 7.3.16.4 a) 1) No LSP in db -> send an ack, but don't
979 * save */
980 /* only needed on explicit update, eg - p2p */
981 if (circuit->circ_type == CIRCUIT_T_P2P)
982 ack_lsp(hdr, circuit, level);
983 return retval; /* FIXME: do we need a purge? */
984 } else {
985 if (memcmp(hdr->lsp_id, isis->sysid, ISIS_SYS_ID_LEN)) {
986 /* LSP by some other system -> do 7.3.16.4 b) */
987 /* 7.3.16.4 b) 1) */
988 if (comp == LSP_NEWER) {
989 lsp_update(lsp, circuit->rcv_stream,
990 circuit->area, level);
991 /* ii */
992 lsp_set_all_srmflags(lsp);
993 /* v */
994 ISIS_FLAGS_CLEAR_ALL(
9d303b37
DL
995 lsp
996 ->SSNflags); /* FIXME:
997 OTHER
998 than c
999 */
d62a17ae 1000
1001 /* For the case of lsp confusion, flood
1002 * the purge back to its
1003 * originator so that it can react.
1004 * Otherwise, don't reflood
1005 * through incoming circuit as usual */
1006 if (!lsp_confusion) {
1007 /* iii */
1008 ISIS_CLEAR_FLAG(lsp->SRMflags,
1009 circuit);
1010 /* iv */
1011 if (circuit->circ_type
1012 != CIRCUIT_T_BROADCAST)
1013 ISIS_SET_FLAG(
1014 lsp->SSNflags,
1015 circuit);
1016 }
1017 } /* 7.3.16.4 b) 2) */
1018 else if (comp == LSP_EQUAL) {
1019 /* i */
1020 ISIS_CLEAR_FLAG(lsp->SRMflags, circuit);
1021 /* ii */
1022 if (circuit->circ_type
1023 != CIRCUIT_T_BROADCAST)
1024 ISIS_SET_FLAG(lsp->SSNflags,
1025 circuit);
1026 } /* 7.3.16.4 b) 3) */
1027 else {
1028 ISIS_SET_FLAG(lsp->SRMflags, circuit);
1029 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
1030 }
1031 } else if (lsp->lsp_header->rem_lifetime != 0) {
1032 /* our own LSP -> 7.3.16.4 c) */
1033 if (comp == LSP_NEWER) {
1034 lsp_inc_seqnum(lsp,
1035 ntohl(hdr->seq_num));
1036 lsp_set_all_srmflags(lsp);
1037 } else {
1038 ISIS_SET_FLAG(lsp->SRMflags, circuit);
1039 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
1040 }
1041 if (isis->debugs & DEBUG_UPDATE_PACKETS)
1042 zlog_debug(
1043 "ISIS-Upd (%s): (1) re-originating LSP %s new "
1044 "seq 0x%08x",
1045 circuit->area->area_tag,
1046 rawlspid_print(hdr->lsp_id),
1047 ntohl(lsp->lsp_header
1048 ->seq_num));
1049 }
f390d2c7 1050 }
d62a17ae 1051 return retval;
f390d2c7 1052 }
d62a17ae 1053 /* 7.3.15.1 c) - If this is our own lsp and we don't have it initiate a
1054 * purge */
1055 if (memcmp(hdr->lsp_id, isis->sysid, ISIS_SYS_ID_LEN) == 0) {
1056 if (!lsp) {
1057 /* 7.3.16.4: initiate a purge */
1058 lsp_purge_non_exist(level, hdr, circuit->area);
1059 return ISIS_OK;
1060 }
1061 /* 7.3.15.1 d) - If this is our own lsp and we have it */
1062
1063 /* In 7.3.16.1, If an Intermediate system R somewhere in the
1064 * domain
1065 * has information that the current sequence number for source S
1066 * is
1067 * "greater" than that held by S, ... */
1068
1069 if (ntohl(hdr->seq_num) > ntohl(lsp->lsp_header->seq_num)) {
1070 /* 7.3.16.1 */
1071 lsp_inc_seqnum(lsp, ntohl(hdr->seq_num));
1072 if (isis->debugs & DEBUG_UPDATE_PACKETS)
1073 zlog_debug(
1074 "ISIS-Upd (%s): (2) re-originating LSP %s new seq "
1075 "0x%08x",
1076 circuit->area->area_tag,
1077 rawlspid_print(hdr->lsp_id),
1078 ntohl(lsp->lsp_header->seq_num));
1079 }
1080 /* If the received LSP is older or equal,
1081 * resend the LSP which will act as ACK */
1082 lsp_set_all_srmflags(lsp);
1083 } else {
1084 /* 7.3.15.1 e) - This lsp originated on another system */
1085
1086 /* 7.3.15.1 e) 1) LSP newer than the one in db or no LSP in db
1087 */
1088 if ((!lsp || comp == LSP_NEWER)) {
1089 /*
1090 * If this lsp is a frag, need to see if we have zero
1091 * lsp present
1092 */
1093 if (LSP_FRAGMENT(hdr->lsp_id) != 0) {
1094 memcpy(lspid, hdr->lsp_id, ISIS_SYS_ID_LEN + 1);
1095 LSP_FRAGMENT(lspid) = 0;
1096 lsp0 = lsp_search(
1097 lspid, circuit->area->lspdb[level - 1]);
1098 if (!lsp0) {
1099 zlog_debug(
1100 "Got lsp frag, while zero lsp not in database");
1101 return ISIS_OK;
1102 }
1103 }
1104 /* i */
1105 if (!lsp) {
1106 lsp = lsp_new_from_stream_ptr(
1107 circuit->rcv_stream, pdu_len, lsp0,
1108 circuit->area, level);
1109 lsp_insert(lsp,
1110 circuit->area->lspdb[level - 1]);
1111 } else /* exists, so we overwrite */
1112 {
1113 lsp_update(lsp, circuit->rcv_stream,
1114 circuit->area, level);
1115 }
1116 /* ii */
1117 lsp_set_all_srmflags(lsp);
1118 /* iii */
1119 ISIS_CLEAR_FLAG(lsp->SRMflags, circuit);
1120
1121 /* iv */
1122 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
1123 ISIS_SET_FLAG(lsp->SSNflags, circuit);
1124 /* FIXME: v) */
1125 }
1126 /* 7.3.15.1 e) 2) LSP equal to the one in db */
1127 else if (comp == LSP_EQUAL) {
1128 ISIS_CLEAR_FLAG(lsp->SRMflags, circuit);
1129 lsp_update(lsp, circuit->rcv_stream, circuit->area,
1130 level);
1131 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
1132 ISIS_SET_FLAG(lsp->SSNflags, circuit);
1133 }
1134 /* 7.3.15.1 e) 3) LSP older than the one in db */
1135 else {
1136 ISIS_SET_FLAG(lsp->SRMflags, circuit);
1137 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
1138 }
f390d2c7 1139 }
d62a17ae 1140 return retval;
eb5d44eb 1141}
1142
1143/*
1144 * Process Sequence Numbers
1145 * ISO - 10589
1146 * Section 7.3.15.2 - Action on receipt of a sequence numbers PDU
1147 */
1148
17c9dcd5 1149static int process_snp(uint8_t pdu_type, struct isis_circuit *circuit,
d62a17ae 1150 const u_char *ssnpa)
eb5d44eb 1151{
17c9dcd5
CF
1152 bool is_csnp = (pdu_type == L1_COMPLETE_SEQ_NUM
1153 || pdu_type == L2_COMPLETE_SEQ_NUM);
1154 char typechar = is_csnp ? 'C' : 'P';
1155 int level = (pdu_type == L1_COMPLETE_SEQ_NUM
1156 || pdu_type == L1_PARTIAL_SEQ_NUM)
1157 ? ISIS_LEVEL1
1158 : ISIS_LEVEL2;
1159
1160 uint16_t pdu_len = stream_getw(circuit->rcv_stream);
1161 uint8_t rem_sys_id[ISIS_SYS_ID_LEN];
1162 stream_get(rem_sys_id, circuit->rcv_stream, ISIS_SYS_ID_LEN);
1163 stream_forward_getp(circuit->rcv_stream, 1); /* Circuit ID - unused */
1164
1165 uint8_t start_lsp_id[ISIS_SYS_ID_LEN + 2] = {};
1166 uint8_t stop_lsp_id[ISIS_SYS_ID_LEN + 2] = {};
1167
1168 if (is_csnp) {
1169 stream_get(start_lsp_id, circuit->rcv_stream,
1170 ISIS_SYS_ID_LEN + 2);
1171 stream_get(stop_lsp_id, circuit->rcv_stream,
1172 ISIS_SYS_ID_LEN + 2);
1173 }
1174
1175 if (pdu_len_validate(pdu_len, circuit)) {
1176 zlog_warn("Received a CSNP with bogus length %d", pdu_len);
1177 return ISIS_WARNING;
f390d2c7 1178 }
eb5d44eb 1179
17c9dcd5
CF
1180 if (isis->debugs & DEBUG_SNP_PACKETS) {
1181 zlog_debug(
1182 "ISIS-Snp (%s): Rcvd L%d %cSNP on %s, cirType %s, cirID %u",
1183 circuit->area->area_tag, level, typechar,
1184 circuit->interface->name,
1185 circuit_t2string(circuit->is_type),
1186 circuit->circuit_id);
1187 if (isis->debugs & DEBUG_PACKET_DUMP)
1188 zlog_dump_data(STREAM_DATA(circuit->rcv_stream),
1189 stream_get_endp(circuit->rcv_stream));
1190 }
eb5d44eb 1191
d62a17ae 1192 /* 7.3.15.2 a) 1 - external domain circuit will discard snp pdu */
1193 if (circuit->ext_domain) {
f390d2c7 1194
d62a17ae 1195 zlog_debug(
1196 "ISIS-Snp (%s): Rcvd L%d %cSNP on %s, "
1197 "skipping: circuit externalDomain = true",
1198 circuit->area->area_tag, level, typechar,
1199 circuit->interface->name);
f390d2c7 1200
d62a17ae 1201 return ISIS_OK;
1202 }
eb5d44eb 1203
d62a17ae 1204 /* 7.3.15.2 a) 2,3 - manualL2OnlyMode not implemented */
1205 if (!accept_level(level, circuit->is_type)) {
eb5d44eb 1206
d62a17ae 1207 zlog_debug(
1208 "ISIS-Snp (%s): Rcvd L%d %cSNP on %s, "
1209 "skipping: circuit type %s does not match level %d",
1210 circuit->area->area_tag, level, typechar,
1211 circuit->interface->name,
1212 circuit_t2string(circuit->is_type), level);
eb5d44eb 1213
d62a17ae 1214 return ISIS_OK;
f390d2c7 1215 }
eb5d44eb 1216
d62a17ae 1217 /* 7.3.15.2 a) 4 - not applicable for CSNP only PSNPs on broadcast */
17c9dcd5
CF
1218 if (!is_csnp && (circuit->circ_type == CIRCUIT_T_BROADCAST)
1219 && !circuit->u.bc.is_dr[level - 1]) {
d62a17ae 1220 zlog_debug(
1221 "ISIS-Snp (%s): Rcvd L%d %cSNP from %s on %s, "
1222 "skipping: we are not the DIS",
1223 circuit->area->area_tag, level, typechar,
1224 snpa_print(ssnpa), circuit->interface->name);
1225
1226 return ISIS_OK;
1227 }
eb5d44eb 1228
d62a17ae 1229 /* 7.3.15.2 a) 5 - need to make sure IDLength matches - already checked
1230 */
1231
1232 /* 7.3.15.2 a) 6 - maximum area match, can be ommited since we only use
1233 * 3
1234 * - already checked */
1235
1236 /* 7.3.15.2 a) 7 - Must check that we have an adjacency of the same
1237 * level */
1238 /* for broadcast circuits, snpa should be compared */
1239 /* FIXME : Do we need to check SNPA? */
1240 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
17c9dcd5
CF
1241 if (!isis_adj_lookup(rem_sys_id,
1242 circuit->u.bc.adjdb[level - 1]))
d62a17ae 1243 return ISIS_OK; /* Silently discard */
1244 } else {
1245 if (!circuit->u.p2p.neighbor) {
1246 zlog_warn("no p2p neighbor on circuit %s",
1247 circuit->interface->name);
1248 return ISIS_OK; /* Silently discard */
1249 }
1250 }
eb5d44eb 1251
17c9dcd5
CF
1252 struct isis_tlvs *tlvs;
1253 int retval = ISIS_WARNING;
1254 const char *error_log;
eb5d44eb 1255
17c9dcd5
CF
1256 if (isis_unpack_tlvs(STREAM_READABLE(circuit->rcv_stream),
1257 circuit->rcv_stream, &tlvs, &error_log)) {
1258 zlog_warn("Something went wrong unpacking the SNP: %s",
1259 error_log);
1260 goto out;
f390d2c7 1261 }
eb5d44eb 1262
17c9dcd5
CF
1263 struct isis_passwd *passwd = (level == IS_LEVEL_1)
1264 ? &circuit->area->area_passwd
1265 : &circuit->area->domain_passwd;
1266 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_RECV)
1267 && !isis_tlvs_auth_is_valid(tlvs, passwd, circuit->rcv_stream)) {
1268 isis_event_auth_failure(circuit->area->area_tag,
1269 "SNP authentication failure",
1270 rem_sys_id);
1271 goto out;
d62a17ae 1272 }
eb5d44eb 1273
17c9dcd5
CF
1274 struct isis_lsp_entry *entry_head =
1275 (struct isis_lsp_entry *)tlvs->lsp_entries.head;
1276
d62a17ae 1277 /* debug isis snp-packets */
1278 if (isis->debugs & DEBUG_SNP_PACKETS) {
1279 zlog_debug("ISIS-Snp (%s): Rcvd L%d %cSNP from %s on %s",
1280 circuit->area->area_tag, level, typechar,
1281 snpa_print(ssnpa), circuit->interface->name);
17c9dcd5
CF
1282 for (struct isis_lsp_entry *entry = entry_head; entry;
1283 entry = entry->next) {
1284 zlog_debug(
1285 "ISIS-Snp (%s): %cSNP entry %s, seq 0x%08" PRIx32
1286 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16 "s",
1287 circuit->area->area_tag, typechar,
1288 rawlspid_print(entry->id), entry->seqno,
1289 entry->checksum, entry->rem_lifetime);
f390d2c7 1290 }
f390d2c7 1291 }
3f045a08 1292
d62a17ae 1293 /* 7.3.15.2 b) Actions on LSP_ENTRIES reported */
17c9dcd5
CF
1294 for (struct isis_lsp_entry *entry = entry_head; entry;
1295 entry = entry->next) {
1296 struct isis_lsp *lsp =
1297 lsp_search(entry->id, circuit->area->lspdb[level - 1]);
1298 bool own_lsp = !memcmp(entry->id, isis->sysid, ISIS_SYS_ID_LEN);
1299 if (lsp) {
1300 /* 7.3.15.2 b) 1) is this LSP newer */
1301 int cmp = lsp_compare(circuit->area->area_tag, lsp,
1302 entry->seqno, entry->checksum,
1303 entry->rem_lifetime);
1304 /* 7.3.15.2 b) 2) if it equals, clear SRM on p2p */
1305 if (cmp == LSP_EQUAL) {
1306 /* if (circuit->circ_type !=
1307 * CIRCUIT_T_BROADCAST) */
1308 ISIS_CLEAR_FLAG(lsp->SRMflags, circuit);
1309 }
1310 /* 7.3.15.2 b) 3) if it is older, clear SSN and set SRM
1311 */
1312 else if (cmp == LSP_OLDER) {
1313 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
1314 ISIS_SET_FLAG(lsp->SRMflags, circuit);
1315 }
1316 /* 7.3.15.2 b) 4) if it is newer, set SSN and clear SRM
1317 on p2p */
1318 else {
1319 if (own_lsp) {
1320 lsp_inc_seqnum(lsp, entry->seqno);
1321 ISIS_SET_FLAG(lsp->SRMflags, circuit);
1322 } else {
1323 ISIS_SET_FLAG(lsp->SSNflags, circuit);
d62a17ae 1324 /* if (circuit->circ_type !=
1325 * CIRCUIT_T_BROADCAST) */
1326 ISIS_CLEAR_FLAG(lsp->SRMflags, circuit);
1327 }
17c9dcd5
CF
1328 }
1329 } else {
1330 /* 7.3.15.2 b) 5) if it was not found, and all of those
1331 * are not 0,
1332 * insert it and set SSN on it */
1333 if (entry->rem_lifetime && entry->checksum
1334 && entry->seqno && memcmp(entry->id, isis->sysid,
1335 ISIS_SYS_ID_LEN)) {
1336 struct isis_lsp *lsp =
1337 lsp_new(circuit->area, entry->id,
1338 entry->rem_lifetime, 0, 0,
1339 entry->checksum, level);
1340 lsp_insert(lsp,
1341 circuit->area->lspdb[level - 1]);
1342 ISIS_FLAGS_CLEAR_ALL(lsp->SRMflags);
1343 ISIS_SET_FLAG(lsp->SSNflags, circuit);
d62a17ae 1344 }
1345 }
1346 }
eb5d44eb 1347
d62a17ae 1348 /* 7.3.15.2 c) on CSNP set SRM for all in range which were not reported
1349 */
17c9dcd5 1350 if (is_csnp) {
d62a17ae 1351 /*
1352 * Build a list from our own LSP db bounded with
1353 * start_lsp_id and stop_lsp_id
1354 */
17c9dcd5
CF
1355 struct list *lsp_list = list_new();
1356 lsp_build_list_nonzero_ht(start_lsp_id, stop_lsp_id, lsp_list,
d62a17ae 1357 circuit->area->lspdb[level - 1]);
1358
1359 /* Fixme: Find a better solution */
17c9dcd5
CF
1360 struct listnode *node, *nnode;
1361 struct isis_lsp *lsp;
1362 for (struct isis_lsp_entry *entry = entry_head; entry;
1363 entry = entry->next) {
1364 for (ALL_LIST_ELEMENTS(lsp_list, node, nnode, lsp)) {
1365 if (lsp_id_cmp(lsp->lsp_header->lsp_id,
1366 entry->id)
1367 == 0) {
1368 list_delete_node(lsp_list, node);
1369 break;
d62a17ae 1370 }
1371 }
1372 }
17c9dcd5 1373
d62a17ae 1374 /* on remaining LSPs we set SRM (neighbor knew not of) */
1375 for (ALL_LIST_ELEMENTS_RO(lsp_list, node, lsp))
1376 ISIS_SET_FLAG(lsp->SRMflags, circuit);
1377 /* lets free it */
1378 list_delete(lsp_list);
1379 }
1380
17c9dcd5
CF
1381 retval = ISIS_OK;
1382out:
1383 isis_free_tlvs(tlvs);
d62a17ae 1384 return retval;
eb5d44eb 1385}
1386
88f9d911
CF
1387static int pdu_size(uint8_t pdu_type, uint8_t *size)
1388{
1389 switch (pdu_type) {
1390 case L1_LAN_HELLO:
1391 case L2_LAN_HELLO:
1392 *size = ISIS_LANHELLO_HDRLEN;
1393 break;
1394 case P2P_HELLO:
1395 *size = ISIS_P2PHELLO_HDRLEN;
1396 break;
1397 case L1_LINK_STATE:
1398 case L2_LINK_STATE:
1399 *size = ISIS_LSP_HDR_LEN;
1400 break;
1401 case L1_COMPLETE_SEQ_NUM:
1402 case L2_COMPLETE_SEQ_NUM:
1403 *size = ISIS_CSNP_HDRLEN;
1404 break;
1405 case L1_PARTIAL_SEQ_NUM:
1406 case L2_PARTIAL_SEQ_NUM:
1407 *size = ISIS_PSNP_HDRLEN;
1408 break;
1409 default:
1410 return 1;
1411 }
1412 *size += ISIS_FIXED_HDR_LEN;
1413 return 0;
1414}
1415
eb5d44eb 1416/*
1417 * PDU Dispatcher
1418 */
1419
d62a17ae 1420static int isis_handle_pdu(struct isis_circuit *circuit, u_char *ssnpa)
eb5d44eb 1421{
d62a17ae 1422 int retval = ISIS_OK;
eb5d44eb 1423
88f9d911
CF
1424 /* Verify that at least the 8 bytes fixed header have been received */
1425 if (stream_get_endp(circuit->rcv_stream) < ISIS_FIXED_HDR_LEN) {
1426 zlog_err("PDU is too short to be IS-IS.");
1427 return ISIS_ERROR;
1428 }
eb5d44eb 1429
88f9d911
CF
1430 uint8_t idrp = stream_getc(circuit->rcv_stream);
1431 uint8_t length = stream_getc(circuit->rcv_stream);
1432 uint8_t version1 = stream_getc(circuit->rcv_stream);
1433 uint8_t id_len = stream_getc(circuit->rcv_stream);
1434 uint8_t pdu_type = stream_getc(circuit->rcv_stream)
1435 & 0x1f; /* bits 6-8 are reserved */
1436 uint8_t version2 = stream_getc(circuit->rcv_stream);
1437 stream_forward_getp(circuit->rcv_stream, 1); /* reserved */
1438 uint8_t max_area_addrs = stream_getc(circuit->rcv_stream);
1439
1440 if (idrp == ISO9542_ESIS) {
1441 zlog_err("No support for ES-IS packet IDRP=%" PRIx8, idrp);
d62a17ae 1442 return ISIS_ERROR;
1443 }
eb5d44eb 1444
88f9d911
CF
1445 if (idrp != ISO10589_ISIS) {
1446 zlog_err("Not an IS-IS packet IDRP=%" PRIx8, idrp);
d62a17ae 1447 return ISIS_ERROR;
1448 }
3f045a08 1449
88f9d911
CF
1450 if (version1 != 1) {
1451 zlog_warn("Unsupported ISIS version %" PRIu8, version1);
1452 return ISIS_WARNING;
1453 }
eb5d44eb 1454
88f9d911
CF
1455 if (id_len != 0 && id_len != ISIS_SYS_ID_LEN) {
1456 zlog_err(
1457 "IDFieldLengthMismatch: ID Length field in a received PDU %" PRIu8
1458 ", while the parameter for this IS is %u",
1459 id_len, ISIS_SYS_ID_LEN);
d62a17ae 1460 return ISIS_ERROR;
1461 }
eb5d44eb 1462
88f9d911
CF
1463 uint8_t expected_length;
1464 if (pdu_size(pdu_type, &expected_length)) {
1465 zlog_warn("Unsupported ISIS PDU %" PRIu8, pdu_type);
d62a17ae 1466 return ISIS_WARNING;
1467 }
88f9d911
CF
1468
1469 if (length != expected_length) {
1470 zlog_err("Exepected fixed header length = %" PRIu8
1471 " but got %" PRIu8,
1472 expected_length, length);
1473 return ISIS_ERROR;
1474 }
1475
1476 if (stream_get_endp(circuit->rcv_stream) < length) {
d62a17ae 1477 zlog_err(
88f9d911 1478 "PDU is too short to contain fixed header of given PDU type.");
d62a17ae 1479 return ISIS_ERROR;
1480 }
eb5d44eb 1481
88f9d911
CF
1482 if (version2 != 1) {
1483 zlog_warn("Unsupported ISIS PDU version %" PRIu8, version2);
d62a17ae 1484 return ISIS_WARNING;
1485 }
3f045a08 1486
d62a17ae 1487 if (circuit->is_passive) {
1488 zlog_warn("Received ISIS PDU on passive circuit %s",
1489 circuit->interface->name);
1490 return ISIS_WARNING;
1491 }
3f045a08 1492
d62a17ae 1493 /* either 3 or 0 */
88f9d911 1494 if (max_area_addrs != 0 && max_area_addrs != isis->max_area_addrs) {
d62a17ae 1495 zlog_err(
88f9d911
CF
1496 "maximumAreaAddressesMismatch: maximumAreaAdresses in a received PDU %" PRIu8
1497 " while the parameter for this IS is %u",
1498 max_area_addrs, isis->max_area_addrs);
d62a17ae 1499 return ISIS_ERROR;
1500 }
eb5d44eb 1501
88f9d911 1502 switch (pdu_type) {
d62a17ae 1503 case L1_LAN_HELLO:
d62a17ae 1504 case L2_LAN_HELLO:
d62a17ae 1505 case P2P_HELLO:
0c1bd758 1506 retval = process_hello(pdu_type, circuit, ssnpa);
d62a17ae 1507 break;
1508 case L1_LINK_STATE:
1509 retval = process_lsp(ISIS_LEVEL1, circuit, ssnpa);
1510 break;
1511 case L2_LINK_STATE:
1512 retval = process_lsp(ISIS_LEVEL2, circuit, ssnpa);
1513 break;
1514 case L1_COMPLETE_SEQ_NUM:
d62a17ae 1515 case L2_COMPLETE_SEQ_NUM:
d62a17ae 1516 case L1_PARTIAL_SEQ_NUM:
d62a17ae 1517 case L2_PARTIAL_SEQ_NUM:
17c9dcd5 1518 retval = process_snp(pdu_type, circuit, ssnpa);
d62a17ae 1519 break;
1520 default:
1521 return ISIS_ERROR;
1522 }
eb5d44eb 1523
d62a17ae 1524 return retval;
eb5d44eb 1525}
1526
d62a17ae 1527int isis_receive(struct thread *thread)
eb5d44eb 1528{
d62a17ae 1529 struct isis_circuit *circuit;
1530 u_char ssnpa[ETH_ALEN];
1531 int retval;
eb5d44eb 1532
d62a17ae 1533 /*
1534 * Get the circuit
1535 */
1536 circuit = THREAD_ARG(thread);
1537 assert(circuit);
eb5d44eb 1538
d62a17ae 1539 circuit->t_read = NULL;
eb5d44eb 1540
d62a17ae 1541 isis_circuit_stream(circuit, &circuit->rcv_stream);
eb5d44eb 1542
d62a17ae 1543 retval = circuit->rx(circuit, ssnpa);
eb5d44eb 1544
d62a17ae 1545 if (retval == ISIS_OK)
1546 retval = isis_handle_pdu(circuit, ssnpa);
eb5d44eb 1547
d62a17ae 1548 /*
1549 * prepare for next packet.
1550 */
1551 if (!circuit->is_passive)
1552 isis_circuit_prepare(circuit);
eb5d44eb 1553
d62a17ae 1554 return retval;
eb5d44eb 1555}
1556
eb5d44eb 1557/*
d62a17ae 1558 * SEND SIDE
eb5d44eb 1559 */
88f9d911 1560void fill_fixed_hdr(uint8_t pdu_type, struct stream *stream)
eb5d44eb 1561{
88f9d911
CF
1562 uint8_t length;
1563
1564 if (pdu_size(pdu_type, &length))
1565 assert(!"Unknown PDU Type");
1566
1567 stream_putc(stream, ISO10589_ISIS); /* IDRP */
1568 stream_putc(stream, length); /* Length of fixed header */
1569 stream_putc(stream, 1); /* Version/Protocol ID Extension 1 */
1570 stream_putc(stream, 0); /* ID Length, 0 => 6 */
1571 stream_putc(stream, pdu_type);
1572 stream_putc(stream, 1); /* Subversion */
1573 stream_putc(stream, 0); /* Reserved */
1574 stream_putc(stream, 0); /* Max Area Addresses 0 => 3 */
eb5d44eb 1575}
1576
0c1bd758
CF
1577static void put_hello_hdr(struct isis_circuit *circuit, int level,
1578 size_t *len_pointer)
eb5d44eb 1579{
88f9d911
CF
1580 uint8_t pdu_type;
1581
d62a17ae 1582 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
88f9d911 1583 pdu_type = (level == IS_LEVEL_1) ? L1_LAN_HELLO : L2_LAN_HELLO;
d62a17ae 1584 else
88f9d911
CF
1585 pdu_type = P2P_HELLO;
1586
0c1bd758 1587 isis_circuit_stream(circuit, &circuit->snd_stream);
88f9d911 1588 fill_fixed_hdr(pdu_type, circuit->snd_stream);
d62a17ae 1589
0c1bd758
CF
1590 stream_putc(circuit->snd_stream, circuit->is_type);
1591 stream_put(circuit->snd_stream, circuit->area->isis->sysid,
1592 ISIS_SYS_ID_LEN);
eb5d44eb 1593
0c1bd758
CF
1594 uint32_t holdtime = circuit->hello_multiplier[level - 1]
1595 * circuit->hello_interval[level - 1];
d62a17ae 1596
0c1bd758
CF
1597 if (holdtime > 0xffff)
1598 holdtime = 0xffff;
d62a17ae 1599
0c1bd758
CF
1600 stream_putw(circuit->snd_stream, holdtime);
1601 *len_pointer = stream_get_endp(circuit->snd_stream);
1602 stream_putw(circuit->snd_stream, 0); /* length is filled in later */
d62a17ae 1603
0c1bd758
CF
1604 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
1605 u_char *desig_is = (level == IS_LEVEL_1)
1606 ? circuit->u.bc.l1_desig_is
1607 : circuit->u.bc.l2_desig_is;
1608 stream_putc(circuit->snd_stream, circuit->priority[level - 1]);
1609 stream_put(circuit->snd_stream, desig_is, ISIS_SYS_ID_LEN + 1);
1610 } else {
1611 stream_putc(circuit->snd_stream, circuit->circuit_id);
d62a17ae 1612 }
0c1bd758 1613}
eb5d44eb 1614
0c1bd758
CF
1615int send_hello(struct isis_circuit *circuit, int level)
1616{
1617 size_t len_pointer;
1618 int retval;
1619
1620 if (circuit->is_passive)
1621 return ISIS_OK;
1622
1623 if (circuit->interface->mtu == 0) {
1624 zlog_warn("circuit has zero MTU");
d62a17ae 1625 return ISIS_WARNING;
0c1bd758
CF
1626 }
1627
1628 put_hello_hdr(circuit, level, &len_pointer);
1629
1630 struct isis_tlvs *tlvs = isis_alloc_tlvs();
1631
1632 isis_tlvs_add_auth(tlvs, &circuit->passwd);
1633
1634 if (!listcount(circuit->area->area_addrs))
d62a17ae 1635 return ISIS_WARNING;
0c1bd758 1636 isis_tlvs_add_area_addresses(tlvs, circuit->area->area_addrs);
d62a17ae 1637
0c1bd758
CF
1638 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
1639 isis_tlvs_add_lan_neighbors(
1640 tlvs, circuit->u.bc.lan_neighs[level - 1]);
1641
1642 isis_tlvs_set_protocols_supported(tlvs, &circuit->nlpids);
d62a17ae 1643
1644 /*
1645 * MT Supported TLV
1646 *
1647 * TLV gets included if no topology is enabled on the interface,
1648 * if one topology other than #0 is enabled, or if multiple topologies
1649 * are enabled.
1650 */
1651 struct isis_circuit_mt_setting **mt_settings;
1652 unsigned int mt_count;
1653
1654 mt_settings = circuit_mt_settings(circuit, &mt_count);
0c1bd758
CF
1655 if (mt_count == 0 && area_is_mt(circuit->area)) {
1656 tlvs->mt_router_info_empty = true;
1657 } else if ((mt_count == 1
1658 && mt_settings[0]->mtid != ISIS_MT_IPV4_UNICAST)
1659 || (mt_count > 1)) {
1660 for (unsigned int i = 0; i < mt_count; i++)
1661 isis_tlvs_add_mt_router_info(tlvs, mt_settings[i]->mtid,
1662 false, false);
d62a17ae 1663 }
eb5d44eb 1664
0c1bd758
CF
1665 if (circuit->ip_router && circuit->ip_addrs)
1666 isis_tlvs_add_ipv4_addresses(tlvs, circuit->ip_addrs);
d62a17ae 1667
0c1bd758
CF
1668 if (circuit->ipv6_router && circuit->ipv6_link)
1669 isis_tlvs_add_ipv6_addresses(tlvs, circuit->ipv6_link);
d62a17ae 1670
0c1bd758
CF
1671 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer,
1672 circuit->pad_hellos)) {
1673 isis_free_tlvs(tlvs);
1674 return ISIS_WARNING; /* XXX: Maybe Log TLV structure? */
f390d2c7 1675 }
d62a17ae 1676
1677 if (isis->debugs & DEBUG_ADJ_PACKETS) {
1678 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
1679 zlog_debug(
1680 "ISIS-Adj (%s): Sending L%d LAN IIH on %s, length %zd",
1681 circuit->area->area_tag, level,
0c1bd758
CF
1682 circuit->interface->name,
1683 stream_get_endp(circuit->snd_stream));
d62a17ae 1684 } else {
1685 zlog_debug(
1686 "ISIS-Adj (%s): Sending P2P IIH on %s, length %zd",
1687 circuit->area->area_tag,
0c1bd758
CF
1688 circuit->interface->name,
1689 stream_get_endp(circuit->snd_stream));
d62a17ae 1690 }
1691 if (isis->debugs & DEBUG_PACKET_DUMP)
1692 zlog_dump_data(STREAM_DATA(circuit->snd_stream),
1693 stream_get_endp(circuit->snd_stream));
f390d2c7 1694 }
eb5d44eb 1695
0c1bd758
CF
1696 isis_free_tlvs(tlvs);
1697
d62a17ae 1698 retval = circuit->tx(circuit, level);
1699 if (retval != ISIS_OK)
1700 zlog_err("ISIS-Adj (%s): Send L%d IIH on %s failed",
1701 circuit->area->area_tag, level,
1702 circuit->interface->name);
eb5d44eb 1703
d62a17ae 1704 return retval;
eb5d44eb 1705}
1706
d62a17ae 1707int send_lan_l1_hello(struct thread *thread)
eb5d44eb 1708{
d62a17ae 1709 struct isis_circuit *circuit;
1710 int retval;
1711
1712 circuit = THREAD_ARG(thread);
1713 assert(circuit);
1714 circuit->u.bc.t_send_lan_hello[0] = NULL;
1715
1716 if (!(circuit->area->is_type & IS_LEVEL_1)) {
1717 zlog_warn(
1718 "ISIS-Hello (%s): Trying to send L1 IIH in L2-only area",
1719 circuit->area->area_tag);
1720 return 1;
1721 }
ddfdbd32 1722
d62a17ae 1723 if (circuit->u.bc.run_dr_elect[0])
1724 isis_dr_elect(circuit, 1);
eb5d44eb 1725
d62a17ae 1726 retval = send_hello(circuit, 1);
eb5d44eb 1727
d62a17ae 1728 /* set next timer thread */
1729 thread_add_timer(master, send_lan_l1_hello, circuit,
1730 isis_jitter(circuit->hello_interval[0], IIH_JITTER),
1731 &circuit->u.bc.t_send_lan_hello[0]);
eb5d44eb 1732
d62a17ae 1733 return retval;
eb5d44eb 1734}
1735
d62a17ae 1736int send_lan_l2_hello(struct thread *thread)
eb5d44eb 1737{
d62a17ae 1738 struct isis_circuit *circuit;
1739 int retval;
eb5d44eb 1740
d62a17ae 1741 circuit = THREAD_ARG(thread);
1742 assert(circuit);
1743 circuit->u.bc.t_send_lan_hello[1] = NULL;
eb5d44eb 1744
d62a17ae 1745 if (!(circuit->area->is_type & IS_LEVEL_2)) {
1746 zlog_warn("ISIS-Hello (%s): Trying to send L2 IIH in L1 area",
1747 circuit->area->area_tag);
1748 return 1;
1749 }
ddfdbd32 1750
d62a17ae 1751 if (circuit->u.bc.run_dr_elect[1])
1752 isis_dr_elect(circuit, 2);
eb5d44eb 1753
d62a17ae 1754 retval = send_hello(circuit, 2);
eb5d44eb 1755
d62a17ae 1756 /* set next timer thread */
1757 thread_add_timer(master, send_lan_l2_hello, circuit,
1758 isis_jitter(circuit->hello_interval[1], IIH_JITTER),
1759 &circuit->u.bc.t_send_lan_hello[1]);
eb5d44eb 1760
d62a17ae 1761 return retval;
eb5d44eb 1762}
1763
d62a17ae 1764int send_p2p_hello(struct thread *thread)
eb5d44eb 1765{
d62a17ae 1766 struct isis_circuit *circuit;
eb5d44eb 1767
d62a17ae 1768 circuit = THREAD_ARG(thread);
1769 assert(circuit);
1770 circuit->u.p2p.t_send_p2p_hello = NULL;
eb5d44eb 1771
d62a17ae 1772 send_hello(circuit, 1);
eb5d44eb 1773
d62a17ae 1774 /* set next timer thread */
1775 thread_add_timer(master, send_p2p_hello, circuit,
1776 isis_jitter(circuit->hello_interval[1], IIH_JITTER),
1777 &circuit->u.p2p.t_send_p2p_hello);
eb5d44eb 1778
d62a17ae 1779 return ISIS_OK;
eb5d44eb 1780}
1781
3f045a08
JB
1782/*
1783 * Count the maximum number of lsps that can be accomodated by a given size.
1784 */
d62a17ae 1785static uint16_t get_max_lsp_count(uint16_t size)
3f045a08 1786{
d62a17ae 1787 uint16_t tlv_count;
1788 uint16_t lsp_count;
1789 uint16_t remaining_size;
3f045a08 1790
d62a17ae 1791 /* First count the full size TLVs */
1792 tlv_count = size / MAX_LSP_ENTRIES_TLV_SIZE;
1793 lsp_count = tlv_count * (MAX_LSP_ENTRIES_TLV_SIZE / LSP_ENTRIES_LEN);
3f045a08 1794
d62a17ae 1795 /* The last TLV, if any */
1796 remaining_size = size % MAX_LSP_ENTRIES_TLV_SIZE;
1797 if (remaining_size - 2 >= LSP_ENTRIES_LEN)
1798 lsp_count += (remaining_size - 2) / LSP_ENTRIES_LEN;
3f045a08 1799
d62a17ae 1800 return lsp_count;
3f045a08
JB
1801}
1802
17c9dcd5 1803int send_csnp(struct isis_circuit *circuit, int level)
3f045a08 1804{
17c9dcd5
CF
1805 if (circuit->area->lspdb[level - 1] == NULL
1806 || dict_count(circuit->area->lspdb[level - 1]) == 0)
1807 return ISIS_OK;
d62a17ae 1808
17c9dcd5
CF
1809 isis_circuit_stream(circuit, &circuit->snd_stream);
1810 fill_fixed_hdr((level == ISIS_LEVEL1) ? L1_COMPLETE_SEQ_NUM
1811 : L2_COMPLETE_SEQ_NUM,
1812 circuit->snd_stream);
3f045a08 1813
17c9dcd5
CF
1814 size_t len_pointer = stream_get_endp(circuit->snd_stream);
1815 stream_putw(circuit->snd_stream, 0);
1816 stream_put(circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
1817 /* with zero circuit id - ref 9.10, 9.11 */
1818 stream_putc(circuit->snd_stream, 0);
3f045a08 1819
17c9dcd5
CF
1820 size_t start_pointer = stream_get_endp(circuit->snd_stream);
1821 stream_put(circuit->snd_stream, 0, ISIS_SYS_ID_LEN + 2);
1822 size_t end_pointer = stream_get_endp(circuit->snd_stream);
1823 stream_put(circuit->snd_stream, 0, ISIS_SYS_ID_LEN + 2);
d62a17ae 1824
17c9dcd5
CF
1825 struct isis_passwd *passwd = (level == ISIS_LEVEL1)
1826 ? &circuit->area->area_passwd
1827 : &circuit->area->domain_passwd;
d62a17ae 1828
17c9dcd5 1829 struct isis_tlvs *tlvs = isis_alloc_tlvs();
3f045a08 1830
17c9dcd5
CF
1831 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
1832 isis_tlvs_add_auth(tlvs, passwd);
eb5d44eb 1833
17c9dcd5
CF
1834 size_t tlv_start = stream_get_endp(circuit->snd_stream);
1835 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer, false)) {
1836 isis_free_tlvs(tlvs);
1837 return ISIS_WARNING;
1838 }
1839 isis_free_tlvs(tlvs);
d62a17ae 1840
17c9dcd5
CF
1841 uint16_t num_lsps =
1842 get_max_lsp_count(STREAM_WRITEABLE(circuit->snd_stream));
d62a17ae 1843
17c9dcd5 1844 uint8_t start[ISIS_SYS_ID_LEN + 2];
d62a17ae 1845 memset(start, 0x00, ISIS_SYS_ID_LEN + 2);
17c9dcd5 1846 uint8_t stop[ISIS_SYS_ID_LEN + 2];
d62a17ae 1847 memset(stop, 0xff, ISIS_SYS_ID_LEN + 2);
1848
17c9dcd5 1849 bool loop = true;
d62a17ae 1850 while (loop) {
17c9dcd5
CF
1851 tlvs = isis_alloc_tlvs();
1852 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
1853 isis_tlvs_add_auth(tlvs, passwd);
1854
1855 struct isis_lsp *last_lsp;
1856 isis_tlvs_add_csnp_entries(tlvs, start, stop, num_lsps,
1857 circuit->area->lspdb[level - 1],
1858 &last_lsp);
d62a17ae 1859 /*
1860 * Update the stop lsp_id before encoding this CSNP.
1861 */
17c9dcd5 1862 if (tlvs->lsp_entries.count < num_lsps) {
d62a17ae 1863 memset(stop, 0xff, ISIS_SYS_ID_LEN + 2);
1864 } else {
17c9dcd5 1865 memcpy(stop, last_lsp->lsp_header->lsp_id,
d62a17ae 1866 ISIS_SYS_ID_LEN + 2);
1867 }
3f045a08 1868
17c9dcd5
CF
1869 memcpy(STREAM_DATA(circuit->snd_stream) + start_pointer, start,
1870 ISIS_SYS_ID_LEN + 2);
1871 memcpy(STREAM_DATA(circuit->snd_stream) + end_pointer, stop,
1872 ISIS_SYS_ID_LEN + 2);
1873 stream_set_endp(circuit->snd_stream, tlv_start);
1874 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer,
1875 false)) {
1876 isis_free_tlvs(tlvs);
1877 return ISIS_WARNING;
d62a17ae 1878 }
eb5d44eb 1879
d62a17ae 1880 if (isis->debugs & DEBUG_SNP_PACKETS) {
1881 zlog_debug(
1882 "ISIS-Snp (%s): Sending L%d CSNP on %s, length %zd",
1883 circuit->area->area_tag, level,
1884 circuit->interface->name,
1885 stream_get_endp(circuit->snd_stream));
17c9dcd5
CF
1886 log_multiline(LOG_DEBUG, " ", "%s",
1887 isis_format_tlvs(tlvs));
d62a17ae 1888 if (isis->debugs & DEBUG_PACKET_DUMP)
1889 zlog_dump_data(
1890 STREAM_DATA(circuit->snd_stream),
1891 stream_get_endp(circuit->snd_stream));
1892 }
eb5d44eb 1893
17c9dcd5 1894 int retval = circuit->tx(circuit, level);
d62a17ae 1895 if (retval != ISIS_OK) {
1896 zlog_err("ISIS-Snp (%s): Send L%d CSNP on %s failed",
1897 circuit->area->area_tag, level,
1898 circuit->interface->name);
17c9dcd5 1899 isis_free_tlvs(tlvs);
d62a17ae 1900 return retval;
1901 }
3f045a08 1902
d62a17ae 1903 /*
1904 * Start lsp_id of the next CSNP should be one plus the
1905 * stop lsp_id in this current CSNP.
1906 */
1907 memcpy(start, stop, ISIS_SYS_ID_LEN + 2);
1908 loop = 0;
17c9dcd5 1909 for (int i = ISIS_SYS_ID_LEN + 1; i >= 0; --i) {
d62a17ae 1910 if (start[i] < (u_char)0xff) {
1911 start[i] += 1;
1912 loop = 1;
1913 break;
1914 }
1915 }
1916 memset(stop, 0xff, ISIS_SYS_ID_LEN + 2);
17c9dcd5 1917 isis_free_tlvs(tlvs);
d62a17ae 1918 }
3f045a08 1919
17c9dcd5 1920 return ISIS_OK;
eb5d44eb 1921}
1922
d62a17ae 1923int send_l1_csnp(struct thread *thread)
eb5d44eb 1924{
d62a17ae 1925 struct isis_circuit *circuit;
1926 int retval = ISIS_OK;
eb5d44eb 1927
d62a17ae 1928 circuit = THREAD_ARG(thread);
1929 assert(circuit);
eb5d44eb 1930
d62a17ae 1931 circuit->t_send_csnp[0] = NULL;
eb5d44eb 1932
d62a17ae 1933 if (circuit->circ_type == CIRCUIT_T_BROADCAST
1934 && circuit->u.bc.is_dr[0]) {
1935 send_csnp(circuit, 1);
1936 }
1937 /* set next timer thread */
1938 thread_add_timer(master, send_l1_csnp, circuit,
1939 isis_jitter(circuit->csnp_interval[0], CSNP_JITTER),
1940 &circuit->t_send_csnp[0]);
eb5d44eb 1941
d62a17ae 1942 return retval;
eb5d44eb 1943}
1944
d62a17ae 1945int send_l2_csnp(struct thread *thread)
eb5d44eb 1946{
d62a17ae 1947 struct isis_circuit *circuit;
1948 int retval = ISIS_OK;
eb5d44eb 1949
d62a17ae 1950 circuit = THREAD_ARG(thread);
1951 assert(circuit);
eb5d44eb 1952
d62a17ae 1953 circuit->t_send_csnp[1] = NULL;
eb5d44eb 1954
d62a17ae 1955 if (circuit->circ_type == CIRCUIT_T_BROADCAST
1956 && circuit->u.bc.is_dr[1]) {
1957 send_csnp(circuit, 2);
1958 }
1959 /* set next timer thread */
1960 thread_add_timer(master, send_l2_csnp, circuit,
1961 isis_jitter(circuit->csnp_interval[1], CSNP_JITTER),
1962 &circuit->t_send_csnp[1]);
d70f99e1 1963
d62a17ae 1964 return retval;
eb5d44eb 1965}
1966
eb5d44eb 1967/*
1968 * 7.3.15.4 action on expiration of partial SNP interval
1969 * level 1
1970 */
d62a17ae 1971static int send_psnp(int level, struct isis_circuit *circuit)
eb5d44eb 1972{
d62a17ae 1973 if (circuit->circ_type == CIRCUIT_T_BROADCAST
1974 && circuit->u.bc.is_dr[level - 1])
1975 return ISIS_OK;
eb5d44eb 1976
d62a17ae 1977 if (circuit->area->lspdb[level - 1] == NULL
1978 || dict_count(circuit->area->lspdb[level - 1]) == 0)
1979 return ISIS_OK;
f390d2c7 1980
d62a17ae 1981 if (!circuit->snd_stream)
1982 return ISIS_ERROR;
e38e0df0 1983
17c9dcd5
CF
1984 isis_circuit_stream(circuit, &circuit->snd_stream);
1985 fill_fixed_hdr((level == ISIS_LEVEL1) ? L1_PARTIAL_SEQ_NUM
1986 : L2_PARTIAL_SEQ_NUM,
1987 circuit->snd_stream);
1988
1989 size_t len_pointer = stream_get_endp(circuit->snd_stream);
1990 stream_putw(circuit->snd_stream, 0); /* length is filled in later */
1991 stream_put(circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
1992 stream_putc(circuit->snd_stream, circuit->idx);
1993
1994 struct isis_passwd *passwd = (level == ISIS_LEVEL1)
1995 ? &circuit->area->area_passwd
1996 : &circuit->area->domain_passwd;
1997
1998 struct isis_tlvs *tlvs = isis_alloc_tlvs();
1999
2000 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
2001 isis_tlvs_add_auth(tlvs, passwd);
2002
2003 size_t tlv_start = stream_get_endp(circuit->snd_stream);
2004 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer, false)) {
2005 isis_free_tlvs(tlvs);
2006 return ISIS_WARNING;
2007 }
2008 isis_free_tlvs(tlvs);
2009
2010 uint16_t num_lsps =
2011 get_max_lsp_count(STREAM_WRITEABLE(circuit->snd_stream));
f390d2c7 2012
d62a17ae 2013 while (1) {
17c9dcd5
CF
2014 tlvs = isis_alloc_tlvs();
2015 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
2016 isis_tlvs_add_auth(tlvs, passwd);
2017
2018 for (dnode_t *dnode =
2019 dict_first(circuit->area->lspdb[level - 1]);
2020 dnode; dnode = dict_next(circuit->area->lspdb[level - 1],
2021 dnode)) {
2022 struct isis_lsp *lsp = dnode_get(dnode);
2023
2024 if (ISIS_CHECK_FLAG(lsp->SSNflags, circuit))
2025 isis_tlvs_add_lsp_entry(tlvs, lsp);
eb5d44eb 2026
17c9dcd5
CF
2027 if (tlvs->lsp_entries.count == num_lsps)
2028 break;
2029 }
2030
2031 if (!tlvs->lsp_entries.count) {
2032 isis_free_tlvs(tlvs);
d62a17ae 2033 return ISIS_OK;
2034 }
2035
17c9dcd5
CF
2036 stream_set_endp(circuit->snd_stream, tlv_start);
2037 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer,
2038 false)) {
2039 isis_free_tlvs(tlvs);
2040 return ISIS_WARNING;
d62a17ae 2041 }
2042
2043 if (isis->debugs & DEBUG_SNP_PACKETS) {
2044 zlog_debug(
2045 "ISIS-Snp (%s): Sending L%d PSNP on %s, length %zd",
2046 circuit->area->area_tag, level,
2047 circuit->interface->name,
2048 stream_get_endp(circuit->snd_stream));
17c9dcd5
CF
2049 log_multiline(LOG_DEBUG, " ", "%s",
2050 isis_format_tlvs(tlvs));
d62a17ae 2051 if (isis->debugs & DEBUG_PACKET_DUMP)
2052 zlog_dump_data(
2053 STREAM_DATA(circuit->snd_stream),
2054 stream_get_endp(circuit->snd_stream));
2055 }
2056
17c9dcd5 2057 int retval = circuit->tx(circuit, level);
d62a17ae 2058 if (retval != ISIS_OK) {
2059 zlog_err("ISIS-Snp (%s): Send L%d PSNP on %s failed",
2060 circuit->area->area_tag, level,
2061 circuit->interface->name);
17c9dcd5 2062 isis_free_tlvs(tlvs);
d62a17ae 2063 return retval;
2064 }
2065
2066 /*
2067 * sending succeeded, we can clear SSN flags of this circuit
2068 * for the LSPs in list
2069 */
17c9dcd5
CF
2070 struct isis_lsp_entry *entry_head;
2071 entry_head = (struct isis_lsp_entry *)tlvs->lsp_entries.head;
2072 for (struct isis_lsp_entry *entry = entry_head; entry;
2073 entry = entry->next)
2074 ISIS_CLEAR_FLAG(entry->lsp->SSNflags, circuit);
2075 isis_free_tlvs(tlvs);
d62a17ae 2076 }
2077
17c9dcd5 2078 return ISIS_OK;
eb5d44eb 2079}
2080
d62a17ae 2081int send_l1_psnp(struct thread *thread)
eb5d44eb 2082{
2083
d62a17ae 2084 struct isis_circuit *circuit;
2085 int retval = ISIS_OK;
eb5d44eb 2086
d62a17ae 2087 circuit = THREAD_ARG(thread);
2088 assert(circuit);
eb5d44eb 2089
d62a17ae 2090 circuit->t_send_psnp[0] = NULL;
eb5d44eb 2091
d62a17ae 2092 send_psnp(1, circuit);
2093 /* set next timer thread */
2094 thread_add_timer(master, send_l1_psnp, circuit,
2095 isis_jitter(circuit->psnp_interval[0], PSNP_JITTER),
2096 &circuit->t_send_psnp[0]);
eb5d44eb 2097
d62a17ae 2098 return retval;
eb5d44eb 2099}
2100
2101/*
2102 * 7.3.15.4 action on expiration of partial SNP interval
2103 * level 2
2104 */
d62a17ae 2105int send_l2_psnp(struct thread *thread)
eb5d44eb 2106{
d62a17ae 2107 struct isis_circuit *circuit;
2108 int retval = ISIS_OK;
eb5d44eb 2109
d62a17ae 2110 circuit = THREAD_ARG(thread);
2111 assert(circuit);
eb5d44eb 2112
d62a17ae 2113 circuit->t_send_psnp[1] = NULL;
eb5d44eb 2114
d62a17ae 2115 send_psnp(2, circuit);
eb5d44eb 2116
d62a17ae 2117 /* set next timer thread */
2118 thread_add_timer(master, send_l2_psnp, circuit,
2119 isis_jitter(circuit->psnp_interval[1], PSNP_JITTER),
2120 &circuit->t_send_psnp[1]);
eb5d44eb 2121
d62a17ae 2122 return retval;
eb5d44eb 2123}
2124
eb5d44eb 2125/*
2126 * ISO 10589 - 7.3.14.3
2127 */
d62a17ae 2128int send_lsp(struct thread *thread)
eb5d44eb 2129{
d62a17ae 2130 struct isis_circuit *circuit;
2131 struct isis_lsp *lsp;
2132 struct listnode *node;
2133 int clear_srm = 1;
2134 int retval = ISIS_OK;
2135
2136 circuit = THREAD_ARG(thread);
2137 assert(circuit);
2138
2139 if (!circuit->lsp_queue)
2140 return ISIS_OK;
2141
2142 node = listhead(circuit->lsp_queue);
2143
2144 /*
2145 * Handle case where there are no LSPs on the queue. This can
2146 * happen, for instance, if an adjacency goes down before this
2147 * thread gets a chance to run.
2148 */
2149 if (!node)
2150 return ISIS_OK;
2151
2152 /*
2153 * Delete LSP from lsp_queue. If it's still in queue, it is assumed
2154 * as 'transmit pending', but send_lsp may never be called again.
2155 * Retry will happen because SRM flag will not be cleared.
2156 */
2157 lsp = listgetdata(node);
2158 list_delete_node(circuit->lsp_queue, node);
2159
2160 /* Set the last-cleared time if the queue is empty. */
2161 /* TODO: Is is possible that new lsps keep being added to the queue
2162 * that the queue is never empty? */
2163 if (list_isempty(circuit->lsp_queue))
2164 circuit->lsp_queue_last_cleared = time(NULL);
2165
2166 if (circuit->state != C_STATE_UP || circuit->is_passive == 1)
2167 goto out;
2168
2169 /*
2170 * Do not send if levels do not match
2171 */
2172 if (!(lsp->level & circuit->is_type))
2173 goto out;
2174
2175 /*
2176 * Do not send if we do not have adjacencies in state up on the circuit
2177 */
2178 if (circuit->upadjcount[lsp->level - 1] == 0)
2179 goto out;
2180
2181 /* stream_copy will assert and stop program execution if LSP is larger
2182 * than
2183 * the circuit's MTU. So handle and log this case here. */
2184 if (stream_get_endp(lsp->pdu) > stream_get_size(circuit->snd_stream)) {
2185 zlog_err(
2186 "ISIS-Upd (%s): Can't send L%d LSP %s, seq 0x%08x,"
2187 " cksum 0x%04x, lifetime %us on %s. LSP Size is %zu"
2188 " while interface stream size is %zu.",
2189 circuit->area->area_tag, lsp->level,
2190 rawlspid_print(lsp->lsp_header->lsp_id),
2191 ntohl(lsp->lsp_header->seq_num),
2192 ntohs(lsp->lsp_header->checksum),
2193 ntohs(lsp->lsp_header->rem_lifetime),
2194 circuit->interface->name, stream_get_endp(lsp->pdu),
2195 stream_get_size(circuit->snd_stream));
2196 if (isis->debugs & DEBUG_PACKET_DUMP)
2197 zlog_dump_data(STREAM_DATA(lsp->pdu),
2198 stream_get_endp(lsp->pdu));
2199 retval = ISIS_ERROR;
2200 goto out;
2201 }
eb5d44eb 2202
d62a17ae 2203 /* copy our lsp to the send buffer */
2204 stream_copy(circuit->snd_stream, lsp->pdu);
2205
2206 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
2207 zlog_debug(
2208 "ISIS-Upd (%s): Sending L%d LSP %s, seq 0x%08x, cksum 0x%04x,"
2209 " lifetime %us on %s",
2210 circuit->area->area_tag, lsp->level,
2211 rawlspid_print(lsp->lsp_header->lsp_id),
2212 ntohl(lsp->lsp_header->seq_num),
2213 ntohs(lsp->lsp_header->checksum),
2214 ntohs(lsp->lsp_header->rem_lifetime),
2215 circuit->interface->name);
2216 if (isis->debugs & DEBUG_PACKET_DUMP)
2217 zlog_dump_data(STREAM_DATA(circuit->snd_stream),
2218 stream_get_endp(circuit->snd_stream));
2219 }
3f045a08 2220
d62a17ae 2221 clear_srm = 0;
2222 retval = circuit->tx(circuit, lsp->level);
2223 if (retval != ISIS_OK) {
2224 zlog_err("ISIS-Upd (%s): Send L%d LSP on %s failed %s",
2225 circuit->area->area_tag, lsp->level,
2226 circuit->interface->name,
2227 (retval == ISIS_WARNING) ? "temporarily"
2228 : "permanently");
2229 }
f390d2c7 2230
cfd1f27b 2231out:
d62a17ae 2232 if (clear_srm
2233 || (retval == ISIS_OK && circuit->circ_type == CIRCUIT_T_BROADCAST)
2234 || (retval != ISIS_OK && retval != ISIS_WARNING)) {
2235 /* SRM flag will trigger retransmission. We will not retransmit
2236 * if we
2237 * encountered a fatal error.
2238 * On success, they should only be cleared if it's a broadcast
2239 * circuit.
2240 * On a P2P circuit, we will wait for the ack from the neighbor
2241 * to clear
2242 * the fag.
2243 */
2244 ISIS_CLEAR_FLAG(lsp->SRMflags, circuit);
2245 }
eb5d44eb 2246
d62a17ae 2247 return retval;
f390d2c7 2248}
eb5d44eb 2249
d62a17ae 2250int ack_lsp(struct isis_link_state_hdr *hdr, struct isis_circuit *circuit,
2251 int level)
eb5d44eb 2252{
d62a17ae 2253 unsigned long lenp;
2254 int retval;
2255 u_int16_t length;
88f9d911
CF
2256 uint8_t pdu_type =
2257 (level == IS_LEVEL_1) ? L1_PARTIAL_SEQ_NUM : L2_PARTIAL_SEQ_NUM;
d62a17ae 2258
2259 isis_circuit_stream(circuit, &circuit->snd_stream);
2260
88f9d911 2261 fill_fixed_hdr(pdu_type, circuit->snd_stream);
d62a17ae 2262
2263 lenp = stream_get_endp(circuit->snd_stream);
2264 stream_putw(circuit->snd_stream, 0); /* PDU length */
2265 stream_put(circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
2266 stream_putc(circuit->snd_stream, circuit->idx);
2267 stream_putc(circuit->snd_stream, 9); /* code */
2268 stream_putc(circuit->snd_stream, 16); /* len */
2269
2270 stream_putw(circuit->snd_stream, ntohs(hdr->rem_lifetime));
2271 stream_put(circuit->snd_stream, hdr->lsp_id, ISIS_SYS_ID_LEN + 2);
2272 stream_putl(circuit->snd_stream, ntohl(hdr->seq_num));
2273 stream_putw(circuit->snd_stream, ntohs(hdr->checksum));
2274
2275 length = (u_int16_t)stream_get_endp(circuit->snd_stream);
2276 /* Update PDU length */
2277 stream_putw_at(circuit->snd_stream, lenp, length);
2278
2279 retval = circuit->tx(circuit, level);
2280 if (retval != ISIS_OK)
2281 zlog_err("ISIS-Upd (%s): Send L%d LSP PSNP on %s failed",
2282 circuit->area->area_tag, level,
2283 circuit->interface->name);
2284
2285 return retval;
eb5d44eb 2286}