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