]> git.proxmox.com Git - mirror_frr.git/blame - isisd/isis_pdu.c
tools, doc: update checkpatch for u_int_*
[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
b9347997 1340int 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
b9347997 1463#if ISIS_METHOD != ISIS_METHOD_BPF
d62a17ae 1464 if (retval == ISIS_OK)
1465 retval = isis_handle_pdu(circuit, ssnpa);
b9347997 1466#endif //ISIS_METHOD != ISIS_METHOD_BPF
eb5d44eb 1467
d62a17ae 1468 /*
1469 * prepare for next packet.
1470 */
1471 if (!circuit->is_passive)
1472 isis_circuit_prepare(circuit);
eb5d44eb 1473
d62a17ae 1474 return retval;
eb5d44eb 1475}
1476
eb5d44eb 1477/*
d62a17ae 1478 * SEND SIDE
eb5d44eb 1479 */
88f9d911 1480void fill_fixed_hdr(uint8_t pdu_type, struct stream *stream)
eb5d44eb 1481{
88f9d911
CF
1482 uint8_t length;
1483
1484 if (pdu_size(pdu_type, &length))
1485 assert(!"Unknown PDU Type");
1486
1487 stream_putc(stream, ISO10589_ISIS); /* IDRP */
1488 stream_putc(stream, length); /* Length of fixed header */
1489 stream_putc(stream, 1); /* Version/Protocol ID Extension 1 */
1490 stream_putc(stream, 0); /* ID Length, 0 => 6 */
1491 stream_putc(stream, pdu_type);
1492 stream_putc(stream, 1); /* Subversion */
1493 stream_putc(stream, 0); /* Reserved */
1494 stream_putc(stream, 0); /* Max Area Addresses 0 => 3 */
eb5d44eb 1495}
1496
0c1bd758
CF
1497static void put_hello_hdr(struct isis_circuit *circuit, int level,
1498 size_t *len_pointer)
eb5d44eb 1499{
88f9d911
CF
1500 uint8_t pdu_type;
1501
d62a17ae 1502 if (circuit->circ_type == CIRCUIT_T_BROADCAST)
88f9d911 1503 pdu_type = (level == IS_LEVEL_1) ? L1_LAN_HELLO : L2_LAN_HELLO;
d62a17ae 1504 else
88f9d911
CF
1505 pdu_type = P2P_HELLO;
1506
0c1bd758 1507 isis_circuit_stream(circuit, &circuit->snd_stream);
88f9d911 1508 fill_fixed_hdr(pdu_type, circuit->snd_stream);
d62a17ae 1509
0c1bd758
CF
1510 stream_putc(circuit->snd_stream, circuit->is_type);
1511 stream_put(circuit->snd_stream, circuit->area->isis->sysid,
1512 ISIS_SYS_ID_LEN);
eb5d44eb 1513
0c1bd758
CF
1514 uint32_t holdtime = circuit->hello_multiplier[level - 1]
1515 * circuit->hello_interval[level - 1];
d62a17ae 1516
0c1bd758
CF
1517 if (holdtime > 0xffff)
1518 holdtime = 0xffff;
d62a17ae 1519
0c1bd758
CF
1520 stream_putw(circuit->snd_stream, holdtime);
1521 *len_pointer = stream_get_endp(circuit->snd_stream);
1522 stream_putw(circuit->snd_stream, 0); /* length is filled in later */
d62a17ae 1523
0c1bd758
CF
1524 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
1525 u_char *desig_is = (level == IS_LEVEL_1)
1526 ? circuit->u.bc.l1_desig_is
1527 : circuit->u.bc.l2_desig_is;
1528 stream_putc(circuit->snd_stream, circuit->priority[level - 1]);
1529 stream_put(circuit->snd_stream, desig_is, ISIS_SYS_ID_LEN + 1);
1530 } else {
1531 stream_putc(circuit->snd_stream, circuit->circuit_id);
d62a17ae 1532 }
0c1bd758 1533}
eb5d44eb 1534
0c1bd758
CF
1535int send_hello(struct isis_circuit *circuit, int level)
1536{
1537 size_t len_pointer;
1538 int retval;
1539
1540 if (circuit->is_passive)
1541 return ISIS_OK;
1542
1543 if (circuit->interface->mtu == 0) {
1544 zlog_warn("circuit has zero MTU");
d62a17ae 1545 return ISIS_WARNING;
0c1bd758
CF
1546 }
1547
1548 put_hello_hdr(circuit, level, &len_pointer);
1549
1550 struct isis_tlvs *tlvs = isis_alloc_tlvs();
1551
1552 isis_tlvs_add_auth(tlvs, &circuit->passwd);
1553
a3acf37d
DS
1554 if (!listcount(circuit->area->area_addrs)) {
1555 isis_free_tlvs(tlvs);
d62a17ae 1556 return ISIS_WARNING;
a3acf37d
DS
1557 }
1558
0c1bd758 1559 isis_tlvs_add_area_addresses(tlvs, circuit->area->area_addrs);
d62a17ae 1560
42fe2621 1561 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
0c1bd758
CF
1562 isis_tlvs_add_lan_neighbors(
1563 tlvs, circuit->u.bc.lan_neighs[level - 1]);
58e5d748
CF
1564 } else if (circuit->circ_type == CIRCUIT_T_P2P
1565 && !circuit->disable_threeway_adj) {
42fe2621
CF
1566 uint32_t ext_circuit_id = circuit->idx;
1567 if (circuit->u.p2p.neighbor) {
1568 isis_tlvs_add_threeway_adj(tlvs,
1569 circuit->u.p2p.neighbor->threeway_state,
1570 ext_circuit_id,
1571 circuit->u.p2p.neighbor->sysid,
1572 circuit->u.p2p.neighbor->ext_circuit_id);
1573 } else {
1574 isis_tlvs_add_threeway_adj(tlvs,
1575 ISIS_THREEWAY_DOWN,
1576 ext_circuit_id,
1577 NULL, 0);
1578 }
1579 }
0c1bd758
CF
1580
1581 isis_tlvs_set_protocols_supported(tlvs, &circuit->nlpids);
d62a17ae 1582
1583 /*
1584 * MT Supported TLV
1585 *
1586 * TLV gets included if no topology is enabled on the interface,
1587 * if one topology other than #0 is enabled, or if multiple topologies
1588 * are enabled.
1589 */
1590 struct isis_circuit_mt_setting **mt_settings;
1591 unsigned int mt_count;
1592
1593 mt_settings = circuit_mt_settings(circuit, &mt_count);
0c1bd758
CF
1594 if (mt_count == 0 && area_is_mt(circuit->area)) {
1595 tlvs->mt_router_info_empty = true;
1596 } else if ((mt_count == 1
1597 && mt_settings[0]->mtid != ISIS_MT_IPV4_UNICAST)
1598 || (mt_count > 1)) {
1599 for (unsigned int i = 0; i < mt_count; i++)
1600 isis_tlvs_add_mt_router_info(tlvs, mt_settings[i]->mtid,
1601 false, false);
d62a17ae 1602 }
eb5d44eb 1603
0c1bd758
CF
1604 if (circuit->ip_router && circuit->ip_addrs)
1605 isis_tlvs_add_ipv4_addresses(tlvs, circuit->ip_addrs);
d62a17ae 1606
0c1bd758
CF
1607 if (circuit->ipv6_router && circuit->ipv6_link)
1608 isis_tlvs_add_ipv6_addresses(tlvs, circuit->ipv6_link);
d62a17ae 1609
0c1bd758 1610 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer,
af8ac8f9 1611 circuit->pad_hellos, false)) {
0c1bd758
CF
1612 isis_free_tlvs(tlvs);
1613 return ISIS_WARNING; /* XXX: Maybe Log TLV structure? */
f390d2c7 1614 }
d62a17ae 1615
1616 if (isis->debugs & DEBUG_ADJ_PACKETS) {
1617 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
1618 zlog_debug(
1619 "ISIS-Adj (%s): Sending L%d LAN IIH on %s, length %zd",
1620 circuit->area->area_tag, level,
0c1bd758
CF
1621 circuit->interface->name,
1622 stream_get_endp(circuit->snd_stream));
d62a17ae 1623 } else {
1624 zlog_debug(
1625 "ISIS-Adj (%s): Sending P2P IIH on %s, length %zd",
1626 circuit->area->area_tag,
0c1bd758
CF
1627 circuit->interface->name,
1628 stream_get_endp(circuit->snd_stream));
d62a17ae 1629 }
1630 if (isis->debugs & DEBUG_PACKET_DUMP)
1631 zlog_dump_data(STREAM_DATA(circuit->snd_stream),
1632 stream_get_endp(circuit->snd_stream));
f390d2c7 1633 }
eb5d44eb 1634
0c1bd758
CF
1635 isis_free_tlvs(tlvs);
1636
d62a17ae 1637 retval = circuit->tx(circuit, level);
1638 if (retval != ISIS_OK)
1639 zlog_err("ISIS-Adj (%s): Send L%d IIH on %s failed",
1640 circuit->area->area_tag, level,
1641 circuit->interface->name);
eb5d44eb 1642
d62a17ae 1643 return retval;
eb5d44eb 1644}
1645
d62a17ae 1646int send_lan_l1_hello(struct thread *thread)
eb5d44eb 1647{
d62a17ae 1648 struct isis_circuit *circuit;
1649 int retval;
1650
1651 circuit = THREAD_ARG(thread);
1652 assert(circuit);
1653 circuit->u.bc.t_send_lan_hello[0] = NULL;
1654
1655 if (!(circuit->area->is_type & IS_LEVEL_1)) {
1656 zlog_warn(
1657 "ISIS-Hello (%s): Trying to send L1 IIH in L2-only area",
1658 circuit->area->area_tag);
1659 return 1;
1660 }
ddfdbd32 1661
d62a17ae 1662 if (circuit->u.bc.run_dr_elect[0])
1663 isis_dr_elect(circuit, 1);
eb5d44eb 1664
d62a17ae 1665 retval = send_hello(circuit, 1);
eb5d44eb 1666
d62a17ae 1667 /* set next timer thread */
1668 thread_add_timer(master, send_lan_l1_hello, circuit,
1669 isis_jitter(circuit->hello_interval[0], IIH_JITTER),
1670 &circuit->u.bc.t_send_lan_hello[0]);
eb5d44eb 1671
d62a17ae 1672 return retval;
eb5d44eb 1673}
1674
d62a17ae 1675int send_lan_l2_hello(struct thread *thread)
eb5d44eb 1676{
d62a17ae 1677 struct isis_circuit *circuit;
1678 int retval;
eb5d44eb 1679
d62a17ae 1680 circuit = THREAD_ARG(thread);
1681 assert(circuit);
1682 circuit->u.bc.t_send_lan_hello[1] = NULL;
eb5d44eb 1683
d62a17ae 1684 if (!(circuit->area->is_type & IS_LEVEL_2)) {
1685 zlog_warn("ISIS-Hello (%s): Trying to send L2 IIH in L1 area",
1686 circuit->area->area_tag);
1687 return 1;
1688 }
ddfdbd32 1689
d62a17ae 1690 if (circuit->u.bc.run_dr_elect[1])
1691 isis_dr_elect(circuit, 2);
eb5d44eb 1692
d62a17ae 1693 retval = send_hello(circuit, 2);
eb5d44eb 1694
d62a17ae 1695 /* set next timer thread */
1696 thread_add_timer(master, send_lan_l2_hello, circuit,
1697 isis_jitter(circuit->hello_interval[1], IIH_JITTER),
1698 &circuit->u.bc.t_send_lan_hello[1]);
eb5d44eb 1699
d62a17ae 1700 return retval;
eb5d44eb 1701}
1702
d62a17ae 1703int send_p2p_hello(struct thread *thread)
eb5d44eb 1704{
d62a17ae 1705 struct isis_circuit *circuit;
eb5d44eb 1706
d62a17ae 1707 circuit = THREAD_ARG(thread);
1708 assert(circuit);
1709 circuit->u.p2p.t_send_p2p_hello = NULL;
eb5d44eb 1710
d62a17ae 1711 send_hello(circuit, 1);
eb5d44eb 1712
d62a17ae 1713 /* set next timer thread */
1714 thread_add_timer(master, send_p2p_hello, circuit,
1715 isis_jitter(circuit->hello_interval[1], IIH_JITTER),
1716 &circuit->u.p2p.t_send_p2p_hello);
eb5d44eb 1717
d62a17ae 1718 return ISIS_OK;
eb5d44eb 1719}
1720
3f045a08
JB
1721/*
1722 * Count the maximum number of lsps that can be accomodated by a given size.
1723 */
af8ac8f9 1724#define LSP_ENTRIES_LEN (10 + ISIS_SYS_ID_LEN)
d62a17ae 1725static uint16_t get_max_lsp_count(uint16_t size)
3f045a08 1726{
d62a17ae 1727 uint16_t tlv_count;
1728 uint16_t lsp_count;
1729 uint16_t remaining_size;
3f045a08 1730
d62a17ae 1731 /* First count the full size TLVs */
1732 tlv_count = size / MAX_LSP_ENTRIES_TLV_SIZE;
1733 lsp_count = tlv_count * (MAX_LSP_ENTRIES_TLV_SIZE / LSP_ENTRIES_LEN);
3f045a08 1734
d62a17ae 1735 /* The last TLV, if any */
1736 remaining_size = size % MAX_LSP_ENTRIES_TLV_SIZE;
1737 if (remaining_size - 2 >= LSP_ENTRIES_LEN)
1738 lsp_count += (remaining_size - 2) / LSP_ENTRIES_LEN;
3f045a08 1739
d62a17ae 1740 return lsp_count;
3f045a08
JB
1741}
1742
17c9dcd5 1743int send_csnp(struct isis_circuit *circuit, int level)
3f045a08 1744{
17c9dcd5
CF
1745 if (circuit->area->lspdb[level - 1] == NULL
1746 || dict_count(circuit->area->lspdb[level - 1]) == 0)
1747 return ISIS_OK;
d62a17ae 1748
17c9dcd5
CF
1749 isis_circuit_stream(circuit, &circuit->snd_stream);
1750 fill_fixed_hdr((level == ISIS_LEVEL1) ? L1_COMPLETE_SEQ_NUM
1751 : L2_COMPLETE_SEQ_NUM,
1752 circuit->snd_stream);
3f045a08 1753
17c9dcd5
CF
1754 size_t len_pointer = stream_get_endp(circuit->snd_stream);
1755 stream_putw(circuit->snd_stream, 0);
1756 stream_put(circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
1757 /* with zero circuit id - ref 9.10, 9.11 */
1758 stream_putc(circuit->snd_stream, 0);
3f045a08 1759
17c9dcd5
CF
1760 size_t start_pointer = stream_get_endp(circuit->snd_stream);
1761 stream_put(circuit->snd_stream, 0, ISIS_SYS_ID_LEN + 2);
1762 size_t end_pointer = stream_get_endp(circuit->snd_stream);
1763 stream_put(circuit->snd_stream, 0, ISIS_SYS_ID_LEN + 2);
d62a17ae 1764
17c9dcd5
CF
1765 struct isis_passwd *passwd = (level == ISIS_LEVEL1)
1766 ? &circuit->area->area_passwd
1767 : &circuit->area->domain_passwd;
d62a17ae 1768
17c9dcd5 1769 struct isis_tlvs *tlvs = isis_alloc_tlvs();
3f045a08 1770
17c9dcd5
CF
1771 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
1772 isis_tlvs_add_auth(tlvs, passwd);
eb5d44eb 1773
17c9dcd5 1774 size_t tlv_start = stream_get_endp(circuit->snd_stream);
af8ac8f9
CF
1775 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer, false,
1776 false)) {
17c9dcd5
CF
1777 isis_free_tlvs(tlvs);
1778 return ISIS_WARNING;
1779 }
1780 isis_free_tlvs(tlvs);
d62a17ae 1781
17c9dcd5
CF
1782 uint16_t num_lsps =
1783 get_max_lsp_count(STREAM_WRITEABLE(circuit->snd_stream));
d62a17ae 1784
17c9dcd5 1785 uint8_t start[ISIS_SYS_ID_LEN + 2];
d62a17ae 1786 memset(start, 0x00, ISIS_SYS_ID_LEN + 2);
17c9dcd5 1787 uint8_t stop[ISIS_SYS_ID_LEN + 2];
d62a17ae 1788 memset(stop, 0xff, ISIS_SYS_ID_LEN + 2);
1789
17c9dcd5 1790 bool loop = true;
d62a17ae 1791 while (loop) {
17c9dcd5
CF
1792 tlvs = isis_alloc_tlvs();
1793 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
1794 isis_tlvs_add_auth(tlvs, passwd);
1795
1796 struct isis_lsp *last_lsp;
1797 isis_tlvs_add_csnp_entries(tlvs, start, stop, num_lsps,
1798 circuit->area->lspdb[level - 1],
1799 &last_lsp);
d62a17ae 1800 /*
1801 * Update the stop lsp_id before encoding this CSNP.
1802 */
17c9dcd5 1803 if (tlvs->lsp_entries.count < num_lsps) {
d62a17ae 1804 memset(stop, 0xff, ISIS_SYS_ID_LEN + 2);
1805 } else {
af8ac8f9 1806 memcpy(stop, last_lsp->hdr.lsp_id, sizeof(stop));
d62a17ae 1807 }
3f045a08 1808
17c9dcd5
CF
1809 memcpy(STREAM_DATA(circuit->snd_stream) + start_pointer, start,
1810 ISIS_SYS_ID_LEN + 2);
1811 memcpy(STREAM_DATA(circuit->snd_stream) + end_pointer, stop,
1812 ISIS_SYS_ID_LEN + 2);
1813 stream_set_endp(circuit->snd_stream, tlv_start);
1814 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer,
af8ac8f9 1815 false, false)) {
17c9dcd5
CF
1816 isis_free_tlvs(tlvs);
1817 return ISIS_WARNING;
d62a17ae 1818 }
eb5d44eb 1819
d62a17ae 1820 if (isis->debugs & DEBUG_SNP_PACKETS) {
1821 zlog_debug(
1822 "ISIS-Snp (%s): Sending L%d CSNP on %s, length %zd",
1823 circuit->area->area_tag, level,
1824 circuit->interface->name,
1825 stream_get_endp(circuit->snd_stream));
17c9dcd5
CF
1826 log_multiline(LOG_DEBUG, " ", "%s",
1827 isis_format_tlvs(tlvs));
d62a17ae 1828 if (isis->debugs & DEBUG_PACKET_DUMP)
1829 zlog_dump_data(
1830 STREAM_DATA(circuit->snd_stream),
1831 stream_get_endp(circuit->snd_stream));
1832 }
eb5d44eb 1833
17c9dcd5 1834 int retval = circuit->tx(circuit, level);
d62a17ae 1835 if (retval != ISIS_OK) {
1836 zlog_err("ISIS-Snp (%s): Send L%d CSNP on %s failed",
1837 circuit->area->area_tag, level,
1838 circuit->interface->name);
17c9dcd5 1839 isis_free_tlvs(tlvs);
d62a17ae 1840 return retval;
1841 }
3f045a08 1842
d62a17ae 1843 /*
1844 * Start lsp_id of the next CSNP should be one plus the
1845 * stop lsp_id in this current CSNP.
1846 */
1847 memcpy(start, stop, ISIS_SYS_ID_LEN + 2);
1848 loop = 0;
17c9dcd5 1849 for (int i = ISIS_SYS_ID_LEN + 1; i >= 0; --i) {
d62a17ae 1850 if (start[i] < (u_char)0xff) {
1851 start[i] += 1;
1852 loop = 1;
1853 break;
1854 }
1855 }
1856 memset(stop, 0xff, ISIS_SYS_ID_LEN + 2);
17c9dcd5 1857 isis_free_tlvs(tlvs);
d62a17ae 1858 }
3f045a08 1859
17c9dcd5 1860 return ISIS_OK;
eb5d44eb 1861}
1862
d62a17ae 1863int send_l1_csnp(struct thread *thread)
eb5d44eb 1864{
d62a17ae 1865 struct isis_circuit *circuit;
1866 int retval = ISIS_OK;
eb5d44eb 1867
d62a17ae 1868 circuit = THREAD_ARG(thread);
1869 assert(circuit);
eb5d44eb 1870
d62a17ae 1871 circuit->t_send_csnp[0] = NULL;
eb5d44eb 1872
d62a17ae 1873 if (circuit->circ_type == CIRCUIT_T_BROADCAST
1874 && circuit->u.bc.is_dr[0]) {
1875 send_csnp(circuit, 1);
1876 }
1877 /* set next timer thread */
1878 thread_add_timer(master, send_l1_csnp, circuit,
1879 isis_jitter(circuit->csnp_interval[0], CSNP_JITTER),
1880 &circuit->t_send_csnp[0]);
eb5d44eb 1881
d62a17ae 1882 return retval;
eb5d44eb 1883}
1884
d62a17ae 1885int send_l2_csnp(struct thread *thread)
eb5d44eb 1886{
d62a17ae 1887 struct isis_circuit *circuit;
1888 int retval = ISIS_OK;
eb5d44eb 1889
d62a17ae 1890 circuit = THREAD_ARG(thread);
1891 assert(circuit);
eb5d44eb 1892
d62a17ae 1893 circuit->t_send_csnp[1] = NULL;
eb5d44eb 1894
d62a17ae 1895 if (circuit->circ_type == CIRCUIT_T_BROADCAST
1896 && circuit->u.bc.is_dr[1]) {
1897 send_csnp(circuit, 2);
1898 }
1899 /* set next timer thread */
1900 thread_add_timer(master, send_l2_csnp, circuit,
1901 isis_jitter(circuit->csnp_interval[1], CSNP_JITTER),
1902 &circuit->t_send_csnp[1]);
d70f99e1 1903
d62a17ae 1904 return retval;
eb5d44eb 1905}
1906
eb5d44eb 1907/*
1908 * 7.3.15.4 action on expiration of partial SNP interval
1909 * level 1
1910 */
d62a17ae 1911static int send_psnp(int level, struct isis_circuit *circuit)
eb5d44eb 1912{
d62a17ae 1913 if (circuit->circ_type == CIRCUIT_T_BROADCAST
1914 && circuit->u.bc.is_dr[level - 1])
1915 return ISIS_OK;
eb5d44eb 1916
d62a17ae 1917 if (circuit->area->lspdb[level - 1] == NULL
1918 || dict_count(circuit->area->lspdb[level - 1]) == 0)
1919 return ISIS_OK;
f390d2c7 1920
d62a17ae 1921 if (!circuit->snd_stream)
1922 return ISIS_ERROR;
e38e0df0 1923
17c9dcd5
CF
1924 isis_circuit_stream(circuit, &circuit->snd_stream);
1925 fill_fixed_hdr((level == ISIS_LEVEL1) ? L1_PARTIAL_SEQ_NUM
1926 : L2_PARTIAL_SEQ_NUM,
1927 circuit->snd_stream);
1928
1929 size_t len_pointer = stream_get_endp(circuit->snd_stream);
1930 stream_putw(circuit->snd_stream, 0); /* length is filled in later */
1931 stream_put(circuit->snd_stream, isis->sysid, ISIS_SYS_ID_LEN);
1932 stream_putc(circuit->snd_stream, circuit->idx);
1933
1934 struct isis_passwd *passwd = (level == ISIS_LEVEL1)
1935 ? &circuit->area->area_passwd
1936 : &circuit->area->domain_passwd;
1937
1938 struct isis_tlvs *tlvs = isis_alloc_tlvs();
1939
1940 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
1941 isis_tlvs_add_auth(tlvs, passwd);
1942
1943 size_t tlv_start = stream_get_endp(circuit->snd_stream);
af8ac8f9
CF
1944 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer, false,
1945 false)) {
17c9dcd5
CF
1946 isis_free_tlvs(tlvs);
1947 return ISIS_WARNING;
1948 }
1949 isis_free_tlvs(tlvs);
1950
1951 uint16_t num_lsps =
1952 get_max_lsp_count(STREAM_WRITEABLE(circuit->snd_stream));
f390d2c7 1953
d62a17ae 1954 while (1) {
17c9dcd5
CF
1955 tlvs = isis_alloc_tlvs();
1956 if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
1957 isis_tlvs_add_auth(tlvs, passwd);
1958
1959 for (dnode_t *dnode =
1960 dict_first(circuit->area->lspdb[level - 1]);
1961 dnode; dnode = dict_next(circuit->area->lspdb[level - 1],
1962 dnode)) {
1963 struct isis_lsp *lsp = dnode_get(dnode);
1964
1965 if (ISIS_CHECK_FLAG(lsp->SSNflags, circuit))
1966 isis_tlvs_add_lsp_entry(tlvs, lsp);
eb5d44eb 1967
17c9dcd5
CF
1968 if (tlvs->lsp_entries.count == num_lsps)
1969 break;
1970 }
1971
1972 if (!tlvs->lsp_entries.count) {
1973 isis_free_tlvs(tlvs);
d62a17ae 1974 return ISIS_OK;
1975 }
1976
17c9dcd5
CF
1977 stream_set_endp(circuit->snd_stream, tlv_start);
1978 if (isis_pack_tlvs(tlvs, circuit->snd_stream, len_pointer,
af8ac8f9 1979 false, false)) {
17c9dcd5
CF
1980 isis_free_tlvs(tlvs);
1981 return ISIS_WARNING;
d62a17ae 1982 }
1983
1984 if (isis->debugs & DEBUG_SNP_PACKETS) {
1985 zlog_debug(
1986 "ISIS-Snp (%s): Sending L%d PSNP on %s, length %zd",
1987 circuit->area->area_tag, level,
1988 circuit->interface->name,
1989 stream_get_endp(circuit->snd_stream));
17c9dcd5
CF
1990 log_multiline(LOG_DEBUG, " ", "%s",
1991 isis_format_tlvs(tlvs));
d62a17ae 1992 if (isis->debugs & DEBUG_PACKET_DUMP)
1993 zlog_dump_data(
1994 STREAM_DATA(circuit->snd_stream),
1995 stream_get_endp(circuit->snd_stream));
1996 }
1997
17c9dcd5 1998 int retval = circuit->tx(circuit, level);
d62a17ae 1999 if (retval != ISIS_OK) {
2000 zlog_err("ISIS-Snp (%s): Send L%d PSNP on %s failed",
2001 circuit->area->area_tag, level,
2002 circuit->interface->name);
17c9dcd5 2003 isis_free_tlvs(tlvs);
d62a17ae 2004 return retval;
2005 }
2006
2007 /*
2008 * sending succeeded, we can clear SSN flags of this circuit
2009 * for the LSPs in list
2010 */
17c9dcd5
CF
2011 struct isis_lsp_entry *entry_head;
2012 entry_head = (struct isis_lsp_entry *)tlvs->lsp_entries.head;
2013 for (struct isis_lsp_entry *entry = entry_head; entry;
2014 entry = entry->next)
2015 ISIS_CLEAR_FLAG(entry->lsp->SSNflags, circuit);
2016 isis_free_tlvs(tlvs);
d62a17ae 2017 }
2018
17c9dcd5 2019 return ISIS_OK;
eb5d44eb 2020}
2021
d62a17ae 2022int send_l1_psnp(struct thread *thread)
eb5d44eb 2023{
2024
d62a17ae 2025 struct isis_circuit *circuit;
2026 int retval = ISIS_OK;
eb5d44eb 2027
d62a17ae 2028 circuit = THREAD_ARG(thread);
2029 assert(circuit);
eb5d44eb 2030
d62a17ae 2031 circuit->t_send_psnp[0] = NULL;
eb5d44eb 2032
d62a17ae 2033 send_psnp(1, circuit);
2034 /* set next timer thread */
2035 thread_add_timer(master, send_l1_psnp, circuit,
2036 isis_jitter(circuit->psnp_interval[0], PSNP_JITTER),
2037 &circuit->t_send_psnp[0]);
eb5d44eb 2038
d62a17ae 2039 return retval;
eb5d44eb 2040}
2041
2042/*
2043 * 7.3.15.4 action on expiration of partial SNP interval
2044 * level 2
2045 */
d62a17ae 2046int send_l2_psnp(struct thread *thread)
eb5d44eb 2047{
d62a17ae 2048 struct isis_circuit *circuit;
2049 int retval = ISIS_OK;
eb5d44eb 2050
d62a17ae 2051 circuit = THREAD_ARG(thread);
2052 assert(circuit);
eb5d44eb 2053
d62a17ae 2054 circuit->t_send_psnp[1] = NULL;
eb5d44eb 2055
d62a17ae 2056 send_psnp(2, circuit);
eb5d44eb 2057
d62a17ae 2058 /* set next timer thread */
2059 thread_add_timer(master, send_l2_psnp, circuit,
2060 isis_jitter(circuit->psnp_interval[1], PSNP_JITTER),
2061 &circuit->t_send_psnp[1]);
eb5d44eb 2062
d62a17ae 2063 return retval;
eb5d44eb 2064}
2065
eb5d44eb 2066/*
2067 * ISO 10589 - 7.3.14.3
2068 */
d62a17ae 2069int send_lsp(struct thread *thread)
eb5d44eb 2070{
d62a17ae 2071 struct isis_circuit *circuit;
2072 struct isis_lsp *lsp;
d62a17ae 2073 int clear_srm = 1;
2074 int retval = ISIS_OK;
2075
2076 circuit = THREAD_ARG(thread);
2077 assert(circuit);
58e16237 2078 circuit->t_send_lsp = NULL;
d62a17ae 2079
58e16237
CF
2080 lsp = isis_circuit_lsp_queue_pop(circuit);
2081 if (!lsp)
d62a17ae 2082 return ISIS_OK;
e5973ce5
CF
2083
2084 if (!list_isempty(circuit->lsp_queue)) {
58e16237
CF
2085 isis_circuit_schedule_lsp_send(circuit);
2086 }
d62a17ae 2087
2088 if (circuit->state != C_STATE_UP || circuit->is_passive == 1)
2089 goto out;
2090
2091 /*
2092 * Do not send if levels do not match
2093 */
2094 if (!(lsp->level & circuit->is_type))
2095 goto out;
2096
2097 /*
2098 * Do not send if we do not have adjacencies in state up on the circuit
2099 */
2100 if (circuit->upadjcount[lsp->level - 1] == 0)
2101 goto out;
2102
2103 /* stream_copy will assert and stop program execution if LSP is larger
2104 * than
2105 * the circuit's MTU. So handle and log this case here. */
2106 if (stream_get_endp(lsp->pdu) > stream_get_size(circuit->snd_stream)) {
2107 zlog_err(
af8ac8f9
CF
2108 "ISIS-Upd (%s): Can't send L%d LSP %s, seq 0x%08" PRIx32
2109 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
2110 "s on %s. LSP Size is %zu while interface stream size is %zu.",
d62a17ae 2111 circuit->area->area_tag, lsp->level,
af8ac8f9
CF
2112 rawlspid_print(lsp->hdr.lsp_id), lsp->hdr.seqno,
2113 lsp->hdr.checksum, lsp->hdr.rem_lifetime,
d62a17ae 2114 circuit->interface->name, stream_get_endp(lsp->pdu),
2115 stream_get_size(circuit->snd_stream));
2116 if (isis->debugs & DEBUG_PACKET_DUMP)
2117 zlog_dump_data(STREAM_DATA(lsp->pdu),
2118 stream_get_endp(lsp->pdu));
2119 retval = ISIS_ERROR;
2120 goto out;
2121 }
eb5d44eb 2122
d62a17ae 2123 /* copy our lsp to the send buffer */
2124 stream_copy(circuit->snd_stream, lsp->pdu);
2125
2126 if (isis->debugs & DEBUG_UPDATE_PACKETS) {
af8ac8f9
CF
2127 zlog_debug("ISIS-Upd (%s): Sending L%d LSP %s, seq 0x%08" PRIx32
2128 ", cksum 0x%04" PRIx16 ", lifetime %" PRIu16
2129 "s on %s",
2130 circuit->area->area_tag, lsp->level,
2131 rawlspid_print(lsp->hdr.lsp_id), lsp->hdr.seqno,
2132 lsp->hdr.checksum, lsp->hdr.rem_lifetime,
2133 circuit->interface->name);
d62a17ae 2134 if (isis->debugs & DEBUG_PACKET_DUMP)
2135 zlog_dump_data(STREAM_DATA(circuit->snd_stream),
2136 stream_get_endp(circuit->snd_stream));
2137 }
3f045a08 2138
d62a17ae 2139 clear_srm = 0;
2140 retval = circuit->tx(circuit, lsp->level);
2141 if (retval != ISIS_OK) {
2142 zlog_err("ISIS-Upd (%s): Send L%d LSP on %s failed %s",
2143 circuit->area->area_tag, lsp->level,
2144 circuit->interface->name,
2145 (retval == ISIS_WARNING) ? "temporarily"
2146 : "permanently");
2147 }
f390d2c7 2148
cfd1f27b 2149out:
d62a17ae 2150 if (clear_srm
2151 || (retval == ISIS_OK && circuit->circ_type == CIRCUIT_T_BROADCAST)
2152 || (retval != ISIS_OK && retval != ISIS_WARNING)) {
2153 /* SRM flag will trigger retransmission. We will not retransmit
2154 * if we
2155 * encountered a fatal error.
2156 * On success, they should only be cleared if it's a broadcast
2157 * circuit.
2158 * On a P2P circuit, we will wait for the ack from the neighbor
2159 * to clear
2160 * the fag.
2161 */
2162 ISIS_CLEAR_FLAG(lsp->SRMflags, circuit);
2163 }
eb5d44eb 2164
d62a17ae 2165 return retval;
f390d2c7 2166}