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