-
Notifications
You must be signed in to change notification settings - Fork 192
/
ofl-messages-unpack.c
1779 lines (1447 loc) · 62.8 KB
/
ofl-messages-unpack.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
/* Copyright (c) 2011, TrafficLab, Ericsson Research, Hungary
* Copyright (c) 2012, CPqD, Brazil
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Ericsson Research nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <endian.h>
#include "ofl-actions.h"
#include "ofl-messages.h"
#include "ofl-structs.h"
#include "ofl-utils.h"
#include "ofl-print.h"
#include "ofl-log.h"
#include "openflow/openflow.h"
#define UNUSED __attribute__((__unused__))
#define LOG_MODULE ofl_msg_u
OFL_LOG_INIT(LOG_MODULE)
/****************************************************************************
* Functions for unpacking ofp wire format to ofl structures.
****************************************************************************/
static ofl_err
ofl_msg_unpack_error(struct ofp_header *src, size_t *len, struct ofl_msg_header **msg) {
struct ofp_error_msg *se;
struct ofl_msg_error *de;
if (*len < sizeof(struct ofp_error_msg)) {
OFL_LOG_WARN(LOG_MODULE, "Received ERROR message invalid length (%zu).", *len);
return OFL_ERROR;
}
*len -= sizeof(struct ofp_error_msg);
se = (struct ofp_error_msg *)src;
de = (struct ofl_msg_error *)malloc(sizeof(struct ofl_msg_error));
de->type = (enum ofp_error_type)((int)ntohs(se->type));
de->code = ntohs(se->code);
de->data_length = *len;
de->data = *len > 0 ? (uint8_t *)memcpy(malloc(*len), se->data, *len) : NULL;
*len = 0;
(*msg) = (struct ofl_msg_header *)de;
return 0;
}
static ofl_err
ofl_msg_unpack_echo(struct ofp_header *src, size_t *len, struct ofl_msg_header **msg) {
struct ofl_msg_echo *e = (struct ofl_msg_echo *)malloc(sizeof(struct ofl_msg_echo));
uint8_t *data;
// ofp_header length was checked at ofl_msg_unpack
*len -= sizeof(struct ofp_header);
data = (uint8_t *)src + sizeof(struct ofp_header);
e->data_length = *len;
e->data = *len > 0 ? (uint8_t *)memcpy(malloc(*len), data, *len) : NULL;
*len = 0;
*msg = (struct ofl_msg_header *)e;
return 0;
}
static ofl_err
ofl_msg_unpack_role_request(struct ofp_header *src, size_t *len, struct ofl_msg_header **msg) {
struct ofp_role_request *srl;
struct ofl_msg_role_request *drl;
if (*len < sizeof(struct ofp_role_request)){
OFL_LOG_WARN(LOG_MODULE, "Received ROLE message has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
*len -= sizeof(struct ofp_role_request);
srl = (struct ofp_role_request *) src;
drl = (struct ofl_msg_role_request *) malloc(sizeof(struct ofl_msg_role_request));
drl->role = ntohl(srl->role);
drl->generation_id = ntoh64(srl->generation_id);
*msg = (struct ofl_msg_header *)drl;
return 0;
}
static ofl_err
ofl_msg_unpack_features_reply(struct ofp_header *src, size_t *len, struct ofl_msg_header **msg) {
struct ofp_switch_features *sr;
struct ofl_msg_features_reply *dr;
if (*len < sizeof(struct ofp_switch_features)) {
OFL_LOG_WARN(LOG_MODULE, "Received FEATURES_REPLY message has invalid length (%zu).", *len);
}
*len -= sizeof(struct ofp_switch_features);
sr = (struct ofp_switch_features *)src;
dr = (struct ofl_msg_features_reply *)malloc(sizeof(struct ofl_msg_features_reply));
dr->datapath_id = ntoh64(sr->datapath_id);
dr->n_buffers = ntohl( sr->n_buffers);
dr->n_tables = sr->n_tables;
dr->auxiliary_id = sr->auxiliary_id;
dr->capabilities = ntohl( sr->capabilities);
*msg = (struct ofl_msg_header *)dr;
return 0;
}
static ofl_err
ofl_msg_unpack_get_config_reply(struct ofp_header *src, size_t *len, struct ofl_msg_header **msg) {
struct ofp_switch_config *sr;
struct ofl_msg_get_config_reply *dr;
if (*len < sizeof(struct ofp_switch_config)) {
OFL_LOG_WARN(LOG_MODULE, "Received GET_CONFIG_REPLY message has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
*len -= sizeof(struct ofp_switch_config);
sr = (struct ofp_switch_config *)src;
dr = (struct ofl_msg_get_config_reply *)malloc(sizeof(struct ofl_msg_get_config_reply));
dr->config = (struct ofl_config *)malloc(sizeof(struct ofl_config));
dr->config->miss_send_len = ntohs(sr->miss_send_len);
dr->config->flags = ntohs(sr->flags);
*msg = (struct ofl_msg_header *)dr;
return 0;
}
static ofl_err
ofl_msg_unpack_set_config(struct ofp_header *src, size_t *len, struct ofl_msg_header **msg) {
struct ofp_switch_config *sr;
struct ofl_msg_set_config *dr;
if (*len < sizeof(struct ofp_switch_config)) {
OFL_LOG_WARN(LOG_MODULE, "Received SET_CONFIG message has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
*len -= sizeof(struct ofp_switch_config);
sr = (struct ofp_switch_config *)src;
dr = (struct ofl_msg_set_config *)malloc(sizeof(struct ofl_msg_set_config));
dr->config = (struct ofl_config *)malloc(sizeof(struct ofl_config));
// TODO Zoltan: validate flags
dr->config->miss_send_len = ntohs(sr->miss_send_len);
dr->config->flags = ntohs(sr->flags);
*msg = (struct ofl_msg_header *)dr;
return 0;
}
static ofl_err
ofl_msg_unpack_async_config(struct ofp_header *src, size_t *len, struct ofl_msg_header **msg){
struct ofp_async_config *sac;
struct ofl_msg_async_config *dac;
int i;
if (*len < sizeof(struct ofp_async_config)) {
OFL_LOG_WARN(LOG_MODULE, "Received ASYNC CONFIG message has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
*len -= sizeof(struct ofp_async_config);
sac = (struct ofp_async_config*)src;
dac = (struct ofl_msg_async_config*)malloc(sizeof(struct ofl_msg_async_config));
dac->config = (struct ofl_async_config*) malloc(sizeof(struct ofl_async_config));
for(i = 0; i < 2; i++){
dac->config->packet_in_mask[i] = ntohl(sac->packet_in_mask[i]);
dac->config->port_status_mask[i] = ntohl(sac->port_status_mask[i]);
dac->config->flow_removed_mask[i] = ntohl(sac->flow_removed_mask[i]);
}
*msg = (struct ofl_msg_header*)dac;
return 0;
}
static ofl_err
ofl_msg_unpack_packet_in(struct ofp_header *src, uint8_t* buf, size_t *len, struct ofl_msg_header **msg) {
struct ofp_packet_in *sp;
struct ofl_msg_packet_in *dp;
uint8_t *ptr;
if (*len < sizeof(struct ofp_packet_in)) {
OFL_LOG_WARN(LOG_MODULE, "Received PACKET_IN message has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
sp = (struct ofp_packet_in *)src;
/* TODO: Check in_port oxm_field */
/*if (ntohl(sp->in_port) == 0 ||
(ntohl(sp->in_port) > OFPP_MAX &&
ntohl(sp->in_port) != OFPP_LOCAL)) {
if (OFL_LOG_IS_WARN_ENABLED(LOG_MODULE)) {
char *ps = ofl_port_to_string(ntohl(sp->in_port));
OFL_LOG_WARN(LOG_MODULE, "Received PACKET_IN message has invalid in_port (%s).", ps);
free(ps);
}
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_PORT);
}*/
if (sp->table_id == PIPELINE_TABLES) {
if (OFL_LOG_IS_WARN_ENABLED(LOG_MODULE)) {
char *ts = ofl_table_to_string(sp->table_id);
OFL_LOG_WARN(LOG_MODULE, "Received PACKET_IN has invalid table_id (%s).", ts);
free(ts);
}
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_TABLE_ID);
}
*len -= sizeof(struct ofp_packet_in) - sizeof(struct ofp_match);
dp = (struct ofl_msg_packet_in *)malloc(sizeof(struct ofl_msg_packet_in));
dp->buffer_id = ntohl(sp->buffer_id);
dp->total_len = ntohs(sp->total_len);
dp->reason = (enum ofp_packet_in_reason)sp->reason;
dp->table_id = sp->table_id;
dp->cookie = ntoh64(sp->cookie);
ptr = buf + (sizeof(struct ofp_packet_in)-4);
ofl_structs_match_unpack(&(sp->match),ptr, len ,&(dp->match),NULL);
ptr = buf + ROUND_UP(sizeof(struct ofp_packet_in)-4 + dp->match->length,8) + 2;
/* Minus padding bytes */
*len -= 2;
dp->data_length = *len;
dp->data = *len > 0 ? (uint8_t *)memcpy(malloc(*len), ptr, *len) : NULL;
*len = 0;
*msg = (struct ofl_msg_header *)dp;
return 0;
}
static ofl_err
ofl_msg_unpack_flow_removed(struct ofp_header *src,uint8_t *buf, size_t *len, struct ofl_msg_header **msg, struct ofl_exp *exp) {
struct ofp_flow_removed *sr;
struct ofl_msg_flow_removed *dr;
ofl_err error;
int match_pos;
if (*len < (sizeof(struct ofp_flow_removed) - sizeof(struct ofp_match))) {
OFL_LOG_WARN(LOG_MODULE, "Received FLOW_REMOVED message has invalid length (%zu).", *len);
return OFL_ERROR;
}
sr = (struct ofp_flow_removed *)src ;
if (sr->table_id >= PIPELINE_TABLES) {
if (OFL_LOG_IS_WARN_ENABLED(LOG_MODULE)) {
char *ts = ofl_table_to_string(sr->table_id);
OFL_LOG_WARN(LOG_MODULE, "Received FLOW_REMOVED message has invalid table_id (%s).", ts);
free(ts);
}
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_TABLE_ID);
}
*len -= sizeof(struct ofp_flow_removed) - sizeof(struct ofp_match) ;
dr = (struct ofl_msg_flow_removed *)malloc(sizeof(struct ofl_msg_flow_removed));
dr->reason = (enum ofp_flow_removed_reason)sr->reason;
dr->stats = (struct ofl_flow_stats *)malloc(sizeof(struct ofl_flow_stats));
dr->stats->table_id = sr->table_id;
dr->stats->duration_sec = ntohl( sr->duration_sec);
dr->stats->duration_nsec = ntohl( sr->duration_nsec);
dr->stats->priority = ntohs(sr->priority);
dr->stats->idle_timeout = ntohs( sr->idle_timeout);
dr->stats->hard_timeout = 0;
dr->stats->cookie = ntoh64(sr->cookie);
dr->stats->packet_count = ntoh64(sr->packet_count);
dr->stats->byte_count = ntoh64(sr->byte_count);
dr->stats->instructions_num = 0;
dr->stats->instructions = NULL;
match_pos = sizeof(struct ofp_flow_removed) - 4;
error = ofl_structs_match_unpack(&(sr->match),buf + match_pos, len, &(dr->stats->match), exp);
if (error) {
free(dr->stats);
free(dr);
return error;
}
*msg = (struct ofl_msg_header *)dr;
return 0;
}
static ofl_err
ofl_msg_unpack_port_status(struct ofp_header *src, size_t *len, struct ofl_msg_header **msg) {
struct ofp_port_status *ss;
struct ofl_msg_port_status *ds;
ofl_err error;
if (*len < sizeof(struct ofp_port_status)) {
OFL_LOG_WARN(LOG_MODULE, "Received PORT_STATUS message has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
*len -= (sizeof(struct ofp_port_status) - sizeof(struct ofp_port));
ss = (struct ofp_port_status *)src;
ds = (struct ofl_msg_port_status *)malloc(sizeof(struct ofl_msg_port_status));
ds->reason = (enum ofp_port_reason) ss->reason;
error = ofl_structs_port_unpack(&(ss->desc), len, &(ds->desc));
if (error) {
free(ds);
return error;
}
*msg = (struct ofl_msg_header *)ds;
return 0;
}
static ofl_err
ofl_msg_unpack_packet_out(struct ofp_header *src, size_t *len, struct ofl_msg_header **msg, struct ofl_exp *exp) {
struct ofp_packet_out *sp;
struct ofl_msg_packet_out *dp;
struct ofp_action_header *act;
uint8_t *data;
ofl_err error;
size_t i, actions_num;
if (*len < sizeof(struct ofp_packet_out)) {
OFL_LOG_WARN(LOG_MODULE, "Received PACKET_OUT message has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
sp = (struct ofp_packet_out *)src;
/*if (ntohl(sp->in_port) == 0 ||
(ntohl(sp->in_port) > OFPP_MAX && ntohl(sp->in_port) != OFPP_CONTROLLER)) {
if (OFL_LOG_IS_WARN_ENABLED(LOG_MODULE)) {
char *ps = ofl_port_to_string(ntohl(sp->in_port));
OFL_LOG_WARN(LOG_MODULE, "Received PACKET_OUT message with invalid in_port (%s).", ps);
free(ps);
}
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_PORT);
}*/
if (ntohl(sp->buffer_id) != 0xffffffff &&
*len != sizeof(struct ofp_packet_out) + ntohs(sp->actions_len)) {
if (OFL_LOG_IS_WARN_ENABLED(LOG_MODULE)) {
char *bs = ofl_buffer_to_string(ntohl(sp->buffer_id));
OFL_LOG_WARN(LOG_MODULE, "Received PACKET_OUT message with data and buffer_id (%s).", bs);
free(bs);
}
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
*len -= sizeof(struct ofp_packet_out);
dp = (struct ofl_msg_packet_out *)malloc(sizeof(struct ofl_msg_packet_out));
dp->buffer_id = ntohl(sp->buffer_id);
dp->in_port = ntohl(sp->in_port);
if (*len < ntohs(sp->actions_len)) {
OFL_LOG_WARN(LOG_MODULE, "Received PACKET_OUT message has invalid action length (%zu).", *len);
free(dp);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
error = ofl_utils_count_ofp_actions(&(sp->actions), ntohs(sp->actions_len), &actions_num);
if (error) {
free(dp);
return error;
}
dp->actions_num = actions_num;
dp->actions = (struct ofl_action_header **)malloc(dp->actions_num * sizeof(struct ofp_action_header *));
// TODO Zoltan: Output actions can contain OFPP_TABLE
act = sp->actions;
for (i = 0; i < dp->actions_num; i++) {
error = ofl_actions_unpack(act, len, &(dp->actions[i]), exp);
if (error) {
OFL_UTILS_FREE_ARR_FUN2(dp->actions, i,
ofl_actions_free, exp);
free(dp);
}
act = (struct ofp_action_header *)((uint8_t *)act + ntohs(act->len));
}
data = ((uint8_t *)sp->actions) + ntohs(sp->actions_len);
dp->data_length = *len;
dp->data = *len > 0 ? (uint8_t *)memcpy(malloc(*len), data, *len) : NULL;
*len = 0;
*msg = (struct ofl_msg_header *)dp;
return 0;
}
static ofl_err
ofl_msg_unpack_flow_mod(struct ofp_header *src,uint8_t* buf, size_t *len, struct ofl_msg_header **msg, struct ofl_exp *exp) {
struct ofp_flow_mod *sm;
struct ofl_msg_flow_mod *dm;
struct ofp_instruction *inst;
ofl_err error;
size_t i;
int match_pos;
if (*len < (sizeof(struct ofp_flow_mod) - sizeof(struct ofp_match))) {
OFL_LOG_WARN(LOG_MODULE, "Received FLOW_MOD message has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
*len -= (sizeof(struct ofp_flow_mod) - sizeof(struct ofp_match));
sm = (struct ofp_flow_mod *)src;
dm = (struct ofl_msg_flow_mod *)malloc(sizeof(struct ofl_msg_flow_mod));
if (sm->table_id >= PIPELINE_TABLES && ((sm->command != OFPFC_DELETE
|| sm->command != OFPFC_DELETE_STRICT) && sm->table_id != OFPTT_ALL)) {
OFL_LOG_WARN(LOG_MODULE, "Received FLOW_MOD message has invalid table id (%d).", sm->table_id );
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_TABLE_ID);
}
dm->cookie = ntoh64(sm->cookie);
dm->cookie_mask = ntoh64(sm->cookie_mask);
dm->table_id = sm->table_id;
dm->command = (enum ofp_flow_mod_command)sm->command;
dm->idle_timeout = ntohs( sm->idle_timeout);
dm->hard_timeout = ntohs( sm->hard_timeout);
dm->priority = ntohs( sm->priority);
dm->buffer_id = ntohl( sm->buffer_id);
dm->out_port = ntohl( sm->out_port);
dm->out_group = ntohl( sm->out_group);
dm->flags = ntohs( sm->flags);
match_pos = sizeof(struct ofp_flow_mod) - 4;
error = ofl_structs_match_unpack(&(sm->match), buf + match_pos, len, &(dm->match), exp);
if (error) {
free(dm);
return error;
}
error = ofl_utils_count_ofp_instructions((struct ofp_instruction *)(buf + ROUND_UP(match_pos + dm->match->length,8)), *len, &dm->instructions_num);
if (error) {
ofl_structs_free_match(dm->match, exp);
free(dm);
return error;
}
dm->instructions = (struct ofl_instruction_header **)malloc(dm->instructions_num * sizeof(struct ofl_instruction_header *));
inst = (struct ofp_instruction *) (buf + ROUND_UP(match_pos + dm->match->length,8));
for (i = 0; i < dm->instructions_num; i++) {
error = ofl_structs_instructions_unpack(inst, len, &(dm->instructions[i]), exp);
if (error) {
OFL_UTILS_FREE_ARR_FUN2(dm->instructions, i,
ofl_structs_free_instruction, exp);
ofl_structs_free_match(dm->match, exp);
free(dm);
return error;
}
inst = (struct ofp_instruction *)((uint8_t *)inst + ntohs(inst->len));
}
*msg = (struct ofl_msg_header *)dm;
return 0;
}
static ofl_err
ofl_msg_unpack_group_mod(struct ofp_header *src, size_t *len, struct ofl_msg_header **msg, struct ofl_exp *exp) {
struct ofp_group_mod *sm;
struct ofl_msg_group_mod *dm;
struct ofp_bucket *bucket;
ofl_err error;
size_t i;
if (*len < sizeof(struct ofp_group_mod)) {
OFL_LOG_WARN(LOG_MODULE, "Received GROUP_MOD message has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
*len -= sizeof(struct ofp_group_mod);
sm = (struct ofp_group_mod *)src;
if (ntohs(sm->command) > OFPGC_DELETE) {
OFL_LOG_WARN(LOG_MODULE, "Received GROUP_MOD message with invalid command (%u).", ntohs(sm->command));
return ofl_error(OFPET_BAD_REQUEST, OFPGMFC_BAD_COMMAND);
}
if (ntohs(sm->type) > OFPGT_FF && ntohs(sm->type) < 128 /* experimenter */) {
OFL_LOG_WARN(LOG_MODULE, "Received GROUP_MOD message with invalid type (%u).", ntohs(sm->type));
return ofl_error(OFPET_BAD_REQUEST, OFPGMFC_BAD_TYPE);
}
if (ntohl(sm->group_id) > OFPG_MAX &&
!(ntohs(sm->command) == OFPGC_DELETE && ntohl(sm->group_id) == OFPG_ALL)) {
if (OFL_LOG_IS_WARN_ENABLED(LOG_MODULE)) {
char *gs = ofl_group_to_string(ntohl(sm->group_id));
OFL_LOG_WARN(LOG_MODULE, "Received GROUP_MOD message with invalid group id (%s).", gs);
free(gs);
}
return ofl_error(OFPET_GROUP_MOD_FAILED, OFPGMFC_INVALID_GROUP);
}
dm = (struct ofl_msg_group_mod *)malloc(sizeof(struct ofl_msg_group_mod));
dm->command = (enum ofp_group_mod_command)((int)ntohs(sm->command));
dm->type = sm->type;
dm->group_id = ntohl(sm->group_id);
error = ofl_utils_count_ofp_buckets(&(sm->buckets), *len, &dm->buckets_num);
if (error) {
free(dm);
return error;
}
if (dm->command == OFPGC_DELETE && dm->buckets_num > 0) {
OFL_LOG_WARN(LOG_MODULE, "Received DELETE group command with buckets (%zu).", dm->buckets_num);
free(dm);
return ofl_error(OFPET_GROUP_MOD_FAILED, OFPGMFC_INVALID_GROUP);
}
if (dm->type == OFPGT_INDIRECT && dm->buckets_num != 1) {
OFL_LOG_WARN(LOG_MODULE, "Received INDIRECT group doesn't have exactly one bucket (%zu).", dm->buckets_num);
free(dm);
return ofl_error(OFPET_GROUP_MOD_FAILED, OFPGMFC_INVALID_GROUP);
}
dm->buckets = (struct ofl_bucket **)malloc(dm->buckets_num * sizeof(struct ofl_bucket *));
bucket = sm->buckets;
for (i = 0; i < dm->buckets_num; i++) {
error = ofl_structs_bucket_unpack(bucket, len, dm->type, &(dm->buckets[i]), exp);
if (error) {
OFL_UTILS_FREE_ARR_FUN2(dm->buckets, i,
ofl_structs_free_bucket, exp);
free(dm);
return error;
}
bucket = (struct ofp_bucket *)((uint8_t *)bucket + ntohs(bucket->len));
}
*msg = (struct ofl_msg_header *)dm;
return 0;
}
static ofl_err
ofl_msg_unpack_meter_mod(struct ofp_header *src, size_t *len, struct ofl_msg_header **msg) {
struct ofp_meter_mod *sm;
struct ofl_msg_meter_mod *dm;
struct ofp_meter_band_header *band;
ofl_err error;
size_t i;
if (*len < sizeof(struct ofp_meter_mod)) {
OFL_LOG_WARN(LOG_MODULE, "Received METER_MOD message has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
*len -= sizeof(struct ofp_meter_mod);
sm = (struct ofp_meter_mod *)src;
if (ntohs(sm->command) > OFPMC_DELETE) {
OFL_LOG_WARN(LOG_MODULE, "Received METER_MOD message with invalid command (%u).", ntohs(sm->command));
return ofl_error(OFPET_BAD_REQUEST, OFPMMFC_BAD_COMMAND);
}
if ((ntohs(sm->flags) >> 3) > 1 ) {
OFL_LOG_WARN(LOG_MODULE, "Received METER_MOD message with invalid flags(%u).", ntohs(sm->flags));
return ofl_error(OFPET_BAD_REQUEST, OFPMMFC_BAD_FLAGS);
}
if (ntohl(sm->meter_id) > OFPM_MAX &&
!(ntohs(sm->command) == OFPMC_DELETE && ntohl(sm->meter_id) == OFPM_ALL)) {
return ofl_error(OFPET_METER_MOD_FAILED, OFPMMFC_INVALID_METER);
}
dm = (struct ofl_msg_meter_mod *)malloc(sizeof(struct ofl_msg_meter_mod));
dm->command = ntohs(sm->command);
dm->flags = ntohs(sm->flags);
dm->meter_id = ntohl(sm->meter_id);
error = ofl_utils_count_ofp_meter_bands(&(sm->bands), *len, &dm->meter_bands_num);
if (error) {
free(dm);
return error;
}
dm->bands = (struct ofl_meter_band_header **)malloc(dm->meter_bands_num * sizeof(struct ofl_meter_band_header *));
band = sm->bands;
for (i = 0; i < dm->meter_bands_num; i++) {
error = ofl_structs_meter_band_unpack(band, len, &(dm->bands[i]));
if (error) {
OFL_UTILS_FREE_ARR_FUN(dm->bands, i,
ofl_structs_free_meter_bands);
free(dm);
return error;
}
band = (struct ofp_meter_band_header *)((uint8_t *)band + ntohs(band->len));
}
*msg = (struct ofl_msg_header *)dm;
return 0;
}
static ofl_err
ofl_msg_unpack_port_mod(struct ofp_header *src, size_t *len, struct ofl_msg_header **msg) {
struct ofp_port_mod *sm;
struct ofl_msg_port_mod *dm;
if (*len < sizeof(struct ofp_port_mod)) {
OFL_LOG_WARN(LOG_MODULE, "Received PORT_MOD has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
sm = (struct ofp_port_mod *)src;
/*if (ntohl(sm->port_no) == 0 || ntohl(sm->port_no) > OFPP_MAX) {
if (OFL_LOG_IS_WARN_ENABLED(LOG_MODULE)) {
char *ps = ofl_port_to_string(ntohl(sm->port_no));
OFL_LOG_WARN(LOG_MODULE, "Received PORT_MOD message has invalid in_port (%s).", ps);
free(ps);
}
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_PORT);
}*/
*len -= sizeof(struct ofp_port_mod);
dm = (struct ofl_msg_port_mod *)malloc(sizeof(struct ofl_msg_port_mod));
dm->port_no = ntohl(sm->port_no);
memcpy(dm->hw_addr, sm->hw_addr, OFP_ETH_ALEN);
dm->config = ntohl(sm->config);
dm->mask = ntohl(sm->mask);
dm->advertise = ntohl(sm->advertise);
*msg = (struct ofl_msg_header *)dm;
return 0;
}
static ofl_err
ofl_msg_unpack_table_mod(struct ofp_header *src, size_t *len, struct ofl_msg_header **msg) {
struct ofp_table_mod *sm;
struct ofl_msg_table_mod *dm;
if (*len < sizeof(struct ofp_table_mod)) {
OFL_LOG_WARN(LOG_MODULE, "Received TABLE_MOD message has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
*len -= sizeof(struct ofp_table_mod);
sm = (struct ofp_table_mod *)src;
dm = (struct ofl_msg_table_mod *)malloc(sizeof(struct ofl_msg_table_mod));
if (sm->table_id >= PIPELINE_TABLES) {
OFL_LOG_WARN(LOG_MODULE, "Received TABLE_MOD message has invalid table id (%d).", sm->table_id );
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_TABLE_ID);
}
dm->table_id = sm->table_id;
dm->config = ntohl(sm->config);
*msg = (struct ofl_msg_header *)dm;
return 0;
}
static ofl_err
ofl_msg_unpack_multipart_request_flow(struct ofp_multipart_request *os, uint8_t* buf, size_t *len, struct ofl_msg_header **msg, struct ofl_exp *exp) {
struct ofp_flow_stats_request *sm;
struct ofl_msg_multipart_request_flow *dm;
ofl_err error = 0;
int match_pos;
// ofp_multipart_request length was checked at ofl_msg_unpack_multipart_request
if (*len < (sizeof(struct ofp_flow_stats_request) - sizeof(struct ofp_match))) {
OFL_LOG_WARN(LOG_MODULE, "Received FLOW stats request has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
*len -= (sizeof(struct ofp_flow_stats_request) - sizeof(struct ofp_match));
sm = (struct ofp_flow_stats_request *)os->body;
dm = (struct ofl_msg_multipart_request_flow *) malloc(sizeof(struct ofl_msg_multipart_request_flow));
if (sm->table_id != OFPTT_ALL && sm->table_id >= PIPELINE_TABLES) {
OFL_LOG_WARN(LOG_MODULE, "Received MULTIPART REQUEST FLOW message has invalid table id (%d).", sm->table_id );
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_TABLE_ID);
}
dm->table_id = sm->table_id;
dm->out_port = ntohl(sm->out_port);
dm->out_group = ntohl(sm->out_group);
dm->cookie = ntoh64(sm->cookie);
dm->cookie_mask = ntoh64(sm->cookie_mask);
match_pos = sizeof(struct ofp_multipart_request) + sizeof(struct ofp_flow_stats_request) - 4;
error = ofl_structs_match_unpack(&(sm->match),buf + match_pos, len, &(dm->match), exp);
if (error) {
free(dm);
return error;
}
*msg = (struct ofl_msg_header *)dm;
return 0;
}
static ofl_err
ofl_msg_unpack_multipart_request_port(struct ofp_multipart_request *os, size_t *len, struct ofl_msg_header **msg) {
struct ofp_port_stats_request *sm;
struct ofl_msg_multipart_request_port *dm;
// ofp_multipart_request length was checked at ofl_msg_unpack_multipart_request
if (*len < sizeof(struct ofp_port_stats_request)) {
OFL_LOG_WARN(LOG_MODULE, "Received PORT stats request has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
sm = (struct ofp_port_stats_request *)os->body;
if (ntohl(sm->port_no) == 0 ||
(ntohl(sm->port_no) > OFPP_MAX && ntohl(sm->port_no) != OFPP_ANY)) {
OFL_LOG_WARN(LOG_MODULE, "Received PORT stats request has invalid port (%u).", ntohl(sm->port_no));
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
*len -= sizeof(struct ofp_port_stats_request);
dm = (struct ofl_msg_multipart_request_port *) malloc(sizeof(struct ofl_msg_multipart_request_port));
dm->port_no = ntohl(sm->port_no);
*msg = (struct ofl_msg_header *)dm;
return 0;
}
static ofl_err
ofl_msg_unpack_multipart_request_empty(struct ofp_multipart_request *os UNUSED, size_t *len, struct ofl_msg_header **msg) {
// ofp_multipart_request length was checked at ofl_msg_unpack_multipart_request
len -= sizeof(struct ofp_multipart_request);
*msg = (struct ofl_msg_header *)malloc(sizeof(struct ofl_msg_multipart_request_header));
return 0;
}
static ofl_err
ofl_msg_unpack_multipart_request_table_features(struct ofp_multipart_request *os, size_t *len, struct ofl_msg_header **msg, struct ofl_exp *exp){
struct ofl_msg_multipart_request_table_features *dm;
ofl_err error;
uint8_t *features;
size_t i;
dm = (struct ofl_msg_multipart_request_table_features*) malloc(sizeof(struct ofl_msg_multipart_request_table_features));
if (!(*len)){
dm->tables_num = 0;
dm->table_features = NULL;
*msg = (struct ofl_msg_header*) dm;
return 0;
}
error = ofl_utils_count_ofp_table_features((uint8_t*) os->body, *len, &dm->tables_num);
if (error) {
free(dm);
return error;
}
dm->table_features = (struct ofl_table_features **) malloc(sizeof(struct ofl_table_features *) * dm->tables_num);
features = (uint8_t* ) os->body;
for(i = 0; i < dm->tables_num; i++){
error = ofl_structs_table_features_unpack((struct ofp_table_features*) features, len, &dm->table_features[i] , exp);
if (error) {
OFL_UTILS_FREE_ARR_FUN2(dm->table_features, i,
ofl_structs_free_table_features, exp);
free(dm);
return error;
}
features += ntohs(((struct ofp_table_features*) features)->length);
}
*msg = (struct ofl_msg_header *)dm;
return 0;
}
static ofl_err
ofl_msg_unpack_multipart_request_queue(struct ofp_multipart_request *os, size_t *len, struct ofl_msg_header **msg) {
struct ofp_queue_stats_request *sm;
struct ofl_msg_multipart_request_queue *dm;
// ofp_multipart_request length was checked at ofl_msg_unpack_multipart_request
if (*len < sizeof(struct ofp_queue_stats_request)) {
OFL_LOG_WARN(LOG_MODULE, "Received QUEUE stats request has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
sm = (struct ofp_queue_stats_request *)os->body;
if (ntohl(sm->port_no) == 0 ||
(ntohl(sm->port_no) > OFPP_MAX && ntohl(sm->port_no) != OFPP_ANY)) {
OFL_LOG_WARN(LOG_MODULE, "Received QUEUE stats request has invalid port (%u).", ntohl(sm->port_no));
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
*len -= sizeof(struct ofp_queue_stats_request);
dm = (struct ofl_msg_multipart_request_queue *) malloc(sizeof(struct ofl_msg_multipart_request_queue));
dm->port_no = ntohl(sm->port_no);
dm->queue_id = ntohl(sm->queue_id);
*msg = (struct ofl_msg_header *)dm;
return 0;
}
static ofl_err
ofl_msg_unpack_multipart_request_group(struct ofp_multipart_request *os, size_t *len, struct ofl_msg_header **msg) {
struct ofp_group_stats_request *sm;
struct ofl_msg_multipart_request_group *dm;
// ofp_multipart_request length was checked at ofl_msg_unpack_multipart_request
if (*len < sizeof(struct ofp_group_stats_request)) {
OFL_LOG_WARN(LOG_MODULE, "Received GROUP stats request has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
*len -= sizeof(struct ofp_group_stats_request);
sm = (struct ofp_group_stats_request *)os->body;
dm = (struct ofl_msg_multipart_request_group *) malloc(sizeof(struct ofl_msg_multipart_request_group));
dm->group_id = ntohl(sm->group_id);
*msg = (struct ofl_msg_header *)dm;
return 0;
}
static ofl_err
ofl_msg_unpack_meter_multipart_request(struct ofp_multipart_request *os, size_t *len, struct ofl_msg_header **msg) {
struct ofp_meter_multipart_request *sm;
struct ofl_msg_multipart_meter_request *dm;
// ofp_multipart_request length was checked at ofl_msg_unpack_multipart_request
if (*len < sizeof(struct ofp_meter_multipart_request)) {
OFL_LOG_WARN(LOG_MODULE, "Received METER multipart request has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
*len -= sizeof(struct ofp_meter_multipart_request);
sm = (struct ofp_meter_multipart_request *)os->body;
dm = (struct ofl_msg_multipart_meter_request *) malloc(sizeof(struct ofl_msg_multipart_meter_request));
dm->meter_id = ntohl(sm->meter_id);
*msg = (struct ofl_msg_header *)dm;
return 0;
}
static ofl_err
ofl_msg_unpack_multipart_request(struct ofp_header *src,uint8_t *buf, size_t *len, struct ofl_msg_header **msg, struct ofl_exp *exp) {
struct ofl_msg_multipart_request_header *ofls;
struct ofp_multipart_request *os;
int error;
if (*len < sizeof(struct ofp_multipart_request)) {
OFL_LOG_WARN(LOG_MODULE, "Received STATS_REQUEST message has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);
}
*len -= sizeof(struct ofp_multipart_request);
os = (struct ofp_multipart_request *)src;
switch (ntohs(os->type)) {
case OFPMP_DESC: {
error = ofl_msg_unpack_multipart_request_empty(os, len, msg);
break;
}
case OFPMP_FLOW:
case OFPMP_AGGREGATE: {
error = ofl_msg_unpack_multipart_request_flow(os,buf, len, msg, exp);
break;
}
case OFPMP_TABLE: {
error = ofl_msg_unpack_multipart_request_empty(os, len, msg);
break;
}
case OFPMP_TABLE_FEATURES:{
error = ofl_msg_unpack_multipart_request_table_features(os, len, msg, exp);
break;
}
case OFPMP_PORT_STATS: {
error = ofl_msg_unpack_multipart_request_port(os, len, msg);
break;
}
case OFPMP_QUEUE: {
error = ofl_msg_unpack_multipart_request_queue(os, len, msg);
break;
}
case OFPMP_GROUP: {
error = ofl_msg_unpack_multipart_request_group(os, len, msg);
break;
}
case OFPMP_GROUP_DESC: {
error = ofl_msg_unpack_multipart_request_empty(os, len, msg);
break;
}
case OFPMP_GROUP_FEATURES:{
error = ofl_msg_unpack_multipart_request_empty(os, len, msg);
break;
}
case OFPMP_METER:
case OFPMP_METER_CONFIG:{
error = ofl_msg_unpack_meter_multipart_request(os, len, msg);
break;
}
case OFPMP_METER_FEATURES:{
error = ofl_msg_unpack_multipart_request_empty(os, len, msg);
break;
}
case OFPMP_PORT_DESC: {
error = ofl_msg_unpack_multipart_request_empty(os, len, msg);
break;
}
case OFPMP_EXPERIMENTER: {
if (exp == NULL || exp->stats == NULL || exp->stats->reply_unpack == NULL) {
OFL_LOG_WARN(LOG_MODULE, "Received EXPERIMENTER stats request, but no callback was given.");
error = ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_MULTIPART);
} else {
error = exp->stats->req_unpack(os, len, (struct ofl_msg_multipart_request_header **)msg);
}
break;
}
default: {
error = ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_MULTIPART);
}
}
if (error) {
return error;
}
ofls = (struct ofl_msg_multipart_request_header *)(*msg);
ofls->type = (enum ofp_multipart_types)((int)ntohs(os->type));
ofls->flags = ntohs(os->flags);
return 0;
}
static ofl_err
ofl_msg_unpack_reply_desc(struct ofp_multipart_reply *os, size_t *len, struct ofl_msg_header **msg) {
struct ofp_desc *sm;
struct ofl_msg_reply_desc *dm;
if (*len < sizeof(struct ofp_desc)) {
OFL_LOG_WARN(LOG_MODULE, "Received DESC stats reply has invalid length (%zu).", *len);
return ofl_error(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN);