Changeset View
Changeset View
Standalone View
Standalone View
head/sbin/ipfw/nat.c
Show First 20 Lines • Show All 111 Lines • ▼ Show 20 Lines | |||||
* message processing. | * message processing. | ||||
*/ | */ | ||||
ifIndex = 0; | ifIndex = 0; | ||||
next = buf; | next = buf; | ||||
while (next < lim) { | while (next < lim) { | ||||
ifm = (struct if_msghdr *)next; | ifm = (struct if_msghdr *)next; | ||||
next += ifm->ifm_msglen; | next += ifm->ifm_msglen; | ||||
if (ifm->ifm_version != RTM_VERSION) { | if (ifm->ifm_version != RTM_VERSION) { | ||||
if (co.verbose) | if (g_co.verbose) | ||||
warnx("routing message version %d " | warnx("routing message version %d " | ||||
"not understood", ifm->ifm_version); | "not understood", ifm->ifm_version); | ||||
continue; | continue; | ||||
} | } | ||||
if (ifm->ifm_type == RTM_IFINFO) { | if (ifm->ifm_type == RTM_IFINFO) { | ||||
sdl = (struct sockaddr_dl *)(ifm + 1); | sdl = (struct sockaddr_dl *)(ifm + 1); | ||||
if (strlen(ifn) == sdl->sdl_nlen && | if (strlen(ifn) == sdl->sdl_nlen && | ||||
strncmp(ifn, sdl->sdl_data, sdl->sdl_nlen) == 0) { | strncmp(ifn, sdl->sdl_data, sdl->sdl_nlen) == 0) { | ||||
ifIndex = ifm->ifm_index; | ifIndex = ifm->ifm_index; | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
if (!ifIndex) | if (!ifIndex) | ||||
errx(1, "unknown interface name %s", ifn); | errx(1, "unknown interface name %s", ifn); | ||||
/* | /* | ||||
* Get interface address. | * Get interface address. | ||||
*/ | */ | ||||
sin = NULL; | sin = NULL; | ||||
while (next < lim) { | while (next < lim) { | ||||
ifam = (struct ifa_msghdr *)next; | ifam = (struct ifa_msghdr *)next; | ||||
next += ifam->ifam_msglen; | next += ifam->ifam_msglen; | ||||
if (ifam->ifam_version != RTM_VERSION) { | if (ifam->ifam_version != RTM_VERSION) { | ||||
if (co.verbose) | if (g_co.verbose) | ||||
warnx("routing message version %d " | warnx("routing message version %d " | ||||
"not understood", ifam->ifam_version); | "not understood", ifam->ifam_version); | ||||
continue; | continue; | ||||
} | } | ||||
if (ifam->ifam_type != RTM_NEWADDR) | if (ifam->ifam_type != RTM_NEWADDR) | ||||
break; | break; | ||||
if (ifam->ifam_addrs & RTA_IFA) { | if (ifam->ifam_addrs & RTA_IFA) { | ||||
int i; | int i; | ||||
▲ Show 20 Lines • Show All 465 Lines • ▼ Show 20 Lines | if (isdigit(***av)) { | ||||
} | } | ||||
} | } | ||||
} | } | ||||
return (space); | return (space); | ||||
} | } | ||||
static void | static void | ||||
nat_show_log(struct nat44_cfg_nat *n, void *arg) | nat_show_log(struct nat44_cfg_nat *n, void *arg __unused) | ||||
{ | { | ||||
char *buf; | char *buf; | ||||
buf = (char *)(n + 1); | buf = (char *)(n + 1); | ||||
if (buf[0] != '\0') | if (buf[0] != '\0') | ||||
printf("nat %s: %s\n", n->name, buf); | printf("nat %s: %s\n", n->name, buf); | ||||
} | } | ||||
static void | static void | ||||
nat_show_cfg(struct nat44_cfg_nat *n, void *arg) | nat_show_cfg(struct nat44_cfg_nat *n, void *arg __unused) | ||||
{ | { | ||||
int i, cnt, off; | |||||
struct nat44_cfg_redir *t; | struct nat44_cfg_redir *t; | ||||
struct nat44_cfg_spool *s; | struct nat44_cfg_spool *s; | ||||
caddr_t buf; | caddr_t buf; | ||||
struct protoent *p; | struct protoent *p; | ||||
uint32_t cnt; | |||||
int i, off; | |||||
buf = (caddr_t)n; | buf = (caddr_t)n; | ||||
off = sizeof(*n); | off = sizeof(*n); | ||||
printf("ipfw nat %s config", n->name); | printf("ipfw nat %s config", n->name); | ||||
if (strlen(n->if_name) != 0) | if (strlen(n->if_name) != 0) | ||||
printf(" if %s", n->if_name); | printf(" if %s", n->if_name); | ||||
else if (n->ip.s_addr != 0) | else if (n->ip.s_addr != 0) | ||||
printf(" ip %s", inet_ntoa(n->ip)); | printf(" ip %s", inet_ntoa(n->ip)); | ||||
▲ Show 20 Lines • Show All 275 Lines • ▼ Show 20 Lines | case TOK_REDIR_PROTO: | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
i = do_set3(IP_FW_NAT44_XCONFIG, &oh->opheader, len); | i = do_set3(IP_FW_NAT44_XCONFIG, &oh->opheader, len); | ||||
if (i != 0) | if (i != 0) | ||||
err(1, "setsockopt(%s)", "IP_FW_NAT44_XCONFIG"); | err(1, "setsockopt(%s)", "IP_FW_NAT44_XCONFIG"); | ||||
if (!co.do_quiet) { | if (!g_co.do_quiet) { | ||||
/* After every modification, we show the resultant rule. */ | /* After every modification, we show the resultant rule. */ | ||||
int _ac = 3; | int _ac = 3; | ||||
const char *_av[] = {"show", "config", id}; | const char *_av[] = {"show", "config", id}; | ||||
ipfw_show_nat(_ac, (char **)(void *)_av); | ipfw_show_nat(_ac, (char **)(void *)_av); | ||||
} | } | ||||
} | } | ||||
static void | static void | ||||
Show All 12 Lines | |||||
{ | { | ||||
ipfw_obj_header oh; | ipfw_obj_header oh; | ||||
int ret; | int ret; | ||||
memset(&oh, 0, sizeof(oh)); | memset(&oh, 0, sizeof(oh)); | ||||
nat_fill_ntlv(&oh.ntlv, i); | nat_fill_ntlv(&oh.ntlv, i); | ||||
ret = do_set3(IP_FW_NAT44_DESTROY, &oh.opheader, sizeof(oh)); | ret = do_set3(IP_FW_NAT44_DESTROY, &oh.opheader, sizeof(oh)); | ||||
if (ret == -1) { | if (ret == -1) { | ||||
if (!co.do_quiet) | if (!g_co.do_quiet) | ||||
warn("nat %u not available", i); | warn("nat %u not available", i); | ||||
return (EX_UNAVAILABLE); | return (EX_UNAVAILABLE); | ||||
} | } | ||||
return (EX_OK); | return (EX_OK); | ||||
} | } | ||||
struct nat_list_arg { | struct nat_list_arg { | ||||
uint16_t cmd; | uint16_t cmd; | ||||
Show All 35 Lines | |||||
/* | /* | ||||
* Compare nat names. | * Compare nat names. | ||||
* Honor number comparison. | * Honor number comparison. | ||||
*/ | */ | ||||
static int | static int | ||||
natname_cmp(const void *a, const void *b) | natname_cmp(const void *a, const void *b) | ||||
{ | { | ||||
struct nat44_cfg_nat *ia, *ib; | const struct nat44_cfg_nat *ia, *ib; | ||||
ia = (struct nat44_cfg_nat *)a; | ia = (const struct nat44_cfg_nat *)a; | ||||
ib = (struct nat44_cfg_nat *)b; | ib = (const struct nat44_cfg_nat *)b; | ||||
return (stringnum_cmp(ia->name, ib->name)); | return (stringnum_cmp(ia->name, ib->name)); | ||||
} | } | ||||
/* | /* | ||||
* Retrieves nat list from kernel, | * Retrieves nat list from kernel, | ||||
* optionally sorts it and calls requested function for each table. | * optionally sorts it and calls requested function for each table. | ||||
* Returns 0 on success. | * Returns 0 on success. | ||||
*/ | */ | ||||
static int | static int | ||||
nat_foreach(nat_cb_t *f, void *arg, int sort) | nat_foreach(nat_cb_t *f, void *arg, int sort) | ||||
{ | { | ||||
ipfw_obj_lheader *olh; | ipfw_obj_lheader *olh; | ||||
struct nat44_cfg_nat *cfg; | struct nat44_cfg_nat *cfg; | ||||
size_t sz; | size_t sz; | ||||
int i, error; | uint32_t i; | ||||
int error; | |||||
/* Start with reasonable default */ | /* Start with reasonable default */ | ||||
sz = sizeof(*olh) + 16 * sizeof(struct nat44_cfg_nat); | sz = sizeof(*olh) + 16 * sizeof(struct nat44_cfg_nat); | ||||
for (;;) { | for (;;) { | ||||
if ((olh = calloc(1, sz)) == NULL) | if ((olh = calloc(1, sz)) == NULL) | ||||
return (ENOMEM); | return (ENOMEM); | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | ipfw_show_nat(int ac, char **av) | ||||
ipfw_obj_header *oh; | ipfw_obj_header *oh; | ||||
char *name; | char *name; | ||||
int cmd; | int cmd; | ||||
struct nat_list_arg nla; | struct nat_list_arg nla; | ||||
ac--; | ac--; | ||||
av++; | av++; | ||||
if (co.test_only) | if (g_co.test_only) | ||||
return; | return; | ||||
/* Parse parameters. */ | /* Parse parameters. */ | ||||
cmd = 0; /* XXX: Change to IP_FW_NAT44_XGETLOG @ MFC */ | cmd = 0; /* XXX: Change to IP_FW_NAT44_XGETLOG @ MFC */ | ||||
name = NULL; | name = NULL; | ||||
for ( ; ac != 0; ac--, av++) { | for ( ; ac != 0; ac--, av++) { | ||||
if (!strncmp(av[0], "config", strlen(av[0]))) { | if (!strncmp(av[0], "config", strlen(av[0]))) { | ||||
cmd = IP_FW_NAT44_XGETCONFIG; | cmd = IP_FW_NAT44_XGETCONFIG; | ||||
Show All 27 Lines |