int do_xfrm_monitor(int argc, char **argv) { char *file = NULL; unsigned groups = ~((unsigned)0); /* XXX */ int lacquire=0; int lexpire=0; int laevent=0; int lpolicy=0; int lsa=0; int lreport=0; rtnl_close(&rth); while (argc > 0) { if (matches(*argv, "file") == 0) { NEXT_ARG(); file = *argv; } else if (matches(*argv, "acquire") == 0) { lacquire=1; groups = 0; } else if (matches(*argv, "expire") == 0) { lexpire=1; groups = 0; } else if (matches(*argv, "SA") == 0) { lsa=1; groups = 0; } else if (matches(*argv, "aevent") == 0) { laevent=1; groups = 0; } else if (matches(*argv, "policy") == 0) { lpolicy=1; groups = 0; } else if (matches(*argv, "report") == 0) { lreport=1; groups = 0; } else if (matches(*argv, "help") == 0) { usage(); } else { fprintf(stderr, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv); exit(-1); } argc--; argv++; } if (lacquire) groups |= nl_mgrp(XFRMNLGRP_ACQUIRE); if (lexpire) groups |= nl_mgrp(XFRMNLGRP_EXPIRE); if (lsa) groups |= nl_mgrp(XFRMNLGRP_SA); if (lpolicy) groups |= nl_mgrp(XFRMNLGRP_POLICY); if (laevent) groups |= nl_mgrp(XFRMNLGRP_AEVENTS); if (lreport) groups |= nl_mgrp(XFRMNLGRP_REPORT); if (file) { FILE *fp; fp = fopen(file, "r"); if (fp == NULL) { perror("Cannot fopen"); exit(-1); } return rtnl_from_file(fp, xfrm_accept_msg, (void*)stdout); } //ll_init_map(&rth); if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0) exit(1); if (rtnl_listen(&rth, xfrm_accept_msg, (void*)stdout) < 0) exit(2); return 0; }
int netl_listen(struct netl_handle *h, void *args) { int len, buflen, err; ssize_t status; struct nlmsghdr *hdr; struct sockaddr_nl nladdr; struct iovec iov; struct msghdr msg = { .msg_name = &nladdr, .msg_namelen = sizeof(nladdr), .msg_iov = &iov, .msg_iovlen = 1, }; char buf[8192]; struct pollfd fds[1]; if (h == NULL) return -1; iov.iov_base = buf; if (h->cb.init != NULL) { err = h->cb.init(args); if (err != 0) return err; } fds[0].events = POLLIN; fds[0].fd = h->fd; while (h->closing != 1) { int res = poll(fds, 1, NETL_POLL_TIMEOUT); if (res < 0 && errno != EINTR) { perror("error during cmdline_run poll"); return 0; } if (fds[0].revents & POLLIN) { iov.iov_len = sizeof(buf); status = recvmsg(h->fd, &msg, 0); if (status < 0) { // TODO: EINT / EAGAIN / ENOBUF should continue return -1; } if (status == 0) { // EOF return -1; } if (msg.msg_namelen != sizeof(nladdr)) { // Invalid length return -1; } for (hdr = (struct nlmsghdr *) buf; (size_t) status >= sizeof(*hdr);) { len = hdr->nlmsg_len; buflen = len - sizeof(*hdr); if (buflen < 0 || buflen > status) { // truncated return -1; } err = netl_handler(h, &nladdr, hdr, args); if (err < 0) return err; status -= NLMSG_ALIGN(len); hdr = (struct nlmsghdr *) ((char *) hdr + NLMSG_ALIGN(len)); } if (status) { // content not read return -1; } } } return 1; } static inline __u32 nl_mgrp(__u32 group) { return group ? (1 << (group - 1)) : 0; } struct netl_handle *netl_create(void) { struct netl_handle *netl_handle; int rcvbuf = 1024 * 1024 * 1024; socklen_t addr_len; unsigned subscriptions = 0; // get notified whenever interface change (new vlans / ...) subscriptions |= nl_mgrp(RTNLGRP_LINK); // get notified whenever ip changes subscriptions |= nl_mgrp(RTNLGRP_IPV4_IFADDR); subscriptions |= nl_mgrp(RTNLGRP_IPV6_IFADDR); // get notified on new routes subscriptions |= nl_mgrp(RTNLGRP_IPV4_ROUTE); subscriptions |= nl_mgrp(RTNLGRP_IPV6_ROUTE); // subscriptions |= RTNLGRP_IPV6_PREFIX; // prefix is for ipv6 RA // get notified by arp or ipv6 nd subscriptions |= nl_mgrp(RTNLGRP_NEIGH); // called whenever an iface is added/removed // subscriptions |= RTNLGRP_IPV4_NETCONF; // subscriptions |= RTNLGRP_IPV6_NETCONF; netl_handle = pktj_calloc("netl_handle", 1, sizeof(struct netl_handle), 0, SOCKET_ID_ANY); if (netl_handle == NULL) return NULL; netl_handle->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE); if (netl_handle->fd < 0) { perror("Cannot open netlink socket"); goto free_netl_handle; } if (setsockopt (netl_handle->fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)) < 0) { perror("Cannot set RCVBUF"); goto free_netl_handle; } memset(&netl_handle->local, 0, sizeof(netl_handle->local)); netl_handle->local.nl_family = AF_NETLINK; netl_handle->local.nl_groups = subscriptions; netl_handle->cb.neighbor4 = NULL; netl_handle->cb.route4 = NULL; if (bind (netl_handle->fd, (struct sockaddr *) &(netl_handle->local), sizeof(netl_handle->local)) < 0) { perror("Cannot bind netlink socket"); goto free_netl_handle; } addr_len = sizeof(netl_handle->local); if (getsockname (netl_handle->fd, (struct sockaddr *) &netl_handle->local, &addr_len) < 0) { perror("Cannot getsockname"); goto free_netl_handle; } if (addr_len != sizeof(netl_handle->local)) { perror("Wrong address length"); goto free_netl_handle; } if (netl_handle->local.nl_family != AF_NETLINK) { perror("Wrong address family"); goto free_netl_handle; } netl_handle->closing = 0; return netl_handle; free_netl_handle: pktj_free(netl_handle); return NULL; } int netl_free(struct netl_handle *h) { if (h != NULL) { if (h->fd > 0) { close(h->fd); h->fd = -1; } pktj_free(h); } return 0; }
int do_ipmonitor(int argc, char **argv) { char *file = NULL; unsigned groups = ~RTMGRP_TC; int llink=0; int laddr=0; int lroute=0; int lprefix=0; rtnl_close(&rth); ipaddr_reset_filter(1); iproute_reset_filter(); ipneigh_reset_filter(); while (argc > 0) { if (matches(*argv, "file") == 0) { NEXT_ARG(); file = *argv; } else if (matches(*argv, "link") == 0) { llink=1; groups = 0; } else if (matches(*argv, "address") == 0) { laddr=1; groups = 0; } else if (matches(*argv, "route") == 0) { lroute=1; groups = 0; } else if (matches(*argv, "prefix") == 0) { lprefix=1; groups = 0; } else if (strcmp(*argv, "all") == 0) { groups = ~RTMGRP_TC; } else if (matches(*argv, "help") == 0) { usage(); } else { fprintf(stderr, "Argument \"%s\" is unknown, try \"ip monitor help\".\n", *argv); exit(-1); } argc--; argv++; } if (llink) groups |= nl_mgrp(RTNLGRP_LINK); if (laddr) { if (!preferred_family || preferred_family == AF_INET) groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR); if (!preferred_family || preferred_family == AF_INET6) groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR); } if (lroute) { if (!preferred_family || preferred_family == AF_INET) groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE); if (!preferred_family || preferred_family == AF_INET6) groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE); } if (lprefix) { if (!preferred_family || preferred_family == AF_INET6) groups |= nl_mgrp(RTNLGRP_IPV6_PREFIX); } if (file) { FILE *fp; fp = fopen(file, "r"); if (fp == NULL) { perror("Cannot fopen"); exit(-1); } return rtnl_from_file(fp, accept_msg, stdout); } if (rtnl_open(&rth, groups) < 0) exit(1); ll_init_map(&rth); if (rtnl_listen(&rth, accept_msg, stdout) < 0) exit(2); return 0; }
int do_ipmonitor(int argc, char **argv) { char *file = NULL; unsigned int groups = 0; int llink = 0; int laddr = 0; int lroute = 0; int lmroute = 0; int lprefix = 0; int lneigh = 0; int lnetconf = 0; int lrule = 0; int lnsid = 0; int ifindex = 0; groups |= nl_mgrp(RTNLGRP_LINK); groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR); groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR); groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE); groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE); groups |= nl_mgrp(RTNLGRP_MPLS_ROUTE); groups |= nl_mgrp(RTNLGRP_IPV4_MROUTE); groups |= nl_mgrp(RTNLGRP_IPV6_MROUTE); groups |= nl_mgrp(RTNLGRP_IPV6_PREFIX); groups |= nl_mgrp(RTNLGRP_NEIGH); groups |= nl_mgrp(RTNLGRP_IPV4_NETCONF); groups |= nl_mgrp(RTNLGRP_IPV6_NETCONF); groups |= nl_mgrp(RTNLGRP_IPV4_RULE); groups |= nl_mgrp(RTNLGRP_IPV6_RULE); groups |= nl_mgrp(RTNLGRP_NSID); groups |= nl_mgrp(RTNLGRP_MPLS_NETCONF); rtnl_close(&rth); while (argc > 0) { if (matches(*argv, "file") == 0) { NEXT_ARG(); file = *argv; } else if (matches(*argv, "label") == 0) { prefix_banner = 1; } else if (matches(*argv, "link") == 0) { llink = 1; groups = 0; } else if (matches(*argv, "address") == 0) { laddr = 1; groups = 0; } else if (matches(*argv, "route") == 0) { lroute = 1; groups = 0; } else if (matches(*argv, "mroute") == 0) { lmroute = 1; groups = 0; } else if (matches(*argv, "prefix") == 0) { lprefix = 1; groups = 0; } else if (matches(*argv, "neigh") == 0) { lneigh = 1; groups = 0; } else if (matches(*argv, "netconf") == 0) { lnetconf = 1; groups = 0; } else if (matches(*argv, "rule") == 0) { lrule = 1; groups = 0; } else if (matches(*argv, "nsid") == 0) { lnsid = 1; groups = 0; } else if (strcmp(*argv, "all") == 0) { prefix_banner = 1; } else if (matches(*argv, "all-nsid") == 0) { listen_all_nsid = 1; } else if (matches(*argv, "help") == 0) { usage(); } else if (strcmp(*argv, "dev") == 0) { NEXT_ARG(); ifindex = ll_name_to_index(*argv); if (!ifindex) invarg("Device does not exist\n", *argv); } else { fprintf(stderr, "Argument \"%s\" is unknown, try \"ip monitor help\".\n", *argv); exit(-1); } argc--; argv++; } ipaddr_reset_filter(1, ifindex); iproute_reset_filter(ifindex); ipmroute_reset_filter(ifindex); ipneigh_reset_filter(ifindex); ipnetconf_reset_filter(ifindex); if (llink) groups |= nl_mgrp(RTNLGRP_LINK); if (laddr) { if (!preferred_family || preferred_family == AF_INET) groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR); if (!preferred_family || preferred_family == AF_INET6) groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR); } if (lroute) { if (!preferred_family || preferred_family == AF_INET) groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE); if (!preferred_family || preferred_family == AF_INET6) groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE); if (!preferred_family || preferred_family == AF_MPLS) groups |= nl_mgrp(RTNLGRP_MPLS_ROUTE); } if (lmroute) { if (!preferred_family || preferred_family == AF_INET) groups |= nl_mgrp(RTNLGRP_IPV4_MROUTE); if (!preferred_family || preferred_family == AF_INET6) groups |= nl_mgrp(RTNLGRP_IPV6_MROUTE); } if (lprefix) { if (!preferred_family || preferred_family == AF_INET6) groups |= nl_mgrp(RTNLGRP_IPV6_PREFIX); } if (lneigh) { groups |= nl_mgrp(RTNLGRP_NEIGH); } if (lnetconf) { if (!preferred_family || preferred_family == AF_INET) groups |= nl_mgrp(RTNLGRP_IPV4_NETCONF); if (!preferred_family || preferred_family == AF_INET6) groups |= nl_mgrp(RTNLGRP_IPV6_NETCONF); if (!preferred_family || preferred_family == AF_MPLS) groups |= nl_mgrp(RTNLGRP_MPLS_NETCONF); } if (lrule) { if (!preferred_family || preferred_family == AF_INET) groups |= nl_mgrp(RTNLGRP_IPV4_RULE); if (!preferred_family || preferred_family == AF_INET6) groups |= nl_mgrp(RTNLGRP_IPV6_RULE); } if (lnsid) { groups |= nl_mgrp(RTNLGRP_NSID); } if (file) { FILE *fp; int err; fp = fopen(file, "r"); if (fp == NULL) { perror("Cannot fopen"); exit(-1); } err = rtnl_from_file(fp, accept_msg, stdout); fclose(fp); return err; } if (rtnl_open(&rth, groups) < 0) exit(1); if (listen_all_nsid && rtnl_listen_all_nsid(&rth) < 0) exit(1); ll_init_map(&rth); netns_nsid_socket_init(); netns_map_init(); if (rtnl_listen(&rth, accept_msg, stdout) < 0) exit(2); return 0; }
int main(int argc, char **argv) { FILE *fp; struct rtnl_handle rth; int family = AF_UNSPEC; unsigned groups = ~0U; int llink = 0; int laddr = 0; int lroute = 0; char *file = NULL; while (argc > 1) { if (matches(argv[1], "-family") == 0) { argc--; argv++; if (argc <= 1) usage(); if (strcmp(argv[1], "inet") == 0) family = AF_INET; else if (strcmp(argv[1], "inet6") == 0) family = AF_INET6; else if (strcmp(argv[1], "link") == 0) family = AF_INET6; else if (strcmp(argv[1], "help") == 0) usage(); else { fprintf(stderr, "Protocol ID \"%s\" is unknown, try \"rtmon help\".\n", argv[1]); exit(-1); } } else if (strcmp(argv[1], "-4") == 0) { family = AF_INET; } else if (strcmp(argv[1], "-6") == 0) { family = AF_INET6; } else if (strcmp(argv[1], "-0") == 0) { family = AF_PACKET; } else if (matches(argv[1], "-Version") == 0) { printf("rtmon utility, iproute2-ss%s\n", SNAPSHOT); exit(0); } else if (matches(argv[1], "file") == 0) { argc--; argv++; if (argc <= 1) usage(); file = argv[1]; } else if (matches(argv[1], "link") == 0) { llink=1; groups = 0; } else if (matches(argv[1], "address") == 0) { laddr=1; groups = 0; } else if (matches(argv[1], "route") == 0) { lroute=1; groups = 0; } else if (strcmp(argv[1], "all") == 0) { groups = ~0U; } else if (matches(argv[1], "help") == 0) { usage(); } else { fprintf(stderr, "Argument \"%s\" is unknown, try \"rtmon help\".\n", argv[1]); exit(-1); } argc--; argv++; } if (file == NULL) { fprintf(stderr, "Not enough information: argument \"file\" is required\n"); exit(-1); } if (llink) groups |= nl_mgrp(RTNLGRP_LINK); if (laddr) { if (!family || family == AF_INET) groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR); if (!family || family == AF_INET6) groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR); } if (lroute) { if (!family || family == AF_INET) groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE); if (!family || family == AF_INET6) groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE); } fp = fopen(file, "w"); if (fp == NULL) { perror("Cannot fopen"); exit(-1); } if (rtnl_open(&rth, groups) < 0) exit(1); if (rtnl_wilddump_request(&rth, AF_UNSPEC, RTM_GETLINK) < 0) { perror("Cannot send dump request"); exit(1); } write_stamp(fp); if (rtnl_dump_filter(&rth, dump_msg, fp) < 0) { fprintf(stderr, "Dump terminated\n"); return 1; } init_phase = 0; if (rtnl_listen(&rth, dump_msg, (void*)fp) < 0) exit(2); exit(0); }
int do_monitor(int argc, char **argv) { char *file = NULL; unsigned groups = ~RTMGRP_TC; int llink=0; int lneigh=0; int lmdb=0; rtnl_close(&rth); while (argc > 0) { if (matches(*argv, "file") == 0) { NEXT_ARG(); file = *argv; } else if (matches(*argv, "link") == 0) { llink=1; groups = 0; } else if (matches(*argv, "fdb") == 0) { lneigh = 1; groups = 0; } else if (matches(*argv, "mdb") == 0) { lmdb = 1; groups = 0; } else if (strcmp(*argv, "all") == 0) { groups = ~RTMGRP_TC; prefix_banner=1; } else if (matches(*argv, "help") == 0) { usage(); } else { fprintf(stderr, "Argument \"%s\" is unknown, try \"bridge monitor help\".\n", *argv); exit(-1); } argc--; argv++; } if (llink) groups |= nl_mgrp(RTNLGRP_LINK); if (lneigh) { groups |= nl_mgrp(RTNLGRP_NEIGH); } if (lmdb) { groups |= nl_mgrp(RTNLGRP_MDB); } if (file) { FILE *fp; int err; fp = fopen(file, "r"); if (fp == NULL) { perror("Cannot fopen"); exit(-1); } err = rtnl_from_file(fp, accept_msg, stdout); fclose(fp); return err; } if (rtnl_open(&rth, groups) < 0) exit(1); ll_init_map(&rth); if (rtnl_listen(&rth, accept_msg, stdout) < 0) exit(2); return 0; }