]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - ip/ipmonitor.c
xfrm: also check for ipv6 state in xfrm_state_keep
[mirror_iproute2.git] / ip / ipmonitor.c
index 4cc75f4ce3f127520c7fbb3458438db710dedebb..685be52cfe64c527840f23a663db08afa9debb97 100644 (file)
@@ -13,7 +13,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <syslog.h>
 #include <fcntl.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include "ip_common.h"
 
 static void usage(void) __attribute__((noreturn));
-int prefix_banner;
+static int prefix_banner;
+int listen_all_nsid;
 
 static void usage(void)
 {
-       fprintf(stderr, "Usage: ip monitor [ all | LISTofOBJECTS ] [ FILE ]"
-                       "[ label ] [dev DEVICE]\n");
-       fprintf(stderr, "LISTofOBJECTS := link | address | route | mroute | prefix |\n");
-       fprintf(stderr, "                 neigh | netconf\n");
-       fprintf(stderr, "FILE := file FILENAME\n");
+       fprintf(stderr,
+               "Usage: ip monitor [ all | LISTofOBJECTS ] [ FILE ] [ label ] [all-nsid] [dev DEVICE]\n"
+               "LISTofOBJECTS := link | address | route | mroute | prefix |\n"
+               "                neigh | netconf | rule | nsid\n"
+               "FILE := file FILENAME\n");
        exit(-1);
 }
 
