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 |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | /* |
| 21 | * based on ipmonitor.c |
| 22 | */ |
| 23 | /* |
| 24 | * Authors: |
| 25 | * Masahide NAKAMURA @USAGI |
| 26 | */ |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <linux/xfrm.h> |
| 32 | #include "utils.h" |
| 33 | #include "xfrm.h" |
| 34 | #include "ip_common.h" |
| 35 | |
| 36 | static void usage(void) __attribute__((noreturn)); |
| 37 | |
| 38 | static void usage(void) |
| 39 | { |
| 40 | fprintf(stderr, "Usage: ip xfrm monitor [ all | LISTofOBJECTS ]\n"); |
| 41 | exit(-1); |
| 42 | } |
| 43 | |
| 44 | static int xfrm_acquire_print(const struct sockaddr_nl *who, |
| 45 | struct nlmsghdr *n, void *arg) |
| 46 | { |
| 47 | FILE *fp = (FILE*)arg; |
| 48 | struct xfrm_user_acquire *xacq = NLMSG_DATA(n); |
| 49 | int len = n->nlmsg_len; |
| 50 | struct rtattr * tb[XFRMA_MAX+1]; |
| 51 | __u16 family; |
| 52 | |
| 53 | if (n->nlmsg_type != XFRM_MSG_ACQUIRE) { |
| 54 | fprintf(stderr, "Not an acquire: %08x %08x %08x\n", |
| 55 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | len -= NLMSG_LENGTH(sizeof(*xacq)); |
| 60 | if (len < 0) { |
| 61 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | parse_rtattr(tb, XFRMA_MAX, XFRMACQ_RTA(xacq), len); |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 66 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 67 | family = xacq->sel.family; |
| 68 | if (family == AF_UNSPEC) |
| 69 | family = xacq->policy.sel.family; |
| 70 | if (family == AF_UNSPEC) |
| 71 | family = preferred_family; |
| 72 | |
| 73 | fprintf(fp, "acquire "); |
| 74 | |
| 75 | fprintf(fp, "proto %s ", strxf_xfrmproto(xacq->id.proto)); |
| 76 | if (show_stats > 0 || xacq->id.spi) { |
| 77 | __u32 spi = ntohl(xacq->id.spi); |
| 78 | fprintf(fp, "spi 0x%08x", spi); |
| 79 | if (show_stats > 0) |
| 80 | fprintf(fp, "(%u)", spi); |
| 81 | fprintf(fp, " "); |
| 82 | } |
| 83 | fprintf(fp, "%s", _SL_); |
| 84 | |
| 85 | xfrm_selector_print(&xacq->sel, family, fp, " sel "); |
| 86 | |
| 87 | xfrm_policy_info_print(&xacq->policy, tb, fp, " ", " policy "); |
| 88 | |
| 89 | if (show_stats > 0) |
| 90 | fprintf(fp, " seq 0x%08u ", xacq->seq); |
| 91 | if (show_stats > 0) { |
| 92 | fprintf(fp, "%s-mask %s ", |
| 93 | strxf_algotype(XFRMA_ALG_CRYPT), |
| 94 | strxf_mask32(xacq->ealgos)); |
| 95 | fprintf(fp, "%s-mask %s ", |
| 96 | strxf_algotype(XFRMA_ALG_AUTH), |
| 97 | strxf_mask32(xacq->aalgos)); |
| 98 | fprintf(fp, "%s-mask %s", |
| 99 | strxf_algotype(XFRMA_ALG_COMP), |
| 100 | strxf_mask32(xacq->calgos)); |
| 101 | } |
| 102 | fprintf(fp, "%s", _SL_); |
| 103 | |
| 104 | if (oneline) |
| 105 | fprintf(fp, "\n"); |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 106 | fflush(fp); |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Masahide NAKAMURA | c54f31e | 2006-12-05 19:16:05 +0900 | [diff] [blame] | 111 | static int xfrm_report_print(const struct sockaddr_nl *who, |
| 112 | struct nlmsghdr *n, void *arg) |
| 113 | { |
| 114 | FILE *fp = (FILE*)arg; |
| 115 | struct xfrm_user_report *xrep = NLMSG_DATA(n); |
| 116 | int len = n->nlmsg_len; |
| 117 | struct rtattr * tb[XFRMA_MAX+1]; |
| 118 | __u16 family; |
| 119 | |
| 120 | if (n->nlmsg_type != XFRM_MSG_REPORT) { |
| 121 | fprintf(stderr, "Not a report: %08x %08x %08x\n", |
| 122 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | len -= NLMSG_LENGTH(sizeof(*xrep)); |
| 127 | if (len < 0) { |
| 128 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); |
| 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | family = xrep->sel.family; |
| 133 | if (family == AF_UNSPEC) |
| 134 | family = preferred_family; |
| 135 | |
| 136 | fprintf(fp, "report "); |
| 137 | |
| 138 | fprintf(fp, "proto %s ", strxf_xfrmproto(xrep->proto)); |
| 139 | fprintf(fp, "%s", _SL_); |
| 140 | |
| 141 | xfrm_selector_print(&xrep->sel, family, fp, " sel "); |
| 142 | |
| 143 | parse_rtattr(tb, XFRMA_MAX, XFRMREP_RTA(xrep), len); |
| 144 | |
| 145 | xfrm_xfrma_print(tb, family, fp, " "); |
| 146 | |
| 147 | if (oneline) |
| 148 | fprintf(fp, "\n"); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
jamal | c9fd974 | 2006-12-07 20:31:14 -0500 | [diff] [blame] | 153 | void xfrm_ae_flags_print(__u32 flags, void *arg) |
| 154 | { |
| 155 | FILE *fp = (FILE*)arg; |
| 156 | fprintf(fp, " (0x%x) ", flags); |
| 157 | if (!flags) |
| 158 | return; |
| 159 | if (flags & XFRM_AE_CR) |
| 160 | fprintf(fp, " replay update "); |
| 161 | if (flags & XFRM_AE_CE) |
| 162 | fprintf(fp, " timer expired "); |
| 163 | if (flags & XFRM_AE_CU) |
| 164 | fprintf(fp, " policy updated "); |
| 165 | |
| 166 | } |
| 167 | |
| 168 | static int xfrm_ae_print(const struct sockaddr_nl *who, |
| 169 | struct nlmsghdr *n, void *arg) |
| 170 | { |
| 171 | FILE *fp = (FILE*)arg; |
| 172 | struct xfrm_aevent_id *id = NLMSG_DATA(n); |
| 173 | char abuf[256]; |
| 174 | |
| 175 | fprintf(fp, "Async event "); |
| 176 | xfrm_ae_flags_print(id->flags, arg); |
| 177 | fprintf(fp,"\n\t"); |
| 178 | memset(abuf, '\0', sizeof(abuf)); |
| 179 | fprintf(fp, "src %s ", rt_addr_n2a(id->sa_id.family, |
| 180 | sizeof(id->saddr), &id->saddr, |
| 181 | abuf, sizeof(abuf))); |
| 182 | memset(abuf, '\0', sizeof(abuf)); |
| 183 | fprintf(fp, "dst %s ", rt_addr_n2a(id->sa_id.family, |
| 184 | sizeof(id->sa_id.daddr), &id->sa_id.daddr, |
| 185 | abuf, sizeof(abuf))); |
| 186 | fprintf(fp, " reqid 0x%x", id->reqid); |
| 187 | fprintf(fp, " protocol %s ", strxf_proto(id->sa_id.proto)); |
| 188 | fprintf(fp, " SPI 0x%x", ntohl(id->sa_id.spi)); |
| 189 | |
| 190 | fprintf(fp, "\n"); |
| 191 | fflush(fp); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 196 | static int xfrm_accept_msg(const struct sockaddr_nl *who, |
| 197 | struct nlmsghdr *n, void *arg) |
| 198 | { |
| 199 | FILE *fp = (FILE*)arg; |
| 200 | |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 201 | if (timestamp) |
| 202 | print_timestamp(fp); |
| 203 | |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 204 | if (n->nlmsg_type == XFRM_MSG_NEWSA || |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 205 | n->nlmsg_type == XFRM_MSG_DELSA || |
| 206 | n->nlmsg_type == XFRM_MSG_UPDSA || |
| 207 | n->nlmsg_type == XFRM_MSG_EXPIRE) { |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 208 | xfrm_state_print(who, n, arg); |
| 209 | return 0; |
| 210 | } |
| 211 | if (n->nlmsg_type == XFRM_MSG_NEWPOLICY || |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 212 | n->nlmsg_type == XFRM_MSG_DELPOLICY || |
| 213 | n->nlmsg_type == XFRM_MSG_UPDPOLICY || |
| 214 | n->nlmsg_type == XFRM_MSG_POLEXPIRE) { |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 215 | xfrm_policy_print(who, n, arg); |
| 216 | return 0; |
| 217 | } |
| 218 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 219 | if (n->nlmsg_type == XFRM_MSG_ACQUIRE) { |
| 220 | xfrm_acquire_print(who, n, arg); |
| 221 | return 0; |
| 222 | } |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 223 | if (n->nlmsg_type == XFRM_MSG_FLUSHSA) { |
| 224 | /* XXX: Todo: show proto in xfrm_usersa_flush */ |
| 225 | fprintf(fp, "Flushed state\n"); |
| 226 | return 0; |
| 227 | } |
| 228 | if (n->nlmsg_type == XFRM_MSG_FLUSHPOLICY) { |
| 229 | fprintf(fp, "Flushed policy\n"); |
| 230 | return 0; |
| 231 | } |
Masahide NAKAMURA | c54f31e | 2006-12-05 19:16:05 +0900 | [diff] [blame] | 232 | if (n->nlmsg_type == XFRM_MSG_REPORT) { |
| 233 | xfrm_report_print(who, n, arg); |
| 234 | return 0; |
| 235 | } |
jamal | c9fd974 | 2006-12-07 20:31:14 -0500 | [diff] [blame] | 236 | if (n->nlmsg_type == XFRM_MSG_NEWAE) { |
| 237 | xfrm_ae_print(who, n, arg); |
| 238 | return 0; |
| 239 | } |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 240 | if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP && |
| 241 | n->nlmsg_type != NLMSG_DONE) { |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 242 | fprintf(fp, "Unknown message: %08d 0x%08x 0x%08x\n", |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 243 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
| 244 | } |
| 245 | return 0; |
| 246 | } |
| 247 | |
Stephen Hemminger | 7721971 | 2006-09-25 17:27:37 -0700 | [diff] [blame] | 248 | extern struct rtnl_handle rth; |
| 249 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 250 | int do_xfrm_monitor(int argc, char **argv) |
| 251 | { |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 252 | char *file = NULL; |
| 253 | unsigned groups = ~((unsigned)0); /* XXX */ |
| 254 | int lacquire=0; |
| 255 | int lexpire=0; |
jamal | c9fd974 | 2006-12-07 20:31:14 -0500 | [diff] [blame] | 256 | int laevent=0; |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 257 | int lpolicy=0; |
| 258 | int lsa=0; |
Masahide NAKAMURA | c54f31e | 2006-12-05 19:16:05 +0900 | [diff] [blame] | 259 | int lreport=0; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 260 | |
Stephen Hemminger | 7721971 | 2006-09-25 17:27:37 -0700 | [diff] [blame] | 261 | rtnl_close(&rth); |
| 262 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 263 | while (argc > 0) { |
| 264 | if (matches(*argv, "file") == 0) { |
| 265 | NEXT_ARG(); |
| 266 | file = *argv; |
| 267 | } else if (matches(*argv, "acquire") == 0) { |
| 268 | lacquire=1; |
| 269 | groups = 0; |
| 270 | } else if (matches(*argv, "expire") == 0) { |
| 271 | lexpire=1; |
| 272 | groups = 0; |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 273 | } else if (matches(*argv, "SA") == 0) { |
| 274 | lsa=1; |
| 275 | groups = 0; |
jamal | c9fd974 | 2006-12-07 20:31:14 -0500 | [diff] [blame] | 276 | } else if (matches(*argv, "aevent") == 0) { |
| 277 | laevent=1; |
| 278 | groups = 0; |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 279 | } else if (matches(*argv, "policy") == 0) { |
| 280 | lpolicy=1; |
| 281 | groups = 0; |
Masahide NAKAMURA | c54f31e | 2006-12-05 19:16:05 +0900 | [diff] [blame] | 282 | } else if (matches(*argv, "report") == 0) { |
| 283 | lreport=1; |
| 284 | groups = 0; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 285 | } else if (matches(*argv, "help") == 0) { |
| 286 | usage(); |
| 287 | } else { |
| 288 | fprintf(stderr, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv); |
| 289 | exit(-1); |
| 290 | } |
| 291 | argc--; argv++; |
| 292 | } |
| 293 | |
| 294 | if (lacquire) |
jamal | 7b82251 | 2006-12-07 20:58:23 -0500 | [diff] [blame^] | 295 | groups |= nl_mgrp(XFRMNLGRP_ACQUIRE); |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 296 | if (lexpire) |
jamal | 7b82251 | 2006-12-07 20:58:23 -0500 | [diff] [blame^] | 297 | groups |= nl_mgrp(XFRMNLGRP_EXPIRE); |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 298 | if (lsa) |
jamal | 7b82251 | 2006-12-07 20:58:23 -0500 | [diff] [blame^] | 299 | groups |= nl_mgrp(XFRMNLGRP_SA); |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 300 | if (lpolicy) |
jamal | 7b82251 | 2006-12-07 20:58:23 -0500 | [diff] [blame^] | 301 | groups |= nl_mgrp(XFRMNLGRP_POLICY); |
jamal | c9fd974 | 2006-12-07 20:31:14 -0500 | [diff] [blame] | 302 | if (laevent) |
jamal | 7b82251 | 2006-12-07 20:58:23 -0500 | [diff] [blame^] | 303 | groups |= nl_mgrp(XFRMNLGRP_AEVENTS); |
Masahide NAKAMURA | c54f31e | 2006-12-05 19:16:05 +0900 | [diff] [blame] | 304 | if (lreport) |
jamal | 7b82251 | 2006-12-07 20:58:23 -0500 | [diff] [blame^] | 305 | groups |= nl_mgrp(XFRMNLGRP_REPORT); |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 306 | |
| 307 | if (file) { |
| 308 | FILE *fp; |
| 309 | fp = fopen(file, "r"); |
| 310 | if (fp == NULL) { |
| 311 | perror("Cannot fopen"); |
| 312 | exit(-1); |
| 313 | } |
| 314 | return rtnl_from_file(fp, xfrm_accept_msg, (void*)stdout); |
| 315 | } |
| 316 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 317 | //ll_init_map(&rth); |
| 318 | |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 319 | if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0) |
| 320 | exit(1); |
| 321 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 322 | if (rtnl_listen(&rth, xfrm_accept_msg, (void*)stdout) < 0) |
| 323 | exit(2); |
| 324 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 325 | return 0; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 326 | } |