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