forked from rwestphal/quagga-ldpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ospf_packet.c
3908 lines (3321 loc) · 114 KB
/
ospf_packet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* OSPF Sending and Receiving OSPF Packets.
* Copyright (C) 1999, 2000 Toshiaki Takada
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Zebra; see the file COPYING. If not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include <zebra.h>
#include "thread.h"
#include "memory.h"
#include "linklist.h"
#include "prefix.h"
#include "if.h"
#include "table.h"
#include "sockunion.h"
#include "stream.h"
#include "log.h"
#include "sockopt.h"
#include "checksum.h"
#include "md5.h"
#include "ospfd/ospfd.h"
#include "ospfd/ospf_network.h"
#include "ospfd/ospf_interface.h"
#include "ospfd/ospf_ism.h"
#include "ospfd/ospf_asbr.h"
#include "ospfd/ospf_lsa.h"
#include "ospfd/ospf_lsdb.h"
#include "ospfd/ospf_neighbor.h"
#include "ospfd/ospf_nsm.h"
#include "ospfd/ospf_packet.h"
#include "ospfd/ospf_spf.h"
#include "ospfd/ospf_flood.h"
#include "ospfd/ospf_dump.h"
/* Packet Type String. */
const struct message ospf_packet_type_str[] =
{
{ OSPF_MSG_HELLO, "Hello" },
{ OSPF_MSG_DB_DESC, "Database Description" },
{ OSPF_MSG_LS_REQ, "Link State Request" },
{ OSPF_MSG_LS_UPD, "Link State Update" },
{ OSPF_MSG_LS_ACK, "Link State Acknowledgment" },
};
const size_t ospf_packet_type_str_max = sizeof (ospf_packet_type_str) /
sizeof (ospf_packet_type_str[0]);
/* Minimum (besides OSPF_HEADER_SIZE) lengths for OSPF packets of
particular types, offset is the "type" field of a packet. */
static const u_int16_t ospf_packet_minlen[] =
{
0,
OSPF_HELLO_MIN_SIZE,
OSPF_DB_DESC_MIN_SIZE,
OSPF_LS_REQ_MIN_SIZE,
OSPF_LS_UPD_MIN_SIZE,
OSPF_LS_ACK_MIN_SIZE,
};
/* Minimum (besides OSPF_LSA_HEADER_SIZE) lengths for LSAs of particular
types, offset is the "LSA type" field. */
static const u_int16_t ospf_lsa_minlen[] =
{
0,
OSPF_ROUTER_LSA_MIN_SIZE,
OSPF_NETWORK_LSA_MIN_SIZE,
OSPF_SUMMARY_LSA_MIN_SIZE,
OSPF_SUMMARY_LSA_MIN_SIZE,
OSPF_AS_EXTERNAL_LSA_MIN_SIZE,
0,
OSPF_AS_EXTERNAL_LSA_MIN_SIZE,
0,
0,
0,
0,
};
/* for ospf_check_auth() */
static int ospf_check_sum (struct ospf_header *);
/* OSPF authentication checking function */
static int
ospf_auth_type (struct ospf_interface *oi)
{
int auth_type;
if (OSPF_IF_PARAM (oi, auth_type) == OSPF_AUTH_NOTSET)
auth_type = oi->area->auth_type;
else
auth_type = OSPF_IF_PARAM (oi, auth_type);
/* Handle case where MD5 key list is not configured aka Cisco */
if (auth_type == OSPF_AUTH_CRYPTOGRAPHIC &&
list_isempty (OSPF_IF_PARAM (oi, auth_crypt)))
return OSPF_AUTH_NULL;
return auth_type;
}
struct ospf_packet *
ospf_packet_new (size_t size)
{
struct ospf_packet *new;
new = XCALLOC (MTYPE_OSPF_PACKET, sizeof (struct ospf_packet));
new->s = stream_new (size);
return new;
}
void
ospf_packet_free (struct ospf_packet *op)
{
if (op->s)
stream_free (op->s);
XFREE (MTYPE_OSPF_PACKET, op);
op = NULL;
}
struct ospf_fifo *
ospf_fifo_new ()
{
struct ospf_fifo *new;
new = XCALLOC (MTYPE_OSPF_FIFO, sizeof (struct ospf_fifo));
return new;
}
/* Add new packet to fifo. */
void
ospf_fifo_push (struct ospf_fifo *fifo, struct ospf_packet *op)
{
if (fifo->tail)
fifo->tail->next = op;
else
fifo->head = op;
fifo->tail = op;
fifo->count++;
}
/* Add new packet to head of fifo. */
static void
ospf_fifo_push_head (struct ospf_fifo *fifo, struct ospf_packet *op)
{
op->next = fifo->head;
if (fifo->tail == NULL)
fifo->tail = op;
fifo->head = op;
fifo->count++;
}
/* Delete first packet from fifo. */
struct ospf_packet *
ospf_fifo_pop (struct ospf_fifo *fifo)
{
struct ospf_packet *op;
op = fifo->head;
if (op)
{
fifo->head = op->next;
if (fifo->head == NULL)
fifo->tail = NULL;
fifo->count--;
}
return op;
}
/* Return first fifo entry. */
struct ospf_packet *
ospf_fifo_head (struct ospf_fifo *fifo)
{
return fifo->head;
}
/* Flush ospf packet fifo. */
void
ospf_fifo_flush (struct ospf_fifo *fifo)
{
struct ospf_packet *op;
struct ospf_packet *next;
for (op = fifo->head; op; op = next)
{
next = op->next;
ospf_packet_free (op);
}
fifo->head = fifo->tail = NULL;
fifo->count = 0;
}
/* Free ospf packet fifo. */
void
ospf_fifo_free (struct ospf_fifo *fifo)
{
ospf_fifo_flush (fifo);
XFREE (MTYPE_OSPF_FIFO, fifo);
}
void
ospf_packet_add (struct ospf_interface *oi, struct ospf_packet *op)
{
if (!oi->obuf)
{
zlog_err("ospf_packet_add(interface %s in state %d [%s], packet type %s, "
"destination %s) called with NULL obuf, ignoring "
"(please report this bug)!\n",
IF_NAME(oi), oi->state, LOOKUP (ospf_ism_state_msg, oi->state),
LOOKUP (ospf_packet_type_str, stream_getc_from(op->s, 1)),
inet_ntoa (op->dst));
return;
}
/* Add packet to end of queue. */
ospf_fifo_push (oi->obuf, op);
/* Debug of packet fifo*/
/* ospf_fifo_debug (oi->obuf); */
}
static void
ospf_packet_add_top (struct ospf_interface *oi, struct ospf_packet *op)
{
if (!oi->obuf)
{
zlog_err("ospf_packet_add(interface %s in state %d [%s], packet type %s, "
"destination %s) called with NULL obuf, ignoring "
"(please report this bug)!\n",
IF_NAME(oi), oi->state, LOOKUP (ospf_ism_state_msg, oi->state),
LOOKUP (ospf_packet_type_str, stream_getc_from(op->s, 1)),
inet_ntoa (op->dst));
return;
}
/* Add packet to head of queue. */
ospf_fifo_push_head (oi->obuf, op);
/* Debug of packet fifo*/
/* ospf_fifo_debug (oi->obuf); */
}
void
ospf_packet_delete (struct ospf_interface *oi)
{
struct ospf_packet *op;
op = ospf_fifo_pop (oi->obuf);
if (op)
ospf_packet_free (op);
}
struct ospf_packet *
ospf_packet_dup (struct ospf_packet *op)
{
struct ospf_packet *new;
if (stream_get_endp(op->s) != op->length)
/* XXX size_t */
zlog_warn ("ospf_packet_dup stream %lu ospf_packet %u size mismatch",
(u_long)STREAM_SIZE(op->s), op->length);
/* Reserve space for MD5 authentication that may be added later. */
new = ospf_packet_new (stream_get_endp(op->s) + OSPF_AUTH_MD5_SIZE);
stream_copy (new->s, op->s);
new->dst = op->dst;
new->length = op->length;
return new;
}
/* XXX inline */
static unsigned int
ospf_packet_authspace (struct ospf_interface *oi)
{
int auth = 0;
if ( ospf_auth_type (oi) == OSPF_AUTH_CRYPTOGRAPHIC)
auth = OSPF_AUTH_MD5_SIZE;
return auth;
}
static unsigned int
ospf_packet_max (struct ospf_interface *oi)
{
int max;
max = oi->ifp->mtu - ospf_packet_authspace(oi);
max -= (OSPF_HEADER_SIZE + sizeof (struct ip));
return max;
}
static int
ospf_check_md5_digest (struct ospf_interface *oi, struct ospf_header *ospfh)
{
MD5_CTX ctx;
unsigned char digest[OSPF_AUTH_MD5_SIZE];
struct crypt_key *ck;
struct ospf_neighbor *nbr;
u_int16_t length = ntohs (ospfh->length);
/* Get secret key. */
ck = ospf_crypt_key_lookup (OSPF_IF_PARAM (oi, auth_crypt),
ospfh->u.crypt.key_id);
if (ck == NULL)
{
zlog_warn ("interface %s: ospf_check_md5 no key %d",
IF_NAME (oi), ospfh->u.crypt.key_id);
return 0;
}
/* check crypto seqnum. */
nbr = ospf_nbr_lookup_by_routerid (oi->nbrs, &ospfh->router_id);
if (nbr && ntohl(nbr->crypt_seqnum) > ntohl(ospfh->u.crypt.crypt_seqnum))
{
zlog_warn ("interface %s: ospf_check_md5 bad sequence %d (expect %d)",
IF_NAME (oi),
ntohl(ospfh->u.crypt.crypt_seqnum),
ntohl(nbr->crypt_seqnum));
return 0;
}
/* Generate a digest for the ospf packet - their digest + our digest. */
memset(&ctx, 0, sizeof(ctx));
MD5Init(&ctx);
MD5Update(&ctx, ospfh, length);
MD5Update(&ctx, ck->auth_key, OSPF_AUTH_MD5_SIZE);
MD5Final(digest, &ctx);
/* compare the two */
if (memcmp ((caddr_t)ospfh + length, digest, OSPF_AUTH_MD5_SIZE))
{
zlog_warn ("interface %s: ospf_check_md5 checksum mismatch",
IF_NAME (oi));
return 0;
}
/* save neighbor's crypt_seqnum */
if (nbr)
nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
return 1;
}
/* This function is called from ospf_write(), it will detect the
authentication scheme and if it is MD5, it will change the sequence
and update the MD5 digest. */
static int
ospf_make_md5_digest (struct ospf_interface *oi, struct ospf_packet *op)
{
struct ospf_header *ospfh;
unsigned char digest[OSPF_AUTH_MD5_SIZE] = {0};
MD5_CTX ctx;
void *ibuf;
u_int32_t t;
struct crypt_key *ck;
const u_int8_t *auth_key;
ibuf = STREAM_DATA (op->s);
ospfh = (struct ospf_header *) ibuf;
if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
return 0;
/* We do this here so when we dup a packet, we don't have to
waste CPU rewriting other headers.
Note that quagga_time /deliberately/ is not used here */
t = (time(NULL) & 0xFFFFFFFF);
if (t > oi->crypt_seqnum)
oi->crypt_seqnum = t;
else
oi->crypt_seqnum++;
ospfh->u.crypt.crypt_seqnum = htonl (oi->crypt_seqnum);
/* Get MD5 Authentication key from auth_key list. */
if (list_isempty (OSPF_IF_PARAM (oi, auth_crypt)))
auth_key = (const u_int8_t *) digest;
else
{
ck = listgetdata (listtail(OSPF_IF_PARAM (oi, auth_crypt)));
auth_key = ck->auth_key;
}
/* Generate a digest for the entire packet + our secret key. */
memset(&ctx, 0, sizeof(ctx));
MD5Init(&ctx);
MD5Update(&ctx, ibuf, ntohs (ospfh->length));
MD5Update(&ctx, auth_key, OSPF_AUTH_MD5_SIZE);
MD5Final(digest, &ctx);
/* Append md5 digest to the end of the stream. */
stream_put (op->s, digest, OSPF_AUTH_MD5_SIZE);
/* We do *NOT* increment the OSPF header length. */
op->length = ntohs (ospfh->length) + OSPF_AUTH_MD5_SIZE;
if (stream_get_endp(op->s) != op->length)
/* XXX size_t */
zlog_warn("ospf_make_md5_digest: length mismatch stream %lu ospf_packet %u",
(u_long)stream_get_endp(op->s), op->length);
return OSPF_AUTH_MD5_SIZE;
}
static int
ospf_ls_req_timer (struct thread *thread)
{
struct ospf_neighbor *nbr;
nbr = THREAD_ARG (thread);
nbr->t_ls_req = NULL;
/* Send Link State Request. */
if (ospf_ls_request_count (nbr))
ospf_ls_req_send (nbr);
/* Set Link State Request retransmission timer. */
OSPF_NSM_TIMER_ON (nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req);
return 0;
}
void
ospf_ls_req_event (struct ospf_neighbor *nbr)
{
if (nbr->t_ls_req)
{
thread_cancel (nbr->t_ls_req);
nbr->t_ls_req = NULL;
}
nbr->t_ls_req = thread_add_event (master, ospf_ls_req_timer, nbr, 0);
}
/* Cyclic timer function. Fist registered in ospf_nbr_new () in
ospf_neighbor.c */
int
ospf_ls_upd_timer (struct thread *thread)
{
struct ospf_neighbor *nbr;
nbr = THREAD_ARG (thread);
nbr->t_ls_upd = NULL;
/* Send Link State Update. */
if (ospf_ls_retransmit_count (nbr) > 0)
{
struct list *update;
struct ospf_lsdb *lsdb;
int i;
int retransmit_interval;
retransmit_interval = OSPF_IF_PARAM (nbr->oi, retransmit_interval);
lsdb = &nbr->ls_rxmt;
update = list_new ();
for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
{
struct route_table *table = lsdb->type[i].db;
struct route_node *rn;
for (rn = route_top (table); rn; rn = route_next (rn))
{
struct ospf_lsa *lsa;
if ((lsa = rn->info) != NULL)
/* Don't retransmit an LSA if we received it within
the last RxmtInterval seconds - this is to allow the
neighbour a chance to acknowledge the LSA as it may
have ben just received before the retransmit timer
fired. This is a small tweak to what is in the RFC,
but it will cut out out a lot of retransmit traffic
- MAG */
if (tv_cmp (tv_sub (recent_relative_time (), lsa->tv_recv),
int2tv (retransmit_interval)) >= 0)
listnode_add (update, rn->info);
}
}
if (listcount (update) > 0)
ospf_ls_upd_send (nbr, update, OSPF_SEND_PACKET_DIRECT);
list_delete (update);
}
/* Set LS Update retransmission timer. */
OSPF_NSM_TIMER_ON (nbr->t_ls_upd, ospf_ls_upd_timer, nbr->v_ls_upd);
return 0;
}
int
ospf_ls_ack_timer (struct thread *thread)
{
struct ospf_interface *oi;
oi = THREAD_ARG (thread);
oi->t_ls_ack = NULL;
/* Send Link State Acknowledgment. */
if (listcount (oi->ls_ack) > 0)
ospf_ls_ack_send_delayed (oi);
/* Set LS Ack timer. */
OSPF_ISM_TIMER_ON (oi->t_ls_ack, ospf_ls_ack_timer, oi->v_ls_ack);
return 0;
}
#ifdef WANT_OSPF_WRITE_FRAGMENT
static void
ospf_write_frags (int fd, struct ospf_packet *op, struct ip *iph,
struct msghdr *msg, unsigned int maxdatasize,
unsigned int mtu, int flags, u_char type)
{
#define OSPF_WRITE_FRAG_SHIFT 3
u_int16_t offset;
struct iovec *iovp;
int ret;
assert ( op->length == stream_get_endp(op->s) );
assert (msg->msg_iovlen == 2);
/* we can but try.
*
* SunOS, BSD and BSD derived kernels likely will clear ip_id, as
* well as the IP_MF flag, making this all quite pointless.
*
* However, for a system on which IP_MF is left alone, and ip_id left
* alone or else which sets same ip_id for each fragment this might
* work, eg linux.
*
* XXX-TODO: It would be much nicer to have the kernel's use their
* existing fragmentation support to do this for us. Bugs/RFEs need to
* be raised against the various kernels.
*/
/* set More Frag */
iph->ip_off |= IP_MF;
/* ip frag offset is expressed in units of 8byte words */
offset = maxdatasize >> OSPF_WRITE_FRAG_SHIFT;
iovp = &msg->msg_iov[1];
while ( (stream_get_endp(op->s) - stream_get_getp (op->s))
> maxdatasize )
{
/* data length of this frag is to next offset value */
iovp->iov_len = offset << OSPF_WRITE_FRAG_SHIFT;
iph->ip_len = iovp->iov_len + sizeof (struct ip);
assert (iph->ip_len <= mtu);
sockopt_iphdrincl_swab_htosys (iph);
ret = sendmsg (fd, msg, flags);
sockopt_iphdrincl_swab_systoh (iph);
if (ret < 0)
zlog_warn ("*** ospf_write_frags: sendmsg failed to %s,"
" id %d, off %d, len %d, mtu %u failed with %s",
inet_ntoa (iph->ip_dst),
iph->ip_id,
iph->ip_off,
iph->ip_len,
mtu,
safe_strerror (errno));
if (IS_DEBUG_OSPF_PACKET (type - 1, SEND))
{
zlog_debug ("ospf_write_frags: sent id %d, off %d, len %d to %s\n",
iph->ip_id, iph->ip_off, iph->ip_len,
inet_ntoa (iph->ip_dst));
if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL))
{
zlog_debug ("-----------------IP Header Dump----------------------");
ospf_ip_header_dump (iph);
zlog_debug ("-----------------------------------------------------");
}
}
iph->ip_off += offset;
stream_forward_getp (op->s, iovp->iov_len);
iovp->iov_base = STREAM_PNT (op->s);
}
/* setup for final fragment */
iovp->iov_len = stream_get_endp(op->s) - stream_get_getp (op->s);
iph->ip_len = iovp->iov_len + sizeof (struct ip);
iph->ip_off &= (~IP_MF);
}
#endif /* WANT_OSPF_WRITE_FRAGMENT */
static int
ospf_write (struct thread *thread)
{
struct ospf *ospf = THREAD_ARG (thread);
struct ospf_interface *oi;
struct ospf_packet *op;
struct sockaddr_in sa_dst;
struct ip iph;
struct msghdr msg;
struct iovec iov[2];
u_char type;
int ret;
int flags = 0;
struct listnode *node;
#ifdef WANT_OSPF_WRITE_FRAGMENT
static u_int16_t ipid = 0;
u_int16_t maxdatasize;
#endif /* WANT_OSPF_WRITE_FRAGMENT */
#define OSPF_WRITE_IPHL_SHIFT 2
ospf->t_write = NULL;
node = listhead (ospf->oi_write_q);
assert (node);
oi = listgetdata (node);
assert (oi);
#ifdef WANT_OSPF_WRITE_FRAGMENT
/* seed ipid static with low order bits of time */
if (ipid == 0)
ipid = (time(NULL) & 0xffff);
/* convenience - max OSPF data per packet,
* and reliability - not more data, than our
* socket can accept
*/
maxdatasize = MIN (oi->ifp->mtu, ospf->maxsndbuflen) -
sizeof (struct ip);
#endif /* WANT_OSPF_WRITE_FRAGMENT */
/* Get one packet from queue. */
op = ospf_fifo_head (oi->obuf);
assert (op);
assert (op->length >= OSPF_HEADER_SIZE);
if (op->dst.s_addr == htonl (OSPF_ALLSPFROUTERS)
|| op->dst.s_addr == htonl (OSPF_ALLDROUTERS))
ospf_if_ipmulticast (ospf, oi->address, oi->ifp->ifindex);
/* Rewrite the md5 signature & update the seq */
ospf_make_md5_digest (oi, op);
/* Retrieve OSPF packet type. */
stream_set_getp (op->s, 1);
type = stream_getc (op->s);
/* reset get pointer */
stream_set_getp (op->s, 0);
memset (&iph, 0, sizeof (struct ip));
memset (&sa_dst, 0, sizeof (sa_dst));
sa_dst.sin_family = AF_INET;
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
sa_dst.sin_len = sizeof(sa_dst);
#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
sa_dst.sin_addr = op->dst;
sa_dst.sin_port = htons (0);
/* Set DONTROUTE flag if dst is unicast. */
if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
if (!IN_MULTICAST (htonl (op->dst.s_addr)))
flags = MSG_DONTROUTE;
iph.ip_hl = sizeof (struct ip) >> OSPF_WRITE_IPHL_SHIFT;
/* it'd be very strange for header to not be 4byte-word aligned but.. */
if ( sizeof (struct ip)
> (unsigned int)(iph.ip_hl << OSPF_WRITE_IPHL_SHIFT) )
iph.ip_hl++; /* we presume sizeof struct ip cant overflow ip_hl.. */
iph.ip_v = IPVERSION;
iph.ip_tos = IPTOS_PREC_INTERNETCONTROL;
iph.ip_len = (iph.ip_hl << OSPF_WRITE_IPHL_SHIFT) + op->length;
#if defined(__DragonFly__)
/*
* DragonFly's raw socket expects ip_len/ip_off in network byte order.
*/
iph.ip_len = htons(iph.ip_len);
#endif
#ifdef WANT_OSPF_WRITE_FRAGMENT
/* XXX-MT: not thread-safe at all..
* XXX: this presumes this is only programme sending OSPF packets
* otherwise, no guarantee ipid will be unique
*/
iph.ip_id = ++ipid;
#endif /* WANT_OSPF_WRITE_FRAGMENT */
iph.ip_off = 0;
if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
iph.ip_ttl = OSPF_VL_IP_TTL;
else
iph.ip_ttl = OSPF_IP_TTL;
iph.ip_p = IPPROTO_OSPFIGP;
iph.ip_sum = 0;
iph.ip_src.s_addr = oi->address->u.prefix4.s_addr;
iph.ip_dst.s_addr = op->dst.s_addr;
memset (&msg, 0, sizeof (msg));
msg.msg_name = (caddr_t) &sa_dst;
msg.msg_namelen = sizeof (sa_dst);
msg.msg_iov = iov;
msg.msg_iovlen = 2;
iov[0].iov_base = (char*)&iph;
iov[0].iov_len = iph.ip_hl << OSPF_WRITE_IPHL_SHIFT;
iov[1].iov_base = STREAM_PNT (op->s);
iov[1].iov_len = op->length;
/* Sadly we can not rely on kernels to fragment packets because of either
* IP_HDRINCL and/or multicast destination being set.
*/
#ifdef WANT_OSPF_WRITE_FRAGMENT
if ( op->length > maxdatasize )
ospf_write_frags (ospf->fd, op, &iph, &msg, maxdatasize,
oi->ifp->mtu, flags, type);
#endif /* WANT_OSPF_WRITE_FRAGMENT */
/* send final fragment (could be first) */
sockopt_iphdrincl_swab_htosys (&iph);
ret = sendmsg (ospf->fd, &msg, flags);
sockopt_iphdrincl_swab_systoh (&iph);
if (ret < 0)
zlog_warn ("*** sendmsg in ospf_write failed to %s, "
"id %d, off %d, len %d, interface %s, mtu %u: %s",
inet_ntoa (iph.ip_dst), iph.ip_id, iph.ip_off, iph.ip_len,
oi->ifp->name, oi->ifp->mtu, safe_strerror (errno));
/* Show debug sending packet. */
if (IS_DEBUG_OSPF_PACKET (type - 1, SEND))
{
if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL))
{
zlog_debug ("-----------------------------------------------------");
ospf_ip_header_dump (&iph);
stream_set_getp (op->s, 0);
ospf_packet_dump (op->s);
}
zlog_debug ("%s sent to [%s] via [%s].",
LOOKUP (ospf_packet_type_str, type), inet_ntoa (op->dst),
IF_NAME (oi));
if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL))
zlog_debug ("-----------------------------------------------------");
}
/* Now delete packet from queue. */
ospf_packet_delete (oi);
/* Move this interface to the tail of write_q to
serve everyone in a round robin fashion */
listnode_move_to_tail (ospf->oi_write_q, node);
if (ospf_fifo_head (oi->obuf) == NULL)
{
oi->on_write_q = 0;
list_delete_node (ospf->oi_write_q, node);
}
/* If packets still remain in queue, call write thread. */
if (!list_isempty (ospf->oi_write_q))
ospf->t_write =
thread_add_write (master, ospf_write, ospf, ospf->fd);
return 0;
}
/* OSPF Hello message read -- RFC2328 Section 10.5. */
static void
ospf_hello (struct ip *iph, struct ospf_header *ospfh,
struct stream * s, struct ospf_interface *oi, int size)
{
struct ospf_hello *hello;
struct ospf_neighbor *nbr;
int old_state;
struct prefix p;
/* increment statistics. */
oi->hello_in++;
hello = (struct ospf_hello *) STREAM_PNT (s);
/* If Hello is myself, silently discard. */
if (IPV4_ADDR_SAME (&ospfh->router_id, &oi->ospf->router_id))
{
if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
{
zlog_debug ("ospf_header[%s/%s]: selforiginated, "
"dropping.",
LOOKUP (ospf_packet_type_str, ospfh->type),
inet_ntoa (iph->ip_src));
}
return;
}
/* get neighbor prefix. */
p.family = AF_INET;
p.prefixlen = ip_masklen (hello->network_mask);
p.u.prefix4 = iph->ip_src;
/* Compare network mask. */
/* Checking is ignored for Point-to-Point and Virtual link. */
if (oi->type != OSPF_IFTYPE_POINTOPOINT
&& oi->type != OSPF_IFTYPE_VIRTUALLINK)
if (oi->address->prefixlen != p.prefixlen)
{
zlog_warn ("Packet %s [Hello:RECV]: NetworkMask mismatch on %s (configured prefix length is %d, but hello packet indicates %d).",
inet_ntoa(ospfh->router_id), IF_NAME(oi),
(int)oi->address->prefixlen, (int)p.prefixlen);
return;
}
/* Compare Router Dead Interval. */
if (OSPF_IF_PARAM (oi, v_wait) != ntohl (hello->dead_interval))
{
zlog_warn ("Packet %s [Hello:RECV]: RouterDeadInterval mismatch "
"(expected %u, but received %u).",
inet_ntoa(ospfh->router_id),
OSPF_IF_PARAM(oi, v_wait), ntohl(hello->dead_interval));
return;
}
/* Compare Hello Interval - ignored if fast-hellos are set. */
if (OSPF_IF_PARAM (oi, fast_hello) == 0)
{
if (OSPF_IF_PARAM (oi, v_hello) != ntohs (hello->hello_interval))
{
zlog_warn ("Packet %s [Hello:RECV]: HelloInterval mismatch "
"(expected %u, but received %u).",
inet_ntoa(ospfh->router_id),
OSPF_IF_PARAM(oi, v_hello), ntohs(hello->hello_interval));
return;
}
}
if (IS_DEBUG_OSPF_EVENT)
zlog_debug ("Packet %s [Hello:RECV]: Options %s",
inet_ntoa (ospfh->router_id),
ospf_options_dump (hello->options));
/* Compare options. */
#define REJECT_IF_TBIT_ON 1 /* XXX */
#ifdef REJECT_IF_TBIT_ON
if (CHECK_FLAG (hello->options, OSPF_OPTION_T))
{
/*
* This router does not support non-zero TOS.
* Drop this Hello packet not to establish neighbor relationship.
*/
zlog_warn ("Packet %s [Hello:RECV]: T-bit on, drop it.",
inet_ntoa (ospfh->router_id));
return;
}
#endif /* REJECT_IF_TBIT_ON */
if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE)
&& CHECK_FLAG (hello->options, OSPF_OPTION_O))
{
/*
* This router does know the correct usage of O-bit
* the bit should be set in DD packet only.
*/
zlog_warn ("Packet %s [Hello:RECV]: O-bit abuse?",
inet_ntoa (ospfh->router_id));
#ifdef STRICT_OBIT_USAGE_CHECK
return; /* Reject this packet. */
#else /* STRICT_OBIT_USAGE_CHECK */
UNSET_FLAG (hello->options, OSPF_OPTION_O); /* Ignore O-bit. */
#endif /* STRICT_OBIT_USAGE_CHECK */
}
/* new for NSSA is to ensure that NP is on and E is off */
if (oi->area->external_routing == OSPF_AREA_NSSA)
{
if (! (CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_NP)
&& CHECK_FLAG (hello->options, OSPF_OPTION_NP)
&& ! CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_E)
&& ! CHECK_FLAG (hello->options, OSPF_OPTION_E)))
{
zlog_warn ("NSSA-Packet-%s[Hello:RECV]: my options: %x, his options %x", inet_ntoa (ospfh->router_id), OPTIONS (oi), hello->options);
return;
}
if (IS_DEBUG_OSPF_NSSA)
zlog_debug ("NSSA-Hello:RECV:Packet from %s:", inet_ntoa(ospfh->router_id));
}
else
/* The setting of the E-bit found in the Hello Packet's Options
field must match this area's ExternalRoutingCapability A
mismatch causes processing to stop and the packet to be
dropped. The setting of the rest of the bits in the Hello
Packet's Options field should be ignored. */
if (CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_E) !=
CHECK_FLAG (hello->options, OSPF_OPTION_E))
{
zlog_warn ("Packet %s [Hello:RECV]: my options: %x, his options %x",
inet_ntoa(ospfh->router_id), OPTIONS (oi), hello->options);
return;
}
/* get neighbour struct */
nbr = ospf_nbr_get (oi, ospfh, iph, &p);
/* neighbour must be valid, ospf_nbr_get creates if none existed */
assert (nbr);
old_state = nbr->state;
/* Add event to thread. */
OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived);
/* RFC2328 Section 9.5.1
If the router is not eligible to become Designated Router,
(snip) It must also send an Hello Packet in reply to an
Hello Packet received from any eligible neighbor (other than
the current Designated Router and Backup Designated Router). */
if (oi->type == OSPF_IFTYPE_NBMA)
if (PRIORITY(oi) == 0 && hello->priority > 0
&& IPV4_ADDR_CMP(&DR(oi), &iph->ip_src)
&& IPV4_ADDR_CMP(&BDR(oi), &iph->ip_src))
OSPF_NSM_TIMER_ON (nbr->t_hello_reply, ospf_hello_reply_timer,
OSPF_HELLO_REPLY_DELAY);
/* on NBMA network type, it happens to receive bidirectional Hello packet
without advance 1-Way Received event.
To avoid incorrect DR-seletion, raise 1-Way Received event.*/
if (oi->type == OSPF_IFTYPE_NBMA &&
(old_state == NSM_Down || old_state == NSM_Attempt))
{
OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_OneWayReceived);
nbr->priority = hello->priority;
nbr->d_router = hello->d_router;
nbr->bd_router = hello->bd_router;
return;
}
if (ospf_nbr_bidirectional (&oi->ospf->router_id, hello->neighbors,
size - OSPF_HELLO_MIN_SIZE))
{
OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_TwoWayReceived);
nbr->options |= hello->options;
}
else
{
OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_OneWayReceived);
/* Set neighbor information. */
nbr->priority = hello->priority;
nbr->d_router = hello->d_router;
nbr->bd_router = hello->bd_router;
return;
}
/* If neighbor itself declares DR and no BDR exists,
cause event BackupSeen */
if (IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->d_router))
if (hello->bd_router.s_addr == 0 && oi->state == ISM_Waiting)
OSPF_ISM_EVENT_SCHEDULE (oi, ISM_BackupSeen);
/* neighbor itself declares BDR. */