xref: /freebsd/share/man/man4/ng_patch.4 (revision 426b3d047a189fc9518c95cf3d7c90b000ff82f8)
1d359a62dSAndrey V. Elsukov.\" Copyright (c) 2010 Maxim Ignatenko <gelraen.ua@gmail.com>
2d359a62dSAndrey V. Elsukov.\" Copyright (c) 2010 Vadim Goncharov <vadimnuclight@tpu.ru>
3*426b3d04SJulian Elischer.\" Copyright (c) 2015 Dmitry Vagin <daemon.hammer@ya.ru>
4d359a62dSAndrey V. Elsukov.\" All rights reserved.
5d359a62dSAndrey V. Elsukov.\"
6d359a62dSAndrey V. Elsukov.\" Redistribution and use in source and binary forms, with or without
7d359a62dSAndrey V. Elsukov.\" modification, are permitted provided that the following conditions
8d359a62dSAndrey V. Elsukov.\" are met:
9d359a62dSAndrey V. Elsukov.\" 1. Redistributions of source code must retain the above copyright
10d359a62dSAndrey V. Elsukov.\"    notice, this list of conditions and the following disclaimer.
11d359a62dSAndrey V. Elsukov.\" 2. Redistributions in binary form must reproduce the above copyright
12d359a62dSAndrey V. Elsukov.\"    notice, this list of conditions and the following disclaimer in the
13d359a62dSAndrey V. Elsukov.\"    documentation and/or other materials provided with the distribution.
14d359a62dSAndrey V. Elsukov.\"
15d359a62dSAndrey V. Elsukov.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16d359a62dSAndrey V. Elsukov.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17d359a62dSAndrey V. Elsukov.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18d359a62dSAndrey V. Elsukov.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19d359a62dSAndrey V. Elsukov.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20d359a62dSAndrey V. Elsukov.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21d359a62dSAndrey V. Elsukov.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22d359a62dSAndrey V. Elsukov.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23d359a62dSAndrey V. Elsukov.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24d359a62dSAndrey V. Elsukov.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25d359a62dSAndrey V. Elsukov.\" SUCH DAMAGE.
26d359a62dSAndrey V. Elsukov.\"
27d359a62dSAndrey V. Elsukov.\" $FreeBSD$
28d359a62dSAndrey V. Elsukov.\"
29*426b3d04SJulian Elischer.Dd November 17, 2015
30d359a62dSAndrey V. Elsukov.Dt NG_PATCH 4
31d359a62dSAndrey V. Elsukov.Os
32d359a62dSAndrey V. Elsukov.Sh NAME
33d359a62dSAndrey V. Elsukov.Nm ng_patch
34d359a62dSAndrey V. Elsukov.Nd "trivial mbuf data modifying netgraph node type"
35d359a62dSAndrey V. Elsukov.Sh SYNOPSIS
36d359a62dSAndrey V. Elsukov.In netgraph/ng_patch.h
37d359a62dSAndrey V. Elsukov.Sh DESCRIPTION
38d359a62dSAndrey V. ElsukovThe
39d359a62dSAndrey V. Elsukov.Nm patch
40d359a62dSAndrey V. Elsukovnode performs data modification of packets passing through it.
41d359a62dSAndrey V. ElsukovModifications are restricted to a subset of C language operations
42d359a62dSAndrey V. Elsukovon unsigned integers of 8, 16, 32 or 64 bit size.
43d359a62dSAndrey V. ElsukovThese are: set to new value (=), addition (+=), subtraction (-=),
44d359a62dSAndrey V. Elsukovmultiplication (*=), division (/=), negation (= -),
45d359a62dSAndrey V. Elsukovbitwise AND (&=), bitwise OR (|=), bitwise eXclusive OR (^=),
46d359a62dSAndrey V. Elsukovshift left (<<=), shift right (>>=).
47d359a62dSAndrey V. ElsukovA negation operation is the one exception: integer is treated as signed
48d359a62dSAndrey V. Elsukovand second operand (the
49d359a62dSAndrey V. Elsukov.Va value )
50d359a62dSAndrey V. Elsukovis not used.
51*426b3d04SJulian ElischerIf there is more than one modification operation, they are applied
52*426b3d04SJulian Elischerto packets sequentially in the order they were specified by the user.
53*426b3d04SJulian ElischerThe data payload of a packet is viewed as an array of bytes, with a zero offset
54*426b3d04SJulian Elischercorresponding to the very first byte of packet headers, and the
55d359a62dSAndrey V. Elsukov.Va length
56d359a62dSAndrey V. Elsukovbytes beginning from
57d359a62dSAndrey V. Elsukov.Va offset
58*426b3d04SJulian Elischeras a single integer in network byte order. An additional offset can be optionally
59*426b3d04SJulian Elischerrequested at configuration time to account for packet type.
60d359a62dSAndrey V. Elsukov.Sh HOOKS
61d359a62dSAndrey V. ElsukovThis node type has two hooks:
62c60bda17SJoel Dahl.Bl -tag -width ".Va out"
63d359a62dSAndrey V. Elsukov.It Va in
64d359a62dSAndrey V. ElsukovPackets received on this hook are modified according to rules specified
65*426b3d04SJulian Elischerin the configuration and then forwarded to the
66d359a62dSAndrey V. Elsukov.Ar out
67*426b3d04SJulian Elischerhook, if it exists.
68d359a62dSAndrey V. ElsukovOtherwise they are reflected back to the
69d359a62dSAndrey V. Elsukov.Ar in
70d359a62dSAndrey V. Elsukovhook.
71d359a62dSAndrey V. Elsukov.It Va out
72*426b3d04SJulian ElischerPackets received on this hook are forwarded to the
73d359a62dSAndrey V. Elsukov.Ar in
74d359a62dSAndrey V. Elsukovhook without any changes.
75d359a62dSAndrey V. Elsukov.El
76d359a62dSAndrey V. Elsukov.Sh CONTROL MESSAGES
77d359a62dSAndrey V. ElsukovThis node type supports the generic control messages, plus the following:
78c60bda17SJoel Dahl.Bl -tag -width foo
79*426b3d04SJulian Elischer.It Dv NGM_PATCH_SETDLT Pq Ic setdlt
80*426b3d04SJulian ElischerSets the data link type on the
81*426b3d04SJulian Elischer.Va in
82*426b3d04SJulian Elischerhook (to help calculate relative offset). Currently, supported types are
83*426b3d04SJulian Elischer.Cm DLT_RAW
84*426b3d04SJulian Elischer(raw IP datagrams , no offset applied, the default) and
85*426b3d04SJulian Elischer.Cm DLT_EN10MB
86*426b3d04SJulian Elischer(Ethernet). DLT_ definitions can be found in
87*426b3d04SJulian Elischer.In net/bpf.h .
88*426b3d04SJulian ElischerIf you want to work on the link layer header you must use no additional offset by specifying
89*426b3d04SJulian Elischer.Cm DLT_RAW .
90*426b3d04SJulian ElischerIf
91*426b3d04SJulian Elischer.Cm EN10MB
92*426b3d04SJulian Elischeris specified, then the optional additional offset will take into account the Ethernet header and a QinQ header if present.
93*426b3d04SJulian Elischer.It Dv NGM_PATCH_GETDLT Pq Ic getdlt
94*426b3d04SJulian ElischerThis control message returns the data link type of the
95*426b3d04SJulian Elischer.Va in
96*426b3d04SJulian Elischerhook.
97c60bda17SJoel Dahl.It Dv NGM_PATCH_SETCONFIG Pq Ic setconfig
98d359a62dSAndrey V. ElsukovThis command sets the sequence of modify operations
99d359a62dSAndrey V. Elsukovthat will be applied to incoming data on a hook.
100d359a62dSAndrey V. ElsukovThe following
101d359a62dSAndrey V. Elsukov.Vt "struct ng_patch_config"
102d359a62dSAndrey V. Elsukovmust be supplied as an argument:
103d359a62dSAndrey V. Elsukov.Bd -literal -offset 4n
104d359a62dSAndrey V. Elsukovstruct ng_patch_op {
105d359a62dSAndrey V. Elsukov	uint32_t	offset;
106d359a62dSAndrey V. Elsukov	uint16_t	length; /* 1,2,4 or 8 bytes */
107d359a62dSAndrey V. Elsukov	uint16_t	mode;
108*426b3d04SJulian Elischer	uint64_t	value;
109d359a62dSAndrey V. Elsukov};
110d359a62dSAndrey V. Elsukov/* Patching modes */
111d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_SET	1
112d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_ADD	2
113d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_SUB	3
114d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_MUL	4
115d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_DIV	5
116d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_NEG	6
117d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_AND	7
118d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_OR	8
119d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_XOR	9
120d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_SHL	10
121d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_SHR	11
122d359a62dSAndrey V. Elsukov
123d359a62dSAndrey V. Elsukovstruct ng_patch_config {
124d359a62dSAndrey V. Elsukov	uint32_t	count;
125d359a62dSAndrey V. Elsukov	uint32_t	csum_flags;
126*426b3d04SJulian Elischer	uint32_t	relative_offset;
127d359a62dSAndrey V. Elsukov	struct ng_patch_op ops[];
128d359a62dSAndrey V. Elsukov};
129d359a62dSAndrey V. Elsukov.Ed
130d359a62dSAndrey V. Elsukov.Pp
131d359a62dSAndrey V. ElsukovThe
132d359a62dSAndrey V. Elsukov.Va csum_flags
133d359a62dSAndrey V. Elsukovcan be set to any combination of CSUM_IP, CSUM_TCP, CSUM_SCTP and CSUM_UDP
134d359a62dSAndrey V. Elsukov(other values are ignored) for instructing the IP stack to recalculate the
135d359a62dSAndrey V. Elsukovcorresponding checksum before transmitting packet on output interface.
136d359a62dSAndrey V. ElsukovThe
137d359a62dSAndrey V. Elsukov.Nm
138d359a62dSAndrey V. Elsukovnode does not do any checksum correction by itself.
139c60bda17SJoel Dahl.It Dv NGM_PATCH_GETCONFIG Pq Ic getconfig
140*426b3d04SJulian ElischerThis control message returns the current set of modify operations,
141*426b3d04SJulian Elischerin the form of a
142d359a62dSAndrey V. Elsukov.Vt "struct ng_patch_config" .
143c60bda17SJoel Dahl.It Dv NGM_PATCH_GET_STATS Pq Ic getstats
144*426b3d04SJulian ElischerReturns the node's statistics as a
145d359a62dSAndrey V. Elsukov.Vt "struct ng_patch_stats" .
146c60bda17SJoel Dahl.It Dv NGM_PATCH_CLR_STATS Pq Ic clrstats
147*426b3d04SJulian ElischerClears the node's statistics.
148c60bda17SJoel Dahl.It Dv NGM_PATCH_GETCLR_STATS Pq Ic getclrstats
149d359a62dSAndrey V. ElsukovThis command is identical to
150d359a62dSAndrey V. Elsukov.Dv NGM_PATCH_GET_STATS ,
151d359a62dSAndrey V. Elsukovexcept that the statistics are also atomically cleared.
152d359a62dSAndrey V. Elsukov.El
153d359a62dSAndrey V. Elsukov.Sh SHUTDOWN
154d359a62dSAndrey V. ElsukovThis node shuts down upon receipt of a
155d359a62dSAndrey V. Elsukov.Dv NGM_SHUTDOWN
156d359a62dSAndrey V. Elsukovcontrol message, or when all hooks have been disconnected.
157d359a62dSAndrey V. Elsukov.Sh EXAMPLES
158*426b3d04SJulian ElischerThis
159d359a62dSAndrey V. Elsukov.Nm
160*426b3d04SJulian Elischernode was designed to modify TTL and TOS/DSCP fields in IP packets.
161*426b3d04SJulian ElischerAs an example,
162*426b3d04SJulian Elischersuppose you have two adjacent simplex links to a remote network
163d359a62dSAndrey V. Elsukov(e.g.\& satellite), so that the packets expiring in between
164d359a62dSAndrey V. Elsukovwill generate unwanted ICMP-replies which have to go forth, not back.
1650dded339SGlen BarberThus you need to raise TTL of every packet entering link by 2
166d359a62dSAndrey V. Elsukovto ensure the TTL will not reach zero there.
167*426b3d04SJulian ElischerTo achieve this you can set an
168d359a62dSAndrey V. Elsukov.Xr ipfw 8
169*426b3d04SJulian Elischerrule to use the
170d359a62dSAndrey V. Elsukov.Cm netgraph
171*426b3d04SJulian Elischeraction to inject packets which are going to the simplex link into the patch node, by using the
172d359a62dSAndrey V. Elsukovfollowing
173d359a62dSAndrey V. Elsukov.Xr ngctl 8
174d359a62dSAndrey V. Elsukovscript:
175d359a62dSAndrey V. Elsukov.Bd -literal -offset 4n
176d359a62dSAndrey V. Elsukov/usr/sbin/ngctl -f- <<-SEQ
177d359a62dSAndrey V. Elsukov	mkpeer ipfw: patch 200 in
178d359a62dSAndrey V. Elsukov	name ipfw:200 ttl_add
179d359a62dSAndrey V. Elsukov	msg ttl_add: setconfig { count=1 csum_flags=1 ops=[	\e
180d359a62dSAndrey V. Elsukov		{ mode=2 value=3 length=1 offset=8 } ] }
181d359a62dSAndrey V. ElsukovSEQ
182d359a62dSAndrey V. Elsukov/sbin/ipfw add 150 netgraph 200 ip from any to simplex.remote.net
183d359a62dSAndrey V. Elsukov.Ed
184d359a62dSAndrey V. Elsukov.Pp
185*426b3d04SJulian ElischerHere the
186d359a62dSAndrey V. Elsukov.Dq Li ttl_add
187d359a62dSAndrey V. Elsukovnode of type
188d359a62dSAndrey V. Elsukov.Nm
189*426b3d04SJulian Elischeris configured to add (mode
190d359a62dSAndrey V. Elsukov.Dv NG_PATCH_MODE_ADD )
191d359a62dSAndrey V. Elsukova
192d359a62dSAndrey V. Elsukov.Va value
193d359a62dSAndrey V. Elsukovof 3 to a one-byte TTL field, which is 9th byte of IP packet header.
194d359a62dSAndrey V. Elsukov.Pp
195d359a62dSAndrey V. ElsukovAnother example would be two consecutive modifications of packet TOS
196d359a62dSAndrey V. Elsukovfield: say, you need to clear the
197d359a62dSAndrey V. Elsukov.Dv IPTOS_THROUGHPUT
198d359a62dSAndrey V. Elsukovbit and set the
199d359a62dSAndrey V. Elsukov.Dv IPTOS_MINCOST
200d359a62dSAndrey V. Elsukovbit.
201d359a62dSAndrey V. ElsukovSo you do:
202d359a62dSAndrey V. Elsukov.Bd -literal -offset 4n
203d359a62dSAndrey V. Elsukov/usr/sbin/ngctl -f- <<-SEQ
204d359a62dSAndrey V. Elsukov	mkpeer ipfw: patch 300 in
205d359a62dSAndrey V. Elsukov	name ipfw:300 tos_chg
206d359a62dSAndrey V. Elsukov	msg tos_chg: setconfig { count=2 csum_flags=1 ops=[	\e
207d359a62dSAndrey V. Elsukov		{ mode=7 value=0xf7 length=1 offset=1 }		\e
208d359a62dSAndrey V. Elsukov		{ mode=8 value=0x02 length=1 offset=1 } ] }
209d359a62dSAndrey V. ElsukovSEQ
2102875b4b9SGleb Smirnoff/sbin/ipfw add 160 netgraph 300 ip from any to any not dst-port 80
211d359a62dSAndrey V. Elsukov.Ed
212d359a62dSAndrey V. Elsukov.Pp
213d359a62dSAndrey V. ElsukovThis first does
214d359a62dSAndrey V. Elsukov.Dv NG_PATCH_MODE_AND
215d359a62dSAndrey V. Elsukovclearing the fourth bit and then
216d359a62dSAndrey V. Elsukov.Dv NG_PATCH_MODE_OR
217d359a62dSAndrey V. Elsukovsetting the third bit.
218d359a62dSAndrey V. Elsukov.Pp
219d359a62dSAndrey V. ElsukovIn both examples the
220d359a62dSAndrey V. Elsukov.Va csum_flags
221d359a62dSAndrey V. Elsukovfield indicates that IP checksum (but not TCP or UDP checksum) should be
222d359a62dSAndrey V. Elsukovrecalculated before transmit.
223d359a62dSAndrey V. Elsukov.Pp
224d359a62dSAndrey V. ElsukovNote: one should ensure that packets are returned to ipfw after processing
225d359a62dSAndrey V. Elsukovinside
226d359a62dSAndrey V. Elsukov.Xr netgraph 4 ,
227d359a62dSAndrey V. Elsukovby setting appropriate
228d359a62dSAndrey V. Elsukov.Xr sysctl 8
229d359a62dSAndrey V. Elsukovvariable:
230d359a62dSAndrey V. Elsukov.Bd -literal -offset 4n
231d359a62dSAndrey V. Elsukovsysctl net.inet.ip.fw.one_pass=0
232d359a62dSAndrey V. Elsukov.Ed
233d359a62dSAndrey V. Elsukov.Sh SEE ALSO
234d359a62dSAndrey V. Elsukov.Xr netgraph 4 ,
235d359a62dSAndrey V. Elsukov.Xr ng_ipfw 4 ,
236d359a62dSAndrey V. Elsukov.Xr ngctl 8
237d359a62dSAndrey V. Elsukov.Sh HISTORY
238d359a62dSAndrey V. ElsukovThe
239d359a62dSAndrey V. Elsukov.Nm
240d359a62dSAndrey V. Elsukovnode type was implemented in
241d359a62dSAndrey V. Elsukov.Fx 8.1 .
242d359a62dSAndrey V. Elsukov.Sh AUTHORS
243*426b3d04SJulian Elischer.An "Maxim Ignatenko" Aq gelraen.ua@gmail.com .
2446c899950SBaptiste Daroussin.Pp
245*426b3d04SJulian ElischerRelative offset code by
246*426b3d04SJulian Elischer.An "DMitry Vagin"
247*426b3d04SJulian Elischer.Pp
248d359a62dSAndrey V. ElsukovThis manual page was written by
249*426b3d04SJulian Elischer.An "Vadim Goncharov" Aq vadimnuclight@tpu.ru .
250d359a62dSAndrey V. Elsukov.Sh BUGS
251*426b3d04SJulian ElischerThe node blindly tries to apply every patching operation to each packet
252d359a62dSAndrey V. Elsukov(except those which offset if greater than length of the packet),
253d359a62dSAndrey V. Elsukovso be sure that you supply only the right packets to it (e.g. changing
254d359a62dSAndrey V. Elsukovbytes in the ARP packets meant to be in IP header could corrupt
255d359a62dSAndrey V. Elsukovthem and make your machine unreachable from the network).
256d359a62dSAndrey V. Elsukov.Pp
257d359a62dSAndrey V. Elsukov.Em !!! WARNING !!!
258d359a62dSAndrey V. Elsukov.Pp
259*426b3d04SJulian ElischerThe output path of the IP stack assumes correct fields and lengths in the
260*426b3d04SJulian Elischerpackets - changing them by to incorrect values can cause
261d359a62dSAndrey V. Elsukovunpredictable results including kernel panics.
262