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 |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 111 | static int xfrm_accept_msg(const struct sockaddr_nl *who, |
| 112 | struct nlmsghdr *n, void *arg) |
| 113 | { |
| 114 | FILE *fp = (FILE*)arg; |
| 115 | |
shemminger | 90f9302 | 2005-06-07 21:55:55 +0000 | [diff] [blame] | 116 | if (timestamp) |
| 117 | print_timestamp(fp); |
| 118 | |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 119 | if (n->nlmsg_type == XFRM_MSG_NEWSA || |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 120 | n->nlmsg_type == XFRM_MSG_DELSA || |
| 121 | n->nlmsg_type == XFRM_MSG_UPDSA || |
| 122 | n->nlmsg_type == XFRM_MSG_EXPIRE) { |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 123 | xfrm_state_print(who, n, arg); |
| 124 | return 0; |
| 125 | } |
| 126 | if (n->nlmsg_type == XFRM_MSG_NEWPOLICY || |
shemminger | 669ae74 | 2005-11-07 18:39:30 +0000 | [diff] [blame] | 127 | n->nlmsg_type == XFRM_MSG_DELPOLICY || |
| 128 | n->nlmsg_type == XFRM_MSG_UPDPOLICY || |
| 129 | n->nlmsg_type == XFRM_MSG_POLEXPIRE) { |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 130 | xfrm_policy_print(who, n, arg); |
| 131 | return 0; |
| 132 | } |
| 133 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 134 | if (n->nlmsg_type == XFRM_MSG_ACQUIRE) { |
| 135 | xfrm_acquire_print(who, n, arg); |
| 136 | return 0; |
| 137 | } |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 138 | if (n->nlmsg_type == XFRM_MSG_FLUSHSA) { |
| 139 | /* XXX: Todo: show proto in xfrm_usersa_flush */ |
| 140 | fprintf(fp, "Flushed state\n"); |
| 141 | return 0; |
| 142 | } |
| 143 | if (n->nlmsg_type == XFRM_MSG_FLUSHPOLICY) { |
| 144 | fprintf(fp, "Flushed policy\n"); |
| 145 | return 0; |
| 146 | } |
| 147 | if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP && |
| 148 | n->nlmsg_type != NLMSG_DONE) { |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 149 | fprintf(fp, "Unknown message: %08d 0x%08x 0x%08x\n", |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 150 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); |
| 151 | } |
| 152 | return 0; |
| 153 | } |
| 154 | |
Stephen Hemminger | 7721971 | 2006-09-25 17:27:37 -0700 | [diff] [blame^] | 155 | extern struct rtnl_handle rth; |
| 156 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 157 | int do_xfrm_monitor(int argc, char **argv) |
| 158 | { |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 159 | char *file = NULL; |
| 160 | unsigned groups = ~((unsigned)0); /* XXX */ |
| 161 | int lacquire=0; |
| 162 | int lexpire=0; |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 163 | int lpolicy=0; |
| 164 | int lsa=0; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 165 | |
Stephen Hemminger | 7721971 | 2006-09-25 17:27:37 -0700 | [diff] [blame^] | 166 | rtnl_close(&rth); |
| 167 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 168 | while (argc > 0) { |
| 169 | if (matches(*argv, "file") == 0) { |
| 170 | NEXT_ARG(); |
| 171 | file = *argv; |
| 172 | } else if (matches(*argv, "acquire") == 0) { |
| 173 | lacquire=1; |
| 174 | groups = 0; |
| 175 | } else if (matches(*argv, "expire") == 0) { |
| 176 | lexpire=1; |
| 177 | groups = 0; |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 178 | } else if (matches(*argv, "SA") == 0) { |
| 179 | lsa=1; |
| 180 | groups = 0; |
| 181 | } else if (matches(*argv, "policy") == 0) { |
| 182 | lpolicy=1; |
| 183 | groups = 0; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 184 | } else if (matches(*argv, "help") == 0) { |
| 185 | usage(); |
| 186 | } else { |
| 187 | fprintf(stderr, "Argument \"%s\" is unknown, try \"ip xfrm monitor help\".\n", *argv); |
| 188 | exit(-1); |
| 189 | } |
| 190 | argc--; argv++; |
| 191 | } |
| 192 | |
| 193 | if (lacquire) |
| 194 | groups |= XFRMGRP_ACQUIRE; |
| 195 | if (lexpire) |
| 196 | groups |= XFRMGRP_EXPIRE; |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 197 | if (lsa) |
| 198 | groups |= XFRMGRP_SA; |
| 199 | if (lpolicy) |
| 200 | groups |= XFRMGRP_POLICY; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 201 | |
| 202 | if (file) { |
| 203 | FILE *fp; |
| 204 | fp = fopen(file, "r"); |
| 205 | if (fp == NULL) { |
| 206 | perror("Cannot fopen"); |
| 207 | exit(-1); |
| 208 | } |
| 209 | return rtnl_from_file(fp, xfrm_accept_msg, (void*)stdout); |
| 210 | } |
| 211 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 212 | //ll_init_map(&rth); |
| 213 | |
shemminger | c595c79 | 2005-11-01 23:03:03 +0000 | [diff] [blame] | 214 | if (rtnl_open_byproto(&rth, groups, NETLINK_XFRM) < 0) |
| 215 | exit(1); |
| 216 | |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 217 | if (rtnl_listen(&rth, xfrm_accept_msg, (void*)stdout) < 0) |
| 218 | exit(2); |
| 219 | |
shemminger | 351efcd | 2005-09-01 19:21:50 +0000 | [diff] [blame] | 220 | return 0; |
linux-ipv6.org!nakam | f9cb3a2 | 2005-03-22 16:13:21 +0000 | [diff] [blame] | 221 | } |