00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <netlink-local.h>
00013 #include <netlink/netlink.h>
00014 #include <netlink/utils.h>
00015 #include <netlink/addr.h>
00016 #include <netlink/attr.h>
00017 #include <netlink/msg.h>
00018 #include <linux/socket.h>
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 int nla_attr_size(int payload)
00109 {
00110 return NLA_HDRLEN + payload;
00111 }
00112
00113
00114
00115
00116
00117 int nla_total_size(int payload)
00118 {
00119 return NLA_ALIGN(nla_attr_size(payload));
00120 }
00121
00122
00123
00124
00125
00126 int nla_padlen(int payload)
00127 {
00128 return nla_total_size(payload) - nla_attr_size(payload);
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 int nla_type(const struct nlattr *nla)
00143 {
00144 return nla->nla_type & NLA_TYPE_MASK;
00145 }
00146
00147
00148
00149
00150
00151 void *nla_data(const struct nlattr *nla)
00152 {
00153 return (char *) nla + NLA_HDRLEN;
00154 }
00155
00156
00157
00158
00159
00160 int nla_len(const struct nlattr *nla)
00161 {
00162 return nla->nla_len - NLA_HDRLEN;
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 int nla_ok(const struct nlattr *nla, int remaining)
00178 {
00179 return remaining >= sizeof(*nla) &&
00180 nla->nla_len >= sizeof(*nla) &&
00181 nla->nla_len <= remaining;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192 struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
00193 {
00194 int totlen = NLA_ALIGN(nla->nla_len);
00195
00196 *remaining -= totlen;
00197 return (struct nlattr *) ((char *) nla + totlen);
00198 }
00199
00200 static uint16_t nla_attr_minlen[NLA_TYPE_MAX+1] = {
00201 [NLA_U8] = sizeof(uint8_t),
00202 [NLA_U16] = sizeof(uint16_t),
00203 [NLA_U32] = sizeof(uint32_t),
00204 [NLA_U64] = sizeof(uint64_t),
00205 [NLA_STRING] = 1,
00206 [NLA_NESTED] = NLA_HDRLEN,
00207 };
00208
00209 static int validate_nla(struct nlattr *nla, int maxtype,
00210 struct nla_policy *policy)
00211 {
00212 struct nla_policy *pt;
00213 int minlen = 0, type = nla_type(nla);
00214
00215 if (type <= 0 || type > maxtype)
00216 return 0;
00217
00218 pt = &policy[type];
00219
00220 if (pt->type > NLA_TYPE_MAX)
00221 BUG();
00222
00223 if (pt->minlen)
00224 minlen = pt->minlen;
00225 else if (pt->type != NLA_UNSPEC)
00226 minlen = nla_attr_minlen[pt->type];
00227
00228 if (pt->type == NLA_FLAG && nla_len(nla) > 0)
00229 return nl_errno(ERANGE);
00230
00231 if (nla_len(nla) < minlen)
00232 return nl_errno(ERANGE);
00233
00234 if (pt->maxlen && nla_len(nla) > pt->maxlen)
00235 return nl_errno(ERANGE);
00236
00237 if (pt->type == NLA_STRING) {
00238 char *data = nla_data(nla);
00239 if (data[nla_len(nla) - 1] != '\0')
00240 return nl_errno(EINVAL);
00241 }
00242
00243 return 0;
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262 int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
00263 struct nla_policy *policy)
00264 {
00265 struct nlattr *nla;
00266 int rem, err;
00267
00268 memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
00269
00270 nla_for_each_attr(nla, head, len, rem) {
00271 int type = nla_type(nla);
00272
00273
00274 if (type == 0)
00275 continue;
00276
00277 if (type <= maxtype) {
00278 if (policy) {
00279 err = validate_nla(nla, maxtype, policy);
00280 if (err < 0)
00281 goto errout;
00282 }
00283
00284 tb[type] = nla;
00285 }
00286 }
00287
00288 if (rem > 0)
00289 fprintf(stderr, "netlink: %d bytes leftover after parsing "
00290 "attributes.\n", rem);
00291
00292 err = 0;
00293 errout:
00294 return err;
00295 }
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla,
00308 struct nla_policy *policy)
00309 {
00310 return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
00311 }
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327 int nla_validate(struct nlattr *head, int len, int maxtype,
00328 struct nla_policy *policy)
00329 {
00330 struct nlattr *nla;
00331 int rem, err;
00332
00333 nla_for_each_attr(nla, head, len, rem) {
00334 err = validate_nla(nla, maxtype, policy);
00335 if (err < 0)
00336 goto errout;
00337 }
00338
00339 err = 0;
00340 errout:
00341 return err;
00342 }
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
00353 {
00354 struct nlattr *nla;
00355 int rem;
00356
00357 nla_for_each_attr(nla, head, len, rem)
00358 if (nla_type(nla) == attrtype)
00359 return nla;
00360
00361 return NULL;
00362 }
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382 int nla_memcpy(void *dest, struct nlattr *src, int count)
00383 {
00384 int minlen;
00385
00386 if (!src)
00387 return 0;
00388
00389 minlen = min_t(int, count, nla_len(src));
00390 memcpy(dest, nla_data(src), minlen);
00391
00392 return minlen;
00393 }
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
00408 {
00409 size_t srclen = nla_len(nla);
00410 char *src = nla_data(nla);
00411
00412 if (srclen > 0 && src[srclen - 1] == '\0')
00413 srclen--;
00414
00415 if (dstsize > 0) {
00416 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
00417
00418 memset(dst, 0, dstsize);
00419 memcpy(dst, src, len);
00420 }
00421
00422 return srclen;
00423 }
00424
00425
00426
00427
00428
00429
00430
00431 int nla_memcmp(const struct nlattr *nla, const void *data,
00432 size_t size)
00433 {
00434 int d = nla_len(nla) - size;
00435
00436 if (d == 0)
00437 d = memcmp(nla_data(nla), data, size);
00438
00439 return d;
00440 }
00441
00442
00443
00444
00445
00446
00447 int nla_strcmp(const struct nlattr *nla, const char *str)
00448 {
00449 int len = strlen(str) + 1;
00450 int d = nla_len(nla) - len;
00451
00452 if (d == 0)
00453 d = memcmp(nla_data(nla), str, len);
00454
00455 return d;
00456 }
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474 struct nlattr *nla_reserve(struct nl_msg *n, int attrtype, int attrlen)
00475 {
00476 struct nlattr *nla;
00477 int tlen;
00478
00479 tlen = NLMSG_ALIGN(n->nm_nlh->nlmsg_len) + nla_total_size(attrlen);
00480
00481 if (tlen > n->nm_size) {
00482 nl_errno(ENOBUFS);
00483 return NULL;
00484 }
00485
00486 nla = (struct nlattr *) nlmsg_tail(n->nm_nlh);
00487 nla->nla_type = attrtype;
00488 nla->nla_len = nla_attr_size(attrlen);
00489
00490 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
00491 n->nm_nlh->nlmsg_len = tlen;
00492
00493 NL_DBG(2, "msg %p: Reserved %d bytes at offset +%td for attr %d "
00494 "nlmsg_len=%d\n", n, attrlen,
00495 (void *) nla - nlmsg_data(n->nm_nlh),
00496 attrtype, n->nm_nlh->nlmsg_len);
00497
00498 return nla;
00499 }
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511 int nla_put(struct nl_msg *n, int attrtype, int attrlen, const void *data)
00512 {
00513 struct nlattr *nla;
00514
00515 nla = nla_reserve(n, attrtype, attrlen);
00516 if (!nla)
00517 return nl_errno(ENOMEM);
00518
00519 memcpy(nla_data(nla), data, attrlen);
00520 NL_DBG(2, "msg %p: Wrote %d bytes at offset +%td for attr %d\n",
00521 n, attrlen, (void *) nla - nlmsg_data(n->nm_nlh), attrtype);
00522
00523 return 0;
00524 }
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535 int nla_put_nested(struct nl_msg *n, int attrtype, struct nl_msg *nested)
00536 {
00537 return nla_put(n, attrtype, nlmsg_len(nested->nm_nlh),
00538 nlmsg_data(nested->nm_nlh));
00539 }
00540
00541
00542
00543
00544
00545
00546
00547 int nla_put_u8(struct nl_msg *n, int attrtype, uint8_t value)
00548 {
00549 return nla_put(n, attrtype, sizeof(uint8_t), &value);
00550 }
00551
00552
00553
00554
00555
00556
00557
00558 int nla_put_u16(struct nl_msg *n, int attrtype, uint16_t value)
00559 {
00560 return nla_put(n, attrtype, sizeof(uint16_t), &value);
00561 }
00562
00563
00564
00565
00566
00567
00568
00569 int nla_put_u32(struct nl_msg *n, int attrtype, uint32_t value)
00570 {
00571 return nla_put(n, attrtype, sizeof(uint32_t), &value);
00572 }
00573
00574
00575
00576
00577
00578
00579
00580 int nla_put_u64(struct nl_msg *n, int attrtype, uint64_t value)
00581 {
00582 return nla_put(n, attrtype, sizeof(uint64_t), &value);
00583 }
00584
00585
00586
00587
00588
00589
00590
00591 int nla_put_string(struct nl_msg *n, int attrtype, const char *str)
00592 {
00593 return nla_put(n, attrtype, strlen(str) + 1, str);
00594 }
00595
00596
00597
00598
00599
00600
00601 int nla_put_flag(struct nl_msg *n, int attrtype)
00602 {
00603 return nla_put(n, attrtype, 0, NULL);
00604 }
00605
00606
00607
00608
00609
00610
00611
00612 int nla_put_msecs(struct nl_msg *n, int attrtype, unsigned long msecs)
00613 {
00614 return nla_put_u64(n, attrtype, msecs);
00615 }
00616
00617
00618
00619
00620
00621
00622
00623 int nla_put_data(struct nl_msg *n, int attrtype, struct nl_data *data)
00624 {
00625 return nla_put(n, attrtype, nl_data_get_size(data),
00626 nl_data_get(data));
00627 }
00628
00629
00630
00631
00632
00633
00634
00635 int nla_put_addr(struct nl_msg *n, int attrtype, struct nl_addr *addr)
00636 {
00637 return nla_put(n, attrtype, nl_addr_get_len(addr),
00638 nl_addr_get_binary_addr(addr));
00639 }
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655 struct nlattr *nla_nest_start(struct nl_msg *n, int attrtype)
00656 {
00657 struct nlattr *start = (struct nlattr *) nlmsg_tail(n->nm_nlh);
00658
00659 if (nla_put(n, attrtype, 0, NULL) < 0)
00660 return NULL;
00661
00662 return start;
00663 }
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675 int nla_nest_end(struct nl_msg *n, struct nlattr *start)
00676 {
00677 start->nla_len = (unsigned char *) nlmsg_tail(n->nm_nlh) -
00678 (unsigned char *) start;
00679 return 0;
00680 }
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693 uint32_t nla_get_u32(struct nlattr *nla)
00694 {
00695 return *(uint32_t *) nla_data(nla);
00696 }
00697
00698
00699
00700
00701
00702 uint16_t nla_get_u16(struct nlattr *nla)
00703 {
00704 return *(uint16_t *) nla_data(nla);
00705 }
00706
00707
00708
00709
00710
00711 uint8_t nla_get_u8(struct nlattr *nla)
00712 {
00713 return *(uint8_t *) nla_data(nla);
00714 }
00715
00716
00717
00718
00719
00720 uint64_t nla_get_u64(struct nlattr *nla)
00721 {
00722 uint64_t tmp;
00723
00724 nla_memcpy(&tmp, nla, sizeof(tmp));
00725
00726 return tmp;
00727 }
00728
00729
00730
00731
00732
00733 char *nla_get_string(struct nlattr *nla)
00734 {
00735 return (char *) nla_data(nla);
00736 }
00737
00738
00739
00740
00741
00742 int nla_get_flag(struct nlattr *nla)
00743 {
00744 return !!nla;
00745 }
00746
00747
00748
00749
00750
00751
00752
00753 unsigned long nla_get_msecs(struct nlattr *nla)
00754 {
00755 return nla_get_u64(nla);
00756 }
00757
00758
00759
00760
00761
00762
00763
00764
00765 struct nl_addr *nla_get_addr(struct nlattr *nla, int family)
00766 {
00767 return nl_addr_build(family, nla_data(nla), nla_len(nla));
00768 }
00769
00770
00771
00772
00773
00774
00775
00776 struct nl_data *nla_get_data(struct nlattr *nla)
00777 {
00778 return nl_data_alloc(nla_data(nla), nla_len(nla));
00779 }
00780
00781
00782
00783