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
|
/*
* lib/route/addr.c Addresses
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
* Copyright (c) 2003-2006 Baruch Even <baruch@ev-en.org>,
* Mediatrix Telecom, inc. <ericb@mediatrix.com>
*/
/**
* @ingroup rtnl
* @defgroup rtaddr Addresses
* @brief
*
* @note The maximum size of an address label is IFNAMSIZ.
*
* @note The address may not contain a prefix length if the peer address
* has been specified already.
*
* @par 1) Address Addition
* @code
* // Allocate an empty address object to be filled out with the attributes
* // of the new address.
* struct rtnl_addr *addr = rtnl_addr_alloc();
*
* // Fill out the mandatory attributes of the new address. Setting the
* // local address will automatically set the address family and the
* // prefix length to the correct values.
* rtnl_addr_set_ifindex(addr, ifindex);
* rtnl_addr_set_local(addr, local_addr);
*
* // The label of the address can be specified, currently only supported
* // by IPv4 and DECnet.
* rtnl_addr_set_label(addr, "mylabel");
*
* // The peer address can be specified if necessary, in either case a peer
* // address will be sent to the kernel in order to fullfil the interface
* // requirements. If none is set, it will equal the local address.
* // Note: Real peer addresses are only supported by IPv4 for now.
* rtnl_addr_set_peer(addr, peer_addr);
*
* // In case you want to have the address have a scope other than global
* // it may be overwritten using rtnl_addr_set_scope(). The scope currently
* // cannot be set for IPv6 addresses.
* rtnl_addr_set_scope(addr, rtnl_str2scope("site"));
*
* // Broadcast address may be specified using the relevant
* // functions, the address family will be verified if one of the other
* // addresses has been set already. Currently only works for IPv4.
* rtnl_addr_set_broadcast(addr, broadcast_addr);
*
* // Build the netlink message and send it to the kernel, the operation will
* // block until the operation has been completed. Alternatively the required
* // netlink message can be built using rtnl_addr_build_add_request() to be
* // sent out using nl_send_auto_complete().
* rtnl_addr_add(sk, addr, 0);
*
* // Free the memory
* rtnl_addr_put(addr);
* @endcode
*
* @par 2) Address Deletion
* @code
* // Allocate an empty address object to be filled out with the attributes
* // matching the address to be deleted. Alternatively a fully equipped
* // address object out of a cache can be used instead.
* struct rtnl_addr *addr = rtnl_addr_alloc();
*
* // The only mandatory parameter besides the address family is the interface
* // index the address is on, i.e. leaving out all other parameters will
* // result in all addresses of the specified address family interface tuple
* // to be deleted.
* rtnl_addr_set_ifindex(addr, ifindex);
*
* // Specyfing the address family manually is only required if neither the
* // local nor peer address have been specified.
* rtnl_addr_set_family(addr, AF_INET);
*
* // Specyfing the local address is optional but the best choice to delete
* // specific addresses.
* rtnl_addr_set_local(addr, local_addr);
*
* // The label of the address can be specified, currently only supported
* // by IPv4 and DECnet.
* rtnl_addr_set_label(addr, "mylabel");
*
* // The peer address can be specified if necessary, in either case a peer
* // address will be sent to the kernel in order to fullfil the interface
* // requirements. If none is set, it will equal the local address.
* // Note: Real peer addresses are only supported by IPv4 for now.
* rtnl_addr_set_peer(addr, peer_addr);
*
* // Build the netlink message and send it to the kernel, the operation will
* // block until the operation has been completed. Alternatively the required
* // netlink message can be built using rtnl_addr_build_delete_request()
* // to be sent out using nl_send_auto_complete().
* rtnl_addr_delete(sk, addr, 0);
*
* // Free the memory
* rtnl_addr_put(addr);
* @endcode
* @{
*/
#include <netlink-local.h>
#include <netlink/netlink.h>
#include <netlink/route/rtnl.h>
#include <netlink/route/addr.h>
#include <netlink/route/route.h>
#include <netlink/route/link.h>
#include <netlink/utils.h>
/** @cond SKIP */
#define ADDR_ATTR_FAMILY 0x0001
#define ADDR_ATTR_PREFIXLEN 0x0002
#define ADDR_ATTR_FLAGS 0x0004
#define ADDR_ATTR_SCOPE 0x0008
#define ADDR_ATTR_IFINDEX 0x0010
#define ADDR_ATTR_LABEL 0x0020
#define ADDR_ATTR_CACHEINFO 0x0040
#define ADDR_ATTR_PEER 0x0080
#define ADDR_ATTR_LOCAL 0x0100
#define ADDR_ATTR_BROADCAST 0x0200
#define ADDR_ATTR_MULTICAST 0x0400
#define ADDR_ATTR_ANYCAST 0x0800
static struct nl_cache_ops rtnl_addr_ops;
static struct nl_object_ops addr_obj_ops;
/** @endcond */
static void addr_constructor(struct nl_object *obj)
{
struct rtnl_addr *addr = nl_object_priv(obj);
addr->a_scope = RT_SCOPE_NOWHERE;
}
static void addr_free_data(struct nl_object *obj)
{
struct rtnl_addr *addr = nl_object_priv(obj);
if (!addr)
return;
nl_addr_put(addr->a_peer);
nl_addr_put(addr->a_local);
nl_addr_put(addr->a_bcast);
nl_addr_put(addr->a_multicast);
nl_addr_put(addr->a_anycast);
}
static int addr_clone(struct nl_object *_dst, struct nl_object *_src)
{
struct rtnl_addr *dst = nl_object_priv(_dst);
struct rtnl_addr *src = nl_object_priv(_src);
if (src->a_peer)
if (!(dst->a_peer = nl_addr_clone(src->a_peer)))
return -NLE_NOMEM;
if (src->a_local)
if (!(dst->a_local = nl_addr_clone(src->a_local)))
return -NLE_NOMEM;
if (src->a_bcast)
if (!(dst->a_bcast = nl_addr_clone(src->a_bcast)))
return -NLE_NOMEM;
if (src->a_multicast)
if (!(dst->a_multicast = nl_addr_clone(src->a_multicast)))
return -NLE_NOMEM;
if (src->a_anycast)
if (!(dst->a_anycast = nl_addr_clone(src->a_anycast)))
return -NLE_NOMEM;
return 0;
}
static struct nla_policy addr_policy[IFA_MAX+1] = {
[IFA_LABEL] = { .type = NLA_STRING,
.maxlen = IFNAMSIZ },
[IFA_CACHEINFO] = { .minlen = sizeof(struct ifa_cacheinfo) },
};
static int addr_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
struct nlmsghdr *nlh, struct nl_parser_param *pp)
{
struct rtnl_addr *addr;
struct ifaddrmsg *ifa;
struct nlattr *tb[IFA_MAX+1];
int err, peer_prefix = 0, family;
addr = rtnl_addr_alloc();
if (!addr)
return -NLE_NOMEM;
addr->ce_msgtype = nlh->nlmsg_type;
err = nlmsg_parse(nlh, sizeof(*ifa), tb, IFA_MAX, addr_policy);
if (err < 0)
goto errout;
ifa = nlmsg_data(nlh);
addr->a_family = family = ifa->ifa_family;
addr->a_prefixlen = ifa->ifa_prefixlen;
addr->a_flags = ifa->ifa_flags;
addr->a_scope = ifa->ifa_scope;
addr->a_ifindex = ifa->ifa_index;
addr->ce_mask = (ADDR_ATTR_FAMILY | ADDR_ATTR_PREFIXLEN |
ADDR_ATTR_FLAGS | ADDR_ATTR_SCOPE | ADDR_ATTR_IFINDEX);
if (tb[IFA_LABEL]) {
nla_strlcpy(addr->a_label, tb[IFA_LABEL], IFNAMSIZ);
addr->ce_mask |= ADDR_ATTR_LABEL;
}
if (tb[IFA_CACHEINFO]) {
struct ifa_cacheinfo *ca;
ca = nla_data(tb[IFA_CACHEINFO]);
addr->a_cacheinfo.aci_prefered = ca->ifa_prefered;
addr->a_cacheinfo.aci_valid = ca->ifa_valid;
addr->a_cacheinfo.aci_cstamp = ca->cstamp;
addr->a_cacheinfo.aci_tstamp = ca->tstamp;
addr->ce_mask |= ADDR_ATTR_CACHEINFO;
}
if (tb[IFA_LOCAL]) {
addr->a_local = nl_addr_alloc_attr(tb[IFA_LOCAL], family);
if (!addr->a_local)
goto errout_nomem;
addr->ce_mask |= ADDR_ATTR_LOCAL;
}
if (tb[IFA_ADDRESS]) {
struct nl_addr *a;
a = nl_addr_alloc_attr(tb[IFA_ADDRESS], family);
if (!a)
goto errout_nomem;
/* IPv6 sends the local address as IFA_ADDRESS with
* no IFA_LOCAL, IPv4 sends both IFA_LOCAL and IFA_ADDRESS
* with IFA_ADDRESS being the peer address if they differ */
if (!tb[IFA_LOCAL] || !nl_addr_cmp(a, addr->a_local)) {
nl_addr_put(addr->a_local);
addr->a_local = a;
addr->ce_mask |= ADDR_ATTR_LOCAL;
} else {
addr->a_peer = a;
addr->ce_mask |= ADDR_ATTR_PEER;
peer_prefix = 1;
}
}
nl_addr_set_prefixlen(peer_prefix ? addr->a_peer : addr->a_local,
addr->a_prefixlen);
if (tb[IFA_BROADCAST]) {
addr->a_bcast = nl_addr_alloc_attr(tb[IFA_BROADCAST], family);
if (!addr->a_bcast)
goto errout_nomem;
addr->ce_mask |= ADDR_ATTR_BROADCAST;
}
if (tb[IFA_MULTICAST]) {
addr->a_multicast = nl_addr_alloc_attr(tb[IFA_MULTICAST],
family);
if (!addr->a_multicast)
goto errout_nomem;
addr->ce_mask |= ADDR_ATTR_MULTICAST;
}
if (tb[IFA_ANYCAST]) {
addr->a_anycast = nl_addr_alloc_attr(tb[IFA_ANYCAST],
family);
if (!addr->a_anycast)
goto errout_nomem;
addr->ce_mask |= ADDR_ATTR_ANYCAST;
}
err = pp->pp_cb((struct nl_object *) addr, pp);
errout:
rtnl_addr_put(addr);
return err;
errout_nomem:
err = -NLE_NOMEM;
goto errout;
}
static int addr_request_update(struct nl_cache *cache, struct nl_sock *sk)
{
return nl_rtgen_request(sk, RTM_GETADDR, AF_UNSPEC, NLM_F_DUMP);
}
static void addr_dump_line(struct nl_object *obj, struct nl_dump_params *p)
{
struct rtnl_addr *addr = (struct rtnl_addr *) obj;
struct nl_cache *link_cache;
char buf[128];
link_cache = nl_cache_mngt_require("route/link");
if (addr->ce_mask & ADDR_ATTR_LOCAL)
nl_dump_line(p, "%s",
nl_addr2str(addr->a_local, buf, sizeof(buf)));
else
nl_dump_line(p, "none");
if (addr->ce_mask & ADDR_ATTR_PEER)
nl_dump(p, " peer %s",
nl_addr2str(addr->a_peer, buf, sizeof(buf)));
nl_dump(p, " %s ", nl_af2str(addr->a_family, buf, sizeof(buf)));
if (link_cache)
nl_dump(p, "dev %s ",
rtnl_link_i2name(link_cache, addr->a_ifindex,
buf, sizeof(buf)));
else
nl_dump(p, "dev %d ", addr->a_ifindex);
nl_dump(p, "scope %s",
rtnl_scope2str(addr->a_scope, buf, sizeof(buf)));
rtnl_addr_flags2str(addr->a_flags, buf, sizeof(buf));
if (buf[0])
nl_dump(p, " <%s>", buf);
nl_dump(p, "\n");
}
static void addr_dump_details(struct nl_object *obj, struct nl_dump_params *p)
{
struct rtnl_addr *addr = (struct rtnl_addr *) obj;
char buf[128];
addr_dump_line(obj, p);
if (addr->ce_mask & (ADDR_ATTR_LABEL | ADDR_ATTR_BROADCAST |
ADDR_ATTR_MULTICAST)) {
nl_dump_line(p, " ");
if (addr->ce_mask & ADDR_ATTR_LABEL)
nl_dump(p, " label %s", addr->a_label);
if (addr->ce_mask & ADDR_ATTR_BROADCAST)
nl_dump(p, " broadcast %s",
nl_addr2str(addr->a_bcast, buf, sizeof(buf)));
if (addr->ce_mask & ADDR_ATTR_MULTICAST)
nl_dump(p, " multicast %s",
nl_addr2str(addr->a_multicast, buf,
sizeof(buf)));
if (addr->ce_mask & ADDR_ATTR_ANYCAST)
nl_dump(p, " anycast %s",
nl_addr2str(addr->a_anycast, buf,
sizeof(buf)));
nl_dump(p, "\n");
}
if (addr->ce_mask & ADDR_ATTR_CACHEINFO) {
struct rtnl_addr_cacheinfo *ci = &addr->a_cacheinfo;
nl_dump_line(p, " valid-lifetime %s",
ci->aci_valid == 0xFFFFFFFFU ? "forever" :
nl_msec2str(ci->aci_valid * 1000,
buf, sizeof(buf)));
nl_dump(p, " preferred-lifetime %s\n",
ci->aci_prefered == 0xFFFFFFFFU ? "forever" :
nl_msec2str(ci->aci_prefered * 1000,
buf, sizeof(buf)));
nl_dump_line(p, " created boot-time+%s ",
nl_msec2str(addr->a_cacheinfo.aci_cstamp * 10,
buf, sizeof(buf)));
nl_dump(p, "last-updated boot-time+%s\n",
nl_msec2str(addr->a_cacheinfo.aci_tstamp * 10,
buf, sizeof(buf)));
}
}
static void addr_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
{
addr_dump_details(obj, p);
}
static int addr_compare(struct nl_object *_a, struct nl_object *_b,
uint32_t attrs, int flags)
{
struct rtnl_addr *a = (struct rtnl_addr *) _a;
struct rtnl_addr *b = (struct rtnl_addr *) _b;
int diff = 0;
#define ADDR_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ADDR_ATTR_##ATTR, a, b, EXPR)
diff |= ADDR_DIFF(IFINDEX, a->a_ifindex != b->a_ifindex);
diff |= ADDR_DIFF(FAMILY, a->a_family != b->a_family);
diff |= ADDR_DIFF(SCOPE, a->a_scope != b->a_scope);
diff |= ADDR_DIFF(LABEL, strcmp(a->a_label, b->a_label));
diff |= ADDR_DIFF(PEER, nl_addr_cmp(a->a_peer, b->a_peer));
diff |= ADDR_DIFF(LOCAL, nl_addr_cmp(a->a_local, b->a_local));
diff |= ADDR_DIFF(MULTICAST, nl_addr_cmp(a->a_multicast,
b->a_multicast));
diff |= ADDR_DIFF(BROADCAST, nl_addr_cmp(a->a_bcast, b->a_bcast));
diff |= ADDR_DIFF(ANYCAST, nl_addr_cmp(a->a_anycast, b->a_anycast));
if (flags & LOOSE_COMPARISON)
diff |= ADDR_DIFF(FLAGS,
(a->a_flags ^ b->a_flags) & b->a_flag_mask);
else
diff |= ADDR_DIFF(FLAGS, a->a_flags != b->a_flags);
#undef ADDR_DIFF
return diff;
}
static const struct trans_tbl addr_attrs[] = {
__ADD(ADDR_ATTR_FAMILY, family)
__ADD(ADDR_ATTR_PREFIXLEN, prefixlen)
__ADD(ADDR_ATTR_FLAGS, flags)
__ADD(ADDR_ATTR_SCOPE, scope)
__ADD(ADDR_ATTR_IFINDEX, ifindex)
__ADD(ADDR_ATTR_LABEL, label)
__ADD(ADDR_ATTR_CACHEINFO, cacheinfo)
__ADD(ADDR_ATTR_PEER, peer)
__ADD(ADDR_ATTR_LOCAL, local)
__ADD(ADDR_ATTR_BROADCAST, broadcast)
__ADD(ADDR_ATTR_MULTICAST, multicast)
};
static char *addr_attrs2str(int attrs, char *buf, size_t len)
{
return __flags2str(attrs, buf, len, addr_attrs,
ARRAY_SIZE(addr_attrs));
}
/**
* @name Allocation/Freeing
* @{
*/
struct rtnl_addr *rtnl_addr_alloc(void)
{
return (struct rtnl_addr *) nl_object_alloc(&addr_obj_ops);
}
void rtnl_addr_put(struct rtnl_addr *addr)
{
nl_object_put((struct nl_object *) addr);
}
/** @} */
/**
* @name Cache Management
* @{
*/
int rtnl_addr_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
{
return nl_cache_alloc_and_fill(&rtnl_addr_ops, sk, result);
}
/** @} */
static int build_addr_msg(struct rtnl_addr *tmpl, int cmd, int flags,
struct nl_msg **result)
{
struct nl_msg *msg;
struct ifaddrmsg am = {
.ifa_family = tmpl->a_family,
.ifa_index = tmpl->a_ifindex,
.ifa_prefixlen = tmpl->a_prefixlen,
};
if (tmpl->ce_mask & ADDR_ATTR_SCOPE)
am.ifa_scope = tmpl->a_scope;
else {
/* compatibility hack */
if (tmpl->a_family == AF_INET &&
tmpl->ce_mask & ADDR_ATTR_LOCAL &&
*((char *) nl_addr_get_binary_addr(tmpl->a_local)) == 127)
am.ifa_scope = RT_SCOPE_HOST;
else
am.ifa_scope = RT_SCOPE_UNIVERSE;
}
msg = nlmsg_alloc_simple(cmd, flags);
if (!msg)
return -NLE_NOMEM;
if (nlmsg_append(msg, &am, sizeof(am), NLMSG_ALIGNTO) < 0)
goto nla_put_failure;
if (tmpl->ce_mask & ADDR_ATTR_LOCAL)
NLA_PUT_ADDR(msg, IFA_LOCAL, tmpl->a_local);
if (tmpl->ce_mask & ADDR_ATTR_PEER)
NLA_PUT_ADDR(msg, IFA_ADDRESS, tmpl->a_peer);
else if (tmpl->ce_mask & ADDR_ATTR_LOCAL)
NLA_PUT_ADDR(msg, IFA_ADDRESS, tmpl->a_local);
if (tmpl->ce_mask & ADDR_ATTR_LABEL)
NLA_PUT_STRING(msg, IFA_LABEL, tmpl->a_label);
if (tmpl->ce_mask & ADDR_ATTR_BROADCAST)
NLA_PUT_ADDR(msg, IFA_BROADCAST, tmpl->a_bcast);
if (tmpl->ce_mask & ADDR_ATTR_CACHEINFO) {
struct ifa_cacheinfo ca = {
.ifa_valid = tmpl->a_cacheinfo.aci_valid,
.ifa_prefered = tmpl->a_cacheinfo.aci_prefered,
};
NLA_PUT(msg, IFA_CACHEINFO, sizeof(ca), &ca);
}
*result = msg;
return 0;
nla_put_failure:
nlmsg_free(msg);
return -NLE_MSGSIZE;
}
/**
* @name Addition
* @{
*/
/**
* Build netlink request message to request addition of new address
* @arg addr Address object representing the new address.
* @arg flags Additional netlink message flags.
* @arg result Pointer to store resulting message.
*
* Builds a new netlink message requesting the addition of a new
* address. The netlink message header isn't fully equipped with
* all relevant fields and must thus be sent out via nl_send_auto_complete()
* or supplemented as needed.
*
* Minimal required attributes:
* - interface index (rtnl_addr_set_ifindex())
* - local address (rtnl_addr_set_local())
*
* The scope will default to universe except for loopback addresses in
* which case a host scope is used if not specified otherwise.
*
* @note Free the memory after usage using nlmsg_free().
*
* @return 0 on success or a negative error code.
*/
int rtnl_addr_build_add_request(struct rtnl_addr *addr, int flags,
struct nl_msg **result)
{
int required = ADDR_ATTR_IFINDEX | ADDR_ATTR_FAMILY |
ADDR_ATTR_PREFIXLEN | ADDR_ATTR_LOCAL;
if ((addr->ce_mask & required) != required)
return -NLE_MISSING_ATTR;
return build_addr_msg(addr, RTM_NEWADDR, NLM_F_CREATE | flags, result);
}
/**
* Request addition of new address
* @arg sk Netlink socket.
* @arg addr Address object representing the new address.
* @arg flags Additional netlink message flags.
*
* Builds a netlink message by calling rtnl_addr_build_add_request(),
* sends the request to the kernel and waits for the next ACK to be
* received and thus blocks until the request has been fullfilled.
*
* @see rtnl_addr_build_add_request()
*
* @return 0 on sucess or a negative error if an error occured.
*/
int rtnl_addr_add(struct nl_sock *sk, struct rtnl_addr *addr, int flags)
{
struct nl_msg *msg;
int err;
if ((err = rtnl_addr_build_add_request(addr, flags, &msg)) < 0)
return err;
err = nl_send_auto_complete(sk, msg);
nlmsg_free(msg);
if (err < 0)
return err;
return wait_for_ack(sk);
}
/** @} */
/**
* @name Deletion
* @{
*/
/**
* Build a netlink request message to request deletion of an address
* @arg addr Address object to be deleteted.
* @arg flags Additional netlink message flags.
* @arg result Pointer to store resulting message.
*
* Builds a new netlink message requesting a deletion of an address.
* The netlink message header isn't fully equipped with all relevant
* fields and must thus be sent out via nl_send_auto_complete()
* or supplemented as needed.
*
* Minimal required attributes:
* - interface index (rtnl_addr_set_ifindex())
* - address family (rtnl_addr_set_family())
*
* Optional attributes:
* - local address (rtnl_addr_set_local())
* - label (rtnl_addr_set_label(), IPv4/DECnet only)
* - peer address (rtnl_addr_set_peer(), IPv4 only)
*
* @note Free the memory after usage using nlmsg_free().
*
* @return 0 on success or a negative error code.
*/
int rtnl_addr_build_delete_request(struct rtnl_addr *addr, int flags,
struct nl_msg **result)
{
int required = ADDR_ATTR_IFINDEX | ADDR_ATTR_FAMILY;
if ((addr->ce_mask & required) != required)
return -NLE_MISSING_ATTR;
return build_addr_msg(addr, RTM_DELADDR, flags, result);
}
/**
* Request deletion of an address
* @arg sk Netlink socket.
* @arg addr Address object to be deleted.
* @arg flags Additional netlink message flags.
*
* Builds a netlink message by calling rtnl_addr_build_delete_request(),
* sends the request to the kernel and waits for the next ACK to be
* received and thus blocks until the request has been fullfilled.
*
* @see rtnl_addr_build_delete_request();
*
* @return 0 on sucess or a negative error if an error occured.
*/
int rtnl_addr_delete(struct nl_sock *sk, struct rtnl_addr *addr, int flags)
{
struct nl_msg *msg;
int err;
if ((err = rtnl_addr_build_delete_request(addr, flags, &msg)) < 0)
return err;
err = nl_send_auto_complete(sk, msg);
nlmsg_free(msg);
if (err < 0)
return err;
return wait_for_ack(sk);
}
/** @} */
/**
* @name Attributes
* @{
*/
int rtnl_addr_set_label(struct rtnl_addr *addr, const char *label)
{
if (strlen(label) > sizeof(addr->a_label) - 1)
return -NLE_RANGE;
strcpy(addr->a_label, label);
addr->ce_mask |= ADDR_ATTR_LABEL;
return 0;
}
char *rtnl_addr_get_label(struct rtnl_addr *addr)
{
if (addr->ce_mask & ADDR_ATTR_LABEL)
return addr->a_label;
else
return NULL;
}
void rtnl_addr_set_ifindex(struct rtnl_addr *addr, int ifindex)
{
addr->a_ifindex = ifindex;
addr->ce_mask |= ADDR_ATTR_IFINDEX;
}
int rtnl_addr_get_ifindex(struct rtnl_addr *addr)
{
return addr->a_ifindex;
}
void rtnl_addr_set_family(struct rtnl_addr *addr, int family)
{
addr->a_family = family;
addr->ce_mask |= ADDR_ATTR_FAMILY;
}
int rtnl_addr_get_family(struct rtnl_addr *addr)
{
return addr->a_family;
}
void rtnl_addr_set_prefixlen(struct rtnl_addr *addr, int prefix)
{
addr->a_prefixlen = prefix;
addr->ce_mask |= ADDR_ATTR_PREFIXLEN;
}
int rtnl_addr_get_prefixlen(struct rtnl_addr *addr)
{
return addr->a_prefixlen;
}
void rtnl_addr_set_scope(struct rtnl_addr *addr, int scope)
{
addr->a_scope = scope;
addr->ce_mask |= ADDR_ATTR_SCOPE;
}
int rtnl_addr_get_scope(struct rtnl_addr *addr)
{
return addr->a_scope;
}
void rtnl_addr_set_flags(struct rtnl_addr *addr, unsigned int flags)
{
addr->a_flag_mask |= flags;
addr->a_flags |= flags;
addr->ce_mask |= ADDR_ATTR_FLAGS;
}
void rtnl_addr_unset_flags(struct rtnl_addr *addr, unsigned int flags)
{
addr->a_flag_mask |= flags;
addr->a_flags &= ~flags;
addr->ce_mask |= ADDR_ATTR_FLAGS;
}
unsigned int rtnl_addr_get_flags(struct rtnl_addr *addr)
{
return addr->a_flags;
}
static inline int __assign_addr(struct rtnl_addr *addr, struct nl_addr **pos,
struct nl_addr *new, int flag)
{
if (addr->ce_mask & ADDR_ATTR_FAMILY) {
if (new->a_family != addr->a_family)
return -NLE_AF_MISMATCH;
} else
addr->a_family = new->a_family;
if (*pos)
nl_addr_put(*pos);
*pos = nl_addr_get(new);
addr->ce_mask |= (flag | ADDR_ATTR_FAMILY);
return 0;
}
int rtnl_addr_set_local(struct rtnl_addr *addr, struct nl_addr *local)
{
int err;
err = __assign_addr(addr, &addr->a_local, local, ADDR_ATTR_LOCAL);
if (err < 0)
return err;
if (!(addr->ce_mask & ADDR_ATTR_PEER)) {
addr->a_prefixlen = nl_addr_get_prefixlen(addr->a_local);
addr->ce_mask |= ADDR_ATTR_PREFIXLEN;
}
return 0;
}
struct nl_addr *rtnl_addr_get_local(struct rtnl_addr *addr)
{
return addr->a_local;
}
int rtnl_addr_set_peer(struct rtnl_addr *addr, struct nl_addr *peer)
{
return __assign_addr(addr, &addr->a_peer, peer, ADDR_ATTR_PEER);
addr->a_prefixlen = nl_addr_get_prefixlen(addr->a_peer);
addr->ce_mask |= ADDR_ATTR_PREFIXLEN;
return 0;
}
struct nl_addr *rtnl_addr_get_peer(struct rtnl_addr *addr)
{
return addr->a_peer;
}
int rtnl_addr_set_broadcast(struct rtnl_addr *addr, struct nl_addr *bcast)
{
return __assign_addr(addr, &addr->a_bcast, bcast, ADDR_ATTR_BROADCAST);
}
struct nl_addr *rtnl_addr_get_broadcast(struct rtnl_addr *addr)
{
return addr->a_bcast;
}
int rtnl_addr_set_multicast(struct rtnl_addr *addr, struct nl_addr *multicast)
{
return __assign_addr(addr, &addr->a_multicast, multicast,
ADDR_ATTR_MULTICAST);
}
struct nl_addr *rtnl_addr_get_multicast(struct rtnl_addr *addr)
{
return addr->a_multicast;
}
int rtnl_addr_set_anycast(struct rtnl_addr *addr, struct nl_addr *anycast)
{
return __assign_addr(addr, &addr->a_anycast, anycast,
ADDR_ATTR_ANYCAST);
}
struct nl_addr *rtnl_addr_get_anycast(struct rtnl_addr *addr)
{
return addr->a_anycast;
}
uint32_t rtnl_addr_get_valid_lifetime(struct rtnl_addr *addr)
{
if (addr->ce_mask & ADDR_ATTR_CACHEINFO)
return addr->a_cacheinfo.aci_valid;
else
return 0xFFFFFFFFU;
}
void rtnl_addr_set_valid_lifetime(struct rtnl_addr *addr, uint32_t lifetime)
{
addr->a_cacheinfo.aci_valid = lifetime;
addr->ce_mask |= ADDR_ATTR_CACHEINFO;
}
uint32_t rtnl_addr_get_preferred_lifetime(struct rtnl_addr *addr)
{
if (addr->ce_mask & ADDR_ATTR_CACHEINFO)
return addr->a_cacheinfo.aci_prefered;
else
return 0xFFFFFFFFU;
}
void rtnl_addr_set_preferred_lifetime(struct rtnl_addr *addr, uint32_t lifetime)
{
addr->a_cacheinfo.aci_prefered = lifetime;
addr->ce_mask |= ADDR_ATTR_CACHEINFO;
}
uint32_t rtnl_addr_get_create_time(struct rtnl_addr *addr)
{
return addr->a_cacheinfo.aci_cstamp;
}
uint32_t rtnl_addr_get_last_update_time(struct rtnl_addr *addr)
{
return addr->a_cacheinfo.aci_tstamp;
}
/** @} */
/**
* @name Flags Translations
* @{
*/
static const struct trans_tbl addr_flags[] = {
__ADD(IFA_F_SECONDARY, secondary)
__ADD(IFA_F_NODAD, nodad)
__ADD(IFA_F_OPTIMISTIC, optimistic)
__ADD(IFA_F_HOMEADDRESS, homeaddress)
__ADD(IFA_F_DEPRECATED, deprecated)
__ADD(IFA_F_TENTATIVE, tentative)
__ADD(IFA_F_PERMANENT, permanent)
};
char *rtnl_addr_flags2str(int flags, char *buf, size_t size)
{
return __flags2str(flags, buf, size, addr_flags,
ARRAY_SIZE(addr_flags));
}
int rtnl_addr_str2flags(const char *name)
{
return __str2flags(name, addr_flags, ARRAY_SIZE(addr_flags));
}
/** @} */
static struct nl_object_ops addr_obj_ops = {
.oo_name = "route/addr",
.oo_size = sizeof(struct rtnl_addr),
.oo_constructor = addr_constructor,
.oo_free_data = addr_free_data,
.oo_clone = addr_clone,
.oo_dump = {
[NL_DUMP_LINE] = addr_dump_line,
[NL_DUMP_DETAILS] = addr_dump_details,
[NL_DUMP_STATS] = addr_dump_stats,
},
.oo_compare = addr_compare,
.oo_attrs2str = addr_attrs2str,
.oo_id_attrs = (ADDR_ATTR_FAMILY | ADDR_ATTR_IFINDEX |
ADDR_ATTR_LOCAL | ADDR_ATTR_PREFIXLEN),
};
static struct nl_af_group addr_groups[] = {
{ AF_INET, RTNLGRP_IPV4_IFADDR },
{ AF_INET6, RTNLGRP_IPV6_IFADDR },
{ END_OF_GROUP_LIST },
};
static struct nl_cache_ops rtnl_addr_ops = {
.co_name = "route/addr",
.co_hdrsize = sizeof(struct ifaddrmsg),
.co_msgtypes = {
{ RTM_NEWADDR, NL_ACT_NEW, "new" },
{ RTM_DELADDR, NL_ACT_DEL, "del" },
{ RTM_GETADDR, NL_ACT_GET, "get" },
END_OF_MSGTYPES_LIST,
},
.co_protocol = NETLINK_ROUTE,
.co_groups = addr_groups,
.co_request_update = addr_request_update,
.co_msg_parser = addr_msg_parser,
.co_obj_ops = &addr_obj_ops,
};
static void __init addr_init(void)
{
nl_cache_mngt_register(&rtnl_addr_ops);
}
static void __exit addr_exit(void)
{
nl_cache_mngt_unregister(&rtnl_addr_ops);
}
/** @} */
|