| 1 |
/* Definitions for network-related stuff. |
| 2 |
* |
| 3 |
* Copyright 2007-2016 Rainer Gerhards and Adiscon GmbH. |
| 4 |
* |
| 5 |
* This file is part of the rsyslog runtime library. |
| 6 |
* |
| 7 |
* The rsyslog runtime library is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Lesser General Public License as published by |
| 9 |
* the Free Software Foundation, either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* The rsyslog runtime library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public License |
| 18 |
* along with the rsyslog runtime library. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
* |
| 20 |
* A copy of the GPL can be found in the file "COPYING" in this distribution. |
| 21 |
* A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution. |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifndef INCLUDED_NET_H |
| 25 |
#define INCLUDED_NET_H |
| 26 |
|
| 27 |
#include <arpa/inet.h> |
| 28 |
#include <netinet/in.h> |
| 29 |
#include <sys/socket.h> /* this is needed on HP UX -- rgerhards, 2008-03-04 */ |
| 30 |
#include "netns_socket.h" |
| 31 |
|
| 32 |
typedef enum _TCPFRAMINGMODE { |
| 33 |
TCP_FRAMING_OCTET_STUFFING = 0, /* traditional LF-delimited */ |
| 34 |
TCP_FRAMING_OCTET_COUNTING = 1 /* -transport-tls like octet count */ |
| 35 |
} TCPFRAMINGMODE; |
| 36 |
|
| 37 |
#define F_SET(where, flag) ((where) |= (flag)) |
| 38 |
#define F_ISSET(where, flag) (((where) & (flag)) == (flag)) |
| 39 |
#define F_UNSET(where, flag) ((where) &= ~(flag)) |
| 40 |
|
| 41 |
#define ADDR_NAME 0x01 /* address is hostname wildcard) */ |
| 42 |
#define ADDR_PRI6 0x02 /* use IPv6 address prior to IPv4 when resolving */ |
| 43 |
|
| 44 |
/* portability: incase IP_FREEBIND is not defined */ |
| 45 |
#ifndef IP_FREEBIND |
| 46 |
#define IP_FREEBIND 0 |
| 47 |
#endif |
| 48 |
/* defines for IP_FREEBIND, currently being used in imudp */ |
| 49 |
#define IPFREEBIND_DISABLED 0x00 /* don't enable IP_FREEBIND in sock option */ |
| 50 |
#define IPFREEBIND_ENABLED_NO_LOG 0x01 /* enable IP_FREEBIND but no warn on success */ |
| 51 |
#define IPFREEBIND_ENABLED_WITH_LOG 0x02 /* enable IP_FREEBIND and warn on success */ |
| 52 |
|
| 53 |
#ifdef OS_BSD |
| 54 |
#ifndef _KERNEL |
| 55 |
#define s6_addr32 __u6_addr.__u6_addr32 |
| 56 |
#endif |
| 57 |
#endif |
| 58 |
|
| 59 |
struct NetAddr { |
| 60 |
uint8_t flags; |
| 61 |
union { |
| 62 |
struct sockaddr *NetAddr; |
| 63 |
char *HostWildcard; |
| 64 |
} addr; |
| 65 |
}; |
| 66 |
|
| 67 |
#ifndef SO_BSDCOMPAT |
| 68 |
/* this shall prevent compiler errors due to undefined name */ |
| 69 |
#define SO_BSDCOMPAT 0 |
| 70 |
#endif |
| 71 |
|
| 72 |
|
| 73 |
/* IPv6 compatibility layer for older platforms |
| 74 |
* We need to handle a few things different if we are running |
| 75 |
* on an older platform which does not support all the glory |
| 76 |
* of IPv6. We try to limit toll on features and reliability, |
| 77 |
* but obviously it is better to run rsyslog on a platform that |
| 78 |
* supports everything... |
| 79 |
* rgerhards, 2007-06-22 |
| 80 |
*/ |
| 81 |
#ifndef AI_NUMERICSERV |
| 82 |
#define AI_NUMERICSERV 0 |
| 83 |
#endif |
| 84 |
|
| 85 |
|
| 86 |
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN |
| 87 |
#define SALEN(sa) ((sa)->sa_len) |
| 88 |
#else |
| 89 |
static inline size_t __attribute__((unused)) SALEN(struct sockaddr *sa) { |
| 90 |
switch (sa->sa_family) { |
| 91 |
case AF_INET: |
| 92 |
return (sizeof(struct sockaddr_in)); |
| 93 |
case AF_INET6: |
| 94 |
return (sizeof(struct sockaddr_in6)); |
| 95 |
default: |
| 96 |
return 0; |
| 97 |
} |
| 98 |
} |
| 99 |
#endif |
| 100 |
|
| 101 |
struct AllowedSenders { |
| 102 |
struct NetAddr allowedSender; /* ip address allowed */ |
| 103 |
uint8_t SignificantBits; /* defines how many bits should be discarded (eqiv to mask) */ |
| 104 |
struct AllowedSenders *pNext; |
| 105 |
}; |
| 106 |
|
| 107 |
|
| 108 |
/* this structure is a helper to implement wildcards in permittedPeers_t. It specifies |
| 109 |
* the domain component and the matching mode. |
| 110 |
* rgerhards, 2008-05-27 |
| 111 |
*/ |
| 112 |
struct permittedPeerWildcard_s { |
| 113 |
uchar *pszDomainPart; |
| 114 |
size_t lenDomainPart; |
| 115 |
enum { |
| 116 |
PEER_WILDCARD_NONE = 0, /**< no wildcard in this entry */ |
| 117 |
PEER_WILDCARD_AT_START = 1, /**< wildcard at start of entry (*name) */ |
| 118 |
PEER_WILDCARD_AT_END = 2, /**< wildcard at end of entry (name*) */ |
| 119 |
PEER_WILDCARD_MATCH_ALL = 3, /**< only * wildcard, matches all values */ |
| 120 |
PEER_WILDCARD_EMPTY_COMPONENT = 4 /**< special case: domain component empty (e.g. "..") */ |
| 121 |
} wildcardType; |
| 122 |
permittedPeerWildcard_t *pNext; |
| 123 |
}; |
| 124 |
|
| 125 |
/* for fingerprints and hostnames, we need to have a temporary linked list of |
| 126 |
* permitted values. Unforutnately, we must also duplicate this in the netstream |
| 127 |
* drivers. However, this is the best interim solution (with the least effort). |
| 128 |
* A clean implementation requires that we have more capable variables and the |
| 129 |
* full-fledged scripting engine available. So we have opted to do the interim |
| 130 |
* solution so that our users can begin to enjoy authenticated TLS. The next step |
| 131 |
* (hopefully) is to enhance RainerScript. -- rgerhards, 2008-05-19 |
| 132 |
*/ |
| 133 |
struct permittedPeers_s { |
| 134 |
uchar *pszID; |
| 135 |
enum { |
| 136 |
PERM_PEER_TYPE_UNDECIDED = 0, /**< we have not yet decided the type (fine in some auth modes) */ |
| 137 |
PERM_PEER_TYPE_PLAIN = 1, /**< just plain text contained */ |
| 138 |
PERM_PEER_TYPE_WILDCARD = 2, /**< wildcards are contained, wildcard struture is filled */ |
| 139 |
} etryType; |
| 140 |
permittedPeers_t *pNext; |
| 141 |
permittedPeerWildcard_t *pWildcardRoot; /**< root of the wildcard, NULL if not initialized */ |
| 142 |
permittedPeerWildcard_t *pWildcardLast; /**< end of the wildcard list, NULL if not initialized */ |
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
/* interfaces */ |
| 147 |
BEGINinterface(net) /* name must also be changed in ENDinterface macro! */ |
| 148 |
rsRetVal (*cvthname)(struct sockaddr_storage *f, prop_t **localName, prop_t **fqdn, prop_t **ip); |
| 149 |
/* things to go away after proper modularization */ |
| 150 |
rsRetVal (*addAllowedSenderLine)(char *pName, uchar **ppRestOfConfLine); |
| 151 |
void (*PrintAllowedSenders)(int iListToPrint); |
| 152 |
void (*clearAllowedSenders)(uchar *); |
| 153 |
void (*debugListenInfo)(int fd, char *type); |
| 154 |
int *(*create_udp_socket)(uchar *hostname, uchar *LogPort, int bIsServer, int rcvbuf, int sndbuf, int ipfreebind, |
| 155 |
char *device); |
| 156 |
void (*closeUDPListenSockets)(int *finet); |
| 157 |
int (*isAllowedSender)(uchar *pszType, struct sockaddr *pFrom, const char *pszFromHost); /* deprecated! */ |
| 158 |
rsRetVal (*getLocalHostname)(rsconf_t *const, uchar **); |
| 159 |
int (*should_use_so_bsdcompat)(void); |
| 160 |
/* permitted peer handling should be replaced by something better (see comments above) */ |
| 161 |
rsRetVal (*AddPermittedPeer)(permittedPeers_t **ppRootPeer, uchar *pszID); |
| 162 |
rsRetVal (*DestructPermittedPeers)(permittedPeers_t **ppRootPeer); |
| 163 |
rsRetVal (*PermittedPeerWildcardMatch)(permittedPeers_t *pPeer, const uchar *pszNameToMatch, int *pbIsMatching); |
| 164 |
/* v5 interface additions */ |
| 165 |
int (*CmpHost)(struct sockaddr_storage *, struct sockaddr_storage *, size_t); |
| 166 |
/* v6 interface additions - 2009-11-16 */ |
| 167 |
rsRetVal (*HasRestrictions)(uchar *, int *bHasRestrictions); |
| 168 |
int (*isAllowedSender2)(uchar *pszType, struct sockaddr *pFrom, const char *pszFromHost, int bChkDNS); |
| 169 |
/* v7 interface additions - 2012-03-06 */ |
| 170 |
rsRetVal (*GetIFIPAddr)(uchar *szif, int family, uchar *pszbuf, int lenBuf); |
| 171 |
/* v8 cvthname() signature change -- rgerhards, 2013-01-18 */ |
| 172 |
/* v9 create_udp_socket() signature change -- dsahern, 2016-11-11 */ |
| 173 |
/* v10 moved data members to rsconf_t -- alakatos, 2021-12-29 */ |
| 174 |
|
| 175 |
/* v11 netns functions -- balsup, 2025-09-11 */ |
| 176 |
/* |
| 177 |
* @brief Open a socket on the given namespace |
| 178 |
* @param fdp A place to store the descriptor. This must not be NULL. |
| 179 |
* A failure will store -1 here. |
| 180 |
* @param domain The communication domain argument to the underlying socket call |
| 181 |
* @param type The type argument to the underlying socket call |
| 182 |
* @param protocol The protocol argument to the underlying socket call |
| 183 |
* @param ns The desired namespace. This may be NULL or the empty string if |
| 184 |
* the current namespace is desired. |
| 185 |
* @return RS_RET_OK on success, otherwise a failure code. |
| 186 |
* @details This is a wrapper to socket, allowing one to open a socket in |
| 187 |
* a given namespace. For platforms that do not support network |
| 188 |
* namespaces, an error will be returned if the ns parameter |
| 189 |
* is not NULL or the empty string. |
| 190 |
*/ |
| 191 |
rsRetVal (*netns_socket)(int *fdp, int domain, int type, int protocol, const char *ns); |
| 192 |
|
| 193 |
/* |
| 194 |
* @brief Switch to the given network namespace |
| 195 |
* @param ns The desired namespace. If this is NULL or the empty string, then |
| 196 |
* this function is a no-op. |
| 197 |
* @return RS_RET_OK on success, otherwise a failure code. |
| 198 |
* @details For platforms that do not support network namespaces, an error will |
| 199 |
* be returned if the ns parameter is not NULL or the empty string. |
| 200 |
*/ |
| 201 |
rsRetVal (*netns_switch)(const char *ns); |
| 202 |
|
| 203 |
/* |
| 204 |
* @brief Save a descriptor to the current network namespace |
| 205 |
* @param fd The location to store the descriptor for the current |
| 206 |
* namespace. This must not not be NULL, and the |
| 207 |
* descriptor must be pre-initialized to -1, i.e. *fd == -1 |
| 208 |
* is a precondition. This style is to prevent inadvertent |
| 209 |
* descriptor leaks that might arise by overwriting a valid |
| 210 |
* descriptor. |
| 211 |
* @return RS_RET_OK on success, otherwise a failure code. |
| 212 |
* @details For platforms that do not support network namespaces, this |
| 213 |
* function is a no-op and will always return RS_RET_OK. |
| 214 |
*/ |
| 215 |
rsRetVal (*netns_save)(int *fd); |
| 216 |
|
| 217 |
/* |
| 218 |
* @brief Restore the original network namespace |
| 219 |
* @param fd A pointer to a descriptor associated with the original |
| 220 |
* namespace. This must not not be NULL. If the descriptor |
| 221 |
* is -1, then this function is a no-op. A valid descriptor |
| 222 |
* is always closed as a side-effect of this function, |
| 223 |
* with the descriptor being updated to -1. |
| 224 |
* @return RS_RET_OK on success, otherwise a failure code. |
| 225 |
* @details For platforms that do not support network namespaces, this |
| 226 |
* function cannot change the network namespace. However, if |
| 227 |
* presented with an fd that is not -1, it will still close |
| 228 |
* that fd and reset the value to -1. |
| 229 |
*/ |
| 230 |
rsRetVal (*netns_restore)(int *fd); |
| 231 |
ENDinterface(net) |
| 232 |
#define netCURR_IF_VERSION 11 /* increment whenever you change the interface structure! */ |
| 233 |
|
| 234 |
/* prototypes */ |
| 235 |
PROTOTYPEObj(net); |
| 236 |
|
| 237 |
/* the name of our library binary */ |
| 238 |
#define LM_NET_FILENAME "lmnet" |
| 239 |
|
| 240 |
#endif /* #ifndef INCLUDED_NET_H */ |