| 1 |
/* |
| 2 |
* Portions of this file are subject to copyright(s). See the Net-SNMP's |
| 3 |
* COPYING file for more details and other copyrights that may apply. |
| 4 |
*/ |
| 5 |
#include <net-snmp/net-snmp-config.h> |
| 6 |
|
| 7 |
#include <sys/types.h> |
| 8 |
#ifdef HAVE_SYS_PARAM_H |
| 9 |
#include <sys/param.h> |
| 10 |
#endif |
| 11 |
#ifdef HAVE_SYS_FILE_H |
| 12 |
#include <sys/file.h> |
| 13 |
#endif |
| 14 |
#ifdef HAVE_SYS_SOCKET_H |
| 15 |
#include <sys/socket.h> |
| 16 |
#endif |
| 17 |
#ifdef HAVE_SYS_SOCKIO_H |
| 18 |
#include <sys/sockio.h> |
| 19 |
#endif |
| 20 |
#ifdef HAVE_SYS_IOCTL_H |
| 21 |
#include <sys/ioctl.h> |
| 22 |
#endif |
| 23 |
#ifdef HAVE_SYS_MBUF_H |
| 24 |
#include <sys/mbuf.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
|
| 28 |
#ifdef HAVE_SYS_STREAM_H |
| 29 |
#include <sys/stream.h> |
| 30 |
#endif |
| 31 |
#ifdef HAVE_NET_ROUTE_H |
| 32 |
#include <net/route.h> |
| 33 |
#endif |
| 34 |
#ifdef HAVE_NETINET_IN_H |
| 35 |
#include <netinet/in.h> |
| 36 |
#endif |
| 37 |
#ifdef HAVE_ARPA_INET_H |
| 38 |
#include <arpa/inet.h> |
| 39 |
#endif |
| 40 |
#ifdef HAVE_NETDB_H |
| 41 |
#include <netdb.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#include <errno.h> |
| 45 |
#ifdef HAVE_UNISTD_H |
| 46 |
#include <unistd.h> |
| 47 |
#endif |
| 48 |
#include <stdio.h> |
| 49 |
#include <ctype.h> |
| 50 |
#ifdef HAVE_STRING_H |
| 51 |
#include <string.h> |
| 52 |
#endif |
| 53 |
#ifdef HAVE_STDLIB_H |
| 54 |
#include <stdlib.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
#include <net-snmp/net-snmp-includes.h> |
| 58 |
#include <net-snmp/agent/net-snmp-agent-includes.h> |
| 59 |
#include <net-snmp/data_access/ipaddress.h> |
| 60 |
#include <net-snmp/data_access/route.h> |
| 61 |
|
| 62 |
#include "ip-forward-mib/inetCidrRouteTable/inetCidrRouteTable.h" |
| 63 |
#include "route_ioctl.h" |
| 64 |
|
| 65 |
#ifdef cygwin |
| 66 |
#include <windows.h> |
| 67 |
#endif |
| 68 |
|
| 69 |
#ifndef HAVE_STRUCT_RTENTRY_RT_DST |
| 70 |
# define rt_dst rt_nodes->rn_key |
| 71 |
#endif |
| 72 |
#ifndef HAVE_STRUCT_RTENTRY_RT_HASH |
| 73 |
# define rt_hash rt_pad1 |
| 74 |
#endif |
| 75 |
|
| 76 |
|
| 77 |
#ifdef linux |
| 78 |
# define NETSNMP_ROUTE_WRITE_PROTOCOL PF_ROUTE |
| 79 |
#else |
| 80 |
# define NETSNMP_ROUTE_WRITE_PROTOCOL 0 |
| 81 |
#endif |
| 82 |
|
| 83 |
#ifdef irix6 |
| 84 |
#define SIOCADDRT SIOCADDMULTI |
| 85 |
#define SIOCDELRT SIOCDELMULTI |
| 86 |
#endif |
| 87 |
|
| 88 |
#if defined SIOCADDRT && !defined(irix6) |
| 89 |
int _netsnmp_ioctl_route_set_v4(netsnmp_route_entry * entry) |
| 90 |
{ |
| 91 |
struct sockaddr_in dst, gateway, mask; |
| 92 |
int s, rc; |
| 93 |
RTENTRY route; |
| 94 |
char * DEBUGSTR; |
| 95 |
|
| 96 |
netsnmp_assert(NULL != entry); /* checked in netsnmp_arch_route_create */ |
| 97 |
netsnmp_assert((4 == entry->rt_dest_len) && (4 == entry->rt_nexthop_len)); |
| 98 |
|
| 99 |
s = socket(AF_INET, SOCK_RAW, NETSNMP_ROUTE_WRITE_PROTOCOL); |
| 100 |
if (s < 0) { |
| 101 |
snmp_log_perror("netsnmp_ioctl_route_set_v4: socket"); |
| 102 |
return -3; |
| 103 |
} |
| 104 |
|
| 105 |
memset(&dst, 0, sizeof(dst)); |
| 106 |
dst.sin_family = AF_INET; |
| 107 |
memcpy(&dst.sin_addr.s_addr, entry->rt_dest, 4); |
| 108 |
DEBUGSTR = inet_ntoa(dst.sin_addr); |
| 109 |
DEBUGMSGTL(("access:route","add route to %s\n", DEBUGSTR)); |
| 110 |
|
| 111 |
memset(&gateway, 0, sizeof(gateway)); |
| 112 |
gateway.sin_family = AF_INET; |
| 113 |
memcpy(&gateway.sin_addr.s_addr, entry->rt_nexthop, 4); |
| 114 |
DEBUGSTR = inet_ntoa(gateway.sin_addr); |
| 115 |
DEBUGMSGTL(("access:route"," via %s\n", DEBUGSTR)); |
| 116 |
|
| 117 |
memset(&mask, 0, sizeof(mask)); |
| 118 |
mask.sin_family = AF_INET; |
| 119 |
if (entry->rt_pfx_len != 0) |
| 120 |
mask.sin_addr.s_addr = netsnmp_ipaddress_ipv4_mask(entry->rt_pfx_len); |
| 121 |
else |
| 122 |
mask.sin_addr.s_addr = entry->rt_mask; |
| 123 |
DEBUGSTR = inet_ntoa(mask.sin_addr); |
| 124 |
DEBUGMSGTL(("access:route"," mask %s\n", DEBUGSTR)); |
| 125 |
|
| 126 |
memset(&route, 0, sizeof(route)); |
| 127 |
memcpy(&route.rt_dst, &dst, sizeof(struct sockaddr_in)); |
| 128 |
memcpy(&route.rt_gateway, &gateway, sizeof(struct sockaddr_in)); |
| 129 |
memcpy(&route.rt_genmask, &mask, sizeof(struct sockaddr_in)); |
| 130 |
|
| 131 |
if (32 == entry->rt_pfx_len) |
| 132 |
route.rt_flags |= RTF_HOST; |
| 133 |
if (INETCIDRROUTETYPE_REMOTE == entry->rt_type) |
| 134 |
route.rt_flags |= RTF_GATEWAY; |
| 135 |
route.rt_flags |= RTF_UP; |
| 136 |
|
| 137 |
#ifndef RTENTRY_4_4 |
| 138 |
route.rt_hash = entry->if_index; |
| 139 |
#endif |
| 140 |
|
| 141 |
rc = ioctl(s, SIOCADDRT, (caddr_t) & route); |
| 142 |
if (rc < 0) { |
| 143 |
snmp_log_perror("netsnmp_ioctl_route_set_v4: ioctl"); |
| 144 |
rc = -4; |
| 145 |
} |
| 146 |
close(s); |
| 147 |
|
| 148 |
return rc; |
| 149 |
} |
| 150 |
#endif |
| 151 |
|
| 152 |
#if defined SIOCDELRT && !defined(irix6) |
| 153 |
int _netsnmp_ioctl_route_delete_v4(netsnmp_route_entry * entry) |
| 154 |
{ |
| 155 |
struct sockaddr_in dst, mask; |
| 156 |
struct sockaddr_in gateway; |
| 157 |
int s, rc; |
| 158 |
RTENTRY route; |
| 159 |
char *DEBUGSTR; |
| 160 |
|
| 161 |
netsnmp_assert(NULL != entry); /* checked in netsnmp_arch_route_delete */ |
| 162 |
netsnmp_assert((4 == entry->rt_dest_len) && (4 == entry->rt_nexthop_len)); |
| 163 |
|
| 164 |
s = socket(AF_INET, SOCK_RAW, NETSNMP_ROUTE_WRITE_PROTOCOL); |
| 165 |
if (s < 0) { |
| 166 |
snmp_log_perror("netsnmp_ioctl_route_delete_v4: socket"); |
| 167 |
return -3; |
| 168 |
} |
| 169 |
|
| 170 |
memset(&route, 0, sizeof(route)); |
| 171 |
|
| 172 |
memset(&dst, 0, sizeof(dst)); |
| 173 |
dst.sin_family = AF_INET; |
| 174 |
memcpy(&dst.sin_addr.s_addr, entry->rt_dest, 4); |
| 175 |
DEBUGSTR = inet_ntoa(dst.sin_addr); |
| 176 |
DEBUGMSGTL(("access:route","del route to %s\n", DEBUGSTR)); |
| 177 |
|
| 178 |
memset(&mask, 0, sizeof(mask)); |
| 179 |
mask.sin_family = AF_INET; |
| 180 |
if (entry->rt_pfx_len != 0) |
| 181 |
mask.sin_addr.s_addr = netsnmp_ipaddress_ipv4_mask(entry->rt_pfx_len); |
| 182 |
else |
| 183 |
mask.sin_addr.s_addr = entry->rt_mask; |
| 184 |
|
| 185 |
memset(&gateway, 0, sizeof(gateway)); |
| 186 |
gateway.sin_family = AF_INET; |
| 187 |
memcpy(&gateway.sin_addr.s_addr, entry->rt_nexthop, 4); |
| 188 |
|
| 189 |
memcpy(&route.rt_dst, &dst, sizeof(struct sockaddr_in)); |
| 190 |
memcpy(&route.rt_genmask, &mask, sizeof(struct sockaddr_in)); |
| 191 |
memcpy(&route.rt_gateway, &gateway, sizeof(struct sockaddr_in)); |
| 192 |
|
| 193 |
if (32 == entry->rt_pfx_len) |
| 194 |
route.rt_flags |= RTF_HOST; |
| 195 |
if (INETCIDRROUTETYPE_REMOTE == entry->rt_type) |
| 196 |
route.rt_flags |= RTF_GATEWAY; |
| 197 |
route.rt_flags |= RTF_UP; |
| 198 |
|
| 199 |
#ifndef RTENTRY_4_4 |
| 200 |
route.rt_hash = entry->if_index; |
| 201 |
#endif |
| 202 |
|
| 203 |
rc = ioctl(s, SIOCDELRT, (caddr_t) & route); |
| 204 |
if (rc < 0) { |
| 205 |
snmp_log_perror("netsnmp_ioctl_route_delete_v4: ioctl"); |
| 206 |
rc = -4; |
| 207 |
} |
| 208 |
close(s); |
| 209 |
|
| 210 |
return rc; |
| 211 |
} |
| 212 |
#endif /* SIOCDELRT */ |