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