linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 1 | /* $USAGI: $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (C)2005 USAGI/WIDE Project |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 5 | * |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 10 | * |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 15 | * |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License |
Stephen Hemminger | 4d98ab0 | 2013-12-06 15:05:07 -0800 | [diff] [blame] | 17 | * along with this program; if not, see <http://www.gnu.org/licenses>. |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 18 | */ |
| 19 | /* |
| 20 | * based on ipmonitor.c |
| 21 | */ |
| 22 | /* |
| 23 | * Authors: |
| 24 | * Masahide NAKAMURA @USAGI |
| 25 | */ |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
Stephen Hemminger | 9339077 | 2015-04-10 13:17:54 -0700 | [diff] [blame] | 30 | #include <netinet/in.h> |
Stephen Hemminger | 9339077 | 2015-04-10 13:17:54 -0700 | [diff] [blame] | 31 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 32 | #include "utils.h" |
| 33 | #include "xfrm.h" |
| 34 | #include "ip_common.h" |
| 35 | |
| 36 | static void usage(void) __attribute__((noreturn)); |
Nicolas Dichtel | b6ec53e | 2015-05-20 16:20:01 +0200 | [diff] [blame] | 37 | int listen_all_nsid; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 38 | |
| 39 | static void usage(void) |
| 40 | { |
Nicolas Dichtel | b6ec53e | 2015-05-20 16:20:01 +0200 | [diff] [blame] | 41 | fprintf(stderr, "Usage: ip xfrm monitor [all-nsid] [ all | OBJECTS | help ]\n"); |
Vadim Kochan | c16298b | 2015-02-14 20:07:44 +0200 | [diff] [blame] | 42 | fprintf(stderr, "OBJECTS := { acquire | expire | SA | aevent | policy | report }\n"); |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 43 | exit(-1); |
| 44 | } |
| 45 | |
| 46 | static int xfrm_acquire_print(const struct sockaddr_nl *who, |
| 47 | struct nlmsghdr *n, void *arg) |
| 48 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 49 | FILE *fp = (FILE *)arg; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 50 | struct xfrm_user_acquire *xacq = NLMSG_DATA(n); |
| 51 | int len = n->nlmsg_len; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 52 | struct rtattr *tb[XFRMA_MAX+1]; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 53 | __u16 family; |
| 54 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 55 | len -= NLMSG_LENGTH(sizeof(*xacq)); |
| 56 | if (len < 0) { |
| 57 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | parse_rtattr(tb, XFRMA_MAX, XFRMACQ_RTA(xacq), len); |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 62 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 63 | family = xacq->sel.family; |
| 64 | if (family == AF_UNSPEC) |
| 65 | family = xacq->policy.sel.family; |
| 66 | if (family == AF_UNSPEC) |
| 67 | family = preferred_family; |
| 68 | |
| 69 | fprintf(fp, "acquire "); |
| 70 | |
| 71 | fprintf(fp, "proto %s ", strxf_xfrmproto(xacq->id.proto)); |
| 72 | if (show_stats > 0 || xacq->id.spi) { |
| 73 | __u32 spi = ntohl(xacq->id.spi); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 74 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 75 | fprintf(fp, "spi 0x%08x", spi); |
| 76 | if (show_stats > 0) |
| 77 | fprintf(fp, "(%u)", spi); |
| 78 | fprintf(fp, " "); |
| 79 | } |
| 80 | fprintf(fp, "%s", _SL_); |
| 81 | |
| 82 | xfrm_selector_print(&xacq->sel, family, fp, " sel "); |
| 83 | |
| 84 | xfrm_policy_info_print(&xacq->policy, tb, fp, " ", " policy "); |
| 85 | |
| 86 | if (show_stats > 0) |
| 87 | fprintf(fp, " seq 0x%08u ", xacq->seq); |
| 88 | if (show_stats > 0) { |
| 89 | fprintf(fp, "%s-mask %s ", |
| 90 | strxf_algotype(XFRMA_ALG_CRYPT), |
| 91 | strxf_mask32(xacq->ealgos)); |
| 92 | fprintf(fp, "%s-mask %s ", |
| 93 | strxf_algotype(XFRMA_ALG_AUTH), |
| 94 | strxf_mask32(xacq->aalgos)); |
| 95 | fprintf(fp, "%s-mask %s", |
| 96 | strxf_algotype(XFRMA_ALG_COMP), |
| 97 | strxf_mask32(xacq->calgos)); |
| 98 | } |
| 99 | fprintf(fp, "%s", _SL_); |
| 100 | |
| 101 | if (oneline) |
| 102 | fprintf(fp, "\n"); |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 103 | fflush(fp); |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 108 | static int xfrm_state_flush_print(const struct sockaddr_nl *who, |
| 109 | struct nlmsghdr *n, void *arg) |
| 110 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 111 | FILE *fp = (FILE *)arg; |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 112 | struct xfrm_usersa_flush *xsf = NLMSG_DATA(n); |
| 113 | int len = n->nlmsg_len; |
| 114 | const char *str; |
| 115 | |
| 116 | len -= NLMSG_SPACE(sizeof(*xsf)); |
| 117 | if (len < 0) { |
| 118 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | fprintf(fp, "Flushed state "); |
| 123 | |
| 124 | str = strxf_xfrmproto(xsf->proto); |
| 125 | if (str) |
| 126 | fprintf(fp, "proto %s", str); |
| 127 | else |
| 128 | fprintf(fp, "proto %u", xsf->proto); |
| 129 | fprintf(fp, "%s", _SL_); |
| 130 | |
| 131 | if (oneline) |
| 132 | fprintf(fp, "\n"); |
| 133 | fflush(fp); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int xfrm_policy_flush_print(const struct sockaddr_nl *who, |
| 139 | struct nlmsghdr *n, void *arg) |
| 140 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 141 | struct rtattr *tb[XFRMA_MAX+1]; |
| 142 | FILE *fp = (FILE *)arg; |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 143 | int len = n->nlmsg_len; |
| 144 | |
| 145 | len -= NLMSG_SPACE(0); |
| 146 | if (len < 0) { |
| 147 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | fprintf(fp, "Flushed policy "); |
| 152 | |
| 153 | parse_rtattr(tb, XFRMA_MAX, NLMSG_DATA(n), len); |
| 154 | |
| 155 | if (tb[XFRMA_POLICY_TYPE]) { |
| 156 | struct xfrm_userpolicy_type *upt; |
| 157 | |
| 158 | fprintf(fp, "ptype "); |
| 159 | |
| 160 | if (RTA_PAYLOAD(tb[XFRMA_POLICY_TYPE]) < sizeof(*upt)) |
| 161 | fprintf(fp, "(ERROR truncated)"); |
| 162 | |
| 163 | upt = (struct xfrm_userpolicy_type *)RTA_DATA(tb[XFRMA_POLICY_TYPE]); |
| 164 | fprintf(fp, "%s ", strxf_ptype(upt->type)); |
| 165 | } |
| 166 | |
| 167 | fprintf(fp, "%s", _SL_); |
| 168 | |
| 169 | if (oneline) |
| 170 | fprintf(fp, "\n"); |
| 171 | fflush(fp); |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
Masahide NAKAMURA | c54f31e | 2006-12-05 19:16:05 +0900 | [diff] [blame] | 176 | static int xfrm_report_print(const struct sockaddr_nl *who, |
| 177 | struct nlmsghdr *n, void *arg) |
| 178 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 179 | FILE *fp = (FILE *)arg; |
Masahide NAKAMURA | c54f31e | 2006-12-05 19:16:05 +0900 | [diff] [blame] | 180 | struct xfrm_user_report *xrep = NLMSG_DATA(n); |
| 181 | int len = n->nlmsg_len; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 182 | struct rtattr *tb[XFRMA_MAX+1]; |
Masahide NAKAMURA | c54f31e | 2006-12-05 19:16:05 +0900 | [diff] [blame] | 183 | __u16 family; |
| 184 | |
Masahide NAKAMURA | c54f31e | 2006-12-05 19:16:05 +0900 | [diff] [blame] | 185 | len -= NLMSG_LENGTH(sizeof(*xrep)); |
| 186 | if (len < 0) { |
| 187 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | family = xrep->sel.family; |
| 192 | if (family == AF_UNSPEC) |
| 193 | family = preferred_family; |
| 194 | |
| 195 | fprintf(fp, "report "); |
| 196 | |
| 197 | fprintf(fp, "proto %s ", strxf_xfrmproto(xrep->proto)); |
| 198 | fprintf(fp, "%s", _SL_); |
| 199 | |
| 200 | xfrm_selector_print(&xrep->sel, family, fp, " sel "); |
| 201 | |
| 202 | parse_rtattr(tb, XFRMA_MAX, XFRMREP_RTA(xrep), len); |
| 203 | |
| 204 | xfrm_xfrma_print(tb, family, fp, " "); |
| 205 | |
| 206 | if (oneline) |
| 207 | fprintf(fp, "\n"); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 212 | static void xfrm_ae_flags_print(__u32 flags, void *arg) |
jamal | c9fd974 | 2006-12-07 20:31:14 -0500 | [diff] [blame] | 213 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 214 | FILE *fp = (FILE *)arg; |
| 215 | |
jamal | c9fd974 | 2006-12-07 20:31:14 -0500 | [diff] [blame] | 216 | fprintf(fp, " (0x%x) ", flags); |
| 217 | if (!flags) |
| 218 | return; |
| 219 | if (flags & XFRM_AE_CR) |
| 220 | fprintf(fp, " replay update "); |
| 221 | if (flags & XFRM_AE_CE) |
| 222 | fprintf(fp, " timer expired "); |
| 223 | if (flags & XFRM_AE_CU) |
| 224 | fprintf(fp, " policy updated "); |
| 225 | |
| 226 | } |
| 227 | |
Florian Westphal | 598a42c | 2012-03-13 12:35:25 +0000 | [diff] [blame] | 228 | static void xfrm_usersa_print(const struct xfrm_usersa_id *sa_id, __u32 reqid, FILE *fp) |
| 229 | { |
Stephen Hemminger | 656111b | 2014-08-04 10:30:35 -0700 | [diff] [blame] | 230 | fprintf(fp, "dst %s ", |
Phil Sutter | 2e96d2c | 2016-03-22 19:35:16 +0100 | [diff] [blame] | 231 | rt_addr_n2a(sa_id->family, sizeof(sa_id->daddr), &sa_id->daddr)); |
Florian Westphal | 598a42c | 2012-03-13 12:35:25 +0000 | [diff] [blame] | 232 | |
| 233 | fprintf(fp, " reqid 0x%x", reqid); |
| 234 | |
| 235 | fprintf(fp, " protocol %s ", strxf_proto(sa_id->proto)); |
| 236 | fprintf(fp, " SPI 0x%x", ntohl(sa_id->spi)); |
| 237 | } |
| 238 | |
jamal | c9fd974 | 2006-12-07 20:31:14 -0500 | [diff] [blame] | 239 | static int xfrm_ae_print(const struct sockaddr_nl *who, |
| 240 | struct nlmsghdr *n, void *arg) |
| 241 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 242 | FILE *fp = (FILE *)arg; |
jamal | c9fd974 | 2006-12-07 20:31:14 -0500 | [diff] [blame] | 243 | struct xfrm_aevent_id *id = NLMSG_DATA(n); |
jamal | c9fd974 | 2006-12-07 20:31:14 -0500 | [diff] [blame] | 244 | |
| 245 | fprintf(fp, "Async event "); |
| 246 | xfrm_ae_flags_print(id->flags, arg); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 247 | fprintf(fp, "\n\t"); |
Eric W. Biederman | 26dcdf3 | 2015-03-15 14:48:32 -0500 | [diff] [blame] | 248 | fprintf(fp, "src %s ", rt_addr_n2a(id->sa_id.family, |
Phil Sutter | 2e96d2c | 2016-03-22 19:35:16 +0100 | [diff] [blame] | 249 | sizeof(id->saddr), &id->saddr)); |
Florian Westphal | 598a42c | 2012-03-13 12:35:25 +0000 | [diff] [blame] | 250 | |
| 251 | xfrm_usersa_print(&id->sa_id, id->reqid, fp); |
jamal | c9fd974 | 2006-12-07 20:31:14 -0500 | [diff] [blame] | 252 | |
| 253 | fprintf(fp, "\n"); |
| 254 | fflush(fp); |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
Stephen Hemminger | 656111b | 2014-08-04 10:30:35 -0700 | [diff] [blame] | 259 | static void xfrm_print_addr(FILE *fp, int family, xfrm_address_t *a) |
Florian Westphal | 598a42c | 2012-03-13 12:35:25 +0000 | [diff] [blame] | 260 | { |
Phil Sutter | 2e96d2c | 2016-03-22 19:35:16 +0100 | [diff] [blame] | 261 | fprintf(fp, "%s", rt_addr_n2a(family, sizeof(*a), a)); |
Florian Westphal | 598a42c | 2012-03-13 12:35:25 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | static int xfrm_mapping_print(const struct sockaddr_nl *who, |
| 265 | struct nlmsghdr *n, void *arg) |
| 266 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 267 | FILE *fp = (FILE *)arg; |
Florian Westphal | 598a42c | 2012-03-13 12:35:25 +0000 | [diff] [blame] | 268 | struct xfrm_user_mapping *map = NLMSG_DATA(n); |
| 269 | |
| 270 | fprintf(fp, "Mapping change "); |
Stephen Hemminger | 656111b | 2014-08-04 10:30:35 -0700 | [diff] [blame] | 271 | xfrm_print_addr(fp, map->id.family, &map->old_saddr); |
Florian Westphal | 598a42c | 2012-03-13 12:35:25 +0000 | [diff] [blame] | 272 | |
| 273 | fprintf(fp, ":%d -> ", ntohs(map->old_sport)); |
Stephen Hemminger | 656111b | 2014-08-04 10:30:35 -0700 | [diff] [blame] | 274 | xfrm_print_addr(fp, map->id.family, &map->new_saddr); |
Florian Westphal | 598a42c | 2012-03-13 12:35:25 +0000 | [diff] [blame] | 275 | fprintf(fp, ":%d\n\t", ntohs(map->new_sport)); |
| 276 | |
| 277 | xfrm_usersa_print(&map->id, map->reqid, fp); |
| 278 | |
| 279 | fprintf(fp, "\n"); |
| 280 | fflush(fp); |
| 281 | return 0; |
| 282 | } |
| 283 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 284 | static int xfrm_accept_msg(const struct sockaddr_nl *who, |
Nicolas Dichtel | 0628cdd | 2015-05-20 16:19:58 +0200 | [diff] [blame] | 285 | struct rtnl_ctrl_data *ctrl, |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 286 | struct nlmsghdr *n, void *arg) |
| 287 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 288 | FILE *fp = (FILE *)arg; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 289 | |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 290 | if (timestamp) |
| 291 | print_timestamp(fp); |
| 292 | |
Nicolas Dichtel | b6ec53e | 2015-05-20 16:20:01 +0200 | [diff] [blame] | 293 | if (listen_all_nsid) { |
| 294 | if (ctrl == NULL || ctrl->nsid < 0) |
| 295 | fprintf(fp, "[nsid current]"); |
| 296 | else |
| 297 | fprintf(fp, "[nsid %d]", ctrl->nsid); |
| 298 | } |
| 299 | |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 300 | switch (n->nlmsg_type) { |
| 301 | case XFRM_MSG_NEWSA: |
| 302 | case XFRM_MSG_DELSA: |
| 303 | case XFRM_MSG_UPDSA: |
| 304 | case XFRM_MSG_EXPIRE: |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 305 | xfrm_state_print(who, n, arg); |
| 306 | return 0; |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 307 | case XFRM_MSG_NEWPOLICY: |
| 308 | case XFRM_MSG_DELPOLICY: |
| 309 | case XFRM_MSG_UPDPOLICY: |
| 310 | case XFRM_MSG_POLEXPIRE: |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 311 | xfrm_policy_print(who, n, arg); |
| 312 | return 0; |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 313 | case XFRM_MSG_ACQUIRE: |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 314 | xfrm_acquire_print(who, n, arg); |
| 315 | return 0; |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 316 | case XFRM_MSG_FLUSHSA: |
| 317 | xfrm_state_flush_print(who, n, arg); |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 318 | return 0; |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 319 | case XFRM_MSG_FLUSHPOLICY: |
| 320 | xfrm_policy_flush_print(who, n, arg); |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 321 | return 0; |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 322 | case XFRM_MSG_REPORT: |
Masahide NAKAMURA | c54f31e | 2006-12-05 19:16:05 +0900 | [diff] [blame] | 323 | xfrm_report_print(who, n, arg); |
| 324 | return 0; |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 325 | case XFRM_MSG_NEWAE: |
jamal | c9fd974 | 2006-12-07 20:31:14 -0500 | [diff] [blame] | 326 | xfrm_ae_print(who, n, arg); |
| 327 | return 0; |
Florian Westphal | 598a42c | 2012-03-13 12:35:25 +0000 | [diff] [blame] | 328 | case XFRM_MSG_MAPPING: |
| 329 | xfrm_mapping_print(who, n, arg); |
| 330 | return 0; |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 331 | default: |
| 332 | break; |
jamal | c9fd974 | 2006-12-07 20:31:14 -0500 | [diff] [blame] | 333 | } |
Masahide NAKAMURA | efe69c1 | 2007-08-24 11:05:24 +0900 | [diff] [blame] | 334 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 335 | if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP && |
| 336 | n->nlmsg_type != NLMSG_DONE) { |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 337 | fprintf(fp, "Unknown message: %08d 0x%08x 0x%08x\n", |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 338 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
| 339 | } |
| 340 | return 0; |
| 341 | } |
| 342 | |
Stephen Hemminger | 7721971 | 2006-09-25 17:27:37 -0700 | [diff] [blame] | 343 | extern struct rtnl_handle rth; |
| 344 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 345 | int do_xfrm_monitor(int argc, char **argv) |
| 346 | { |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 347 | char *file = NULL; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 348 | unsigned int groups = ~((unsigned)0); /* XXX */ |
| 349 | int lacquire = 0; |
| 350 | int lexpire = 0; |
| 351 | int laevent = 0; |
| 352 | int lpolicy = 0; |
| 353 | int lsa = 0; |
| 354 | int lreport = 0; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 355 | |
Stephen Hemminger | 7721971 | 2006-09-25 17:27:37 -0700 | [diff] [blame] | 356 | rtnl_close(&rth); |
| 357 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 358 | while (argc > 0) { |
| 359 | if (matches(*argv, "file") == 0) { |
| 360 | NEXT_ARG(); |
| 361 | file = *argv; |
Nicolas Dichtel | b6ec53e | 2015-05-20 16:20:01 +0200 | [diff] [blame] | 362 | } else if (matches(*argv, "all-nsid") == 0) { |
| 363 | listen_all_nsid = 1; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 364 | } else if (matches(*argv, "acquire") == 0) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 365 | lacquire = 1; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 366 | groups = 0; |
| 367 | } else if (matches(*argv, "expire") == 0) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 368 | lexpire = 1; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 369 | groups = 0; |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 370 | } else if (matches(*argv, "SA") == 0) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 371 | lsa = 1; |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 372 | groups = 0; |
jamal | c9fd974 | 2006-12-07 20:31:14 -0500 | [diff] [blame] | 373 | } else if (matches(*argv, "aevent") == 0) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 374 | laevent = 1; |
jamal | c9fd974 | 2006-12-07 20:31:14 -0500 | [diff] [blame] | 375 | groups = 0; |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 376 | } else if (matches(*argv, "policy") == 0) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 377 | lpolicy = 1; |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 378 | groups = 0; |
Masahide NAKAMURA | c54f31e | 2006-12-05 19:16:05 +0900 | [diff] [blame] | 379 | } else if (matches(*argv, "report") == 0) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 380 | lreport = 1; |
Masahide NAKAMURA | c54f31e | 2006-12-05 19:16:05 +0900 | [diff] [blame] | 381 | groups = 0; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 382 | } else if (matches(*argv, "help") == 0) { |
| 383 | usage(); |
Vadim Kochan | 5bf9f5c | 2015-02-14 19:45:04 +0200 | [diff] [blame] | 384 | } else if (strcmp(*argv, "all")) { |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 385 | fprintf(stderr, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv); |
| 386 | exit(-1); |
| 387 | } |
| 388 | argc--; argv++; |
| 389 | } |
| 390 | |
| 391 | if (lacquire) |
jamal | 7b82251 | 2006-12-07 20:58:23 -0500 | [diff] [blame] | 392 | groups |= nl_mgrp(XFRMNLGRP_ACQUIRE); |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 393 | if (lexpire) |
jamal | 7b82251 | 2006-12-07 20:58:23 -0500 | [diff] [blame] | 394 | groups |= nl_mgrp(XFRMNLGRP_EXPIRE); |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 395 | if (lsa) |
jamal | 7b82251 | 2006-12-07 20:58:23 -0500 | [diff] [blame] | 396 | groups |= nl_mgrp(XFRMNLGRP_SA); |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 397 | if (lpolicy) |
jamal | 7b82251 | 2006-12-07 20:58:23 -0500 | [diff] [blame] | 398 | groups |= nl_mgrp(XFRMNLGRP_POLICY); |
jamal | c9fd974 | 2006-12-07 20:31:14 -0500 | [diff] [blame] | 399 | if (laevent) |
jamal | 7b82251 | 2006-12-07 20:58:23 -0500 | [diff] [blame] | 400 | groups |= nl_mgrp(XFRMNLGRP_AEVENTS); |
Masahide NAKAMURA | c54f31e | 2006-12-05 19:16:05 +0900 | [diff] [blame] | 401 | if (lreport) |
jamal | 7b82251 | 2006-12-07 20:58:23 -0500 | [diff] [blame] | 402 | groups |= nl_mgrp(XFRMNLGRP_REPORT); |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 403 | |
| 404 | if (file) { |
| 405 | FILE *fp; |
Stephen Hemminger | e49b51d | 2015-12-30 17:19:04 -0800 | [diff] [blame] | 406 | int err; |
| 407 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 408 | fp = fopen(file, "r"); |
| 409 | if (fp == NULL) { |
| 410 | perror("Cannot fopen"); |
| 411 | exit(-1); |
| 412 | } |
Stephen Hemminger | e49b51d | 2015-12-30 17:19:04 -0800 | [diff] [blame] | 413 | err = rtnl_from_file(fp, xfrm_accept_msg, stdout); |
| 414 | fclose(fp); |
| 415 | return err; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 416 | } |
| 417 | |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 418 | if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0) |
| 419 | exit(1); |
Nicolas Dichtel | b6ec53e | 2015-05-20 16:20:01 +0200 | [diff] [blame] | 420 | if (listen_all_nsid && rtnl_listen_all_nsid(&rth) < 0) |
| 421 | exit(1); |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 422 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 423 | if (rtnl_listen(&rth, xfrm_accept_msg, (void *)stdout) < 0) |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 424 | exit(2); |
| 425 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 426 | return 0; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 427 | } |