hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* ip_fragment.c
 *
 * Copyright (C) 2002      Ulrich Marx <marx@kammer.uni-hannover.de>
 *               2003      Mathias Koehrer <mathias_koehrer@yahoo.de>
 *               2003-2005 Jan Kiszka <jan.kiszka@web.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
#include <linux/module.h>
#include <net/checksum.h>
#include <net/ip.h>
 
#include <rtdev.h>
#include <rtnet_internal.h>
#include <rtnet_socket.h>
 
#include <linux/ip.h>
#include <linux/in.h>
 
#include <ipv4/ip_fragment.h>
 
#if IS_ENABLED(CONFIG_XENO_DRIVERS_NET_ADDON_PROXY)
#include <ipv4/ip_input.h>
#endif /* CONFIG_XENO_DRIVERS_NET_ADDON_PROXY */
 
/*
 * This defined sets the number of incoming fragmented IP messages that
 * can be handled in parallel.
 */
#define COLLECTOR_COUNT 10
 
struct ip_collector {
   int in_use;
   __u32 saddr;
   __u32 daddr;
   __u16 id;
   __u8 protocol;
 
   struct rtskb_queue frags;
   struct rtsocket *sock;
   unsigned int buf_size;
};
 
static struct ip_collector collector[COLLECTOR_COUNT];
 
static void alloc_collector(struct rtskb *skb, struct rtsocket *sock)
{
   int i;
   rtdm_lockctx_t context;
   struct ip_collector *p_coll;
   struct iphdr *iph = skb->nh.iph;
 
   /*
     * Find a free collector
     *
     * Note: We once used to clean up probably outdated chains, but the
     * algorithm was not stable enough and could cause incorrect drops even
     * under medium load. If we run in overload, we will loose data anyhow.
     * What we should do in the future is to account collectors per socket or
     * socket owner and set quotations.
     * Garbage collection is now performed only on socket close.
     */
   for (i = 0; i < COLLECTOR_COUNT; i++) {
       p_coll = &collector[i];
       rtdm_lock_get_irqsave(&p_coll->frags.lock, context);
 
       if (!p_coll->in_use) {
           p_coll->in_use = 1;
           p_coll->buf_size = skb->len;
           p_coll->frags.first = skb;
           p_coll->frags.last = skb;
           p_coll->saddr = iph->saddr;
           p_coll->daddr = iph->daddr;
           p_coll->id = iph->id;
           p_coll->protocol = iph->protocol;
           p_coll->sock = sock;
 
           rtdm_lock_put_irqrestore(&p_coll->frags.lock, context);
 
           return;
       }
 
       rtdm_lock_put_irqrestore(&p_coll->frags.lock, context);
   }
 
   rtdm_printk("RTnet: IP fragmentation - no collector available\n");
   kfree_rtskb(skb);
}
 
/*
 * Return a pointer to the collector that holds the message which
 * fits to the iphdr of the passed rtskb.
 * */
static struct rtskb *add_to_collector(struct rtskb *skb, unsigned int offset,
                     int more_frags)
{
   int i, err;
   rtdm_lockctx_t context;
   struct ip_collector *p_coll;
   struct iphdr *iph = skb->nh.iph;
   struct rtskb *first_skb;
 
   /* Search in existing collectors */
   for (i = 0; i < COLLECTOR_COUNT; i++) {
       p_coll = &collector[i];
       rtdm_lock_get_irqsave(&p_coll->frags.lock, context);
 
       if (p_coll->in_use && (iph->saddr == p_coll->saddr) &&
           (iph->daddr == p_coll->daddr) && (iph->id == p_coll->id) &&
           (iph->protocol == p_coll->protocol)) {
           first_skb = p_coll->frags.first;
 
           /* Acquire the rtskb at the expense of the protocol pool */
           if (rtskb_acquire(skb, &p_coll->sock->skb_pool) != 0) {
               /* We have to drop this fragment => clean up the whole chain */
               p_coll->in_use = 0;
 
               rtdm_lock_put_irqrestore(&p_coll->frags.lock,
                            context);
 
#ifdef FRAG_DBG
               rtdm_printk(
                   "RTnet: Compensation pool empty - IP fragments "
                   "dropped (saddr:%x, daddr:%x)\n",
                   iph->saddr, iph->daddr);
#endif
 
               kfree_rtskb(first_skb);
               kfree_rtskb(skb);
               return NULL;
           }
 
           /* Optimized version of __rtskb_queue_tail */
           skb->next = NULL;
           p_coll->frags.last->next = skb;
           p_coll->frags.last = skb;
 
           /* Extend the chain */
           first_skb->chain_end = skb;
 
           /* Sanity check: unordered fragments are not allowed! */
           if (offset != p_coll->buf_size) {
               /* We have to drop this fragment => clean up the whole chain */
               p_coll->in_use = 0;
               skb = first_skb;
 
               rtdm_lock_put_irqrestore(&p_coll->frags.lock,
                            context);
               break; /* leave the for loop */
           }
 
           p_coll->buf_size += skb->len;
 
           if (!more_frags) {
               p_coll->in_use = 0;
 
               err = rt_socket_reference(p_coll->sock);
 
               rtdm_lock_put_irqrestore(&p_coll->frags.lock,
                            context);
 
               if (err < 0) {
                   kfree_rtskb(first_skb);
                   return NULL;
               }
 
               return first_skb;
           } else {
               rtdm_lock_put_irqrestore(&p_coll->frags.lock,
                            context);
               return NULL;
           }
       }
 
       rtdm_lock_put_irqrestore(&p_coll->frags.lock, context);
   }
 
#if IS_ENABLED(CONFIG_XENO_DRIVERS_NET_ADDON_PROXY)
   if (rt_ip_fallback_handler) {
       __rtskb_push(skb, iph->ihl * 4);
       rt_ip_fallback_handler(skb);
       return NULL;
   }
#endif
 
#ifdef FRAG_DBG
   rtdm_printk("RTnet: Unordered IP fragment (saddr:%x, daddr:%x)"
           " - dropped\n",
           iph->saddr, iph->daddr);
#endif
 
   kfree_rtskb(skb);
   return NULL;
}
 
