]> git.proxmox.com Git - mirror_frr.git/blame - isisd/isis_pdu.c
fabricd: Remove processing for unneded PDUs and TLVs
[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)
af4c2728 93 flog_err(ISIS_ERR_PACKET,
54ece698
DS
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)) {
af4c2728 628 flog_err(ISIS_ERR_PACKET,
54ece698
DS
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
0c1bd758
CF
682 iih.v4_usable = (circuit->ip_addrs && listcount(circuit->ip_addrs)
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
705/*
706 * Process Level 1/2 Link State
707 * ISO - 10589
708 * Section 7.3.15.1 - Action on receipt of a link state PDU
f390d2c7 709 */
af8ac8f9 710static int process_lsp(uint8_t pdu_type, struct isis_circuit *circuit,
9b39405f 711 const uint8_t *ssnpa, uint8_t max_area_addrs)
eb5d44eb 712{
1cbd5b37
CF
713 int level;
714 bool circuit_scoped;
715
716 if (pdu_type == FS_LINK_STATE) {
717 if (!fabricd)
718 return ISIS_ERROR;
719 if (max_area_addrs != L2_CIRCUIT_FLOODING_SCOPE)
720 return ISIS_ERROR;
721 level = ISIS_LEVEL2;
722 circuit_scoped = true;
723
724 /* The stream is used verbatim for sending out new LSPDUs.
725 * So make sure we store it as an L2 LSPDU internally.
726 * (compare for the reverse in `send_lsp`) */
727 stream_putc_at(circuit->rcv_stream, 4, L2_LINK_STATE);
728 stream_putc_at(circuit->rcv_stream, 7, 0);
729 } else {
730 if (pdu_type == L1_LINK_STATE)
731 level = ISIS_LEVEL1;
732 else
733 level = ISIS_LEVEL2;
734 circuit_scoped = false;
735 }
d62a17ae 736
737 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
738 zlog_debug(
1cbd5b37
CF
739 "ISIS-Upd (%s): Rcvd %sL%d LSP on %s, cirType %s, cirID %u",
740 circuit->area->area_tag,
741 circuit_scoped ? "Circuit scoped " : "", level,
d62a17ae 742 circuit->interface->name,
743 circuit_t2string(circuit->is_type),
744 circuit->circuit_id);
745 if (isis->debugs & DEBUG_PACKET_DUMP)
746 zlog_dump_data(STREAM_DATA(circuit->rcv_stream),
747 stream_get_endp(circuit->rcv_stream));
748 }
f390d2c7 749
af8ac8f9 750 struct isis_lsp_hdr hdr = {};
eb5d44eb 751
af8ac8f9
CF
752 hdr.pdu_len = stream_getw(circuit->rcv_stream);
753 hdr.rem_lifetime = stream_getw(circuit->rcv_stream);
754 stream_get(hdr.lsp_id, circuit->rcv_stream, sizeof(hdr.lsp_id));
755 hdr.seqno = stream_getl(circuit->rcv_stream);
756 hdr.checksum = stream_getw(circuit->rcv_stream);
757 hdr.lsp_bits = stream_getc(circuit->rcv_stream);
eb5d44eb 758
af8ac8f9
CF
759 if (pdu_len_validate(hdr.pdu_len, circuit)) {
760 zlog_debug("ISIS-Upd (%s): LSP %s invalid LSP length %" PRIu16,
761 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
762 hdr.pdu_len);
d62a17ae 763 return ISIS_WARNING;
f390d2c7 764 }
d62a17ae 765
d62a17ae 766 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
af8ac8f9
CF
767 zlog_debug("ISIS-Upd (%s): Rcvd L%d LSP %s, seq 0x%08" PRIx32
768 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
769 "s, len %" PRIu16 ", on %s",
770 circuit->area->area_tag, level,
771 rawlspid_print(hdr.lsp_id), hdr.seqno, hdr.checksum,
772 hdr.rem_lifetime, hdr.pdu_len,
773 circuit->interface->name);
f390d2c7 774 }
d62a17ae 775
776 /* lsp is_type check */
af8ac8f9
CF
777 if ((hdr.lsp_bits & IS_LEVEL_1) != IS_LEVEL_1) {
778 zlog_debug(
779 "ISIS-Upd (%s): LSP %s invalid LSP is type 0x%" PRIx8,
780 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
781 hdr.lsp_bits & IS_LEVEL_1_AND_2);
d62a17ae 782 /* continue as per RFC1122 Be liberal in what you accept, and
783 * conservative in what you send */
f390d2c7 784 }
d62a17ae 785
786 /* Checksum sanity check - FIXME: move to correct place */
787 /* 12 = sysid+pdu+remtime */
af8ac8f9
CF
788 if (iso_csum_verify(STREAM_DATA(circuit->rcv_stream) + 12,
789 hdr.pdu_len - 12, hdr.checksum, 12)) {
790 zlog_debug(
791 "ISIS-Upd (%s): LSP %s invalid LSP checksum 0x%04" PRIx16,
792 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
793 hdr.checksum);
d62a17ae 794 return ISIS_WARNING;
f390d2c7 795 }
3f045a08 796
d62a17ae 797 /* 7.3.15.1 a) 1 - external domain circuit will discard lsps */
798 if (circuit->ext_domain) {
799 zlog_debug(
800 "ISIS-Upd (%s): LSP %s received at level %d over circuit with "
801 "externalDomain = true",
af8ac8f9 802 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
d62a17ae 803 level);
d62a17ae 804 return ISIS_WARNING;
805 }
eb5d44eb 806
d62a17ae 807 /* 7.3.15.1 a) 2,3 - manualL2OnlyMode not implemented */
af8ac8f9 808 if (!(circuit->is_type & level)) {
d62a17ae 809 zlog_debug(
810 "ISIS-Upd (%s): LSP %s received at level %d over circuit of"
811 " type %s",
af8ac8f9 812 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
d62a17ae 813 level, circuit_t2string(circuit->is_type));
d62a17ae 814 return ISIS_WARNING;
815 }
0250758d 816
af8ac8f9
CF
817 struct isis_tlvs *tlvs = NULL;
818 int retval = ISIS_WARNING;
819 const char *error_log;
820
821 if (isis_unpack_tlvs(STREAM_READABLE(circuit->rcv_stream),
822 circuit->rcv_stream, &tlvs, &error_log)) {
823 zlog_warn("Something went wrong unpacking the LSP: %s",
824 error_log);
825 goto out;
826 }
827
d62a17ae 828 /* 7.3.15.1 a) 4 - need to make sure IDLength matches */
829
830 /* 7.3.15.1 a) 5 - maximum area match, can be ommited since we only use
831 * 3 */
832
833 /* 7.3.15.1 a) 7 - password check */
af8ac8f9
CF
834 struct isis_passwd *passwd = (level == ISIS_LEVEL1)
835 ? &circuit->area->area_passwd
836 : &circuit->area->domain_passwd;
837 if (!isis_tlvs_auth_is_valid(tlvs, passwd, circuit->rcv_stream, true)) {
838 isis_event_auth_failure(circuit->area->area_tag,
839 "LSP authentication failure",
840 hdr.lsp_id);
841 goto out;
f390d2c7 842 }
af8ac8f9 843
d62a17ae 844 /* Find the LSP in our database and compare it to this Link State header
845 */
af8ac8f9
CF
846 struct isis_lsp *lsp =
847 lsp_search(hdr.lsp_id, circuit->area->lspdb[level - 1]);
848 int comp = 0;
d62a17ae 849 if (lsp)
af8ac8f9
CF
850 comp = lsp_compare(circuit->area->area_tag, lsp, hdr.seqno,
851 hdr.checksum, hdr.rem_lifetime);
d62a17ae 852 if (lsp && (lsp->own_lsp))
853 goto dontcheckadj;
854
855 /* 7.3.15.1 a) 6 - Must check that we have an adjacency of the same
856 * level */
857 /* for broadcast circuits, snpa should be compared */
858
859 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
af8ac8f9
CF
860 if (!isis_adj_lookup_snpa(ssnpa,
861 circuit->u.bc.adjdb[level - 1])) {
862 zlog_debug("(%s): DS ======= LSP %s, seq 0x%08" PRIx32
863 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
864 "s on %s",
865 circuit->area->area_tag,
866 rawlspid_print(hdr.lsp_id), hdr.seqno,
867 hdr.checksum, hdr.rem_lifetime,
868 circuit->interface->name);
869 goto out; /* Silently discard */
d62a17ae 870 }
871 }
872 /* for non broadcast, we just need to find same level adj */
873 else {
874 /* If no adj, or no sharing of level */
875 if (!circuit->u.p2p.neighbor) {
af8ac8f9
CF
876 retval = ISIS_OK;
877 goto out;
d62a17ae 878 } else {
879 if (((level == IS_LEVEL_1)
880 && (circuit->u.p2p.neighbor->adj_usage
881 == ISIS_ADJ_LEVEL2))
882 || ((level == IS_LEVEL_2)
883 && (circuit->u.p2p.neighbor->adj_usage
884 == ISIS_ADJ_LEVEL1)))
af8ac8f9 885 goto out;
f390d2c7 886 }
f390d2c7 887 }
f390d2c7 888
af8ac8f9
CF
889 bool lsp_confusion;
890
d62a17ae 891dontcheckadj:
892 /* 7.3.15.1 a) 7 - Passwords for level 1 - not implemented */
893
894 /* 7.3.15.1 a) 8 - Passwords for level 2 - not implemented */
895
896 /* 7.3.15.1 a) 9 - OriginatingLSPBufferSize - not implemented FIXME: do
897 * it */
898
899 /* 7.3.16.2 - If this is an LSP from another IS with identical seq_num
900 * but
901 * wrong checksum, initiate a purge. */
af8ac8f9 902 if (lsp && (lsp->hdr.seqno == hdr.seqno)
2c92bee4
CF
903 && (lsp->hdr.checksum != hdr.checksum)
904 && hdr.rem_lifetime) {
af8ac8f9
CF
905 zlog_warn("ISIS-Upd (%s): LSP %s seq 0x%08" PRIx32
906 " with confused checksum received.",
907 circuit->area->area_tag, rawlspid_print(hdr.lsp_id),
908 hdr.seqno);
909 hdr.rem_lifetime = 0;
910 lsp_confusion = true;
d62a17ae 911 } else
af8ac8f9 912 lsp_confusion = false;
d62a17ae 913
914 /* 7.3.15.1 b) - If the remaining life time is 0, we perform 7.3.16.4 */
af8ac8f9 915 if (hdr.rem_lifetime == 0) {
d62a17ae 916 if (!lsp) {
917 /* 7.3.16.4 a) 1) No LSP in db -> send an ack, but don't
918 * save */
919 /* only needed on explicit update, eg - p2p */
920 if (circuit->circ_type == CIRCUIT_T_P2P)
af8ac8f9
CF
921 ack_lsp(&hdr, circuit, level);
922 goto out; /* FIXME: do we need a purge? */
d62a17ae 923 } else {
af8ac8f9 924 if (memcmp(hdr.lsp_id, isis->sysid, ISIS_SYS_ID_LEN)) {
d62a17ae 925 /* LSP by some other system -> do 7.3.16.4 b) */
926 /* 7.3.16.4 b) 1) */
927 if (comp == LSP_NEWER) {
af8ac8f9
CF
928 lsp_update(lsp, &hdr, tlvs,
929 circuit->rcv_stream,
164066e4
CF
930 circuit->area, level,
931 lsp_confusion);
af8ac8f9 932 tlvs = NULL;
d62a17ae 933 /* ii */
1cbd5b37
CF
934 if (!circuit_scoped)
935 lsp_flood(lsp, NULL);
d62a17ae 936 /* v */
937 ISIS_FLAGS_CLEAR_ALL(
996c9314
LB
938 lsp->SSNflags); /* FIXME:
939 OTHER
940 than c
941 */
d62a17ae 942
943 /* For the case of lsp confusion, flood
944 * the purge back to its
945 * originator so that it can react.
946 * Otherwise, don't reflood
947 * through incoming circuit as usual */
948 if (!lsp_confusion) {
9b39405f
CF
949 isis_tx_queue_del(
950 circuit->tx_queue,
951 lsp);
952
d62a17ae 953 /* iv */
954 if (circuit->circ_type
955 != CIRCUIT_T_BROADCAST)
956 ISIS_SET_FLAG(
957 lsp->SSNflags,
958 circuit);
959 }
960 } /* 7.3.16.4 b) 2) */
961 else if (comp == LSP_EQUAL) {
962 /* i */
9b39405f
CF
963 isis_tx_queue_del(circuit->tx_queue,
964 lsp);
d62a17ae 965 /* ii */
966 if (circuit->circ_type
967 != CIRCUIT_T_BROADCAST)
968 ISIS_SET_FLAG(lsp->SSNflags,
969 circuit);
970 } /* 7.3.16.4 b) 3) */
971 else {
9b39405f
CF
972 isis_tx_queue_add(circuit->tx_queue,
973 lsp, TX_LSP_NORMAL);
d62a17ae 974 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
975 }
af8ac8f9 976 } else if (lsp->hdr.rem_lifetime != 0) {
d62a17ae 977 /* our own LSP -> 7.3.16.4 c) */
978 if (comp == LSP_NEWER) {
af8ac8f9 979 lsp_inc_seqno(lsp, hdr.seqno);
1cbd5b37
CF
980 if (!circuit_scoped)
981 lsp_flood(lsp, NULL);
d62a17ae 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 }
987 if (isis->debugs & DEBUG_UPDATE_PACKETS)
988 zlog_debug(
af8ac8f9 989 "ISIS-Upd (%s): (1) re-originating LSP %s new seq 0x%08" PRIx32,
d62a17ae 990 circuit->area->area_tag,
af8ac8f9
CF
991 rawlspid_print(hdr.lsp_id),
992 lsp->hdr.seqno);
d62a17ae 993 }
f390d2c7 994 }
af8ac8f9 995 goto out;
f390d2c7 996 }
d62a17ae 997 /* 7.3.15.1 c) - If this is our own lsp and we don't have it initiate a
998 * purge */
af8ac8f9 999 if (memcmp(hdr.lsp_id, isis->sysid, ISIS_SYS_ID_LEN) == 0) {
d62a17ae 1000 if (!lsp) {
1001 /* 7.3.16.4: initiate a purge */
af8ac8f9
CF
1002 lsp_purge_non_exist(level, &hdr, circuit->area);
1003 retval = ISIS_OK;
1004 goto out;
d62a17ae 1005 }
1006 /* 7.3.15.1 d) - If this is our own lsp and we have it */
1007
1008 /* In 7.3.16.1, If an Intermediate system R somewhere in the
1009 * domain
1010 * has information that the current sequence number for source S
1011 * is
1012 * "greater" than that held by S, ... */
1013
af8ac8f9 1014 if (hdr.seqno > lsp->hdr.seqno) {
d62a17ae 1015 /* 7.3.16.1 */
af8ac8f9 1016 lsp_inc_seqno(lsp, hdr.seqno);
d62a17ae 1017 if (isis->debugs & DEBUG_UPDATE_PACKETS)
1018 zlog_debug(
af8ac8f9 1019 "ISIS-Upd (%s): (2) re-originating LSP %s new seq 0x%08" PRIx32,
d62a17ae 1020 circuit->area->area_tag,
af8ac8f9
CF
1021 rawlspid_print(hdr.lsp_id),
1022 lsp->hdr.seqno);
d62a17ae 1023 }
1024 /* If the received LSP is older or equal,
1025 * resend the LSP which will act as ACK */
9b39405f 1026 lsp_flood(lsp, NULL);
d62a17ae 1027 } else {
1028 /* 7.3.15.1 e) - This lsp originated on another system */
1029
1030 /* 7.3.15.1 e) 1) LSP newer than the one in db or no LSP in db
1031 */
1032 if ((!lsp || comp == LSP_NEWER)) {
1033 /*
1034 * If this lsp is a frag, need to see if we have zero
1035 * lsp present
1036 */
af8ac8f9
CF
1037 struct isis_lsp *lsp0 = NULL;
1038 if (LSP_FRAGMENT(hdr.lsp_id) != 0) {
1039 uint8_t lspid[ISIS_SYS_ID_LEN + 2];
1040 memcpy(lspid, hdr.lsp_id, ISIS_SYS_ID_LEN + 1);
d62a17ae 1041 LSP_FRAGMENT(lspid) = 0;
1042 lsp0 = lsp_search(
1043 lspid, circuit->area->lspdb[level - 1]);
1044 if (!lsp0) {
1045 zlog_debug(
1046 "Got lsp frag, while zero lsp not in database");
1047 return ISIS_OK;
1048 }
1049 }
1050 /* i */
1051 if (!lsp) {
af8ac8f9
CF
1052 lsp = lsp_new_from_recv(
1053 &hdr, tlvs, circuit->rcv_stream, lsp0,
d62a17ae 1054 circuit->area, level);
af8ac8f9 1055 tlvs = NULL;
d62a17ae 1056 lsp_insert(lsp,
1057 circuit->area->lspdb[level - 1]);
1058 } else /* exists, so we overwrite */
1059 {
af8ac8f9 1060 lsp_update(lsp, &hdr, tlvs, circuit->rcv_stream,
164066e4 1061 circuit->area, level, false);
af8ac8f9 1062 tlvs = NULL;
d62a17ae 1063 }
1cbd5b37
CF
1064 if (!circuit_scoped)
1065 lsp_flood(lsp, circuit);
d62a17ae 1066
1067 /* iv */
1068 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
1069 ISIS_SET_FLAG(lsp->SSNflags, circuit);
1070 /* FIXME: v) */
1071 }
1072 /* 7.3.15.1 e) 2) LSP equal to the one in db */
1073 else if (comp == LSP_EQUAL) {
9b39405f 1074 isis_tx_queue_del(circuit->tx_queue, lsp);
af8ac8f9 1075 lsp_update(lsp, &hdr, tlvs, circuit->rcv_stream,
164066e4 1076 circuit->area, level, false);
af8ac8f9 1077 tlvs = NULL;
d62a17ae 1078 if (circuit->circ_type != CIRCUIT_T_BROADCAST)
1079 ISIS_SET_FLAG(lsp->SSNflags, circuit);
1080 }
1081 /* 7.3.15.1 e) 3) LSP older than the one in db */
1082 else {
9b39405f
CF
1083 isis_tx_queue_add(circuit->tx_queue, lsp,
1084 TX_LSP_NORMAL);
d62a17ae 1085 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
1086 }
f390d2c7 1087 }
af8ac8f9
CF
1088
1089 retval = ISIS_OK;
1090
1091out:
1092 isis_free_tlvs(tlvs);
d62a17ae 1093 return retval;
eb5d44eb 1094}
1095
1096/*
1097 * Process Sequence Numbers
1098 * ISO - 10589
1099 * Section 7.3.15.2 - Action on receipt of a sequence numbers PDU
1100 */
1101
17c9dcd5 1102static int process_snp(uint8_t pdu_type, struct isis_circuit *circuit,
d7c0a89a 1103 const uint8_t *ssnpa)
eb5d44eb 1104{
17c9dcd5
CF
1105 bool is_csnp = (pdu_type == L1_COMPLETE_SEQ_NUM
1106 || pdu_type == L2_COMPLETE_SEQ_NUM);
1107 char typechar = is_csnp ? 'C' : 'P';
1108 int level = (pdu_type == L1_COMPLETE_SEQ_NUM
1109 || pdu_type == L1_PARTIAL_SEQ_NUM)
1110 ? ISIS_LEVEL1
1111 : ISIS_LEVEL2;
1112
1113 uint16_t pdu_len = stream_getw(circuit->rcv_stream);
1114 uint8_t rem_sys_id[ISIS_SYS_ID_LEN];
1115 stream_get(rem_sys_id, circuit->rcv_stream, ISIS_SYS_ID_LEN);
1116 stream_forward_getp(circuit->rcv_stream, 1); /* Circuit ID - unused */
1117
1118 uint8_t start_lsp_id[ISIS_SYS_ID_LEN + 2] = {};
1119 uint8_t stop_lsp_id[ISIS_SYS_ID_LEN + 2] = {};
1120
1121 if (is_csnp) {
1122 stream_get(start_lsp_id, circuit->rcv_stream,
1123 ISIS_SYS_ID_LEN + 2);
1124 stream_get(stop_lsp_id, circuit->rcv_stream,
1125 ISIS_SYS_ID_LEN + 2);
1126 }
1127
1128 if (pdu_len_validate(pdu_len, circuit)) {
1129 zlog_warn("Received a CSNP with bogus length %d", pdu_len);
1130 return ISIS_WARNING;
f390d2c7 1131 }
eb5d44eb 1132
17c9dcd5
CF
1133 if (isis->debugs & DEBUG_SNP_PACKETS) {
1134 zlog_debug(
1135 "ISIS-Snp (%s): Rcvd L%d %cSNP on %s, cirType %s, cirID %u",
1136 circuit->area->area_tag, level, typechar,
1137 circuit->interface->name,
1138 circuit_t2string(circuit->is_type),
1139 circuit->circuit_id);
1140 if (isis->debugs & DEBUG_PACKET_DUMP)
1141 zlog_dump_data(STREAM_DATA(circuit->rcv_stream),
1142 stream_get_endp(circuit->rcv_stream));
1143 }
eb5d44eb 1144
d62a17ae 1145 /* 7.3.15.2 a) 1 - external domain circuit will discard snp pdu */
1146 if (circuit->ext_domain) {
f390d2c7 1147
d62a17ae 1148 zlog_debug(
1149 "ISIS-Snp (%s): Rcvd L%d %cSNP on %s, "
1150 "skipping: circuit externalDomain = true",
1151 circuit->area->area_tag, level, typechar,
1152 circuit->interface->name);
f390d2c7 1153
d62a17ae 1154 return ISIS_OK;
1155 }
eb5d44eb 1156
d62a17ae 1157 /* 7.3.15.2 a) 2,3 - manualL2OnlyMode not implemented */
af8ac8f9 1158 if (!(circuit->is_type & level)) {
d62a17ae 1159 zlog_debug(
1160 "ISIS-Snp (%s): Rcvd L%d %cSNP on %s, "
1161 "skipping: circuit type %s does not match level %d",
1162 circuit->area->area_tag, level, typechar,
1163 circuit->interface->name,
1164 circuit_t2string(circuit->is_type), level);
eb5d44eb 1165
d62a17ae 1166 return ISIS_OK;
f390d2c7 1167 }
eb5d44eb 1168
d62a17ae 1169 /* 7.3.15.2 a) 4 - not applicable for CSNP only PSNPs on broadcast */
17c9dcd5
CF
1170 if (!is_csnp && (circuit->circ_type == CIRCUIT_T_BROADCAST)
1171 && !circuit->u.bc.is_dr[level - 1]) {
d62a17ae 1172 zlog_debug(
1173 "ISIS-Snp (%s): Rcvd L%d %cSNP from %s on %s, "
1174 "skipping: we are not the DIS",
1175 circuit->area->area_tag, level, typechar,
1176 snpa_print(ssnpa), circuit->interface->name);
1177
1178 return ISIS_OK;
1179 }
eb5d44eb 1180
d62a17ae 1181 /* 7.3.15.2 a) 5 - need to make sure IDLength matches - already checked
1182 */
1183
1184 /* 7.3.15.2 a) 6 - maximum area match, can be ommited since we only use
1185 * 3
1186 * - already checked */
1187
1188 /* 7.3.15.2 a) 7 - Must check that we have an adjacency of the same
1189 * level */
1190 /* for broadcast circuits, snpa should be compared */
1191 /* FIXME : Do we need to check SNPA? */
1192 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
17c9dcd5
CF
1193 if (!isis_adj_lookup(rem_sys_id,
1194 circuit->u.bc.adjdb[level - 1]))
d62a17ae 1195 return ISIS_OK; /* Silently discard */
1196 } else {
8e6fb83b 1197 if (!fabricd && !circuit->u.p2p.neighbor) {
d62a17ae 1198 zlog_warn("no p2p neighbor on circuit %s",
1199 circuit->interface->name);
1200 return ISIS_OK; /* Silently discard */
1201 }
1202 }
eb5d44eb 1203
17c9dcd5
CF
1204 struct isis_tlvs *tlvs;
1205 int retval = ISIS_WARNING;
1206 const char *error_log;
eb5d44eb 1207
17c9dcd5
CF
1208 if (isis_unpack_tlvs(STREAM_READABLE(circuit->rcv_stream),
1209 circuit->rcv_stream, &tlvs, &error_log)) {
1210 zlog_warn("Something went wrong unpacking the SNP: %s",
1211 error_log);
1212 goto out;
f390d2c7 1213 }
eb5d44eb 1214
17c9dcd5
CF
1215 struct isis_passwd *passwd = (level == IS_LEVEL_1)
1216 ? &circuit->area->area_passwd
1217 : &circuit->area->domain_passwd;
1218 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_RECV)
af8ac8f9
CF
1219 && !isis_tlvs_auth_is_valid(tlvs, passwd, circuit->rcv_stream,
1220 false)) {
17c9dcd5
CF
1221 isis_event_auth_failure(circuit->area->area_tag,
1222 "SNP authentication failure",
1223 rem_sys_id);
1224 goto out;
d62a17ae 1225 }
eb5d44eb 1226
17c9dcd5
CF
1227 struct isis_lsp_entry *entry_head =
1228 (struct isis_lsp_entry *)tlvs->lsp_entries.head;
1229
d62a17ae 1230 /* debug isis snp-packets */
1231 if (isis->debugs & DEBUG_SNP_PACKETS) {
1232 zlog_debug("ISIS-Snp (%s): Rcvd L%d %cSNP from %s on %s",
1233 circuit->area->area_tag, level, typechar,
1234 snpa_print(ssnpa), circuit->interface->name);
17c9dcd5
CF
1235 for (struct isis_lsp_entry *entry = entry_head; entry;
1236 entry = entry->next) {
1237 zlog_debug(
1238 "ISIS-Snp (%s): %cSNP entry %s, seq 0x%08" PRIx32
98c5bc15 1239 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16 "s",
17c9dcd5
CF
1240 circuit->area->area_tag, typechar,
1241 rawlspid_print(entry->id), entry->seqno,
1242 entry->checksum, entry->rem_lifetime);
f390d2c7 1243 }
f390d2c7 1244 }
3f045a08 1245
d62a17ae 1246 /* 7.3.15.2 b) Actions on LSP_ENTRIES reported */
17c9dcd5
CF
1247 for (struct isis_lsp_entry *entry = entry_head; entry;
1248 entry = entry->next) {
1249 struct isis_lsp *lsp =
1250 lsp_search(entry->id, circuit->area->lspdb[level - 1]);
1251 bool own_lsp = !memcmp(entry->id, isis->sysid, ISIS_SYS_ID_LEN);
1252 if (lsp) {
1253 /* 7.3.15.2 b) 1) is this LSP newer */
1254 int cmp = lsp_compare(circuit->area->area_tag, lsp,
1255 entry->seqno, entry->checksum,
1256 entry->rem_lifetime);
1257 /* 7.3.15.2 b) 2) if it equals, clear SRM on p2p */
1258 if (cmp == LSP_EQUAL) {
1259 /* if (circuit->circ_type !=
1260 * CIRCUIT_T_BROADCAST) */
9b39405f 1261 isis_tx_queue_del(circuit->tx_queue, lsp);
17c9dcd5
CF
1262 }
1263 /* 7.3.15.2 b) 3) if it is older, clear SSN and set SRM
1264 */
1265 else if (cmp == LSP_OLDER) {
1266 ISIS_CLEAR_FLAG(lsp->SSNflags, circuit);
9b39405f
CF
1267 isis_tx_queue_add(circuit->tx_queue, lsp,
1268 TX_LSP_NORMAL);
17c9dcd5
CF
1269 }
1270 /* 7.3.15.2 b) 4) if it is newer, set SSN and clear SRM
1271 on p2p */
1272 else {
1273 if (own_lsp) {
af8ac8f9 1274 lsp_inc_seqno(lsp, entry->seqno);
9b39405f
CF
1275 isis_tx_queue_add(circuit->tx_queue, lsp,
1276 TX_LSP_NORMAL);
17c9dcd5
CF
1277 } else {
1278 ISIS_SET_FLAG(lsp->SSNflags, circuit);
d62a17ae 1279 /* if (circuit->circ_type !=
1280 * CIRCUIT_T_BROADCAST) */
9b39405f 1281 isis_tx_queue_del(circuit->tx_queue, lsp);
d62a17ae 1282 }
17c9dcd5
CF
1283 }
1284 } else {
1285 /* 7.3.15.2 b) 5) if it was not found, and all of those
1286 * are not 0,
1287 * insert it and set SSN on it */
1288 if (entry->rem_lifetime && entry->checksum
1289 && entry->seqno && memcmp(entry->id, isis->sysid,
1290 ISIS_SYS_ID_LEN)) {
8f5dbe18
CF
1291 struct isis_lsp *lsp0 = NULL;
1292
1293 if (LSP_FRAGMENT(entry->id)) {
1294 uint8_t lspid[ISIS_SYS_ID_LEN + 2];
1295
1296 memcpy(lspid, entry->id,
1297 ISIS_SYS_ID_LEN + 1);
1298 LSP_FRAGMENT(lspid) = 0;
1299 lsp0 = lsp_search(
98c5bc15
CF
1300 lspid,
1301 circuit->area->lspdb[level - 1]);
8f5dbe18 1302 if (!lsp0) {
98c5bc15 1303 zlog_debug("Got lsp frag in snp, while zero not in database");
8f5dbe18
CF
1304 continue;
1305 }
1306 }
17c9dcd5
CF
1307 struct isis_lsp *lsp =
1308 lsp_new(circuit->area, entry->id,
1309 entry->rem_lifetime, 0, 0,
8f5dbe18 1310 entry->checksum, lsp0, level);
17c9dcd5
CF
1311 lsp_insert(lsp,
1312 circuit->area->lspdb[level - 1]);
9b39405f
CF
1313
1314 lsp_set_all_srmflags(lsp, false);
17c9dcd5 1315 ISIS_SET_FLAG(lsp->SSNflags, circuit);
d62a17ae 1316 }
1317 }
1318 }
eb5d44eb 1319
d62a17ae 1320 /* 7.3.15.2 c) on CSNP set SRM for all in range which were not reported
1321 */
17c9dcd5 1322 if (is_csnp) {
d62a17ae 1323 /*
1324 * Build a list from our own LSP db bounded with
1325 * start_lsp_id and stop_lsp_id
1326 */
17c9dcd5
CF
1327 struct list *lsp_list = list_new();
1328 lsp_build_list_nonzero_ht(start_lsp_id, stop_lsp_id, lsp_list,
d62a17ae 1329 circuit->area->lspdb[level - 1]);
1330
1331 /* Fixme: Find a better solution */
17c9dcd5
CF
1332 struct listnode *node, *nnode;
1333 struct isis_lsp *lsp;
1334 for (struct isis_lsp_entry *entry = entry_head; entry;
1335 entry = entry->next) {
1336 for (ALL_LIST_ELEMENTS(lsp_list, node, nnode, lsp)) {
af8ac8f9 1337 if (lsp_id_cmp(lsp->hdr.lsp_id, entry->id)
17c9dcd5
CF
1338 == 0) {
1339 list_delete_node(lsp_list, node);
1340 break;
d62a17ae 1341 }
1342 }
1343 }
17c9dcd5 1344
d62a17ae 1345 /* on remaining LSPs we set SRM (neighbor knew not of) */
9b39405f
CF
1346 for (ALL_LIST_ELEMENTS_RO(lsp_list, node, lsp)) {
1347 isis_tx_queue_add(circuit->tx_queue, lsp, TX_LSP_NORMAL);
1348 }
1349
d62a17ae 1350 /* lets free it */
affe9e99 1351 list_delete_and_null(&lsp_list);
d62a17ae 1352 }
1353
17c9dcd5
CF
1354 retval = ISIS_OK;
1355out:
1356 isis_free_tlvs(tlvs);
d62a17ae 1357 return retval;
eb5d44eb 1358}
1359
88f9d911
CF
1360static int pdu_size(uint8_t pdu_type, uint8_t *size)
1361{
1362 switch (pdu_type) {
1363 case L1_LAN_HELLO:
1364 case L2_LAN_HELLO:
1365 *size = ISIS_LANHELLO_HDRLEN;
1366 break;
1367 case P2P_HELLO:
1368 *size = ISIS_P2PHELLO_HDRLEN;
1369 break;
1370 case L1_LINK_STATE:
1371 case L2_LINK_STATE:
1cbd5b37 1372 case FS_LINK_STATE:
88f9d911
CF
1373 *size = ISIS_LSP_HDR_LEN;
1374 break;
1375 case L1_COMPLETE_SEQ_NUM:
1376 case L2_COMPLETE_SEQ_NUM:
1377 *size = ISIS_CSNP_HDRLEN;
1378 break;
1379 case L1_PARTIAL_SEQ_NUM:
1380 case L2_PARTIAL_SEQ_NUM:
1381 *size = ISIS_PSNP_HDRLEN;
1382 break;
1383 default:
1384 return 1;
1385 }
1386 *size += ISIS_FIXED_HDR_LEN;
1387 return 0;
1388}
1389
eb5d44eb 1390/*
1391 * PDU Dispatcher
1392 */
1393
d7c0a89a 1394int isis_handle_pdu(struct isis_circuit *circuit, uint8_t *ssnpa)
eb5d44eb 1395{
d62a17ae 1396 int retval = ISIS_OK;
eb5d44eb 1397
88f9d911
CF
1398 /* Verify that at least the 8 bytes fixed header have been received */
1399 if (stream_get_endp(circuit->rcv_stream) < ISIS_FIXED_HDR_LEN) {
af4c2728 1400 flog_err(ISIS_ERR_PACKET, "PDU is too short to be IS-IS.");
88f9d911
CF
1401 return ISIS_ERROR;
1402 }
eb5d44eb 1403
88f9d911
CF
1404 uint8_t idrp = stream_getc(circuit->rcv_stream);
1405 uint8_t length = stream_getc(circuit->rcv_stream);
1406 uint8_t version1 = stream_getc(circuit->rcv_stream);
1407 uint8_t id_len = stream_getc(circuit->rcv_stream);
1408 uint8_t pdu_type = stream_getc(circuit->rcv_stream)
1409 & 0x1f; /* bits 6-8 are reserved */
1410 uint8_t version2 = stream_getc(circuit->rcv_stream);
1411 stream_forward_getp(circuit->rcv_stream, 1); /* reserved */
1412 uint8_t max_area_addrs = stream_getc(circuit->rcv_stream);
1413
1414 if (idrp == ISO9542_ESIS) {
af4c2728 1415 flog_err(LIB_ERR_DEVELOPMENT,
38937bd5 1416 "No support for ES-IS packet IDRP=%" PRIx8, idrp);
d62a17ae 1417 return ISIS_ERROR;
1418 }
eb5d44eb 1419
88f9d911 1420 if (idrp != ISO10589_ISIS) {
af4c2728 1421 flog_err(ISIS_ERR_PACKET, "Not an IS-IS packet IDRP=%" PRIx8,
54ece698 1422 idrp);
d62a17ae 1423 return ISIS_ERROR;
1424 }
3f045a08 1425
88f9d911
CF
1426 if (version1 != 1) {
1427 zlog_warn("Unsupported ISIS version %" PRIu8, version1);
1428 return ISIS_WARNING;
1429 }
eb5d44eb 1430
88f9d911 1431 if (id_len != 0 && id_len != ISIS_SYS_ID_LEN) {
af4c2728 1432 flog_err(
54ece698 1433 ISIS_ERR_PACKET,
88f9d911
CF
1434 "IDFieldLengthMismatch: ID Length field in a received PDU %" PRIu8
1435 ", while the parameter for this IS is %u",
1436 id_len, ISIS_SYS_ID_LEN);
d62a17ae 1437 return ISIS_ERROR;
1438 }
eb5d44eb 1439
88f9d911
CF
1440 uint8_t expected_length;
1441 if (pdu_size(pdu_type, &expected_length)) {
1442 zlog_warn("Unsupported ISIS PDU %" PRIu8, pdu_type);
d62a17ae 1443 return ISIS_WARNING;
1444 }
88f9d911
CF
1445
1446 if (length != expected_length) {
af4c2728 1447 flog_err(ISIS_ERR_PACKET,
54ece698
DS
1448 "Exepected fixed header length = %" PRIu8
1449 " but got %" PRIu8,
1450 expected_length, length);
88f9d911
CF
1451 return ISIS_ERROR;
1452 }
1453
1454 if (stream_get_endp(circuit->rcv_stream) < length) {
af4c2728 1455 flog_err(
54ece698 1456 ISIS_ERR_PACKET,
88f9d911 1457 "PDU is too short to contain fixed header of given PDU type.");
d62a17ae 1458 return ISIS_ERROR;
1459 }
eb5d44eb 1460
88f9d911
CF
1461 if (version2 != 1) {
1462 zlog_warn("Unsupported ISIS PDU version %" PRIu8, version2);
d62a17ae 1463 return ISIS_WARNING;
1464 }
3f045a08 1465
d62a17ae 1466 if (circuit->is_passive) {
1467 zlog_warn("Received ISIS PDU on passive circuit %s",
1468 circuit->interface->name);
1469 return ISIS_WARNING;
1470 }
3f045a08 1471
d62a17ae 1472 /* either 3 or 0 */
1cbd5b37
CF
1473 if (pdu_type != FS_LINK_STATE /* FS PDU doesn't contain max area addr field */
1474 && max_area_addrs != 0
1475 && max_area_addrs != isis->max_area_addrs) {
af4c2728 1476 flog_err(
54ece698 1477 ISIS_ERR_PACKET,
88f9d911
CF
1478 "maximumAreaAddressesMismatch: maximumAreaAdresses in a received PDU %" PRIu8
1479 " while the parameter for this IS is %u",
1480 max_area_addrs, isis->max_area_addrs);
d62a17ae 1481 return ISIS_ERROR;
1482 }
eb5d44eb 1483
88f9d911 1484 switch (pdu_type) {
d62a17ae 1485 case L1_LAN_HELLO:
d62a17ae 1486 case L2_LAN_HELLO:
d62a17ae 1487 case P2P_HELLO:
a2d41bb0
CF
1488 if (fabricd && pdu_type != P2P_HELLO)
1489 return ISIS_ERROR;
0c1bd758 1490 retval = process_hello(pdu_type, circuit, ssnpa);
d62a17ae 1491 break;
1492 case L1_LINK_STATE:
d62a17ae 1493 case L2_LINK_STATE:
1cbd5b37 1494 case FS_LINK_STATE:
a2d41bb0
CF
1495 if (fabricd
1496 && pdu_type != L2_LINK_STATE
1497 && pdu_type != FS_LINK_STATE)
1498 return ISIS_ERROR;
9b39405f 1499 retval = process_lsp(pdu_type, circuit, ssnpa, max_area_addrs);
d62a17ae 1500 break;
1501 case L1_COMPLETE_SEQ_NUM:
d62a17ae 1502 case L2_COMPLETE_SEQ_NUM:
d62a17ae 1503 case L1_PARTIAL_SEQ_NUM:
d62a17ae 1504 case L2_PARTIAL_SEQ_NUM:
17c9dcd5 1505 retval = process_snp(pdu_type, circuit, ssnpa);
d62a17ae 1506 break;
1507 default:
1508 return ISIS_ERROR;
1509 }
eb5d44eb 1510
d62a17ae 1511 return retval;
eb5d44eb 1512}
1513
d62a17ae 1514int isis_receive(struct thread *thread)
eb5d44eb 1515{
d62a17ae 1516 struct isis_circuit *circuit;
d7c0a89a 1517 uint8_t ssnpa[ETH_ALEN];
d62a17ae 1518 int retval;
eb5d44eb 1519
d62a17ae 1520 /*
1521 * Get the circuit
1522 */
1523 circuit = THREAD_ARG(thread);
1524 assert(circuit);
eb5d44eb 1525
d62a17ae 1526 circuit->t_read = NULL;
eb5d44eb 1527
d62a17ae 1528 isis_circuit_stream(circuit, &circuit->rcv_stream);
eb5d44eb 1529
d62a17ae 1530 retval = circuit->rx(circuit, ssnpa);
eb5d44eb 1531
b9347997 1532#if ISIS_METHOD != ISIS_METHOD_BPF
d62a17ae 1533 if (retval == ISIS_OK)
1534 retval = isis_handle_pdu(circuit, ssnpa);
b9347997 1535#endif //ISIS_METHOD != ISIS_METHOD_BPF
eb5d44eb 1536
d62a17ae 1537 /*
1538 * prepare for next packet.
1539 */
1540 if (!circuit->is_passive)
1541 isis_circuit_prepare(circuit);
eb5d44eb 1542
d62a17ae 1543 return retval;
eb5d44eb 1544}
1545
eb5d44eb 1546/*
d62a17ae 1547 * SEND SIDE
eb5d44eb 1548 */
88f9d911 1549void fill_fixed_hdr(uint8_t pdu_type, struct stream *stream)
eb5d44eb 1550{
88f9d911
CF
1551 uint8_t length;
1552
1553 if (pdu_size(pdu_type, &length))
1554 assert(!"Unknown PDU Type");
1555
1556 stream_putc(stream, ISO10589_ISIS); /* IDRP */
1557 stream_putc(stream, length); /* Length of fixed header */
1558 stream_putc(stream, 1); /* Version/Protocol ID Extension 1 */
1559 stream_putc(stream, 0); /* ID Length, 0 => 6 */
1560 stream_putc(stream, pdu_type);
1561 stream_putc(stream, 1); /* Subversion */
1562 stream_putc(stream, 0); /* Reserved */
1563 stream_putc(stream, 0); /* Max Area Addresses 0 => 3 */
eb5d44eb 1564}
1565
0c1bd758
CF
1566static void put_hello_hdr(struct isis_circuit *circuit, int level,
1567 size_t *len_pointer)
eb5d44eb 1568{
88f9d911
CF
1569 uint8_t pdu_type;
1570
d62a17ae 1571 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
88f9d911 1572 pdu_type = (level == IS_LEVEL_1) ? L1_LAN_HELLO : L2_LAN_HELLO;
d62a17ae 1573 else
88f9d911
CF
1574 pdu_type = P2P_HELLO;
1575
0c1bd758 1576 isis_circuit_stream(circuit, &circuit->snd_stream);
88f9d911 1577 fill_fixed_hdr(pdu_type, circuit->snd_stream);
d62a17ae 1578
0c1bd758
CF
1579 stream_putc(circuit->snd_stream, circuit->is_type);
1580 stream_put(circuit->snd_stream, circuit->area->isis->sysid,
1581 ISIS_SYS_ID_LEN);
eb5d44eb 1582
0c1bd758
CF
1583 uint32_t holdtime = circuit->hello_multiplier[level - 1]
1584 * circuit->hello_interval[level - 1];
d62a17ae 1585
0c1bd758
CF
1586 if (holdtime > 0xffff)
1587 holdtime = 0xffff;
d62a17ae 1588
0c1bd758
CF
1589 stream_putw(circuit->snd_stream, holdtime);
1590 *len_pointer = stream_get_endp(circuit->snd_stream);
1591 stream_putw(circuit->snd_stream, 0); /* length is filled in later */
d62a17ae 1592
0c1bd758 1593 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
d7c0a89a
QY
1594 uint8_t *desig_is = (level == IS_LEVEL_1)
1595 ? circuit->u.bc.l1_desig_is
1596 : circuit->u.bc.l2_desig_is;
0c1bd758
CF
1597 stream_putc(circuit->snd_stream, circuit->priority[level - 1]);
1598 stream_put(circuit->snd_stream, desig_is, ISIS_SYS_ID_LEN + 1);
1599 } else {
1600 stream_putc(circuit->snd_stream, circuit->circuit_id);
d62a17ae 1601 }
0c1bd758 1602}
eb5d44eb 1603
0c1bd758
CF
1604int send_hello(struct isis_circuit *circuit, int level)
1605{
1606 size_t len_pointer;
1607 int retval;
1608
1609 if (circuit->is_passive)
1610 return ISIS_OK;
1611
1612 if (circuit->interface->mtu == 0) {
1613 zlog_warn("circuit has zero MTU");
d62a17ae 1614 return ISIS_WARNING;
0c1bd758
CF
1615 }
1616
1617 put_hello_hdr(circuit, level, &len_pointer);
1618
1619 struct isis_tlvs *tlvs = isis_alloc_tlvs();
1620
1621 isis_tlvs_add_auth(tlvs, &circuit->passwd);
1622
a3acf37d
DS
1623 if (!listcount(circuit->area->area_addrs)) {
1624 isis_free_tlvs(tlvs);
d62a17ae 1625 return ISIS_WARNING;
a3acf37d
DS
1626 }
1627
0c1bd758 1628 isis_tlvs_add_area_addresses(tlvs, circuit->area->area_addrs);
d62a17ae 1629
42fe2621 1630 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
0c1bd758
CF
1631 isis_tlvs_add_lan_neighbors(
1632 tlvs, circuit->u.bc.lan_neighs[level - 1]);
58e5d748
CF
1633 } else if (circuit->circ_type == CIRCUIT_T_P2P
1634 && !circuit->disable_threeway_adj) {
42fe2621
CF
1635 uint32_t ext_circuit_id = circuit->idx;
1636 if (circuit->u.p2p.neighbor) {
8e6fb83b
CF
1637 uint8_t threeway_state;
1638
1639 if (fabricd_initial_sync_is_in_progress(circuit->area)
1640 && fabricd_initial_sync_circuit(circuit->area) != circuit)
1641 threeway_state = ISIS_THREEWAY_DOWN;
1642 else
1643 threeway_state = circuit->u.p2p.neighbor->threeway_state;
42fe2621 1644 isis_tlvs_add_threeway_adj(tlvs,
8e6fb83b 1645 threeway_state,
42fe2621
CF
1646 ext_circuit_id,
1647 circuit->u.p2p.neighbor->sysid,
1648 circuit->u.p2p.neighbor->ext_circuit_id);
1649 } else {
1650 isis_tlvs_add_threeway_adj(tlvs,
1651 ISIS_THREEWAY_DOWN,
1652 ext_circuit_id,
1653 NULL, 0);
1654 }
1655 }
0c1bd758
CF
1656
1657 isis_tlvs_set_protocols_supported(tlvs, &circuit->nlpids);
d62a17ae 1658
1659 /*
1660 * MT Supported TLV
1661 *
1662 * TLV gets included if no topology is enabled on the interface,
1663 * if one topology other than #0 is enabled, or if multiple topologies
1664 * are enabled.
1665 */
1666 struct isis_circuit_mt_setting **mt_settings;
1667 unsigned int mt_count;
1668
1669 mt_settings = circuit_mt_settings(circuit, &mt_count);
0c1bd758
CF
1670 if (mt_count == 0 && area_is_mt(circuit->area)) {
1671 tlvs->mt_router_info_empty = true;
1672 } else if ((mt_count == 1
1673 && mt_settings[0]->mtid != ISIS_MT_IPV4_UNICAST)
1674 || (mt_count > 1)) {
1675 for (unsigned int i = 0; i < mt_count; i++)
1676 isis_tlvs_add_mt_router_info(tlvs, mt_settings[i]->mtid,
1677 false, false);
d62a17ae 1678 }
eb5d44eb 1679
0c1bd758
CF
1680 if (circuit->ip_router && circuit->ip_addrs)
1681 isis_tlvs_add_ipv4_addresses(tlvs, circuit->ip_addrs);
d62a17ae 1682
0c1bd758
CF
1683 if (circuit->ipv6_router && circuit->ipv6_link)
1684 isis_tlvs_add_ipv6_addresses(tlvs, circuit->ipv6_link);
d62a17ae 1685
0c1bd758 1686 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer,
af8ac8f9 1687 circuit->pad_hellos, false)) {
0c1bd758
CF
1688 isis_free_tlvs(tlvs);
1689 return ISIS_WARNING; /* XXX: Maybe Log TLV structure? */
f390d2c7 1690 }
d62a17ae 1691
1692 if (isis->debugs & DEBUG_ADJ_PACKETS) {
1693 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
1694 zlog_debug(
1695 "ISIS-Adj (%s): Sending L%d LAN IIH on %s, length %zd",
1696 circuit->area->area_tag, level,
0c1bd758
CF
1697 circuit->interface->name,
1698 stream_get_endp(circuit->snd_stream));
d62a17ae 1699 } else {
1700 zlog_debug(
1701 "ISIS-Adj (%s): Sending P2P IIH on %s, length %zd",
1702 circuit->area->area_tag,
0c1bd758
CF
1703 circuit->interface->name,
1704 stream_get_endp(circuit->snd_stream));
d62a17ae 1705 }
1706 if (isis->debugs & DEBUG_PACKET_DUMP)
1707 zlog_dump_data(STREAM_DATA(circuit->snd_stream),
1708 stream_get_endp(circuit->snd_stream));
f390d2c7 1709 }
eb5d44eb 1710
0c1bd758
CF
1711 isis_free_tlvs(tlvs);
1712
d62a17ae 1713 retval = circuit->tx(circuit, level);
1714 if (retval != ISIS_OK)
af4c2728 1715 flog_err(ISIS_ERR_PACKET,
54ece698
DS
1716 "ISIS-Adj (%s): Send L%d IIH on %s failed",
1717 circuit->area->area_tag, level,
1718 circuit->interface->name);
eb5d44eb 1719
d62a17ae 1720 return retval;
eb5d44eb 1721}
1722
d62a17ae 1723int send_lan_l1_hello(struct thread *thread)
eb5d44eb 1724{
d62a17ae 1725 struct isis_circuit *circuit;
1726 int retval;
1727
1728 circuit = THREAD_ARG(thread);
1729 assert(circuit);
1730 circuit->u.bc.t_send_lan_hello[0] = NULL;
1731
1732 if (!(circuit->area->is_type & IS_LEVEL_1)) {
1733 zlog_warn(
1734 "ISIS-Hello (%s): Trying to send L1 IIH in L2-only area",
1735 circuit->area->area_tag);
1736 return 1;
1737 }
ddfdbd32 1738
d62a17ae 1739 if (circuit->u.bc.run_dr_elect[0])
1740 isis_dr_elect(circuit, 1);
eb5d44eb 1741
d62a17ae 1742 retval = send_hello(circuit, 1);
eb5d44eb 1743
d62a17ae 1744 /* set next timer thread */
1745 thread_add_timer(master, send_lan_l1_hello, circuit,
1746 isis_jitter(circuit->hello_interval[0], IIH_JITTER),
1747 &circuit->u.bc.t_send_lan_hello[0]);
eb5d44eb 1748
d62a17ae 1749 return retval;
eb5d44eb 1750}
1751
d62a17ae 1752int send_lan_l2_hello(struct thread *thread)
eb5d44eb 1753{
d62a17ae 1754 struct isis_circuit *circuit;
1755 int retval;
eb5d44eb 1756
d62a17ae 1757 circuit = THREAD_ARG(thread);
1758 assert(circuit);
1759 circuit->u.bc.t_send_lan_hello[1] = NULL;
eb5d44eb 1760
d62a17ae 1761 if (!(circuit->area->is_type & IS_LEVEL_2)) {
1762 zlog_warn("ISIS-Hello (%s): Trying to send L2 IIH in L1 area",
1763 circuit->area->area_tag);
1764 return 1;
1765 }
ddfdbd32 1766
d62a17ae 1767 if (circuit->u.bc.run_dr_elect[1])
1768 isis_dr_elect(circuit, 2);
eb5d44eb 1769
d62a17ae 1770 retval = send_hello(circuit, 2);
eb5d44eb 1771
d62a17ae 1772 /* set next timer thread */
1773 thread_add_timer(master, send_lan_l2_hello, circuit,
1774 isis_jitter(circuit->hello_interval[1], IIH_JITTER),
1775 &circuit->u.bc.t_send_lan_hello[1]);
eb5d44eb 1776
d62a17ae 1777 return retval;
eb5d44eb 1778}
1779
d62a17ae 1780int send_p2p_hello(struct thread *thread)
eb5d44eb 1781{
d62a17ae 1782 struct isis_circuit *circuit;
eb5d44eb 1783
d62a17ae 1784 circuit = THREAD_ARG(thread);
1785 assert(circuit);
1786 circuit->u.p2p.t_send_p2p_hello = NULL;
eb5d44eb 1787
d62a17ae 1788 send_hello(circuit, 1);
eb5d44eb 1789
d62a17ae 1790 /* set next timer thread */
1791 thread_add_timer(master, send_p2p_hello, circuit,
1792 isis_jitter(circuit->hello_interval[1], IIH_JITTER),
1793 &circuit->u.p2p.t_send_p2p_hello);
eb5d44eb 1794
d62a17ae 1795 return ISIS_OK;
eb5d44eb 1796}
1797
3f045a08
JB
1798/*
1799 * Count the maximum number of lsps that can be accomodated by a given size.
1800 */
af8ac8f9 1801#define LSP_ENTRIES_LEN (10 + ISIS_SYS_ID_LEN)
d62a17ae 1802static uint16_t get_max_lsp_count(uint16_t size)
3f045a08 1803{
d62a17ae 1804 uint16_t tlv_count;
1805 uint16_t lsp_count;
1806 uint16_t remaining_size;
3f045a08 1807
d62a17ae 1808 /* First count the full size TLVs */
1809 tlv_count = size / MAX_LSP_ENTRIES_TLV_SIZE;
1810 lsp_count = tlv_count * (MAX_LSP_ENTRIES_TLV_SIZE / LSP_ENTRIES_LEN);
3f045a08 1811
d62a17ae 1812 /* The last TLV, if any */
1813 remaining_size = size % MAX_LSP_ENTRIES_TLV_SIZE;
1814 if (remaining_size - 2 >= LSP_ENTRIES_LEN)
1815 lsp_count += (remaining_size - 2) / LSP_ENTRIES_LEN;
3f045a08 1816
d62a17ae 1817 return lsp_count;
3f045a08
JB
1818}
1819
17c9dcd5 1820int send_csnp(struct isis_circuit *circuit, int level)
3f045a08 1821{
17c9dcd5
CF
1822 if (circuit->area->lspdb[level - 1] == NULL
1823 || dict_count(circuit->area->lspdb[level - 1]) == 0)
1824 return ISIS_OK;
d62a17ae 1825
17c9dcd5
CF
1826 isis_circuit_stream(circuit, &circuit->snd_stream);
1827 fill_fixed_hdr((level == ISIS_LEVEL1) ? L1_COMPLETE_SEQ_NUM
1828 : L2_COMPLETE_SEQ_NUM,
1829 circuit->snd_stream);
3f045a08 1830
17c9dcd5
CF
1831 size_t len_pointer = stream_get_endp(circuit->snd_stream);
1832 stream_putw(circuit->snd_stream, 0);
1833 stream_put(circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
1834 /* with zero circuit id - ref 9.10, 9.11 */
1835 stream_putc(circuit->snd_stream, 0);
3f045a08 1836
17c9dcd5
CF
1837 size_t start_pointer = stream_get_endp(circuit->snd_stream);
1838 stream_put(circuit->snd_stream, 0, ISIS_SYS_ID_LEN + 2);
1839 size_t end_pointer = stream_get_endp(circuit->snd_stream);
1840 stream_put(circuit->snd_stream, 0, ISIS_SYS_ID_LEN + 2);
d62a17ae 1841
17c9dcd5
CF
1842 struct isis_passwd *passwd = (level == ISIS_LEVEL1)
1843 ? &circuit->area->area_passwd
1844 : &circuit->area->domain_passwd;
d62a17ae 1845
17c9dcd5 1846 struct isis_tlvs *tlvs = isis_alloc_tlvs();
3f045a08 1847
17c9dcd5
CF
1848 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
1849 isis_tlvs_add_auth(tlvs, passwd);
eb5d44eb 1850
17c9dcd5 1851 size_t tlv_start = stream_get_endp(circuit->snd_stream);
af8ac8f9
CF
1852 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer, false,
1853 false)) {
17c9dcd5
CF
1854 isis_free_tlvs(tlvs);
1855 return ISIS_WARNING;
1856 }
1857 isis_free_tlvs(tlvs);
d62a17ae 1858
17c9dcd5
CF
1859 uint16_t num_lsps =
1860 get_max_lsp_count(STREAM_WRITEABLE(circuit->snd_stream));
d62a17ae 1861
17c9dcd5 1862 uint8_t start[ISIS_SYS_ID_LEN + 2];
d62a17ae 1863 memset(start, 0x00, ISIS_SYS_ID_LEN + 2);
17c9dcd5 1864 uint8_t stop[ISIS_SYS_ID_LEN + 2];
d62a17ae 1865 memset(stop, 0xff, ISIS_SYS_ID_LEN + 2);
1866
17c9dcd5 1867 bool loop = true;
d62a17ae 1868 while (loop) {
17c9dcd5
CF
1869 tlvs = isis_alloc_tlvs();
1870 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
1871 isis_tlvs_add_auth(tlvs, passwd);
1872
1873 struct isis_lsp *last_lsp;
1874 isis_tlvs_add_csnp_entries(tlvs, start, stop, num_lsps,
1875 circuit->area->lspdb[level - 1],
1876 &last_lsp);
d62a17ae 1877 /*
1878 * Update the stop lsp_id before encoding this CSNP.
1879 */
17c9dcd5 1880 if (tlvs->lsp_entries.count < num_lsps) {
d62a17ae 1881 memset(stop, 0xff, ISIS_SYS_ID_LEN + 2);
1882 } else {
af8ac8f9 1883 memcpy(stop, last_lsp->hdr.lsp_id, sizeof(stop));
d62a17ae 1884 }
3f045a08 1885
17c9dcd5
CF
1886 memcpy(STREAM_DATA(circuit->snd_stream) + start_pointer, start,
1887 ISIS_SYS_ID_LEN + 2);
1888 memcpy(STREAM_DATA(circuit->snd_stream) + end_pointer, stop,
1889 ISIS_SYS_ID_LEN + 2);
1890 stream_set_endp(circuit->snd_stream, tlv_start);
1891 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer,
af8ac8f9 1892 false, false)) {
17c9dcd5
CF
1893 isis_free_tlvs(tlvs);
1894 return ISIS_WARNING;
d62a17ae 1895 }
eb5d44eb 1896
d62a17ae 1897 if (isis->debugs & DEBUG_SNP_PACKETS) {
1898 zlog_debug(
1899 "ISIS-Snp (%s): Sending L%d CSNP on %s, length %zd",
1900 circuit->area->area_tag, level,
1901 circuit->interface->name,
1902 stream_get_endp(circuit->snd_stream));
17c9dcd5
CF
1903 log_multiline(LOG_DEBUG, " ", "%s",
1904 isis_format_tlvs(tlvs));
d62a17ae 1905 if (isis->debugs & DEBUG_PACKET_DUMP)
1906 zlog_dump_data(
1907 STREAM_DATA(circuit->snd_stream),
1908 stream_get_endp(circuit->snd_stream));
1909 }
eb5d44eb 1910
17c9dcd5 1911 int retval = circuit->tx(circuit, level);
d62a17ae 1912 if (retval != ISIS_OK) {
af4c2728 1913 flog_err(ISIS_ERR_PACKET,
54ece698
DS
1914 "ISIS-Snp (%s): Send L%d CSNP on %s failed",
1915 circuit->area->area_tag, level,
1916 circuit->interface->name);
17c9dcd5 1917 isis_free_tlvs(tlvs);
d62a17ae 1918 return retval;
1919 }
3f045a08 1920
d62a17ae 1921 /*
1922 * Start lsp_id of the next CSNP should be one plus the
1923 * stop lsp_id in this current CSNP.
1924 */
1925 memcpy(start, stop, ISIS_SYS_ID_LEN + 2);
1926 loop = 0;
17c9dcd5 1927 for (int i = ISIS_SYS_ID_LEN + 1; i >= 0; --i) {
d7c0a89a 1928 if (start[i] < (uint8_t)0xff) {
d62a17ae 1929 start[i] += 1;
1930 loop = 1;
1931 break;
1932 }
1933 }
1934 memset(stop, 0xff, ISIS_SYS_ID_LEN + 2);
17c9dcd5 1935 isis_free_tlvs(tlvs);
d62a17ae 1936 }
3f045a08 1937
17c9dcd5 1938 return ISIS_OK;
eb5d44eb 1939}
1940
d62a17ae 1941int send_l1_csnp(struct thread *thread)
eb5d44eb 1942{
d62a17ae 1943 struct isis_circuit *circuit;
1944 int retval = ISIS_OK;
eb5d44eb 1945
d62a17ae 1946 circuit = THREAD_ARG(thread);
1947 assert(circuit);
eb5d44eb 1948
d62a17ae 1949 circuit->t_send_csnp[0] = NULL;
eb5d44eb 1950
8e6fb83b
CF
1951 if ((circuit->circ_type == CIRCUIT_T_BROADCAST
1952 && circuit->u.bc.is_dr[0])
1953 || circuit->circ_type == CIRCUIT_T_P2P) {
d62a17ae 1954 send_csnp(circuit, 1);
1955 }
1956 /* set next timer thread */
1957 thread_add_timer(master, send_l1_csnp, circuit,
1958 isis_jitter(circuit->csnp_interval[0], CSNP_JITTER),
1959 &circuit->t_send_csnp[0]);
eb5d44eb 1960
d62a17ae 1961 return retval;
eb5d44eb 1962}
1963
d62a17ae 1964int send_l2_csnp(struct thread *thread)
eb5d44eb 1965{
d62a17ae 1966 struct isis_circuit *circuit;
1967 int retval = ISIS_OK;
eb5d44eb 1968
d62a17ae 1969 circuit = THREAD_ARG(thread);
1970 assert(circuit);
eb5d44eb 1971
d62a17ae 1972 circuit->t_send_csnp[1] = NULL;
eb5d44eb 1973
8e6fb83b
CF
1974 if ((circuit->circ_type == CIRCUIT_T_BROADCAST
1975 && circuit->u.bc.is_dr[1])
1976 || circuit->circ_type == CIRCUIT_T_P2P) {
d62a17ae 1977 send_csnp(circuit, 2);
1978 }
1979 /* set next timer thread */
1980 thread_add_timer(master, send_l2_csnp, circuit,
1981 isis_jitter(circuit->csnp_interval[1], CSNP_JITTER),
1982 &circuit->t_send_csnp[1]);
d70f99e1 1983
d62a17ae 1984 return retval;
eb5d44eb 1985}
1986
eb5d44eb 1987/*
1988 * 7.3.15.4 action on expiration of partial SNP interval
1989 * level 1
1990 */
d62a17ae 1991static int send_psnp(int level, struct isis_circuit *circuit)
eb5d44eb 1992{
d62a17ae 1993 if (circuit->circ_type == CIRCUIT_T_BROADCAST
1994 && circuit->u.bc.is_dr[level - 1])
1995 return ISIS_OK;
eb5d44eb 1996
d62a17ae 1997 if (circuit->area->lspdb[level - 1] == NULL
1998 || dict_count(circuit->area->lspdb[level - 1]) == 0)
1999 return ISIS_OK;
f390d2c7 2000
d62a17ae 2001 if (!circuit->snd_stream)
2002 return ISIS_ERROR;
e38e0df0 2003
17c9dcd5
CF
2004 isis_circuit_stream(circuit, &circuit->snd_stream);
2005 fill_fixed_hdr((level == ISIS_LEVEL1) ? L1_PARTIAL_SEQ_NUM
2006 : L2_PARTIAL_SEQ_NUM,
2007 circuit->snd_stream);
2008
2009 size_t len_pointer = stream_get_endp(circuit->snd_stream);
2010 stream_putw(circuit->snd_stream, 0); /* length is filled in later */
2011 stream_put(circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
2012 stream_putc(circuit->snd_stream, circuit->idx);
2013
2014 struct isis_passwd *passwd = (level == ISIS_LEVEL1)
2015 ? &circuit->area->area_passwd
2016 : &circuit->area->domain_passwd;
2017
2018 struct isis_tlvs *tlvs = isis_alloc_tlvs();
2019
2020 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
2021 isis_tlvs_add_auth(tlvs, passwd);
2022
2023 size_t tlv_start = stream_get_endp(circuit->snd_stream);
af8ac8f9
CF
2024 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer, false,
2025 false)) {
17c9dcd5
CF
2026 isis_free_tlvs(tlvs);
2027 return ISIS_WARNING;
2028 }
2029 isis_free_tlvs(tlvs);
2030
2031 uint16_t num_lsps =
2032 get_max_lsp_count(STREAM_WRITEABLE(circuit->snd_stream));
f390d2c7 2033
d62a17ae 2034 while (1) {
17c9dcd5
CF
2035 tlvs = isis_alloc_tlvs();
2036 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
2037 isis_tlvs_add_auth(tlvs, passwd);
2038
2039 for (dnode_t *dnode =
2040 dict_first(circuit->area->lspdb[level - 1]);
2041 dnode; dnode = dict_next(circuit->area->lspdb[level - 1],
2042 dnode)) {
2043 struct isis_lsp *lsp = dnode_get(dnode);
2044
2045 if (ISIS_CHECK_FLAG(lsp->SSNflags, circuit))
2046 isis_tlvs_add_lsp_entry(tlvs, lsp);
eb5d44eb 2047
17c9dcd5
CF
2048 if (tlvs->lsp_entries.count == num_lsps)
2049 break;
2050 }
2051
2052 if (!tlvs->lsp_entries.count) {
2053 isis_free_tlvs(tlvs);
d62a17ae 2054 return ISIS_OK;
2055 }
2056
17c9dcd5
CF
2057 stream_set_endp(circuit->snd_stream, tlv_start);
2058 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer,
af8ac8f9 2059 false, false)) {
17c9dcd5
CF
2060 isis_free_tlvs(tlvs);
2061 return ISIS_WARNING;
d62a17ae 2062 }
2063
2064 if (isis->debugs & DEBUG_SNP_PACKETS) {
2065 zlog_debug(
2066 "ISIS-Snp (%s): Sending L%d PSNP on %s, length %zd",
2067 circuit->area->area_tag, level,
2068 circuit->interface->name,
2069 stream_get_endp(circuit->snd_stream));
17c9dcd5
CF
2070 log_multiline(LOG_DEBUG, " ", "%s",
2071 isis_format_tlvs(tlvs));
d62a17ae 2072 if (isis->debugs & DEBUG_PACKET_DUMP)
2073 zlog_dump_data(
2074 STREAM_DATA(circuit->snd_stream),
2075 stream_get_endp(circuit->snd_stream));
2076 }
2077
17c9dcd5 2078 int retval = circuit->tx(circuit, level);
d62a17ae 2079 if (retval != ISIS_OK) {
af4c2728 2080 flog_err(ISIS_ERR_PACKET,
54ece698
DS
2081 "ISIS-Snp (%s): Send L%d PSNP on %s failed",
2082 circuit->area->area_tag, level,
2083 circuit->interface->name);
17c9dcd5 2084 isis_free_tlvs(tlvs);
d62a17ae 2085 return retval;
2086 }
2087
2088 /*
2089 * sending succeeded, we can clear SSN flags of this circuit
2090 * for the LSPs in list
2091 */
17c9dcd5
CF
2092 struct isis_lsp_entry *entry_head;
2093 entry_head = (struct isis_lsp_entry *)tlvs->lsp_entries.head;
2094 for (struct isis_lsp_entry *entry = entry_head; entry;
2095 entry = entry->next)
2096 ISIS_CLEAR_FLAG(entry->lsp->SSNflags, circuit);
2097 isis_free_tlvs(tlvs);
d62a17ae 2098 }
2099
17c9dcd5 2100 return ISIS_OK;
eb5d44eb 2101}
2102
d62a17ae 2103int send_l1_psnp(struct thread *thread)
eb5d44eb 2104{
2105
d62a17ae 2106 struct isis_circuit *circuit;
2107 int retval = ISIS_OK;
eb5d44eb 2108
d62a17ae 2109 circuit = THREAD_ARG(thread);
2110 assert(circuit);
eb5d44eb 2111
d62a17ae 2112 circuit->t_send_psnp[0] = NULL;
eb5d44eb 2113
d62a17ae 2114 send_psnp(1, circuit);
2115 /* set next timer thread */
2116 thread_add_timer(master, send_l1_psnp, circuit,
2117 isis_jitter(circuit->psnp_interval[0], PSNP_JITTER),
2118 &circuit->t_send_psnp[0]);
eb5d44eb 2119
d62a17ae 2120 return retval;
eb5d44eb 2121}
2122
2123/*
2124 * 7.3.15.4 action on expiration of partial SNP interval
2125 * level 2
2126 */
d62a17ae 2127int send_l2_psnp(struct thread *thread)
eb5d44eb 2128{
d62a17ae 2129 struct isis_circuit *circuit;
2130 int retval = ISIS_OK;
eb5d44eb 2131
d62a17ae 2132 circuit = THREAD_ARG(thread);
2133 assert(circuit);
eb5d44eb 2134
d62a17ae 2135 circuit->t_send_psnp[1] = NULL;
eb5d44eb 2136
d62a17ae 2137 send_psnp(2, circuit);
eb5d44eb 2138
d62a17ae 2139 /* set next timer thread */
2140 thread_add_timer(master, send_l2_psnp, circuit,
2141 isis_jitter(circuit->psnp_interval[1], PSNP_JITTER),
2142 &circuit->t_send_psnp[1]);
eb5d44eb 2143
d62a17ae 2144 return retval;
eb5d44eb 2145}
2146
eb5d44eb 2147/*
2148 * ISO 10589 - 7.3.14.3
2149 */
9b39405f 2150void send_lsp(void *arg, struct isis_lsp *lsp, enum isis_tx_type tx_type)
eb5d44eb 2151{
9b39405f 2152 struct isis_circuit *circuit = arg;
d62a17ae 2153 int clear_srm = 1;
2154 int retval = ISIS_OK;
2155
d62a17ae 2156 if (circuit->state != C_STATE_UP || circuit->is_passive == 1)
2157 goto out;
2158
2159 /*
2160 * Do not send if levels do not match
2161 */
2162 if (!(lsp->level & circuit->is_type))
2163 goto out;
2164
2165 /*
2166 * Do not send if we do not have adjacencies in state up on the circuit
2167 */
2168 if (circuit->upadjcount[lsp->level - 1] == 0)
2169 goto out;
2170
2171 /* stream_copy will assert and stop program execution if LSP is larger
2172 * than
2173 * the circuit's MTU. So handle and log this case here. */
2174 if (stream_get_endp(lsp->pdu) > stream_get_size(circuit->snd_stream)) {
af4c2728 2175 flog_err(
54ece698 2176 ISIS_ERR_PACKET,
af8ac8f9
CF
2177 "ISIS-Upd (%s): Can't send L%d LSP %s, seq 0x%08" PRIx32
2178 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
2179 "s on %s. LSP Size is %zu while interface stream size is %zu.",
d62a17ae 2180 circuit->area->area_tag, lsp->level,
af8ac8f9
CF
2181 rawlspid_print(lsp->hdr.lsp_id), lsp->hdr.seqno,
2182 lsp->hdr.checksum, lsp->hdr.rem_lifetime,
d62a17ae 2183 circuit->interface->name, stream_get_endp(lsp->pdu),
2184 stream_get_size(circuit->snd_stream));
2185 if (isis->debugs & DEBUG_PACKET_DUMP)
2186 zlog_dump_data(STREAM_DATA(lsp->pdu),
2187 stream_get_endp(lsp->pdu));
2188 retval = ISIS_ERROR;
2189 goto out;
2190 }
eb5d44eb 2191
d62a17ae 2192 /* copy our lsp to the send buffer */
2193 stream_copy(circuit->snd_stream, lsp->pdu);
2194
1cbd5b37
CF
2195 if (tx_type == TX_LSP_CIRCUIT_SCOPED) {
2196 stream_putc_at(circuit->snd_stream, 4, FS_LINK_STATE);
2197 stream_putc_at(circuit->snd_stream, 7, L2_CIRCUIT_FLOODING_SCOPE);
2198 }
2199
d62a17ae 2200 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
af8ac8f9
CF
2201 zlog_debug("ISIS-Upd (%s): Sending L%d LSP %s, seq 0x%08" PRIx32
2202 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
2203 "s on %s",
2204 circuit->area->area_tag, lsp->level,
2205 rawlspid_print(lsp->hdr.lsp_id), lsp->hdr.seqno,
2206 lsp->hdr.checksum, lsp->hdr.rem_lifetime,
2207 circuit->interface->name);
d62a17ae 2208 if (isis->debugs & DEBUG_PACKET_DUMP)
2209 zlog_dump_data(STREAM_DATA(circuit->snd_stream),
2210 stream_get_endp(circuit->snd_stream));
2211 }
3f045a08 2212
d62a17ae 2213 clear_srm = 0;
2214 retval = circuit->tx(circuit, lsp->level);
2215 if (retval != ISIS_OK) {
af4c2728 2216 flog_err(ISIS_ERR_PACKET,
54ece698
DS
2217 "ISIS-Upd (%s): Send L%d LSP on %s failed %s",
2218 circuit->area->area_tag, lsp->level,
2219 circuit->interface->name,
2220 (retval == ISIS_WARNING) ? "temporarily"
2221 : "permanently");
d62a17ae 2222 }
f390d2c7 2223
cfd1f27b 2224out:
d62a17ae 2225 if (clear_srm
2226 || (retval == ISIS_OK && circuit->circ_type == CIRCUIT_T_BROADCAST)
2227 || (retval != ISIS_OK && retval != ISIS_WARNING)) {
2228 /* SRM flag will trigger retransmission. We will not retransmit
2229 * if we
2230 * encountered a fatal error.
2231 * On success, they should only be cleared if it's a broadcast
2232 * circuit.
2233 * On a P2P circuit, we will wait for the ack from the neighbor
2234 * to clear
2235 * the fag.
2236 */
9b39405f 2237 isis_tx_queue_del(circuit->tx_queue, lsp);
d62a17ae 2238 }
f390d2c7 2239}