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