int main(int argc, char *argv[]) { struct nl_handle *nlh; struct rtnl_addr *addr; struct nl_addr *local; int err = 1; if (argc < 3 || !strcmp(argv[1], "-h")) { printf("Usage: nl-addr-delete <addr> <ifindex>\n"); goto errout; } if (nltool_init(argc, argv) < 0) goto errout; nlh = nltool_alloc_handle(); if (!nlh) goto errout; addr = rtnl_addr_alloc(); if (!addr) goto errout_free_handle; if (nltool_connect(nlh, NETLINK_ROUTE) < 0) goto errout_free_addr; local = nltool_addr_parse(argv[1]); if (!local) goto errout_close; if (rtnl_addr_set_local(addr, local) < 0) { fprintf(stderr, "Unable to set local address: %s\n", nl_geterror()); goto errout_addr_put; } rtnl_addr_set_ifindex(addr, strtoul(argv[2], NULL, 0)); if (rtnl_addr_delete(nlh, addr, 0) < 0) { fprintf(stderr, "Unable to delete address: %s\n", nl_geterror()); goto errout_addr_put; } err = 0; errout_addr_put: nl_addr_put(local); errout_close: nl_close(nlh); errout_free_addr: rtnl_addr_put(addr); errout_free_handle: nl_handle_destroy(nlh); errout: return err; }
int main(int argc, char *argv[]) { struct nl_handle *nlh; struct nl_cache *link_cache, *addr_cache; struct rtnl_addr *addr; struct nl_dump_params params = { .dp_fd = stdout, .dp_type = NL_DUMP_BRIEF }; int err = 1; if (nltool_init(argc, argv) < 0) return -1; nlh = nltool_alloc_handle(); if (!nlh) return -1; addr = rtnl_addr_alloc(); if (!addr) goto errout; if (argc < 2 || !strcmp(argv[1], "-h")) print_usage(); if (nltool_connect(nlh, NETLINK_ROUTE) < 0) goto errout_free; link_cache = nltool_alloc_link_cache(nlh); if (!link_cache) goto errout_close; addr_cache = nltool_alloc_addr_cache(nlh); if (!addr_cache) goto errout_link_cache; params.dp_type = nltool_parse_dumptype(argv[1]); if (params.dp_type < 0) goto errout_addr_cache; get_filter(addr, argc, argv, 2, link_cache); nl_cache_dump_filter(addr_cache, ¶ms, (struct nl_object *) addr); err = 0; errout_addr_cache: nl_cache_free(addr_cache); errout_link_cache: nl_cache_free(link_cache); errout_close: nl_close(nlh); errout_free: rtnl_addr_put(addr); errout: return err; }
int main(int argc, char *argv[]) { struct nl_handle *nlh; struct nl_cache *link_cache; int err = 1; if (nltool_init(argc, argv) < 0) return -1; if (argc < 3 || !strcmp(argv[1], "-h")) print_usage(); nlh = nltool_alloc_handle(); if (!nlh) return -1; if (nltool_connect(nlh, NETLINK_ROUTE) < 0) goto errout; link_cache = nltool_alloc_link_cache(nlh); if (!link_cache) goto errout_close; gargv = &argv[2]; gargc = argc - 2; if (!strcasecmp(argv[1], "all")) nl_cache_foreach(link_cache, dump_stats, NULL); else { int ifindex = strtoul(argv[1], NULL, 0); struct rtnl_link *link = rtnl_link_get(link_cache, ifindex); if (!link) { fprintf(stderr, "Could not find ifindex %d\n", ifindex); goto errout_link_cache; } dump_stats((struct nl_object *) link, NULL); rtnl_link_put(link); } err = 0; errout_link_cache: nl_cache_free(link_cache); errout_close: nl_close(nlh); errout: nl_handle_destroy(nlh); return err; }
int main(int argc, char *argv[]) { struct nl_cache *link_cache; if (nltool_init(argc, argv) < 0) return -1; dump_params.dp_fd = stdout; if (argc > 1) { if (!strcasecmp(argv[1], "brief")) dump_params.dp_type = NL_DUMP_BRIEF; else if (!strcasecmp(argv[1], "full")) dump_params.dp_type = NL_DUMP_FULL; else if (!strcasecmp(argv[1], "stats")) dump_params.dp_type = NL_DUMP_STATS; } nl_handle = nltool_alloc_handle(); if (!nl_handle) return 1; if (nltool_connect(nl_handle, NETLINK_ROUTE) < 0) return 1; link_cache = nltool_alloc_link_cache(nl_handle); if (!link_cache) return 1; qdisc_cache = nltool_alloc_qdisc_cache(nl_handle); if (!qdisc_cache) return 1; nl_cache_foreach(link_cache, &print_link, NULL); nl_cache_free(qdisc_cache); nl_cache_free(link_cache); nl_close(nl_handle); nl_handle_destroy(nl_handle); return 0; }
static void obj_input(struct nl_object *obj, void *arg) { struct nfnl_queue_msg *msg = (struct nfnl_queue_msg *) obj; struct nl_dump_params dp = { .dp_type = NL_DUMP_STATS, .dp_fd = stdout, .dp_dump_msgtype = 1, }; nfnl_queue_msg_set_verdict(msg, NF_ACCEPT); nl_object_dump(obj, &dp); nfnl_queue_msg_send_verdict(nfnlh, msg); } static int event_input(struct nl_msg *msg, void *arg) { if (nl_msg_parse(msg, &obj_input, NULL) < 0) fprintf(stderr, "<<EVENT>> Unknown message type\n"); /* Exit nl_recvmsgs_def() and return to the main select() */ return NL_STOP; } int main(int argc, char *argv[]) { struct nl_handle *rtnlh; struct nl_cache *link_cache; struct nfnl_queue *queue; enum nfnl_queue_copy_mode copy_mode; uint32_t copy_range; int err = 1; int family; if (nltool_init(argc, argv) < 0) return -1; nfnlh = nltool_alloc_handle(); if (nfnlh == NULL) return -1; nl_disable_sequence_check(nfnlh); nl_socket_modify_cb(nfnlh, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL); if ((argc > 1 && !strcasecmp(argv[1], "-h")) || argc < 3) { printf("Usage: nf-queue family group [ copy_mode ] " "[ copy_range ]\n"); return 2; } if (nfnl_connect(nfnlh) < 0) { fprintf(stderr, "%s\n", nl_geterror()); goto errout; } family = nl_str2af(argv[1]); if (family == AF_UNSPEC) { fprintf(stderr, "Unknown family: %s\n", argv[1]); goto errout; } nfnl_queue_pf_unbind(nfnlh, family); if (nfnl_queue_pf_bind(nfnlh, family) < 0) { fprintf(stderr, "%s\n", nl_geterror()); goto errout; } queue = nfnl_queue_alloc(); if (queue == NULL) { fprintf(stderr, "%s\n", nl_geterror()); goto errout; } nfnl_queue_set_group(queue, atoi(argv[2])); copy_mode = NFNL_QUEUE_COPY_PACKET; if (argc > 3) { copy_mode = nfnl_queue_str2copy_mode(argv[3]); if (copy_mode < 0) { fprintf(stderr, "%s\n", nl_geterror()); goto errout; } } nfnl_queue_set_copy_mode(queue, copy_mode); copy_range = 0xFFFF; if (argc > 4) copy_range = atoi(argv[4]); nfnl_queue_set_copy_range(queue, copy_range); if (nfnl_queue_create(nfnlh, queue) < 0) { fprintf(stderr, "%s\n", nl_geterror()); goto errout; } rtnlh = nltool_alloc_handle(); if (rtnlh == NULL) { goto errout_close; } if (nl_connect(rtnlh, NETLINK_ROUTE) < 0) { fprintf(stderr, "%s\n", nl_geterror()); goto errout; } if ((link_cache = rtnl_link_alloc_cache(rtnlh)) == NULL) { fprintf(stderr, "%s\n", nl_geterror()); goto errout_close; } nl_cache_mngt_provide(link_cache); while (1) { fd_set rfds; int nffd, rtfd, maxfd, retval; FD_ZERO(&rfds); maxfd = nffd = nl_socket_get_fd(nfnlh); FD_SET(nffd, &rfds); rtfd = nl_socket_get_fd(rtnlh); FD_SET(rtfd, &rfds); if (maxfd < rtfd) maxfd = rtfd; /* wait for an incoming message on the netlink socket */ retval = select(maxfd+1, &rfds, NULL, NULL, NULL); if (retval) { if (FD_ISSET(nffd, &rfds)) nl_recvmsgs_default(nfnlh); if (FD_ISSET(rtfd, &rfds)) nl_recvmsgs_default(rtnlh); } } nl_cache_mngt_unprovide(link_cache); nl_cache_free(link_cache); nfnl_queue_put(queue); nl_close(rtnlh); nl_handle_destroy(rtnlh); errout_close: nl_close(nfnlh); nl_handle_destroy(nfnlh); errout: return err; }
int main(int argc, char *argv[]) { struct nl_sock *nlh; struct rtnl_qdisc *qdisc; uint32_t handle, parent; int err = 1; if (nltool_init(argc, argv) < 0) return -1; if (argc < 5 || !strcmp(argv[1], "-h")) print_usage(); nlh = nltool_alloc_handle(); if (!nlh) goto errout; qdisc = rtnl_qdisc_alloc(); if (!qdisc) goto errout_free_handle; rtnl_qdisc_set_ifindex(qdisc, strtoul(argv[1], NULL, 0)); if (rtnl_tc_str2handle(argv[2], &handle) < 0) { fprintf(stderr, "%s\n", nl_geterror()); goto errout_free_qdisc; } if (rtnl_tc_str2handle(argv[3], &parent) < 0) { fprintf(stderr, "%s\n", nl_geterror()); goto errout_free_qdisc; } rtnl_qdisc_set_handle(qdisc, handle); rtnl_qdisc_set_parent(qdisc, parent); rtnl_qdisc_set_kind(qdisc, argv[4]); if (!strcasecmp(argv[4], "blackhole")) err = parse_blackhole_opts(qdisc, &argv[5], argc-5); else if (!strcasecmp(argv[4], "pfifo")) err = parse_pfifo_opts(qdisc, &argv[5], argc-5); else if (!strcasecmp(argv[4], "bfifo")) err = parse_bfifo_opts(qdisc, &argv[5], argc-5); else if (!strcasecmp(argv[4], "prio")) err = parse_prio_opts(qdisc, &argv[5], argc-5); else { fprintf(stderr, "Unknown qdisc \"%s\"\n", argv[4]); goto errout_free_qdisc; } if (err < 0) goto errout_free_qdisc; if (nltool_connect(nlh, NETLINK_ROUTE) < 0) goto errout_free_qdisc; if (rtnl_qdisc_add(nlh, qdisc, NLM_F_REPLACE) < 0) { fprintf(stderr, "Unable to add Qdisc: %s\n", nl_geterror()); goto errout_close; } err = 0; errout_close: nl_close(nlh); errout_free_qdisc: rtnl_qdisc_put(qdisc); errout_free_handle: nl_handle_destroy(nlh); errout: return err; }
int main(int argc, char *argv[]) { struct nl_handle *nlh; struct nl_cache *link_cache, *route_cache; struct nl_addr *dst; struct rtnl_route *route; struct ip_lookup_res res; struct nl_dump_params params = { .dp_fd = stdout, .dp_type = NL_DUMP_FULL }; int err = 1; if (argc < 2 || !strcmp(argv[1], "-h")) print_usage(); if (nltool_init(argc, argv) < 0) goto errout; nlh = nltool_alloc_handle(); if (!nlh) goto errout; route = rtnl_route_alloc(); if (!route) goto errout_free_handle; if (nltool_connect(nlh, NETLINK_ROUTE) < 0) goto errout_free_route; link_cache = nltool_alloc_link_cache(nlh); if (!link_cache) goto errout_close; dst = nltool_addr_parse(argv[1]); if (!dst) goto errout_link_cache; route_cache = nltool_alloc_route_cache(nlh); if (!route_cache) goto errout_addr_put; { struct nl_msg *m; struct rtmsg rmsg = { .rtm_family = nl_addr_get_family(dst), .rtm_dst_len = nl_addr_get_prefixlen(dst), }; m = nlmsg_alloc_simple(RTM_GETROUTE, 0); nlmsg_append(m, &rmsg, sizeof(rmsg), NLMSG_ALIGNTO); nla_put_addr(m, RTA_DST, dst); if ((err = nl_send_auto_complete(nlh, m)) < 0) { nlmsg_free(m); fprintf(stderr, "%s\n", nl_geterror()); goto errout_route_cache; } nlmsg_free(m); nl_socket_modify_cb(nlh, NL_CB_VALID, NL_CB_CUSTOM, cb, route_cache); if (nl_recvmsgs_default(nlh) < 0) { fprintf(stderr, "%s\n", nl_geterror()); goto errout_route_cache; } } rtnl_route_set_dst(route, dst); nl_cache_dump_filter(route_cache, ¶ms, (struct nl_object *) route); memset(&res, 0, sizeof(res)); nl_cache_foreach_filter(route_cache, (struct nl_object *) route, route_proc_cb, &res); printf("ip lookup result: oif idx: %d oif name %s ", res.oif, res.oifname); if (res.nh_addr) { char buf[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &res.nh_addr, buf, sizeof(buf)); printf("via %s", buf); } printf ("\n"); err = 0; errout_route_cache: nl_cache_free(route_cache); errout_addr_put: nl_addr_put(dst); errout_link_cache: nl_cache_free(link_cache); errout_close: nl_close(nlh); errout_free_route: rtnl_route_put(route); errout_free_handle: nl_handle_destroy(nlh); errout: return err; }
static void obj_input(struct nl_object *obj, void *arg) { struct nl_dump_params dp = { .dp_type = NL_DUMP_STATS, .dp_fd = stdout, .dp_dump_msgtype = 1, }; nl_object_dump(obj, &dp); } static int event_input(struct nl_msg *msg, void *arg) { if (nl_msg_parse(msg, &obj_input, NULL) < 0) fprintf(stderr, "<<EVENT>> Unknown message type\n"); /* Exit nl_recvmsgs_def() and return to the main select() */ return NL_STOP; } int main(int argc, char *argv[]) { struct nl_handle *nlh; int err = 1; int i, idx; static const struct { enum nfnetlink_groups gr_id; const char* gr_name; } known_groups[] = { { NFNLGRP_CONNTRACK_NEW, "ct-new" }, { NFNLGRP_CONNTRACK_UPDATE, "ct-update" }, { NFNLGRP_CONNTRACK_DESTROY, "ct-destroy" }, { NFNLGRP_NONE, NULL } }; if (nltool_init(argc, argv) < 0) return -1; nlh = nltool_alloc_handle(); if (nlh == NULL) return -1; nl_disable_sequence_check(nlh); nl_socket_modify_cb(nlh, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL); if (argc > 1 && !strcasecmp(argv[1], "-h")) { printf("Usage: nf-monitor [<groups>]\n"); printf("Known groups:"); for (i = 0; known_groups[i].gr_id != NFNLGRP_NONE; i++) printf(" %s", known_groups[i].gr_name); printf("\n"); return 2; } if (nfnl_connect(nlh) < 0) { fprintf(stderr, "%s\n", nl_geterror()); goto errout; } for (idx = 1; argc > idx; idx++) { for (i = 0; known_groups[i].gr_id != NFNLGRP_NONE; i++) { if (!strcmp(argv[idx], known_groups[i].gr_name)) { if (nl_socket_add_membership(nlh, known_groups[i].gr_id) < 0) { fprintf(stderr, "%s: %s\n", argv[idx], nl_geterror()); goto errout; } break; } } if (known_groups[i].gr_id == NFNLGRP_NONE) fprintf(stderr, "Warning: Unknown group: %s\n", argv[idx]); } while (1) { fd_set rfds; int fd, retval; fd = nl_socket_get_fd(nlh); FD_ZERO(&rfds); FD_SET(fd, &rfds); /* wait for an incoming message on the netlink socket */ retval = select(fd+1, &rfds, NULL, NULL, NULL); if (retval) { /* FD_ISSET(fd, &rfds) will be true */ nl_recvmsgs_default(nlh); } } nl_close(nlh); errout: return err; }