📄 eroute.c
字号:
/* * manipulate eroutes * Copyright (C) 1996 John Ioannidis. * Copyright (C) 1997, 1998, 1999, 2000, 2001 Richard Guy Briggs. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. */char eroute_c_version[] = "RCSID $Id: eroute.c,v 1.43 2001/06/15 05:02:05 rgb Exp $";#include <sys/types.h>#include <linux/types.h> /* new */#include <string.h>#include <errno.h>#include <stdlib.h> /* system(), strtoul() */#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/stat.h>#include <fcntl.h>#include <netdb.h>#include <unistd.h>#include <freeswan.h>#if 0#include <linux/autoconf.h> /* CONFIG_IPSEC_PFKEYv2 */#endif/* permanently turn it on since netlink support has been disabled */ #include <signal.h> #include <pfkeyv2.h> #include <pfkey.h>#include "radij.h"#include "ipsec_encap.h"#include "ipsec_netlink.h"#include <stdio.h>#include <getopt.h>char *program_name;extern char *optarg;extern int optind, opterr, optopt;char *eroute_af_opt, *said_af_opt, *edst_opt, *spi_opt, *proto_opt, *said_opt, *dst_opt, *src_opt;int action_type = 0;extern unsigned int pfkey_lib_debug; /* used by libfreeswan/pfkey_v2_build */int pfkey_sock;fd_set pfkey_socks;uint32_t pfkey_seq = 0;static voidusage(char* arg){ fprintf(stdout, "usage: %s --{add,replace} --eraf <inet | inet6> --src <src>/<srcmaskbits>|<srcmask> --dst <dst>/<dstmaskbits>|<dstmask> <SA>\n", arg); fprintf(stdout, " where <SA> is '--af <inet | inet6> --edst <edst> --spi <spi> --proto <proto>' OR '--said <said>' OR '--said <%%passthrough | %%passthrough4 | %%passthrough6 | %%drop | %%reject | %%trap | %%hold | %%pass>'.\n"); fprintf(stdout, " %s --del --eraf <inet | inet6>--src <src>/<srcmaskbits>|<srcmask> --dst <dst>/<dstmaskbits>|<dstmask>\n", arg); fprintf(stdout, " %s --clear\n", arg); fprintf(stdout, " %s --help\n", arg); fprintf(stdout, " %s --version\n", arg); fprintf(stdout, " %s\n", arg); fprintf(stdout, " [ --debug ] is optional to any %s command.\n", arg); fprintf(stdout, " [ --label <label> ] is optional to any %s command.\n", arg);exit(1);}static struct option const longopts[] ={ {"dst", 1, 0, 'D'}, {"src", 1, 0, 'S'}, {"eraf", 1, 0, 'f'}, {"add", 0, 0, 'a'}, {"replace", 0, 0, 'r'}, {"clear", 0, 0, 'c'}, {"del", 0, 0, 'd'}, {"af", 1, 0, 'i'}, {"edst", 1, 0, 'e'}, {"proto", 1, 0, 'p'}, {"help", 0, 0, 'h'}, {"spi", 1, 0, 's'}, {"said", 1, 0, 'I'}, {"version", 0, 0, 'v'}, {"label", 1, 0, 'l'}, {"optionsfrom", 1, 0, '+'}, {"debug", 0, 0, 'g'}, {0, 0, 0, 0}};intmain(int argc, char **argv){/* int fd; */ char *endptr;/* int ret; */ int c, previous = -1; const char* error_s; int debug = 0; int error = 0; char ipaddr_txt[ADDRTOT_BUF]; struct sadb_ext *extensions[SADB_EXT_MAX + 1]; struct sadb_msg *pfkey_msg; ip_address pfkey_address_s_ska; /*struct sockaddr_in pfkey_address_d_ska;*/ ip_address pfkey_address_sflow_ska; ip_address pfkey_address_dflow_ska; ip_address pfkey_address_smask_ska; ip_address pfkey_address_dmask_ska; ip_said said; ip_subnet s_subnet, d_subnet; int eroute_af = 0; int said_af = 0; int argcount = argc; program_name = argv[0]; eroute_af_opt = said_af_opt = edst_opt = spi_opt = proto_opt = said_opt = dst_opt = src_opt = NULL; while((c = getopt_long(argc, argv, ""/*"acdD:e:i:hprs:S:f:vl:+:g"*/, longopts, 0)) != EOF) { switch(c) { case 'g': debug = 1; pfkey_lib_debug = 1; argcount--; break; case 'a': if(action_type) { fprintf(stderr, "%s: Only one of '--add', '--replace', '--clear', or '--del' options permitted.\n", program_name); exit(1); } action_type = EMT_SETEROUTE; break; case 'r': if(action_type) { fprintf(stderr, "%s: Only one of '--add', '--replace', '--clear', or '--del' options permitted.\n", program_name); exit(1); } action_type = EMT_RPLACEROUTE; break; case 'c': if(action_type) { fprintf(stderr, "%s: Only one of '--add', '--replace', '--clear', or '--del' options permitted.\n", program_name); exit(1); } action_type = EMT_CLREROUTE; break; case 'd': if(action_type) { fprintf(stderr, "%s: Only one of '--add', '--replace', '--clear', or '--del' options permitted.\n", program_name); exit(1); } action_type = EMT_DELEROUTE; break; case 'e': if(said_opt) { fprintf(stderr, "%s: Error, EDST parameter redefined:%s, already defined in SA:%s\n", program_name, optarg, said_opt); exit (1); } if(edst_opt) { fprintf(stderr, "%s: Error, EDST parameter redefined:%s, already defined as:%s\n", program_name, optarg, edst_opt); exit (1); } if((error_s = ttoaddr(optarg, 0, said_af, &said.dst)) != 0) { if(error_s) { fprintf(stderr, "%s: Error, %s converting --edst argument:%s\n", program_name, error_s, optarg); exit (1); } } edst_opt = optarg; break; case 'h': case '?': usage(program_name); exit(1); case 's': if(said_opt) { fprintf(stderr, "%s: Error, SPI parameter redefined:%s, already defined in SA:%s\n", program_name, optarg, said_opt); exit (1); } if(spi_opt) { fprintf(stderr, "%s: Error, SPI parameter redefined:%s, already defined as:%s\n", program_name, optarg, spi_opt); exit (1); } said.spi = htonl(strtoul(optarg, &endptr, 0)); if(!(endptr == optarg + strlen(optarg))) { fprintf(stderr, "%s: Invalid character in SPI parameter: %s\n", program_name, optarg); exit (1); } if(ntohl(said.spi) < 0x100) { fprintf(stderr, "%s: Illegal reserved spi: %s => 0x%x Must be larger than 0x100.\n", program_name, optarg, ntohl(said.spi)); exit(1); } spi_opt = optarg; break; case 'p': if(said_opt) { fprintf(stderr, "%s: Error, PROTO parameter redefined:%s, already defined in SA:%s\n", program_name, optarg, said_opt); exit (1); } if(proto_opt) { fprintf(stderr, "%s: Error, PROTO parameter redefined:%s, already defined as:%s\n", program_name, optarg, proto_opt); exit (1); }#if 0 if(said.proto) { fprintf(stderr, "%s: Warning, PROTO parameter redefined:%s\n", program_name, optarg); exit (1); }#endif if(!strcmp(optarg, "ah")) said.proto = SA_AH; if(!strcmp(optarg, "esp")) said.proto = SA_ESP; if(!strcmp(optarg, "tun")) said.proto = SA_IPIP; if(!strcmp(optarg, "comp")) said.proto = SA_COMP; if(said.proto == 0) { fprintf(stderr, "%s: Invalid PROTO parameter: %s\n", program_name, optarg); exit (1); } proto_opt = optarg; break; case 'I': if(said_opt) { fprintf(stderr, "%s: Error, SAID parameter redefined:%s, already defined in SA:%s\n", program_name, optarg, said_opt); exit (1); } if(proto_opt) { fprintf(stderr, "%s: Error, PROTO parameter redefined in SA:%s, already defined as:%s\n", program_name, optarg, proto_opt); exit (1); } if(edst_opt) { fprintf(stderr, "%s: Error, EDST parameter redefined in SA:%s, already defined as:%s\n", program_name, optarg, edst_opt); exit (1); } if(spi_opt) { fprintf(stderr, "%s: Error, SPI parameter redefined in SA:%s, already defined as:%s\n", program_name, optarg, spi_opt); exit (1); } if(said_af_opt) { fprintf(stderr, "%s: Error, address family parameter redefined in SA:%s, already defined as:%s\n", program_name, optarg, said_af_opt); exit (1); } if((error_s = ttosa(optarg, 0, &said)) != NULL) { fprintf(stderr, "%s: Error, %s converting --sa argument:%s\n", program_name, error_s, optarg); exit (1); } else if(ntohl(said.spi) < 0x100){ fprintf(stderr, "%s: Illegal reserved spi: %s => 0x%x Must be larger than or equal to 0x100.\n", program_name, optarg, said.spi); exit(1); } said_af = addrtypeof(&said.dst); said_opt = optarg; break; case 'v': fprintf(stdout, "%s, %s\n", program_name, eroute_c_version); exit(1); case 'D': if(dst_opt) { fprintf(stderr, "%s: Error, DDST parameter redefined:%s, already defined as:%s\n", program_name, optarg, dst_opt); exit (1); } if (ttosubnet(optarg, 0, eroute_af, &d_subnet)) { fprintf(stderr, "%s: Invalid formation of destination network/mask: %s\n", program_name, optarg); exit (1); } dst_opt = optarg; break; case 'S': if(src_opt) { fprintf(stderr, "%s: Error, SDST parameter redefined:%s, already defined as:%s\n", program_name, optarg, src_opt); exit (1); } if (ttosubnet(optarg, 0, eroute_af, &s_subnet)) { fprintf(stderr, "%s: Invalid formation of source network/mask: %s\n", program_name, optarg); exit (1); } src_opt = optarg; break; case 'l': program_name = malloc(strlen(argv[0]) + 10 /* update this when changing the sprintf() */ + strlen(optarg)); sprintf(program_name, "%s --label %s", argv[0], optarg); argcount -= 2; break; case 'i': /* specifies the address family of the SAID, stored in said_af */ if(said_af_opt) { fprintf(stderr, "%s: Error, address family of SAID redefined:%s, already defined as:%s\n", program_name, optarg, said_af_opt); exit (1); } if(!strcmp(optarg, "inet")) said_af = AF_INET; if(!strcmp(optarg, "inet6")) said_af = AF_INET6; if(said_af == 0) { fprintf(stderr, "%s: Invalid address family parameter for SAID: %s\n", program_name, optarg); exit (1); } said_af_opt = optarg; break; case 'f': /* specifies the address family of the eroute, stored in eroute_af */ if(eroute_af_opt) { fprintf(stderr, "%s: Error, address family of eroute redefined:%s, already defined as:%s\n", program_name, optarg, eroute_af_opt); exit (1); } if(!strcmp(optarg, "inet")) eroute_af = AF_INET; if(!strcmp(optarg, "inet6")) eroute_af = AF_INET6; if(eroute_af == 0) { fprintf(stderr, "%s: Invalid address family parameter for eroute: %s\n", program_name, optarg); exit (1); } eroute_af_opt = optarg; break; case '+': /* optionsfrom */ optionsfrom(optarg, &argc, &argv, optind, stderr); /* no return on error */ break; default: } previous = c; } if(debug) { fprintf(stdout, "%s: DEBUG: argc=%d\n", program_name, argc); } if(argcount == 1) { system("cat /proc/net/ipsec_eroute"); exit(0); } /* Sanity checks */ if(debug) { fprintf(stdout, "%s: DEBUG: action_type=%d\n", program_name, action_type); } switch(action_type) { case EMT_SETEROUTE: case EMT_RPLACEROUTE: if(!(said_af_opt && edst_opt && spi_opt && proto_opt) && !(said_opt)) { fprintf(stderr, "%s: add option must have SA specified.\n", program_name); exit(1); } case EMT_DELEROUTE: if(!src_opt) { fprintf(stderr, "%s: Error -- %s option '--src' is required.\n", program_name, (action_type == EMT_SETEROUTE) ? "add" : "del"); exit(1); } if(!dst_opt) { fprintf(stderr, "%s: Error -- %s option '--dst' is required.\n", program_name, (action_type == EMT_SETEROUTE) ? "add" : "del"); exit(1); } case EMT_CLREROUTE: break; default: fprintf(stderr, "%s: exactly one of '--add', '--replace', '--del' or '--clear' options must be specified.\n" "Try %s --help' for usage information.\n", program_name, program_name); exit(1); } if((pfkey_sock = socket(PF_KEY, SOCK_RAW, PF_KEY_V2) ) < 0) { fprintf(stderr, "%s: Trouble openning PF_KEY family socket with error: ", program_name); switch(errno) { case ENOENT: fprintf(stderr, "device does not exist. See FreeS/WAN installation procedure.\n"); break; case EACCES: fprintf(stderr, "access denied. "); if(getuid() == 0) { fprintf(stderr, "Check permissions. Should be 600.\n"); } else { fprintf(stderr, "You must be root to open this file.\n"); } break; case EUNATCH: fprintf(stderr, "KLIPS not loaded.\n"); break; case ENODEV: fprintf(stderr, "KLIPS not loaded or enabled.\n"); break; case EBUSY: fprintf(stderr, "KLIPS is busy. Most likely a serious internal error occured in a previous command. Please report as much detail as possible to development team.\n"); break; case EINVAL: fprintf(stderr, "Invalid argument, KLIPS not loaded or check kernel log messages for specifics.\n"); break; case ENOBUFS: case ENOMEM: case ENFILE:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -