Changeset View
Changeset View
Standalone View
Standalone View
sbin/ifconfig/ifgre.c
Show All 38 Lines | |||||
#include <limits.h> | #include <limits.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <err.h> | #include <err.h> | ||||
#include "ifconfig.h" | #include "ifconfig.h" | ||||
#define GREBITS "\020\01ENABLE_CSUM\02ENABLE_SEQ" | #define GREBITS "\020\01ENABLE_CSUM\02ENABLE_SEQ\03UDPENCAP" | ||||
static void gre_status(int s); | static void gre_status(int s); | ||||
static void | static void | ||||
gre_status(int s) | gre_status(int s) | ||||
{ | { | ||||
uint32_t opts = 0; | uint32_t opts, port; | ||||
opts = 0; | |||||
ifr.ifr_data = (caddr_t)&opts; | ifr.ifr_data = (caddr_t)&opts; | ||||
if (ioctl(s, GREGKEY, &ifr) == 0) | if (ioctl(s, GREGKEY, &ifr) == 0) | ||||
if (opts != 0) | if (opts != 0) | ||||
printf("\tgrekey: 0x%x (%u)\n", opts, opts); | printf("\tgrekey: 0x%x (%u)\n", opts, opts); | ||||
opts = 0; | opts = 0; | ||||
if (ioctl(s, GREGOPTS, &ifr) != 0 || opts == 0) | if (ioctl(s, GREGOPTS, &ifr) != 0 || opts == 0) | ||||
return; | return; | ||||
port = 0; | |||||
ifr.ifr_data = (caddr_t)&port; | |||||
if (ioctl(s, GREGPORT, &ifr) == 0 && port != 0) | |||||
printf("\tudpport: %u\n", port); | |||||
printb("\toptions", opts, GREBITS); | printb("\toptions", opts, GREBITS); | ||||
putchar('\n'); | putchar('\n'); | ||||
} | } | ||||
static void | static void | ||||
setifgrekey(const char *val, int dummy __unused, int s, | setifgrekey(const char *val, int dummy __unused, int s, | ||||
const struct afswtch *afp) | const struct afswtch *afp) | ||||
{ | { | ||||
uint32_t grekey = strtol(val, NULL, 0); | uint32_t grekey = strtol(val, NULL, 0); | ||||
strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); | strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); | ||||
ifr.ifr_data = (caddr_t)&grekey; | ifr.ifr_data = (caddr_t)&grekey; | ||||
if (ioctl(s, GRESKEY, (caddr_t)&ifr) < 0) | if (ioctl(s, GRESKEY, (caddr_t)&ifr) < 0) | ||||
warn("ioctl (set grekey)"); | warn("ioctl (set grekey)"); | ||||
} | } | ||||
static void | static void | ||||
setifgreport(const char *val, int dummy __unused, int s, | |||||
const struct afswtch *afp) | |||||
{ | |||||
uint32_t udpport = strtol(val, NULL, 0); | |||||
strlcpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); | |||||
eugen_grosbein.net: Please consider using sysctl net.inet.ip.portrange.hifirst and net.inet.ip.portrange.hilast… | |||||
ifr.ifr_data = (caddr_t)&udpport; | |||||
if (ioctl(s, GRESPORT, (caddr_t)&ifr) < 0) | |||||
warn("ioctl (set udpport)"); | |||||
} | |||||
static void | |||||
setifgreopts(const char *val, int d, int s, const struct afswtch *afp) | setifgreopts(const char *val, int d, int s, const struct afswtch *afp) | ||||
{ | { | ||||
uint32_t opts; | uint32_t opts; | ||||
ifr.ifr_data = (caddr_t)&opts; | ifr.ifr_data = (caddr_t)&opts; | ||||
if (ioctl(s, GREGOPTS, &ifr) == -1) { | if (ioctl(s, GREGOPTS, &ifr) == -1) { | ||||
warn("ioctl(GREGOPTS)"); | warn("ioctl(GREGOPTS)"); | ||||
return; | return; | ||||
} | } | ||||
if (d < 0) | if (d < 0) | ||||
opts &= ~(-d); | opts &= ~(-d); | ||||
else | else | ||||
opts |= d; | opts |= d; | ||||
if (ioctl(s, GRESOPTS, &ifr) == -1) { | if (ioctl(s, GRESOPTS, &ifr) == -1) { | ||||
warn("ioctl(GIFSOPTS)"); | warn("ioctl(GIFSOPTS)"); | ||||
return; | return; | ||||
} | } | ||||
} | } | ||||
static struct cmd gre_cmds[] = { | static struct cmd gre_cmds[] = { | ||||
DEF_CMD_ARG("grekey", setifgrekey), | DEF_CMD_ARG("grekey", setifgrekey), | ||||
DEF_CMD_ARG("udpport", setifgreport), | |||||
DEF_CMD("enable_csum", GRE_ENABLE_CSUM, setifgreopts), | DEF_CMD("enable_csum", GRE_ENABLE_CSUM, setifgreopts), | ||||
DEF_CMD("-enable_csum",-GRE_ENABLE_CSUM,setifgreopts), | DEF_CMD("-enable_csum",-GRE_ENABLE_CSUM,setifgreopts), | ||||
DEF_CMD("enable_seq", GRE_ENABLE_SEQ, setifgreopts), | DEF_CMD("enable_seq", GRE_ENABLE_SEQ, setifgreopts), | ||||
DEF_CMD("-enable_seq",-GRE_ENABLE_SEQ, setifgreopts), | DEF_CMD("-enable_seq",-GRE_ENABLE_SEQ, setifgreopts), | ||||
DEF_CMD("udpencap", GRE_UDPENCAP, setifgreopts), | |||||
DEF_CMD("-udpencap",-GRE_UDPENCAP, setifgreopts), | |||||
}; | }; | ||||
static struct afswtch af_gre = { | static struct afswtch af_gre = { | ||||
.af_name = "af_gre", | .af_name = "af_gre", | ||||
.af_af = AF_UNSPEC, | .af_af = AF_UNSPEC, | ||||
.af_other_status = gre_status, | .af_other_status = gre_status, | ||||
}; | }; | ||||
static __constructor void | static __constructor void | ||||
gre_ctor(void) | gre_ctor(void) | ||||
{ | { | ||||
size_t i; | size_t i; | ||||
for (i = 0; i < nitems(gre_cmds); i++) | for (i = 0; i < nitems(gre_cmds); i++) | ||||
cmd_register(&gre_cmds[i]); | cmd_register(&gre_cmds[i]); | ||||
af_register(&af_gre); | af_register(&af_gre); | ||||
} | } |
Please consider using sysctl net.inet.ip.portrange.hifirst and net.inet.ip.portrange.hilast instead of embedding magic constants 0xC000 and 0xFFFF.
And maybe issue a warning if value is outside of the range but still process it.