1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
| | /* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright Red Hat
* Author: Stefano Brivio <sbrivio@redhat.com>
* Author: David Gibson <david@gibson.dropbear.id.au>
*
* Forwarding rule definitions shared between passt/pasta and pesto
*/
#ifndef FWD_RULE_H
#define FWD_RULE_H
#include <stdint.h>
#include <net/if.h>
#include <netinet/in.h>
#include "common.h"
#include "inany.h"
/**
* struct fwd_rule - Forwarding rule governing a range of ports
* @addr: Address to forward from
* @ifname: Interface to forward from
* @first: First port number to forward
* @last: Last port number to forward
* @to: Target port for @first, port n goes to @to + (n - @first)
* @proto: Protocol to forward
* @flags: Flag mask
* FWD_DUAL_STACK_ANY - match any IPv4 or IPv6 address (@addr should be ::)
* FWD_WEAK - Don't give an error if binds fail for some forwards
* FWD_SCAN - Only forward if the matching port in the target is listening
*/
struct fwd_rule {
union inany_addr addr;
char ifname[IFNAMSIZ];
in_port_t first;
in_port_t last;
in_port_t to;
uint8_t proto;
#define FWD_DUAL_STACK_ANY BIT(0)
#define FWD_WEAK BIT(1)
#define FWD_SCAN BIT(2)
uint8_t flags;
};
/**
* struct fwd_rule_state - Forwarding rule and associated state
* @rule: Rule specification
* @socks: Array of listening sockets for this entry
*/
struct fwd_rule_state {
struct fwd_rule rule;
int *socks;
};
#define FWD_RULE_BITS 8
#define MAX_FWD_RULES MAX_FROM_BITS(FWD_RULE_BITS)
#define FWD_NO_HINT (-1)
/**
* struct fwd_listen_ref - information about a single listening socket
* @port: Bound port number of the socket
* @pif: pif in which the socket is listening
* @rule: Index of forwarding rule
*/
struct fwd_listen_ref {
in_port_t port;
uint8_t pif;
unsigned rule :FWD_RULE_BITS;
};
/* Maximum number of listening sockets (per pif)
*
* Rationale: This lets us listen on every port for two addresses and two
* protocols (which we need for -T auto -U auto without SO_BINDTODEVICE), plus a
* comfortable number of extras.
*/
#define MAX_LISTEN_SOCKS (NUM_PORTS * 5)
/**
* struct fwd_table - Table of forwarding rules (per initiating pif)
* @count: Number of forwarding rules
* @rules: Array of forwarding rules
* @sock_count: Number of entries used in @socks
* @socks: Listening sockets for forwarding
*/
struct fwd_table {
unsigned count;
struct fwd_rule_state rules[MAX_FWD_RULES];
unsigned sock_count;
int socks[MAX_LISTEN_SOCKS];
};
#define PORT_BITMAP_SIZE DIV_ROUND_UP(NUM_PORTS, 8)
const union inany_addr *fwd_rule_addr(const struct fwd_rule *rule);
void fwd_rule_add(struct fwd_table *fwd, uint8_t proto, uint8_t flags,
const union inany_addr *addr, const char *ifname,
in_port_t first, in_port_t last, in_port_t to);
#define FWD_RULE_STRLEN \
(IPPROTO_STRLEN - 1 \
+ INANY_ADDRSTRLEN - 1 \
+ IFNAMSIZ - 1 \
+ sizeof(" (best effort)") - 1 \
+ sizeof(" (auto-scan)") - 1 \
+ 15)
const char *fwd_rule_ntop(const struct fwd_rule *rule, char *dst, size_t size);
int fwd_rule_seread(int fd, struct fwd_rule *rule);
int fwd_rule_sewrite(int fd, const struct fwd_rule *rule);
#endif /* FWD_RULE_H */
|