/
interface.c
1393 lines (1157 loc) · 40.9 KB
/
interface.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
/*
* DBus encapsulation for network interfaces
*
* Copyright (C) 2011-2012 Olaf Kirch <okir@suse.de>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <getopt.h>
#include <errno.h>
#include <wicked/netinfo.h>
#include <wicked/addrconf.h>
#include <wicked/logging.h>
#include <wicked/dbus-errors.h>
#include <wicked/dbus-service.h>
#include <wicked/system.h>
#include <wicked/xml.h>
#include "netinfo_priv.h"
#include "dbus-common.h"
#include "model.h"
#include "debug.h"
extern dbus_bool_t ni_objectmodel_netif_list_refresh(ni_dbus_object_t *);
static void ni_objectmodel_register_netif_factory_service(ni_dbus_service_t *);
static void ni_objectmodel_netif_initialize(ni_dbus_object_t *object);
static void ni_objectmodel_netif_destroy(ni_dbus_object_t *object);
const ni_dbus_class_t ni_objectmodel_netif_class = {
.name = NI_OBJECTMODEL_NETIF_CLASS,
.initialize = ni_objectmodel_netif_initialize,
.destroy = ni_objectmodel_netif_destroy,
};
static ni_dbus_class_t ni_objectmodel_ifreq_class = {
.name = NI_OBJECTMODEL_NETIF_REQUEST_CLASS,
};
static const ni_dbus_class_t ni_objectmodel_netif_list_class;
static ni_dbus_service_t ni_objectmodel_netif_list_service;
static ni_dbus_service_t ni_objectmodel_netif_service;
extern ni_dbus_service_t ni_objectmodel_addrconf_ipv4_static_service;
extern ni_dbus_service_t ni_objectmodel_addrconf_ipv6_static_service;
extern ni_dbus_service_t ni_objectmodel_addrconf_ipv4_dhcp_service;
extern ni_dbus_service_t ni_objectmodel_addrconf_ipv6_dhcp_service;
extern ni_dbus_service_t ni_objectmodel_addrconf_ipv4ll_service;
extern ni_dbus_service_t ni_objectmodel_wireless_service;
static ni_dbus_property_t ni_objectmodel_netif_request_properties[];
/*
* For all link layer types, create a dbus object class named "netif-$linktype".
* This allows to define extensions and interface for specific link layers.
*/
void
ni_objectmodel_register_netif_classes(void)
{
ni_dbus_class_t *link_class;
unsigned int iftype;
/* register the netif-list class (to allow extensions to attach to it) */
ni_objectmodel_register_class(&ni_objectmodel_netif_list_class);
/* register the netif class (to allow extensions to attach to it) */
ni_objectmodel_register_class(&ni_objectmodel_netif_class);
for (iftype = 0; iftype < __NI_IFTYPE_MAX; ++iftype) {
const char *classname;
if (!(classname = ni_objectmodel_link_classname(iftype)))
continue;
/* Create and register the new link class */
link_class = ni_objectmodel_class_new(classname, &ni_objectmodel_netif_class);
ni_objectmodel_register_class(link_class);
}
}
void
ni_objectmodel_register_netif_services(void)
{
ni_objectmodel_register_service(&ni_objectmodel_netif_service);
ni_objectmodel_register_service(&ni_objectmodel_netif_list_service);
/* register built-in protocol services */
ni_objectmodel_register_netif_service(NI_IFTYPE_UNKNOWN, &ni_objectmodel_ipv4_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_UNKNOWN, &ni_objectmodel_ipv6_service);
/* register our built-in addrconf services */
ni_objectmodel_register_netif_service(NI_IFTYPE_UNKNOWN, &ni_objectmodel_addrconf_ipv4_static_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_UNKNOWN, &ni_objectmodel_addrconf_ipv6_static_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_UNKNOWN, &ni_objectmodel_addrconf_ipv4_dhcp_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_UNKNOWN, &ni_objectmodel_addrconf_ipv6_dhcp_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_UNKNOWN, &ni_objectmodel_addrconf_ipv4ll_service);
/* LLDP agent */
ni_objectmodel_register_netif_service(NI_IFTYPE_UNKNOWN, &ni_objectmodel_lldp_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_ETHERNET, &ni_objectmodel_ethernet_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_VLAN, &ni_objectmodel_vlan_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_MACVLAN, &ni_objectmodel_macvlan_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_MACVTAP, &ni_objectmodel_macvtap_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_DUMMY, &ni_objectmodel_dummy_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_BOND, &ni_objectmodel_bond_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_BRIDGE, &ni_objectmodel_bridge_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_WIRELESS, &ni_objectmodel_wireless_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_TUN, &ni_objectmodel_tun_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_TAP, &ni_objectmodel_tap_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_SIT, &ni_objectmodel_sit_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_IPIP, &ni_objectmodel_ipip_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_GRE, &ni_objectmodel_gre_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_INFINIBAND, &ni_objectmodel_ibparent_service);
ni_objectmodel_register_netif_service(NI_IFTYPE_INFINIBAND_CHILD, &ni_objectmodel_ibchild_service);
ni_objectmodel_register_netif_factory_service(&ni_objectmodel_bond_factory_service);
ni_objectmodel_register_netif_factory_service(&ni_objectmodel_bridge_factory_service);
ni_objectmodel_register_netif_factory_service(&ni_objectmodel_vlan_factory_service);
ni_objectmodel_register_netif_factory_service(&ni_objectmodel_macvlan_factory_service);
ni_objectmodel_register_netif_factory_service(&ni_objectmodel_macvtap_factory_service);
ni_objectmodel_register_netif_factory_service(&ni_objectmodel_dummy_factory_service);
ni_objectmodel_register_netif_factory_service(&ni_objectmodel_tun_factory_service);
ni_objectmodel_register_netif_factory_service(&ni_objectmodel_tap_factory_service);
ni_objectmodel_register_netif_factory_service(&ni_objectmodel_ibchild_factory_service);
ni_objectmodel_register_netif_factory_service(&ni_objectmodel_sit_factory_service);
ni_objectmodel_register_netif_factory_service(&ni_objectmodel_ipip_factory_service);
ni_objectmodel_register_netif_factory_service(&ni_objectmodel_gre_factory_service);
/* Register all builtin naming services */
ni_objectmodel_register_ns_builtin();
}
static void
ni_objectmodel_register_netif_factory_service(ni_dbus_service_t *svc)
{
svc->compatible = &ni_objectmodel_netif_list_class;
ni_objectmodel_register_service(svc);
}
void
ni_objectmodel_register_netif_service(ni_iftype_t iftype, ni_dbus_service_t *svc)
{
svc->compatible = ni_objectmodel_link_class(iftype);
ni_assert(svc->compatible);
ni_objectmodel_register_service(svc);
}
/*
* netif list class
*/
void
ni_objectmodel_create_netif_list(ni_dbus_server_t *server)
{
ni_dbus_object_t *object;
/* Register the list of all interfaces */
object = ni_dbus_server_register_object(server,
NI_OBJECTMODEL_NETIF_LIST_PATH,
&ni_objectmodel_netif_list_class,
NULL);
if (object == NULL)
ni_fatal("Unable to create dbus object for interfaces");
ni_objectmodel_bind_compatible_interfaces(object);
}
static const ni_dbus_class_t ni_objectmodel_netif_list_class = {
.name = NI_OBJECTMODEL_NETIF_LIST_CLASS,
.list = {
.item_class = &ni_objectmodel_netif_class,
},
.refresh = ni_objectmodel_netif_list_refresh,
};
/*
* Refresh the netif list
* This function is called from the dbus object handling code prior
* to invoking any method of this object.
*
* GetManagedObject relies on this - without this, we'd never
* refresh the interface properties.
* Note that this still doesn't fix things when calling GetManagedObjects
* or GetAllProperties on a netif object directly; we haven't assigned
* refresh handlers to these.
*
* FIXME: remove this ruin and the dbus_class.list stuff with it
*/
dbus_bool_t
ni_objectmodel_netif_list_refresh(ni_dbus_object_t *object)
{
/* We're notified about automatically via RTM_NEW/DELLINK */
(void)object;
return TRUE;
}
/*
* General dbus object lookup
* FIXME: move this to model.c
*/
ni_dbus_object_t *
ni_objectmodel_resolve_name(ni_dbus_object_t *parent, const char *naming_service, const ni_dbus_variant_t *var)
{
ni_dbus_object_t *result = NULL;
ni_objectmodel_ns_t *ns;
const char *key, *value;
if (!(ns = ni_objectmodel_get_ns(naming_service))) {
ni_warn("unknown naming service \"%s\"", naming_service);
return NULL;
}
if (ni_dbus_variant_get_string(var, &value)) {
if (ns->lookup_by_name == NULL)
return NULL;
result = ns->lookup_by_name(ns, value);
} else {
/* Loop over all dict entries and append them to the var array */
ni_var_array_t attrs = NI_VAR_ARRAY_INIT;
const ni_dbus_variant_t *dict = var;
unsigned int i = 0;
while ((var = ni_dbus_dict_get_entry(dict, i++, &key)) != NULL) {
if (!ni_dbus_variant_get_string(var, &value))
goto done;
ni_var_array_set(&attrs, key, value);
}
result = ni_objectmodel_lookup_by_attrs(parent, ns, &attrs);
done:
ni_var_array_destroy(&attrs);
}
return result;
}
/*
* InterfaceList.identifyDevice
*/
static dbus_bool_t
ni_objectmodel_netif_list_device_by_name(ni_dbus_object_t *object, const ni_dbus_method_t *method,
unsigned int argc, const ni_dbus_variant_t *argv,
ni_dbus_message_t *reply, DBusError *error)
{
ni_netconfig_t *nc;
const char *name;
ni_netdev_t *dev;
if (argc != 1 || !ni_dbus_variant_get_string(&argv[0], &name))
return ni_dbus_error_invalid_args(error, object->path, method->name);
if (!(nc = ni_global_state_handle(0)) || !(dev = ni_netdev_by_name(nc, name))) {
dbus_set_error(error, NI_DBUS_ERROR_DEVICE_NOT_KNOWN,
"failed to identify interface %s", name);
return FALSE;
}
ni_dbus_message_append_string(reply, ni_objectmodel_netif_full_path(dev));
return TRUE;
}
/*
* InterfaceList.identifyDevice
*/
static dbus_bool_t
ni_objectmodel_netif_list_identify_device(ni_dbus_object_t *object, const ni_dbus_method_t *method,
unsigned int argc, const ni_dbus_variant_t *argv,
ni_dbus_message_t *reply, DBusError *error)
{
const char *namespace;
ni_dbus_object_t *found;
if (argc != 2
|| !ni_dbus_variant_get_string(&argv[0], &namespace)
|| (!ni_dbus_variant_is_dict(&argv[1]) && argv[1].type != DBUS_TYPE_STRING))
return ni_dbus_error_invalid_args(error, object->path, method->name);
found = ni_objectmodel_resolve_name(object, namespace, &argv[1]);
if (found == NULL) {
dbus_set_error(error, NI_DBUS_ERROR_DEVICE_NOT_KNOWN,
"unable to identify interface via %s", namespace);
return FALSE;
}
if (ni_objectmodel_unwrap_netif(found, NULL) == NULL) {
dbus_set_error(error, NI_DBUS_ERROR_DEVICE_NOT_KNOWN,
"failed to identify interface via %s - naming service returned "
"a %s object", namespace, found->class->name);
return FALSE;
}
ni_dbus_message_append_string(reply, found->path);
return TRUE;
}
static ni_dbus_method_t ni_objectmodel_netif_list_methods[] = {
{ "deviceByName", "s", ni_objectmodel_netif_list_device_by_name },
{ "identifyDevice", "sa{sv}", ni_objectmodel_netif_list_identify_device },
{ NULL }
};
static ni_dbus_service_t ni_objectmodel_netif_list_service = {
.name = NI_OBJECTMODEL_NETIFLIST_INTERFACE,
.compatible = &ni_objectmodel_netif_list_class,
.methods = ni_objectmodel_netif_list_methods,
};
/*
* For a given link type, return a canonical class name
*/
const char *
ni_objectmodel_link_classname(ni_iftype_t link_type)
{
const char *link_type_name;
static char namebuf[128];
if (link_type == NI_IFTYPE_UNKNOWN)
return NULL;
if (!(link_type_name = ni_linktype_type_to_name(link_type)))
return NULL;
snprintf(namebuf, sizeof(namebuf), "netif-%s", link_type_name);
return namebuf;
}
const ni_dbus_class_t *
ni_objectmodel_link_class(ni_iftype_t iftype)
{
const ni_dbus_class_t *class = NULL;
const char *classname;
if ((classname = ni_objectmodel_link_classname(iftype)) != NULL)
class = ni_objectmodel_get_class(classname);
if (class == NULL)
class = &ni_objectmodel_netif_class;
return class;
}
/*
* Build a dbus-object encapsulating a network device.
* If @server is non-NULL, register the object with a canonical object path
*/
static ni_dbus_object_t *
__ni_objectmodel_build_netif_object(ni_dbus_server_t *server, ni_netdev_t *dev, const ni_dbus_class_t *requested_class)
{
const ni_dbus_class_t *class;
ni_dbus_object_t *object;
class = ni_objectmodel_link_class(dev->link.type);
/* If the caller requests a specific class for this object, it must be a
* subclass of the link type class. */
if (requested_class) {
if (!ni_dbus_class_is_subclass(requested_class, class)) {
ni_warn("ignoring caller specified class %s for netdev %s (class %s)",
requested_class->name, dev->name, class->name);
} else {
class = requested_class;
}
}
if (server != NULL) {
object = ni_dbus_server_register_object(server,
ni_objectmodel_netif_path(dev),
class, ni_netdev_get(dev));
} else {
object = ni_dbus_object_new(class, NULL, ni_netdev_get(dev));
}
if (object == NULL) {
ni_error("Unable to create dbus object for network interface %s", dev->name);
return NULL;
}
ni_objectmodel_bind_compatible_interfaces(object);
return object;
}
/*
* Register a network interface with our dbus server,
* and add the appropriate dbus services
*/
ni_dbus_object_t *
ni_objectmodel_register_netif(ni_dbus_server_t *server, ni_netdev_t *ifp, const ni_dbus_class_t *override_class)
{
return __ni_objectmodel_build_netif_object(server, ifp, override_class);
}
/*
* Unregister a network interface from our dbus server.
*/
dbus_bool_t
ni_objectmodel_unregister_netif(ni_dbus_server_t *server, ni_netdev_t *ifp)
{
if (ni_dbus_server_unregister_object(server, ifp)) {
ni_debug_dbus("unregistered interface %s", ifp->name);
return 1;
}
return 0;
}
/*
* Return the canonical object path for an interface object
*/
const char *
ni_objectmodel_netif_path(const ni_netdev_t *ifp)
{
static char object_path[256];
snprintf(object_path, sizeof(object_path), "Interface/%u", ifp->link.ifindex);
return object_path;
}
const char *
ni_objectmodel_netif_full_path(const ni_netdev_t *ifp)
{
static char object_path[256];
snprintf(object_path, sizeof(object_path), NI_OBJECTMODEL_OBJECT_PATH "/Interface/%u", ifp->link.ifindex);
return object_path;
}
/*
* Common helper function to extract a network device argument from a properties dict.
* The attributes are specific to a given DBus interface.
*/
ni_netdev_t *
ni_objectmodel_get_netif_argument(const ni_dbus_variant_t *dict, ni_iftype_t iftype, const ni_dbus_service_t *service)
{
ni_dbus_object_t *dev_object;
ni_netdev_t *dev;
dbus_bool_t rv;
dev = ni_netdev_new(NULL, 0);
dev->link.type = iftype;
dev_object = ni_objectmodel_wrap_netif(dev);
rv = ni_dbus_object_set_properties_from_dict(dev_object, service, dict, NULL);
ni_dbus_object_free(dev_object);
if (!rv) {
ni_netdev_put(dev);
dev = NULL;
}
return dev;
}
/*
* Device factory functions need to register the newly created interface with the
* dbus service, and return the device's object path
*/
dbus_bool_t
ni_objectmodel_netif_factory_result(ni_dbus_server_t *server, ni_dbus_message_t *reply,
ni_netdev_t *dev, const ni_dbus_class_t *override_class,
DBusError *error)
{
ni_dbus_variant_t result = NI_DBUS_VARIANT_INIT;
ni_dbus_object_t *new_object;
dbus_bool_t rv;
new_object = ni_dbus_server_find_object_by_handle(server, dev);
if (new_object == NULL)
new_object = ni_objectmodel_register_netif(server, dev, override_class);
if (!new_object) {
dbus_set_error(error, DBUS_ERROR_FAILED,
"failed to register new device %s",
dev->name);
return FALSE;
}
/* For now, we return a string here. This should really be an object-path,
* though. */
ni_dbus_variant_set_string(&result, new_object->path);
rv = ni_dbus_message_serialize_variants(reply, 1, &result, error);
ni_dbus_variant_destroy(&result);
return rv;
}
/*
* Build a dummy dbus object encapsulating a network interface,
* and add the appropriate dbus services
*/
ni_dbus_object_t *
ni_objectmodel_wrap_netif(ni_netdev_t *ifp)
{
return __ni_objectmodel_build_netif_object(NULL, ifp, NULL);
}
ni_dbus_object_t *
ni_objectmodel_wrap_netif_request(ni_netdev_req_t *req)
{
return ni_dbus_object_new(&ni_objectmodel_ifreq_class, NULL, req);
}
ni_netdev_t *
ni_objectmodel_unwrap_netif(const ni_dbus_object_t *object, DBusError *error)
{
ni_netdev_t *dev;
if (!object) {
if (error)
dbus_set_error(error, DBUS_ERROR_FAILED,
"Cannot unwrap network interface from a NULL dbus object");
return NULL;
}
dev = object->handle;
if (ni_dbus_object_isa(object, &ni_objectmodel_netif_class))
return dev;
if (error)
dbus_set_error(error,
DBUS_ERROR_FAILED,
"method not compatible with object %s of class %s (not a network interface)",
object->path, object->class->name);
return NULL;
}
/*
* Given a network device, look up the server object encapsulating it
*/
ni_dbus_object_t *
ni_objectmodel_get_netif_object(ni_dbus_server_t *server, const ni_netdev_t *dev)
{
ni_dbus_object_t *object;
if (!dev)
return NULL;
if (!server && !(server = __ni_objectmodel_server))
return NULL;
object = ni_dbus_server_find_object_by_handle(server, dev);
if (object == NULL)
return NULL;
if (!ni_dbus_object_isa(object, &ni_objectmodel_netif_class)) {
ni_error("%s: netdev is encapsulated by a %s class object", __func__, object->class->name);
return NULL;
}
return object;
}
/*
* Helper functions to extract all properties from a dict argument
*/
static dbus_bool_t
get_properties_from_dict(const ni_dbus_service_t *service, void *handle, const ni_dbus_variant_t *dict, DBusError *error)
{
ni_dbus_object_t dummy;
memset(&dummy, 0, sizeof(dummy));
dummy.class = service->compatible;
dummy.handle = handle;
return ni_dbus_object_set_properties_from_dict(&dummy, service, dict, error);
}
static dbus_bool_t
put_properties_to_dict(const ni_dbus_service_t *service, const void *handle, ni_dbus_variant_t *dict, DBusError *error)
{
ni_dbus_object_t dummy;
memset(&dummy, 0, sizeof(dummy));
dummy.class = service->compatible;
dummy.handle = (void *) handle;
ni_dbus_variant_init_dict(dict);
return ni_dbus_object_get_properties_as_dict(&dummy, service, dict, error);
}
static ni_dbus_service_t ni_objectmodel_netifreq_service = {
.name = NI_OBJECTMODEL_NETIF_INTERFACE "Request",
.compatible = &ni_objectmodel_ifreq_class,
.properties = ni_objectmodel_netif_request_properties,
};
dbus_bool_t
ni_objectmodel_marshal_netdev_request(const ni_netdev_req_t *req, ni_dbus_variant_t *dict, DBusError *error)
{
return put_properties_to_dict(&ni_objectmodel_netifreq_service, req, dict, error);
}
dbus_bool_t
ni_objectmodel_unmarshal_netdev_request(ni_netdev_req_t *req, const ni_dbus_variant_t *dict, DBusError *error)
{
return get_properties_from_dict(&ni_objectmodel_netifreq_service, req, dict, error);
}
/*
* Interface.setMonitor(bool)
*
* Bring up the network interface, and assign the requested addresses.
* In the case of virtual interfaces like VLANs or bridges, the interface
* must have been created and configured prior to this call.
*
* The options dictionary contains interface properties.
*/
static dbus_bool_t
ni_objectmodel_netif_link_monitor(ni_dbus_object_t *object, const ni_dbus_method_t *method,
unsigned int argc, const ni_dbus_variant_t *argv,
ni_dbus_message_t *reply, DBusError *error)
{
ni_netdev_t *dev;
int rv;
if (!(dev = ni_objectmodel_unwrap_netif(object, error)))
return FALSE;
NI_TRACE_ENTER_ARGS("dev=%s", dev->name);
/* Create an interface_request object and extract configuration from dict */
if (argc != 0)
return ni_dbus_error_invalid_args(error, object->path, method->name);
if ((rv = ni_system_interface_link_monitor(dev)) < 0) {
ni_dbus_set_error_from_code(error, rv,
"failed to enable monitoring for interface %s",
dev->name);
return FALSE;
}
return TRUE;
}
/*
* Interface.getNames()
*
* Bring up the network interface, and assign the requested addresses.
* In the case of virtual interfaces like VLANs or bridges, the interface
* must have been created and configured prior to this call.
*
* The options dictionary contains interface properties.
*/
static dbus_bool_t
ni_objectmodel_netif_get_names(ni_dbus_object_t *object, const ni_dbus_method_t *method,
unsigned int argc, const ni_dbus_variant_t *argv,
ni_dbus_message_t *reply, DBusError *error)
{
ni_dbus_variant_t result = NI_DBUS_VARIANT_INIT;
ni_netdev_t *dev;
xml_node_t *names;
dbus_bool_t rv = FALSE;
if (!(dev = ni_objectmodel_unwrap_netif(object, error)))
return FALSE;
NI_TRACE_ENTER_ARGS("dev=%s", dev->name);
/* Create an interface_request object and extract configuration from dict */
if (argc != 0)
return ni_dbus_error_invalid_args(error, object->path, method->name);
names = ni_objectmodel_get_names(object);
ni_objectmodel_get_name_array(names, &result);
rv = ni_dbus_message_serialize_variants(reply, 1, &result, error);
ni_dbus_variant_destroy(&result);
/* Destroy the XML object last - the results dict will reference the
* attribute name strings while it's around */
if (names)
xml_node_free(names);
return rv;
}
/*
* Interface.clearEventFilters()
*/
static dbus_bool_t
ni_objectmodel_netif_clear_event_filters(ni_dbus_object_t *object, const ni_dbus_method_t *method,
unsigned int argc, const ni_dbus_variant_t *argv,
ni_dbus_message_t *reply, DBusError *error)
{
ni_netdev_t *dev;
if (!(dev = ni_objectmodel_unwrap_netif(object, error)))
return FALSE;
NI_TRACE_ENTER_ARGS("dev=%s", dev->name);
/* Create an interface_request object and extract configuration from dict */
if (argc != 0)
return ni_dbus_error_invalid_args(error, object->path, method->name);
ni_netdev_clear_event_filters(dev);
return TRUE;
}
/*
* Interface.linkUp(dict options)
*
* Bring up the network interface, and wait for link negotiation to complete.
* In the case of virtual interfaces like VLANs or bridges, the interface
* must have been created and configured prior to this call.
*
* The options dictionary contains interface properties.
*/
static dbus_bool_t
ni_objectmodel_netif_link_up(ni_dbus_object_t *object, const ni_dbus_method_t *method,
unsigned int argc, const ni_dbus_variant_t *argv,
ni_dbus_message_t *reply, DBusError *error)
{
ni_netconfig_t *nc = ni_global_state_handle(0);
ni_netdev_t *dev;
ni_netdev_req_t *req = NULL;
dbus_bool_t ret = FALSE;
int rv;
if (!(dev = ni_objectmodel_unwrap_netif(object, error)))
return FALSE;
NI_TRACE_ENTER_ARGS("dev=%s", dev->name);
/* Create an interface_request object and extract configuration from dict */
if (argc != 1)
return ni_dbus_error_invalid_args(error, object->path, method->name);
req = ni_netdev_req_new();
if (!ni_objectmodel_unmarshal_netdev_request(req, &argv[0], error))
goto failed;
if (req->mtu) {
if (dev->link.lowerdev.index) {
ni_netdev_t *lower;
lower = ni_netdev_by_index(nc, dev->link.lowerdev.index);
if (lower && req->mtu > lower->link.mtu) {
ni_info("Lowering requested %s mtu %u to lower device mtu %u",
dev->name, req->mtu, lower->link.mtu);
req->mtu = lower->link.mtu;
}
}
/*
* MTU change on device-up interfaces quite often causes either
* error -16 (busy) or sets all upper interfaces LOWERLAYERDOWN.
*/
if (ni_netdev_device_is_up(dev)) {
ni_debug_objectmodel("Skipping MTU change on %s: device is up",
dev->name);
req->mtu = 0;
}
if (req->mtu != dev->link.mtu &&
ni_system_mtu_change(nc, dev, req->mtu) < 0) {
ni_info("Unable to set %s MTU to %u", dev->name, req->mtu);
}
req->mtu = 0;
}
req->ifflags = NI_IFF_LINK_UP | NI_IFF_NETWORK_UP;
if ((rv = ni_system_interface_link_change(dev, req)) < 0) {
ni_dbus_set_error_from_code(error, rv,
"failed to configure interface %s", dev->name);
ret = FALSE;
goto failed;
}
ret = TRUE;
/* If the link is up, there's nothing to return */
if (!(dev->link.ifflags & NI_IFF_LINK_UP)) {
const ni_uuid_t *uuid;
/* Link is not up yet. Tell the caller to wait for an event. */
uuid = ni_netdev_add_event_filter(dev, (1 << NI_EVENT_LINK_UP) | (1 << NI_EVENT_LINK_DOWN));
ret = __ni_objectmodel_return_callback_info(reply, NI_EVENT_LINK_UP, uuid, error);
}
failed:
if (req)
ni_netdev_req_free(req);
return ret;
}
static dbus_bool_t
ni_objectmodel_netif_link_down(ni_dbus_object_t *object, const ni_dbus_method_t *method,
unsigned int argc, const ni_dbus_variant_t *argv,
ni_dbus_message_t *reply, DBusError *error)
{
ni_netdev_t *dev;
int rv;
if (!(dev = ni_objectmodel_unwrap_netif(object, error)))
return FALSE;
NI_TRACE_ENTER_ARGS("dev=%s", dev->name);
if ((rv = ni_system_interface_link_change(dev, NULL)) < 0) {
ni_dbus_set_error_from_code(error, rv,
"failed to shut down interface %s", dev->name);
return FALSE;
}
return TRUE;
}
/*
* Interface.installLease()
*
* This is used by network layers such as PPP or OpenVPN to inform wickedd about
* some intrinsic address configuration.
*
* The options dictionary contains address and route properties.
*/
static dbus_bool_t
ni_objectmodel_netif_install_lease(ni_dbus_object_t *object, const ni_dbus_method_t *method,
unsigned int argc, const ni_dbus_variant_t *argv,
ni_dbus_message_t *reply, DBusError *error)
{
ni_netdev_t *dev;
ni_addrconf_lease_t *lease;
dbus_bool_t ret = FALSE;
int rv;
if (!(dev = ni_objectmodel_unwrap_netif(object, error)))
return FALSE;
NI_TRACE_ENTER_ARGS("dev=%s", dev->name);
/* Create an interface_request object and extract configuration from dict */
if (argc != 1)
return ni_dbus_error_invalid_args(error, object->path, method->name);
lease = ni_addrconf_lease_new(NI_ADDRCONF_INTRINSIC, AF_INET);
if (!__ni_objectmodel_set_addrconf_lease(lease, &argv[0], error))
goto failed;
/*
* The following call updates the system with the information given in
* the lease. This includes setting all addresses, as well as updating
* resolver and hostname, if provided.
* When a lease is dropped, we either fall back to the config information
* from the next best lease, or if there is none, we restore the original
* system settings.
*
* Note, lease may be NULL after this, as the interface object
* takes ownership of it.
*/
rv = __ni_system_interface_update_lease(dev, &lease);
if (rv < 0) {
ni_dbus_set_error_from_code(error, rv,
"failed to install intrinsic lease on interface %s", dev->name);
goto failed;
}
ret = TRUE;
failed:
if (lease)
ni_addrconf_lease_free(lease);
return ret;
}
static void
__ni_objectmodel_netif_set_client_state_save_trigger(ni_netdev_t *dev)
{
if (dev && dev->client_state) {
ni_client_state_save(dev->client_state, dev->link.ifindex);
ni_debug_dbus("saving %s structure into a file for %s",
NI_CLIENT_STATE_XML_NODE, dev->name);
}
}
/*
* Interface.setClientControl()
*
* This is used by clients to record control flags
*/
static dbus_bool_t
ni_objectmodel_netif_set_client_state_control(ni_dbus_object_t *object, const ni_dbus_method_t *method,
unsigned int argc, const ni_dbus_variant_t *argv,
ni_dbus_message_t *reply, DBusError *error)
{
ni_netdev_t *dev;
ni_client_state_t *cs;
if (!(dev = ni_objectmodel_unwrap_netif(object, error)))
return FALSE;
if (argc != 1 || !ni_dbus_variant_is_dict(&argv[0]))
return ni_dbus_error_invalid_args(error, object->path, method->name);
cs = ni_netdev_get_client_state(dev);
if (!ni_objectmodel_netif_client_state_control_from_dict(&cs->control, &argv[0]))
return ni_dbus_error_invalid_args(error, object->path, method->name);
__ni_objectmodel_netif_set_client_state_save_trigger(dev);
return TRUE;
}
/*
* Interface.setClientConfig()
*
* This is used by clients to record configuration origin and UUID
*/
static dbus_bool_t
ni_objectmodel_netif_set_client_state_config(ni_dbus_object_t *object, const ni_dbus_method_t *method,
unsigned int argc, const ni_dbus_variant_t *argv,
ni_dbus_message_t *reply, DBusError *error)
{
ni_netdev_t *dev;
ni_client_state_t *cs;
if (!(dev = ni_objectmodel_unwrap_netif(object, error)))
return FALSE;
if (argc != 1 || !ni_dbus_variant_is_dict(&argv[0]))
return ni_dbus_error_invalid_args(error, object->path, method->name);
cs = ni_netdev_get_client_state(dev);
if (!ni_objectmodel_netif_client_state_config_from_dict(&cs->config, &argv[0]))
return ni_dbus_error_invalid_args(error, object->path, method->name);
__ni_objectmodel_netif_set_client_state_save_trigger(dev);
return TRUE;
}
/*
* Broadcast an interface event
* The optional uuid argument helps the client match e.g. notifications
* from an addrconf service against its current state.
*/
dbus_bool_t
ni_objectmodel_send_netif_event(ni_dbus_server_t *server, ni_dbus_object_t *object,
ni_event_t ifevent, const ni_uuid_t *uuid)
{
if (ifevent >= __NI_EVENT_MAX)
return FALSE;
if (!server && !(server = __ni_objectmodel_server)) {
ni_error("%s: help! No dbus server handle! Cannot send signal.", __func__);
return FALSE;
}
return __ni_objectmodel_device_event(server, object, NI_OBJECTMODEL_NETIF_INTERFACE, ifevent, uuid);
}
dbus_bool_t
__ni_objectmodel_device_event(ni_dbus_server_t *server, ni_dbus_object_t *object,
const char *interface, ni_event_t ifevent, const ni_uuid_t *uuid)
{
ni_dbus_variant_t arg = NI_DBUS_VARIANT_INIT;
const char *signal_name = NULL;
unsigned int argc = 0;
if (!(signal_name = ni_objectmodel_event_to_signal(ifevent))) {
ni_warn("%s: no signal name for event %u", __func__, ifevent);
return FALSE;
}
if (!server
&& !(server = ni_dbus_object_get_server(object))
&& !(server = __ni_objectmodel_server)) {
ni_error("%s: help! No dbus server handle! Cannot send signal.", __func__);
return FALSE;
}
if (uuid) {
ni_dbus_variant_set_uuid(&arg, uuid);
argc++;
}
ni_debug_dbus("sending device event \"%s\" for %s", signal_name, ni_dbus_object_get_path(object));
ni_dbus_server_send_signal(server, object, interface, signal_name, argc, &arg);
ni_dbus_variant_destroy(&arg);
return TRUE;
}
static ni_intmap_t __ni_objectmodel_event_names[] = {
{ "deviceCreate", NI_EVENT_DEVICE_CREATE },
{ "deviceDelete", NI_EVENT_DEVICE_DELETE },
{ "deviceChange", NI_EVENT_DEVICE_CHANGE },
{ "deviceReady", NI_EVENT_DEVICE_READY },
{ "deviceUp", NI_EVENT_DEVICE_UP },
{ "deviceDown", NI_EVENT_DEVICE_DOWN },
{ "linkAssociated", NI_EVENT_LINK_ASSOCIATED },
{ "linkAssociationLost", NI_EVENT_LINK_ASSOCIATION_LOST },
{ "linkScanUpdated", NI_EVENT_LINK_SCAN_UPDATED },
{ "linkUp", NI_EVENT_LINK_UP },
{ "linkDown", NI_EVENT_LINK_DOWN },
{ "networkUp", NI_EVENT_NETWORK_UP },
{ "networkDown", NI_EVENT_NETWORK_DOWN },
{ "addressAcquired", NI_EVENT_ADDRESS_ACQUIRED },
{ "addressReleased", NI_EVENT_ADDRESS_RELEASED },
{ "addressDeferred", NI_EVENT_ADDRESS_DEFERRED },
{ "addressLost", NI_EVENT_ADDRESS_LOST },
{ "resolverUpdated", NI_EVENT_RESOLVER_UPDATED },
{ "hostnameUpdated", NI_EVENT_HOSTNAME_UPDATED },