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