Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * ipnetconf.c "ip netconf". |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Nicolas Dichtel, <nicolas.dichtel@6wind.com> |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <unistd.h> |
| 16 | #include <syslog.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <string.h> |
| 19 | #include <sys/time.h> |
| 20 | #include <sys/socket.h> |
| 21 | #include <netinet/in.h> |
| 22 | |
| 23 | #include "rt_names.h" |
| 24 | #include "utils.h" |
| 25 | #include "ip_common.h" |
| 26 | |
Stephen Hemminger | 52a4474 | 2016-03-21 12:13:57 -0700 | [diff] [blame] | 27 | static struct { |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 28 | int family; |
| 29 | int ifindex; |
| 30 | } filter; |
| 31 | |
| 32 | static void usage(void) __attribute__((noreturn)); |
| 33 | |
| 34 | static void usage(void) |
| 35 | { |
| 36 | fprintf(stderr, "Usage: ip netconf show [ dev STRING ]\n"); |
| 37 | exit(-1); |
| 38 | } |
| 39 | |
Zhang Shengju | 95c9d0d | 2016-03-21 12:16:25 -0700 | [diff] [blame] | 40 | static void print_onoff(FILE *f, const char *flag, __u32 val) |
| 41 | { |
| 42 | fprintf(f, "%s %s ", flag, val ? "on" : "off"); |
| 43 | } |
| 44 | |
Stephen Hemminger | 52a4474 | 2016-03-21 12:13:57 -0700 | [diff] [blame] | 45 | static struct rtattr *netconf_rta(struct netconfmsg *ncm) |
| 46 | { |
| 47 | return (struct rtattr *)((char *)ncm |
| 48 | + NLMSG_ALIGN(sizeof(struct netconfmsg))); |
| 49 | } |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 50 | |
Nicolas Dichtel | 0628cdd | 2015-05-20 16:19:58 +0200 | [diff] [blame] | 51 | int print_netconf(const struct sockaddr_nl *who, struct rtnl_ctrl_data *ctrl, |
| 52 | struct nlmsghdr *n, void *arg) |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 53 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 54 | FILE *fp = (FILE *)arg; |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 55 | struct netconfmsg *ncm = NLMSG_DATA(n); |
| 56 | int len = n->nlmsg_len; |
| 57 | struct rtattr *tb[NETCONFA_MAX+1]; |
| 58 | |
| 59 | if (n->nlmsg_type == NLMSG_ERROR) |
| 60 | return -1; |
| 61 | if (n->nlmsg_type != RTM_NEWNETCONF) { |
| 62 | fprintf(stderr, "Not RTM_NEWNETCONF: %08x %08x %08x\n", |
| 63 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
| 64 | |
| 65 | return -1; |
| 66 | } |
| 67 | len -= NLMSG_SPACE(sizeof(*ncm)); |
| 68 | if (len < 0) { |
| 69 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | if (filter.family && filter.family != ncm->ncm_family) |
| 74 | return 0; |
| 75 | |
Stephen Hemminger | 52a4474 | 2016-03-21 12:13:57 -0700 | [diff] [blame] | 76 | parse_rtattr(tb, NETCONFA_MAX, netconf_rta(ncm), |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 77 | NLMSG_PAYLOAD(n, sizeof(*ncm))); |
| 78 | |
| 79 | switch (ncm->ncm_family) { |
| 80 | case AF_INET: |
| 81 | fprintf(fp, "ipv4 "); |
| 82 | break; |
| 83 | case AF_INET6: |
| 84 | fprintf(fp, "ipv6 "); |
| 85 | break; |
| 86 | default: |
| 87 | fprintf(fp, "unknown "); |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | if (tb[NETCONFA_IFINDEX]) { |
Zhang Shengju | 95c9d0d | 2016-03-21 12:16:25 -0700 | [diff] [blame] | 92 | int *ifindex = (int *)rta_getattr_str(tb[NETCONFA_IFINDEX]); |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 93 | |
| 94 | switch (*ifindex) { |
| 95 | case NETCONFA_IFINDEX_ALL: |
| 96 | fprintf(fp, "all "); |
| 97 | break; |
| 98 | case NETCONFA_IFINDEX_DEFAULT: |
| 99 | fprintf(fp, "default "); |
| 100 | break; |
| 101 | default: |
| 102 | fprintf(fp, "dev %s ", ll_index_to_name(*ifindex)); |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (tb[NETCONFA_FORWARDING]) |
Zhang Shengju | 95c9d0d | 2016-03-21 12:16:25 -0700 | [diff] [blame] | 108 | print_onoff(fp, "forwarding", |
| 109 | rta_getattr_u32(tb[NETCONFA_FORWARDING])); |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 110 | if (tb[NETCONFA_RP_FILTER]) { |
Zhang Shengju | 95c9d0d | 2016-03-21 12:16:25 -0700 | [diff] [blame] | 111 | __u32 rp_filter = rta_getattr_u32(tb[NETCONFA_RP_FILTER]); |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 112 | |
| 113 | if (rp_filter == 0) |
| 114 | fprintf(fp, "rp_filter off "); |
| 115 | else if (rp_filter == 1) |
| 116 | fprintf(fp, "rp_filter strict "); |
| 117 | else if (rp_filter == 2) |
| 118 | fprintf(fp, "rp_filter loose "); |
| 119 | else |
| 120 | fprintf(fp, "rp_filter unknown mode "); |
| 121 | } |
| 122 | if (tb[NETCONFA_MC_FORWARDING]) |
Zhang Shengju | 95c9d0d | 2016-03-21 12:16:25 -0700 | [diff] [blame] | 123 | print_onoff(fp, "mc_forwarding", |
| 124 | rta_getattr_u32(tb[NETCONFA_MC_FORWARDING])); |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 125 | |
Stephen Hemminger | 29cc864 | 2013-12-17 22:26:56 -0800 | [diff] [blame] | 126 | if (tb[NETCONFA_PROXY_NEIGH]) |
Zhang Shengju | 95c9d0d | 2016-03-21 12:16:25 -0700 | [diff] [blame] | 127 | print_onoff(fp, "proxy_neigh", |
| 128 | rta_getattr_u32(tb[NETCONFA_PROXY_NEIGH])); |
| 129 | |
| 130 | if (tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN]) |
| 131 | print_onoff(fp, "ignore_routes_with_linkdown", |
| 132 | rta_getattr_u32(tb[NETCONFA_IGNORE_ROUTES_WITH_LINKDOWN])); |
Stephen Hemminger | 29cc864 | 2013-12-17 22:26:56 -0800 | [diff] [blame] | 133 | |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 134 | fprintf(fp, "\n"); |
| 135 | fflush(fp); |
| 136 | return 0; |
| 137 | } |
| 138 | |
Nicolas Dichtel | 0628cdd | 2015-05-20 16:19:58 +0200 | [diff] [blame] | 139 | static int print_netconf2(const struct sockaddr_nl *who, |
| 140 | struct nlmsghdr *n, void *arg) |
| 141 | { |
| 142 | return print_netconf(who, NULL, n, arg); |
| 143 | } |
| 144 | |
vadimk | 093b764 | 2014-10-20 12:25:17 +0300 | [diff] [blame] | 145 | void ipnetconf_reset_filter(int ifindex) |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 146 | { |
| 147 | memset(&filter, 0, sizeof(filter)); |
vadimk | 093b764 | 2014-10-20 12:25:17 +0300 | [diff] [blame] | 148 | filter.ifindex = ifindex; |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 151 | static int do_show(int argc, char **argv) |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 152 | { |
| 153 | struct { |
| 154 | struct nlmsghdr n; |
| 155 | struct netconfmsg ncm; |
| 156 | char buf[1024]; |
| 157 | } req; |
| 158 | |
vadimk | 093b764 | 2014-10-20 12:25:17 +0300 | [diff] [blame] | 159 | ipnetconf_reset_filter(0); |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 160 | filter.family = preferred_family; |
| 161 | if (filter.family == AF_UNSPEC) |
| 162 | filter.family = AF_INET; |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 163 | |
| 164 | while (argc > 0) { |
| 165 | if (strcmp(*argv, "dev") == 0) { |
| 166 | NEXT_ARG(); |
| 167 | filter.ifindex = ll_name_to_index(*argv); |
| 168 | if (filter.ifindex <= 0) { |
| 169 | fprintf(stderr, "Device \"%s\" does not exist.\n", |
| 170 | *argv); |
| 171 | return -1; |
| 172 | } |
| 173 | } |
| 174 | argv++; argc--; |
| 175 | } |
| 176 | |
| 177 | ll_init_map(&rth); |
Nicolas Dichtel | f7431e2 | 2013-05-17 01:47:33 -0700 | [diff] [blame] | 178 | if (filter.ifindex) { |
| 179 | memset(&req, 0, sizeof(req)); |
| 180 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct netconfmsg)); |
| 181 | req.n.nlmsg_flags = NLM_F_REQUEST|NLM_F_ACK; |
| 182 | req.n.nlmsg_type = RTM_GETNETCONF; |
| 183 | req.ncm.ncm_family = filter.family; |
| 184 | if (filter.ifindex) |
| 185 | addattr_l(&req.n, sizeof(req), NETCONFA_IFINDEX, |
| 186 | &filter.ifindex, sizeof(filter.ifindex)); |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 187 | |
Stephen Hemminger | d2468da | 2013-12-20 08:15:02 -0800 | [diff] [blame] | 188 | if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) { |
| 189 | perror("Can not send request"); |
| 190 | exit(1); |
| 191 | } |
Nicolas Dichtel | f7431e2 | 2013-05-17 01:47:33 -0700 | [diff] [blame] | 192 | rtnl_listen(&rth, print_netconf, stdout); |
| 193 | } else { |
| 194 | dump: |
| 195 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETNETCONF) < 0) { |
| 196 | perror("Cannot send dump request"); |
| 197 | exit(1); |
| 198 | } |
Nicolas Dichtel | 0628cdd | 2015-05-20 16:19:58 +0200 | [diff] [blame] | 199 | if (rtnl_dump_filter(&rth, print_netconf2, stdout) < 0) { |
Nicolas Dichtel | f7431e2 | 2013-05-17 01:47:33 -0700 | [diff] [blame] | 200 | fprintf(stderr, "Dump terminated\n"); |
| 201 | exit(1); |
| 202 | } |
| 203 | if (preferred_family == AF_UNSPEC) { |
| 204 | preferred_family = AF_INET6; |
| 205 | filter.family = AF_INET6; |
| 206 | goto dump; |
| 207 | } |
| 208 | } |
Nicolas Dichtel | 4852ba7 | 2012-12-12 09:05:51 -0800 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | int do_ipnetconf(int argc, char **argv) |
| 213 | { |
| 214 | if (argc > 0) { |
| 215 | if (matches(*argv, "show") == 0 || |
| 216 | matches(*argv, "lst") == 0 || |
| 217 | matches(*argv, "list") == 0) |
| 218 | return do_show(argc-1, argv+1); |
| 219 | if (matches(*argv, "help") == 0) |
| 220 | usage(); |
| 221 | } else |
| 222 | return do_show(0, NULL); |
| 223 | |
| 224 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip netconf help\".\n", *argv); |
| 225 | exit(-1); |
| 226 | } |