/*
 * Cleans up all collectors referring to the specified socket.
 * This is now the only kind of garbage collection we do.
 */
void rt_ip_frag_invalidate_socket(struct rtsocket *sock)
{
   int i;
   rtdm_lockctx_t context;
   struct ip_collector *p_coll;
 
   for (i = 0; i < COLLECTOR_COUNT; i++) {
       p_coll = &collector[i];
       rtdm_lock_get_irqsave(&p_coll->frags.lock, context);
 
       if ((p_coll->in_use) && (p_coll->sock == sock)) {
           p_coll->in_use = 0;
           kfree_rtskb(p_coll->frags.first);
       }
 
       rtdm_lock_put_irqrestore(&p_coll->frags.lock, context);
   }
}
EXPORT_SYMBOL_GPL(rt_ip_frag_invalidate_socket);
 
/*
 * Cleans up all existing collectors
 */
static void cleanup_all_collectors(void)
{
   int i;
   rtdm_lockctx_t context;
   struct ip_collector *p_coll;
 
   for (i = 0; i < COLLECTOR_COUNT; i++) {
       p_coll = &collector[i];
       rtdm_lock_get_irqsave(&p_coll->frags.lock, context);
 
       if (p_coll->in_use) {
           p_coll->in_use = 0;
           kfree_rtskb(p_coll->frags.first);
       }
 
       rtdm_lock_put_irqrestore(&p_coll->frags.lock, context);
   }
}
 
/*
 * This function returns an rtskb that contains the complete, accumulated IP message.
 * If not all fragments of the IP message have been received yet, it returns NULL
 * Note: the IP header must have already been pulled from the rtskb!
 * */
struct rtskb *rt_ip_defrag(struct rtskb *skb, struct rtinet_protocol *ipprot)
{
   unsigned int more_frags;
   unsigned int offset;
   struct rtsocket *sock;
   struct iphdr *iph = skb->nh.iph;
   int ret;
 
   /* Parse the IP header */
   offset = ntohs(iph->frag_off);
   more_frags = offset & IP_MF;
   offset &= IP_OFFSET;
   offset <<= 3; /* offset is in 8-byte chunks */
 
   /* First fragment? */
   if (offset == 0) {
       /* Get the destination socket */
       if ((sock = ipprot->dest_socket(skb)) == NULL) {
#if IS_ENABLED(CONFIG_XENO_DRIVERS_NET_ADDON_PROXY)
           if (rt_ip_fallback_handler) {
               __rtskb_push(skb, iph->ihl * 4);
               rt_ip_fallback_handler(skb);
               return NULL;
           }
#endif
           /* Drop the rtskb */
           kfree_rtskb(skb);
           return NULL;
       }
 
       /* Acquire the rtskb, to unlock the device skb pool */
       ret = rtskb_acquire(skb, &sock->skb_pool);
 
       if (ret != 0) {
           /* Drop the rtskb */
           kfree_rtskb(skb);
       } else {
           /* Allocates a new collector */
           alloc_collector(skb, sock);
       }
 
       /* Packet is queued or freed, socket can be released */
       rt_socket_dereference(sock);
 
       return NULL;
   } else {
       /* Add to an existing collector */
       return add_to_collector(skb, offset, more_frags);
   }
}
 
int __init rt_ip_fragment_init(void)
{
   int i;
 
   /* Probably not needed (static variable...) */
   memset(collector, 0, sizeof(collector));
 
   for (i = 0; i < COLLECTOR_COUNT; i++)
       rtdm_lock_init(&collector[i].frags.lock);
 
   return 0;
}
 
void rt_ip_fragment_cleanup(void)
{
   cleanup_all_collectors();
}