-static int accept_msg(const struct sockaddr_nl *who,
+static void print_headers(FILE *fp, char *label, struct rtnl_ctrl_data *ctrl)
+{
+       if (timestamp)
+               print_timestamp(fp);
+
+       if (listen_all_nsid) {
+               if (ctrl == NULL || ctrl->nsid < 0)
+                       fprintf(fp, "[nsid current]");
+               else
+                       fprintf(fp, "[nsid %d]", ctrl->nsid);
+       }
+
+       if (prefix_banner)
+               fprintf(fp, "%s", label);
+}
+
+static int accept_msg(struct rtnl_ctrl_data *ctrl,
                      struct nlmsghdr *n, void *arg)
 {
-       FILE *fp = (FILE*)arg;
+       FILE *fp = (FILE *)arg;
 
-       if (n->nlmsg_type == RTM_NEWROUTE || n->nlmsg_type == RTM_DELROUTE) {
+       switch (n->nlmsg_type) {
+       case RTM_NEWROUTE:
+       case RTM_DELROUTE: {
                struct rtmsg *r = NLMSG_DATA(n);
                int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
 
@@ -54,47 +72,46 @@ static int accept_msg(const struct sockaddr_nl *who,
                if (r->rtm_flags & RTM_F_CLONED)
                        return 0;
 
-               if (timestamp)
-                       print_timestamp(fp);
-
                if (r->rtm_family == RTNL_FAMILY_IPMR ||
                    r->rtm_family == RTNL_FAMILY_IP6MR) {
-                       if (prefix_banner)
-                               fprintf(fp, "[MROUTE]");
-                       print_mroute(who, n, arg);
+                       print_headers(fp, "[MROUTE]", ctrl);
+                       print_mroute(n, arg);
                        return 0;
                } else {
-                       if (prefix_banner)
-                               fprintf(fp, "[ROUTE]");
-                       print_route(who, n, arg);
+                       print_headers(fp, "[ROUTE]", ctrl);
+                       print_route(n, arg);
                        return 0;
                }
        }
 
-       if (timestamp)
-               print_timestamp(fp);
+       case RTM_NEWNEXTHOP:
+       case RTM_DELNEXTHOP:
+               print_headers(fp, "[NEXTHOP]", ctrl);
+               print_nexthop(n, arg);
+               return 0;
 
-       if (n->nlmsg_type == RTM_NEWLINK || n->nlmsg_type == RTM_DELLINK) {
-               ll_remember_index(who, n, NULL);
-               if (prefix_banner)
-                       fprintf(fp, "[LINK]");
-               print_linkinfo(who, n, arg);
+       case RTM_NEWLINK:
+       case RTM_DELLINK:
+               ll_remember_index(n, NULL);
+               print_headers(fp, "[LINK]", ctrl);
+               print_linkinfo(n, arg);
                return 0;
-       }
-       if (n->nlmsg_type == RTM_NEWADDR || n->nlmsg_type == RTM_DELADDR) {
-               if (prefix_banner)
-                       fprintf(fp, "[ADDR]");
-               print_addrinfo(who, n, arg);
+
+       case RTM_NEWADDR:
+       case RTM_DELADDR:
+               print_headers(fp, "[ADDR]", ctrl);
+               print_addrinfo(n, arg);
                return 0;
-       }
-       if (n->nlmsg_type == RTM_NEWADDRLABEL || n->nlmsg_type == RTM_DELADDRLABEL) {
-               if (prefix_banner)
-                       fprintf(fp, "[ADDRLABEL]");
-               print_addrlabel(who, n, arg);
+
+       case RTM_NEWADDRLABEL:
+       case RTM_DELADDRLABEL:
+               print_headers(fp, "[ADDRLABEL]", ctrl);
+               print_addrlabel(n, arg);
                return 0;
-       }
-       if (n->nlmsg_type == RTM_NEWNEIGH || n->nlmsg_type == RTM_DELNEIGH ||
-           n->nlmsg_type == RTM_GETNEIGH) {
+
+       case RTM_NEWNEIGH:
+       case RTM_DELNEIGH:
+       case RTM_GETNEIGH:
                if (preferred_family) {
                        struct ndmsg *r = NLMSG_DATA(n);
 
@@ -102,50 +119,46 @@ static int accept_msg(const struct sockaddr_nl *who,
                                return 0;
                }
 
-               if (prefix_banner)
-                       fprintf(fp, "[NEIGH]");
-               print_neigh(who, n, arg);
+               print_headers(fp, "[NEIGH]", ctrl);
+               print_neigh(n, arg);
                return 0;
-       }
-       if (n->nlmsg_type == RTM_NEWPREFIX) {
-               if (prefix_banner)
-                       fprintf(fp, "[PREFIX]");
-               print_prefix(who, n, arg);
+
+       case RTM_NEWPREFIX:
+               print_headers(fp, "[PREFIX]", ctrl);
+               print_prefix(n, arg);
                return 0;
-       }
-       if (n->nlmsg_type == RTM_NEWRULE || n->nlmsg_type == RTM_DELRULE) {
-               if (prefix_banner)
-                       fprintf(fp, "[RULE]");
-               print_rule(who, n, arg);
+
+       case RTM_NEWRULE:
+       case RTM_DELRULE:
+               print_headers(fp, "[RULE]", ctrl);
+               print_rule(n, arg);
                return 0;
-       }
-       if (n->nlmsg_type == RTM_NEWNETCONF) {
-               if (prefix_banner)
-                       fprintf(fp, "[NETCONF]");
-               print_netconf(who, n, arg);
+
+       case NLMSG_TSTAMP:
+               print_nlmsg_timestamp(fp, n);
                return 0;
-       }
-       if (n->nlmsg_type == 15) {
-               char *tstr;
-               time_t secs = ((__u32*)NLMSG_DATA(n))[0];
-               long usecs = ((__u32*)NLMSG_DATA(n))[1];
-               tstr = asctime(localtime(&secs));
-               tstr[strlen(tstr)-1] = 0;
-               fprintf(fp, "Timestamp: %s %lu us\n", tstr, usecs);
+
+       case RTM_NEWNETCONF:
+       case RTM_DELNETCONF:
+               print_headers(fp, "[NETCONF]", ctrl);
+               print_netconf(ctrl, n, arg);
                return 0;
-       }
-       if (n->nlmsg_type == RTM_NEWQDISC ||
-           n->nlmsg_type == RTM_DELQDISC ||
-           n->nlmsg_type == RTM_NEWTCLASS ||
-           n->nlmsg_type == RTM_DELTCLASS ||
-           n->nlmsg_type == RTM_NEWTFILTER ||
-           n->nlmsg_type == RTM_DELTFILTER ||
-           n->nlmsg_type == RTM_NEWNDUSEROPT)
+
+       case RTM_DELNSID:
+       case RTM_NEWNSID:
+               print_headers(fp, "[NSID]", ctrl);
+               print_nsid(n, arg);
                return 0;
-       if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
-           n->nlmsg_type != NLMSG_DONE) {
-               fprintf(fp, "Unknown message: type=0x%08x(%d) flags=0x%08x(%d)"
-                       "len=0x%08x(%d)\n", n->nlmsg_type, n->nlmsg_type,
+
+       case NLMSG_ERROR:
+       case NLMSG_NOOP:
+       case NLMSG_DONE:
+               break;  /* ignore */
+
+       default:
+               fprintf(stderr,
+                       "Unknown message: type=0x%08x(%d) flags=0x%08x(%d) len=0x%08x(%d)\n",
+                       n->nlmsg_type, n->nlmsg_type,
                        n->nlmsg_flags, n->nlmsg_flags, n->nlmsg_len,
                        n->nlmsg_len);
        }
@@ -154,16 +167,36 @@ static int accept_msg(const struct sockaddr_nl *who,
 
 int do_ipmonitor(int argc, char **argv)
 {
+       int lnexthop = 0, nh_set = 1;
        char *file = NULL;
-       unsigned groups = ~RTMGRP_TC;
-       int llink=0;
-       int laddr=0;
-       int lroute=0;
-       int lmroute=0;
-       int lprefix=0;
-       int lneigh=0;
-       int lnetconf=0;
-       int ifindex=0;
+       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);
 
@@ -174,29 +207,48 @@ int do_ipmonitor(int argc, char **argv)
                } else if (matches(*argv, "label") == 0) {
                        prefix_banner = 1;
                } else if (matches(*argv, "link") == 0) {
-                       llink=1;
+                       llink = 1;
                        groups = 0;
+                       nh_set = 0;
                } else if (matches(*argv, "address") == 0) {
-                       laddr=1;
+                       laddr = 1;
                        groups = 0;
+                       nh_set = 0;
                } else if (matches(*argv, "route") == 0) {
-                       lroute=1;
+                       lroute = 1;
                        groups = 0;
+                       nh_set = 0;
                } else if (matches(*argv, "mroute") == 0) {
-                       lmroute=1;
+                       lmroute = 1;
                        groups = 0;
+                       nh_set = 0;
                } else if (matches(*argv, "prefix") == 0) {
-                       lprefix=1;
+                       lprefix = 1;
                        groups = 0;
+                       nh_set = 0;
                } else if (matches(*argv, "neigh") == 0) {
                        lneigh = 1;
                        groups = 0;
+                       nh_set = 0;
                } else if (matches(*argv, "netconf") == 0) {
                        lnetconf = 1;
                        groups = 0;
+                       nh_set = 0;
+               } else if (matches(*argv, "rule") == 0) {
+                       lrule = 1;
+                       groups = 0;
+                       nh_set = 0;
+               } else if (matches(*argv, "nsid") == 0) {
+                       lnsid = 1;
+                       groups = 0;
+                       nh_set = 0;
+               } else if (matches(*argv, "nexthop") == 0) {
+                       lnexthop = 1;
+                       groups = 0;
                } else if (strcmp(*argv, "all") == 0) {
-                       groups = ~RTMGRP_TC;
-                       prefix_banner=1;
+                       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) {
@@ -231,6 +283,8 @@ int do_ipmonitor(int argc, char **argv)
                        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)
@@ -250,20 +304,49 @@ int do_ipmonitor(int argc, char **argv)
                        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 (nh_set)
+               lnexthop = 1;
+
        if (file) {
                FILE *fp;
+               int err;
+
                fp = fopen(file, "r");
                if (fp == NULL) {
                        perror("Cannot fopen");
                        exit(-1);
                }
-               return rtnl_from_file(fp, accept_msg, stdout);
+               err = rtnl_from_file(fp, accept_msg, stdout);
+               fclose(fp);
+               return err;
        }
 
        if (rtnl_open(&rth, groups) < 0)
                exit(1);
+
+       if (lnexthop && rtnl_add_nl_group(&rth, RTNLGRP_NEXTHOP) < 0) {
+               fprintf(stderr, "Failed to add nexthop group to list\n");
+               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);