00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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 #include <netlink-local.h>
00093 #include <netlink/netlink.h>
00094 #include <netlink/utils.h>
00095 #include <netlink/handlers.h>
00096 #include <netlink/msg.h>
00097 #include <netlink/attr.h>
00098
00099 static int default_cb = NL_CB_DEFAULT;
00100
00101 static void __init init_default_cb(void)
00102 {
00103 char *nlcb;
00104
00105 if ((nlcb = getenv("NLCB"))) {
00106 if (!strcasecmp(nlcb, "default"))
00107 default_cb = NL_CB_DEFAULT;
00108 else if (!strcasecmp(nlcb, "verbose"))
00109 default_cb = NL_CB_VERBOSE;
00110 else if (!strcasecmp(nlcb, "debug"))
00111 default_cb = NL_CB_DEBUG;
00112 else {
00113 fprintf(stderr, "Unknown value for NLCB, valid values: "
00114 "{default | verbose | debug}\n");
00115 }
00116 }
00117 }
00118
00119 static uint32_t used_ports_map[32];
00120
00121 static uint32_t generate_local_port(void)
00122 {
00123 int i, n;
00124 uint32_t pid = getpid() & 0x3FFFFF;
00125
00126 for (i = 0; i < 32; i++) {
00127 if (used_ports_map[i] == 0xFFFFFFFF)
00128 continue;
00129
00130 for (n = 0; n < 32; n++) {
00131 if (1UL & (used_ports_map[i] >> n))
00132 continue;
00133
00134 used_ports_map[i] |= (1UL << n);
00135 n += (i * 32);
00136
00137
00138
00139 return pid + (n << 22);
00140
00141 }
00142 }
00143
00144
00145 return UINT_MAX;
00146 }
00147
00148 static void release_local_port(uint32_t port)
00149 {
00150 int nr;
00151
00152 if (port == UINT_MAX)
00153 return;
00154
00155 nr = port >> 22;
00156 used_ports_map[nr / 32] &= ~((nr % 32) + 1);
00157 }
00158
00159
00160
00161
00162
00163
00164 static struct nl_handle *__alloc_handle(struct nl_cb *cb)
00165 {
00166 struct nl_handle *handle;
00167
00168 handle = calloc(1, sizeof(*handle));
00169 if (!handle) {
00170 nl_errno(ENOMEM);
00171 return NULL;
00172 }
00173
00174 handle->h_fd = -1;
00175 handle->h_cb = cb;
00176 handle->h_local.nl_family = AF_NETLINK;
00177 handle->h_peer.nl_family = AF_NETLINK;
00178 handle->h_seq_expect = handle->h_seq_next = time(0);
00179 handle->h_local.nl_pid = generate_local_port();
00180 if (handle->h_local.nl_pid == UINT_MAX) {
00181 nl_handle_destroy(handle);
00182 nl_error(ENOBUFS, "Out of local ports");
00183 return NULL;
00184 }
00185
00186 return handle;
00187 }
00188
00189
00190
00191
00192
00193
00194 struct nl_handle *nl_handle_alloc(void)
00195 {
00196 struct nl_cb *cb;
00197
00198 cb = nl_cb_alloc(default_cb);
00199 if (!cb) {
00200 nl_errno(ENOMEM);
00201 return NULL;
00202 }
00203
00204 return __alloc_handle(cb);
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 struct nl_handle *nl_handle_alloc_cb(struct nl_cb *cb)
00217 {
00218 if (cb == NULL)
00219 BUG();
00220
00221 return __alloc_handle(nl_cb_get(cb));
00222 }
00223
00224
00225
00226
00227
00228 void nl_handle_destroy(struct nl_handle *handle)
00229 {
00230 if (!handle)
00231 return;
00232
00233 if (handle->h_fd >= 0)
00234 close(handle->h_fd);
00235
00236 if (!(handle->h_flags & NL_OWN_PORT))
00237 release_local_port(handle->h_local.nl_pid);
00238
00239 nl_cb_put(handle->h_cb);
00240 free(handle);
00241 }
00242
00243
00244
00245
00246
00247
00248
00249
00250 static int noop_seq_check(struct nl_msg *msg, void *arg)
00251 {
00252 return NL_OK;
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 void nl_disable_sequence_check(struct nl_handle *handle)
00268 {
00269 nl_cb_set(handle->h_cb, NL_CB_SEQ_CHECK,
00270 NL_CB_CUSTOM, noop_seq_check, NULL);
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 unsigned int nl_socket_use_seq(struct nl_handle *handle)
00283 {
00284 return handle->h_seq_next++;
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294 uint32_t nl_socket_get_local_port(struct nl_handle *handle)
00295 {
00296 return handle->h_local.nl_pid;
00297 }
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 void nl_socket_set_local_port(struct nl_handle *handle, uint32_t port)
00308 {
00309 if (port == 0) {
00310 port = generate_local_port();
00311 handle->h_flags &= ~NL_OWN_PORT;
00312 } else {
00313 if (!(handle->h_flags & NL_OWN_PORT))
00314 release_local_port(handle->h_local.nl_pid);
00315 handle->h_flags |= NL_OWN_PORT;
00316 }
00317
00318 handle->h_local.nl_pid = port;
00319 }
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343 int nl_socket_add_membership(struct nl_handle *handle, int group)
00344 {
00345 int err;
00346
00347 if (handle->h_fd == -1)
00348 return nl_error(EBADFD, "Socket not connected");
00349
00350 err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
00351 &group, sizeof(group));
00352 if (err < 0)
00353 return nl_error(errno, "setsockopt(NETLINK_ADD_MEMBERSHIP) "
00354 "failed");
00355
00356 return 0;
00357 }
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370 int nl_socket_drop_membership(struct nl_handle *handle, int group)
00371 {
00372 int err;
00373
00374 if (handle->h_fd == -1)
00375 return nl_error(EBADFD, "Socket not connected");
00376
00377 err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_DROP_MEMBERSHIP,
00378 &group, sizeof(group));
00379 if (err < 0)
00380 return nl_error(errno, "setsockopt(NETLINK_DROP_MEMBERSHIP) "
00381 "failed");
00382
00383 return 0;
00384 }
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395 void nl_join_groups(struct nl_handle *handle, int groups)
00396 {
00397 handle->h_local.nl_groups |= groups;
00398 }
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408 uint32_t nl_socket_get_peer_port(struct nl_handle *handle)
00409 {
00410 return handle->h_peer.nl_pid;
00411 }
00412
00413 void nl_socket_set_peer_port(struct nl_handle *handle, uint32_t port)
00414 {
00415 handle->h_peer.nl_pid = port;
00416 }
00417
00418
00419
00420
00421
00422
00423
00424
00425 int nl_socket_get_fd(struct nl_handle *handle)
00426 {
00427 return handle->h_fd;
00428 }
00429
00430
00431
00432
00433
00434
00435
00436 int nl_socket_set_nonblocking(struct nl_handle *handle)
00437 {
00438 if (handle->h_fd == -1)
00439 return nl_error(EBADFD, "Socket not connected");
00440
00441 if (fcntl(handle->h_fd, F_SETFL, O_NONBLOCK) < 0)
00442 return nl_error(errno, "fcntl(F_SETFL, O_NONBLOCK) failed");
00443
00444 return 0;
00445 }
00446
00447
00448
00449
00450
00451 void nl_socket_enable_msg_peek(struct nl_handle *handle)
00452 {
00453 handle->h_flags |= NL_MSG_PEEK;
00454 }
00455
00456
00457
00458
00459
00460 void nl_socket_disable_msg_peek(struct nl_handle *handle)
00461 {
00462 handle->h_flags &= ~NL_MSG_PEEK;
00463 }
00464
00465
00466
00467
00468
00469
00470
00471
00472 struct nl_cb *nl_socket_get_cb(struct nl_handle *handle)
00473 {
00474 return nl_cb_get(handle->h_cb);
00475 }
00476
00477 void nl_socket_set_cb(struct nl_handle *handle, struct nl_cb *cb)
00478 {
00479 nl_cb_put(handle->h_cb);
00480 handle->h_cb = nl_cb_get(cb);
00481 }
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493 int nl_socket_modify_cb(struct nl_handle *handle, enum nl_cb_type type,
00494 enum nl_cb_kind kind, nl_recvmsg_msg_cb_t func,
00495 void *arg)
00496 {
00497 return nl_cb_set(handle->h_cb, type, kind, func, arg);
00498 }
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520 int nl_set_buffer_size(struct nl_handle *handle, int rxbuf, int txbuf)
00521 {
00522 int err;
00523
00524 if (rxbuf <= 0)
00525 rxbuf = 32768;
00526
00527 if (txbuf <= 0)
00528 txbuf = 32768;
00529
00530 if (handle->h_fd == -1)
00531 return nl_error(EBADFD, "Socket not connected");
00532
00533 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_SNDBUF,
00534 &txbuf, sizeof(txbuf));
00535 if (err < 0)
00536 return nl_error(errno, "setsockopt(SO_SNDBUF) failed");
00537
00538 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_RCVBUF,
00539 &rxbuf, sizeof(rxbuf));
00540 if (err < 0)
00541 return nl_error(errno, "setsockopt(SO_RCVBUF) failed");
00542
00543 handle->h_flags |= NL_SOCK_BUFSIZE_SET;
00544
00545 return 0;
00546 }
00547
00548
00549
00550
00551
00552
00553
00554
00555 int nl_set_passcred(struct nl_handle *handle, int state)
00556 {
00557 int err;
00558
00559 if (handle->h_fd == -1)
00560 return nl_error(EBADFD, "Socket not connected");
00561
00562 err = setsockopt(handle->h_fd, SOL_SOCKET, SO_PASSCRED,
00563 &state, sizeof(state));
00564 if (err < 0)
00565 return nl_error(errno, "setsockopt(SO_PASSCRED) failed");
00566
00567 if (state)
00568 handle->h_flags |= NL_SOCK_PASSCRED;
00569 else
00570 handle->h_flags &= ~NL_SOCK_PASSCRED;
00571
00572 return 0;
00573 }
00574
00575
00576
00577
00578
00579
00580
00581
00582 int nl_socket_recv_pktinfo(struct nl_handle *handle, int state)
00583 {
00584 int err;
00585
00586 if (handle->h_fd == -1)
00587 return nl_error(EBADFD, "Socket not connected");
00588
00589 err = setsockopt(handle->h_fd, SOL_NETLINK, NETLINK_PKTINFO,
00590 &state, sizeof(state));
00591 if (err < 0)
00592 return nl_error(errno, "setsockopt(NETLINK_PKTINFO) failed");
00593
00594 return 0;
00595 }
00596
00597
00598
00599