xref: /freebsd/share/man/man4/ng_patch.4 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
1d359a62dSAndrey V. Elsukov.\" Copyright (c) 2010 Maxim Ignatenko <gelraen.ua@gmail.com>
2d359a62dSAndrey V. Elsukov.\" Copyright (c) 2010 Vadim Goncharov <vadimnuclight@tpu.ru>
3426b3d04SJulian 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.\"
27426b3d04SJulian Elischer.Dd November 17, 2015
28d359a62dSAndrey V. Elsukov.Dt NG_PATCH 4
29d359a62dSAndrey V. Elsukov.Os
30d359a62dSAndrey V. Elsukov.Sh NAME
31d359a62dSAndrey V. Elsukov.Nm ng_patch
32d359a62dSAndrey V. Elsukov.Nd "trivial mbuf data modifying netgraph node type"
33d359a62dSAndrey V. Elsukov.Sh SYNOPSIS
34d359a62dSAndrey V. Elsukov.In netgraph/ng_patch.h
35d359a62dSAndrey V. Elsukov.Sh DESCRIPTION
36d359a62dSAndrey V. ElsukovThe
37d359a62dSAndrey V. Elsukov.Nm patch
38d359a62dSAndrey V. Elsukovnode performs data modification of packets passing through it.
39d359a62dSAndrey V. ElsukovModifications are restricted to a subset of C language operations
40d359a62dSAndrey V. Elsukovon unsigned integers of 8, 16, 32 or 64 bit size.
41d359a62dSAndrey V. ElsukovThese are: set to new value (=), addition (+=), subtraction (-=),
42d359a62dSAndrey V. Elsukovmultiplication (*=), division (/=), negation (= -),
43d359a62dSAndrey V. Elsukovbitwise AND (&=), bitwise OR (|=), bitwise eXclusive OR (^=),
44d359a62dSAndrey V. Elsukovshift left (<<=), shift right (>>=).
45d359a62dSAndrey V. ElsukovA negation operation is the one exception: integer is treated as signed
46d359a62dSAndrey V. Elsukovand second operand (the
47d359a62dSAndrey V. Elsukov.Va value )
48d359a62dSAndrey V. Elsukovis not used.
49426b3d04SJulian ElischerIf there is more than one modification operation, they are applied
50426b3d04SJulian Elischerto packets sequentially in the order they were specified by the user.
51426b3d04SJulian ElischerThe data payload of a packet is viewed as an array of bytes, with a zero offset
52426b3d04SJulian Elischercorresponding to the very first byte of packet headers, and the
53d359a62dSAndrey V. Elsukov.Va length
54d359a62dSAndrey V. Elsukovbytes beginning from
55d359a62dSAndrey V. Elsukov.Va offset
56*d458747eSChristian Bruefferas a single integer in network byte order.
57*d458747eSChristian BruefferAn additional offset can be optionally
58426b3d04SJulian Elischerrequested at configuration time to account for packet type.
59d359a62dSAndrey V. Elsukov.Sh HOOKS
60d359a62dSAndrey V. ElsukovThis node type has two hooks:
61c60bda17SJoel Dahl.Bl -tag -width ".Va out"
62d359a62dSAndrey V. Elsukov.It Va in
63d359a62dSAndrey V. ElsukovPackets received on this hook are modified according to rules specified
64426b3d04SJulian Elischerin the configuration and then forwarded to the
65d359a62dSAndrey V. Elsukov.Ar out
66426b3d04SJulian Elischerhook, if it exists.
67d359a62dSAndrey V. ElsukovOtherwise they are reflected back to the
68d359a62dSAndrey V. Elsukov.Ar in
69d359a62dSAndrey V. Elsukovhook.
70d359a62dSAndrey V. Elsukov.It Va out
71426b3d04SJulian ElischerPackets received on this hook are forwarded to the
72d359a62dSAndrey V. Elsukov.Ar in
73d359a62dSAndrey V. Elsukovhook without any changes.
74d359a62dSAndrey V. Elsukov.El
75d359a62dSAndrey V. Elsukov.Sh CONTROL MESSAGES
76d359a62dSAndrey V. ElsukovThis node type supports the generic control messages, plus the following:
77c60bda17SJoel Dahl.Bl -tag -width foo
78426b3d04SJulian Elischer.It Dv NGM_PATCH_SETDLT Pq Ic setdlt
79426b3d04SJulian ElischerSets the data link type on the
80426b3d04SJulian Elischer.Va in
81426b3d04SJulian Elischerhook (to help calculate relative offset). Currently, supported types are
82426b3d04SJulian Elischer.Cm DLT_RAW
83426b3d04SJulian Elischer(raw IP datagrams , no offset applied, the default) and
84426b3d04SJulian Elischer.Cm DLT_EN10MB
85426b3d04SJulian Elischer(Ethernet). DLT_ definitions can be found in
86426b3d04SJulian Elischer.In net/bpf.h .
87426b3d04SJulian ElischerIf you want to work on the link layer header you must use no additional offset by specifying
88426b3d04SJulian Elischer.Cm DLT_RAW .
89426b3d04SJulian ElischerIf
90426b3d04SJulian Elischer.Cm EN10MB
91426b3d04SJulian Elischeris specified, then the optional additional offset will take into account the Ethernet header and a QinQ header if present.
92426b3d04SJulian Elischer.It Dv NGM_PATCH_GETDLT Pq Ic getdlt
93426b3d04SJulian ElischerThis control message returns the data link type of the
94426b3d04SJulian Elischer.Va in
95426b3d04SJulian Elischerhook.
96c60bda17SJoel Dahl.It Dv NGM_PATCH_SETCONFIG Pq Ic setconfig
97d359a62dSAndrey V. ElsukovThis command sets the sequence of modify operations
98d359a62dSAndrey V. Elsukovthat will be applied to incoming data on a hook.
99d359a62dSAndrey V. ElsukovThe following
100d359a62dSAndrey V. Elsukov.Vt "struct ng_patch_config"
101d359a62dSAndrey V. Elsukovmust be supplied as an argument:
102d359a62dSAndrey V. Elsukov.Bd -literal -offset 4n
103d359a62dSAndrey V. Elsukovstruct ng_patch_op {
104d359a62dSAndrey V. Elsukov	uint32_t	offset;
105d359a62dSAndrey V. Elsukov	uint16_t	length; /* 1,2,4 or 8 bytes */
106d359a62dSAndrey V. Elsukov	uint16_t	mode;
107426b3d04SJulian Elischer	uint64_t	value;
108d359a62dSAndrey V. Elsukov};
109d359a62dSAndrey V. Elsukov/* Patching modes */
110d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_SET	1
111d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_ADD	2
112d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_SUB	3
113d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_MUL	4
114d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_DIV	5
115d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_NEG	6
116d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_AND	7
117d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_OR	8
118d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_XOR	9
119d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_SHL	10
120d359a62dSAndrey V. Elsukov#define NG_PATCH_MODE_SHR	11
121d359a62dSAndrey V. Elsukov
122d359a62dSAndrey V. Elsukovstruct ng_patch_config {
123d359a62dSAndrey V. Elsukov	uint32_t	count;
124d359a62dSAndrey V. Elsukov	uint32_t	csum_flags;
125426b3d04SJulian Elischer	uint32_t	relative_offset;
126d359a62dSAndrey V. Elsukov	struct ng_patch_op ops[];
127d359a62dSAndrey V. Elsukov};
128d359a62dSAndrey V. Elsukov.Ed
129d359a62dSAndrey V. Elsukov.Pp
130d359a62dSAndrey V. ElsukovThe
131d359a62dSAndrey V. Elsukov.Va csum_flags
132d359a62dSAndrey V. Elsukovcan be set to any combination of CSUM_IP, CSUM_TCP, CSUM_SCTP and CSUM_UDP
133d359a62dSAndrey V. Elsukov(other values are ignored) for instructing the IP stack to recalculate the
134d359a62dSAndrey V. Elsukovcorresponding checksum before transmitting packet on output interface.
135d359a62dSAndrey V. ElsukovThe
136d359a62dSAndrey V. Elsukov.Nm
137d359a62dSAndrey V. Elsukovnode does not do any checksum correction by itself.
138c60bda17SJoel Dahl.It Dv NGM_PATCH_GETCONFIG Pq Ic getconfig
139426b3d04SJulian ElischerThis control message returns the current set of modify operations,
140426b3d04SJulian Elischerin the form of a
141d359a62dSAndrey V. Elsukov.Vt "struct ng_patch_config" .
142c60bda17SJoel Dahl.It Dv NGM_PATCH_GET_STATS Pq Ic getstats
143426b3d04SJulian ElischerReturns the node's statistics as a
144d359a62dSAndrey V. Elsukov.Vt "struct ng_patch_stats" .
145c60bda17SJoel Dahl.It Dv NGM_PATCH_CLR_STATS Pq Ic clrstats
146426b3d04SJulian ElischerClears the node's statistics.
147c60bda17SJoel Dahl.It Dv NGM_PATCH_GETCLR_STATS Pq Ic getclrstats
148d359a62dSAndrey V. ElsukovThis command is identical to
149d359a62dSAndrey V. Elsukov.Dv NGM_PATCH_GET_STATS ,
150d359a62dSAndrey V. Elsukovexcept that the statistics are also atomically cleared.
151d359a62dSAndrey V. Elsukov.El
152d359a62dSAndrey V. Elsukov.Sh SHUTDOWN
153d359a62dSAndrey V. ElsukovThis node shuts down upon receipt of a
154d359a62dSAndrey V. Elsukov.Dv NGM_SHUTDOWN
155d359a62dSAndrey V. Elsukovcontrol message, or when all hooks have been disconnected.
156d359a62dSAndrey V. Elsukov.Sh EXAMPLES
157426b3d04SJulian ElischerThis
158d359a62dSAndrey V. Elsukov.Nm
159426b3d04SJulian Elischernode was designed to modify TTL and TOS/DSCP fields in IP packets.
160426b3d04SJulian ElischerAs an example,
161426b3d04SJulian Elischersuppose you have two adjacent simplex links to a remote network
162d359a62dSAndrey V. Elsukov(e.g.\& satellite), so that the packets expiring in between
163d359a62dSAndrey V. Elsukovwill generate unwanted ICMP-replies which have to go forth, not back.
1640dded339SGlen BarberThus you need to raise TTL of every packet entering link by 2
165d359a62dSAndrey V. Elsukovto ensure the TTL will not reach zero there.
166426b3d04SJulian ElischerTo achieve this you can set an
167d359a62dSAndrey V. Elsukov.Xr ipfw 8
168426b3d04SJulian Elischerrule to use the
169d359a62dSAndrey V. Elsukov.Cm netgraph
170426b3d04SJulian Elischeraction to inject packets which are going to the simplex link into the patch node, by using the
171d359a62dSAndrey V. Elsukovfollowing
172d359a62dSAndrey V. Elsukov.Xr ngctl 8
173d359a62dSAndrey V. Elsukovscript:
174d359a62dSAndrey V. Elsukov.Bd -literal -offset 4n
175d359a62dSAndrey V. Elsukov/usr/sbin/ngctl -f- <<-SEQ
176d359a62dSAndrey V. Elsukov	mkpeer ipfw: patch 200 in
177d359a62dSAndrey V. Elsukov	name ipfw:200 ttl_add
178d359a62dSAndrey V. Elsukov	msg ttl_add: setconfig { count=1 csum_flags=1 ops=[	\e
179d359a62dSAndrey V. Elsukov		{ mode=2 value=3 length=1 offset=8 } ] }
180d359a62dSAndrey V. ElsukovSEQ
181d359a62dSAndrey V. Elsukov/sbin/ipfw add 150 netgraph 200 ip from any to simplex.remote.net
182d359a62dSAndrey V. Elsukov.Ed
183d359a62dSAndrey V. Elsukov.Pp
184426b3d04SJulian ElischerHere the
185d359a62dSAndrey V. Elsukov.Dq Li ttl_add
186d359a62dSAndrey V. Elsukovnode of type
187d359a62dSAndrey V. Elsukov.Nm
188426b3d04SJulian Elischeris configured to add (mode
189d359a62dSAndrey V. Elsukov.Dv NG_PATCH_MODE_ADD )
190d359a62dSAndrey V. Elsukova
191d359a62dSAndrey V. Elsukov.Va value
192d359a62dSAndrey V. Elsukovof 3 to a one-byte TTL field, which is 9th byte of IP packet header.
193d359a62dSAndrey V. Elsukov.Pp
194d359a62dSAndrey V. ElsukovAnother example would be two consecutive modifications of packet TOS
195d359a62dSAndrey V. Elsukovfield: say, you need to clear the
196d359a62dSAndrey V. Elsukov.Dv IPTOS_THROUGHPUT
197d359a62dSAndrey V. Elsukovbit and set the
198d359a62dSAndrey V. Elsukov.Dv IPTOS_MINCOST
199d359a62dSAndrey V. Elsukovbit.
200d359a62dSAndrey V. ElsukovSo you do:
201d359a62dSAndrey V. Elsukov.Bd -literal -offset 4n
202d359a62dSAndrey V. Elsukov/usr/sbin/ngctl -f- <<-SEQ
203d359a62dSAndrey V. Elsukov	mkpeer ipfw: patch 300 in
204d359a62dSAndrey V. Elsukov	name ipfw:300 tos_chg
205d359a62dSAndrey V. Elsukov	msg tos_chg: setconfig { count=2 csum_flags=1 ops=[	\e
206d359a62dSAndrey V. Elsukov		{ mode=7 value=0xf7 length=1 offset=1 }		\e
207d359a62dSAndrey V. Elsukov		{ mode=8 value=0x02 length=1 offset=1 } ] }
208d359a62dSAndrey V. ElsukovSEQ
2092875b4b9SGleb Smirnoff/sbin/ipfw add 160 netgraph 300 ip from any to any not dst-port 80
210d359a62dSAndrey V. Elsukov.Ed
211d359a62dSAndrey V. Elsukov.Pp
212d359a62dSAndrey V. ElsukovThis first does
213d359a62dSAndrey V. Elsukov.Dv NG_PATCH_MODE_AND
214d359a62dSAndrey V. Elsukovclearing the fourth bit and then
215d359a62dSAndrey V. Elsukov.Dv NG_PATCH_MODE_OR
216d359a62dSAndrey V. Elsukovsetting the third bit.
217d359a62dSAndrey V. Elsukov.Pp
218d359a62dSAndrey V. ElsukovIn both examples the
219d359a62dSAndrey V. Elsukov.Va csum_flags
220d359a62dSAndrey V. Elsukovfield indicates that IP checksum (but not TCP or UDP checksum) should be
221d359a62dSAndrey V. Elsukovrecalculated before transmit.
222d359a62dSAndrey V. Elsukov.Pp
223d359a62dSAndrey V. ElsukovNote: one should ensure that packets are returned to ipfw after processing
224d359a62dSAndrey V. Elsukovinside
225d359a62dSAndrey V. Elsukov.Xr netgraph 4 ,
226d359a62dSAndrey V. Elsukovby setting appropriate
227d359a62dSAndrey V. Elsukov.Xr sysctl 8
228d359a62dSAndrey V. Elsukovvariable:
229d359a62dSAndrey V. Elsukov.Bd -literal -offset 4n
230d359a62dSAndrey V. Elsukovsysctl net.inet.ip.fw.one_pass=0
231d359a62dSAndrey V. Elsukov.Ed
232d359a62dSAndrey V. Elsukov.Sh SEE ALSO
233d359a62dSAndrey V. Elsukov.Xr netgraph 4 ,
234d359a62dSAndrey V. Elsukov.Xr ng_ipfw 4 ,
235d359a62dSAndrey V. Elsukov.Xr ngctl 8
236d359a62dSAndrey V. Elsukov.Sh HISTORY
237d359a62dSAndrey V. ElsukovThe
238d359a62dSAndrey V. Elsukov.Nm
239d359a62dSAndrey V. Elsukovnode type was implemented in
240d359a62dSAndrey V. Elsukov.Fx 8.1 .
241d359a62dSAndrey V. Elsukov.Sh AUTHORS
242426b3d04SJulian Elischer.An "Maxim Ignatenko" Aq gelraen.ua@gmail.com .
2436c899950SBaptiste Daroussin.Pp
244426b3d04SJulian ElischerRelative offset code by
245426b3d04SJulian Elischer.An "DMitry Vagin"
246426b3d04SJulian Elischer.Pp
247d359a62dSAndrey V. ElsukovThis manual page was written by
248426b3d04SJulian Elischer.An "Vadim Goncharov" Aq vadimnuclight@tpu.ru .
249d359a62dSAndrey V. Elsukov.Sh BUGS
250426b3d04SJulian ElischerThe node blindly tries to apply every patching operation to each packet
251d359a62dSAndrey V. Elsukov(except those which offset if greater than length of the packet),
252d359a62dSAndrey V. Elsukovso be sure that you supply only the right packets to it (e.g. changing
253d359a62dSAndrey V. Elsukovbytes in the ARP packets meant to be in IP header could corrupt
254d359a62dSAndrey V. Elsukovthem and make your machine unreachable from the network).
255d359a62dSAndrey V. Elsukov.Pp
256d359a62dSAndrey V. Elsukov.Em !!! WARNING !!!
257d359a62dSAndrey V. Elsukov.Pp
258426b3d04SJulian ElischerThe output path of the IP stack assumes correct fields and lengths in the
259426b3d04SJulian Elischerpackets - changing them by to incorrect values can cause
260d359a62dSAndrey V. Elsukovunpredictable results including kernel panics.
261