1 From 14ff975c660696fa636e8d6b58d0abed0ddc72ce Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Thu, 29 Sep 2011 20:29:54 +0200
4 Subject: [PATCH 22/24] MIPS: lantiq: adds udp in-kernel redirect
7 include/linux/udp_redirect.h | 57 +++++++++++++
9 net/ipv4/Makefile | 3 +
10 net/ipv4/udp.c | 28 ++++++-
11 net/ipv4/udp_redirect_symb.c | 186 ++++++++++++++++++++++++++++++++++++++++++
12 5 files changed, 276 insertions(+), 4 deletions(-)
13 create mode 100644 include/linux/udp_redirect.h
14 create mode 100644 net/ipv4/udp_redirect_symb.c
17 +++ b/include/linux/udp_redirect.h
19 +#ifndef _UDP_REDIRECT_H
20 +#define _UDP_REDIRECT_H
22 +/******************************************************************************
25 + Infineon Technologies AG
26 + Am Campeon 1-12; 81726 Munich, Germany
28 + THE DELIVERY OF THIS SOFTWARE AS WELL AS THE HEREBY GRANTED NON-EXCLUSIVE,
29 + WORLDWIDE LICENSE TO USE, COPY, MODIFY, DISTRIBUTE AND SUBLICENSE THIS
30 + SOFTWARE IS FREE OF CHARGE.
32 + THE LICENSED SOFTWARE IS PROVIDED "AS IS" AND INFINEON EXPRESSLY DISCLAIMS
33 + ALL REPRESENTATIONS AND WARRANTIES, WHETHER EXPRESS OR IMPLIED, INCLUDING
34 + WITHOUT LIMITATION, WARRANTIES OR REPRESENTATIONS OF WORKMANSHIP,
35 + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, DURABILITY, THAT THE
36 + OPERATING OF THE LICENSED SOFTWARE WILL BE ERROR FREE OR FREE OF ANY THIRD
37 + PARTY CLAIMS, INCLUDING WITHOUT LIMITATION CLAIMS OF THIRD PARTY INTELLECTUAL
38 + PROPERTY INFRINGEMENT.
40 + EXCEPT FOR ANY LIABILITY DUE TO WILFUL ACTS OR GROSS NEGLIGENCE AND EXCEPT
41 + FOR ANY PERSONAL INJURY INFINEON SHALL IN NO EVENT BE LIABLE FOR ANY CLAIM
42 + OR DAMAGES OF ANY KIND, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
43 + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
44 + DEALINGS IN THE SOFTWARE.
46 +******************************************************************************/
48 +/* ============================= */
50 +/* ============================= */
51 +#ifndef _LINUX_TYPES_H
52 +#include <linux/types.h>
56 +/* ============================= */
58 +/* ============================= */
59 +#define UDP_REDIRECT_MAGIC (void*)0x55445052L
62 +/* ============================= */
63 +/* Global variable declaration */
64 +/* ============================= */
65 +extern int (*udp_do_redirect_fn)(struct sock *sk, struct sk_buff *skb);
66 +extern int (*udpredirect_getfrag_fn)(void *p, char * to,
67 + int offset, int fraglen, int odd,
68 + struct sk_buff *skb);
69 +/* ============================= */
70 +/* Global function declaration */
71 +/* ============================= */
73 +extern int udpredirect_getfrag(void *p, char * to, int offset,
74 + int fraglen, int odd, struct sk_buff *skb);
78 @@ -72,6 +72,12 @@ config INET
82 +config IFX_UDP_REDIRECT
83 + bool "IFX Kernel Packet Interface for UDP redirection"
85 + You can say Y here if you want to use hooks from kernel for
89 source "net/ipv4/Kconfig"
90 source "net/ipv6/Kconfig"
91 --- a/net/ipv4/Makefile
92 +++ b/net/ipv4/Makefile
93 @@ -14,6 +14,9 @@ obj-y := route.o inetpeer.o protocol
94 inet_fragment.o ping.o
96 obj-$(CONFIG_SYSCTL) += sysctl_net_ipv4.o
97 +ifneq ($(CONFIG_IFX_UDP_REDIRECT),)
98 +obj-$(CONFIG_IFX_UDP_REDIRECT) += udp_redirect_symb.o
100 obj-$(CONFIG_PROC_FS) += proc.o
101 obj-$(CONFIG_IP_MULTIPLE_TABLES) += fib_rules.o
102 obj-$(CONFIG_IP_MROUTE) += ipmr.o
106 #include <trace/events/udp.h>
107 #include "udp_impl.h"
109 +#if defined(CONFIG_IFX_UDP_REDIRECT) || defined(CONFIG_IFX_UDP_REDIRECT_MODULE)
110 +#include <linux/udp_redirect.h>
113 struct udp_table udp_table __read_mostly;
114 EXPORT_SYMBOL(udp_table);
116 @@ -803,7 +807,7 @@ int udp_sendmsg(struct kiocb *iocb, stru
118 int err, is_udplite = IS_UDPLITE(sk);
119 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
120 - int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
121 + int (*getfrag)(void *, char *, int, int, int, struct sk_buff *) = NULL;
123 struct ip_options_data opt_copy;
125 @@ -820,7 +824,13 @@ int udp_sendmsg(struct kiocb *iocb, stru
129 - getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
131 +#if defined(CONFIG_IFX_UDP_REDIRECT) || defined(CONFIG_IFX_UDP_REDIRECT_MODULE)
132 + if(udpredirect_getfrag_fn && sk->sk_user_data == UDP_REDIRECT_MAGIC)
133 + getfrag = udpredirect_getfrag_fn;
135 +#endif /* IFX_UDP_REDIRECT */
136 + getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
138 fl4 = &inet->cork.fl.u.ip4;
140 @@ -1621,6 +1631,7 @@ int __udp4_lib_rcv(struct sk_buff *skb,
141 struct rtable *rt = skb_rtable(skb);
143 struct net *net = dev_net(skb->dev);
147 * Validate the packet.
148 @@ -1653,7 +1664,16 @@ int __udp4_lib_rcv(struct sk_buff *skb,
149 sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
152 - int ret = udp_queue_rcv_skb(sk, skb);
154 +#if defined(CONFIG_IFX_UDP_REDIRECT) || defined(CONFIG_IFX_UDP_REDIRECT_MODULE)
155 + if(udp_do_redirect_fn && sk->sk_user_data == UDP_REDIRECT_MAGIC)
157 + udp_do_redirect_fn(sk,skb);
162 + ret = udp_queue_rcv_skb(sk, skb);
165 /* a return value > 0 means to resubmit the input, but
166 @@ -1950,7 +1970,7 @@ struct proto udp_prot = {
167 .clear_sk = sk_prot_clear_portaddr_nulls,
169 EXPORT_SYMBOL(udp_prot);
171 +EXPORT_SYMBOL(udp_rcv);
172 /* ------------------------------------------------------------------------ */
173 #ifdef CONFIG_PROC_FS
176 +++ b/net/ipv4/udp_redirect_symb.c
178 +/******************************************************************************
181 + Infineon Technologies AG
182 + Am Campeon 1-12; 81726 Munich, Germany
184 + THE DELIVERY OF THIS SOFTWARE AS WELL AS THE HEREBY GRANTED NON-EXCLUSIVE,
185 + WORLDWIDE LICENSE TO USE, COPY, MODIFY, DISTRIBUTE AND SUBLICENSE THIS
186 + SOFTWARE IS FREE OF CHARGE.
188 + THE LICENSED SOFTWARE IS PROVIDED "AS IS" AND INFINEON EXPRESSLY DISCLAIMS
189 + ALL REPRESENTATIONS AND WARRANTIES, WHETHER EXPRESS OR IMPLIED, INCLUDING
190 + WITHOUT LIMITATION, WARRANTIES OR REPRESENTATIONS OF WORKMANSHIP,
191 + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, DURABILITY, THAT THE
192 + OPERATING OF THE LICENSED SOFTWARE WILL BE ERROR FREE OR FREE OF ANY THIRD
193 + PARTY CLAIMS, INCLUDING WITHOUT LIMITATION CLAIMS OF THIRD PARTY INTELLECTUAL
194 + PROPERTY INFRINGEMENT.
196 + EXCEPT FOR ANY LIABILITY DUE TO WILFUL ACTS OR GROSS NEGLIGENCE AND EXCEPT
197 + FOR ANY PERSONAL INJURY INFINEON SHALL IN NO EVENT BE LIABLE FOR ANY CLAIM
198 + OR DAMAGES OF ANY KIND, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
199 + ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
200 + DEALINGS IN THE SOFTWARE.
202 +******************************************************************************/
203 +#if defined(CONFIG_IFX_UDP_REDIRECT) || defined(CONFIG_IFX_UDP_REDIRECT_MODULE)
204 +/* ============================= */
206 +/* ============================= */
207 +#include <net/checksum.h>
208 +#include <net/udp.h>
209 +#include <linux/module.h>
210 +#include <linux/skbuff.h>
211 +#include <linux/udp_redirect.h>
213 +/* ============================= */
214 +/* Global variable definition */
215 +/* ============================= */
216 +int (*udpredirect_getfrag_fn) (void *p, char * to, int offset,
217 + int fraglen, int odd, struct sk_buff *skb) = NULL;
218 +int (*udp_do_redirect_fn)(struct sock *sk, struct sk_buff *skb) = NULL;
220 +/* ============================= */
221 +/* Local type definitions */
222 +/* ============================= */
232 +/* ============================= */
233 +/* Local function declaration */
234 +/* ============================= */
235 +static int udpredirect_csum_partial_copy_fromiovecend(unsigned char *kdata,
236 + struct iovec *iov, int offset, unsigned int len, __wsum *csump);
238 +static int udpredirect_memcpy_fromiovecend(unsigned char *kdata, struct iovec *iov, int offset,
241 +/* ============================= */
242 +/* Global function definition */
243 +/* ============================= */
246 + Copy of udp_getfrag() from udp.c
247 + This function exists because no copy_from_user() is needed for udpredirect.
251 +udpredirect_getfrag(void *from, char *to, int offset, int len, int odd, struct sk_buff *skb)
253 + struct iovec *iov = from;
255 + if (skb->ip_summed == CHECKSUM_PARTIAL) {
256 + if (udpredirect_memcpy_fromiovecend(to, iov, offset, len) < 0)
260 + if (udpredirect_csum_partial_copy_fromiovecend(to, iov, offset, len, &csum) < 0)
262 + skb->csum = csum_block_add(skb->csum, csum, odd);
267 +static int udpredirect_memcpy_fromiovecend(unsigned char *kdata, struct iovec *iov, int offset,
270 + /* Skip over the finished iovecs */
271 + while (offset >= iov->iov_len) {
272 + offset -= iov->iov_len;
277 + u8 __user *base = iov->iov_base + offset;
278 + int copy = min_t(unsigned int, len, iov->iov_len - offset);
281 + memcpy(kdata, base, copy);
291 + Copy of csum_partial_copy_fromiovecend() from iovec.c
292 + This function exists because no copy_from_user() is needed for udpredirect.
295 +int udpredirect_csum_partial_copy_fromiovecend(unsigned char *kdata, struct iovec *iov,
296 + int offset, unsigned int len, __wsum *csump)
298 + __wsum csum = *csump;
299 + int partial_cnt = 0, err = 0;
301 + /* Skip over the finished iovecs */
302 + while (offset >= iov->iov_len) {
303 + offset -= iov->iov_len;
308 + u8 __user *base = iov->iov_base + offset;
309 + int copy = min_t(unsigned int, len, iov->iov_len - offset);
313 + /* There is a remnant from previous iov. */
315 + int par_len = 4 - partial_cnt;
317 + /* iov component is too short ... */
318 + if (par_len > copy) {
319 + memcpy(kdata, base, copy);
322 + partial_cnt += copy;
327 + *csump = csum_partial(kdata - partial_cnt,
328 + partial_cnt, csum);
331 + memcpy(kdata, base, par_len);
332 + csum = csum_partial(kdata - partial_cnt, 4, csum);
341 + partial_cnt = copy % 4;
343 + copy -= partial_cnt;
344 + memcpy(kdata + copy, base + copy, partial_cnt);
349 + csum = csum_partial_copy_nocheck(base, kdata, copy, csum);
351 + len -= copy + partial_cnt;
352 + kdata += copy + partial_cnt;
360 +EXPORT_SYMBOL(udpredirect_getfrag);
361 +EXPORT_SYMBOL(udp_do_redirect_fn);
362 +EXPORT_SYMBOL(udpredirect_getfrag_fn);
363 +#endif /* CONFIG_IFX_UDP_REDIRECT* */