]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* |
2 | * OSPF Sending and Receiving OSPF Packets. | |
3 | * Copyright (C) 1999, 2000 Toshiaki Takada | |
4 | * | |
5 | * This file is part of GNU Zebra. | |
6 | * | |
7 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation; either version 2, or (at your option) any | |
10 | * later version. | |
11 | * | |
12 | * GNU Zebra is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with GNU Zebra; see the file COPYING. If not, write to the Free | |
19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
20 | * 02111-1307, USA. | |
21 | */ | |
22 | ||
23 | #include <zebra.h> | |
24 | ||
25 | #include "thread.h" | |
26 | #include "memory.h" | |
27 | #include "linklist.h" | |
28 | #include "prefix.h" | |
29 | #include "if.h" | |
30 | #include "table.h" | |
31 | #include "sockunion.h" | |
32 | #include "stream.h" | |
33 | #include "log.h" | |
2dd8bb4e | 34 | #include "sockopt.h" |
718e3744 | 35 | #include "md5-gnu.h" |
36 | ||
37 | #include "ospfd/ospfd.h" | |
38 | #include "ospfd/ospf_network.h" | |
39 | #include "ospfd/ospf_interface.h" | |
40 | #include "ospfd/ospf_ism.h" | |
41 | #include "ospfd/ospf_asbr.h" | |
42 | #include "ospfd/ospf_lsa.h" | |
43 | #include "ospfd/ospf_lsdb.h" | |
44 | #include "ospfd/ospf_neighbor.h" | |
45 | #include "ospfd/ospf_nsm.h" | |
46 | #include "ospfd/ospf_packet.h" | |
47 | #include "ospfd/ospf_spf.h" | |
48 | #include "ospfd/ospf_flood.h" | |
49 | #include "ospfd/ospf_dump.h" | |
50 | ||
52dc7ee6 | 51 | static void ospf_ls_ack_send_list (struct ospf_interface *, struct list *, |
718e3744 | 52 | struct in_addr); |
53 | ||
54 | /* Packet Type String. */ | |
55 | char *ospf_packet_type_str[] = | |
56 | { | |
57 | "unknown", | |
58 | "Hello", | |
59 | "Database Description", | |
60 | "Link State Request", | |
61 | "Link State Update", | |
62 | "Link State Acknowledgment", | |
63 | }; | |
64 | ||
65 | extern int in_cksum (void *ptr, int nbytes); | |
66 | ||
67 | /* OSPF authentication checking function */ | |
68 | int | |
69 | ospf_auth_type (struct ospf_interface *oi) | |
70 | { | |
71 | int auth_type; | |
72 | ||
73 | if (OSPF_IF_PARAM (oi, auth_type) == OSPF_AUTH_NOTSET) | |
74 | auth_type = oi->area->auth_type; | |
75 | else | |
76 | auth_type = OSPF_IF_PARAM (oi, auth_type); | |
77 | ||
78 | /* Handle case where MD5 key list is not configured aka Cisco */ | |
79 | if (auth_type == OSPF_AUTH_CRYPTOGRAPHIC && | |
80 | list_isempty (OSPF_IF_PARAM (oi, auth_crypt))) | |
81 | return OSPF_AUTH_NULL; | |
82 | ||
83 | return auth_type; | |
84 | ||
85 | } | |
86 | ||
87 | /* forward output pointer. */ | |
88 | void | |
89 | ospf_output_forward (struct stream *s, int size) | |
90 | { | |
91 | s->putp += size; | |
92 | } | |
93 | ||
94 | struct ospf_packet * | |
95 | ospf_packet_new (size_t size) | |
96 | { | |
97 | struct ospf_packet *new; | |
98 | ||
99 | new = XCALLOC (MTYPE_OSPF_PACKET, sizeof (struct ospf_packet)); | |
100 | new->s = stream_new (size); | |
101 | ||
102 | return new; | |
103 | } | |
104 | ||
105 | void | |
106 | ospf_packet_free (struct ospf_packet *op) | |
107 | { | |
108 | if (op->s) | |
109 | stream_free (op->s); | |
110 | ||
111 | XFREE (MTYPE_OSPF_PACKET, op); | |
112 | ||
113 | op = NULL; | |
114 | } | |
115 | ||
116 | struct ospf_fifo * | |
117 | ospf_fifo_new () | |
118 | { | |
119 | struct ospf_fifo *new; | |
120 | ||
121 | new = XCALLOC (MTYPE_OSPF_FIFO, sizeof (struct ospf_fifo)); | |
122 | return new; | |
123 | } | |
124 | ||
125 | /* Add new packet to fifo. */ | |
126 | void | |
127 | ospf_fifo_push (struct ospf_fifo *fifo, struct ospf_packet *op) | |
128 | { | |
129 | if (fifo->tail) | |
130 | fifo->tail->next = op; | |
131 | else | |
132 | fifo->head = op; | |
133 | ||
134 | fifo->tail = op; | |
135 | ||
136 | fifo->count++; | |
137 | } | |
138 | ||
139 | /* Delete first packet from fifo. */ | |
140 | struct ospf_packet * | |
141 | ospf_fifo_pop (struct ospf_fifo *fifo) | |
142 | { | |
143 | struct ospf_packet *op; | |
144 | ||
145 | op = fifo->head; | |
146 | ||
147 | if (op) | |
148 | { | |
149 | fifo->head = op->next; | |
150 | ||
151 | if (fifo->head == NULL) | |
152 | fifo->tail = NULL; | |
153 | ||
154 | fifo->count--; | |
155 | } | |
156 | ||
157 | return op; | |
158 | } | |
159 | ||
160 | /* Return first fifo entry. */ | |
161 | struct ospf_packet * | |
162 | ospf_fifo_head (struct ospf_fifo *fifo) | |
163 | { | |
164 | return fifo->head; | |
165 | } | |
166 | ||
167 | /* Flush ospf packet fifo. */ | |
168 | void | |
169 | ospf_fifo_flush (struct ospf_fifo *fifo) | |
170 | { | |
171 | struct ospf_packet *op; | |
172 | struct ospf_packet *next; | |
173 | ||
174 | for (op = fifo->head; op; op = next) | |
175 | { | |
176 | next = op->next; | |
177 | ospf_packet_free (op); | |
178 | } | |
179 | fifo->head = fifo->tail = NULL; | |
180 | fifo->count = 0; | |
181 | } | |
182 | ||
183 | /* Free ospf packet fifo. */ | |
184 | void | |
185 | ospf_fifo_free (struct ospf_fifo *fifo) | |
186 | { | |
187 | ospf_fifo_flush (fifo); | |
188 | ||
189 | XFREE (MTYPE_OSPF_FIFO, fifo); | |
190 | } | |
191 | ||
192 | void | |
193 | ospf_packet_add (struct ospf_interface *oi, struct ospf_packet *op) | |
194 | { | |
195 | /* Add packet to end of queue. */ | |
196 | ospf_fifo_push (oi->obuf, op); | |
197 | ||
198 | /* Debug of packet fifo*/ | |
199 | /* ospf_fifo_debug (oi->obuf); */ | |
200 | } | |
201 | ||
202 | void | |
203 | ospf_packet_delete (struct ospf_interface *oi) | |
204 | { | |
205 | struct ospf_packet *op; | |
206 | ||
207 | op = ospf_fifo_pop (oi->obuf); | |
208 | ||
209 | if (op) | |
210 | ospf_packet_free (op); | |
211 | } | |
212 | ||
213 | struct stream * | |
214 | ospf_stream_copy (struct stream *new, struct stream *s) | |
215 | { | |
216 | new->endp = s->endp; | |
217 | new->putp = s->putp; | |
218 | new->getp = s->getp; | |
219 | ||
220 | memcpy (new->data, s->data, stream_get_endp (s)); | |
221 | ||
222 | return new; | |
223 | } | |
224 | ||
225 | struct ospf_packet * | |
226 | ospf_packet_dup (struct ospf_packet *op) | |
227 | { | |
228 | struct ospf_packet *new; | |
229 | ||
37163d6d | 230 | if (stream_get_endp(op->s) != op->length) |
231 | zlog_warn ("ospf_packet_dup stream %ld ospf_packet %d size mismatch", | |
30961a15 | 232 | STREAM_SIZE(op->s), op->length); |
30961a15 | 233 | |
234 | /* Reserve space for MD5 authentication that may be added later. */ | |
235 | new = ospf_packet_new (stream_get_endp(op->s) + OSPF_AUTH_MD5_SIZE); | |
718e3744 | 236 | ospf_stream_copy (new->s, op->s); |
237 | ||
238 | new->dst = op->dst; | |
239 | new->length = op->length; | |
240 | ||
241 | return new; | |
242 | } | |
243 | ||
244 | int | |
245 | ospf_packet_max (struct ospf_interface *oi) | |
246 | { | |
247 | int max; | |
248 | ||
249 | if ( ospf_auth_type (oi) == OSPF_AUTH_CRYPTOGRAPHIC) | |
68b7339a | 250 | max = oi->ifp->mtu - OSPF_AUTH_MD5_SIZE; |
718e3744 | 251 | else |
68b7339a | 252 | max = oi->ifp->mtu; |
253 | ||
254 | max -= (OSPF_HEADER_SIZE + sizeof (struct ip)); | |
718e3744 | 255 | |
256 | return max; | |
257 | } | |
258 | ||
259 | \f | |
260 | int | |
261 | ospf_check_md5_digest (struct ospf_interface *oi, struct stream *s, | |
262 | u_int16_t length) | |
263 | { | |
264 | void *ibuf; | |
265 | struct md5_ctx ctx; | |
266 | unsigned char digest[OSPF_AUTH_MD5_SIZE]; | |
267 | unsigned char *pdigest; | |
268 | struct crypt_key *ck; | |
269 | struct ospf_header *ospfh; | |
270 | struct ospf_neighbor *nbr; | |
271 | ||
272 | ||
273 | ibuf = STREAM_PNT (s); | |
274 | ospfh = (struct ospf_header *) ibuf; | |
275 | ||
276 | /* Get pointer to the end of the packet. */ | |
277 | pdigest = ibuf + length; | |
278 | ||
279 | /* Get secret key. */ | |
280 | ck = ospf_crypt_key_lookup (OSPF_IF_PARAM (oi, auth_crypt), | |
281 | ospfh->u.crypt.key_id); | |
282 | if (ck == NULL) | |
283 | { | |
284 | zlog_warn ("interface %s: ospf_check_md5 no key %d", | |
285 | IF_NAME (oi), ospfh->u.crypt.key_id); | |
286 | return 0; | |
287 | } | |
288 | ||
289 | /* check crypto seqnum. */ | |
290 | nbr = ospf_nbr_lookup_by_routerid (oi->nbrs, &ospfh->router_id); | |
291 | ||
292 | if (nbr && ntohl(nbr->crypt_seqnum) > ntohl(ospfh->u.crypt.crypt_seqnum)) | |
293 | { | |
294 | zlog_warn ("interface %s: ospf_check_md5 bad sequence %d (expect %d)", | |
295 | IF_NAME (oi), | |
296 | ntohl(ospfh->u.crypt.crypt_seqnum), | |
297 | ntohl(nbr->crypt_seqnum)); | |
298 | return 0; | |
299 | } | |
300 | ||
301 | /* Generate a digest for the ospf packet - their digest + our digest. */ | |
302 | md5_init_ctx (&ctx); | |
303 | md5_process_bytes (ibuf, length, &ctx); | |
304 | md5_process_bytes (ck->auth_key, OSPF_AUTH_MD5_SIZE, &ctx); | |
305 | md5_finish_ctx (&ctx, digest); | |
306 | ||
307 | /* compare the two */ | |
308 | if (memcmp (pdigest, digest, OSPF_AUTH_MD5_SIZE)) | |
309 | { | |
310 | zlog_warn ("interface %s: ospf_check_md5 checksum mismatch", | |
311 | IF_NAME (oi)); | |
312 | return 0; | |
313 | } | |
314 | ||
315 | /* save neighbor's crypt_seqnum */ | |
316 | if (nbr) | |
317 | nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum; | |
318 | return 1; | |
319 | } | |
320 | ||
321 | /* This function is called from ospf_write(), it will detect the | |
322 | authentication scheme and if it is MD5, it will change the sequence | |
323 | and update the MD5 digest. */ | |
324 | int | |
325 | ospf_make_md5_digest (struct ospf_interface *oi, struct ospf_packet *op) | |
326 | { | |
327 | struct ospf_header *ospfh; | |
328 | unsigned char digest[OSPF_AUTH_MD5_SIZE]; | |
329 | struct md5_ctx ctx; | |
330 | void *ibuf; | |
331 | unsigned long oldputp; | |
9483e155 | 332 | u_int32_t t; |
718e3744 | 333 | struct crypt_key *ck; |
334 | char *auth_key; | |
335 | ||
336 | ibuf = STREAM_DATA (op->s); | |
337 | ospfh = (struct ospf_header *) ibuf; | |
338 | ||
339 | if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC) | |
340 | return 0; | |
341 | ||
342 | /* We do this here so when we dup a packet, we don't have to | |
343 | waste CPU rewriting other headers. */ | |
9483e155 | 344 | t = (time(NULL) & 0xFFFFFFFF); |
345 | oi->crypt_seqnum = ( t > oi->crypt_seqnum ? t : oi->crypt_seqnum++); | |
346 | ospfh->u.crypt.crypt_seqnum = htonl (oi->crypt_seqnum); | |
718e3744 | 347 | |
348 | /* Get MD5 Authentication key from auth_key list. */ | |
349 | if (list_isempty (OSPF_IF_PARAM (oi, auth_crypt))) | |
350 | auth_key = ""; | |
351 | else | |
352 | { | |
353 | ck = getdata (OSPF_IF_PARAM (oi, auth_crypt)->tail); | |
c9e52be3 | 354 | auth_key = (char *) ck->auth_key; |
718e3744 | 355 | } |
356 | ||
357 | /* Generate a digest for the entire packet + our secret key. */ | |
358 | md5_init_ctx (&ctx); | |
359 | md5_process_bytes (ibuf, ntohs (ospfh->length), &ctx); | |
360 | md5_process_bytes (auth_key, OSPF_AUTH_MD5_SIZE, &ctx); | |
361 | md5_finish_ctx (&ctx, digest); | |
362 | ||
363 | /* Append md5 digest to the end of the stream. */ | |
364 | oldputp = stream_get_putp (op->s); | |
365 | stream_set_putp (op->s, ntohs (ospfh->length)); | |
366 | stream_put (op->s, digest, OSPF_AUTH_MD5_SIZE); | |
367 | stream_set_putp (op->s, oldputp); | |
368 | ||
369 | /* We do *NOT* increment the OSPF header length. */ | |
30961a15 | 370 | op->length = ntohs (ospfh->length) + OSPF_AUTH_MD5_SIZE; |
371 | ||
37163d6d | 372 | if (stream_get_endp(op->s) != op->length) |
373 | zlog_warn("ospf_make_md5_digest: length mismatch stream %ld ospf_packet %d", stream_get_endp(op->s), op->length); | |
718e3744 | 374 | |
375 | return OSPF_AUTH_MD5_SIZE; | |
376 | } | |
377 | ||
378 | \f | |
379 | int | |
380 | ospf_ls_req_timer (struct thread *thread) | |
381 | { | |
382 | struct ospf_neighbor *nbr; | |
383 | ||
384 | nbr = THREAD_ARG (thread); | |
385 | nbr->t_ls_req = NULL; | |
386 | ||
387 | /* Send Link State Request. */ | |
388 | if (ospf_ls_request_count (nbr)) | |
389 | ospf_ls_req_send (nbr); | |
390 | ||
391 | /* Set Link State Request retransmission timer. */ | |
392 | OSPF_NSM_TIMER_ON (nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req); | |
393 | ||
394 | return 0; | |
395 | } | |
396 | ||
397 | void | |
398 | ospf_ls_req_event (struct ospf_neighbor *nbr) | |
399 | { | |
400 | if (nbr->t_ls_req) | |
401 | { | |
402 | thread_cancel (nbr->t_ls_req); | |
403 | nbr->t_ls_req = NULL; | |
404 | } | |
405 | nbr->t_ls_req = thread_add_event (master, ospf_ls_req_timer, nbr, 0); | |
406 | } | |
407 | ||
408 | /* Cyclic timer function. Fist registered in ospf_nbr_new () in | |
409 | ospf_neighbor.c */ | |
410 | int | |
411 | ospf_ls_upd_timer (struct thread *thread) | |
412 | { | |
413 | struct ospf_neighbor *nbr; | |
414 | ||
415 | nbr = THREAD_ARG (thread); | |
416 | nbr->t_ls_upd = NULL; | |
417 | ||
418 | /* Send Link State Update. */ | |
419 | if (ospf_ls_retransmit_count (nbr) > 0) | |
420 | { | |
52dc7ee6 | 421 | struct list *update; |
718e3744 | 422 | struct ospf_lsdb *lsdb; |
423 | int i; | |
424 | struct timeval now; | |
425 | int retransmit_interval; | |
426 | ||
427 | gettimeofday (&now, NULL); | |
428 | retransmit_interval = OSPF_IF_PARAM (nbr->oi, retransmit_interval); | |
429 | ||
430 | lsdb = &nbr->ls_rxmt; | |
431 | update = list_new (); | |
432 | ||
433 | for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) | |
434 | { | |
435 | struct route_table *table = lsdb->type[i].db; | |
436 | struct route_node *rn; | |
437 | ||
438 | for (rn = route_top (table); rn; rn = route_next (rn)) | |
439 | { | |
440 | struct ospf_lsa *lsa; | |
441 | ||
442 | if ((lsa = rn->info) != NULL) | |
443 | /* Don't retransmit an LSA if we received it within | |
444 | the last RxmtInterval seconds - this is to allow the | |
445 | neighbour a chance to acknowledge the LSA as it may | |
446 | have ben just received before the retransmit timer | |
447 | fired. This is a small tweak to what is in the RFC, | |
448 | but it will cut out out a lot of retransmit traffic | |
449 | - MAG */ | |
450 | if (tv_cmp (tv_sub (now, lsa->tv_recv), | |
451 | int2tv (retransmit_interval)) >= 0) | |
452 | listnode_add (update, rn->info); | |
453 | } | |
454 | } | |
455 | ||
456 | if (listcount (update) > 0) | |
457 | ospf_ls_upd_send (nbr, update, OSPF_SEND_PACKET_DIRECT); | |
458 | list_delete (update); | |
459 | } | |
460 | ||
461 | /* Set LS Update retransmission timer. */ | |
462 | OSPF_NSM_TIMER_ON (nbr->t_ls_upd, ospf_ls_upd_timer, nbr->v_ls_upd); | |
463 | ||
464 | return 0; | |
465 | } | |
466 | ||
467 | int | |
468 | ospf_ls_ack_timer (struct thread *thread) | |
469 | { | |
470 | struct ospf_interface *oi; | |
471 | ||
472 | oi = THREAD_ARG (thread); | |
473 | oi->t_ls_ack = NULL; | |
474 | ||
475 | /* Send Link State Acknowledgment. */ | |
476 | if (listcount (oi->ls_ack) > 0) | |
477 | ospf_ls_ack_send_delayed (oi); | |
478 | ||
479 | /* Set LS Ack timer. */ | |
480 | OSPF_ISM_TIMER_ON (oi->t_ls_ack, ospf_ls_ack_timer, oi->v_ls_ack); | |
481 | ||
482 | return 0; | |
483 | } | |
484 | ||
68b7339a | 485 | /* swab ip header fields to required order for sendmsg */ |
486 | void | |
487 | ospf_swab_iph_ton (struct ip *iph) | |
488 | { | |
489 | /* BSD and derived take iph in network order, except for | |
490 | * ip_len and ip_off | |
491 | */ | |
492 | #ifdef GNU_LINUX | |
493 | iph->ip_len = htons(iph->ip_len); | |
494 | iph->ip_off = htons(iph->ip_off); | |
495 | #endif | |
496 | iph->ip_id = htons(iph->ip_id); | |
497 | } | |
498 | ||
499 | /* swab ip header fields to host order, as required */ | |
500 | void | |
501 | ospf_swab_iph_toh (struct ip *iph) | |
502 | { | |
503 | #ifdef GNU_LINUX | |
504 | iph->ip_len = ntohs(iph->ip_len); | |
505 | iph->ip_off = ntohs(iph->ip_off); | |
506 | #endif | |
507 | iph->ip_id = ntohs(iph->ip_id); | |
508 | } | |
509 | ||
0bfeca3f | 510 | #ifdef WANT_OSPF_WRITE_FRAGMENT |
511 | void | |
512 | ospf_write_frags (struct ospf_packet *op, struct ip *ip, struct msghdr *msg, | |
513 | struct iovec *iov, int maxdatasize); | |
514 | { | |
515 | #define OSPF_WRITE_FRAG_SHIFT 3 | |
516 | ||
517 | assert ( op->length == stream_get_endp(op->s) ); | |
518 | ||
519 | /* we can but try. | |
520 | * | |
521 | * SunOS, BSD and BSD derived kernels likely will clear ip_id, as | |
522 | * well as the IP_MF flag, making this all quite pointless. | |
523 | * | |
524 | * However, for a system on which IP_MF is left alone, and ip_id left | |
525 | * alone or else which sets same ip_id for each fragment this might | |
526 | * work, eg linux. | |
527 | * | |
528 | * XXX-TODO: It would be much nicer to have the kernel's use their | |
529 | * existing fragmentation support to do this for us. Bugs/RFEs need to | |
530 | * be raised against the various kernels. | |
531 | */ | |
532 | ||
533 | /* set More Frag */ | |
534 | iph->ip_off |= IP_MF; | |
535 | ||
536 | /* ip frag offset is expressed in units of 8byte words */ | |
537 | offset = maxdatasize >> OSPF_WRITE_FRAG_SHIFT; | |
538 | ||
539 | while ( (stream_get_endp(op->s) - stream_get_getp (op->s)) | |
540 | > maxdatasize ) | |
541 | { | |
542 | /* data length of this frag is to next offset value */ | |
543 | iov[1]->iov_len = offset << OSPF_WRITE_FRAG_SHIFT; | |
544 | iph->ip_len = iov[1]->iov_len + sizeof (struct ip); | |
545 | assert (iph->ip_len <= oi->ifp->mtu); | |
546 | ||
547 | ospf_swab_iph_ton (iph); | |
548 | ||
549 | ret = sendmsg (ospf->fd, msg, flags); | |
550 | ||
551 | ospf_swab_iph_toh (iph); | |
552 | ||
553 | if (ret < 0) | |
554 | zlog_warn ("*** sendmsg in ospf_write to %s," | |
555 | " id %d, off %d, len %d failed with %s", | |
556 | inet_ntoa (iph->ip_dst), | |
557 | iph->ip_id, | |
558 | iph->ip_off, | |
559 | iph->ip_len, | |
560 | strerror (errno)); | |
561 | ||
562 | iph->ip_off += offset; | |
563 | stream_forward (op->s, iov[1]->iov_len); | |
564 | iov[1]->iov_base = STREAM_PNT (op->s); | |
565 | } | |
566 | ||
567 | /* setup for final fragment */ | |
568 | iov[1]->iov_len = stream_get_endp(op->s) - stream_get_getp (op->s); | |
569 | iph->ip_len = iov[1]->iov_len + sizeof (struct ip); | |
570 | iph->ip_off &= (~IP_MF); | |
571 | } | |
572 | #endif /* WANT_OSPF_WRITE_FRAGMENT */ | |
573 | ||
718e3744 | 574 | int |
575 | ospf_write (struct thread *thread) | |
576 | { | |
68980084 | 577 | struct ospf *ospf = THREAD_ARG (thread); |
718e3744 | 578 | struct ospf_interface *oi; |
579 | struct ospf_packet *op; | |
580 | struct sockaddr_in sa_dst; | |
718e3744 | 581 | struct ip iph; |
582 | struct msghdr msg; | |
583 | struct iovec iov[2]; | |
68980084 | 584 | u_char type; |
585 | int ret; | |
586 | int flags = 0; | |
52dc7ee6 | 587 | struct listnode *node; |
0bfeca3f | 588 | #ifdef WANT_OSPF_WRITE_FRAGMENT |
68b7339a | 589 | static u_int16_t ipid = 0; |
0bfeca3f | 590 | #endif /* WANT_OSPF_WRITE_FRAGMENT */ |
68b7339a | 591 | u_int16_t maxdatasize, offset; |
592 | #define OSPF_WRITE_IPHL_SHIFT 2 | |
718e3744 | 593 | |
68980084 | 594 | ospf->t_write = NULL; |
718e3744 | 595 | |
68980084 | 596 | node = listhead (ospf->oi_write_q); |
718e3744 | 597 | assert (node); |
598 | oi = getdata (node); | |
599 | assert (oi); | |
0bfeca3f | 600 | |
601 | #ifdef WANT_OSPF_WRITE_FRAGMENT | |
68b7339a | 602 | /* seed ipid static with low order bits of time */ |
603 | if (ipid == 0) | |
604 | ipid = (time(NULL) & 0xffff); | |
0bfeca3f | 605 | #endif /* WANT_OSPF_WRITE_FRAGMENT */ |
606 | ||
68b7339a | 607 | /* convenience - max OSPF data per packet */ |
608 | maxdatasize = oi->ifp->mtu - sizeof (struct ip); | |
609 | ||
718e3744 | 610 | /* Get one packet from queue. */ |
611 | op = ospf_fifo_head (oi->obuf); | |
612 | assert (op); | |
613 | assert (op->length >= OSPF_HEADER_SIZE); | |
614 | ||
68980084 | 615 | if (op->dst.s_addr == htonl (OSPF_ALLSPFROUTERS) |
616 | || op->dst.s_addr == htonl (OSPF_ALLDROUTERS)) | |
68b7339a | 617 | ospf_if_ipmulticast (ospf, oi->address, oi->ifp->ifindex); |
618 | ||
718e3744 | 619 | /* Rewrite the md5 signature & update the seq */ |
620 | ospf_make_md5_digest (oi, op); | |
621 | ||
68b7339a | 622 | /* reset get pointer */ |
623 | stream_set_getp (op->s, 0); | |
624 | ||
625 | memset (&iph, 0, sizeof (struct ip)); | |
718e3744 | 626 | memset (&sa_dst, 0, sizeof (sa_dst)); |
68b7339a | 627 | |
718e3744 | 628 | sa_dst.sin_family = AF_INET; |
629 | #ifdef HAVE_SIN_LEN | |
630 | sa_dst.sin_len = sizeof(sa_dst); | |
631 | #endif /* HAVE_SIN_LEN */ | |
632 | sa_dst.sin_addr = op->dst; | |
633 | sa_dst.sin_port = htons (0); | |
634 | ||
635 | /* Set DONTROUTE flag if dst is unicast. */ | |
636 | if (oi->type != OSPF_IFTYPE_VIRTUALLINK) | |
637 | if (!IN_MULTICAST (htonl (op->dst.s_addr))) | |
638 | flags = MSG_DONTROUTE; | |
639 | ||
68b7339a | 640 | iph.ip_hl = sizeof (struct ip) >> OSPF_WRITE_IPHL_SHIFT; |
641 | /* it'd be very strange for header to not be 4byte-word aligned but.. */ | |
642 | if ( sizeof (struct ip) > (iph.ip_hl << OSPF_WRITE_IPHL_SHIFT) ) | |
643 | iph.ip_hl++; /* we presume sizeof struct ip cant overflow ip_hl.. */ | |
644 | ||
718e3744 | 645 | iph.ip_v = IPVERSION; |
68980084 | 646 | iph.ip_tos = IPTOS_PREC_INTERNETCONTROL; |
68b7339a | 647 | iph.ip_len = (iph.ip_hl << OSPF_WRITE_IPHL_SHIFT) + op->length; |
68b7339a | 648 | |
0bfeca3f | 649 | #ifdef WANT_OSPF_WRITE_FRAGMENT |
68b7339a | 650 | /* XXX-MT: not thread-safe at all.. |
651 | * XXX: this presumes this is only programme sending OSPF packets | |
652 | * otherwise, no guarantee ipid will be unique | |
653 | */ | |
654 | iph.ip_id = ++ipid; | |
0bfeca3f | 655 | #endif /* WANT_OSPF_WRITE_FRAGMENT */ |
656 | ||
718e3744 | 657 | iph.ip_off = 0; |
658 | if (oi->type == OSPF_IFTYPE_VIRTUALLINK) | |
659 | iph.ip_ttl = OSPF_VL_IP_TTL; | |
660 | else | |
661 | iph.ip_ttl = OSPF_IP_TTL; | |
662 | iph.ip_p = IPPROTO_OSPFIGP; | |
663 | iph.ip_sum = 0; | |
664 | iph.ip_src.s_addr = oi->address->u.prefix4.s_addr; | |
665 | iph.ip_dst.s_addr = op->dst.s_addr; | |
666 | ||
667 | memset (&msg, 0, sizeof (msg)); | |
68defd6d | 668 | msg.msg_name = (caddr_t) &sa_dst; |
718e3744 | 669 | msg.msg_namelen = sizeof (sa_dst); |
670 | msg.msg_iov = iov; | |
671 | msg.msg_iovlen = 2; | |
672 | iov[0].iov_base = (char*)&iph; | |
68b7339a | 673 | iov[0].iov_len = iph.ip_hl << OSPF_WRITE_IPHL_SHIFT; |
674 | iov[1].iov_base = STREAM_PNT (op->s); | |
718e3744 | 675 | iov[1].iov_len = op->length; |
68b7339a | 676 | |
677 | /* Sadly we can not rely on kernels to fragment packets because of either | |
678 | * IP_HDRINCL and/or multicast destination being set. | |
679 | */ | |
0bfeca3f | 680 | #ifdef WANT_OSPF_WRITE_FRAGMENT |
68b7339a | 681 | if ( op->length > maxdatasize ) |
0bfeca3f | 682 | ospf_write_frags (&op, &ip, &msg, &iov, maxdatasize); |
683 | #endif /* WANT_OSPF_WRITE_FRAGMENT */ | |
718e3744 | 684 | |
68b7339a | 685 | /* send final fragment (could be first) */ |
686 | ospf_swab_iph_ton (&iph); | |
68980084 | 687 | ret = sendmsg (ospf->fd, &msg, flags); |
68b7339a | 688 | ospf_swab_iph_toh (&iph); |
718e3744 | 689 | |
690 | if (ret < 0) | |
68b7339a | 691 | zlog_warn ("*** sendmsg in ospf_write to %s failed with %s", |
692 | inet_ntoa (iph.ip_dst), strerror (errno)); | |
718e3744 | 693 | |
694 | /* Retrieve OSPF packet type. */ | |
695 | stream_set_getp (op->s, 1); | |
696 | type = stream_getc (op->s); | |
697 | ||
698 | /* Show debug sending packet. */ | |
699 | if (IS_DEBUG_OSPF_PACKET (type - 1, SEND)) | |
700 | { | |
701 | if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL)) | |
702 | { | |
703 | zlog_info ("-----------------------------------------------------"); | |
704 | stream_set_getp (op->s, 0); | |
705 | ospf_packet_dump (op->s); | |
706 | } | |
707 | ||
708 | zlog_info ("%s sent to [%s] via [%s].", | |
709 | ospf_packet_type_str[type], inet_ntoa (op->dst), | |
710 | IF_NAME (oi)); | |
711 | ||
712 | if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL)) | |
713 | zlog_info ("-----------------------------------------------------"); | |
714 | } | |
715 | ||
716 | /* Now delete packet from queue. */ | |
717 | ospf_packet_delete (oi); | |
718 | ||
719 | if (ospf_fifo_head (oi->obuf) == NULL) | |
720 | { | |
721 | oi->on_write_q = 0; | |
68980084 | 722 | list_delete_node (ospf->oi_write_q, node); |
718e3744 | 723 | } |
724 | ||
725 | /* If packets still remain in queue, call write thread. */ | |
68980084 | 726 | if (!list_isempty (ospf->oi_write_q)) |
727 | ospf->t_write = | |
728 | thread_add_write (master, ospf_write, ospf, ospf->fd); | |
718e3744 | 729 | |
730 | return 0; | |
731 | } | |
732 | ||
733 | /* OSPF Hello message read -- RFC2328 Section 10.5. */ | |
734 | void | |
735 | ospf_hello (struct ip *iph, struct ospf_header *ospfh, | |
736 | struct stream * s, struct ospf_interface *oi, int size) | |
737 | { | |
738 | struct ospf_hello *hello; | |
739 | struct ospf_neighbor *nbr; | |
718e3744 | 740 | int old_state; |
d3f0d621 | 741 | struct prefix p; |
718e3744 | 742 | |
743 | /* increment statistics. */ | |
744 | oi->hello_in++; | |
745 | ||
746 | hello = (struct ospf_hello *) STREAM_PNT (s); | |
747 | ||
748 | /* If Hello is myself, silently discard. */ | |
68980084 | 749 | if (IPV4_ADDR_SAME (&ospfh->router_id, &oi->ospf->router_id)) |
d324181c | 750 | { |
751 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV)) | |
752 | { | |
753 | zlog_info ("ospf_header[%s/%s]: selforiginated, " | |
754 | "dropping.", | |
755 | ospf_packet_type_str[ospfh->type], | |
756 | inet_ntoa (iph->ip_src)); | |
757 | } | |
758 | return; | |
759 | } | |
718e3744 | 760 | |
761 | /* If incoming interface is passive one, ignore Hello. */ | |
f2c80652 | 762 | if (OSPF_IF_PARAM (oi, passive_interface) == OSPF_IF_PASSIVE) { |
c2191eae | 763 | zlog_info ("Packet %s [HELLO:RECV]: oi is passive", |
764 | inet_ntoa (ospfh->router_id)); | |
718e3744 | 765 | return; |
f2c80652 | 766 | } |
718e3744 | 767 | |
768 | /* get neighbor prefix. */ | |
769 | p.family = AF_INET; | |
770 | p.prefixlen = ip_masklen (hello->network_mask); | |
771 | p.u.prefix4 = iph->ip_src; | |
772 | ||
773 | /* Compare network mask. */ | |
774 | /* Checking is ignored for Point-to-Point and Virtual link. */ | |
775 | if (oi->type != OSPF_IFTYPE_POINTOPOINT | |
776 | && oi->type != OSPF_IFTYPE_VIRTUALLINK) | |
777 | if (oi->address->prefixlen != p.prefixlen) | |
778 | { | |
779 | zlog_warn ("Packet %s [Hello:RECV]: NetworkMask mismatch.", | |
780 | inet_ntoa (ospfh->router_id)); | |
781 | return; | |
782 | } | |
783 | ||
784 | /* Compare Hello Interval. */ | |
785 | if (OSPF_IF_PARAM (oi, v_hello) != ntohs (hello->hello_interval)) | |
786 | { | |
787 | zlog_warn ("Packet %s [Hello:RECV]: HelloInterval mismatch.", | |
788 | inet_ntoa (ospfh->router_id)); | |
789 | return; | |
790 | } | |
791 | ||
792 | /* Compare Router Dead Interval. */ | |
793 | if (OSPF_IF_PARAM (oi, v_wait) != ntohl (hello->dead_interval)) | |
794 | { | |
795 | zlog_warn ("Packet %s [Hello:RECV]: RouterDeadInterval mismatch.", | |
796 | inet_ntoa (ospfh->router_id)); | |
797 | return; | |
798 | } | |
799 | ||
800 | if (IS_DEBUG_OSPF_EVENT) | |
801 | zlog_info ("Packet %s [Hello:RECV]: Options %s", | |
802 | inet_ntoa (ospfh->router_id), | |
803 | ospf_options_dump (hello->options)); | |
804 | ||
805 | /* Compare options. */ | |
806 | #define REJECT_IF_TBIT_ON 1 /* XXX */ | |
807 | #ifdef REJECT_IF_TBIT_ON | |
808 | if (CHECK_FLAG (hello->options, OSPF_OPTION_T)) | |
809 | { | |
810 | /* | |
811 | * This router does not support non-zero TOS. | |
812 | * Drop this Hello packet not to establish neighbor relationship. | |
813 | */ | |
814 | zlog_warn ("Packet %s [Hello:RECV]: T-bit on, drop it.", | |
815 | inet_ntoa (ospfh->router_id)); | |
816 | return; | |
817 | } | |
818 | #endif /* REJECT_IF_TBIT_ON */ | |
819 | ||
820 | #ifdef HAVE_OPAQUE_LSA | |
68980084 | 821 | if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE) |
718e3744 | 822 | && CHECK_FLAG (hello->options, OSPF_OPTION_O)) |
823 | { | |
824 | /* | |
825 | * This router does know the correct usage of O-bit | |
826 | * the bit should be set in DD packet only. | |
827 | */ | |
828 | zlog_warn ("Packet %s [Hello:RECV]: O-bit abuse?", | |
829 | inet_ntoa (ospfh->router_id)); | |
830 | #ifdef STRICT_OBIT_USAGE_CHECK | |
831 | return; /* Reject this packet. */ | |
832 | #else /* STRICT_OBIT_USAGE_CHECK */ | |
833 | UNSET_FLAG (hello->options, OSPF_OPTION_O); /* Ignore O-bit. */ | |
834 | #endif /* STRICT_OBIT_USAGE_CHECK */ | |
835 | } | |
836 | #endif /* HAVE_OPAQUE_LSA */ | |
837 | ||
838 | /* new for NSSA is to ensure that NP is on and E is off */ | |
839 | ||
718e3744 | 840 | if (oi->area->external_routing == OSPF_AREA_NSSA) |
841 | { | |
842 | if (! (CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_NP) | |
843 | && CHECK_FLAG (hello->options, OSPF_OPTION_NP) | |
844 | && ! CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_E) | |
845 | && ! CHECK_FLAG (hello->options, OSPF_OPTION_E))) | |
846 | { | |
847 | zlog_warn ("NSSA-Packet-%s[Hello:RECV]: my options: %x, his options %x", inet_ntoa (ospfh->router_id), OPTIONS (oi), hello->options); | |
848 | return; | |
849 | } | |
850 | if (IS_DEBUG_OSPF_NSSA) | |
851 | zlog_info ("NSSA-Hello:RECV:Packet from %s:", inet_ntoa(ospfh->router_id)); | |
852 | } | |
853 | else | |
718e3744 | 854 | /* The setting of the E-bit found in the Hello Packet's Options |
855 | field must match this area's ExternalRoutingCapability A | |
856 | mismatch causes processing to stop and the packet to be | |
857 | dropped. The setting of the rest of the bits in the Hello | |
858 | Packet's Options field should be ignored. */ | |
859 | if (CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_E) != | |
860 | CHECK_FLAG (hello->options, OSPF_OPTION_E)) | |
861 | { | |
862 | zlog_warn ("Packet[Hello:RECV]: my options: %x, his options %x", | |
863 | OPTIONS (oi), hello->options); | |
864 | return; | |
865 | } | |
718e3744 | 866 | |
d3f0d621 | 867 | /* get neighbour struct */ |
868 | nbr = ospf_nbr_get (oi, ospfh, iph, &p); | |
869 | ||
870 | /* neighbour must be valid, ospf_nbr_get creates if none existed */ | |
871 | assert (nbr); | |
718e3744 | 872 | |
873 | old_state = nbr->state; | |
874 | ||
875 | /* Add event to thread. */ | |
876 | OSPF_NSM_EVENT_EXECUTE (nbr, NSM_HelloReceived); | |
877 | ||
878 | /* RFC2328 Section 9.5.1 | |
879 | If the router is not eligible to become Designated Router, | |
880 | (snip) It must also send an Hello Packet in reply to an | |
881 | Hello Packet received from any eligible neighbor (other than | |
882 | the current Designated Router and Backup Designated Router). */ | |
883 | if (oi->type == OSPF_IFTYPE_NBMA) | |
884 | if (PRIORITY(oi) == 0 && hello->priority > 0 | |
885 | && IPV4_ADDR_CMP(&DR(oi), &iph->ip_src) | |
886 | && IPV4_ADDR_CMP(&BDR(oi), &iph->ip_src)) | |
887 | OSPF_NSM_TIMER_ON (nbr->t_hello_reply, ospf_hello_reply_timer, | |
888 | OSPF_HELLO_REPLY_DELAY); | |
889 | ||
890 | /* on NBMA network type, it happens to receive bidirectional Hello packet | |
891 | without advance 1-Way Received event. | |
892 | To avoid incorrect DR-seletion, raise 1-Way Received event.*/ | |
893 | if (oi->type == OSPF_IFTYPE_NBMA && | |
894 | (old_state == NSM_Down || old_state == NSM_Attempt)) | |
895 | { | |
896 | OSPF_NSM_EVENT_EXECUTE (nbr, NSM_OneWayReceived); | |
897 | nbr->priority = hello->priority; | |
898 | nbr->d_router = hello->d_router; | |
899 | nbr->bd_router = hello->bd_router; | |
900 | return; | |
901 | } | |
902 | ||
68980084 | 903 | if (ospf_nbr_bidirectional (&oi->ospf->router_id, hello->neighbors, |
718e3744 | 904 | size - OSPF_HELLO_MIN_SIZE)) |
905 | { | |
906 | OSPF_NSM_EVENT_EXECUTE (nbr, NSM_TwoWayReceived); | |
907 | nbr->options |= hello->options; | |
908 | } | |
909 | else | |
910 | { | |
911 | OSPF_NSM_EVENT_EXECUTE (nbr, NSM_OneWayReceived); | |
912 | /* Set neighbor information. */ | |
913 | nbr->priority = hello->priority; | |
914 | nbr->d_router = hello->d_router; | |
915 | nbr->bd_router = hello->bd_router; | |
916 | return; | |
917 | } | |
918 | ||
919 | /* If neighbor itself declares DR and no BDR exists, | |
920 | cause event BackupSeen */ | |
921 | if (IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->d_router)) | |
922 | if (hello->bd_router.s_addr == 0 && oi->state == ISM_Waiting) | |
923 | OSPF_ISM_EVENT_SCHEDULE (oi, ISM_BackupSeen); | |
924 | ||
925 | /* neighbor itself declares BDR. */ | |
926 | if (oi->state == ISM_Waiting && | |
927 | IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->bd_router)) | |
928 | OSPF_ISM_EVENT_SCHEDULE (oi, ISM_BackupSeen); | |
929 | ||
930 | /* had not previously. */ | |
931 | if ((IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->d_router) && | |
932 | IPV4_ADDR_CMP (&nbr->address.u.prefix4, &nbr->d_router)) || | |
933 | (IPV4_ADDR_CMP (&nbr->address.u.prefix4, &hello->d_router) && | |
934 | IPV4_ADDR_SAME (&nbr->address.u.prefix4, &nbr->d_router))) | |
935 | OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange); | |
936 | ||
937 | /* had not previously. */ | |
938 | if ((IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->bd_router) && | |
939 | IPV4_ADDR_CMP (&nbr->address.u.prefix4, &nbr->bd_router)) || | |
940 | (IPV4_ADDR_CMP (&nbr->address.u.prefix4, &hello->bd_router) && | |
941 | IPV4_ADDR_SAME (&nbr->address.u.prefix4, &nbr->bd_router))) | |
942 | OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange); | |
943 | ||
944 | /* Neighbor priority check. */ | |
945 | if (nbr->priority >= 0 && nbr->priority != hello->priority) | |
946 | OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange); | |
947 | ||
948 | /* Set neighbor information. */ | |
949 | nbr->priority = hello->priority; | |
950 | nbr->d_router = hello->d_router; | |
951 | nbr->bd_router = hello->bd_router; | |
952 | } | |
953 | ||
954 | /* Save DD flags/options/Seqnum received. */ | |
955 | void | |
956 | ospf_db_desc_save_current (struct ospf_neighbor *nbr, | |
957 | struct ospf_db_desc *dd) | |
958 | { | |
959 | nbr->last_recv.flags = dd->flags; | |
960 | nbr->last_recv.options = dd->options; | |
961 | nbr->last_recv.dd_seqnum = ntohl (dd->dd_seqnum); | |
962 | } | |
963 | ||
964 | /* Process rest of DD packet. */ | |
965 | static void | |
966 | ospf_db_desc_proc (struct stream *s, struct ospf_interface *oi, | |
967 | struct ospf_neighbor *nbr, struct ospf_db_desc *dd, | |
968 | u_int16_t size) | |
969 | { | |
970 | struct ospf_lsa *new, *find; | |
971 | struct lsa_header *lsah; | |
972 | ||
973 | stream_forward (s, OSPF_DB_DESC_MIN_SIZE); | |
974 | for (size -= OSPF_DB_DESC_MIN_SIZE; | |
975 | size >= OSPF_LSA_HEADER_SIZE; size -= OSPF_LSA_HEADER_SIZE) | |
976 | { | |
977 | lsah = (struct lsa_header *) STREAM_PNT (s); | |
978 | stream_forward (s, OSPF_LSA_HEADER_SIZE); | |
979 | ||
980 | /* Unknown LS type. */ | |
981 | if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA) | |
982 | { | |
983 | zlog_warn ("Pakcet [DD:RECV]: Unknown LS type %d.", lsah->type); | |
984 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); | |
985 | return; | |
986 | } | |
987 | ||
988 | #ifdef HAVE_OPAQUE_LSA | |
989 | if (IS_OPAQUE_LSA (lsah->type) | |
990 | && ! CHECK_FLAG (nbr->options, OSPF_OPTION_O)) | |
991 | { | |
992 | zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah->type, inet_ntoa (lsah->id)); | |
993 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); | |
994 | return; | |
995 | } | |
996 | #endif /* HAVE_OPAQUE_LSA */ | |
997 | ||
998 | switch (lsah->type) | |
999 | { | |
1000 | case OSPF_AS_EXTERNAL_LSA: | |
1001 | #ifdef HAVE_OPAQUE_LSA | |
1002 | case OSPF_OPAQUE_AS_LSA: | |
1003 | #endif /* HAVE_OPAQUE_LSA */ | |
718e3744 | 1004 | /* Check for stub area. Reject if AS-External from stub but |
1005 | allow if from NSSA. */ | |
1006 | if (oi->area->external_routing == OSPF_AREA_STUB) | |
718e3744 | 1007 | { |
1008 | zlog_warn ("Packet [DD:RECV]: LSA[Type%d:%s] from %s area.", | |
1009 | lsah->type, inet_ntoa (lsah->id), | |
1010 | (oi->area->external_routing == OSPF_AREA_STUB) ?\ | |
1011 | "STUB" : "NSSA"); | |
1012 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); | |
1013 | return; | |
1014 | } | |
1015 | break; | |
1016 | default: | |
1017 | break; | |
1018 | } | |
1019 | ||
1020 | /* Create LS-request object. */ | |
1021 | new = ospf_ls_request_new (lsah); | |
1022 | ||
1023 | /* Lookup received LSA, then add LS request list. */ | |
1024 | find = ospf_lsa_lookup_by_header (oi->area, lsah); | |
1025 | if (!find || ospf_lsa_more_recent (find, new) < 0) | |
1026 | { | |
1027 | ospf_ls_request_add (nbr, new); | |
1028 | ospf_lsa_discard (new); | |
1029 | } | |
1030 | else | |
1031 | { | |
1032 | /* Received LSA is not recent. */ | |
1033 | if (IS_DEBUG_OSPF_EVENT) | |
1034 | zlog_info ("Packet [DD:RECV]: LSA received Type %d, " | |
1035 | "ID %s is not recent.", lsah->type, inet_ntoa (lsah->id)); | |
1036 | ospf_lsa_discard (new); | |
1037 | continue; | |
1038 | } | |
1039 | } | |
1040 | ||
1041 | /* Master */ | |
1042 | if (IS_SET_DD_MS (nbr->dd_flags)) | |
1043 | { | |
1044 | nbr->dd_seqnum++; | |
1045 | /* Entire DD packet sent. */ | |
1046 | if (!IS_SET_DD_M (dd->flags) && !IS_SET_DD_M (nbr->dd_flags)) | |
1047 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_ExchangeDone); | |
1048 | else | |
1049 | /* Send new DD packet. */ | |
1050 | ospf_db_desc_send (nbr); | |
1051 | } | |
1052 | /* Slave */ | |
1053 | else | |
1054 | { | |
1055 | nbr->dd_seqnum = ntohl (dd->dd_seqnum); | |
1056 | ||
1057 | /* When master's more flags is not set. */ | |
1058 | if (!IS_SET_DD_M (dd->flags) && ospf_db_summary_isempty (nbr)) | |
1059 | { | |
1060 | nbr->dd_flags &= ~(OSPF_DD_FLAG_M); | |
1061 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_ExchangeDone); | |
1062 | } | |
1063 | ||
1064 | /* Send DD pakcet in reply. */ | |
1065 | ospf_db_desc_send (nbr); | |
1066 | } | |
1067 | ||
1068 | /* Save received neighbor values from DD. */ | |
1069 | ospf_db_desc_save_current (nbr, dd); | |
1070 | } | |
1071 | ||
1072 | int | |
1073 | ospf_db_desc_is_dup (struct ospf_db_desc *dd, struct ospf_neighbor *nbr) | |
1074 | { | |
1075 | /* Is DD duplicated? */ | |
1076 | if (dd->options == nbr->last_recv.options && | |
1077 | dd->flags == nbr->last_recv.flags && | |
1078 | dd->dd_seqnum == htonl (nbr->last_recv.dd_seqnum)) | |
1079 | return 1; | |
1080 | ||
1081 | return 0; | |
1082 | } | |
1083 | ||
1084 | /* OSPF Database Description message read -- RFC2328 Section 10.6. */ | |
1085 | void | |
1086 | ospf_db_desc (struct ip *iph, struct ospf_header *ospfh, | |
1087 | struct stream *s, struct ospf_interface *oi, u_int16_t size) | |
1088 | { | |
1089 | struct ospf_db_desc *dd; | |
1090 | struct ospf_neighbor *nbr; | |
1091 | ||
1092 | /* Increment statistics. */ | |
1093 | oi->db_desc_in++; | |
1094 | ||
1095 | dd = (struct ospf_db_desc *) STREAM_PNT (s); | |
d363df2c | 1096 | |
d3f0d621 | 1097 | nbr = ospf_nbr_lookup (oi, iph, ospfh); |
718e3744 | 1098 | if (nbr == NULL) |
1099 | { | |
1100 | zlog_warn ("Packet[DD]: Unknown Neighbor %s", | |
1101 | inet_ntoa (ospfh->router_id)); | |
1102 | return; | |
1103 | } | |
1104 | ||
1105 | /* Check MTU. */ | |
1106 | if (ntohs (dd->mtu) > oi->ifp->mtu) | |
1107 | { | |
1108 | zlog_warn ("Packet[DD]: MTU is larger than [%s]'s MTU", IF_NAME (oi)); | |
1109 | return; | |
1110 | } | |
1111 | ||
d363df2c | 1112 | /* |
1113 | * XXX HACK by Hasso Tepper. Setting N/P bit in NSSA area DD packets is not | |
1114 | * required. In fact at least JunOS sends DD packets with P bit clear. | |
1115 | * Until proper solution is developped, this hack should help. | |
1116 | * | |
1117 | * Update: According to the RFCs, N bit is specified /only/ for Hello | |
1118 | * options, unfortunately its use in DD options is not specified. Hence some | |
1119 | * implementations follow E-bit semantics and set it in DD options, and some | |
1120 | * treat it as unspecified and hence follow the directive "default for | |
1121 | * options is clear", ie unset. | |
1122 | * | |
1123 | * Reset the flag, as ospfd follows E-bit semantics. | |
1124 | */ | |
1125 | if ( (oi->area->external_routing == OSPF_AREA_NSSA) | |
1126 | && (CHECK_FLAG (nbr->options, OSPF_OPTION_NP)) | |
1127 | && (!CHECK_FLAG (dd->options, OSPF_OPTION_NP)) ) | |
1128 | { | |
1129 | if (IS_DEBUG_OSPF_EVENT) | |
3db0a778 | 1130 | zlog_notice ("Packet[DD]: Neighbour %s: Has NSSA capability, sends with N bit clear in DD options", |
d363df2c | 1131 | inet_ntoa (nbr->router_id) ); |
1132 | SET_FLAG (dd->options, OSPF_OPTION_NP); | |
1133 | } | |
d363df2c | 1134 | |
718e3744 | 1135 | #ifdef REJECT_IF_TBIT_ON |
1136 | if (CHECK_FLAG (dd->options, OSPF_OPTION_T)) | |
1137 | { | |
1138 | /* | |
1139 | * In Hello protocol, optional capability must have checked | |
1140 | * to prevent this T-bit enabled router be my neighbor. | |
1141 | */ | |
1142 | zlog_warn ("Packet[DD]: Neighbor %s: T-bit on?", inet_ntoa (nbr->router_id)); | |
1143 | return; | |
1144 | } | |
1145 | #endif /* REJECT_IF_TBIT_ON */ | |
1146 | ||
1147 | #ifdef HAVE_OPAQUE_LSA | |
1148 | if (CHECK_FLAG (dd->options, OSPF_OPTION_O) | |
68980084 | 1149 | && !CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE)) |
718e3744 | 1150 | { |
1151 | /* | |
1152 | * This node is not configured to handle O-bit, for now. | |
1153 | * Clear it to ignore unsupported capability proposed by neighbor. | |
1154 | */ | |
1155 | UNSET_FLAG (dd->options, OSPF_OPTION_O); | |
1156 | } | |
1157 | #endif /* HAVE_OPAQUE_LSA */ | |
1158 | ||
1159 | /* Process DD packet by neighbor status. */ | |
1160 | switch (nbr->state) | |
1161 | { | |
1162 | case NSM_Down: | |
1163 | case NSM_Attempt: | |
1164 | case NSM_TwoWay: | |
1165 | zlog_warn ("Packet[DD]: Neighbor state is %s, packet discarded.", | |
1166 | LOOKUP (ospf_nsm_state_msg, nbr->state)); | |
1167 | break; | |
1168 | case NSM_Init: | |
1169 | OSPF_NSM_EVENT_EXECUTE (nbr, NSM_TwoWayReceived); | |
1170 | /* If the new state is ExStart, the processing of the current | |
1171 | packet should then continue in this new state by falling | |
1172 | through to case ExStart below. */ | |
1173 | if (nbr->state != NSM_ExStart) | |
1174 | break; | |
1175 | case NSM_ExStart: | |
1176 | /* Initial DBD */ | |
1177 | if ((IS_SET_DD_ALL (dd->flags) == OSPF_DD_FLAG_ALL) && | |
1178 | (size == OSPF_DB_DESC_MIN_SIZE)) | |
1179 | { | |
68980084 | 1180 | if (IPV4_ADDR_CMP (&nbr->router_id, &oi->ospf->router_id) > 0) |
718e3744 | 1181 | { |
1182 | /* We're Slave---obey */ | |
1183 | zlog_warn ("Packet[DD]: Negotiation done (Slave)."); | |
1184 | nbr->dd_seqnum = ntohl (dd->dd_seqnum); | |
1185 | nbr->dd_flags &= ~(OSPF_DD_FLAG_MS|OSPF_DD_FLAG_I); /* Reset I/MS */ | |
1186 | } | |
1187 | else | |
1188 | { | |
1189 | /* We're Master, ignore the initial DBD from Slave */ | |
1190 | zlog_warn ("Packet[DD]: Initial DBD from Slave, ignoring."); | |
1191 | break; | |
1192 | } | |
1193 | } | |
1194 | /* Ack from the Slave */ | |
1195 | else if (!IS_SET_DD_MS (dd->flags) && !IS_SET_DD_I (dd->flags) && | |
1196 | ntohl (dd->dd_seqnum) == nbr->dd_seqnum && | |
68980084 | 1197 | IPV4_ADDR_CMP (&nbr->router_id, &oi->ospf->router_id) < 0) |
718e3744 | 1198 | { |
1199 | zlog_warn ("Packet[DD]: Negotiation done (Master)."); | |
1200 | nbr->dd_flags &= ~OSPF_DD_FLAG_I; | |
1201 | } | |
1202 | else | |
1203 | { | |
1204 | zlog_warn ("Packet[DD]: Negotiation fails."); | |
1205 | break; | |
1206 | } | |
1207 | ||
1208 | /* This is where the real Options are saved */ | |
1209 | nbr->options = dd->options; | |
1210 | ||
1211 | #ifdef HAVE_OPAQUE_LSA | |
68980084 | 1212 | if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE)) |
718e3744 | 1213 | { |
1214 | if (IS_DEBUG_OSPF_EVENT) | |
1215 | zlog_info ("Neighbor[%s] is %sOpaque-capable.", | |
1216 | inet_ntoa (nbr->router_id), | |
1217 | CHECK_FLAG (nbr->options, OSPF_OPTION_O) ? "" : "NOT "); | |
1218 | ||
1219 | if (! CHECK_FLAG (nbr->options, OSPF_OPTION_O) | |
1220 | && IPV4_ADDR_SAME (&DR (oi), &nbr->address.u.prefix4)) | |
1221 | { | |
1222 | zlog_warn ("DR-neighbor[%s] is NOT opaque-capable; Opaque-LSAs cannot be reliably advertised in this network.", inet_ntoa (nbr->router_id)); | |
1223 | /* This situation is undesirable, but not a real error. */ | |
1224 | } | |
1225 | } | |
1226 | #endif /* HAVE_OPAQUE_LSA */ | |
1227 | ||
1228 | OSPF_NSM_EVENT_EXECUTE (nbr, NSM_NegotiationDone); | |
1229 | ||
1230 | /* continue processing rest of packet. */ | |
1231 | ospf_db_desc_proc (s, oi, nbr, dd, size); | |
1232 | break; | |
1233 | case NSM_Exchange: | |
1234 | if (ospf_db_desc_is_dup (dd, nbr)) | |
1235 | { | |
1236 | if (IS_SET_DD_MS (nbr->dd_flags)) | |
1237 | /* Master: discard duplicated DD packet. */ | |
1238 | zlog_warn ("Packet[DD] (Master): packet duplicated."); | |
1239 | else | |
1240 | /* Slave: cause to retransmit the last Database Description. */ | |
1241 | { | |
1242 | zlog_warn ("Packet[DD] [Slave]: packet duplicated."); | |
1243 | ospf_db_desc_resend (nbr); | |
1244 | } | |
1245 | break; | |
1246 | } | |
1247 | ||
1248 | /* Otherwise DD packet should be checked. */ | |
1249 | /* Check Master/Slave bit mismatch */ | |
1250 | if (IS_SET_DD_MS (dd->flags) != IS_SET_DD_MS (nbr->last_recv.flags)) | |
1251 | { | |
1252 | zlog_warn ("Packet[DD]: MS-bit mismatch."); | |
1253 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); | |
1254 | if (IS_DEBUG_OSPF_EVENT) | |
1255 | zlog_info ("Packet[DD]: dd->flags=%d, nbr->dd_flags=%d", | |
1256 | dd->flags, nbr->dd_flags); | |
1257 | break; | |
1258 | } | |
1259 | ||
1260 | /* Check initialize bit is set. */ | |
1261 | if (IS_SET_DD_I (dd->flags)) | |
1262 | { | |
1263 | zlog_warn ("Packet[DD]: I-bit set."); | |
1264 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); | |
1265 | break; | |
1266 | } | |
1267 | ||
1268 | /* Check DD Options. */ | |
1269 | if (dd->options != nbr->options) | |
1270 | { | |
1271 | #ifdef ORIGINAL_CODING | |
1272 | /* Save the new options for debugging */ | |
1273 | nbr->options = dd->options; | |
1274 | #endif /* ORIGINAL_CODING */ | |
1275 | zlog_warn ("Packet[DD]: options mismatch."); | |
1276 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); | |
1277 | break; | |
1278 | } | |
1279 | ||
1280 | /* Check DD sequence number. */ | |
1281 | if ((IS_SET_DD_MS (nbr->dd_flags) && | |
1282 | ntohl (dd->dd_seqnum) != nbr->dd_seqnum) || | |
1283 | (!IS_SET_DD_MS (nbr->dd_flags) && | |
1284 | ntohl (dd->dd_seqnum) != nbr->dd_seqnum + 1)) | |
1285 | { | |
1286 | zlog_warn ("Pakcet[DD]: sequence number mismatch."); | |
1287 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); | |
1288 | break; | |
1289 | } | |
1290 | ||
1291 | /* Continue processing rest of packet. */ | |
1292 | ospf_db_desc_proc (s, oi, nbr, dd, size); | |
1293 | break; | |
1294 | case NSM_Loading: | |
1295 | case NSM_Full: | |
1296 | if (ospf_db_desc_is_dup (dd, nbr)) | |
1297 | { | |
1298 | if (IS_SET_DD_MS (nbr->dd_flags)) | |
1299 | { | |
1300 | /* Master should discard duplicate DD packet. */ | |
1301 | zlog_warn ("Pakcet[DD]: duplicated, packet discarded."); | |
1302 | break; | |
1303 | } | |
1304 | else | |
1305 | { | |
1306 | struct timeval t, now; | |
1307 | gettimeofday (&now, NULL); | |
1308 | t = tv_sub (now, nbr->last_send_ts); | |
1309 | if (tv_cmp (t, int2tv (nbr->v_inactivity)) < 0) | |
1310 | { | |
1311 | /* In states Loading and Full the slave must resend | |
1312 | its last Database Description packet in response to | |
1313 | duplicate Database Description packets received | |
1314 | from the master. For this reason the slave must | |
1315 | wait RouterDeadInterval seconds before freeing the | |
1316 | last Database Description packet. Reception of a | |
1317 | Database Description packet from the master after | |
1318 | this interval will generate a SeqNumberMismatch | |
1319 | neighbor event. RFC2328 Section 10.8 */ | |
1320 | ospf_db_desc_resend (nbr); | |
1321 | break; | |
1322 | } | |
1323 | } | |
1324 | } | |
1325 | ||
1326 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); | |
1327 | break; | |
1328 | default: | |
1329 | zlog_warn ("Packet[DD]: NSM illegal status."); | |
1330 | break; | |
1331 | } | |
1332 | } | |
1333 | ||
1334 | #define OSPF_LSA_KEY_SIZE 12 /* type(4) + id(4) + ar(4) */ | |
1335 | ||
1336 | /* OSPF Link State Request Read -- RFC2328 Section 10.7. */ | |
1337 | void | |
1338 | ospf_ls_req (struct ip *iph, struct ospf_header *ospfh, | |
1339 | struct stream *s, struct ospf_interface *oi, u_int16_t size) | |
1340 | { | |
1341 | struct ospf_neighbor *nbr; | |
1342 | u_int32_t ls_type; | |
1343 | struct in_addr ls_id; | |
1344 | struct in_addr adv_router; | |
1345 | struct ospf_lsa *find; | |
52dc7ee6 | 1346 | struct list *ls_upd; |
718e3744 | 1347 | int length; |
1348 | ||
1349 | /* Increment statistics. */ | |
1350 | oi->ls_req_in++; | |
1351 | ||
d3f0d621 | 1352 | nbr = ospf_nbr_lookup (oi, iph, ospfh); |
718e3744 | 1353 | if (nbr == NULL) |
1354 | { | |
1355 | zlog_warn ("Link State Request: Unknown Neighbor %s.", | |
1356 | inet_ntoa (ospfh->router_id)); | |
1357 | return; | |
1358 | } | |
1359 | ||
1360 | /* Neighbor State should be Exchange or later. */ | |
1361 | if (nbr->state != NSM_Exchange && | |
1362 | nbr->state != NSM_Loading && | |
1363 | nbr->state != NSM_Full) | |
1364 | { | |
1365 | zlog_warn ("Link State Request: Neighbor state is %s, packet discarded.", | |
1366 | LOOKUP (ospf_nsm_state_msg, nbr->state)); | |
1367 | return; | |
1368 | } | |
1369 | ||
1370 | /* Send Link State Update for ALL requested LSAs. */ | |
1371 | ls_upd = list_new (); | |
1372 | length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE; | |
1373 | ||
1374 | while (size >= OSPF_LSA_KEY_SIZE) | |
1375 | { | |
1376 | /* Get one slice of Link State Request. */ | |
1377 | ls_type = stream_getl (s); | |
1378 | ls_id.s_addr = stream_get_ipv4 (s); | |
1379 | adv_router.s_addr = stream_get_ipv4 (s); | |
1380 | ||
1381 | /* Verify LSA type. */ | |
1382 | if (ls_type < OSPF_MIN_LSA || ls_type >= OSPF_MAX_LSA) | |
1383 | { | |
1384 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq); | |
1385 | list_delete (ls_upd); | |
1386 | return; | |
1387 | } | |
1388 | ||
1389 | /* Search proper LSA in LSDB. */ | |
1390 | find = ospf_lsa_lookup (oi->area, ls_type, ls_id, adv_router); | |
1391 | if (find == NULL) | |
1392 | { | |
1393 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq); | |
1394 | list_delete (ls_upd); | |
1395 | return; | |
1396 | } | |
1397 | ||
1398 | /* Packet overflows MTU size, send immediatly. */ | |
1399 | if (length + ntohs (find->data->length) > OSPF_PACKET_MAX (oi)) | |
1400 | { | |
1401 | if (oi->type == OSPF_IFTYPE_NBMA) | |
1402 | ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_DIRECT); | |
1403 | else | |
1404 | ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_INDIRECT); | |
1405 | ||
1406 | /* Only remove list contents. Keep ls_upd. */ | |
1407 | list_delete_all_node (ls_upd); | |
1408 | ||
1409 | length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE; | |
1410 | } | |
1411 | ||
1412 | /* Append LSA to update list. */ | |
1413 | listnode_add (ls_upd, find); | |
1414 | length += ntohs (find->data->length); | |
1415 | ||
1416 | size -= OSPF_LSA_KEY_SIZE; | |
1417 | } | |
1418 | ||
1419 | /* Send rest of Link State Update. */ | |
1420 | if (listcount (ls_upd) > 0) | |
1421 | { | |
1422 | if (oi->type == OSPF_IFTYPE_NBMA) | |
1423 | ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_DIRECT); | |
1424 | else | |
1425 | ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_INDIRECT); | |
1426 | ||
1427 | list_delete (ls_upd); | |
1428 | } | |
1429 | else | |
1430 | list_free (ls_upd); | |
1431 | } | |
1432 | ||
1433 | /* Get the list of LSAs from Link State Update packet. | |
1434 | And process some validation -- RFC2328 Section 13. (1)-(2). */ | |
52dc7ee6 | 1435 | static struct list * |
718e3744 | 1436 | ospf_ls_upd_list_lsa (struct ospf_neighbor *nbr, struct stream *s, |
1437 | struct ospf_interface *oi, size_t size) | |
1438 | { | |
1439 | u_int16_t count, sum; | |
1440 | u_int32_t length; | |
1441 | struct lsa_header *lsah; | |
1442 | struct ospf_lsa *lsa; | |
52dc7ee6 | 1443 | struct list *lsas; |
718e3744 | 1444 | |
1445 | lsas = list_new (); | |
1446 | ||
1447 | count = stream_getl (s); | |
1448 | size -= OSPF_LS_UPD_MIN_SIZE; /* # LSAs */ | |
1449 | ||
1450 | for (; size >= OSPF_LSA_HEADER_SIZE && count > 0; | |
1451 | size -= length, stream_forward (s, length), count--) | |
1452 | { | |
1453 | lsah = (struct lsa_header *) STREAM_PNT (s); | |
1454 | length = ntohs (lsah->length); | |
1455 | ||
1456 | if (length > size) | |
1457 | { | |
1458 | zlog_warn ("Link State Update: LSA length exceeds packet size."); | |
1459 | break; | |
1460 | } | |
1461 | ||
1462 | /* Validate the LSA's LS checksum. */ | |
1463 | sum = lsah->checksum; | |
1464 | if (sum != ospf_lsa_checksum (lsah)) | |
1465 | { | |
1466 | zlog_warn ("Link State Update: LSA checksum error %x, %x.", | |
1467 | sum, lsah->checksum); | |
1468 | continue; | |
1469 | } | |
1470 | ||
1471 | /* Examine the LSA's LS type. */ | |
1472 | if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA) | |
1473 | { | |
1474 | zlog_warn ("Link State Update: Unknown LS type %d", lsah->type); | |
1475 | continue; | |
1476 | } | |
1477 | ||
1478 | /* | |
1479 | * What if the received LSA's age is greater than MaxAge? | |
1480 | * Treat it as a MaxAge case -- endo. | |
1481 | */ | |
1482 | if (ntohs (lsah->ls_age) > OSPF_LSA_MAXAGE) | |
1483 | lsah->ls_age = htons (OSPF_LSA_MAXAGE); | |
1484 | ||
1485 | #ifdef HAVE_OPAQUE_LSA | |
1486 | if (CHECK_FLAG (nbr->options, OSPF_OPTION_O)) | |
1487 | { | |
1488 | #ifdef STRICT_OBIT_USAGE_CHECK | |
1489 | if ((IS_OPAQUE_LSA(lsah->type) && | |
1490 | ! CHECK_FLAG (lsah->options, OSPF_OPTION_O)) | |
1491 | || (! IS_OPAQUE_LSA(lsah->type) && | |
1492 | CHECK_FLAG (lsah->options, OSPF_OPTION_O))) | |
1493 | { | |
1494 | /* | |
1495 | * This neighbor must know the exact usage of O-bit; | |
1496 | * the bit will be set in Type-9,10,11 LSAs only. | |
1497 | */ | |
1498 | zlog_warn ("LSA[Type%d:%s]: O-bit abuse?", lsah->type, inet_ntoa (lsah->id)); | |
1499 | continue; | |
1500 | } | |
1501 | #endif /* STRICT_OBIT_USAGE_CHECK */ | |
1502 | ||
1503 | /* Do not take in AS External Opaque-LSAs if we are a stub. */ | |
1504 | if (lsah->type == OSPF_OPAQUE_AS_LSA | |
1505 | && nbr->oi->area->external_routing != OSPF_AREA_DEFAULT) | |
1506 | { | |
1507 | if (IS_DEBUG_OSPF_EVENT) | |
1508 | zlog_info ("LSA[Type%d:%s]: We are a stub, don't take this LSA.", lsah->type, inet_ntoa (lsah->id)); | |
1509 | continue; | |
1510 | } | |
1511 | } | |
1512 | else if (IS_OPAQUE_LSA(lsah->type)) | |
1513 | { | |
1514 | zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah->type, inet_ntoa (lsah->id)); | |
1515 | continue; | |
1516 | } | |
1517 | #endif /* HAVE_OPAQUE_LSA */ | |
1518 | ||
1519 | /* Create OSPF LSA instance. */ | |
1520 | lsa = ospf_lsa_new (); | |
1521 | ||
1522 | /* We may wish to put some error checking if type NSSA comes in | |
1523 | and area not in NSSA mode */ | |
1524 | switch (lsah->type) | |
1525 | { | |
1526 | case OSPF_AS_EXTERNAL_LSA: | |
1527 | #ifdef HAVE_OPAQUE_LSA | |
1528 | case OSPF_OPAQUE_AS_LSA: | |
1529 | lsa->area = NULL; | |
1530 | break; | |
1531 | case OSPF_OPAQUE_LINK_LSA: | |
1532 | lsa->oi = oi; /* Remember incoming interface for flooding control. */ | |
1533 | /* Fallthrough */ | |
1534 | #endif /* HAVE_OPAQUE_LSA */ | |
1535 | default: | |
1536 | lsa->area = oi->area; | |
1537 | break; | |
1538 | } | |
1539 | ||
1540 | lsa->data = ospf_lsa_data_new (length); | |
1541 | memcpy (lsa->data, lsah, length); | |
1542 | ||
1543 | if (IS_DEBUG_OSPF_EVENT) | |
1544 | zlog_info("LSA[Type%d:%s]: %p new LSA created with Link State Update", | |
1545 | lsa->data->type, inet_ntoa (lsa->data->id), lsa); | |
1546 | listnode_add (lsas, lsa); | |
1547 | } | |
1548 | ||
1549 | return lsas; | |
1550 | } | |
1551 | ||
1552 | /* Cleanup Update list. */ | |
1553 | void | |
52dc7ee6 | 1554 | ospf_upd_list_clean (struct list *lsas) |
718e3744 | 1555 | { |
52dc7ee6 | 1556 | struct listnode *node; |
718e3744 | 1557 | struct ospf_lsa *lsa; |
1558 | ||
1559 | for (node = listhead (lsas); node; nextnode (node)) | |
1560 | if ((lsa = getdata (node)) != NULL) | |
1561 | ospf_lsa_discard (lsa); | |
1562 | ||
1563 | list_delete (lsas); | |
1564 | } | |
1565 | ||
1566 | /* OSPF Link State Update message read -- RFC2328 Section 13. */ | |
1567 | void | |
1568 | ospf_ls_upd (struct ip *iph, struct ospf_header *ospfh, | |
1569 | struct stream *s, struct ospf_interface *oi, u_int16_t size) | |
1570 | { | |
1571 | struct ospf_neighbor *nbr; | |
52dc7ee6 | 1572 | struct list *lsas; |
718e3744 | 1573 | #ifdef HAVE_OPAQUE_LSA |
52dc7ee6 | 1574 | struct list *mylsa_acks, *mylsa_upds; |
718e3744 | 1575 | #endif /* HAVE_OPAQUE_LSA */ |
52dc7ee6 | 1576 | struct listnode *node, *next; |
718e3744 | 1577 | struct ospf_lsa *lsa = NULL; |
1578 | /* unsigned long ls_req_found = 0; */ | |
1579 | ||
1580 | /* Dis-assemble the stream, update each entry, re-encapsulate for flooding */ | |
1581 | ||
1582 | /* Increment statistics. */ | |
1583 | oi->ls_upd_in++; | |
1584 | ||
1585 | /* Check neighbor. */ | |
d3f0d621 | 1586 | nbr = ospf_nbr_lookup (oi, iph, ospfh); |
718e3744 | 1587 | if (nbr == NULL) |
1588 | { | |
1589 | zlog_warn ("Link State Update: Unknown Neighbor %s on int: %s", | |
1590 | inet_ntoa (ospfh->router_id), IF_NAME (oi)); | |
1591 | return; | |
1592 | } | |
1593 | ||
1594 | /* Check neighbor state. */ | |
1595 | if (nbr->state < NSM_Exchange) | |
1596 | { | |
1597 | zlog_warn ("Link State Update: Neighbor[%s] state is less than Exchange", | |
1598 | inet_ntoa (ospfh->router_id)); | |
1599 | return; | |
1600 | } | |
1601 | ||
1602 | /* Get list of LSAs from Link State Update packet. - Also perorms Stages | |
1603 | * 1 (validate LSA checksum) and 2 (check for LSA consistent type) | |
1604 | * of section 13. | |
1605 | */ | |
1606 | lsas = ospf_ls_upd_list_lsa (nbr, s, oi, size); | |
1607 | ||
1608 | #ifdef HAVE_OPAQUE_LSA | |
1609 | /* | |
1610 | * Prepare two kinds of lists to clean up unwanted self-originated | |
1611 | * Opaque-LSAs from the routing domain as soon as possible. | |
1612 | */ | |
1613 | mylsa_acks = list_new (); /* Let the sender cease retransmission. */ | |
1614 | mylsa_upds = list_new (); /* Flush target LSAs if necessary. */ | |
1615 | ||
1616 | /* | |
1617 | * If self-originated Opaque-LSAs that have flooded before restart | |
1618 | * are contained in the received LSUpd message, corresponding LSReq | |
1619 | * messages to be sent may have to be modified. | |
1620 | * To eliminate possible race conditions such that flushing and normal | |
1621 | * updating for the same LSA would take place alternately, this trick | |
1622 | * must be done before entering to the loop below. | |
1623 | */ | |
1624 | ospf_opaque_adjust_lsreq (nbr, lsas); | |
1625 | #endif /* HAVE_OPAQUE_LSA */ | |
1626 | ||
1627 | #define DISCARD_LSA(L,N) {\ | |
1628 | if (IS_DEBUG_OSPF_EVENT) \ | |
1629 | zlog_info ("ospf_lsa_discard() in ospf_ls_upd() point %d: lsa %p Type-%d", N, lsa, (int) lsa->data->type); \ | |
1630 | ospf_lsa_discard (L); \ | |
1631 | continue; } | |
1632 | ||
1633 | /* Process each LSA received in the one packet. */ | |
1634 | for (node = listhead (lsas); node; node = next) | |
1635 | { | |
1636 | struct ospf_lsa *ls_ret, *current; | |
1637 | int ret = 1; | |
1638 | ||
1639 | next = node->next; | |
1640 | ||
1641 | lsa = getdata (node); | |
1642 | ||
718e3744 | 1643 | if (IS_DEBUG_OSPF_NSSA) |
1644 | { | |
1645 | char buf1[INET_ADDRSTRLEN]; | |
1646 | char buf2[INET_ADDRSTRLEN]; | |
1647 | char buf3[INET_ADDRSTRLEN]; | |
1648 | ||
1649 | zlog_info("LSA Type-%d from %s, ID: %s, ADV: %s", | |
1650 | lsa->data->type, | |
1651 | inet_ntop (AF_INET, &ospfh->router_id, | |
1652 | buf1, INET_ADDRSTRLEN), | |
1653 | inet_ntop (AF_INET, &lsa->data->id, | |
1654 | buf2, INET_ADDRSTRLEN), | |
1655 | inet_ntop (AF_INET, &lsa->data->adv_router, | |
1656 | buf3, INET_ADDRSTRLEN)); | |
1657 | } | |
718e3744 | 1658 | |
1659 | listnode_delete (lsas, lsa); /* We don't need it in list anymore */ | |
1660 | ||
1661 | /* Validate Checksum - Done above by ospf_ls_upd_list_lsa() */ | |
1662 | ||
1663 | /* LSA Type - Done above by ospf_ls_upd_list_lsa() */ | |
1664 | ||
1665 | /* Do not take in AS External LSAs if we are a stub or NSSA. */ | |
1666 | ||
1667 | /* Do not take in AS NSSA if this neighbor and we are not NSSA */ | |
1668 | ||
1669 | /* Do take in Type-7's if we are an NSSA */ | |
1670 | ||
1671 | /* If we are also an ABR, later translate them to a Type-5 packet */ | |
1672 | ||
1673 | /* Later, an NSSA Re-fresh can Re-fresh Type-7's and an ABR will | |
1674 | translate them to a separate Type-5 packet. */ | |
1675 | ||
1676 | if (lsa->data->type == OSPF_AS_EXTERNAL_LSA) | |
1677 | /* Reject from STUB or NSSA */ | |
1678 | if (nbr->oi->area->external_routing != OSPF_AREA_DEFAULT) | |
1679 | { | |
1680 | DISCARD_LSA (lsa, 1); | |
718e3744 | 1681 | if (IS_DEBUG_OSPF_NSSA) |
1682 | zlog_info("Incoming External LSA Discarded: We are NSSA/STUB Area"); | |
718e3744 | 1683 | } |
1684 | ||
718e3744 | 1685 | if (lsa->data->type == OSPF_AS_NSSA_LSA) |
1686 | if (nbr->oi->area->external_routing != OSPF_AREA_NSSA) | |
1687 | { | |
1688 | DISCARD_LSA (lsa,2); | |
1689 | if (IS_DEBUG_OSPF_NSSA) | |
1690 | zlog_info("Incoming NSSA LSA Discarded: Not NSSA Area"); | |
1691 | } | |
718e3744 | 1692 | |
1693 | /* Find the LSA in the current database. */ | |
1694 | ||
1695 | current = ospf_lsa_lookup_by_header (oi->area, lsa->data); | |
1696 | ||
1697 | /* If the LSA's LS age is equal to MaxAge, and there is currently | |
1698 | no instance of the LSA in the router's link state database, | |
1699 | and none of router's neighbors are in states Exchange or Loading, | |
1700 | then take the following actions. */ | |
1701 | ||
1702 | if (IS_LSA_MAXAGE (lsa) && !current && | |
68980084 | 1703 | (ospf_nbr_count (oi, NSM_Exchange) + |
1704 | ospf_nbr_count (oi, NSM_Loading)) == 0) | |
718e3744 | 1705 | { |
1706 | /* Response Link State Acknowledgment. */ | |
1707 | ospf_ls_ack_send (nbr, lsa); | |
1708 | ||
1709 | /* Discard LSA. */ | |
1710 | zlog_warn ("Link State Update: LS age is equal to MaxAge."); | |
1711 | DISCARD_LSA (lsa, 3); | |
1712 | } | |
1713 | ||
1714 | #ifdef HAVE_OPAQUE_LSA | |
1715 | if (IS_OPAQUE_LSA (lsa->data->type) | |
68980084 | 1716 | && IPV4_ADDR_SAME (&lsa->data->adv_router, &oi->ospf->router_id)) |
718e3744 | 1717 | { |
1718 | /* | |
1719 | * Even if initial flushing seems to be completed, there might | |
1720 | * be a case that self-originated LSA with MaxAge still remain | |
1721 | * in the routing domain. | |
1722 | * Just send an LSAck message to cease retransmission. | |
1723 | */ | |
1724 | if (IS_LSA_MAXAGE (lsa)) | |
1725 | { | |
1726 | zlog_warn ("LSA[%s]: Boomerang effect?", dump_lsa_key (lsa)); | |
1727 | ospf_ls_ack_send (nbr, lsa); | |
1728 | ospf_lsa_discard (lsa); | |
1729 | ||
1730 | if (current != NULL && ! IS_LSA_MAXAGE (current)) | |
1731 | ospf_opaque_lsa_refresh_schedule (current); | |
1732 | continue; | |
1733 | } | |
1734 | ||
1735 | /* | |
1736 | * If an instance of self-originated Opaque-LSA is not found | |
1737 | * in the LSDB, there are some possible cases here. | |
1738 | * | |
1739 | * 1) This node lost opaque-capability after restart. | |
1740 | * 2) Else, a part of opaque-type is no more supported. | |
1741 | * 3) Else, a part of opaque-id is no more supported. | |
1742 | * | |
1743 | * Anyway, it is still this node's responsibility to flush it. | |
1744 | * Otherwise, the LSA instance remains in the routing domain | |
1745 | * until its age reaches to MaxAge. | |
1746 | */ | |
1747 | if (current == NULL) | |
1748 | { | |
1749 | if (IS_DEBUG_OSPF_EVENT) | |
1750 | zlog_info ("LSA[%s]: Previously originated Opaque-LSA, not found in the LSDB.", dump_lsa_key (lsa)); | |
1751 | ||
1752 | SET_FLAG (lsa->flags, OSPF_LSA_SELF); | |
1753 | listnode_add (mylsa_upds, ospf_lsa_dup (lsa)); | |
1754 | listnode_add (mylsa_acks, ospf_lsa_lock (lsa)); | |
1755 | continue; | |
1756 | } | |
1757 | } | |
1758 | #endif /* HAVE_OPAQUE_LSA */ | |
cb05eb28 | 1759 | /* It might be happen that received LSA is self-originated network LSA, but |
1760 | * router ID is cahnged. So, we should check if LSA is a network-LSA whose | |
1761 | * Link State ID is one of the router's own IP interface addresses but whose | |
1762 | * Advertising Router is not equal to the router's own Router ID | |
1763 | * According to RFC 2328 12.4.2 and 13.4 this LSA should be flushed. | |
1764 | */ | |
1765 | ||
1766 | if(lsa->data->type == OSPF_NETWORK_LSA) | |
1767 | { | |
52dc7ee6 | 1768 | struct listnode *oi_node; |
cb05eb28 | 1769 | int Flag = 0; |
1770 | ||
1771 | for(oi_node = listhead(oi->ospf->oiflist); oi_node; oi_node = nextnode(oi_node)) | |
1772 | { | |
1773 | struct ospf_interface *out_if = getdata(oi_node); | |
1774 | if(out_if == NULL) | |
1775 | break; | |
1776 | ||
1777 | if((IPV4_ADDR_SAME(&out_if->address->u.prefix4, &lsa->data->id)) && | |
1778 | (!(IPV4_ADDR_SAME(&oi->ospf->router_id, &lsa->data->adv_router)))) | |
1779 | { | |
1780 | if(out_if->network_lsa_self) | |
1781 | { | |
1782 | ospf_lsa_flush_area(lsa,out_if->area); | |
1783 | if(IS_DEBUG_OSPF_EVENT) | |
1784 | zlog_info ("ospf_lsa_discard() in ospf_ls_upd() point 9: lsa %p Type-%d", | |
1785 | lsa, (int) lsa->data->type); | |
1786 | ospf_lsa_discard (lsa); | |
1787 | Flag = 1; | |
1788 | } | |
1789 | break; | |
1790 | } | |
1791 | } | |
1792 | if(Flag) | |
1793 | continue; | |
1794 | } | |
718e3744 | 1795 | |
1796 | /* (5) Find the instance of this LSA that is currently contained | |
1797 | in the router's link state database. If there is no | |
1798 | database copy, or the received LSA is more recent than | |
1799 | the database copy the following steps must be performed. */ | |
1800 | ||
1801 | if (current == NULL || | |
1802 | (ret = ospf_lsa_more_recent (current, lsa)) < 0) | |
1803 | { | |
1804 | /* Actual flooding procedure. */ | |
68980084 | 1805 | if (ospf_flood (oi->ospf, nbr, current, lsa) < 0) /* Trap NSSA later. */ |
718e3744 | 1806 | DISCARD_LSA (lsa, 4); |
1807 | continue; | |
1808 | } | |
1809 | ||
1810 | /* (6) Else, If there is an instance of the LSA on the sending | |
1811 | neighbor's Link state request list, an error has occurred in | |
1812 | the Database Exchange process. In this case, restart the | |
1813 | Database Exchange process by generating the neighbor event | |
1814 | BadLSReq for the sending neighbor and stop processing the | |
1815 | Link State Update packet. */ | |
1816 | ||
1817 | if (ospf_ls_request_lookup (nbr, lsa)) | |
1818 | { | |
1819 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq); | |
1820 | zlog_warn ("LSA instance exists on Link state request list"); | |
1821 | ||
1822 | /* Clean list of LSAs. */ | |
1823 | ospf_upd_list_clean (lsas); | |
1824 | /* this lsa is not on lsas list already. */ | |
1825 | ospf_lsa_discard (lsa); | |
1826 | #ifdef HAVE_OPAQUE_LSA | |
1827 | list_delete (mylsa_acks); | |
1828 | list_delete (mylsa_upds); | |
1829 | #endif /* HAVE_OPAQUE_LSA */ | |
1830 | return; | |
1831 | } | |
1832 | ||
1833 | /* If the received LSA is the same instance as the database copy | |
1834 | (i.e., neither one is more recent) the following two steps | |
1835 | should be performed: */ | |
1836 | ||
1837 | if (ret == 0) | |
1838 | { | |
1839 | /* If the LSA is listed in the Link state retransmission list | |
1840 | for the receiving adjacency, the router itself is expecting | |
1841 | an acknowledgment for this LSA. The router should treat the | |
1842 | received LSA as an acknowledgment by removing the LSA from | |
1843 | the Link state retransmission list. This is termed an | |
1844 | "implied acknowledgment". */ | |
1845 | ||
1846 | ls_ret = ospf_ls_retransmit_lookup (nbr, lsa); | |
1847 | ||
1848 | if (ls_ret != NULL) | |
1849 | { | |
1850 | ospf_ls_retransmit_delete (nbr, ls_ret); | |
1851 | ||
1852 | /* Delayed acknowledgment sent if advertisement received | |
1853 | from Designated Router, otherwise do nothing. */ | |
1854 | if (oi->state == ISM_Backup) | |
1855 | if (NBR_IS_DR (nbr)) | |
1856 | listnode_add (oi->ls_ack, ospf_lsa_lock (lsa)); | |
1857 | ||
1858 | DISCARD_LSA (lsa, 5); | |
1859 | } | |
1860 | else | |
1861 | /* Acknowledge the receipt of the LSA by sending a | |
1862 | Link State Acknowledgment packet back out the receiving | |
1863 | interface. */ | |
1864 | { | |
1865 | ospf_ls_ack_send (nbr, lsa); | |
1866 | DISCARD_LSA (lsa, 6); | |
1867 | } | |
1868 | } | |
1869 | ||
1870 | /* The database copy is more recent. If the database copy | |
1871 | has LS age equal to MaxAge and LS sequence number equal to | |
1872 | MaxSequenceNumber, simply discard the received LSA without | |
1873 | acknowledging it. (In this case, the LSA's LS sequence number is | |
1874 | wrapping, and the MaxSequenceNumber LSA must be completely | |
1875 | flushed before any new LSA instance can be introduced). */ | |
1876 | ||
1877 | else if (ret > 0) /* Database copy is more recent */ | |
1878 | { | |
1879 | if (IS_LSA_MAXAGE (current) && | |
1880 | current->data->ls_seqnum == htonl (OSPF_MAX_SEQUENCE_NUMBER)) | |
1881 | { | |
1882 | DISCARD_LSA (lsa, 7); | |
1883 | } | |
1884 | /* Otherwise, as long as the database copy has not been sent in a | |
1885 | Link State Update within the last MinLSArrival seconds, send the | |
1886 | database copy back to the sending neighbor, encapsulated within | |
1887 | a Link State Update Packet. The Link State Update Packet should | |
1888 | be sent directly to the neighbor. In so doing, do not put the | |
1889 | database copy of the LSA on the neighbor's link state | |
1890 | retransmission list, and do not acknowledge the received (less | |
1891 | recent) LSA instance. */ | |
1892 | else | |
1893 | { | |
1894 | struct timeval now; | |
1895 | ||
1896 | gettimeofday (&now, NULL); | |
1897 | ||
1898 | if (tv_cmp (tv_sub (now, current->tv_orig), | |
1899 | int2tv (OSPF_MIN_LS_ARRIVAL)) > 0) | |
1900 | /* Trap NSSA type later.*/ | |
1901 | ospf_ls_upd_send_lsa (nbr, current, OSPF_SEND_PACKET_DIRECT); | |
1902 | DISCARD_LSA (lsa, 8); | |
1903 | } | |
1904 | } | |
1905 | } | |
1906 | ||
1907 | #ifdef HAVE_OPAQUE_LSA | |
1908 | /* | |
1909 | * Now that previously originated Opaque-LSAs those which not yet | |
1910 | * installed into LSDB are captured, take several steps to clear | |
1911 | * them completely from the routing domain, before proceeding to | |
1912 | * origination for the current target Opaque-LSAs. | |
1913 | */ | |
1914 | while (listcount (mylsa_acks) > 0) | |
1915 | ospf_ls_ack_send_list (oi, mylsa_acks, nbr->address.u.prefix4); | |
1916 | ||
1917 | if (listcount (mylsa_upds) > 0) | |
1918 | ospf_opaque_self_originated_lsa_received (nbr, mylsa_upds); | |
1919 | ||
1920 | list_delete (mylsa_upds); | |
683b2265 | 1921 | list_delete (mylsa_acks); |
718e3744 | 1922 | #endif /* HAVE_OPAQUE_LSA */ |
1923 | ||
1924 | assert (listcount (lsas) == 0); | |
1925 | list_delete (lsas); | |
1926 | } | |
1927 | ||
1928 | /* OSPF Link State Acknowledgment message read -- RFC2328 Section 13.7. */ | |
1929 | void | |
1930 | ospf_ls_ack (struct ip *iph, struct ospf_header *ospfh, | |
1931 | struct stream *s, struct ospf_interface *oi, u_int16_t size) | |
1932 | { | |
1933 | struct ospf_neighbor *nbr; | |
1934 | #ifdef HAVE_OPAQUE_LSA | |
87d6f87a | 1935 | struct list *opaque_acks; |
718e3744 | 1936 | #endif /* HAVE_OPAQUE_LSA */ |
1937 | ||
1938 | /* increment statistics. */ | |
1939 | oi->ls_ack_in++; | |
1940 | ||
d3f0d621 | 1941 | nbr = ospf_nbr_lookup (oi, iph, ospfh); |
718e3744 | 1942 | if (nbr == NULL) |
1943 | { | |
1944 | zlog_warn ("Link State Acknowledgment: Unknown Neighbor %s.", | |
1945 | inet_ntoa (ospfh->router_id)); | |
1946 | return; | |
1947 | } | |
1948 | ||
1949 | if (nbr->state < NSM_Exchange) | |
1950 | { | |
1951 | zlog_warn ("Link State Acknowledgment: State is less than Exchange."); | |
1952 | return; | |
1953 | } | |
1954 | ||
1955 | #ifdef HAVE_OPAQUE_LSA | |
1956 | opaque_acks = list_new (); | |
1957 | #endif /* HAVE_OPAQUE_LSA */ | |
1958 | ||
1959 | while (size >= OSPF_LSA_HEADER_SIZE) | |
1960 | { | |
1961 | struct ospf_lsa *lsa, *lsr; | |
1962 | ||
1963 | lsa = ospf_lsa_new (); | |
1964 | lsa->data = (struct lsa_header *) STREAM_PNT (s); | |
1965 | ||
1966 | /* lsah = (struct lsa_header *) STREAM_PNT (s); */ | |
1967 | size -= OSPF_LSA_HEADER_SIZE; | |
1968 | stream_forward (s, OSPF_LSA_HEADER_SIZE); | |
1969 | ||
1970 | if (lsa->data->type < OSPF_MIN_LSA || lsa->data->type >= OSPF_MAX_LSA) | |
1971 | { | |
1972 | lsa->data = NULL; | |
1973 | ospf_lsa_discard (lsa); | |
1974 | continue; | |
1975 | } | |
1976 | ||
1977 | lsr = ospf_ls_retransmit_lookup (nbr, lsa); | |
1978 | ||
1979 | if (lsr != NULL && lsr->data->ls_seqnum == lsa->data->ls_seqnum) | |
1980 | { | |
1981 | #ifdef HAVE_OPAQUE_LSA | |
1982 | /* Keep this LSA entry for later reference. */ | |
1983 | if (IS_OPAQUE_LSA (lsr->data->type)) | |
1984 | listnode_add (opaque_acks, ospf_lsa_dup (lsr)); | |
1985 | #endif /* HAVE_OPAQUE_LSA */ | |
1986 | ||
1987 | ospf_ls_retransmit_delete (nbr, lsr); | |
1988 | } | |
1989 | ||
1990 | lsa->data = NULL; | |
1991 | ospf_lsa_discard (lsa); | |
1992 | } | |
1993 | ||
1994 | #ifdef HAVE_OPAQUE_LSA | |
1995 | if (listcount (opaque_acks) > 0) | |
1996 | ospf_opaque_ls_ack_received (nbr, opaque_acks); | |
1997 | ||
1998 | list_delete (opaque_acks); | |
1999 | return; | |
2000 | #endif /* HAVE_OPAQUE_LSA */ | |
2001 | } | |
2002 | \f | |
2003 | struct stream * | |
2004 | ospf_recv_packet (int fd, struct interface **ifp) | |
2005 | { | |
2006 | int ret; | |
2007 | struct ip iph; | |
2008 | u_int16_t ip_len; | |
2009 | struct stream *ibuf; | |
2010 | unsigned int ifindex = 0; | |
2011 | struct iovec iov; | |
2012 | struct cmsghdr *cmsg; | |
d0deca68 | 2013 | #if defined(CMSG_SPACE) |
2014 | /* Header and data both require alignment. */ | |
e304982e | 2015 | char buff [CMSG_SPACE(SOPT_SIZE_CMSG_IFINDEX_IPV4())]; |
d0deca68 | 2016 | #else |
2017 | char buff [sizeof (*cmsg) + SOPT_SIZE_CMSG_IFINDEX_IPV4()]; | |
2018 | #endif | |
2dd8bb4e | 2019 | struct msghdr msgh; |
2020 | ||
68defd6d | 2021 | memset (&msgh, 0, sizeof (struct msghdr)); |
2dd8bb4e | 2022 | msgh.msg_iov = &iov; |
2023 | msgh.msg_iovlen = 1; | |
2024 | msgh.msg_control = (caddr_t) buff; | |
2025 | msgh.msg_controllen = sizeof (buff); | |
2dd8bb4e | 2026 | |
718e3744 | 2027 | ret = recvfrom (fd, (void *)&iph, sizeof (iph), MSG_PEEK, NULL, 0); |
2028 | ||
2029 | if (ret != sizeof (iph)) | |
2030 | { | |
2031 | zlog_warn ("ospf_recv_packet packet smaller than ip header"); | |
2032 | return NULL; | |
2033 | } | |
2034 | ||
239aecc0 | 2035 | #if defined(__NetBSD__) || defined(__FreeBSD__) || (defined(__OpenBSD__) && (OpenBSD < 200311)) |
718e3744 | 2036 | ip_len = iph.ip_len; |
2037 | #else | |
2038 | ip_len = ntohs (iph.ip_len); | |
2039 | #endif | |
2040 | ||
239aecc0 | 2041 | #if !defined(GNU_LINUX) && (OpenBSD < 200311) |
718e3744 | 2042 | /* |
2043 | * Kernel network code touches incoming IP header parameters, | |
2044 | * before protocol specific processing. | |
2045 | * | |
2046 | * 1) Convert byteorder to host representation. | |
2047 | * --> ip_len, ip_id, ip_off | |
2048 | * | |
2049 | * 2) Adjust ip_len to strip IP header size! | |
2050 | * --> If user process receives entire IP packet via RAW | |
2051 | * socket, it must consider adding IP header size to | |
2052 | * the "ip_len" field of "ip" structure. | |
2053 | * | |
2054 | * For more details, see <netinet/ip_input.c>. | |
2055 | */ | |
2056 | ip_len = ip_len + (iph.ip_hl << 2); | |
2057 | #endif | |
2058 | ||
2059 | ibuf = stream_new (ip_len); | |
2060 | iov.iov_base = STREAM_DATA (ibuf); | |
2061 | iov.iov_len = ip_len; | |
2062 | ret = recvmsg (fd, &msgh, 0); | |
2063 | ||
863082d5 | 2064 | ifindex = getsockopt_ifindex (AF_INET, &msgh); |
718e3744 | 2065 | |
2066 | *ifp = if_lookup_by_index (ifindex); | |
2067 | ||
2068 | if (ret != ip_len) | |
2069 | { | |
2070 | zlog_warn ("ospf_recv_packet short read. " | |
2071 | "ip_len %d bytes read %d", ip_len, ret); | |
2072 | stream_free (ibuf); | |
2073 | return NULL; | |
2074 | } | |
2075 | ||
2076 | return ibuf; | |
2077 | } | |
2078 | ||
2079 | struct ospf_interface * | |
d3f0d621 | 2080 | ospf_associate_packet_vl (struct ospf *ospf, struct interface *ifp, |
718e3744 | 2081 | struct ip *iph, struct ospf_header *ospfh) |
2082 | { | |
2083 | struct ospf_interface *rcv_oi; | |
718e3744 | 2084 | struct ospf_vl_data *vl_data; |
2085 | struct ospf_area *vl_area; | |
52dc7ee6 | 2086 | struct listnode *node; |
718e3744 | 2087 | |
2088 | if (IN_MULTICAST (ntohl (iph->ip_dst.s_addr)) || | |
2089 | !OSPF_IS_AREA_BACKBONE (ospfh)) | |
d3f0d621 | 2090 | return NULL; |
718e3744 | 2091 | |
d3f0d621 | 2092 | /* look for local OSPF interface matching the destination |
2093 | * to determine Area ID. We presume therefore the destination address | |
2094 | * is unique, or at least (for "unnumbered" links), not used in other | |
2095 | * areas | |
2096 | */ | |
2097 | if ((rcv_oi = ospf_if_lookup_by_local_addr (ospf, NULL, | |
2098 | iph->ip_dst)) == NULL) | |
2099 | return NULL; | |
718e3744 | 2100 | |
020709f9 | 2101 | for (node = listhead (ospf->vlinks); node; nextnode (node)) |
718e3744 | 2102 | { |
2103 | if ((vl_data = getdata (node)) == NULL) | |
2104 | continue; | |
2105 | ||
020709f9 | 2106 | vl_area = ospf_area_lookup_by_area_id (ospf, vl_data->vl_area_id); |
718e3744 | 2107 | if (!vl_area) |
2108 | continue; | |
2109 | ||
2110 | if (OSPF_AREA_SAME (&vl_area, &rcv_oi->area) && | |
2111 | IPV4_ADDR_SAME (&vl_data->vl_peer, &ospfh->router_id)) | |
2112 | { | |
2113 | if (IS_DEBUG_OSPF_EVENT) | |
2114 | zlog_info ("associating packet with %s", | |
2115 | IF_NAME (vl_data->vl_oi)); | |
2116 | if (! CHECK_FLAG (vl_data->vl_oi->ifp->flags, IFF_UP)) | |
2117 | { | |
2118 | if (IS_DEBUG_OSPF_EVENT) | |
2119 | zlog_info ("This VL is not up yet, sorry"); | |
2120 | return NULL; | |
2121 | } | |
2122 | ||
2123 | return vl_data->vl_oi; | |
2124 | } | |
2125 | } | |
2126 | ||
2127 | if (IS_DEBUG_OSPF_EVENT) | |
2128 | zlog_info ("couldn't find any VL to associate the packet with"); | |
2129 | ||
d3f0d621 | 2130 | return NULL; |
718e3744 | 2131 | } |
2132 | ||
2133 | int | |
2134 | ospf_check_area_id (struct ospf_interface *oi, struct ospf_header *ospfh) | |
2135 | { | |
2136 | /* Check match the Area ID of the receiving interface. */ | |
2137 | if (OSPF_AREA_SAME (&oi->area, &ospfh)) | |
2138 | return 1; | |
2139 | ||
2140 | return 0; | |
2141 | } | |
2142 | ||
2143 | /* Unbound socket will accept any Raw IP packets if proto is matched. | |
2144 | To prevent it, compare src IP address and i/f address with masking | |
2145 | i/f network mask. */ | |
2146 | int | |
2147 | ospf_check_network_mask (struct ospf_interface *oi, struct in_addr ip_src) | |
2148 | { | |
2149 | struct in_addr mask, me, him; | |
2150 | ||
2151 | if (oi->type == OSPF_IFTYPE_POINTOPOINT || | |
2152 | oi->type == OSPF_IFTYPE_VIRTUALLINK) | |
2153 | return 1; | |
2154 | ||
2155 | masklen2ip (oi->address->prefixlen, &mask); | |
2156 | ||
2157 | me.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr; | |
2158 | him.s_addr = ip_src.s_addr & mask.s_addr; | |
2159 | ||
2160 | if (IPV4_ADDR_SAME (&me, &him)) | |
2161 | return 1; | |
2162 | ||
2163 | return 0; | |
2164 | } | |
2165 | ||
2166 | int | |
2167 | ospf_check_auth (struct ospf_interface *oi, struct stream *ibuf, | |
2168 | struct ospf_header *ospfh) | |
2169 | { | |
2170 | int ret = 0; | |
2171 | struct crypt_key *ck; | |
2172 | ||
2173 | switch (ntohs (ospfh->auth_type)) | |
2174 | { | |
2175 | case OSPF_AUTH_NULL: | |
2176 | ret = 1; | |
2177 | break; | |
2178 | case OSPF_AUTH_SIMPLE: | |
2179 | if (!memcmp (OSPF_IF_PARAM (oi, auth_simple), ospfh->u.auth_data, OSPF_AUTH_SIMPLE_SIZE)) | |
2180 | ret = 1; | |
2181 | else | |
2182 | ret = 0; | |
2183 | break; | |
2184 | case OSPF_AUTH_CRYPTOGRAPHIC: | |
2185 | if ((ck = getdata (OSPF_IF_PARAM (oi,auth_crypt)->tail)) == NULL) | |
2186 | { | |
2187 | ret = 0; | |
2188 | break; | |
2189 | } | |
2190 | ||
2191 | /* This is very basic, the digest processing is elsewhere */ | |
2192 | if (ospfh->u.crypt.auth_data_len == OSPF_AUTH_MD5_SIZE && | |
2193 | ospfh->u.crypt.key_id == ck->key_id && | |
2194 | ntohs (ospfh->length) + OSPF_AUTH_SIMPLE_SIZE <= stream_get_size (ibuf)) | |
2195 | ret = 1; | |
2196 | else | |
2197 | ret = 0; | |
2198 | break; | |
2199 | default: | |
2200 | ret = 0; | |
2201 | break; | |
2202 | } | |
2203 | ||
2204 | return ret; | |
2205 | } | |
2206 | ||
2207 | int | |
2208 | ospf_check_sum (struct ospf_header *ospfh) | |
2209 | { | |
2210 | u_int32_t ret; | |
2211 | u_int16_t sum; | |
2212 | int in_cksum (void *ptr, int nbytes); | |
2213 | ||
2214 | /* clear auth_data for checksum. */ | |
2215 | memset (ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE); | |
2216 | ||
2217 | /* keep checksum and clear. */ | |
2218 | sum = ospfh->checksum; | |
2219 | memset (&ospfh->checksum, 0, sizeof (u_int16_t)); | |
2220 | ||
2221 | /* calculate checksum. */ | |
2222 | ret = in_cksum (ospfh, ntohs (ospfh->length)); | |
2223 | ||
2224 | if (ret != sum) | |
2225 | { | |
2226 | zlog_info ("ospf_check_sum(): checksum mismatch, my %X, his %X", | |
2227 | ret, sum); | |
2228 | return 0; | |
2229 | } | |
2230 | ||
2231 | return 1; | |
2232 | } | |
2233 | ||
2234 | /* OSPF Header verification. */ | |
2235 | int | |
2236 | ospf_verify_header (struct stream *ibuf, struct ospf_interface *oi, | |
2237 | struct ip *iph, struct ospf_header *ospfh) | |
2238 | { | |
2239 | /* check version. */ | |
2240 | if (ospfh->version != OSPF_VERSION) | |
2241 | { | |
2242 | zlog_warn ("interface %s: ospf_read version number mismatch.", | |
2243 | IF_NAME (oi)); | |
2244 | return -1; | |
2245 | } | |
2246 | ||
2247 | /* Check Area ID. */ | |
2248 | if (!ospf_check_area_id (oi, ospfh)) | |
2249 | { | |
2250 | zlog_warn ("interface %s: ospf_read invalid Area ID %s.", | |
2251 | IF_NAME (oi), inet_ntoa (ospfh->area_id)); | |
2252 | return -1; | |
2253 | } | |
2254 | ||
2255 | /* Check network mask, Silently discarded. */ | |
2256 | if (! ospf_check_network_mask (oi, iph->ip_src)) | |
2257 | { | |
2258 | zlog_warn ("interface %s: ospf_read network address is not same [%s]", | |
2259 | IF_NAME (oi), inet_ntoa (iph->ip_src)); | |
2260 | return -1; | |
2261 | } | |
2262 | ||
2263 | /* Check authentication. */ | |
2264 | if (ospf_auth_type (oi) != ntohs (ospfh->auth_type)) | |
2265 | { | |
2266 | zlog_warn ("interface %s: ospf_read authentication type mismatch.", | |
2267 | IF_NAME (oi)); | |
2268 | return -1; | |
2269 | } | |
2270 | ||
2271 | if (! ospf_check_auth (oi, ibuf, ospfh)) | |
2272 | { | |
2273 | zlog_warn ("interface %s: ospf_read authentication failed.", | |
2274 | IF_NAME (oi)); | |
2275 | return -1; | |
2276 | } | |
2277 | ||
2278 | /* if check sum is invalid, packet is discarded. */ | |
2279 | if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC) | |
2280 | { | |
2281 | if (! ospf_check_sum (ospfh)) | |
2282 | { | |
2283 | zlog_warn ("interface %s: ospf_read packet checksum error %s", | |
2284 | IF_NAME (oi), inet_ntoa (ospfh->router_id)); | |
2285 | return -1; | |
2286 | } | |
2287 | } | |
2288 | else | |
2289 | { | |
2290 | if (ospfh->checksum != 0) | |
2291 | return -1; | |
2292 | if (ospf_check_md5_digest (oi, ibuf, ntohs (ospfh->length)) == 0) | |
2293 | { | |
2294 | zlog_warn ("interface %s: ospf_read md5 authentication failed.", | |
2295 | IF_NAME (oi)); | |
2296 | return -1; | |
2297 | } | |
2298 | } | |
2299 | ||
2300 | return 0; | |
2301 | } | |
2302 | ||
2303 | /* Starting point of packet process function. */ | |
2304 | int | |
2305 | ospf_read (struct thread *thread) | |
2306 | { | |
2307 | int ret; | |
2308 | struct stream *ibuf; | |
68980084 | 2309 | struct ospf *ospf; |
718e3744 | 2310 | struct ospf_interface *oi; |
2311 | struct ip *iph; | |
2312 | struct ospf_header *ospfh; | |
2313 | u_int16_t length; | |
2314 | struct interface *ifp; | |
2315 | ||
2316 | /* first of all get interface pointer. */ | |
68980084 | 2317 | ospf = THREAD_ARG (thread); |
2318 | ospf->t_read = NULL; | |
718e3744 | 2319 | |
2320 | /* read OSPF packet. */ | |
68980084 | 2321 | ibuf = ospf_recv_packet (ospf->fd, &ifp); |
718e3744 | 2322 | if (ibuf == NULL) |
2323 | return -1; | |
2324 | ||
d3f0d621 | 2325 | if (ifp == NULL) |
2326 | { | |
2327 | stream_free (ibuf); | |
2328 | return 0; | |
2329 | } | |
2330 | ||
718e3744 | 2331 | iph = (struct ip *) STREAM_DATA (ibuf); |
2332 | ||
2333 | /* prepare for next packet. */ | |
68980084 | 2334 | ospf->t_read = thread_add_read (master, ospf_read, ospf, ospf->fd); |
718e3744 | 2335 | |
2336 | /* IP Header dump. */ | |
17b78d38 | 2337 | if (IS_DEBUG_OSPF_PACKET(0, RECV)) |
7d95c611 | 2338 | ospf_ip_header_dump (ibuf); |
2339 | ||
718e3744 | 2340 | /* Self-originated packet should be discarded silently. */ |
68980084 | 2341 | if (ospf_if_lookup_by_local_addr (ospf, NULL, iph->ip_src)) |
718e3744 | 2342 | { |
d324181c | 2343 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
2344 | { | |
2345 | zlog_info ("ospf_read[%s]: Dropping self-originated packet", | |
2346 | inet_ntoa (iph->ip_src)); | |
2347 | } | |
718e3744 | 2348 | stream_free (ibuf); |
2349 | return 0; | |
2350 | } | |
2351 | ||
2352 | /* Adjust size to message length. */ | |
2353 | stream_forward (ibuf, iph->ip_hl * 4); | |
2354 | ||
2355 | /* Get ospf packet header. */ | |
2356 | ospfh = (struct ospf_header *) STREAM_PNT (ibuf); | |
2357 | ||
2358 | /* associate packet with ospf interface */ | |
68980084 | 2359 | oi = ospf_if_lookup_recv_if (ospf, iph->ip_src); |
d3f0d621 | 2360 | |
2361 | /* if no local ospf_interface, | |
2362 | * or header area is backbone but ospf_interface is not | |
2363 | * check for VLINK interface | |
2364 | */ | |
2365 | if ( (oi == NULL) || | |
2366 | (OSPF_IS_AREA_ID_BACKBONE(ospfh->area_id) | |
2367 | && !OSPF_IS_AREA_ID_BACKBONE(oi->area->area_id)) | |
2368 | ) | |
718e3744 | 2369 | { |
d3f0d621 | 2370 | if ((oi = ospf_associate_packet_vl (ospf, ifp, iph, ospfh)) == NULL) |
d324181c | 2371 | { |
d3f0d621 | 2372 | zlog_warn ("Packet from [%s] received on link %s" |
2373 | " but no ospf_interface", | |
2374 | inet_ntoa (iph->ip_src), ifp->name); | |
2375 | stream_free (ibuf); | |
2376 | return 0; | |
d324181c | 2377 | } |
d3f0d621 | 2378 | } |
2379 | ||
2380 | /* else it must be a local ospf interface, check it was received on | |
2381 | * correct link | |
2382 | */ | |
2383 | else if (oi->ifp != ifp) | |
2384 | { | |
2385 | zlog_warn ("Packet from [%s] received on wrong link %s", | |
2386 | inet_ntoa (iph->ip_src), ifp->name); | |
718e3744 | 2387 | stream_free (ibuf); |
2388 | return 0; | |
2389 | } | |
2390 | ||
2391 | /* | |
2392 | * If the received packet is destined for AllDRouters, the packet | |
2393 | * should be accepted only if the received ospf interface state is | |
2394 | * either DR or Backup -- endo. | |
2395 | */ | |
2396 | if (iph->ip_dst.s_addr == htonl (OSPF_ALLDROUTERS) | |
2397 | && (oi->state != ISM_DR && oi->state != ISM_Backup)) | |
2398 | { | |
2399 | zlog_info ("Packet for AllDRouters from [%s] via [%s] (ISM: %s)", | |
2400 | inet_ntoa (iph->ip_src), IF_NAME (oi), | |
2401 | LOOKUP (ospf_ism_state_msg, oi->state)); | |
2402 | stream_free (ibuf); | |
2403 | return 0; | |
2404 | } | |
2405 | ||
2406 | /* Show debug receiving packet. */ | |
1aa7b399 | 2407 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV)) |
2408 | { | |
718e3744 | 2409 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, DETAIL)) |
1aa7b399 | 2410 | { |
2411 | zlog_info ("-----------------------------------------------------"); | |
2412 | ospf_packet_dump (ibuf); | |
2413 | } | |
718e3744 | 2414 | |
2415 | zlog_info ("%s received from [%s] via [%s]", | |
1aa7b399 | 2416 | ospf_packet_type_str[ospfh->type], |
2417 | inet_ntoa (ospfh->router_id), IF_NAME (oi)); | |
718e3744 | 2418 | zlog_info (" src [%s],", inet_ntoa (iph->ip_src)); |
2419 | zlog_info (" dst [%s]", inet_ntoa (iph->ip_dst)); | |
2420 | ||
2421 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, DETAIL)) | |
2422 | zlog_info ("-----------------------------------------------------"); | |
1aa7b399 | 2423 | } |
718e3744 | 2424 | |
2425 | /* Some header verification. */ | |
2426 | ret = ospf_verify_header (ibuf, oi, iph, ospfh); | |
2427 | if (ret < 0) | |
2428 | { | |
d324181c | 2429 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV)) |
2430 | { | |
2431 | zlog_info ("ospf_read[%s/%s]: Header check failed, " | |
2432 | "dropping.", | |
2433 | ospf_packet_type_str[ospfh->type], | |
2434 | inet_ntoa (iph->ip_src)); | |
2435 | } | |
718e3744 | 2436 | stream_free (ibuf); |
2437 | return ret; | |
2438 | } | |
2439 | ||
2440 | stream_forward (ibuf, OSPF_HEADER_SIZE); | |
2441 | ||
2442 | /* Adjust size to message length. */ | |
2443 | length = ntohs (ospfh->length) - OSPF_HEADER_SIZE; | |
2444 | ||
2445 | /* Read rest of the packet and call each sort of packet routine. */ | |
2446 | switch (ospfh->type) | |
2447 | { | |
2448 | case OSPF_MSG_HELLO: | |
2449 | ospf_hello (iph, ospfh, ibuf, oi, length); | |
2450 | break; | |
2451 | case OSPF_MSG_DB_DESC: | |
2452 | ospf_db_desc (iph, ospfh, ibuf, oi, length); | |
2453 | break; | |
2454 | case OSPF_MSG_LS_REQ: | |
2455 | ospf_ls_req (iph, ospfh, ibuf, oi, length); | |
2456 | break; | |
2457 | case OSPF_MSG_LS_UPD: | |
2458 | ospf_ls_upd (iph, ospfh, ibuf, oi, length); | |
2459 | break; | |
2460 | case OSPF_MSG_LS_ACK: | |
2461 | ospf_ls_ack (iph, ospfh, ibuf, oi, length); | |
2462 | break; | |
2463 | default: | |
2464 | zlog (NULL, LOG_WARNING, | |
2465 | "interface %s: OSPF packet header type %d is illegal", | |
2466 | IF_NAME (oi), ospfh->type); | |
2467 | break; | |
2468 | } | |
2469 | ||
2470 | stream_free (ibuf); | |
2471 | return 0; | |
2472 | } | |
2473 | ||
2474 | /* Make OSPF header. */ | |
2475 | void | |
2476 | ospf_make_header (int type, struct ospf_interface *oi, struct stream *s) | |
2477 | { | |
2478 | struct ospf_header *ospfh; | |
2479 | ||
2480 | ospfh = (struct ospf_header *) STREAM_DATA (s); | |
2481 | ||
2482 | ospfh->version = (u_char) OSPF_VERSION; | |
2483 | ospfh->type = (u_char) type; | |
2484 | ||
68980084 | 2485 | ospfh->router_id = oi->ospf->router_id; |
718e3744 | 2486 | |
2487 | ospfh->checksum = 0; | |
2488 | ospfh->area_id = oi->area->area_id; | |
2489 | ospfh->auth_type = htons (ospf_auth_type (oi)); | |
2490 | ||
2491 | memset (ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE); | |
2492 | ||
2493 | ospf_output_forward (s, OSPF_HEADER_SIZE); | |
2494 | } | |
2495 | ||
2496 | /* Make Authentication Data. */ | |
2497 | int | |
2498 | ospf_make_auth (struct ospf_interface *oi, struct ospf_header *ospfh) | |
2499 | { | |
2500 | struct crypt_key *ck; | |
2501 | ||
2502 | switch (ospf_auth_type (oi)) | |
2503 | { | |
2504 | case OSPF_AUTH_NULL: | |
2505 | /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */ | |
2506 | break; | |
2507 | case OSPF_AUTH_SIMPLE: | |
2508 | memcpy (ospfh->u.auth_data, OSPF_IF_PARAM (oi, auth_simple), | |
2509 | OSPF_AUTH_SIMPLE_SIZE); | |
2510 | break; | |
2511 | case OSPF_AUTH_CRYPTOGRAPHIC: | |
2512 | /* If key is not set, then set 0. */ | |
2513 | if (list_isempty (OSPF_IF_PARAM (oi, auth_crypt))) | |
2514 | { | |
2515 | ospfh->u.crypt.zero = 0; | |
2516 | ospfh->u.crypt.key_id = 0; | |
2517 | ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE; | |
2518 | } | |
2519 | else | |
2520 | { | |
2521 | ck = getdata (OSPF_IF_PARAM (oi, auth_crypt)->tail); | |
2522 | ospfh->u.crypt.zero = 0; | |
2523 | ospfh->u.crypt.key_id = ck->key_id; | |
2524 | ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE; | |
2525 | } | |
2526 | /* note: the seq is done in ospf_make_md5_digest() */ | |
2527 | break; | |
2528 | default: | |
2529 | /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */ | |
2530 | break; | |
2531 | } | |
2532 | ||
2533 | return 0; | |
2534 | } | |
2535 | ||
2536 | /* Fill rest of OSPF header. */ | |
2537 | void | |
2538 | ospf_fill_header (struct ospf_interface *oi, | |
2539 | struct stream *s, u_int16_t length) | |
2540 | { | |
2541 | struct ospf_header *ospfh; | |
2542 | ||
2543 | ospfh = (struct ospf_header *) STREAM_DATA (s); | |
2544 | ||
2545 | /* Fill length. */ | |
2546 | ospfh->length = htons (length); | |
2547 | ||
2548 | /* Calculate checksum. */ | |
2549 | if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC) | |
2550 | ospfh->checksum = in_cksum (ospfh, length); | |
2551 | else | |
2552 | ospfh->checksum = 0; | |
2553 | ||
2554 | /* Add Authentication Data. */ | |
2555 | ospf_make_auth (oi, ospfh); | |
2556 | } | |
2557 | ||
2558 | int | |
2559 | ospf_make_hello (struct ospf_interface *oi, struct stream *s) | |
2560 | { | |
2561 | struct ospf_neighbor *nbr; | |
2562 | struct route_node *rn; | |
2563 | u_int16_t length = OSPF_HELLO_MIN_SIZE; | |
2564 | struct in_addr mask; | |
2565 | unsigned long p; | |
2566 | int flag = 0; | |
2567 | ||
2568 | /* Set netmask of interface. */ | |
2569 | if (oi->type != OSPF_IFTYPE_POINTOPOINT && | |
2570 | oi->type != OSPF_IFTYPE_VIRTUALLINK) | |
2571 | masklen2ip (oi->address->prefixlen, &mask); | |
2572 | else | |
2573 | memset ((char *) &mask, 0, sizeof (struct in_addr)); | |
2574 | stream_put_ipv4 (s, mask.s_addr); | |
2575 | ||
2576 | /* Set Hello Interval. */ | |
2577 | stream_putw (s, OSPF_IF_PARAM (oi, v_hello)); | |
2578 | ||
2579 | if (IS_DEBUG_OSPF_EVENT) | |
2580 | zlog_info ("make_hello: options: %x, int: %s", | |
2581 | OPTIONS(oi), IF_NAME (oi)); | |
2582 | ||
2583 | /* Set Options. */ | |
2584 | stream_putc (s, OPTIONS (oi)); | |
2585 | ||
2586 | /* Set Router Priority. */ | |
2587 | stream_putc (s, PRIORITY (oi)); | |
2588 | ||
2589 | /* Set Router Dead Interval. */ | |
2590 | stream_putl (s, OSPF_IF_PARAM (oi, v_wait)); | |
2591 | ||
2592 | /* Set Designated Router. */ | |
2593 | stream_put_ipv4 (s, DR (oi).s_addr); | |
2594 | ||
2595 | p = s->putp; | |
2596 | ||
2597 | /* Set Backup Designated Router. */ | |
2598 | stream_put_ipv4 (s, BDR (oi).s_addr); | |
2599 | ||
2600 | /* Add neighbor seen. */ | |
2601 | for (rn = route_top (oi->nbrs); rn; rn = route_next (rn)) | |
68980084 | 2602 | if ((nbr = rn->info)) |
2603 | if (nbr->router_id.s_addr != 0) /* Ignore 0.0.0.0 node. */ | |
2604 | if (nbr->state != NSM_Attempt) /* Ignore Down neighbor. */ | |
2605 | if (nbr->state != NSM_Down) /* This is myself for DR election. */ | |
2606 | if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id)) | |
718e3744 | 2607 | { |
2608 | /* Check neighbor is sane? */ | |
68980084 | 2609 | if (nbr->d_router.s_addr != 0 |
2610 | && IPV4_ADDR_SAME (&nbr->d_router, &oi->address->u.prefix4) | |
2611 | && IPV4_ADDR_SAME (&nbr->bd_router, &oi->address->u.prefix4)) | |
2612 | flag = 1; | |
718e3744 | 2613 | |
2614 | stream_put_ipv4 (s, nbr->router_id.s_addr); | |
2615 | length += 4; | |
2616 | } | |
2617 | ||
2618 | /* Let neighbor generate BackupSeen. */ | |
2619 | if (flag == 1) | |
2620 | { | |
2621 | stream_set_putp (s, p); | |
2622 | stream_put_ipv4 (s, 0); | |
2623 | } | |
2624 | ||
2625 | return length; | |
2626 | } | |
2627 | ||
2628 | int | |
2629 | ospf_make_db_desc (struct ospf_interface *oi, struct ospf_neighbor *nbr, | |
2630 | struct stream *s) | |
2631 | { | |
2632 | struct ospf_lsa *lsa; | |
2633 | u_int16_t length = OSPF_DB_DESC_MIN_SIZE; | |
2634 | u_char options; | |
2635 | unsigned long pp; | |
2636 | int i; | |
2637 | struct ospf_lsdb *lsdb; | |
2638 | ||
2639 | /* Set Interface MTU. */ | |
2640 | if (oi->type == OSPF_IFTYPE_VIRTUALLINK) | |
2641 | stream_putw (s, 0); | |
2642 | else | |
2643 | stream_putw (s, oi->ifp->mtu); | |
2644 | ||
2645 | /* Set Options. */ | |
2646 | options = OPTIONS (oi); | |
2647 | #ifdef HAVE_OPAQUE_LSA | |
68980084 | 2648 | if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE)) |
718e3744 | 2649 | { |
2650 | if (IS_SET_DD_I (nbr->dd_flags) | |
2651 | || CHECK_FLAG (nbr->options, OSPF_OPTION_O)) | |
2652 | /* | |
2653 | * Set O-bit in the outgoing DD packet for capablity negotiation, | |
2654 | * if one of following case is applicable. | |
2655 | * | |
2656 | * 1) WaitTimer expiration event triggered the neighbor state to | |
2657 | * change to Exstart, but no (valid) DD packet has received | |
2658 | * from the neighbor yet. | |
2659 | * | |
2660 | * 2) At least one DD packet with O-bit on has received from the | |
2661 | * neighbor. | |
2662 | */ | |
2663 | SET_FLAG (options, OSPF_OPTION_O); | |
2664 | } | |
2665 | #endif /* HAVE_OPAQUE_LSA */ | |
2666 | stream_putc (s, options); | |
2667 | ||
2668 | /* Keep pointer to flags. */ | |
2669 | pp = stream_get_putp (s); | |
2670 | stream_putc (s, nbr->dd_flags); | |
2671 | ||
2672 | /* Set DD Sequence Number. */ | |
2673 | stream_putl (s, nbr->dd_seqnum); | |
2674 | ||
2675 | if (ospf_db_summary_isempty (nbr)) | |
2676 | { | |
2677 | if (nbr->state >= NSM_Exchange) | |
2678 | { | |
2679 | nbr->dd_flags &= ~OSPF_DD_FLAG_M; | |
2680 | /* Set DD flags again */ | |
2681 | stream_set_putp (s, pp); | |
2682 | stream_putc (s, nbr->dd_flags); | |
2683 | } | |
2684 | return length; | |
2685 | } | |
2686 | ||
2687 | /* Describe LSA Header from Database Summary List. */ | |
2688 | lsdb = &nbr->db_sum; | |
2689 | ||
2690 | for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) | |
2691 | { | |
2692 | struct route_table *table = lsdb->type[i].db; | |
2693 | struct route_node *rn; | |
2694 | ||
2695 | for (rn = route_top (table); rn; rn = route_next (rn)) | |
2696 | if ((lsa = rn->info) != NULL) | |
2697 | { | |
2698 | #ifdef HAVE_OPAQUE_LSA | |
2699 | if (IS_OPAQUE_LSA (lsa->data->type) | |
2700 | && (! CHECK_FLAG (options, OSPF_OPTION_O))) | |
2701 | { | |
2702 | /* Suppress advertising opaque-informations. */ | |
2703 | /* Remove LSA from DB summary list. */ | |
2704 | ospf_lsdb_delete (lsdb, lsa); | |
2705 | continue; | |
2706 | } | |
2707 | #endif /* HAVE_OPAQUE_LSA */ | |
2708 | ||
2709 | if (!CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD)) | |
2710 | { | |
2711 | struct lsa_header *lsah; | |
2712 | u_int16_t ls_age; | |
2713 | ||
2714 | /* DD packet overflows interface MTU. */ | |
2715 | if (length + OSPF_LSA_HEADER_SIZE > OSPF_PACKET_MAX (oi)) | |
2716 | break; | |
2717 | ||
2718 | /* Keep pointer to LS age. */ | |
2719 | lsah = (struct lsa_header *) (STREAM_DATA (s) + | |
2720 | stream_get_putp (s)); | |
2721 | ||
2722 | /* Proceed stream pointer. */ | |
2723 | stream_put (s, lsa->data, OSPF_LSA_HEADER_SIZE); | |
2724 | length += OSPF_LSA_HEADER_SIZE; | |
2725 | ||
2726 | /* Set LS age. */ | |
2727 | ls_age = LS_AGE (lsa); | |
2728 | lsah->ls_age = htons (ls_age); | |
2729 | ||
2730 | } | |
2731 | ||
2732 | /* Remove LSA from DB summary list. */ | |
2733 | ospf_lsdb_delete (lsdb, lsa); | |
2734 | } | |
2735 | } | |
2736 | ||
2737 | return length; | |
2738 | } | |
2739 | ||
2740 | int | |
2741 | ospf_make_ls_req_func (struct stream *s, u_int16_t *length, | |
2742 | unsigned long delta, struct ospf_neighbor *nbr, | |
2743 | struct ospf_lsa *lsa) | |
2744 | { | |
2745 | struct ospf_interface *oi; | |
2746 | ||
2747 | oi = nbr->oi; | |
2748 | ||
2749 | /* LS Request packet overflows interface MTU. */ | |
2750 | if (*length + delta > OSPF_PACKET_MAX(oi)) | |
2751 | return 0; | |
2752 | ||
2753 | stream_putl (s, lsa->data->type); | |
2754 | stream_put_ipv4 (s, lsa->data->id.s_addr); | |
2755 | stream_put_ipv4 (s, lsa->data->adv_router.s_addr); | |
2756 | ||
2757 | ospf_lsa_unlock (nbr->ls_req_last); | |
2758 | nbr->ls_req_last = ospf_lsa_lock (lsa); | |
2759 | ||
2760 | *length += 12; | |
2761 | return 1; | |
2762 | } | |
2763 | ||
2764 | int | |
2765 | ospf_make_ls_req (struct ospf_neighbor *nbr, struct stream *s) | |
2766 | { | |
2767 | struct ospf_lsa *lsa; | |
2768 | u_int16_t length = OSPF_LS_REQ_MIN_SIZE; | |
2769 | unsigned long delta = stream_get_putp(s)+12; | |
2770 | struct route_table *table; | |
2771 | struct route_node *rn; | |
2772 | int i; | |
2773 | struct ospf_lsdb *lsdb; | |
2774 | ||
2775 | lsdb = &nbr->ls_req; | |
2776 | ||
2777 | for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) | |
2778 | { | |
2779 | table = lsdb->type[i].db; | |
2780 | for (rn = route_top (table); rn; rn = route_next (rn)) | |
2781 | if ((lsa = (rn->info)) != NULL) | |
2782 | if (ospf_make_ls_req_func (s, &length, delta, nbr, lsa) == 0) | |
2783 | { | |
2784 | route_unlock_node (rn); | |
2785 | break; | |
2786 | } | |
2787 | } | |
2788 | return length; | |
2789 | } | |
2790 | ||
2791 | int | |
2792 | ls_age_increment (struct ospf_lsa *lsa, int delay) | |
2793 | { | |
2794 | int age; | |
2795 | ||
2796 | age = IS_LSA_MAXAGE (lsa) ? OSPF_LSA_MAXAGE : LS_AGE (lsa) + delay; | |
2797 | ||
2798 | return (age > OSPF_LSA_MAXAGE ? OSPF_LSA_MAXAGE : age); | |
2799 | } | |
2800 | ||
2801 | int | |
52dc7ee6 | 2802 | ospf_make_ls_upd (struct ospf_interface *oi, struct list *update, struct stream *s) |
718e3744 | 2803 | { |
2804 | struct ospf_lsa *lsa; | |
52dc7ee6 | 2805 | struct listnode *node; |
718e3744 | 2806 | u_int16_t length = OSPF_LS_UPD_MIN_SIZE; |
2807 | unsigned long delta = stream_get_putp (s); | |
2808 | unsigned long pp; | |
2809 | int count = 0; | |
2810 | ||
2811 | if (IS_DEBUG_OSPF_EVENT) | |
59ea14c6 | 2812 | zlog_info ("ospf_make_ls_upd: Start"); |
2813 | ||
718e3744 | 2814 | pp = stream_get_putp (s); |
68b7339a | 2815 | ospf_output_forward (s, OSPF_LS_UPD_MIN_SIZE); |
718e3744 | 2816 | |
2817 | while ((node = listhead (update)) != NULL) | |
2818 | { | |
2819 | struct lsa_header *lsah; | |
2820 | u_int16_t ls_age; | |
2821 | ||
2822 | if (IS_DEBUG_OSPF_EVENT) | |
59ea14c6 | 2823 | zlog_info ("ospf_make_ls_upd: List Iteration"); |
718e3744 | 2824 | |
2825 | lsa = getdata (node); | |
2826 | assert (lsa); | |
2827 | assert (lsa->data); | |
2828 | ||
68b7339a | 2829 | /* Will it fit? */ |
2830 | if (length + delta + ntohs (lsa->data->length) > stream_get_size (s)) | |
59ea14c6 | 2831 | break; |
2832 | ||
718e3744 | 2833 | /* Keep pointer to LS age. */ |
2834 | lsah = (struct lsa_header *) (STREAM_DATA (s) + stream_get_putp (s)); | |
2835 | ||
2836 | /* Put LSA to Link State Request. */ | |
2837 | stream_put (s, lsa->data, ntohs (lsa->data->length)); | |
2838 | ||
2839 | /* Set LS age. */ | |
2840 | /* each hop must increment an lsa_age by transmit_delay | |
2841 | of OSPF interface */ | |
2842 | ls_age = ls_age_increment (lsa, OSPF_IF_PARAM (oi, transmit_delay)); | |
2843 | lsah->ls_age = htons (ls_age); | |
2844 | ||
2845 | length += ntohs (lsa->data->length); | |
2846 | count++; | |
2847 | ||
2848 | list_delete_node (update, node); | |
2849 | ospf_lsa_unlock (lsa); | |
2850 | } | |
2851 | ||
2852 | /* Now set #LSAs. */ | |
2853 | stream_set_putp (s, pp); | |
2854 | stream_putl (s, count); | |
2855 | ||
2856 | stream_set_putp (s, s->endp); | |
2857 | ||
2858 | if (IS_DEBUG_OSPF_EVENT) | |
59ea14c6 | 2859 | zlog_info ("ospf_make_ls_upd: Stop"); |
718e3744 | 2860 | return length; |
2861 | } | |
2862 | ||
2863 | int | |
52dc7ee6 | 2864 | ospf_make_ls_ack (struct ospf_interface *oi, struct list *ack, struct stream *s) |
718e3744 | 2865 | { |
52dc7ee6 | 2866 | struct list *rm_list; |
2867 | struct listnode *node; | |
718e3744 | 2868 | u_int16_t length = OSPF_LS_ACK_MIN_SIZE; |
2869 | unsigned long delta = stream_get_putp(s) + 24; | |
2870 | struct ospf_lsa *lsa; | |
2871 | ||
2872 | rm_list = list_new (); | |
2873 | ||
2874 | for (node = listhead (ack); node; nextnode (node)) | |
2875 | { | |
2876 | lsa = getdata (node); | |
2877 | assert (lsa); | |
2878 | ||
2879 | if (length + delta > OSPF_PACKET_MAX (oi)) | |
2880 | break; | |
2881 | ||
2882 | stream_put (s, lsa->data, OSPF_LSA_HEADER_SIZE); | |
2883 | length += OSPF_LSA_HEADER_SIZE; | |
2884 | ||
2885 | listnode_add (rm_list, lsa); | |
2886 | } | |
2887 | ||
2888 | /* Remove LSA from LS-Ack list. */ | |
2889 | for (node = listhead (rm_list); node; nextnode (node)) | |
2890 | { | |
2891 | lsa = (struct ospf_lsa *) getdata (node); | |
2892 | ||
2893 | listnode_delete (ack, lsa); | |
2894 | ospf_lsa_unlock (lsa); | |
2895 | } | |
2896 | ||
2897 | list_delete (rm_list); | |
2898 | ||
2899 | return length; | |
2900 | } | |
2901 | ||
2902 | void | |
2903 | ospf_hello_send_sub (struct ospf_interface *oi, struct in_addr *addr) | |
2904 | { | |
2905 | struct ospf_packet *op; | |
2906 | u_int16_t length = OSPF_HEADER_SIZE; | |
2907 | ||
2908 | op = ospf_packet_new (oi->ifp->mtu); | |
2909 | ||
2910 | /* Prepare OSPF common header. */ | |
2911 | ospf_make_header (OSPF_MSG_HELLO, oi, op->s); | |
2912 | ||
2913 | /* Prepare OSPF Hello body. */ | |
2914 | length += ospf_make_hello (oi, op->s); | |
2915 | ||
2916 | /* Fill OSPF header. */ | |
2917 | ospf_fill_header (oi, op->s, length); | |
2918 | ||
2919 | /* Set packet length. */ | |
2920 | op->length = length; | |
2921 | ||
2922 | op->dst.s_addr = addr->s_addr; | |
2923 | ||
2924 | /* Add packet to the interface output queue. */ | |
2925 | ospf_packet_add (oi, op); | |
2926 | ||
2927 | /* Hook thread to write packet. */ | |
020709f9 | 2928 | OSPF_ISM_WRITE_ON (oi->ospf); |
718e3744 | 2929 | } |
2930 | ||
2931 | void | |
2932 | ospf_poll_send (struct ospf_nbr_nbma *nbr_nbma) | |
2933 | { | |
2934 | struct ospf_interface *oi; | |
2935 | ||
2936 | oi = nbr_nbma->oi; | |
2937 | assert(oi); | |
2938 | ||
2939 | /* If this is passive interface, do not send OSPF Hello. */ | |
2940 | if (OSPF_IF_PARAM (oi, passive_interface) == OSPF_IF_PASSIVE) | |
2941 | return; | |
2942 | ||
2943 | if (oi->type != OSPF_IFTYPE_NBMA) | |
2944 | return; | |
2945 | ||
2946 | if (nbr_nbma->nbr != NULL && nbr_nbma->nbr->state != NSM_Down) | |
2947 | return; | |
2948 | ||
2949 | if (PRIORITY(oi) == 0) | |
2950 | return; | |
2951 | ||
2952 | if (nbr_nbma->priority == 0 | |
2953 | && oi->state != ISM_DR && oi->state != ISM_Backup) | |
2954 | return; | |
2955 | ||
2956 | ospf_hello_send_sub (oi, &nbr_nbma->addr); | |
2957 | } | |
2958 | ||
2959 | int | |
2960 | ospf_poll_timer (struct thread *thread) | |
2961 | { | |
2962 | struct ospf_nbr_nbma *nbr_nbma; | |
2963 | ||
2964 | nbr_nbma = THREAD_ARG (thread); | |
2965 | nbr_nbma->t_poll = NULL; | |
2966 | ||
2967 | if (IS_DEBUG_OSPF (nsm, NSM_TIMERS)) | |
2968 | zlog (NULL, LOG_INFO, "NSM[%s:%s]: Timer (Poll timer expire)", | |
2969 | IF_NAME (nbr_nbma->oi), inet_ntoa (nbr_nbma->addr)); | |
2970 | ||
2971 | ospf_poll_send (nbr_nbma); | |
2972 | ||
2973 | if (nbr_nbma->v_poll > 0) | |
2974 | OSPF_POLL_TIMER_ON (nbr_nbma->t_poll, ospf_poll_timer, | |
2975 | nbr_nbma->v_poll); | |
2976 | ||
2977 | return 0; | |
2978 | } | |
2979 | ||
2980 | ||
2981 | int | |
2982 | ospf_hello_reply_timer (struct thread *thread) | |
2983 | { | |
2984 | struct ospf_neighbor *nbr; | |
2985 | ||
2986 | nbr = THREAD_ARG (thread); | |
2987 | nbr->t_hello_reply = NULL; | |
2988 | ||
2989 | assert (nbr->oi); | |
2990 | ||
2991 | if (IS_DEBUG_OSPF (nsm, NSM_TIMERS)) | |
2992 | zlog (NULL, LOG_INFO, "NSM[%s:%s]: Timer (hello-reply timer expire)", | |
2993 | IF_NAME (nbr->oi), inet_ntoa (nbr->router_id)); | |
2994 | ||
2995 | ospf_hello_send_sub (nbr->oi, &nbr->address.u.prefix4); | |
2996 | ||
2997 | return 0; | |
2998 | } | |
2999 | ||
3000 | /* Send OSPF Hello. */ | |
3001 | void | |
3002 | ospf_hello_send (struct ospf_interface *oi) | |
3003 | { | |
3004 | struct ospf_packet *op; | |
3005 | u_int16_t length = OSPF_HEADER_SIZE; | |
3006 | ||
3007 | /* If this is passive interface, do not send OSPF Hello. */ | |
3008 | if (OSPF_IF_PARAM (oi, passive_interface) == OSPF_IF_PASSIVE) | |
3009 | return; | |
3010 | ||
3011 | op = ospf_packet_new (oi->ifp->mtu); | |
3012 | ||
3013 | /* Prepare OSPF common header. */ | |
3014 | ospf_make_header (OSPF_MSG_HELLO, oi, op->s); | |
3015 | ||
3016 | /* Prepare OSPF Hello body. */ | |
3017 | length += ospf_make_hello (oi, op->s); | |
3018 | ||
3019 | /* Fill OSPF header. */ | |
3020 | ospf_fill_header (oi, op->s, length); | |
3021 | ||
3022 | /* Set packet length. */ | |
3023 | op->length = length; | |
3024 | ||
3025 | if (oi->type == OSPF_IFTYPE_NBMA) | |
3026 | { | |
3027 | struct ospf_neighbor *nbr; | |
3028 | struct route_node *rn; | |
3029 | ||
3030 | for (rn = route_top (oi->nbrs); rn; rn = route_next (rn)) | |
3031 | if ((nbr = rn->info)) | |
3032 | if (nbr != oi->nbr_self) | |
3033 | if (nbr->state != NSM_Down) | |
3034 | { | |
3035 | /* RFC 2328 Section 9.5.1 | |
3036 | If the router is not eligible to become Designated Router, | |
3037 | it must periodically send Hello Packets to both the | |
3038 | Designated Router and the Backup Designated Router (if they | |
3039 | exist). */ | |
3040 | if (PRIORITY(oi) == 0 && | |
3041 | IPV4_ADDR_CMP(&DR(oi), &nbr->address.u.prefix4) && | |
3042 | IPV4_ADDR_CMP(&BDR(oi), &nbr->address.u.prefix4)) | |
3043 | continue; | |
3044 | ||
3045 | /* If the router is eligible to become Designated Router, it | |
3046 | must periodically send Hello Packets to all neighbors that | |
3047 | are also eligible. In addition, if the router is itself the | |
3048 | Designated Router or Backup Designated Router, it must also | |
3049 | send periodic Hello Packets to all other neighbors. */ | |
3050 | ||
3051 | if (nbr->priority == 0 && oi->state == ISM_DROther) | |
3052 | continue; | |
3053 | /* if oi->state == Waiting, send hello to all neighbors */ | |
3054 | { | |
3055 | struct ospf_packet *op_dup; | |
3056 | ||
3057 | op_dup = ospf_packet_dup(op); | |
3058 | op_dup->dst = nbr->address.u.prefix4; | |
3059 | ||
3060 | /* Add packet to the interface output queue. */ | |
3061 | ospf_packet_add (oi, op_dup); | |
3062 | ||
020709f9 | 3063 | OSPF_ISM_WRITE_ON (oi->ospf); |
718e3744 | 3064 | } |
3065 | ||
3066 | } | |
3067 | ospf_packet_free (op); | |
3068 | } | |
3069 | else | |
3070 | { | |
3071 | /* Decide destination address. */ | |
3072 | if (oi->type == OSPF_IFTYPE_VIRTUALLINK) | |
3073 | op->dst.s_addr = oi->vl_data->peer_addr.s_addr; | |
3074 | else | |
3075 | op->dst.s_addr = htonl (OSPF_ALLSPFROUTERS); | |
3076 | ||
3077 | /* Add packet to the interface output queue. */ | |
3078 | ospf_packet_add (oi, op); | |
3079 | ||
3080 | /* Hook thread to write packet. */ | |
020709f9 | 3081 | OSPF_ISM_WRITE_ON (oi->ospf); |
718e3744 | 3082 | } |
3083 | } | |
3084 | ||
3085 | /* Send OSPF Database Description. */ | |
3086 | void | |
3087 | ospf_db_desc_send (struct ospf_neighbor *nbr) | |
3088 | { | |
3089 | struct ospf_interface *oi; | |
3090 | struct ospf_packet *op; | |
3091 | u_int16_t length = OSPF_HEADER_SIZE; | |
3092 | ||
3093 | oi = nbr->oi; | |
3094 | op = ospf_packet_new (oi->ifp->mtu); | |
3095 | ||
3096 | /* Prepare OSPF common header. */ | |
3097 | ospf_make_header (OSPF_MSG_DB_DESC, oi, op->s); | |
3098 | ||
3099 | /* Prepare OSPF Database Description body. */ | |
3100 | length += ospf_make_db_desc (oi, nbr, op->s); | |
3101 | ||
3102 | /* Fill OSPF header. */ | |
3103 | ospf_fill_header (oi, op->s, length); | |
3104 | ||
3105 | /* Set packet length. */ | |
3106 | op->length = length; | |
3107 | ||
3108 | /* Decide destination address. */ | |
3109 | op->dst = nbr->address.u.prefix4; | |
3110 | ||
3111 | /* Add packet to the interface output queue. */ | |
3112 | ospf_packet_add (oi, op); | |
3113 | ||
3114 | /* Hook thread to write packet. */ | |
020709f9 | 3115 | OSPF_ISM_WRITE_ON (oi->ospf); |
718e3744 | 3116 | |
3117 | /* Remove old DD packet, then copy new one and keep in neighbor structure. */ | |
3118 | if (nbr->last_send) | |
3119 | ospf_packet_free (nbr->last_send); | |
3120 | nbr->last_send = ospf_packet_dup (op); | |
3121 | gettimeofday (&nbr->last_send_ts, NULL); | |
3122 | } | |
3123 | ||
3124 | /* Re-send Database Description. */ | |
3125 | void | |
3126 | ospf_db_desc_resend (struct ospf_neighbor *nbr) | |
3127 | { | |
3128 | struct ospf_interface *oi; | |
3129 | ||
3130 | oi = nbr->oi; | |
3131 | ||
3132 | /* Add packet to the interface output queue. */ | |
3133 | ospf_packet_add (oi, ospf_packet_dup (nbr->last_send)); | |
3134 | ||
3135 | /* Hook thread to write packet. */ | |
020709f9 | 3136 | OSPF_ISM_WRITE_ON (oi->ospf); |
718e3744 | 3137 | } |
3138 | ||
3139 | /* Send Link State Request. */ | |
3140 | void | |
3141 | ospf_ls_req_send (struct ospf_neighbor *nbr) | |
3142 | { | |
3143 | struct ospf_interface *oi; | |
3144 | struct ospf_packet *op; | |
3145 | u_int16_t length = OSPF_HEADER_SIZE; | |
3146 | ||
3147 | oi = nbr->oi; | |
3148 | op = ospf_packet_new (oi->ifp->mtu); | |
3149 | ||
3150 | /* Prepare OSPF common header. */ | |
3151 | ospf_make_header (OSPF_MSG_LS_REQ, oi, op->s); | |
3152 | ||
3153 | /* Prepare OSPF Link State Request body. */ | |
3154 | length += ospf_make_ls_req (nbr, op->s); | |
3155 | if (length == OSPF_HEADER_SIZE) | |
3156 | { | |
3157 | ospf_packet_free (op); | |
3158 | return; | |
3159 | } | |
3160 | ||
3161 | /* Fill OSPF header. */ | |
3162 | ospf_fill_header (oi, op->s, length); | |
3163 | ||
3164 | /* Set packet length. */ | |
3165 | op->length = length; | |
3166 | ||
3167 | /* Decide destination address. */ | |
3168 | op->dst = nbr->address.u.prefix4; | |
3169 | ||
3170 | /* Add packet to the interface output queue. */ | |
3171 | ospf_packet_add (oi, op); | |
3172 | ||
3173 | /* Hook thread to write packet. */ | |
020709f9 | 3174 | OSPF_ISM_WRITE_ON (oi->ospf); |
718e3744 | 3175 | |
3176 | /* Add Link State Request Retransmission Timer. */ | |
3177 | OSPF_NSM_TIMER_ON (nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req); | |
3178 | } | |
3179 | ||
3180 | /* Send Link State Update with an LSA. */ | |
3181 | void | |
3182 | ospf_ls_upd_send_lsa (struct ospf_neighbor *nbr, struct ospf_lsa *lsa, | |
3183 | int flag) | |
3184 | { | |
52dc7ee6 | 3185 | struct list *update; |
718e3744 | 3186 | |
3187 | update = list_new (); | |
3188 | ||
3189 | listnode_add (update, lsa); | |
3190 | ospf_ls_upd_send (nbr, update, flag); | |
3191 | ||
3192 | list_delete (update); | |
3193 | } | |
3194 | ||
68b7339a | 3195 | /* Determine size for packet. Must be at least big enough to accomodate next |
3196 | * LSA on list, which may be bigger than MTU size. | |
3197 | * | |
3198 | * Return pointer to new ospf_packet | |
3199 | * NULL if we can not allocate, eg because LSA is bigger than imposed limit | |
3200 | * on packet sizes (in which case offending LSA is deleted from update list) | |
3201 | */ | |
3202 | static struct ospf_packet * | |
3203 | ospf_ls_upd_packet_new (struct list *update, struct ospf_interface *oi) | |
3204 | { | |
3205 | struct ospf_lsa *lsa; | |
3206 | struct listnode *ln; | |
3207 | size_t size; | |
3208 | static char warned = 0; | |
3209 | ||
3210 | ln = listhead (update); | |
3211 | lsa = getdata (ln); | |
3212 | assert (lsa); | |
3213 | assert (lsa->data); | |
3214 | ||
3215 | if ((OSPF_LS_UPD_MIN_SIZE + ntohs (lsa->data->length)) | |
3216 | > ospf_packet_max (oi)) | |
3217 | { | |
3218 | if (!warned) | |
3219 | { | |
3220 | zlog_warn ("ospf_ls_upd_packet_new: oversized LSA encountered!" | |
3221 | "will need to fragment. Not optimal. Try divide up" | |
3222 | " your network with areas. Use 'debug ospf packet send'" | |
3223 | " to see details, or look at 'show ip ospf database ..'"); | |
3224 | warned = 1; | |
3225 | } | |
3226 | ||
3227 | if (IS_DEBUG_OSPF_PACKET (0, SEND)) | |
3228 | zlog_warn ("ospf_ls_upd_packet_new: oversized LSA id:%s," | |
3229 | " %d bytes originated by %s, will be fragmented!", | |
3230 | inet_ntoa (lsa->data->id), | |
3231 | ntohs (lsa->data->length), | |
3232 | inet_ntoa (lsa->data->adv_router)); | |
3233 | ||
3234 | /* | |
3235 | * Allocate just enough to fit this LSA only, to avoid including other | |
3236 | * LSAs in fragmented LSA Updates. | |
3237 | */ | |
3238 | size = ntohs (lsa->data->length) + (oi->ifp->mtu - ospf_packet_max (oi)) | |
3239 | + OSPF_LS_UPD_MIN_SIZE; | |
3240 | } | |
3241 | else | |
3242 | size = oi->ifp->mtu; | |
3243 | ||
3244 | if (size > OSPF_MAX_PACKET_SIZE) | |
3245 | { | |
3246 | zlog_warn ("ospf_ls_upd_packet_new: oversized LSA id:%s too big," | |
3247 | " %d bytes, dropping it completely." | |
3248 | " OSPF routing is broken!", | |
3249 | inet_ntoa (lsa->data->id), ntohs (lsa->data->length)); | |
3250 | list_delete_node (update, ln); | |
3251 | return NULL; | |
3252 | } | |
3253 | ||
3254 | return ospf_packet_new (size); | |
3255 | } | |
3256 | ||
718e3744 | 3257 | static void |
52dc7ee6 | 3258 | ospf_ls_upd_queue_send (struct ospf_interface *oi, struct list *update, |
718e3744 | 3259 | struct in_addr addr) |
3260 | { | |
3261 | struct ospf_packet *op; | |
3262 | u_int16_t length = OSPF_HEADER_SIZE; | |
3263 | ||
3264 | if (IS_DEBUG_OSPF_EVENT) | |
3265 | zlog_info ("listcount = %d, dst %s", listcount (update), inet_ntoa(addr)); | |
68b7339a | 3266 | |
3267 | op = ospf_ls_upd_packet_new (update, oi); | |
718e3744 | 3268 | |
3269 | /* Prepare OSPF common header. */ | |
3270 | ospf_make_header (OSPF_MSG_LS_UPD, oi, op->s); | |
3271 | ||
59ea14c6 | 3272 | /* Prepare OSPF Link State Update body. |
3273 | * Includes Type-7 translation. | |
3274 | */ | |
718e3744 | 3275 | length += ospf_make_ls_upd (oi, update, op->s); |
3276 | ||
3277 | /* Fill OSPF header. */ | |
3278 | ospf_fill_header (oi, op->s, length); | |
3279 | ||
3280 | /* Set packet length. */ | |
3281 | op->length = length; | |
3282 | ||
3283 | /* Decide destination address. */ | |
3284 | op->dst.s_addr = addr.s_addr; | |
3285 | ||
3286 | /* Add packet to the interface output queue. */ | |
3287 | ospf_packet_add (oi, op); | |
3288 | ||
3289 | /* Hook thread to write packet. */ | |
020709f9 | 3290 | OSPF_ISM_WRITE_ON (oi->ospf); |
718e3744 | 3291 | } |
3292 | ||
3293 | static int | |
3294 | ospf_ls_upd_send_queue_event (struct thread *thread) | |
3295 | { | |
3296 | struct ospf_interface *oi = THREAD_ARG(thread); | |
3297 | struct route_node *rn; | |
736d3441 | 3298 | struct route_node *rnext; |
59ea14c6 | 3299 | struct list *update; |
68b7339a | 3300 | char again = 0; |
718e3744 | 3301 | |
3302 | oi->t_ls_upd_event = NULL; | |
3303 | ||
3304 | if (IS_DEBUG_OSPF_EVENT) | |
3305 | zlog_info ("ospf_ls_upd_send_queue start"); | |
3306 | ||
736d3441 | 3307 | for (rn = route_top (oi->ls_upd_queue); rn; rn = rnext) |
718e3744 | 3308 | { |
736d3441 | 3309 | rnext = route_next (rn); |
3310 | ||
718e3744 | 3311 | if (rn->info == NULL) |
736d3441 | 3312 | continue; |
68b7339a | 3313 | |
3314 | update = (struct list *)rn->info; | |
718e3744 | 3315 | |
48fe13bf | 3316 | ospf_ls_upd_queue_send (oi, update, rn->p.u.prefix4); |
718e3744 | 3317 | |
68b7339a | 3318 | /* list might not be empty. */ |
59ea14c6 | 3319 | if (listcount(update) == 0) |
3320 | { | |
3321 | list_delete (rn->info); | |
3322 | rn->info = NULL; | |
3323 | route_unlock_node (rn); | |
3324 | } | |
3325 | else | |
68b7339a | 3326 | again = 1; |
59ea14c6 | 3327 | } |
3328 | ||
3329 | if (again != 0) | |
3330 | { | |
3331 | if (IS_DEBUG_OSPF_EVENT) | |
3332 | zlog_info ("ospf_ls_upd_send_queue: update lists not cleared," | |
3333 | " %d nodes to try again, raising new event", again); | |
3334 | oi->t_ls_upd_event = | |
3335 | thread_add_event (master, ospf_ls_upd_send_queue_event, oi, 0); | |
718e3744 | 3336 | } |
3337 | ||
3338 | if (IS_DEBUG_OSPF_EVENT) | |
3339 | zlog_info ("ospf_ls_upd_send_queue stop"); | |
59ea14c6 | 3340 | |
718e3744 | 3341 | return 0; |
3342 | } | |
3343 | ||
3344 | void | |
52dc7ee6 | 3345 | ospf_ls_upd_send (struct ospf_neighbor *nbr, struct list *update, int flag) |
718e3744 | 3346 | { |
3347 | struct ospf_interface *oi; | |
3348 | struct prefix_ipv4 p; | |
3349 | struct route_node *rn; | |
52dc7ee6 | 3350 | struct listnode *n; |
718e3744 | 3351 | |
3352 | oi = nbr->oi; | |
3353 | ||
3354 | p.family = AF_INET; | |
3355 | p.prefixlen = IPV4_MAX_BITLEN; | |
3356 | ||
3357 | /* Decide destination address. */ | |
3358 | if (oi->type == OSPF_IFTYPE_VIRTUALLINK) | |
3359 | p.prefix = oi->vl_data->peer_addr; | |
3360 | else if (flag == OSPF_SEND_PACKET_DIRECT) | |
3361 | p.prefix = nbr->address.u.prefix4; | |
3362 | else if (oi->state == ISM_DR || oi->state == ISM_Backup) | |
3363 | p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS); | |
3364 | else if ((oi->type == OSPF_IFTYPE_POINTOPOINT) | |
3365 | && (flag == OSPF_SEND_PACKET_INDIRECT)) | |
3366 | p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS); | |
7afa08da | 3367 | else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT) |
3368 | p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS); | |
718e3744 | 3369 | else |
3370 | p.prefix.s_addr = htonl (OSPF_ALLDROUTERS); | |
3371 | ||
3372 | if (oi->type == OSPF_IFTYPE_NBMA) | |
3373 | { | |
3374 | if (flag == OSPF_SEND_PACKET_INDIRECT) | |
3375 | zlog_warn ("* LS-Update is directly sent on NBMA network."); | |
3376 | if (IPV4_ADDR_SAME(&oi->address->u.prefix4, &p.prefix.s_addr)) | |
3377 | zlog_warn ("* LS-Update is sent to myself."); | |
3378 | } | |
3379 | ||
3380 | rn = route_node_get (oi->ls_upd_queue, (struct prefix *) &p); | |
3381 | ||
3382 | if (rn->info == NULL) | |
3383 | rn->info = list_new (); | |
3384 | ||
3385 | for (n = listhead (update); n; nextnode (n)) | |
3386 | listnode_add (rn->info, ospf_lsa_lock (getdata (n))); | |
3387 | ||
3388 | if (oi->t_ls_upd_event == NULL) | |
3389 | oi->t_ls_upd_event = | |
3390 | thread_add_event (master, ospf_ls_upd_send_queue_event, oi, 0); | |
3391 | } | |
3392 | ||
3393 | static void | |
52dc7ee6 | 3394 | ospf_ls_ack_send_list (struct ospf_interface *oi, struct list *ack, |
3395 | struct in_addr dst) | |
718e3744 | 3396 | { |
3397 | struct ospf_packet *op; | |
3398 | u_int16_t length = OSPF_HEADER_SIZE; | |
3399 | ||
3400 | op = ospf_packet_new (oi->ifp->mtu); | |
3401 | ||
3402 | /* Prepare OSPF common header. */ | |
3403 | ospf_make_header (OSPF_MSG_LS_ACK, oi, op->s); | |
3404 | ||
3405 | /* Prepare OSPF Link State Acknowledgment body. */ | |
3406 | length += ospf_make_ls_ack (oi, ack, op->s); | |
3407 | ||
3408 | /* Fill OSPF header. */ | |
3409 | ospf_fill_header (oi, op->s, length); | |
3410 | ||
3411 | /* Set packet length. */ | |
3412 | op->length = length; | |
3413 | ||
3414 | /* Set destination IP address. */ | |
3415 | op->dst = dst; | |
3416 | ||
3417 | /* Add packet to the interface output queue. */ | |
3418 | ospf_packet_add (oi, op); | |
3419 | ||
3420 | /* Hook thread to write packet. */ | |
020709f9 | 3421 | OSPF_ISM_WRITE_ON (oi->ospf); |
718e3744 | 3422 | } |
3423 | ||
3424 | static int | |
3425 | ospf_ls_ack_send_event (struct thread *thread) | |
3426 | { | |
3427 | struct ospf_interface *oi = THREAD_ARG (thread); | |
3428 | ||
3429 | oi->t_ls_ack_direct = NULL; | |
3430 | ||
3431 | while (listcount (oi->ls_ack_direct.ls_ack)) | |
3432 | ospf_ls_ack_send_list (oi, oi->ls_ack_direct.ls_ack, | |
3433 | oi->ls_ack_direct.dst); | |
3434 | ||
3435 | return 0; | |
3436 | } | |
3437 | ||
3438 | void | |
3439 | ospf_ls_ack_send (struct ospf_neighbor *nbr, struct ospf_lsa *lsa) | |
3440 | { | |
3441 | struct ospf_interface *oi = nbr->oi; | |
3442 | ||
3443 | if (listcount (oi->ls_ack_direct.ls_ack) == 0) | |
3444 | oi->ls_ack_direct.dst = nbr->address.u.prefix4; | |
3445 | ||
3446 | listnode_add (oi->ls_ack_direct.ls_ack, ospf_lsa_lock (lsa)); | |
3447 | ||
3448 | if (oi->t_ls_ack_direct == NULL) | |
3449 | oi->t_ls_ack_direct = | |
3450 | thread_add_event (master, ospf_ls_ack_send_event, oi, 0); | |
3451 | } | |
3452 | ||
3453 | /* Send Link State Acknowledgment delayed. */ | |
3454 | void | |
3455 | ospf_ls_ack_send_delayed (struct ospf_interface *oi) | |
3456 | { | |
3457 | struct in_addr dst; | |
3458 | ||
3459 | /* Decide destination address. */ | |
3460 | /* RFC2328 Section 13.5 On non-broadcast | |
3461 | networks, delayed Link State Acknowledgment packets must be | |
3462 | unicast separately over each adjacency (i.e., neighbor whose | |
3463 | state is >= Exchange). */ | |
3464 | if (oi->type == OSPF_IFTYPE_NBMA) | |
3465 | { | |
3466 | struct ospf_neighbor *nbr; | |
3467 | struct route_node *rn; | |
3468 | ||
3469 | for (rn = route_top (oi->nbrs); rn; rn = route_next (rn)) | |
3470 | if ((nbr = rn->info) != NULL) | |
3471 | if (nbr != oi->nbr_self && nbr->state >= NSM_Exchange) | |
3472 | while (listcount (oi->ls_ack)) | |
3473 | ospf_ls_ack_send_list (oi, oi->ls_ack, nbr->address.u.prefix4); | |
3474 | return; | |
3475 | } | |
3476 | if (oi->type == OSPF_IFTYPE_VIRTUALLINK) | |
3477 | dst.s_addr = oi->vl_data->peer_addr.s_addr; | |
3478 | else if (oi->state == ISM_DR || oi->state == ISM_Backup) | |
3479 | dst.s_addr = htonl (OSPF_ALLSPFROUTERS); | |
3480 | else if (oi->type == OSPF_IFTYPE_POINTOPOINT) | |
3481 | dst.s_addr = htonl (OSPF_ALLSPFROUTERS); | |
630e4807 | 3482 | else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT) |
3483 | dst.s_addr = htonl (OSPF_ALLSPFROUTERS); | |
718e3744 | 3484 | else |
3485 | dst.s_addr = htonl (OSPF_ALLDROUTERS); | |
3486 | ||
3487 | while (listcount (oi->ls_ack)) | |
3488 | ospf_ls_ack_send_list (oi, oi->ls_ack, dst); | |
3489 | } |