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 if (type == 0) {
00274 fprintf(stderr, "Illegal nla->nla_type == 0\n");
00275 continue;
00276 }
00277
00278 if (type <= maxtype) {
00279 if (policy) {
00280 err = validate_nla(nla, maxtype, policy);
00281 if (err < 0)
00282 goto errout;
00283 }
00284
00285 tb[type] = nla;
00286 }
00287 }
00288
00289 if (rem > 0)
00290 fprintf(stderr, "netlink: %d bytes leftover after parsing "
00291 "attributes.\n", rem);
00292
00293 err = 0;
00294 errout:
00295 return err;
00296 }
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308 int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla,
00309 struct nla_policy *policy)
00310 {
00311 return nla_parse(tb, maxtype, nla_data(nla), nla_len(nla), policy);
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328 int nla_validate(struct nlattr *head, int len, int maxtype,
00329 struct nla_policy *policy)
00330 {
00331 struct nlattr *nla;
00332 int rem, err;
00333
00334 nla_for_each_attr(nla, head, len, rem) {
00335 err = validate_nla(nla, maxtype, policy);
00336 if (err < 0)
00337 goto errout;
00338 }
00339
00340 err = 0;
00341 errout:
00342 return err;
00343 }
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353 struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
00354 {
00355 struct nlattr *nla;
00356 int rem;
00357
00358 nla_for_each_attr(nla, head, len, rem)
00359 if (nla_type(nla) == attrtype)
00360 return nla;
00361
00362 return NULL;
00363 }
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383 int nla_memcpy(void *dest, struct nlattr *src, int count)
00384 {
00385 int minlen;
00386
00387 if (!src)
00388 return 0;
00389
00390 minlen = min_t(int, count, nla_len(src));
00391 memcpy(dest, nla_data(src), minlen);
00392
00393 return minlen;
00394 }
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408 size_t nla_strlcpy(char *dst, const struct nlattr *nla, size_t dstsize)
00409 {
00410 size_t srclen = nla_len(nla);
00411 char *src = nla_data(nla);
00412
00413 if (srclen > 0 && src[srclen - 1] == '\0')
00414 srclen--;
00415
00416 if (dstsize > 0) {
00417 size_t len = (srclen >= dstsize) ? dstsize - 1 : srclen;
00418
00419 memset(dst, 0, dstsize);
00420 memcpy(dst, src, len);
00421 }
00422
00423 return srclen;
00424 }
00425
00426
00427
00428
00429
00430
00431
00432 int nla_memcmp(const struct nlattr *nla, const void *data,
00433 size_t size)
00434 {
00435 int d = nla_len(nla) - size;
00436
00437 if (d == 0)
00438 d = memcmp(nla_data(nla), data, size);
00439
00440 return d;
00441 }
00442
00443
00444
00445
00446
00447
00448 int nla_strcmp(const struct nlattr *nla, const char *str)
00449 {
00450 int len = strlen(str) + 1;
00451 int d = nla_len(nla) - len;
00452
00453 if (d == 0)
00454 d = memcmp(nla_data(nla), str, len);
00455
00456 return d;
00457 }
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475 struct nlattr *nla_reserve(struct nl_msg *n, int attrtype, int attrlen)
00476 {
00477 struct nlattr *nla;
00478 int tlen;
00479
00480 tlen = NLMSG_ALIGN(n->nm_nlh->nlmsg_len) + nla_total_size(attrlen);
00481
00482 if ((tlen + n->nm_nlh->nlmsg_len) > n->nm_size) {
00483 nl_errno(ENOBUFS);
00484 return NULL;
00485 }
00486
00487 nla = (struct nlattr *) nlmsg_tail(n->nm_nlh);
00488 nla->nla_type = attrtype;
00489 nla->nla_len = nla_attr_size(attrlen);
00490
00491 memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
00492 n->nm_nlh->nlmsg_len = tlen;
00493
00494 NL_DBG(2, "msg %p: Reserved %d bytes at offset +%td for attr %d "
00495 "nlmsg_len=%d\n", n, attrlen,
00496 (void *) nla - nlmsg_data(n->nm_nlh),
00497 attrtype, n->nm_nlh->nlmsg_len);
00498
00499 return nla;
00500 }
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512 int nla_put(struct nl_msg *n, int attrtype, int attrlen, const void *data)
00513 {
00514 struct nlattr *nla;
00515
00516 nla = nla_reserve(n, attrtype, attrlen);
00517 if (!nla)
00518 return nl_errno(ENOMEM);
00519
00520 memcpy(nla_data(nla), data, attrlen);
00521 NL_DBG(2, "msg %p: Wrote %d bytes at offset +%td for attr %d\n",
00522 n, attrlen, (void *) nla - nlmsg_data(n->nm_nlh), attrtype);
00523
00524 return 0;
00525 }
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536 int nla_put_nested(struct nl_msg *n, int attrtype, struct nl_msg *nested)
00537 {
00538 return nla_put(n, attrtype, nlmsg_len(nested->nm_nlh),
00539 nlmsg_data(nested->nm_nlh));
00540 }
00541
00542
00543
00544
00545
00546
00547
00548 int nla_put_u8(struct nl_msg *n, int attrtype, uint8_t value)
00549 {
00550 return nla_put(n, attrtype, sizeof(uint8_t), &value);
00551 }
00552
00553
00554
00555
00556
00557
00558
00559 int nla_put_u16(struct nl_msg *n, int attrtype, uint16_t value)
00560 {
00561 return nla_put(n, attrtype, sizeof(uint16_t), &value);
00562 }
00563
00564
00565
00566
00567
00568
00569
00570 int nla_put_u32(struct nl_msg *n, int attrtype, uint32_t value)
00571 {
00572 return nla_put(n, attrtype, sizeof(uint32_t), &value);
00573 }
00574
00575
00576
00577
00578
00579
00580
00581 int nla_put_u64(struct nl_msg *n, int attrtype, uint64_t value)
00582 {
00583 return nla_put(n, attrtype, sizeof(uint64_t), &value);
00584 }
00585
00586
00587
00588
00589
00590
00591
00592 int nla_put_string(struct nl_msg *n, int attrtype, const char *str)
00593 {
00594 return nla_put(n, attrtype, strlen(str) + 1, str);
00595 }
00596
00597
00598
00599
00600
00601
00602 int nla_put_flag(struct nl_msg *n, int attrtype)
00603 {
00604 return nla_put(n, attrtype, 0, NULL);
00605 }
00606
00607
00608
00609
00610
00611
00612
00613 int nla_put_msecs(struct nl_msg *n, int attrtype, unsigned long msecs)
00614 {
00615 return nla_put_u64(n, attrtype, msecs);
00616 }
00617
00618
00619
00620
00621
00622
00623
00624 int nla_put_data(struct nl_msg *n, int attrtype, struct nl_data *data)
00625 {
00626 return nla_put(n, attrtype, nl_data_get_size(data),
00627 nl_data_get(data));
00628 }
00629
00630
00631
00632
00633
00634
00635
00636 int nla_put_addr(struct nl_msg *n, int attrtype, struct nl_addr *addr)
00637 {
00638 return nla_put(n, attrtype, nl_addr_get_len(addr),
00639 nl_addr_get_binary_addr(addr));
00640 }
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656 struct nlattr *nla_nest_start(struct nl_msg *n, int attrtype)
00657 {
00658 struct nlattr *start = (struct nlattr *) nlmsg_tail(n->nm_nlh);
00659
00660 if (nla_put(n, attrtype, 0, NULL) < 0)
00661 return NULL;
00662
00663 return start;
00664 }
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676 int nla_nest_end(struct nl_msg *n, struct nlattr *start)
00677 {
00678 start->nla_len = (unsigned char *) nlmsg_tail(n->nm_nlh) -
00679 (unsigned char *) start;
00680 return 0;
00681 }
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694 uint32_t nla_get_u32(struct nlattr *nla)
00695 {
00696 return *(uint32_t *) nla_data(nla);
00697 }
00698
00699
00700
00701
00702
00703 uint16_t nla_get_u16(struct nlattr *nla)
00704 {
00705 return *(uint16_t *) nla_data(nla);
00706 }
00707
00708
00709
00710
00711
00712 uint8_t nla_get_u8(struct nlattr *nla)
00713 {
00714 return *(uint8_t *) nla_data(nla);
00715 }
00716
00717
00718
00719
00720
00721 uint64_t nla_get_u64(struct nlattr *nla)
00722 {
00723 uint64_t tmp;
00724
00725 nla_memcpy(&tmp, nla, sizeof(tmp));
00726
00727 return tmp;
00728 }
00729
00730
00731
00732
00733
00734 char *nla_get_string(struct nlattr *nla)
00735 {
00736 return (char *) nla_data(nla);
00737 }
00738
00739
00740
00741
00742
00743 int nla_get_flag(struct nlattr *nla)
00744 {
00745 return !!nla;
00746 }
00747
00748
00749
00750
00751
00752
00753
00754 unsigned long nla_get_msecs(struct nlattr *nla)
00755 {
00756 return nla_get_u64(nla);
00757 }
00758
00759
00760
00761
00762
00763
00764
00765
00766 struct nl_addr *nla_get_addr(struct nlattr *nla, int family)
00767 {
00768 return nl_addr_build(family, nla_data(nla), nla_len(nla));
00769 }
00770
00771
00772
00773
00774
00775
00776
00777 struct nl_data *nla_get_data(struct nlattr *nla)
00778 {
00779 return nl_data_alloc(nla_data(nla), nla_len(nla));
00780 }
00781
00782
00783
00784